@defisaver/positions-sdk 2.1.54 → 2.1.56-dev-exposure

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