@defisaver/positions-sdk 2.0.14 → 2.0.15-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.
Files changed (152) hide show
  1. package/.mocharc.json +4 -4
  2. package/.nvmrc +1 -1
  3. package/README.md +64 -64
  4. package/cjs/aaveV2/index.js +9 -5
  5. package/cjs/aaveV3/index.js +63 -46
  6. package/cjs/aaveV3/merit.d.ts +17 -0
  7. package/cjs/aaveV3/merit.js +95 -0
  8. package/cjs/aaveV3/merkl.d.ts +19 -0
  9. package/cjs/aaveV3/merkl.js +98 -0
  10. package/cjs/compoundV2/index.js +13 -7
  11. package/cjs/compoundV3/index.js +7 -2
  12. package/cjs/config/contracts.d.ts +6510 -1851
  13. package/cjs/config/contracts.js +33 -12
  14. package/cjs/contracts.d.ts +178 -0
  15. package/cjs/eulerV2/index.js +11 -2
  16. package/cjs/fluid/index.js +105 -34
  17. package/cjs/helpers/aaveHelpers/index.js +0 -1
  18. package/cjs/helpers/compoundHelpers/index.d.ts +3 -5
  19. package/cjs/helpers/compoundHelpers/index.js +15 -11
  20. package/cjs/helpers/eulerHelpers/index.d.ts +0 -5
  21. package/cjs/helpers/eulerHelpers/index.js +2 -31
  22. package/cjs/helpers/fluidHelpers/index.js +2 -0
  23. package/cjs/helpers/liquityV2Helpers/index.js +3 -2
  24. package/cjs/helpers/morphoBlueHelpers/index.js +66 -66
  25. package/cjs/liquityV2/index.js +10 -2
  26. package/cjs/llamaLend/index.js +10 -2
  27. package/cjs/morphoBlue/index.js +20 -6
  28. package/cjs/spark/index.js +20 -30
  29. package/cjs/staking/eligibility.d.ts +11 -0
  30. package/cjs/staking/eligibility.js +43 -0
  31. package/cjs/staking/index.d.ts +1 -0
  32. package/cjs/staking/index.js +1 -0
  33. package/cjs/staking/staking.d.ts +1 -7
  34. package/cjs/staking/staking.js +29 -55
  35. package/cjs/types/aave.d.ts +3 -8
  36. package/cjs/types/common.d.ts +16 -4
  37. package/cjs/types/common.js +10 -1
  38. package/cjs/types/euler.d.ts +3 -3
  39. package/cjs/types/fluid.d.ts +3 -5
  40. package/cjs/types/liquityV2.d.ts +3 -3
  41. package/cjs/types/llamaLend.d.ts +3 -1
  42. package/cjs/types/morphoBlue.d.ts +3 -5
  43. package/cjs/types/spark.d.ts +0 -3
  44. package/esm/aaveV2/index.js +9 -5
  45. package/esm/aaveV3/index.js +64 -47
  46. package/esm/aaveV3/merit.d.ts +17 -0
  47. package/esm/aaveV3/merit.js +90 -0
  48. package/esm/aaveV3/merkl.d.ts +19 -0
  49. package/esm/aaveV3/merkl.js +92 -0
  50. package/esm/compoundV2/index.js +13 -7
  51. package/esm/compoundV3/index.js +7 -2
  52. package/esm/config/contracts.d.ts +6510 -1851
  53. package/esm/config/contracts.js +33 -12
  54. package/esm/contracts.d.ts +178 -0
  55. package/esm/eulerV2/index.js +11 -2
  56. package/esm/fluid/index.js +106 -35
  57. package/esm/helpers/aaveHelpers/index.js +0 -1
  58. package/esm/helpers/compoundHelpers/index.d.ts +3 -5
  59. package/esm/helpers/compoundHelpers/index.js +16 -12
  60. package/esm/helpers/eulerHelpers/index.d.ts +0 -5
  61. package/esm/helpers/eulerHelpers/index.js +2 -30
  62. package/esm/helpers/fluidHelpers/index.js +2 -0
  63. package/esm/helpers/liquityV2Helpers/index.js +3 -2
  64. package/esm/helpers/morphoBlueHelpers/index.js +66 -66
  65. package/esm/liquityV2/index.js +11 -3
  66. package/esm/llamaLend/index.js +11 -3
  67. package/esm/morphoBlue/index.js +21 -7
  68. package/esm/spark/index.js +21 -31
  69. package/esm/staking/eligibility.d.ts +11 -0
  70. package/esm/staking/eligibility.js +36 -0
  71. package/esm/staking/index.d.ts +1 -0
  72. package/esm/staking/index.js +1 -0
  73. package/esm/staking/staking.d.ts +1 -7
  74. package/esm/staking/staking.js +28 -53
  75. package/esm/types/aave.d.ts +3 -8
  76. package/esm/types/common.d.ts +16 -4
  77. package/esm/types/common.js +9 -0
  78. package/esm/types/euler.d.ts +3 -3
  79. package/esm/types/fluid.d.ts +3 -5
  80. package/esm/types/liquityV2.d.ts +3 -3
  81. package/esm/types/llamaLend.d.ts +3 -1
  82. package/esm/types/morphoBlue.d.ts +3 -5
  83. package/esm/types/spark.d.ts +0 -3
  84. package/package.json +47 -47
  85. package/src/aaveV2/index.ts +239 -236
  86. package/src/aaveV3/index.ts +511 -488
  87. package/src/aaveV3/merit.ts +98 -0
  88. package/src/aaveV3/merkl.ts +141 -0
  89. package/src/compoundV2/index.ts +244 -240
  90. package/src/compoundV3/index.ts +274 -270
  91. package/src/config/contracts.ts +1129 -1108
  92. package/src/constants/index.ts +6 -6
  93. package/src/contracts.ts +107 -107
  94. package/src/curveUsd/index.ts +250 -250
  95. package/src/eulerV2/index.ts +324 -314
  96. package/src/exchange/index.ts +25 -25
  97. package/src/fluid/index.ts +1636 -1568
  98. package/src/helpers/aaveHelpers/index.ts +169 -170
  99. package/src/helpers/compoundHelpers/index.ts +267 -261
  100. package/src/helpers/curveUsdHelpers/index.ts +40 -40
  101. package/src/helpers/eulerHelpers/index.ts +222 -259
  102. package/src/helpers/fluidHelpers/index.ts +326 -324
  103. package/src/helpers/index.ts +10 -10
  104. package/src/helpers/liquityV2Helpers/index.ts +82 -80
  105. package/src/helpers/llamaLendHelpers/index.ts +53 -53
  106. package/src/helpers/makerHelpers/index.ts +52 -52
  107. package/src/helpers/morphoBlueHelpers/index.ts +390 -390
  108. package/src/helpers/sparkHelpers/index.ts +155 -155
  109. package/src/index.ts +45 -45
  110. package/src/liquity/index.ts +104 -104
  111. package/src/liquityV2/index.ts +418 -408
  112. package/src/llamaLend/index.ts +305 -296
  113. package/src/maker/index.ts +223 -223
  114. package/src/markets/aave/index.ts +116 -116
  115. package/src/markets/aave/marketAssets.ts +49 -49
  116. package/src/markets/compound/index.ts +227 -227
  117. package/src/markets/compound/marketsAssets.ts +90 -90
  118. package/src/markets/curveUsd/index.ts +69 -69
  119. package/src/markets/euler/index.ts +26 -26
  120. package/src/markets/fluid/index.ts +2456 -2456
  121. package/src/markets/index.ts +25 -25
  122. package/src/markets/liquityV2/index.ts +102 -102
  123. package/src/markets/llamaLend/contractAddresses.ts +141 -141
  124. package/src/markets/llamaLend/index.ts +235 -235
  125. package/src/markets/morphoBlue/index.ts +895 -895
  126. package/src/markets/spark/index.ts +29 -29
  127. package/src/markets/spark/marketAssets.ts +11 -11
  128. package/src/moneymarket/moneymarketCommonService.ts +80 -80
  129. package/src/morphoBlue/index.ts +236 -222
  130. package/src/portfolio/index.ts +285 -285
  131. package/src/services/priceService.ts +159 -159
  132. package/src/services/utils.ts +63 -63
  133. package/src/services/viem.ts +32 -32
  134. package/src/setup.ts +8 -8
  135. package/src/spark/index.ts +444 -456
  136. package/src/staking/eligibility.ts +37 -0
  137. package/src/staking/index.ts +2 -1
  138. package/src/staking/staking.ts +169 -194
  139. package/src/types/aave.ts +189 -194
  140. package/src/types/common.ts +103 -88
  141. package/src/types/compound.ts +136 -136
  142. package/src/types/curveUsd.ts +121 -121
  143. package/src/types/euler.ts +175 -174
  144. package/src/types/fluid.ts +448 -450
  145. package/src/types/index.ts +11 -11
  146. package/src/types/liquity.ts +30 -30
  147. package/src/types/liquityV2.ts +126 -126
  148. package/src/types/llamaLend.ts +159 -157
  149. package/src/types/maker.ts +63 -63
  150. package/src/types/morphoBlue.ts +194 -194
  151. package/src/types/portfolio.ts +60 -60
  152. package/src/types/spark.ts +135 -137
@@ -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);