@gearbox-protocol/sdk 14.12.0-next.32 → 14.12.0-next.34

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 (29) hide show
  1. package/dist/cjs/dev/withdrawalAbi.js +64 -70
  2. package/dist/cjs/dev/withdrawalUtils.js +1 -1
  3. package/dist/cjs/plugins/adapters/abi/adapters/iMidasGatewayAdapterV311.js +40 -280
  4. package/dist/cjs/plugins/adapters/contracts/MidasGatewayAdapterContract.js +45 -44
  5. package/dist/cjs/preview/prerequisites/buildCollateralPrerequisites.js +39 -3
  6. package/dist/cjs/preview/prerequisites/checkPrerequisites.js +2 -1
  7. package/dist/cjs/preview/preview/buildDelayedPreview.js +9 -4
  8. package/dist/cjs/preview/preview/detectCloseOrRepay.js +2 -2
  9. package/dist/cjs/preview/preview/previewCloseOrRepayCreditAccount.js +17 -2
  10. package/dist/cjs/preview/preview/previewOperation.js +4 -1
  11. package/dist/esm/dev/withdrawalAbi.js +64 -70
  12. package/dist/esm/dev/withdrawalUtils.js +1 -1
  13. package/dist/esm/plugins/adapters/abi/adapters/iMidasGatewayAdapterV311.js +40 -280
  14. package/dist/esm/plugins/adapters/contracts/MidasGatewayAdapterContract.js +47 -45
  15. package/dist/esm/preview/prerequisites/buildCollateralPrerequisites.js +43 -4
  16. package/dist/esm/preview/prerequisites/checkPrerequisites.js +2 -1
  17. package/dist/esm/preview/preview/buildDelayedPreview.js +9 -4
  18. package/dist/esm/preview/preview/detectCloseOrRepay.js +2 -2
  19. package/dist/esm/preview/preview/previewCloseOrRepayCreditAccount.js +22 -3
  20. package/dist/esm/preview/preview/previewOperation.js +4 -1
  21. package/dist/types/dev/withdrawalAbi.d.ts +53 -57
  22. package/dist/types/plugins/adapters/abi/adapters/iMidasGatewayAdapterV311.d.ts +33 -219
  23. package/dist/types/plugins/adapters/contracts/MidasGatewayAdapterContract.d.ts +44 -232
  24. package/dist/types/preview/prerequisites/buildCollateralPrerequisites.d.ts +1 -0
  25. package/dist/types/preview/prerequisites/types.d.ts +10 -2
  26. package/dist/types/preview/preview/buildDelayedPreview.d.ts +7 -2
  27. package/dist/types/preview/preview/detectCloseOrRepay.d.ts +5 -3
  28. package/dist/types/preview/preview/types.d.ts +6 -4
  29. package/package.json +1 -1
@@ -5,7 +5,7 @@ import {
5
5
  ERROR_UNPRICEABLE_TOKEN,
6
6
  PREVIEW_DUST
7
7
  } from "./types.js";
8
- function buildDelayedPreview(afterInstant, before, detected, convert) {
8
+ function buildDelayedPreview(afterInstant, before, detected, convert, receivedToken) {
9
9
  const { request, intent } = detected;
10
10
  const post = afterInstant.clone();
11
11
  const converter = makeSafeConverter(convert);
@@ -13,7 +13,7 @@ function buildDelayedPreview(afterInstant, before, detected, convert) {
13
13
  const collateralWithdrawn = new AssetsMap();
14
14
  switch (intent?.type) {
15
15
  case "CLOSE_ACCOUNT":
16
- return buildClosePreview(post, converter);
16
+ return buildClosePreview(post, converter, receivedToken);
17
17
  case "DECREASE_LEVERAGE":
18
18
  repayFromClaim(post, request.claimToken, converter.convert, claimed);
19
19
  break;
@@ -84,14 +84,19 @@ function totalValueInUnderlying(post, convert, dust) {
84
84
  (token, balance) => balance > dust ? convert(token, post.underlying, balance) : 0n
85
85
  );
86
86
  }
87
- function buildClosePreview(post, converter) {
87
+ function buildClosePreview(post, converter, receivedToken) {
88
88
  const totalValue = totalValueInUnderlying(post, converter.convert, 0n);
89
89
  return {
90
90
  operation: "CloseCreditAccount",
91
91
  permanent: false,
92
92
  creditManager: post.creditManager,
93
93
  creditAccount: post.creditAccount,
94
- receivedAmount: BigIntMath.max(totalValue - post.totalDebt, 0n),
94
+ // Oracle estimate computed in the underlying; RWA underlyings convert
95
+ // 1:1 with their vault asset, so the amount holds for `receivedToken`
96
+ receivedAmount: {
97
+ token: receivedToken,
98
+ balance: BigIntMath.max(totalValue - post.totalDebt, 0n)
99
+ },
95
100
  error: converter.error
96
101
  };
97
102
  }
@@ -9,12 +9,12 @@ function isCloseOrRepay(multicall) {
9
9
  );
10
10
  return repaysFullDebt && withdrawsEverything;
11
11
  }
12
- function classifyCloseOrRepay(multicall, underlying) {
12
+ function classifyCloseOrRepay(multicall, exitTokens) {
13
13
  for (const op of multicall) {
14
14
  if (op.operation === "AddCollateral") {
15
15
  return "repay";
16
16
  }
17
- if (op.operation === "WithdrawCollateral" && !isAddressEqual(op.token, underlying)) {
17
+ if (op.operation === "WithdrawCollateral" && !exitTokens.some((token) => isAddressEqual(op.token, token))) {
18
18
  return "repay";
19
19
  }
20
20
  }
@@ -1,4 +1,8 @@
1
- import { AP_WETH_TOKEN, NO_VERSION } from "../../sdk/index.js";
1
+ import {
2
+ AP_WETH_TOKEN,
3
+ MAX_UINT256,
4
+ NO_VERSION
5
+ } from "../../sdk/index.js";
2
6
  import { classifyCloseOrRepay } from "./detectCloseOrRepay.js";
3
7
  import {
4
8
  replayMulticall
@@ -9,8 +13,13 @@ async function previewCloseOrRepayCreditAccount(input, operation, permanent, opt
9
13
  const market = sdk.marketRegister.findByCreditManager(
10
14
  operation.creditManager
11
15
  );
16
+ const exitTokens = [market.underlying];
17
+ const meta = sdk.tokensMeta.get(market.underlying);
18
+ if (meta && sdk.tokensMeta.isRWAUnderlying(meta)) {
19
+ exitTokens.push(meta.asset);
20
+ }
12
21
  const replay = await replayMulticall(sdk, operation, options);
13
- const kind = classifyCloseOrRepay(operation.multicall, market.underlying);
22
+ const kind = classifyCloseOrRepay(operation.multicall, exitTokens);
14
23
  return kind === "close" ? previewCloseCreditAccount(input, operation, permanent, replay) : previewRepayCreditAccount(input, operation, permanent, replay);
15
24
  }
16
25
  function previewCloseCreditAccount(input, operation, permanent, replay) {
@@ -19,6 +28,13 @@ function previewCloseCreditAccount(input, operation, permanent, replay) {
19
28
  operation.creditManager
20
29
  );
21
30
  const { after, error } = replay;
31
+ let receivedToken = market.underlying;
32
+ for (const m of operation.multicall) {
33
+ if (m.operation === "WithdrawCollateral" && m.amount === MAX_UINT256) {
34
+ receivedToken = m.token;
35
+ break;
36
+ }
37
+ }
22
38
  return {
23
39
  operation: "CloseCreditAccount",
24
40
  permanent,
@@ -26,7 +42,10 @@ function previewCloseCreditAccount(input, operation, permanent, replay) {
26
42
  creditAccount: operation.creditAccount,
27
43
  // On a malformed multicall the withdrawn amount depends on best-effort
28
44
  // replayed balances and may be unreliable
29
- receivedAmount: after.collateralWithdrawn.getOrZero(market.underlying),
45
+ receivedAmount: {
46
+ token: receivedToken,
47
+ balance: after.collateralWithdrawn.getOrZero(receivedToken)
48
+ },
30
49
  error
31
50
  };
32
51
  }
@@ -56,6 +56,8 @@ async function previewMulticallOperation(input, operation, options) {
56
56
  operation.creditManager
57
57
  );
58
58
  const convert = (token, to, amount) => market.priceOracle.convert(token, to, amount);
59
+ const meta = sdk.tokensMeta.get(market.underlying);
60
+ const receivedToken = meta && sdk.tokensMeta.isRWAUnderlying(meta) ? meta.asset : market.underlying;
59
61
  return {
60
62
  operation: "DelayedCreditAccountOperation",
61
63
  creditAccount: operation.creditAccount,
@@ -66,7 +68,8 @@ async function previewMulticallOperation(input, operation, options) {
66
68
  after.account,
67
69
  before,
68
70
  delayed,
69
- convert
71
+ convert,
72
+ receivedToken
70
73
  )
71
74
  };
72
75
  }
@@ -9,13 +9,13 @@ export declare const midasGatewayAbi: readonly [{
9
9
  readonly type: "address";
10
10
  readonly internalType: "address";
11
11
  }, {
12
- readonly name: "_accessControl";
12
+ readonly name: "_quoteToken";
13
13
  readonly type: "address";
14
14
  readonly internalType: "address";
15
15
  }, {
16
- readonly name: "_transferMaster";
17
- readonly type: "address";
18
- readonly internalType: "address";
16
+ readonly name: "_isAccessControlled";
17
+ readonly type: "bool";
18
+ readonly internalType: "bool";
19
19
  }, {
20
20
  readonly name: "_allowedMarketConfigurator";
21
21
  readonly type: "address";
@@ -32,6 +32,10 @@ export declare const midasGatewayAbi: readonly [{
32
32
  readonly name: "_redemptionLogger";
33
33
  readonly type: "address";
34
34
  readonly internalType: "address";
35
+ }, {
36
+ readonly name: "_withDelayedWithdrawals";
37
+ readonly type: "bool";
38
+ readonly internalType: "bool";
35
39
  }];
36
40
  readonly stateMutability: "nonpayable";
37
41
  }, {
@@ -78,10 +82,6 @@ export declare const midasGatewayAbi: readonly [{
78
82
  readonly type: "function";
79
83
  readonly name: "depositInstant";
80
84
  readonly inputs: readonly [{
81
- readonly name: "tokenIn";
82
- readonly type: "address";
83
- readonly internalType: "address";
84
- }, {
85
85
  readonly name: "amountToken";
86
86
  readonly type: "uint256";
87
87
  readonly internalType: "uint256";
@@ -153,10 +153,6 @@ export declare const midasGatewayAbi: readonly [{
153
153
  readonly name: "account";
154
154
  readonly type: "address";
155
155
  readonly internalType: "address";
156
- }, {
157
- readonly name: "tokenOut";
158
- readonly type: "address";
159
- readonly internalType: "address";
160
156
  }];
161
157
  readonly outputs: readonly [{
162
158
  readonly name: "pendingAmount";
@@ -184,12 +180,28 @@ export declare const midasGatewayAbi: readonly [{
184
180
  readonly stateMutability: "view";
185
181
  }, {
186
182
  readonly type: "function";
187
- readonly name: "redeemInstant";
188
- readonly inputs: readonly [{
189
- readonly name: "tokenOut";
183
+ readonly name: "phantomToken";
184
+ readonly inputs: readonly [];
185
+ readonly outputs: readonly [{
186
+ readonly name: "";
190
187
  readonly type: "address";
191
188
  readonly internalType: "address";
192
- }, {
189
+ }];
190
+ readonly stateMutability: "view";
191
+ }, {
192
+ readonly type: "function";
193
+ readonly name: "quoteToken";
194
+ readonly inputs: readonly [];
195
+ readonly outputs: readonly [{
196
+ readonly name: "";
197
+ readonly type: "address";
198
+ readonly internalType: "address";
199
+ }];
200
+ readonly stateMutability: "view";
201
+ }, {
202
+ readonly type: "function";
203
+ readonly name: "redeemInstant";
204
+ readonly inputs: readonly [{
193
205
  readonly name: "amountMTokenIn";
194
206
  readonly type: "uint256";
195
207
  readonly internalType: "uint256";
@@ -214,10 +226,6 @@ export declare const midasGatewayAbi: readonly [{
214
226
  readonly type: "function";
215
227
  readonly name: "requestRedeem";
216
228
  readonly inputs: readonly [{
217
- readonly name: "tokenOut";
218
- readonly type: "address";
219
- readonly internalType: "address";
220
- }, {
221
229
  readonly name: "amountMTokenIn";
222
230
  readonly type: "uint256";
223
231
  readonly internalType: "uint256";
@@ -266,10 +274,6 @@ export declare const midasGatewayAbi: readonly [{
266
274
  readonly type: "function";
267
275
  readonly name: "withdraw";
268
276
  readonly inputs: readonly [{
269
- readonly name: "tokenOut";
270
- readonly type: "address";
271
- readonly internalType: "address";
272
- }, {
273
277
  readonly name: "amount";
274
278
  readonly type: "uint256";
275
279
  readonly internalType: "uint256";
@@ -283,10 +287,6 @@ export declare const midasGatewayAbi: readonly [{
283
287
  readonly name: "redeemer";
284
288
  readonly type: "address";
285
289
  readonly internalType: "address";
286
- }, {
287
- readonly name: "tokenOut";
288
- readonly type: "address";
289
- readonly internalType: "address";
290
290
  }, {
291
291
  readonly name: "amount";
292
292
  readonly type: "uint256";
@@ -302,6 +302,10 @@ export declare const midasGatewayAbi: readonly [{
302
302
  readonly type: "error";
303
303
  readonly name: "CreditAccountNotEligibleException";
304
304
  readonly inputs: readonly [];
305
+ }, {
306
+ readonly type: "error";
307
+ readonly name: "IncompatibleAccessControlsException";
308
+ readonly inputs: readonly [];
305
309
  }, {
306
310
  readonly type: "error";
307
311
  readonly name: "IncompatibleIssuanceAndRedemptionVaultsException";
@@ -333,6 +337,10 @@ export declare const midasRedeemerAbi: readonly [{
333
337
  readonly name: "_midasRedemptionVault";
334
338
  readonly type: "address";
335
339
  readonly internalType: "address";
340
+ }, {
341
+ readonly name: "_quoteToken";
342
+ readonly type: "address";
343
+ readonly internalType: "address";
336
344
  }];
337
345
  readonly stateMutability: "nonpayable";
338
346
  }, {
@@ -358,11 +366,7 @@ export declare const midasRedeemerAbi: readonly [{
358
366
  }, {
359
367
  readonly type: "function";
360
368
  readonly name: "claimableTokenOutAmount";
361
- readonly inputs: readonly [{
362
- readonly name: "tokenOut";
363
- readonly type: "address";
364
- readonly internalType: "address";
365
- }];
369
+ readonly inputs: readonly [];
366
370
  readonly outputs: readonly [{
367
371
  readonly name: "";
368
372
  readonly type: "uint256";
@@ -432,17 +436,23 @@ export declare const midasRedeemerAbi: readonly [{
432
436
  }, {
433
437
  readonly type: "function";
434
438
  readonly name: "pendingTokenOutAmount";
435
- readonly inputs: readonly [{
436
- readonly name: "tokenOut";
437
- readonly type: "address";
438
- readonly internalType: "address";
439
- }];
439
+ readonly inputs: readonly [];
440
440
  readonly outputs: readonly [{
441
441
  readonly name: "";
442
442
  readonly type: "uint256";
443
443
  readonly internalType: "uint256";
444
444
  }];
445
445
  readonly stateMutability: "view";
446
+ }, {
447
+ readonly type: "function";
448
+ readonly name: "quoteToken";
449
+ readonly inputs: readonly [];
450
+ readonly outputs: readonly [{
451
+ readonly name: "";
452
+ readonly type: "address";
453
+ readonly internalType: "address";
454
+ }];
455
+ readonly stateMutability: "view";
446
456
  }, {
447
457
  readonly type: "function";
448
458
  readonly name: "redemptionStartTimestamp";
@@ -467,10 +477,6 @@ export declare const midasRedeemerAbi: readonly [{
467
477
  readonly type: "function";
468
478
  readonly name: "requestRedeem";
469
479
  readonly inputs: readonly [{
470
- readonly name: "tokenOut";
471
- readonly type: "address";
472
- readonly internalType: "address";
473
- }, {
474
480
  readonly name: "amountMTokenIn";
475
481
  readonly type: "uint256";
476
482
  readonly internalType: "uint256";
@@ -491,10 +497,6 @@ export declare const midasRedeemerAbi: readonly [{
491
497
  readonly type: "function";
492
498
  readonly name: "withdraw";
493
499
  readonly inputs: readonly [{
494
- readonly name: "tokenOut";
495
- readonly type: "address";
496
- readonly internalType: "address";
497
- }, {
498
500
  readonly name: "amount";
499
501
  readonly type: "uint256";
500
502
  readonly internalType: "uint256";
@@ -529,7 +531,11 @@ export declare const midasRedemptionVaultPhantomTokenAbi: readonly [{
529
531
  readonly type: "address";
530
532
  readonly internalType: "address";
531
533
  }, {
532
- readonly name: "_tokenOut";
534
+ readonly name: "_mToken";
535
+ readonly type: "address";
536
+ readonly internalType: "address";
537
+ }, {
538
+ readonly name: "_quoteToken";
533
539
  readonly type: "address";
534
540
  readonly internalType: "address";
535
541
  }];
@@ -658,16 +664,6 @@ export declare const midasRedemptionVaultPhantomTokenAbi: readonly [{
658
664
  readonly internalType: "string";
659
665
  }];
660
666
  readonly stateMutability: "view";
661
- }, {
662
- readonly type: "function";
663
- readonly name: "tokenOut";
664
- readonly inputs: readonly [];
665
- readonly outputs: readonly [{
666
- readonly name: "";
667
- readonly type: "address";
668
- readonly internalType: "address";
669
- }];
670
- readonly stateMutability: "view";
671
667
  }, {
672
668
  readonly type: "function";
673
669
  readonly name: "totalSupply";