defi-state-querier 0.5.4__py3-none-any.whl → 0.5.6__py3-none-any.whl

Sign up to get free protection for your applications and to get access to all the features.
defi_services/__init__.py CHANGED
@@ -1 +1 @@
1
- __version__ = "0.5.4"
1
+ __version__ = "0.5.6"
@@ -1,9 +1,5 @@
1
1
  class Denoms:
2
- def __init__(self, denom: str, decimal: int):
3
- self.denom = denom
4
- self.decimal = decimal
5
-
6
- data = {
2
+ cosmos = {
7
3
  "uatom": {
8
4
  "name": "ATOM",
9
5
  "denom": "uatom",
@@ -185,3 +181,124 @@ class Denoms:
185
181
  "decimal": 6
186
182
  }
187
183
  }
184
+ orai = {
185
+ "orai1gjdmce2cfu2ld7cw0p9p7mkmssnry4zhwz3tmsmah435pkdv9qkqg2n7qm": {
186
+ "name": "Aave USDT",
187
+ "denom": "orai1gjdmce2cfu2ld7cw0p9p7mkmssnry4zhwz3tmsmah435pkdv9qkqg2n7qm",
188
+ "decimal": 6
189
+ },
190
+ "orai10ldgzued6zjp0mkqwsv2mux3ml50l97c74x8sg": {
191
+ "name": "aiRight",
192
+ "denom": "orai10ldgzued6zjp0mkqwsv2mux3ml50l97c74x8sg",
193
+ "decimal": 6,
194
+ "base_denom": "AIRI"
195
+ },
196
+ "orai10g6frpysmdgw5tdqke47als6f97aqmr8s3cljsvjce4n5enjftcqtamzsd": {
197
+ "name": "Bitcoin",
198
+ "denom": "orai10g6frpysmdgw5tdqke47als6f97aqmr8s3cljsvjce4n5enjftcqtamzsd",
199
+ "decimal": 6
200
+ },
201
+ "ibc/a2e2eec9057a4a1c2c0a6a4c78b0239118df5f278830f50b4a6bdd7a66506b78": {
202
+ "name": "Cosmos Hub",
203
+ "denom": "ibc/a2e2eec9057a4a1c2c0a6a4c78b0239118df5f278830f50b4a6bdd7a66506b78",
204
+ "decimal": 6
205
+ },
206
+ "orai19rtmkk6sn4tppvjmp5d5zj6gfsdykrl5rw2euu5gwur3luheuuusesqn49": {
207
+ "name": "Injective",
208
+ "denom": "orai19rtmkk6sn4tppvjmp5d5zj6gfsdykrl5rw2euu5gwur3luheuuusesqn49",
209
+ "decimal": 6
210
+ },
211
+ "orai1nd4r053e3kgedgld2ymen8l9yrw8xpjyaal7j5": {
212
+ "name": "Kawaii Islands",
213
+ "denom": "orai1nd4r053e3kgedgld2ymen8l9yrw8xpjyaal7j5",
214
+ "decimal": 6
215
+ },
216
+ "orai1gzvndtzceqwfymu2kqhta2jn6gmzxvzqwdgvjw": {
217
+ "name": "Milky",
218
+ "denom": "orai1gzvndtzceqwfymu2kqhta2jn6gmzxvzqwdgvjw",
219
+ "decimal": 6,
220
+ "base_denom": "MILKY"
221
+ },
222
+ "ibc/576b1d63e401b6a9a071c78a1d1316d016ec9333d2feb14ad503fac4b8731cd1": {
223
+ "name": "Neutaro",
224
+ "denom": "ibc/576b1d63e401b6a9a071c78a1d1316d016ec9333d2feb14ad503fac4b8731cd1",
225
+ "decimal": 6
226
+ },
227
+ "orai1hn8w33cqvysun2aujk5sv33tku4pgcxhhnsxmvnkfvdxagcx0p8qa4l98q": {
228
+ "name": "OraiDEX",
229
+ "denom": "orai1hn8w33cqvysun2aujk5sv33tku4pgcxhhnsxmvnkfvdxagcx0p8qa4l98q",
230
+ "decimal": 6
231
+ },
232
+ "orai1lplapmgqnelqn253stz6kmvm3ulgdaytn89a8mz9y85xq8wd684s6xl3lt": {
233
+ "name": "Orchai",
234
+ "denom": "orai1lplapmgqnelqn253stz6kmvm3ulgdaytn89a8mz9y85xq8wd684s6xl3lt",
235
+ "decimal": 6
236
+ },
237
+ "orai": {
238
+ "name": "Oraichain",
239
+ "denom": "orai",
240
+ "decimal": 6,
241
+ "base_denom": "orai"
242
+ },
243
+ "orai1lus0f0rhx8s03gdllx2n6vhkmf0536dv57wfge": {
244
+ "name": "OraiDEX",
245
+ "denom": "orai1lus0f0rhx8s03gdllx2n6vhkmf0536dv57wfge",
246
+ "decimal": 6
247
+ },
248
+ "orai19q4qak2g3cj2xc2y3060t0quzn3gfhzx08rjlrdd3vqxhjtat0cq668phq": {
249
+ "name": "Orchai Protocol Staked Compound ATOM",
250
+ "denom": "orai19q4qak2g3cj2xc2y3060t0quzn3gfhzx08rjlrdd3vqxhjtat0cq668phq",
251
+ "decimal": 6
252
+ },
253
+ "ibc/9c4dcd21b48231d0bc2ac3d1b74a864746b37e4292694c93c617324250d002fc": {
254
+ "name": "Osmosis",
255
+ "denom": "ibc/9c4dcd21b48231d0bc2ac3d1b74a864746b37e4292694c93c617324250d002fc",
256
+ "decimal": 6
257
+ },
258
+ "orai1x6uwy6p5rkh4tv44pztafvkj7y9pgqpfyg82006ts4l97fy0kjzs5mzm8p": {
259
+ "name": "Stride Staked Atom",
260
+ "denom": "orai1x6uwy6p5rkh4tv44pztafvkj7y9pgqpfyg82006ts4l97fy0kjzs5mzm8p",
261
+ "decimal": 6
262
+ },
263
+ "orai1d2e02yn3h382ajsqjuz0xlc8qvv5d320m2walmcnfsc406azk9vqf639fa": {
264
+ "name": "Stride Staked Osmo",
265
+ "denom": "orai1d2e02yn3h382ajsqjuz0xlc8qvv5d320m2walmcnfsc406azk9vqf639fa",
266
+ "decimal": 6
267
+ },
268
+ "orai12hzjxfh77wl572gdzct2fxv2arxcwh6gykc7qh": {
269
+ "name": "Tether",
270
+ "denom": "orai12hzjxfh77wl572gdzct2fxv2arxcwh6gykc7qh",
271
+ "decimal": 6
272
+ },
273
+ "orai15un8msx3n5zf9ahlxmfeqd2kwa5wm0nrpxer304m9nd5q6qq0g6sku5pdd": {
274
+ "name": "USDC",
275
+ "denom": "orai15un8msx3n5zf9ahlxmfeqd2kwa5wm0nrpxer304m9nd5q6qq0g6sku5pdd",
276
+ "decimal": 6,
277
+ "base_denom": "USDC"
278
+ },
279
+ "orai1dqa52a7hxxuv8ghe7q5v0s36ra0cthea960q2cukznleqhk0wpnshfegez": {
280
+ "name": "WETH",
281
+ "denom": "orai1dqa52a7hxxuv8ghe7q5v0s36ra0cthea960q2cukznleqhk0wpnshfegez",
282
+ "decimal": 6
283
+ },
284
+ "orai1c7tpjenafvgjtgm9aqwm7afnke6c56hpdms8jc6md40xs3ugd0es5encn0": {
285
+ "name": "Wrapped Tron",
286
+ "denom": "orai1c7tpjenafvgjtgm9aqwm7afnke6c56hpdms8jc6md40xs3ugd0es5encn0",
287
+ "decimal": 6
288
+ },
289
+ "orai1065qe48g7aemju045aeyprflytemx7kecxkf5m7u5h5mphd0qlcs47pclp": {
290
+ "name": "Staking Compound ORAI",
291
+ "denom": "orai1065qe48g7aemju045aeyprflytemx7kecxkf5m7u5h5mphd0qlcs47pclp",
292
+ "decimal": 6
293
+ }
294
+ }
295
+ all = {**orai, **cosmos}
296
+
297
+
298
+ class MulticallContract:
299
+ mapping = {
300
+ "orai": {
301
+ "multicall_balance": "orai1dyljypavg7qpt5d72a48a4pyg38d580aat55qql6tdcwfgydy6jsznk0h5",
302
+ "multicall": "orai1q7x644gmf7h8u8y6y8t9z9nnwl8djkmspypr6mxavsk9ual7dj0sxpmgwd"
303
+ }
304
+ }
@@ -1,26 +1,32 @@
1
- from defi_services.constants.cosmos_decimals_constant import Denoms
1
+ from defi_services.constants.cosmos_decimals_constant import Denoms, MulticallContract
2
+ from defi_services.constants.network_constants import Chains
2
3
  from defi_services.services.cosmos_token_services import CosmosTokenServices
3
4
 
4
5
 
5
6
  class CosmosStateProcessor:
6
- def __init__(self, lcd: str, denom: str):
7
+ def __init__(self, lcd: str, rest_uri: str = None, chain_id: str = Chains.oraichain):
8
+ self.chain_id = chain_id
7
9
  self.lcd = lcd
8
- self.denom = denom
10
+ self.rest_uri = rest_uri
9
11
 
10
- def get_token_balance(self, address):
11
- cosmos = CosmosTokenServices(self.lcd, self.denom)
12
- data = cosmos.query_balances(address)
13
- result = {}
14
- for item in data:
15
- denom = item.get('denom', "").lower()
16
- amount = int(item.get('amount', None))
17
- decimal = Denoms.data.get(denom, {}).get("decimal", 0)
18
- result[denom] = amount / 10 ** decimal
19
- return result
12
+ def get_token_balance(self, address, tokens):
13
+ cosmos = CosmosTokenServices(self.lcd, self.rest_uri, self.chain_id)
14
+ if self.chain_id in MulticallContract.mapping:
15
+ data = cosmos.query_balances(address, tokens)
16
+ else:
17
+ data = cosmos.query_coin_balances(address=address)
18
+ result = {}
19
+ for item in data:
20
+ denom = item.get('denom', "").lower()
21
+ amount = int(item.get('amount', None))
22
+ decimal = Denoms.all.get(denom, {}).get("decimal", 0)
23
+ result[denom] = amount / 10 ** decimal
24
+ return data
20
25
 
21
26
  def run(self, address: str, queries: list):
22
27
  result = []
23
- token_balances = self.get_token_balance(address)
28
+ tokens = [query.get('entity_id').lower() for query in queries if query.get('query_type') == 'token_balance']
29
+ token_balances = self.get_token_balance(address, tokens)
24
30
  for query in queries:
25
31
  query_id = query.get("query_id")
26
32
  entity_id = query.get("entity_id")
@@ -1,15 +1,26 @@
1
1
  import json
2
2
  from urllib.parse import quote
3
3
  import requests
4
+ import base64
5
+
6
+ from cosmpy.cosmwasm.rest_client import RestClient
7
+ from cosmpy.cosmwasm.rest_client import CosmWasmRestClient
8
+ from cosmpy.protos.cosmwasm.wasm.v1.query_pb2 import QuerySmartContractStateRequest
9
+
10
+ from defi_services.constants.cosmos_decimals_constant import Denoms, MulticallContract
4
11
 
5
12
 
6
13
  class CosmosTokenServices:
7
- def __init__(self, lcd: str, denom: str):
14
+ def __init__(self, lcd: str, rest_uri: str, chain_id: str):
15
+ self.chain_id = chain_id
8
16
  self.lcd = lcd
9
- self.denom = denom
17
+ self.rest_uri = rest_uri
18
+ self.client = CosmWasmRestClient(RestClient(rest_address=rest_uri))
19
+ self.decimals = Denoms.cosmos
20
+ self.decimals.update(Denoms.orai)
10
21
 
11
22
  # queries the balance of all coins for a single account.
12
- def query_balances(self, address: str):
23
+ def query_coin_balances(self, address: str):
13
24
  responses = []
14
25
  endpoint = '/cosmos/bank/v1beta1/balances/'
15
26
  try:
@@ -40,3 +51,91 @@ class CosmosTokenServices:
40
51
  return json.loads(requests.get(self.lcd + endpoint, timeout=60).content)
41
52
  except Exception:
42
53
  raise Exception
54
+
55
+ def query_balances(self, address: str, tokens: list):
56
+ balance_query, cw20_tokens = self.get_balance_function_info(address, tokens)
57
+ queries = []
58
+ for token in cw20_tokens:
59
+ queries.append({
60
+ "address": token,
61
+ "data": self.encode_data({"token_info": {}})
62
+ })
63
+ queries.append({
64
+ "address": MulticallContract.mapping.get(self.chain_id).get("multicall_balance"),
65
+ "data": self.encode_data(balance_query)
66
+ })
67
+ query = {
68
+ "aggregate": {
69
+ "queries": queries
70
+ }
71
+ }
72
+ query = json.dumps(query).encode('utf-8')
73
+ request_ = QuerySmartContractStateRequest(
74
+ address=MulticallContract.mapping.get(self.chain_id).get("multicall"), query_data=query)
75
+ response_ = self.client.SmartContractState(request_)
76
+ decoded_data = self.decode_response_data(response_, cw20_tokens, tokens)
77
+ return decoded_data
78
+
79
+ def decode_response_data(self, response_data, cw20_tokens, tokens):
80
+ response_data = json.loads(response_data.data)
81
+ return_data = response_data.get("return_data", [])
82
+ if not len(return_data):
83
+ return None
84
+ decimals = {}
85
+ for idx in range(0, len(cw20_tokens)):
86
+ encoded_data = return_data[idx]
87
+ decoded_data = self.decode_data(encoded_data['data'])
88
+ decimals[cw20_tokens[idx]] = decoded_data.get("decimals")
89
+
90
+ balance_data = self.decode_data(return_data[-1]['data'])
91
+ balances = {}
92
+ for idx in range(0, len(tokens)):
93
+ balances[tokens[idx]] = int(balance_data[idx])
94
+
95
+ for token in tokens:
96
+ if token in decimals:
97
+ balances[token] /= 10**decimals[token]
98
+ else:
99
+ balances[token] /= 10**self.decimals.get(token, {}).get("decimal", 0)
100
+
101
+ return balances
102
+
103
+ @staticmethod
104
+ def encode_data(params: dict):
105
+ data = json.dumps(params).encode('utf-8')
106
+ encode_data = base64.b64encode(data).decode()
107
+ return encode_data
108
+
109
+ @staticmethod
110
+ def decode_data(encode_data: str):
111
+ decoded_data = base64.b64decode(encode_data)
112
+ decoded_data = json.loads(decoded_data)
113
+ return decoded_data
114
+
115
+ @staticmethod
116
+ def get_balance_function_info(address: str, tokens: list):
117
+ data = {
118
+ "balance": {
119
+ "address": address,
120
+ "assets": []
121
+ }
122
+ }
123
+ cw20_tokens = []
124
+ for token in tokens:
125
+ head = token[:4]
126
+ if len(token) < 10 or head == 'ibc/':
127
+ data["balance"]["assets"].append({
128
+ "native_token": {
129
+ "denom": token
130
+ }
131
+ })
132
+ else:
133
+ data["balance"]["assets"].append(
134
+ {
135
+ "token": {
136
+ "contract_addr": token
137
+ }
138
+ }
139
+ )
140
+ cw20_tokens.append(token)
141
+ return data, cw20_tokens
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: defi-state-querier
3
- Version: 0.5.4
3
+ Version: 0.5.6
4
4
  Summary: Calculate apy, apr, and wallet information,... in decentralized applications.
5
5
  Home-page: https://github.com/Centic-io/defi-state-querier
6
6
  Author: Viet-Bang Pham
@@ -13,9 +13,10 @@ Requires-Python: >=3.6
13
13
  Description-Content-Type: text/markdown
14
14
  License-File: LICENSE
15
15
  Requires-Dist: web3 >=6.0.0
16
- Requires-Dist: query-blockchain-state-lib ==1.0.1
16
+ Requires-Dist: query-blockchain-state-lib >=1.0.1
17
17
  Requires-Dist: python-dotenv >=1.0.0
18
18
  Requires-Dist: pymongo >=4.3.3
19
+ Requires-Dist: cosmpy >=0.9.2
19
20
 
20
21
  # defi-state-querier
21
22
 
@@ -1,4 +1,4 @@
1
- defi_services/__init__.py,sha256=DITpct-LrdIsTgwx2NgH5Ghx5y8Xgz1YMimy1ZV5RTY,22
1
+ defi_services/__init__.py,sha256=CMH34Gt1AqO7z_TqRj94XwohGoVCf8aes0djkqm45mk,22
2
2
  defi_services/abis/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
3
3
  defi_services/abis/multicall_v3_abi.py,sha256=0aPxjrJJFU17fODjvYFRDn5Y5J1yi_AJKc8v1uohNGY,12352
4
4
  defi_services/abis/dex/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
@@ -295,7 +295,7 @@ defi_services/abis/vault/lido/lido_wsteth.py,sha256=DJhMToJS43vV2r5WP2kZSiagt7R8
295
295
  defi_services/abis/vault/lido/lidor_accounting_oracle_hash_consensus.py,sha256=TWCLaccDgvWJZZKLAeoznYgGi3g3M_hFqoo3KE_L7K8,21039
296
296
  defi_services/constants/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
297
297
  defi_services/constants/chain_constant.py,sha256=aU7xLaQYLro-7sI5Jpyi0tEHhEQcchyxlxEEJaotFMM,1018
298
- defi_services/constants/cosmos_decimals_constant.py,sha256=Qk51o5d3VNVYJxMqU23UXTOTWBIyKHlbQ-nuYS8An10,8630
298
+ defi_services/constants/cosmos_decimals_constant.py,sha256=SjFkFbkvxVAk0jtok_1EaBh69ZE8S1fFAbVn6bdJO4s,13635
299
299
  defi_services/constants/db_constant.py,sha256=gDMFFjhPOgQHuS9sPOTFbsfSdIfdWQEE4tGlxHQhNsE,2566
300
300
  defi_services/constants/mongo_constant.py,sha256=qsDIq2Gg5MkBGaEOh4Fd9y6fOOidF4gGPnQwtaPon3E,398
301
301
  defi_services/constants/network_constants.py,sha256=aCUhPFOA9oAMXrIrw67wfEvc4B3HcTlCCz9U1Ku4o50,13574
@@ -313,7 +313,7 @@ defi_services/constants/entities/vault_services.py,sha256=IMmQc15wS1KA7EuQqM_Ara
313
313
  defi_services/jobs/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
314
314
  defi_services/jobs/processors/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
315
315
  defi_services/jobs/processors/call_state_processor.py,sha256=WseX0OaMA5D_ANYofrz7W2BooSMdHlGRay4NuiABmM4,436
316
- defi_services/jobs/processors/cosmos_state_processor.py,sha256=2S5fhqHQ4pUtRaLnbFJNUo4jKc5MHq7HA-0FHebOJyo,1334
316
+ defi_services/jobs/processors/cosmos_state_processor.py,sha256=eZa2fO5Vh4n_VTJjKSHTud325KjHvFA_e8NcywDEun4,1813
317
317
  defi_services/jobs/processors/multi_call_state_processor.py,sha256=AksswGMSyStbnvkQcBVb0Y2tdpkUgc1CWoeSiMpGy8U,462
318
318
  defi_services/jobs/processors/multi_state_processor.py,sha256=Od7Y8zPz7xj0L-_0YuK4wg_94fX8Q1PquuKUUYteIJU,5731
319
319
  defi_services/jobs/processors/solana_state_processor.py,sha256=szsBYrkEV8kBzuA_iaSVescnwKJ2MHoTbut5kORbWTs,4065
@@ -325,7 +325,7 @@ defi_services/jobs/queriers/solana_state_querier.py,sha256=TQELYo6GUoF8s-LfetqYb
325
325
  defi_services/jobs/queriers/state_querier.py,sha256=q7cAW395urZjcWTpqw8m_nDunLyyTuAXJ3DRx_1HOxo,7384
326
326
  defi_services/jobs/queriers/substrate_state_querier.py,sha256=_T0Dk06sP_uBKaxgj2J_EBtJDoq-hi8jU7Np4iuO0LA,3858
327
327
  defi_services/services/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
328
- defi_services/services/cosmos_token_services.py,sha256=6z_eE7lF5tY461VJ6uVoG18hOdXKeWR4NgDDP-MGI0k,1603
328
+ defi_services/services/cosmos_token_services.py,sha256=-QGmhS-WtwJbOnOFQ7CuM10glPgzZvYPtPlCE6IZIKM,5248
329
329
  defi_services/services/dex_protocol_services.py,sha256=J93P7kwy1v98aeA95ErT7jDCCOAelFuCNiTXl3PVFFo,6656
330
330
  defi_services/services/nft_services.py,sha256=vKva0OJQTTxgP1lrZ5ij2sw2N44JxN5dfLLtvNJB6aA,2188
331
331
  defi_services/services/protocol_services.py,sha256=3Yca433xGXowcV4EjFoQ90AJHg-SfuOBsDF7aMCJ8Zk,7626
@@ -464,8 +464,8 @@ defi_services/utils/market_service.py,sha256=imPtPHBkpEx5JnhqeIWYqbCjsIEm8IKBYHN
464
464
  defi_services/utils/memory_storage.py,sha256=BOT8laB0iVSCGE-oDlpWJQLbSC6X2blKX4zuQbs4inc,851
465
465
  defi_services/utils/sqrt_price_math.py,sha256=9lgUeWFT4wjl3Vq3b7-jZ2bGvvZx7dDBSfVnM3lsZ8o,5575
466
466
  defi_services/utils/thread_proxy.py,sha256=5Z8biAyEReUkh3vfJSvEv7GwMe3CsE5M8CbghkQtePw,2951
467
- defi_state_querier-0.5.4.dist-info/LICENSE,sha256=6jmfxa8nUIwfKnzZUxAHJSJ_IS7h7mpbJq26cWjoo-o,1063
468
- defi_state_querier-0.5.4.dist-info/METADATA,sha256=gTQEjp_THAVXFoD6zox3Quu6bFZqKb4hXlMPBCcgMzw,4387
469
- defi_state_querier-0.5.4.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
470
- defi_state_querier-0.5.4.dist-info/top_level.txt,sha256=C-OTxHK6MknKK-nAbEzCPDUl1M6pktRhgJrmsozdf6g,14
471
- defi_state_querier-0.5.4.dist-info/RECORD,,
467
+ defi_state_querier-0.5.6.dist-info/LICENSE,sha256=6jmfxa8nUIwfKnzZUxAHJSJ_IS7h7mpbJq26cWjoo-o,1063
468
+ defi_state_querier-0.5.6.dist-info/METADATA,sha256=jksOD0A7CWoHH9ykENU3VhLBGav0P_DA5MWriPxQ74o,4417
469
+ defi_state_querier-0.5.6.dist-info/WHEEL,sha256=mguMlWGMX-VHnMpKOjjQidIo1ssRlCFu4a4mBpz1s2M,91
470
+ defi_state_querier-0.5.6.dist-info/top_level.txt,sha256=C-OTxHK6MknKK-nAbEzCPDUl1M6pktRhgJrmsozdf6g,14
471
+ defi_state_querier-0.5.6.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: bdist_wheel (0.43.0)
2
+ Generator: setuptools (70.1.1)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5