@gearbox-protocol/sdk 14.12.0-next.21 → 14.12.0-next.23

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 (53) hide show
  1. package/dist/cjs/abi/IWithdrawalCompressorV313.js +2 -1
  2. package/dist/cjs/abi/compressors/withdrawalCompressor.js +2 -1
  3. package/dist/cjs/plugins/delayed-withdrawal/DelayedWithdrawalPlugin.js +1 -5
  4. package/dist/cjs/preview/preview/applyInnerOperations.js +83 -58
  5. package/dist/cjs/preview/preview/applyQuotaChanges.js +43 -0
  6. package/dist/cjs/preview/preview/errors.js +0 -16
  7. package/dist/cjs/preview/preview/index.js +2 -0
  8. package/dist/cjs/preview/preview/previewAdjustCreditAccount.js +19 -8
  9. package/dist/cjs/preview/preview/previewCloseOrRepayCreditAccount.js +18 -7
  10. package/dist/cjs/preview/preview/previewOpenCreditAccount.js +18 -6
  11. package/dist/cjs/preview/preview/types.js +30 -0
  12. package/dist/cjs/preview/preview/unwrapNativeCollateral.js +10 -4
  13. package/dist/cjs/sdk/OnchainSDK.js +3 -6
  14. package/dist/cjs/sdk/accounts/CreditAccountsServiceV310.js +45 -37
  15. package/dist/cjs/sdk/accounts/withdrawal-compressor/AbstractWithdrawalCompressorContract.js +14 -22
  16. package/dist/cjs/sdk/accounts/withdrawal-compressor/addresses.js +6 -18
  17. package/dist/cjs/sdk/accounts/withdrawal-compressor/createWithdrawalCompressor.js +3 -5
  18. package/dist/esm/abi/IWithdrawalCompressorV313.js +2 -1
  19. package/dist/esm/abi/compressors/withdrawalCompressor.js +2 -1
  20. package/dist/esm/plugins/delayed-withdrawal/DelayedWithdrawalPlugin.js +2 -9
  21. package/dist/esm/preview/preview/applyInnerOperations.js +90 -59
  22. package/dist/esm/preview/preview/applyQuotaChanges.js +19 -0
  23. package/dist/esm/preview/preview/errors.js +0 -15
  24. package/dist/esm/preview/preview/index.js +1 -0
  25. package/dist/esm/preview/preview/previewAdjustCreditAccount.js +20 -8
  26. package/dist/esm/preview/preview/previewCloseOrRepayCreditAccount.js +18 -7
  27. package/dist/esm/preview/preview/previewOpenCreditAccount.js +20 -6
  28. package/dist/esm/preview/preview/types.js +16 -0
  29. package/dist/esm/preview/preview/unwrapNativeCollateral.js +12 -4
  30. package/dist/esm/sdk/OnchainSDK.js +3 -6
  31. package/dist/esm/sdk/accounts/CreditAccountsServiceV310.js +45 -37
  32. package/dist/esm/sdk/accounts/withdrawal-compressor/AbstractWithdrawalCompressorContract.js +14 -22
  33. package/dist/esm/sdk/accounts/withdrawal-compressor/addresses.js +6 -18
  34. package/dist/esm/sdk/accounts/withdrawal-compressor/createWithdrawalCompressor.js +3 -5
  35. package/dist/types/abi/IWithdrawalCompressorV313.d.ts +4 -0
  36. package/dist/types/abi/compressors/withdrawalCompressor.d.ts +4 -0
  37. package/dist/types/permissionless/bindings/compressors/withdrawal-compressor.d.ts +4 -0
  38. package/dist/types/preview/preview/applyInnerOperations.d.ts +8 -33
  39. package/dist/types/preview/preview/applyQuotaChanges.d.ts +24 -0
  40. package/dist/types/preview/preview/errors.d.ts +0 -12
  41. package/dist/types/preview/preview/index.d.ts +1 -0
  42. package/dist/types/preview/preview/previewAdjustCreditAccount.d.ts +1 -1
  43. package/dist/types/preview/preview/previewOpenCreditAccount.d.ts +1 -1
  44. package/dist/types/preview/preview/types.d.ts +81 -0
  45. package/dist/types/preview/preview/unwrapNativeCollateral.d.ts +19 -4
  46. package/dist/types/sdk/OnchainSDK.d.ts +3 -6
  47. package/dist/types/sdk/accounts/CreditAccountsServiceV310.d.ts +5 -1
  48. package/dist/types/sdk/accounts/types.d.ts +32 -3
  49. package/dist/types/sdk/accounts/withdrawal-compressor/WithdrawalCompressorV313Contract.d.ts +4 -0
  50. package/dist/types/sdk/accounts/withdrawal-compressor/addresses.d.ts +1 -2
  51. package/dist/types/sdk/accounts/withdrawal-compressor/createWithdrawalCompressor.d.ts +2 -4
  52. package/dist/types/sdk/accounts/withdrawal-compressor/types.d.ts +7 -0
  53. package/package.json +1 -1
@@ -95,7 +95,8 @@ const iWithdrawalCompressorV313Abi = [
95
95
  { name: "amount", type: "uint256", internalType: "uint256" }
96
96
  ]
97
97
  },
98
- { name: "claimableAt", type: "uint256", internalType: "uint256" }
98
+ { name: "claimableAt", type: "uint256", internalType: "uint256" },
99
+ { name: "extraData", type: "bytes", internalType: "bytes" }
99
100
  ]
100
101
  }
101
102
  ],
@@ -117,7 +117,8 @@ const withdrawalCompressorAbi = [
117
117
  { name: "amount", type: "uint256", internalType: "uint256" }
118
118
  ]
119
119
  },
120
- { name: "claimableAt", type: "uint256", internalType: "uint256" }
120
+ { name: "claimableAt", type: "uint256", internalType: "uint256" },
121
+ { name: "extraData", type: "bytes", internalType: "bytes" }
121
122
  ]
122
123
  }
123
124
  ],
@@ -30,11 +30,7 @@ class DelayedWithdrawalPlugin extends import_sdk.BasePlugin {
30
30
  return this.state;
31
31
  }
32
32
  this.#withdrawableAssets = new import_sdk.AddressMap(void 0, MAP_LABEL);
33
- const compressor = this.sdk.withdrawalCompressor();
34
- this.sdk.logger?.debug(
35
- `loading delayed withdrawal plugin with compressor ${compressor.address}`
36
- );
37
- const resp = await compressor.getWithdrawableAssetsBatch();
33
+ const resp = await this.sdk.withdrawalCompressor.getWithdrawableAssetsBatch();
38
34
  const byCreditManager = /* @__PURE__ */ new Map();
39
35
  for (const cfg of resp) {
40
36
  const assets = byCreditManager.get(cfg.creditManager) ?? [];
@@ -19,13 +19,13 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
19
19
  var applyInnerOperations_exports = {};
20
20
  __export(applyInnerOperations_exports, {
21
21
  applyInnerOperations: () => applyInnerOperations,
22
- applyQuotaChanges: () => applyQuotaChanges,
23
22
  makeInnerOperationsState: () => makeInnerOperationsState
24
23
  });
25
24
  module.exports = __toCommonJS(applyInnerOperations_exports);
26
25
  var import_adapters = require("../../plugins/adapters/index.js");
27
26
  var import_sdk = require("../../sdk/index.js");
28
27
  var import_applyRWAWrapUnwrap = require("./applyRWAWrapUnwrap.js");
28
+ var import_types = require("./types.js");
29
29
  function makeInnerOperationsState() {
30
30
  return {
31
31
  balances: new import_sdk.AssetsMap(),
@@ -39,41 +39,31 @@ function makeInnerOperationsState() {
39
39
  function applyInnerOperations(sdk, multicall, state) {
40
40
  let inBracket = false;
41
41
  let storeSeen = false;
42
+ let error;
42
43
  for (const op of multicall) {
44
+ let opError;
43
45
  switch (op.operation) {
44
46
  case "AddCollateral":
45
- state.collateralAdded.inc(op.token, op.amount);
46
- state.balances.inc(op.token, op.amount);
47
+ applyAddCollateral(state, op);
47
48
  break;
48
- case "WithdrawCollateral": {
49
- const running = state.balances.getOrZero(op.token);
50
- const amount = op.amount === import_sdk.MAX_UINT256 ? running > 0n ? running : 0n : op.amount;
51
- state.balances.dec(op.token, amount);
52
- state.collateralWithdrawn.inc(op.token, amount);
49
+ case "WithdrawCollateral":
50
+ applyWithdrawCollateral(state, op);
53
51
  break;
54
- }
55
52
  case "IncreaseBorrowedAmount":
56
- state.debt += op.amount;
57
- state.totalDebt += op.amount;
58
- state.balances.inc(op.token, op.amount);
53
+ applyIncreaseDebt(state, op);
59
54
  break;
60
- case "DecreaseBorrowedAmount": {
61
- const repaid = op.amount > state.totalDebt ? state.totalDebt : op.amount;
62
- state.balances.dec(op.token, repaid);
63
- state.totalDebt -= repaid;
64
- if (state.debt > state.totalDebt) {
65
- state.debt = state.totalDebt;
66
- }
55
+ case "DecreaseBorrowedAmount":
56
+ applyDecreaseDebt(state, op);
67
57
  break;
68
- }
69
58
  case "UpdateQuota":
70
59
  state.quotaChanges.push({ token: op.token, balance: op.change });
71
60
  break;
72
61
  case "StoreExpectedBalances":
73
62
  if (storeSeen) {
74
- throw new Error(
75
- "malformed multicall: duplicate storeExpectedBalances call"
76
- );
63
+ opError = {
64
+ code: import_types.ERROR_MALFORMED_BRACKET,
65
+ message: "duplicate storeExpectedBalances call"
66
+ };
77
67
  }
78
68
  storeSeen = true;
79
69
  inBracket = true;
@@ -83,32 +73,83 @@ function applyInnerOperations(sdk, multicall, state) {
83
73
  break;
84
74
  case "CompareBalances":
85
75
  if (!inBracket) {
86
- throw new Error(
87
- "malformed multicall: compareBalances without a preceding storeExpectedBalances"
88
- );
76
+ opError = {
77
+ code: import_types.ERROR_MALFORMED_BRACKET,
78
+ message: "compareBalances without a preceding storeExpectedBalances"
79
+ };
89
80
  }
90
81
  inBracket = false;
91
82
  break;
92
- case "Execute": {
93
- const adapter = sdk.getContract(op.adapter);
94
- if (inBracket) {
95
- if (!(adapter instanceof import_adapters.AbstractAdapterContract)) {
96
- throw new Error(
97
- `call to ${op.adapter} between storeExpectedBalances and compareBalances is not an adapter call`
98
- );
99
- }
100
- adapter.previewBalanceChanges(state.balances, op.calldata);
101
- } else if (isRWAShare(sdk, adapter)) {
102
- (0, import_applyRWAWrapUnwrap.applyRWAWrapUnwrap)(adapter, op.calldata, state.balances);
103
- }
83
+ case "Execute":
84
+ opError = applyExecute(sdk, op, inBracket, state.balances);
104
85
  break;
105
- }
106
86
  }
87
+ error ??= opError;
107
88
  }
108
89
  if (inBracket) {
109
- throw new Error(
110
- "malformed multicall: storeExpectedBalances without a matching compareBalances"
111
- );
90
+ error ??= {
91
+ code: import_types.ERROR_MALFORMED_BRACKET,
92
+ message: "storeExpectedBalances without a matching compareBalances"
93
+ };
94
+ }
95
+ return error;
96
+ }
97
+ function applyAddCollateral(state, op) {
98
+ state.collateralAdded.inc(op.token, op.amount);
99
+ state.balances.inc(op.token, op.amount);
100
+ }
101
+ function applyWithdrawCollateral(state, op) {
102
+ const running = state.balances.getOrZero(op.token);
103
+ const amount = op.amount === import_sdk.MAX_UINT256 ? running > 0n ? running : 0n : op.amount;
104
+ state.balances.dec(op.token, amount);
105
+ state.collateralWithdrawn.inc(op.token, amount);
106
+ }
107
+ function applyIncreaseDebt(state, op) {
108
+ state.debt += op.amount;
109
+ state.totalDebt += op.amount;
110
+ state.balances.inc(op.token, op.amount);
111
+ }
112
+ function applyDecreaseDebt(state, op) {
113
+ const repaid = op.amount > state.totalDebt ? state.totalDebt : op.amount;
114
+ state.balances.dec(op.token, repaid);
115
+ state.totalDebt -= repaid;
116
+ if (state.debt > state.totalDebt) {
117
+ state.debt = state.totalDebt;
118
+ }
119
+ }
120
+ function applyExecute(sdk, op, inBracket, balances) {
121
+ const adapter = sdk.getContract(op.adapter);
122
+ if (!inBracket) {
123
+ if (!isRWAShare(sdk, adapter)) {
124
+ return {
125
+ code: import_types.ERROR_ADAPTER_CALL_OUTSIDE_BRACKET,
126
+ message: `call to ${op.adapter} outside of a storeExpectedBalances/compareBalances bracket`
127
+ };
128
+ }
129
+ try {
130
+ (0, import_applyRWAWrapUnwrap.applyRWAWrapUnwrap)(adapter, op.calldata, balances);
131
+ return void 0;
132
+ } catch (e) {
133
+ return {
134
+ code: import_types.ERROR_UNPREVIEWABLE_RWA_WRAP_UNWRAP,
135
+ message: e instanceof Error ? e.message : String(e)
136
+ };
137
+ }
138
+ }
139
+ if (!(adapter instanceof import_adapters.AbstractAdapterContract)) {
140
+ return {
141
+ code: import_types.ERROR_NON_ADAPTER_CALL_IN_BRACKET,
142
+ message: `call to ${op.adapter} between storeExpectedBalances and compareBalances is not an adapter call`
143
+ };
144
+ }
145
+ try {
146
+ adapter.previewBalanceChanges(balances, op.calldata);
147
+ return void 0;
148
+ } catch (e) {
149
+ return {
150
+ code: import_types.ERROR_UNPREVIEWABLE_ADAPTER_CALL,
151
+ message: e instanceof Error ? e.message : String(e)
152
+ };
112
153
  }
113
154
  }
114
155
  function isRWAShare(sdk, adapter) {
@@ -118,24 +159,8 @@ function isRWAShare(sdk, adapter) {
118
159
  }
119
160
  return false;
120
161
  }
121
- function applyQuotaChanges(initialQuotas, changes) {
122
- const final = initialQuotas.clone();
123
- for (const { token, balance: change } of changes) {
124
- if (change === import_sdk.MIN_INT96) {
125
- final.upsert(token, 0n);
126
- } else {
127
- const next = final.getOrZero(token) + change;
128
- final.upsert(token, next > 0n ? next : 0n);
129
- }
130
- }
131
- return {
132
- quotas: final.toAssets(0n),
133
- quotasChange: final.difference(initialQuotas).toAssets()
134
- };
135
- }
136
162
  // Annotate the CommonJS export names for ESM import in node:
137
163
  0 && (module.exports = {
138
164
  applyInnerOperations,
139
- applyQuotaChanges,
140
165
  makeInnerOperationsState
141
166
  });
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var applyQuotaChanges_exports = {};
20
+ __export(applyQuotaChanges_exports, {
21
+ applyQuotaChanges: () => applyQuotaChanges
22
+ });
23
+ module.exports = __toCommonJS(applyQuotaChanges_exports);
24
+ var import_sdk = require("../../sdk/index.js");
25
+ function applyQuotaChanges(initialQuotas, changes) {
26
+ const final = initialQuotas.clone();
27
+ for (const { token, balance: change } of changes) {
28
+ if (change === import_sdk.MIN_INT96) {
29
+ final.upsert(token, 0n);
30
+ } else {
31
+ const next = final.getOrZero(token) + change;
32
+ final.upsert(token, next > 0n ? next : 0n);
33
+ }
34
+ }
35
+ return {
36
+ quotas: final.toAssets(0n),
37
+ quotasChange: final.difference(initialQuotas).toAssets()
38
+ };
39
+ }
40
+ // Annotate the CommonJS export names for ESM import in node:
41
+ 0 && (module.exports = {
42
+ applyQuotaChanges
43
+ });
@@ -18,24 +18,9 @@ var __copyProps = (to, from, except, desc) => {
18
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
19
  var errors_exports = {};
20
20
  __export(errors_exports, {
21
- InvalidTransactionValueError: () => InvalidTransactionValueError,
22
21
  UnsupportedOperationError: () => UnsupportedOperationError
23
22
  });
24
23
  module.exports = __toCommonJS(errors_exports);
25
- class InvalidTransactionValueError extends Error {
26
- /** Transaction `msg.value`. */
27
- value;
28
- /** Amount of wrapped native token added as collateral. */
29
- wethCollateral;
30
- constructor(value, wethCollateral) {
31
- super(
32
- `transaction value ${value} exceeds WETH collateral ${wethCollateral}`
33
- );
34
- this.name = "InvalidTransactionValueError";
35
- this.value = value;
36
- this.wethCollateral = wethCollateral;
37
- }
38
- }
39
24
  class UnsupportedOperationError extends Error {
40
25
  /** The parsed operation kind (the `operation` discriminant). */
41
26
  operation;
@@ -47,6 +32,5 @@ class UnsupportedOperationError extends Error {
47
32
  }
48
33
  // Annotate the CommonJS export names for ESM import in node:
49
34
  0 && (module.exports = {
50
- InvalidTransactionValueError,
51
35
  UnsupportedOperationError
52
36
  });
@@ -16,6 +16,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
16
16
  var preview_exports = {};
17
17
  module.exports = __toCommonJS(preview_exports);
18
18
  __reExport(preview_exports, require("./applyInnerOperations.js"), module.exports);
19
+ __reExport(preview_exports, require("./applyQuotaChanges.js"), module.exports);
19
20
  __reExport(preview_exports, require("./detectCloseOrRepay.js"), module.exports);
20
21
  __reExport(preview_exports, require("./errors.js"), module.exports);
21
22
  __reExport(preview_exports, require("./previewAdjustCreditAccount.js"), module.exports);
@@ -25,6 +26,7 @@ __reExport(preview_exports, require("./types.js"), module.exports);
25
26
  // Annotate the CommonJS export names for ESM import in node:
26
27
  0 && (module.exports = {
27
28
  ...require("./applyInnerOperations.js"),
29
+ ...require("./applyQuotaChanges.js"),
28
30
  ...require("./detectCloseOrRepay.js"),
29
31
  ...require("./errors.js"),
30
32
  ...require("./previewAdjustCreditAccount.js"),
@@ -23,6 +23,8 @@ __export(previewAdjustCreditAccount_exports, {
23
23
  module.exports = __toCommonJS(previewAdjustCreditAccount_exports);
24
24
  var import_sdk = require("../../sdk/index.js");
25
25
  var import_applyInnerOperations = require("./applyInnerOperations.js");
26
+ var import_applyQuotaChanges = require("./applyQuotaChanges.js");
27
+ var import_types = require("./types.js");
26
28
  var import_unwrapNativeCollateral = require("./unwrapNativeCollateral.js");
27
29
  async function previewAdjustCreditAccount(input, operation, options) {
28
30
  const { sdk, value = 0n } = input;
@@ -53,22 +55,30 @@ async function previewAdjustCreditAccount(input, operation, options) {
53
55
  state.balances = initialBalances.clone();
54
56
  state.debt = ca.debt;
55
57
  state.totalDebt = ca.debt + ca.accruedInterest + ca.accruedFees;
56
- (0, import_applyInnerOperations.applyInnerOperations)(sdk, operation.multicall, state);
57
- const collateralAdded = (0, import_unwrapNativeCollateral.unwrapNativeCollateral)(
58
+ let error = (0, import_applyInnerOperations.applyInnerOperations)(sdk, operation.multicall, state);
59
+ const { assets: collateralAdded, error: unwrapError } = (0, import_unwrapNativeCollateral.unwrapNativeCollateral)(
58
60
  state.collateralAdded.toAssets(),
59
61
  value,
60
62
  sdk.addressProvider.getAddress(import_sdk.AP_WETH_TOKEN, import_sdk.NO_VERSION)
61
63
  );
62
- const { quotas, quotasChange } = (0, import_applyInnerOperations.applyQuotaChanges)(
64
+ error ??= unwrapError;
65
+ const { quotas, quotasChange } = (0, import_applyQuotaChanges.applyQuotaChanges)(
63
66
  initialQuotas,
64
67
  state.quotaChanges
65
68
  );
66
69
  const assets = state.balances.toAssets(1n);
67
70
  const assetsChange = state.balances.difference(initialBalances).toAssets(1n);
68
- const totalValue = assets.reduce(
69
- (acc, { token, balance }) => acc + market.priceOracle.convert(token, market.underlying, balance),
70
- 0n
71
- );
71
+ const totalValue = assets.reduce((acc, { token, balance }) => {
72
+ try {
73
+ return acc + market.priceOracle.convert(token, market.underlying, balance);
74
+ } catch {
75
+ error ??= {
76
+ code: import_types.ERROR_UNPRICEABLE_TOKEN,
77
+ message: `cannot price token ${token}`
78
+ };
79
+ return acc;
80
+ }
81
+ }, 0n);
72
82
  return {
73
83
  operation: "AdjustCreditAccount",
74
84
  creditManager: operation.creditManager,
@@ -81,7 +91,8 @@ async function previewAdjustCreditAccount(input, operation, options) {
81
91
  quotas,
82
92
  quotasChange,
83
93
  assets,
84
- assetsChange
94
+ assetsChange,
95
+ error
85
96
  };
86
97
  }
87
98
  // Annotate the CommonJS export names for ESM import in node:
@@ -38,23 +38,31 @@ async function previewCloseCreditAccount(input, operation, permanent, options) {
38
38
  const market = sdk.marketRegister.findByCreditManager(
39
39
  operation.creditManager
40
40
  );
41
- const { state } = await replayMulticall(sdk, operation, options);
41
+ const { state, error } = await replayMulticall(sdk, operation, options);
42
42
  return {
43
43
  operation: "CloseCreditAccount",
44
44
  permanent,
45
45
  creditManager: operation.creditManager,
46
46
  creditAccount: operation.creditAccount,
47
- receivedAmount: state.collateralWithdrawn.getOrZero(market.underlying)
47
+ // On a malformed multicall the withdrawn amount depends on best-effort
48
+ // replayed balances and may be unreliable
49
+ receivedAmount: state.collateralWithdrawn.getOrZero(market.underlying),
50
+ error
48
51
  };
49
52
  }
50
53
  async function previewRepayCreditAccount(input, operation, permanent, options) {
51
54
  const { sdk, value = 0n } = input;
52
- const { ca, state } = await replayMulticall(sdk, operation, options);
53
- const collateralAdded = (0, import_unwrapNativeCollateral.unwrapNativeCollateral)(
55
+ const {
56
+ ca,
57
+ state,
58
+ error: replayError
59
+ } = await replayMulticall(sdk, operation, options);
60
+ const { assets: collateralAdded, error: unwrapError } = (0, import_unwrapNativeCollateral.unwrapNativeCollateral)(
54
61
  state.collateralAdded.toAssets(),
55
62
  value,
56
63
  sdk.addressProvider.getAddress(import_sdk.AP_WETH_TOKEN, import_sdk.NO_VERSION)
57
64
  );
65
+ const error = replayError ?? unwrapError;
58
66
  const initialTotalDebt = ca.debt + ca.accruedInterest + ca.accruedFees;
59
67
  return {
60
68
  operation: "RepayCreditAccount",
@@ -63,7 +71,10 @@ async function previewRepayCreditAccount(input, operation, permanent, options) {
63
71
  creditAccount: operation.creditAccount,
64
72
  collateralAdded,
65
73
  debtRepaid: initialTotalDebt - state.totalDebt,
66
- collateralWithdrawn: state.collateralWithdrawn.toAssets()
74
+ // On a malformed multicall the MAX_UINT256 withdrawal sentinel resolves
75
+ // against best-effort replayed balances and may be unreliable
76
+ collateralWithdrawn: state.collateralWithdrawn.toAssets(),
77
+ error
67
78
  };
68
79
  }
69
80
  async function replayMulticall(sdk, operation, options) {
@@ -85,8 +96,8 @@ async function replayMulticall(sdk, operation, options) {
85
96
  }
86
97
  state.debt = ca.debt;
87
98
  state.totalDebt = ca.debt + ca.accruedInterest + ca.accruedFees;
88
- (0, import_applyInnerOperations.applyInnerOperations)(sdk, operation.multicall, state);
89
- return { ca, state };
99
+ const error = (0, import_applyInnerOperations.applyInnerOperations)(sdk, operation.multicall, state);
100
+ return { ca, state, error };
90
101
  }
91
102
  // Annotate the CommonJS export names for ESM import in node:
92
103
  0 && (module.exports = {
@@ -23,6 +23,7 @@ __export(previewOpenCreditAccount_exports, {
23
23
  module.exports = __toCommonJS(previewOpenCreditAccount_exports);
24
24
  var import_sdk = require("../../sdk/index.js");
25
25
  var import_applyInnerOperations = require("./applyInnerOperations.js");
26
+ var import_types = require("./types.js");
26
27
  var import_unwrapNativeCollateral = require("./unwrapNativeCollateral.js");
27
28
  function previewOpenCreditAccount(input, operation) {
28
29
  const { sdk, value = 0n } = input;
@@ -30,15 +31,25 @@ function previewOpenCreditAccount(input, operation) {
30
31
  operation.creditManager
31
32
  );
32
33
  const state = (0, import_applyInnerOperations.makeInnerOperationsState)();
33
- (0, import_applyInnerOperations.applyInnerOperations)(sdk, operation.multicall, state);
34
- const collateralValue = state.collateralAdded.sum(
35
- (token, balance) => market.priceOracle.convert(token, market.underlying, balance)
36
- );
37
- const collateral = (0, import_unwrapNativeCollateral.unwrapNativeCollateral)(
34
+ let error = (0, import_applyInnerOperations.applyInnerOperations)(sdk, operation.multicall, state);
35
+ let priceError;
36
+ const collateralValue = state.collateralAdded.sum((token, balance) => {
37
+ try {
38
+ return market.priceOracle.convert(token, market.underlying, balance);
39
+ } catch {
40
+ priceError ??= {
41
+ code: import_types.ERROR_UNPRICEABLE_TOKEN,
42
+ message: `cannot price token ${token}`
43
+ };
44
+ return 0n;
45
+ }
46
+ });
47
+ const { assets: collateral, error: unwrapError } = (0, import_unwrapNativeCollateral.unwrapNativeCollateral)(
38
48
  state.collateralAdded.toAssets(),
39
49
  value,
40
50
  sdk.addressProvider.getAddress(import_sdk.AP_WETH_TOKEN, import_sdk.NO_VERSION)
41
51
  );
52
+ error ??= unwrapError ?? priceError;
42
53
  const assets = state.balances.toAssets(1n);
43
54
  return {
44
55
  operation: operation.operation,
@@ -49,7 +60,8 @@ function previewOpenCreditAccount(input, operation) {
49
60
  debt: state.debt,
50
61
  // On opening, initial quotas are zero, so the raw changes are the quotas.
51
62
  quotas: state.quotaChanges,
52
- assets
63
+ assets,
64
+ error
53
65
  };
54
66
  }
55
67
  function inferTargetAsset(multicall, balances) {
@@ -3,6 +3,10 @@ var __defProp = Object.defineProperty;
3
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
5
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
6
10
  var __copyProps = (to, from, except, desc) => {
7
11
  if (from && typeof from === "object" || typeof from === "function") {
8
12
  for (let key of __getOwnPropNames(from))
@@ -13,4 +17,30 @@ var __copyProps = (to, from, except, desc) => {
13
17
  };
14
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
15
19
  var types_exports = {};
20
+ __export(types_exports, {
21
+ ERROR_ADAPTER_CALL_OUTSIDE_BRACKET: () => ERROR_ADAPTER_CALL_OUTSIDE_BRACKET,
22
+ ERROR_INVALID_TRANSACTION_VALUE: () => ERROR_INVALID_TRANSACTION_VALUE,
23
+ ERROR_MALFORMED_BRACKET: () => ERROR_MALFORMED_BRACKET,
24
+ ERROR_NON_ADAPTER_CALL_IN_BRACKET: () => ERROR_NON_ADAPTER_CALL_IN_BRACKET,
25
+ ERROR_UNPREVIEWABLE_ADAPTER_CALL: () => ERROR_UNPREVIEWABLE_ADAPTER_CALL,
26
+ ERROR_UNPREVIEWABLE_RWA_WRAP_UNWRAP: () => ERROR_UNPREVIEWABLE_RWA_WRAP_UNWRAP,
27
+ ERROR_UNPRICEABLE_TOKEN: () => ERROR_UNPRICEABLE_TOKEN
28
+ });
16
29
  module.exports = __toCommonJS(types_exports);
30
+ const ERROR_MALFORMED_BRACKET = 1001;
31
+ const ERROR_ADAPTER_CALL_OUTSIDE_BRACKET = 1002;
32
+ const ERROR_NON_ADAPTER_CALL_IN_BRACKET = 1003;
33
+ const ERROR_UNPREVIEWABLE_ADAPTER_CALL = 1004;
34
+ const ERROR_UNPREVIEWABLE_RWA_WRAP_UNWRAP = 1005;
35
+ const ERROR_INVALID_TRANSACTION_VALUE = 1006;
36
+ const ERROR_UNPRICEABLE_TOKEN = 2001;
37
+ // Annotate the CommonJS export names for ESM import in node:
38
+ 0 && (module.exports = {
39
+ ERROR_ADAPTER_CALL_OUTSIDE_BRACKET,
40
+ ERROR_INVALID_TRANSACTION_VALUE,
41
+ ERROR_MALFORMED_BRACKET,
42
+ ERROR_NON_ADAPTER_CALL_IN_BRACKET,
43
+ ERROR_UNPREVIEWABLE_ADAPTER_CALL,
44
+ ERROR_UNPREVIEWABLE_RWA_WRAP_UNWRAP,
45
+ ERROR_UNPRICEABLE_TOKEN
46
+ });
@@ -22,22 +22,28 @@ __export(unwrapNativeCollateral_exports, {
22
22
  });
23
23
  module.exports = __toCommonJS(unwrapNativeCollateral_exports);
24
24
  var import_sdk = require("../../sdk/index.js");
25
- var import_errors = require("./errors.js");
25
+ var import_types = require("./types.js");
26
26
  function unwrapNativeCollateral(collateral, nativeAmount, wethToken) {
27
27
  if (nativeAmount === 0n) {
28
- return collateral;
28
+ return { assets: collateral };
29
29
  }
30
30
  const balances = new import_sdk.AssetsMap(collateral);
31
31
  const wethBalance = balances.get(wethToken) ?? 0n;
32
32
  if (wethBalance < nativeAmount) {
33
- throw new import_errors.InvalidTransactionValueError(nativeAmount, wethBalance);
33
+ return {
34
+ assets: collateral,
35
+ error: {
36
+ code: import_types.ERROR_INVALID_TRANSACTION_VALUE,
37
+ message: `transaction value ${nativeAmount} exceeds WETH collateral ${wethBalance}`
38
+ }
39
+ };
34
40
  }
35
41
  balances.upsert(
36
42
  wethToken,
37
43
  wethBalance === nativeAmount ? void 0 : wethBalance - nativeAmount
38
44
  );
39
45
  balances.inc(import_sdk.NATIVE_ADDRESS, nativeAmount);
40
- return balances.toAssets();
46
+ return { assets: balances.toAssets() };
41
47
  }
42
48
  // Annotate the CommonJS export names for ESM import in node:
43
49
  0 && (module.exports = {
@@ -538,13 +538,10 @@ class OnchainSDK extends import_base.ChainContractsRegister {
538
538
  /**
539
539
  * Lazily returns a withdrawal compressor contract for the current chain.
540
540
  *
541
- * @param version - Desired compressor version; when omitted, the latest
542
- * version supported on the current chain is used.
543
- * @throws If no withdrawal compressor of the given version is supported
544
- * on the current chain.
541
+ * @throws If no withdrawal compressor is supported on the current chain.
545
542
  **/
546
- withdrawalCompressor(version) {
547
- return (0, import_accounts.createWithdrawalCompressor)(this, version);
543
+ get withdrawalCompressor() {
544
+ return (0, import_accounts.createWithdrawalCompressor)(this);
548
545
  }
549
546
  }
550
547
  // Annotate the CommonJS export names for ESM import in node: