@defisaver/positions-sdk 0.0.198 → 0.0.199-dev-2
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/config/contracts.d.ts +126 -4
- package/cjs/config/contracts.js +14 -0
- package/cjs/contracts.d.ts +1 -0
- package/cjs/contracts.js +2 -1
- package/cjs/helpers/index.d.ts +1 -0
- package/cjs/helpers/index.js +2 -1
- package/cjs/helpers/liquityV2Helpers/index.d.ts +12 -0
- package/cjs/helpers/liquityV2Helpers/index.js +63 -0
- package/cjs/helpers/morphoBlueHelpers/index.js +66 -66
- package/cjs/index.d.ts +2 -1
- package/cjs/index.js +3 -1
- package/cjs/liquityV2/index.d.ts +18 -0
- package/cjs/liquityV2/index.js +184 -0
- package/cjs/markets/aave/marketAssets.js +2 -4
- package/cjs/markets/index.d.ts +1 -0
- package/cjs/markets/index.js +3 -1
- package/cjs/markets/liquityV2/index.d.ts +10 -0
- package/cjs/markets/liquityV2/index.js +47 -0
- package/cjs/moneymarket/moneymarketCommonService.js +1 -1
- package/cjs/types/contracts/generated/LiquityV2CollSurplusPool.d.ts +64 -0
- package/cjs/types/contracts/generated/LiquityV2CollSurplusPool.js +5 -0
- package/cjs/types/contracts/generated/LiquityV2TroveNFT.d.ts +73 -0
- package/cjs/types/contracts/generated/LiquityV2TroveNFT.js +5 -0
- package/cjs/types/contracts/generated/LiquityV2View.d.ts +244 -0
- package/cjs/types/contracts/generated/LiquityV2View.js +5 -0
- package/cjs/types/contracts/generated/index.d.ts +3 -0
- package/cjs/types/index.d.ts +1 -0
- package/cjs/types/index.js +1 -0
- package/cjs/types/liquityV2.d.ts +111 -0
- package/cjs/types/liquityV2.js +24 -0
- package/esm/config/contracts.d.ts +126 -4
- package/esm/config/contracts.js +14 -0
- package/esm/contracts.d.ts +1 -0
- package/esm/contracts.js +1 -0
- package/esm/helpers/index.d.ts +1 -0
- package/esm/helpers/index.js +1 -0
- package/esm/helpers/liquityV2Helpers/index.d.ts +12 -0
- package/esm/helpers/liquityV2Helpers/index.js +55 -0
- package/esm/helpers/morphoBlueHelpers/index.js +66 -66
- package/esm/index.d.ts +2 -1
- package/esm/index.js +2 -1
- package/esm/liquityV2/index.d.ts +18 -0
- package/esm/liquityV2/index.js +173 -0
- package/esm/markets/aave/marketAssets.js +2 -4
- package/esm/markets/index.d.ts +1 -0
- package/esm/markets/index.js +1 -0
- package/esm/markets/liquityV2/index.d.ts +10 -0
- package/esm/markets/liquityV2/index.js +40 -0
- package/esm/moneymarket/moneymarketCommonService.js +1 -1
- package/esm/types/contracts/generated/LiquityV2CollSurplusPool.d.ts +64 -0
- package/esm/types/contracts/generated/LiquityV2CollSurplusPool.js +4 -0
- package/esm/types/contracts/generated/LiquityV2TroveNFT.d.ts +73 -0
- package/esm/types/contracts/generated/LiquityV2TroveNFT.js +4 -0
- package/esm/types/contracts/generated/LiquityV2View.d.ts +244 -0
- package/esm/types/contracts/generated/LiquityV2View.js +4 -0
- package/esm/types/contracts/generated/index.d.ts +3 -0
- package/esm/types/index.d.ts +1 -0
- package/esm/types/index.js +1 -0
- package/esm/types/liquityV2.d.ts +111 -0
- package/esm/types/liquityV2.js +21 -0
- 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 -1095
- package/src/constants/index.ts +6 -6
- package/src/contracts.ts +133 -132
- 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 -9
- package/src/helpers/liquityV2Helpers/index.ts +80 -0
- 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 -48
- package/src/liquity/index.ts +116 -116
- package/src/liquityV2/index.ts +220 -0
- 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 -46
- 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 +44 -0
- 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/contracts/generated/LiquityV2CollSurplusPool.ts +130 -0
- package/src/types/contracts/generated/LiquityV2TroveNFT.ts +150 -0
- package/src/types/contracts/generated/LiquityV2View.ts +315 -0
- package/src/types/contracts/generated/index.ts +3 -0
- package/src/types/curveUsd.ts +118 -118
- package/src/types/euler.ts +171 -171
- package/src/types/index.ts +10 -9
- package/src/types/liquity.ts +30 -30
- package/src/types/liquityV2.ts +119 -0
- 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/assets/index.ts
CHANGED
|
@@ -1,61 +1,61 @@
|
|
|
1
|
-
import { assetAmountInEth, getAssetInfo } from '@defisaver/tokens';
|
|
2
|
-
import Web3 from 'web3';
|
|
3
|
-
import { EthAddress, NetworkNumber } from '../types/common';
|
|
4
|
-
import { compareAddresses, isAddress } from '../services/utils';
|
|
5
|
-
import { BalanceScannerContract, getErc20Contract } from '../contracts';
|
|
6
|
-
|
|
7
|
-
export const getAssetBalance = async (
|
|
8
|
-
asset: string,
|
|
9
|
-
address: EthAddress,
|
|
10
|
-
chainId: NetworkNumber,
|
|
11
|
-
web3: Web3,
|
|
12
|
-
): Promise<string> => {
|
|
13
|
-
if (!address) return '0';
|
|
14
|
-
// noinspection JSUnusedAssignment
|
|
15
|
-
let data = '';
|
|
16
|
-
|
|
17
|
-
const isAssetAddress = isAddress(asset);
|
|
18
|
-
|
|
19
|
-
const network: NetworkNumber = chainId;
|
|
20
|
-
const isOptimism = network === 10;
|
|
21
|
-
|
|
22
|
-
if (
|
|
23
|
-
!isOptimism
|
|
24
|
-
&& (asset === 'ETH' || compareAddresses(asset, '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee'))
|
|
25
|
-
) {
|
|
26
|
-
data = await web3.eth.getBalance(address);
|
|
27
|
-
} else {
|
|
28
|
-
let _asset = isAssetAddress ? asset : getAssetInfo(asset).address;
|
|
29
|
-
|
|
30
|
-
if (isOptimism && compareAddresses(_asset, '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee')) {
|
|
31
|
-
_asset = '0xDeadDeAddeAddEAddeadDEaDDEAdDeaDDeAD0000';
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
const contract = await getErc20Contract(_asset, web3);
|
|
35
|
-
data = (await contract.methods.balanceOf(address).call()).toString();
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
return isAssetAddress
|
|
39
|
-
// @ts-ignore
|
|
40
|
-
? getEthAmountForDecimals(data, (await getERC20TokenData(asset)).decimals)
|
|
41
|
-
: assetAmountInEth(data, asset);
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
export const getAssetsBalances = async (assets: string[], address: EthAddress, networkId: NetworkNumber, web3: Web3): Promise<string[]> => {
|
|
45
|
-
const contract = BalanceScannerContract(web3, networkId);
|
|
46
|
-
|
|
47
|
-
const balancesFromChain = [...await contract.methods.tokensBalance(address, assets.map(a => getAssetInfo(a, networkId).address)).call()];
|
|
48
|
-
|
|
49
|
-
const balances: string[] = balancesFromChain.map((item, i) => (
|
|
50
|
-
item.success
|
|
51
|
-
? assetAmountInEth(web3.utils.hexToNumberString(item.data), assets[i])
|
|
52
|
-
: '0'
|
|
53
|
-
));
|
|
54
|
-
|
|
55
|
-
const ethIndex = assets.indexOf('ETH');
|
|
56
|
-
if (ethIndex !== -1) {
|
|
57
|
-
balances[ethIndex] = await getAssetBalance('ETH', address, networkId, web3);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
return balances;
|
|
1
|
+
import { assetAmountInEth, getAssetInfo } from '@defisaver/tokens';
|
|
2
|
+
import Web3 from 'web3';
|
|
3
|
+
import { EthAddress, NetworkNumber } from '../types/common';
|
|
4
|
+
import { compareAddresses, isAddress } from '../services/utils';
|
|
5
|
+
import { BalanceScannerContract, getErc20Contract } from '../contracts';
|
|
6
|
+
|
|
7
|
+
export const getAssetBalance = async (
|
|
8
|
+
asset: string,
|
|
9
|
+
address: EthAddress,
|
|
10
|
+
chainId: NetworkNumber,
|
|
11
|
+
web3: Web3,
|
|
12
|
+
): Promise<string> => {
|
|
13
|
+
if (!address) return '0';
|
|
14
|
+
// noinspection JSUnusedAssignment
|
|
15
|
+
let data = '';
|
|
16
|
+
|
|
17
|
+
const isAssetAddress = isAddress(asset);
|
|
18
|
+
|
|
19
|
+
const network: NetworkNumber = chainId;
|
|
20
|
+
const isOptimism = network === 10;
|
|
21
|
+
|
|
22
|
+
if (
|
|
23
|
+
!isOptimism
|
|
24
|
+
&& (asset === 'ETH' || compareAddresses(asset, '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee'))
|
|
25
|
+
) {
|
|
26
|
+
data = await web3.eth.getBalance(address);
|
|
27
|
+
} else {
|
|
28
|
+
let _asset = isAssetAddress ? asset : getAssetInfo(asset).address;
|
|
29
|
+
|
|
30
|
+
if (isOptimism && compareAddresses(_asset, '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee')) {
|
|
31
|
+
_asset = '0xDeadDeAddeAddEAddeadDEaDDEAdDeaDDeAD0000';
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const contract = await getErc20Contract(_asset, web3);
|
|
35
|
+
data = (await contract.methods.balanceOf(address).call()).toString();
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return isAssetAddress
|
|
39
|
+
// @ts-ignore
|
|
40
|
+
? getEthAmountForDecimals(data, (await getERC20TokenData(asset)).decimals)
|
|
41
|
+
: assetAmountInEth(data, asset);
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export const getAssetsBalances = async (assets: string[], address: EthAddress, networkId: NetworkNumber, web3: Web3): Promise<string[]> => {
|
|
45
|
+
const contract = BalanceScannerContract(web3, networkId);
|
|
46
|
+
|
|
47
|
+
const balancesFromChain = [...await contract.methods.tokensBalance(address, assets.map(a => getAssetInfo(a, networkId).address)).call()];
|
|
48
|
+
|
|
49
|
+
const balances: string[] = balancesFromChain.map((item, i) => (
|
|
50
|
+
item.success
|
|
51
|
+
? assetAmountInEth(web3.utils.hexToNumberString(item.data), assets[i])
|
|
52
|
+
: '0'
|
|
53
|
+
));
|
|
54
|
+
|
|
55
|
+
const ethIndex = assets.indexOf('ETH');
|
|
56
|
+
if (ethIndex !== -1) {
|
|
57
|
+
balances[ethIndex] = await getAssetBalance('ETH', address, networkId, web3);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return balances;
|
|
61
61
|
};
|
|
@@ -1,123 +1,123 @@
|
|
|
1
|
-
import Web3 from 'web3';
|
|
2
|
-
import Dec from 'decimal.js';
|
|
3
|
-
|
|
4
|
-
import { assetAmountInEth, getAssetInfo } from '@defisaver/tokens';
|
|
5
|
-
import {
|
|
6
|
-
Blockish, NetworkNumber, PositionBalances,
|
|
7
|
-
} from '../types/common';
|
|
8
|
-
|
|
9
|
-
import { ChickenBondsManagerContract, ChickenBondsViewContract } from '../contracts';
|
|
10
|
-
import { BondInfoBasic, ChickenBondsSystemInfoBasic } from '../types';
|
|
11
|
-
import { multicall } from '../multicall';
|
|
12
|
-
import { calcAverageBondAgeMs, calcCBondsBLUSDFloorPrice, decodeTokenURIToSvg } from '../helpers/chickenBondsHelpers';
|
|
13
|
-
|
|
14
|
-
export const getChickenBondsAccountBalances = async (web3: Web3, network: NetworkNumber, block: Blockish, addressMapping: boolean, bondId: string): Promise<PositionBalances> => {
|
|
15
|
-
const viewContract = ChickenBondsViewContract(web3, network, block);
|
|
16
|
-
|
|
17
|
-
const fullBondInfo = await viewContract.methods.getBondFullInfo(bondId).call({}, block);
|
|
18
|
-
|
|
19
|
-
return {
|
|
20
|
-
deposited: {
|
|
21
|
-
[addressMapping ? getAssetInfo('LUSD', network).address.toLowerCase() : 'LUSD']: fullBondInfo.lusdAmount,
|
|
22
|
-
},
|
|
23
|
-
};
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
export const fetchCBondsSystemInfo = async (web3: Web3, network: NetworkNumber): Promise<ChickenBondsSystemInfoBasic> => {
|
|
27
|
-
const cBondsView = ChickenBondsViewContract(web3, network);
|
|
28
|
-
const cBondsManager = ChickenBondsManagerContract(web3, network);
|
|
29
|
-
const multicallData = [
|
|
30
|
-
{
|
|
31
|
-
target: cBondsView.options.address,
|
|
32
|
-
abiItem: cBondsView.options.jsonInterface.find(({ name }) => name === 'getSystemInfo'),
|
|
33
|
-
params: [],
|
|
34
|
-
},
|
|
35
|
-
{
|
|
36
|
-
target: cBondsManager.options.address,
|
|
37
|
-
abiItem: cBondsManager.options.jsonInterface.find(({ name }) => name === 'totalWeightedStartTimes'),
|
|
38
|
-
params: [],
|
|
39
|
-
},
|
|
40
|
-
{
|
|
41
|
-
target: cBondsManager.options.address,
|
|
42
|
-
abiItem: cBondsManager.options.jsonInterface.find(({ name }) => name === 'getAcquiredLUSDInSP'),
|
|
43
|
-
params: [],
|
|
44
|
-
},
|
|
45
|
-
{
|
|
46
|
-
target: cBondsManager.options.address,
|
|
47
|
-
abiItem: cBondsManager.options.jsonInterface.find(({ name }) => name === 'getAcquiredLUSDInCurve'),
|
|
48
|
-
params: [],
|
|
49
|
-
},
|
|
50
|
-
{
|
|
51
|
-
target: cBondsManager.options.address,
|
|
52
|
-
abiItem: cBondsManager.options.jsonInterface.find(({ name }) => name === 'yTokensHeldByCBM'),
|
|
53
|
-
params: [],
|
|
54
|
-
},
|
|
55
|
-
];
|
|
56
|
-
const [{ 0: systemInfo }, { 0: totalWeightedStartTimes }, { 0: acquiredLUSDInSP }, { 0: acquiredLUSDInCurve }, { 0: yTokensHeldByCBM }] = await multicall(multicallData, web3, network);
|
|
57
|
-
const payload = {
|
|
58
|
-
numPendingBonds: systemInfo.numPendingBonds,
|
|
59
|
-
numChickenInBonds: systemInfo.numChickenInBonds,
|
|
60
|
-
numChickenOutBonds: systemInfo.numChickenOutBonds,
|
|
61
|
-
accrualParameter: new Dec(assetAmountInEth(systemInfo.accrualParameter)).mul(1000).toString(),
|
|
62
|
-
bLUSDSupply: assetAmountInEth(systemInfo.bLUSDSupply, 'bLUSD'),
|
|
63
|
-
chickenInAMMFee: assetAmountInEth(systemInfo.chickenInAMMFee),
|
|
64
|
-
ownedLUSDInCurve: assetAmountInEth(systemInfo.ownedLUSDInCurve, 'LUSD'),
|
|
65
|
-
systemBackingRatio: assetAmountInEth(systemInfo.systemBackingRatio),
|
|
66
|
-
ownedLUSDInSP: assetAmountInEth(systemInfo.ownedLUSDInSP, 'LUSD'),
|
|
67
|
-
totalPendingLUSD: assetAmountInEth(systemInfo.totalPendingLUSD, 'LUSD'),
|
|
68
|
-
totalPermanentLUSD: assetAmountInEth(systemInfo.totalPermanentLUSD, 'LUSD'),
|
|
69
|
-
totalReserveLUSD: assetAmountInEth(systemInfo.totalReserveLUSD, 'LUSD'),
|
|
70
|
-
targetAverageAgeMs: 1296000000,
|
|
71
|
-
totalWeightedStartTimes: assetAmountInEth(totalWeightedStartTimes),
|
|
72
|
-
acquiredLUSDInSP: assetAmountInEth(acquiredLUSDInSP, 'LUSD'),
|
|
73
|
-
acquiredLUSDInCurve: assetAmountInEth(acquiredLUSDInCurve, 'LUSD'),
|
|
74
|
-
yTokensHeldByCBM: assetAmountInEth(yTokensHeldByCBM, 'ETH'), // yTokens is 18 decimals
|
|
75
|
-
};
|
|
76
|
-
|
|
77
|
-
const floorPrice = calcCBondsBLUSDFloorPrice(payload.bLUSDSupply, payload.totalReserveLUSD);
|
|
78
|
-
const averageBondAgeMs = calcAverageBondAgeMs(payload.totalWeightedStartTimes, payload.totalPendingLUSD);
|
|
79
|
-
|
|
80
|
-
return {
|
|
81
|
-
...payload,
|
|
82
|
-
floorPrice,
|
|
83
|
-
averageBondAgeMs,
|
|
84
|
-
};
|
|
85
|
-
};
|
|
86
|
-
|
|
87
|
-
export const fetchCBondsForUser = async (web3: Web3, network: NetworkNumber, address: string): Promise<BondInfoBasic[]> => {
|
|
88
|
-
const cBondsView = ChickenBondsViewContract(web3, network);
|
|
89
|
-
|
|
90
|
-
const bonds = await cBondsView.methods.getUsersBonds(address).call();
|
|
91
|
-
|
|
92
|
-
return bonds.map(({
|
|
93
|
-
bondID, accruedBLUSD, claimedBLUSD, endTime, lusdAmount, maxAmountBLUSD, startTime, status, tokenURI,
|
|
94
|
-
}) => ({
|
|
95
|
-
bondId: bondID,
|
|
96
|
-
status,
|
|
97
|
-
tokenURI: decodeTokenURIToSvg(tokenURI),
|
|
98
|
-
startTime: new Date(+startTime * 1000),
|
|
99
|
-
endTime: new Date(+endTime * 1000),
|
|
100
|
-
accruedBLUSD: assetAmountInEth(accruedBLUSD, 'bLUSD'),
|
|
101
|
-
claimedBLUSD: assetAmountInEth(claimedBLUSD, 'bLUSD'),
|
|
102
|
-
lusdAmount: assetAmountInEth(lusdAmount, 'LUSD'),
|
|
103
|
-
maxAmountBLUSD: assetAmountInEth(maxAmountBLUSD, 'bLUSD'),
|
|
104
|
-
}));
|
|
105
|
-
};
|
|
106
|
-
|
|
107
|
-
export const fetchCBondForId = async (web3: Web3, network: NetworkNumber, bondId: string): Promise<BondInfoBasic> => {
|
|
108
|
-
const cBondsView = ChickenBondsViewContract(web3, network);
|
|
109
|
-
|
|
110
|
-
const bond = await cBondsView.methods.getBondFullInfo(bondId).call();
|
|
111
|
-
|
|
112
|
-
return {
|
|
113
|
-
bondId,
|
|
114
|
-
status: bond.status,
|
|
115
|
-
startTime: new Date(+bond.startTime * 1000),
|
|
116
|
-
endTime: new Date(+bond.endTime * 1000),
|
|
117
|
-
accruedBLUSD: assetAmountInEth(bond.accruedBLUSD, 'bLUSD'),
|
|
118
|
-
claimedBLUSD: assetAmountInEth(bond.claimedBLUSD, 'bLUSD'),
|
|
119
|
-
lusdAmount: assetAmountInEth(bond.lusdAmount, 'LUSD'),
|
|
120
|
-
maxAmountBLUSD: assetAmountInEth(bond.maxAmountBLUSD, 'bLUSD'),
|
|
121
|
-
tokenURI: decodeTokenURIToSvg(bond.tokenURI),
|
|
122
|
-
};
|
|
123
|
-
};
|
|
1
|
+
import Web3 from 'web3';
|
|
2
|
+
import Dec from 'decimal.js';
|
|
3
|
+
|
|
4
|
+
import { assetAmountInEth, getAssetInfo } from '@defisaver/tokens';
|
|
5
|
+
import {
|
|
6
|
+
Blockish, NetworkNumber, PositionBalances,
|
|
7
|
+
} from '../types/common';
|
|
8
|
+
|
|
9
|
+
import { ChickenBondsManagerContract, ChickenBondsViewContract } from '../contracts';
|
|
10
|
+
import { BondInfoBasic, ChickenBondsSystemInfoBasic } from '../types';
|
|
11
|
+
import { multicall } from '../multicall';
|
|
12
|
+
import { calcAverageBondAgeMs, calcCBondsBLUSDFloorPrice, decodeTokenURIToSvg } from '../helpers/chickenBondsHelpers';
|
|
13
|
+
|
|
14
|
+
export const getChickenBondsAccountBalances = async (web3: Web3, network: NetworkNumber, block: Blockish, addressMapping: boolean, bondId: string): Promise<PositionBalances> => {
|
|
15
|
+
const viewContract = ChickenBondsViewContract(web3, network, block);
|
|
16
|
+
|
|
17
|
+
const fullBondInfo = await viewContract.methods.getBondFullInfo(bondId).call({}, block);
|
|
18
|
+
|
|
19
|
+
return {
|
|
20
|
+
deposited: {
|
|
21
|
+
[addressMapping ? getAssetInfo('LUSD', network).address.toLowerCase() : 'LUSD']: fullBondInfo.lusdAmount,
|
|
22
|
+
},
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export const fetchCBondsSystemInfo = async (web3: Web3, network: NetworkNumber): Promise<ChickenBondsSystemInfoBasic> => {
|
|
27
|
+
const cBondsView = ChickenBondsViewContract(web3, network);
|
|
28
|
+
const cBondsManager = ChickenBondsManagerContract(web3, network);
|
|
29
|
+
const multicallData = [
|
|
30
|
+
{
|
|
31
|
+
target: cBondsView.options.address,
|
|
32
|
+
abiItem: cBondsView.options.jsonInterface.find(({ name }) => name === 'getSystemInfo'),
|
|
33
|
+
params: [],
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
target: cBondsManager.options.address,
|
|
37
|
+
abiItem: cBondsManager.options.jsonInterface.find(({ name }) => name === 'totalWeightedStartTimes'),
|
|
38
|
+
params: [],
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
target: cBondsManager.options.address,
|
|
42
|
+
abiItem: cBondsManager.options.jsonInterface.find(({ name }) => name === 'getAcquiredLUSDInSP'),
|
|
43
|
+
params: [],
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
target: cBondsManager.options.address,
|
|
47
|
+
abiItem: cBondsManager.options.jsonInterface.find(({ name }) => name === 'getAcquiredLUSDInCurve'),
|
|
48
|
+
params: [],
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
target: cBondsManager.options.address,
|
|
52
|
+
abiItem: cBondsManager.options.jsonInterface.find(({ name }) => name === 'yTokensHeldByCBM'),
|
|
53
|
+
params: [],
|
|
54
|
+
},
|
|
55
|
+
];
|
|
56
|
+
const [{ 0: systemInfo }, { 0: totalWeightedStartTimes }, { 0: acquiredLUSDInSP }, { 0: acquiredLUSDInCurve }, { 0: yTokensHeldByCBM }] = await multicall(multicallData, web3, network);
|
|
57
|
+
const payload = {
|
|
58
|
+
numPendingBonds: systemInfo.numPendingBonds,
|
|
59
|
+
numChickenInBonds: systemInfo.numChickenInBonds,
|
|
60
|
+
numChickenOutBonds: systemInfo.numChickenOutBonds,
|
|
61
|
+
accrualParameter: new Dec(assetAmountInEth(systemInfo.accrualParameter)).mul(1000).toString(),
|
|
62
|
+
bLUSDSupply: assetAmountInEth(systemInfo.bLUSDSupply, 'bLUSD'),
|
|
63
|
+
chickenInAMMFee: assetAmountInEth(systemInfo.chickenInAMMFee),
|
|
64
|
+
ownedLUSDInCurve: assetAmountInEth(systemInfo.ownedLUSDInCurve, 'LUSD'),
|
|
65
|
+
systemBackingRatio: assetAmountInEth(systemInfo.systemBackingRatio),
|
|
66
|
+
ownedLUSDInSP: assetAmountInEth(systemInfo.ownedLUSDInSP, 'LUSD'),
|
|
67
|
+
totalPendingLUSD: assetAmountInEth(systemInfo.totalPendingLUSD, 'LUSD'),
|
|
68
|
+
totalPermanentLUSD: assetAmountInEth(systemInfo.totalPermanentLUSD, 'LUSD'),
|
|
69
|
+
totalReserveLUSD: assetAmountInEth(systemInfo.totalReserveLUSD, 'LUSD'),
|
|
70
|
+
targetAverageAgeMs: 1296000000,
|
|
71
|
+
totalWeightedStartTimes: assetAmountInEth(totalWeightedStartTimes),
|
|
72
|
+
acquiredLUSDInSP: assetAmountInEth(acquiredLUSDInSP, 'LUSD'),
|
|
73
|
+
acquiredLUSDInCurve: assetAmountInEth(acquiredLUSDInCurve, 'LUSD'),
|
|
74
|
+
yTokensHeldByCBM: assetAmountInEth(yTokensHeldByCBM, 'ETH'), // yTokens is 18 decimals
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
const floorPrice = calcCBondsBLUSDFloorPrice(payload.bLUSDSupply, payload.totalReserveLUSD);
|
|
78
|
+
const averageBondAgeMs = calcAverageBondAgeMs(payload.totalWeightedStartTimes, payload.totalPendingLUSD);
|
|
79
|
+
|
|
80
|
+
return {
|
|
81
|
+
...payload,
|
|
82
|
+
floorPrice,
|
|
83
|
+
averageBondAgeMs,
|
|
84
|
+
};
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
export const fetchCBondsForUser = async (web3: Web3, network: NetworkNumber, address: string): Promise<BondInfoBasic[]> => {
|
|
88
|
+
const cBondsView = ChickenBondsViewContract(web3, network);
|
|
89
|
+
|
|
90
|
+
const bonds = await cBondsView.methods.getUsersBonds(address).call();
|
|
91
|
+
|
|
92
|
+
return bonds.map(({
|
|
93
|
+
bondID, accruedBLUSD, claimedBLUSD, endTime, lusdAmount, maxAmountBLUSD, startTime, status, tokenURI,
|
|
94
|
+
}) => ({
|
|
95
|
+
bondId: bondID,
|
|
96
|
+
status,
|
|
97
|
+
tokenURI: decodeTokenURIToSvg(tokenURI),
|
|
98
|
+
startTime: new Date(+startTime * 1000),
|
|
99
|
+
endTime: new Date(+endTime * 1000),
|
|
100
|
+
accruedBLUSD: assetAmountInEth(accruedBLUSD, 'bLUSD'),
|
|
101
|
+
claimedBLUSD: assetAmountInEth(claimedBLUSD, 'bLUSD'),
|
|
102
|
+
lusdAmount: assetAmountInEth(lusdAmount, 'LUSD'),
|
|
103
|
+
maxAmountBLUSD: assetAmountInEth(maxAmountBLUSD, 'bLUSD'),
|
|
104
|
+
}));
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
export const fetchCBondForId = async (web3: Web3, network: NetworkNumber, bondId: string): Promise<BondInfoBasic> => {
|
|
108
|
+
const cBondsView = ChickenBondsViewContract(web3, network);
|
|
109
|
+
|
|
110
|
+
const bond = await cBondsView.methods.getBondFullInfo(bondId).call();
|
|
111
|
+
|
|
112
|
+
return {
|
|
113
|
+
bondId,
|
|
114
|
+
status: bond.status,
|
|
115
|
+
startTime: new Date(+bond.startTime * 1000),
|
|
116
|
+
endTime: new Date(+bond.endTime * 1000),
|
|
117
|
+
accruedBLUSD: assetAmountInEth(bond.accruedBLUSD, 'bLUSD'),
|
|
118
|
+
claimedBLUSD: assetAmountInEth(bond.claimedBLUSD, 'bLUSD'),
|
|
119
|
+
lusdAmount: assetAmountInEth(bond.lusdAmount, 'LUSD'),
|
|
120
|
+
maxAmountBLUSD: assetAmountInEth(bond.maxAmountBLUSD, 'bLUSD'),
|
|
121
|
+
tokenURI: decodeTokenURIToSvg(bond.tokenURI),
|
|
122
|
+
};
|
|
123
|
+
};
|