@lifi/compose-spec 0.5.1 → 0.6.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/README.md +2 -1
- package/dist/index.cjs +96 -30
- package/dist/index.d.cts +184 -58
- package/dist/index.d.ts +184 -58
- package/dist/index.js +90 -32
- package/package.json +1 -1
- package/src/chainCapabilities.ts +187 -38
- package/src/compile.ts +9 -3
- package/src/continuation.ts +65 -36
- package/src/descriptor.ts +13 -17
- package/src/index.ts +27 -8
- package/src/output.ts +67 -0
- package/src/preconditions.ts +4 -0
- package/src/resource.ts +0 -16
- package/src/route.ts +2 -2
- package/src/version.ts +23 -15
package/dist/index.js
CHANGED
|
@@ -13,6 +13,13 @@ var TEMPO_NATIVE_ERC20 = {
|
|
|
13
13
|
symbol: "pathUSD",
|
|
14
14
|
decimals: 6
|
|
15
15
|
};
|
|
16
|
+
var ARC_CHAIN_ID = 5042;
|
|
17
|
+
var ARC_NATIVE_ERC20 = {
|
|
18
|
+
token: "0x3600000000000000000000000000000000000000",
|
|
19
|
+
symbol: "USDC",
|
|
20
|
+
decimals: 6
|
|
21
|
+
};
|
|
22
|
+
var ARC_NATIVE_SCALE = 10n ** BigInt(18 - ARC_NATIVE_ERC20.decimals);
|
|
16
23
|
var REGISTRY = /* @__PURE__ */ new Map([
|
|
17
24
|
[
|
|
18
25
|
TEMPO_CHAIN_ID,
|
|
@@ -21,31 +28,60 @@ var REGISTRY = /* @__PURE__ */ new Map([
|
|
|
21
28
|
chainId: TEMPO_CHAIN_ID,
|
|
22
29
|
nativeErc20: TEMPO_NATIVE_ERC20
|
|
23
30
|
}
|
|
31
|
+
],
|
|
32
|
+
[
|
|
33
|
+
ARC_CHAIN_ID,
|
|
34
|
+
{
|
|
35
|
+
kind: "native-mirrored-erc20",
|
|
36
|
+
chainId: ARC_CHAIN_ID,
|
|
37
|
+
nativeErc20: ARC_NATIVE_ERC20,
|
|
38
|
+
nativeScale: ARC_NATIVE_SCALE
|
|
39
|
+
}
|
|
24
40
|
]
|
|
25
41
|
]);
|
|
26
42
|
var getChainCapabilities = (chainId) => REGISTRY.get(chainId) ?? { kind: "evm-native", chainId };
|
|
27
|
-
var
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
43
|
+
var foldChainCapabilities = (caps, cases) => {
|
|
44
|
+
switch (caps.kind) {
|
|
45
|
+
case "evm-native":
|
|
46
|
+
return cases.evmNative(caps.chainId);
|
|
47
|
+
case "as-erc20":
|
|
48
|
+
return cases.asErc20(caps.nativeErc20, caps.chainId);
|
|
49
|
+
case "native-mirrored-erc20":
|
|
50
|
+
return cases.nativeMirroredErc20(
|
|
51
|
+
caps.nativeErc20,
|
|
52
|
+
caps.nativeScale,
|
|
53
|
+
caps.chainId
|
|
54
|
+
);
|
|
55
|
+
}
|
|
31
56
|
};
|
|
57
|
+
var nativeCheckPolicy = (chainId) => foldChainCapabilities(getChainCapabilities(chainId), {
|
|
58
|
+
evmNative: () => ({ kind: "keep" }),
|
|
59
|
+
asErc20: (nativeErc20) => ({ kind: "rewrite", token: nativeErc20.token }),
|
|
60
|
+
nativeMirroredErc20: (nativeErc20) => ({
|
|
61
|
+
kind: "reject",
|
|
62
|
+
reason: `nativeBalanceGte is not accepted on chain ${chainId}: its canonical native asset is an ERC-20 facade whose units differ from native wei \u2014 author the check as erc20BalanceGte of ${nativeErc20.token}`
|
|
63
|
+
})
|
|
64
|
+
});
|
|
65
|
+
var isAsErc20Chain = (chainId) => getChainCapabilities(chainId).kind === "as-erc20";
|
|
66
|
+
var erc20CanonicalNative = (caps) => caps.kind === "as-erc20" || caps.kind === "native-mirrored-erc20" ? caps.nativeErc20 : void 0;
|
|
67
|
+
var hasErc20CanonicalNative = (chainId) => erc20CanonicalNative(getChainCapabilities(chainId)) !== void 0;
|
|
68
|
+
var canonicalNativeAddress = (chainId) => erc20CanonicalNative(getChainCapabilities(chainId))?.token ?? EVM_NATIVE_SENTINEL;
|
|
32
69
|
var isNativeAddressForChain = (chainId, address) => {
|
|
33
|
-
const
|
|
34
|
-
const target = caps.kind === "as-erc20" ? caps.nativeErc20.token : EVM_NATIVE_SENTINEL;
|
|
70
|
+
const target = canonicalNativeAddress(chainId);
|
|
35
71
|
return address.toLowerCase() === target.toLowerCase();
|
|
36
72
|
};
|
|
37
|
-
var isMsgValueNative = (chainId, address) => !
|
|
73
|
+
var isMsgValueNative = (chainId, address) => !hasErc20CanonicalNative(chainId) && isNativeSentinel(address);
|
|
38
74
|
var normalizeResource = (r) => {
|
|
39
75
|
if (r.kind !== "native") return r;
|
|
40
|
-
const
|
|
41
|
-
if (
|
|
76
|
+
const erc202 = erc20CanonicalNative(getChainCapabilities(r.chainId));
|
|
77
|
+
if (erc202 === void 0) return r;
|
|
42
78
|
return {
|
|
43
79
|
kind: "erc20",
|
|
44
80
|
chainId: r.chainId,
|
|
45
|
-
token:
|
|
81
|
+
token: erc202.token
|
|
46
82
|
};
|
|
47
83
|
};
|
|
48
|
-
var normalizeNativeTokenAddress = (token, chainId) =>
|
|
84
|
+
var normalizeNativeTokenAddress = (token, chainId) => hasErc20CanonicalNative(chainId) && isNativeSentinel(token) ? canonicalNativeAddress(chainId) : token;
|
|
49
85
|
|
|
50
86
|
// src/flowSchema.ts
|
|
51
87
|
var SolTypeSchema = Schema.Literal(
|
|
@@ -135,7 +171,7 @@ var FlowSchema = Schema.Struct({
|
|
|
135
171
|
});
|
|
136
172
|
|
|
137
173
|
// src/continuation.ts
|
|
138
|
-
import { Schema as Schema2 } from "effect";
|
|
174
|
+
import { ParseResult, Schema as Schema2 } from "effect";
|
|
139
175
|
var CONTINUATION_COMMITMENT_OP_ID = "continuation.commitment";
|
|
140
176
|
var RawContinuationOutcomeSchema = Schema2.Struct({
|
|
141
177
|
token: Schema2.String,
|
|
@@ -211,25 +247,36 @@ var CallCheckSpecSchema = Schema2.Struct({
|
|
|
211
247
|
comparator: Schema2.Literal("gte", "eq", "lte"),
|
|
212
248
|
value: Schema2.String
|
|
213
249
|
});
|
|
214
|
-
var asErc20BalanceGteCheck = (
|
|
250
|
+
var asErc20BalanceGteCheck = (check, token) => ({
|
|
215
251
|
kind: "erc20BalanceGte",
|
|
216
|
-
chainId,
|
|
217
|
-
token
|
|
218
|
-
owner,
|
|
219
|
-
minAmount
|
|
220
|
-
});
|
|
252
|
+
chainId: check.chainId,
|
|
253
|
+
token,
|
|
254
|
+
owner: check.owner,
|
|
255
|
+
minAmount: check.minAmount
|
|
256
|
+
});
|
|
257
|
+
var decodeNativeBalanceGte = (check, ast) => {
|
|
258
|
+
const policy = nativeCheckPolicy(check.chainId);
|
|
259
|
+
switch (policy.kind) {
|
|
260
|
+
case "keep":
|
|
261
|
+
return ParseResult.succeed(check);
|
|
262
|
+
case "rewrite":
|
|
263
|
+
return ParseResult.succeed(asErc20BalanceGteCheck(check, policy.token));
|
|
264
|
+
case "reject":
|
|
265
|
+
return ParseResult.fail(new ParseResult.Type(ast, check, policy.reason));
|
|
266
|
+
}
|
|
267
|
+
};
|
|
221
268
|
var RawCheckSchema = Schema2.Union(
|
|
222
269
|
Erc20BalanceGteSpecSchema,
|
|
223
270
|
NativeBalanceGteSpecSchema,
|
|
224
271
|
CallCheckSpecSchema
|
|
225
272
|
);
|
|
226
|
-
var CheckSchema = Schema2.
|
|
273
|
+
var CheckSchema = Schema2.transformOrFail(
|
|
227
274
|
RawCheckSchema,
|
|
228
275
|
Schema2.typeSchema(RawCheckSchema),
|
|
229
276
|
{
|
|
230
277
|
strict: true,
|
|
231
|
-
decode: (check) => check.kind === "nativeBalanceGte"
|
|
232
|
-
encode: (check) => check
|
|
278
|
+
decode: (check, _options, ast) => check.kind === "nativeBalanceGte" ? decodeNativeBalanceGte(check, ast) : ParseResult.succeed(check),
|
|
279
|
+
encode: (check) => ParseResult.succeed(check)
|
|
233
280
|
}
|
|
234
281
|
);
|
|
235
282
|
var Erc20BalanceDeltaGteSpecSchema = Schema2.Struct({
|
|
@@ -259,12 +306,13 @@ var ContinuationPlanSchema = Schema2.Struct({
|
|
|
259
306
|
validity: Schema2.Struct({ expiresAtMs: Schema2.Number }),
|
|
260
307
|
correlationId: Schema2.optional(Schema2.String)
|
|
261
308
|
});
|
|
309
|
+
var settledOutcome = (intent) => intent.outcomes[0];
|
|
262
310
|
var isGTECheck = (check) => check.kind === "call" && check.comparator === "gte";
|
|
263
311
|
var isLTECheck = (check) => check.kind === "call" && check.comparator === "lte";
|
|
264
312
|
var isEQCheck = (check) => check.kind === "call" && check.comparator === "eq";
|
|
265
313
|
|
|
266
314
|
// src/descriptor.ts
|
|
267
|
-
import { Schema as Schema3 } from "effect";
|
|
315
|
+
import { ParseResult as ParseResult2, Schema as Schema3 } from "effect";
|
|
268
316
|
|
|
269
317
|
// src/hexPatterns.ts
|
|
270
318
|
var BYTES32_LOWER_HEX = /^0x[0-9a-f]{64}$/;
|
|
@@ -293,13 +341,13 @@ var RawAssertedPredicateSchema = Schema3.Union(
|
|
|
293
341
|
Erc20BalanceDeltaGteSpecSchema,
|
|
294
342
|
NativeBalanceDeltaGteSpecSchema
|
|
295
343
|
);
|
|
296
|
-
var AssertedPredicateSchema = Schema3.
|
|
344
|
+
var AssertedPredicateSchema = Schema3.transformOrFail(
|
|
297
345
|
RawAssertedPredicateSchema,
|
|
298
346
|
Schema3.typeSchema(RawAssertedPredicateSchema),
|
|
299
347
|
{
|
|
300
348
|
strict: true,
|
|
301
|
-
decode: (pred) => pred.kind === "nativeBalanceGte"
|
|
302
|
-
encode: (pred) => pred
|
|
349
|
+
decode: (pred, _options, ast) => pred.kind === "nativeBalanceGte" ? decodeNativeBalanceGte(pred, ast) : ParseResult2.succeed(pred),
|
|
350
|
+
encode: (pred) => ParseResult2.succeed(pred)
|
|
303
351
|
}
|
|
304
352
|
);
|
|
305
353
|
var VerificationDescriptorSchema = Schema3.Struct({
|
|
@@ -506,6 +554,7 @@ var refKey = (ref2) => foldRef(parseRef(ref2), {
|
|
|
506
554
|
var erc20Balance = (config) => ({ type: "Erc20Balance", ...config });
|
|
507
555
|
var nativeBalance = (config) => ({ type: "NativeBalance", ...config });
|
|
508
556
|
var erc20Allowance = (config) => ({ type: "Erc20Allowance", ...config });
|
|
557
|
+
var isErc20BalancePrecondition = (p) => p.type === "Erc20Balance";
|
|
509
558
|
var isErc20AllowancePrecondition = (p) => p.type === "Erc20Allowance";
|
|
510
559
|
var isNativeBalancePrecondition = (p) => p.type === "NativeBalance";
|
|
511
560
|
|
|
@@ -529,12 +578,13 @@ var parseVersion = (version) => {
|
|
|
529
578
|
return { major, minor, patch };
|
|
530
579
|
};
|
|
531
580
|
var compareVersions = (a, b) => a.major !== b.major ? a.major - b.major : a.minor !== b.minor ? a.minor - b.minor : a.patch - b.patch;
|
|
532
|
-
var checkCompatibility = (client, server
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
581
|
+
var checkCompatibility = (client, server, minimum = {
|
|
582
|
+
major: server.major,
|
|
583
|
+
minor: server.minor,
|
|
584
|
+
patch: 0
|
|
585
|
+
}) => {
|
|
586
|
+
if (compareVersions(client, server) > 0) return "server_outdated";
|
|
587
|
+
if (compareVersions(client, minimum) < 0) return "sdk_outdated";
|
|
538
588
|
return "compatible";
|
|
539
589
|
};
|
|
540
590
|
|
|
@@ -779,6 +829,9 @@ var ContinuationPlanZod = z.object({
|
|
|
779
829
|
correlationId: z.string().optional()
|
|
780
830
|
});
|
|
781
831
|
export {
|
|
832
|
+
ARC_CHAIN_ID,
|
|
833
|
+
ARC_NATIVE_ERC20,
|
|
834
|
+
ARC_NATIVE_SCALE,
|
|
782
835
|
AppliedGuardSchema,
|
|
783
836
|
AssertedErc20BalanceGteSpecSchema,
|
|
784
837
|
AssertedNativeBalanceGteSpecSchema,
|
|
@@ -852,16 +905,19 @@ export {
|
|
|
852
905
|
flowInputMode,
|
|
853
906
|
flowInputResource,
|
|
854
907
|
flowInputType,
|
|
908
|
+
foldChainCapabilities,
|
|
855
909
|
foldMaterialiserMetadata,
|
|
856
910
|
foldRef,
|
|
857
911
|
foldResource,
|
|
858
912
|
getChainCapabilities,
|
|
859
913
|
handle,
|
|
914
|
+
hasErc20CanonicalNative,
|
|
860
915
|
isAsErc20Chain,
|
|
861
916
|
isContextKey,
|
|
862
917
|
isEQCheck,
|
|
863
918
|
isERC20Resource,
|
|
864
919
|
isErc20AllowancePrecondition,
|
|
920
|
+
isErc20BalancePrecondition,
|
|
865
921
|
isFutureResourceOutput,
|
|
866
922
|
isGTECheck,
|
|
867
923
|
isHandlePort,
|
|
@@ -879,6 +935,7 @@ export {
|
|
|
879
935
|
linear,
|
|
880
936
|
native,
|
|
881
937
|
nativeBalance,
|
|
938
|
+
nativeCheckPolicy,
|
|
882
939
|
nativeResource,
|
|
883
940
|
normalizeNativeTokenAddress,
|
|
884
941
|
normalizeResource,
|
|
@@ -894,5 +951,6 @@ export {
|
|
|
894
951
|
resourceKey,
|
|
895
952
|
resourceOutput,
|
|
896
953
|
resourceToken,
|
|
897
|
-
resourcesEqual
|
|
954
|
+
resourcesEqual,
|
|
955
|
+
settledOutcome
|
|
898
956
|
};
|
package/package.json
CHANGED
package/src/chainCapabilities.ts
CHANGED
|
@@ -4,15 +4,26 @@ import type { Resource } from './resource.js';
|
|
|
4
4
|
* Per-chain native-asset semantics.
|
|
5
5
|
*
|
|
6
6
|
* Most EVM chains have an ETH-style native gas coin sent via `msg.value` (the
|
|
7
|
-
* `evm-native` policy). A small number
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
7
|
+
* `evm-native` policy). A small number express their "native" coin as an
|
|
8
|
+
* ERC-20 token: on `as-erc20` chains (Tempo) no `msg.value` coin exists at
|
|
9
|
+
* all, while on `native-mirrored-erc20` chains (Arc) a real `msg.value` coin
|
|
10
|
+
* exists but is canonically routed through an ERC-20 facade that mirrors the
|
|
11
|
+
* native balance (`balanceOf == native wei / nativeScale`, with no balance
|
|
12
|
+
* storage of its own). Code that needs to lower or simulate a `Resource` of
|
|
13
|
+
* kind `"native"` should consult `getChainCapabilities(chainId)` rather than
|
|
14
|
+
* hardcoding chain IDs.
|
|
11
15
|
*
|
|
12
16
|
* The compose compiler uses this registry to transparently normalise native
|
|
13
|
-
* resources into ERC-20 resources on
|
|
14
|
-
* (see `normalizeResource` below and the `ResourceSchema`
|
|
15
|
-
* `flowSchema.ts`).
|
|
17
|
+
* resources into ERC-20 resources on both ERC-20-canonical kinds at
|
|
18
|
+
* schema-decode time (see `normalizeResource` below and the `ResourceSchema`
|
|
19
|
+
* transform in `flowSchema.ts`).
|
|
20
|
+
*
|
|
21
|
+
* The two ERC-20-canonical kinds behave identically at the resource level —
|
|
22
|
+
* `hasErc20CanonicalNative` is the predicate for that shared behaviour. They
|
|
23
|
+
* diverge only at the simulation layer: `as-erc20` balances live in ordinary
|
|
24
|
+
* token storage, while `native-mirrored-erc20` balances have no storage slot
|
|
25
|
+
* and are satisfied by a native balance override scaled by `nativeScale`
|
|
26
|
+
* (see compose-core's `rewritePreconditions`).
|
|
16
27
|
*
|
|
17
28
|
* This union is the extension point for future native-asset policies. A chain
|
|
18
29
|
* with no canonical native asset would get a `reject-native` arm, under which a
|
|
@@ -25,6 +36,13 @@ export type ChainCapabilities =
|
|
|
25
36
|
readonly kind: 'as-erc20';
|
|
26
37
|
readonly chainId: number;
|
|
27
38
|
readonly nativeErc20: NativeErc20;
|
|
39
|
+
}
|
|
40
|
+
| {
|
|
41
|
+
readonly kind: 'native-mirrored-erc20';
|
|
42
|
+
readonly chainId: number;
|
|
43
|
+
readonly nativeErc20: NativeErc20;
|
|
44
|
+
/** Native wei per 1 unit of the facade ERC-20 (1e12 on Arc: 18-dec native, 6-dec USDC). */
|
|
45
|
+
readonly nativeScale: bigint;
|
|
28
46
|
};
|
|
29
47
|
|
|
30
48
|
export interface NativeErc20 {
|
|
@@ -62,6 +80,25 @@ export const TEMPO_NATIVE_ERC20: NativeErc20 = {
|
|
|
62
80
|
decimals: 6,
|
|
63
81
|
};
|
|
64
82
|
|
|
83
|
+
export const ARC_CHAIN_ID = 5042;
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Arc's native gas coin is USDC (18-decimal wei at the EVM level). The USDC
|
|
87
|
+
* ERC-20 at this address is a view over native balances: `balanceOf` returns
|
|
88
|
+
* `native wei / 1e12` and has no balance storage slot; allowances live in
|
|
89
|
+
* regular storage. The composer interacts with USDC exclusively through this
|
|
90
|
+
* facade, never via `msg.value`.
|
|
91
|
+
*/
|
|
92
|
+
export const ARC_NATIVE_ERC20: NativeErc20 = {
|
|
93
|
+
token: '0x3600000000000000000000000000000000000000',
|
|
94
|
+
symbol: 'USDC',
|
|
95
|
+
decimals: 6,
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
// Derived from the facade's decimals so a decimal-count change cannot leave
|
|
99
|
+
// the scale silently wrong. Native wei is 18-decimal at the EVM level.
|
|
100
|
+
export const ARC_NATIVE_SCALE = 10n ** BigInt(18 - ARC_NATIVE_ERC20.decimals);
|
|
101
|
+
|
|
65
102
|
const REGISTRY: ReadonlyMap<number, ChainCapabilities> = new Map<
|
|
66
103
|
number,
|
|
67
104
|
ChainCapabilities
|
|
@@ -74,41 +111,151 @@ const REGISTRY: ReadonlyMap<number, ChainCapabilities> = new Map<
|
|
|
74
111
|
nativeErc20: TEMPO_NATIVE_ERC20,
|
|
75
112
|
},
|
|
76
113
|
],
|
|
114
|
+
[
|
|
115
|
+
ARC_CHAIN_ID,
|
|
116
|
+
{
|
|
117
|
+
kind: 'native-mirrored-erc20',
|
|
118
|
+
chainId: ARC_CHAIN_ID,
|
|
119
|
+
nativeErc20: ARC_NATIVE_ERC20,
|
|
120
|
+
nativeScale: ARC_NATIVE_SCALE,
|
|
121
|
+
},
|
|
122
|
+
],
|
|
77
123
|
]);
|
|
78
124
|
|
|
79
125
|
export const getChainCapabilities = (chainId: number): ChainCapabilities =>
|
|
80
126
|
REGISTRY.get(chainId) ?? { kind: 'evm-native', chainId };
|
|
81
127
|
|
|
128
|
+
/**
|
|
129
|
+
* Structural fold over the `ChainCapabilities` union — the capabilities
|
|
130
|
+
* counterpart of `foldResource`/`foldRef`. Dispatches on `kind` and hands each
|
|
131
|
+
* case its narrowed fields, so consumers that must branch on the native-asset
|
|
132
|
+
* policy (e.g. compose-core's `rewritePreconditions`) do so through one
|
|
133
|
+
* combinator whose exhaustiveness the compiler enforces: adding a new arm to
|
|
134
|
+
* the union above turns every `foldChainCapabilities` call into a type error
|
|
135
|
+
* until it handles the new case.
|
|
136
|
+
*/
|
|
137
|
+
export const foldChainCapabilities = <R>(
|
|
138
|
+
caps: ChainCapabilities,
|
|
139
|
+
cases: {
|
|
140
|
+
readonly evmNative: (chainId: number) => R;
|
|
141
|
+
readonly asErc20: (nativeErc20: NativeErc20, chainId: number) => R;
|
|
142
|
+
readonly nativeMirroredErc20: (
|
|
143
|
+
nativeErc20: NativeErc20,
|
|
144
|
+
nativeScale: bigint,
|
|
145
|
+
chainId: number,
|
|
146
|
+
) => R;
|
|
147
|
+
},
|
|
148
|
+
): R => {
|
|
149
|
+
switch (caps.kind) {
|
|
150
|
+
case 'evm-native':
|
|
151
|
+
return cases.evmNative(caps.chainId);
|
|
152
|
+
case 'as-erc20':
|
|
153
|
+
return cases.asErc20(caps.nativeErc20, caps.chainId);
|
|
154
|
+
case 'native-mirrored-erc20':
|
|
155
|
+
return cases.nativeMirroredErc20(
|
|
156
|
+
caps.nativeErc20,
|
|
157
|
+
caps.nativeScale,
|
|
158
|
+
caps.chainId,
|
|
159
|
+
);
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* What a wire-level native-balance check (`nativeBalanceGte`) must become on
|
|
165
|
+
* a chain, decided once here from the native-asset policy:
|
|
166
|
+
*
|
|
167
|
+
* - `keep` — `evm-native`: the native read is the right read.
|
|
168
|
+
* - `rewrite` — `as-erc20` (Tempo): no `msg.value` coin exists, so the check
|
|
169
|
+
* becomes an `erc20BalanceGte` of `token`, the stand-in ERC-20. Amounts were
|
|
170
|
+
* always facade-denominated, so `minAmount` carries over verbatim.
|
|
171
|
+
* - `reject` — `native-mirrored-erc20` (Arc): a native read returns real
|
|
172
|
+
* 18-decimal wei, `nativeScale`× the facade's units, so no interpretation of
|
|
173
|
+
* `minAmount` is safe — reusing it on a facade read is 1e12 too strict for a
|
|
174
|
+
* wei-denominated check, keeping the native read lets a facade-denominated
|
|
175
|
+
* check pass 1e12 too easily. The check must be authored as
|
|
176
|
+
* `erc20BalanceGte` of the facade; `reason` says so.
|
|
177
|
+
*
|
|
178
|
+
* Consumers (the `CheckSchema` / `AssertedPredicateSchema` decodes and
|
|
179
|
+
* compose-core's derive backstop) are adapters over this result, so the unit
|
|
180
|
+
* rule lives in one place and the fold's exhaustiveness reaches every site.
|
|
181
|
+
*/
|
|
182
|
+
export type NativeCheckPolicy =
|
|
183
|
+
| { readonly kind: 'keep' }
|
|
184
|
+
| { readonly kind: 'rewrite'; readonly token: `0x${string}` }
|
|
185
|
+
| { readonly kind: 'reject'; readonly reason: string };
|
|
186
|
+
|
|
187
|
+
export const nativeCheckPolicy = (chainId: number): NativeCheckPolicy =>
|
|
188
|
+
foldChainCapabilities<NativeCheckPolicy>(getChainCapabilities(chainId), {
|
|
189
|
+
evmNative: () => ({ kind: 'keep' }),
|
|
190
|
+
asErc20: (nativeErc20) => ({ kind: 'rewrite', token: nativeErc20.token }),
|
|
191
|
+
nativeMirroredErc20: (nativeErc20) => ({
|
|
192
|
+
kind: 'reject',
|
|
193
|
+
reason:
|
|
194
|
+
`nativeBalanceGte is not accepted on chain ${chainId}: its canonical ` +
|
|
195
|
+
`native asset is an ERC-20 facade whose units differ from native wei — ` +
|
|
196
|
+
`author the check as erc20BalanceGte of ${nativeErc20.token}`,
|
|
197
|
+
}),
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Exact-kind check for `as-erc20` (currently Tempo only) — the rare case that
|
|
202
|
+
* genuinely means "no `msg.value` coin exists on this chain". Most consumers
|
|
203
|
+
* want `hasErc20CanonicalNative` instead, which also covers
|
|
204
|
+
* `native-mirrored-erc20` chains (Arc), where the composer's ERC-20-only
|
|
205
|
+
* policy applies even though `msg.value` works. Code that must treat the two
|
|
206
|
+
* ERC-20-native kinds differently goes through `foldChainCapabilities` or
|
|
207
|
+
* `nativeCheckPolicy`, never a second exact-kind predicate.
|
|
208
|
+
*/
|
|
82
209
|
export const isAsErc20Chain = (chainId: number): boolean =>
|
|
83
210
|
getChainCapabilities(chainId).kind === 'as-erc20';
|
|
84
211
|
|
|
212
|
+
/**
|
|
213
|
+
* The chain's stand-in native ERC-20 when its canonical native asset is an
|
|
214
|
+
* ERC-20 (`as-erc20` and `native-mirrored-erc20`), `undefined` on plain
|
|
215
|
+
* `evm-native` chains. Module-local narrowing helper behind the exported
|
|
216
|
+
* predicates and rewrites below.
|
|
217
|
+
*/
|
|
218
|
+
const erc20CanonicalNative = (
|
|
219
|
+
caps: ChainCapabilities,
|
|
220
|
+
): NativeErc20 | undefined =>
|
|
221
|
+
caps.kind === 'as-erc20' || caps.kind === 'native-mirrored-erc20'
|
|
222
|
+
? caps.nativeErc20
|
|
223
|
+
: undefined;
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Whether the chain's canonical native asset is an ERC-20 — true for both
|
|
227
|
+
* `as-erc20` chains (Tempo: no `msg.value` coin exists) and
|
|
228
|
+
* `native-mirrored-erc20` chains (Arc: `msg.value` exists but is never used).
|
|
229
|
+
* This is the predicate for the shared resource-level behaviour: native
|
|
230
|
+
* resources are rewritten to the ERC-20, native routing edges are dropped, and
|
|
231
|
+
* a surviving native resource is a compile error.
|
|
232
|
+
*/
|
|
233
|
+
export const hasErc20CanonicalNative = (chainId: number): boolean =>
|
|
234
|
+
erc20CanonicalNative(getChainCapabilities(chainId)) !== undefined;
|
|
235
|
+
|
|
85
236
|
/**
|
|
86
237
|
* The address that represents the chain's native asset.
|
|
87
238
|
*
|
|
88
|
-
* On EVM-native chains this is the zero-address sentinel `0x0…0`. On
|
|
89
|
-
*
|
|
239
|
+
* On EVM-native chains this is the zero-address sentinel `0x0…0`. On chains
|
|
240
|
+
* whose canonical native asset is an ERC-20 it is the chain's stand-in token
|
|
241
|
+
* (PathUSD on Tempo, the USDC facade on Arc).
|
|
90
242
|
*/
|
|
91
|
-
export const canonicalNativeAddress = (chainId: number): `0x${string}` =>
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
? caps.nativeErc20.token
|
|
95
|
-
: EVM_NATIVE_SENTINEL;
|
|
96
|
-
};
|
|
243
|
+
export const canonicalNativeAddress = (chainId: number): `0x${string}` =>
|
|
244
|
+
erc20CanonicalNative(getChainCapabilities(chainId))?.token ??
|
|
245
|
+
EVM_NATIVE_SENTINEL;
|
|
97
246
|
|
|
98
247
|
/**
|
|
99
248
|
* Whether `address` represents the native asset of `chainId`.
|
|
100
249
|
*
|
|
101
250
|
* On EVM-native chains, this is the case-insensitive zero-address check. On
|
|
102
|
-
*
|
|
103
|
-
* chain's stand-in ERC-20 address matches.
|
|
251
|
+
* chains whose canonical native asset is an ERC-20, the literal zero address
|
|
252
|
+
* returns `false` — only the chain's stand-in ERC-20 address matches.
|
|
104
253
|
*/
|
|
105
254
|
export const isNativeAddressForChain = (
|
|
106
255
|
chainId: number,
|
|
107
256
|
address: string,
|
|
108
257
|
): boolean => {
|
|
109
|
-
const
|
|
110
|
-
const target =
|
|
111
|
-
caps.kind === 'as-erc20' ? caps.nativeErc20.token : EVM_NATIVE_SENTINEL;
|
|
258
|
+
const target = canonicalNativeAddress(chainId);
|
|
112
259
|
return address.toLowerCase() === target.toLowerCase();
|
|
113
260
|
};
|
|
114
261
|
|
|
@@ -119,30 +266,32 @@ export const isNativeAddressForChain = (
|
|
|
119
266
|
* This is the single predicate that gates `msg.value` semantics — choosing a
|
|
120
267
|
* `NativeBalance` over an `Erc20Balance` simulation requirement, and skipping
|
|
121
268
|
* the ERC-20 approval a native input would otherwise need. It returns `false`
|
|
122
|
-
* for every address on
|
|
123
|
-
*
|
|
124
|
-
* never `msg.value
|
|
125
|
-
*
|
|
126
|
-
*
|
|
127
|
-
*
|
|
128
|
-
* `
|
|
269
|
+
* for every address on a chain whose canonical native asset is an ERC-20
|
|
270
|
+
* (including the zero sentinel): on `as-erc20` chains the "native" coin is a
|
|
271
|
+
* real ERC-20 moved via `transferFrom`, never `msg.value`, and on
|
|
272
|
+
* `native-mirrored-erc20` chains `msg.value` works but the composer's policy
|
|
273
|
+
* is to move the coin exclusively through the ERC-20 facade. Use this rather
|
|
274
|
+
* than re-deriving the check at call sites, and rather than
|
|
275
|
+
* `isNativeAddressForChain`, which returns `true` for the stand-in ERC-20
|
|
276
|
+
* (PathUSD on Tempo) and would wrongly route it to `msg.value`.
|
|
129
277
|
*/
|
|
130
278
|
export const isMsgValueNative = (chainId: number, address: string): boolean =>
|
|
131
|
-
!
|
|
279
|
+
!hasErc20CanonicalNative(chainId) && isNativeSentinel(address);
|
|
132
280
|
|
|
133
281
|
/**
|
|
134
282
|
* Rewrite a single `Resource` according to its chain's capabilities.
|
|
135
283
|
*
|
|
136
284
|
* On chains with the default `evm-native` policy, every resource is returned
|
|
137
|
-
* unchanged. On
|
|
138
|
-
* resource of kind `"native"` is
|
|
139
|
-
* resource pointing at the chain's
|
|
285
|
+
* unchanged. On chains whose canonical native asset is an ERC-20 (`as-erc20`
|
|
286
|
+
* Tempo, `native-mirrored-erc20` Arc), a resource of kind `"native"` is
|
|
287
|
+
* rewritten into the equivalent ERC-20 resource pointing at the chain's
|
|
288
|
+
* stand-in token (PathUSD on Tempo, the USDC facade on Arc).
|
|
140
289
|
*
|
|
141
290
|
* This is the rewrite primitive behind the `ResourceSchema` decode transform
|
|
142
291
|
* (see `flowSchema.ts`), which is THE chokepoint: it runs every time a
|
|
143
292
|
* `Resource` is parsed — flow inputs via `FlowSchema`, op configs via
|
|
144
293
|
* `decodeConfig` — so every downstream pass (`normalizeFlow`, `resolve`,
|
|
145
|
-
* `lower`, `simulate`) sees only ERC-20 resources on
|
|
294
|
+
* `lower`, `simulate`) sees only ERC-20 resources on those chains.
|
|
146
295
|
*
|
|
147
296
|
* The function preserves referential identity when nothing changes (returns the
|
|
148
297
|
* input `r` unchanged on evm-native chains and for ERC-20 resources) and is
|
|
@@ -150,25 +299,25 @@ export const isMsgValueNative = (chainId: number, address: string): boolean =>
|
|
|
150
299
|
*/
|
|
151
300
|
export const normalizeResource = (r: Resource): Resource => {
|
|
152
301
|
if (r.kind !== 'native') return r;
|
|
153
|
-
const
|
|
154
|
-
if (
|
|
302
|
+
const erc20 = erc20CanonicalNative(getChainCapabilities(r.chainId));
|
|
303
|
+
if (erc20 === undefined) return r;
|
|
155
304
|
return {
|
|
156
305
|
kind: 'erc20',
|
|
157
306
|
chainId: r.chainId,
|
|
158
|
-
token:
|
|
307
|
+
token: erc20.token,
|
|
159
308
|
};
|
|
160
309
|
};
|
|
161
310
|
|
|
162
311
|
/**
|
|
163
312
|
* Address-level counterpart of `normalizeResource` for wire fields that carry a
|
|
164
313
|
* native asset as a bare token string (a continuation outcome's `token`). On
|
|
165
|
-
*
|
|
166
|
-
* ERC-20; every other address passes through.
|
|
314
|
+
* chains whose canonical native asset is an ERC-20, the native sentinel is
|
|
315
|
+
* rewritten to the chain's stand-in ERC-20; every other address passes through.
|
|
167
316
|
*/
|
|
168
317
|
export const normalizeNativeTokenAddress = (
|
|
169
318
|
token: string,
|
|
170
319
|
chainId: number,
|
|
171
320
|
): string =>
|
|
172
|
-
|
|
321
|
+
hasErc20CanonicalNative(chainId) && isNativeSentinel(token)
|
|
173
322
|
? canonicalNativeAddress(chainId)
|
|
174
323
|
: token;
|
package/src/compile.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { VerificationDescriptor } from './descriptor.js';
|
|
2
2
|
import type { InputSpec, Precondition } from './flow.js';
|
|
3
3
|
import type { Flow } from './flowSchema.js';
|
|
4
|
-
import type {
|
|
4
|
+
import type { Outputs } from './output.js';
|
|
5
5
|
import type { ContinuationPlanWire, Phase1ArtifactWire } from './zodSchemas.js';
|
|
6
6
|
|
|
7
7
|
export type SimulationPolicy = 'strict' | 'allow-revert';
|
|
@@ -149,7 +149,11 @@ export interface ComposeFill {
|
|
|
149
149
|
}
|
|
150
150
|
|
|
151
151
|
export interface ComposeCompileSuccessData extends ComposeContinuationData {
|
|
152
|
-
|
|
152
|
+
// Every linear resource output port, consumed or terminal, keyed
|
|
153
|
+
// `<nodeId>.<portName>`. See `Output` for the per-entry rules. Hand-mirrored
|
|
154
|
+
// from api-schemas' `OutputSchema` (the OpenAPI source of truth; this
|
|
155
|
+
// package cannot depend on it); the SDK decodes the wire strings to `bigint`.
|
|
156
|
+
readonly outputs: Outputs;
|
|
153
157
|
// The OpenAPI source of truth documents the optionality and funding modes.
|
|
154
158
|
readonly transactionRequest?: ComposeTransactionRequest;
|
|
155
159
|
readonly actions?: readonly ComposeAction[];
|
|
@@ -176,7 +180,9 @@ export interface ComposeCompileSuccessData extends ComposeContinuationData {
|
|
|
176
180
|
}
|
|
177
181
|
|
|
178
182
|
export interface ComposeCompilePartialData extends ComposeContinuationData {
|
|
179
|
-
|
|
183
|
+
// Mirrors `ComposeCompileSuccessData.outputs`. A revert measured nothing, so
|
|
184
|
+
// estimates are quotes and minimums are op-sourced.
|
|
185
|
+
readonly outputs: Outputs;
|
|
180
186
|
readonly transactionRequest: ComposeTransactionRequest;
|
|
181
187
|
readonly userProxy: string;
|
|
182
188
|
readonly simulationRevert: SimulationRevert;
|