@defisaver/positions-sdk 2.1.6 → 2.1.8

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.
Files changed (132) hide show
  1. package/.mocharc.json +4 -4
  2. package/.nvmrc +1 -1
  3. package/README.md +64 -64
  4. package/cjs/aaveV3/index.js +3 -3
  5. package/cjs/aaveV3/merit.js +5 -3
  6. package/cjs/aaveV3/merkl.d.ts +1 -1
  7. package/cjs/config/contracts.d.ts +23 -0
  8. package/cjs/config/contracts.js +23 -0
  9. package/cjs/helpers/aaveHelpers/index.d.ts +1 -1
  10. package/cjs/helpers/aaveHelpers/index.js +6 -4
  11. package/cjs/helpers/morphoBlueHelpers/index.d.ts +1 -1
  12. package/cjs/helpers/morphoBlueHelpers/index.js +66 -66
  13. package/cjs/markets/aave/index.js +1 -1
  14. package/cjs/markets/aave/marketAssets.d.ts +4 -0
  15. package/cjs/markets/aave/marketAssets.js +5 -1
  16. package/cjs/markets/compound/index.js +11 -0
  17. package/cjs/markets/compound/marketsAssets.d.ts +7 -0
  18. package/cjs/markets/compound/marketsAssets.js +7 -0
  19. package/cjs/markets/spark/marketAssets.d.ts +1 -0
  20. package/cjs/markets/spark/marketAssets.js +1 -0
  21. package/cjs/services/utils.d.ts +8 -4
  22. package/cjs/services/utils.js +30 -10
  23. package/cjs/services/viem.d.ts +54 -15
  24. package/cjs/services/viem.js +2 -0
  25. package/cjs/staking/eligibility.js +1 -6
  26. package/cjs/types/common.d.ts +4 -2
  27. package/cjs/types/common.js +2 -0
  28. package/esm/aaveV3/index.js +4 -4
  29. package/esm/aaveV3/merit.js +5 -3
  30. package/esm/aaveV3/merkl.d.ts +1 -1
  31. package/esm/config/contracts.d.ts +23 -0
  32. package/esm/config/contracts.js +23 -0
  33. package/esm/helpers/aaveHelpers/index.d.ts +1 -1
  34. package/esm/helpers/aaveHelpers/index.js +7 -5
  35. package/esm/helpers/morphoBlueHelpers/index.d.ts +1 -1
  36. package/esm/helpers/morphoBlueHelpers/index.js +66 -66
  37. package/esm/markets/aave/index.js +1 -1
  38. package/esm/markets/aave/marketAssets.d.ts +4 -0
  39. package/esm/markets/aave/marketAssets.js +4 -0
  40. package/esm/markets/compound/index.js +11 -0
  41. package/esm/markets/compound/marketsAssets.d.ts +7 -0
  42. package/esm/markets/compound/marketsAssets.js +7 -0
  43. package/esm/markets/spark/marketAssets.d.ts +1 -0
  44. package/esm/markets/spark/marketAssets.js +1 -0
  45. package/esm/services/utils.d.ts +8 -4
  46. package/esm/services/utils.js +21 -5
  47. package/esm/services/viem.d.ts +54 -15
  48. package/esm/services/viem.js +3 -1
  49. package/esm/staking/eligibility.js +1 -6
  50. package/esm/types/common.d.ts +4 -2
  51. package/esm/types/common.js +2 -0
  52. package/package.json +47 -47
  53. package/src/aaveV2/index.ts +240 -240
  54. package/src/aaveV3/index.ts +614 -614
  55. package/src/aaveV3/merit.ts +97 -94
  56. package/src/aaveV3/merkl.ts +74 -74
  57. package/src/claiming/aaveV3.ts +154 -154
  58. package/src/claiming/compV3.ts +22 -22
  59. package/src/claiming/index.ts +12 -12
  60. package/src/claiming/king.ts +66 -66
  61. package/src/claiming/morphoBlue.ts +118 -118
  62. package/src/claiming/spark.ts +225 -225
  63. package/src/compoundV2/index.ts +244 -244
  64. package/src/compoundV3/index.ts +274 -274
  65. package/src/config/contracts.ts +1251 -1228
  66. package/src/constants/index.ts +10 -10
  67. package/src/contracts.ts +120 -120
  68. package/src/curveUsd/index.ts +254 -254
  69. package/src/eulerV2/index.ts +324 -324
  70. package/src/exchange/index.ts +25 -25
  71. package/src/fluid/index.ts +1638 -1638
  72. package/src/helpers/aaveHelpers/index.ts +187 -185
  73. package/src/helpers/compoundHelpers/index.ts +283 -283
  74. package/src/helpers/curveUsdHelpers/index.ts +40 -40
  75. package/src/helpers/eulerHelpers/index.ts +222 -222
  76. package/src/helpers/fluidHelpers/index.ts +326 -326
  77. package/src/helpers/index.ts +10 -10
  78. package/src/helpers/liquityV2Helpers/index.ts +82 -82
  79. package/src/helpers/llamaLendHelpers/index.ts +53 -53
  80. package/src/helpers/makerHelpers/index.ts +52 -52
  81. package/src/helpers/morphoBlueHelpers/index.ts +396 -396
  82. package/src/helpers/sparkHelpers/index.ts +155 -155
  83. package/src/index.ts +47 -47
  84. package/src/liquity/index.ts +159 -159
  85. package/src/liquityV2/index.ts +657 -657
  86. package/src/llamaLend/index.ts +305 -305
  87. package/src/maker/index.ts +223 -223
  88. package/src/markets/aave/index.ts +116 -116
  89. package/src/markets/aave/marketAssets.ts +54 -49
  90. package/src/markets/compound/index.ts +238 -227
  91. package/src/markets/compound/marketsAssets.ts +97 -90
  92. package/src/markets/curveUsd/index.ts +69 -69
  93. package/src/markets/euler/index.ts +26 -26
  94. package/src/markets/fluid/index.ts +2456 -2456
  95. package/src/markets/index.ts +25 -25
  96. package/src/markets/liquityV2/index.ts +102 -102
  97. package/src/markets/llamaLend/contractAddresses.ts +141 -141
  98. package/src/markets/llamaLend/index.ts +235 -235
  99. package/src/markets/morphoBlue/index.ts +895 -895
  100. package/src/markets/spark/index.ts +29 -29
  101. package/src/markets/spark/marketAssets.ts +12 -11
  102. package/src/moneymarket/moneymarketCommonService.ts +80 -80
  103. package/src/morphoBlue/index.ts +274 -274
  104. package/src/portfolio/index.ts +570 -570
  105. package/src/services/priceService.ts +159 -159
  106. package/src/services/utils.ts +115 -99
  107. package/src/services/viem.ts +34 -32
  108. package/src/setup.ts +8 -8
  109. package/src/spark/index.ts +445 -445
  110. package/src/staking/eligibility.ts +53 -59
  111. package/src/staking/index.ts +1 -1
  112. package/src/staking/staking.ts +170 -170
  113. package/src/types/aave.ts +189 -189
  114. package/src/types/claiming.ts +109 -109
  115. package/src/types/common.ts +107 -105
  116. package/src/types/compound.ts +136 -136
  117. package/src/types/curveUsd.ts +123 -123
  118. package/src/types/euler.ts +175 -175
  119. package/src/types/fluid.ts +448 -448
  120. package/src/types/index.ts +13 -13
  121. package/src/types/liquity.ts +30 -30
  122. package/src/types/liquityV2.ts +126 -126
  123. package/src/types/llamaLend.ts +159 -159
  124. package/src/types/maker.ts +63 -63
  125. package/src/types/merit.ts +1 -1
  126. package/src/types/merkl.ts +70 -70
  127. package/src/types/morphoBlue.ts +194 -194
  128. package/src/types/portfolio.ts +60 -60
  129. package/src/types/spark.ts +135 -135
  130. package/src/umbrella/index.ts +69 -69
  131. package/src/umbrella/umbrellaUtils.ts +29 -29
  132. package/CLAUDE.md +0 -32
@@ -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);