@defisaver/positions-sdk 1.0.3 → 1.0.4
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 +7 -7
- package/cjs/curveUsd/index.js +8 -1
- package/cjs/fluid/index.d.ts +2 -0
- package/cjs/helpers/aaveHelpers/index.js +6 -0
- package/cjs/helpers/compoundHelpers/index.js +2 -0
- package/cjs/helpers/eulerHelpers/index.js +2 -0
- package/cjs/helpers/fluidHelpers/index.js +2 -0
- package/cjs/helpers/morphoBlueHelpers/index.js +68 -66
- package/cjs/helpers/sparkHelpers/index.js +2 -0
- package/cjs/staking/staking.d.ts +2 -1
- package/cjs/staking/staking.js +3 -7
- package/cjs/types/aave.d.ts +4 -0
- package/cjs/types/compound.d.ts +2 -0
- package/cjs/types/curveUsd.d.ts +1 -0
- package/cjs/types/euler.d.ts +2 -0
- package/cjs/types/fluid.d.ts +2 -0
- package/cjs/types/morphoBlue.d.ts +2 -0
- package/cjs/types/spark.d.ts +2 -0
- package/esm/aaveV3/index.js +7 -7
- package/esm/curveUsd/index.js +10 -3
- package/esm/fluid/index.d.ts +2 -0
- package/esm/helpers/aaveHelpers/index.js +6 -0
- package/esm/helpers/compoundHelpers/index.js +2 -0
- package/esm/helpers/eulerHelpers/index.js +2 -0
- package/esm/helpers/fluidHelpers/index.js +2 -0
- package/esm/helpers/morphoBlueHelpers/index.js +68 -66
- package/esm/helpers/sparkHelpers/index.js +2 -0
- package/esm/staking/staking.d.ts +2 -1
- package/esm/staking/staking.js +3 -6
- package/esm/types/aave.d.ts +4 -0
- package/esm/types/compound.d.ts +2 -0
- package/esm/types/curveUsd.d.ts +1 -0
- package/esm/types/euler.d.ts +2 -0
- package/esm/types/fluid.d.ts +2 -0
- package/esm/types/morphoBlue.d.ts +2 -0
- package/esm/types/spark.d.ts +2 -0
- package/package.json +54 -52
- package/src/aaveV2/index.ts +227 -227
- package/src/aaveV3/index.ts +624 -625
- 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 +291 -291
- package/src/config/contracts.js +1147 -1147
- package/src/constants/index.ts +6 -6
- package/src/contracts.ts +134 -134
- package/src/curveUsd/index.ts +239 -230
- package/src/eulerV2/index.ts +303 -303
- package/src/exchange/index.ts +17 -17
- package/src/fluid/index.ts +354 -354
- package/src/helpers/aaveHelpers/index.ts +204 -198
- package/src/helpers/chickenBondsHelpers/index.ts +23 -23
- package/src/helpers/compoundHelpers/index.ts +248 -246
- package/src/helpers/curveUsdHelpers/index.ts +40 -40
- package/src/helpers/eulerHelpers/index.ts +234 -232
- package/src/helpers/fluidHelpers/index.ts +56 -53
- package/src/helpers/index.ts +11 -11
- package/src/helpers/liquityV2Helpers/index.ts +80 -80
- package/src/helpers/llamaLendHelpers/index.ts +53 -53
- package/src/helpers/makerHelpers/index.ts +94 -94
- package/src/helpers/morphoBlueHelpers/index.ts +367 -365
- package/src/helpers/sparkHelpers/index.ts +152 -150
- package/src/index.ts +52 -52
- package/src/liquity/index.ts +116 -116
- package/src/liquityV2/index.ts +295 -295
- 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 +44 -44
- package/src/markets/compound/index.ts +213 -213
- package/src/markets/compound/marketsAssets.ts +82 -82
- package/src/markets/curveUsd/index.ts +69 -69
- package/src/markets/euler/index.ts +26 -26
- package/src/markets/fluid/index.ts +2012 -2012
- package/src/markets/index.ts +27 -27
- package/src/markets/liquityV2/index.ts +54 -54
- 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 +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 +202 -202
- package/src/multicall/index.ts +33 -33
- package/src/services/priceService.ts +91 -91
- package/src/services/utils.ts +59 -59
- package/src/setup.ts +8 -8
- package/src/spark/index.ts +460 -460
- package/src/staking/staking.ts +217 -220
- package/src/types/aave.ts +275 -271
- package/src/types/chickenBonds.ts +45 -45
- package/src/types/common.ts +84 -84
- package/src/types/compound.ts +133 -131
- package/src/types/curveUsd.ts +119 -118
- package/src/types/euler.ts +173 -171
- package/src/types/fluid.ts +268 -266
- package/src/types/index.ts +11 -11
- package/src/types/liquity.ts +30 -30
- package/src/types/liquityV2.ts +119 -119
- package/src/types/llamaLend.ts +155 -155
- package/src/types/maker.ts +50 -50
- package/src/types/morphoBlue.ts +194 -192
- package/src/types/spark.ts +133 -131
package/src/liquityV2/index.ts
CHANGED
|
@@ -1,295 +1,295 @@
|
|
|
1
|
-
import Web3 from 'web3';
|
|
2
|
-
import Dec from 'decimal.js';
|
|
3
|
-
import { assetAmountInEth, getAssetInfo } from '@defisaver/tokens';
|
|
4
|
-
import { createContractWrapper, LiquityV2ViewContract } from '../contracts';
|
|
5
|
-
import { EthAddress, NetworkNumber } from '../types/common';
|
|
6
|
-
import {
|
|
7
|
-
InnerLiquityV2MarketData,
|
|
8
|
-
LIQUITY_V2_TROVE_STATUS_ENUM,
|
|
9
|
-
LiquityV2AssetsData, LiquityV2MarketData, LiquityV2MarketInfo, LiquityV2TroveData, LiquityV2UsedAssets,
|
|
10
|
-
LiquityV2Versions,
|
|
11
|
-
} from '../types';
|
|
12
|
-
import { getStakingApy, STAKING_ASSETS } from '../staking';
|
|
13
|
-
import { getLiquityV2AggregatedPositionData } from '../helpers/liquityV2Helpers';
|
|
14
|
-
import { compareAddresses, ethToWeth, MAXUINT } from '../services/utils';
|
|
15
|
-
import { LiquityV2View } from '../types/contracts/generated';
|
|
16
|
-
import { ZERO_ADDRESS } from '../constants';
|
|
17
|
-
import { LiquityV2Markets } from '../markets';
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
export const getLiquityV2MarketData = async (web3: Web3, network: NetworkNumber, selectedMarket: LiquityV2MarketInfo, mainnetWeb3: Web3): Promise<LiquityV2MarketData> => {
|
|
21
|
-
const viewContract = LiquityV2ViewContract(web3, network);
|
|
22
|
-
const { marketAddress, debtToken, collateralToken } = selectedMarket;
|
|
23
|
-
const data = await viewContract.methods.getMarketData(marketAddress).call();
|
|
24
|
-
const hintHelperAddress = data.hintHelpers;
|
|
25
|
-
const troveNFTAddress = data.troveNFT;
|
|
26
|
-
const borrowerOperationsAddress = data.borrowerOperations;
|
|
27
|
-
const troveManagerAddress = data.troveManager;
|
|
28
|
-
const stabilityPoolAddress = data.stabilityPool;
|
|
29
|
-
const collSurplusPoolAddress = data.collSurplusPool;
|
|
30
|
-
const activePoolAddress = data.activePool;
|
|
31
|
-
|
|
32
|
-
const minCollRatio = new Dec(data.MCR).div(1e16).toString();
|
|
33
|
-
const criticalCollRatio = new Dec(data.CCR).div(1e18).toString();
|
|
34
|
-
|
|
35
|
-
const totalMarketBorrow = assetAmountInEth(data.entireSystemDebt);
|
|
36
|
-
const totalMarketSupply = assetAmountInEth(data.entireSystemColl);
|
|
37
|
-
const collPrice = assetAmountInEth(data.collPrice);
|
|
38
|
-
|
|
39
|
-
const totalCollRatio = new Dec(totalMarketSupply).mul(collPrice).div(totalMarketBorrow).toString();
|
|
40
|
-
const leftToBorrowGlobal = new Dec(totalMarketSupply).mul(collPrice).div(criticalCollRatio).sub(totalMarketBorrow)
|
|
41
|
-
.toString();
|
|
42
|
-
const minCollAmountForCurrentBorrow = new Dec(totalMarketBorrow).mul(criticalCollRatio).div(collPrice).toString();
|
|
43
|
-
const leftToWithdrawGlobal = new Dec(totalMarketSupply).sub(minCollAmountForCurrentBorrow).toString();
|
|
44
|
-
|
|
45
|
-
const assetsData: LiquityV2AssetsData = {};
|
|
46
|
-
assetsData[debtToken] = {
|
|
47
|
-
symbol: debtToken,
|
|
48
|
-
address: getAssetInfo(debtToken, network).address,
|
|
49
|
-
price: '1',
|
|
50
|
-
totalSupply: '0',
|
|
51
|
-
totalBorrow: totalMarketBorrow,
|
|
52
|
-
canBeSupplied: false,
|
|
53
|
-
canBeBorrowed: true,
|
|
54
|
-
leftToBorrowGlobal,
|
|
55
|
-
leftToWithdrawGlobal: '0',
|
|
56
|
-
};
|
|
57
|
-
assetsData[collateralToken] = {
|
|
58
|
-
symbol: collateralToken,
|
|
59
|
-
address: getAssetInfo(ethToWeth(collateralToken), network).address,
|
|
60
|
-
price: collPrice,
|
|
61
|
-
totalSupply: totalMarketSupply,
|
|
62
|
-
totalBorrow: '0',
|
|
63
|
-
canBeSupplied: true,
|
|
64
|
-
canBeBorrowed: false,
|
|
65
|
-
leftToBorrowGlobal: '0',
|
|
66
|
-
leftToWithdrawGlobal,
|
|
67
|
-
};
|
|
68
|
-
if (STAKING_ASSETS.includes(collateralToken)) {
|
|
69
|
-
assetsData[collateralToken].incentiveSupplyApy = await getStakingApy(collateralToken, mainnetWeb3);
|
|
70
|
-
assetsData[collateralToken].incentiveSupplyToken = collateralToken;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
return {
|
|
74
|
-
assetsData,
|
|
75
|
-
marketData: {
|
|
76
|
-
minCollRatio,
|
|
77
|
-
totalCollRatio: new Dec(totalCollRatio).mul(100).toString(),
|
|
78
|
-
criticalCollRatio: new Dec(criticalCollRatio).mul(100).toString(),
|
|
79
|
-
isUnderCollateralized: new Dec(totalCollRatio).lt(criticalCollRatio),
|
|
80
|
-
hintHelperAddress,
|
|
81
|
-
troveNFTAddress,
|
|
82
|
-
borrowerOperationsAddress,
|
|
83
|
-
troveManagerAddress,
|
|
84
|
-
stabilityPoolAddress,
|
|
85
|
-
collSurplusPoolAddress,
|
|
86
|
-
activePoolAddress,
|
|
87
|
-
},
|
|
88
|
-
};
|
|
89
|
-
};
|
|
90
|
-
|
|
91
|
-
const _getUserTroves = async (viewContract: any, account: EthAddress, marketAddress: EthAddress, startIndex = 0, endIndex = 100) => viewContract.methods.getUserTroves(account, marketAddress, startIndex, endIndex).call();
|
|
92
|
-
|
|
93
|
-
const getUserTroves = async (viewContract: any, account: EthAddress, marketAddress: EthAddress, startIndex = 0, endIndex = 100, troves: LiquityV2View.ExistingTroveStructOutput[] = []): Promise<{ troves: LiquityV2View.ExistingTroveStructOutput[], nextFreeTroveIndex: string }> => {
|
|
94
|
-
const result = await _getUserTroves(viewContract, account, marketAddress, startIndex, endIndex);
|
|
95
|
-
const newStartIndex = endIndex + 1;
|
|
96
|
-
const nextFreeTroveIndex = result.nextFreeTroveIndex;
|
|
97
|
-
const existingTroves = [...troves, ...result.troves];
|
|
98
|
-
if (nextFreeTroveIndex !== '-1') return { troves: existingTroves.filter((trove) => trove.ownedByUser), nextFreeTroveIndex };
|
|
99
|
-
return getUserTroves(viewContract, account, marketAddress, newStartIndex, newStartIndex + 100, existingTroves);
|
|
100
|
-
};
|
|
101
|
-
|
|
102
|
-
const TransferEventSig = '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef';
|
|
103
|
-
|
|
104
|
-
const nftContractCreationBlockMapping = {
|
|
105
|
-
ETH: 21686215,
|
|
106
|
-
wstETH: 21686238,
|
|
107
|
-
rETH: 21686257,
|
|
108
|
-
};
|
|
109
|
-
|
|
110
|
-
const getTransferredTroves = async (web3: Web3, network: NetworkNumber, troveNFTAddress: EthAddress, limitBlocksForEventFetching: boolean, collAsset: string, account: EthAddress): Promise<{ troveId: string }[]> => {
|
|
111
|
-
const nftContract = createContractWrapper(web3, network, 'LiquityV2TroveNFT', troveNFTAddress);
|
|
112
|
-
// @ts-ignore
|
|
113
|
-
const nftContractCreationBlock = nftContractCreationBlockMapping[collAsset];
|
|
114
|
-
const currentBlock = await web3.eth.getBlockNumber();
|
|
115
|
-
const events = await nftContract.getPastEvents(
|
|
116
|
-
TransferEventSig,
|
|
117
|
-
{
|
|
118
|
-
filter: { to: account },
|
|
119
|
-
fromBlock: limitBlocksForEventFetching ? (currentBlock - 1000) : nftContractCreationBlock,
|
|
120
|
-
},
|
|
121
|
-
);
|
|
122
|
-
const userTransferredTroves = events.filter((event) => !compareAddresses(event.returnValues.from, ZERO_ADDRESS) && compareAddresses(event.returnValues.to, account));
|
|
123
|
-
|
|
124
|
-
// check if the last know transfer address is the user
|
|
125
|
-
userTransferredTroves.forEach((event, index) => {
|
|
126
|
-
const otherTransfers = events.filter((e) => event.blockNumber < e.blockNumber && e.returnValues.tokenId === event.returnValues.tokenId);
|
|
127
|
-
// @ts-ignore
|
|
128
|
-
userTransferredTroves[index].invalid = !!otherTransfers.length;
|
|
129
|
-
});
|
|
130
|
-
// @ts-ignore
|
|
131
|
-
return userTransferredTroves.filter((event) => !event.invalid).map((event) => ({ troveId: event.returnValues.tokenId }));
|
|
132
|
-
};
|
|
133
|
-
|
|
134
|
-
export const getLiquityV2UserTroveIds = async (web3: Web3, network: NetworkNumber, selectedMarket: LiquityV2MarketInfo, troveNFTAddress: EthAddress, limitBlocksForEventFetching: boolean, account: EthAddress): Promise<{ troves: { troveId: string }[], nextFreeTroveIndex: string }> => {
|
|
135
|
-
const viewContract = LiquityV2ViewContract(web3, network);
|
|
136
|
-
const [{ troves: userTroves, nextFreeTroveIndex }, userTransferredTroves] = await Promise.all([
|
|
137
|
-
getUserTroves(viewContract, account, selectedMarket.marketAddress),
|
|
138
|
-
getTransferredTroves(web3, network, troveNFTAddress, limitBlocksForEventFetching, selectedMarket.collateralToken, account),
|
|
139
|
-
]);
|
|
140
|
-
const troves = [...userTroves.map(({ troveId }) => ({ troveId })), ...userTransferredTroves];
|
|
141
|
-
const filteredTroves = troves.filter((value, index, self) => index === self.findIndex((t) => (
|
|
142
|
-
t.troveId === value.troveId
|
|
143
|
-
)),
|
|
144
|
-
);
|
|
145
|
-
return { troves: filteredTroves, nextFreeTroveIndex };
|
|
146
|
-
};
|
|
147
|
-
|
|
148
|
-
const _getDebtInFrontForSingleMarket = async (viewContract: any, marketAddress: EthAddress, troveId: string, accumulatedSum = '0', iterations = 2000) => viewContract.methods.getDebtInFront(marketAddress, troveId, accumulatedSum, iterations).call();
|
|
149
|
-
|
|
150
|
-
export const getDebtInFrontForSingleMarketLiquityV2 = async (viewContract: any, marketAddress: EthAddress, troveId: string, accumulatedSum = '0', iterations = 2000): Promise<string> => {
|
|
151
|
-
const { debt, next } = await _getDebtInFrontForSingleMarket(viewContract, marketAddress, troveId, accumulatedSum, iterations);
|
|
152
|
-
if (next === '0') return assetAmountInEth(debt);
|
|
153
|
-
return getDebtInFrontForSingleMarketLiquityV2(viewContract, marketAddress, next, debt, iterations);
|
|
154
|
-
};
|
|
155
|
-
|
|
156
|
-
const _getDebtInFrontForInterestRateSingleMarketLiquityV2 = async (viewContract: any, marketAddress: EthAddress, troveId = '0', accumulatedSum = '0', iterations = 2000, interestRate: string) => viewContract.methods.getDebtInFrontByInterestRate(marketAddress, troveId, accumulatedSum, iterations, interestRate).call();
|
|
157
|
-
|
|
158
|
-
export const getDebtInFrontForInterestRateSingleMarketLiquityV2 = async (viewContract: any, marketAddress: EthAddress, interestRate: string, troveId = '0', accumulatedSum = '0', iterations = 2000): Promise<string> => {
|
|
159
|
-
if (+interestRate === 0 || !interestRate) return '0';
|
|
160
|
-
const interestRateWei = new Dec(interestRate).times(1e16).toFixed(0).toString();
|
|
161
|
-
const res = await _getDebtInFrontForInterestRateSingleMarketLiquityV2(viewContract, marketAddress, troveId, accumulatedSum, iterations, interestRateWei);
|
|
162
|
-
const { debt, next } = res;
|
|
163
|
-
if (next === '0') return assetAmountInEth(debt);
|
|
164
|
-
return getDebtInFrontForInterestRateSingleMarketLiquityV2(viewContract, marketAddress, interestRate, next, debt, iterations);
|
|
165
|
-
};
|
|
166
|
-
|
|
167
|
-
const getUnbackedDebtForSingleMarket = async (totalBorrowed: string, web3: Web3, network: NetworkNumber, stabilityPoolAddress: EthAddress) => {
|
|
168
|
-
const stabilityPoolContract = createContractWrapper(web3, network, 'LiquityV2StabilityPool', stabilityPoolAddress);
|
|
169
|
-
const totalBoldDeposits = await stabilityPoolContract.methods.getTotalBoldDeposits().call();
|
|
170
|
-
const totalBoldDepositsInEth = assetAmountInEth(totalBoldDeposits);
|
|
171
|
-
|
|
172
|
-
return Dec.max(new Dec(totalBorrowed).sub(totalBoldDepositsInEth), 0).toString();
|
|
173
|
-
};
|
|
174
|
-
|
|
175
|
-
export const getAllMarketsUnbackedDebts = async (markets: Record<LiquityV2Versions, LiquityV2MarketData>, web3: Web3, network: NetworkNumber): Promise<Record<LiquityV2Versions, string>> => {
|
|
176
|
-
const allMarketsUnbackedDebt = await Promise.all(Object.entries(markets).map(async ([version, market]) => {
|
|
177
|
-
const { assetsData, marketData } = market;
|
|
178
|
-
const unbackedDebt = await getUnbackedDebtForSingleMarket(assetsData.BOLD.totalBorrow, web3, network, marketData.stabilityPoolAddress);
|
|
179
|
-
return [version, unbackedDebt];
|
|
180
|
-
}));
|
|
181
|
-
|
|
182
|
-
return Object.fromEntries(allMarketsUnbackedDebt) as Record<LiquityV2Versions, string>;
|
|
183
|
-
};
|
|
184
|
-
|
|
185
|
-
export const calculateDebtInFrontLiquityV2 = (markets: Record<LiquityV2Versions, LiquityV2MarketData>, selectedMarket: LiquityV2Versions, allMarketsUnbackedDebts: Record<LiquityV2Versions, string>, interestRateDebtInFront: string): string => {
|
|
186
|
-
const selectedMarketUnbackedDebt = new Dec(allMarketsUnbackedDebts[selectedMarket]);
|
|
187
|
-
if (selectedMarketUnbackedDebt.eq(0)) return 'N/A';
|
|
188
|
-
|
|
189
|
-
const amountBeingReedemedOnEachMarket = Object.entries(markets).map(([version, market]) => {
|
|
190
|
-
if (version === selectedMarket) return new Dec(interestRateDebtInFront);
|
|
191
|
-
const { assetsData } = market;
|
|
192
|
-
const unbackedDebt = new Dec(allMarketsUnbackedDebts[version as LiquityV2Versions]);
|
|
193
|
-
const totalBorrow = new Dec(assetsData.BOLD.totalBorrow);
|
|
194
|
-
const amountToReedem = new Dec(interestRateDebtInFront).mul(unbackedDebt).div(selectedMarketUnbackedDebt);
|
|
195
|
-
return Dec.min(amountToReedem, totalBorrow);
|
|
196
|
-
});
|
|
197
|
-
|
|
198
|
-
return amountBeingReedemedOnEachMarket.reduce((acc, val) => acc.plus(val), new Dec(0)).toString();
|
|
199
|
-
};
|
|
200
|
-
|
|
201
|
-
export const getDebtInFrontLiquityV2 = async (markets: Record<LiquityV2Versions, LiquityV2MarketData>, selectedMarket: LiquityV2Versions, web3: Web3, network: NetworkNumber, viewContract: any, troveId: string) => {
|
|
202
|
-
const allMarketsUnbackedDebts = await getAllMarketsUnbackedDebts(markets, web3, network);
|
|
203
|
-
const interestRateDebtInFront = await getDebtInFrontForSingleMarketLiquityV2(viewContract, LiquityV2Markets(network)[selectedMarket].marketAddress, troveId);
|
|
204
|
-
|
|
205
|
-
return calculateDebtInFrontLiquityV2(markets, selectedMarket, allMarketsUnbackedDebts, interestRateDebtInFront.toString());
|
|
206
|
-
};
|
|
207
|
-
|
|
208
|
-
export const getDebtInFrontForInterestRateLiquityV2 = async (markets: Record<LiquityV2Versions, LiquityV2MarketData>, selectedMarket: LiquityV2Versions, web3: Web3, network: NetworkNumber, viewContract: any, interestRate: string) => {
|
|
209
|
-
const allMarketsUnbackedDebts = await getAllMarketsUnbackedDebts(markets, web3, network);
|
|
210
|
-
const interestRateDebtInFront = new Dec(await getDebtInFrontForInterestRateSingleMarketLiquityV2(viewContract, LiquityV2Markets(network)[selectedMarket].marketAddress, interestRate));
|
|
211
|
-
|
|
212
|
-
return calculateDebtInFrontLiquityV2(markets, selectedMarket, allMarketsUnbackedDebts, interestRateDebtInFront.toString());
|
|
213
|
-
};
|
|
214
|
-
|
|
215
|
-
export const getLiquityV2TroveData = async (
|
|
216
|
-
web3: Web3,
|
|
217
|
-
network: NetworkNumber,
|
|
218
|
-
{
|
|
219
|
-
selectedMarket,
|
|
220
|
-
assetsData,
|
|
221
|
-
troveId,
|
|
222
|
-
allMarketsData,
|
|
223
|
-
}:
|
|
224
|
-
{
|
|
225
|
-
selectedMarket: LiquityV2MarketInfo,
|
|
226
|
-
assetsData: LiquityV2AssetsData,
|
|
227
|
-
troveId: string,
|
|
228
|
-
allMarketsData: Record<LiquityV2Versions, LiquityV2MarketData>,
|
|
229
|
-
},
|
|
230
|
-
): Promise<LiquityV2TroveData> => {
|
|
231
|
-
const viewContract = LiquityV2ViewContract(web3, network);
|
|
232
|
-
const { minCollRatio } = allMarketsData[selectedMarket.value].marketData;
|
|
233
|
-
const { collateralToken, marketAddress, debtToken } = selectedMarket;
|
|
234
|
-
const [_data, debtInFront] = await Promise.all([
|
|
235
|
-
viewContract.methods.getTroveInfo(marketAddress, troveId).call(),
|
|
236
|
-
getDebtInFrontLiquityV2(allMarketsData, selectedMarket.value, web3, network, viewContract, troveId),
|
|
237
|
-
]);
|
|
238
|
-
const data = {
|
|
239
|
-
..._data,
|
|
240
|
-
TCRatio: _data.TCRatio === MAXUINT ? '0' : _data.TCRatio, // mistake on contract side when debt is 0
|
|
241
|
-
};
|
|
242
|
-
const usedAssets: LiquityV2UsedAssets = {};
|
|
243
|
-
|
|
244
|
-
const debtAssetData = assetsData[debtToken];
|
|
245
|
-
const borrowed = assetAmountInEth(data.debtAmount);
|
|
246
|
-
usedAssets[debtToken] = {
|
|
247
|
-
symbol: debtToken,
|
|
248
|
-
supplied: '0',
|
|
249
|
-
suppliedUsd: '0',
|
|
250
|
-
borrowed,
|
|
251
|
-
borrowedUsd: new Dec(borrowed).mul(debtAssetData.price).toString(),
|
|
252
|
-
isBorrowed: true,
|
|
253
|
-
isSupplied: false,
|
|
254
|
-
};
|
|
255
|
-
|
|
256
|
-
const collAssetData = assetsData[collateralToken];
|
|
257
|
-
const suppliedColl = assetAmountInEth(data.collAmount);
|
|
258
|
-
usedAssets[collateralToken] = {
|
|
259
|
-
symbol: collateralToken,
|
|
260
|
-
supplied: suppliedColl,
|
|
261
|
-
suppliedUsd: new Dec(suppliedColl).mul(collAssetData.price).toString(),
|
|
262
|
-
borrowed: '0',
|
|
263
|
-
borrowedUsd: '0',
|
|
264
|
-
isBorrowed: false,
|
|
265
|
-
isSupplied: true,
|
|
266
|
-
collateral: true,
|
|
267
|
-
};
|
|
268
|
-
|
|
269
|
-
const collRatio = new Dec(data.TCRatio).div(1e16).toString();
|
|
270
|
-
const interestRate = new Dec(data.annualInterestRate).div(1e16).toString();
|
|
271
|
-
const interestBatchManager = data.interestBatchManager;
|
|
272
|
-
const lastInterestRateAdjTime = data.lastInterestRateAdjTime;
|
|
273
|
-
|
|
274
|
-
const payload: LiquityV2TroveData = {
|
|
275
|
-
usedAssets,
|
|
276
|
-
troveId,
|
|
277
|
-
interestRate,
|
|
278
|
-
interestBatchManager,
|
|
279
|
-
debtInFront,
|
|
280
|
-
lastInterestRateAdjTime,
|
|
281
|
-
troveStatus: LIQUITY_V2_TROVE_STATUS_ENUM[parseInt(data.status, 10)],
|
|
282
|
-
...getLiquityV2AggregatedPositionData({
|
|
283
|
-
usedAssets, assetsData, minCollRatio, interestRate,
|
|
284
|
-
}),
|
|
285
|
-
collRatio,
|
|
286
|
-
};
|
|
287
|
-
|
|
288
|
-
return payload;
|
|
289
|
-
};
|
|
290
|
-
|
|
291
|
-
export const getLiquityV2ClaimableCollateral = async (collSurplusPoolAddress: EthAddress, account: EthAddress, web3: Web3, network: NetworkNumber): Promise<string> => {
|
|
292
|
-
const collSurplusPoolContract = createContractWrapper(web3, network, 'LiquityV2CollSurplusPool', collSurplusPoolAddress);
|
|
293
|
-
const claimableCollateral = await collSurplusPoolContract.methods.getCollateral(account).call();
|
|
294
|
-
return claimableCollateral;
|
|
295
|
-
};
|
|
1
|
+
import Web3 from 'web3';
|
|
2
|
+
import Dec from 'decimal.js';
|
|
3
|
+
import { assetAmountInEth, getAssetInfo } from '@defisaver/tokens';
|
|
4
|
+
import { createContractWrapper, LiquityV2ViewContract } from '../contracts';
|
|
5
|
+
import { EthAddress, NetworkNumber } from '../types/common';
|
|
6
|
+
import {
|
|
7
|
+
InnerLiquityV2MarketData,
|
|
8
|
+
LIQUITY_V2_TROVE_STATUS_ENUM,
|
|
9
|
+
LiquityV2AssetsData, LiquityV2MarketData, LiquityV2MarketInfo, LiquityV2TroveData, LiquityV2UsedAssets,
|
|
10
|
+
LiquityV2Versions,
|
|
11
|
+
} from '../types';
|
|
12
|
+
import { getStakingApy, STAKING_ASSETS } from '../staking';
|
|
13
|
+
import { getLiquityV2AggregatedPositionData } from '../helpers/liquityV2Helpers';
|
|
14
|
+
import { compareAddresses, ethToWeth, MAXUINT } from '../services/utils';
|
|
15
|
+
import { LiquityV2View } from '../types/contracts/generated';
|
|
16
|
+
import { ZERO_ADDRESS } from '../constants';
|
|
17
|
+
import { LiquityV2Markets } from '../markets';
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
export const getLiquityV2MarketData = async (web3: Web3, network: NetworkNumber, selectedMarket: LiquityV2MarketInfo, mainnetWeb3: Web3): Promise<LiquityV2MarketData> => {
|
|
21
|
+
const viewContract = LiquityV2ViewContract(web3, network);
|
|
22
|
+
const { marketAddress, debtToken, collateralToken } = selectedMarket;
|
|
23
|
+
const data = await viewContract.methods.getMarketData(marketAddress).call();
|
|
24
|
+
const hintHelperAddress = data.hintHelpers;
|
|
25
|
+
const troveNFTAddress = data.troveNFT;
|
|
26
|
+
const borrowerOperationsAddress = data.borrowerOperations;
|
|
27
|
+
const troveManagerAddress = data.troveManager;
|
|
28
|
+
const stabilityPoolAddress = data.stabilityPool;
|
|
29
|
+
const collSurplusPoolAddress = data.collSurplusPool;
|
|
30
|
+
const activePoolAddress = data.activePool;
|
|
31
|
+
|
|
32
|
+
const minCollRatio = new Dec(data.MCR).div(1e16).toString();
|
|
33
|
+
const criticalCollRatio = new Dec(data.CCR).div(1e18).toString();
|
|
34
|
+
|
|
35
|
+
const totalMarketBorrow = assetAmountInEth(data.entireSystemDebt);
|
|
36
|
+
const totalMarketSupply = assetAmountInEth(data.entireSystemColl);
|
|
37
|
+
const collPrice = assetAmountInEth(data.collPrice);
|
|
38
|
+
|
|
39
|
+
const totalCollRatio = new Dec(totalMarketSupply).mul(collPrice).div(totalMarketBorrow).toString();
|
|
40
|
+
const leftToBorrowGlobal = new Dec(totalMarketSupply).mul(collPrice).div(criticalCollRatio).sub(totalMarketBorrow)
|
|
41
|
+
.toString();
|
|
42
|
+
const minCollAmountForCurrentBorrow = new Dec(totalMarketBorrow).mul(criticalCollRatio).div(collPrice).toString();
|
|
43
|
+
const leftToWithdrawGlobal = new Dec(totalMarketSupply).sub(minCollAmountForCurrentBorrow).toString();
|
|
44
|
+
|
|
45
|
+
const assetsData: LiquityV2AssetsData = {};
|
|
46
|
+
assetsData[debtToken] = {
|
|
47
|
+
symbol: debtToken,
|
|
48
|
+
address: getAssetInfo(debtToken, network).address,
|
|
49
|
+
price: '1',
|
|
50
|
+
totalSupply: '0',
|
|
51
|
+
totalBorrow: totalMarketBorrow,
|
|
52
|
+
canBeSupplied: false,
|
|
53
|
+
canBeBorrowed: true,
|
|
54
|
+
leftToBorrowGlobal,
|
|
55
|
+
leftToWithdrawGlobal: '0',
|
|
56
|
+
};
|
|
57
|
+
assetsData[collateralToken] = {
|
|
58
|
+
symbol: collateralToken,
|
|
59
|
+
address: getAssetInfo(ethToWeth(collateralToken), network).address,
|
|
60
|
+
price: collPrice,
|
|
61
|
+
totalSupply: totalMarketSupply,
|
|
62
|
+
totalBorrow: '0',
|
|
63
|
+
canBeSupplied: true,
|
|
64
|
+
canBeBorrowed: false,
|
|
65
|
+
leftToBorrowGlobal: '0',
|
|
66
|
+
leftToWithdrawGlobal,
|
|
67
|
+
};
|
|
68
|
+
if (STAKING_ASSETS.includes(collateralToken)) {
|
|
69
|
+
assetsData[collateralToken].incentiveSupplyApy = await getStakingApy(collateralToken, mainnetWeb3);
|
|
70
|
+
assetsData[collateralToken].incentiveSupplyToken = collateralToken;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return {
|
|
74
|
+
assetsData,
|
|
75
|
+
marketData: {
|
|
76
|
+
minCollRatio,
|
|
77
|
+
totalCollRatio: new Dec(totalCollRatio).mul(100).toString(),
|
|
78
|
+
criticalCollRatio: new Dec(criticalCollRatio).mul(100).toString(),
|
|
79
|
+
isUnderCollateralized: new Dec(totalCollRatio).lt(criticalCollRatio),
|
|
80
|
+
hintHelperAddress,
|
|
81
|
+
troveNFTAddress,
|
|
82
|
+
borrowerOperationsAddress,
|
|
83
|
+
troveManagerAddress,
|
|
84
|
+
stabilityPoolAddress,
|
|
85
|
+
collSurplusPoolAddress,
|
|
86
|
+
activePoolAddress,
|
|
87
|
+
},
|
|
88
|
+
};
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
const _getUserTroves = async (viewContract: any, account: EthAddress, marketAddress: EthAddress, startIndex = 0, endIndex = 100) => viewContract.methods.getUserTroves(account, marketAddress, startIndex, endIndex).call();
|
|
92
|
+
|
|
93
|
+
const getUserTroves = async (viewContract: any, account: EthAddress, marketAddress: EthAddress, startIndex = 0, endIndex = 100, troves: LiquityV2View.ExistingTroveStructOutput[] = []): Promise<{ troves: LiquityV2View.ExistingTroveStructOutput[], nextFreeTroveIndex: string }> => {
|
|
94
|
+
const result = await _getUserTroves(viewContract, account, marketAddress, startIndex, endIndex);
|
|
95
|
+
const newStartIndex = endIndex + 1;
|
|
96
|
+
const nextFreeTroveIndex = result.nextFreeTroveIndex;
|
|
97
|
+
const existingTroves = [...troves, ...result.troves];
|
|
98
|
+
if (nextFreeTroveIndex !== '-1') return { troves: existingTroves.filter((trove) => trove.ownedByUser), nextFreeTroveIndex };
|
|
99
|
+
return getUserTroves(viewContract, account, marketAddress, newStartIndex, newStartIndex + 100, existingTroves);
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
const TransferEventSig = '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef';
|
|
103
|
+
|
|
104
|
+
const nftContractCreationBlockMapping = {
|
|
105
|
+
ETH: 21686215,
|
|
106
|
+
wstETH: 21686238,
|
|
107
|
+
rETH: 21686257,
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
const getTransferredTroves = async (web3: Web3, network: NetworkNumber, troveNFTAddress: EthAddress, limitBlocksForEventFetching: boolean, collAsset: string, account: EthAddress): Promise<{ troveId: string }[]> => {
|
|
111
|
+
const nftContract = createContractWrapper(web3, network, 'LiquityV2TroveNFT', troveNFTAddress);
|
|
112
|
+
// @ts-ignore
|
|
113
|
+
const nftContractCreationBlock = nftContractCreationBlockMapping[collAsset];
|
|
114
|
+
const currentBlock = await web3.eth.getBlockNumber();
|
|
115
|
+
const events = await nftContract.getPastEvents(
|
|
116
|
+
TransferEventSig,
|
|
117
|
+
{
|
|
118
|
+
filter: { to: account },
|
|
119
|
+
fromBlock: limitBlocksForEventFetching ? (currentBlock - 1000) : nftContractCreationBlock,
|
|
120
|
+
},
|
|
121
|
+
);
|
|
122
|
+
const userTransferredTroves = events.filter((event) => !compareAddresses(event.returnValues.from, ZERO_ADDRESS) && compareAddresses(event.returnValues.to, account));
|
|
123
|
+
|
|
124
|
+
// check if the last know transfer address is the user
|
|
125
|
+
userTransferredTroves.forEach((event, index) => {
|
|
126
|
+
const otherTransfers = events.filter((e) => event.blockNumber < e.blockNumber && e.returnValues.tokenId === event.returnValues.tokenId);
|
|
127
|
+
// @ts-ignore
|
|
128
|
+
userTransferredTroves[index].invalid = !!otherTransfers.length;
|
|
129
|
+
});
|
|
130
|
+
// @ts-ignore
|
|
131
|
+
return userTransferredTroves.filter((event) => !event.invalid).map((event) => ({ troveId: event.returnValues.tokenId }));
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
export const getLiquityV2UserTroveIds = async (web3: Web3, network: NetworkNumber, selectedMarket: LiquityV2MarketInfo, troveNFTAddress: EthAddress, limitBlocksForEventFetching: boolean, account: EthAddress): Promise<{ troves: { troveId: string }[], nextFreeTroveIndex: string }> => {
|
|
135
|
+
const viewContract = LiquityV2ViewContract(web3, network);
|
|
136
|
+
const [{ troves: userTroves, nextFreeTroveIndex }, userTransferredTroves] = await Promise.all([
|
|
137
|
+
getUserTroves(viewContract, account, selectedMarket.marketAddress),
|
|
138
|
+
getTransferredTroves(web3, network, troveNFTAddress, limitBlocksForEventFetching, selectedMarket.collateralToken, account),
|
|
139
|
+
]);
|
|
140
|
+
const troves = [...userTroves.map(({ troveId }) => ({ troveId })), ...userTransferredTroves];
|
|
141
|
+
const filteredTroves = troves.filter((value, index, self) => index === self.findIndex((t) => (
|
|
142
|
+
t.troveId === value.troveId
|
|
143
|
+
)),
|
|
144
|
+
);
|
|
145
|
+
return { troves: filteredTroves, nextFreeTroveIndex };
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
const _getDebtInFrontForSingleMarket = async (viewContract: any, marketAddress: EthAddress, troveId: string, accumulatedSum = '0', iterations = 2000) => viewContract.methods.getDebtInFront(marketAddress, troveId, accumulatedSum, iterations).call();
|
|
149
|
+
|
|
150
|
+
export const getDebtInFrontForSingleMarketLiquityV2 = async (viewContract: any, marketAddress: EthAddress, troveId: string, accumulatedSum = '0', iterations = 2000): Promise<string> => {
|
|
151
|
+
const { debt, next } = await _getDebtInFrontForSingleMarket(viewContract, marketAddress, troveId, accumulatedSum, iterations);
|
|
152
|
+
if (next === '0') return assetAmountInEth(debt);
|
|
153
|
+
return getDebtInFrontForSingleMarketLiquityV2(viewContract, marketAddress, next, debt, iterations);
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
const _getDebtInFrontForInterestRateSingleMarketLiquityV2 = async (viewContract: any, marketAddress: EthAddress, troveId = '0', accumulatedSum = '0', iterations = 2000, interestRate: string) => viewContract.methods.getDebtInFrontByInterestRate(marketAddress, troveId, accumulatedSum, iterations, interestRate).call();
|
|
157
|
+
|
|
158
|
+
export const getDebtInFrontForInterestRateSingleMarketLiquityV2 = async (viewContract: any, marketAddress: EthAddress, interestRate: string, troveId = '0', accumulatedSum = '0', iterations = 2000): Promise<string> => {
|
|
159
|
+
if (+interestRate === 0 || !interestRate) return '0';
|
|
160
|
+
const interestRateWei = new Dec(interestRate).times(1e16).toFixed(0).toString();
|
|
161
|
+
const res = await _getDebtInFrontForInterestRateSingleMarketLiquityV2(viewContract, marketAddress, troveId, accumulatedSum, iterations, interestRateWei);
|
|
162
|
+
const { debt, next } = res;
|
|
163
|
+
if (next === '0') return assetAmountInEth(debt);
|
|
164
|
+
return getDebtInFrontForInterestRateSingleMarketLiquityV2(viewContract, marketAddress, interestRate, next, debt, iterations);
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
const getUnbackedDebtForSingleMarket = async (totalBorrowed: string, web3: Web3, network: NetworkNumber, stabilityPoolAddress: EthAddress) => {
|
|
168
|
+
const stabilityPoolContract = createContractWrapper(web3, network, 'LiquityV2StabilityPool', stabilityPoolAddress);
|
|
169
|
+
const totalBoldDeposits = await stabilityPoolContract.methods.getTotalBoldDeposits().call();
|
|
170
|
+
const totalBoldDepositsInEth = assetAmountInEth(totalBoldDeposits);
|
|
171
|
+
|
|
172
|
+
return Dec.max(new Dec(totalBorrowed).sub(totalBoldDepositsInEth), 0).toString();
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
export const getAllMarketsUnbackedDebts = async (markets: Record<LiquityV2Versions, LiquityV2MarketData>, web3: Web3, network: NetworkNumber): Promise<Record<LiquityV2Versions, string>> => {
|
|
176
|
+
const allMarketsUnbackedDebt = await Promise.all(Object.entries(markets).map(async ([version, market]) => {
|
|
177
|
+
const { assetsData, marketData } = market;
|
|
178
|
+
const unbackedDebt = await getUnbackedDebtForSingleMarket(assetsData.BOLD.totalBorrow, web3, network, marketData.stabilityPoolAddress);
|
|
179
|
+
return [version, unbackedDebt];
|
|
180
|
+
}));
|
|
181
|
+
|
|
182
|
+
return Object.fromEntries(allMarketsUnbackedDebt) as Record<LiquityV2Versions, string>;
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
export const calculateDebtInFrontLiquityV2 = (markets: Record<LiquityV2Versions, LiquityV2MarketData>, selectedMarket: LiquityV2Versions, allMarketsUnbackedDebts: Record<LiquityV2Versions, string>, interestRateDebtInFront: string): string => {
|
|
186
|
+
const selectedMarketUnbackedDebt = new Dec(allMarketsUnbackedDebts[selectedMarket]);
|
|
187
|
+
if (selectedMarketUnbackedDebt.eq(0)) return 'N/A';
|
|
188
|
+
|
|
189
|
+
const amountBeingReedemedOnEachMarket = Object.entries(markets).map(([version, market]) => {
|
|
190
|
+
if (version === selectedMarket) return new Dec(interestRateDebtInFront);
|
|
191
|
+
const { assetsData } = market;
|
|
192
|
+
const unbackedDebt = new Dec(allMarketsUnbackedDebts[version as LiquityV2Versions]);
|
|
193
|
+
const totalBorrow = new Dec(assetsData.BOLD.totalBorrow);
|
|
194
|
+
const amountToReedem = new Dec(interestRateDebtInFront).mul(unbackedDebt).div(selectedMarketUnbackedDebt);
|
|
195
|
+
return Dec.min(amountToReedem, totalBorrow);
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
return amountBeingReedemedOnEachMarket.reduce((acc, val) => acc.plus(val), new Dec(0)).toString();
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
export const getDebtInFrontLiquityV2 = async (markets: Record<LiquityV2Versions, LiquityV2MarketData>, selectedMarket: LiquityV2Versions, web3: Web3, network: NetworkNumber, viewContract: any, troveId: string) => {
|
|
202
|
+
const allMarketsUnbackedDebts = await getAllMarketsUnbackedDebts(markets, web3, network);
|
|
203
|
+
const interestRateDebtInFront = await getDebtInFrontForSingleMarketLiquityV2(viewContract, LiquityV2Markets(network)[selectedMarket].marketAddress, troveId);
|
|
204
|
+
|
|
205
|
+
return calculateDebtInFrontLiquityV2(markets, selectedMarket, allMarketsUnbackedDebts, interestRateDebtInFront.toString());
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
export const getDebtInFrontForInterestRateLiquityV2 = async (markets: Record<LiquityV2Versions, LiquityV2MarketData>, selectedMarket: LiquityV2Versions, web3: Web3, network: NetworkNumber, viewContract: any, interestRate: string) => {
|
|
209
|
+
const allMarketsUnbackedDebts = await getAllMarketsUnbackedDebts(markets, web3, network);
|
|
210
|
+
const interestRateDebtInFront = new Dec(await getDebtInFrontForInterestRateSingleMarketLiquityV2(viewContract, LiquityV2Markets(network)[selectedMarket].marketAddress, interestRate));
|
|
211
|
+
|
|
212
|
+
return calculateDebtInFrontLiquityV2(markets, selectedMarket, allMarketsUnbackedDebts, interestRateDebtInFront.toString());
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
export const getLiquityV2TroveData = async (
|
|
216
|
+
web3: Web3,
|
|
217
|
+
network: NetworkNumber,
|
|
218
|
+
{
|
|
219
|
+
selectedMarket,
|
|
220
|
+
assetsData,
|
|
221
|
+
troveId,
|
|
222
|
+
allMarketsData,
|
|
223
|
+
}:
|
|
224
|
+
{
|
|
225
|
+
selectedMarket: LiquityV2MarketInfo,
|
|
226
|
+
assetsData: LiquityV2AssetsData,
|
|
227
|
+
troveId: string,
|
|
228
|
+
allMarketsData: Record<LiquityV2Versions, LiquityV2MarketData>,
|
|
229
|
+
},
|
|
230
|
+
): Promise<LiquityV2TroveData> => {
|
|
231
|
+
const viewContract = LiquityV2ViewContract(web3, network);
|
|
232
|
+
const { minCollRatio } = allMarketsData[selectedMarket.value].marketData;
|
|
233
|
+
const { collateralToken, marketAddress, debtToken } = selectedMarket;
|
|
234
|
+
const [_data, debtInFront] = await Promise.all([
|
|
235
|
+
viewContract.methods.getTroveInfo(marketAddress, troveId).call(),
|
|
236
|
+
getDebtInFrontLiquityV2(allMarketsData, selectedMarket.value, web3, network, viewContract, troveId),
|
|
237
|
+
]);
|
|
238
|
+
const data = {
|
|
239
|
+
..._data,
|
|
240
|
+
TCRatio: _data.TCRatio === MAXUINT ? '0' : _data.TCRatio, // mistake on contract side when debt is 0
|
|
241
|
+
};
|
|
242
|
+
const usedAssets: LiquityV2UsedAssets = {};
|
|
243
|
+
|
|
244
|
+
const debtAssetData = assetsData[debtToken];
|
|
245
|
+
const borrowed = assetAmountInEth(data.debtAmount);
|
|
246
|
+
usedAssets[debtToken] = {
|
|
247
|
+
symbol: debtToken,
|
|
248
|
+
supplied: '0',
|
|
249
|
+
suppliedUsd: '0',
|
|
250
|
+
borrowed,
|
|
251
|
+
borrowedUsd: new Dec(borrowed).mul(debtAssetData.price).toString(),
|
|
252
|
+
isBorrowed: true,
|
|
253
|
+
isSupplied: false,
|
|
254
|
+
};
|
|
255
|
+
|
|
256
|
+
const collAssetData = assetsData[collateralToken];
|
|
257
|
+
const suppliedColl = assetAmountInEth(data.collAmount);
|
|
258
|
+
usedAssets[collateralToken] = {
|
|
259
|
+
symbol: collateralToken,
|
|
260
|
+
supplied: suppliedColl,
|
|
261
|
+
suppliedUsd: new Dec(suppliedColl).mul(collAssetData.price).toString(),
|
|
262
|
+
borrowed: '0',
|
|
263
|
+
borrowedUsd: '0',
|
|
264
|
+
isBorrowed: false,
|
|
265
|
+
isSupplied: true,
|
|
266
|
+
collateral: true,
|
|
267
|
+
};
|
|
268
|
+
|
|
269
|
+
const collRatio = new Dec(data.TCRatio).div(1e16).toString();
|
|
270
|
+
const interestRate = new Dec(data.annualInterestRate).div(1e16).toString();
|
|
271
|
+
const interestBatchManager = data.interestBatchManager;
|
|
272
|
+
const lastInterestRateAdjTime = data.lastInterestRateAdjTime;
|
|
273
|
+
|
|
274
|
+
const payload: LiquityV2TroveData = {
|
|
275
|
+
usedAssets,
|
|
276
|
+
troveId,
|
|
277
|
+
interestRate,
|
|
278
|
+
interestBatchManager,
|
|
279
|
+
debtInFront,
|
|
280
|
+
lastInterestRateAdjTime,
|
|
281
|
+
troveStatus: LIQUITY_V2_TROVE_STATUS_ENUM[parseInt(data.status, 10)],
|
|
282
|
+
...getLiquityV2AggregatedPositionData({
|
|
283
|
+
usedAssets, assetsData, minCollRatio, interestRate,
|
|
284
|
+
}),
|
|
285
|
+
collRatio,
|
|
286
|
+
};
|
|
287
|
+
|
|
288
|
+
return payload;
|
|
289
|
+
};
|
|
290
|
+
|
|
291
|
+
export const getLiquityV2ClaimableCollateral = async (collSurplusPoolAddress: EthAddress, account: EthAddress, web3: Web3, network: NetworkNumber): Promise<string> => {
|
|
292
|
+
const collSurplusPoolContract = createContractWrapper(web3, network, 'LiquityV2CollSurplusPool', collSurplusPoolAddress);
|
|
293
|
+
const claimableCollateral = await collSurplusPoolContract.methods.getCollateral(account).call();
|
|
294
|
+
return claimableCollateral;
|
|
295
|
+
};
|