@lifi/compose-spec 0.2.0-staging.5 → 0.3.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.
- package/dist/index.cjs +448 -4
- package/dist/index.d.cts +1419 -115
- package/dist/index.d.ts +1419 -115
- package/dist/index.js +406 -4
- package/package.json +23 -3
- package/src/chainCapabilities.ts +174 -0
- package/src/compile.ts +44 -11
- package/src/constructors.ts +55 -53
- package/src/continuation.ts +278 -0
- package/src/descriptor.ts +151 -0
- package/src/flashloan.ts +8 -8
- package/src/flow.ts +46 -46
- package/src/flowSchema.ts +61 -27
- package/src/hexPatterns.ts +16 -0
- package/src/index.ts +109 -17
- package/src/materialiserMetadata.ts +1 -1
- package/src/ref.ts +21 -21
- package/src/resource.ts +19 -9
- package/src/simulate.ts +183 -0
- package/src/zodSchemas.ts +208 -48
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
import { Schema } from 'effect';
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
canonicalNativeAddress,
|
|
5
|
+
isAsErc20Chain,
|
|
6
|
+
normalizeNativeTokenAddress,
|
|
7
|
+
} from './chainCapabilities.js';
|
|
8
|
+
import { ChainIdSchema, FlowSchema } from './flowSchema.js';
|
|
9
|
+
|
|
10
|
+
// The op id of the self-describing commitment carrier node embedded in a
|
|
11
|
+
// persisted committed continuation action (verified continuations, plan GD-8,
|
|
12
|
+
// seam C5). It is wire vocabulary — the node's `op` value inside a re-submitted
|
|
13
|
+
// `Flow` — so it lives here as the single source of truth. The op definition
|
|
14
|
+
// (compose-builtins) and the settlement-echo trigger (compose-core) both import
|
|
15
|
+
// this constant; a drift between them would make the echo fail open (a genuine
|
|
16
|
+
// settlement fill answered as an ordinary flow with no descriptor).
|
|
17
|
+
export const CONTINUATION_COMMITMENT_OP_ID = 'continuation.commitment';
|
|
18
|
+
|
|
19
|
+
// A single committed outcome of a deferred settlement: at least `minAmount` of
|
|
20
|
+
// `token` must be delivered to `destination` on `chainId`. Amounts are decimal
|
|
21
|
+
// strings on the wire because JSON has no bigint; `chainId` reuses the shared
|
|
22
|
+
// `ChainIdSchema` from `flowSchema.ts`.
|
|
23
|
+
const RawContinuationOutcomeSchema = Schema.Struct({
|
|
24
|
+
token: Schema.String,
|
|
25
|
+
minAmount: Schema.String,
|
|
26
|
+
destination: Schema.String,
|
|
27
|
+
chainId: ChainIdSchema,
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
// Decode transform mirroring `ResourceSchema`: `token` is a bare string that
|
|
31
|
+
// never flows through `ResourceSchema`, so on `as-erc20` chains the native
|
|
32
|
+
// sentinel is rewritten to the chain's ERC-20 here — otherwise downstream would
|
|
33
|
+
// treat the delivery asset as native. Encode is the identity (the ERC-20 form
|
|
34
|
+
// is valid wire input).
|
|
35
|
+
export const ContinuationOutcomeSchema = Schema.transform(
|
|
36
|
+
RawContinuationOutcomeSchema,
|
|
37
|
+
Schema.typeSchema(RawContinuationOutcomeSchema),
|
|
38
|
+
{
|
|
39
|
+
strict: true,
|
|
40
|
+
decode: (raw) => ({
|
|
41
|
+
...raw,
|
|
42
|
+
token: normalizeNativeTokenAddress(raw.token, raw.chainId),
|
|
43
|
+
}),
|
|
44
|
+
encode: (outcome) => outcome,
|
|
45
|
+
},
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
// Backend-neutral config for the `continuation.settle` op. Note: the funding
|
|
49
|
+
// input is NOT a config field — the deferred step's funding resource/amount is
|
|
50
|
+
// wired via the node's `bind.input` (see M3b). Only the committed terms below
|
|
51
|
+
// (`outcomes`/`continuationFlow`/`validity`/`perpetual`/`provider`) are the
|
|
52
|
+
// node's `config`.
|
|
53
|
+
//
|
|
54
|
+
// `continuationFlow` is an inlined Flow document that the settlement backend
|
|
55
|
+
// stores and runs at settlement time against the deferred proceeds (used by the
|
|
56
|
+
// cross-chain path). It is independent of the Flow's top-level `continuation`
|
|
57
|
+
// pointer. `FlowSchema` lives in the same package, so importing it here keeps
|
|
58
|
+
// `compose-spec` dependency-free.
|
|
59
|
+
export const ContinuationIntentSchema = Schema.Struct({
|
|
60
|
+
// `NonEmptyArray` enforces ≥1 outcome at the parse boundary, so downstream needs no guard.
|
|
61
|
+
outcomes: Schema.NonEmptyArray(ContinuationOutcomeSchema),
|
|
62
|
+
continuationFlow: Schema.optional(FlowSchema),
|
|
63
|
+
validity: Schema.Struct({ expiresAtMs: Schema.Number }),
|
|
64
|
+
perpetual: Schema.optional(Schema.Boolean),
|
|
65
|
+
provider: Schema.optional(Schema.String),
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
// The phase-1 action a client must take after compiling a deferred flow.
|
|
69
|
+
//
|
|
70
|
+
// - `transaction` — a funding transaction (fund an escrow / call a protocol
|
|
71
|
+
// open). Usually the client funds via the main response-level
|
|
72
|
+
// `transactionRequest`; a venue whose phase-1 tx differs may instead emit an
|
|
73
|
+
// explicit `transactionRequest` override here.
|
|
74
|
+
// - `signables` — EIP-712 order(s) to sign (for signed-order venues; not
|
|
75
|
+
// produced in v1, defined for the seam).
|
|
76
|
+
// - `none` — nothing to sign now.
|
|
77
|
+
export const Phase1ArtifactSchema = Schema.Union(
|
|
78
|
+
Schema.Struct({
|
|
79
|
+
kind: Schema.Literal('transaction'),
|
|
80
|
+
transactionRequest: Schema.optional(
|
|
81
|
+
Schema.Struct({
|
|
82
|
+
to: Schema.String,
|
|
83
|
+
data: Schema.String,
|
|
84
|
+
value: Schema.String,
|
|
85
|
+
}),
|
|
86
|
+
),
|
|
87
|
+
}),
|
|
88
|
+
Schema.Struct({
|
|
89
|
+
kind: Schema.Literal('signables'),
|
|
90
|
+
typedData: Schema.Array(Schema.Unknown),
|
|
91
|
+
}),
|
|
92
|
+
Schema.Struct({ kind: Schema.Literal('none') }),
|
|
93
|
+
);
|
|
94
|
+
|
|
95
|
+
// A readiness predicate evaluated against on-chain state. It is both a
|
|
96
|
+
// client-pollable check ("is the second leg ready yet?") and an on-chain
|
|
97
|
+
// assertion compiled into the second leg, so a premature re-submission reverts
|
|
98
|
+
// safely. Amounts are decimal strings on the wire because JSON has no bigint.
|
|
99
|
+
//
|
|
100
|
+
// - `erc20BalanceGte` — `owner`'s balance of `token` on `chainId` is at least
|
|
101
|
+
// `minAmount`.
|
|
102
|
+
// - `nativeBalanceGte` — `owner`'s native balance on `chainId` is at least
|
|
103
|
+
// `minAmount`.
|
|
104
|
+
// - `call` — a generic `eth_call` to `to` with `calldata`, comparing the
|
|
105
|
+
// returned value against `value` using `comparator`.
|
|
106
|
+
//
|
|
107
|
+
// Address/bytes fields (`token`/`owner`/`to`/`calldata`) require a `0x` prefix.
|
|
108
|
+
// Validation is intentionally lenient (prefix only, not full 40-hex) to match
|
|
109
|
+
// the rest of the compose wire format — `ContinuationOutcomeSchema.token`/
|
|
110
|
+
// `destination` are bare strings too — and to keep placeholder fixtures usable;
|
|
111
|
+
// the exact address is validated by `toAddress` when the check is lowered to an
|
|
112
|
+
// on-chain assertion. The prefix gate still rejects empty/garbage values early.
|
|
113
|
+
export const HexStringSchema = Schema.String.pipe(Schema.startsWith('0x'));
|
|
114
|
+
|
|
115
|
+
// A balance owner in a derivation-only assertion spec. Concrete addresses stay
|
|
116
|
+
// hex; the two symbolic owners `"escrow"` / `"delivery"` are resolved to
|
|
117
|
+
// validator-injected register handles at settlement time (see the
|
|
118
|
+
// verified-continuations derivation). This widening applies ONLY to the
|
|
119
|
+
// derivation-only delta members below — never to the wire `Check` members. A
|
|
120
|
+
// readiness `Check` is authored/re-submitted through `/compose` and its owner
|
|
121
|
+
// has no injected slot, so a symbolic owner there cannot be resolved (it would
|
|
122
|
+
// throw at lowering); keeping `Check` owners hex-only rejects such a value
|
|
123
|
+
// cleanly at validation instead.
|
|
124
|
+
export const AssertionOwnerSchema = Schema.Union(
|
|
125
|
+
HexStringSchema,
|
|
126
|
+
Schema.Literal('escrow', 'delivery'),
|
|
127
|
+
);
|
|
128
|
+
export type AssertionOwner = typeof AssertionOwnerSchema.Type;
|
|
129
|
+
|
|
130
|
+
// The three `CheckSchema` members are exported by name so the derivation
|
|
131
|
+
// (`AssertionSpec`) can reuse them without re-declaring their shape. Their
|
|
132
|
+
// `owner` is hex-only: a captured invariant always resolves to a concrete
|
|
133
|
+
// address, and a readiness `Check` never carries a symbolic owner.
|
|
134
|
+
export const Erc20BalanceGteSpecSchema = Schema.Struct({
|
|
135
|
+
kind: Schema.Literal('erc20BalanceGte'),
|
|
136
|
+
chainId: ChainIdSchema,
|
|
137
|
+
token: HexStringSchema,
|
|
138
|
+
owner: HexStringSchema,
|
|
139
|
+
minAmount: Schema.String,
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
export const NativeBalanceGteSpecSchema = Schema.Struct({
|
|
143
|
+
kind: Schema.Literal('nativeBalanceGte'),
|
|
144
|
+
chainId: ChainIdSchema,
|
|
145
|
+
owner: HexStringSchema,
|
|
146
|
+
minAmount: Schema.String,
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
export const CallCheckSpecSchema = Schema.Struct({
|
|
150
|
+
kind: Schema.Literal('call'),
|
|
151
|
+
chainId: ChainIdSchema,
|
|
152
|
+
to: HexStringSchema,
|
|
153
|
+
calldata: HexStringSchema,
|
|
154
|
+
comparator: Schema.Literal('gte', 'eq', 'lte'),
|
|
155
|
+
value: Schema.String,
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
// The equivalent `erc20BalanceGte` of a `nativeBalanceGte` spec on an
|
|
159
|
+
// `as-erc20` chain: the chain's stand-in ERC-20 (PathUSD on Tempo) as `token`,
|
|
160
|
+
// every other field carried through. Shared by `CheckSchema` and the descriptor
|
|
161
|
+
// union `AssertedPredicateSchema` so both wire shapes rewrite identically —
|
|
162
|
+
// authoring↔settlement hash parity requires the readiness capture and the
|
|
163
|
+
// committed re-assertion to derive the *same* program. `owner` is `string` for
|
|
164
|
+
// both callers (`AssertionOwnerSchema.Type` collapses to `string`).
|
|
165
|
+
export const asErc20BalanceGteCheck = (
|
|
166
|
+
chainId: number,
|
|
167
|
+
owner: string,
|
|
168
|
+
minAmount: string,
|
|
169
|
+
) => ({
|
|
170
|
+
kind: 'erc20BalanceGte' as const,
|
|
171
|
+
chainId,
|
|
172
|
+
token: canonicalNativeAddress(chainId),
|
|
173
|
+
owner,
|
|
174
|
+
minAmount,
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
// The pre-transform union of the three named members.
|
|
178
|
+
const RawCheckSchema = Schema.Union(
|
|
179
|
+
Erc20BalanceGteSpecSchema,
|
|
180
|
+
NativeBalanceGteSpecSchema,
|
|
181
|
+
CallCheckSpecSchema,
|
|
182
|
+
);
|
|
183
|
+
|
|
184
|
+
// Decode transform mirroring `ContinuationOutcomeSchema`: a `nativeBalanceGte`
|
|
185
|
+
// check carries no `token`, so on an `as-erc20` chain it is rewritten into an
|
|
186
|
+
// `erc20BalanceGte` of the chain's stand-in ERC-20. Without this it lowers to
|
|
187
|
+
// `builder.native.getBalance` (in the assertion→builder lowering, a later PR of
|
|
188
|
+
// this stack), which reads ~0 on a
|
|
189
|
+
// chain whose gas coin is really an ERC-20 — the readiness check and its
|
|
190
|
+
// embedded on-chain assertion then fail permanently, making a legitimate
|
|
191
|
+
// continuation un-settleable. Kind-changing (unlike the outcome fix's token
|
|
192
|
+
// swap) because the native member has no `token` field. Only `nativeBalanceGte`
|
|
193
|
+
// is touched; every other member decodes unchanged. Idempotent: an already
|
|
194
|
+
// `erc20BalanceGte` spec and every evm-native chain pass through. Encode is the
|
|
195
|
+
// identity (the erc20 form is valid wire input).
|
|
196
|
+
export const CheckSchema = Schema.transform(
|
|
197
|
+
RawCheckSchema,
|
|
198
|
+
Schema.typeSchema(RawCheckSchema),
|
|
199
|
+
{
|
|
200
|
+
strict: true,
|
|
201
|
+
decode: (check) =>
|
|
202
|
+
check.kind === 'nativeBalanceGte' && isAsErc20Chain(check.chainId)
|
|
203
|
+
? asErc20BalanceGteCheck(check.chainId, check.owner, check.minAmount)
|
|
204
|
+
: check,
|
|
205
|
+
encode: (check) => check,
|
|
206
|
+
},
|
|
207
|
+
);
|
|
208
|
+
|
|
209
|
+
// The derivation vocabulary (verified continuations, plan GD-1). An
|
|
210
|
+
// `AssertionSpec` is the structured, backend-neutral form of one invariant a
|
|
211
|
+
// derived validation program must enforce against the executor's re-quoted
|
|
212
|
+
// fill. It extends the three `Check` members (reused verbatim) with two
|
|
213
|
+
// derivation-only members. Amounts stay decimal strings on the wire, mirroring
|
|
214
|
+
// `Check`.
|
|
215
|
+
//
|
|
216
|
+
// - `erc20BalanceDeltaGte` / `nativeBalanceDeltaGte` — `owner`'s post-fill
|
|
217
|
+
// balance of `token` (native) on `chainId` must exceed its pre-fill balance
|
|
218
|
+
// by at least `minDelta`. Derived from each committed outcome's floor;
|
|
219
|
+
// asserted as `post ≥ pre + minDelta` (the pre-balance is validator-injected
|
|
220
|
+
// at settlement).
|
|
221
|
+
export const Erc20BalanceDeltaGteSpecSchema = Schema.Struct({
|
|
222
|
+
kind: Schema.Literal('erc20BalanceDeltaGte'),
|
|
223
|
+
chainId: ChainIdSchema,
|
|
224
|
+
token: HexStringSchema,
|
|
225
|
+
owner: AssertionOwnerSchema,
|
|
226
|
+
minDelta: Schema.String,
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
export const NativeBalanceDeltaGteSpecSchema = Schema.Struct({
|
|
230
|
+
kind: Schema.Literal('nativeBalanceDeltaGte'),
|
|
231
|
+
chainId: ChainIdSchema,
|
|
232
|
+
owner: AssertionOwnerSchema,
|
|
233
|
+
minDelta: Schema.String,
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
export const AssertionSpecSchema = Schema.Union(
|
|
237
|
+
Erc20BalanceGteSpecSchema,
|
|
238
|
+
NativeBalanceGteSpecSchema,
|
|
239
|
+
CallCheckSpecSchema,
|
|
240
|
+
Erc20BalanceDeltaGteSpecSchema,
|
|
241
|
+
NativeBalanceDeltaGteSpecSchema,
|
|
242
|
+
);
|
|
243
|
+
|
|
244
|
+
// The continuation plan returned to the caller for a self-serve (manual-relay)
|
|
245
|
+
// settle node: the next Flow to run plus a readiness `check`. The caller polls
|
|
246
|
+
// the `check`, then re-submits `nextFlow` through the normal `/compose`
|
|
247
|
+
// pipeline once it passes. The `check` is also embedded into `nextFlow` as an
|
|
248
|
+
// on-chain assertion, so a premature re-submission reverts instead of executing
|
|
249
|
+
// on stale balances. `FlowSchema` lives in the same package, so importing it
|
|
250
|
+
// here keeps `compose-spec` dependency-free.
|
|
251
|
+
export const ContinuationPlanSchema = Schema.Struct({
|
|
252
|
+
chainId: ChainIdSchema,
|
|
253
|
+
nextFlow: FlowSchema,
|
|
254
|
+
check: CheckSchema,
|
|
255
|
+
validity: Schema.Struct({ expiresAtMs: Schema.Number }),
|
|
256
|
+
correlationId: Schema.optional(Schema.String),
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
export type ContinuationOutcome = typeof ContinuationOutcomeSchema.Type;
|
|
260
|
+
export type ContinuationIntent = typeof ContinuationIntentSchema.Type;
|
|
261
|
+
export type Phase1Artifact = typeof Phase1ArtifactSchema.Type;
|
|
262
|
+
export type Check = typeof CheckSchema.Type;
|
|
263
|
+
export type AssertionSpec = typeof AssertionSpecSchema.Type;
|
|
264
|
+
export type ContinuationPlan = typeof ContinuationPlanSchema.Type;
|
|
265
|
+
|
|
266
|
+
// The comparison operator carried by a `call` check. Mirrors the inline
|
|
267
|
+
// `comparator` literal on the `call` member of `RawCheckSchema` above.
|
|
268
|
+
export type CheckComparator = 'gte' | 'eq' | 'lte';
|
|
269
|
+
|
|
270
|
+
// Comparator predicates for `call` checks, matching the package's `is*`
|
|
271
|
+
// convention (see `flow.ts`). Each is only true for a `call` check with the
|
|
272
|
+
// corresponding comparator; the other check kinds carry no comparator.
|
|
273
|
+
export const isGTECheck = (check: Check): boolean =>
|
|
274
|
+
check.kind === 'call' && check.comparator === 'gte';
|
|
275
|
+
export const isLTECheck = (check: Check): boolean =>
|
|
276
|
+
check.kind === 'call' && check.comparator === 'lte';
|
|
277
|
+
export const isEQCheck = (check: Check): boolean =>
|
|
278
|
+
check.kind === 'call' && check.comparator === 'eq';
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { Schema } from 'effect';
|
|
2
|
+
|
|
3
|
+
import { isAsErc20Chain } from './chainCapabilities.js';
|
|
4
|
+
import {
|
|
5
|
+
asErc20BalanceGteCheck,
|
|
6
|
+
AssertionOwnerSchema,
|
|
7
|
+
CallCheckSpecSchema,
|
|
8
|
+
Erc20BalanceDeltaGteSpecSchema,
|
|
9
|
+
HexStringSchema,
|
|
10
|
+
NativeBalanceDeltaGteSpecSchema,
|
|
11
|
+
} from './continuation.js';
|
|
12
|
+
import type { AssertionSpec } from './continuation.js';
|
|
13
|
+
import { ChainIdSchema } from './flowSchema.js';
|
|
14
|
+
import { BYTES32_LOWER_HEX } from './hexPatterns.js';
|
|
15
|
+
|
|
16
|
+
// The neutral per-leg verification descriptor (verified continuations, plan
|
|
17
|
+
// GD-3, seam C4). It is the wire-visible statement of what a committed
|
|
18
|
+
// validation program enforces, so a user funding phase 1 can give informed
|
|
19
|
+
// consent (PRD goal G8). It is backend-neutral (G7): no validator address, no
|
|
20
|
+
// constraint, no settlement-backend vocabulary — the only escrow-ish term is the
|
|
21
|
+
// symbolic owner `"escrow"`, explicitly allowlisted by the boundary check.
|
|
22
|
+
//
|
|
23
|
+
// The design trick that makes disclosure trustworthy-by-construction:
|
|
24
|
+
// `assertedPredicate[]` is NOT a new DSL. It is the canonical JSON serialization
|
|
25
|
+
// of the same `AssertionSpec[]` the validation program is derived from, so
|
|
26
|
+
// disclosure cannot drift from enforcement — both are projections of one array.
|
|
27
|
+
|
|
28
|
+
// `ChainIdSchema` (from `flowSchema.ts`) and `HexStringSchema` (from
|
|
29
|
+
// `continuation.ts`) are reused directly, so the widened members below differ
|
|
30
|
+
// from their readiness counterparts in exactly one field (`owner`) with no
|
|
31
|
+
// risk of a drifting re-declaration.
|
|
32
|
+
|
|
33
|
+
// The `validationProgramHash` (GD-2, seam C1): `keccak256` over the canonical
|
|
34
|
+
// program body. Strict 32-byte lowercase hex (`BYTES32_LOWER_HEX`) — see the
|
|
35
|
+
// rationale in `hexPatterns.ts`.
|
|
36
|
+
export const Bytes32HexSchema = Schema.String.pipe(
|
|
37
|
+
Schema.pattern(BYTES32_LOWER_HEX),
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
// The disclosure-path widening of the two balance-`Gte` readiness members: same
|
|
41
|
+
// fields as their `continuation.ts` counterparts, but `owner` accepts a symbolic
|
|
42
|
+
// `"escrow"`/`"delivery"` too. The derivation genuinely produces such values —
|
|
43
|
+
// a captured invariant can carry a symbolic owner, which `resolveOwner` maps to
|
|
44
|
+
// a validator-injected slot at settlement (see the `uc1` invariant). The
|
|
45
|
+
// readiness `CheckSchema` stays hex-only on purpose (a polled `Check` has no
|
|
46
|
+
// injected slot), so this widening lives in the descriptor union only.
|
|
47
|
+
export const AssertedErc20BalanceGteSpecSchema = Schema.Struct({
|
|
48
|
+
kind: Schema.Literal('erc20BalanceGte'),
|
|
49
|
+
chainId: ChainIdSchema,
|
|
50
|
+
token: HexStringSchema,
|
|
51
|
+
owner: AssertionOwnerSchema,
|
|
52
|
+
minAmount: Schema.String,
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
export const AssertedNativeBalanceGteSpecSchema = Schema.Struct({
|
|
56
|
+
kind: Schema.Literal('nativeBalanceGte'),
|
|
57
|
+
chainId: ChainIdSchema,
|
|
58
|
+
owner: AssertionOwnerSchema,
|
|
59
|
+
minAmount: Schema.String,
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
// The disclosed predicate: the five `AssertionSpec` kinds, with symbolic owners
|
|
63
|
+
// admitted on the two balance-`Gte` members. The `call` member and the two
|
|
64
|
+
// derivation-only delta members are reused verbatim from `continuation.ts` (the
|
|
65
|
+
// delta members already carry `AssertionOwnerSchema`).
|
|
66
|
+
const RawAssertedPredicateSchema = Schema.Union(
|
|
67
|
+
AssertedErc20BalanceGteSpecSchema,
|
|
68
|
+
AssertedNativeBalanceGteSpecSchema,
|
|
69
|
+
CallCheckSpecSchema,
|
|
70
|
+
Erc20BalanceDeltaGteSpecSchema,
|
|
71
|
+
NativeBalanceDeltaGteSpecSchema,
|
|
72
|
+
);
|
|
73
|
+
|
|
74
|
+
// Same `nativeBalanceGte` → `erc20BalanceGte` rewrite as `CheckSchema` (see
|
|
75
|
+
// there), applied to the disclosed/committed predicate so the settlement-side
|
|
76
|
+
// re-assertion derives the identical program on `as-erc20` chains and the
|
|
77
|
+
// authoring↔settlement hashes stay equal. A committed action authored through
|
|
78
|
+
// the fixed `CheckSchema` already carries the erc20 form, so this pass is
|
|
79
|
+
// idempotent there; it additionally normalizes a hand-forged committed action
|
|
80
|
+
// that embeds a raw native check. The delta members (`nativeBalanceDeltaGte`)
|
|
81
|
+
// are intentionally untouched — they derive from already-normalized outcomes.
|
|
82
|
+
export const AssertedPredicateSchema = Schema.transform(
|
|
83
|
+
RawAssertedPredicateSchema,
|
|
84
|
+
Schema.typeSchema(RawAssertedPredicateSchema),
|
|
85
|
+
{
|
|
86
|
+
strict: true,
|
|
87
|
+
decode: (pred) =>
|
|
88
|
+
pred.kind === 'nativeBalanceGte' && isAsErc20Chain(pred.chainId)
|
|
89
|
+
? asErc20BalanceGteCheck(pred.chainId, pred.owner, pred.minAmount)
|
|
90
|
+
: pred,
|
|
91
|
+
encode: (pred) => pred,
|
|
92
|
+
},
|
|
93
|
+
);
|
|
94
|
+
|
|
95
|
+
export const VerificationDescriptorSchema = Schema.Struct({
|
|
96
|
+
validationProgramHash: Bytes32HexSchema,
|
|
97
|
+
// The C5 echo's second hash (verified continuations, plan GD-8): `keccak256`
|
|
98
|
+
// over the canonical params vector (`paramsHash`, GD-2 seam C1). Required and
|
|
99
|
+
// jointly necessary with `validationProgramHash` — the program hash is
|
|
100
|
+
// value-blind (it never binds the delivery address or amounts), so a descriptor
|
|
101
|
+
// that enforces only it is a fund-redirect hole. Empty params still yield a
|
|
102
|
+
// present `0x00…00`, so a real descriptor never omits it. Strict 32-byte
|
|
103
|
+
// lowercase hex, same rule as `validationProgramHash`.
|
|
104
|
+
paramsHash: Bytes32HexSchema,
|
|
105
|
+
assertedPredicate: Schema.Array(AssertedPredicateSchema),
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
// Deterministic, key-order-pinned JSON serialization of an `AssertionSpec[]`.
|
|
109
|
+
// Canonicalization rules:
|
|
110
|
+
// - Object keys are emitted in recursive lexicographic order (sorted).
|
|
111
|
+
// - Compact separators — no whitespace anywhere.
|
|
112
|
+
// - Array element order is PRESERVED, never sorted: it is semantic (derivation
|
|
113
|
+
// order — deltas, then invariants) and is a faithful projection of the
|
|
114
|
+
// program's assertion order.
|
|
115
|
+
//
|
|
116
|
+
// All `AssertionSpec` leaf values are JSON-safe (decimal strings, `0x` hex
|
|
117
|
+
// strings, positive-int `chainId`), so lexicographic key sorting is total and
|
|
118
|
+
// stable with no number-canonicalization edge cases, and is self-maintaining
|
|
119
|
+
// when members gain fields. Embedding this string per fixture vector pins the
|
|
120
|
+
// serialization cross-repo (GD-5's consumer asserts it reproduces the string).
|
|
121
|
+
//
|
|
122
|
+
// Boundary-forced local mirror of `@lifi/utils`'s `canonicalJson`: compose-spec
|
|
123
|
+
// is the wire-format leaf and may not import `@lifi/utils` (enforced by the
|
|
124
|
+
// compose boundary check), so do not "fix" this duplication by importing across
|
|
125
|
+
// the boundary.
|
|
126
|
+
const canonicalJson = (value: unknown): string => {
|
|
127
|
+
if (Array.isArray(value)) {
|
|
128
|
+
return `[${value.map(canonicalJson).join(',')}]`;
|
|
129
|
+
}
|
|
130
|
+
if (value !== null && typeof value === 'object') {
|
|
131
|
+
const record = value as Record<string, unknown>;
|
|
132
|
+
const entries = Object.keys(record)
|
|
133
|
+
.sort()
|
|
134
|
+
.map((key) => `${JSON.stringify(key)}:${canonicalJson(record[key])}`);
|
|
135
|
+
return `{${entries.join(',')}}`;
|
|
136
|
+
}
|
|
137
|
+
return JSON.stringify(value);
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
export const canonicalAssertedPredicate = (
|
|
141
|
+
specs: readonly AssertionSpec[],
|
|
142
|
+
): string => canonicalJson(specs);
|
|
143
|
+
|
|
144
|
+
export type VerificationDescriptor = typeof VerificationDescriptorSchema.Type;
|
|
145
|
+
|
|
146
|
+
// `AssertedPredicate` is structurally identical to `AssertionSpec` at the TS
|
|
147
|
+
// type level — `AssertionOwner` is a subtype of `string`, so widening `owner`
|
|
148
|
+
// changes only schema *decode* behaviour, not the static type. It is therefore
|
|
149
|
+
// exported as a plain alias, keeping `canonicalAssertedPredicate` and the
|
|
150
|
+
// derivation's `specs` array interchangeable without casts.
|
|
151
|
+
export type AssertedPredicate = AssertionSpec;
|
package/src/flashloan.ts
CHANGED
|
@@ -12,19 +12,19 @@ export const PROVIDER_KIND = {
|
|
|
12
12
|
} as const;
|
|
13
13
|
|
|
14
14
|
export const providerKindNames = [
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
15
|
+
'aave-v3',
|
|
16
|
+
'erc3156',
|
|
17
|
+
'balancer-v2',
|
|
18
|
+
'morpho-blue',
|
|
19
19
|
] as const;
|
|
20
20
|
|
|
21
21
|
export type ProviderKindName = (typeof providerKindNames)[number];
|
|
22
22
|
|
|
23
23
|
export const providerKindByName: Record<ProviderKindName, number> = {
|
|
24
|
-
|
|
24
|
+
'aave-v3': PROVIDER_KIND.AAVE_V3,
|
|
25
25
|
erc3156: PROVIDER_KIND.ERC3156,
|
|
26
|
-
|
|
27
|
-
|
|
26
|
+
'balancer-v2': PROVIDER_KIND.BALANCER_V2,
|
|
27
|
+
'morpho-blue': PROVIDER_KIND.MORPHO_BLUE,
|
|
28
28
|
};
|
|
29
29
|
|
|
30
30
|
// The fully-narrowed JSON shape of a `flashloan` MaterialiserInput once it
|
|
@@ -35,7 +35,7 @@ export const providerKindByName: Record<ProviderKindName, number> = {
|
|
|
35
35
|
// materialiser's `configSchema` (see compose-builtins/materialisers/flashloan)
|
|
36
36
|
// validates the shape before any consumer reads it.
|
|
37
37
|
export interface FlashloanMaterialiserInput {
|
|
38
|
-
readonly kind:
|
|
38
|
+
readonly kind: 'flashloan';
|
|
39
39
|
readonly providerKind: ProviderKindName;
|
|
40
40
|
readonly amount: string;
|
|
41
41
|
}
|
package/src/flow.ts
CHANGED
|
@@ -1,79 +1,81 @@
|
|
|
1
|
-
import type { SimulationRevert } from
|
|
2
|
-
import type { FlowInput, ResourceInput, SolType } from
|
|
3
|
-
import type { Availability, Resource } from
|
|
4
|
-
|
|
5
|
-
export type {
|
|
1
|
+
import type { SimulationRevert } from './compile.js';
|
|
2
|
+
import type { FlowInput, ResourceInput, SolType } from './flowSchema.js';
|
|
3
|
+
import type { Availability, Resource } from './resource.js';
|
|
4
|
+
import type {
|
|
6
5
|
HandlePort,
|
|
7
6
|
OpInputPort,
|
|
8
7
|
OpOutputPort,
|
|
9
8
|
ResourceOutputPort,
|
|
10
9
|
ResourcePort,
|
|
11
|
-
} from
|
|
10
|
+
} from './zodSchemas.js';
|
|
12
11
|
|
|
13
|
-
|
|
12
|
+
export type {
|
|
14
13
|
HandlePort,
|
|
15
14
|
OpInputPort,
|
|
16
15
|
OpOutputPort,
|
|
17
16
|
ResourceOutputPort,
|
|
18
17
|
ResourcePort,
|
|
19
|
-
} from
|
|
18
|
+
} from './zodSchemas.js';
|
|
20
19
|
|
|
21
20
|
export interface Port {
|
|
22
21
|
readonly name: string;
|
|
23
22
|
readonly type: SolType;
|
|
24
|
-
readonly mode:
|
|
23
|
+
readonly mode: 'linear' | 'copy';
|
|
25
24
|
readonly availability?: Availability;
|
|
26
25
|
readonly resource?: Resource;
|
|
27
26
|
}
|
|
28
27
|
|
|
29
28
|
export const isResourceInput = (input: FlowInput): input is ResourceInput =>
|
|
30
|
-
|
|
29
|
+
'resource' in input;
|
|
31
30
|
|
|
32
31
|
export const flowInputType = (input: FlowInput): SolType =>
|
|
33
|
-
isResourceInput(input) ?
|
|
32
|
+
isResourceInput(input) ? 'uint256' : input.type;
|
|
34
33
|
|
|
35
|
-
export const flowInputMode = (input: FlowInput):
|
|
36
|
-
isResourceInput(input) ?
|
|
34
|
+
export const flowInputMode = (input: FlowInput): 'linear' | 'copy' =>
|
|
35
|
+
isResourceInput(input) ? 'linear' : 'copy';
|
|
37
36
|
|
|
38
37
|
export const flowInputResource = (input: FlowInput): Resource | undefined =>
|
|
39
38
|
isResourceInput(input) ? input.resource : undefined;
|
|
40
39
|
|
|
41
40
|
export const isResourcePort = (port: OpInputPort): port is ResourcePort =>
|
|
42
|
-
port.kind ===
|
|
41
|
+
port.kind === 'resource';
|
|
43
42
|
|
|
44
43
|
export const isHandlePort = (
|
|
45
|
-
port: OpInputPort | OpOutputPort
|
|
46
|
-
): port is HandlePort => port.kind ===
|
|
44
|
+
port: OpInputPort | OpOutputPort,
|
|
45
|
+
): port is HandlePort => port.kind === 'handle';
|
|
47
46
|
|
|
48
47
|
export const isResourceOutputPort = (
|
|
49
|
-
port: OpInputPort | OpOutputPort
|
|
50
|
-
): port is ResourceOutputPort => port.kind ===
|
|
48
|
+
port: OpInputPort | OpOutputPort,
|
|
49
|
+
): port is ResourceOutputPort => port.kind === 'resource_output';
|
|
51
50
|
|
|
52
51
|
export type ComposeErrorKind =
|
|
53
|
-
|
|
|
54
|
-
|
|
|
55
|
-
|
|
|
56
|
-
|
|
|
57
|
-
|
|
|
58
|
-
|
|
|
59
|
-
|
|
|
60
|
-
|
|
|
61
|
-
|
|
|
62
|
-
|
|
|
63
|
-
|
|
|
64
|
-
|
|
|
65
|
-
|
|
|
66
|
-
|
|
|
67
|
-
|
|
|
68
|
-
|
|
|
69
|
-
|
|
|
70
|
-
|
|
|
71
|
-
|
|
|
72
|
-
|
|
|
52
|
+
| 'decode_error'
|
|
53
|
+
| 'validation_error'
|
|
54
|
+
| 'linearity_error'
|
|
55
|
+
| 'resolve_error'
|
|
56
|
+
| 'preparation_error'
|
|
57
|
+
| 'provider_error'
|
|
58
|
+
| 'no_route_error'
|
|
59
|
+
| 'resolution_error'
|
|
60
|
+
| 'lowering_error'
|
|
61
|
+
| 'empty_program'
|
|
62
|
+
| 'simulation_error'
|
|
63
|
+
| 'simulation_setup_error'
|
|
64
|
+
| 'simulation_rpc_error'
|
|
65
|
+
| 'simulation_revert'
|
|
66
|
+
| 'subgraph_simulation_revert'
|
|
67
|
+
| 'guard_error'
|
|
68
|
+
| 'continuation_error'
|
|
69
|
+
| 'compilation_error'
|
|
70
|
+
| 'verification_error'
|
|
71
|
+
| 'price_impact_exceeded'
|
|
72
|
+
| 'fee_resolution_error'
|
|
73
|
+
| 'fee_resolution_unavailable'
|
|
74
|
+
| 'flashloan_unauthorized';
|
|
73
75
|
|
|
74
76
|
export type GenericComposeErrorKind = Exclude<
|
|
75
77
|
ComposeErrorKind,
|
|
76
|
-
|
|
78
|
+
'preparation_error' | 'simulation_revert'
|
|
77
79
|
>;
|
|
78
80
|
|
|
79
81
|
export interface FailedPreparedOp {
|
|
@@ -90,13 +92,13 @@ interface ComposeErrorBase {
|
|
|
90
92
|
}
|
|
91
93
|
|
|
92
94
|
export interface PreparationComposeError extends ComposeErrorBase {
|
|
93
|
-
readonly kind:
|
|
95
|
+
readonly kind: 'preparation_error';
|
|
94
96
|
readonly failedOps: readonly FailedPreparedOp[];
|
|
95
97
|
readonly succeededOps: readonly string[];
|
|
96
98
|
}
|
|
97
99
|
|
|
98
100
|
export interface SimulationRevertComposeError extends ComposeErrorBase {
|
|
99
|
-
readonly kind:
|
|
101
|
+
readonly kind: 'simulation_revert';
|
|
100
102
|
readonly details?: SimulationRevert;
|
|
101
103
|
}
|
|
102
104
|
|
|
@@ -106,9 +108,7 @@ export interface GenericComposeError extends ComposeErrorBase {
|
|
|
106
108
|
}
|
|
107
109
|
|
|
108
110
|
export type ComposeError =
|
|
109
|
-
|
|
|
110
|
-
| SimulationRevertComposeError
|
|
111
|
-
| GenericComposeError;
|
|
111
|
+
PreparationComposeError | SimulationRevertComposeError | GenericComposeError;
|
|
112
112
|
|
|
113
113
|
export interface MaterialiserInputBase {
|
|
114
114
|
readonly kind: string;
|
|
@@ -121,7 +121,7 @@ export type MaterialiserInputOf<C extends object> = MaterialiserInputBase & C;
|
|
|
121
121
|
export type MaterialiserConfigOf<C extends object> = C;
|
|
122
122
|
|
|
123
123
|
export const isMaterialiserInput = (s: InputSpec): s is MaterialiserInput =>
|
|
124
|
-
typeof s ===
|
|
124
|
+
typeof s === 'object' && s !== null && 'kind' in s;
|
|
125
125
|
|
|
126
126
|
export type InputSpec = bigint | string | MaterialiserInput;
|
|
127
127
|
|
|
@@ -135,6 +135,6 @@ export type PreconditionOf<C extends object> = PreconditionBase & C;
|
|
|
135
135
|
|
|
136
136
|
export type PreconditionConfigOf<C extends object> = Omit<
|
|
137
137
|
PreconditionBase,
|
|
138
|
-
|
|
138
|
+
'type'
|
|
139
139
|
> &
|
|
140
140
|
C;
|