@gearbox-protocol/sdk 14.12.0-next.3 → 14.12.0-next.5

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 (87) hide show
  1. package/dist/cjs/preview/index.js +2 -4
  2. package/dist/cjs/preview/parse/index.js +0 -2
  3. package/dist/cjs/preview/parse/parseFacadeOperationCalldata.js +5 -12
  4. package/dist/cjs/preview/parse/parseOperationCalldata.js +6 -4
  5. package/dist/cjs/preview/parse/parsePoolOperationCalldata.js +4 -0
  6. package/dist/cjs/preview/parse/parseRWAFactoryOperationCalldata.js +22 -10
  7. package/dist/cjs/preview/parse/types-rwa.js +16 -0
  8. package/dist/cjs/preview/parse/types.js +9 -2
  9. package/dist/cjs/preview/prerequisites/AllowancePrerequisite.js +13 -13
  10. package/dist/cjs/preview/prerequisites/BalancePrerequisite.js +32 -14
  11. package/dist/cjs/preview/prerequisites/RWAOpenRequirementsPrerequisite.js +66 -0
  12. package/dist/cjs/preview/prerequisites/buildPrerequisites.js +95 -27
  13. package/dist/cjs/preview/prerequisites/index.js +2 -0
  14. package/dist/cjs/preview/{simulate/holders.js → preview/errors.js} +12 -21
  15. package/dist/cjs/preview/{parse → preview}/extractExpectedBalanceChanges.js +1 -1
  16. package/dist/cjs/preview/preview/index.js +26 -0
  17. package/dist/cjs/preview/preview/previewOpenCreditAccount.js +99 -0
  18. package/dist/cjs/preview/preview/previewOperation.js +41 -0
  19. package/dist/cjs/preview/preview/previewPoolOperation.js +75 -0
  20. package/dist/cjs/preview/preview/types.js +16 -0
  21. package/dist/cjs/preview/simulate/index.js +5 -2
  22. package/dist/cjs/preview/simulate/simulateOperation.js +4 -0
  23. package/dist/cjs/preview/simulate/simulatePoolOperation.js +20 -116
  24. package/dist/cjs/preview/simulate/simulateRWAOperation.js +30 -0
  25. package/dist/cjs/sdk/market/rwa/securitize/SecuritizeRWAFactory.js +14 -4
  26. package/dist/cjs/sdk/market/rwa/types.js +5 -2
  27. package/dist/cjs/sdk/market/zapper/ZapperContract.js +0 -3
  28. package/dist/esm/preview/index.js +1 -2
  29. package/dist/esm/preview/parse/index.js +0 -1
  30. package/dist/esm/preview/parse/parseFacadeOperationCalldata.js +5 -12
  31. package/dist/esm/preview/parse/parseOperationCalldata.js +7 -5
  32. package/dist/esm/preview/parse/parsePoolOperationCalldata.js +4 -0
  33. package/dist/esm/preview/parse/parseRWAFactoryOperationCalldata.js +25 -11
  34. package/dist/esm/preview/parse/types-rwa.js +0 -0
  35. package/dist/esm/preview/parse/types.js +6 -1
  36. package/dist/esm/preview/prerequisites/AllowancePrerequisite.js +13 -13
  37. package/dist/esm/preview/prerequisites/BalancePrerequisite.js +32 -14
  38. package/dist/esm/preview/prerequisites/RWAOpenRequirementsPrerequisite.js +42 -0
  39. package/dist/esm/preview/prerequisites/buildPrerequisites.js +100 -27
  40. package/dist/esm/preview/prerequisites/index.js +1 -0
  41. package/dist/esm/preview/preview/errors.js +12 -0
  42. package/dist/esm/preview/{parse → preview}/extractExpectedBalanceChanges.js +1 -1
  43. package/dist/esm/preview/preview/index.js +3 -0
  44. package/dist/esm/preview/preview/previewOpenCreditAccount.js +77 -0
  45. package/dist/esm/preview/preview/previewOperation.js +17 -0
  46. package/dist/esm/preview/preview/previewPoolOperation.js +53 -0
  47. package/dist/esm/preview/preview/types.js +0 -0
  48. package/dist/esm/preview/simulate/index.js +3 -1
  49. package/dist/esm/preview/simulate/simulateOperation.js +8 -1
  50. package/dist/esm/preview/simulate/simulatePoolOperation.js +21 -120
  51. package/dist/esm/preview/simulate/simulateRWAOperation.js +6 -0
  52. package/dist/esm/sdk/market/rwa/securitize/SecuritizeRWAFactory.js +14 -4
  53. package/dist/esm/sdk/market/rwa/types.js +5 -2
  54. package/dist/esm/sdk/market/zapper/ZapperContract.js +0 -3
  55. package/dist/types/history/types.d.ts +4 -4
  56. package/dist/types/preview/index.d.ts +1 -2
  57. package/dist/types/preview/parse/index.d.ts +0 -1
  58. package/dist/types/preview/parse/parseRWAFactoryOperationCalldata.d.ts +10 -11
  59. package/dist/types/preview/parse/types-facades.d.ts +0 -33
  60. package/dist/types/preview/parse/types-pools.d.ts +29 -1
  61. package/dist/types/preview/parse/types-rwa.d.ts +48 -0
  62. package/dist/types/preview/parse/types.d.ts +11 -6
  63. package/dist/types/preview/prerequisites/AllowancePrerequisite.d.ts +1 -3
  64. package/dist/types/preview/prerequisites/BalancePrerequisite.d.ts +9 -6
  65. package/dist/types/preview/prerequisites/RWAOpenRequirementsPrerequisite.d.ts +36 -0
  66. package/dist/types/preview/prerequisites/buildPrerequisites.d.ts +15 -6
  67. package/dist/types/preview/prerequisites/index.d.ts +1 -0
  68. package/dist/types/preview/prerequisites/types.d.ts +10 -8
  69. package/dist/types/preview/preview/errors.d.ts +9 -0
  70. package/dist/types/preview/{parse → preview}/extractExpectedBalanceChanges.d.ts +4 -5
  71. package/dist/types/preview/preview/index.d.ts +3 -0
  72. package/dist/types/preview/preview/previewOpenCreditAccount.d.ts +4 -0
  73. package/dist/types/preview/preview/previewOperation.d.ts +8 -0
  74. package/dist/types/preview/preview/previewPoolOperation.d.ts +5 -0
  75. package/dist/types/preview/preview/types.d.ts +103 -0
  76. package/dist/types/preview/simulate/index.d.ts +3 -1
  77. package/dist/types/preview/simulate/simulateFacadeOperation.d.ts +0 -2
  78. package/dist/types/preview/simulate/simulateOperation.d.ts +0 -2
  79. package/dist/types/preview/simulate/simulatePoolOperation.d.ts +6 -18
  80. package/dist/types/preview/simulate/simulateRWAOperation.d.ts +23 -0
  81. package/dist/types/preview/simulate/types.d.ts +12 -26
  82. package/dist/types/sdk/market/rwa/securitize/SecuritizeRWAFactory.d.ts +4 -0
  83. package/dist/types/sdk/market/rwa/types.d.ts +13 -5
  84. package/dist/types/sdk/market/zapper/types.d.ts +6 -6
  85. package/package.json +1 -1
  86. package/dist/esm/preview/simulate/holders.js +0 -21
  87. package/dist/types/preview/simulate/holders.d.ts +0 -7
@@ -1,6 +1,5 @@
1
1
  import { isAddressEqual, zeroAddress } from "viem";
2
2
  import { classifyInnerOperations } from "./classifyInnerOperations.js";
3
- import { extractExpectedBalanceChanges } from "./extractExpectedBalanceChanges.js";
4
3
  function parseFacadeOperationCalldata(props) {
5
4
  const { sdk, facade, calldata } = props;
6
5
  const parsed = sdk.parseFunctionDataV2(facade.address, calldata);
@@ -21,23 +20,20 @@ function parseFacadeOperationCalldata(props) {
21
20
  sdk,
22
21
  underlying: suite.underlying
23
22
  });
24
- const expectedBalanceChanges = extractExpectedBalanceChanges(innerCalls);
25
23
  switch (functionName) {
26
24
  case "multicall":
27
25
  return {
28
26
  ...metadata,
29
27
  operation: "MultiCall",
30
28
  creditAccount: rawArgs.creditAccount,
31
- multicall,
32
- expectedBalanceChanges
29
+ multicall
33
30
  };
34
31
  case "botMulticall":
35
32
  return {
36
33
  ...metadata,
37
34
  operation: "BotMulticall",
38
35
  creditAccount: rawArgs.creditAccount,
39
- multicall,
40
- expectedBalanceChanges
36
+ multicall
41
37
  };
42
38
  case "openCreditAccount":
43
39
  return {
@@ -46,16 +42,14 @@ function parseFacadeOperationCalldata(props) {
46
42
  creditAccount: zeroAddress,
47
43
  onBehalfOf: rawArgs.onBehalfOf,
48
44
  referralCode: rawArgs.referralCode,
49
- multicall,
50
- expectedBalanceChanges
45
+ multicall
51
46
  };
52
47
  case "closeCreditAccount":
53
48
  return {
54
49
  ...metadata,
55
50
  operation: "CloseCreditAccount",
56
51
  creditAccount: rawArgs.creditAccount,
57
- multicall,
58
- expectedBalanceChanges
52
+ multicall
59
53
  };
60
54
  case "liquidateCreditAccount":
61
55
  return {
@@ -67,8 +61,7 @@ function parseFacadeOperationCalldata(props) {
67
61
  // liquidation, so they are not recoverable from raw calldata.
68
62
  token: zeroAddress,
69
63
  remainingFunds: 0n,
70
- multicall,
71
- expectedBalanceChanges
64
+ multicall
72
65
  };
73
66
  case "partiallyLiquidateCreditAccount":
74
67
  return {
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  CreditFacadeV310Contract,
3
+ isRWAFactory,
3
4
  PoolV310Contract,
4
- SecuritizeRWAFactory,
5
5
  ZapperContract
6
6
  } from "../../sdk/index.js";
7
7
  import { UnsupportedTargetError } from "./errors.js";
@@ -23,7 +23,8 @@ function parseOperationCalldata(input) {
23
23
  receiver: parsed.receiver,
24
24
  assets: parsed.assets,
25
25
  underlying: parsed.underlying,
26
- tokenIn: parsed.token,
26
+ tokenIn: contract.tokenIn.addr,
27
+ tokenOut: contract.tokenOut.addr,
27
28
  zapper: parsed.zapper,
28
29
  referralCode: parsed.referralCode
29
30
  };
@@ -33,12 +34,13 @@ function parseOperationCalldata(input) {
33
34
  operation: "Redeem",
34
35
  pool: parsed.pool,
35
36
  receiver: parsed.receiver,
36
- // The zapper burns the caller's pool shares, so the share owner is the
37
+ // The zapper burns the caller's share tokens, so the share owner is the
37
38
  // transaction sender.
38
39
  owner: sender,
39
40
  shares: parsed.shares,
40
41
  underlying: parsed.underlying,
41
- tokenOut: parsed.token,
42
+ tokenIn: contract.tokenOut.addr,
43
+ tokenOut: contract.tokenIn.addr,
42
44
  zapper: parsed.zapper
43
45
  };
44
46
  return op;
@@ -50,7 +52,7 @@ function parseOperationCalldata(input) {
50
52
  calldata
51
53
  });
52
54
  }
53
- if (contract instanceof SecuritizeRWAFactory) {
55
+ if (contract && isRWAFactory(contract)) {
54
56
  return parseRWAFactoryOperationCalldata({
55
57
  sdk,
56
58
  factory: contract,
@@ -15,6 +15,7 @@ function parsePoolOperationCalldata(props) {
15
15
  assets: rawArgs.assets,
16
16
  underlying,
17
17
  tokenIn: underlying,
18
+ tokenOut: pool.address,
18
19
  zapper: void 0,
19
20
  referralCode: functionName === "depositWithReferral" ? rawArgs.referralCode : void 0
20
21
  };
@@ -27,6 +28,7 @@ function parsePoolOperationCalldata(props) {
27
28
  shares: rawArgs.shares,
28
29
  underlying,
29
30
  tokenIn: underlying,
31
+ tokenOut: pool.address,
30
32
  zapper: void 0,
31
33
  referralCode: functionName === "mintWithReferral" ? rawArgs.referralCode : void 0
32
34
  };
@@ -38,6 +40,7 @@ function parsePoolOperationCalldata(props) {
38
40
  owner: rawArgs.owner,
39
41
  assets: rawArgs.assets,
40
42
  underlying,
43
+ tokenIn: pool.address,
41
44
  tokenOut: underlying,
42
45
  zapper: void 0
43
46
  };
@@ -49,6 +52,7 @@ function parsePoolOperationCalldata(props) {
49
52
  owner: rawArgs.owner,
50
53
  shares: rawArgs.shares,
51
54
  underlying,
55
+ tokenIn: pool.address,
52
56
  tokenOut: underlying,
53
57
  zapper: void 0
54
58
  };
@@ -1,19 +1,35 @@
1
- import { zeroAddress } from "viem";
2
1
  import { AbstractAdapterContract } from "../../plugins/adapters/index.js";
3
2
  import {
4
3
  CreditFacadeV310Contract,
5
- hexEq
4
+ hexEq,
5
+ isRWAFactory,
6
+ RWA_FACTORY_SECURITIZE
6
7
  } from "../../sdk/index.js";
7
8
  import { classifyInnerOperations } from "./classifyInnerOperations.js";
8
- import { extractExpectedBalanceChanges } from "./extractExpectedBalanceChanges.js";
9
9
  function parseRWAFactoryOperationCalldata(props) {
10
+ const { contractType, address } = props.factory;
11
+ if (isRWAFactory(props.factory, RWA_FACTORY_SECURITIZE)) {
12
+ return parseSecuritizeOperationCalldata(props);
13
+ }
14
+ throw new Error(
15
+ `unsupported RWA factory type "${contractType}" on ${address}`
16
+ );
17
+ }
18
+ function parseSecuritizeOperationCalldata(props) {
10
19
  const { sdk, factory, calldata } = props;
11
20
  const parsed = sdk.parseFunctionDataV2(factory.address, calldata);
12
21
  const functionName = parsed.functionName.split("(")[0];
13
22
  const { rawArgs } = parsed;
14
23
  const innerCalls = rawArgs.calls ?? [];
24
+ const tokensToRegister = [
25
+ ...rawArgs.tokensToRegister ?? []
26
+ ];
27
+ const signaturesToCache = [
28
+ ...rawArgs.signaturesToCache ?? []
29
+ ];
15
30
  const suite = functionName === "openCreditAccount" ? sdk.marketRegister.findCreditManager(rawArgs.creditManager) : resolveSuiteForMulticall(sdk, factory, innerCalls);
16
31
  const metadata = {
32
+ factory: factory.address,
17
33
  creditManager: suite.creditManager.address,
18
34
  creditFacade: suite.creditFacade.address
19
35
  };
@@ -21,25 +37,23 @@ function parseRWAFactoryOperationCalldata(props) {
21
37
  sdk,
22
38
  underlying: suite.underlying
23
39
  });
24
- const expectedBalanceChanges = extractExpectedBalanceChanges(innerCalls);
25
40
  switch (functionName) {
26
41
  case "openCreditAccount":
27
42
  return {
28
43
  ...metadata,
29
- operation: "OpenCreditAccount",
30
- creditAccount: zeroAddress,
31
- onBehalfOf: zeroAddress,
32
- referralCode: 0n,
44
+ operation: "SecuritizeOpenCreditAccount",
33
45
  multicall,
34
- expectedBalanceChanges
46
+ tokensToRegister,
47
+ signaturesToCache
35
48
  };
36
49
  case "multicall":
37
50
  return {
38
51
  ...metadata,
39
- operation: "MultiCall",
52
+ operation: "SecuritizeMulticall",
40
53
  creditAccount: rawArgs.creditAccount,
41
54
  multicall,
42
- expectedBalanceChanges
55
+ tokensToRegister,
56
+ signaturesToCache
43
57
  };
44
58
  default:
45
59
  throw new Error(
File without changes
@@ -1,9 +1,14 @@
1
1
  export * from "./types-adapters.js";
2
2
  export * from "./types-facades.js";
3
3
  export * from "./types-pools.js";
4
+ export * from "./types-rwa.js";
4
5
  function isPoolOperation(tx) {
5
6
  return tx.operation === "Deposit" || tx.operation === "Mint" || tx.operation === "Withdraw" || tx.operation === "Redeem";
6
7
  }
8
+ function isRWAOperation(tx) {
9
+ return tx.operation === "SecuritizeOpenCreditAccount" || tx.operation === "SecuritizeMulticall";
10
+ }
7
11
  export {
8
- isPoolOperation
12
+ isPoolOperation,
13
+ isRWAOperation
9
14
  };
@@ -4,14 +4,14 @@ import {
4
4
  toPrerequisiteError
5
5
  } from "./Prerequisite.js";
6
6
  class AllowancePrerequisite extends Prerequisite {
7
- _id;
8
- _title;
9
- _detail;
7
+ #id;
8
+ #title;
9
+ #detail;
10
10
  constructor(props) {
11
11
  super();
12
- this._id = props.id ?? `allowance:${props.token}:${props.owner}:${props.spender}`;
13
- this._title = props.title ?? "Token approval";
14
- this._detail = {
12
+ this.#id = props.id ?? `allowance:${props.token}:${props.owner}:${props.spender}`;
13
+ this.#title = props.title ?? "Token approval";
14
+ this.#detail = {
15
15
  token: props.token,
16
16
  owner: props.owner,
17
17
  spender: props.spender,
@@ -19,24 +19,24 @@ class AllowancePrerequisite extends Prerequisite {
19
19
  };
20
20
  }
21
21
  get id() {
22
- return this._id;
22
+ return this.#id;
23
23
  }
24
24
  get kind() {
25
25
  return "allowance";
26
26
  }
27
27
  get title() {
28
- return this._title;
28
+ return this.#title;
29
29
  }
30
30
  get detail() {
31
- return this._detail;
31
+ return this.#detail;
32
32
  }
33
33
  calls() {
34
34
  return [
35
35
  {
36
- address: this._detail.token,
36
+ address: this.#detail.token,
37
37
  abi: erc20Abi,
38
38
  functionName: "allowance",
39
- args: [this._detail.owner, this._detail.spender]
39
+ args: [this.#detail.owner, this.#detail.spender]
40
40
  }
41
41
  ];
42
42
  }
@@ -46,8 +46,8 @@ class AllowancePrerequisite extends Prerequisite {
46
46
  return this.errorResult(toPrerequisiteError(res?.error));
47
47
  }
48
48
  const actual = res.result;
49
- return this.satisfiedResult(actual >= this._detail.required, {
50
- ...this._detail,
49
+ return this.satisfiedResult(actual >= this.#detail.required, {
50
+ ...this.#detail,
51
51
  actual
52
52
  });
53
53
  }
@@ -1,41 +1,59 @@
1
1
  import { erc20Abi } from "viem";
2
+ import { iMulticall3Abi } from "../../abi/iMulticall3.js";
3
+ import { hexEq, NATIVE_ADDRESS } from "../../sdk/index.js";
2
4
  import {
3
5
  Prerequisite,
4
6
  toPrerequisiteError
5
7
  } from "./Prerequisite.js";
6
8
  class BalancePrerequisite extends Prerequisite {
7
- _id;
8
- _title;
9
- _detail;
9
+ #id;
10
+ #title;
11
+ #detail;
10
12
  constructor(props) {
11
13
  super();
12
- this._id = props.id ?? `balance:${props.token}:${props.owner}`;
13
- this._title = props.title ?? "Sufficient balance";
14
- this._detail = {
14
+ this.#id = props.id ?? `balance:${props.token}:${props.owner}`;
15
+ this.#title = props.title ?? "Sufficient balance";
16
+ this.#detail = {
15
17
  token: props.token,
16
18
  owner: props.owner,
17
19
  required: props.required
18
20
  };
19
21
  }
20
22
  get id() {
21
- return this._id;
23
+ return this.#id;
22
24
  }
23
25
  get kind() {
24
26
  return "balance";
25
27
  }
26
28
  get title() {
27
- return this._title;
29
+ return this.#title;
28
30
  }
29
31
  get detail() {
30
- return this._detail;
32
+ return this.#detail;
31
33
  }
32
- calls() {
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
+ }
33
51
  return [
34
52
  {
35
- address: this._detail.token,
53
+ address: this.#detail.token,
36
54
  abi: erc20Abi,
37
55
  functionName: "balanceOf",
38
- args: [this._detail.owner]
56
+ args: [this.#detail.owner]
39
57
  }
40
58
  ];
41
59
  }
@@ -45,8 +63,8 @@ class BalancePrerequisite extends Prerequisite {
45
63
  return this.errorResult(toPrerequisiteError(res?.error));
46
64
  }
47
65
  const actual = res.result;
48
- return this.satisfiedResult(actual >= this._detail.required, {
49
- ...this._detail,
66
+ return this.satisfiedResult(actual >= this.#detail.required, {
67
+ ...this.#detail,
50
68
  actual
51
69
  });
52
70
  }
@@ -0,0 +1,42 @@
1
+ import { Prerequisite } from "./Prerequisite.js";
2
+ class RWAOpenRequirementsPrerequisite extends Prerequisite {
3
+ #id;
4
+ #title;
5
+ #detail;
6
+ constructor(props) {
7
+ super();
8
+ this.#id = props.id ?? `rwaOpenRequirements:${props.factory}:${props.token}`;
9
+ this.#title = props.title ?? "RWA account requirements fulfilled";
10
+ this.#detail = props.requirements;
11
+ }
12
+ get id() {
13
+ return this.#id;
14
+ }
15
+ get kind() {
16
+ return "rwaOpenRequirements";
17
+ }
18
+ get title() {
19
+ return this.#title;
20
+ }
21
+ get detail() {
22
+ return this.#detail;
23
+ }
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;
38
+ }
39
+ }
40
+ export {
41
+ RWAOpenRequirementsPrerequisite
42
+ };
@@ -1,8 +1,18 @@
1
1
  import { isAddressEqual } from "viem";
2
+ import {
3
+ AddressSet,
4
+ NATIVE_ADDRESS
5
+ } from "../../sdk/index.js";
6
+ import {
7
+ parseOperationCalldata
8
+ } from "../parse/parseOperationCalldata.js";
2
9
  import { AllowancePrerequisite } from "./AllowancePrerequisite.js";
3
10
  import { BalancePrerequisite } from "./BalancePrerequisite.js";
4
- async function buildPrerequisites(tx, ctx) {
5
- const { wallet } = ctx;
11
+ import { RWAOpenRequirementsPrerequisite } from "./RWAOpenRequirementsPrerequisite.js";
12
+ async function buildPrerequisites(input) {
13
+ const { sdk, sender: wallet, blockNumber } = input;
14
+ const tx = parseOperationCalldata(input);
15
+ const ctx = { sdk, wallet, blockNumber };
6
16
  switch (tx.operation) {
7
17
  // Deposit and Mint both pull the underlying from the caller into the pool;
8
18
  // they only differ in which side (assets vs shares) the caller specifies.
@@ -12,21 +22,27 @@ async function buildPrerequisites(tx, ctx) {
12
22
  // not knowable from calldata alone).
13
23
  case "Deposit":
14
24
  if (tx.zapper) {
15
- return [
16
- new AllowancePrerequisite({
17
- token: tx.tokenIn,
18
- owner: wallet,
19
- spender: tx.zapper,
20
- required: tx.assets,
21
- title: "Token approved to zapper"
22
- }),
25
+ const prereqs = [];
26
+ if (!isAddressEqual(tx.tokenIn, NATIVE_ADDRESS)) {
27
+ prereqs.push(
28
+ new AllowancePrerequisite({
29
+ token: tx.tokenIn,
30
+ owner: wallet,
31
+ spender: tx.zapper,
32
+ required: tx.assets,
33
+ title: "Token approved to zapper"
34
+ })
35
+ );
36
+ }
37
+ prereqs.push(
23
38
  new BalancePrerequisite({
24
39
  token: tx.tokenIn,
25
40
  owner: wallet,
26
41
  required: tx.assets,
27
42
  title: "Sufficient token balance"
28
43
  })
29
- ];
44
+ );
45
+ return prereqs;
30
46
  }
31
47
  return [
32
48
  new AllowancePrerequisite({
@@ -65,17 +81,17 @@ async function buildPrerequisites(tx, ctx) {
65
81
  if (tx.zapper) {
66
82
  return [
67
83
  new BalancePrerequisite({
68
- token: tx.pool,
84
+ token: tx.tokenIn,
69
85
  owner: wallet,
70
86
  required: tx.shares,
71
- title: "Sufficient LP token balance"
87
+ title: "Sufficient share token balance"
72
88
  }),
73
89
  new AllowancePrerequisite({
74
- token: tx.pool,
90
+ token: tx.tokenIn,
75
91
  owner: wallet,
76
92
  spender: tx.zapper,
77
93
  required: tx.shares,
78
- title: "LP token approved to zapper"
94
+ title: "Share token approved to zapper"
79
95
  })
80
96
  ];
81
97
  }
@@ -128,10 +144,34 @@ async function buildPrerequisites(tx, ctx) {
128
144
  case "CloseCreditAccount":
129
145
  case "LiquidateCreditAccount":
130
146
  return collateralPrerequisites(
131
- tx.operation,
147
+ tx.operation === "OpenCreditAccount" ? { creditManager: tx.creditManager, borrower: wallet } : {
148
+ creditManager: tx.creditManager,
149
+ creditAccount: tx.creditAccount
150
+ },
151
+ tx.multicall,
152
+ ctx
153
+ );
154
+ // RWA-factory operations: same collateral checks as facade operations,
155
+ // plus (for opening) the factory's open-account requirements. The parsed
156
+ // operation carries template (empty) `tokensToRegister`/`signaturesToCache`;
157
+ // the prerequisite detail provides the real values.
158
+ case "SecuritizeOpenCreditAccount":
159
+ return [
160
+ ...await collateralPrerequisites(
161
+ { creditManager: tx.creditManager, borrower: wallet },
162
+ tx.multicall,
163
+ ctx
164
+ ),
165
+ ...await rwaOpenRequirementsPrerequisites(
166
+ tx.multicall,
167
+ tx.creditManager,
168
+ ctx
169
+ )
170
+ ];
171
+ case "SecuritizeMulticall":
172
+ return collateralPrerequisites(
173
+ { creditManager: tx.creditManager, creditAccount: tx.creditAccount },
132
174
  tx.multicall,
133
- tx.creditManager,
134
- tx.creditAccount,
135
175
  ctx
136
176
  );
137
177
  case "PartiallyLiquidateCreditAccount": {
@@ -159,26 +199,24 @@ async function buildPrerequisites(tx, ctx) {
159
199
  return [];
160
200
  }
161
201
  }
162
- async function collateralPrerequisites(op, multicall, creditManager, creditAccount, ctx) {
202
+ async function collateralPrerequisites(spenderOptions, multicall, ctx) {
163
203
  const { sdk, wallet } = ctx;
164
204
  const required = /* @__PURE__ */ new Map();
165
- for (const op2 of multicall) {
166
- if (op2.operation !== "AddCollateral" || op2.amount === 0n) {
205
+ for (const op of multicall) {
206
+ if (op.operation !== "AddCollateral" || op.amount === 0n) {
167
207
  continue;
168
208
  }
169
- const key = op2.token.toLowerCase();
209
+ const key = op.token.toLowerCase();
170
210
  const existing = required.get(key);
171
211
  required.set(key, {
172
- token: op2.token,
173
- amount: (existing?.amount ?? 0n) + op2.amount
212
+ token: op.token,
213
+ amount: (existing?.amount ?? 0n) + op.amount
174
214
  });
175
215
  }
176
216
  if (required.size === 0) {
177
217
  return [];
178
218
  }
179
- const spender = await sdk.accounts.getApprovalAddress(
180
- op === "OpenCreditAccount" ? { creditManager, borrower: wallet } : { creditManager, creditAccount }
181
- );
219
+ const spender = await sdk.accounts.getApprovalAddress(spenderOptions);
182
220
  const prereqs = [];
183
221
  for (const { token, amount } of required.values()) {
184
222
  prereqs.push(
@@ -199,6 +237,41 @@ async function collateralPrerequisites(op, multicall, creditManager, creditAccou
199
237
  }
200
238
  return prereqs;
201
239
  }
240
+ async function rwaOpenRequirementsPrerequisites(multicall, creditManager, ctx) {
241
+ const { sdk, wallet } = ctx;
242
+ const { rwaFactory } = sdk.marketRegister.findByCreditManager(creditManager);
243
+ if (!rwaFactory) {
244
+ return [];
245
+ }
246
+ const rwaTokens = new AddressSet(rwaFactory.getTokens());
247
+ const candidates = new AddressSet();
248
+ for (const op of multicall) {
249
+ if (op.operation === "AddCollateral" || op.operation === "UpdateQuota") {
250
+ candidates.add(op.token);
251
+ }
252
+ }
253
+ const prereqs = [];
254
+ for (const token of candidates) {
255
+ if (!rwaTokens.has(token)) {
256
+ continue;
257
+ }
258
+ const requirements = await sdk.accounts.getOpenAccountRequirements(
259
+ wallet,
260
+ creditManager,
261
+ { tokenOutAddress: token }
262
+ );
263
+ if (requirements) {
264
+ prereqs.push(
265
+ new RWAOpenRequirementsPrerequisite({
266
+ requirements,
267
+ token,
268
+ factory: rwaFactory.address
269
+ })
270
+ );
271
+ }
272
+ }
273
+ return prereqs;
274
+ }
202
275
  function underlyingOf(sdk, creditManager) {
203
276
  const suite = sdk.marketRegister.creditManagers.find(
204
277
  (cm) => isAddressEqual(cm.creditManager.address, creditManager)
@@ -3,5 +3,6 @@ export * from "./BalancePrerequisite.js";
3
3
  export * from "./buildPrerequisites.js";
4
4
  export * from "./Prerequisite.js";
5
5
  export * from "./prepareAction.js";
6
+ export * from "./RWAOpenRequirementsPrerequisite.js";
6
7
  export * from "./runPrerequisites.js";
7
8
  export * from "./types.js";
@@ -0,0 +1,12 @@
1
+ class UnsupportedOperationError extends Error {
2
+ /** The parsed operation kind (the `operation` discriminant). */
3
+ operation;
4
+ constructor(operation) {
5
+ super(`operation "${operation}" is not supported by previewOperation`);
6
+ this.name = "UnsupportedOperationError";
7
+ this.operation = operation;
8
+ }
9
+ }
10
+ export {
11
+ UnsupportedOperationError
12
+ };
@@ -14,7 +14,7 @@ function extractExpectedBalanceChanges(innerCalls) {
14
14
  if (!balanceDeltas) {
15
15
  return void 0;
16
16
  }
17
- return balanceDeltas.map(({ token, amount }) => ({ token, delta: amount }));
17
+ return balanceDeltas.map(({ token, amount }) => ({ token, balance: amount }));
18
18
  }
19
19
  function functionName(call) {
20
20
  return call.functionName.split("(")[0];
@@ -0,0 +1,3 @@
1
+ export * from "./errors.js";
2
+ export * from "./previewOperation.js";
3
+ export * from "./types.js";