@defisaver/positions-sdk 0.0.29 → 0.0.30-dev
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/README.md +63 -63
- package/cjs/config/contracts.d.ts +908 -41
- package/cjs/config/contracts.js +172 -45
- package/cjs/contracts.js +4 -10
- package/cjs/curveUsd/index.js +1 -1
- package/cjs/helpers/curveUsdHelpers/index.d.ts +2 -1
- package/cjs/helpers/curveUsdHelpers/index.js +25 -17
- package/cjs/markets/aave/marketAssets.d.ts +9 -5
- package/cjs/markets/aave/marketAssets.js +16 -23
- package/cjs/markets/compound/marketsAssets.d.ts +11 -5
- package/cjs/markets/compound/marketsAssets.js +17 -19
- package/cjs/markets/spark/marketAssets.d.ts +3 -2
- package/cjs/markets/spark/marketAssets.js +5 -5
- package/cjs/moneymarket/moneymarketCommonService.js +1 -1
- package/cjs/services/utils.d.ts +4 -0
- package/cjs/services/utils.js +13 -1
- package/cjs/types/contracts/generated/CrvUSDView.d.ts +30 -4
- package/cjs/types/contracts/generated/MorphoAaveV3ProxyEthMarket.d.ts +1 -0
- package/cjs/types/curveUsd.d.ts +6 -0
- package/esm/config/contracts.d.ts +908 -41
- package/esm/config/contracts.js +172 -45
- package/esm/contracts.js +4 -10
- package/esm/curveUsd/index.js +1 -1
- package/esm/helpers/curveUsdHelpers/index.d.ts +2 -1
- package/esm/helpers/curveUsdHelpers/index.js +26 -18
- package/esm/markets/aave/marketAssets.d.ts +9 -5
- package/esm/markets/aave/marketAssets.js +15 -22
- package/esm/markets/compound/marketsAssets.d.ts +11 -5
- package/esm/markets/compound/marketsAssets.js +16 -18
- package/esm/markets/spark/marketAssets.d.ts +3 -2
- package/esm/markets/spark/marketAssets.js +4 -4
- package/esm/moneymarket/moneymarketCommonService.js +1 -1
- package/esm/services/utils.d.ts +4 -0
- package/esm/services/utils.js +11 -0
- package/esm/types/contracts/generated/CrvUSDView.d.ts +30 -4
- package/esm/types/contracts/generated/MorphoAaveV3ProxyEthMarket.d.ts +1 -0
- package/esm/types/curveUsd.d.ts +6 -0
- package/package.json +40 -40
- package/src/aaveV2/index.ts +226 -226
- package/src/aaveV3/index.ts +561 -561
- package/src/assets/index.ts +60 -60
- package/src/chickenBonds/index.ts +123 -123
- package/src/compoundV2/index.ts +219 -219
- package/src/compoundV3/index.ts +275 -275
- package/src/config/contracts.js +803 -676
- package/src/constants/index.ts +3 -3
- package/src/contracts.ts +120 -126
- package/src/curveUsd/index.ts +228 -228
- package/src/exchange/index.ts +17 -17
- package/src/helpers/aaveHelpers/index.ts +134 -134
- package/src/helpers/chickenBondsHelpers/index.ts +23 -23
- package/src/helpers/compoundHelpers/index.ts +181 -181
- package/src/helpers/curveUsdHelpers/index.ts +40 -32
- package/src/helpers/index.ts +5 -5
- package/src/helpers/makerHelpers/index.ts +94 -94
- package/src/helpers/sparkHelpers/index.ts +106 -106
- package/src/index.ts +40 -40
- package/src/liquity/index.ts +116 -116
- package/src/maker/index.ts +101 -101
- package/src/markets/aave/index.ts +80 -80
- package/src/markets/aave/marketAssets.ts +24 -32
- package/src/markets/compound/index.ts +141 -141
- package/src/markets/compound/marketsAssets.ts +48 -46
- package/src/markets/curveUsd/index.ts +69 -69
- package/src/markets/index.ts +3 -3
- package/src/markets/spark/index.ts +29 -29
- package/src/markets/spark/marketAssets.ts +10 -9
- package/src/moneymarket/moneymarketCommonService.ts +75 -75
- package/src/morpho/markets.ts +39 -39
- package/src/morphoAaveV2/index.ts +255 -255
- package/src/morphoAaveV3/index.ts +619 -619
- package/src/multicall/index.ts +22 -22
- package/src/services/dsrService.ts +15 -15
- package/src/services/priceService.ts +21 -21
- package/src/services/utils.ts +48 -35
- package/src/spark/index.ts +422 -422
- package/src/staking/staking.ts +167 -167
- package/src/types/aave.ts +256 -256
- package/src/types/chickenBonds.ts +45 -45
- package/src/types/common.ts +83 -83
- package/src/types/compound.ts +128 -128
- package/src/types/contracts/generated/CrvUSDView.ts +43 -8
- package/src/types/contracts/generated/MorphoAaveV3ProxyEthMarket.ts +2 -0
- package/src/types/curveUsd.ts +118 -112
- package/src/types/index.ts +6 -6
- package/src/types/liquity.ts +30 -30
- package/src/types/maker.ts +50 -50
- package/src/types/spark.ts +106 -106
- package/yarn-error.log +64 -0
package/src/liquity/index.ts
CHANGED
|
@@ -1,117 +1,117 @@
|
|
|
1
|
-
import Web3 from 'web3';
|
|
2
|
-
import Dec from 'decimal.js';
|
|
3
|
-
import { assetAmountInEth, getAssetInfo } from '@defisaver/tokens';
|
|
4
|
-
import {
|
|
5
|
-
Blockish, EthAddress, NetworkNumber, PositionBalances,
|
|
6
|
-
} from '../types/common';
|
|
7
|
-
import {
|
|
8
|
-
LiquityActivePoolContract, LiquityCollSurplusPoolContract, LiquityPriceFeedContract, LiquityTroveManagerContract, LiquityViewContract,
|
|
9
|
-
} from '../contracts';
|
|
10
|
-
import { multicall } from '../multicall';
|
|
11
|
-
import { LIQUITY_TROVE_STATUS_ENUM, LiquityTroveInfo } from '../types';
|
|
12
|
-
import { ZERO_ADDRESS } from '../constants';
|
|
13
|
-
|
|
14
|
-
export const LIQUITY_NORMAL_MODE_RATIO = 110; // MCR
|
|
15
|
-
export const LIQUITY_RECOVERY_MODE_RATIO = 150; // CCR
|
|
16
|
-
|
|
17
|
-
export const getLiquityAccountBalances = async (web3: Web3, network: NetworkNumber, block: Blockish, addressMapping: boolean, address: EthAddress): Promise<PositionBalances> => {
|
|
18
|
-
let balances: PositionBalances = {
|
|
19
|
-
collateral: {},
|
|
20
|
-
debt: {},
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
if (!address) {
|
|
24
|
-
return balances;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
const viewContract = LiquityViewContract(web3, network, block);
|
|
28
|
-
const troveInfo = await viewContract.methods.getTroveInfo(address).call({}, block);
|
|
29
|
-
|
|
30
|
-
balances = {
|
|
31
|
-
collateral: {
|
|
32
|
-
[addressMapping ? getAssetInfo('ETH', network).address.toLowerCase() : 'ETH']: troveInfo[1],
|
|
33
|
-
},
|
|
34
|
-
debt: {
|
|
35
|
-
[addressMapping ? getAssetInfo('LUSD', network).address.toLowerCase() : 'LUSD']: troveInfo[2],
|
|
36
|
-
},
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
return balances;
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
const _getDebtInFront = async (viewContract: any, address: string, accumulatedSum = '0', iterations = 2000) => viewContract.methods.getDebtInFront(address, accumulatedSum, iterations).call();
|
|
43
|
-
|
|
44
|
-
export const getDebtInFront = async (viewContract: any, address: string, accumulatedSum = '0', iterations = 2000): Promise<string> => {
|
|
45
|
-
const { debt, next } = await _getDebtInFront(viewContract, address, accumulatedSum, iterations);
|
|
46
|
-
if (next === ZERO_ADDRESS) return assetAmountInEth(debt, 'LUSD');
|
|
47
|
-
return getDebtInFront(viewContract, next, debt, iterations);
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
export const getLiquityTroveInfo = async (web3: Web3, network: NetworkNumber, address: string): Promise<LiquityTroveInfo> => {
|
|
51
|
-
const viewContract = LiquityViewContract(web3, network);
|
|
52
|
-
const collSurplusPoolContract = LiquityCollSurplusPoolContract(web3, network);
|
|
53
|
-
const troveManagerContract = LiquityTroveManagerContract(web3, network);
|
|
54
|
-
const priceFeedContract = LiquityPriceFeedContract(web3, network);
|
|
55
|
-
const activePoolContract = LiquityActivePoolContract(web3, network);
|
|
56
|
-
|
|
57
|
-
const multicallData = [
|
|
58
|
-
{
|
|
59
|
-
target: viewContract.options.address,
|
|
60
|
-
abiItem: viewContract.options.jsonInterface.find(({ name }) => name === 'getTroveInfo'),
|
|
61
|
-
params: [address],
|
|
62
|
-
},
|
|
63
|
-
{
|
|
64
|
-
target: collSurplusPoolContract.options.address,
|
|
65
|
-
abiItem: collSurplusPoolContract.options.jsonInterface.find(({ name }) => name === 'getCollateral'),
|
|
66
|
-
params: [address],
|
|
67
|
-
},
|
|
68
|
-
{
|
|
69
|
-
target: troveManagerContract.options.address,
|
|
70
|
-
abiItem: troveManagerContract.options.jsonInterface.find(({ name }) => name === 'getBorrowingRateWithDecay'),
|
|
71
|
-
params: [],
|
|
72
|
-
},
|
|
73
|
-
{
|
|
74
|
-
target: priceFeedContract.options.address,
|
|
75
|
-
abiItem: priceFeedContract.options.jsonInterface.find(({ name }) => name === 'fetchPrice'),
|
|
76
|
-
params: [],
|
|
77
|
-
},
|
|
78
|
-
{
|
|
79
|
-
target: activePoolContract.options.address,
|
|
80
|
-
abiItem: activePoolContract.options.jsonInterface.find(({ name }) => name === 'getETH'),
|
|
81
|
-
params: [],
|
|
82
|
-
},
|
|
83
|
-
{
|
|
84
|
-
target: activePoolContract.options.address,
|
|
85
|
-
abiItem: activePoolContract.options.jsonInterface.find(({ name }) => name === 'getLUSDDebt'),
|
|
86
|
-
params: [],
|
|
87
|
-
},
|
|
88
|
-
];
|
|
89
|
-
|
|
90
|
-
const [multiRes, debtInFront] = await Promise.all([
|
|
91
|
-
multicall(multicallData, web3, network),
|
|
92
|
-
getDebtInFront(viewContract, address),
|
|
93
|
-
]);
|
|
94
|
-
|
|
95
|
-
const recoveryMode = multiRes[0][6];
|
|
96
|
-
const totalETH = multiRes[4][0];
|
|
97
|
-
const totalLUSD = multiRes[5][0];
|
|
98
|
-
|
|
99
|
-
const payload = {
|
|
100
|
-
troveStatus: LIQUITY_TROVE_STATUS_ENUM[+multiRes[0][0].toString()],
|
|
101
|
-
collateral: assetAmountInEth(multiRes[0][1]),
|
|
102
|
-
debtInAsset: assetAmountInEth(multiRes[0][2]),
|
|
103
|
-
TCRatio: assetAmountInEth(multiRes[0][4]),
|
|
104
|
-
recoveryMode,
|
|
105
|
-
claimableCollateral: assetAmountInEth(multiRes[1][0]),
|
|
106
|
-
borrowingRateWithDecay: assetAmountInEth(multiRes[2][0]),
|
|
107
|
-
assetPrice: assetAmountInEth(multiRes[3][0]),
|
|
108
|
-
totalETH,
|
|
109
|
-
totalLUSD,
|
|
110
|
-
debtInFront,
|
|
111
|
-
minCollateralRatio: recoveryMode ? LIQUITY_RECOVERY_MODE_RATIO : LIQUITY_NORMAL_MODE_RATIO,
|
|
112
|
-
priceForRecovery: new Dec(recoveryMode ? LIQUITY_RECOVERY_MODE_RATIO : LIQUITY_NORMAL_MODE_RATIO).mul(totalLUSD).div(totalETH).div(100)
|
|
113
|
-
.toString(),
|
|
114
|
-
};
|
|
115
|
-
|
|
116
|
-
return payload;
|
|
1
|
+
import Web3 from 'web3';
|
|
2
|
+
import Dec from 'decimal.js';
|
|
3
|
+
import { assetAmountInEth, getAssetInfo } from '@defisaver/tokens';
|
|
4
|
+
import {
|
|
5
|
+
Blockish, EthAddress, NetworkNumber, PositionBalances,
|
|
6
|
+
} from '../types/common';
|
|
7
|
+
import {
|
|
8
|
+
LiquityActivePoolContract, LiquityCollSurplusPoolContract, LiquityPriceFeedContract, LiquityTroveManagerContract, LiquityViewContract,
|
|
9
|
+
} from '../contracts';
|
|
10
|
+
import { multicall } from '../multicall';
|
|
11
|
+
import { LIQUITY_TROVE_STATUS_ENUM, LiquityTroveInfo } from '../types';
|
|
12
|
+
import { ZERO_ADDRESS } from '../constants';
|
|
13
|
+
|
|
14
|
+
export const LIQUITY_NORMAL_MODE_RATIO = 110; // MCR
|
|
15
|
+
export const LIQUITY_RECOVERY_MODE_RATIO = 150; // CCR
|
|
16
|
+
|
|
17
|
+
export const getLiquityAccountBalances = async (web3: Web3, network: NetworkNumber, block: Blockish, addressMapping: boolean, address: EthAddress): Promise<PositionBalances> => {
|
|
18
|
+
let balances: PositionBalances = {
|
|
19
|
+
collateral: {},
|
|
20
|
+
debt: {},
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
if (!address) {
|
|
24
|
+
return balances;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const viewContract = LiquityViewContract(web3, network, block);
|
|
28
|
+
const troveInfo = await viewContract.methods.getTroveInfo(address).call({}, block);
|
|
29
|
+
|
|
30
|
+
balances = {
|
|
31
|
+
collateral: {
|
|
32
|
+
[addressMapping ? getAssetInfo('ETH', network).address.toLowerCase() : 'ETH']: troveInfo[1],
|
|
33
|
+
},
|
|
34
|
+
debt: {
|
|
35
|
+
[addressMapping ? getAssetInfo('LUSD', network).address.toLowerCase() : 'LUSD']: troveInfo[2],
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
return balances;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
const _getDebtInFront = async (viewContract: any, address: string, accumulatedSum = '0', iterations = 2000) => viewContract.methods.getDebtInFront(address, accumulatedSum, iterations).call();
|
|
43
|
+
|
|
44
|
+
export const getDebtInFront = async (viewContract: any, address: string, accumulatedSum = '0', iterations = 2000): Promise<string> => {
|
|
45
|
+
const { debt, next } = await _getDebtInFront(viewContract, address, accumulatedSum, iterations);
|
|
46
|
+
if (next === ZERO_ADDRESS) return assetAmountInEth(debt, 'LUSD');
|
|
47
|
+
return getDebtInFront(viewContract, next, debt, iterations);
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
export const getLiquityTroveInfo = async (web3: Web3, network: NetworkNumber, address: string): Promise<LiquityTroveInfo> => {
|
|
51
|
+
const viewContract = LiquityViewContract(web3, network);
|
|
52
|
+
const collSurplusPoolContract = LiquityCollSurplusPoolContract(web3, network);
|
|
53
|
+
const troveManagerContract = LiquityTroveManagerContract(web3, network);
|
|
54
|
+
const priceFeedContract = LiquityPriceFeedContract(web3, network);
|
|
55
|
+
const activePoolContract = LiquityActivePoolContract(web3, network);
|
|
56
|
+
|
|
57
|
+
const multicallData = [
|
|
58
|
+
{
|
|
59
|
+
target: viewContract.options.address,
|
|
60
|
+
abiItem: viewContract.options.jsonInterface.find(({ name }) => name === 'getTroveInfo'),
|
|
61
|
+
params: [address],
|
|
62
|
+
},
|
|
63
|
+
{
|
|
64
|
+
target: collSurplusPoolContract.options.address,
|
|
65
|
+
abiItem: collSurplusPoolContract.options.jsonInterface.find(({ name }) => name === 'getCollateral'),
|
|
66
|
+
params: [address],
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
target: troveManagerContract.options.address,
|
|
70
|
+
abiItem: troveManagerContract.options.jsonInterface.find(({ name }) => name === 'getBorrowingRateWithDecay'),
|
|
71
|
+
params: [],
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
target: priceFeedContract.options.address,
|
|
75
|
+
abiItem: priceFeedContract.options.jsonInterface.find(({ name }) => name === 'fetchPrice'),
|
|
76
|
+
params: [],
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
target: activePoolContract.options.address,
|
|
80
|
+
abiItem: activePoolContract.options.jsonInterface.find(({ name }) => name === 'getETH'),
|
|
81
|
+
params: [],
|
|
82
|
+
},
|
|
83
|
+
{
|
|
84
|
+
target: activePoolContract.options.address,
|
|
85
|
+
abiItem: activePoolContract.options.jsonInterface.find(({ name }) => name === 'getLUSDDebt'),
|
|
86
|
+
params: [],
|
|
87
|
+
},
|
|
88
|
+
];
|
|
89
|
+
|
|
90
|
+
const [multiRes, debtInFront] = await Promise.all([
|
|
91
|
+
multicall(multicallData, web3, network),
|
|
92
|
+
getDebtInFront(viewContract, address),
|
|
93
|
+
]);
|
|
94
|
+
|
|
95
|
+
const recoveryMode = multiRes[0][6];
|
|
96
|
+
const totalETH = multiRes[4][0];
|
|
97
|
+
const totalLUSD = multiRes[5][0];
|
|
98
|
+
|
|
99
|
+
const payload = {
|
|
100
|
+
troveStatus: LIQUITY_TROVE_STATUS_ENUM[+multiRes[0][0].toString()],
|
|
101
|
+
collateral: assetAmountInEth(multiRes[0][1]),
|
|
102
|
+
debtInAsset: assetAmountInEth(multiRes[0][2]),
|
|
103
|
+
TCRatio: assetAmountInEth(multiRes[0][4]),
|
|
104
|
+
recoveryMode,
|
|
105
|
+
claimableCollateral: assetAmountInEth(multiRes[1][0]),
|
|
106
|
+
borrowingRateWithDecay: assetAmountInEth(multiRes[2][0]),
|
|
107
|
+
assetPrice: assetAmountInEth(multiRes[3][0]),
|
|
108
|
+
totalETH,
|
|
109
|
+
totalLUSD,
|
|
110
|
+
debtInFront,
|
|
111
|
+
minCollateralRatio: recoveryMode ? LIQUITY_RECOVERY_MODE_RATIO : LIQUITY_NORMAL_MODE_RATIO,
|
|
112
|
+
priceForRecovery: new Dec(recoveryMode ? LIQUITY_RECOVERY_MODE_RATIO : LIQUITY_NORMAL_MODE_RATIO).mul(totalLUSD).div(totalETH).div(100)
|
|
113
|
+
.toString(),
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
return payload;
|
|
117
117
|
};
|
package/src/maker/index.ts
CHANGED
|
@@ -1,102 +1,102 @@
|
|
|
1
|
-
import Web3 from 'web3';
|
|
2
|
-
import Dec from 'decimal.js';
|
|
3
|
-
import { assetAmountInEth, getAssetInfo, ilkToAsset } from '@defisaver/tokens';
|
|
4
|
-
import { Blockish, NetworkNumber, PositionBalances } from '../types/common';
|
|
5
|
-
import { McdViewContract } from '../contracts';
|
|
6
|
-
import { makerHelpers } from '../helpers';
|
|
7
|
-
import { CdpData } from '../types';
|
|
8
|
-
import { wethToEth } from '../services/utils';
|
|
9
|
-
|
|
10
|
-
export const getMakerAccountBalances = async (web3: Web3, network: NetworkNumber, block: Blockish, addressMapping: boolean, cdpId: string): Promise<PositionBalances> => {
|
|
11
|
-
let balances: PositionBalances = {
|
|
12
|
-
collateral: {},
|
|
13
|
-
debt: {},
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
if (!cdpId) {
|
|
17
|
-
return balances;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
const viewContract = McdViewContract(web3, network, block);
|
|
21
|
-
// @ts-ignore
|
|
22
|
-
const cdpInfo = await viewContract.methods.getCdpInfo(cdpId).call({}, block);
|
|
23
|
-
const ilkInfo = await makerHelpers.getCollateralInfo(cdpInfo.ilk, web3, network, block);
|
|
24
|
-
|
|
25
|
-
const asset = wethToEth(ilkToAsset(cdpInfo.ilk));
|
|
26
|
-
|
|
27
|
-
balances = {
|
|
28
|
-
collateral: {
|
|
29
|
-
[addressMapping ? getAssetInfo(asset, network).address.toLowerCase() : asset]: asset === 'WBTC' ? new Dec(cdpInfo.collateral).div(1e10).floor().toString() : cdpInfo.collateral,
|
|
30
|
-
},
|
|
31
|
-
debt: {
|
|
32
|
-
[addressMapping ? getAssetInfo('DAI', network).address.toLowerCase() : 'DAI']: new Dec(cdpInfo.debt).times(ilkInfo.currentRate).div(1e27).floor()
|
|
33
|
-
.toString(),
|
|
34
|
-
},
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
return balances;
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
export const getMakerCdpData = async (web3: Web3, network: NetworkNumber, cdpId: string): Promise<CdpData> => {
|
|
41
|
-
const viewContract = McdViewContract(web3, network);
|
|
42
|
-
|
|
43
|
-
// @ts-ignore
|
|
44
|
-
const cdpInfo = await viewContract.methods.getCdpInfo(cdpId).call();
|
|
45
|
-
|
|
46
|
-
const [ilkInfo, coll] = await Promise.all([
|
|
47
|
-
makerHelpers.getCollateralInfo(cdpInfo.ilk, web3, network),
|
|
48
|
-
makerHelpers.getUnclaimedCollateral(web3, network, cdpInfo.urn, cdpInfo.ilk),
|
|
49
|
-
]);
|
|
50
|
-
|
|
51
|
-
const asset = ilkToAsset(cdpInfo.ilk);
|
|
52
|
-
|
|
53
|
-
const collateralUsd = new Dec(cdpInfo.collateral).mul(ilkInfo.assetPrice).floor().toString();
|
|
54
|
-
const debt = new Dec(cdpInfo.debt).times(ilkInfo.currentRate).div(1e27).floor()
|
|
55
|
-
.toString();
|
|
56
|
-
const futureDebt = new Dec(cdpInfo.debt).times(ilkInfo.futureRate).div(1e27).floor()
|
|
57
|
-
.toString(); // after drip
|
|
58
|
-
const debtDripDelta = assetAmountInEth(new Dec(futureDebt).sub(debt).toString(), 'DAI');
|
|
59
|
-
const liquidationPrice = new Dec(debt).times(ilkInfo.liqRatio).div(cdpInfo.collateral).toString();
|
|
60
|
-
|
|
61
|
-
let ratio = new Dec(cdpInfo.collateral).times(ilkInfo.assetPrice).div(debt).times(100)
|
|
62
|
-
.toString();
|
|
63
|
-
if (new Dec(debt).eq(0)) ratio = '0';
|
|
64
|
-
|
|
65
|
-
const debtTooLow = new Dec(debt).gt(0) && new Dec(assetAmountInEth(debt, 'DAI')).lt(ilkInfo.minDebt);
|
|
66
|
-
|
|
67
|
-
const par = '1';
|
|
68
|
-
return {
|
|
69
|
-
owner: cdpInfo.owner,
|
|
70
|
-
userAddress: cdpInfo.userAddr,
|
|
71
|
-
id: cdpId,
|
|
72
|
-
urn: cdpInfo.urn,
|
|
73
|
-
type: 'mcd',
|
|
74
|
-
ilk: cdpInfo.ilk,
|
|
75
|
-
ilkLabel: ilkInfo.ilkLabel,
|
|
76
|
-
asset,
|
|
77
|
-
collateral: assetAmountInEth(cdpInfo.collateral, `MCD-${asset}`),
|
|
78
|
-
collateralUsd: assetAmountInEth(collateralUsd, `MCD-${asset}`),
|
|
79
|
-
futureDebt: assetAmountInEth(futureDebt, 'DAI'),
|
|
80
|
-
debtDai: assetAmountInEth(debt, 'DAI'),
|
|
81
|
-
debtUsd: assetAmountInEth(debt, 'DAI'),
|
|
82
|
-
debtInAsset: assetAmountInEth(debt, 'DAI'),
|
|
83
|
-
debtAssetPrice: par,
|
|
84
|
-
debtAssetMarketPrice: par,
|
|
85
|
-
liquidationPrice,
|
|
86
|
-
ratio,
|
|
87
|
-
liqRatio: ilkInfo.liqRatio.toString(),
|
|
88
|
-
liqPercent: parseFloat(ilkInfo.liqPercent.toString()),
|
|
89
|
-
assetPrice: ilkInfo.assetPrice,
|
|
90
|
-
daiLabel: 'DAI',
|
|
91
|
-
debtAsset: 'DAI',
|
|
92
|
-
unclaimedCollateral: assetAmountInEth(coll, asset),
|
|
93
|
-
debtTooLow,
|
|
94
|
-
minDebt: ilkInfo.minDebt,
|
|
95
|
-
stabilityFee: ilkInfo.stabilityFee,
|
|
96
|
-
creatableDebt: ilkInfo.creatableDebt,
|
|
97
|
-
globalDebtCeiling: ilkInfo.globalDebtCeiling,
|
|
98
|
-
globalDebtCurrent: ilkInfo.globalDebtCurrent,
|
|
99
|
-
liquidationFee: ilkInfo.liquidationFee,
|
|
100
|
-
lastUpdated: Date.now(),
|
|
101
|
-
};
|
|
1
|
+
import Web3 from 'web3';
|
|
2
|
+
import Dec from 'decimal.js';
|
|
3
|
+
import { assetAmountInEth, getAssetInfo, ilkToAsset } from '@defisaver/tokens';
|
|
4
|
+
import { Blockish, NetworkNumber, PositionBalances } from '../types/common';
|
|
5
|
+
import { McdViewContract } from '../contracts';
|
|
6
|
+
import { makerHelpers } from '../helpers';
|
|
7
|
+
import { CdpData } from '../types';
|
|
8
|
+
import { wethToEth } from '../services/utils';
|
|
9
|
+
|
|
10
|
+
export const getMakerAccountBalances = async (web3: Web3, network: NetworkNumber, block: Blockish, addressMapping: boolean, cdpId: string): Promise<PositionBalances> => {
|
|
11
|
+
let balances: PositionBalances = {
|
|
12
|
+
collateral: {},
|
|
13
|
+
debt: {},
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
if (!cdpId) {
|
|
17
|
+
return balances;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const viewContract = McdViewContract(web3, network, block);
|
|
21
|
+
// @ts-ignore
|
|
22
|
+
const cdpInfo = await viewContract.methods.getCdpInfo(cdpId).call({}, block);
|
|
23
|
+
const ilkInfo = await makerHelpers.getCollateralInfo(cdpInfo.ilk, web3, network, block);
|
|
24
|
+
|
|
25
|
+
const asset = wethToEth(ilkToAsset(cdpInfo.ilk));
|
|
26
|
+
|
|
27
|
+
balances = {
|
|
28
|
+
collateral: {
|
|
29
|
+
[addressMapping ? getAssetInfo(asset, network).address.toLowerCase() : asset]: asset === 'WBTC' ? new Dec(cdpInfo.collateral).div(1e10).floor().toString() : cdpInfo.collateral,
|
|
30
|
+
},
|
|
31
|
+
debt: {
|
|
32
|
+
[addressMapping ? getAssetInfo('DAI', network).address.toLowerCase() : 'DAI']: new Dec(cdpInfo.debt).times(ilkInfo.currentRate).div(1e27).floor()
|
|
33
|
+
.toString(),
|
|
34
|
+
},
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
return balances;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
export const getMakerCdpData = async (web3: Web3, network: NetworkNumber, cdpId: string): Promise<CdpData> => {
|
|
41
|
+
const viewContract = McdViewContract(web3, network);
|
|
42
|
+
|
|
43
|
+
// @ts-ignore
|
|
44
|
+
const cdpInfo = await viewContract.methods.getCdpInfo(cdpId).call();
|
|
45
|
+
|
|
46
|
+
const [ilkInfo, coll] = await Promise.all([
|
|
47
|
+
makerHelpers.getCollateralInfo(cdpInfo.ilk, web3, network),
|
|
48
|
+
makerHelpers.getUnclaimedCollateral(web3, network, cdpInfo.urn, cdpInfo.ilk),
|
|
49
|
+
]);
|
|
50
|
+
|
|
51
|
+
const asset = ilkToAsset(cdpInfo.ilk);
|
|
52
|
+
|
|
53
|
+
const collateralUsd = new Dec(cdpInfo.collateral).mul(ilkInfo.assetPrice).floor().toString();
|
|
54
|
+
const debt = new Dec(cdpInfo.debt).times(ilkInfo.currentRate).div(1e27).floor()
|
|
55
|
+
.toString();
|
|
56
|
+
const futureDebt = new Dec(cdpInfo.debt).times(ilkInfo.futureRate).div(1e27).floor()
|
|
57
|
+
.toString(); // after drip
|
|
58
|
+
const debtDripDelta = assetAmountInEth(new Dec(futureDebt).sub(debt).toString(), 'DAI');
|
|
59
|
+
const liquidationPrice = new Dec(debt).times(ilkInfo.liqRatio).div(cdpInfo.collateral).toString();
|
|
60
|
+
|
|
61
|
+
let ratio = new Dec(cdpInfo.collateral).times(ilkInfo.assetPrice).div(debt).times(100)
|
|
62
|
+
.toString();
|
|
63
|
+
if (new Dec(debt).eq(0)) ratio = '0';
|
|
64
|
+
|
|
65
|
+
const debtTooLow = new Dec(debt).gt(0) && new Dec(assetAmountInEth(debt, 'DAI')).lt(ilkInfo.minDebt);
|
|
66
|
+
|
|
67
|
+
const par = '1';
|
|
68
|
+
return {
|
|
69
|
+
owner: cdpInfo.owner,
|
|
70
|
+
userAddress: cdpInfo.userAddr,
|
|
71
|
+
id: cdpId,
|
|
72
|
+
urn: cdpInfo.urn,
|
|
73
|
+
type: 'mcd',
|
|
74
|
+
ilk: cdpInfo.ilk,
|
|
75
|
+
ilkLabel: ilkInfo.ilkLabel,
|
|
76
|
+
asset,
|
|
77
|
+
collateral: assetAmountInEth(cdpInfo.collateral, `MCD-${asset}`),
|
|
78
|
+
collateralUsd: assetAmountInEth(collateralUsd, `MCD-${asset}`),
|
|
79
|
+
futureDebt: assetAmountInEth(futureDebt, 'DAI'),
|
|
80
|
+
debtDai: assetAmountInEth(debt, 'DAI'),
|
|
81
|
+
debtUsd: assetAmountInEth(debt, 'DAI'),
|
|
82
|
+
debtInAsset: assetAmountInEth(debt, 'DAI'),
|
|
83
|
+
debtAssetPrice: par,
|
|
84
|
+
debtAssetMarketPrice: par,
|
|
85
|
+
liquidationPrice,
|
|
86
|
+
ratio,
|
|
87
|
+
liqRatio: ilkInfo.liqRatio.toString(),
|
|
88
|
+
liqPercent: parseFloat(ilkInfo.liqPercent.toString()),
|
|
89
|
+
assetPrice: ilkInfo.assetPrice,
|
|
90
|
+
daiLabel: 'DAI',
|
|
91
|
+
debtAsset: 'DAI',
|
|
92
|
+
unclaimedCollateral: assetAmountInEth(coll, asset),
|
|
93
|
+
debtTooLow,
|
|
94
|
+
minDebt: ilkInfo.minDebt,
|
|
95
|
+
stabilityFee: ilkInfo.stabilityFee,
|
|
96
|
+
creatableDebt: ilkInfo.creatableDebt,
|
|
97
|
+
globalDebtCeiling: ilkInfo.globalDebtCeiling,
|
|
98
|
+
globalDebtCurrent: ilkInfo.globalDebtCurrent,
|
|
99
|
+
liquidationFee: ilkInfo.liquidationFee,
|
|
100
|
+
lastUpdated: Date.now(),
|
|
101
|
+
};
|
|
102
102
|
};
|
|
@@ -1,80 +1,80 @@
|
|
|
1
|
-
import { getConfigContractAddress } from '../../contracts';
|
|
2
|
-
import {
|
|
3
|
-
AaveMarketInfo, AaveVersions, MorphoAaveV2MarketInfo, MorphoAaveV3MarketInfo,
|
|
4
|
-
} from '../../types';
|
|
5
|
-
import { NetworkNumber } from '../../types/common';
|
|
6
|
-
import {
|
|
7
|
-
aaveV2AssetsDefaultMarket, aaveV3AssetsDefaultMarket, morphoAaveV2AssetDefaultMarket, morphoAaveV3AssetEthMarket,
|
|
8
|
-
} from './marketAssets';
|
|
9
|
-
|
|
10
|
-
export const AAVE_V2: AaveMarketInfo = {
|
|
11
|
-
chainIds: [1],
|
|
12
|
-
label: 'Aave v2',
|
|
13
|
-
shortLabel: 'v2',
|
|
14
|
-
value: AaveVersions.AaveV2,
|
|
15
|
-
url: 'default',
|
|
16
|
-
assets: aaveV2AssetsDefaultMarket,
|
|
17
|
-
provider: 'LendingPoolAddressesProvider',
|
|
18
|
-
providerAddress: getConfigContractAddress('LendingPoolAddressesProvider', 1), // rename
|
|
19
|
-
lendingPool: 'AaveLendingPoolV2',
|
|
20
|
-
lendingPoolAddress: getConfigContractAddress('AaveLendingPoolV2', 1),
|
|
21
|
-
protocolData: 'AaveProtocolDataProvider',
|
|
22
|
-
protocolDataAddress: getConfigContractAddress('AaveProtocolDataProvider', 1),
|
|
23
|
-
// icon: SvgAdapter(protocolIcons.aave),
|
|
24
|
-
protocolName: 'aave',
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
export const AAVE_V3 = (networkId: NetworkNumber): AaveMarketInfo => ({
|
|
28
|
-
chainIds: [NetworkNumber.Eth, NetworkNumber.Opt, NetworkNumber.Arb, NetworkNumber.Base],
|
|
29
|
-
label: 'Aave v3',
|
|
30
|
-
shortLabel: 'v3',
|
|
31
|
-
value: AaveVersions.AaveV3,
|
|
32
|
-
url: 'default',
|
|
33
|
-
assets: networkId ? aaveV3AssetsDefaultMarket[networkId] : [],
|
|
34
|
-
provider: 'AaveV3PoolAddressesProvider',
|
|
35
|
-
providerAddress: getConfigContractAddress('AaveV3PoolAddressesProvider', networkId),
|
|
36
|
-
lendingPool: 'AaveV3LendingPool',
|
|
37
|
-
lendingPoolAddress: getConfigContractAddress('AaveV3LendingPool', networkId),
|
|
38
|
-
protocolData: 'AaveV3ProtocolDataProvider',
|
|
39
|
-
protocolDataAddress: getConfigContractAddress('AaveV3ProtocolDataProvider', networkId),
|
|
40
|
-
// icon: SvgAdapter(protocolIcons.aave),
|
|
41
|
-
protocolName: 'aave',
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
export const MORPHO_AAVE_V2: MorphoAaveV2MarketInfo = {
|
|
45
|
-
chainIds: [1],
|
|
46
|
-
label: 'Morpho-Aave V2',
|
|
47
|
-
shortLabel: 'morpho-aave-v2',
|
|
48
|
-
value: AaveVersions.MorphoAaveV2,
|
|
49
|
-
url: '',
|
|
50
|
-
assets: morphoAaveV2AssetDefaultMarket,
|
|
51
|
-
providerAddress: getConfigContractAddress('LendingPoolAddressesProvider', 1),
|
|
52
|
-
lendingPoolAddress: getConfigContractAddress('MorphoAaveV2Proxy', 1),
|
|
53
|
-
// icon: SvgAdapter(protocolIcons.morpho),
|
|
54
|
-
protocolName: 'morpho',
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
export const MORPHO_AAVE_V3_ETH = (networkId: NetworkNumber = NetworkNumber.Eth): MorphoAaveV3MarketInfo => ({
|
|
58
|
-
chainIds: [1],
|
|
59
|
-
label: 'Morpho-Aave V3',
|
|
60
|
-
shortLabel: 'morpho-aave-v3',
|
|
61
|
-
subVersionLabel: 'ETH Optimizer',
|
|
62
|
-
value: AaveVersions.MorphoAaveV3Eth,
|
|
63
|
-
url: 'eth-optimizer',
|
|
64
|
-
assets: morphoAaveV3AssetEthMarket,
|
|
65
|
-
providerAddress: getConfigContractAddress('AaveV3PoolAddressesProvider', networkId), // TODO - check if used and if value is good?
|
|
66
|
-
protocolData: 'AaveV3ProtocolDataProvider',
|
|
67
|
-
protocolDataAddress: getConfigContractAddress('AaveV3ProtocolDataProvider', networkId),
|
|
68
|
-
lendingPool: 'MorphoAaveV3ProxyEthMarket',
|
|
69
|
-
lendingPoolAddress: getConfigContractAddress('MorphoAaveV3ProxyEthMarket', 1),
|
|
70
|
-
// icon: SvgAdapter(protocolIcons.morpho),
|
|
71
|
-
protocolName: 'morpho',
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
export const AaveMarkets = (networkId: NetworkNumber) => ({
|
|
76
|
-
[AaveVersions.AaveV2]: AAVE_V2,
|
|
77
|
-
[AaveVersions.AaveV3]: AAVE_V3(networkId),
|
|
78
|
-
[AaveVersions.MorphoAaveV3Eth]: MORPHO_AAVE_V3_ETH(networkId),
|
|
79
|
-
[AaveVersions.MorphoAaveV2]: MORPHO_AAVE_V2,
|
|
80
|
-
}) as const;
|
|
1
|
+
import { getConfigContractAddress } from '../../contracts';
|
|
2
|
+
import {
|
|
3
|
+
AaveMarketInfo, AaveVersions, MorphoAaveV2MarketInfo, MorphoAaveV3MarketInfo,
|
|
4
|
+
} from '../../types';
|
|
5
|
+
import { NetworkNumber } from '../../types/common';
|
|
6
|
+
import {
|
|
7
|
+
aaveV2AssetsDefaultMarket, aaveV3AssetsDefaultMarket, morphoAaveV2AssetDefaultMarket, morphoAaveV3AssetEthMarket,
|
|
8
|
+
} from './marketAssets';
|
|
9
|
+
|
|
10
|
+
export const AAVE_V2: AaveMarketInfo = {
|
|
11
|
+
chainIds: [1],
|
|
12
|
+
label: 'Aave v2',
|
|
13
|
+
shortLabel: 'v2',
|
|
14
|
+
value: AaveVersions.AaveV2,
|
|
15
|
+
url: 'default',
|
|
16
|
+
assets: aaveV2AssetsDefaultMarket,
|
|
17
|
+
provider: 'LendingPoolAddressesProvider',
|
|
18
|
+
providerAddress: getConfigContractAddress('LendingPoolAddressesProvider', 1), // rename
|
|
19
|
+
lendingPool: 'AaveLendingPoolV2',
|
|
20
|
+
lendingPoolAddress: getConfigContractAddress('AaveLendingPoolV2', 1),
|
|
21
|
+
protocolData: 'AaveProtocolDataProvider',
|
|
22
|
+
protocolDataAddress: getConfigContractAddress('AaveProtocolDataProvider', 1),
|
|
23
|
+
// icon: SvgAdapter(protocolIcons.aave),
|
|
24
|
+
protocolName: 'aave',
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export const AAVE_V3 = (networkId: NetworkNumber): AaveMarketInfo => ({
|
|
28
|
+
chainIds: [NetworkNumber.Eth, NetworkNumber.Opt, NetworkNumber.Arb, NetworkNumber.Base],
|
|
29
|
+
label: 'Aave v3',
|
|
30
|
+
shortLabel: 'v3',
|
|
31
|
+
value: AaveVersions.AaveV3,
|
|
32
|
+
url: 'default',
|
|
33
|
+
assets: networkId ? aaveV3AssetsDefaultMarket[networkId] : [],
|
|
34
|
+
provider: 'AaveV3PoolAddressesProvider',
|
|
35
|
+
providerAddress: getConfigContractAddress('AaveV3PoolAddressesProvider', networkId),
|
|
36
|
+
lendingPool: 'AaveV3LendingPool',
|
|
37
|
+
lendingPoolAddress: getConfigContractAddress('AaveV3LendingPool', networkId),
|
|
38
|
+
protocolData: 'AaveV3ProtocolDataProvider',
|
|
39
|
+
protocolDataAddress: getConfigContractAddress('AaveV3ProtocolDataProvider', networkId),
|
|
40
|
+
// icon: SvgAdapter(protocolIcons.aave),
|
|
41
|
+
protocolName: 'aave',
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
export const MORPHO_AAVE_V2: MorphoAaveV2MarketInfo = {
|
|
45
|
+
chainIds: [1],
|
|
46
|
+
label: 'Morpho-Aave V2',
|
|
47
|
+
shortLabel: 'morpho-aave-v2',
|
|
48
|
+
value: AaveVersions.MorphoAaveV2,
|
|
49
|
+
url: '',
|
|
50
|
+
assets: morphoAaveV2AssetDefaultMarket,
|
|
51
|
+
providerAddress: getConfigContractAddress('LendingPoolAddressesProvider', 1),
|
|
52
|
+
lendingPoolAddress: getConfigContractAddress('MorphoAaveV2Proxy', 1),
|
|
53
|
+
// icon: SvgAdapter(protocolIcons.morpho),
|
|
54
|
+
protocolName: 'morpho',
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
export const MORPHO_AAVE_V3_ETH = (networkId: NetworkNumber = NetworkNumber.Eth): MorphoAaveV3MarketInfo => ({
|
|
58
|
+
chainIds: [1],
|
|
59
|
+
label: 'Morpho-Aave V3',
|
|
60
|
+
shortLabel: 'morpho-aave-v3',
|
|
61
|
+
subVersionLabel: 'ETH Optimizer',
|
|
62
|
+
value: AaveVersions.MorphoAaveV3Eth,
|
|
63
|
+
url: 'eth-optimizer',
|
|
64
|
+
assets: morphoAaveV3AssetEthMarket,
|
|
65
|
+
providerAddress: getConfigContractAddress('AaveV3PoolAddressesProvider', networkId), // TODO - check if used and if value is good?
|
|
66
|
+
protocolData: 'AaveV3ProtocolDataProvider',
|
|
67
|
+
protocolDataAddress: getConfigContractAddress('AaveV3ProtocolDataProvider', networkId),
|
|
68
|
+
lendingPool: 'MorphoAaveV3ProxyEthMarket',
|
|
69
|
+
lendingPoolAddress: getConfigContractAddress('MorphoAaveV3ProxyEthMarket', 1),
|
|
70
|
+
// icon: SvgAdapter(protocolIcons.morpho),
|
|
71
|
+
protocolName: 'morpho',
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
export const AaveMarkets = (networkId: NetworkNumber) => ({
|
|
76
|
+
[AaveVersions.AaveV2]: AAVE_V2,
|
|
77
|
+
[AaveVersions.AaveV3]: AAVE_V3(networkId),
|
|
78
|
+
[AaveVersions.MorphoAaveV3Eth]: MORPHO_AAVE_V3_ETH(networkId),
|
|
79
|
+
[AaveVersions.MorphoAaveV2]: MORPHO_AAVE_V2,
|
|
80
|
+
}) as const;
|