@gearbox-protocol/sdk 14.12.0-next.11 → 14.12.0-next.13

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/preview/parse/parseRWAFactoryOperationCalldata.js +13 -12
  2. package/dist/cjs/preview/parse/types.js +1 -1
  3. package/dist/cjs/preview/prerequisites/AllowancePrerequisite.js +10 -16
  4. package/dist/cjs/preview/prerequisites/BalancePrerequisite.js +17 -33
  5. package/dist/cjs/preview/prerequisites/Prerequisite.js +13 -18
  6. package/dist/cjs/preview/prerequisites/RWAOpenRequirementsPrerequisite.js +36 -15
  7. package/dist/cjs/preview/prerequisites/buildCollateralPrerequisites.js +57 -0
  8. package/dist/cjs/preview/prerequisites/{prepareAction.js → buildPartialLiquidationPrerequisites.js} +18 -24
  9. package/dist/cjs/preview/prerequisites/buildPoolPrerequisites.js +125 -0
  10. package/dist/cjs/preview/prerequisites/buildRWAPrerequisites.js +57 -0
  11. package/dist/cjs/preview/prerequisites/checkPrerequisites.js +85 -0
  12. package/dist/cjs/preview/prerequisites/helpers.js +47 -0
  13. package/dist/cjs/preview/prerequisites/index.js +2 -6
  14. package/dist/cjs/preview/preview/detectCloseOrRepay.js +51 -0
  15. package/dist/cjs/preview/preview/index.js +4 -0
  16. package/dist/cjs/preview/preview/previewCloseOrRepay.js +94 -0
  17. package/dist/cjs/preview/preview/previewOperation.js +10 -2
  18. package/dist/cjs/sdk/accounts/CreditAccountsServiceV310.js +104 -49
  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/preview/parse/parseRWAFactoryOperationCalldata.js +13 -12
  22. package/dist/esm/preview/parse/types.js +1 -1
  23. package/dist/esm/preview/prerequisites/AllowancePrerequisite.js +11 -20
  24. package/dist/esm/preview/prerequisites/BalancePrerequisite.js +18 -37
  25. package/dist/esm/preview/prerequisites/Prerequisite.js +13 -20
  26. package/dist/esm/preview/prerequisites/RWAOpenRequirementsPrerequisite.js +36 -15
  27. package/dist/esm/preview/prerequisites/buildCollateralPrerequisites.js +35 -0
  28. package/dist/esm/preview/prerequisites/buildPartialLiquidationPrerequisites.js +18 -0
  29. package/dist/esm/preview/prerequisites/buildPoolPrerequisites.js +101 -0
  30. package/dist/esm/preview/prerequisites/buildRWAPrerequisites.js +33 -0
  31. package/dist/esm/preview/prerequisites/checkPrerequisites.js +63 -0
  32. package/dist/esm/preview/prerequisites/helpers.js +23 -0
  33. package/dist/esm/preview/prerequisites/index.js +1 -3
  34. package/dist/esm/preview/preview/detectCloseOrRepay.js +26 -0
  35. package/dist/esm/preview/preview/index.js +2 -0
  36. package/dist/esm/preview/preview/previewCloseOrRepay.js +76 -0
  37. package/dist/esm/preview/preview/previewOperation.js +10 -2
  38. package/dist/esm/sdk/accounts/CreditAccountsServiceV310.js +104 -49
  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/preview/parse/parseRWAFactoryOperationCalldata.d.ts +1 -1
  42. package/dist/types/preview/parse/types-rwa.d.ts +11 -23
  43. package/dist/types/preview/prerequisites/AllowancePrerequisite.d.ts +4 -5
  44. package/dist/types/preview/prerequisites/BalancePrerequisite.d.ts +4 -6
  45. package/dist/types/preview/prerequisites/Prerequisite.d.ts +16 -40
  46. package/dist/types/preview/prerequisites/RWAOpenRequirementsPrerequisite.d.ts +22 -16
  47. package/dist/types/preview/prerequisites/buildCollateralPrerequisites.d.ts +14 -0
  48. package/dist/types/preview/prerequisites/buildPartialLiquidationPrerequisites.d.ts +8 -0
  49. package/dist/types/preview/prerequisites/buildPoolPrerequisites.d.ts +8 -0
  50. package/dist/types/preview/prerequisites/buildRWAPrerequisites.d.ts +21 -0
  51. package/dist/types/preview/prerequisites/checkPrerequisites.d.ts +27 -0
  52. package/dist/types/preview/prerequisites/helpers.d.ts +15 -0
  53. package/dist/types/preview/prerequisites/index.d.ts +1 -3
  54. package/dist/types/preview/prerequisites/types.d.ts +21 -2
  55. package/dist/types/preview/preview/detectCloseOrRepay.d.ts +18 -0
  56. package/dist/types/preview/preview/index.d.ts +2 -0
  57. package/dist/types/preview/preview/previewAdjustCreditAccount.d.ts +2 -2
  58. package/dist/types/preview/preview/previewCloseOrRepay.d.ts +10 -0
  59. package/dist/types/preview/preview/previewOpenCreditAccount.d.ts +2 -2
  60. package/dist/types/preview/preview/types.d.ts +60 -3
  61. package/dist/types/sdk/accounts/CreditAccountsServiceV310.d.ts +6 -1
  62. package/dist/types/sdk/accounts/types.d.ts +66 -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
@@ -1589,19 +1589,14 @@ class CreditAccountsServiceV310 extends SDKConstruct {
1589
1589
  );
1590
1590
  }
1591
1591
  /**
1592
- * {@inheritDoc ICreditAccountsService.assembleCaUpdateCalls}
1592
+ * {@inheritDoc ICreditAccountsService.assembleCaOperations}
1593
1593
  */
1594
- assembleCaUpdateCalls({
1594
+ assembleCaOperations({
1595
1595
  operations,
1596
- routerCallGroups,
1597
1596
  creditFacade,
1598
- withdrawTo,
1599
- creditAccount,
1600
- underlyingToken
1597
+ withdrawTo
1601
1598
  }) {
1602
1599
  const calls = [];
1603
- let swapGroupIndex = 0;
1604
- let routerGroupsConsumed = 0;
1605
1600
  for (const op of operations) {
1606
1601
  switch (op.type) {
1607
1602
  case "increaseDebt":
@@ -1629,18 +1624,10 @@ class CreditAccountsServiceV310 extends SDKConstruct {
1629
1624
  )
1630
1625
  );
1631
1626
  break;
1632
- case "swap": {
1633
- const routerCalls = routerCallGroups[swapGroupIndex];
1634
- if (!routerCalls) {
1635
- throw new Error(
1636
- `assembleCaUpdateCalls: missing router calls for swap leg ${swapGroupIndex}`
1637
- );
1638
- }
1639
- calls.push(...routerCalls);
1640
- swapGroupIndex += 1;
1641
- routerGroupsConsumed += 1;
1627
+ case "swap":
1628
+ case "wrapRwaCollateral":
1629
+ calls.push(...op.calls);
1642
1630
  break;
1643
- }
1644
1631
  case "changeQuota": {
1645
1632
  const quotaAssets = [...op.quotaIncrease, ...op.quotaDecrease];
1646
1633
  if (quotaAssets.length === 0) {
@@ -1654,43 +1641,110 @@ class CreditAccountsServiceV310 extends SDKConstruct {
1654
1641
  );
1655
1642
  break;
1656
1643
  }
1657
- case "closeCreditAccount": {
1658
- for (const group of routerCallGroups) {
1659
- calls.push(...group);
1660
- routerGroupsConsumed += 1;
1661
- }
1644
+ default: {
1645
+ const _exhaustive = op;
1646
+ throw new Error(
1647
+ `assembleCaOperations: unsupported operation ${JSON.stringify(_exhaustive)}`
1648
+ );
1649
+ }
1650
+ }
1651
+ }
1652
+ return calls;
1653
+ }
1654
+ /**
1655
+ * {@inheritDoc ICreditAccountsService.assembleCaUpdateCalls}
1656
+ * @deprecated Prefer {@link CreditAccountsServiceV310.assembleCaOperations}.
1657
+ */
1658
+ assembleCaUpdateCalls({
1659
+ operations,
1660
+ routerCallGroups,
1661
+ creditFacade,
1662
+ withdrawTo,
1663
+ creditAccount,
1664
+ underlyingToken
1665
+ }) {
1666
+ const calls = [];
1667
+ let swapGroupIndex = 0;
1668
+ let routerGroupsConsumed = 0;
1669
+ const encodableOps = [];
1670
+ let pendingClose = null;
1671
+ const flushEncodable = () => {
1672
+ if (encodableOps.length === 0) {
1673
+ return;
1674
+ }
1675
+ calls.push(
1676
+ ...this.assembleCaOperations({
1677
+ operations: encodableOps.splice(0, encodableOps.length),
1678
+ creditFacade,
1679
+ withdrawTo
1680
+ })
1681
+ );
1682
+ };
1683
+ for (const op of operations) {
1684
+ if (op.type === "closeCreditAccount") {
1685
+ flushEncodable();
1686
+ pendingClose = { routerGroupsStart: routerGroupsConsumed };
1687
+ for (const group of routerCallGroups.slice(routerGroupsConsumed)) {
1688
+ calls.push(...group);
1689
+ routerGroupsConsumed += 1;
1690
+ }
1691
+ calls.push(
1692
+ ...this.#prepareDisableQuotas({
1693
+ creditFacade,
1694
+ tokens: Object.entries(creditAccount.initialQuotas).map(
1695
+ ([token, { quota }]) => ({
1696
+ token,
1697
+ quota
1698
+ })
1699
+ )
1700
+ })
1701
+ );
1702
+ if (creditAccount.debt > 0n) {
1662
1703
  calls.push(
1663
- ...this.#prepareDisableQuotas({
1704
+ ...this.#prepareDecreaseDebt({
1664
1705
  creditFacade,
1665
- tokens: Object.entries(creditAccount.initialQuotas).map(
1666
- ([token, { quota }]) => ({
1667
- token,
1668
- quota
1669
- })
1670
- )
1706
+ debt: creditAccount.debt
1671
1707
  })
1672
1708
  );
1673
- if (creditAccount.debt > 0n) {
1674
- calls.push(
1675
- ...this.#prepareDecreaseDebt({
1676
- creditFacade,
1677
- debt: creditAccount.debt
1678
- })
1679
- );
1680
- }
1681
- const hasAssets = creditAccount.assets.some(
1682
- (asset) => asset.balance > 0n
1709
+ }
1710
+ const hasAssets = creditAccount.assets.some(
1711
+ (asset) => asset.balance > 0n
1712
+ );
1713
+ if (hasAssets) {
1714
+ calls.push(
1715
+ this.#prepareWithdrawToken(
1716
+ creditFacade,
1717
+ underlyingToken,
1718
+ MAX_UINT256,
1719
+ withdrawTo
1720
+ )
1683
1721
  );
1684
- if (hasAssets) {
1685
- calls.push(
1686
- this.#prepareWithdrawToken(
1687
- creditFacade,
1688
- underlyingToken,
1689
- MAX_UINT256,
1690
- withdrawTo
1691
- )
1722
+ }
1723
+ continue;
1724
+ }
1725
+ if (pendingClose !== null) {
1726
+ throw new Error(
1727
+ "assembleCaUpdateCalls: operations after closeCreditAccount are not supported"
1728
+ );
1729
+ }
1730
+ switch (op.type) {
1731
+ case "increaseDebt":
1732
+ case "decreaseDebt":
1733
+ case "addCollateral":
1734
+ case "withdrawCollateral":
1735
+ case "changeQuota":
1736
+ encodableOps.push(op);
1737
+ break;
1738
+ case "swap": {
1739
+ const routerCalls = routerCallGroups[swapGroupIndex];
1740
+ if (!routerCalls) {
1741
+ throw new Error(
1742
+ `assembleCaUpdateCalls: missing router calls for swap leg ${swapGroupIndex}`
1692
1743
  );
1693
1744
  }
1745
+ encodableOps.push({ type: "swap", calls: routerCalls });
1746
+ swapGroupIndex += 1;
1747
+ routerGroupsConsumed += 1;
1694
1748
  break;
1695
1749
  }
1696
1750
  default: {
@@ -1701,6 +1755,7 @@ class CreditAccountsServiceV310 extends SDKConstruct {
1701
1755
  }
1702
1756
  }
1703
1757
  }
1758
+ flushEncodable();
1704
1759
  if (routerGroupsConsumed !== routerCallGroups.length) {
1705
1760
  throw new Error(
1706
1761
  `assembleCaUpdateCalls: router call group mismatch (consumed ${routerGroupsConsumed}, got ${routerCallGroups.length})`
@@ -170,8 +170,8 @@ class SecuritizeRWAFactory extends BaseContract {
170
170
  /**
171
171
  * {@inheritDoc IRWAFactory.multicall}
172
172
  */
173
- multicall(creditAccount, calls, options) {
174
- const { tokensToRegister = [], signaturesToCache = [] } = options ?? {};
173
+ multicall(creditAccount, calls, args) {
174
+ const { tokensToRegister = [], signaturesToCache = [] } = args ?? {};
175
175
  return this.createRawTx({
176
176
  functionName: "multicall",
177
177
  args: [creditAccount, calls, tokensToRegister, signaturesToCache]
@@ -201,11 +201,33 @@ class SecuritizeRWAFactory extends BaseContract {
201
201
  requiredSignatures
202
202
  };
203
203
  }
204
+ /**
205
+ * {@inheritDoc IRWAFactory.getMissingRequirements}
206
+ *
207
+ * A required signature is omitted when `providedArgs.signaturesToCache`
208
+ * already carries a signature for the same token: the transaction caches it
209
+ * on-chain as part of the operation.
210
+ */
211
+ getMissingRequirements(requirements, providedArgs) {
212
+ const providedTokens = new AddressSet(
213
+ (providedArgs?.signaturesToCache ?? []).map((s) => s.token)
214
+ );
215
+ const requiredSignatures = requirements.requiredSignatures.filter(
216
+ (message) => !providedTokens.has(message.message.token)
217
+ );
218
+ if (requiredSignatures.length === 0) {
219
+ return void 0;
220
+ }
221
+ return {
222
+ type: RWA_FACTORY_SECURITIZE,
223
+ requiredSignatures
224
+ };
225
+ }
204
226
  /**
205
227
  * {@inheritDoc IRWAFactory.openCreditAccount}
206
228
  */
207
- openCreditAccount(creditManager, calls, options) {
208
- const { tokensToRegister = [], signaturesToCache = [] } = options ?? {};
229
+ openCreditAccount(creditManager, calls, args) {
230
+ const { tokensToRegister = [], signaturesToCache = [] } = args ?? {};
209
231
  return this.createRawTx({
210
232
  functionName: "openCreditAccount",
211
233
  args: [creditManager, calls, tokensToRegister, signaturesToCache]
@@ -15,6 +15,13 @@ class AddressSet extends Set {
15
15
  has(value) {
16
16
  return super.has(getAddress(value));
17
17
  }
18
+ difference(other) {
19
+ const result = new AddressSet(this.keys());
20
+ for (const key of other) {
21
+ result.delete(getAddress(key));
22
+ }
23
+ return result;
24
+ }
18
25
  /**
19
26
  * Returns all addresses as an array.
20
27
  **/
@@ -17,6 +17,6 @@ export interface ParseRWAFactoryOperationCalldataProps {
17
17
  * identical to {@link parseFacadeOperationCalldata}.
18
18
  *
19
19
  * Dispatches on the factory's contract type; supporting a new factory type
20
- * requires a new branch here plus new operation types in `types-rwa.ts`.
20
+ * requires a new branch here.
21
21
  */
22
22
  export declare function parseRWAFactoryOperationCalldata(props: ParseRWAFactoryOperationCalldataProps): RWAOperation;
@@ -1,5 +1,5 @@
1
1
  import type { Address } from "viem";
2
- import type { SecuritizeRegisterMessage } from "../../sdk/index.js";
2
+ import type { RWAOperationArgs } from "../../sdk/index.js";
3
3
  import type { InnerOperation } from "./types-facades.js";
4
4
  /**
5
5
  * Metadata shared by all RWA factory operations. RWA credit accounts are
@@ -13,36 +13,24 @@ export interface RWAOperationMetadata {
13
13
  creditManager: Address;
14
14
  creditFacade: Address;
15
15
  }
16
- export interface SecuritizeOpenCreditAccountOperation<Ext extends object = {}> extends RWAOperationMetadata {
17
- operation: "SecuritizeOpenCreditAccount";
16
+ export interface RWAOpenCreditAccountOperation<Ext extends object = {}> extends RWAOperationMetadata {
17
+ operation: "RWAOpenCreditAccount";
18
18
  multicall: InnerOperation<Ext>[];
19
19
  /**
20
- * DSToken addresses to register, decoded from calldata. Empty in the
21
- * template flow, where the real value comes from the factory's open-account
22
- * requirements.
20
+ * Factory-specific registration args decoded from calldata (for
21
+ * Securitize: `tokensToRegister`/`signaturesToCache`).
23
22
  */
24
- tokensToRegister: Address[];
25
- /**
26
- * EIP-712 registration signatures to store on-chain, decoded from calldata.
27
- * Empty in the template flow.
28
- */
29
- signaturesToCache: SecuritizeRegisterMessage[];
23
+ args: RWAOperationArgs;
30
24
  }
31
- /**
32
- * `SecuritizeRWAFactory.multicall` call: like the facade's `multicall`, plus
33
- * the factory-specific registration args.
34
- */
35
- export interface SecuritizeMulticallOperation<Ext extends object = {}> extends RWAOperationMetadata {
36
- operation: "SecuritizeMulticall";
25
+ export interface RWAMulticallOperation<Ext extends object = {}> extends RWAOperationMetadata {
26
+ operation: "RWAMulticall";
37
27
  creditAccount: Address;
38
28
  multicall: InnerOperation<Ext>[];
39
- /** DSToken addresses to register, decoded from calldata. */
40
- tokensToRegister: Address[];
41
- /** EIP-712 registration signatures to store on-chain, decoded from calldata. */
42
- signaturesToCache: SecuritizeRegisterMessage[];
29
+ /** Factory-specific registration args decoded from calldata. */
30
+ args: RWAOperationArgs;
43
31
  }
44
32
  /**
45
33
  * Discriminated union of all RWA factory operation types. Extend it when
46
34
  * adding support for a new RWA factory type.
47
35
  */
48
- export type RWAOperation<Ext extends object = {}> = SecuritizeOpenCreditAccountOperation<Ext> | SecuritizeMulticallOperation<Ext>;
36
+ export type RWAOperation<Ext extends object = {}> = RWAOpenCreditAccountOperation<Ext> | RWAMulticallOperation<Ext>;
@@ -1,6 +1,6 @@
1
- import { type Address, type ContractFunctionParameters } from "viem";
2
- import { type MulticallCallResult, Prerequisite } from "./Prerequisite.js";
3
- import type { PrerequisiteDetail, PrerequisiteResult } from "./types.js";
1
+ import { type Address } from "viem";
2
+ import { Prerequisite } from "./Prerequisite.js";
3
+ import type { PrerequisiteContext, PrerequisiteDetail, PrerequisiteResult } from "./types.js";
4
4
  export interface AllowancePrerequisiteProps {
5
5
  token: Address;
6
6
  owner: Address;
@@ -17,6 +17,5 @@ export declare class AllowancePrerequisite extends Prerequisite<"allowance"> {
17
17
  get kind(): "allowance";
18
18
  get title(): string;
19
19
  get detail(): PrerequisiteDetail<"allowance">;
20
- calls(): ContractFunctionParameters[];
21
- resolve(slice: MulticallCallResult[]): PrerequisiteResult<"allowance">;
20
+ protected check(ctx: PrerequisiteContext): Promise<PrerequisiteResult<"allowance">>;
22
21
  }
@@ -1,5 +1,5 @@
1
- import { type Address, type ContractFunctionParameters } from "viem";
2
- import { type MulticallCallResult, Prerequisite } from "./Prerequisite.js";
1
+ import { type Address } from "viem";
2
+ import { Prerequisite } from "./Prerequisite.js";
3
3
  import type { PrerequisiteContext, PrerequisiteDetail, PrerequisiteResult } from "./types.js";
4
4
  export interface BalancePrerequisiteProps {
5
5
  token: Address;
@@ -11,8 +11,7 @@ export interface BalancePrerequisiteProps {
11
11
  /**
12
12
  * Checks that `owner` holds a token balance >= `required`. The token is read
13
13
  * as an ERC-20, except for the native pseudo-address ({@link NATIVE_ADDRESS},
14
- * e.g. ETH-zapper deposits) whose balance is read via multicall3's
15
- * `getEthBalance`.
14
+ * e.g. ETH-zapper deposits) whose balance is read via `getBalance`.
16
15
  */
17
16
  export declare class BalancePrerequisite extends Prerequisite<"balance"> {
18
17
  #private;
@@ -21,6 +20,5 @@ export declare class BalancePrerequisite extends Prerequisite<"balance"> {
21
20
  get kind(): "balance";
22
21
  get title(): string;
23
22
  get detail(): PrerequisiteDetail<"balance">;
24
- calls(ctx: PrerequisiteContext): ContractFunctionParameters[];
25
- resolve(slice: MulticallCallResult[]): PrerequisiteResult<"balance">;
23
+ protected check(ctx: PrerequisiteContext): Promise<PrerequisiteResult<"balance">>;
26
24
  }
@@ -1,37 +1,10 @@
1
- import { type ContractFunctionParameters } from "viem";
2
- import type { PrerequisiteContext, PrerequisiteDetail, PrerequisiteError, PrerequisiteKind, PrerequisiteResult } from "./types.js";
1
+ import type { PrerequisiteContext, PrerequisiteDetail, PrerequisiteKind, PrerequisiteResult } from "./types.js";
3
2
  /**
4
- * One entry of a viem `multicall({ allowFailure: true })` response: either a
5
- * decoded `result` or the `error` that the call reverted with.
6
- */
7
- export type MulticallCallResult = {
8
- status: "success";
9
- result: unknown;
10
- error?: undefined;
11
- } | {
12
- status: "failure";
13
- result?: undefined;
14
- error: Error;
15
- };
16
- /**
17
- * Decodes an unknown error into a {@link PrerequisiteError} with a
18
- * human-readable revert reason. Mirrors the SDK's `extractCallError`: walk the
19
- * viem error chain for a {@link ContractFunctionRevertedError} and use its
20
- * `errorName`, otherwise fall back to the error's short message / name.
21
- */
22
- export declare function toPrerequisiteError(cause: unknown): PrerequisiteError;
23
- /**
24
- * A single verifiable prerequisite for a transaction. Subclasses describe the
25
- * on-chain reads they need ({@link calls}) and how to turn the multicall slice
26
- * into a {@link PrerequisiteResult} ({@link resolve}).
3
+ * A single verifiable prerequisite for a transaction. Subclasses implement
4
+ * {@link check} to perform whatever async reads they need (an ERC-20
5
+ * `readContract`, a compressor call, ...) and map the outcome into a
6
+ * {@link PrerequisiteResult}.
27
7
  *
28
- * Identity (`id`, `kind`, `title`) and build-time `detail` are exposed as
29
- * read-only accessors; subclasses back them with private fields. The shared
30
- * {@link satisfiedResult}/{@link errorResult} helpers are `protected` so each
31
- * subclass builds results consistently.
32
- *
33
- * The same instance can be verified standalone via {@link verify} or batched
34
- * together with others by `verifyPrerequisites`.
35
8
  */
36
9
  export declare abstract class Prerequisite<K extends PrerequisiteKind> {
37
10
  /** Stable identifier, used as a React key and for deduplication. */
@@ -42,19 +15,22 @@ export declare abstract class Prerequisite<K extends PrerequisiteKind> {
42
15
  abstract get title(): string;
43
16
  /** Inputs known before reading the chain (no `actual` yet). */
44
17
  abstract get detail(): PrerequisiteDetail<K>;
45
- /** Contract reads this check needs (usually one). */
46
- abstract calls(ctx: PrerequisiteContext): ContractFunctionParameters[];
47
- /** Maps this check's slice of the multicall response into a result. */
48
- abstract resolve(slice: MulticallCallResult[]): PrerequisiteResult<K>;
49
18
  /**
50
- * Verifies this prerequisite on its own using an `allowFailure` multicall.
51
- * Prefer `verifyPrerequisites` to batch several checks into one round-trip.
19
+ * Verifies this prerequisite. Never rejects: any error thrown by
20
+ * {@link check} (revert, RPC/network failure, compressor error) is
21
+ * normalized into an `error` result.
52
22
  */
53
23
  verify(ctx: PrerequisiteContext): Promise<PrerequisiteResult<K>>;
24
+ /**
25
+ * Performs the reads this prerequisite needs and maps them into a result.
26
+ * May throw on read failure; {@link verify} converts throws into `error`
27
+ * results.
28
+ */
29
+ protected abstract check(ctx: PrerequisiteContext): Promise<PrerequisiteResult<K>>;
54
30
  /** Builds a successful result; `detail` carries the on-chain `actual`. */
55
31
  protected satisfiedResult(satisfied: boolean, detail: PrerequisiteDetail<K>): PrerequisiteResult<K>;
56
- /** Builds a failed result from a decoded read error. */
57
- protected errorResult(error: PrerequisiteError): PrerequisiteResult<K>;
32
+ /** Builds a failed result from a raw read error (normalized internally). */
33
+ protected errorResult(cause: unknown): PrerequisiteResult<K>;
58
34
  }
59
35
  /** Any prerequisite regardless of its kind, used for heterogeneous lists. */
60
36
  export type AnyPrerequisite = Prerequisite<PrerequisiteKind>;
@@ -1,14 +1,21 @@
1
- import type { Address, ContractFunctionParameters } from "viem";
2
- import type { RWAOpenAccountRequirements } from "../../sdk/index.js";
1
+ import type { Address } from "viem";
2
+ import type { RWAOperationArgs } from "../../sdk/index.js";
3
3
  import { Prerequisite } from "./Prerequisite.js";
4
- import type { PrerequisiteDetail, PrerequisiteResult } from "./types.js";
4
+ import type { PrerequisiteContext, PrerequisiteDetail, PrerequisiteResult } from "./types.js";
5
5
  export interface RWAOpenRequirementsPrerequisiteProps {
6
- /** Requirements already resolved via `sdk.accounts.getOpenAccountRequirements`. */
7
- requirements: RWAOpenAccountRequirements;
8
- /** RWA token (e.g. Securitize DSToken) the requirements were computed for. */
6
+ /** RWA token (e.g. Securitize DSToken) to check the requirements for. */
9
7
  token: Address;
10
- /** RWA factory that produced the requirements. */
8
+ /** Credit manager of the RWA market. */
9
+ creditManager: Address;
10
+ /** RWA factory that gates the token. */
11
11
  factory: Address;
12
+ /**
13
+ * Factory-specific registration params already carried by the transaction
14
+ * calldata. Passed through to the factory's `getMissingRequirements`: e.g.
15
+ * signatures provided here need not be signed again, since the transaction
16
+ * itself caches them on-chain.
17
+ */
18
+ providedArgs?: RWAOperationArgs;
12
19
  title?: string;
13
20
  id?: string;
14
21
  }
@@ -16,12 +23,13 @@ export interface RWAOpenRequirementsPrerequisiteProps {
16
23
  * Checks that the borrower has fulfilled the RWA factory's open-account
17
24
  * requirements (issuer-side token registration, EIP-712 signatures).
18
25
  *
19
- * Unlike `allowance`/`balance`, the requirements cannot be derived from plain
20
- * ERC-20 multicall reads: they come from an async compressor read performed by
21
- * `sdk.accounts.getOpenAccountRequirements`. The prerequisite is therefore
22
- * *pre-resolved*: it is constructed with the requirements already computed,
23
- * contributes no calls to the verification multicall, and `resolve` only
24
- * interprets the stored payload.
26
+ * The requirements are fetched at verify time via
27
+ * `sdk.accounts.getOpenAccountRequirements`, so re-running verification picks
28
+ * up off-chain progress (the borrower registering on the issuer's website or
29
+ * signing the factory's message) without rebuilding the prerequisites. The
30
+ * unfulfilled subset is computed by the factory itself
31
+ * (`IRWAFactory.getMissingRequirements`) and exposed on the result's
32
+ * `detail.missing`; the full requirements are kept on `detail.requirements`.
25
33
  */
26
34
  export declare class RWAOpenRequirementsPrerequisite extends Prerequisite<"rwaOpenRequirements"> {
27
35
  #private;
@@ -30,7 +38,5 @@ export declare class RWAOpenRequirementsPrerequisite extends Prerequisite<"rwaOp
30
38
  get kind(): "rwaOpenRequirements";
31
39
  get title(): string;
32
40
  get detail(): PrerequisiteDetail<"rwaOpenRequirements">;
33
- /** Pre-resolved: no on-chain reads to contribute. */
34
- calls(): ContractFunctionParameters[];
35
- resolve(): PrerequisiteResult<"rwaOpenRequirements">;
41
+ protected check(ctx: PrerequisiteContext): Promise<PrerequisiteResult<"rwaOpenRequirements">>;
36
42
  }
@@ -0,0 +1,14 @@
1
+ import { type GetApprovalAddressProps } from "../../sdk/index.js";
2
+ import type { InnerOperation } from "../parse/index.js";
3
+ import type { AnyPrerequisite } from "./Prerequisite.js";
4
+ import type { PrerequisiteContext } from "./types.js";
5
+ /**
6
+ * For every `addCollateral` inside a multicall, requires the wallet to approve
7
+ * and hold the collateral token for the collateral spender.
8
+ *
9
+ * The approval spender is resolved via {@link OnchainSDK.accounts.getApprovalAddress}:
10
+ * the credit manager for classic markets, but a per-investor "special wallet"
11
+ * for RWA markets. The resolution happens at build time because the spender is
12
+ * part of the prerequisite's identity and UI detail.
13
+ */
14
+ export declare function buildCollateralPrerequisites(spenderOptions: GetApprovalAddressProps, multicall: InnerOperation[], ctx: PrerequisiteContext): Promise<AnyPrerequisite[]>;
@@ -0,0 +1,8 @@
1
+ import type { PartialLiquidationOperation } from "../parse/index.js";
2
+ import type { AnyPrerequisite } from "./Prerequisite.js";
3
+ import type { PrerequisiteContext } from "./types.js";
4
+ /**
5
+ * Partial liquidation pulls the repaid underlying from the liquidator: they
6
+ * must have approved it to the credit manager and hold enough of it.
7
+ */
8
+ export declare function buildPartialLiquidationPrerequisites(tx: PartialLiquidationOperation, ctx: PrerequisiteContext): AnyPrerequisite[];
@@ -0,0 +1,8 @@
1
+ import { type Address } from "viem";
2
+ import type { PoolOperation } from "../parse/index.js";
3
+ import type { AnyPrerequisite } from "./Prerequisite.js";
4
+ /**
5
+ * Prerequisites for ERC4626 pool operations (deposit, mint, withdraw, redeem),
6
+ * either direct or zapper-routed.
7
+ */
8
+ export declare function buildPoolPrerequisites(tx: PoolOperation, wallet: Address): AnyPrerequisite[];
@@ -0,0 +1,21 @@
1
+ import type { Address } from "viem";
2
+ import { type RWAOperationArgs } from "../../sdk/index.js";
3
+ import type { InnerOperation } from "../parse/index.js";
4
+ import type { AnyPrerequisite } from "./Prerequisite.js";
5
+ import type { PrerequisiteContext } from "./types.js";
6
+ /**
7
+ * For RWA-factory open-account operations (e.g. `RWAOpenCreditAccount`),
8
+ * builds a prerequisite per RWA token the multicall touches; each one checks
9
+ * the factory's open-account requirements (issuer-side token registration,
10
+ * EIP-712 signatures) at verify time.
11
+ *
12
+ * The `providedArgs` params are the registration values decoded from the
13
+ * transaction calldata itself: signatures already included there satisfy the
14
+ * signature requirement, so a fully built transaction is not reported as
15
+ * missing signatures. In the template flow they are empty and the verified
16
+ * result's `detail.missing` describes what is still unfulfilled; acting on it
17
+ * (signing, rebuilding calldata) is up to the consumer.
18
+ *
19
+ * Returns nothing for non-RWA markets.
20
+ */
21
+ export declare function buildRWAPrerequisites(multicall: InnerOperation[], creditManager: Address, providedArgs: RWAOperationArgs, ctx: PrerequisiteContext): AnyPrerequisite[];
@@ -0,0 +1,27 @@
1
+ import type { PluginsMap } from "../../sdk/index.js";
2
+ import { type ParseOperationCalldataInput } from "../parse/parseOperationCalldata.js";
3
+ import type { AnyPrerequisiteResult } from "./types.js";
4
+ /**
5
+ * Input of {@link checkPrerequisites}: the same raw-calldata input as the
6
+ * internal calldata parser, plus an optional block to read at.
7
+ */
8
+ export type CheckPrerequisitesInput<P extends PluginsMap = PluginsMap> = ParseOperationCalldataInput<P> & {
9
+ /** Block to read at; defaults to latest. Only set for testnet forks. */
10
+ blockNumber?: bigint;
11
+ };
12
+ /**
13
+ * Derives and verifies the on-chain prerequisites for an operation given its
14
+ * raw calldata: the conditions that must hold for the call not to revert and
15
+ * that we can verify with the SDK (token approvals, wallet balances, RWA
16
+ * open-account requirements).
17
+ *
18
+ * Each prerequisite performs its own reads and resolves into an
19
+ * {@link AnyPrerequisiteResult} (`satisfied` or `error`).
20
+ *
21
+ * Only *sender-actionable* prerequisites belong here: conditions the LP
22
+ * provider or borrower can fix themselves before retrying (e.g. approve a
23
+ * token, top up a balance, sign the RWA factory's messages). Non-actionable
24
+ * protocol/admin state (e.g. pool is paused) is intentionally out of scope,
25
+ * since the user cannot resolve it.
26
+ */
27
+ export declare function checkPrerequisites<P extends PluginsMap>(input: CheckPrerequisitesInput<P>): Promise<AnyPrerequisiteResult[]>;
@@ -0,0 +1,15 @@
1
+ import type { Address } from "viem";
2
+ import type { AnyPrerequisite } from "./Prerequisite.js";
3
+ export interface AllowanceAndBalanceProps {
4
+ token: Address;
5
+ owner: Address;
6
+ spender: Address;
7
+ required: bigint;
8
+ allowanceTitle: string;
9
+ balanceTitle: string;
10
+ }
11
+ /**
12
+ * The most common prerequisite pair: `owner` must have approved `spender` for
13
+ * `required` of `token` and must hold at least that much of it.
14
+ */
15
+ export declare function allowanceAndBalance(props: AllowanceAndBalanceProps): AnyPrerequisite[];
@@ -1,8 +1,6 @@
1
1
  export * from "./AllowancePrerequisite.js";
2
2
  export * from "./BalancePrerequisite.js";
3
- export * from "./buildPrerequisites.js";
3
+ export * from "./checkPrerequisites.js";
4
4
  export * from "./Prerequisite.js";
5
- export * from "./prepareAction.js";
6
5
  export * from "./RWAOpenRequirementsPrerequisite.js";
7
- export * from "./runPrerequisites.js";
8
6
  export * from "./types.js";
@@ -1,5 +1,5 @@
1
1
  import type { Address } from "viem";
2
- import type { OnchainSDK, RWAOpenAccountRequirements } from "../../sdk/index.js";
2
+ import type { OnchainSDK, RWAMissingOpenAccountRequirements, RWAOpenAccountRequirements } from "../../sdk/index.js";
3
3
  /**
4
4
  * Extension point that ties each prerequisite kind to its detail payload.
5
5
  *
@@ -33,7 +33,26 @@ export interface PrerequisiteDetailMap {
33
33
  * registration and/or EIP-712 signatures the borrower must provide before
34
34
  * opening a credit account in an RWA market.
35
35
  */
36
- rwaOpenRequirements: RWAOpenAccountRequirements;
36
+ rwaOpenRequirements: {
37
+ /** RWA token (e.g. Securitize DSToken) the requirements are checked for. */
38
+ token: Address;
39
+ /** Credit manager of the RWA market. */
40
+ creditManager: Address;
41
+ /** RWA factory that gates the token. */
42
+ factory: Address;
43
+ /**
44
+ * Full requirements fetched at verify time via
45
+ * `sdk.accounts.getOpenAccountRequirements`; only present when the read
46
+ * succeeded and the factory gates the token.
47
+ */
48
+ requirements?: RWAOpenAccountRequirements;
49
+ /**
50
+ * Subset of `requirements` still unfulfilled, computed by the RWA
51
+ * factory at verify time (`IRWAFactory.getMissingRequirements`);
52
+ * `undefined` when everything is satisfied.
53
+ */
54
+ missing?: RWAMissingOpenAccountRequirements;
55
+ };
37
56
  }
38
57
  export type PrerequisiteKind = keyof PrerequisiteDetailMap;
39
58
  export type PrerequisiteDetail<K extends PrerequisiteKind = PrerequisiteKind> = PrerequisiteDetailMap[K];