@defisaver/positions-sdk 2.0.14 → 2.0.15-dev-2
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.
- package/.mocharc.json +4 -4
- package/.nvmrc +1 -1
- package/README.md +64 -64
- package/cjs/aaveV2/index.js +9 -5
- package/cjs/aaveV3/index.js +63 -46
- package/cjs/aaveV3/merit.d.ts +17 -0
- package/cjs/aaveV3/merit.js +95 -0
- package/cjs/aaveV3/merkl.d.ts +19 -0
- package/cjs/aaveV3/merkl.js +98 -0
- package/cjs/compoundV2/index.js +13 -7
- package/cjs/compoundV3/index.js +7 -2
- package/cjs/config/contracts.d.ts +6510 -1851
- package/cjs/config/contracts.js +33 -12
- package/cjs/contracts.d.ts +178 -0
- package/cjs/eulerV2/index.js +11 -2
- package/cjs/fluid/index.js +105 -34
- package/cjs/helpers/aaveHelpers/index.js +0 -1
- package/cjs/helpers/compoundHelpers/index.d.ts +3 -5
- package/cjs/helpers/compoundHelpers/index.js +15 -11
- package/cjs/helpers/eulerHelpers/index.d.ts +0 -5
- package/cjs/helpers/eulerHelpers/index.js +2 -31
- package/cjs/helpers/fluidHelpers/index.js +2 -0
- package/cjs/helpers/liquityV2Helpers/index.js +3 -2
- package/cjs/helpers/morphoBlueHelpers/index.js +66 -66
- package/cjs/liquityV2/index.js +10 -2
- package/cjs/llamaLend/index.js +10 -2
- package/cjs/morphoBlue/index.js +20 -6
- package/cjs/spark/index.js +20 -30
- package/cjs/staking/eligibility.d.ts +11 -0
- package/cjs/staking/eligibility.js +43 -0
- package/cjs/staking/index.d.ts +1 -0
- package/cjs/staking/index.js +1 -0
- package/cjs/staking/staking.d.ts +1 -7
- package/cjs/staking/staking.js +29 -55
- package/cjs/types/aave.d.ts +3 -8
- package/cjs/types/common.d.ts +16 -4
- package/cjs/types/common.js +10 -1
- package/cjs/types/euler.d.ts +3 -3
- package/cjs/types/fluid.d.ts +3 -5
- package/cjs/types/liquityV2.d.ts +3 -3
- package/cjs/types/llamaLend.d.ts +3 -1
- package/cjs/types/morphoBlue.d.ts +3 -5
- package/cjs/types/spark.d.ts +0 -3
- package/esm/aaveV2/index.js +9 -5
- package/esm/aaveV3/index.js +64 -47
- package/esm/aaveV3/merit.d.ts +17 -0
- package/esm/aaveV3/merit.js +90 -0
- package/esm/aaveV3/merkl.d.ts +19 -0
- package/esm/aaveV3/merkl.js +92 -0
- package/esm/compoundV2/index.js +13 -7
- package/esm/compoundV3/index.js +7 -2
- package/esm/config/contracts.d.ts +6510 -1851
- package/esm/config/contracts.js +33 -12
- package/esm/contracts.d.ts +178 -0
- package/esm/eulerV2/index.js +11 -2
- package/esm/fluid/index.js +106 -35
- package/esm/helpers/aaveHelpers/index.js +0 -1
- package/esm/helpers/compoundHelpers/index.d.ts +3 -5
- package/esm/helpers/compoundHelpers/index.js +16 -12
- package/esm/helpers/eulerHelpers/index.d.ts +0 -5
- package/esm/helpers/eulerHelpers/index.js +2 -30
- package/esm/helpers/fluidHelpers/index.js +2 -0
- package/esm/helpers/liquityV2Helpers/index.js +3 -2
- package/esm/helpers/morphoBlueHelpers/index.js +66 -66
- package/esm/liquityV2/index.js +11 -3
- package/esm/llamaLend/index.js +11 -3
- package/esm/morphoBlue/index.js +21 -7
- package/esm/spark/index.js +21 -31
- package/esm/staking/eligibility.d.ts +11 -0
- package/esm/staking/eligibility.js +36 -0
- package/esm/staking/index.d.ts +1 -0
- package/esm/staking/index.js +1 -0
- package/esm/staking/staking.d.ts +1 -7
- package/esm/staking/staking.js +28 -53
- package/esm/types/aave.d.ts +3 -8
- package/esm/types/common.d.ts +16 -4
- package/esm/types/common.js +9 -0
- package/esm/types/euler.d.ts +3 -3
- package/esm/types/fluid.d.ts +3 -5
- package/esm/types/liquityV2.d.ts +3 -3
- package/esm/types/llamaLend.d.ts +3 -1
- package/esm/types/morphoBlue.d.ts +3 -5
- package/esm/types/spark.d.ts +0 -3
- package/package.json +47 -47
- package/src/aaveV2/index.ts +239 -236
- package/src/aaveV3/index.ts +511 -488
- package/src/aaveV3/merit.ts +98 -0
- package/src/aaveV3/merkl.ts +141 -0
- package/src/compoundV2/index.ts +244 -240
- package/src/compoundV3/index.ts +274 -270
- package/src/config/contracts.ts +1129 -1108
- package/src/constants/index.ts +6 -6
- package/src/contracts.ts +107 -107
- package/src/curveUsd/index.ts +250 -250
- package/src/eulerV2/index.ts +324 -314
- package/src/exchange/index.ts +25 -25
- package/src/fluid/index.ts +1636 -1568
- package/src/helpers/aaveHelpers/index.ts +169 -170
- package/src/helpers/compoundHelpers/index.ts +267 -261
- package/src/helpers/curveUsdHelpers/index.ts +40 -40
- package/src/helpers/eulerHelpers/index.ts +222 -259
- package/src/helpers/fluidHelpers/index.ts +326 -324
- package/src/helpers/index.ts +10 -10
- package/src/helpers/liquityV2Helpers/index.ts +82 -80
- package/src/helpers/llamaLendHelpers/index.ts +53 -53
- package/src/helpers/makerHelpers/index.ts +52 -52
- package/src/helpers/morphoBlueHelpers/index.ts +390 -390
- package/src/helpers/sparkHelpers/index.ts +155 -155
- package/src/index.ts +45 -45
- package/src/liquity/index.ts +104 -104
- package/src/liquityV2/index.ts +418 -408
- package/src/llamaLend/index.ts +305 -296
- package/src/maker/index.ts +223 -223
- package/src/markets/aave/index.ts +116 -116
- package/src/markets/aave/marketAssets.ts +49 -49
- package/src/markets/compound/index.ts +227 -227
- package/src/markets/compound/marketsAssets.ts +90 -90
- package/src/markets/curveUsd/index.ts +69 -69
- package/src/markets/euler/index.ts +26 -26
- package/src/markets/fluid/index.ts +2456 -2456
- package/src/markets/index.ts +25 -25
- package/src/markets/liquityV2/index.ts +102 -102
- package/src/markets/llamaLend/contractAddresses.ts +141 -141
- package/src/markets/llamaLend/index.ts +235 -235
- package/src/markets/morphoBlue/index.ts +895 -895
- package/src/markets/spark/index.ts +29 -29
- package/src/markets/spark/marketAssets.ts +11 -11
- package/src/moneymarket/moneymarketCommonService.ts +80 -80
- package/src/morphoBlue/index.ts +236 -222
- package/src/portfolio/index.ts +285 -285
- package/src/services/priceService.ts +159 -159
- package/src/services/utils.ts +63 -63
- package/src/services/viem.ts +32 -32
- package/src/setup.ts +8 -8
- package/src/spark/index.ts +444 -456
- package/src/staking/eligibility.ts +37 -0
- package/src/staking/index.ts +2 -1
- package/src/staking/staking.ts +169 -194
- package/src/types/aave.ts +189 -194
- package/src/types/common.ts +103 -88
- package/src/types/compound.ts +136 -136
- package/src/types/curveUsd.ts +121 -121
- package/src/types/euler.ts +175 -174
- package/src/types/fluid.ts +448 -450
- package/src/types/index.ts +11 -11
- package/src/types/liquity.ts +30 -30
- package/src/types/liquityV2.ts +126 -126
- package/src/types/llamaLend.ts +159 -157
- package/src/types/maker.ts +63 -63
- package/src/types/morphoBlue.ts +194 -194
- package/src/types/portfolio.ts +60 -60
- package/src/types/spark.ts +135 -137
package/package.json
CHANGED
|
@@ -1,47 +1,47 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@defisaver/positions-sdk",
|
|
3
|
-
"version": "2.0.
|
|
4
|
-
"description": "",
|
|
5
|
-
"main": "./cjs/index.js",
|
|
6
|
-
"module": "./esm/index.js",
|
|
7
|
-
"types": "./esm/index.d.ts",
|
|
8
|
-
"scripts": {
|
|
9
|
-
"build:esm": "rm -rf esm && tsc -p tsconfig.esm.json",
|
|
10
|
-
"build:cjs": "rm -rf cjs && tsc -p tsconfig.cjs.json",
|
|
11
|
-
"build": "npm run lint && npm run build:cjs && npm run build:esm",
|
|
12
|
-
"dev": "tsc -p tsconfig.json --watch",
|
|
13
|
-
"lint": "eslint src/ --fix",
|
|
14
|
-
"lint-check": "eslint src/",
|
|
15
|
-
"test": "mocha tests/*",
|
|
16
|
-
"test-single": "mocha ./tests/$npm_config_name.ts",
|
|
17
|
-
"test:debugger": "mocha --inspect-brk tests/*",
|
|
18
|
-
"version-bump": "git commit -am \"Version bump to $(npm version patch | cut -c 2-)\""
|
|
19
|
-
},
|
|
20
|
-
"keywords": [],
|
|
21
|
-
"author": "",
|
|
22
|
-
"license": "ISC",
|
|
23
|
-
"dependencies": {
|
|
24
|
-
"@defisaver/tokens": "^1.7.1",
|
|
25
|
-
"@types/lodash": "^4.17.15",
|
|
26
|
-
"@types/memoizee": "^0.4.12",
|
|
27
|
-
"decimal.js": "^10.6.0",
|
|
28
|
-
"lodash": "^4.17.21",
|
|
29
|
-
"memoizee": "^0.4.17",
|
|
30
|
-
"viem": "^2.31.4"
|
|
31
|
-
},
|
|
32
|
-
"devDependencies": {
|
|
33
|
-
"@defisaver/eslint-config": "^1.0.1",
|
|
34
|
-
"@metamask/eth-json-rpc-middleware": "^15.0.1",
|
|
35
|
-
"@metamask/eth-json-rpc-provider": "^4.1.6",
|
|
36
|
-
"@types/chai": "^5.0.0",
|
|
37
|
-
"@types/mocha": "^10.0.9",
|
|
38
|
-
"chai": "^4.3.8",
|
|
39
|
-
"dotenv": "^16.3.1",
|
|
40
|
-
"eslint": "^8.49.0",
|
|
41
|
-
"eslint-plugin-import": "^2.31.0",
|
|
42
|
-
"mocha": "^10.2.0",
|
|
43
|
-
"nock": "^14.0.0",
|
|
44
|
-
"ts-node": "^10.9.2",
|
|
45
|
-
"typescript": "^5.2.2"
|
|
46
|
-
}
|
|
47
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@defisaver/positions-sdk",
|
|
3
|
+
"version": "2.0.15-dev-2",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "./cjs/index.js",
|
|
6
|
+
"module": "./esm/index.js",
|
|
7
|
+
"types": "./esm/index.d.ts",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build:esm": "rm -rf esm && tsc -p tsconfig.esm.json",
|
|
10
|
+
"build:cjs": "rm -rf cjs && tsc -p tsconfig.cjs.json",
|
|
11
|
+
"build": "npm run lint && npm run build:cjs && npm run build:esm",
|
|
12
|
+
"dev": "tsc -p tsconfig.json --watch",
|
|
13
|
+
"lint": "eslint src/ --fix",
|
|
14
|
+
"lint-check": "eslint src/",
|
|
15
|
+
"test": "mocha tests/*",
|
|
16
|
+
"test-single": "mocha ./tests/$npm_config_name.ts",
|
|
17
|
+
"test:debugger": "mocha --inspect-brk tests/*",
|
|
18
|
+
"version-bump": "git commit -am \"Version bump to $(npm version patch | cut -c 2-)\""
|
|
19
|
+
},
|
|
20
|
+
"keywords": [],
|
|
21
|
+
"author": "",
|
|
22
|
+
"license": "ISC",
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@defisaver/tokens": "^1.7.1",
|
|
25
|
+
"@types/lodash": "^4.17.15",
|
|
26
|
+
"@types/memoizee": "^0.4.12",
|
|
27
|
+
"decimal.js": "^10.6.0",
|
|
28
|
+
"lodash": "^4.17.21",
|
|
29
|
+
"memoizee": "^0.4.17",
|
|
30
|
+
"viem": "^2.31.4"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@defisaver/eslint-config": "^1.0.1",
|
|
34
|
+
"@metamask/eth-json-rpc-middleware": "^15.0.1",
|
|
35
|
+
"@metamask/eth-json-rpc-provider": "^4.1.6",
|
|
36
|
+
"@types/chai": "^5.0.0",
|
|
37
|
+
"@types/mocha": "^10.0.9",
|
|
38
|
+
"chai": "^4.3.8",
|
|
39
|
+
"dotenv": "^16.3.1",
|
|
40
|
+
"eslint": "^8.49.0",
|
|
41
|
+
"eslint-plugin-import": "^2.31.0",
|
|
42
|
+
"mocha": "^10.2.0",
|
|
43
|
+
"nock": "^14.0.0",
|
|
44
|
+
"ts-node": "^10.9.2",
|
|
45
|
+
"typescript": "^5.2.2"
|
|
46
|
+
}
|
|
47
|
+
}
|
package/src/aaveV2/index.ts
CHANGED
|
@@ -1,236 +1,239 @@
|
|
|
1
|
-
import Dec from 'decimal.js';
|
|
2
|
-
import { assetAmountInEth, getAssetInfo } from '@defisaver/tokens';
|
|
3
|
-
import { Client } from 'viem';
|
|
4
|
-
import {
|
|
5
|
-
Blockish, EthAddress, EthereumProvider, NetworkNumber, PositionBalances,
|
|
6
|
-
} from '../types/common';
|
|
7
|
-
import { calculateNetApy, getStakingApy } from '../staking';
|
|
8
|
-
import { ethToWeth, wethToEth, wethToEthByAddress } from '../services/utils';
|
|
9
|
-
import { AaveLoanInfoV2ContractViem, createViemContractFromConfigFunc } from '../contracts';
|
|
10
|
-
import { aprToApy, calculateBorrowingAssetLimit } from '../moneymarket';
|
|
11
|
-
import {
|
|
12
|
-
AaveMarketInfo, AaveV2AssetData, AaveV2AssetsData, AaveV2PositionData, AaveV2UsedAsset, AaveV2UsedAssets,
|
|
13
|
-
} from '../types';
|
|
14
|
-
import { EMPTY_AAVE_DATA } from '../aaveV3';
|
|
15
|
-
import { AAVE_V2 } from '../markets/aave';
|
|
16
|
-
import { aaveAnyGetAggregatedPositionData } from '../helpers/aaveHelpers';
|
|
17
|
-
import { getEthPrice } from '../services/priceService';
|
|
18
|
-
import { getViemProvider, setViemBlockNumber } from '../services/viem';
|
|
19
|
-
|
|
20
|
-
export const _getAaveV2MarketsData = async (provider: Client, network: NetworkNumber, selectedMarket: AaveMarketInfo) => {
|
|
21
|
-
const _addresses = selectedMarket.assets.map(a => getAssetInfo(ethToWeth(a)).address);
|
|
22
|
-
const loanInfoContract = AaveLoanInfoV2ContractViem(provider, network);
|
|
23
|
-
const marketAddress = selectedMarket.providerAddress;
|
|
24
|
-
const [loanInfo, ethPrice] = await Promise.all([
|
|
25
|
-
loanInfoContract.read.getFullTokensInfo([marketAddress, _addresses as EthAddress[]]),
|
|
26
|
-
getEthPrice(provider),
|
|
27
|
-
]);
|
|
28
|
-
const markets = loanInfo
|
|
29
|
-
.map((market, i) => ({
|
|
30
|
-
symbol: selectedMarket.assets[i],
|
|
31
|
-
underlyingTokenAddress: market.underlyingTokenAddress,
|
|
32
|
-
supplyRate: aprToApy(new Dec(market.supplyRate.toString()).div(1e25).toString()),
|
|
33
|
-
borrowRate: aprToApy(new Dec(market.borrowRateVariable.toString()).div(1e25).toString()),
|
|
34
|
-
borrowRateStable: aprToApy(new Dec(market.borrowRateStable.toString()).div(1e25).toString()),
|
|
35
|
-
collateralFactor: new Dec(market.collateralFactor.toString()).div(10000).toString(),
|
|
36
|
-
liquidationRatio: new Dec(market.liquidationRatio.toString()).div(10000).toString(),
|
|
37
|
-
marketLiquidity: assetAmountInEth(new Dec(market.totalSupply.toString())
|
|
38
|
-
.sub(market.totalBorrow.toString())
|
|
39
|
-
.toString(), selectedMarket.assets[i]),
|
|
40
|
-
utilization: new Dec(market.totalBorrow.toString())
|
|
41
|
-
.div(new Dec(market.totalSupply.toString()))
|
|
42
|
-
.times(100)
|
|
43
|
-
.toString(),
|
|
44
|
-
usageAsCollateralEnabled: market.usageAsCollateralEnabled,
|
|
45
|
-
supplyCap: '0',
|
|
46
|
-
borrowCap: '0', // v2 doesnt have borrow cap but adding it for compatability with v3
|
|
47
|
-
totalSupply: assetAmountInEth(market.totalSupply.toString(), selectedMarket.assets[i]),
|
|
48
|
-
isInactive: !market.isActive,
|
|
49
|
-
isFrozen: market.isFrozen,
|
|
50
|
-
canBeBorrowed: market.isActive && market.borrowingEnabled && !market.isFrozen,
|
|
51
|
-
canBeSupplied: market.isActive && !market.isFrozen,
|
|
52
|
-
canBeWithdrawn: market.isActive,
|
|
53
|
-
canBePayBacked: market.isActive,
|
|
54
|
-
disabledStableBorrowing: !market.stableBorrowRateEnabled,
|
|
55
|
-
totalBorrow: assetAmountInEth(market.totalBorrow.toString(), selectedMarket.assets[i]),
|
|
56
|
-
totalBorrowVar: assetAmountInEth(market.totalBorrowVar.toString(), selectedMarket.assets[i]),
|
|
57
|
-
priceInEth: new Dec(market.price.toString()).div(1e18).toString(),
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
})
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
};
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
const
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
const
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
};
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
const
|
|
151
|
-
const
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
const
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
const
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
payload.
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
1
|
+
import Dec from 'decimal.js';
|
|
2
|
+
import { assetAmountInEth, getAssetInfo } from '@defisaver/tokens';
|
|
3
|
+
import { Client } from 'viem';
|
|
4
|
+
import {
|
|
5
|
+
Blockish, EthAddress, EthereumProvider, IncentiveKind, NetworkNumber, PositionBalances,
|
|
6
|
+
} from '../types/common';
|
|
7
|
+
import { calculateNetApy, getStakingApy } from '../staking';
|
|
8
|
+
import { ethToWeth, wethToEth, wethToEthByAddress } from '../services/utils';
|
|
9
|
+
import { AaveLoanInfoV2ContractViem, createViemContractFromConfigFunc } from '../contracts';
|
|
10
|
+
import { aprToApy, calculateBorrowingAssetLimit } from '../moneymarket';
|
|
11
|
+
import {
|
|
12
|
+
AaveMarketInfo, AaveV2AssetData, AaveV2AssetsData, AaveV2PositionData, AaveV2UsedAsset, AaveV2UsedAssets,
|
|
13
|
+
} from '../types';
|
|
14
|
+
import { EMPTY_AAVE_DATA } from '../aaveV3';
|
|
15
|
+
import { AAVE_V2 } from '../markets/aave';
|
|
16
|
+
import { aaveAnyGetAggregatedPositionData } from '../helpers/aaveHelpers';
|
|
17
|
+
import { getEthPrice } from '../services/priceService';
|
|
18
|
+
import { getViemProvider, setViemBlockNumber } from '../services/viem';
|
|
19
|
+
|
|
20
|
+
export const _getAaveV2MarketsData = async (provider: Client, network: NetworkNumber, selectedMarket: AaveMarketInfo) => {
|
|
21
|
+
const _addresses = selectedMarket.assets.map(a => getAssetInfo(ethToWeth(a)).address);
|
|
22
|
+
const loanInfoContract = AaveLoanInfoV2ContractViem(provider, network);
|
|
23
|
+
const marketAddress = selectedMarket.providerAddress;
|
|
24
|
+
const [loanInfo, ethPrice] = await Promise.all([
|
|
25
|
+
loanInfoContract.read.getFullTokensInfo([marketAddress, _addresses as EthAddress[]]),
|
|
26
|
+
getEthPrice(provider),
|
|
27
|
+
]);
|
|
28
|
+
const markets: AaveV2AssetData[] = loanInfo
|
|
29
|
+
.map((market, i) => ({
|
|
30
|
+
symbol: selectedMarket.assets[i],
|
|
31
|
+
underlyingTokenAddress: market.underlyingTokenAddress,
|
|
32
|
+
supplyRate: aprToApy(new Dec(market.supplyRate.toString()).div(1e25).toString()),
|
|
33
|
+
borrowRate: aprToApy(new Dec(market.borrowRateVariable.toString()).div(1e25).toString()),
|
|
34
|
+
borrowRateStable: aprToApy(new Dec(market.borrowRateStable.toString()).div(1e25).toString()),
|
|
35
|
+
collateralFactor: new Dec(market.collateralFactor.toString()).div(10000).toString(),
|
|
36
|
+
liquidationRatio: new Dec(market.liquidationRatio.toString()).div(10000).toString(),
|
|
37
|
+
marketLiquidity: assetAmountInEth(new Dec(market.totalSupply.toString())
|
|
38
|
+
.sub(market.totalBorrow.toString())
|
|
39
|
+
.toString(), selectedMarket.assets[i]),
|
|
40
|
+
utilization: new Dec(market.totalBorrow.toString())
|
|
41
|
+
.div(new Dec(market.totalSupply.toString()))
|
|
42
|
+
.times(100)
|
|
43
|
+
.toString(),
|
|
44
|
+
usageAsCollateralEnabled: market.usageAsCollateralEnabled,
|
|
45
|
+
supplyCap: '0',
|
|
46
|
+
borrowCap: '0', // v2 doesnt have borrow cap but adding it for compatability with v3
|
|
47
|
+
totalSupply: assetAmountInEth(market.totalSupply.toString(), selectedMarket.assets[i]),
|
|
48
|
+
isInactive: !market.isActive,
|
|
49
|
+
isFrozen: market.isFrozen,
|
|
50
|
+
canBeBorrowed: market.isActive && market.borrowingEnabled && !market.isFrozen,
|
|
51
|
+
canBeSupplied: market.isActive && !market.isFrozen,
|
|
52
|
+
canBeWithdrawn: market.isActive,
|
|
53
|
+
canBePayBacked: market.isActive,
|
|
54
|
+
disabledStableBorrowing: !market.stableBorrowRateEnabled,
|
|
55
|
+
totalBorrow: assetAmountInEth(market.totalBorrow.toString(), selectedMarket.assets[i]),
|
|
56
|
+
totalBorrowVar: assetAmountInEth(market.totalBorrowVar.toString(), selectedMarket.assets[i]),
|
|
57
|
+
priceInEth: new Dec(market.price.toString()).div(1e18).toString(),
|
|
58
|
+
supplyIncentives: [],
|
|
59
|
+
borrowIncentives: [],
|
|
60
|
+
price: new Dec(market.price.toString()).div(1e18).mul(ethPrice).toString(),
|
|
61
|
+
}));
|
|
62
|
+
|
|
63
|
+
const stEthMarket = markets.find(({ symbol }) => symbol === 'stETH');
|
|
64
|
+
if (stEthMarket) {
|
|
65
|
+
stEthMarket.supplyIncentives.push({
|
|
66
|
+
apy: await getStakingApy('stETH'),
|
|
67
|
+
token: 'stETH',
|
|
68
|
+
incentiveKind: IncentiveKind.Staking,
|
|
69
|
+
description: 'Native stETH yield.',
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const payload: AaveV2AssetsData = {};
|
|
74
|
+
// Sort by market size
|
|
75
|
+
markets
|
|
76
|
+
.sort((a, b) => {
|
|
77
|
+
const aMarket = new Dec(a.price).times(a.totalSupply).toString();
|
|
78
|
+
const bMarket = new Dec(b.price).times(b.totalSupply).toString();
|
|
79
|
+
|
|
80
|
+
return new Dec(bMarket).minus(aMarket).toNumber();
|
|
81
|
+
})
|
|
82
|
+
.forEach((assetData: AaveV2AssetData, i) => {
|
|
83
|
+
payload[assetData.symbol] = { ...assetData, sortIndex: i };
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
return { assetsData: payload };
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
export const getAaveV2MarketsData = async (provider: EthereumProvider, network: NetworkNumber, selectedMarket: AaveMarketInfo) => _getAaveV2MarketsData(getViemProvider(provider, network), network, selectedMarket);
|
|
90
|
+
|
|
91
|
+
export const _getAaveV2AccountBalances = async (provider: Client, network: NetworkNumber, block: Blockish, addressMapping: boolean, address: EthAddress): Promise<PositionBalances> => {
|
|
92
|
+
let balances: PositionBalances = {
|
|
93
|
+
collateral: {},
|
|
94
|
+
debt: {},
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
if (!address) {
|
|
98
|
+
return balances;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const market = AAVE_V2;
|
|
102
|
+
|
|
103
|
+
const loanInfoContract = AaveLoanInfoV2ContractViem(provider, network, block);
|
|
104
|
+
|
|
105
|
+
const marketAddress = market.providerAddress;
|
|
106
|
+
// @ts-ignore
|
|
107
|
+
const protocolDataProviderContract = createViemContractFromConfigFunc(market.protocolData, market.protocolDataAddress)(provider, network);
|
|
108
|
+
|
|
109
|
+
const reserveTokens = await protocolDataProviderContract.read.getAllReservesTokens(setViemBlockNumber(block));
|
|
110
|
+
const symbols = reserveTokens.map(({ symbol }: { symbol: string }) => symbol);
|
|
111
|
+
const _addresses = reserveTokens.map(({ tokenAddress }: { tokenAddress: EthAddress }) => tokenAddress);
|
|
112
|
+
|
|
113
|
+
const loanInfo = await loanInfoContract.read.getTokenBalances([marketAddress, address, _addresses as EthAddress[]], setViemBlockNumber(block));
|
|
114
|
+
|
|
115
|
+
loanInfo.forEach((_tokenInfo: any, i: number) => {
|
|
116
|
+
const asset = wethToEth(symbols[i]);
|
|
117
|
+
const assetAddr = wethToEthByAddress(_addresses[i], network).toLowerCase();
|
|
118
|
+
const tokenInfo = { ..._tokenInfo };
|
|
119
|
+
|
|
120
|
+
// known bug: stETH leaves 1 wei on every transfer
|
|
121
|
+
if (asset === 'stETH' && tokenInfo.balance.toString() === '1') {
|
|
122
|
+
tokenInfo.balance = '0';
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
balances = {
|
|
126
|
+
collateral: {
|
|
127
|
+
...balances.collateral,
|
|
128
|
+
[addressMapping ? assetAddr : asset]: tokenInfo.balance.toString(),
|
|
129
|
+
},
|
|
130
|
+
debt: {
|
|
131
|
+
...balances.debt,
|
|
132
|
+
[addressMapping ? assetAddr : asset]: new Dec(tokenInfo.borrowsStable.toString()).add(tokenInfo.borrowsVariable.toString()).toString(),
|
|
133
|
+
},
|
|
134
|
+
};
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
return balances;
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
export const getAaveV2AccountBalances = async (provider: EthereumProvider, network: NetworkNumber, block: Blockish, addressMapping: boolean, address: EthAddress): Promise<PositionBalances> => _getAaveV2AccountBalances(getViemProvider(provider, network), network, block, addressMapping, address);
|
|
141
|
+
|
|
142
|
+
export const _getAaveV2AccountData = async (provider: Client, network: NetworkNumber, address: EthAddress, assetsData: AaveV2AssetsData, market: AaveMarketInfo): Promise<AaveV2PositionData> => {
|
|
143
|
+
if (!address) throw new Error('Address is required');
|
|
144
|
+
|
|
145
|
+
let payload: AaveV2PositionData = {
|
|
146
|
+
...EMPTY_AAVE_DATA,
|
|
147
|
+
lastUpdated: Date.now(),
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
const loanInfoContract = AaveLoanInfoV2ContractViem(provider, network);
|
|
151
|
+
const marketAddress = market.providerAddress;
|
|
152
|
+
const _addresses = market.assets.map(a => getAssetInfo(ethToWeth(a)).address);
|
|
153
|
+
const loanInfo = await loanInfoContract.read.getTokenBalances([marketAddress, address, _addresses as EthAddress[]]);
|
|
154
|
+
const usedAssets = {} as AaveV2UsedAssets;
|
|
155
|
+
loanInfo.forEach((_tokenInfo, i) => {
|
|
156
|
+
const asset = market.assets[i];
|
|
157
|
+
const tokenInfo = { ..._tokenInfo };
|
|
158
|
+
|
|
159
|
+
// known bug: stETH leaves 1 wei on every transfer
|
|
160
|
+
if (asset === 'stETH' && tokenInfo.balance.toString() === '1') tokenInfo.balance = BigInt('0');
|
|
161
|
+
|
|
162
|
+
const isSupplied = tokenInfo.balance.toString() !== '0';
|
|
163
|
+
const isBorrowed = tokenInfo.borrowsStable.toString() !== '0' || tokenInfo.borrowsVariable.toString() !== '0';
|
|
164
|
+
if (!isSupplied && !isBorrowed) return;
|
|
165
|
+
|
|
166
|
+
const supplied = assetAmountInEth(tokenInfo.balance.toString(), asset);
|
|
167
|
+
const borrowedStable = assetAmountInEth(tokenInfo.borrowsStable.toString(), asset);
|
|
168
|
+
const borrowedVariable = assetAmountInEth(tokenInfo.borrowsVariable.toString(), asset);
|
|
169
|
+
const enabledAsCollateral = assetsData[asset].usageAsCollateralEnabled ? tokenInfo.enabledAsCollateral : false;
|
|
170
|
+
let interestMode;
|
|
171
|
+
if (borrowedVariable === '0' && borrowedStable !== '0') {
|
|
172
|
+
interestMode = '1';
|
|
173
|
+
} else if (borrowedVariable !== '0' && borrowedStable === '0') {
|
|
174
|
+
interestMode = '2';
|
|
175
|
+
} else {
|
|
176
|
+
interestMode = 'both';
|
|
177
|
+
}
|
|
178
|
+
if (!usedAssets[asset]) usedAssets[asset] = {} as AaveV2UsedAsset;
|
|
179
|
+
usedAssets[asset] = {
|
|
180
|
+
...usedAssets[asset],
|
|
181
|
+
symbol: asset,
|
|
182
|
+
supplied,
|
|
183
|
+
suppliedUsd: new Dec(supplied).mul(assetsData[asset].price).toString(),
|
|
184
|
+
isSupplied,
|
|
185
|
+
collateral: enabledAsCollateral,
|
|
186
|
+
stableBorrowRate: aprToApy(new Dec(tokenInfo.stableBorrowRate).div(1e25).toString()),
|
|
187
|
+
borrowedStable,
|
|
188
|
+
borrowedVariable,
|
|
189
|
+
borrowedUsdStable: new Dec(borrowedStable).mul(assetsData[asset].price).toString(),
|
|
190
|
+
borrowedUsdVariable: new Dec(borrowedVariable).mul(assetsData[asset].price).toString(),
|
|
191
|
+
borrowed: new Dec(borrowedStable).add(borrowedVariable).toString(),
|
|
192
|
+
borrowedUsd: new Dec(new Dec(borrowedVariable).add(borrowedStable)).mul(assetsData[asset].price).toString(),
|
|
193
|
+
isBorrowed,
|
|
194
|
+
interestMode,
|
|
195
|
+
};
|
|
196
|
+
});
|
|
197
|
+
payload = {
|
|
198
|
+
...payload,
|
|
199
|
+
usedAssets,
|
|
200
|
+
...aaveAnyGetAggregatedPositionData({
|
|
201
|
+
usedAssets, assetsData, eModeCategory: 0, selectedMarket: market,
|
|
202
|
+
}),
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
payload.ratio = payload.borrowedUsd && payload.borrowedUsd !== '0'
|
|
206
|
+
? new Dec(payload.borrowLimitUsd).div(payload.borrowedUsd).mul(100).toString()
|
|
207
|
+
: '0';
|
|
208
|
+
payload.minRatio = '100';
|
|
209
|
+
payload.collRatio = payload.borrowedUsd && payload.borrowedUsd !== '0'
|
|
210
|
+
? new Dec(payload.suppliedCollateralUsd).div(payload.borrowedUsd).mul(100).toString()
|
|
211
|
+
: '0';
|
|
212
|
+
|
|
213
|
+
// Calculate borrow limits per asset
|
|
214
|
+
Object.values(payload.usedAssets).forEach((item) => {
|
|
215
|
+
if (item.isBorrowed) {
|
|
216
|
+
// eslint-disable-next-line no-param-reassign
|
|
217
|
+
item.stableLimit = calculateBorrowingAssetLimit(item.borrowedUsdStable, payload.borrowLimitUsd);
|
|
218
|
+
// eslint-disable-next-line no-param-reassign
|
|
219
|
+
item.variableLimit = calculateBorrowingAssetLimit(item.borrowedUsdVariable, payload.borrowLimitUsd);
|
|
220
|
+
// eslint-disable-next-line no-param-reassign
|
|
221
|
+
item.limit = calculateBorrowingAssetLimit(item.borrowedUsd, payload.borrowLimitUsd);
|
|
222
|
+
}
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
const { netApy, incentiveUsd, totalInterestUsd } = calculateNetApy({ usedAssets, assetsData });
|
|
226
|
+
payload.netApy = netApy;
|
|
227
|
+
payload.incentiveUsd = incentiveUsd;
|
|
228
|
+
payload.totalInterestUsd = totalInterestUsd;
|
|
229
|
+
|
|
230
|
+
return payload;
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
export const getAaveV2AccountData = async (provider: EthereumProvider, network: NetworkNumber, address: EthAddress, assetsData: AaveV2AssetsData, market: AaveMarketInfo): Promise<AaveV2PositionData> => _getAaveV2AccountData(getViemProvider(provider, network), network, address, assetsData, market);
|
|
234
|
+
|
|
235
|
+
export const getAaveV2FullPositionData = async (provider: EthereumProvider, network: NetworkNumber, address: EthAddress, market: AaveMarketInfo): Promise<AaveV2PositionData> => {
|
|
236
|
+
const marketData = await getAaveV2MarketsData(provider, network, market);
|
|
237
|
+
const positionData = await getAaveV2AccountData(provider, network, address, marketData.assetsData, market);
|
|
238
|
+
return positionData;
|
|
239
|
+
};
|