@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/src/compoundV2/index.ts
CHANGED
|
@@ -1,220 +1,220 @@
|
|
|
1
|
-
import { assetAmountInEth, getAssetInfo, getAssetInfoByAddress } from '@defisaver/tokens';
|
|
2
|
-
import Dec from 'decimal.js';
|
|
3
|
-
import Web3 from 'web3';
|
|
4
|
-
import { BLOCKS_IN_A_YEAR } from '../constants';
|
|
5
|
-
import { aprToApy } from '../moneymarket';
|
|
6
|
-
import { compareAddresses, handleWbtcLegacy, wethToEth } from '../services/utils';
|
|
7
|
-
import {
|
|
8
|
-
Blockish, EthAddress, NetworkNumber, PositionBalances,
|
|
9
|
-
} from '../types/common';
|
|
10
|
-
import { CompoundLoanInfoContract, ComptrollerContract } from '../contracts';
|
|
11
|
-
import { compoundV2CollateralAssets } from '../markets';
|
|
12
|
-
import {
|
|
13
|
-
CompoundV2AssetsData, CompoundV2MarketsData, CompoundV2PositionData, CompoundV2UsedAsset, CompoundV2UsedAssets,
|
|
14
|
-
} from '../types';
|
|
15
|
-
import { getCompoundV2AggregatedData } from '../helpers/compoundHelpers';
|
|
16
|
-
|
|
17
|
-
const compAddress = '0xc00e94cb662c3520282e6f5717214004a7f26888';
|
|
18
|
-
|
|
19
|
-
export const getCompoundV2MarketsData = async (web3: Web3, network: NetworkNumber): Promise<CompoundV2MarketsData> => {
|
|
20
|
-
const cAddresses = compoundV2CollateralAssets.map(a => a.address);
|
|
21
|
-
|
|
22
|
-
const loanInfoContract = CompoundLoanInfoContract(web3, network);
|
|
23
|
-
const loanInfo = await loanInfoContract.methods.getFullTokensInfo(cAddresses).call();
|
|
24
|
-
|
|
25
|
-
const compPrice = loanInfo.find(m => compareAddresses(m.underlyingTokenAddress, compAddress))!.price.toString();
|
|
26
|
-
|
|
27
|
-
const assetsData = loanInfo
|
|
28
|
-
.map((market, i) => {
|
|
29
|
-
let symbol = getAssetInfoByAddress(cAddresses[i]).underlyingAsset;
|
|
30
|
-
let isWbtcLegacy = false;
|
|
31
|
-
const totalSupply = new Dec(market.totalSupply.toString()).div(1e18).times(market.exchangeRate.toString());
|
|
32
|
-
const totalBorrow = market.totalBorrow.toString();
|
|
33
|
-
const borrowCap = market.borrowCap.toString();
|
|
34
|
-
const compSupplySpeeds = market.compSupplySpeeds.toString();
|
|
35
|
-
const compBorrowSpeeds = market.compBorrowSpeeds.toString();
|
|
36
|
-
const assetPrice = market.price.toString();
|
|
37
|
-
|
|
38
|
-
const pricePrecisionDiff = 18 - getAssetInfo(getAssetInfoByAddress(cAddresses[i]).underlyingAsset).decimals;
|
|
39
|
-
|
|
40
|
-
// compSupplySpeeds/compBorrowSpeeds is per block per market (borrow & supply are separate markets)
|
|
41
|
-
const incentiveSupplyApy = aprToApy((100 * BLOCKS_IN_A_YEAR * +compSupplySpeeds * +compPrice) / +assetPrice / +totalSupply).toString();
|
|
42
|
-
const incentiveBorrowApy = aprToApy((100 * BLOCKS_IN_A_YEAR * +compBorrowSpeeds * +compPrice) / +assetPrice / +totalBorrow).toString();
|
|
43
|
-
|
|
44
|
-
if (cAddresses[i].toLowerCase() === '0xc11b1268c1a384e55c48c2391d8d480264a3a7f4'.toLowerCase()) {
|
|
45
|
-
symbol = 'WBTC Legacy';
|
|
46
|
-
isWbtcLegacy = true;
|
|
47
|
-
}
|
|
48
|
-
return {
|
|
49
|
-
symbol,
|
|
50
|
-
underlyingTokenAddress: market.underlyingTokenAddress,
|
|
51
|
-
supplyRate: aprToApy(new Dec(BLOCKS_IN_A_YEAR).times(market.supplyRate.toString()).div(1e16).toString()).toString(),
|
|
52
|
-
borrowRate: aprToApy(new Dec(BLOCKS_IN_A_YEAR).times(market.borrowRate.toString()).div(1e16).toString()).toString(),
|
|
53
|
-
incentiveSupplyToken: 'COMP',
|
|
54
|
-
incentiveBorrowToken: 'COMP',
|
|
55
|
-
incentiveSupplyApy,
|
|
56
|
-
incentiveBorrowApy,
|
|
57
|
-
collateralFactor: new Dec(market.collateralFactor.toString()).div(1e18).toString(),
|
|
58
|
-
marketLiquidity: assetAmountInEth(market.marketLiquidity.toString(), handleWbtcLegacy(symbol)),
|
|
59
|
-
utilization: new Dec(market.totalBorrow.toString()).div(totalSupply).times(100).toString(),
|
|
60
|
-
totalSupply: assetAmountInEth(totalSupply, handleWbtcLegacy(symbol)),
|
|
61
|
-
totalBorrow: assetAmountInEth(totalBorrow, handleWbtcLegacy(symbol)),
|
|
62
|
-
exchangeRate: new Dec(market.exchangeRate.toString()).div(1e28).toString(),
|
|
63
|
-
borrowCap: assetAmountInEth(borrowCap, handleWbtcLegacy(symbol)),
|
|
64
|
-
canBeBorrowed: market.canBorrow,
|
|
65
|
-
canBeSupplied: market.canMint,
|
|
66
|
-
price: new Dec(market.price.toString()).div(10 ** (18 + pricePrecisionDiff)).toString(),
|
|
67
|
-
};
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
const payload = {} as CompoundV2AssetsData;
|
|
71
|
-
// Sort by market size
|
|
72
|
-
assetsData
|
|
73
|
-
.sort((a, b) => {
|
|
74
|
-
const aMarket = new Dec(a.price).times(a.totalSupply).toString();
|
|
75
|
-
const bMarket = new Dec(b.price).times(b.totalSupply).toString();
|
|
76
|
-
|
|
77
|
-
return new Dec(bMarket).minus(aMarket).toNumber();
|
|
78
|
-
})
|
|
79
|
-
.forEach((market, i) => { payload[market.symbol] = { ...market, sortIndex: i }; });
|
|
80
|
-
|
|
81
|
-
return { assetsData: payload };
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
export const EMPTY_COMPOUND_DATA = {
|
|
85
|
-
usedAssets: {},
|
|
86
|
-
suppliedUsd: '0',
|
|
87
|
-
borrowedUsd: '0',
|
|
88
|
-
borrowLimitUsd: '0',
|
|
89
|
-
leftToBorrowUsd: '0',
|
|
90
|
-
ratio: '0',
|
|
91
|
-
minRatio: '0',
|
|
92
|
-
netApy: '0',
|
|
93
|
-
incentiveUsd: '0',
|
|
94
|
-
totalInterestUsd: '0',
|
|
95
|
-
borrowStableSupplyUnstable: false,
|
|
96
|
-
};
|
|
97
|
-
|
|
98
|
-
export const getCollateralAssetsAddresses = async (web3: Web3, network: NetworkNumber, account: string) => {
|
|
99
|
-
const contract = ComptrollerContract(web3, network);
|
|
100
|
-
|
|
101
|
-
return contract.methods.getAssetsIn(account).call();
|
|
102
|
-
};
|
|
103
|
-
|
|
104
|
-
export const getAllMarketAddresses = async (web3: Web3, network: NetworkNumber, block: Blockish) => {
|
|
105
|
-
const contract = ComptrollerContract(web3, network);
|
|
106
|
-
|
|
107
|
-
return contract.methods.getAllMarkets().call({}, block);
|
|
108
|
-
};
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
export const getCompoundV2AccountBalances = async (web3: Web3, network: NetworkNumber, block: Blockish, addressMapping: boolean, address: EthAddress): Promise<PositionBalances> => {
|
|
112
|
-
let balances: PositionBalances = {
|
|
113
|
-
collateral: {},
|
|
114
|
-
debt: {},
|
|
115
|
-
};
|
|
116
|
-
|
|
117
|
-
if (!address) {
|
|
118
|
-
return balances;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
const assets = await getAllMarketAddresses(web3, network, block);
|
|
122
|
-
const assetInfo = assets.map(a => getAssetInfoByAddress(a, network));
|
|
123
|
-
const loanInfoContract = CompoundLoanInfoContract(web3, network, block);
|
|
124
|
-
const loanInfo = await loanInfoContract.methods.getTokenBalances(address, assets).call({}, block);
|
|
125
|
-
|
|
126
|
-
loanInfo.balances.forEach((weiAmount: any, i: number) => {
|
|
127
|
-
const asset = wethToEth(
|
|
128
|
-
assetInfo[i].symbol === 'cWBTC Legacy'
|
|
129
|
-
? `${assetInfo[i].underlyingAsset} Legacy`
|
|
130
|
-
: assetInfo[i].underlyingAsset,
|
|
131
|
-
);
|
|
132
|
-
|
|
133
|
-
balances = {
|
|
134
|
-
collateral: {
|
|
135
|
-
...balances.collateral,
|
|
136
|
-
[addressMapping ? getAssetInfo(asset, network).address.toLowerCase() : asset]: weiAmount.toString(),
|
|
137
|
-
},
|
|
138
|
-
};
|
|
139
|
-
});
|
|
140
|
-
loanInfo.borrows.forEach((weiAmount: any, i: number) => {
|
|
141
|
-
const asset = wethToEth(
|
|
142
|
-
assetInfo[i].symbol === 'cWBTC Legacy'
|
|
143
|
-
? `${assetInfo[i].underlyingAsset} Legacy`
|
|
144
|
-
: assetInfo[i].underlyingAsset,
|
|
145
|
-
);
|
|
146
|
-
|
|
147
|
-
balances = {
|
|
148
|
-
...balances,
|
|
149
|
-
debt: {
|
|
150
|
-
...balances.debt,
|
|
151
|
-
[addressMapping ? getAssetInfo(asset, network).address.toLowerCase() : asset]: weiAmount.toString(),
|
|
152
|
-
},
|
|
153
|
-
};
|
|
154
|
-
});
|
|
155
|
-
|
|
156
|
-
return balances;
|
|
157
|
-
};
|
|
158
|
-
|
|
159
|
-
export const getCompoundV2AccountData = async (web3: Web3, network: NetworkNumber, address: string, assetsData: CompoundV2AssetsData): Promise<CompoundV2PositionData> => {
|
|
160
|
-
if (!address) throw new Error('No address provided');
|
|
161
|
-
|
|
162
|
-
let payload = { ...EMPTY_COMPOUND_DATA, lastUpdated: Date.now() };
|
|
163
|
-
|
|
164
|
-
const loanInfoContract = CompoundLoanInfoContract(web3, network);
|
|
165
|
-
const loanInfo = await loanInfoContract.methods.getTokenBalances(address, compoundV2CollateralAssets.map(a => a.address)).call();
|
|
166
|
-
const collateralAssetsAddresses = await getCollateralAssetsAddresses(web3, network, address);
|
|
167
|
-
|
|
168
|
-
const usedAssets = {} as CompoundV2UsedAssets;
|
|
169
|
-
|
|
170
|
-
loanInfo.balances.forEach((weiAmount: string, i: number) => {
|
|
171
|
-
const asset = compoundV2CollateralAssets[i].symbol === 'cWBTC Legacy'
|
|
172
|
-
? `${compoundV2CollateralAssets[i].underlyingAsset} Legacy`
|
|
173
|
-
: compoundV2CollateralAssets[i].underlyingAsset;
|
|
174
|
-
const amount = assetAmountInEth(weiAmount.toString(), asset);
|
|
175
|
-
const collateral = !!collateralAssetsAddresses.find(a => compareAddresses(a, compoundV2CollateralAssets[i].address));
|
|
176
|
-
if (weiAmount.toString() === '0' && !collateral) return;
|
|
177
|
-
if (!usedAssets[asset]) usedAssets[asset] = {} as CompoundV2UsedAsset;
|
|
178
|
-
usedAssets[asset] = {
|
|
179
|
-
...usedAssets[asset],
|
|
180
|
-
symbol: asset,
|
|
181
|
-
supplied: amount,
|
|
182
|
-
suppliedUsd: new Dec(amount).mul(assetsData[handleWbtcLegacy(asset)].price).toString(),
|
|
183
|
-
isSupplied: +amount > 0,
|
|
184
|
-
collateral,
|
|
185
|
-
};
|
|
186
|
-
});
|
|
187
|
-
|
|
188
|
-
loanInfo.borrows.forEach((weiAmount, i) => {
|
|
189
|
-
if (weiAmount.toString() === '0') return;
|
|
190
|
-
const asset = compoundV2CollateralAssets[i].symbol === 'cWBTC Legacy'
|
|
191
|
-
? `${compoundV2CollateralAssets[i].underlyingAsset} Legacy`
|
|
192
|
-
: compoundV2CollateralAssets[i].underlyingAsset;
|
|
193
|
-
const amount = assetAmountInEth(weiAmount.toString(), asset);
|
|
194
|
-
if (!usedAssets[asset]) usedAssets[asset] = {} as CompoundV2UsedAsset;
|
|
195
|
-
usedAssets[asset] = {
|
|
196
|
-
...usedAssets[asset],
|
|
197
|
-
symbol: asset,
|
|
198
|
-
borrowed: amount,
|
|
199
|
-
borrowedUsd: new Dec(amount).mul(assetsData[handleWbtcLegacy(asset)].price).toString(),
|
|
200
|
-
isBorrowed: true,
|
|
201
|
-
collateral: !!usedAssets[asset].collateral,
|
|
202
|
-
};
|
|
203
|
-
});
|
|
204
|
-
|
|
205
|
-
payload = {
|
|
206
|
-
...payload,
|
|
207
|
-
usedAssets,
|
|
208
|
-
...getCompoundV2AggregatedData({
|
|
209
|
-
usedAssets, assetsData,
|
|
210
|
-
}),
|
|
211
|
-
};
|
|
212
|
-
|
|
213
|
-
return payload;
|
|
214
|
-
};
|
|
215
|
-
|
|
216
|
-
export const getCompoundV2FullPositionData = async (web3: Web3, network: NetworkNumber, address: string): Promise<CompoundV2PositionData> => {
|
|
217
|
-
const marketData = await getCompoundV2MarketsData(web3, network);
|
|
218
|
-
const positionData = await getCompoundV2AccountData(web3, network, address, marketData.assetsData);
|
|
219
|
-
return positionData;
|
|
220
|
-
};
|
|
1
|
+
import { assetAmountInEth, getAssetInfo, getAssetInfoByAddress } from '@defisaver/tokens';
|
|
2
|
+
import Dec from 'decimal.js';
|
|
3
|
+
import Web3 from 'web3';
|
|
4
|
+
import { BLOCKS_IN_A_YEAR } from '../constants';
|
|
5
|
+
import { aprToApy } from '../moneymarket';
|
|
6
|
+
import { compareAddresses, handleWbtcLegacy, wethToEth } from '../services/utils';
|
|
7
|
+
import {
|
|
8
|
+
Blockish, EthAddress, NetworkNumber, PositionBalances,
|
|
9
|
+
} from '../types/common';
|
|
10
|
+
import { CompoundLoanInfoContract, ComptrollerContract } from '../contracts';
|
|
11
|
+
import { compoundV2CollateralAssets } from '../markets';
|
|
12
|
+
import {
|
|
13
|
+
CompoundV2AssetsData, CompoundV2MarketsData, CompoundV2PositionData, CompoundV2UsedAsset, CompoundV2UsedAssets,
|
|
14
|
+
} from '../types';
|
|
15
|
+
import { getCompoundV2AggregatedData } from '../helpers/compoundHelpers';
|
|
16
|
+
|
|
17
|
+
const compAddress = '0xc00e94cb662c3520282e6f5717214004a7f26888';
|
|
18
|
+
|
|
19
|
+
export const getCompoundV2MarketsData = async (web3: Web3, network: NetworkNumber): Promise<CompoundV2MarketsData> => {
|
|
20
|
+
const cAddresses = compoundV2CollateralAssets.map(a => a.address);
|
|
21
|
+
|
|
22
|
+
const loanInfoContract = CompoundLoanInfoContract(web3, network);
|
|
23
|
+
const loanInfo = await loanInfoContract.methods.getFullTokensInfo(cAddresses).call();
|
|
24
|
+
|
|
25
|
+
const compPrice = loanInfo.find(m => compareAddresses(m.underlyingTokenAddress, compAddress))!.price.toString();
|
|
26
|
+
|
|
27
|
+
const assetsData = loanInfo
|
|
28
|
+
.map((market, i) => {
|
|
29
|
+
let symbol = getAssetInfoByAddress(cAddresses[i]).underlyingAsset;
|
|
30
|
+
let isWbtcLegacy = false;
|
|
31
|
+
const totalSupply = new Dec(market.totalSupply.toString()).div(1e18).times(market.exchangeRate.toString());
|
|
32
|
+
const totalBorrow = market.totalBorrow.toString();
|
|
33
|
+
const borrowCap = market.borrowCap.toString();
|
|
34
|
+
const compSupplySpeeds = market.compSupplySpeeds.toString();
|
|
35
|
+
const compBorrowSpeeds = market.compBorrowSpeeds.toString();
|
|
36
|
+
const assetPrice = market.price.toString();
|
|
37
|
+
|
|
38
|
+
const pricePrecisionDiff = 18 - getAssetInfo(getAssetInfoByAddress(cAddresses[i]).underlyingAsset).decimals;
|
|
39
|
+
|
|
40
|
+
// compSupplySpeeds/compBorrowSpeeds is per block per market (borrow & supply are separate markets)
|
|
41
|
+
const incentiveSupplyApy = aprToApy((100 * BLOCKS_IN_A_YEAR * +compSupplySpeeds * +compPrice) / +assetPrice / +totalSupply).toString();
|
|
42
|
+
const incentiveBorrowApy = aprToApy((100 * BLOCKS_IN_A_YEAR * +compBorrowSpeeds * +compPrice) / +assetPrice / +totalBorrow).toString();
|
|
43
|
+
|
|
44
|
+
if (cAddresses[i].toLowerCase() === '0xc11b1268c1a384e55c48c2391d8d480264a3a7f4'.toLowerCase()) {
|
|
45
|
+
symbol = 'WBTC Legacy';
|
|
46
|
+
isWbtcLegacy = true;
|
|
47
|
+
}
|
|
48
|
+
return {
|
|
49
|
+
symbol,
|
|
50
|
+
underlyingTokenAddress: market.underlyingTokenAddress,
|
|
51
|
+
supplyRate: aprToApy(new Dec(BLOCKS_IN_A_YEAR).times(market.supplyRate.toString()).div(1e16).toString()).toString(),
|
|
52
|
+
borrowRate: aprToApy(new Dec(BLOCKS_IN_A_YEAR).times(market.borrowRate.toString()).div(1e16).toString()).toString(),
|
|
53
|
+
incentiveSupplyToken: 'COMP',
|
|
54
|
+
incentiveBorrowToken: 'COMP',
|
|
55
|
+
incentiveSupplyApy,
|
|
56
|
+
incentiveBorrowApy,
|
|
57
|
+
collateralFactor: new Dec(market.collateralFactor.toString()).div(1e18).toString(),
|
|
58
|
+
marketLiquidity: assetAmountInEth(market.marketLiquidity.toString(), handleWbtcLegacy(symbol)),
|
|
59
|
+
utilization: new Dec(market.totalBorrow.toString()).div(totalSupply).times(100).toString(),
|
|
60
|
+
totalSupply: assetAmountInEth(totalSupply, handleWbtcLegacy(symbol)),
|
|
61
|
+
totalBorrow: assetAmountInEth(totalBorrow, handleWbtcLegacy(symbol)),
|
|
62
|
+
exchangeRate: new Dec(market.exchangeRate.toString()).div(1e28).toString(),
|
|
63
|
+
borrowCap: assetAmountInEth(borrowCap, handleWbtcLegacy(symbol)),
|
|
64
|
+
canBeBorrowed: market.canBorrow,
|
|
65
|
+
canBeSupplied: market.canMint,
|
|
66
|
+
price: new Dec(market.price.toString()).div(10 ** (18 + pricePrecisionDiff)).toString(),
|
|
67
|
+
};
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
const payload = {} as CompoundV2AssetsData;
|
|
71
|
+
// Sort by market size
|
|
72
|
+
assetsData
|
|
73
|
+
.sort((a, b) => {
|
|
74
|
+
const aMarket = new Dec(a.price).times(a.totalSupply).toString();
|
|
75
|
+
const bMarket = new Dec(b.price).times(b.totalSupply).toString();
|
|
76
|
+
|
|
77
|
+
return new Dec(bMarket).minus(aMarket).toNumber();
|
|
78
|
+
})
|
|
79
|
+
.forEach((market, i) => { payload[market.symbol] = { ...market, sortIndex: i }; });
|
|
80
|
+
|
|
81
|
+
return { assetsData: payload };
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
export const EMPTY_COMPOUND_DATA = {
|
|
85
|
+
usedAssets: {},
|
|
86
|
+
suppliedUsd: '0',
|
|
87
|
+
borrowedUsd: '0',
|
|
88
|
+
borrowLimitUsd: '0',
|
|
89
|
+
leftToBorrowUsd: '0',
|
|
90
|
+
ratio: '0',
|
|
91
|
+
minRatio: '0',
|
|
92
|
+
netApy: '0',
|
|
93
|
+
incentiveUsd: '0',
|
|
94
|
+
totalInterestUsd: '0',
|
|
95
|
+
borrowStableSupplyUnstable: false,
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
export const getCollateralAssetsAddresses = async (web3: Web3, network: NetworkNumber, account: string) => {
|
|
99
|
+
const contract = ComptrollerContract(web3, network);
|
|
100
|
+
|
|
101
|
+
return contract.methods.getAssetsIn(account).call();
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
export const getAllMarketAddresses = async (web3: Web3, network: NetworkNumber, block: Blockish) => {
|
|
105
|
+
const contract = ComptrollerContract(web3, network);
|
|
106
|
+
|
|
107
|
+
return contract.methods.getAllMarkets().call({}, block);
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
export const getCompoundV2AccountBalances = async (web3: Web3, network: NetworkNumber, block: Blockish, addressMapping: boolean, address: EthAddress): Promise<PositionBalances> => {
|
|
112
|
+
let balances: PositionBalances = {
|
|
113
|
+
collateral: {},
|
|
114
|
+
debt: {},
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
if (!address) {
|
|
118
|
+
return balances;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const assets = await getAllMarketAddresses(web3, network, block);
|
|
122
|
+
const assetInfo = assets.map(a => getAssetInfoByAddress(a, network));
|
|
123
|
+
const loanInfoContract = CompoundLoanInfoContract(web3, network, block);
|
|
124
|
+
const loanInfo = await loanInfoContract.methods.getTokenBalances(address, assets).call({}, block);
|
|
125
|
+
|
|
126
|
+
loanInfo.balances.forEach((weiAmount: any, i: number) => {
|
|
127
|
+
const asset = wethToEth(
|
|
128
|
+
assetInfo[i].symbol === 'cWBTC Legacy'
|
|
129
|
+
? `${assetInfo[i].underlyingAsset} Legacy`
|
|
130
|
+
: assetInfo[i].underlyingAsset,
|
|
131
|
+
);
|
|
132
|
+
|
|
133
|
+
balances = {
|
|
134
|
+
collateral: {
|
|
135
|
+
...balances.collateral,
|
|
136
|
+
[addressMapping ? getAssetInfo(asset, network).address.toLowerCase() : asset]: weiAmount.toString(),
|
|
137
|
+
},
|
|
138
|
+
};
|
|
139
|
+
});
|
|
140
|
+
loanInfo.borrows.forEach((weiAmount: any, i: number) => {
|
|
141
|
+
const asset = wethToEth(
|
|
142
|
+
assetInfo[i].symbol === 'cWBTC Legacy'
|
|
143
|
+
? `${assetInfo[i].underlyingAsset} Legacy`
|
|
144
|
+
: assetInfo[i].underlyingAsset,
|
|
145
|
+
);
|
|
146
|
+
|
|
147
|
+
balances = {
|
|
148
|
+
...balances,
|
|
149
|
+
debt: {
|
|
150
|
+
...balances.debt,
|
|
151
|
+
[addressMapping ? getAssetInfo(asset, network).address.toLowerCase() : asset]: weiAmount.toString(),
|
|
152
|
+
},
|
|
153
|
+
};
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
return balances;
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
export const getCompoundV2AccountData = async (web3: Web3, network: NetworkNumber, address: string, assetsData: CompoundV2AssetsData): Promise<CompoundV2PositionData> => {
|
|
160
|
+
if (!address) throw new Error('No address provided');
|
|
161
|
+
|
|
162
|
+
let payload = { ...EMPTY_COMPOUND_DATA, lastUpdated: Date.now() };
|
|
163
|
+
|
|
164
|
+
const loanInfoContract = CompoundLoanInfoContract(web3, network);
|
|
165
|
+
const loanInfo = await loanInfoContract.methods.getTokenBalances(address, compoundV2CollateralAssets.map(a => a.address)).call();
|
|
166
|
+
const collateralAssetsAddresses = await getCollateralAssetsAddresses(web3, network, address);
|
|
167
|
+
|
|
168
|
+
const usedAssets = {} as CompoundV2UsedAssets;
|
|
169
|
+
|
|
170
|
+
loanInfo.balances.forEach((weiAmount: string, i: number) => {
|
|
171
|
+
const asset = compoundV2CollateralAssets[i].symbol === 'cWBTC Legacy'
|
|
172
|
+
? `${compoundV2CollateralAssets[i].underlyingAsset} Legacy`
|
|
173
|
+
: compoundV2CollateralAssets[i].underlyingAsset;
|
|
174
|
+
const amount = assetAmountInEth(weiAmount.toString(), asset);
|
|
175
|
+
const collateral = !!collateralAssetsAddresses.find(a => compareAddresses(a, compoundV2CollateralAssets[i].address));
|
|
176
|
+
if (weiAmount.toString() === '0' && !collateral) return;
|
|
177
|
+
if (!usedAssets[asset]) usedAssets[asset] = {} as CompoundV2UsedAsset;
|
|
178
|
+
usedAssets[asset] = {
|
|
179
|
+
...usedAssets[asset],
|
|
180
|
+
symbol: asset,
|
|
181
|
+
supplied: amount,
|
|
182
|
+
suppliedUsd: new Dec(amount).mul(assetsData[handleWbtcLegacy(asset)].price).toString(),
|
|
183
|
+
isSupplied: +amount > 0,
|
|
184
|
+
collateral,
|
|
185
|
+
};
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
loanInfo.borrows.forEach((weiAmount, i) => {
|
|
189
|
+
if (weiAmount.toString() === '0') return;
|
|
190
|
+
const asset = compoundV2CollateralAssets[i].symbol === 'cWBTC Legacy'
|
|
191
|
+
? `${compoundV2CollateralAssets[i].underlyingAsset} Legacy`
|
|
192
|
+
: compoundV2CollateralAssets[i].underlyingAsset;
|
|
193
|
+
const amount = assetAmountInEth(weiAmount.toString(), asset);
|
|
194
|
+
if (!usedAssets[asset]) usedAssets[asset] = {} as CompoundV2UsedAsset;
|
|
195
|
+
usedAssets[asset] = {
|
|
196
|
+
...usedAssets[asset],
|
|
197
|
+
symbol: asset,
|
|
198
|
+
borrowed: amount,
|
|
199
|
+
borrowedUsd: new Dec(amount).mul(assetsData[handleWbtcLegacy(asset)].price).toString(),
|
|
200
|
+
isBorrowed: true,
|
|
201
|
+
collateral: !!usedAssets[asset].collateral,
|
|
202
|
+
};
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
payload = {
|
|
206
|
+
...payload,
|
|
207
|
+
usedAssets,
|
|
208
|
+
...getCompoundV2AggregatedData({
|
|
209
|
+
usedAssets, assetsData,
|
|
210
|
+
}),
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
return payload;
|
|
214
|
+
};
|
|
215
|
+
|
|
216
|
+
export const getCompoundV2FullPositionData = async (web3: Web3, network: NetworkNumber, address: string): Promise<CompoundV2PositionData> => {
|
|
217
|
+
const marketData = await getCompoundV2MarketsData(web3, network);
|
|
218
|
+
const positionData = await getCompoundV2AccountData(web3, network, address, marketData.assetsData);
|
|
219
|
+
return positionData;
|
|
220
|
+
};
|