@defisaver/positions-sdk 0.0.201 → 0.0.202
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/helpers/morphoBlueHelpers/index.js +66 -66
- package/cjs/markets/spark/marketAssets.js +1 -1
- package/esm/helpers/morphoBlueHelpers/index.js +66 -66
- package/esm/markets/spark/marketAssets.js +1 -1
- package/package.json +49 -49
- package/src/aaveV2/index.ts +227 -227
- package/src/aaveV3/index.ts +625 -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 +1109 -1109
- package/src/constants/index.ts +6 -6
- package/src/contracts.ts +133 -133
- 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 -198
- 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 +10 -10
- package/src/helpers/liquityV2Helpers/index.ts +79 -79
- package/src/helpers/llamaLendHelpers/index.ts +53 -53
- package/src/helpers/makerHelpers/index.ts +94 -94
- package/src/helpers/morphoBlueHelpers/index.ts +365 -365
- package/src/helpers/sparkHelpers/index.ts +150 -150
- package/src/index.ts +50 -50
- package/src/liquity/index.ts +116 -116
- package/src/liquityV2/index.ts +227 -227
- 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/index.ts +24 -24
- package/src/markets/liquityV2/index.ts +43 -43
- 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 +631 -631
- package/src/morphoBlue/index.ts +204 -204
- package/src/multicall/index.ts +22 -22
- package/src/services/dsrService.ts +15 -15
- package/src/services/priceService.ts +62 -62
- 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 -220
- package/src/types/aave.ts +271 -271
- package/src/types/chickenBonds.ts +45 -45
- package/src/types/common.ts +84 -84
- package/src/types/compound.ts +131 -131
- package/src/types/curveUsd.ts +118 -118
- package/src/types/euler.ts +171 -171
- package/src/types/index.ts +10 -10
- package/src/types/liquity.ts +30 -30
- package/src/types/liquityV2.ts +118 -118
- package/src/types/llamaLend.ts +155 -155
- package/src/types/maker.ts +50 -50
- package/src/types/morphoBlue.ts +192 -192
- package/src/types/spark.ts +131 -131
package/src/liquityV2/index.ts
CHANGED
|
@@ -1,228 +1,228 @@
|
|
|
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
|
-
} from '../types';
|
|
11
|
-
import { getStakingApy, STAKING_ASSETS } from '../staking';
|
|
12
|
-
import { getLiquityV2AggregatedPositionData } from '../helpers/liquityV2Helpers';
|
|
13
|
-
import { addToObjectIf, compareAddresses, ethToWeth } from '../services/utils';
|
|
14
|
-
import { LiquityV2View } from '../types/contracts/generated';
|
|
15
|
-
import { ZERO_ADDRESS } from '../constants';
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
export const getLiquityV2MarketData = async (web3: Web3, network: NetworkNumber, selectedMarket: LiquityV2MarketInfo, mainnetWeb3: Web3): Promise<LiquityV2MarketData> => {
|
|
19
|
-
const viewContract = LiquityV2ViewContract(web3, network);
|
|
20
|
-
const { marketAddress, debtToken, collateralToken } = selectedMarket;
|
|
21
|
-
const data = await viewContract.methods.getMarketData(marketAddress).call();
|
|
22
|
-
const hintHelperAddress = data.hintHelpers;
|
|
23
|
-
const troveNFTAddress = data.troveNFT;
|
|
24
|
-
const borrowerOperationsAddress = data.borrowerOperations;
|
|
25
|
-
const troveManagerAddress = data.troveManager;
|
|
26
|
-
const stabilityPoolAddress = data.stabilityPool;
|
|
27
|
-
const collSurplusPoolAddress = data.collSurplusPool;
|
|
28
|
-
const activePoolAddress = data.activePool;
|
|
29
|
-
|
|
30
|
-
const minCollRatio = new Dec(data.MCR).div(1e16).toString();
|
|
31
|
-
const criticalCollRatio = new Dec(data.CCR).div(1e18).toString();
|
|
32
|
-
|
|
33
|
-
const totalMarketBorrow = assetAmountInEth(data.entireSystemDebt);
|
|
34
|
-
const totalMarketSupply = assetAmountInEth(data.entireSystemColl);
|
|
35
|
-
const collPrice = assetAmountInEth(data.collPrice);
|
|
36
|
-
|
|
37
|
-
const totalCollRatio = new Dec(totalMarketSupply).mul(collPrice).div(totalMarketBorrow).toString();
|
|
38
|
-
const leftToBorrowGlobal = new Dec(totalMarketSupply).mul(collPrice).div(criticalCollRatio).sub(totalMarketBorrow)
|
|
39
|
-
.toString();
|
|
40
|
-
const minCollAmountForCurrentBorrow = new Dec(totalMarketBorrow).mul(criticalCollRatio).div(collPrice).toString();
|
|
41
|
-
const leftToWithdrawGlobal = new Dec(totalMarketSupply).sub(minCollAmountForCurrentBorrow).toString();
|
|
42
|
-
|
|
43
|
-
const assetsData: LiquityV2AssetsData = {};
|
|
44
|
-
assetsData[debtToken] = {
|
|
45
|
-
symbol: debtToken,
|
|
46
|
-
address: getAssetInfo(debtToken, network).address,
|
|
47
|
-
price: '1',
|
|
48
|
-
totalSupply: '0',
|
|
49
|
-
totalBorrow: totalMarketBorrow,
|
|
50
|
-
canBeSupplied: false,
|
|
51
|
-
canBeBorrowed: true,
|
|
52
|
-
leftToBorrowGlobal,
|
|
53
|
-
leftToWithdrawGlobal: '0',
|
|
54
|
-
};
|
|
55
|
-
assetsData[collateralToken] = {
|
|
56
|
-
symbol: collateralToken,
|
|
57
|
-
address: getAssetInfo(ethToWeth(collateralToken), network).address,
|
|
58
|
-
price: collPrice,
|
|
59
|
-
totalSupply: totalMarketSupply,
|
|
60
|
-
totalBorrow: '0',
|
|
61
|
-
canBeSupplied: true,
|
|
62
|
-
canBeBorrowed: false,
|
|
63
|
-
leftToBorrowGlobal: '0',
|
|
64
|
-
leftToWithdrawGlobal,
|
|
65
|
-
};
|
|
66
|
-
if (STAKING_ASSETS.includes(collateralToken)) {
|
|
67
|
-
assetsData[collateralToken].incentiveSupplyApy = await getStakingApy(collateralToken, mainnetWeb3);
|
|
68
|
-
assetsData[collateralToken].incentiveSupplyToken = collateralToken;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
return {
|
|
72
|
-
assetsData,
|
|
73
|
-
marketData: {
|
|
74
|
-
minCollRatio,
|
|
75
|
-
totalCollRatio: new Dec(totalCollRatio).mul(100).toString(),
|
|
76
|
-
criticalCollRatio: new Dec(criticalCollRatio).mul(100).toString(),
|
|
77
|
-
isUnderCollateralized: new Dec(totalCollRatio).lt(criticalCollRatio),
|
|
78
|
-
hintHelperAddress,
|
|
79
|
-
troveNFTAddress,
|
|
80
|
-
borrowerOperationsAddress,
|
|
81
|
-
troveManagerAddress,
|
|
82
|
-
stabilityPoolAddress,
|
|
83
|
-
collSurplusPoolAddress,
|
|
84
|
-
activePoolAddress,
|
|
85
|
-
},
|
|
86
|
-
};
|
|
87
|
-
};
|
|
88
|
-
|
|
89
|
-
const _getUserTroves = async (viewContract: any, account: EthAddress, marketAddress: EthAddress, startIndex = 0, endIndex = 100) => viewContract.methods.getUserTroves(account, marketAddress, startIndex, endIndex).call();
|
|
90
|
-
|
|
91
|
-
const getUserTroves = async (viewContract: any, account: EthAddress, marketAddress: EthAddress, startIndex = 0, endIndex = 100, troves: LiquityV2View.ExistingTroveStructOutput[] = []): Promise<{ troves: LiquityV2View.ExistingTroveStructOutput[], nextFreeTroveIndex: string }> => {
|
|
92
|
-
const result = await _getUserTroves(viewContract, account, marketAddress, startIndex, endIndex);
|
|
93
|
-
const newStartIndex = endIndex + 1;
|
|
94
|
-
const nextFreeTroveIndex = result.nextFreeTroveIndex;
|
|
95
|
-
const existingTroves = [...troves, ...result.troves];
|
|
96
|
-
if (nextFreeTroveIndex !== '-1') return { troves: existingTroves.filter((trove) => trove.ownedByUser), nextFreeTroveIndex };
|
|
97
|
-
return getUserTroves(viewContract, account, marketAddress, newStartIndex, newStartIndex + 100, existingTroves);
|
|
98
|
-
};
|
|
99
|
-
|
|
100
|
-
const TransferEventSig = '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef';
|
|
101
|
-
|
|
102
|
-
const nftContractCreationBlockMapping = {
|
|
103
|
-
ETH: 21686215,
|
|
104
|
-
wstETH: 21686238,
|
|
105
|
-
rETH: 21686257,
|
|
106
|
-
};
|
|
107
|
-
|
|
108
|
-
const getTransferredTroves = async (web3: Web3, network: NetworkNumber, troveNFTAddress: EthAddress, limitBlocksForEventFetching: boolean, collAsset: string, account: EthAddress): Promise<{ troveId: string }[]> => {
|
|
109
|
-
const nftContract = createContractWrapper(web3, network, 'LiquityV2TroveNFT', troveNFTAddress);
|
|
110
|
-
// @ts-ignore
|
|
111
|
-
const nftContractCreationBlock = nftContractCreationBlockMapping[collAsset];
|
|
112
|
-
const currentBlock = await web3.eth.getBlockNumber();
|
|
113
|
-
const events = await nftContract.getPastEvents(
|
|
114
|
-
TransferEventSig,
|
|
115
|
-
{
|
|
116
|
-
filter: { to: account },
|
|
117
|
-
fromBlock: limitBlocksForEventFetching ? (currentBlock - 1000) : nftContractCreationBlock,
|
|
118
|
-
},
|
|
119
|
-
);
|
|
120
|
-
const userTransferredTroves = events.filter((event) => !compareAddresses(event.returnValues.from, ZERO_ADDRESS) && compareAddresses(event.returnValues.to, account));
|
|
121
|
-
|
|
122
|
-
// check if the last know transfer address is the user
|
|
123
|
-
userTransferredTroves.forEach((event, index) => {
|
|
124
|
-
const otherTransfers = events.filter((e) => event.blockNumber < e.blockNumber && e.returnValues.tokenId === event.returnValues.tokenId);
|
|
125
|
-
// @ts-ignore
|
|
126
|
-
userTransferredTroves[index].invalid = !!otherTransfers.length;
|
|
127
|
-
});
|
|
128
|
-
// @ts-ignore
|
|
129
|
-
return userTransferredTroves.filter((event) => !event.invalid).map((event) => ({ troveId: event.returnValues.tokenId }));
|
|
130
|
-
};
|
|
131
|
-
|
|
132
|
-
export const getLiquityV2UserTroveIds = async (web3: Web3, network: NetworkNumber, selectedMarket: LiquityV2MarketInfo, troveNFTAddress: EthAddress, limitBlocksForEventFetching: boolean, account: EthAddress): Promise<{ troves: { troveId: string }[], nextFreeTroveIndex: string }> => {
|
|
133
|
-
const viewContract = LiquityV2ViewContract(web3, network);
|
|
134
|
-
const [{ troves: userTroves, nextFreeTroveIndex }, userTransferredTroves] = await Promise.all([
|
|
135
|
-
getUserTroves(viewContract, account, selectedMarket.marketAddress),
|
|
136
|
-
getTransferredTroves(web3, network, troveNFTAddress, limitBlocksForEventFetching, selectedMarket.collateralToken, account),
|
|
137
|
-
]);
|
|
138
|
-
const troves = [...userTroves.map(({ troveId }) => ({ troveId })), ...userTransferredTroves];
|
|
139
|
-
const filteredTroves = troves.filter((value, index, self) => index === self.findIndex((t) => (
|
|
140
|
-
t.troveId === value.troveId
|
|
141
|
-
)),
|
|
142
|
-
);
|
|
143
|
-
return { troves: filteredTroves, nextFreeTroveIndex };
|
|
144
|
-
};
|
|
145
|
-
|
|
146
|
-
const _getDebtInFront = async (viewContract: any, marketAddress: EthAddress, troveId: string, accumulatedSum = '0', iterations = 2000) => viewContract.methods.getDebtInFront(marketAddress, troveId, accumulatedSum, iterations).call();
|
|
147
|
-
|
|
148
|
-
export const getDebtInFrontLiquityV2 = async (viewContract: any, marketAddress: EthAddress, troveId: string, accumulatedSum = '0', iterations = 2000): Promise<string> => {
|
|
149
|
-
const { debt, next } = await _getDebtInFront(viewContract, marketAddress, troveId, accumulatedSum, iterations);
|
|
150
|
-
if (next === '0') return assetAmountInEth(debt);
|
|
151
|
-
return getDebtInFrontLiquityV2(viewContract, marketAddress, next, debt, iterations);
|
|
152
|
-
};
|
|
153
|
-
|
|
154
|
-
export const getLiquityV2TroveData = async (
|
|
155
|
-
web3: Web3,
|
|
156
|
-
network: NetworkNumber,
|
|
157
|
-
{
|
|
158
|
-
selectedMarket,
|
|
159
|
-
assetsData,
|
|
160
|
-
marketData,
|
|
161
|
-
troveId,
|
|
162
|
-
}:
|
|
163
|
-
{
|
|
164
|
-
selectedMarket: LiquityV2MarketInfo,
|
|
165
|
-
assetsData: LiquityV2AssetsData,
|
|
166
|
-
marketData: InnerLiquityV2MarketData,
|
|
167
|
-
troveId: string
|
|
168
|
-
},
|
|
169
|
-
): Promise<LiquityV2TroveData> => {
|
|
170
|
-
const viewContract = LiquityV2ViewContract(web3, network);
|
|
171
|
-
const { minCollRatio } = marketData;
|
|
172
|
-
const { collateralToken, marketAddress, debtToken } = selectedMarket;
|
|
173
|
-
const [data, debtInFront] = await Promise.all([
|
|
174
|
-
viewContract.methods.getTroveInfo(marketAddress, troveId).call(),
|
|
175
|
-
getDebtInFrontLiquityV2(viewContract, marketAddress, troveId),
|
|
176
|
-
]);
|
|
177
|
-
const usedAssets: LiquityV2UsedAssets = {};
|
|
178
|
-
|
|
179
|
-
const debtAssetData = assetsData[debtToken];
|
|
180
|
-
const borrowed = assetAmountInEth(data.debtAmount);
|
|
181
|
-
usedAssets[debtToken] = {
|
|
182
|
-
symbol: debtToken,
|
|
183
|
-
supplied: '0',
|
|
184
|
-
suppliedUsd: '0',
|
|
185
|
-
borrowed,
|
|
186
|
-
borrowedUsd: new Dec(borrowed).mul(debtAssetData.price).toString(),
|
|
187
|
-
isBorrowed: true,
|
|
188
|
-
isSupplied: false,
|
|
189
|
-
};
|
|
190
|
-
|
|
191
|
-
const collAssetData = assetsData[collateralToken];
|
|
192
|
-
const suppliedColl = assetAmountInEth(data.collAmount);
|
|
193
|
-
usedAssets[collateralToken] = {
|
|
194
|
-
symbol: collateralToken,
|
|
195
|
-
supplied: suppliedColl,
|
|
196
|
-
suppliedUsd: new Dec(suppliedColl).mul(collAssetData.price).toString(),
|
|
197
|
-
borrowed: '0',
|
|
198
|
-
borrowedUsd: '0',
|
|
199
|
-
isBorrowed: false,
|
|
200
|
-
isSupplied: true,
|
|
201
|
-
collateral: true,
|
|
202
|
-
};
|
|
203
|
-
|
|
204
|
-
const collRatio = new Dec(data.TCRatio).div(1e16).toString();
|
|
205
|
-
const interestRate = new Dec(data.annualInterestRate).div(1e16).toString();
|
|
206
|
-
const interestBatchManager = data.interestBatchManager;
|
|
207
|
-
|
|
208
|
-
const payload: LiquityV2TroveData = {
|
|
209
|
-
usedAssets,
|
|
210
|
-
troveId,
|
|
211
|
-
interestRate,
|
|
212
|
-
interestBatchManager,
|
|
213
|
-
debtInFront,
|
|
214
|
-
troveStatus: LIQUITY_V2_TROVE_STATUS_ENUM[parseInt(data.status, 10)],
|
|
215
|
-
...getLiquityV2AggregatedPositionData({
|
|
216
|
-
usedAssets, assetsData, minCollRatio, interestRate,
|
|
217
|
-
}),
|
|
218
|
-
collRatio,
|
|
219
|
-
};
|
|
220
|
-
|
|
221
|
-
return payload;
|
|
222
|
-
};
|
|
223
|
-
|
|
224
|
-
export const getLiquityV2ClaimableCollateral = async (collSurplusPoolAddress: EthAddress, account: EthAddress, web3: Web3, network: NetworkNumber): Promise<string> => {
|
|
225
|
-
const collSurplusPoolContract = createContractWrapper(web3, network, 'LiquityV2CollSurplusPool', collSurplusPoolAddress);
|
|
226
|
-
const claimableCollateral = await collSurplusPoolContract.methods.getCollateral(account).call();
|
|
227
|
-
return claimableCollateral;
|
|
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
|
+
} from '../types';
|
|
11
|
+
import { getStakingApy, STAKING_ASSETS } from '../staking';
|
|
12
|
+
import { getLiquityV2AggregatedPositionData } from '../helpers/liquityV2Helpers';
|
|
13
|
+
import { addToObjectIf, compareAddresses, ethToWeth } from '../services/utils';
|
|
14
|
+
import { LiquityV2View } from '../types/contracts/generated';
|
|
15
|
+
import { ZERO_ADDRESS } from '../constants';
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
export const getLiquityV2MarketData = async (web3: Web3, network: NetworkNumber, selectedMarket: LiquityV2MarketInfo, mainnetWeb3: Web3): Promise<LiquityV2MarketData> => {
|
|
19
|
+
const viewContract = LiquityV2ViewContract(web3, network);
|
|
20
|
+
const { marketAddress, debtToken, collateralToken } = selectedMarket;
|
|
21
|
+
const data = await viewContract.methods.getMarketData(marketAddress).call();
|
|
22
|
+
const hintHelperAddress = data.hintHelpers;
|
|
23
|
+
const troveNFTAddress = data.troveNFT;
|
|
24
|
+
const borrowerOperationsAddress = data.borrowerOperations;
|
|
25
|
+
const troveManagerAddress = data.troveManager;
|
|
26
|
+
const stabilityPoolAddress = data.stabilityPool;
|
|
27
|
+
const collSurplusPoolAddress = data.collSurplusPool;
|
|
28
|
+
const activePoolAddress = data.activePool;
|
|
29
|
+
|
|
30
|
+
const minCollRatio = new Dec(data.MCR).div(1e16).toString();
|
|
31
|
+
const criticalCollRatio = new Dec(data.CCR).div(1e18).toString();
|
|
32
|
+
|
|
33
|
+
const totalMarketBorrow = assetAmountInEth(data.entireSystemDebt);
|
|
34
|
+
const totalMarketSupply = assetAmountInEth(data.entireSystemColl);
|
|
35
|
+
const collPrice = assetAmountInEth(data.collPrice);
|
|
36
|
+
|
|
37
|
+
const totalCollRatio = new Dec(totalMarketSupply).mul(collPrice).div(totalMarketBorrow).toString();
|
|
38
|
+
const leftToBorrowGlobal = new Dec(totalMarketSupply).mul(collPrice).div(criticalCollRatio).sub(totalMarketBorrow)
|
|
39
|
+
.toString();
|
|
40
|
+
const minCollAmountForCurrentBorrow = new Dec(totalMarketBorrow).mul(criticalCollRatio).div(collPrice).toString();
|
|
41
|
+
const leftToWithdrawGlobal = new Dec(totalMarketSupply).sub(minCollAmountForCurrentBorrow).toString();
|
|
42
|
+
|
|
43
|
+
const assetsData: LiquityV2AssetsData = {};
|
|
44
|
+
assetsData[debtToken] = {
|
|
45
|
+
symbol: debtToken,
|
|
46
|
+
address: getAssetInfo(debtToken, network).address,
|
|
47
|
+
price: '1',
|
|
48
|
+
totalSupply: '0',
|
|
49
|
+
totalBorrow: totalMarketBorrow,
|
|
50
|
+
canBeSupplied: false,
|
|
51
|
+
canBeBorrowed: true,
|
|
52
|
+
leftToBorrowGlobal,
|
|
53
|
+
leftToWithdrawGlobal: '0',
|
|
54
|
+
};
|
|
55
|
+
assetsData[collateralToken] = {
|
|
56
|
+
symbol: collateralToken,
|
|
57
|
+
address: getAssetInfo(ethToWeth(collateralToken), network).address,
|
|
58
|
+
price: collPrice,
|
|
59
|
+
totalSupply: totalMarketSupply,
|
|
60
|
+
totalBorrow: '0',
|
|
61
|
+
canBeSupplied: true,
|
|
62
|
+
canBeBorrowed: false,
|
|
63
|
+
leftToBorrowGlobal: '0',
|
|
64
|
+
leftToWithdrawGlobal,
|
|
65
|
+
};
|
|
66
|
+
if (STAKING_ASSETS.includes(collateralToken)) {
|
|
67
|
+
assetsData[collateralToken].incentiveSupplyApy = await getStakingApy(collateralToken, mainnetWeb3);
|
|
68
|
+
assetsData[collateralToken].incentiveSupplyToken = collateralToken;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return {
|
|
72
|
+
assetsData,
|
|
73
|
+
marketData: {
|
|
74
|
+
minCollRatio,
|
|
75
|
+
totalCollRatio: new Dec(totalCollRatio).mul(100).toString(),
|
|
76
|
+
criticalCollRatio: new Dec(criticalCollRatio).mul(100).toString(),
|
|
77
|
+
isUnderCollateralized: new Dec(totalCollRatio).lt(criticalCollRatio),
|
|
78
|
+
hintHelperAddress,
|
|
79
|
+
troveNFTAddress,
|
|
80
|
+
borrowerOperationsAddress,
|
|
81
|
+
troveManagerAddress,
|
|
82
|
+
stabilityPoolAddress,
|
|
83
|
+
collSurplusPoolAddress,
|
|
84
|
+
activePoolAddress,
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
const _getUserTroves = async (viewContract: any, account: EthAddress, marketAddress: EthAddress, startIndex = 0, endIndex = 100) => viewContract.methods.getUserTroves(account, marketAddress, startIndex, endIndex).call();
|
|
90
|
+
|
|
91
|
+
const getUserTroves = async (viewContract: any, account: EthAddress, marketAddress: EthAddress, startIndex = 0, endIndex = 100, troves: LiquityV2View.ExistingTroveStructOutput[] = []): Promise<{ troves: LiquityV2View.ExistingTroveStructOutput[], nextFreeTroveIndex: string }> => {
|
|
92
|
+
const result = await _getUserTroves(viewContract, account, marketAddress, startIndex, endIndex);
|
|
93
|
+
const newStartIndex = endIndex + 1;
|
|
94
|
+
const nextFreeTroveIndex = result.nextFreeTroveIndex;
|
|
95
|
+
const existingTroves = [...troves, ...result.troves];
|
|
96
|
+
if (nextFreeTroveIndex !== '-1') return { troves: existingTroves.filter((trove) => trove.ownedByUser), nextFreeTroveIndex };
|
|
97
|
+
return getUserTroves(viewContract, account, marketAddress, newStartIndex, newStartIndex + 100, existingTroves);
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
const TransferEventSig = '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef';
|
|
101
|
+
|
|
102
|
+
const nftContractCreationBlockMapping = {
|
|
103
|
+
ETH: 21686215,
|
|
104
|
+
wstETH: 21686238,
|
|
105
|
+
rETH: 21686257,
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
const getTransferredTroves = async (web3: Web3, network: NetworkNumber, troveNFTAddress: EthAddress, limitBlocksForEventFetching: boolean, collAsset: string, account: EthAddress): Promise<{ troveId: string }[]> => {
|
|
109
|
+
const nftContract = createContractWrapper(web3, network, 'LiquityV2TroveNFT', troveNFTAddress);
|
|
110
|
+
// @ts-ignore
|
|
111
|
+
const nftContractCreationBlock = nftContractCreationBlockMapping[collAsset];
|
|
112
|
+
const currentBlock = await web3.eth.getBlockNumber();
|
|
113
|
+
const events = await nftContract.getPastEvents(
|
|
114
|
+
TransferEventSig,
|
|
115
|
+
{
|
|
116
|
+
filter: { to: account },
|
|
117
|
+
fromBlock: limitBlocksForEventFetching ? (currentBlock - 1000) : nftContractCreationBlock,
|
|
118
|
+
},
|
|
119
|
+
);
|
|
120
|
+
const userTransferredTroves = events.filter((event) => !compareAddresses(event.returnValues.from, ZERO_ADDRESS) && compareAddresses(event.returnValues.to, account));
|
|
121
|
+
|
|
122
|
+
// check if the last know transfer address is the user
|
|
123
|
+
userTransferredTroves.forEach((event, index) => {
|
|
124
|
+
const otherTransfers = events.filter((e) => event.blockNumber < e.blockNumber && e.returnValues.tokenId === event.returnValues.tokenId);
|
|
125
|
+
// @ts-ignore
|
|
126
|
+
userTransferredTroves[index].invalid = !!otherTransfers.length;
|
|
127
|
+
});
|
|
128
|
+
// @ts-ignore
|
|
129
|
+
return userTransferredTroves.filter((event) => !event.invalid).map((event) => ({ troveId: event.returnValues.tokenId }));
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
export const getLiquityV2UserTroveIds = async (web3: Web3, network: NetworkNumber, selectedMarket: LiquityV2MarketInfo, troveNFTAddress: EthAddress, limitBlocksForEventFetching: boolean, account: EthAddress): Promise<{ troves: { troveId: string }[], nextFreeTroveIndex: string }> => {
|
|
133
|
+
const viewContract = LiquityV2ViewContract(web3, network);
|
|
134
|
+
const [{ troves: userTroves, nextFreeTroveIndex }, userTransferredTroves] = await Promise.all([
|
|
135
|
+
getUserTroves(viewContract, account, selectedMarket.marketAddress),
|
|
136
|
+
getTransferredTroves(web3, network, troveNFTAddress, limitBlocksForEventFetching, selectedMarket.collateralToken, account),
|
|
137
|
+
]);
|
|
138
|
+
const troves = [...userTroves.map(({ troveId }) => ({ troveId })), ...userTransferredTroves];
|
|
139
|
+
const filteredTroves = troves.filter((value, index, self) => index === self.findIndex((t) => (
|
|
140
|
+
t.troveId === value.troveId
|
|
141
|
+
)),
|
|
142
|
+
);
|
|
143
|
+
return { troves: filteredTroves, nextFreeTroveIndex };
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
const _getDebtInFront = async (viewContract: any, marketAddress: EthAddress, troveId: string, accumulatedSum = '0', iterations = 2000) => viewContract.methods.getDebtInFront(marketAddress, troveId, accumulatedSum, iterations).call();
|
|
147
|
+
|
|
148
|
+
export const getDebtInFrontLiquityV2 = async (viewContract: any, marketAddress: EthAddress, troveId: string, accumulatedSum = '0', iterations = 2000): Promise<string> => {
|
|
149
|
+
const { debt, next } = await _getDebtInFront(viewContract, marketAddress, troveId, accumulatedSum, iterations);
|
|
150
|
+
if (next === '0') return assetAmountInEth(debt);
|
|
151
|
+
return getDebtInFrontLiquityV2(viewContract, marketAddress, next, debt, iterations);
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
export const getLiquityV2TroveData = async (
|
|
155
|
+
web3: Web3,
|
|
156
|
+
network: NetworkNumber,
|
|
157
|
+
{
|
|
158
|
+
selectedMarket,
|
|
159
|
+
assetsData,
|
|
160
|
+
marketData,
|
|
161
|
+
troveId,
|
|
162
|
+
}:
|
|
163
|
+
{
|
|
164
|
+
selectedMarket: LiquityV2MarketInfo,
|
|
165
|
+
assetsData: LiquityV2AssetsData,
|
|
166
|
+
marketData: InnerLiquityV2MarketData,
|
|
167
|
+
troveId: string
|
|
168
|
+
},
|
|
169
|
+
): Promise<LiquityV2TroveData> => {
|
|
170
|
+
const viewContract = LiquityV2ViewContract(web3, network);
|
|
171
|
+
const { minCollRatio } = marketData;
|
|
172
|
+
const { collateralToken, marketAddress, debtToken } = selectedMarket;
|
|
173
|
+
const [data, debtInFront] = await Promise.all([
|
|
174
|
+
viewContract.methods.getTroveInfo(marketAddress, troveId).call(),
|
|
175
|
+
getDebtInFrontLiquityV2(viewContract, marketAddress, troveId),
|
|
176
|
+
]);
|
|
177
|
+
const usedAssets: LiquityV2UsedAssets = {};
|
|
178
|
+
|
|
179
|
+
const debtAssetData = assetsData[debtToken];
|
|
180
|
+
const borrowed = assetAmountInEth(data.debtAmount);
|
|
181
|
+
usedAssets[debtToken] = {
|
|
182
|
+
symbol: debtToken,
|
|
183
|
+
supplied: '0',
|
|
184
|
+
suppliedUsd: '0',
|
|
185
|
+
borrowed,
|
|
186
|
+
borrowedUsd: new Dec(borrowed).mul(debtAssetData.price).toString(),
|
|
187
|
+
isBorrowed: true,
|
|
188
|
+
isSupplied: false,
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
const collAssetData = assetsData[collateralToken];
|
|
192
|
+
const suppliedColl = assetAmountInEth(data.collAmount);
|
|
193
|
+
usedAssets[collateralToken] = {
|
|
194
|
+
symbol: collateralToken,
|
|
195
|
+
supplied: suppliedColl,
|
|
196
|
+
suppliedUsd: new Dec(suppliedColl).mul(collAssetData.price).toString(),
|
|
197
|
+
borrowed: '0',
|
|
198
|
+
borrowedUsd: '0',
|
|
199
|
+
isBorrowed: false,
|
|
200
|
+
isSupplied: true,
|
|
201
|
+
collateral: true,
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
const collRatio = new Dec(data.TCRatio).div(1e16).toString();
|
|
205
|
+
const interestRate = new Dec(data.annualInterestRate).div(1e16).toString();
|
|
206
|
+
const interestBatchManager = data.interestBatchManager;
|
|
207
|
+
|
|
208
|
+
const payload: LiquityV2TroveData = {
|
|
209
|
+
usedAssets,
|
|
210
|
+
troveId,
|
|
211
|
+
interestRate,
|
|
212
|
+
interestBatchManager,
|
|
213
|
+
debtInFront,
|
|
214
|
+
troveStatus: LIQUITY_V2_TROVE_STATUS_ENUM[parseInt(data.status, 10)],
|
|
215
|
+
...getLiquityV2AggregatedPositionData({
|
|
216
|
+
usedAssets, assetsData, minCollRatio, interestRate,
|
|
217
|
+
}),
|
|
218
|
+
collRatio,
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
return payload;
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
export const getLiquityV2ClaimableCollateral = async (collSurplusPoolAddress: EthAddress, account: EthAddress, web3: Web3, network: NetworkNumber): Promise<string> => {
|
|
225
|
+
const collSurplusPoolContract = createContractWrapper(web3, network, 'LiquityV2CollSurplusPool', collSurplusPoolAddress);
|
|
226
|
+
const claimableCollateral = await collSurplusPoolContract.methods.getCollateral(account).call();
|
|
227
|
+
return claimableCollateral;
|
|
228
228
|
};
|