@defisaver/positions-sdk 2.1.9-dev-spark-1 → 2.1.9-fluid-plasma
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/CLAUDE.md +32 -0
- package/README.md +64 -64
- package/cjs/config/contracts.d.ts +12 -0
- package/cjs/config/contracts.js +7 -3
- package/cjs/fluid/index.js +32 -11
- package/cjs/helpers/morphoBlueHelpers/index.js +66 -66
- package/cjs/helpers/sparkHelpers/index.d.ts +4 -4
- package/cjs/helpers/sparkHelpers/index.js +12 -12
- package/cjs/markets/fluid/index.d.ts +46 -0
- package/cjs/markets/fluid/index.js +436 -1
- package/cjs/portfolio/index.js +1 -1
- package/cjs/services/priceService.js +1 -1
- package/cjs/spark/index.d.ts +1 -3
- package/cjs/spark/index.js +14 -24
- package/cjs/types/fluid.d.ts +30 -1
- package/cjs/types/fluid.js +31 -1
- package/cjs/types/spark.d.ts +1 -3
- package/esm/config/contracts.d.ts +12 -0
- package/esm/config/contracts.js +7 -3
- package/esm/fluid/index.js +32 -11
- package/esm/helpers/morphoBlueHelpers/index.js +66 -66
- package/esm/helpers/sparkHelpers/index.d.ts +4 -4
- package/esm/helpers/sparkHelpers/index.js +13 -13
- package/esm/markets/fluid/index.d.ts +46 -0
- package/esm/markets/fluid/index.js +415 -1
- package/esm/portfolio/index.js +1 -1
- package/esm/services/priceService.js +2 -2
- package/esm/spark/index.d.ts +1 -3
- package/esm/spark/index.js +15 -25
- package/esm/types/fluid.d.ts +30 -1
- package/esm/types/fluid.js +30 -0
- package/esm/types/spark.d.ts +1 -3
- package/package.json +47 -47
- package/src/aaveV2/index.ts +240 -240
- package/src/aaveV3/index.ts +614 -614
- package/src/aaveV3/merit.ts +97 -97
- package/src/aaveV3/merkl.ts +74 -74
- package/src/claiming/aaveV3.ts +154 -154
- package/src/claiming/compV3.ts +22 -22
- package/src/claiming/index.ts +12 -12
- package/src/claiming/king.ts +66 -66
- package/src/claiming/morphoBlue.ts +118 -118
- package/src/claiming/spark.ts +225 -225
- package/src/compoundV2/index.ts +244 -244
- package/src/compoundV3/index.ts +274 -274
- package/src/config/contracts.ts +1255 -1251
- package/src/constants/index.ts +10 -10
- package/src/contracts.ts +120 -120
- package/src/curveUsd/index.ts +254 -254
- package/src/eulerV2/index.ts +324 -324
- package/src/exchange/index.ts +25 -25
- package/src/fluid/index.ts +1693 -1668
- package/src/helpers/aaveHelpers/index.ts +187 -187
- package/src/helpers/compoundHelpers/index.ts +283 -283
- package/src/helpers/curveUsdHelpers/index.ts +40 -40
- package/src/helpers/eulerHelpers/index.ts +222 -222
- package/src/helpers/fluidHelpers/index.ts +326 -326
- package/src/helpers/index.ts +10 -10
- package/src/helpers/liquityV2Helpers/index.ts +82 -82
- package/src/helpers/llamaLendHelpers/index.ts +53 -53
- package/src/helpers/makerHelpers/index.ts +52 -52
- package/src/helpers/morphoBlueHelpers/index.ts +396 -396
- package/src/helpers/sparkHelpers/index.ts +155 -158
- package/src/index.ts +47 -47
- package/src/liquity/index.ts +159 -159
- package/src/liquityV2/index.ts +657 -657
- package/src/llamaLend/index.ts +305 -305
- package/src/maker/index.ts +223 -223
- package/src/markets/aave/index.ts +116 -116
- package/src/markets/aave/marketAssets.ts +54 -54
- package/src/markets/compound/index.ts +238 -238
- package/src/markets/compound/marketsAssets.ts +97 -97
- package/src/markets/curveUsd/index.ts +69 -69
- package/src/markets/euler/index.ts +26 -26
- package/src/markets/fluid/index.ts +2900 -2460
- 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 +12 -12
- package/src/moneymarket/moneymarketCommonService.ts +80 -80
- package/src/morphoBlue/index.ts +274 -274
- package/src/portfolio/index.ts +570 -570
- package/src/services/priceService.ts +159 -159
- package/src/services/utils.ts +115 -115
- package/src/services/viem.ts +34 -34
- package/src/setup.ts +8 -8
- package/src/spark/index.ts +445 -465
- package/src/staking/eligibility.ts +53 -53
- package/src/staking/index.ts +1 -1
- package/src/staking/staking.ts +170 -170
- package/src/types/aave.ts +189 -189
- package/src/types/claiming.ts +109 -109
- package/src/types/common.ts +107 -107
- package/src/types/compound.ts +136 -136
- package/src/types/curveUsd.ts +123 -123
- package/src/types/euler.ts +175 -175
- package/src/types/fluid.ts +483 -452
- package/src/types/index.ts +13 -13
- package/src/types/liquity.ts +30 -30
- package/src/types/liquityV2.ts +126 -126
- package/src/types/llamaLend.ts +159 -159
- package/src/types/maker.ts +63 -63
- package/src/types/merit.ts +1 -1
- package/src/types/merkl.ts +70 -70
- package/src/types/morphoBlue.ts +194 -194
- package/src/types/portfolio.ts +60 -60
- package/src/types/spark.ts +135 -136
- package/src/umbrella/index.ts +69 -69
- package/src/umbrella/umbrellaUtils.ts +29 -29
package/esm/types/fluid.js
CHANGED
|
@@ -125,6 +125,30 @@ export var FluidBaseVersions;
|
|
|
125
125
|
FluidBaseVersions["FLUID_LBTC_USDC_21_BASE"] = "FLUID_LBTC_USDC_21_BASE";
|
|
126
126
|
FluidBaseVersions["FLUID_LBTC_SUSDS_22_BASE"] = "FLUID_LBTC_SUSDS_22_BASE";
|
|
127
127
|
})(FluidBaseVersions || (FluidBaseVersions = {}));
|
|
128
|
+
export var FluidPlasmaVersions;
|
|
129
|
+
(function (FluidPlasmaVersions) {
|
|
130
|
+
FluidPlasmaVersions["FLUID_ETH_USDT_1_PLASMA"] = "FLUID_ETH_USDT_1_PLASMA";
|
|
131
|
+
FluidPlasmaVersions["FLUID_ETH_USDE_2_PLASMA"] = "FLUID_ETH_USDE_2_PLASMA";
|
|
132
|
+
FluidPlasmaVersions["FLUID_WEETH_ETH_3_PLASMA"] = "FLUID_WEETH_ETH_3_PLASMA";
|
|
133
|
+
FluidPlasmaVersions["FLUID_SUSDE_USDT_4_PLASMA"] = "FLUID_SUSDE_USDT_4_PLASMA";
|
|
134
|
+
FluidPlasmaVersions["FLUID_WEETH_USDT_5_PLASMA"] = "FLUID_WEETH_USDT_5_PLASMA";
|
|
135
|
+
FluidPlasmaVersions["FLUID_WEETH_USDE_6_PLASMA"] = "FLUID_WEETH_USDE_6_PLASMA";
|
|
136
|
+
FluidPlasmaVersions["FLUID_XAUT_USDT_7_PLASMA"] = "FLUID_XAUT_USDT_7_PLASMA";
|
|
137
|
+
FluidPlasmaVersions["FLUID_XAUT_USDE_8_PLASMA"] = "FLUID_XAUT_USDE_8_PLASMA";
|
|
138
|
+
FluidPlasmaVersions["FLUID_USDE_USDT_9_PLASMA"] = "FLUID_USDE_USDT_9_PLASMA";
|
|
139
|
+
FluidPlasmaVersions["FLUID_USDE_USDT_10_PLASMA"] = "FLUID_USDE_USDT_10_PLASMA";
|
|
140
|
+
FluidPlasmaVersions["FLUID_WEETH_ETH_ETH_11_PLASMA"] = "FLUID_WEETH_ETH_ETH_11_PLASMA";
|
|
141
|
+
FluidPlasmaVersions["FLUID_SUSDE_USDT_USDT_12_PLASMA"] = "FLUID_SUSDE_USDT_USDT_12_PLASMA";
|
|
142
|
+
FluidPlasmaVersions["FLUID_USDE_USDT_USDT_13_PLASMA"] = "FLUID_USDE_USDT_USDT_13_PLASMA";
|
|
143
|
+
FluidPlasmaVersions["FLUID_USDAI_USDT_USDT_14_PLASMA"] = "FLUID_USDAI_USDT_USDT_14_PLASMA";
|
|
144
|
+
FluidPlasmaVersions["FLUID_USDT_SYRUPUSDT_USDT_15_PLASMA"] = "FLUID_USDT_SYRUPUSDT_USDT_15_PLASMA";
|
|
145
|
+
FluidPlasmaVersions["FLUID_XPL_USDT_16_PLASMA"] = "FLUID_XPL_USDT_16_PLASMA";
|
|
146
|
+
FluidPlasmaVersions["FLUID_XPL_USDE_17_PLASMA"] = "FLUID_XPL_USDE_17_PLASMA";
|
|
147
|
+
FluidPlasmaVersions["FLUID_WSTUSR_USDT_18_PLASMA"] = "FLUID_WSTUSR_USDT_18_PLASMA";
|
|
148
|
+
FluidPlasmaVersions["FLUID_WSTUSR_USDT_USDT_19_PLASMA"] = "FLUID_WSTUSR_USDT_USDT_19_PLASMA";
|
|
149
|
+
FluidPlasmaVersions["FLUID_SYRUPUSDT_USDT_20_PLASMA"] = "FLUID_SYRUPUSDT_USDT_20_PLASMA";
|
|
150
|
+
FluidPlasmaVersions["FLUID_ETH_WRSETH_ETH_21_PLASMA"] = "FLUID_ETH_WRSETH_ETH_21_PLASMA";
|
|
151
|
+
})(FluidPlasmaVersions || (FluidPlasmaVersions = {}));
|
|
128
152
|
export var FluidMainnetDepositToken;
|
|
129
153
|
(function (FluidMainnetDepositToken) {
|
|
130
154
|
FluidMainnetDepositToken["ETH"] = "ETH";
|
|
@@ -154,6 +178,12 @@ export var FluidBaseDepositToken;
|
|
|
154
178
|
FluidBaseDepositToken["sUSDS"] = "sUSDS";
|
|
155
179
|
FluidBaseDepositToken["GHO"] = "GHO";
|
|
156
180
|
})(FluidBaseDepositToken || (FluidBaseDepositToken = {}));
|
|
181
|
+
export var FluidPlasmaDepositToken;
|
|
182
|
+
(function (FluidPlasmaDepositToken) {
|
|
183
|
+
FluidPlasmaDepositToken["USDe"] = "USDe";
|
|
184
|
+
FluidPlasmaDepositToken["USDT"] = "USDT";
|
|
185
|
+
FluidPlasmaDepositToken["ETH"] = "ETH";
|
|
186
|
+
})(FluidPlasmaDepositToken || (FluidPlasmaDepositToken = {}));
|
|
157
187
|
export var FluidVaultType;
|
|
158
188
|
(function (FluidVaultType) {
|
|
159
189
|
FluidVaultType["T1"] = "T1";
|
package/esm/types/spark.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { EModeCategoriesData } from './aave';
|
|
2
1
|
import { EthAddress, MMAssetData, MMPositionData, MMUsedAsset, NetworkNumber } from './common';
|
|
3
2
|
export declare enum SparkVersions {
|
|
4
3
|
SparkV1 = "v1default"
|
|
@@ -54,7 +53,6 @@ export interface SparkAssetsData {
|
|
|
54
53
|
}
|
|
55
54
|
export type SparkMarketsData = {
|
|
56
55
|
assetsData: SparkAssetsData;
|
|
57
|
-
eModeCategoriesData: EModeCategoriesData;
|
|
58
56
|
};
|
|
59
57
|
export interface SparkUsedAsset extends MMUsedAsset {
|
|
60
58
|
stableBorrowRate: string;
|
|
@@ -73,7 +71,7 @@ export interface SparkUsedAssets {
|
|
|
73
71
|
export interface SparkHelperCommon {
|
|
74
72
|
usedAssets: SparkUsedAssets;
|
|
75
73
|
eModeCategory: number;
|
|
76
|
-
|
|
74
|
+
eModeCategories?: object;
|
|
77
75
|
assetsData: SparkAssetsData;
|
|
78
76
|
selectedMarket?: SparkMarketData;
|
|
79
77
|
network?: NetworkNumber;
|
package/package.json
CHANGED
|
@@ -1,47 +1,47 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@defisaver/positions-sdk",
|
|
3
|
-
"version": "2.1.9-
|
|
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.
|
|
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.37.9"
|
|
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.1.9-fluid-plasma",
|
|
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.6",
|
|
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.37.9"
|
|
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,240 +1,240 @@
|
|
|
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
|
-
// @ts-ignore
|
|
110
|
-
const reserveTokens = await protocolDataProviderContract.read.getAllReservesTokens(setViemBlockNumber(block)) as { symbol: string, tokenAddress: EthAddress }[];
|
|
111
|
-
const symbols = reserveTokens.map(({ symbol }: { symbol: string }) => symbol);
|
|
112
|
-
const _addresses = reserveTokens.map(({ tokenAddress }: { tokenAddress: EthAddress }) => tokenAddress);
|
|
113
|
-
|
|
114
|
-
const loanInfo = await loanInfoContract.read.getTokenBalances([marketAddress, address, _addresses as EthAddress[]], setViemBlockNumber(block));
|
|
115
|
-
|
|
116
|
-
loanInfo.forEach((_tokenInfo: any, i: number) => {
|
|
117
|
-
const asset = wethToEth(symbols[i]);
|
|
118
|
-
const assetAddr = wethToEthByAddress(_addresses[i], network).toLowerCase();
|
|
119
|
-
const tokenInfo = { ..._tokenInfo };
|
|
120
|
-
|
|
121
|
-
// known bug: stETH leaves 1 wei on every transfer
|
|
122
|
-
if (asset === 'stETH' && tokenInfo.balance.toString() === '1') {
|
|
123
|
-
tokenInfo.balance = '0';
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
balances = {
|
|
127
|
-
collateral: {
|
|
128
|
-
...balances.collateral,
|
|
129
|
-
[addressMapping ? assetAddr : asset]: tokenInfo.balance.toString(),
|
|
130
|
-
},
|
|
131
|
-
debt: {
|
|
132
|
-
...balances.debt,
|
|
133
|
-
[addressMapping ? assetAddr : asset]: new Dec(tokenInfo.borrowsStable.toString()).add(tokenInfo.borrowsVariable.toString()).toString(),
|
|
134
|
-
},
|
|
135
|
-
};
|
|
136
|
-
});
|
|
137
|
-
|
|
138
|
-
return balances;
|
|
139
|
-
};
|
|
140
|
-
|
|
141
|
-
export const getAaveV2AccountBalances = async (provider: EthereumProvider, network: NetworkNumber, block: Blockish, addressMapping: boolean, address: EthAddress): Promise<PositionBalances> => _getAaveV2AccountBalances(getViemProvider(provider, network), network, block, addressMapping, address);
|
|
142
|
-
|
|
143
|
-
export const _getAaveV2AccountData = async (provider: Client, network: NetworkNumber, address: EthAddress, assetsData: AaveV2AssetsData, market: AaveMarketInfo): Promise<AaveV2PositionData> => {
|
|
144
|
-
if (!address) throw new Error('Address is required');
|
|
145
|
-
|
|
146
|
-
let payload: AaveV2PositionData = {
|
|
147
|
-
...EMPTY_AAVE_DATA,
|
|
148
|
-
lastUpdated: Date.now(),
|
|
149
|
-
};
|
|
150
|
-
|
|
151
|
-
const loanInfoContract = AaveLoanInfoV2ContractViem(provider, network);
|
|
152
|
-
const marketAddress = market.providerAddress;
|
|
153
|
-
const _addresses = market.assets.map(a => getAssetInfo(ethToWeth(a)).address);
|
|
154
|
-
const loanInfo = await loanInfoContract.read.getTokenBalances([marketAddress, address, _addresses as EthAddress[]]);
|
|
155
|
-
const usedAssets = {} as AaveV2UsedAssets;
|
|
156
|
-
loanInfo.forEach((_tokenInfo, i) => {
|
|
157
|
-
const asset = market.assets[i];
|
|
158
|
-
const tokenInfo = { ..._tokenInfo };
|
|
159
|
-
|
|
160
|
-
// known bug: stETH leaves 1 wei on every transfer
|
|
161
|
-
if (asset === 'stETH' && tokenInfo.balance.toString() === '1') tokenInfo.balance = BigInt('0');
|
|
162
|
-
|
|
163
|
-
const isSupplied = tokenInfo.balance.toString() !== '0';
|
|
164
|
-
const isBorrowed = tokenInfo.borrowsStable.toString() !== '0' || tokenInfo.borrowsVariable.toString() !== '0';
|
|
165
|
-
if (!isSupplied && !isBorrowed) return;
|
|
166
|
-
|
|
167
|
-
const supplied = assetAmountInEth(tokenInfo.balance.toString(), asset);
|
|
168
|
-
const borrowedStable = assetAmountInEth(tokenInfo.borrowsStable.toString(), asset);
|
|
169
|
-
const borrowedVariable = assetAmountInEth(tokenInfo.borrowsVariable.toString(), asset);
|
|
170
|
-
const enabledAsCollateral = assetsData[asset].usageAsCollateralEnabled ? tokenInfo.enabledAsCollateral : false;
|
|
171
|
-
let interestMode;
|
|
172
|
-
if (borrowedVariable === '0' && borrowedStable !== '0') {
|
|
173
|
-
interestMode = '1';
|
|
174
|
-
} else if (borrowedVariable !== '0' && borrowedStable === '0') {
|
|
175
|
-
interestMode = '2';
|
|
176
|
-
} else {
|
|
177
|
-
interestMode = 'both';
|
|
178
|
-
}
|
|
179
|
-
if (!usedAssets[asset]) usedAssets[asset] = {} as AaveV2UsedAsset;
|
|
180
|
-
usedAssets[asset] = {
|
|
181
|
-
...usedAssets[asset],
|
|
182
|
-
symbol: asset,
|
|
183
|
-
supplied,
|
|
184
|
-
suppliedUsd: new Dec(supplied).mul(assetsData[asset].price).toString(),
|
|
185
|
-
isSupplied,
|
|
186
|
-
collateral: enabledAsCollateral,
|
|
187
|
-
stableBorrowRate: aprToApy(new Dec(tokenInfo.stableBorrowRate).div(1e25).toString()),
|
|
188
|
-
borrowedStable,
|
|
189
|
-
borrowedVariable,
|
|
190
|
-
borrowedUsdStable: new Dec(borrowedStable).mul(assetsData[asset].price).toString(),
|
|
191
|
-
borrowedUsdVariable: new Dec(borrowedVariable).mul(assetsData[asset].price).toString(),
|
|
192
|
-
borrowed: new Dec(borrowedStable).add(borrowedVariable).toString(),
|
|
193
|
-
borrowedUsd: new Dec(new Dec(borrowedVariable).add(borrowedStable)).mul(assetsData[asset].price).toString(),
|
|
194
|
-
isBorrowed,
|
|
195
|
-
interestMode,
|
|
196
|
-
};
|
|
197
|
-
});
|
|
198
|
-
payload = {
|
|
199
|
-
...payload,
|
|
200
|
-
usedAssets,
|
|
201
|
-
...aaveAnyGetAggregatedPositionData({
|
|
202
|
-
usedAssets, assetsData, eModeCategory: 0, selectedMarket: market,
|
|
203
|
-
}),
|
|
204
|
-
};
|
|
205
|
-
|
|
206
|
-
payload.ratio = payload.borrowedUsd && payload.borrowedUsd !== '0'
|
|
207
|
-
? new Dec(payload.borrowLimitUsd).div(payload.borrowedUsd).mul(100).toString()
|
|
208
|
-
: '0';
|
|
209
|
-
payload.minRatio = '100';
|
|
210
|
-
payload.collRatio = payload.borrowedUsd && payload.borrowedUsd !== '0'
|
|
211
|
-
? new Dec(payload.suppliedCollateralUsd).div(payload.borrowedUsd).mul(100).toString()
|
|
212
|
-
: '0';
|
|
213
|
-
|
|
214
|
-
// Calculate borrow limits per asset
|
|
215
|
-
Object.values(payload.usedAssets).forEach((item) => {
|
|
216
|
-
if (item.isBorrowed) {
|
|
217
|
-
// eslint-disable-next-line no-param-reassign
|
|
218
|
-
item.stableLimit = calculateBorrowingAssetLimit(item.borrowedUsdStable, payload.borrowLimitUsd);
|
|
219
|
-
// eslint-disable-next-line no-param-reassign
|
|
220
|
-
item.variableLimit = calculateBorrowingAssetLimit(item.borrowedUsdVariable, payload.borrowLimitUsd);
|
|
221
|
-
// eslint-disable-next-line no-param-reassign
|
|
222
|
-
item.limit = calculateBorrowingAssetLimit(item.borrowedUsd, payload.borrowLimitUsd);
|
|
223
|
-
}
|
|
224
|
-
});
|
|
225
|
-
|
|
226
|
-
const { netApy, incentiveUsd, totalInterestUsd } = calculateNetApy({ usedAssets, assetsData });
|
|
227
|
-
payload.netApy = netApy;
|
|
228
|
-
payload.incentiveUsd = incentiveUsd;
|
|
229
|
-
payload.totalInterestUsd = totalInterestUsd;
|
|
230
|
-
|
|
231
|
-
return payload;
|
|
232
|
-
};
|
|
233
|
-
|
|
234
|
-
export const getAaveV2AccountData = async (provider: EthereumProvider, network: NetworkNumber, address: EthAddress, assetsData: AaveV2AssetsData, market: AaveMarketInfo): Promise<AaveV2PositionData> => _getAaveV2AccountData(getViemProvider(provider, network), network, address, assetsData, market);
|
|
235
|
-
|
|
236
|
-
export const getAaveV2FullPositionData = async (provider: EthereumProvider, network: NetworkNumber, address: EthAddress, market: AaveMarketInfo): Promise<AaveV2PositionData> => {
|
|
237
|
-
const marketData = await getAaveV2MarketsData(provider, network, market);
|
|
238
|
-
const positionData = await getAaveV2AccountData(provider, network, address, marketData.assetsData, market);
|
|
239
|
-
return positionData;
|
|
240
|
-
};
|
|
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
|
+
// @ts-ignore
|
|
110
|
+
const reserveTokens = await protocolDataProviderContract.read.getAllReservesTokens(setViemBlockNumber(block)) as { symbol: string, tokenAddress: EthAddress }[];
|
|
111
|
+
const symbols = reserveTokens.map(({ symbol }: { symbol: string }) => symbol);
|
|
112
|
+
const _addresses = reserveTokens.map(({ tokenAddress }: { tokenAddress: EthAddress }) => tokenAddress);
|
|
113
|
+
|
|
114
|
+
const loanInfo = await loanInfoContract.read.getTokenBalances([marketAddress, address, _addresses as EthAddress[]], setViemBlockNumber(block));
|
|
115
|
+
|
|
116
|
+
loanInfo.forEach((_tokenInfo: any, i: number) => {
|
|
117
|
+
const asset = wethToEth(symbols[i]);
|
|
118
|
+
const assetAddr = wethToEthByAddress(_addresses[i], network).toLowerCase();
|
|
119
|
+
const tokenInfo = { ..._tokenInfo };
|
|
120
|
+
|
|
121
|
+
// known bug: stETH leaves 1 wei on every transfer
|
|
122
|
+
if (asset === 'stETH' && tokenInfo.balance.toString() === '1') {
|
|
123
|
+
tokenInfo.balance = '0';
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
balances = {
|
|
127
|
+
collateral: {
|
|
128
|
+
...balances.collateral,
|
|
129
|
+
[addressMapping ? assetAddr : asset]: tokenInfo.balance.toString(),
|
|
130
|
+
},
|
|
131
|
+
debt: {
|
|
132
|
+
...balances.debt,
|
|
133
|
+
[addressMapping ? assetAddr : asset]: new Dec(tokenInfo.borrowsStable.toString()).add(tokenInfo.borrowsVariable.toString()).toString(),
|
|
134
|
+
},
|
|
135
|
+
};
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
return balances;
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
export const getAaveV2AccountBalances = async (provider: EthereumProvider, network: NetworkNumber, block: Blockish, addressMapping: boolean, address: EthAddress): Promise<PositionBalances> => _getAaveV2AccountBalances(getViemProvider(provider, network), network, block, addressMapping, address);
|
|
142
|
+
|
|
143
|
+
export const _getAaveV2AccountData = async (provider: Client, network: NetworkNumber, address: EthAddress, assetsData: AaveV2AssetsData, market: AaveMarketInfo): Promise<AaveV2PositionData> => {
|
|
144
|
+
if (!address) throw new Error('Address is required');
|
|
145
|
+
|
|
146
|
+
let payload: AaveV2PositionData = {
|
|
147
|
+
...EMPTY_AAVE_DATA,
|
|
148
|
+
lastUpdated: Date.now(),
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
const loanInfoContract = AaveLoanInfoV2ContractViem(provider, network);
|
|
152
|
+
const marketAddress = market.providerAddress;
|
|
153
|
+
const _addresses = market.assets.map(a => getAssetInfo(ethToWeth(a)).address);
|
|
154
|
+
const loanInfo = await loanInfoContract.read.getTokenBalances([marketAddress, address, _addresses as EthAddress[]]);
|
|
155
|
+
const usedAssets = {} as AaveV2UsedAssets;
|
|
156
|
+
loanInfo.forEach((_tokenInfo, i) => {
|
|
157
|
+
const asset = market.assets[i];
|
|
158
|
+
const tokenInfo = { ..._tokenInfo };
|
|
159
|
+
|
|
160
|
+
// known bug: stETH leaves 1 wei on every transfer
|
|
161
|
+
if (asset === 'stETH' && tokenInfo.balance.toString() === '1') tokenInfo.balance = BigInt('0');
|
|
162
|
+
|
|
163
|
+
const isSupplied = tokenInfo.balance.toString() !== '0';
|
|
164
|
+
const isBorrowed = tokenInfo.borrowsStable.toString() !== '0' || tokenInfo.borrowsVariable.toString() !== '0';
|
|
165
|
+
if (!isSupplied && !isBorrowed) return;
|
|
166
|
+
|
|
167
|
+
const supplied = assetAmountInEth(tokenInfo.balance.toString(), asset);
|
|
168
|
+
const borrowedStable = assetAmountInEth(tokenInfo.borrowsStable.toString(), asset);
|
|
169
|
+
const borrowedVariable = assetAmountInEth(tokenInfo.borrowsVariable.toString(), asset);
|
|
170
|
+
const enabledAsCollateral = assetsData[asset].usageAsCollateralEnabled ? tokenInfo.enabledAsCollateral : false;
|
|
171
|
+
let interestMode;
|
|
172
|
+
if (borrowedVariable === '0' && borrowedStable !== '0') {
|
|
173
|
+
interestMode = '1';
|
|
174
|
+
} else if (borrowedVariable !== '0' && borrowedStable === '0') {
|
|
175
|
+
interestMode = '2';
|
|
176
|
+
} else {
|
|
177
|
+
interestMode = 'both';
|
|
178
|
+
}
|
|
179
|
+
if (!usedAssets[asset]) usedAssets[asset] = {} as AaveV2UsedAsset;
|
|
180
|
+
usedAssets[asset] = {
|
|
181
|
+
...usedAssets[asset],
|
|
182
|
+
symbol: asset,
|
|
183
|
+
supplied,
|
|
184
|
+
suppliedUsd: new Dec(supplied).mul(assetsData[asset].price).toString(),
|
|
185
|
+
isSupplied,
|
|
186
|
+
collateral: enabledAsCollateral,
|
|
187
|
+
stableBorrowRate: aprToApy(new Dec(tokenInfo.stableBorrowRate).div(1e25).toString()),
|
|
188
|
+
borrowedStable,
|
|
189
|
+
borrowedVariable,
|
|
190
|
+
borrowedUsdStable: new Dec(borrowedStable).mul(assetsData[asset].price).toString(),
|
|
191
|
+
borrowedUsdVariable: new Dec(borrowedVariable).mul(assetsData[asset].price).toString(),
|
|
192
|
+
borrowed: new Dec(borrowedStable).add(borrowedVariable).toString(),
|
|
193
|
+
borrowedUsd: new Dec(new Dec(borrowedVariable).add(borrowedStable)).mul(assetsData[asset].price).toString(),
|
|
194
|
+
isBorrowed,
|
|
195
|
+
interestMode,
|
|
196
|
+
};
|
|
197
|
+
});
|
|
198
|
+
payload = {
|
|
199
|
+
...payload,
|
|
200
|
+
usedAssets,
|
|
201
|
+
...aaveAnyGetAggregatedPositionData({
|
|
202
|
+
usedAssets, assetsData, eModeCategory: 0, selectedMarket: market,
|
|
203
|
+
}),
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
payload.ratio = payload.borrowedUsd && payload.borrowedUsd !== '0'
|
|
207
|
+
? new Dec(payload.borrowLimitUsd).div(payload.borrowedUsd).mul(100).toString()
|
|
208
|
+
: '0';
|
|
209
|
+
payload.minRatio = '100';
|
|
210
|
+
payload.collRatio = payload.borrowedUsd && payload.borrowedUsd !== '0'
|
|
211
|
+
? new Dec(payload.suppliedCollateralUsd).div(payload.borrowedUsd).mul(100).toString()
|
|
212
|
+
: '0';
|
|
213
|
+
|
|
214
|
+
// Calculate borrow limits per asset
|
|
215
|
+
Object.values(payload.usedAssets).forEach((item) => {
|
|
216
|
+
if (item.isBorrowed) {
|
|
217
|
+
// eslint-disable-next-line no-param-reassign
|
|
218
|
+
item.stableLimit = calculateBorrowingAssetLimit(item.borrowedUsdStable, payload.borrowLimitUsd);
|
|
219
|
+
// eslint-disable-next-line no-param-reassign
|
|
220
|
+
item.variableLimit = calculateBorrowingAssetLimit(item.borrowedUsdVariable, payload.borrowLimitUsd);
|
|
221
|
+
// eslint-disable-next-line no-param-reassign
|
|
222
|
+
item.limit = calculateBorrowingAssetLimit(item.borrowedUsd, payload.borrowLimitUsd);
|
|
223
|
+
}
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
const { netApy, incentiveUsd, totalInterestUsd } = calculateNetApy({ usedAssets, assetsData });
|
|
227
|
+
payload.netApy = netApy;
|
|
228
|
+
payload.incentiveUsd = incentiveUsd;
|
|
229
|
+
payload.totalInterestUsd = totalInterestUsd;
|
|
230
|
+
|
|
231
|
+
return payload;
|
|
232
|
+
};
|
|
233
|
+
|
|
234
|
+
export const getAaveV2AccountData = async (provider: EthereumProvider, network: NetworkNumber, address: EthAddress, assetsData: AaveV2AssetsData, market: AaveMarketInfo): Promise<AaveV2PositionData> => _getAaveV2AccountData(getViemProvider(provider, network), network, address, assetsData, market);
|
|
235
|
+
|
|
236
|
+
export const getAaveV2FullPositionData = async (provider: EthereumProvider, network: NetworkNumber, address: EthAddress, market: AaveMarketInfo): Promise<AaveV2PositionData> => {
|
|
237
|
+
const marketData = await getAaveV2MarketsData(provider, network, market);
|
|
238
|
+
const positionData = await getAaveV2AccountData(provider, network, address, marketData.assetsData, market);
|
|
239
|
+
return positionData;
|
|
240
|
+
};
|