@defisaver/positions-sdk 0.0.182 → 0.0.183-dev-allocator
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 +69 -69
- package/cjs/aaveV3/index.js +3 -3
- package/cjs/config/contracts.d.ts +3 -0
- package/cjs/config/contracts.js +3 -0
- package/cjs/helpers/aaveHelpers/index.js +0 -1
- package/cjs/helpers/morphoBlueHelpers/index.d.ts +5 -0
- package/cjs/helpers/morphoBlueHelpers/index.js +127 -1
- package/cjs/staking/staking.d.ts +2 -3
- package/cjs/staking/staking.js +2 -2
- package/cjs/types/morphoBlue.d.ts +25 -0
- package/esm/aaveV3/index.js +4 -4
- package/esm/config/contracts.d.ts +3 -0
- package/esm/config/contracts.js +3 -0
- package/esm/helpers/aaveHelpers/index.js +0 -1
- package/esm/helpers/morphoBlueHelpers/index.d.ts +5 -0
- package/esm/helpers/morphoBlueHelpers/index.js +124 -0
- package/esm/staking/staking.d.ts +2 -3
- package/esm/staking/staking.js +2 -2
- package/esm/types/morphoBlue.d.ts +25 -0
- package/package.json +49 -49
- package/src/aaveV2/index.ts +227 -227
- package/src/aaveV3/index.ts +624 -628
- package/src/assets/index.ts +60 -60
- package/src/chickenBonds/index.ts +123 -123
- package/src/compoundV2/index.ts +220 -220
- package/src/compoundV3/index.ts +282 -282
- package/src/config/contracts.js +1043 -1040
- package/src/constants/index.ts +6 -6
- package/src/contracts.ts +130 -130
- package/src/curveUsd/index.ts +229 -229
- package/src/eulerV2/index.ts +303 -303
- package/src/exchange/index.ts +17 -17
- package/src/helpers/aaveHelpers/index.ts +198 -199
- package/src/helpers/chickenBondsHelpers/index.ts +23 -23
- package/src/helpers/compoundHelpers/index.ts +246 -246
- package/src/helpers/curveUsdHelpers/index.ts +40 -40
- package/src/helpers/eulerHelpers/index.ts +232 -232
- package/src/helpers/index.ts +8 -8
- package/src/helpers/llamaLendHelpers/index.ts +53 -53
- package/src/helpers/makerHelpers/index.ts +94 -94
- package/src/helpers/morphoBlueHelpers/index.ts +256 -115
- package/src/helpers/sparkHelpers/index.ts +150 -150
- package/src/index.ts +48 -48
- package/src/liquity/index.ts +116 -116
- package/src/llamaLend/index.ts +275 -275
- package/src/maker/index.ts +117 -117
- package/src/markets/aave/index.ts +152 -152
- package/src/markets/aave/marketAssets.ts +46 -46
- package/src/markets/compound/index.ts +173 -173
- package/src/markets/compound/marketsAssets.ts +64 -64
- package/src/markets/curveUsd/index.ts +69 -69
- package/src/markets/euler/index.ts +26 -26
- package/src/markets/index.ts +23 -23
- package/src/markets/llamaLend/contractAddresses.ts +141 -141
- package/src/markets/llamaLend/index.ts +235 -235
- package/src/markets/morphoBlue/index.ts +809 -809
- package/src/markets/spark/index.ts +29 -29
- package/src/markets/spark/marketAssets.ts +10 -10
- package/src/moneymarket/moneymarketCommonService.ts +80 -80
- package/src/morphoAaveV2/index.ts +256 -256
- package/src/morphoAaveV3/index.ts +630 -630
- package/src/morphoBlue/index.ts +171 -171
- package/src/multicall/index.ts +22 -22
- package/src/services/dsrService.ts +15 -15
- package/src/services/priceService.ts +21 -21
- package/src/services/utils.ts +56 -56
- package/src/setup.ts +8 -8
- package/src/spark/index.ts +461 -461
- package/src/staking/staking.ts +220 -222
- package/src/types/aave.ts +270 -270
- package/src/types/chickenBonds.ts +45 -45
- package/src/types/common.ts +84 -84
- package/src/types/compound.ts +129 -129
- package/src/types/curveUsd.ts +118 -118
- package/src/types/euler.ts +171 -171
- package/src/types/index.ts +9 -9
- package/src/types/liquity.ts +30 -30
- package/src/types/llamaLend.ts +155 -155
- package/src/types/maker.ts +50 -50
- package/src/types/morphoBlue.ts +177 -154
- package/src/types/spark.ts +131 -131
- package/.vscode/launch.json +0 -17
- package/.vscode/settings.json +0 -37
package/esm/staking/staking.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import Web3 from 'web3';
|
|
2
|
-
import { MMAssetsData, MMUsedAssets
|
|
2
|
+
import { MMAssetsData, MMUsedAssets } from '../types/common';
|
|
3
3
|
export declare const getStETHApy: (web3: Web3, fromBlock?: number, blockNumber?: 'latest' | number) => Promise<string>;
|
|
4
4
|
export declare const getCbETHApy: (web3: Web3, blockNumber?: 'latest' | number) => Promise<string>;
|
|
5
5
|
export declare const getREthApy: (web3: Web3, blockNumber?: 'latest' | number) => Promise<string>;
|
|
@@ -8,11 +8,10 @@ export declare const getSsrApy: () => Promise<string>;
|
|
|
8
8
|
export declare const STAKING_ASSETS: string[];
|
|
9
9
|
export declare const getStakingApy: (asset: string, web3: Web3, blockNumber?: 'latest' | number, fromBlock?: number | undefined) => "0" | Promise<any> | undefined;
|
|
10
10
|
export declare const calculateInterestEarned: (principal: string, interest: string, type: string, apy?: boolean) => number;
|
|
11
|
-
export declare const calculateNetApy: ({ usedAssets, assetsData, isMorpho
|
|
11
|
+
export declare const calculateNetApy: ({ usedAssets, assetsData, isMorpho }: {
|
|
12
12
|
usedAssets: MMUsedAssets;
|
|
13
13
|
assetsData: MMAssetsData;
|
|
14
14
|
isMorpho?: boolean | undefined;
|
|
15
|
-
network?: NetworkNumber | undefined;
|
|
16
15
|
}) => {
|
|
17
16
|
netApy: string;
|
|
18
17
|
totalInterestUsd: string;
|
package/esm/staking/staking.js
CHANGED
|
@@ -157,7 +157,7 @@ export const calculateInterestEarned = (principal, interest, type, apy = false)
|
|
|
157
157
|
}
|
|
158
158
|
return (+principal * (Math.pow(((1 + (+interest / 100) / BLOCKS_IN_A_YEAR)), (BLOCKS_IN_A_YEAR * interval)))) - +principal; // eslint-disable-line
|
|
159
159
|
};
|
|
160
|
-
export const calculateNetApy = ({ usedAssets, assetsData, isMorpho = false
|
|
160
|
+
export const calculateNetApy = ({ usedAssets, assetsData, isMorpho = false }) => {
|
|
161
161
|
const sumValues = Object.values(usedAssets).reduce((_acc, usedAsset) => {
|
|
162
162
|
const acc = Object.assign({}, _acc);
|
|
163
163
|
const assetData = assetsData[usedAsset.symbol];
|
|
@@ -180,7 +180,7 @@ export const calculateNetApy = ({ usedAssets, assetsData, isMorpho = false, netw
|
|
|
180
180
|
acc.borrowedUsd = new Dec(acc.borrowedUsd).add(amount).toString();
|
|
181
181
|
const rate = isMorpho
|
|
182
182
|
? usedAsset.borrowRate === '0' ? assetData.borrowRateP2P : usedAsset.borrowRate
|
|
183
|
-
: (usedAsset.symbol === 'GHO' &&
|
|
183
|
+
: (usedAsset.symbol === 'GHO' && assetsData.nativeAsset)
|
|
184
184
|
? usedAsset.discountedBorrowRate
|
|
185
185
|
: ((usedAsset === null || usedAsset === void 0 ? void 0 : usedAsset.interestMode) === '1' ? usedAsset.stableBorrowRate : assetData.borrowRate);
|
|
186
186
|
const borrowInterest = calculateInterestEarned(amount, rate, 'year', true);
|
|
@@ -137,3 +137,28 @@ export interface MorphoBluePositionData {
|
|
|
137
137
|
supplyShares: string;
|
|
138
138
|
borrowShares: string;
|
|
139
139
|
}
|
|
140
|
+
export interface MorphoBlueVault {
|
|
141
|
+
address: string;
|
|
142
|
+
}
|
|
143
|
+
export interface MorphoBlueAllocationMarket {
|
|
144
|
+
loanAsset: {
|
|
145
|
+
address: string;
|
|
146
|
+
};
|
|
147
|
+
collateralAsset: {
|
|
148
|
+
address: string;
|
|
149
|
+
};
|
|
150
|
+
oracle: {
|
|
151
|
+
address: string;
|
|
152
|
+
};
|
|
153
|
+
irmAddress: string;
|
|
154
|
+
lltv: string;
|
|
155
|
+
}
|
|
156
|
+
export interface MorphoBluePublicAllocatorItem {
|
|
157
|
+
vault: MorphoBlueVault;
|
|
158
|
+
assets: string;
|
|
159
|
+
allocationMarket: MorphoBlueAllocationMarket;
|
|
160
|
+
}
|
|
161
|
+
export interface MorphoBlueRealloactionMarketData {
|
|
162
|
+
reallocatableLiquidityAssets: string;
|
|
163
|
+
publicAllocatorSharedLiquidity: MorphoBluePublicAllocatorItem[];
|
|
164
|
+
}
|
package/package.json
CHANGED
|
@@ -1,49 +1,49 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@defisaver/positions-sdk",
|
|
3
|
-
"version": "0.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 generate-contracts && npm run build:cjs && npm run build:esm",
|
|
12
|
-
"dev": "npm run generate-contracts && tsc -p tsconfig.json --watch",
|
|
13
|
-
"lint": "eslint src/ --fix",
|
|
14
|
-
"lint-check": "eslint src/",
|
|
15
|
-
"generate-contracts": "node scripts/generateContracts.js",
|
|
16
|
-
"test": "mocha tests/*",
|
|
17
|
-
"test-single": "mocha ./tests/$npm_config_name.ts",
|
|
18
|
-
"test:debugger": "mocha --inspect-brk tests/*",
|
|
19
|
-
"build-test": "npm run build && npm run test",
|
|
20
|
-
"version-bump": "git commit -am \"Version bump to $(npm version patch | cut -c 2-)\""
|
|
21
|
-
},
|
|
22
|
-
"keywords": [],
|
|
23
|
-
"author": "",
|
|
24
|
-
"license": "ISC",
|
|
25
|
-
"dependencies": {
|
|
26
|
-
"@defisaver/tokens": "^1.5.50",
|
|
27
|
-
"@ethersproject/bignumber": "^5.7.0",
|
|
28
|
-
"@morpho-org/morpho-aave-v3-sdk": "^1.5.3",
|
|
29
|
-
"decimal.js": "^10.4.3"
|
|
30
|
-
},
|
|
31
|
-
"devDependencies": {
|
|
32
|
-
"@defisaver/eslint-config": "^1.0.1",
|
|
33
|
-
"@types/chai": "^5.0.0",
|
|
34
|
-
"@types/mocha": "^10.0.9",
|
|
35
|
-
"@types/web3": "^1.0.20",
|
|
36
|
-
"chai": "^4.3.8",
|
|
37
|
-
"dotenv": "^16.3.1",
|
|
38
|
-
"eslint": "^8.49.0",
|
|
39
|
-
"eslint-plugin-import": "^2.31.0",
|
|
40
|
-
"mocha": "^10.2.0",
|
|
41
|
-
"ts-node": "^10.9.2",
|
|
42
|
-
"typechain": "^8.3.1",
|
|
43
|
-
"typechain-target-web3-v1-3mihai3": "^6.0.2",
|
|
44
|
-
"typescript": "^5.2.2"
|
|
45
|
-
},
|
|
46
|
-
"peerDependencies": {
|
|
47
|
-
"web3": "^1.10.2"
|
|
48
|
-
}
|
|
49
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@defisaver/positions-sdk",
|
|
3
|
+
"version": "0.0.183-dev-allocator",
|
|
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 generate-contracts && npm run build:cjs && npm run build:esm",
|
|
12
|
+
"dev": "npm run generate-contracts && tsc -p tsconfig.json --watch",
|
|
13
|
+
"lint": "eslint src/ --fix",
|
|
14
|
+
"lint-check": "eslint src/",
|
|
15
|
+
"generate-contracts": "node scripts/generateContracts.js",
|
|
16
|
+
"test": "mocha tests/*",
|
|
17
|
+
"test-single": "mocha ./tests/$npm_config_name.ts",
|
|
18
|
+
"test:debugger": "mocha --inspect-brk tests/*",
|
|
19
|
+
"build-test": "npm run build && npm run test",
|
|
20
|
+
"version-bump": "git commit -am \"Version bump to $(npm version patch | cut -c 2-)\""
|
|
21
|
+
},
|
|
22
|
+
"keywords": [],
|
|
23
|
+
"author": "",
|
|
24
|
+
"license": "ISC",
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"@defisaver/tokens": "^1.5.50",
|
|
27
|
+
"@ethersproject/bignumber": "^5.7.0",
|
|
28
|
+
"@morpho-org/morpho-aave-v3-sdk": "^1.5.3",
|
|
29
|
+
"decimal.js": "^10.4.3"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@defisaver/eslint-config": "^1.0.1",
|
|
33
|
+
"@types/chai": "^5.0.0",
|
|
34
|
+
"@types/mocha": "^10.0.9",
|
|
35
|
+
"@types/web3": "^1.0.20",
|
|
36
|
+
"chai": "^4.3.8",
|
|
37
|
+
"dotenv": "^16.3.1",
|
|
38
|
+
"eslint": "^8.49.0",
|
|
39
|
+
"eslint-plugin-import": "^2.31.0",
|
|
40
|
+
"mocha": "^10.2.0",
|
|
41
|
+
"ts-node": "^10.9.2",
|
|
42
|
+
"typechain": "^8.3.1",
|
|
43
|
+
"typechain-target-web3-v1-3mihai3": "^6.0.2",
|
|
44
|
+
"typescript": "^5.2.2"
|
|
45
|
+
},
|
|
46
|
+
"peerDependencies": {
|
|
47
|
+
"web3": "^1.10.2"
|
|
48
|
+
}
|
|
49
|
+
}
|
package/src/aaveV2/index.ts
CHANGED
|
@@ -1,227 +1,227 @@
|
|
|
1
|
-
import Web3 from 'web3';
|
|
2
|
-
import Dec from 'decimal.js';
|
|
3
|
-
import { assetAmountInEth, getAssetInfo } from '@defisaver/tokens';
|
|
4
|
-
import {
|
|
5
|
-
Blockish, EthAddress, NetworkNumber, PositionBalances,
|
|
6
|
-
} from '../types/common';
|
|
7
|
-
import { calculateNetApy, getStETHApy } from '../staking';
|
|
8
|
-
import { ethToWeth, wethToEth, wethToEthByAddress } from '../services/utils';
|
|
9
|
-
import { AaveLoanInfoV2Contract, createContractWrapper } 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
|
-
|
|
19
|
-
export const getAaveV2MarketsData = async (web3: Web3, network: NetworkNumber, selectedMarket: AaveMarketInfo, mainnetWeb3: Web3) => {
|
|
20
|
-
const ethPrice = await getEthPrice(web3);
|
|
21
|
-
const _addresses = selectedMarket.assets.map(a => getAssetInfo(ethToWeth(a)).address);
|
|
22
|
-
const loanInfoContract = AaveLoanInfoV2Contract(web3, network);
|
|
23
|
-
const marketAddress = selectedMarket.providerAddress;
|
|
24
|
-
const loanInfo = await loanInfoContract.methods.getFullTokensInfo(marketAddress, _addresses).call();
|
|
25
|
-
const markets = loanInfo
|
|
26
|
-
.map((market, i) => ({
|
|
27
|
-
symbol: selectedMarket.assets[i],
|
|
28
|
-
underlyingTokenAddress: market.underlyingTokenAddress,
|
|
29
|
-
supplyRate: aprToApy(new Dec(market.supplyRate.toString()).div(1e25).toString()),
|
|
30
|
-
borrowRate: aprToApy(new Dec(market.borrowRateVariable.toString()).div(1e25).toString()),
|
|
31
|
-
borrowRateStable: aprToApy(new Dec(market.borrowRateStable.toString()).div(1e25).toString()),
|
|
32
|
-
collateralFactor: new Dec(market.collateralFactor.toString()).div(10000).toString(),
|
|
33
|
-
liquidationRatio: new Dec(market.liquidationRatio.toString()).div(10000).toString(),
|
|
34
|
-
marketLiquidity: assetAmountInEth(new Dec(market.totalSupply.toString())
|
|
35
|
-
.sub(market.totalBorrow.toString())
|
|
36
|
-
.toString(), selectedMarket.assets[i]),
|
|
37
|
-
utilization: new Dec(market.totalBorrow.toString())
|
|
38
|
-
.div(new Dec(market.totalSupply.toString()))
|
|
39
|
-
.times(100)
|
|
40
|
-
.toString(),
|
|
41
|
-
usageAsCollateralEnabled: market.usageAsCollateralEnabled,
|
|
42
|
-
supplyCap: '0',
|
|
43
|
-
borrowCap: '0', // v2 doesnt have borrow cap but adding it for compatability with v3
|
|
44
|
-
totalSupply: assetAmountInEth(market.totalSupply.toString(), selectedMarket.assets[i]),
|
|
45
|
-
isInactive: !market.isActive,
|
|
46
|
-
isFrozen: market.isFrozen,
|
|
47
|
-
canBeBorrowed: market.isActive && market.borrowingEnabled && !market.isFrozen,
|
|
48
|
-
canBeSupplied: market.isActive && !market.isFrozen,
|
|
49
|
-
canBeWithdrawn: market.isActive,
|
|
50
|
-
canBePayBacked: market.isActive,
|
|
51
|
-
disabledStableBorrowing: !market.stableBorrowRateEnabled,
|
|
52
|
-
totalBorrow: assetAmountInEth(market.totalBorrow.toString(), selectedMarket.assets[i]),
|
|
53
|
-
totalBorrowVar: assetAmountInEth(market.totalBorrowVar.toString(), selectedMarket.assets[i]),
|
|
54
|
-
priceInEth: new Dec(market.price.toString()).div(1e18).toString(),
|
|
55
|
-
incentiveSupplyToken: 'AAVE',
|
|
56
|
-
incentiveBorrowToken: 'AAVE',
|
|
57
|
-
incentiveSupplyApy: '0',
|
|
58
|
-
price: new Dec(market.price.toString()).div(1e18).mul(ethPrice).toString(),
|
|
59
|
-
}));
|
|
60
|
-
|
|
61
|
-
const stEthMarket = markets.find(({ symbol }) => symbol === 'stETH');
|
|
62
|
-
if (stEthMarket) {
|
|
63
|
-
stEthMarket.incentiveSupplyApy = await getStETHApy(mainnetWeb3);
|
|
64
|
-
stEthMarket.incentiveSupplyToken = 'stETH';
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
const payload: AaveV2AssetsData = {};
|
|
68
|
-
// Sort by market size
|
|
69
|
-
markets
|
|
70
|
-
.sort((a, b) => {
|
|
71
|
-
const aMarket = new Dec(a.price).times(a.totalSupply).toString();
|
|
72
|
-
const bMarket = new Dec(b.price).times(b.totalSupply).toString();
|
|
73
|
-
|
|
74
|
-
return new Dec(bMarket).minus(aMarket).toNumber();
|
|
75
|
-
})
|
|
76
|
-
.forEach((assetData: AaveV2AssetData, i) => {
|
|
77
|
-
payload[assetData.symbol] = { ...assetData, sortIndex: i };
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
return { assetsData: payload };
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
export const getAaveV2AccountBalances = async (web3: Web3, network: NetworkNumber, block: Blockish, addressMapping: boolean, address: EthAddress): Promise<PositionBalances> => {
|
|
84
|
-
let balances: PositionBalances = {
|
|
85
|
-
collateral: {},
|
|
86
|
-
debt: {},
|
|
87
|
-
};
|
|
88
|
-
|
|
89
|
-
if (!address) {
|
|
90
|
-
return balances;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
const market = AAVE_V2;
|
|
94
|
-
|
|
95
|
-
const loanInfoContract = AaveLoanInfoV2Contract(web3, network, block);
|
|
96
|
-
|
|
97
|
-
const marketAddress = market.providerAddress;
|
|
98
|
-
// @ts-ignore
|
|
99
|
-
const protocolDataProviderContract = createContractWrapper(web3, network, market.protocolData, market.protocolDataAddress);
|
|
100
|
-
|
|
101
|
-
const reserveTokens = await protocolDataProviderContract.methods.getAllReservesTokens().call({}, block);
|
|
102
|
-
const symbols = reserveTokens.map(({ symbol }: { symbol: string }) => symbol);
|
|
103
|
-
const _addresses = reserveTokens.map(({ tokenAddress }: { tokenAddress: EthAddress }) => tokenAddress);
|
|
104
|
-
|
|
105
|
-
const loanInfo = await loanInfoContract.methods.getTokenBalances(marketAddress, address, _addresses).call({}, block);
|
|
106
|
-
|
|
107
|
-
loanInfo.forEach((_tokenInfo: any, i: number) => {
|
|
108
|
-
const asset = wethToEth(symbols[i]);
|
|
109
|
-
const assetAddr = wethToEthByAddress(_addresses[i], network).toLowerCase();
|
|
110
|
-
const tokenInfo = { ..._tokenInfo };
|
|
111
|
-
|
|
112
|
-
// known bug: stETH leaves 1 wei on every transfer
|
|
113
|
-
if (asset === 'stETH' && tokenInfo.balance.toString() === '1') {
|
|
114
|
-
tokenInfo.balance = '0';
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
balances = {
|
|
118
|
-
collateral: {
|
|
119
|
-
...balances.collateral,
|
|
120
|
-
[addressMapping ? assetAddr : asset]: tokenInfo.balance.toString(),
|
|
121
|
-
},
|
|
122
|
-
debt: {
|
|
123
|
-
...balances.debt,
|
|
124
|
-
[addressMapping ? assetAddr : asset]: new Dec(tokenInfo.borrowsStable.toString()).add(tokenInfo.borrowsVariable.toString()).toString(),
|
|
125
|
-
},
|
|
126
|
-
};
|
|
127
|
-
});
|
|
128
|
-
|
|
129
|
-
return balances;
|
|
130
|
-
};
|
|
131
|
-
|
|
132
|
-
export const getAaveV2AccountData = async (web3: Web3, network: NetworkNumber, address: string, assetsData: AaveV2AssetsData, market: AaveMarketInfo): Promise<AaveV2PositionData> => {
|
|
133
|
-
if (!address) throw new Error('Address is required');
|
|
134
|
-
|
|
135
|
-
let payload: AaveV2PositionData = {
|
|
136
|
-
...EMPTY_AAVE_DATA,
|
|
137
|
-
lastUpdated: Date.now(),
|
|
138
|
-
};
|
|
139
|
-
|
|
140
|
-
const loanInfoContract = AaveLoanInfoV2Contract(web3, network);
|
|
141
|
-
const marketAddress = market.providerAddress;
|
|
142
|
-
const _addresses = market.assets.map(a => getAssetInfo(ethToWeth(a)).address);
|
|
143
|
-
const loanInfo = await loanInfoContract.methods.getTokenBalances(marketAddress, address, _addresses).call();
|
|
144
|
-
const usedAssets = {} as AaveV2UsedAssets;
|
|
145
|
-
loanInfo.forEach((_tokenInfo, i) => {
|
|
146
|
-
const asset = market.assets[i];
|
|
147
|
-
const tokenInfo = { ..._tokenInfo };
|
|
148
|
-
|
|
149
|
-
// known bug: stETH leaves 1 wei on every transfer
|
|
150
|
-
if (asset === 'stETH' && tokenInfo.balance.toString() === '1') tokenInfo.balance = '0';
|
|
151
|
-
|
|
152
|
-
const isSupplied = tokenInfo.balance.toString() !== '0';
|
|
153
|
-
const isBorrowed = tokenInfo.borrowsStable.toString() !== '0' || tokenInfo.borrowsVariable.toString() !== '0';
|
|
154
|
-
if (!isSupplied && !isBorrowed) return;
|
|
155
|
-
|
|
156
|
-
const supplied = assetAmountInEth(tokenInfo.balance.toString(), asset);
|
|
157
|
-
const borrowedStable = assetAmountInEth(tokenInfo.borrowsStable.toString(), asset);
|
|
158
|
-
const borrowedVariable = assetAmountInEth(tokenInfo.borrowsVariable.toString(), asset);
|
|
159
|
-
const enabledAsCollateral = assetsData[asset].usageAsCollateralEnabled ? tokenInfo.enabledAsCollateral : false;
|
|
160
|
-
let interestMode;
|
|
161
|
-
if (borrowedVariable === '0' && borrowedStable !== '0') {
|
|
162
|
-
interestMode = '1';
|
|
163
|
-
} else if (borrowedVariable !== '0' && borrowedStable === '0') {
|
|
164
|
-
interestMode = '2';
|
|
165
|
-
} else {
|
|
166
|
-
interestMode = 'both';
|
|
167
|
-
}
|
|
168
|
-
if (!usedAssets[asset]) usedAssets[asset] = {} as AaveV2UsedAsset;
|
|
169
|
-
usedAssets[asset] = {
|
|
170
|
-
...usedAssets[asset],
|
|
171
|
-
symbol: asset,
|
|
172
|
-
supplied,
|
|
173
|
-
suppliedUsd: new Dec(supplied).mul(assetsData[asset].price).toString(),
|
|
174
|
-
isSupplied,
|
|
175
|
-
collateral: enabledAsCollateral,
|
|
176
|
-
stableBorrowRate: aprToApy(new Dec(tokenInfo.stableBorrowRate).div(1e25).toString()),
|
|
177
|
-
borrowedStable,
|
|
178
|
-
borrowedVariable,
|
|
179
|
-
borrowedUsdStable: new Dec(borrowedStable).mul(assetsData[asset].price).toString(),
|
|
180
|
-
borrowedUsdVariable: new Dec(borrowedVariable).mul(assetsData[asset].price).toString(),
|
|
181
|
-
borrowed: new Dec(borrowedStable).add(borrowedVariable).toString(),
|
|
182
|
-
borrowedUsd: new Dec(new Dec(borrowedVariable).add(borrowedStable)).mul(assetsData[asset].price).toString(),
|
|
183
|
-
isBorrowed,
|
|
184
|
-
interestMode,
|
|
185
|
-
};
|
|
186
|
-
});
|
|
187
|
-
payload = {
|
|
188
|
-
...payload,
|
|
189
|
-
usedAssets,
|
|
190
|
-
...aaveAnyGetAggregatedPositionData({
|
|
191
|
-
usedAssets, assetsData, eModeCategory: 0, selectedMarket: market,
|
|
192
|
-
}),
|
|
193
|
-
};
|
|
194
|
-
|
|
195
|
-
payload.ratio = payload.borrowedUsd && payload.borrowedUsd !== '0'
|
|
196
|
-
? new Dec(payload.borrowLimitUsd).div(payload.borrowedUsd).mul(100).toString()
|
|
197
|
-
: '0';
|
|
198
|
-
payload.minRatio = '100';
|
|
199
|
-
payload.collRatio = payload.borrowedUsd && payload.borrowedUsd !== '0'
|
|
200
|
-
? new Dec(payload.suppliedCollateralUsd).div(payload.borrowedUsd).mul(100).toString()
|
|
201
|
-
: '0';
|
|
202
|
-
|
|
203
|
-
// Calculate borrow limits per asset
|
|
204
|
-
Object.values(payload.usedAssets).forEach((item) => {
|
|
205
|
-
if (item.isBorrowed) {
|
|
206
|
-
// eslint-disable-next-line no-param-reassign
|
|
207
|
-
item.stableLimit = calculateBorrowingAssetLimit(item.borrowedUsdStable, payload.borrowLimitUsd);
|
|
208
|
-
// eslint-disable-next-line no-param-reassign
|
|
209
|
-
item.variableLimit = calculateBorrowingAssetLimit(item.borrowedUsdVariable, payload.borrowLimitUsd);
|
|
210
|
-
// eslint-disable-next-line no-param-reassign
|
|
211
|
-
item.limit = calculateBorrowingAssetLimit(item.borrowedUsd, payload.borrowLimitUsd);
|
|
212
|
-
}
|
|
213
|
-
});
|
|
214
|
-
|
|
215
|
-
const { netApy, incentiveUsd, totalInterestUsd } = calculateNetApy({ usedAssets, assetsData });
|
|
216
|
-
payload.netApy = netApy;
|
|
217
|
-
payload.incentiveUsd = incentiveUsd;
|
|
218
|
-
payload.totalInterestUsd = totalInterestUsd;
|
|
219
|
-
|
|
220
|
-
return payload;
|
|
221
|
-
};
|
|
222
|
-
|
|
223
|
-
export const getAaveV2FullPositionData = async (web3: Web3, network: NetworkNumber, address: string, market: AaveMarketInfo, mainnetWeb3: Web3): Promise<AaveV2PositionData> => {
|
|
224
|
-
const marketData = await getAaveV2MarketsData(web3, network, market, mainnetWeb3);
|
|
225
|
-
const positionData = await getAaveV2AccountData(web3, network, address, marketData.assetsData, market);
|
|
226
|
-
return positionData;
|
|
227
|
-
};
|
|
1
|
+
import Web3 from 'web3';
|
|
2
|
+
import Dec from 'decimal.js';
|
|
3
|
+
import { assetAmountInEth, getAssetInfo } from '@defisaver/tokens';
|
|
4
|
+
import {
|
|
5
|
+
Blockish, EthAddress, NetworkNumber, PositionBalances,
|
|
6
|
+
} from '../types/common';
|
|
7
|
+
import { calculateNetApy, getStETHApy } from '../staking';
|
|
8
|
+
import { ethToWeth, wethToEth, wethToEthByAddress } from '../services/utils';
|
|
9
|
+
import { AaveLoanInfoV2Contract, createContractWrapper } 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
|
+
|
|
19
|
+
export const getAaveV2MarketsData = async (web3: Web3, network: NetworkNumber, selectedMarket: AaveMarketInfo, mainnetWeb3: Web3) => {
|
|
20
|
+
const ethPrice = await getEthPrice(web3);
|
|
21
|
+
const _addresses = selectedMarket.assets.map(a => getAssetInfo(ethToWeth(a)).address);
|
|
22
|
+
const loanInfoContract = AaveLoanInfoV2Contract(web3, network);
|
|
23
|
+
const marketAddress = selectedMarket.providerAddress;
|
|
24
|
+
const loanInfo = await loanInfoContract.methods.getFullTokensInfo(marketAddress, _addresses).call();
|
|
25
|
+
const markets = loanInfo
|
|
26
|
+
.map((market, i) => ({
|
|
27
|
+
symbol: selectedMarket.assets[i],
|
|
28
|
+
underlyingTokenAddress: market.underlyingTokenAddress,
|
|
29
|
+
supplyRate: aprToApy(new Dec(market.supplyRate.toString()).div(1e25).toString()),
|
|
30
|
+
borrowRate: aprToApy(new Dec(market.borrowRateVariable.toString()).div(1e25).toString()),
|
|
31
|
+
borrowRateStable: aprToApy(new Dec(market.borrowRateStable.toString()).div(1e25).toString()),
|
|
32
|
+
collateralFactor: new Dec(market.collateralFactor.toString()).div(10000).toString(),
|
|
33
|
+
liquidationRatio: new Dec(market.liquidationRatio.toString()).div(10000).toString(),
|
|
34
|
+
marketLiquidity: assetAmountInEth(new Dec(market.totalSupply.toString())
|
|
35
|
+
.sub(market.totalBorrow.toString())
|
|
36
|
+
.toString(), selectedMarket.assets[i]),
|
|
37
|
+
utilization: new Dec(market.totalBorrow.toString())
|
|
38
|
+
.div(new Dec(market.totalSupply.toString()))
|
|
39
|
+
.times(100)
|
|
40
|
+
.toString(),
|
|
41
|
+
usageAsCollateralEnabled: market.usageAsCollateralEnabled,
|
|
42
|
+
supplyCap: '0',
|
|
43
|
+
borrowCap: '0', // v2 doesnt have borrow cap but adding it for compatability with v3
|
|
44
|
+
totalSupply: assetAmountInEth(market.totalSupply.toString(), selectedMarket.assets[i]),
|
|
45
|
+
isInactive: !market.isActive,
|
|
46
|
+
isFrozen: market.isFrozen,
|
|
47
|
+
canBeBorrowed: market.isActive && market.borrowingEnabled && !market.isFrozen,
|
|
48
|
+
canBeSupplied: market.isActive && !market.isFrozen,
|
|
49
|
+
canBeWithdrawn: market.isActive,
|
|
50
|
+
canBePayBacked: market.isActive,
|
|
51
|
+
disabledStableBorrowing: !market.stableBorrowRateEnabled,
|
|
52
|
+
totalBorrow: assetAmountInEth(market.totalBorrow.toString(), selectedMarket.assets[i]),
|
|
53
|
+
totalBorrowVar: assetAmountInEth(market.totalBorrowVar.toString(), selectedMarket.assets[i]),
|
|
54
|
+
priceInEth: new Dec(market.price.toString()).div(1e18).toString(),
|
|
55
|
+
incentiveSupplyToken: 'AAVE',
|
|
56
|
+
incentiveBorrowToken: 'AAVE',
|
|
57
|
+
incentiveSupplyApy: '0',
|
|
58
|
+
price: new Dec(market.price.toString()).div(1e18).mul(ethPrice).toString(),
|
|
59
|
+
}));
|
|
60
|
+
|
|
61
|
+
const stEthMarket = markets.find(({ symbol }) => symbol === 'stETH');
|
|
62
|
+
if (stEthMarket) {
|
|
63
|
+
stEthMarket.incentiveSupplyApy = await getStETHApy(mainnetWeb3);
|
|
64
|
+
stEthMarket.incentiveSupplyToken = 'stETH';
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const payload: AaveV2AssetsData = {};
|
|
68
|
+
// Sort by market size
|
|
69
|
+
markets
|
|
70
|
+
.sort((a, b) => {
|
|
71
|
+
const aMarket = new Dec(a.price).times(a.totalSupply).toString();
|
|
72
|
+
const bMarket = new Dec(b.price).times(b.totalSupply).toString();
|
|
73
|
+
|
|
74
|
+
return new Dec(bMarket).minus(aMarket).toNumber();
|
|
75
|
+
})
|
|
76
|
+
.forEach((assetData: AaveV2AssetData, i) => {
|
|
77
|
+
payload[assetData.symbol] = { ...assetData, sortIndex: i };
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
return { assetsData: payload };
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
export const getAaveV2AccountBalances = async (web3: Web3, network: NetworkNumber, block: Blockish, addressMapping: boolean, address: EthAddress): Promise<PositionBalances> => {
|
|
84
|
+
let balances: PositionBalances = {
|
|
85
|
+
collateral: {},
|
|
86
|
+
debt: {},
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
if (!address) {
|
|
90
|
+
return balances;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const market = AAVE_V2;
|
|
94
|
+
|
|
95
|
+
const loanInfoContract = AaveLoanInfoV2Contract(web3, network, block);
|
|
96
|
+
|
|
97
|
+
const marketAddress = market.providerAddress;
|
|
98
|
+
// @ts-ignore
|
|
99
|
+
const protocolDataProviderContract = createContractWrapper(web3, network, market.protocolData, market.protocolDataAddress);
|
|
100
|
+
|
|
101
|
+
const reserveTokens = await protocolDataProviderContract.methods.getAllReservesTokens().call({}, block);
|
|
102
|
+
const symbols = reserveTokens.map(({ symbol }: { symbol: string }) => symbol);
|
|
103
|
+
const _addresses = reserveTokens.map(({ tokenAddress }: { tokenAddress: EthAddress }) => tokenAddress);
|
|
104
|
+
|
|
105
|
+
const loanInfo = await loanInfoContract.methods.getTokenBalances(marketAddress, address, _addresses).call({}, block);
|
|
106
|
+
|
|
107
|
+
loanInfo.forEach((_tokenInfo: any, i: number) => {
|
|
108
|
+
const asset = wethToEth(symbols[i]);
|
|
109
|
+
const assetAddr = wethToEthByAddress(_addresses[i], network).toLowerCase();
|
|
110
|
+
const tokenInfo = { ..._tokenInfo };
|
|
111
|
+
|
|
112
|
+
// known bug: stETH leaves 1 wei on every transfer
|
|
113
|
+
if (asset === 'stETH' && tokenInfo.balance.toString() === '1') {
|
|
114
|
+
tokenInfo.balance = '0';
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
balances = {
|
|
118
|
+
collateral: {
|
|
119
|
+
...balances.collateral,
|
|
120
|
+
[addressMapping ? assetAddr : asset]: tokenInfo.balance.toString(),
|
|
121
|
+
},
|
|
122
|
+
debt: {
|
|
123
|
+
...balances.debt,
|
|
124
|
+
[addressMapping ? assetAddr : asset]: new Dec(tokenInfo.borrowsStable.toString()).add(tokenInfo.borrowsVariable.toString()).toString(),
|
|
125
|
+
},
|
|
126
|
+
};
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
return balances;
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
export const getAaveV2AccountData = async (web3: Web3, network: NetworkNumber, address: string, assetsData: AaveV2AssetsData, market: AaveMarketInfo): Promise<AaveV2PositionData> => {
|
|
133
|
+
if (!address) throw new Error('Address is required');
|
|
134
|
+
|
|
135
|
+
let payload: AaveV2PositionData = {
|
|
136
|
+
...EMPTY_AAVE_DATA,
|
|
137
|
+
lastUpdated: Date.now(),
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
const loanInfoContract = AaveLoanInfoV2Contract(web3, network);
|
|
141
|
+
const marketAddress = market.providerAddress;
|
|
142
|
+
const _addresses = market.assets.map(a => getAssetInfo(ethToWeth(a)).address);
|
|
143
|
+
const loanInfo = await loanInfoContract.methods.getTokenBalances(marketAddress, address, _addresses).call();
|
|
144
|
+
const usedAssets = {} as AaveV2UsedAssets;
|
|
145
|
+
loanInfo.forEach((_tokenInfo, i) => {
|
|
146
|
+
const asset = market.assets[i];
|
|
147
|
+
const tokenInfo = { ..._tokenInfo };
|
|
148
|
+
|
|
149
|
+
// known bug: stETH leaves 1 wei on every transfer
|
|
150
|
+
if (asset === 'stETH' && tokenInfo.balance.toString() === '1') tokenInfo.balance = '0';
|
|
151
|
+
|
|
152
|
+
const isSupplied = tokenInfo.balance.toString() !== '0';
|
|
153
|
+
const isBorrowed = tokenInfo.borrowsStable.toString() !== '0' || tokenInfo.borrowsVariable.toString() !== '0';
|
|
154
|
+
if (!isSupplied && !isBorrowed) return;
|
|
155
|
+
|
|
156
|
+
const supplied = assetAmountInEth(tokenInfo.balance.toString(), asset);
|
|
157
|
+
const borrowedStable = assetAmountInEth(tokenInfo.borrowsStable.toString(), asset);
|
|
158
|
+
const borrowedVariable = assetAmountInEth(tokenInfo.borrowsVariable.toString(), asset);
|
|
159
|
+
const enabledAsCollateral = assetsData[asset].usageAsCollateralEnabled ? tokenInfo.enabledAsCollateral : false;
|
|
160
|
+
let interestMode;
|
|
161
|
+
if (borrowedVariable === '0' && borrowedStable !== '0') {
|
|
162
|
+
interestMode = '1';
|
|
163
|
+
} else if (borrowedVariable !== '0' && borrowedStable === '0') {
|
|
164
|
+
interestMode = '2';
|
|
165
|
+
} else {
|
|
166
|
+
interestMode = 'both';
|
|
167
|
+
}
|
|
168
|
+
if (!usedAssets[asset]) usedAssets[asset] = {} as AaveV2UsedAsset;
|
|
169
|
+
usedAssets[asset] = {
|
|
170
|
+
...usedAssets[asset],
|
|
171
|
+
symbol: asset,
|
|
172
|
+
supplied,
|
|
173
|
+
suppliedUsd: new Dec(supplied).mul(assetsData[asset].price).toString(),
|
|
174
|
+
isSupplied,
|
|
175
|
+
collateral: enabledAsCollateral,
|
|
176
|
+
stableBorrowRate: aprToApy(new Dec(tokenInfo.stableBorrowRate).div(1e25).toString()),
|
|
177
|
+
borrowedStable,
|
|
178
|
+
borrowedVariable,
|
|
179
|
+
borrowedUsdStable: new Dec(borrowedStable).mul(assetsData[asset].price).toString(),
|
|
180
|
+
borrowedUsdVariable: new Dec(borrowedVariable).mul(assetsData[asset].price).toString(),
|
|
181
|
+
borrowed: new Dec(borrowedStable).add(borrowedVariable).toString(),
|
|
182
|
+
borrowedUsd: new Dec(new Dec(borrowedVariable).add(borrowedStable)).mul(assetsData[asset].price).toString(),
|
|
183
|
+
isBorrowed,
|
|
184
|
+
interestMode,
|
|
185
|
+
};
|
|
186
|
+
});
|
|
187
|
+
payload = {
|
|
188
|
+
...payload,
|
|
189
|
+
usedAssets,
|
|
190
|
+
...aaveAnyGetAggregatedPositionData({
|
|
191
|
+
usedAssets, assetsData, eModeCategory: 0, selectedMarket: market,
|
|
192
|
+
}),
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
payload.ratio = payload.borrowedUsd && payload.borrowedUsd !== '0'
|
|
196
|
+
? new Dec(payload.borrowLimitUsd).div(payload.borrowedUsd).mul(100).toString()
|
|
197
|
+
: '0';
|
|
198
|
+
payload.minRatio = '100';
|
|
199
|
+
payload.collRatio = payload.borrowedUsd && payload.borrowedUsd !== '0'
|
|
200
|
+
? new Dec(payload.suppliedCollateralUsd).div(payload.borrowedUsd).mul(100).toString()
|
|
201
|
+
: '0';
|
|
202
|
+
|
|
203
|
+
// Calculate borrow limits per asset
|
|
204
|
+
Object.values(payload.usedAssets).forEach((item) => {
|
|
205
|
+
if (item.isBorrowed) {
|
|
206
|
+
// eslint-disable-next-line no-param-reassign
|
|
207
|
+
item.stableLimit = calculateBorrowingAssetLimit(item.borrowedUsdStable, payload.borrowLimitUsd);
|
|
208
|
+
// eslint-disable-next-line no-param-reassign
|
|
209
|
+
item.variableLimit = calculateBorrowingAssetLimit(item.borrowedUsdVariable, payload.borrowLimitUsd);
|
|
210
|
+
// eslint-disable-next-line no-param-reassign
|
|
211
|
+
item.limit = calculateBorrowingAssetLimit(item.borrowedUsd, payload.borrowLimitUsd);
|
|
212
|
+
}
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
const { netApy, incentiveUsd, totalInterestUsd } = calculateNetApy({ usedAssets, assetsData });
|
|
216
|
+
payload.netApy = netApy;
|
|
217
|
+
payload.incentiveUsd = incentiveUsd;
|
|
218
|
+
payload.totalInterestUsd = totalInterestUsd;
|
|
219
|
+
|
|
220
|
+
return payload;
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
export const getAaveV2FullPositionData = async (web3: Web3, network: NetworkNumber, address: string, market: AaveMarketInfo, mainnetWeb3: Web3): Promise<AaveV2PositionData> => {
|
|
224
|
+
const marketData = await getAaveV2MarketsData(web3, network, market, mainnetWeb3);
|
|
225
|
+
const positionData = await getAaveV2AccountData(web3, network, address, marketData.assetsData, market);
|
|
226
|
+
return positionData;
|
|
227
|
+
};
|