Skip to content

Commit 6dedca5

Browse files
author
abel
committed
(fix) Added extra logic to allow the tokens initialization to accept tokens with repeated symbol but different denom as different tokens
1 parent 3951c1a commit 6dedca5

File tree

5 files changed

+96
-87
lines changed

5 files changed

+96
-87
lines changed

pyinjective/async_client.py

Lines changed: 44 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,32 +1067,22 @@ async def _initialize_tokens_and_markets(self):
10671067

10681068
for market_info in markets_info:
10691069
base_token_symbol, quote_token_symbol = market_info.ticker.split(constant.TICKER_TOKENS_SEPARATOR)
1070-
base_token = tokens.get(base_token_symbol, None)
1071-
if base_token is None:
1072-
base_token = Token(
1073-
name=market_info.base_token_meta.name,
1074-
symbol=base_token_symbol,
1075-
denom=market_info.base_denom,
1076-
address=market_info.base_token_meta.address,
1077-
decimals=market_info.base_token_meta.decimals,
1078-
logo=market_info.base_token_meta.logo,
1079-
updated=market_info.base_token_meta.updated_at,
1080-
)
1081-
tokens[base_token_symbol] = base_token
1070+
base_token = self._token_representation(
1071+
symbol=base_token_symbol,
1072+
token_meta=market_info.base_token_meta,
1073+
denom=market_info.base_denom,
1074+
all_tokens=tokens,
1075+
)
1076+
if base_token.denom not in tokens_by_denom:
10821077
tokens_by_denom[base_token.denom] = base_token
10831078

1084-
quote_token = tokens.get(quote_token_symbol, None)
1085-
if quote_token is None:
1086-
quote_token = Token(
1087-
name=market_info.quote_token_meta.name,
1088-
symbol=quote_token_symbol,
1089-
denom=market_info.quote_denom,
1090-
address=market_info.quote_token_meta.address,
1091-
decimals=market_info.quote_token_meta.decimals,
1092-
logo=market_info.quote_token_meta.logo,
1093-
updated=market_info.quote_token_meta.updated_at,
1094-
)
1095-
tokens[quote_token_symbol] = quote_token
1079+
quote_token = self._token_representation(
1080+
symbol=quote_token_symbol,
1081+
token_meta=market_info.quote_token_meta,
1082+
denom=market_info.quote_denom,
1083+
all_tokens=tokens,
1084+
)
1085+
if quote_token.denom not in tokens_by_denom:
10961086
tokens_by_denom[quote_token.denom] = quote_token
10971087

10981088
market = SpotMarket(
@@ -1114,18 +1104,13 @@ async def _initialize_tokens_and_markets(self):
11141104
for market_info in markets_info:
11151105
quote_token_symbol = market_info.quote_token_meta.symbol
11161106

1117-
quote_token = tokens.get(quote_token_symbol, None)
1118-
if quote_token is None:
1119-
quote_token = Token(
1120-
name=market_info.quote_token_meta.name,
1121-
symbol=quote_token_symbol,
1122-
denom=market_info.quote_denom,
1123-
address=market_info.quote_token_meta.address,
1124-
decimals=market_info.quote_token_meta.decimals,
1125-
logo=market_info.quote_token_meta.logo,
1126-
updated=market_info.quote_token_meta.updated_at,
1127-
)
1128-
tokens[quote_token_symbol] = quote_token
1107+
quote_token = self._token_representation(
1108+
symbol=quote_token_symbol,
1109+
token_meta=market_info.quote_token_meta,
1110+
denom=market_info.quote_denom,
1111+
all_tokens=tokens,
1112+
)
1113+
if quote_token.denom not in tokens_by_denom:
11291114
tokens_by_denom[quote_token.denom] = quote_token
11301115

11311116
market = DerivativeMarket(
@@ -1176,3 +1161,26 @@ async def _initialize_tokens_and_markets(self):
11761161
self._spot_markets = spot_markets
11771162
self._derivative_markets = derivative_markets
11781163
self._binary_option_markets = binary_option_markets
1164+
1165+
def _token_representation(self, symbol: str, token_meta, denom: str, all_tokens: Dict[str, Token]) -> Token:
1166+
token = Token(
1167+
name=token_meta.name,
1168+
symbol=symbol,
1169+
denom=denom,
1170+
address=token_meta.address,
1171+
decimals=token_meta.decimals,
1172+
logo=token_meta.logo,
1173+
updated=token_meta.updated_at,
1174+
)
1175+
1176+
existing_token = all_tokens.get(token.symbol, None)
1177+
if existing_token is None:
1178+
all_tokens[token.symbol] = token
1179+
existing_token = token
1180+
elif existing_token.denom != denom:
1181+
existing_token = all_tokens.get(token.name, None)
1182+
if existing_token is None:
1183+
all_tokens[token.name] = token
1184+
existing_token = token
1185+
1186+
return existing_token

pyinjective/denoms_testnet.ini

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ min_display_quantity_tick_size = 1e-13
3636

3737
[0x1c315bd2cfcc769a8d8eca49ce7b1bc5fb0353bfcb9fa82895fe0c1c2a62306e]
3838
description = 'Testnet Spot WBTC/USDT'
39-
base = 6
39+
base = 8
4040
quote = 6
4141
min_price_tick_size = 0.00001
4242
min_display_price_tick_size = 0.001
@@ -56,25 +56,7 @@ min_display_quantity_tick_size = 0.1
5656
description = 'Testnet Derivative INJ/USDT PERP'
5757
base = 0
5858
quote = 6
59-
min_price_tick_size = 100000
60-
min_display_price_tick_size = 0.1
61-
min_quantity_tick_size = 0.0001
62-
min_display_quantity_tick_size = 0.0001
63-
64-
[0xd5e4b12b19ecf176e4e14b42944731c27677819d2ed93be4104ad7025529c7ff]
65-
description = 'Testnet Derivative ETH/USDT PERP'
66-
base = 0
67-
quote = 6
68-
min_price_tick_size = 100000
69-
min_display_price_tick_size = 0.1
70-
min_quantity_tick_size = 0.0001
71-
min_display_quantity_tick_size = 0.0001
72-
73-
[0x90e662193fa29a3a7e6c07be4407c94833e762d9ee82136a2cc712d6b87d7de3]
74-
description = 'Testnet Derivative BTC/USDT PERP'
75-
base = 0
76-
quote = 6
77-
min_price_tick_size = 100000
59+
min_price_tick_size = 100
7860
min_display_price_tick_size = 0.1
7961
min_quantity_tick_size = 0.0001
8062
min_display_quantity_tick_size = 0.0001

pyinjective/utils/metadata_validation.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
def find_metadata_inconsistencies(network: Network) -> Tuple[List[Any]]:
1212
client = AsyncClient(network, insecure=False)
13+
ini_config = constant.CONFIGS[network.string()]
1314

1415
spot_markets = asyncio.get_event_loop().run_until_complete(client.all_spot_markets())
1516
derivative_markets = asyncio.get_event_loop().run_until_complete(client.all_derivative_markets())
@@ -21,7 +22,7 @@ def find_metadata_inconsistencies(network: Network) -> Tuple[List[Any]]:
2122
peggy_denoms_not_found = []
2223
peggy_denoms_with_diffs = []
2324

24-
for config_key in constant.mainnet_config:
25+
for config_key in ini_config:
2526
if config_key.startswith("0x"):
2627
denom = constant.Denom.load_market(network=network.string(), market_id=config_key)
2728
if config_key in spot_markets:

tests/rpc_fixtures/markets_fixtures.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,21 @@ def usdt_token_meta():
4646

4747
return token
4848

49+
@pytest.fixture
50+
def usdt_token_meta_second_denom():
51+
from pyinjective.proto.exchange.injective_spot_exchange_rpc_pb2 import TokenMeta
52+
53+
token = TokenMeta(
54+
name="USDT Second Denom",
55+
address="0x0000000000000000000000000000000000000000",
56+
symbol="USDT",
57+
logo="https://static.alchemyapi.io/images/assets/826.png",
58+
decimals=6,
59+
updated_at=1691739137645
60+
)
61+
62+
return token
63+
4964

5065
@pytest.fixture
5166
def usdt_perp_token_meta():
@@ -64,7 +79,7 @@ def usdt_perp_token_meta():
6479

6580

6681
@pytest.fixture
67-
def ape_usdt_spot_market(ape_token_meta, usdt_token_meta):
82+
def ape_usdt_spot_market_meta(ape_token_meta, usdt_token_meta_second_denom):
6883
from pyinjective.proto.exchange.injective_spot_exchange_rpc_pb2 import SpotMarketInfo
6984

7085
market = SpotMarketInfo(
@@ -73,8 +88,8 @@ def ape_usdt_spot_market(ape_token_meta, usdt_token_meta):
7388
ticker="APE/USDT",
7489
base_denom="peggy0x44C21afAaF20c270EBbF5914Cfc3b5022173FEB7",
7590
base_token_meta=ape_token_meta,
76-
quote_denom="peggy0x87aB3B4C8661e07D6372361211B96ed4Dc36B1B5",
77-
quote_token_meta=usdt_token_meta,
91+
quote_denom="factory/peggy0x87aB3B4C8661e07D6372361211B96ed4Dc300000",
92+
quote_token_meta=usdt_token_meta_second_denom,
7893
maker_fee_rate="-0.0001",
7994
taker_fee_rate="0.001",
8095
service_provider_fee="0.4",
@@ -85,7 +100,7 @@ def ape_usdt_spot_market(ape_token_meta, usdt_token_meta):
85100
return market
86101

87102
@pytest.fixture
88-
def inj_usdt_spot_market(inj_token_meta, usdt_token_meta):
103+
def inj_usdt_spot_market_meta(inj_token_meta, usdt_token_meta):
89104
from pyinjective.proto.exchange.injective_spot_exchange_rpc_pb2 import SpotMarketInfo
90105

91106
market = SpotMarketInfo(
@@ -106,7 +121,7 @@ def inj_usdt_spot_market(inj_token_meta, usdt_token_meta):
106121
return market
107122

108123
@pytest.fixture
109-
def btc_usdt_perp_market(usdt_perp_token_meta):
124+
def btc_usdt_perp_market_meta(usdt_perp_token_meta):
110125
from pyinjective.proto.exchange.injective_derivative_exchange_rpc_pb2 import (
111126
DerivativeMarketInfo,
112127
PerpetualMarketInfo,
@@ -150,7 +165,7 @@ def btc_usdt_perp_market(usdt_perp_token_meta):
150165
return market
151166

152167
@pytest.fixture
153-
def first_match_bet_market(inj_usdt_spot_market):
168+
def first_match_bet_market_meta(inj_usdt_spot_market_meta):
154169
from pyinjective.proto.exchange.injective_derivative_exchange_rpc_pb2 import BinaryOptionsMarketInfo
155170

156171
market = BinaryOptionsMarketInfo(
@@ -163,7 +178,7 @@ def first_match_bet_market(inj_usdt_spot_market):
163178
oracle_scale_factor=6,
164179
expiration_timestamp=1707800399,
165180
settlement_timestamp=1707843599,
166-
quote_denom=inj_usdt_spot_market.quote_denom,
181+
quote_denom=inj_usdt_spot_market_meta.quote_denom,
167182
maker_fee_rate="0",
168183
taker_fee_rate="0",
169184
service_provider_fee="0.4",

tests/test_async_client.py

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@
1212
inj_token_meta,
1313
ape_token_meta,
1414
usdt_token_meta,
15+
usdt_token_meta_second_denom,
1516
usdt_perp_token_meta,
16-
inj_usdt_spot_market,
17-
ape_usdt_spot_market,
18-
btc_usdt_perp_market,
19-
first_match_bet_market,
17+
inj_usdt_spot_market_meta,
18+
ape_usdt_spot_market_meta,
19+
btc_usdt_perp_market_meta,
20+
first_match_bet_market_meta,
2021
)
2122
from tests.rpc_fixtures.configurable_servicers import ConfigurableInjectiveDerivativeExchangeRPCServicer, \
2223
ConfigurableInjectiveSpotExchangeRPCServicer
@@ -114,20 +115,20 @@ async def test_initialize_tokens_and_markets(
114115
self,
115116
spot_servicer,
116117
derivative_servicer,
117-
inj_usdt_spot_market,
118-
ape_usdt_spot_market,
119-
btc_usdt_perp_market,
120-
first_match_bet_market,
118+
inj_usdt_spot_market_meta,
119+
ape_usdt_spot_market_meta,
120+
btc_usdt_perp_market_meta,
121+
first_match_bet_market_meta,
121122
):
122123
spot_servicer.markets_queue.append(injective_spot_exchange_rpc_pb2.MarketsResponse(
123-
markets=[inj_usdt_spot_market, ape_usdt_spot_market]
124+
markets=[inj_usdt_spot_market_meta, ape_usdt_spot_market_meta]
124125
))
125126
derivative_servicer.markets_queue.append(injective_derivative_exchange_rpc_pb2.MarketsResponse(
126-
markets=[btc_usdt_perp_market]
127+
markets=[btc_usdt_perp_market_meta]
127128
))
128129
derivative_servicer.binary_option_markets_queue.append(
129130
injective_derivative_exchange_rpc_pb2.BinaryOptionsMarketsResponse(
130-
markets=[first_match_bet_market]
131+
markets=[first_match_bet_market_meta]
131132
))
132133

133134
client = AsyncClient(
@@ -141,24 +142,26 @@ async def test_initialize_tokens_and_markets(
141142
await client._initialize_tokens_and_markets()
142143

143144
all_tokens = await client.all_tokens()
144-
assert(4 == len(all_tokens))
145-
inj_symbol, usdt_symbol = inj_usdt_spot_market.ticker.split("/")
146-
ape_symbol, _ = ape_usdt_spot_market.ticker.split("/")
147-
usdt_perp_symbol = btc_usdt_perp_market.quote_token_meta.symbol
148-
assert(any((inj_symbol == token.symbol for _, token in all_tokens.items())))
149-
assert (any((usdt_symbol == token.symbol for _, token in all_tokens.items())))
150-
assert (any((ape_symbol == token.symbol for _, token in all_tokens.items())))
151-
assert (any((usdt_perp_symbol == token.symbol for _, token in all_tokens.items())))
145+
assert(5 == len(all_tokens))
146+
inj_symbol, usdt_symbol = inj_usdt_spot_market_meta.ticker.split("/")
147+
ape_symbol, _ = ape_usdt_spot_market_meta.ticker.split("/")
148+
alternative_usdt_name = ape_usdt_spot_market_meta.quote_token_meta.name
149+
usdt_perp_symbol = btc_usdt_perp_market_meta.quote_token_meta.symbol
150+
assert(inj_symbol in all_tokens)
151+
assert(usdt_symbol in all_tokens)
152+
assert(alternative_usdt_name in all_tokens)
153+
assert(ape_symbol in all_tokens)
154+
assert(usdt_perp_symbol in all_tokens)
152155

153156
all_spot_markets = await client.all_spot_markets()
154157
assert (2 == len(all_spot_markets))
155-
assert (any((inj_usdt_spot_market.market_id == market.id for market in all_spot_markets.values())))
156-
assert (any((ape_usdt_spot_market.market_id == market.id for market in all_spot_markets.values())))
158+
assert (any((inj_usdt_spot_market_meta.market_id == market.id for market in all_spot_markets.values())))
159+
assert (any((ape_usdt_spot_market_meta.market_id == market.id for market in all_spot_markets.values())))
157160

158161
all_derivative_markets = await client.all_derivative_markets()
159162
assert (1 == len(all_derivative_markets))
160-
assert (any((btc_usdt_perp_market.market_id == market.id for market in all_derivative_markets.values())))
163+
assert (any((btc_usdt_perp_market_meta.market_id == market.id for market in all_derivative_markets.values())))
161164

162165
all_binary_option_markets = await client.all_binary_option_markets()
163166
assert (1 == len(all_binary_option_markets))
164-
assert (any((first_match_bet_market.market_id == market.id for market in all_binary_option_markets.values())))
167+
assert (any((first_match_bet_market_meta.market_id == market.id for market in all_binary_option_markets.values())))

0 commit comments

Comments
 (0)