Skip to content

Prod sync #311

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/pr-pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install beautifulsoup4 web3
pip install beautifulsoup4 web3 bech32
- name: Run check script
run: ./check.sh >> $GITHUB_STEP_SUMMARY
20 changes: 20 additions & 0 deletions chain/cardano/tokens.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[
{
"address": "6ac8ef33b510ec004fe11585f7c5a9f0c07f0c23428ab4f29c1d7d10-4d454c44",
"decimals": 6,
"displaySymbol": "MELD",
"logo": "https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/cardano/assets/asset1zvn33mj5kzgxtct7jr5qjyefu9ewk22xp0s0yw/logo.png",
"name": "MELD",
"symbol": "MELD.ADA",
"website": "https://meld.com/"
},
{
"address": "279c909f348e533da5808898f87f9a14bb2c3dfbbacccd631d927a3f-534e454b",
"decimals": 0,
"displaySymbol": "SNEK",
"logo": "https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/cardano/assets/asset108xu02ckwrfc8qs9d97mgyh4kn8gdu9w8f5sxk/logo.png",
"name": "Snek",
"symbol": "SNEK.ADA",
"website": "https://www.snek.com"
}
]
6 changes: 6 additions & 0 deletions chain/list.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,11 @@
"displayName": "TRON",
"native": "TRX",
"tokens": "chain/tron/tokens.json"
},
{
"chain": "cardano",
"displayName": "Cardano",
"native": "ADA",
"tokens": "chain/cardano/tokens.json"
}
]
2 changes: 1 addition & 1 deletion coins.json
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@
"name": "Polygon",
"key": "polygon",
"decimals": 18,
"logo": "https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/polygon/info/logo.png",
"logo": "https://raw.githubusercontent.com/blockchain/coin-definitions/master/extensions/blockchains/polygon/info/logo.png",
"website": "https://polygon.technology/solutions/polygon-pos/"
},
{
Expand Down
Empty file.
Binary file added extensions/blockchains/polygon/info/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6,573 changes: 2,993 additions & 3,580 deletions extensions/prices.json

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion scripts/build-lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from statics import BLOCKCHAINS, EXT_BLOCKCHAINS_DENYLIST, EXT_BLOCKCHAINS, EXT_PRICES, FINAL_BLOCKCHAINS_LIST, \
NETWORKS, EXT_OVERRIDES

from utils import filter_cardano_tokens_by_price

def read_json(path, comment_marker=None):
with open(path) as json_file:
Expand Down Expand Up @@ -197,7 +198,10 @@ def build_tokens_list(network, fill_from_coingecko=False, ci=False):
print(f"Tokens before price filter {len(tokens)}")

# Clean up by price:
tokens = list(filter(lambda token: (token.address + "." + network.symbol) in prices['prices'], tokens))
if network.symbol.lower() == 'ada':
tokens = filter_cardano_tokens_by_price(tokens, prices)
else:
tokens = list(filter(lambda token: (token.address + "." + network.symbol) in prices['prices'], tokens))

print(f"Tokens after price filter {len(tokens)}")

Expand Down
9 changes: 7 additions & 2 deletions scripts/coin_gecko.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from web3 import Web3

from common_classes import build_dataclass_from_dict, Description, Token
from utils import map_chunked
from utils import map_chunked, get_cardano_tokens_by_id

BATCH_SIZE = 250

Expand Down Expand Up @@ -94,6 +94,7 @@
"SOL": "solana",
"TON": "the-open-network",
"TRX": "tron",
"ADA": "cardano"
}


Expand Down Expand Up @@ -284,15 +285,19 @@ def get_coins_by_id(coins):
coins_by_id.setdefault(coin_gecko_id, []).append(coin)
return coins_by_id


def get_tokens_by_id(network, tokens):
tokens_by_id = {}
network_coin_gecko_id = network_mappings.get(network.symbol)

if network_coin_gecko_id is not None:
if network.symbol.lower() == 'ada':
coin_list = coin_list_by_platform_and_address.get(network_coin_gecko_id, {})
return get_cardano_tokens_by_id(tokens, coin_list)
for token in tokens:
coin = coin_list_by_platform_and_address.get(network_coin_gecko_id, {}).get(token.address.lower(), None)
if coin is not None:
tokens_by_id.setdefault(coin.id, []).append(token)

return tokens_by_id


Expand Down
6 changes: 6 additions & 0 deletions scripts/statics.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,11 @@ class Network:
symbol="TON",
output_file="chain/ton/tokens.json",
explorer_url="https://tonscan.org/jetton/"
),
Network(
chain="cardano",
symbol="ADA",
output_file="chain/cardano/tokens.json",
explorer_url="https://cardanoscan.com/token/"
)
]
85 changes: 85 additions & 0 deletions scripts/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import json
import sys
from typing import List, Any, Dict, Tuple, TypeVar, Callable, Generator
import bech32
import hashlib

T = TypeVar('T')
R = TypeVar('R')
Expand All @@ -27,3 +29,86 @@ def multiread_json(base_dir: str, pattern: str) -> Generator[Tuple[str, Dict[str
for target in sorted(glob.glob(base_dir + pattern)):
key = target.replace(base_dir, '').partition("/")[0]
yield key, read_json(target)

def encode_cardano_fingerprint(policy_id: str, asset_name_hex: str) -> str:
policy_id_bytes = bytes.fromhex(policy_id)
asset_name_bytes = bytes.fromhex(asset_name_hex)
asset_id_bytes = policy_id_bytes + asset_name_bytes

fingerprint_bytes = hashlib.blake2b(asset_id_bytes, digest_size=20).digest()
data = bech32.convertbits(fingerprint_bytes, 8, 5)
fingerprint = bech32.bech32_encode('asset', data)

return fingerprint

def filter_cardano_tokens_by_price(tokens, prices):
fingerprint_map = {}
for price_key in prices['prices']:
if not price_key.endswith(".ADA"):
continue
token_id = price_key.split(".")[0]
if "-" not in token_id:
continue
policy_id, asset_name_hex = token_id.split("-")
fingerprint = encode_cardano_fingerprint(policy_id, asset_name_hex)
fingerprint_map[fingerprint] = token_id

filtered_tokens = []
for token in tokens:
if token.address in fingerprint_map:
token.address = fingerprint_map[token.address]
filtered_tokens.append(token)

return filtered_tokens

def get_cardano_tokens_by_id(tokens, coin_list):
# here we handle different cases when contract address in coingecko is sometimes considered as:
# 1- the asset_id (policy_id + asset_name_hex).
# 2- the policy_id and missing the asset_name_hex
# 3- the fingerprint (in hex or readable)

tokens_by_id = {}
fingerprint_to_coin = {}

for asset_id, coin in coin_list.items():
# scenario-1: as the fingerprint(hex or readable)
if len(asset_id) < 56:
try:
hrp, data = bech32.bech32_decode(asset_id)
if hrp == "asset":
fingerprint_to_coin[asset_id] = (coin, asset_id)
continue
except Exception:
pass

try:
fingerprint_bytes = bytes.fromhex(asset_id)
fingerprint_words = bech32.convertbits(fingerprint_bytes, 8, 5)
fingerprint = bech32.bech32_encode("asset", fingerprint_words)
fingerprint_to_coin[fingerprint] = (coin, fingerprint)
continue
except Exception:
continue # Skip invalid entries

# scenario-2: as the policy_id
elif len(asset_id) == 56:
policy_id = asset_id
asset_name_hex = coin.symbol.upper().encode("utf-8").hex()

# scenario-3: as the asset_id (polic_id + asset_name_hex)
else:
policy_id = asset_id[:56]
asset_name_hex = asset_id[56:]

fingerprint = encode_cardano_fingerprint(policy_id, asset_name_hex)
asset_id = f"{policy_id}-{asset_name_hex}"
fingerprint_to_coin[fingerprint] = (coin, asset_id)

for token in tokens:
coin_to_asset = fingerprint_to_coin.get(token.address)
if coin_to_asset:
coin, asset_id = coin_to_asset
token.address = asset_id
tokens_by_id.setdefault(coin.id, []).append(token)

return tokens_by_id