defi-state-querier 0.0.6__py3-none-any.whl → 0.0.9__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- defi_services/__init__.py +1 -1
- defi_services/abis/lending/morpho/__init__.py +0 -0
- defi_services/abis/lending/morpho/morpho_aave_v2_comptroller_abi.py +2301 -0
- defi_services/abis/lending/morpho/morpho_aave_v2_lens_abi.py +1208 -0
- defi_services/abis/lending/morpho/morpho_aave_v3_comptroller_abi.py +2994 -0
- defi_services/abis/lending/morpho/morpho_compound_comptroller_abi.py +2301 -0
- defi_services/abis/lending/morpho/morpho_compound_lens_abi.py +1402 -0
- defi_services/abis/lending/morpho/morpho_compound_reward_manager_abi.py +307 -0
- defi_services/abis/lending/radiant_v2/__init__.py +0 -0
- defi_services/abis/lending/radiant_v2/radiant_v2_incentive_abi.py +1016 -0
- defi_services/abis/lending/wepiggy/__init__.py +0 -0
- defi_services/abis/lending/wepiggy/wepiggy_distribution_abi.py +1047 -0
- defi_services/constants/chain_constant.py +1 -0
- defi_services/constants/entities/lending_constant.py +26 -0
- defi_services/constants/entities/lending_services.py +85 -0
- defi_services/constants/query_constant.py +1 -0
- defi_services/constants/token_constant.py +2 -0
- defi_services/jobs/processors/__init__.py +0 -0
- defi_services/jobs/processors/solana_state_processor.py +101 -0
- defi_services/jobs/{state_processor.py → processors/state_processor.py} +9 -4
- defi_services/jobs/processors/substrate_state_processor.py +97 -0
- defi_services/jobs/queriers/__init__.py +0 -0
- defi_services/jobs/queriers/solana_state_querier.py +83 -0
- defi_services/jobs/{state_querier.py → queriers/state_querier.py} +3 -2
- defi_services/jobs/queriers/substrate_state_querier.py +84 -0
- defi_services/services/lending/aave_v2_services.py +8 -7
- defi_services/services/lending/aave_v3_services.py +12 -9
- defi_services/services/lending/compound_service.py +29 -10
- defi_services/services/lending/cream_services.py +5 -4
- defi_services/services/lending/flux_services.py +34 -27
- defi_services/services/lending/geist_services.py +4 -3
- defi_services/services/lending/granary_v1_services.py +6 -7
- defi_services/services/lending/iron_bank_service.py +20 -13
- defi_services/services/lending/lending_info/aave_v2_services.py +2 -2
- defi_services/services/lending/lending_info/ethereum/morpho_aave_v2_eth.py +53 -0
- defi_services/services/lending/lending_info/ethereum/morpho_aave_v3_eth.py +51 -0
- defi_services/services/lending/lending_info/ethereum/morpho_compound_eth.py +44 -0
- defi_services/services/lending/lending_info/ethereum/wepiggy_eth.py +41 -0
- defi_services/services/lending/liqee_service.py +5 -26
- defi_services/services/lending/morpho_aave_v2_services.py +89 -0
- defi_services/services/lending/morpho_aave_v3_services.py +159 -0
- defi_services/services/lending/morpho_compound_services.py +233 -0
- defi_services/services/lending/onyx_service.py +32 -13
- defi_services/services/lending/radiant_v2_services.py +30 -3
- defi_services/services/lending/strike_service.py +29 -10
- defi_services/services/lending/trava_services.py +8 -7
- defi_services/services/lending/uwu_services.py +8 -7
- defi_services/services/lending/valas_services.py +6 -5
- defi_services/services/lending/venus_services.py +28 -8
- defi_services/services/lending/wepiggy_services.py +519 -0
- defi_services/services/nft_services.py +1 -1
- defi_services/services/solana_token_services.py +46 -0
- defi_services/services/substrate_token_services.py +70 -0
- defi_services/services/token_services.py +1 -3
- defi_services/utils/init_services.py +3 -3
- {defi_state_querier-0.0.6.dist-info → defi_state_querier-0.0.9.dist-info}/METADATA +3 -3
- {defi_state_querier-0.0.6.dist-info → defi_state_querier-0.0.9.dist-info}/RECORD +60 -32
- defi_services/constants/entities/lending.py +0 -97
- {defi_state_querier-0.0.6.dist-info → defi_state_querier-0.0.9.dist-info}/LICENSE +0 -0
- {defi_state_querier-0.0.6.dist-info → defi_state_querier-0.0.9.dist-info}/WHEEL +0 -0
- {defi_state_querier-0.0.6.dist-info → defi_state_querier-0.0.9.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,51 @@
|
|
1
|
+
MORPHO_AAVE_V3_ETH = {
|
2
|
+
"name": "Compound Lending Pool",
|
3
|
+
"comptrollerAddress": "0x33333aea097c193e66081e930c33020272b33333",
|
4
|
+
"poolToken": "0x9994e35db50125e0df82e4c2dde62496ce330999",
|
5
|
+
"type": "LENDING_POOL",
|
6
|
+
"forked": "morpho-aave-v3",
|
7
|
+
"reservesList": {
|
8
|
+
"0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2": {
|
9
|
+
"tToken": "0x4d5f47fa6a74757f35c14fd3a6ef8e3c9bc514e8",
|
10
|
+
"sdToken": "0x102633152313c81cd80419b6ecf66d14ad68949a",
|
11
|
+
"dToken": "0xea51d7853eefb32b6ee06b1c12e6dcca88be0ffe",
|
12
|
+
"liquidationThreshold": 0.83
|
13
|
+
},
|
14
|
+
"0x7f39c581f595b53c5cb19bd0b3f8da6c935e2ca0": {
|
15
|
+
"tToken": "0x0b925ed163218f6662a35e0f0371ac234f9e9371",
|
16
|
+
"sdToken": "0x39739943199c0fbfe9e5f1b5b160cd73a64cb85d",
|
17
|
+
"dToken": "0xc96113eed8cab59cd8a66813bcb0ceb29f06d2e4",
|
18
|
+
"liquidationThreshold": 0.8
|
19
|
+
},
|
20
|
+
"0x6b175474e89094c44da98b954eedeac495271d0f": {
|
21
|
+
"tToken": "0x018008bfb33d285247a21d44e50697654f754e63",
|
22
|
+
"sdToken": "0x413adac9e2ef8683adf5ddaece8f19613d60d1bb",
|
23
|
+
"dToken": "0xcf8d0c70c850859266f5c338b38f9d663181c314",
|
24
|
+
"liquidationThreshold": 0.8
|
25
|
+
},
|
26
|
+
"0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48": {
|
27
|
+
"tToken": "0x98c23e9d8f34fefb1b7bd6a91b7ff122f4e16f5c",
|
28
|
+
"sdToken": "0xb0fe3d292f4bd50de902ba5bdf120ad66e9d7a39",
|
29
|
+
"dToken": "0x72e95b8931767c79ba4eee721354d6e99a61d004",
|
30
|
+
"liquidationThreshold": 0.8
|
31
|
+
},
|
32
|
+
"0x2260fac5e5542a773aa44fbcfedf7c193bc2c599": {
|
33
|
+
"tToken": "0x5ee5bf7ae06d1be5997a1a72006fe6c607ec6de8",
|
34
|
+
"sdToken": "0xa1773f1ccf6db192ad8fe826d15fe1d328b03284",
|
35
|
+
"dToken": "0x40aabef1aa8f0eec637e0e7d92fbffb2f26a8b7b",
|
36
|
+
"liquidationThreshold": 0.78
|
37
|
+
},
|
38
|
+
"0xae78736cd615f374d3085123a210448e74fc6393": {
|
39
|
+
"tToken": "0xcc9ee9483f662091a1de4795249e24ac0ac2630f",
|
40
|
+
"sdToken": "0x1d1906f909cae494c7441604dafdddbd0485a925",
|
41
|
+
"dToken": "0xae8593dd575fe29a9745056aa91c4b746eee62c8",
|
42
|
+
"liquidationThreshold": 0.74
|
43
|
+
},
|
44
|
+
"0xbe9895146f7af43049ca1c1ae358b0541ea49704": {
|
45
|
+
"tToken": "0x977b6fc5de62598b08c85ac8cf2b745874e8b78c",
|
46
|
+
"sdToken": "0x82be6012cea6d147b968ebaea5ceecf6a5b4f493",
|
47
|
+
"dToken": "0x0c91bca95b5fe69164ce583a2ec9429a569798ed",
|
48
|
+
"liquidationThreshold": 0.74
|
49
|
+
},
|
50
|
+
}
|
51
|
+
}
|
@@ -0,0 +1,44 @@
|
|
1
|
+
MORPHO_COMPOUND_ETH = {
|
2
|
+
"name": "Compound Lending Pool",
|
3
|
+
"rewardToken": "0xc00e94cb662c3520282e6f5717214004a7f26888",
|
4
|
+
"comptrollerAddress": "0x8888882f8f843896699869179fb6e4f7e3b58888",
|
5
|
+
"lensAddress": "0x930f1b46e1d081ec1524efd95752be3ece51ef67",
|
6
|
+
"rewardManager": '0x78681e63b6f3ad81ecd64aecc404d765b529c80d',
|
7
|
+
"poolToken": "0x9994e35db50125e0df82e4c2dde62496ce330999",
|
8
|
+
"type": "LENDING_POOL",
|
9
|
+
"forked": "morpho",
|
10
|
+
"reservesList": {
|
11
|
+
"0x6b175474e89094c44da98b954eedeac495271d0f": {
|
12
|
+
"cToken": "0x5d3a536e4d6dbd6114cc1ead35777bab948e3643",
|
13
|
+
"liquidationThreshold": 0.835
|
14
|
+
},
|
15
|
+
"0xc00e94cb662c3520282e6f5717214004a7f26888": {
|
16
|
+
"cToken": "0x70e36f6bf80a52b3b46b3af8e106cc0ed743e8e4",
|
17
|
+
"liquidationThreshold": 0.6
|
18
|
+
},
|
19
|
+
"0x0000000000000000000000000000000000000000": {
|
20
|
+
"cToken": "0x4ddc2d193948926d02f9b1fe9e1daa0718270ed5",
|
21
|
+
"liquidationThreshold": 0.825
|
22
|
+
},
|
23
|
+
"0x956f47f50a910163d8bf957cf5846d573e7f87ca": {
|
24
|
+
"cToken": "0x7713dd9ca933848f6819f38b8352d9a15ea73f67",
|
25
|
+
"liquidationThreshold": 0.0
|
26
|
+
},
|
27
|
+
"0x1f9840a85d5af5bf1d1762f925bdaddc4201f984": {
|
28
|
+
"cToken": "0x35a18000230da775cac24873d00ff85bccded550",
|
29
|
+
"liquidationThreshold": 0.75
|
30
|
+
},
|
31
|
+
"0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48": {
|
32
|
+
"cToken": "0x39aa39c021dfbae8fac545936693ac917d5e7563",
|
33
|
+
"liquidationThreshold": 0.855
|
34
|
+
},
|
35
|
+
"0xdac17f958d2ee523a2206206994597c13d831ec7": {
|
36
|
+
"cToken": "0xf650c3d88d12db855b8bf7d11be6c55a4e07dcc9",
|
37
|
+
"liquidationThreshold": 0.0
|
38
|
+
},
|
39
|
+
"0x2260fac5e5542a773aa44fbcfedf7c193bc2c599": {
|
40
|
+
"cToken": "0xccf4429db6322d5c611ee964527d42e5d685dd6a",
|
41
|
+
"liquidationThreshold": 0.7
|
42
|
+
},
|
43
|
+
}
|
44
|
+
}
|
@@ -0,0 +1,41 @@
|
|
1
|
+
WEPIGGY_ETH = {
|
2
|
+
"name": "Wepiggy Lending Pool",
|
3
|
+
"rewardToken": "0x6f620ec89b8479e97a6985792d0c64f237566746",
|
4
|
+
"comptrollerAddress": "0x0c8c1ab017c3c0c8a48dd9f1db2f59022d190f0b",
|
5
|
+
"lensAddress": "0x6a05bd123d780055c38526cc05d3c9b90d0e471c",
|
6
|
+
"distributionAddress": "0x3e5496e50793e72e6143a15bed1c2535f0b0b9b0",
|
7
|
+
"poolToken": "0x6f620ec89b8479e97a6985792d0c64f237566746",
|
8
|
+
"type": "LENDING_POOL",
|
9
|
+
"forked": "compound",
|
10
|
+
"reservesList": {
|
11
|
+
"0x1f9840a85d5af5bf1d1762f925bdaddc4201f984": {
|
12
|
+
"cToken": "0x82413f75f0da101e0fe7f6ff6cba3461f7e04f29",
|
13
|
+
"liquidationThreshold": 0.6
|
14
|
+
},
|
15
|
+
"0x6b175474e89094c44da98b954eedeac495271d0f": {
|
16
|
+
"cToken": "0x85166b72c87697a6acff24101b43fd54fe28a179",
|
17
|
+
"liquidationThreshold": 0.75
|
18
|
+
},
|
19
|
+
"0xdac17f958d2ee523a2206206994597c13d831ec7": {
|
20
|
+
"cToken": "0x5cfad792c4df1323188180778aec58e00eace32a",
|
21
|
+
"liquidationThreshold": 0.8
|
22
|
+
},
|
23
|
+
"0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48": {
|
24
|
+
"cToken": "0xf8e5b9738bf63adfff36a849f9b9c9617c8d8c1f",
|
25
|
+
"liquidationThreshold": 0.8
|
26
|
+
},
|
27
|
+
"0x2260fac5e5542a773aa44fbcfedf7c193bc2c599": {
|
28
|
+
"cToken": "0xc12b9d620bfcb48be3e0ccbf0ea80c717333b46f",
|
29
|
+
"liquidationThreshold": 0.7
|
30
|
+
},
|
31
|
+
"0x0000000000000000000000000000000000000000": {
|
32
|
+
"cToken": "0x27a94869341838d5783368a8503fda5fbcd7987c",
|
33
|
+
"liquidationThreshold": 0.8
|
34
|
+
},
|
35
|
+
"0xbbbbca6a901c926f240b89eacb641d8aec7aeafd": {
|
36
|
+
"cToken": "0x690aa2591e57180cba5a6123e9d462907a5e1c95",
|
37
|
+
"liquidationThreshold": 0.5
|
38
|
+
},
|
39
|
+
|
40
|
+
}
|
41
|
+
}
|
@@ -3,18 +3,16 @@ import time
|
|
3
3
|
|
4
4
|
from web3 import Web3
|
5
5
|
|
6
|
-
from defi_services.abis.lending.cream.cream_comptroller_abi import CREAM_COMPTROLLER_ABI
|
7
|
-
from defi_services.abis.lending.cream.cream_lens_abi import CREAM_LENS_ABI
|
8
6
|
from defi_services.abis.lending.liqee.liqee_comptroller_abi import LIQEE_CONTROLLER_ABI
|
9
7
|
from defi_services.abis.lending.liqee.liqee_lending_data_abi import LIQEE_LENDING_DATA_ABI
|
10
8
|
from defi_services.abis.lending.liqee.liqee_token_abi import LIQEE_TOKEN_ABI
|
11
9
|
from defi_services.abis.token.ctoken_abi import CTOKEN_ABI
|
12
10
|
from defi_services.abis.token.erc20_abi import ERC20_ABI
|
13
11
|
from defi_services.constants.chain_constant import Chain
|
14
|
-
from defi_services.constants.
|
12
|
+
from defi_services.constants.entities.lending_constant import Lending
|
15
13
|
from defi_services.constants.query_constant import Query
|
16
|
-
from defi_services.constants.token_constant import
|
17
|
-
from defi_services.jobs.state_querier import StateQuerier
|
14
|
+
from defi_services.constants.token_constant import Token
|
15
|
+
from defi_services.jobs.queriers.state_querier import StateQuerier
|
18
16
|
from defi_services.services.lending.lending_info.ethereum.liqee_eth import LIQEE_ETH
|
19
17
|
from defi_services.services.protocol_services import ProtocolServices
|
20
18
|
|
@@ -29,7 +27,7 @@ class LiqeeInfo:
|
|
29
27
|
|
30
28
|
class LiqeeStateService(ProtocolServices):
|
31
29
|
def __init__(self, state_service: StateQuerier, chain_id: str = "0x1"):
|
32
|
-
self.name = f"{chain_id}
|
30
|
+
self.name = f"{chain_id}_{Lending.liqee}"
|
33
31
|
self.chain_id = chain_id
|
34
32
|
self.pool_info = LiqeeInfo.mapping.get(chain_id)
|
35
33
|
self.state_service = state_service
|
@@ -40,7 +38,7 @@ class LiqeeStateService(ProtocolServices):
|
|
40
38
|
# BASIC FUNCTIONS
|
41
39
|
def get_service_info(self):
|
42
40
|
info = {
|
43
|
-
|
41
|
+
Lending.liqee: {
|
44
42
|
"chain_id": self.chain_id,
|
45
43
|
"type": "lending",
|
46
44
|
"protocol_info": self.pool_info
|
@@ -300,25 +298,6 @@ class LiqeeStateService(ProtocolServices):
|
|
300
298
|
ctoken, CTOKEN_ABI, fn_name, fn_paras, block_number
|
301
299
|
)
|
302
300
|
|
303
|
-
def get_ctoken_metadata_all(
|
304
|
-
self,
|
305
|
-
reserves_info: dict = None,
|
306
|
-
block_number: int = "latest"
|
307
|
-
):
|
308
|
-
tokens = [Web3.toChecksumAddress(value['cToken']) for key, value in reserves_info.items()]
|
309
|
-
key = f"cTokenMetadataAll_{self.pool_info.get('lensAddress')}_{block_number}".lower()
|
310
|
-
return {
|
311
|
-
key: self.get_lens_function_info("cTokenMetadataAll", tokens, block_number)
|
312
|
-
}
|
313
|
-
|
314
|
-
def ctoken_underlying_price_all(
|
315
|
-
self, reserves_info, block_number: int = 'latest'):
|
316
|
-
tokens = [Web3.toChecksumAddress(value['cToken']) for key, value in reserves_info.items()]
|
317
|
-
key = f"cTokenUnderlyingPriceAll_{self.pool_info.get('lensAddress')}_{block_number}".lower()
|
318
|
-
return {
|
319
|
-
key: self.get_lens_function_info("cTokenUnderlyingPriceAll", tokens, block_number)
|
320
|
-
}
|
321
|
-
|
322
301
|
def get_all_markets(
|
323
302
|
self, block_number: int = 'latest'):
|
324
303
|
key = f"getAllMarkets_{self.pool_info.get('comptrollerAddress')}_{block_number}".lower()
|
@@ -0,0 +1,89 @@
|
|
1
|
+
import logging
|
2
|
+
import time
|
3
|
+
|
4
|
+
from web3 import Web3
|
5
|
+
|
6
|
+
from defi_services.abis.lending.aave_v2_and_forlks.aave_v2_incentives_abi import AAVE_V2_INCENTIVES_ABI
|
7
|
+
from defi_services.abis.lending.aave_v2_and_forlks.lending_pool_abi import LENDING_POOL_ABI
|
8
|
+
from defi_services.abis.lending.aave_v2_and_forlks.oracle_abi import ORACLE_ABI
|
9
|
+
from defi_services.abis.lending.morpho.morpho_aave_v2_comptroller_abi import MORPHO_AAVE_V2_COMPTROLLER
|
10
|
+
from defi_services.abis.lending.morpho.morpho_aave_v2_lens_abi import MORPHO_AAVE_V2_LENS_ABI
|
11
|
+
from defi_services.constants.chain_constant import Chain
|
12
|
+
from defi_services.constants.entities.lending_constant import Lending
|
13
|
+
from defi_services.jobs.queriers.state_querier import StateQuerier
|
14
|
+
from defi_services.services.lending.lending_info.aave_v2_services import AaveInfo
|
15
|
+
from defi_services.services.lending.lending_info.ethereum.morpho_aave_v2_eth import MORPHO_AAVE_V2_ETH
|
16
|
+
from defi_services.services.lending.morpho_compound_services import MorphoCompoundServices
|
17
|
+
|
18
|
+
logger = logging.getLogger("Compound Lending Pool State Service")
|
19
|
+
|
20
|
+
|
21
|
+
class MorphoAaveV2Info:
|
22
|
+
mapping = {
|
23
|
+
Chain.ethereum: MORPHO_AAVE_V2_ETH
|
24
|
+
}
|
25
|
+
|
26
|
+
|
27
|
+
class MorphoAaveV2Services(MorphoCompoundServices):
|
28
|
+
def __init__(self, state_service: StateQuerier, chain_id: str = "0x1"):
|
29
|
+
super().__init__(state_service, chain_id)
|
30
|
+
self.name = f"{chain_id}_{Lending.morpho_aave_v2}"
|
31
|
+
self.chain_id = chain_id
|
32
|
+
self.compound_info = AaveInfo.mapping.get(chain_id)
|
33
|
+
self.pool_info = MorphoAaveV2Info.mapping.get(chain_id)
|
34
|
+
self.state_service = state_service
|
35
|
+
self.lending_abi = LENDING_POOL_ABI
|
36
|
+
self.incentive_abi = AAVE_V2_INCENTIVES_ABI
|
37
|
+
self.oracle_abi = ORACLE_ABI
|
38
|
+
self.lens_abi = MORPHO_AAVE_V2_LENS_ABI
|
39
|
+
self.comptroller_abi = MORPHO_AAVE_V2_COMPTROLLER
|
40
|
+
self.market_key = 'tToken'
|
41
|
+
|
42
|
+
# BASIC FUNCTIONS
|
43
|
+
def get_service_info(self):
|
44
|
+
info = {
|
45
|
+
Lending.morpho_aave_v2: {
|
46
|
+
"chain_id": self.chain_id,
|
47
|
+
"type": "lending",
|
48
|
+
"protocol_info": self.pool_info
|
49
|
+
}
|
50
|
+
}
|
51
|
+
return info
|
52
|
+
|
53
|
+
def get_dapp_asset_info(
|
54
|
+
self,
|
55
|
+
block_number: int = "latest"):
|
56
|
+
begin = time.time()
|
57
|
+
_w3 = self.state_service.get_w3()
|
58
|
+
pool_address = Web3.toChecksumAddress(self.pool_info['address'])
|
59
|
+
contract = _w3.eth.contract(address=pool_address, abi=self.lending_abi)
|
60
|
+
comptroller_contract = _w3.eth.contract(
|
61
|
+
address=_w3.toChecksumAddress(self.pool_info.get("comptrollerAddress")), abi=self.comptroller_abi)
|
62
|
+
markets = comptroller_contract.functions.getAllMarkets().call(block_identifier=block_number)
|
63
|
+
reserves_list = contract.functions.getReservesList().call(block_identifier=block_number)
|
64
|
+
reserves_info = {}
|
65
|
+
for token in reserves_list:
|
66
|
+
value = contract.functions.getReserveData(token).call(block_identifier=block_number)
|
67
|
+
key = token.lower()
|
68
|
+
ttoken = value[7]
|
69
|
+
if ttoken in markets:
|
70
|
+
reserves_info[key] = {}
|
71
|
+
reserves_info[key]["tToken"] = ttoken.lower()
|
72
|
+
reserves_info[key]["dToken"] = value[9].lower()
|
73
|
+
reserves_info[key]["sdToken"] = value[8].lower()
|
74
|
+
risk_param = bin(value[0][0])[2:]
|
75
|
+
reserves_info[key]["liquidationThreshold"] = int(risk_param[-31:-16], 2) / 10 ** 4
|
76
|
+
logger.info(f"Get reserves information in {time.time() - begin}s")
|
77
|
+
return reserves_info
|
78
|
+
|
79
|
+
# REWARDS BALANCE
|
80
|
+
def get_claimable_rewards_balance_function_info(
|
81
|
+
self,
|
82
|
+
wallet_address: str,
|
83
|
+
block_number: int = "latest",
|
84
|
+
):
|
85
|
+
return {}
|
86
|
+
|
87
|
+
def calculate_claimable_rewards_balance(self, wallet_address: str, decoded_data: dict,
|
88
|
+
block_number: int = "latest"):
|
89
|
+
return {}
|
@@ -0,0 +1,159 @@
|
|
1
|
+
import logging
|
2
|
+
import time
|
3
|
+
|
4
|
+
from web3 import Web3
|
5
|
+
|
6
|
+
from defi_services.abis.lending.aave_v2_and_forlks.aave_v2_incentives_abi import AAVE_V2_INCENTIVES_ABI
|
7
|
+
from defi_services.abis.lending.aave_v2_and_forlks.lending_pool_abi import LENDING_POOL_ABI
|
8
|
+
from defi_services.abis.lending.aave_v2_and_forlks.oracle_abi import ORACLE_ABI
|
9
|
+
from defi_services.abis.lending.morpho.morpho_aave_v3_comptroller_abi import MORPHO_AAVE_V3_COMPTROLLER_ABI
|
10
|
+
from defi_services.abis.token.erc20_abi import ERC20_ABI
|
11
|
+
from defi_services.constants.chain_constant import Chain
|
12
|
+
from defi_services.constants.entities.lending_constant import Lending
|
13
|
+
from defi_services.constants.token_constant import Token
|
14
|
+
from defi_services.jobs.queriers.state_querier import StateQuerier
|
15
|
+
from defi_services.services.lending.aave_v3_services import AaveV3Info
|
16
|
+
from defi_services.services.lending.lending_info.ethereum.morpho_aave_v3_eth import MORPHO_AAVE_V3_ETH
|
17
|
+
from defi_services.services.lending.morpho_compound_services import MorphoCompoundServices
|
18
|
+
|
19
|
+
logger = logging.getLogger("Compound Lending Pool State Service")
|
20
|
+
|
21
|
+
|
22
|
+
class MorphoAaveV3Info:
|
23
|
+
mapping = {
|
24
|
+
Chain.ethereum: MORPHO_AAVE_V3_ETH
|
25
|
+
}
|
26
|
+
|
27
|
+
|
28
|
+
class MorphoAaveV3Services(MorphoCompoundServices):
|
29
|
+
def __init__(self, state_service: StateQuerier, chain_id: str = "0x1"):
|
30
|
+
super().__init__(state_service, chain_id)
|
31
|
+
self.name = f"{chain_id}_{Lending.morpho_aave_v3}"
|
32
|
+
self.chain_id = chain_id
|
33
|
+
self.aave_info = AaveV3Info.mapping.get(chain_id)
|
34
|
+
self.pool_info = MorphoAaveV3Info.mapping.get(chain_id)
|
35
|
+
self.state_service = state_service
|
36
|
+
self.lending_abi = LENDING_POOL_ABI
|
37
|
+
self.incentive_abi = AAVE_V2_INCENTIVES_ABI
|
38
|
+
self.oracle_abi = ORACLE_ABI
|
39
|
+
self.comptroller_abi = MORPHO_AAVE_V3_COMPTROLLER_ABI
|
40
|
+
self.market_key = 'tToken'
|
41
|
+
|
42
|
+
# BASIC FUNCTIONS
|
43
|
+
def get_service_info(self):
|
44
|
+
info = {
|
45
|
+
Lending.morpho_aave_v3: {
|
46
|
+
"chain_id": self.chain_id,
|
47
|
+
"type": "lending",
|
48
|
+
"protocol_info": self.pool_info
|
49
|
+
}
|
50
|
+
}
|
51
|
+
return info
|
52
|
+
|
53
|
+
def get_dapp_asset_info(
|
54
|
+
self,
|
55
|
+
block_number: int = "latest"):
|
56
|
+
begin = time.time()
|
57
|
+
_w3 = self.state_service.get_w3()
|
58
|
+
pool_address = Web3.toChecksumAddress(self.aave_info['address'])
|
59
|
+
contract = _w3.eth.contract(address=pool_address, abi=self.lending_abi)
|
60
|
+
comptroller_contract = _w3.eth.contract(
|
61
|
+
address=_w3.toChecksumAddress(self.pool_info.get("comptrollerAddress")), abi=self.comptroller_abi)
|
62
|
+
markets = comptroller_contract.functions.getAllMarkets().call(block_identifier=block_number)
|
63
|
+
markets = [i.lower() for i in markets]
|
64
|
+
reserves_list = contract.functions.getReservesList().call(block_identifier=block_number)
|
65
|
+
reserves_info = {}
|
66
|
+
for token in reserves_list:
|
67
|
+
value = contract.functions.getReserveData(token).call(block_identifier=block_number)
|
68
|
+
key = token.lower()
|
69
|
+
if token in markets:
|
70
|
+
reserves_info[key] = {}
|
71
|
+
reserves_info[key]["tToken"] = value[8].lower()
|
72
|
+
reserves_info[key]["dToken"] = value[9].lower()
|
73
|
+
reserves_info[key]["sdToken"] = value[10].lower()
|
74
|
+
risk_param = bin(value[0][0])[2:]
|
75
|
+
reserves_info[key]["liquidationThreshold"] = int(risk_param[-31:-16], 2) / 10 ** 4
|
76
|
+
logger.info(f"Get reserves information in {time.time() - begin}s")
|
77
|
+
return reserves_info
|
78
|
+
|
79
|
+
def get_token_list(self):
|
80
|
+
begin = time.time()
|
81
|
+
tokens = [self.aave_info.get("poolToken")]
|
82
|
+
for token in self.aave_info.get("reservesList"):
|
83
|
+
if token == Token.native_token:
|
84
|
+
tokens.append(Token.wrapped_token.get(self.chain_id))
|
85
|
+
continue
|
86
|
+
tokens.append(token)
|
87
|
+
logger.info(f"Get token list related in {time.time()-begin}s")
|
88
|
+
return tokens
|
89
|
+
|
90
|
+
# REWARDS BALANCE
|
91
|
+
def get_claimable_rewards_balance_function_info(
|
92
|
+
self,
|
93
|
+
wallet_address: str,
|
94
|
+
block_number: int = "latest",
|
95
|
+
):
|
96
|
+
return {}
|
97
|
+
|
98
|
+
def calculate_claimable_rewards_balance(self, wallet_address: str, decoded_data: dict,
|
99
|
+
block_number: int = "latest"):
|
100
|
+
return {}
|
101
|
+
|
102
|
+
# WALLET DEPOSIT BORROW BALANCE
|
103
|
+
def get_wallet_deposit_borrow_balance_function_info(
|
104
|
+
self,
|
105
|
+
wallet_address: str,
|
106
|
+
reserves_info: dict,
|
107
|
+
block_number: int = "latest"
|
108
|
+
):
|
109
|
+
|
110
|
+
rpc_calls = {}
|
111
|
+
for token, value in reserves_info.items():
|
112
|
+
underlying = token
|
113
|
+
ctoken = value.get(self.market_key)
|
114
|
+
if token == Token.native_token:
|
115
|
+
underlying = Token.wrapped_token.get(self.chain_id)
|
116
|
+
underlying_borrow_key = f"borrowBalance_{self.name}_{ctoken}_{wallet_address}_{block_number}".lower()
|
117
|
+
underlying_balance_key = f"collateralBalance_{self.name}_{ctoken}_{wallet_address}_{block_number}".lower()
|
118
|
+
underlying_decimals_key = f"decimals_{underlying}_{block_number}".lower()
|
119
|
+
rpc_calls[underlying_borrow_key] = self.get_comptroller_function_info(
|
120
|
+
"borrowBalance", [token, wallet_address], block_number)
|
121
|
+
rpc_calls[underlying_balance_key] = self.get_comptroller_function_info(
|
122
|
+
"collateralBalance", [token, wallet_address], block_number)
|
123
|
+
rpc_calls[underlying_decimals_key] = self.state_service.get_function_info(
|
124
|
+
underlying, ERC20_ABI, "decimals", [], block_number
|
125
|
+
)
|
126
|
+
|
127
|
+
return rpc_calls
|
128
|
+
|
129
|
+
def calculate_wallet_deposit_borrow_balance(
|
130
|
+
self, wallet_address: str, reserves_info: dict, decoded_data: dict, token_prices: dict = None,
|
131
|
+
block_number: int = "latest"):
|
132
|
+
if token_prices is None:
|
133
|
+
token_prices = {}
|
134
|
+
result = {}
|
135
|
+
for token, value in reserves_info.items():
|
136
|
+
underlying = token
|
137
|
+
ctoken = value.get(self.market_key)
|
138
|
+
if token == Token.native_token:
|
139
|
+
underlying = Token.wrapped_token.get(self.chain_id)
|
140
|
+
get_total_deposit_id = f"collateralBalance_{self.name}_{ctoken}_{wallet_address}_{block_number}".lower()
|
141
|
+
get_total_borrow_id = f"borrowBalance_{self.name}_{ctoken}_{wallet_address}_{block_number}".lower()
|
142
|
+
get_decimals_id = f"decimals_{underlying}_{block_number}".lower()
|
143
|
+
decimals = decoded_data[get_decimals_id]
|
144
|
+
deposit_amount = decoded_data[get_total_deposit_id] / 10 ** decimals
|
145
|
+
borrow_amount = decoded_data[get_total_borrow_id] / 10 ** decimals
|
146
|
+
result[token] = {
|
147
|
+
"borrow_amount": borrow_amount,
|
148
|
+
"deposit_amount": deposit_amount,
|
149
|
+
}
|
150
|
+
if token_prices:
|
151
|
+
token_price = token_prices.get(underlying)
|
152
|
+
else:
|
153
|
+
token_price = None
|
154
|
+
if token_price is not None:
|
155
|
+
deposit_amount_in_usd = deposit_amount * token_price
|
156
|
+
borrow_amount_in_usd = borrow_amount * token_price
|
157
|
+
result[token]['borrow_amount_in_usd'] += borrow_amount_in_usd
|
158
|
+
result[token]['deposit_amount_in_usd'] += deposit_amount_in_usd
|
159
|
+
return result
|