@lifi/compose-spec 0.6.0 → 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/dist/index.cjs +84 -22
- package/dist/index.d.cts +125 -33
- package/dist/index.d.ts +125 -33
- package/dist/index.js +80 -25
- package/package.json +1 -1
- package/src/chainCapabilities.ts +187 -38
- package/src/continuation.ts +55 -36
- package/src/descriptor.ts +13 -17
- package/src/index.ts +12 -1
- package/src/preconditions.ts +4 -0
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/continuation.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import { Schema } from 'effect';
|
|
1
|
+
import { type Effect, ParseResult, Schema, type SchemaAST } from 'effect';
|
|
2
2
|
|
|
3
3
|
import {
|
|
4
|
-
|
|
5
|
-
isAsErc20Chain,
|
|
4
|
+
nativeCheckPolicy,
|
|
6
5
|
normalizeNativeTokenAddress,
|
|
7
6
|
} from './chainCapabilities.js';
|
|
8
7
|
import { ChainIdSchema, FlowSchema } from './flowSchema.js';
|
|
@@ -155,25 +154,55 @@ export const CallCheckSpecSchema = Schema.Struct({
|
|
|
155
154
|
value: Schema.String,
|
|
156
155
|
});
|
|
157
156
|
|
|
158
|
-
// The
|
|
159
|
-
//
|
|
160
|
-
//
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
157
|
+
// The shape shared by the wire `nativeBalanceGte` check and the descriptor's
|
|
158
|
+
// symbolic-owner variant. `owner` is `string` for both (`AssertionOwnerSchema.Type`
|
|
159
|
+
// collapses to `string`).
|
|
160
|
+
interface NativeBalanceGteLike {
|
|
161
|
+
readonly kind: 'nativeBalanceGte';
|
|
162
|
+
readonly chainId: number;
|
|
163
|
+
readonly owner: string;
|
|
164
|
+
readonly minAmount: string;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// The equivalent `erc20BalanceGte` of a `nativeBalanceGte` spec: `token` is
|
|
168
|
+
// the chain's stand-in ERC-20 (PathUSD on Tempo), every other field carried
|
|
169
|
+
// through.
|
|
170
|
+
const asErc20BalanceGteCheck = (
|
|
171
|
+
check: NativeBalanceGteLike,
|
|
172
|
+
token: `0x${string}`,
|
|
169
173
|
) => ({
|
|
170
174
|
kind: 'erc20BalanceGte' as const,
|
|
171
|
-
chainId,
|
|
172
|
-
token
|
|
173
|
-
owner,
|
|
174
|
-
minAmount,
|
|
175
|
+
chainId: check.chainId,
|
|
176
|
+
token,
|
|
177
|
+
owner: check.owner,
|
|
178
|
+
minAmount: check.minAmount,
|
|
175
179
|
});
|
|
176
180
|
|
|
181
|
+
// Decode adapter over `nativeCheckPolicy` (`chainCapabilities.ts` holds the
|
|
182
|
+
// unit reasoning): a `nativeBalanceGte` check is kept on an evm-native chain,
|
|
183
|
+
// rewritten to `erc20BalanceGte` of the stand-in ERC-20 on an `as-erc20`
|
|
184
|
+
// chain, and rejected on a `native-mirrored-erc20` chain. Shared by
|
|
185
|
+
// `CheckSchema` and the descriptor's `AssertedPredicateSchema` so the
|
|
186
|
+
// readiness capture and the committed re-assertion derive the same program
|
|
187
|
+
// (authoring↔settlement hash parity).
|
|
188
|
+
export const decodeNativeBalanceGte = <C extends NativeBalanceGteLike>(
|
|
189
|
+
check: C,
|
|
190
|
+
ast: SchemaAST.AST,
|
|
191
|
+
): Effect.Effect<
|
|
192
|
+
C | ReturnType<typeof asErc20BalanceGteCheck>,
|
|
193
|
+
ParseResult.ParseIssue
|
|
194
|
+
> => {
|
|
195
|
+
const policy = nativeCheckPolicy(check.chainId);
|
|
196
|
+
switch (policy.kind) {
|
|
197
|
+
case 'keep':
|
|
198
|
+
return ParseResult.succeed(check);
|
|
199
|
+
case 'rewrite':
|
|
200
|
+
return ParseResult.succeed(asErc20BalanceGteCheck(check, policy.token));
|
|
201
|
+
case 'reject':
|
|
202
|
+
return ParseResult.fail(new ParseResult.Type(ast, check, policy.reason));
|
|
203
|
+
}
|
|
204
|
+
};
|
|
205
|
+
|
|
177
206
|
// The pre-transform union of the three named members.
|
|
178
207
|
const RawCheckSchema = Schema.Union(
|
|
179
208
|
Erc20BalanceGteSpecSchema,
|
|
@@ -181,28 +210,18 @@ const RawCheckSchema = Schema.Union(
|
|
|
181
210
|
CallCheckSpecSchema,
|
|
182
211
|
);
|
|
183
212
|
|
|
184
|
-
//
|
|
185
|
-
//
|
|
186
|
-
|
|
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(
|
|
213
|
+
// Only `nativeBalanceGte` is touched (via `decodeNativeBalanceGte`); every
|
|
214
|
+
// other member decodes unchanged. Encode is the identity.
|
|
215
|
+
export const CheckSchema = Schema.transformOrFail(
|
|
197
216
|
RawCheckSchema,
|
|
198
217
|
Schema.typeSchema(RawCheckSchema),
|
|
199
218
|
{
|
|
200
219
|
strict: true,
|
|
201
|
-
decode: (check) =>
|
|
202
|
-
check.kind === 'nativeBalanceGte'
|
|
203
|
-
?
|
|
204
|
-
: check,
|
|
205
|
-
encode: (check) => check,
|
|
220
|
+
decode: (check, _options, ast) =>
|
|
221
|
+
check.kind === 'nativeBalanceGte'
|
|
222
|
+
? decodeNativeBalanceGte(check, ast)
|
|
223
|
+
: ParseResult.succeed(check),
|
|
224
|
+
encode: (check) => ParseResult.succeed(check),
|
|
206
225
|
},
|
|
207
226
|
);
|
|
208
227
|
|
package/src/descriptor.ts
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
import { Schema } from 'effect';
|
|
1
|
+
import { ParseResult, Schema } from 'effect';
|
|
2
2
|
|
|
3
|
-
import { isAsErc20Chain } from './chainCapabilities.js';
|
|
4
3
|
import {
|
|
5
|
-
asErc20BalanceGteCheck,
|
|
6
4
|
AssertionOwnerSchema,
|
|
7
5
|
CallCheckSpecSchema,
|
|
6
|
+
decodeNativeBalanceGte,
|
|
8
7
|
Erc20BalanceDeltaGteSpecSchema,
|
|
9
8
|
HexStringSchema,
|
|
10
9
|
NativeBalanceDeltaGteSpecSchema,
|
|
@@ -71,24 +70,21 @@ const RawAssertedPredicateSchema = Schema.Union(
|
|
|
71
70
|
NativeBalanceDeltaGteSpecSchema,
|
|
72
71
|
);
|
|
73
72
|
|
|
74
|
-
// Same `nativeBalanceGte`
|
|
75
|
-
//
|
|
76
|
-
//
|
|
77
|
-
//
|
|
78
|
-
//
|
|
79
|
-
|
|
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(
|
|
73
|
+
// Same `nativeBalanceGte` handling as `CheckSchema`, applied to the
|
|
74
|
+
// disclosed/committed predicate so settlement derives the identical program.
|
|
75
|
+
// Idempotent on actions authored through `CheckSchema`; normalizes or refuses
|
|
76
|
+
// a hand-forged committed action that embeds a raw native check. The delta
|
|
77
|
+
// members are untouched — they derive from already-normalized outcomes.
|
|
78
|
+
export const AssertedPredicateSchema = Schema.transformOrFail(
|
|
83
79
|
RawAssertedPredicateSchema,
|
|
84
80
|
Schema.typeSchema(RawAssertedPredicateSchema),
|
|
85
81
|
{
|
|
86
82
|
strict: true,
|
|
87
|
-
decode: (pred) =>
|
|
88
|
-
pred.kind === 'nativeBalanceGte'
|
|
89
|
-
?
|
|
90
|
-
: pred,
|
|
91
|
-
encode: (pred) => pred,
|
|
83
|
+
decode: (pred, _options, ast) =>
|
|
84
|
+
pred.kind === 'nativeBalanceGte'
|
|
85
|
+
? decodeNativeBalanceGte(pred, ast)
|
|
86
|
+
: ParseResult.succeed(pred),
|
|
87
|
+
encode: (pred) => ParseResult.succeed(pred),
|
|
92
88
|
},
|
|
93
89
|
);
|
|
94
90
|
|
package/src/index.ts
CHANGED
|
@@ -180,13 +180,23 @@ export type {
|
|
|
180
180
|
GuardCompatibility,
|
|
181
181
|
} from './manifest.js';
|
|
182
182
|
|
|
183
|
-
export type {
|
|
183
|
+
export type {
|
|
184
|
+
ChainCapabilities,
|
|
185
|
+
NativeCheckPolicy,
|
|
186
|
+
NativeErc20,
|
|
187
|
+
} from './chainCapabilities.js';
|
|
184
188
|
export {
|
|
185
189
|
EVM_NATIVE_SENTINEL,
|
|
186
190
|
TEMPO_CHAIN_ID,
|
|
187
191
|
TEMPO_NATIVE_ERC20,
|
|
192
|
+
ARC_CHAIN_ID,
|
|
193
|
+
ARC_NATIVE_ERC20,
|
|
194
|
+
ARC_NATIVE_SCALE,
|
|
188
195
|
getChainCapabilities,
|
|
196
|
+
foldChainCapabilities,
|
|
197
|
+
nativeCheckPolicy,
|
|
189
198
|
isAsErc20Chain,
|
|
199
|
+
hasErc20CanonicalNative,
|
|
190
200
|
canonicalNativeAddress,
|
|
191
201
|
isNativeAddressForChain,
|
|
192
202
|
isMsgValueNative,
|
|
@@ -243,6 +253,7 @@ export {
|
|
|
243
253
|
erc20Balance,
|
|
244
254
|
nativeBalance,
|
|
245
255
|
erc20Allowance,
|
|
256
|
+
isErc20BalancePrecondition,
|
|
246
257
|
isErc20AllowancePrecondition,
|
|
247
258
|
isNativeBalancePrecondition,
|
|
248
259
|
} from './preconditions.js';
|
package/src/preconditions.ts
CHANGED
|
@@ -42,6 +42,10 @@ export const erc20Allowance = (
|
|
|
42
42
|
config: Omit<Erc20AllowancePrecondition, 'type'>,
|
|
43
43
|
): Erc20AllowancePrecondition => ({ type: 'Erc20Allowance', ...config });
|
|
44
44
|
|
|
45
|
+
export const isErc20BalancePrecondition = (
|
|
46
|
+
p: Precondition,
|
|
47
|
+
): p is Erc20BalancePrecondition => p.type === 'Erc20Balance';
|
|
48
|
+
|
|
45
49
|
export const isErc20AllowancePrecondition = (
|
|
46
50
|
p: Precondition,
|
|
47
51
|
): p is Erc20AllowancePrecondition => p.type === 'Erc20Allowance';
|