@morpho-org/evm-simulation 4.2.0 → 5.0.0

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 (160) hide show
  1. package/README.md +109 -17
  2. package/lib/cjs/authorizations.d.ts +95 -0
  3. package/lib/cjs/authorizations.js +2 -0
  4. package/lib/cjs/errors.d.ts +207 -13
  5. package/lib/cjs/errors.js +197 -13
  6. package/lib/cjs/index.d.ts +10 -2
  7. package/lib/cjs/index.js +19 -3
  8. package/lib/cjs/limits.d.ts +112 -0
  9. package/lib/cjs/limits.js +50 -0
  10. package/lib/cjs/params.d.ts +25 -0
  11. package/lib/cjs/params.js +5 -0
  12. package/lib/cjs/requirements/index.d.ts +1 -0
  13. package/lib/cjs/requirements/index.js +5 -0
  14. package/lib/cjs/requirements/requirements.d.ts +64 -0
  15. package/lib/cjs/requirements/requirements.js +149 -0
  16. package/lib/cjs/result.d.ts +49 -0
  17. package/lib/cjs/result.js +2 -0
  18. package/lib/cjs/simulate/asset-changes.d.ts +7 -5
  19. package/lib/cjs/simulate/asset-changes.js +6 -5
  20. package/lib/cjs/simulate/backends/client.d.ts +16 -0
  21. package/lib/cjs/simulate/backends/client.js +27 -0
  22. package/lib/cjs/simulate/backends/eth-simulate-v1.d.ts +66 -20
  23. package/lib/cjs/simulate/backends/eth-simulate-v1.js +172 -98
  24. package/lib/cjs/simulate/backends/index.d.ts +1 -2
  25. package/lib/cjs/simulate/backends/index.js +3 -4
  26. package/lib/cjs/simulate/backends/parse-response.d.ts +69 -0
  27. package/lib/cjs/simulate/backends/parse-response.js +168 -0
  28. package/lib/cjs/simulate/backends/resolve-assets.d.ts +26 -0
  29. package/lib/cjs/simulate/backends/resolve-assets.js +111 -0
  30. package/lib/cjs/simulate/backends/resolve-pinned-block.d.ts +33 -0
  31. package/lib/cjs/simulate/backends/resolve-pinned-block.js +46 -0
  32. package/lib/cjs/simulate/context.d.ts +14 -0
  33. package/lib/cjs/simulate/context.js +2 -0
  34. package/lib/cjs/simulate/measurement-plan.d.ts +29 -0
  35. package/lib/cjs/simulate/measurement-plan.js +115 -0
  36. package/lib/cjs/simulate/parsing/transfers.d.ts +4 -3
  37. package/lib/cjs/simulate/parsing/transfers.js +4 -6
  38. package/lib/cjs/simulate/pipeline/bundles-retention.d.ts +7 -10
  39. package/lib/cjs/simulate/pipeline/bundles-retention.js +11 -14
  40. package/lib/cjs/simulate/pipeline/resolve-chain.d.ts +5 -11
  41. package/lib/cjs/simulate/pipeline/resolve-chain.js +5 -10
  42. package/lib/cjs/simulate/plan/plan-execution.d.ts +58 -0
  43. package/lib/cjs/simulate/plan/plan-execution.js +75 -0
  44. package/lib/cjs/simulate/prepare.d.ts +30 -0
  45. package/lib/cjs/simulate/prepare.js +80 -0
  46. package/lib/cjs/simulate/request/index.d.ts +1 -0
  47. package/lib/cjs/simulate/request/index.js +5 -0
  48. package/lib/cjs/simulate/request/parse-request.d.ts +44 -0
  49. package/lib/cjs/simulate/request/parse-request.js +759 -0
  50. package/lib/cjs/simulate/result.d.ts +19 -0
  51. package/lib/cjs/simulate/result.js +29 -0
  52. package/lib/cjs/simulate/run-simulation.d.ts +18 -0
  53. package/lib/cjs/simulate/run-simulation.js +151 -0
  54. package/lib/cjs/simulate/simulate.d.ts +100 -50
  55. package/lib/cjs/simulate/simulate.js +99 -81
  56. package/lib/cjs/simulate/slippage.d.ts +18 -0
  57. package/lib/cjs/simulate/slippage.js +41 -0
  58. package/lib/cjs/simulate/state/contract.d.ts +19 -0
  59. package/lib/cjs/simulate/state/contract.js +2 -0
  60. package/lib/cjs/simulate/state/read-state.d.ts +60 -0
  61. package/lib/cjs/simulate/state/read-state.js +148 -0
  62. package/lib/cjs/simulate/verify-slippage.d.ts +24 -0
  63. package/lib/cjs/simulate/verify-slippage.js +85 -0
  64. package/lib/cjs/types.d.ts +35 -95
  65. package/lib/esm/authorizations.d.ts +95 -0
  66. package/lib/esm/authorizations.js +1 -0
  67. package/lib/esm/errors.d.ts +207 -13
  68. package/lib/esm/errors.js +189 -12
  69. package/lib/esm/index.d.ts +10 -2
  70. package/lib/esm/index.js +4 -1
  71. package/lib/esm/limits.d.ts +112 -0
  72. package/lib/esm/limits.js +47 -0
  73. package/lib/esm/params.d.ts +25 -0
  74. package/lib/esm/params.js +2 -0
  75. package/lib/esm/requirements/index.d.ts +1 -0
  76. package/lib/esm/requirements/index.js +1 -0
  77. package/lib/esm/requirements/requirements.d.ts +64 -0
  78. package/lib/esm/requirements/requirements.js +146 -0
  79. package/lib/esm/result.d.ts +49 -0
  80. package/lib/esm/result.js +1 -0
  81. package/lib/esm/simulate/asset-changes.d.ts +7 -5
  82. package/lib/esm/simulate/asset-changes.js +6 -5
  83. package/lib/esm/simulate/backends/client.d.ts +16 -0
  84. package/lib/esm/simulate/backends/client.js +24 -0
  85. package/lib/esm/simulate/backends/eth-simulate-v1.d.ts +66 -20
  86. package/lib/esm/simulate/backends/eth-simulate-v1.js +170 -99
  87. package/lib/esm/simulate/backends/index.d.ts +1 -2
  88. package/lib/esm/simulate/backends/index.js +1 -2
  89. package/lib/esm/simulate/backends/parse-response.d.ts +69 -0
  90. package/lib/esm/simulate/backends/parse-response.js +165 -0
  91. package/lib/esm/simulate/backends/resolve-assets.d.ts +26 -0
  92. package/lib/esm/simulate/backends/resolve-assets.js +108 -0
  93. package/lib/esm/simulate/backends/resolve-pinned-block.d.ts +33 -0
  94. package/lib/esm/simulate/backends/resolve-pinned-block.js +43 -0
  95. package/lib/esm/simulate/context.d.ts +14 -0
  96. package/lib/esm/simulate/context.js +1 -0
  97. package/lib/esm/simulate/measurement-plan.d.ts +29 -0
  98. package/lib/esm/simulate/measurement-plan.js +112 -0
  99. package/lib/esm/simulate/parsing/transfers.d.ts +4 -3
  100. package/lib/esm/simulate/parsing/transfers.js +4 -6
  101. package/lib/esm/simulate/pipeline/bundles-retention.d.ts +7 -10
  102. package/lib/esm/simulate/pipeline/bundles-retention.js +11 -14
  103. package/lib/esm/simulate/pipeline/resolve-chain.d.ts +5 -11
  104. package/lib/esm/simulate/pipeline/resolve-chain.js +5 -10
  105. package/lib/esm/simulate/plan/plan-execution.d.ts +58 -0
  106. package/lib/esm/simulate/plan/plan-execution.js +72 -0
  107. package/lib/esm/simulate/prepare.d.ts +30 -0
  108. package/lib/esm/simulate/prepare.js +77 -0
  109. package/lib/esm/simulate/request/index.d.ts +1 -0
  110. package/lib/esm/simulate/request/index.js +1 -0
  111. package/lib/esm/simulate/request/parse-request.d.ts +44 -0
  112. package/lib/esm/simulate/request/parse-request.js +756 -0
  113. package/lib/esm/simulate/result.d.ts +19 -0
  114. package/lib/esm/simulate/result.js +26 -0
  115. package/lib/esm/simulate/run-simulation.d.ts +18 -0
  116. package/lib/esm/simulate/run-simulation.js +148 -0
  117. package/lib/esm/simulate/simulate.d.ts +100 -50
  118. package/lib/esm/simulate/simulate.js +99 -81
  119. package/lib/esm/simulate/slippage.d.ts +18 -0
  120. package/lib/esm/simulate/slippage.js +38 -0
  121. package/lib/esm/simulate/state/contract.d.ts +19 -0
  122. package/lib/esm/simulate/state/contract.js +1 -0
  123. package/lib/esm/simulate/state/read-state.d.ts +60 -0
  124. package/lib/esm/simulate/state/read-state.js +144 -0
  125. package/lib/esm/simulate/verify-slippage.d.ts +24 -0
  126. package/lib/esm/simulate/verify-slippage.js +82 -0
  127. package/lib/esm/types.d.ts +35 -95
  128. package/package.json +7 -6
  129. package/lib/cjs/simulate/authorizations/index.d.ts +0 -2
  130. package/lib/cjs/simulate/authorizations/index.js +0 -7
  131. package/lib/cjs/simulate/authorizations/resolve.d.ts +0 -9
  132. package/lib/cjs/simulate/authorizations/resolve.js +0 -29
  133. package/lib/cjs/simulate/authorizations/validate.d.ts +0 -6
  134. package/lib/cjs/simulate/authorizations/validate.js +0 -23
  135. package/lib/cjs/simulate/backends/tenderly-rpc.d.ts +0 -23
  136. package/lib/cjs/simulate/backends/tenderly-rpc.js +0 -234
  137. package/lib/cjs/simulate/pipeline/build-simulation-txs.d.ts +0 -17
  138. package/lib/cjs/simulate/pipeline/build-simulation-txs.js +0 -50
  139. package/lib/cjs/simulate/pipeline/execute-simulation.d.ts +0 -22
  140. package/lib/cjs/simulate/pipeline/execute-simulation.js +0 -82
  141. package/lib/cjs/simulate/pipeline/index.d.ts +0 -4
  142. package/lib/cjs/simulate/pipeline/index.js +0 -11
  143. package/lib/cjs/simulate/pipeline/validate-input.d.ts +0 -10
  144. package/lib/cjs/simulate/pipeline/validate-input.js +0 -55
  145. package/lib/esm/simulate/authorizations/index.d.ts +0 -2
  146. package/lib/esm/simulate/authorizations/index.js +0 -2
  147. package/lib/esm/simulate/authorizations/resolve.d.ts +0 -9
  148. package/lib/esm/simulate/authorizations/resolve.js +0 -26
  149. package/lib/esm/simulate/authorizations/validate.d.ts +0 -6
  150. package/lib/esm/simulate/authorizations/validate.js +0 -20
  151. package/lib/esm/simulate/backends/tenderly-rpc.d.ts +0 -23
  152. package/lib/esm/simulate/backends/tenderly-rpc.js +0 -231
  153. package/lib/esm/simulate/pipeline/build-simulation-txs.d.ts +0 -17
  154. package/lib/esm/simulate/pipeline/build-simulation-txs.js +0 -47
  155. package/lib/esm/simulate/pipeline/execute-simulation.d.ts +0 -22
  156. package/lib/esm/simulate/pipeline/execute-simulation.js +0 -79
  157. package/lib/esm/simulate/pipeline/index.d.ts +0 -4
  158. package/lib/esm/simulate/pipeline/index.js +0 -4
  159. package/lib/esm/simulate/pipeline/validate-input.d.ts +0 -10
  160. package/lib/esm/simulate/pipeline/validate-input.js +0 -52
@@ -0,0 +1,75 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.planExecution = planExecution;
4
+ const morpho_ts_1 = require("@morpho-org/morpho-ts");
5
+ const viem_1 = require("viem");
6
+ /**
7
+ * Plan the execution of a request as ordered `eth_simulateV1` calls.
8
+ *
9
+ * Sequence (design §plan order): every `before` state read → preparation
10
+ * calls → every user transaction → every `after` state read.
11
+ *
12
+ * Preparation calls run `from` the owner; state reads run `from`
13
+ * `zeroAddress` and never receive a public `txIdx`.
14
+ *
15
+ * @param request - The parsed request.
16
+ * @param owner - The bundle owner; sender of preparation calls.
17
+ * @param preparations - Ordered authorization preparations to simulate.
18
+ * @param reads - The full state-read list, replayed at `before` and `after`.
19
+ * @returns A deep-frozen {@link ExecutionPlan}; pure — equal inputs produce
20
+ * structurally equal plans.
21
+ * @internal
22
+ */
23
+ function planExecution(params) {
24
+ const { request, owner, preparations, reads } = params;
25
+ const calls = [];
26
+ for (const read of reads) {
27
+ calls.push({
28
+ type: "stateRead",
29
+ phase: "before",
30
+ read,
31
+ transaction: {
32
+ from: viem_1.zeroAddress,
33
+ to: read.to,
34
+ data: read.data,
35
+ value: 0n,
36
+ },
37
+ });
38
+ }
39
+ for (const preparation of preparations) {
40
+ preparation.calls.forEach((transaction, callIndex) => {
41
+ calls.push({
42
+ type: "preparation",
43
+ authorizationIndex: preparation.authorizationIndex,
44
+ callIndex,
45
+ transaction: { ...transaction, from: owner },
46
+ });
47
+ });
48
+ }
49
+ request.transactions.forEach((transaction, i) => {
50
+ calls.push({
51
+ type: "transaction",
52
+ transactionIndex: i,
53
+ transaction: {
54
+ from: transaction.from,
55
+ to: transaction.to,
56
+ data: transaction.data,
57
+ value: transaction.value ?? 0n,
58
+ },
59
+ });
60
+ });
61
+ for (const read of reads) {
62
+ calls.push({
63
+ type: "stateRead",
64
+ phase: "after",
65
+ read,
66
+ transaction: {
67
+ from: viem_1.zeroAddress,
68
+ to: read.to,
69
+ data: read.data,
70
+ value: 0n,
71
+ },
72
+ });
73
+ }
74
+ return (0, morpho_ts_1.deepFreeze)({ request, calls });
75
+ }
@@ -0,0 +1,30 @@
1
+ import { type Address } from "viem";
2
+ import type { SimulationAuthorization } from "../authorizations.js";
3
+ import type { SimulationTransaction } from "../types.js";
4
+ /** One authorization's preparation calls, in execution order. @internal */
5
+ export interface PlannedPreparation {
6
+ readonly authorizationIndex: number;
7
+ readonly calls: readonly Required<Readonly<SimulationTransaction>>[];
8
+ }
9
+ /**
10
+ * Build the ordered preparation calls a preview execution runs before the
11
+ * user transactions, so each pending wallet request is satisfied onchain.
12
+ *
13
+ * - `erc20Approval` → `approve(spender, amount)`; zero-reset entries produce
14
+ * their own `approve(0)` call in list order.
15
+ * - Signature variants (`erc2612Permit`, `permit2SignatureTransfer`) grant
16
+ * owner→bundle allowance for the previewed pull — a permit2 transfer makes
17
+ * no change to the nonce bitmap.
18
+ * - `blueAuthorization` / `blueAuthorizationSignature` →
19
+ * `setAuthorization(authorized, isAuthorized)`.
20
+ *
21
+ * Final mode produces no preparations. Successful execution proves only the
22
+ * simulated permissions, not the validity of a future signature.
23
+ *
24
+ * @internal
25
+ */
26
+ export declare function prepareAuthorizations(params: {
27
+ readonly authorizations: readonly SimulationAuthorization[];
28
+ readonly owner: Address;
29
+ readonly morpho: Address;
30
+ }): readonly PlannedPreparation[];
@@ -0,0 +1,80 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.prepareAuthorizations = prepareAuthorizations;
4
+ const abis_1 = require("@morpho-org/morpho-sdk/abis");
5
+ const morpho_ts_1 = require("@morpho-org/morpho-ts");
6
+ const viem_1 = require("viem");
7
+ // biome-ignore lint/complexity/useMaxParams: lookup helpers read clearest with positional arguments
8
+ const approvalTx = (owner, token, spender, amount) => ({
9
+ from: (0, viem_1.getAddress)(owner),
10
+ to: (0, viem_1.getAddress)(token),
11
+ data: (0, viem_1.encodeFunctionData)({
12
+ abi: viem_1.erc20Abi,
13
+ functionName: "approve",
14
+ args: [spender, amount],
15
+ }),
16
+ value: 0n,
17
+ });
18
+ /**
19
+ * Build the ordered preparation calls a preview execution runs before the
20
+ * user transactions, so each pending wallet request is satisfied onchain.
21
+ *
22
+ * - `erc20Approval` → `approve(spender, amount)`; zero-reset entries produce
23
+ * their own `approve(0)` call in list order.
24
+ * - Signature variants (`erc2612Permit`, `permit2SignatureTransfer`) grant
25
+ * owner→bundle allowance for the previewed pull — a permit2 transfer makes
26
+ * no change to the nonce bitmap.
27
+ * - `blueAuthorization` / `blueAuthorizationSignature` →
28
+ * `setAuthorization(authorized, isAuthorized)`.
29
+ *
30
+ * Final mode produces no preparations. Successful execution proves only the
31
+ * simulated permissions, not the validity of a future signature.
32
+ *
33
+ * @internal
34
+ */
35
+ function prepareAuthorizations(params) {
36
+ const { authorizations, owner, morpho } = params;
37
+ const preparations = [];
38
+ authorizations.forEach((auth, authorizationIndex) => {
39
+ const calls = [];
40
+ switch (auth.type) {
41
+ case "erc20Approval":
42
+ calls.push(approvalTx(owner, auth.token, auth.spender, auth.amount));
43
+ break;
44
+ case "erc2612Permit": {
45
+ const { message, domain } = auth.typedData;
46
+ calls.push(approvalTx(owner, domain.verifyingContract, message.spender, message.value));
47
+ break;
48
+ }
49
+ case "permit2SignatureTransfer": {
50
+ const { message } = auth.typedData;
51
+ calls.push(approvalTx(owner, message.permitted.token, message.spender, message.permitted.amount));
52
+ break;
53
+ }
54
+ case "blueAuthorization":
55
+ case "blueAuthorizationSignature": {
56
+ const { authorized, isAuthorized } = auth.type === "blueAuthorization" ? auth : auth.typedData.message;
57
+ calls.push({
58
+ from: (0, viem_1.getAddress)(owner),
59
+ to: (0, viem_1.getAddress)(morpho),
60
+ data: (0, viem_1.encodeFunctionData)({
61
+ abi: abis_1.blueAbi,
62
+ functionName: "setAuthorization",
63
+ args: [authorized, isAuthorized],
64
+ }),
65
+ value: 0n,
66
+ });
67
+ break;
68
+ }
69
+ default: {
70
+ const exhaustive = auth;
71
+ void exhaustive;
72
+ }
73
+ }
74
+ preparations.push({
75
+ authorizationIndex,
76
+ calls: (0, morpho_ts_1.deepFreeze)(calls),
77
+ });
78
+ });
79
+ return (0, morpho_ts_1.deepFreeze)(preparations);
80
+ }
@@ -0,0 +1 @@
1
+ export { parseRequest } from "./parse-request.js";
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseRequest = void 0;
4
+ var parse_request_js_1 = require("./parse-request.js");
5
+ Object.defineProperty(exports, "parseRequest", { enumerable: true, get: function () { return parse_request_js_1.parseRequest; } });
@@ -0,0 +1,44 @@
1
+ import { type Address, type BlockTag, type Hex } from "viem";
2
+ import type { SimulationAuthorization } from "../../authorizations.js";
3
+ import type { SimulationLimits } from "../../limits.js";
4
+ import { type SimulateParams, type SimulationMode } from "../../params.js";
5
+ /** A normalized user transaction: checksummed addresses, `value` defaulted to `0n`.
6
+ * @internal
7
+ */
8
+ export interface ParsedTransaction {
9
+ readonly from: Address;
10
+ readonly to: Address;
11
+ readonly data: Hex;
12
+ readonly value: bigint;
13
+ }
14
+ /** The output of {@link parseRequest}: the caller's input validated, checksummed and defaulted.
15
+ * @internal
16
+ */
17
+ export interface ParsedRequest {
18
+ readonly chainId: number;
19
+ readonly mode: SimulationMode;
20
+ readonly transactions: readonly ParsedTransaction[];
21
+ /** Always empty in final mode. */
22
+ readonly authorizations: readonly SimulationAuthorization[];
23
+ readonly blockNumber?: bigint | Exclude<BlockTag, "pending">;
24
+ readonly limits?: SimulationLimits;
25
+ }
26
+ /**
27
+ * Parse and normalize raw `simulate` input into a {@link ParsedRequest}.
28
+ *
29
+ * The public types own the key shape; runtime validation rejects unknown keys
30
+ * (`<path>.<key>: unknown field`), checks values — address and hex formats,
31
+ * uint256 ranges, typed-data field lists — and rejects legacy `{type: "approval"}` / `{type: "signature"}` authorizations and Permit2
32
+ * `PermitSingle` payloads rather than silently reinterpreting them. Cross-field
33
+ * rules then pin a single owner: every transaction `from` and every
34
+ * authorization owner must be the same checksummed address, typed-data domains
35
+ * must bind to `chainId`, and `mode: "final"` rejects authorizations outright.
36
+ *
37
+ * @param input - Caller input (`SimulateParams`-shaped).
38
+ * @returns A deep-frozen, checksummed request: `mode` explicit (`"final"`
39
+ * default), `authorizations` always an array, `value` defaulted to `0n`.
40
+ * @throws {SimulationValidationError} On any unknown key, value or cross-field
41
+ * violation.
42
+ * @internal
43
+ */
44
+ export declare function parseRequest(input: SimulateParams): ParsedRequest;