@lifi/composer-sdk
Version:
Public Composer SDK for building and submitting flows
1 lines • 3.99 kB
Source Map (JSON)
{"version":3,"sources":["../../src/run/simulate.ts"],"sourcesContent":["import type {\n ComposeCompileResult,\n SimulateRequest,\n SlotFinderRequirement,\n TrackedBalance,\n} from '@lifi/compose-spec';\n\n/**\n * Input for {@link buildSimulateRequest}: a compile result plus the fields a\n * simulation needs that a compiled flow does not carry.\n */\nexport interface BuildSimulateRequestInput {\n /**\n * A result from `builder.compile(...)` / `client.compile(...)`. Both\n * `success` and `partial` results are valid input — the helper does not\n * discriminate on `status`. A success result for an empty compiled program\n * (a settle-only `funding: 'external'` flow) carries no\n * `transactionRequest`; there is nothing to simulate and this helper\n * throws — submit the result's `actions` entries directly instead.\n */\n readonly result: ComposeCompileResult;\n /** EVM chain id. A compile result does not carry the target chain. */\n readonly chainId: number;\n /**\n * The transaction sender (`from`). A compile result does not carry the\n * signer, so it must be supplied here.\n */\n readonly signer: string;\n /**\n * Balances to watch across the simulation. These cannot be inferred from a\n * flow and must be supplied by the caller.\n */\n readonly trackedBalances: readonly TrackedBalance[];\n /** Optional funding instructions applied before simulation. */\n readonly requirements?: readonly SlotFinderRequirement[];\n /** Optional block to pin the simulation to. Omitted ⇒ chain head. */\n readonly block?: number;\n /**\n * Optional native value override. Defaults to the compiled\n * `transactionRequest.value` when omitted.\n */\n readonly value?: bigint | string;\n}\n\n/**\n * Assembles a {@link SimulateRequest} from a compile result and the\n * caller-supplied chain, signer, and tracked balances. Pure: performs no\n * network I/O, does not mutate its input, and returns a fresh object.\n *\n * Bridges the common case — simulate the transaction you just compiled —\n * without coupling raw-transaction simulation to the flow builder. The\n * `value` defaults to the compiled transaction's value unless overridden.\n *\n * @example\n * ```ts\n * const compiled = await builder.compile({ inputs: { ... }, signer: '0x1111...' });\n * const req = buildSimulateRequest({\n * result: compiled,\n * chainId: 1,\n * signer: '0x1111111111111111111111111111111111111111',\n * trackedBalances: [{ token: '0xA0b8...', owner: '0x1111...' }],\n * });\n * const sim = await sdk.client.simulate(req);\n * ```\n */\nexport const buildSimulateRequest = (\n input: BuildSimulateRequestInput,\n): SimulateRequest => {\n const { result, chainId, signer, trackedBalances, requirements, block } =\n input;\n // A settle-only flow authored with `funding: 'external'` compiles to an\n // empty program: the success response carries no `transactionRequest`, so\n // there is nothing to simulate — the required submissions are the\n // `actions` entries, submitted directly.\n if (result.transactionRequest === undefined) {\n throw new Error(\n 'buildSimulateRequest: the compile result has no transactionRequest (empty compiled program — a settle-only external-funding flow); submit the `actions` entries instead of simulating',\n );\n }\n return {\n chainId,\n from: signer,\n to: result.transactionRequest.to,\n data: result.transactionRequest.data,\n value: input.value ?? result.transactionRequest.value,\n block,\n requirements,\n trackedBalances,\n };\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAiEO,MAAM,uBAAuB,CAClC,UACoB;AACpB,QAAM,EAAE,QAAQ,SAAS,QAAQ,iBAAiB,cAAc,MAAM,IACpE;AAKF,MAAI,OAAO,uBAAuB,QAAW;AAC3C,UAAM,IAAI;AAAA,MACR;AAAA,IACF;AAAA,EACF;AACA,SAAO;AAAA,IACL;AAAA,IACA,MAAM;AAAA,IACN,IAAI,OAAO,mBAAmB;AAAA,IAC9B,MAAM,OAAO,mBAAmB;AAAA,IAChC,OAAO,MAAM,SAAS,OAAO,mBAAmB;AAAA,IAChD;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;","names":[]}