@defisaver/positions-sdk 2.0.15-dev.0 → 2.1.0

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