@lifi/compose-spec 0.2.0 → 0.4.0-staging.1
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 +440 -4
- package/dist/index.d.cts +1259 -121
- package/dist/index.d.ts +1259 -121
- package/dist/index.js +400 -4
- package/package.json +22 -2
- 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 +92 -19
- package/src/materialiserMetadata.ts +1 -1
- package/src/ref.ts +21 -21
- package/src/resource.ts +19 -9
- package/src/simulate.ts +7 -9
- package/src/zodSchemas.ts +208 -48
package/src/flowSchema.ts
CHANGED
|
@@ -1,31 +1,65 @@
|
|
|
1
|
-
import { Schema } from
|
|
1
|
+
import { Schema } from 'effect';
|
|
2
|
+
|
|
3
|
+
import { normalizeResource } from './chainCapabilities.js';
|
|
2
4
|
|
|
3
5
|
export const SolTypeSchema = Schema.Literal(
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
6
|
+
'uint8',
|
|
7
|
+
'uint16',
|
|
8
|
+
'uint32',
|
|
9
|
+
'uint64',
|
|
10
|
+
'uint128',
|
|
11
|
+
'uint256',
|
|
12
|
+
'int128',
|
|
13
|
+
'int256',
|
|
14
|
+
'address',
|
|
15
|
+
'bool',
|
|
16
|
+
'bytes',
|
|
17
|
+
'bytes4',
|
|
18
|
+
'bytes32',
|
|
19
|
+
'string',
|
|
18
20
|
);
|
|
19
21
|
|
|
20
|
-
|
|
22
|
+
// A positive integer chain id. Single source of truth on the Effect side
|
|
23
|
+
// (`continuation.ts` imports it; `zodSchemas.ts` mirrors it as `chainIdZod`).
|
|
24
|
+
export const ChainIdSchema = Schema.Number.pipe(
|
|
25
|
+
Schema.int(),
|
|
26
|
+
Schema.positive(),
|
|
27
|
+
);
|
|
21
28
|
|
|
22
|
-
|
|
23
|
-
Schema.Struct({ kind: Schema.Literal(
|
|
29
|
+
const RawResourceSchema = Schema.Union(
|
|
30
|
+
Schema.Struct({ kind: Schema.Literal('native'), chainId: ChainIdSchema }),
|
|
24
31
|
Schema.Struct({
|
|
25
|
-
kind: Schema.Literal(
|
|
32
|
+
kind: Schema.Literal('erc20'),
|
|
26
33
|
token: Schema.String,
|
|
27
34
|
chainId: ChainIdSchema,
|
|
28
|
-
})
|
|
35
|
+
}),
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* THE chain-capabilities chokepoint.
|
|
40
|
+
*
|
|
41
|
+
* `ResourceSchema` is a decode transform: it parses the plain `native | erc20`
|
|
42
|
+
* wire union (`RawResourceSchema`) and then runs `normalizeResource`, so on
|
|
43
|
+
* `as-erc20` chains (e.g. Tempo) a `{ kind: "native", chainId }` resource is
|
|
44
|
+
* rewritten to its ERC-20 form (PathUSD) before any application code sees it.
|
|
45
|
+
* Because every Resource-bearing schema embeds this one — `FlowSchema` for flow
|
|
46
|
+
* inputs, op `configSchema`s for op configs — making the schema itself
|
|
47
|
+
* chain-aware covers every parsed entry point in one place.
|
|
48
|
+
*
|
|
49
|
+
* The `encode` direction is the identity: the ERC-20 form is itself valid wire
|
|
50
|
+
* format, and the rewrite is deliberately not reversible (we never want to
|
|
51
|
+
* re-introduce a `native` resource when serialising). `JSONSchema.make` derives
|
|
52
|
+
* the published wire format from the encoded (`from`) side, so the manifest
|
|
53
|
+
* keeps advertising the plain union with both arms.
|
|
54
|
+
*/
|
|
55
|
+
export const ResourceSchema = Schema.transform(
|
|
56
|
+
RawResourceSchema,
|
|
57
|
+
Schema.typeSchema(RawResourceSchema),
|
|
58
|
+
{
|
|
59
|
+
strict: true,
|
|
60
|
+
decode: (raw) => normalizeResource(raw),
|
|
61
|
+
encode: (resource) => resource,
|
|
62
|
+
},
|
|
29
63
|
);
|
|
30
64
|
|
|
31
65
|
export const ResourceInputSchema = Schema.Struct({
|
|
@@ -40,15 +74,15 @@ export const HandleInputSchema = Schema.Struct({
|
|
|
40
74
|
|
|
41
75
|
export const FlowInputSchema = Schema.Union(
|
|
42
76
|
ResourceInputSchema,
|
|
43
|
-
HandleInputSchema
|
|
77
|
+
HandleInputSchema,
|
|
44
78
|
);
|
|
45
79
|
|
|
46
80
|
export const RefSchema = Schema.Struct({
|
|
47
81
|
$ref: Schema.String.pipe(
|
|
48
|
-
Schema.filter((s) => s.indexOf(
|
|
82
|
+
Schema.filter((s) => s.indexOf('.') > 0, {
|
|
49
83
|
message: () =>
|
|
50
84
|
'$ref must be a dotpath like "input.name" or "nodeId.port"',
|
|
51
|
-
})
|
|
85
|
+
}),
|
|
52
86
|
),
|
|
53
87
|
});
|
|
54
88
|
|
|
@@ -61,7 +95,7 @@ export const BindValueSchema = Schema.Union(RefSchema, LiteralBindingSchema);
|
|
|
61
95
|
|
|
62
96
|
export const AppliedGuardSchema = Schema.Struct(
|
|
63
97
|
{ kind: Schema.String },
|
|
64
|
-
{ key: Schema.String, value: Schema.Unknown }
|
|
98
|
+
{ key: Schema.String, value: Schema.Unknown },
|
|
65
99
|
);
|
|
66
100
|
|
|
67
101
|
export const CallSchema = Schema.Struct({
|
|
@@ -69,11 +103,11 @@ export const CallSchema = Schema.Struct({
|
|
|
69
103
|
op: Schema.String,
|
|
70
104
|
bind: Schema.optionalWith(
|
|
71
105
|
Schema.Record({ key: Schema.String, value: BindValueSchema }),
|
|
72
|
-
{ default: () => ({}) }
|
|
106
|
+
{ default: () => ({}) },
|
|
73
107
|
),
|
|
74
108
|
config: Schema.optionalWith(
|
|
75
109
|
Schema.Record({ key: Schema.String, value: Schema.Unknown }),
|
|
76
|
-
{ default: () => ({}) }
|
|
110
|
+
{ default: () => ({}) },
|
|
77
111
|
),
|
|
78
112
|
guards: Schema.optional(Schema.Array(AppliedGuardSchema)),
|
|
79
113
|
});
|
|
@@ -89,7 +123,7 @@ export const FlowSchema = Schema.Struct({
|
|
|
89
123
|
chainId: ChainIdSchema,
|
|
90
124
|
inputs: Schema.Array(FlowInputSchema),
|
|
91
125
|
nodes: Schema.Array(CallSchema),
|
|
92
|
-
|
|
126
|
+
continuations: Schema.optional(Schema.Array(ContinuationSchema)),
|
|
93
127
|
});
|
|
94
128
|
|
|
95
129
|
export type SolType = typeof SolTypeSchema.Type;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// Shared hex-string patterns for the compose wire format. Zero-dependency leaf
|
|
2
|
+
// so both the Effect-schema side (`descriptor.ts`) and the standalone Zod
|
|
3
|
+
// mirror (`zodSchemas.ts`) consume one source of truth without importing across
|
|
4
|
+
// layers. compose-spec is the wire-format leaf and may not import `@lifi/utils`
|
|
5
|
+
// (enforced by `scripts/check-compose-boundaries.mjs`), so the equivalent of
|
|
6
|
+
// `makeHexStringRegex(32, { lowercase: true })` is inlined here.
|
|
7
|
+
|
|
8
|
+
// A `0x`-prefixed, exactly-32-byte, STRICTLY lowercase hex string. Validated
|
|
9
|
+
// strictly — deliberately overriding compose-spec's lenient `0x`-prefix-only
|
|
10
|
+
// convention for address fields. These hashes (`validationProgramHash`,
|
|
11
|
+
// `paramsHash`) are machine-generated (ethers `keccak256` emits lowercase) and
|
|
12
|
+
// never hand-typed, so strictness costs nothing; leniency would admit two
|
|
13
|
+
// serializations of one hash (mixed case) and truncated values, both of which
|
|
14
|
+
// poison determinism and the byte-identity fixture discipline. A zero hash
|
|
15
|
+
// `0x00…00` (64 zeros) remains valid under the strict rule.
|
|
16
|
+
export const BYTES32_LOWER_HEX = /^0x[0-9a-f]{64}$/;
|
package/src/index.ts
CHANGED
|
@@ -10,7 +10,7 @@ export type {
|
|
|
10
10
|
Call,
|
|
11
11
|
Continuation,
|
|
12
12
|
Flow,
|
|
13
|
-
} from
|
|
13
|
+
} from './flowSchema.js';
|
|
14
14
|
export {
|
|
15
15
|
FlowSchema,
|
|
16
16
|
ResourceSchema,
|
|
@@ -24,7 +24,50 @@ export {
|
|
|
24
24
|
AppliedGuardSchema,
|
|
25
25
|
CallSchema,
|
|
26
26
|
ContinuationSchema,
|
|
27
|
-
} from
|
|
27
|
+
} from './flowSchema.js';
|
|
28
|
+
|
|
29
|
+
export type {
|
|
30
|
+
ContinuationOutcome,
|
|
31
|
+
ContinuationIntent,
|
|
32
|
+
Phase1Artifact,
|
|
33
|
+
Check,
|
|
34
|
+
CheckComparator,
|
|
35
|
+
AssertionOwner,
|
|
36
|
+
AssertionSpec,
|
|
37
|
+
ContinuationPlan,
|
|
38
|
+
} from './continuation.js';
|
|
39
|
+
export {
|
|
40
|
+
CONTINUATION_COMMITMENT_OP_ID,
|
|
41
|
+
HexStringSchema,
|
|
42
|
+
ContinuationOutcomeSchema,
|
|
43
|
+
ContinuationIntentSchema,
|
|
44
|
+
Phase1ArtifactSchema,
|
|
45
|
+
CheckSchema,
|
|
46
|
+
AssertionOwnerSchema,
|
|
47
|
+
Erc20BalanceGteSpecSchema,
|
|
48
|
+
NativeBalanceGteSpecSchema,
|
|
49
|
+
CallCheckSpecSchema,
|
|
50
|
+
Erc20BalanceDeltaGteSpecSchema,
|
|
51
|
+
NativeBalanceDeltaGteSpecSchema,
|
|
52
|
+
AssertionSpecSchema,
|
|
53
|
+
ContinuationPlanSchema,
|
|
54
|
+
isGTECheck,
|
|
55
|
+
isLTECheck,
|
|
56
|
+
isEQCheck,
|
|
57
|
+
} from './continuation.js';
|
|
58
|
+
|
|
59
|
+
export type {
|
|
60
|
+
AssertedPredicate,
|
|
61
|
+
VerificationDescriptor,
|
|
62
|
+
} from './descriptor.js';
|
|
63
|
+
export {
|
|
64
|
+
Bytes32HexSchema,
|
|
65
|
+
AssertedErc20BalanceGteSpecSchema,
|
|
66
|
+
AssertedNativeBalanceGteSpecSchema,
|
|
67
|
+
AssertedPredicateSchema,
|
|
68
|
+
VerificationDescriptorSchema,
|
|
69
|
+
canonicalAssertedPredicate,
|
|
70
|
+
} from './descriptor.js';
|
|
28
71
|
|
|
29
72
|
export type {
|
|
30
73
|
Port,
|
|
@@ -49,7 +92,7 @@ export type {
|
|
|
49
92
|
Precondition,
|
|
50
93
|
PreconditionOf,
|
|
51
94
|
PreconditionConfigOf,
|
|
52
|
-
} from
|
|
95
|
+
} from './flow.js';
|
|
53
96
|
export {
|
|
54
97
|
isResourceInput,
|
|
55
98
|
flowInputType,
|
|
@@ -59,7 +102,7 @@ export {
|
|
|
59
102
|
isHandlePort,
|
|
60
103
|
isResourceOutputPort,
|
|
61
104
|
isMaterialiserInput,
|
|
62
|
-
} from
|
|
105
|
+
} from './flow.js';
|
|
63
106
|
|
|
64
107
|
export type {
|
|
65
108
|
Availability,
|
|
@@ -67,7 +110,7 @@ export type {
|
|
|
67
110
|
Resource,
|
|
68
111
|
ProducedResource,
|
|
69
112
|
SimulatedAmounts,
|
|
70
|
-
} from
|
|
113
|
+
} from './resource.js';
|
|
71
114
|
export {
|
|
72
115
|
erc20Resource,
|
|
73
116
|
erc20Token,
|
|
@@ -76,18 +119,19 @@ export {
|
|
|
76
119
|
isNativeResource,
|
|
77
120
|
nativeResource,
|
|
78
121
|
resourceKey,
|
|
122
|
+
resourceToken,
|
|
79
123
|
resourcesEqual,
|
|
80
|
-
} from
|
|
124
|
+
} from './resource.js';
|
|
81
125
|
|
|
82
126
|
export type {
|
|
83
127
|
ProviderKindName,
|
|
84
128
|
FlashloanMaterialiserInput,
|
|
85
|
-
} from
|
|
129
|
+
} from './flashloan.js';
|
|
86
130
|
export {
|
|
87
131
|
PROVIDER_KIND,
|
|
88
132
|
providerKindByName,
|
|
89
133
|
providerKindNames,
|
|
90
|
-
} from
|
|
134
|
+
} from './flashloan.js';
|
|
91
135
|
|
|
92
136
|
export {
|
|
93
137
|
linear,
|
|
@@ -101,9 +145,9 @@ export {
|
|
|
101
145
|
resourceOutput,
|
|
102
146
|
inputRef,
|
|
103
147
|
outputRef,
|
|
104
|
-
} from
|
|
148
|
+
} from './constructors.js';
|
|
105
149
|
|
|
106
|
-
export type { ParsedRef, RefScope, ContextKey } from
|
|
150
|
+
export type { ParsedRef, RefScope, ContextKey } from './ref.js';
|
|
107
151
|
export {
|
|
108
152
|
parseRef,
|
|
109
153
|
foldRef,
|
|
@@ -111,7 +155,7 @@ export {
|
|
|
111
155
|
isRef,
|
|
112
156
|
isContextKey,
|
|
113
157
|
RESERVED_REF_SCOPES,
|
|
114
|
-
} from
|
|
158
|
+
} from './ref.js';
|
|
115
159
|
|
|
116
160
|
export type {
|
|
117
161
|
ComposeManifest,
|
|
@@ -121,7 +165,21 @@ export type {
|
|
|
121
165
|
ManifestPrecondition,
|
|
122
166
|
GuardSelector,
|
|
123
167
|
GuardCompatibility,
|
|
124
|
-
} from
|
|
168
|
+
} from './manifest.js';
|
|
169
|
+
|
|
170
|
+
export type { ChainCapabilities, NativeErc20 } from './chainCapabilities.js';
|
|
171
|
+
export {
|
|
172
|
+
EVM_NATIVE_SENTINEL,
|
|
173
|
+
TEMPO_CHAIN_ID,
|
|
174
|
+
TEMPO_NATIVE_ERC20,
|
|
175
|
+
getChainCapabilities,
|
|
176
|
+
isAsErc20Chain,
|
|
177
|
+
canonicalNativeAddress,
|
|
178
|
+
isNativeAddressForChain,
|
|
179
|
+
isMsgValueNative,
|
|
180
|
+
normalizeResource,
|
|
181
|
+
normalizeNativeTokenAddress,
|
|
182
|
+
} from './chainCapabilities.js';
|
|
125
183
|
|
|
126
184
|
export type {
|
|
127
185
|
SweepTo,
|
|
@@ -130,13 +188,14 @@ export type {
|
|
|
130
188
|
ComposeCompileSuccessData,
|
|
131
189
|
ComposeContinuation,
|
|
132
190
|
PriceImpact,
|
|
191
|
+
SpendEntry,
|
|
133
192
|
SimulationPolicy,
|
|
134
193
|
ApprovalEntry,
|
|
135
194
|
SimulationRevert,
|
|
136
195
|
ComposeTransactionRequest,
|
|
137
196
|
ComposeCompilePartialData,
|
|
138
197
|
ComposeCompileResult,
|
|
139
|
-
} from
|
|
198
|
+
} from './compile.js';
|
|
140
199
|
|
|
141
200
|
export type {
|
|
142
201
|
TrackedBalance,
|
|
@@ -151,17 +210,17 @@ export type {
|
|
|
151
210
|
SimulateRevertResult,
|
|
152
211
|
SimulateSetupErrorResult,
|
|
153
212
|
SimulateResult,
|
|
154
|
-
} from
|
|
213
|
+
} from './simulate.js';
|
|
155
214
|
export {
|
|
156
215
|
SIMULATE_MAX_TRACKED_BALANCES,
|
|
157
216
|
SIMULATE_MAX_REQUIREMENTS,
|
|
158
|
-
} from
|
|
217
|
+
} from './simulate.js';
|
|
159
218
|
|
|
160
219
|
export type {
|
|
161
220
|
MaterialiserMetadata,
|
|
162
221
|
MaterialiserResolution,
|
|
163
|
-
} from
|
|
164
|
-
export { foldMaterialiserMetadata } from
|
|
222
|
+
} from './materialiserMetadata.js';
|
|
223
|
+
export { foldMaterialiserMetadata } from './materialiserMetadata.js';
|
|
165
224
|
|
|
166
225
|
export {
|
|
167
226
|
ResourcePortZod,
|
|
@@ -181,5 +240,19 @@ export {
|
|
|
181
240
|
ComposeManifestZod,
|
|
182
241
|
GuardSelectorZod,
|
|
183
242
|
GuardCompatibilityZod,
|
|
184
|
-
|
|
185
|
-
|
|
243
|
+
Phase1ArtifactZod,
|
|
244
|
+
CheckZod,
|
|
245
|
+
AssertionSpecZod,
|
|
246
|
+
AssertedPredicateZod,
|
|
247
|
+
VerificationDescriptorZod,
|
|
248
|
+
ContinuationPlanZod,
|
|
249
|
+
} from './zodSchemas.js';
|
|
250
|
+
export type {
|
|
251
|
+
HandleUnits,
|
|
252
|
+
StaticSolType,
|
|
253
|
+
Phase1ArtifactWire,
|
|
254
|
+
AssertionSpecWire,
|
|
255
|
+
AssertedPredicateWire,
|
|
256
|
+
VerificationDescriptorWire,
|
|
257
|
+
ContinuationPlanWire,
|
|
258
|
+
} from './zodSchemas.js';
|
|
@@ -16,7 +16,7 @@ export const foldMaterialiserMetadata = <R>(
|
|
|
16
16
|
readonly exact: (amount: bigint) => R;
|
|
17
17
|
readonly exactNative: (amount: bigint) => R;
|
|
18
18
|
readonly runtime: () => R;
|
|
19
|
-
}
|
|
19
|
+
},
|
|
20
20
|
): R => {
|
|
21
21
|
if (meta.kind === 'exact') return cases.exact(meta.amount);
|
|
22
22
|
if (meta.kind === 'exact-native') return cases.exactNative(meta.amount);
|
package/src/ref.ts
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
import type { Ref } from
|
|
1
|
+
import type { Ref } from './flowSchema.js';
|
|
2
2
|
|
|
3
3
|
export type ParsedRef =
|
|
4
|
-
| { readonly scope:
|
|
5
|
-
| { readonly scope:
|
|
6
|
-
| { readonly scope:
|
|
4
|
+
| { readonly scope: 'input'; readonly port: string }
|
|
5
|
+
| { readonly scope: 'output'; readonly node: string; readonly port: string }
|
|
6
|
+
| { readonly scope: 'context'; readonly key: ContextKey };
|
|
7
7
|
|
|
8
|
-
export type RefScope = ParsedRef[
|
|
8
|
+
export type RefScope = ParsedRef['scope'];
|
|
9
9
|
|
|
10
|
-
export type ContextKey =
|
|
10
|
+
export type ContextKey = 'sender' | 'executionAddress';
|
|
11
11
|
|
|
12
12
|
export const isContextKey = (key: string): key is ContextKey =>
|
|
13
|
-
key ===
|
|
13
|
+
key === 'sender' || key === 'executionAddress';
|
|
14
14
|
|
|
15
15
|
/**
|
|
16
16
|
* Ref prefixes that parseRef intercepts before the fallback `output` scope.
|
|
@@ -19,30 +19,30 @@ export const isContextKey = (key: string): key is ContextKey =>
|
|
|
19
19
|
* Validation must reject these as node ids.
|
|
20
20
|
*/
|
|
21
21
|
export const RESERVED_REF_SCOPES: ReadonlySet<string> = new Set([
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
22
|
+
'input',
|
|
23
|
+
'context',
|
|
24
|
+
'literal',
|
|
25
25
|
]);
|
|
26
26
|
|
|
27
27
|
export const isRef = (v: unknown): v is Ref =>
|
|
28
|
-
typeof v ===
|
|
28
|
+
typeof v === 'object' &&
|
|
29
29
|
v !== null &&
|
|
30
|
-
|
|
31
|
-
typeof (v as { $ref: unknown }).$ref ===
|
|
30
|
+
'$ref' in v &&
|
|
31
|
+
typeof (v as { $ref: unknown }).$ref === 'string';
|
|
32
32
|
|
|
33
33
|
export const parseRef = (ref: Ref): ParsedRef => {
|
|
34
|
-
const dot = ref.$ref.indexOf(
|
|
34
|
+
const dot = ref.$ref.indexOf('.');
|
|
35
35
|
if (dot === -1)
|
|
36
36
|
throw new Error(`Invalid ref: "${ref.$ref}" (must contain a dot)`);
|
|
37
37
|
const prefix = ref.$ref.slice(0, dot);
|
|
38
38
|
const suffix = ref.$ref.slice(dot + 1);
|
|
39
|
-
if (prefix ===
|
|
40
|
-
if (prefix ===
|
|
39
|
+
if (prefix === 'input') return { scope: 'input', port: suffix };
|
|
40
|
+
if (prefix === 'context') {
|
|
41
41
|
if (!isContextKey(suffix))
|
|
42
42
|
throw new Error(`Unknown context key: "${suffix}"`);
|
|
43
|
-
return { scope:
|
|
43
|
+
return { scope: 'context', key: suffix };
|
|
44
44
|
}
|
|
45
|
-
return { scope:
|
|
45
|
+
return { scope: 'output', node: prefix, port: suffix };
|
|
46
46
|
};
|
|
47
47
|
|
|
48
48
|
export const foldRef = <R>(
|
|
@@ -51,10 +51,10 @@ export const foldRef = <R>(
|
|
|
51
51
|
readonly input: (port: string) => R;
|
|
52
52
|
readonly context: (key: ContextKey) => R;
|
|
53
53
|
readonly output: (node: string, port: string) => R;
|
|
54
|
-
}
|
|
54
|
+
},
|
|
55
55
|
): R => {
|
|
56
|
-
if (ref.scope ===
|
|
57
|
-
if (ref.scope ===
|
|
56
|
+
if (ref.scope === 'input') return cases.input(ref.port);
|
|
57
|
+
if (ref.scope === 'context') return cases.context(ref.key);
|
|
58
58
|
return cases.output(ref.node, ref.port);
|
|
59
59
|
};
|
|
60
60
|
|
package/src/resource.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
import { EVM_NATIVE_SENTINEL } from './chainCapabilities.js';
|
|
2
|
+
|
|
3
|
+
export type Availability = 'now' | 'future';
|
|
2
4
|
|
|
3
5
|
export interface OutputAmount {
|
|
4
6
|
readonly expected: bigint;
|
|
@@ -6,9 +8,9 @@ export interface OutputAmount {
|
|
|
6
8
|
}
|
|
7
9
|
|
|
8
10
|
export type Resource =
|
|
9
|
-
| { readonly kind:
|
|
11
|
+
| { readonly kind: 'native'; readonly chainId: number }
|
|
10
12
|
| {
|
|
11
|
-
readonly kind:
|
|
13
|
+
readonly kind: 'erc20';
|
|
12
14
|
readonly token: string;
|
|
13
15
|
readonly chainId: number;
|
|
14
16
|
};
|
|
@@ -25,23 +27,23 @@ export type ProducedResource = Resource & {
|
|
|
25
27
|
};
|
|
26
28
|
|
|
27
29
|
export const erc20Resource = (token: string, chainId: number): Resource => ({
|
|
28
|
-
kind:
|
|
30
|
+
kind: 'erc20',
|
|
29
31
|
token,
|
|
30
32
|
chainId,
|
|
31
33
|
});
|
|
32
34
|
|
|
33
35
|
export const nativeResource = (chainId: number): Resource => ({
|
|
34
|
-
kind:
|
|
36
|
+
kind: 'native',
|
|
35
37
|
chainId,
|
|
36
38
|
});
|
|
37
39
|
|
|
38
40
|
export const isERC20Resource = (
|
|
39
41
|
r: Resource,
|
|
40
|
-
): r is Extract<Resource, { kind:
|
|
42
|
+
): r is Extract<Resource, { kind: 'erc20' }> => r.kind === 'erc20';
|
|
41
43
|
|
|
42
44
|
export const isNativeResource = (
|
|
43
45
|
r: Resource,
|
|
44
|
-
): r is Extract<Resource, { kind:
|
|
46
|
+
): r is Extract<Resource, { kind: 'native' }> => r.kind === 'native';
|
|
45
47
|
|
|
46
48
|
export const resourcesEqual = (a: Resource, b: Resource): boolean => {
|
|
47
49
|
if (isNativeResource(a) && isNativeResource(b)) {
|
|
@@ -62,10 +64,18 @@ export const foldResource = <R>(
|
|
|
62
64
|
readonly erc20: (token: string, chainId: number) => R;
|
|
63
65
|
},
|
|
64
66
|
): R =>
|
|
65
|
-
r.kind ===
|
|
67
|
+
r.kind === 'native'
|
|
66
68
|
? cases.native(r.chainId)
|
|
67
69
|
: cases.erc20(r.token, r.chainId);
|
|
68
70
|
|
|
71
|
+
// Canonical address representation of a resource for consumers whose wire
|
|
72
|
+
// contract uses the EVM-native sentinel rather than the Resource union.
|
|
73
|
+
export const resourceToken = (r: Resource): string =>
|
|
74
|
+
foldResource(r, {
|
|
75
|
+
native: () => EVM_NATIVE_SENTINEL,
|
|
76
|
+
erc20: (token) => token,
|
|
77
|
+
});
|
|
78
|
+
|
|
69
79
|
export const resourceKey = (r: Resource): string =>
|
|
70
80
|
foldResource(r, {
|
|
71
81
|
native: (chainId) => `native:${chainId}`,
|
|
@@ -73,7 +83,7 @@ export const resourceKey = (r: Resource): string =>
|
|
|
73
83
|
});
|
|
74
84
|
|
|
75
85
|
export const erc20Token = (r: Resource): string => {
|
|
76
|
-
if (r.kind !==
|
|
86
|
+
if (r.kind !== 'erc20')
|
|
77
87
|
throw new Error(`Expected erc20 resource, got ${r.kind}`);
|
|
78
88
|
return r.token;
|
|
79
89
|
};
|
package/src/simulate.ts
CHANGED
|
@@ -53,7 +53,7 @@ export interface TrackedBalance {
|
|
|
53
53
|
* string; the string side covers values already stringified.
|
|
54
54
|
*/
|
|
55
55
|
export interface Erc20BalanceRequirement {
|
|
56
|
-
readonly type:
|
|
56
|
+
readonly type: 'Erc20Balance';
|
|
57
57
|
readonly wallet: string;
|
|
58
58
|
readonly token: string;
|
|
59
59
|
readonly balance: bigint | string;
|
|
@@ -61,14 +61,14 @@ export interface Erc20BalanceRequirement {
|
|
|
61
61
|
|
|
62
62
|
/** Funding instruction: seed a native balance on a wallet before simulation. */
|
|
63
63
|
export interface NativeBalanceRequirement {
|
|
64
|
-
readonly type:
|
|
64
|
+
readonly type: 'NativeBalance';
|
|
65
65
|
readonly wallet: string;
|
|
66
66
|
readonly balance: bigint | string;
|
|
67
67
|
}
|
|
68
68
|
|
|
69
69
|
/** Funding instruction: seed an ERC-20 allowance before simulation. */
|
|
70
70
|
export interface Erc20AllowanceRequirement {
|
|
71
|
-
readonly type:
|
|
71
|
+
readonly type: 'Erc20Allowance';
|
|
72
72
|
readonly owner: string;
|
|
73
73
|
readonly spender: string;
|
|
74
74
|
readonly token: string;
|
|
@@ -134,7 +134,7 @@ export interface SimulateBalanceEntry {
|
|
|
134
134
|
* bidirectional conformance assertion in `@lifi/api-schemas` relies on this.
|
|
135
135
|
*/
|
|
136
136
|
export interface SimulateOkResult {
|
|
137
|
-
readonly status:
|
|
137
|
+
readonly status: 'ok';
|
|
138
138
|
readonly block: number;
|
|
139
139
|
readonly timestamp: number;
|
|
140
140
|
readonly balancesBefore: SimulateBalanceEntry[];
|
|
@@ -157,7 +157,7 @@ export interface SimulateRevertDecodeResult {
|
|
|
157
157
|
* on-chain. A revert is a *successful simulation*, not a transport error.
|
|
158
158
|
*/
|
|
159
159
|
export interface SimulateRevertResult {
|
|
160
|
-
readonly status:
|
|
160
|
+
readonly status: 'revert';
|
|
161
161
|
readonly block: number;
|
|
162
162
|
readonly timestamp: number;
|
|
163
163
|
readonly revertReason?: string;
|
|
@@ -171,7 +171,7 @@ export interface SimulateRevertResult {
|
|
|
171
171
|
* could not be set up or run. `message` is intentionally generic.
|
|
172
172
|
*/
|
|
173
173
|
export interface SimulateSetupErrorResult {
|
|
174
|
-
readonly status:
|
|
174
|
+
readonly status: 'error';
|
|
175
175
|
readonly message: string;
|
|
176
176
|
}
|
|
177
177
|
|
|
@@ -180,6 +180,4 @@ export interface SimulateSetupErrorResult {
|
|
|
180
180
|
* one member: `ok` and `revert` come back with HTTP 200, `error` with HTTP 422.
|
|
181
181
|
*/
|
|
182
182
|
export type SimulateResult =
|
|
183
|
-
|
|
|
184
|
-
| SimulateRevertResult
|
|
185
|
-
| SimulateSetupErrorResult;
|
|
183
|
+
SimulateOkResult | SimulateRevertResult | SimulateSetupErrorResult;
|