@gearbox-protocol/sdk 14.12.0-test.1 → 14.12.0-test.3

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 (48) hide show
  1. package/dist/cjs/abi/compressors/subcompressors/withdrawal/mellowWithdrawalSubcompressor.js +85 -1
  2. package/dist/cjs/abi/compressors/subcompressors/withdrawal/midasWithdrawalSubcompressor.js +85 -1
  3. package/dist/cjs/abi/compressors/subcompressors/withdrawal/securitizeRedemptionSubcompressor.js +85 -1
  4. package/dist/cjs/abi/compressors/withdrawalCompressor.js +197 -1
  5. package/dist/cjs/abi/helpers/redemptionLogger.js +160 -0
  6. package/dist/cjs/abi/router/midasWorker.js +4 -4
  7. package/dist/cjs/dev/AccountOpener.js +2 -2
  8. package/dist/cjs/plugins/adapters/abi/actionAbi.js +13 -5
  9. package/dist/cjs/plugins/adapters/abi/adapters/iSecuritizeRedemptionGatewayAdapterV311.js +23 -8
  10. package/dist/cjs/plugins/adapters/abi/conctructorAbi.js +2 -1
  11. package/dist/cjs/preview/preview/buildDelayedPreview.js +38 -16
  12. package/dist/cjs/sdk/accounts/liquidations/LiquidationsService.js +21 -0
  13. package/dist/cjs/sdk/accounts/liquidations/MultichainLiquidationsService.js +26 -0
  14. package/dist/cjs/sdk/accounts/liquidations/helpers.js +29 -2
  15. package/dist/cjs/sdk/accounts/withdrawal-compressor/intent-codec.js +4 -6
  16. package/dist/esm/abi/compressors/subcompressors/withdrawal/mellowWithdrawalSubcompressor.js +85 -1
  17. package/dist/esm/abi/compressors/subcompressors/withdrawal/midasWithdrawalSubcompressor.js +85 -1
  18. package/dist/esm/abi/compressors/subcompressors/withdrawal/securitizeRedemptionSubcompressor.js +85 -1
  19. package/dist/esm/abi/compressors/withdrawalCompressor.js +197 -1
  20. package/dist/esm/abi/helpers/redemptionLogger.js +136 -0
  21. package/dist/esm/abi/router/midasWorker.js +4 -4
  22. package/dist/esm/dev/AccountOpener.js +2 -2
  23. package/dist/esm/plugins/adapters/abi/actionAbi.js +13 -5
  24. package/dist/esm/plugins/adapters/abi/adapters/iSecuritizeRedemptionGatewayAdapterV311.js +23 -8
  25. package/dist/esm/plugins/adapters/abi/conctructorAbi.js +2 -1
  26. package/dist/esm/preview/preview/buildDelayedPreview.js +41 -17
  27. package/dist/esm/sdk/accounts/liquidations/LiquidationsService.js +23 -1
  28. package/dist/esm/sdk/accounts/liquidations/MultichainLiquidationsService.js +26 -0
  29. package/dist/esm/sdk/accounts/liquidations/helpers.js +27 -1
  30. package/dist/esm/sdk/accounts/withdrawal-compressor/intent-codec.js +4 -6
  31. package/dist/types/abi/compressors/subcompressors/withdrawal/mellowWithdrawalSubcompressor.d.ts +121 -0
  32. package/dist/types/abi/compressors/subcompressors/withdrawal/midasWithdrawalSubcompressor.d.ts +121 -0
  33. package/dist/types/abi/compressors/subcompressors/withdrawal/securitizeRedemptionSubcompressor.d.ts +121 -0
  34. package/dist/types/abi/compressors/withdrawalCompressor.d.ts +274 -0
  35. package/dist/types/abi/helpers/redemptionLogger.d.ts +167 -0
  36. package/dist/types/abi/router/midasWorker.d.ts +4 -4
  37. package/dist/types/permissionless/bindings/compressors/withdrawal-compressor.d.ts +274 -0
  38. package/dist/types/plugins/adapters/abi/actionAbi.d.ts +1 -1
  39. package/dist/types/plugins/adapters/abi/adapters/iSecuritizeRedemptionGatewayAdapterV311.d.ts +37 -7
  40. package/dist/types/plugins/adapters/contracts/SecuritizeRedemptionGatewayAdapterContract.d.ts +37 -7
  41. package/dist/types/preview/preview/buildDelayedPreview.d.ts +8 -26
  42. package/dist/types/sdk/accounts/liquidations/LiquidationsService.d.ts +5 -1
  43. package/dist/types/sdk/accounts/liquidations/MultichainLiquidationsService.d.ts +5 -1
  44. package/dist/types/sdk/accounts/liquidations/helpers.d.ts +12 -0
  45. package/dist/types/sdk/accounts/liquidations/types.d.ts +55 -0
  46. package/dist/types/sdk/accounts/withdrawal-compressor/intent-codec.d.ts +1 -1
  47. package/dist/types/sdk/accounts/withdrawal-compressor/types.d.ts +4 -9
  48. package/package.json +1 -1
@@ -17,7 +17,10 @@ const withdrawalCompressorAbi = [
17
17
  {
18
18
  type: "function",
19
19
  name: "compressorTypeToCompressor",
20
- inputs: [{ name: "", type: "bytes32", internalType: "bytes32" }],
20
+ inputs: [
21
+ { name: "", type: "bytes32", internalType: "bytes32" },
22
+ { name: "", type: "uint256", internalType: "uint256" }
23
+ ],
21
24
  outputs: [{ name: "", type: "address", internalType: "address" }],
22
25
  stateMutability: "view"
23
26
  },
@@ -101,6 +104,158 @@ const withdrawalCompressorAbi = [
101
104
  ],
102
105
  stateMutability: "view"
103
106
  },
107
+ {
108
+ type: "function",
109
+ name: "getExternalAccountCurrentWithdrawals",
110
+ inputs: [
111
+ {
112
+ name: "withdrawalTokens",
113
+ type: "address[]",
114
+ internalType: "address[]"
115
+ },
116
+ { name: "account", type: "address", internalType: "address" }
117
+ ],
118
+ outputs: [
119
+ {
120
+ name: "",
121
+ type: "tuple[]",
122
+ internalType: "struct ClaimableWithdrawal[]",
123
+ components: [
124
+ { name: "token", type: "address", internalType: "address" },
125
+ {
126
+ name: "withdrawalPhantomToken",
127
+ type: "address",
128
+ internalType: "address"
129
+ },
130
+ {
131
+ name: "withdrawalTokenSpent",
132
+ type: "uint256",
133
+ internalType: "uint256"
134
+ },
135
+ {
136
+ name: "outputs",
137
+ type: "tuple[]",
138
+ internalType: "struct WithdrawalOutput[]",
139
+ components: [
140
+ { name: "token", type: "address", internalType: "address" },
141
+ { name: "isDelayed", type: "bool", internalType: "bool" },
142
+ { name: "amount", type: "uint256", internalType: "uint256" }
143
+ ]
144
+ },
145
+ {
146
+ name: "claimCalls",
147
+ type: "tuple[]",
148
+ internalType: "struct MultiCall[]",
149
+ components: [
150
+ { name: "target", type: "address", internalType: "address" },
151
+ { name: "callData", type: "bytes", internalType: "bytes" }
152
+ ]
153
+ },
154
+ { name: "extraData", type: "bytes", internalType: "bytes" }
155
+ ]
156
+ },
157
+ {
158
+ name: "",
159
+ type: "tuple[]",
160
+ internalType: "struct PendingWithdrawal[]",
161
+ components: [
162
+ { name: "token", type: "address", internalType: "address" },
163
+ {
164
+ name: "withdrawalPhantomToken",
165
+ type: "address",
166
+ internalType: "address"
167
+ },
168
+ {
169
+ name: "expectedOutputs",
170
+ type: "tuple[]",
171
+ internalType: "struct WithdrawalOutput[]",
172
+ components: [
173
+ { name: "token", type: "address", internalType: "address" },
174
+ { name: "isDelayed", type: "bool", internalType: "bool" },
175
+ { name: "amount", type: "uint256", internalType: "uint256" }
176
+ ]
177
+ },
178
+ { name: "claimableAt", type: "uint256", internalType: "uint256" },
179
+ { name: "extraData", type: "bytes", internalType: "bytes" }
180
+ ]
181
+ }
182
+ ],
183
+ stateMutability: "view"
184
+ },
185
+ {
186
+ type: "function",
187
+ name: "getExternalAccountCurrentWithdrawals",
188
+ inputs: [
189
+ { name: "withdrawalToken", type: "address", internalType: "address" },
190
+ { name: "account", type: "address", internalType: "address" }
191
+ ],
192
+ outputs: [
193
+ {
194
+ name: "",
195
+ type: "tuple[]",
196
+ internalType: "struct ClaimableWithdrawal[]",
197
+ components: [
198
+ { name: "token", type: "address", internalType: "address" },
199
+ {
200
+ name: "withdrawalPhantomToken",
201
+ type: "address",
202
+ internalType: "address"
203
+ },
204
+ {
205
+ name: "withdrawalTokenSpent",
206
+ type: "uint256",
207
+ internalType: "uint256"
208
+ },
209
+ {
210
+ name: "outputs",
211
+ type: "tuple[]",
212
+ internalType: "struct WithdrawalOutput[]",
213
+ components: [
214
+ { name: "token", type: "address", internalType: "address" },
215
+ { name: "isDelayed", type: "bool", internalType: "bool" },
216
+ { name: "amount", type: "uint256", internalType: "uint256" }
217
+ ]
218
+ },
219
+ {
220
+ name: "claimCalls",
221
+ type: "tuple[]",
222
+ internalType: "struct MultiCall[]",
223
+ components: [
224
+ { name: "target", type: "address", internalType: "address" },
225
+ { name: "callData", type: "bytes", internalType: "bytes" }
226
+ ]
227
+ },
228
+ { name: "extraData", type: "bytes", internalType: "bytes" }
229
+ ]
230
+ },
231
+ {
232
+ name: "",
233
+ type: "tuple[]",
234
+ internalType: "struct PendingWithdrawal[]",
235
+ components: [
236
+ { name: "token", type: "address", internalType: "address" },
237
+ {
238
+ name: "withdrawalPhantomToken",
239
+ type: "address",
240
+ internalType: "address"
241
+ },
242
+ {
243
+ name: "expectedOutputs",
244
+ type: "tuple[]",
245
+ internalType: "struct WithdrawalOutput[]",
246
+ components: [
247
+ { name: "token", type: "address", internalType: "address" },
248
+ { name: "isDelayed", type: "bool", internalType: "bool" },
249
+ { name: "amount", type: "uint256", internalType: "uint256" }
250
+ ]
251
+ },
252
+ { name: "claimableAt", type: "uint256", internalType: "uint256" },
253
+ { name: "extraData", type: "bytes", internalType: "bytes" }
254
+ ]
255
+ }
256
+ ],
257
+ stateMutability: "view"
258
+ },
104
259
  {
105
260
  type: "function",
106
261
  name: "getWithdrawableAssets",
@@ -257,6 +412,26 @@ const withdrawalCompressorAbi = [
257
412
  ],
258
413
  stateMutability: "view"
259
414
  },
415
+ {
416
+ type: "function",
417
+ name: "getWithdrawalStatus",
418
+ inputs: [
419
+ { name: "redeemers", type: "address[]", internalType: "address[]" }
420
+ ],
421
+ outputs: [
422
+ { name: "", type: "uint8[]", internalType: "enum WithdrawalStatus[]" }
423
+ ],
424
+ stateMutability: "view"
425
+ },
426
+ {
427
+ type: "function",
428
+ name: "getWithdrawalStatus",
429
+ inputs: [{ name: "redeemer", type: "address", internalType: "address" }],
430
+ outputs: [
431
+ { name: "", type: "uint8", internalType: "enum WithdrawalStatus" }
432
+ ],
433
+ stateMutability: "view"
434
+ },
260
435
  {
261
436
  type: "function",
262
437
  name: "owner",
@@ -290,6 +465,17 @@ const withdrawalCompressorAbi = [
290
465
  outputs: [],
291
466
  stateMutability: "nonpayable"
292
467
  },
468
+ {
469
+ type: "function",
470
+ name: "setWithdrawableVersionToSpecificCompressorVersion",
471
+ inputs: [
472
+ { name: "withdrawableType", type: "bytes32", internalType: "bytes32" },
473
+ { name: "withdrawableVersion", type: "uint256", internalType: "uint256" },
474
+ { name: "compressorVersion", type: "uint256", internalType: "uint256" }
475
+ ],
476
+ outputs: [],
477
+ stateMutability: "nonpayable"
478
+ },
293
479
  {
294
480
  type: "function",
295
481
  name: "transferOwnership",
@@ -311,6 +497,16 @@ const withdrawalCompressorAbi = [
311
497
  outputs: [{ name: "", type: "bytes32", internalType: "bytes32" }],
312
498
  stateMutability: "view"
313
499
  },
500
+ {
501
+ type: "function",
502
+ name: "withdrawableTypeToSpecificCompressorVersion",
503
+ inputs: [
504
+ { name: "", type: "bytes32", internalType: "bytes32" },
505
+ { name: "", type: "uint256", internalType: "uint256" }
506
+ ],
507
+ outputs: [{ name: "", type: "uint256", internalType: "uint256" }],
508
+ stateMutability: "view"
509
+ },
314
510
  {
315
511
  type: "event",
316
512
  name: "OwnershipTransferred",
@@ -0,0 +1,136 @@
1
+ const redemptionLoggerAbi = [
2
+ {
3
+ type: "constructor",
4
+ inputs: [{ name: "_owner", type: "address", internalType: "address" }],
5
+ stateMutability: "nonpayable"
6
+ },
7
+ {
8
+ type: "function",
9
+ name: "allowedGateways",
10
+ inputs: [{ name: "", type: "address", internalType: "address" }],
11
+ outputs: [{ name: "", type: "bool", internalType: "bool" }],
12
+ stateMutability: "view"
13
+ },
14
+ {
15
+ type: "function",
16
+ name: "contractType",
17
+ inputs: [],
18
+ outputs: [{ name: "", type: "bytes32", internalType: "bytes32" }],
19
+ stateMutability: "view"
20
+ },
21
+ {
22
+ type: "function",
23
+ name: "logRedemption",
24
+ inputs: [
25
+ { name: "creditAccount", type: "address", internalType: "address" },
26
+ { name: "redeemer", type: "address", internalType: "address" },
27
+ { name: "extraData", type: "bytes", internalType: "bytes" }
28
+ ],
29
+ outputs: [],
30
+ stateMutability: "nonpayable"
31
+ },
32
+ {
33
+ type: "function",
34
+ name: "owner",
35
+ inputs: [],
36
+ outputs: [{ name: "", type: "address", internalType: "address" }],
37
+ stateMutability: "view"
38
+ },
39
+ {
40
+ type: "function",
41
+ name: "redemptionLogs",
42
+ inputs: [{ name: "redeemer", type: "address", internalType: "address" }],
43
+ outputs: [
44
+ {
45
+ name: "",
46
+ type: "tuple",
47
+ internalType: "struct IRedemptionLogger.RedemptionLog",
48
+ components: [
49
+ { name: "creditAccount", type: "address", internalType: "address" },
50
+ { name: "redeemer", type: "address", internalType: "address" },
51
+ { name: "extraData", type: "bytes", internalType: "bytes" }
52
+ ]
53
+ }
54
+ ],
55
+ stateMutability: "view"
56
+ },
57
+ {
58
+ type: "function",
59
+ name: "renounceOwnership",
60
+ inputs: [],
61
+ outputs: [],
62
+ stateMutability: "nonpayable"
63
+ },
64
+ {
65
+ type: "function",
66
+ name: "setGatewayAllowed",
67
+ inputs: [
68
+ { name: "gateway", type: "address", internalType: "address" },
69
+ { name: "allowed", type: "bool", internalType: "bool" }
70
+ ],
71
+ outputs: [],
72
+ stateMutability: "nonpayable"
73
+ },
74
+ {
75
+ type: "function",
76
+ name: "transferOwnership",
77
+ inputs: [{ name: "newOwner", type: "address", internalType: "address" }],
78
+ outputs: [],
79
+ stateMutability: "nonpayable"
80
+ },
81
+ {
82
+ type: "function",
83
+ name: "version",
84
+ inputs: [],
85
+ outputs: [{ name: "", type: "uint256", internalType: "uint256" }],
86
+ stateMutability: "view"
87
+ },
88
+ {
89
+ type: "event",
90
+ name: "OwnershipTransferred",
91
+ inputs: [
92
+ {
93
+ name: "previousOwner",
94
+ type: "address",
95
+ indexed: true,
96
+ internalType: "address"
97
+ },
98
+ {
99
+ name: "newOwner",
100
+ type: "address",
101
+ indexed: true,
102
+ internalType: "address"
103
+ }
104
+ ],
105
+ anonymous: false
106
+ },
107
+ {
108
+ type: "event",
109
+ name: "RedemptionLogged",
110
+ inputs: [
111
+ {
112
+ name: "creditAccount",
113
+ type: "address",
114
+ indexed: true,
115
+ internalType: "address"
116
+ },
117
+ {
118
+ name: "redeemer",
119
+ type: "address",
120
+ indexed: true,
121
+ internalType: "address"
122
+ },
123
+ {
124
+ name: "extraData",
125
+ type: "bytes",
126
+ indexed: false,
127
+ internalType: "bytes"
128
+ }
129
+ ],
130
+ anonymous: false
131
+ },
132
+ { type: "error", name: "GatewayNotAllowedException", inputs: [] }
133
+ ];
134
+ export {
135
+ redemptionLoggerAbi
136
+ };
@@ -400,7 +400,7 @@ const midasWorkerAbi = [
400
400
  name: "trimSpecialVertex",
401
401
  inputs: [
402
402
  {
403
- name: "edge",
403
+ name: "",
404
404
  type: "tuple",
405
405
  internalType: "struct Edge",
406
406
  components: [
@@ -421,7 +421,7 @@ const midasWorkerAbi = [
421
421
  ]
422
422
  },
423
423
  {
424
- name: "graph",
424
+ name: "",
425
425
  type: "tuple",
426
426
  internalType: "struct Graph",
427
427
  components: [
@@ -480,10 +480,10 @@ const midasWorkerAbi = [
480
480
  }
481
481
  ]
482
482
  },
483
- { name: "targetToken", type: "address", internalType: "address" }
483
+ { name: "", type: "address", internalType: "address" }
484
484
  ],
485
485
  outputs: [{ name: "", type: "bool", internalType: "bool" }],
486
- stateMutability: "view"
486
+ stateMutability: "pure"
487
487
  },
488
488
  {
489
489
  type: "function",
@@ -489,10 +489,10 @@ class AccountOpener extends SDKConstruct {
489
489
  );
490
490
  txHash = await this.#anvil.writeContract({
491
491
  account: depositor,
492
- address: metadata.approveTarget,
492
+ address: underlying,
493
493
  abi: ierc20Abi,
494
494
  functionName: "approve",
495
- args: [address, allowance],
495
+ args: [metadata.approveTarget, allowance],
496
496
  chain: this.#anvil.chain
497
497
  });
498
498
  let receipt = await this.#anvil.waitForTransactionReceipt({
@@ -62,7 +62,7 @@ const adapterActionAbi = Object.fromEntries(
62
62
  Object.fromEntries(
63
63
  Object.entries(versionedSignature).map(([version, signature]) => [
64
64
  version,
65
- parseAbi([signature])
65
+ parseAbi(Array.isArray(signature) ? signature : [signature])
66
66
  ])
67
67
  )
68
68
  ]
@@ -70,10 +70,18 @@ const adapterActionAbi = Object.fromEntries(
70
70
  );
71
71
  const adapterActionSelectors = Object.fromEntries(
72
72
  Object.entries(adapterActionSignatures).flatMap(
73
- ([adapterType, versionedSignature]) => Object.entries(versionedSignature).map(([version, signature]) => [
74
- toFunctionSelector(signature),
75
- { version: +version, signature, adapterType }
76
- ])
73
+ ([adapterType, versionedSignature]) => Object.entries(versionedSignature).map(([version, signature]) => {
74
+ if (Array.isArray(signature)) {
75
+ return signature.map((s) => [
76
+ toFunctionSelector(s),
77
+ { version: +version, signature: s, adapterType }
78
+ ]);
79
+ }
80
+ return [
81
+ toFunctionSelector(signature),
82
+ { version: +version, signature, adapterType }
83
+ ];
84
+ })
77
85
  )
78
86
  );
79
87
  export {
@@ -1,4 +1,11 @@
1
1
  const iSecuritizeRedemptionGatewayAdapterV311Abi = [
2
+ {
3
+ type: "function",
4
+ name: "acl",
5
+ inputs: [],
6
+ outputs: [{ name: "", type: "address", internalType: "address" }],
7
+ stateMutability: "view"
8
+ },
2
9
  {
3
10
  type: "function",
4
11
  name: "claim",
@@ -26,11 +33,11 @@ const iSecuritizeRedemptionGatewayAdapterV311Abi = [
26
33
  type: "function",
27
34
  name: "depositPhantomToken",
28
35
  inputs: [
29
- { name: "token", type: "address", internalType: "address" },
30
- { name: "amount", type: "uint256", internalType: "uint256" }
36
+ { name: "", type: "address", internalType: "address" },
37
+ { name: "", type: "uint256", internalType: "uint256" }
31
38
  ],
32
39
  outputs: [{ name: "", type: "bool", internalType: "bool" }],
33
- stateMutability: "nonpayable"
40
+ stateMutability: "view"
34
41
  },
35
42
  {
36
43
  type: "function",
@@ -126,14 +133,22 @@ const iSecuritizeRedemptionGatewayAdapterV311Abi = [
126
133
  type: "function",
127
134
  name: "withdrawPhantomToken",
128
135
  inputs: [
129
- { name: "token", type: "address", internalType: "address" },
130
- { name: "amount", type: "uint256", internalType: "uint256" }
136
+ { name: "", type: "address", internalType: "address" },
137
+ { name: "", type: "uint256", internalType: "uint256" }
131
138
  ],
132
- outputs: [{ name: "useSafePrices", type: "bool", internalType: "bool" }],
133
- stateMutability: "nonpayable"
139
+ outputs: [{ name: "", type: "bool", internalType: "bool" }],
140
+ stateMutability: "view"
141
+ },
142
+ {
143
+ type: "error",
144
+ name: "AddressIsNotContractException",
145
+ inputs: [{ name: "", type: "address", internalType: "address" }]
134
146
  },
147
+ { type: "error", name: "CallerNotCreditFacadeException", inputs: [] },
135
148
  { type: "error", name: "IncorrectStakedPhantomTokenException", inputs: [] },
136
- { type: "error", name: "InvalidRedemptionGatewayException", inputs: [] }
149
+ { type: "error", name: "InvalidRedemptionGatewayException", inputs: [] },
150
+ { type: "error", name: "NotImplementedException", inputs: [] },
151
+ { type: "error", name: "ZeroAddressException", inputs: [] }
137
152
  ];
138
153
  export {
139
154
  iSecuritizeRedemptionGatewayAdapterV311Abi
@@ -133,7 +133,8 @@ const adapterConstructorAbi = {
133
133
  310: PHANTOM_TOKEN_ADAPTER_ABI
134
134
  },
135
135
  [AdapterType.SECURITIZE_REDEMPTION]: {
136
- 310: PHANTOM_TOKEN_ADAPTER_ABI
136
+ 310: PHANTOM_TOKEN_ADAPTER_ABI,
137
+ 311: PHANTOM_TOKEN_ADAPTER_ABI
137
138
  },
138
139
  // other patterns
139
140
  [AdapterType.STAKING_REWARDS]: {
@@ -1,6 +1,8 @@
1
1
  import { isAddressEqual } from "viem";
2
2
  import { BigIntMath } from "../../common-utils/index.js";
3
- import { AssetsMap } from "../../sdk/index.js";
3
+ import {
4
+ AssetsMap
5
+ } from "../../sdk/index.js";
4
6
  import {
5
7
  ERROR_UNPRICEABLE_TOKEN,
6
8
  PREVIEW_DUST
@@ -18,23 +20,12 @@ function buildDelayedPreview(afterInstant, before, detected, convert, receivedTo
18
20
  repayFromClaim(post, request.claimToken, converter.convert, claimed);
19
21
  break;
20
22
  case "WITHDRAW_COLLATERAL": {
21
- const withdrawn = BigIntMath.min(
22
- intent.withdrawAmount,
23
- post.balances.getOrZero(intent.withdrawToken)
24
- );
25
- if (withdrawn > 0n) {
26
- post.balances.dec(intent.withdrawToken, withdrawn);
27
- collateralWithdrawn.upsert(intent.withdrawToken, withdrawn);
28
- }
29
- const claimSpentOnWithdrawal = isAddressEqual(
30
- intent.withdrawToken,
31
- request.claimToken
32
- ) ? withdrawn : 0n;
33
- repayFromClaim(
23
+ applyWithdrawCollateral(
34
24
  post,
35
- request.claimToken,
36
- converter.convert,
37
- BigIntMath.max(claimed - claimSpentOnWithdrawal, 0n)
25
+ request,
26
+ intent,
27
+ converter,
28
+ collateralWithdrawn
38
29
  );
39
30
  break;
40
31
  }
@@ -69,6 +60,39 @@ function applyClaim(post, request) {
69
60
  post.balances.inc(request.claimToken, claimed);
70
61
  return claimed;
71
62
  }
63
+ function applyWithdrawCollateral(post, request, intent, converter, collateralWithdrawn) {
64
+ const { withdrawToken, withdrawAmount, debtRepaid } = intent;
65
+ const { claimToken } = request;
66
+ const sameToken = isAddressEqual(withdrawToken, claimToken);
67
+ const fromBalance = BigIntMath.min(
68
+ withdrawAmount,
69
+ post.balances.getOrZero(withdrawToken)
70
+ );
71
+ post.balances.dec(withdrawToken, fromBalance);
72
+ let withdrawn = fromBalance;
73
+ const missing = withdrawAmount - fromBalance;
74
+ if (missing > 0n && !sameToken) {
75
+ const available = post.balances.getOrZero(claimToken);
76
+ const cost = converter.convert(withdrawToken, claimToken, missing);
77
+ if (cost > 0n && cost <= available) {
78
+ post.balances.dec(claimToken, cost);
79
+ withdrawn += missing;
80
+ } else if (available > 0n) {
81
+ post.balances.dec(claimToken, available);
82
+ withdrawn += converter.convert(claimToken, withdrawToken, available);
83
+ }
84
+ }
85
+ if (withdrawn > 0n) {
86
+ collateralWithdrawn.upsert(withdrawToken, withdrawn);
87
+ }
88
+ const remaining = post.balances.getOrZero(claimToken);
89
+ if (remaining > 0n) {
90
+ const proceeds = converter.convert(claimToken, post.underlying, remaining);
91
+ post.balances.dec(claimToken, remaining);
92
+ post.balances.inc(post.underlying, proceeds);
93
+ post.repay(BigIntMath.min(proceeds, debtRepaid));
94
+ }
95
+ }
72
96
  function repayFromClaim(post, claimToken, convert, amount) {
73
97
  const spent = BigIntMath.min(amount, post.balances.getOrZero(claimToken));
74
98
  if (spent <= 0n) {
@@ -5,7 +5,8 @@ import {
5
5
  calcEstimatedProfit,
6
6
  calcRepaymentAmount,
7
7
  DUST_THRESHOLD,
8
- pickMainAsset
8
+ pickMainAsset,
9
+ toLiquidatorWithdrawals
9
10
  } from "./helpers.js";
10
11
  class LiquidationsService extends SDKConstruct {
11
12
  /**
@@ -103,6 +104,27 @@ class LiquidationsService extends SDKConstruct {
103
104
  }
104
105
  return { ...account, receivedAssets };
105
106
  }
107
+ /**
108
+ * {@inheritDoc ILiquidationsService.getLiquidatorWithdrawals}
109
+ **/
110
+ async getLiquidatorWithdrawals(props) {
111
+ if (props.networks && !props.networks.includes(this.sdk.networkType)) {
112
+ return [];
113
+ }
114
+ const compressor = this.sdk.withdrawalCompressor;
115
+ if (!compressor) {
116
+ return [];
117
+ }
118
+ await compressor.loadWithdrawableAssets();
119
+ const phantomTokens = new AddressSet(
120
+ compressor.getWithdrawableAssets().map((a) => a.withdrawalPhantomToken)
121
+ );
122
+ const current = await compressor.getExternalAccountCurrentWithdrawals(
123
+ props.liquidator,
124
+ ...phantomTokens.asArray()
125
+ );
126
+ return toLiquidatorWithdrawals(current, this.sdk.networkType);
127
+ }
106
128
  /**
107
129
  * Accounts of expired credit managers with outstanding debt are liquidatable
108
130
  * regardless of their health factor.
@@ -35,6 +35,32 @@ class MultichainLiquidationsService {
35
35
  async getLiquidationDetails(props) {
36
36
  return this.#sdk.chain(props.network).liquidations.getLiquidationDetails(props);
37
37
  }
38
+ /**
39
+ * {@inheritDoc ILiquidationsService.getLiquidatorWithdrawals}
40
+ **/
41
+ async getLiquidatorWithdrawals(props) {
42
+ const chains = [...this.#sdk.chains.entries()].filter(
43
+ ([network]) => !props.networks || props.networks.includes(network)
44
+ );
45
+ const results = await Promise.allSettled(
46
+ chains.map(
47
+ ([, chainSdk]) => chainSdk.liquidations.getLiquidatorWithdrawals(props)
48
+ )
49
+ );
50
+ const withdrawals = [];
51
+ results.forEach((result, i) => {
52
+ const [network, chainSdk] = chains[i];
53
+ if (result.status === "fulfilled") {
54
+ withdrawals.push(...result.value);
55
+ } else {
56
+ chainSdk.logger?.warn(
57
+ result.reason,
58
+ `failed to get liquidator withdrawals on ${network}`
59
+ );
60
+ }
61
+ });
62
+ return withdrawals;
63
+ }
38
64
  }
39
65
  export {
40
66
  MultichainLiquidationsService