@defisaver/positions-sdk 2.1.11 → 2.1.13
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 +64 -64
- package/cjs/aaveV3/index.js +1 -1
- package/cjs/contracts.d.ts +100947 -134653
- package/cjs/helpers/morphoBlueHelpers/index.js +66 -66
- package/cjs/markets/aave/marketAssets.js +1 -1
- package/cjs/services/viem.d.ts +2 -3254
- package/cjs/staking/staking.d.ts +1 -1
- package/cjs/staking/staking.js +17 -14
- package/esm/aaveV3/index.js +1 -1
- package/esm/contracts.d.ts +100947 -134653
- package/esm/helpers/morphoBlueHelpers/index.js +66 -66
- package/esm/markets/aave/marketAssets.js +1 -1
- package/esm/services/viem.d.ts +2 -3254
- package/esm/staking/staking.d.ts +1 -1
- package/esm/staking/staking.js +17 -14
- package/package.json +47 -47
- package/src/aaveV2/index.ts +240 -240
- package/src/aaveV3/index.ts +614 -614
- package/src/aaveV3/merit.ts +97 -97
- package/src/aaveV3/merkl.ts +74 -74
- package/src/claiming/aaveV3.ts +154 -154
- package/src/claiming/compV3.ts +22 -22
- package/src/claiming/index.ts +12 -12
- package/src/claiming/king.ts +66 -66
- package/src/claiming/morphoBlue.ts +118 -118
- package/src/claiming/spark.ts +225 -225
- package/src/compoundV2/index.ts +244 -244
- package/src/compoundV3/index.ts +274 -274
- package/src/config/contracts.ts +1251 -1251
- package/src/constants/index.ts +10 -10
- package/src/contracts.ts +120 -120
- package/src/curveUsd/index.ts +254 -254
- package/src/eulerV2/index.ts +324 -324
- package/src/exchange/index.ts +25 -25
- package/src/fluid/index.ts +1668 -1668
- package/src/helpers/aaveHelpers/index.ts +187 -187
- package/src/helpers/compoundHelpers/index.ts +283 -283
- package/src/helpers/curveUsdHelpers/index.ts +40 -40
- package/src/helpers/eulerHelpers/index.ts +222 -222
- package/src/helpers/fluidHelpers/index.ts +326 -326
- package/src/helpers/index.ts +10 -10
- package/src/helpers/liquityV2Helpers/index.ts +82 -82
- package/src/helpers/llamaLendHelpers/index.ts +53 -53
- package/src/helpers/makerHelpers/index.ts +52 -52
- package/src/helpers/morphoBlueHelpers/index.ts +396 -396
- package/src/helpers/sparkHelpers/index.ts +158 -158
- package/src/index.ts +47 -47
- package/src/liquity/index.ts +159 -159
- package/src/liquityV2/index.ts +657 -657
- package/src/llamaLend/index.ts +305 -305
- package/src/maker/index.ts +223 -223
- package/src/markets/aave/index.ts +116 -116
- package/src/markets/aave/marketAssets.ts +54 -54
- package/src/markets/compound/index.ts +238 -238
- package/src/markets/compound/marketsAssets.ts +97 -97
- package/src/markets/curveUsd/index.ts +69 -69
- package/src/markets/euler/index.ts +26 -26
- package/src/markets/fluid/index.ts +2460 -2460
- package/src/markets/index.ts +25 -25
- package/src/markets/liquityV2/index.ts +102 -102
- 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 +12 -12
- package/src/moneymarket/moneymarketCommonService.ts +80 -80
- package/src/morphoBlue/index.ts +274 -274
- package/src/portfolio/index.ts +570 -570
- package/src/services/priceService.ts +159 -159
- package/src/services/utils.ts +115 -115
- package/src/services/viem.ts +34 -34
- package/src/setup.ts +8 -8
- package/src/spark/index.ts +456 -456
- package/src/staking/eligibility.ts +53 -53
- package/src/staking/index.ts +1 -1
- package/src/staking/staking.ts +177 -172
- package/src/types/aave.ts +189 -189
- package/src/types/claiming.ts +109 -109
- package/src/types/common.ts +107 -107
- package/src/types/compound.ts +136 -136
- package/src/types/curveUsd.ts +123 -123
- package/src/types/euler.ts +175 -175
- package/src/types/fluid.ts +452 -452
- package/src/types/index.ts +13 -13
- package/src/types/liquity.ts +30 -30
- package/src/types/liquityV2.ts +126 -126
- package/src/types/llamaLend.ts +159 -159
- package/src/types/maker.ts +63 -63
- package/src/types/merit.ts +1 -1
- package/src/types/merkl.ts +70 -70
- package/src/types/morphoBlue.ts +194 -194
- package/src/types/portfolio.ts +60 -60
- package/src/types/spark.ts +133 -133
- package/src/umbrella/index.ts +69 -69
- package/src/umbrella/umbrellaUtils.ts +29 -29
package/src/maker/index.ts
CHANGED
|
@@ -1,224 +1,224 @@
|
|
|
1
|
-
import Dec from 'decimal.js';
|
|
2
|
-
import {
|
|
3
|
-
assetAmountInEth, bytesToString, getAssetInfo, ilkToAsset,
|
|
4
|
-
} from '@defisaver/tokens';
|
|
5
|
-
import { Client, PublicClient } from 'viem';
|
|
6
|
-
import {
|
|
7
|
-
Blockish, EthAddress, EthereumProvider, NetworkNumber, PositionBalances,
|
|
8
|
-
} from '../types/common';
|
|
9
|
-
import {
|
|
10
|
-
getConfigContractAddress, McdDogContractViem, McdGetCdpsContractViem, McdJugContractViem, McdSpotterContractViem, McdVatContractViem, McdViewContractViem,
|
|
11
|
-
} from '../contracts';
|
|
12
|
-
import { CdpData, CdpInfo, CdpType } from '../types';
|
|
13
|
-
import { wethToEth } from '../services/utils';
|
|
14
|
-
import { parseCollateralInfo } from '../helpers/makerHelpers';
|
|
15
|
-
import { getViemProvider, setViemBlockNumber } from '../services/viem';
|
|
16
|
-
|
|
17
|
-
export const _getMakerAccountBalances = async (provider: PublicClient, network: NetworkNumber, block: Blockish, addressMapping: boolean, cdpId: string, _managerAddress?: EthAddress): Promise<PositionBalances> => {
|
|
18
|
-
let balances: PositionBalances = {
|
|
19
|
-
collateral: {},
|
|
20
|
-
debt: {},
|
|
21
|
-
};
|
|
22
|
-
const managerAddress = _managerAddress || '0x5ef30b9986345249bc32d8928B7ee64DE9435E39'; // Default CDP Manager (This is used only to differentiate BProtocol CDPs)
|
|
23
|
-
|
|
24
|
-
if (!cdpId) {
|
|
25
|
-
return balances;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
const viewContract = McdViewContractViem(provider, network, block);
|
|
29
|
-
const vatContract = McdVatContractViem(provider, network, block);
|
|
30
|
-
const spotterContract = McdSpotterContractViem(provider, network, block);
|
|
31
|
-
const dogContract = McdDogContractViem(provider, network, block);
|
|
32
|
-
const jugContract = McdJugContractViem(provider, network, block);
|
|
33
|
-
|
|
34
|
-
let ilk;
|
|
35
|
-
|
|
36
|
-
const needsIlk = block !== 'latest' && new Dec(block).lt(14410792) && new Dec(block).gte(14384301);
|
|
37
|
-
|
|
38
|
-
if (needsIlk) {
|
|
39
|
-
ilk = (await viewContract.read.getUrnAndIlk([managerAddress, BigInt(cdpId)], setViemBlockNumber(block)))[1];
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
// @ts-ignore
|
|
43
|
-
// [urn, owner, userAddr, ilk, collateral, debt]
|
|
44
|
-
const cdpInfo: any = await viewContract.read.getCdpInfo((needsIlk ? [managerAddress, cdpId, ilk] : [cdpId]), setViemBlockNumber(block));
|
|
45
|
-
cdpInfo.ilk = cdpInfo[3];
|
|
46
|
-
|
|
47
|
-
const [
|
|
48
|
-
par,
|
|
49
|
-
[_, mat],
|
|
50
|
-
[artGlobal, rate, spot, line],
|
|
51
|
-
[duty],
|
|
52
|
-
futureRate,
|
|
53
|
-
chop,
|
|
54
|
-
] = await Promise.all([
|
|
55
|
-
spotterContract.read.par(setViemBlockNumber(block)),
|
|
56
|
-
spotterContract.read.ilks(needsIlk ? [ilk] : [cdpInfo.ilk], setViemBlockNumber(block)),
|
|
57
|
-
vatContract.read.ilks(needsIlk ? [ilk] : [cdpInfo.ilk], setViemBlockNumber(block)),
|
|
58
|
-
jugContract.read.ilks(needsIlk ? [ilk] : [cdpInfo.ilk], setViemBlockNumber(block)),
|
|
59
|
-
jugContract.read.drip(needsIlk ? [ilk] : [cdpInfo.ilk], setViemBlockNumber(block)),
|
|
60
|
-
dogContract.read.chop(needsIlk ? [ilk] : [cdpInfo.ilk], setViemBlockNumber(block)),
|
|
61
|
-
]);
|
|
62
|
-
|
|
63
|
-
const ilkInfo = parseCollateralInfo(
|
|
64
|
-
needsIlk ? ilk : cdpInfo.ilk,
|
|
65
|
-
par.toString(),
|
|
66
|
-
mat.toString(),
|
|
67
|
-
artGlobal.toString(),
|
|
68
|
-
rate.toString(),
|
|
69
|
-
spot.toString(),
|
|
70
|
-
line.toString(),
|
|
71
|
-
duty.toString(),
|
|
72
|
-
futureRate.toString(),
|
|
73
|
-
chop.toString(),
|
|
74
|
-
);
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
const collateral = cdpInfo[4];
|
|
78
|
-
const debt = cdpInfo[5];
|
|
79
|
-
|
|
80
|
-
const asset = wethToEth(ilkToAsset(needsIlk ? ilk : cdpInfo.ilk));
|
|
81
|
-
|
|
82
|
-
balances = {
|
|
83
|
-
collateral: {
|
|
84
|
-
[addressMapping ? getAssetInfo(asset, network).address.toLowerCase() : asset]: asset === 'WBTC' ? new Dec(collateral).div(1e10).floor().toString() : collateral,
|
|
85
|
-
},
|
|
86
|
-
debt: {
|
|
87
|
-
[addressMapping ? getAssetInfo('DAI', network).address.toLowerCase() : 'DAI']: new Dec(debt).times(ilkInfo.currentRate).div(1e27).floor()
|
|
88
|
-
.toString(),
|
|
89
|
-
},
|
|
90
|
-
};
|
|
91
|
-
|
|
92
|
-
return balances;
|
|
93
|
-
};
|
|
94
|
-
|
|
95
|
-
export const getMakerAccountBalances = async (
|
|
96
|
-
provider: EthereumProvider,
|
|
97
|
-
network: NetworkNumber,
|
|
98
|
-
block: Blockish,
|
|
99
|
-
addressMapping: boolean,
|
|
100
|
-
cdpId: string,
|
|
101
|
-
_managerAddress?: EthAddress,
|
|
102
|
-
): Promise<PositionBalances> => _getMakerAccountBalances(getViemProvider(provider, network, { batch: { multicall: true } }), network, block, addressMapping, cdpId, _managerAddress);
|
|
103
|
-
|
|
104
|
-
export const _getUserCdps = async (provider: Client, network: NetworkNumber, userAddress: EthAddress): Promise<CdpInfo[]> => {
|
|
105
|
-
if (!userAddress) return [];
|
|
106
|
-
const mcdGetCdpsContract = McdGetCdpsContractViem(provider, network);
|
|
107
|
-
|
|
108
|
-
const mcdCdpManagerAddress = getConfigContractAddress('McdCdpManager', network);
|
|
109
|
-
|
|
110
|
-
const standardCdps = await mcdGetCdpsContract.read.getCdpsAsc([mcdCdpManagerAddress, userAddress]);
|
|
111
|
-
|
|
112
|
-
const parsedStandardCdps = standardCdps[0].map((id, i) => ({
|
|
113
|
-
id: parseInt(id.toString(), 10),
|
|
114
|
-
ilk: standardCdps[2][i].toLowerCase() as EthAddress, // collateral type
|
|
115
|
-
ilkLabel: bytesToString(standardCdps[2][i].toLowerCase()),
|
|
116
|
-
urn: standardCdps[1][i].toLowerCase() as EthAddress, // contract of cdp
|
|
117
|
-
asset: ilkToAsset(standardCdps[2][i]),
|
|
118
|
-
type: CdpType.MCD,
|
|
119
|
-
owner: userAddress,
|
|
120
|
-
}));
|
|
121
|
-
|
|
122
|
-
return parsedStandardCdps;
|
|
123
|
-
};
|
|
124
|
-
|
|
125
|
-
export const getUserCdps = async (
|
|
126
|
-
provider: EthereumProvider,
|
|
127
|
-
network: NetworkNumber,
|
|
128
|
-
userAddress: EthAddress,
|
|
129
|
-
): Promise<CdpInfo[]> => _getUserCdps(getViemProvider(provider, network), network, userAddress);
|
|
130
|
-
|
|
131
|
-
export const _getMakerCdpData = async (provider: Client, network: NetworkNumber, cdp: CdpInfo): Promise<CdpData> => {
|
|
132
|
-
const vatContract = McdVatContractViem(provider, network);
|
|
133
|
-
const spotterContract = McdSpotterContractViem(provider, network);
|
|
134
|
-
const dogContract = McdDogContractViem(provider, network);
|
|
135
|
-
const jugContract = McdJugContractViem(provider, network);
|
|
136
|
-
|
|
137
|
-
const [
|
|
138
|
-
[ink, art],
|
|
139
|
-
coll,
|
|
140
|
-
par,
|
|
141
|
-
[_, mat],
|
|
142
|
-
[artGlobal, rate, spot, line],
|
|
143
|
-
[duty],
|
|
144
|
-
futureRate,
|
|
145
|
-
chop,
|
|
146
|
-
] = await Promise.all([
|
|
147
|
-
vatContract.read.urns([cdp.ilk, cdp.urn]),
|
|
148
|
-
vatContract.read.gem([cdp.ilk, cdp.urn]),
|
|
149
|
-
spotterContract.read.par(),
|
|
150
|
-
spotterContract.read.ilks([cdp.ilk]),
|
|
151
|
-
vatContract.read.ilks([cdp.ilk]),
|
|
152
|
-
jugContract.read.ilks([cdp.ilk]),
|
|
153
|
-
jugContract.read.drip([cdp.ilk]),
|
|
154
|
-
dogContract.read.chop([cdp.ilk]),
|
|
155
|
-
]);
|
|
156
|
-
|
|
157
|
-
const collInfo = parseCollateralInfo(
|
|
158
|
-
cdp.ilk,
|
|
159
|
-
par.toString(),
|
|
160
|
-
mat.toString(),
|
|
161
|
-
artGlobal.toString(),
|
|
162
|
-
rate.toString(),
|
|
163
|
-
spot.toString(),
|
|
164
|
-
line.toString(),
|
|
165
|
-
duty.toString(),
|
|
166
|
-
futureRate.toString(),
|
|
167
|
-
chop.toString(),
|
|
168
|
-
);
|
|
169
|
-
|
|
170
|
-
const collateral = assetAmountInEth(ink.toString(), `MCD-${cdp.asset}`);
|
|
171
|
-
|
|
172
|
-
const collateralUsd = new Dec(collateral).mul(collInfo.assetPrice).toString();
|
|
173
|
-
const debt = new Dec(art).times(collInfo.currentRate).div(1e27).floor()
|
|
174
|
-
.toString();
|
|
175
|
-
const futureDebt = new Dec(art).times(collInfo.futureRate).div(1e27).floor()
|
|
176
|
-
.toString(); // after drip
|
|
177
|
-
const liquidationPrice = new Dec(debt).times(collInfo.liqRatio).div(ink).toString();
|
|
178
|
-
|
|
179
|
-
let ratio = new Dec(ink).times(collInfo.assetPrice).div(debt).times(100)
|
|
180
|
-
.toString();
|
|
181
|
-
if (new Dec(debt).eq(0)) ratio = '0';
|
|
182
|
-
|
|
183
|
-
const debtTooLow = new Dec(debt).gt(0) && new Dec(assetAmountInEth(debt, 'DAI')).lt(collInfo.minDebt);
|
|
184
|
-
|
|
185
|
-
return {
|
|
186
|
-
owner: cdp.owner,
|
|
187
|
-
id: cdp.id.toString(),
|
|
188
|
-
urn: cdp.urn,
|
|
189
|
-
type: CdpType.MCD,
|
|
190
|
-
ilk: cdp.ilk,
|
|
191
|
-
ilkLabel: collInfo.ilkLabel,
|
|
192
|
-
asset: cdp.asset,
|
|
193
|
-
collateral,
|
|
194
|
-
collateralUsd,
|
|
195
|
-
futureDebt: assetAmountInEth(futureDebt, 'DAI'),
|
|
196
|
-
debtDai: assetAmountInEth(debt, 'DAI'),
|
|
197
|
-
debtUsd: assetAmountInEth(debt, 'DAI'),
|
|
198
|
-
debtInAsset: assetAmountInEth(debt, 'DAI'),
|
|
199
|
-
debtAssetPrice: '1',
|
|
200
|
-
debtAssetMarketPrice: '1',
|
|
201
|
-
liquidationPrice,
|
|
202
|
-
ratio,
|
|
203
|
-
liqRatio: collInfo.liqRatio.toString(),
|
|
204
|
-
liqPercent: parseFloat(collInfo.liqPercent.toString()),
|
|
205
|
-
assetPrice: collInfo.assetPrice,
|
|
206
|
-
daiLabel: 'DAI',
|
|
207
|
-
debtAsset: 'DAI',
|
|
208
|
-
unclaimedCollateral: assetAmountInEth(coll.toString(), cdp.asset),
|
|
209
|
-
debtTooLow,
|
|
210
|
-
minDebt: collInfo.minDebt,
|
|
211
|
-
stabilityFee: collInfo.stabilityFee,
|
|
212
|
-
creatableDebt: collInfo.creatableDebt,
|
|
213
|
-
globalDebtCeiling: collInfo.globalDebtCeiling,
|
|
214
|
-
globalDebtCurrent: collInfo.globalDebtCurrent,
|
|
215
|
-
liquidationFee: collInfo.liquidationFee,
|
|
216
|
-
lastUpdated: Date.now(),
|
|
217
|
-
};
|
|
218
|
-
};
|
|
219
|
-
|
|
220
|
-
export const getMakerCdpData = async (
|
|
221
|
-
provider: EthereumProvider,
|
|
222
|
-
network: NetworkNumber,
|
|
223
|
-
cdp: CdpInfo,
|
|
1
|
+
import Dec from 'decimal.js';
|
|
2
|
+
import {
|
|
3
|
+
assetAmountInEth, bytesToString, getAssetInfo, ilkToAsset,
|
|
4
|
+
} from '@defisaver/tokens';
|
|
5
|
+
import { Client, PublicClient } from 'viem';
|
|
6
|
+
import {
|
|
7
|
+
Blockish, EthAddress, EthereumProvider, NetworkNumber, PositionBalances,
|
|
8
|
+
} from '../types/common';
|
|
9
|
+
import {
|
|
10
|
+
getConfigContractAddress, McdDogContractViem, McdGetCdpsContractViem, McdJugContractViem, McdSpotterContractViem, McdVatContractViem, McdViewContractViem,
|
|
11
|
+
} from '../contracts';
|
|
12
|
+
import { CdpData, CdpInfo, CdpType } from '../types';
|
|
13
|
+
import { wethToEth } from '../services/utils';
|
|
14
|
+
import { parseCollateralInfo } from '../helpers/makerHelpers';
|
|
15
|
+
import { getViemProvider, setViemBlockNumber } from '../services/viem';
|
|
16
|
+
|
|
17
|
+
export const _getMakerAccountBalances = async (provider: PublicClient, network: NetworkNumber, block: Blockish, addressMapping: boolean, cdpId: string, _managerAddress?: EthAddress): Promise<PositionBalances> => {
|
|
18
|
+
let balances: PositionBalances = {
|
|
19
|
+
collateral: {},
|
|
20
|
+
debt: {},
|
|
21
|
+
};
|
|
22
|
+
const managerAddress = _managerAddress || '0x5ef30b9986345249bc32d8928B7ee64DE9435E39'; // Default CDP Manager (This is used only to differentiate BProtocol CDPs)
|
|
23
|
+
|
|
24
|
+
if (!cdpId) {
|
|
25
|
+
return balances;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const viewContract = McdViewContractViem(provider, network, block);
|
|
29
|
+
const vatContract = McdVatContractViem(provider, network, block);
|
|
30
|
+
const spotterContract = McdSpotterContractViem(provider, network, block);
|
|
31
|
+
const dogContract = McdDogContractViem(provider, network, block);
|
|
32
|
+
const jugContract = McdJugContractViem(provider, network, block);
|
|
33
|
+
|
|
34
|
+
let ilk;
|
|
35
|
+
|
|
36
|
+
const needsIlk = block !== 'latest' && new Dec(block).lt(14410792) && new Dec(block).gte(14384301);
|
|
37
|
+
|
|
38
|
+
if (needsIlk) {
|
|
39
|
+
ilk = (await viewContract.read.getUrnAndIlk([managerAddress, BigInt(cdpId)], setViemBlockNumber(block)))[1];
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// @ts-ignore
|
|
43
|
+
// [urn, owner, userAddr, ilk, collateral, debt]
|
|
44
|
+
const cdpInfo: any = await viewContract.read.getCdpInfo((needsIlk ? [managerAddress, cdpId, ilk] : [cdpId]), setViemBlockNumber(block));
|
|
45
|
+
cdpInfo.ilk = cdpInfo[3];
|
|
46
|
+
|
|
47
|
+
const [
|
|
48
|
+
par,
|
|
49
|
+
[_, mat],
|
|
50
|
+
[artGlobal, rate, spot, line],
|
|
51
|
+
[duty],
|
|
52
|
+
futureRate,
|
|
53
|
+
chop,
|
|
54
|
+
] = await Promise.all([
|
|
55
|
+
spotterContract.read.par(setViemBlockNumber(block)),
|
|
56
|
+
spotterContract.read.ilks(needsIlk ? [ilk] : [cdpInfo.ilk], setViemBlockNumber(block)),
|
|
57
|
+
vatContract.read.ilks(needsIlk ? [ilk] : [cdpInfo.ilk], setViemBlockNumber(block)),
|
|
58
|
+
jugContract.read.ilks(needsIlk ? [ilk] : [cdpInfo.ilk], setViemBlockNumber(block)),
|
|
59
|
+
jugContract.read.drip(needsIlk ? [ilk] : [cdpInfo.ilk], setViemBlockNumber(block)),
|
|
60
|
+
dogContract.read.chop(needsIlk ? [ilk] : [cdpInfo.ilk], setViemBlockNumber(block)),
|
|
61
|
+
]);
|
|
62
|
+
|
|
63
|
+
const ilkInfo = parseCollateralInfo(
|
|
64
|
+
needsIlk ? ilk : cdpInfo.ilk,
|
|
65
|
+
par.toString(),
|
|
66
|
+
mat.toString(),
|
|
67
|
+
artGlobal.toString(),
|
|
68
|
+
rate.toString(),
|
|
69
|
+
spot.toString(),
|
|
70
|
+
line.toString(),
|
|
71
|
+
duty.toString(),
|
|
72
|
+
futureRate.toString(),
|
|
73
|
+
chop.toString(),
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
const collateral = cdpInfo[4];
|
|
78
|
+
const debt = cdpInfo[5];
|
|
79
|
+
|
|
80
|
+
const asset = wethToEth(ilkToAsset(needsIlk ? ilk : cdpInfo.ilk));
|
|
81
|
+
|
|
82
|
+
balances = {
|
|
83
|
+
collateral: {
|
|
84
|
+
[addressMapping ? getAssetInfo(asset, network).address.toLowerCase() : asset]: asset === 'WBTC' ? new Dec(collateral).div(1e10).floor().toString() : collateral,
|
|
85
|
+
},
|
|
86
|
+
debt: {
|
|
87
|
+
[addressMapping ? getAssetInfo('DAI', network).address.toLowerCase() : 'DAI']: new Dec(debt).times(ilkInfo.currentRate).div(1e27).floor()
|
|
88
|
+
.toString(),
|
|
89
|
+
},
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
return balances;
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
export const getMakerAccountBalances = async (
|
|
96
|
+
provider: EthereumProvider,
|
|
97
|
+
network: NetworkNumber,
|
|
98
|
+
block: Blockish,
|
|
99
|
+
addressMapping: boolean,
|
|
100
|
+
cdpId: string,
|
|
101
|
+
_managerAddress?: EthAddress,
|
|
102
|
+
): Promise<PositionBalances> => _getMakerAccountBalances(getViemProvider(provider, network, { batch: { multicall: true } }), network, block, addressMapping, cdpId, _managerAddress);
|
|
103
|
+
|
|
104
|
+
export const _getUserCdps = async (provider: Client, network: NetworkNumber, userAddress: EthAddress): Promise<CdpInfo[]> => {
|
|
105
|
+
if (!userAddress) return [];
|
|
106
|
+
const mcdGetCdpsContract = McdGetCdpsContractViem(provider, network);
|
|
107
|
+
|
|
108
|
+
const mcdCdpManagerAddress = getConfigContractAddress('McdCdpManager', network);
|
|
109
|
+
|
|
110
|
+
const standardCdps = await mcdGetCdpsContract.read.getCdpsAsc([mcdCdpManagerAddress, userAddress]);
|
|
111
|
+
|
|
112
|
+
const parsedStandardCdps = standardCdps[0].map((id, i) => ({
|
|
113
|
+
id: parseInt(id.toString(), 10),
|
|
114
|
+
ilk: standardCdps[2][i].toLowerCase() as EthAddress, // collateral type
|
|
115
|
+
ilkLabel: bytesToString(standardCdps[2][i].toLowerCase()),
|
|
116
|
+
urn: standardCdps[1][i].toLowerCase() as EthAddress, // contract of cdp
|
|
117
|
+
asset: ilkToAsset(standardCdps[2][i]),
|
|
118
|
+
type: CdpType.MCD,
|
|
119
|
+
owner: userAddress,
|
|
120
|
+
}));
|
|
121
|
+
|
|
122
|
+
return parsedStandardCdps;
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
export const getUserCdps = async (
|
|
126
|
+
provider: EthereumProvider,
|
|
127
|
+
network: NetworkNumber,
|
|
128
|
+
userAddress: EthAddress,
|
|
129
|
+
): Promise<CdpInfo[]> => _getUserCdps(getViemProvider(provider, network), network, userAddress);
|
|
130
|
+
|
|
131
|
+
export const _getMakerCdpData = async (provider: Client, network: NetworkNumber, cdp: CdpInfo): Promise<CdpData> => {
|
|
132
|
+
const vatContract = McdVatContractViem(provider, network);
|
|
133
|
+
const spotterContract = McdSpotterContractViem(provider, network);
|
|
134
|
+
const dogContract = McdDogContractViem(provider, network);
|
|
135
|
+
const jugContract = McdJugContractViem(provider, network);
|
|
136
|
+
|
|
137
|
+
const [
|
|
138
|
+
[ink, art],
|
|
139
|
+
coll,
|
|
140
|
+
par,
|
|
141
|
+
[_, mat],
|
|
142
|
+
[artGlobal, rate, spot, line],
|
|
143
|
+
[duty],
|
|
144
|
+
futureRate,
|
|
145
|
+
chop,
|
|
146
|
+
] = await Promise.all([
|
|
147
|
+
vatContract.read.urns([cdp.ilk, cdp.urn]),
|
|
148
|
+
vatContract.read.gem([cdp.ilk, cdp.urn]),
|
|
149
|
+
spotterContract.read.par(),
|
|
150
|
+
spotterContract.read.ilks([cdp.ilk]),
|
|
151
|
+
vatContract.read.ilks([cdp.ilk]),
|
|
152
|
+
jugContract.read.ilks([cdp.ilk]),
|
|
153
|
+
jugContract.read.drip([cdp.ilk]),
|
|
154
|
+
dogContract.read.chop([cdp.ilk]),
|
|
155
|
+
]);
|
|
156
|
+
|
|
157
|
+
const collInfo = parseCollateralInfo(
|
|
158
|
+
cdp.ilk,
|
|
159
|
+
par.toString(),
|
|
160
|
+
mat.toString(),
|
|
161
|
+
artGlobal.toString(),
|
|
162
|
+
rate.toString(),
|
|
163
|
+
spot.toString(),
|
|
164
|
+
line.toString(),
|
|
165
|
+
duty.toString(),
|
|
166
|
+
futureRate.toString(),
|
|
167
|
+
chop.toString(),
|
|
168
|
+
);
|
|
169
|
+
|
|
170
|
+
const collateral = assetAmountInEth(ink.toString(), `MCD-${cdp.asset}`);
|
|
171
|
+
|
|
172
|
+
const collateralUsd = new Dec(collateral).mul(collInfo.assetPrice).toString();
|
|
173
|
+
const debt = new Dec(art).times(collInfo.currentRate).div(1e27).floor()
|
|
174
|
+
.toString();
|
|
175
|
+
const futureDebt = new Dec(art).times(collInfo.futureRate).div(1e27).floor()
|
|
176
|
+
.toString(); // after drip
|
|
177
|
+
const liquidationPrice = new Dec(debt).times(collInfo.liqRatio).div(ink).toString();
|
|
178
|
+
|
|
179
|
+
let ratio = new Dec(ink).times(collInfo.assetPrice).div(debt).times(100)
|
|
180
|
+
.toString();
|
|
181
|
+
if (new Dec(debt).eq(0)) ratio = '0';
|
|
182
|
+
|
|
183
|
+
const debtTooLow = new Dec(debt).gt(0) && new Dec(assetAmountInEth(debt, 'DAI')).lt(collInfo.minDebt);
|
|
184
|
+
|
|
185
|
+
return {
|
|
186
|
+
owner: cdp.owner,
|
|
187
|
+
id: cdp.id.toString(),
|
|
188
|
+
urn: cdp.urn,
|
|
189
|
+
type: CdpType.MCD,
|
|
190
|
+
ilk: cdp.ilk,
|
|
191
|
+
ilkLabel: collInfo.ilkLabel,
|
|
192
|
+
asset: cdp.asset,
|
|
193
|
+
collateral,
|
|
194
|
+
collateralUsd,
|
|
195
|
+
futureDebt: assetAmountInEth(futureDebt, 'DAI'),
|
|
196
|
+
debtDai: assetAmountInEth(debt, 'DAI'),
|
|
197
|
+
debtUsd: assetAmountInEth(debt, 'DAI'),
|
|
198
|
+
debtInAsset: assetAmountInEth(debt, 'DAI'),
|
|
199
|
+
debtAssetPrice: '1',
|
|
200
|
+
debtAssetMarketPrice: '1',
|
|
201
|
+
liquidationPrice,
|
|
202
|
+
ratio,
|
|
203
|
+
liqRatio: collInfo.liqRatio.toString(),
|
|
204
|
+
liqPercent: parseFloat(collInfo.liqPercent.toString()),
|
|
205
|
+
assetPrice: collInfo.assetPrice,
|
|
206
|
+
daiLabel: 'DAI',
|
|
207
|
+
debtAsset: 'DAI',
|
|
208
|
+
unclaimedCollateral: assetAmountInEth(coll.toString(), cdp.asset),
|
|
209
|
+
debtTooLow,
|
|
210
|
+
minDebt: collInfo.minDebt,
|
|
211
|
+
stabilityFee: collInfo.stabilityFee,
|
|
212
|
+
creatableDebt: collInfo.creatableDebt,
|
|
213
|
+
globalDebtCeiling: collInfo.globalDebtCeiling,
|
|
214
|
+
globalDebtCurrent: collInfo.globalDebtCurrent,
|
|
215
|
+
liquidationFee: collInfo.liquidationFee,
|
|
216
|
+
lastUpdated: Date.now(),
|
|
217
|
+
};
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
export const getMakerCdpData = async (
|
|
221
|
+
provider: EthereumProvider,
|
|
222
|
+
network: NetworkNumber,
|
|
223
|
+
cdp: CdpInfo,
|
|
224
224
|
): Promise<CdpData> => _getMakerCdpData(getViemProvider(provider, network), network, cdp);
|