@gearbox-protocol/sdk 14.12.0-next.12 → 14.12.0-next.14

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 (75) hide show
  1. package/dist/cjs/abi/IWithdrawalCompressorV312.js +186 -0
  2. package/dist/cjs/preview/parse/parseRWAFactoryOperationCalldata.js +13 -12
  3. package/dist/cjs/preview/parse/types.js +1 -1
  4. package/dist/cjs/preview/prerequisites/AllowancePrerequisite.js +10 -16
  5. package/dist/cjs/preview/prerequisites/BalancePrerequisite.js +17 -33
  6. package/dist/cjs/preview/prerequisites/Prerequisite.js +13 -18
  7. package/dist/cjs/preview/prerequisites/RWAOpenRequirementsPrerequisite.js +36 -15
  8. package/dist/cjs/preview/prerequisites/buildCollateralPrerequisites.js +57 -0
  9. package/dist/cjs/preview/prerequisites/{prepareAction.js → buildPartialLiquidationPrerequisites.js} +18 -24
  10. package/dist/cjs/preview/prerequisites/buildPoolPrerequisites.js +125 -0
  11. package/dist/cjs/preview/prerequisites/buildRWAPrerequisites.js +57 -0
  12. package/dist/cjs/preview/prerequisites/checkPrerequisites.js +85 -0
  13. package/dist/cjs/preview/prerequisites/helpers.js +47 -0
  14. package/dist/cjs/preview/prerequisites/index.js +2 -6
  15. package/dist/cjs/preview/preview/detectCloseOrRepay.js +51 -0
  16. package/dist/cjs/preview/preview/index.js +4 -0
  17. package/dist/cjs/preview/preview/previewCloseOrRepay.js +94 -0
  18. package/dist/cjs/preview/preview/previewOperation.js +10 -2
  19. package/dist/cjs/sdk/market/rwa/securitize/SecuritizeRWAFactory.js +26 -4
  20. package/dist/cjs/sdk/utils/AddressSet.js +7 -0
  21. package/dist/esm/abi/IWithdrawalCompressorV312.js +162 -0
  22. package/dist/esm/preview/parse/parseRWAFactoryOperationCalldata.js +13 -12
  23. package/dist/esm/preview/parse/types.js +1 -1
  24. package/dist/esm/preview/prerequisites/AllowancePrerequisite.js +11 -20
  25. package/dist/esm/preview/prerequisites/BalancePrerequisite.js +18 -37
  26. package/dist/esm/preview/prerequisites/Prerequisite.js +13 -20
  27. package/dist/esm/preview/prerequisites/RWAOpenRequirementsPrerequisite.js +36 -15
  28. package/dist/esm/preview/prerequisites/buildCollateralPrerequisites.js +35 -0
  29. package/dist/esm/preview/prerequisites/buildPartialLiquidationPrerequisites.js +18 -0
  30. package/dist/esm/preview/prerequisites/buildPoolPrerequisites.js +101 -0
  31. package/dist/esm/preview/prerequisites/buildRWAPrerequisites.js +33 -0
  32. package/dist/esm/preview/prerequisites/checkPrerequisites.js +63 -0
  33. package/dist/esm/preview/prerequisites/helpers.js +23 -0
  34. package/dist/esm/preview/prerequisites/index.js +1 -3
  35. package/dist/esm/preview/preview/detectCloseOrRepay.js +26 -0
  36. package/dist/esm/preview/preview/index.js +2 -0
  37. package/dist/esm/preview/preview/previewCloseOrRepay.js +76 -0
  38. package/dist/esm/preview/preview/previewOperation.js +10 -2
  39. package/dist/esm/sdk/market/rwa/securitize/SecuritizeRWAFactory.js +26 -4
  40. package/dist/esm/sdk/utils/AddressSet.js +7 -0
  41. package/dist/types/abi/IWithdrawalCompressorV312.d.ts +216 -0
  42. package/dist/types/preview/parse/parseRWAFactoryOperationCalldata.d.ts +1 -1
  43. package/dist/types/preview/parse/types-rwa.d.ts +11 -23
  44. package/dist/types/preview/prerequisites/AllowancePrerequisite.d.ts +4 -5
  45. package/dist/types/preview/prerequisites/BalancePrerequisite.d.ts +4 -6
  46. package/dist/types/preview/prerequisites/Prerequisite.d.ts +16 -40
  47. package/dist/types/preview/prerequisites/RWAOpenRequirementsPrerequisite.d.ts +22 -16
  48. package/dist/types/preview/prerequisites/buildCollateralPrerequisites.d.ts +14 -0
  49. package/dist/types/preview/prerequisites/buildPartialLiquidationPrerequisites.d.ts +8 -0
  50. package/dist/types/preview/prerequisites/buildPoolPrerequisites.d.ts +8 -0
  51. package/dist/types/preview/prerequisites/buildRWAPrerequisites.d.ts +21 -0
  52. package/dist/types/preview/prerequisites/checkPrerequisites.d.ts +27 -0
  53. package/dist/types/preview/prerequisites/helpers.d.ts +15 -0
  54. package/dist/types/preview/prerequisites/index.d.ts +1 -3
  55. package/dist/types/preview/prerequisites/types.d.ts +21 -2
  56. package/dist/types/preview/preview/detectCloseOrRepay.d.ts +18 -0
  57. package/dist/types/preview/preview/index.d.ts +2 -0
  58. package/dist/types/preview/preview/previewAdjustCreditAccount.d.ts +2 -2
  59. package/dist/types/preview/preview/previewCloseOrRepay.d.ts +10 -0
  60. package/dist/types/preview/preview/previewOpenCreditAccount.d.ts +2 -2
  61. package/dist/types/preview/preview/types.d.ts +60 -3
  62. package/dist/types/sdk/accounts/types.d.ts +3 -3
  63. package/dist/types/sdk/market/rwa/securitize/SecuritizeRWAFactory.d.ts +11 -3
  64. package/dist/types/sdk/market/rwa/securitize/types.d.ts +18 -2
  65. package/dist/types/sdk/market/rwa/types.d.ts +26 -9
  66. package/dist/types/sdk/utils/AddressSet.d.ts +1 -0
  67. package/package.json +1 -1
  68. package/dist/cjs/preview/prerequisites/buildPrerequisites.js +0 -297
  69. package/dist/cjs/preview/prerequisites/runPrerequisites.js +0 -71
  70. package/dist/esm/preview/prerequisites/buildPrerequisites.js +0 -279
  71. package/dist/esm/preview/prerequisites/prepareAction.js +0 -23
  72. package/dist/esm/preview/prerequisites/runPrerequisites.js +0 -47
  73. package/dist/types/preview/prerequisites/buildPrerequisites.d.ts +0 -25
  74. package/dist/types/preview/prerequisites/prepareAction.d.ts +0 -52
  75. package/dist/types/preview/prerequisites/runPrerequisites.d.ts +0 -12
@@ -0,0 +1,162 @@
1
+ const iWithdrawalCompressorV312Abi = [
2
+ {
3
+ type: "function",
4
+ name: "contractType",
5
+ inputs: [],
6
+ outputs: [{ name: "", type: "bytes32", internalType: "bytes32" }],
7
+ stateMutability: "view"
8
+ },
9
+ {
10
+ type: "function",
11
+ name: "getCurrentWithdrawals",
12
+ inputs: [
13
+ { name: "creditAccount", type: "address", internalType: "address" }
14
+ ],
15
+ outputs: [
16
+ {
17
+ name: "",
18
+ type: "tuple[]",
19
+ internalType: "struct ClaimableWithdrawal[]",
20
+ components: [
21
+ { name: "token", type: "address", internalType: "address" },
22
+ {
23
+ name: "withdrawalPhantomToken",
24
+ type: "address",
25
+ internalType: "address"
26
+ },
27
+ {
28
+ name: "withdrawalTokenSpent",
29
+ type: "uint256",
30
+ internalType: "uint256"
31
+ },
32
+ {
33
+ name: "outputs",
34
+ type: "tuple[]",
35
+ internalType: "struct WithdrawalOutput[]",
36
+ components: [
37
+ { name: "token", type: "address", internalType: "address" },
38
+ { name: "isDelayed", type: "bool", internalType: "bool" },
39
+ { name: "amount", type: "uint256", internalType: "uint256" }
40
+ ]
41
+ },
42
+ {
43
+ name: "claimCalls",
44
+ type: "tuple[]",
45
+ internalType: "struct MultiCall[]",
46
+ components: [
47
+ { name: "target", type: "address", internalType: "address" },
48
+ { name: "callData", type: "bytes", internalType: "bytes" }
49
+ ]
50
+ },
51
+ { name: "extraData", type: "bytes", internalType: "bytes" }
52
+ ]
53
+ },
54
+ {
55
+ name: "",
56
+ type: "tuple[]",
57
+ internalType: "struct PendingWithdrawal[]",
58
+ components: [
59
+ { name: "token", type: "address", internalType: "address" },
60
+ {
61
+ name: "withdrawalPhantomToken",
62
+ type: "address",
63
+ internalType: "address"
64
+ },
65
+ {
66
+ name: "expectedOutputs",
67
+ type: "tuple[]",
68
+ internalType: "struct WithdrawalOutput[]",
69
+ components: [
70
+ { name: "token", type: "address", internalType: "address" },
71
+ { name: "isDelayed", type: "bool", internalType: "bool" },
72
+ { name: "amount", type: "uint256", internalType: "uint256" }
73
+ ]
74
+ },
75
+ { name: "claimableAt", type: "uint256", internalType: "uint256" }
76
+ ]
77
+ }
78
+ ],
79
+ stateMutability: "view"
80
+ },
81
+ {
82
+ type: "function",
83
+ name: "getWithdrawableAssets",
84
+ inputs: [
85
+ { name: "creditManager", type: "address", internalType: "address" }
86
+ ],
87
+ outputs: [
88
+ {
89
+ name: "",
90
+ type: "tuple[]",
91
+ internalType: "struct WithdrawableAsset[]",
92
+ components: [
93
+ { name: "token", type: "address", internalType: "address" },
94
+ {
95
+ name: "withdrawalPhantomToken",
96
+ type: "address",
97
+ internalType: "address"
98
+ },
99
+ { name: "underlying", type: "address", internalType: "address" },
100
+ {
101
+ name: "withdrawalLength",
102
+ type: "uint256",
103
+ internalType: "uint256"
104
+ },
105
+ { name: "maxWithdrawals", type: "uint256", internalType: "uint256" }
106
+ ]
107
+ }
108
+ ],
109
+ stateMutability: "view"
110
+ },
111
+ {
112
+ type: "function",
113
+ name: "getWithdrawalRequestResult",
114
+ inputs: [
115
+ { name: "creditAccount", type: "address", internalType: "address" },
116
+ { name: "token", type: "address", internalType: "address" },
117
+ { name: "amount", type: "uint256", internalType: "uint256" }
118
+ ],
119
+ outputs: [
120
+ {
121
+ name: "",
122
+ type: "tuple",
123
+ internalType: "struct RequestableWithdrawal",
124
+ components: [
125
+ { name: "token", type: "address", internalType: "address" },
126
+ { name: "amountIn", type: "uint256", internalType: "uint256" },
127
+ {
128
+ name: "outputs",
129
+ type: "tuple[]",
130
+ internalType: "struct WithdrawalOutput[]",
131
+ components: [
132
+ { name: "token", type: "address", internalType: "address" },
133
+ { name: "isDelayed", type: "bool", internalType: "bool" },
134
+ { name: "amount", type: "uint256", internalType: "uint256" }
135
+ ]
136
+ },
137
+ {
138
+ name: "requestCalls",
139
+ type: "tuple[]",
140
+ internalType: "struct MultiCall[]",
141
+ components: [
142
+ { name: "target", type: "address", internalType: "address" },
143
+ { name: "callData", type: "bytes", internalType: "bytes" }
144
+ ]
145
+ },
146
+ { name: "claimableAt", type: "uint256", internalType: "uint256" }
147
+ ]
148
+ }
149
+ ],
150
+ stateMutability: "view"
151
+ },
152
+ {
153
+ type: "function",
154
+ name: "version",
155
+ inputs: [],
156
+ outputs: [{ name: "", type: "uint256", internalType: "uint256" }],
157
+ stateMutability: "view"
158
+ }
159
+ ];
160
+ export {
161
+ iWithdrawalCompressorV312Abi
162
+ };
@@ -21,12 +21,15 @@ function parseSecuritizeOperationCalldata(props) {
21
21
  const functionName = parsed.functionName.split("(")[0];
22
22
  const { rawArgs } = parsed;
23
23
  const innerCalls = rawArgs.calls ?? [];
24
- const tokensToRegister = [
25
- ...rawArgs.tokensToRegister ?? []
26
- ];
27
- const signaturesToCache = [
28
- ...rawArgs.signaturesToCache ?? []
29
- ];
24
+ const args = {
25
+ type: RWA_FACTORY_SECURITIZE,
26
+ tokensToRegister: [
27
+ ...rawArgs.tokensToRegister ?? []
28
+ ],
29
+ signaturesToCache: [
30
+ ...rawArgs.signaturesToCache ?? []
31
+ ]
32
+ };
30
33
  const suite = functionName === "openCreditAccount" ? sdk.marketRegister.findCreditManager(rawArgs.creditManager) : resolveSuiteForMulticall(sdk, factory, innerCalls);
31
34
  const metadata = {
32
35
  factory: factory.address,
@@ -41,19 +44,17 @@ function parseSecuritizeOperationCalldata(props) {
41
44
  case "openCreditAccount":
42
45
  return {
43
46
  ...metadata,
44
- operation: "SecuritizeOpenCreditAccount",
47
+ operation: "RWAOpenCreditAccount",
45
48
  multicall,
46
- tokensToRegister,
47
- signaturesToCache
49
+ args
48
50
  };
49
51
  case "multicall":
50
52
  return {
51
53
  ...metadata,
52
- operation: "SecuritizeMulticall",
54
+ operation: "RWAMulticall",
53
55
  creditAccount: rawArgs.creditAccount,
54
56
  multicall,
55
- tokensToRegister,
56
- signaturesToCache
57
+ args
57
58
  };
58
59
  default:
59
60
  throw new Error(
@@ -6,7 +6,7 @@ function isPoolOperation(tx) {
6
6
  return tx.operation === "Deposit" || tx.operation === "Mint" || tx.operation === "Withdraw" || tx.operation === "Redeem";
7
7
  }
8
8
  function isRWAOperation(tx) {
9
- return tx.operation === "SecuritizeOpenCreditAccount" || tx.operation === "SecuritizeMulticall";
9
+ return tx.operation === "RWAOpenCreditAccount" || tx.operation === "RWAMulticall";
10
10
  }
11
11
  export {
12
12
  isPoolOperation,
@@ -1,8 +1,5 @@
1
1
  import { erc20Abi } from "viem";
2
- import {
3
- Prerequisite,
4
- toPrerequisiteError
5
- } from "./Prerequisite.js";
2
+ import { Prerequisite } from "./Prerequisite.js";
6
3
  class AllowancePrerequisite extends Prerequisite {
7
4
  #id;
8
5
  #title;
@@ -30,22 +27,16 @@ class AllowancePrerequisite extends Prerequisite {
30
27
  get detail() {
31
28
  return this.#detail;
32
29
  }
33
- calls() {
34
- return [
35
- {
36
- address: this.#detail.token,
37
- abi: erc20Abi,
38
- functionName: "allowance",
39
- args: [this.#detail.owner, this.#detail.spender]
40
- }
41
- ];
42
- }
43
- resolve(slice) {
44
- const res = slice[0];
45
- if (!res || res.status === "failure") {
46
- return this.errorResult(toPrerequisiteError(res?.error));
47
- }
48
- const actual = res.result;
30
+ async check(ctx) {
31
+ const actual = await ctx.sdk.client.readContract({
32
+ address: this.#detail.token,
33
+ abi: erc20Abi,
34
+ functionName: "allowance",
35
+ args: [this.#detail.owner, this.#detail.spender],
36
+ // `undefined` lets viem read at `latest`; `blockNumber` is only set for
37
+ // testnet forks pinned to a specific block.
38
+ blockNumber: ctx.blockNumber
39
+ });
49
40
  return this.satisfiedResult(actual >= this.#detail.required, {
50
41
  ...this.#detail,
51
42
  actual
@@ -1,10 +1,6 @@
1
1
  import { erc20Abi } from "viem";
2
- import { iMulticall3Abi } from "../../abi/iMulticall3.js";
3
2
  import { hexEq, NATIVE_ADDRESS } from "../../sdk/index.js";
4
- import {
5
- Prerequisite,
6
- toPrerequisiteError
7
- } from "./Prerequisite.js";
3
+ import { Prerequisite } from "./Prerequisite.js";
8
4
  class BalancePrerequisite extends Prerequisite {
9
5
  #id;
10
6
  #title;
@@ -31,43 +27,28 @@ class BalancePrerequisite extends Prerequisite {
31
27
  get detail() {
32
28
  return this.#detail;
33
29
  }
34
- calls(ctx) {
35
- if (hexEq(this.#detail.token, NATIVE_ADDRESS)) {
36
- const multicall3 = ctx.sdk.client.chain.contracts?.multicall3?.address;
37
- if (!multicall3) {
38
- throw new Error(
39
- "multicall3 is not configured for this chain, cannot read native balance"
40
- );
41
- }
42
- return [
43
- {
44
- address: multicall3,
45
- abi: iMulticall3Abi,
46
- functionName: "getEthBalance",
47
- args: [this.#detail.owner]
48
- }
49
- ];
50
- }
51
- return [
52
- {
53
- address: this.#detail.token,
54
- abi: erc20Abi,
55
- functionName: "balanceOf",
56
- args: [this.#detail.owner]
57
- }
58
- ];
59
- }
60
- resolve(slice) {
61
- const res = slice[0];
62
- if (!res || res.status === "failure") {
63
- return this.errorResult(toPrerequisiteError(res?.error));
64
- }
65
- const actual = res.result;
30
+ async check(ctx) {
31
+ const actual = await this.#readBalance(ctx);
66
32
  return this.satisfiedResult(actual >= this.#detail.required, {
67
33
  ...this.#detail,
68
34
  actual
69
35
  });
70
36
  }
37
+ async #readBalance(ctx) {
38
+ if (hexEq(this.#detail.token, NATIVE_ADDRESS)) {
39
+ return ctx.sdk.client.getBalance({
40
+ address: this.#detail.owner,
41
+ blockNumber: ctx.blockNumber
42
+ });
43
+ }
44
+ return ctx.sdk.client.readContract({
45
+ address: this.#detail.token,
46
+ abi: erc20Abi,
47
+ functionName: "balanceOf",
48
+ args: [this.#detail.owner],
49
+ blockNumber: ctx.blockNumber
50
+ });
51
+ }
71
52
  }
72
53
  export {
73
54
  BalancePrerequisite
@@ -1,7 +1,4 @@
1
- import {
2
- BaseError,
3
- ContractFunctionRevertedError
4
- } from "viem";
1
+ import { BaseError, ContractFunctionRevertedError } from "viem";
5
2
  function toPrerequisiteError(cause) {
6
3
  if (cause instanceof BaseError) {
7
4
  const reverted = cause.walk(
@@ -22,19 +19,16 @@ function toPrerequisiteError(cause) {
22
19
  }
23
20
  class Prerequisite {
24
21
  /**
25
- * Verifies this prerequisite on its own using an `allowFailure` multicall.
26
- * Prefer `verifyPrerequisites` to batch several checks into one round-trip.
22
+ * Verifies this prerequisite. Never rejects: any error thrown by
23
+ * {@link check} (revert, RPC/network failure, compressor error) is
24
+ * normalized into an `error` result.
27
25
  */
28
26
  async verify(ctx) {
29
- const calls = this.calls(ctx);
30
- const results = await ctx.sdk.client.multicall({
31
- allowFailure: true,
32
- contracts: calls,
33
- // `undefined` lets viem read at `latest`; `blockNumber` is only set for
34
- // testnet forks pinned to a specific block.
35
- blockNumber: ctx.blockNumber
36
- });
37
- return this.resolve(results);
27
+ try {
28
+ return await this.check(ctx);
29
+ } catch (cause) {
30
+ return this.errorResult(cause);
31
+ }
38
32
  }
39
33
  /** Builds a successful result; `detail` carries the on-chain `actual`. */
40
34
  satisfiedResult(satisfied, detail) {
@@ -46,18 +40,17 @@ class Prerequisite {
46
40
  satisfied
47
41
  };
48
42
  }
49
- /** Builds a failed result from a decoded read error. */
50
- errorResult(error) {
43
+ /** Builds a failed result from a raw read error (normalized internally). */
44
+ errorResult(cause) {
51
45
  return {
52
46
  id: this.id,
53
47
  kind: this.kind,
54
48
  title: this.title,
55
49
  detail: this.detail,
56
- error
50
+ error: toPrerequisiteError(cause)
57
51
  };
58
52
  }
59
53
  }
60
54
  export {
61
- Prerequisite,
62
- toPrerequisiteError
55
+ Prerequisite
63
56
  };
@@ -3,11 +3,17 @@ class RWAOpenRequirementsPrerequisite extends Prerequisite {
3
3
  #id;
4
4
  #title;
5
5
  #detail;
6
+ #providedArgs;
6
7
  constructor(props) {
7
8
  super();
8
9
  this.#id = props.id ?? `rwaOpenRequirements:${props.factory}:${props.token}`;
9
10
  this.#title = props.title ?? "RWA account requirements fulfilled";
10
- this.#detail = props.requirements;
11
+ this.#detail = {
12
+ token: props.token,
13
+ creditManager: props.creditManager,
14
+ factory: props.factory
15
+ };
16
+ this.#providedArgs = props.providedArgs;
11
17
  }
12
18
  get id() {
13
19
  return this.#id;
@@ -21,20 +27,35 @@ class RWAOpenRequirementsPrerequisite extends Prerequisite {
21
27
  get detail() {
22
28
  return this.#detail;
23
29
  }
24
- /** Pre-resolved: no on-chain reads to contribute. */
25
- calls() {
26
- return [];
27
- }
28
- resolve() {
29
- return this.satisfiedResult(this.#satisfied(), this.#detail);
30
- }
31
- /**
32
- * Satisfied when the borrower has nothing left to do: no tokens pending
33
- * issuer-side registration and no messages left to sign.
34
- */
35
- #satisfied() {
36
- const { securitizeTokensToRegister, requiredSignatures } = this.#detail;
37
- return securitizeTokensToRegister.length === 0 && requiredSignatures.length === 0;
30
+ async check(ctx) {
31
+ const requirements = await ctx.sdk.accounts.getOpenAccountRequirements(
32
+ ctx.wallet,
33
+ this.#detail.creditManager,
34
+ { tokenOutAddress: this.#detail.token }
35
+ );
36
+ if (!requirements) {
37
+ return this.satisfiedResult(true, this.#detail);
38
+ }
39
+ const { rwaFactory } = ctx.sdk.marketRegister.findByCreditManager(
40
+ this.#detail.creditManager
41
+ );
42
+ if (!rwaFactory) {
43
+ throw new Error(
44
+ `no RWA factory found for credit manager ${this.#detail.creditManager}`
45
+ );
46
+ }
47
+ const missing = rwaFactory.getMissingRequirements(
48
+ requirements,
49
+ this.#providedArgs
50
+ );
51
+ return this.satisfiedResult(
52
+ !missing && requirements.securitizeTokensToRegister.length === 0,
53
+ {
54
+ ...this.#detail,
55
+ requirements,
56
+ missing
57
+ }
58
+ );
38
59
  }
39
60
  }
40
61
  export {
@@ -0,0 +1,35 @@
1
+ import {
2
+ AssetsMap
3
+ } from "../../sdk/index.js";
4
+ import { allowanceAndBalance } from "./helpers.js";
5
+ async function buildCollateralPrerequisites(spenderOptions, multicall, ctx) {
6
+ const { sdk, wallet } = ctx;
7
+ const required = new AssetsMap();
8
+ for (const op of multicall) {
9
+ if (op.operation !== "AddCollateral" || op.amount === 0n) {
10
+ continue;
11
+ }
12
+ required.inc(op.token, op.amount);
13
+ }
14
+ if (required.size === 0) {
15
+ return [];
16
+ }
17
+ const spender = await sdk.accounts.getApprovalAddress(spenderOptions);
18
+ const prereqs = [];
19
+ for (const [token, amount] of required.entries()) {
20
+ prereqs.push(
21
+ ...allowanceAndBalance({
22
+ token,
23
+ owner: wallet,
24
+ spender,
25
+ required: amount,
26
+ allowanceTitle: "Collateral approved",
27
+ balanceTitle: "Sufficient collateral balance"
28
+ })
29
+ );
30
+ }
31
+ return prereqs;
32
+ }
33
+ export {
34
+ buildCollateralPrerequisites
35
+ };
@@ -0,0 +1,18 @@
1
+ import { allowanceAndBalance } from "./helpers.js";
2
+ function buildPartialLiquidationPrerequisites(tx, ctx) {
3
+ if (tx.repaidAmount === 0n) {
4
+ return [];
5
+ }
6
+ const suite = ctx.sdk.marketRegister.findByCreditManager(tx.creditManager);
7
+ return allowanceAndBalance({
8
+ token: suite.underlying,
9
+ owner: ctx.wallet,
10
+ spender: tx.creditManager,
11
+ required: tx.repaidAmount,
12
+ allowanceTitle: "Underlying approved to credit manager",
13
+ balanceTitle: "Sufficient underlying balance"
14
+ });
15
+ }
16
+ export {
17
+ buildPartialLiquidationPrerequisites
18
+ };
@@ -0,0 +1,101 @@
1
+ import { isAddressEqual } from "viem";
2
+ import { NATIVE_ADDRESS } from "../../sdk/index.js";
3
+ import { AllowancePrerequisite } from "./AllowancePrerequisite.js";
4
+ import { BalancePrerequisite } from "./BalancePrerequisite.js";
5
+ import { allowanceAndBalance } from "./helpers.js";
6
+ function buildPoolPrerequisites(tx, wallet) {
7
+ switch (tx.operation) {
8
+ // Deposit and Mint both pull the underlying from the caller into the pool;
9
+ // they only differ in which side (assets vs shares) the caller specifies.
10
+ // The exact underlying amount for Mint is resolved by the pool, so we can
11
+ // only require an allowance/balance against the known specified amount —
12
+ // here we approximate Mint by its shares amount (a lower bound on assets is
13
+ // not knowable from calldata alone).
14
+ case "Deposit":
15
+ if (tx.zapper) {
16
+ if (isAddressEqual(tx.tokenIn, NATIVE_ADDRESS)) {
17
+ return [
18
+ new BalancePrerequisite({
19
+ token: tx.tokenIn,
20
+ owner: wallet,
21
+ required: tx.assets,
22
+ title: "Sufficient token balance"
23
+ })
24
+ ];
25
+ }
26
+ return allowanceAndBalance({
27
+ token: tx.tokenIn,
28
+ owner: wallet,
29
+ spender: tx.zapper,
30
+ required: tx.assets,
31
+ allowanceTitle: "Token approved to zapper",
32
+ balanceTitle: "Sufficient token balance"
33
+ });
34
+ }
35
+ return allowanceAndBalance({
36
+ token: tx.underlying,
37
+ owner: wallet,
38
+ spender: tx.pool,
39
+ required: tx.assets,
40
+ allowanceTitle: "Token approved to pool",
41
+ balanceTitle: "Sufficient token balance"
42
+ });
43
+ case "Mint":
44
+ return allowanceAndBalance({
45
+ token: tx.underlying,
46
+ owner: wallet,
47
+ spender: tx.pool,
48
+ required: tx.shares,
49
+ allowanceTitle: "Token approved to pool",
50
+ balanceTitle: "Sufficient token balance"
51
+ });
52
+ // Redeem and Withdraw both burn LP shares from `owner`; they only differ in
53
+ // which side (shares vs assets) the caller specifies.
54
+ case "Redeem":
55
+ if (tx.zapper) {
56
+ return [
57
+ new BalancePrerequisite({
58
+ token: tx.tokenIn,
59
+ owner: wallet,
60
+ required: tx.shares,
61
+ title: "Sufficient share token balance"
62
+ }),
63
+ new AllowancePrerequisite({
64
+ token: tx.tokenIn,
65
+ owner: wallet,
66
+ spender: tx.zapper,
67
+ required: tx.shares,
68
+ title: "Share token approved to zapper"
69
+ })
70
+ ];
71
+ }
72
+ return lpSharePrerequisites(tx.pool, tx.owner, tx.shares, wallet);
73
+ case "Withdraw":
74
+ return lpSharePrerequisites(tx.pool, tx.owner, tx.assets, wallet);
75
+ }
76
+ }
77
+ function lpSharePrerequisites(pool, owner, required, wallet) {
78
+ const prereqs = [
79
+ new BalancePrerequisite({
80
+ token: pool,
81
+ owner,
82
+ required,
83
+ title: "Sufficient LP token balance"
84
+ })
85
+ ];
86
+ if (!isAddressEqual(owner, wallet)) {
87
+ prereqs.push(
88
+ new AllowancePrerequisite({
89
+ token: pool,
90
+ owner,
91
+ spender: wallet,
92
+ required,
93
+ title: "LP token approved to caller"
94
+ })
95
+ );
96
+ }
97
+ return prereqs;
98
+ }
99
+ export {
100
+ buildPoolPrerequisites
101
+ };
@@ -0,0 +1,33 @@
1
+ import { AddressSet } from "../../sdk/index.js";
2
+ import { RWAOpenRequirementsPrerequisite } from "./RWAOpenRequirementsPrerequisite.js";
3
+ function buildRWAPrerequisites(multicall, creditManager, providedArgs, ctx) {
4
+ const { rwaFactory } = ctx.sdk.marketRegister.findByCreditManager(creditManager);
5
+ if (!rwaFactory) {
6
+ return [];
7
+ }
8
+ const rwaTokens = new AddressSet(rwaFactory.getTokens());
9
+ const candidates = new AddressSet();
10
+ for (const op of multicall) {
11
+ if (op.operation === "AddCollateral" || op.operation === "UpdateQuota") {
12
+ candidates.add(op.token);
13
+ }
14
+ }
15
+ const prereqs = [];
16
+ for (const token of candidates) {
17
+ if (!rwaTokens.has(token)) {
18
+ continue;
19
+ }
20
+ prereqs.push(
21
+ new RWAOpenRequirementsPrerequisite({
22
+ token,
23
+ creditManager,
24
+ factory: rwaFactory.address,
25
+ providedArgs
26
+ })
27
+ );
28
+ }
29
+ return prereqs;
30
+ }
31
+ export {
32
+ buildRWAPrerequisites
33
+ };