@artblocks/abx-sdk 0.1.0-alpha.2 → 0.1.0-alpha.21
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/CHANGELOG.md +2203 -0
- package/README.md +131 -0
- package/dist/abi/generated.d.ts +8502 -129
- package/dist/abi/generated.d.ts.map +1 -1
- package/dist/abi/generated.js +40 -22
- package/dist/abi/generated.js.map +1 -1
- package/dist/abi/index.d.ts +2645 -168
- package/dist/abi/index.d.ts.map +1 -1
- package/dist/abi/index.js +34 -7
- package/dist/abi/index.js.map +1 -1
- package/dist/anchors.d.ts +319 -0
- package/dist/anchors.d.ts.map +1 -0
- package/dist/anchors.js +703 -0
- package/dist/anchors.js.map +1 -0
- package/dist/chains.d.ts +15 -9
- package/dist/chains.d.ts.map +1 -1
- package/dist/chains.js +19 -15
- package/dist/chains.js.map +1 -1
- package/dist/chunks.d.ts +53 -23
- package/dist/chunks.d.ts.map +1 -1
- package/dist/chunks.js +68 -6
- package/dist/chunks.js.map +1 -1
- package/dist/clients.d.ts +23 -11
- package/dist/clients.d.ts.map +1 -1
- package/dist/clients.js +17 -18
- package/dist/clients.js.map +1 -1
- package/dist/create2.d.ts +85 -1
- package/dist/create2.d.ts.map +1 -1
- package/dist/create2.js +159 -2
- package/dist/create2.js.map +1 -1
- package/dist/creator-token.d.ts +132 -0
- package/dist/creator-token.d.ts.map +1 -0
- package/dist/creator-token.js +183 -0
- package/dist/creator-token.js.map +1 -0
- package/dist/deploy.d.ts +234 -14
- package/dist/deploy.d.ts.map +1 -1
- package/dist/deploy.js +289 -111
- package/dist/deploy.js.map +1 -1
- package/dist/deployments.d.ts +63 -0
- package/dist/deployments.d.ts.map +1 -1
- package/dist/deployments.js +202 -28
- package/dist/deployments.js.map +1 -1
- package/dist/deps.d.ts +122 -5
- package/dist/deps.d.ts.map +1 -1
- package/dist/deps.js +248 -6
- package/dist/deps.js.map +1 -1
- package/dist/env.d.ts.map +1 -1
- package/dist/env.js +8 -0
- package/dist/env.js.map +1 -1
- package/dist/errors.d.ts +94 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +126 -0
- package/dist/errors.js.map +1 -0
- package/dist/execute.d.ts +115 -0
- package/dist/execute.d.ts.map +1 -0
- package/dist/execute.js +142 -0
- package/dist/execute.js.map +1 -0
- package/dist/gateways.d.ts +79 -0
- package/dist/gateways.d.ts.map +1 -0
- package/dist/gateways.js +156 -0
- package/dist/gateways.js.map +1 -0
- package/dist/generator-document.d.ts +58 -0
- package/dist/generator-document.d.ts.map +1 -0
- package/dist/generator-document.js +124 -0
- package/dist/generator-document.js.map +1 -0
- package/dist/generator.d.ts +43 -1
- package/dist/generator.d.ts.map +1 -1
- package/dist/generator.js +29 -0
- package/dist/generator.js.map +1 -1
- package/dist/index.d.ts +18 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +18 -3
- package/dist/index.js.map +1 -1
- package/dist/inspect.d.ts +48 -0
- package/dist/inspect.d.ts.map +1 -0
- package/dist/inspect.js +295 -0
- package/dist/inspect.js.map +1 -0
- package/dist/migrate.d.ts +41 -0
- package/dist/migrate.d.ts.map +1 -0
- package/dist/migrate.js +142 -0
- package/dist/migrate.js.map +1 -0
- package/dist/mime.d.ts +11 -0
- package/dist/mime.d.ts.map +1 -0
- package/dist/mime.js +40 -0
- package/dist/mime.js.map +1 -0
- package/dist/node.d.ts +10 -0
- package/dist/node.d.ts.map +1 -0
- package/dist/node.js +24 -0
- package/dist/node.js.map +1 -0
- package/dist/onchain-uri.d.ts +110 -0
- package/dist/onchain-uri.d.ts.map +1 -0
- package/dist/onchain-uri.js +277 -0
- package/dist/onchain-uri.js.map +1 -0
- package/dist/ops.d.ts +507 -14
- package/dist/ops.d.ts.map +1 -1
- package/dist/ops.js +848 -32
- package/dist/ops.js.map +1 -1
- package/dist/policy.d.ts +58 -0
- package/dist/policy.d.ts.map +1 -0
- package/dist/policy.js +36 -0
- package/dist/policy.js.map +1 -0
- package/dist/probe.d.ts +29 -0
- package/dist/probe.d.ts.map +1 -1
- package/dist/probe.js +104 -9
- package/dist/probe.js.map +1 -1
- package/dist/reconstruct.d.ts +94 -2
- package/dist/reconstruct.d.ts.map +1 -1
- package/dist/reconstruct.js +432 -97
- package/dist/reconstruct.js.map +1 -1
- package/dist/resume.d.ts +96 -0
- package/dist/resume.d.ts.map +1 -0
- package/dist/resume.js +95 -0
- package/dist/resume.js.map +1 -0
- package/dist/script-chunks.d.ts +24 -0
- package/dist/script-chunks.d.ts.map +1 -0
- package/dist/script-chunks.js +63 -0
- package/dist/script-chunks.js.map +1 -0
- package/dist/service.d.ts +206 -9
- package/dist/service.d.ts.map +1 -1
- package/dist/service.js +228 -18
- package/dist/service.js.map +1 -1
- package/dist/spine.d.ts +47 -4
- package/dist/spine.d.ts.map +1 -1
- package/dist/spine.js +0 -0
- package/dist/spine.js.map +1 -1
- package/dist/staging.d.ts +124 -0
- package/dist/staging.d.ts.map +1 -0
- package/dist/staging.js +134 -0
- package/dist/staging.js.map +1 -0
- package/dist/tokendata.d.ts +62 -2
- package/dist/tokendata.d.ts.map +1 -1
- package/dist/tokendata.js +79 -17
- package/dist/tokendata.js.map +1 -1
- package/dist/tokens.d.ts +120 -0
- package/dist/tokens.d.ts.map +1 -0
- package/dist/tokens.js +317 -0
- package/dist/tokens.js.map +1 -0
- package/dist/types.d.ts +108 -4
- package/dist/types.d.ts.map +1 -1
- package/dist/util.d.ts +74 -0
- package/dist/util.d.ts.map +1 -0
- package/dist/util.js +106 -0
- package/dist/util.js.map +1 -0
- package/package.json +11 -2
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Gas limits for a sequenced transaction — decided HERE, once, rather than re-read at signing time.
|
|
3
|
+
*
|
|
4
|
+
* The failure this exists to prevent, in full, because it is not obvious and it cost a reporter every
|
|
5
|
+
* `deploy-code` attempt of a session: a project deploys in two transactions, and the second targets
|
|
6
|
+
* the contract the first just created. If the node answering `eth_estimateGas` has not yet seen the
|
|
7
|
+
* deploy block, the target looks like an account with no code — and an estimate for a call to a
|
|
8
|
+
* codeless account is just the calldata cost, around 200k. That number is then sent as the gas limit.
|
|
9
|
+
* Once the contract does exist the same call needs ~941k, because storing a program on-chain is
|
|
10
|
+
* dominated by CREATE code deposit at ~200 gas per byte. The CREATE receives 63/64 of a budget that
|
|
11
|
+
* cannot cover the deposit, returns 0, and Solady's `SSTORE2.write` reverts `DeploymentFailed()` — a
|
|
12
|
+
* revert that reads like a contract bug and is really an out-of-gas.
|
|
13
|
+
*
|
|
14
|
+
* (Measured, from the real transactions: `gasLimit 201,616 / gasUsed 198,870` and
|
|
15
|
+
* `gasLimit 169,301 / gasUsed 166,810` — 98.6% and 98.5% of their limits. Replaying those payloads
|
|
16
|
+
* against a codeless address reproduces both limits to the gas; against the real contract the same
|
|
17
|
+
* calls estimate at 941,331.)
|
|
18
|
+
*
|
|
19
|
+
* The deploy loop already pinned the *nonce* against exactly this read-after-write lag on a
|
|
20
|
+
* distributed RPC. The rule generalizes, and it is the reason this module exists: **anything read at
|
|
21
|
+
* send time on a distributed RPC needs pinning, not just the nonce.**
|
|
22
|
+
*/
|
|
23
|
+
import type { Account, Address, Hex, PublicClient, TransactionReceipt, WalletClient } from 'viem';
|
|
24
|
+
import type { PreparedTx } from './ops.js';
|
|
25
|
+
/**
|
|
26
|
+
* Wait until `address` has code from THIS client's point of view. A deploy receipt proves the
|
|
27
|
+
* contract exists on chain; it does not prove the node answering the next request has caught up.
|
|
28
|
+
* Bounded — on timeout we proceed and let the gas floor carry it, since a slow RPC is not a reason
|
|
29
|
+
* to refuse to continue a deploy that already spent money.
|
|
30
|
+
*/
|
|
31
|
+
export declare function waitForCodeAt(client: PublicClient, address: Address, timeoutMs?: number): Promise<boolean>;
|
|
32
|
+
/**
|
|
33
|
+
* Decide a transaction's gas limit once, here.
|
|
34
|
+
*
|
|
35
|
+
* The important design point, because the obvious approach is wrong: when an estimate looks too low
|
|
36
|
+
* we do **not** substitute a computed number. A caller can only compute the *provable* part of a
|
|
37
|
+
* payload's cost (see `PreparedTx.gasFloor` — code deposit is 200 gas/byte and nothing else is
|
|
38
|
+
* physics); the same setup multicall also carries schema writes, dependency legs, URI legs and
|
|
39
|
+
* mints, whose cost we cannot derive without simulating them. A "probably enough" constant is a
|
|
40
|
+
* number tuned to whatever case was in front of its author: it papers over the symptom, then
|
|
41
|
+
* under-funds the next payload that carries a few more legs, producing the identical
|
|
42
|
+
* `DeploymentFailed()` with a fresh mystery attached.
|
|
43
|
+
*
|
|
44
|
+
* So the floor is used as a **detector**. An estimate below a provable minimum is not "low", it is
|
|
45
|
+
* *impossible* — proof that the node answering us is looking at the wrong state (typically it has
|
|
46
|
+
* not seen the deploy block yet, so the target reads as an account with no code and the estimate
|
|
47
|
+
* comes back as the calldata cost alone). The right response to a broken measurement is to take it
|
|
48
|
+
* again, and if it stays broken, to refuse — sending a transaction we can prove is under-funded
|
|
49
|
+
* would burn the gas AND orphan the contract.
|
|
50
|
+
*
|
|
51
|
+
* When the estimate IS plausible it is trusted, plus headroom for state drift between estimate and
|
|
52
|
+
* inclusion.
|
|
53
|
+
*/
|
|
54
|
+
export declare function pinGas(client: PublicClient, tx: {
|
|
55
|
+
from: Address;
|
|
56
|
+
to: Address | null;
|
|
57
|
+
data: Hex;
|
|
58
|
+
value?: Hex;
|
|
59
|
+
gasFloor?: Hex;
|
|
60
|
+
}, opts?: {
|
|
61
|
+
attempts?: number;
|
|
62
|
+
delayMs?: number;
|
|
63
|
+
}): Promise<bigint>;
|
|
64
|
+
/** Sign + broadcast one {@link PreparedTx} and return its mined receipt (for event parsing). The
|
|
65
|
+
* SDK's one send-injection point: a hot key ({@link makeHotSender}), a browser wallet (a
|
|
66
|
+
* `WalletSession`), or anything else a caller wires up — the SDK never picks or performs the
|
|
67
|
+
* signing itself. */
|
|
68
|
+
export type SendTx = (tx: PreparedTx) => Promise<TransactionReceipt>;
|
|
69
|
+
/** Progress from a {@link SendTx} built by {@link makeHotSender}, so a caller can narrate without
|
|
70
|
+
* the SDK printing anything itself (the SDK never prints — see clients.ts / chunks.ts for the same
|
|
71
|
+
* `onEvent` pattern). */
|
|
72
|
+
export type SendEvent = {
|
|
73
|
+
kind: 'sending';
|
|
74
|
+
tx: PreparedTx;
|
|
75
|
+
} | {
|
|
76
|
+
kind: 'mined';
|
|
77
|
+
receipt: TransactionReceipt;
|
|
78
|
+
tx: PreparedTx;
|
|
79
|
+
};
|
|
80
|
+
/**
|
|
81
|
+
* Build a {@link SendTx} that signs with an env-style hot key and broadcasts — the generalization of
|
|
82
|
+
* what used to be hand-rolled per call site (a deploy-then-setup loop in the CLI, the hot lane in
|
|
83
|
+
* `signer.ts`, the chunk-staging sender in `ownerops.ts`): one function that gets the read-after-
|
|
84
|
+
* write-lag handling right ONCE.
|
|
85
|
+
*
|
|
86
|
+
* Two lags, pinned once at construction / tracked locally per send, rather than re-read at send time:
|
|
87
|
+
* - the NONCE — fetched once here, then incremented locally per tx. Right after a tx is mined a
|
|
88
|
+
* distributed RPC (e.g. Alchemy) can briefly serve a stale `pending` transaction count
|
|
89
|
+
* (read-after-write lag), so re-fetching it for the NEXT send in the same sequence risks reusing
|
|
90
|
+
* the just-spent nonce — the tx it already has in the mempool rejects the duplicate as
|
|
91
|
+
* "replacement transaction underpriced", and that send is silently lost.
|
|
92
|
+
* - GAS, one field over: a tx after the first in a sequence may target a contract an EARLIER tx in
|
|
93
|
+
* this same sequence just created. If the node answering `eth_estimateGas` hasn't seen that
|
|
94
|
+
* block yet, the target looks codeless and the estimate comes back as the calldata cost alone —
|
|
95
|
+
* see `pinGas` above for the full story (and the measured numbers). So every send after the
|
|
96
|
+
* first waits for its target's code to be visible before estimating.
|
|
97
|
+
*
|
|
98
|
+
* Every send: wait for code at the target when it's not the first send in this sender's sequence
|
|
99
|
+
* (mirrors the exact rule the deploy-code hot lane hand-rolled: every tx after the first, since the
|
|
100
|
+
* first is what creates what the rest target), pin the gas (a provable floor detector, never a
|
|
101
|
+
* substitute limit), broadcast, wait for the receipt, and throw a typed {@link TxRevertedError} —
|
|
102
|
+
* carrying the tx hash — on a `reverted` status rather than reporting a burned transaction as
|
|
103
|
+
* "confirmed".
|
|
104
|
+
*/
|
|
105
|
+
export declare function makeHotSender(args: {
|
|
106
|
+
wallet: WalletClient;
|
|
107
|
+
account: Account;
|
|
108
|
+
publicClient: PublicClient;
|
|
109
|
+
onEvent?: (e: SendEvent) => void;
|
|
110
|
+
}): SendTx;
|
|
111
|
+
/** Send a list of prepared txs, in order, via `send`; returns their receipts in the same order.
|
|
112
|
+
* A small convenience for the common "just run this sequence" case — narration (if any) rides
|
|
113
|
+
* `send`'s own `onEvent`, not a callback here. */
|
|
114
|
+
export declare function runPrepared(txs: PreparedTx[], send: SendTx): Promise<TransactionReceipt[]>;
|
|
115
|
+
//# sourceMappingURL=execute.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"execute.d.ts","sourceRoot":"","sources":["../src/execute.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,OAAO,KAAK,EAAC,OAAO,EAAE,OAAO,EAAE,GAAG,EAAE,YAAY,EAAE,kBAAkB,EAAE,YAAY,EAAC,MAAM,MAAM,CAAC;AAChG,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,UAAU,CAAC;AAGzC;;;;;GAKG;AACH,wBAAsB,aAAa,CAAC,MAAM,EAAE,YAAY,EAAE,OAAO,EAAE,OAAO,EAAE,SAAS,SAAS,GAAG,OAAO,CAAC,OAAO,CAAC,CAQhH;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAsB,MAAM,CAC1B,MAAM,EAAE,YAAY,EACpB,EAAE,EAAE;IAAC,IAAI,EAAE,OAAO,CAAC;IAAC,EAAE,EAAE,OAAO,GAAG,IAAI,CAAC;IAAC,IAAI,EAAE,GAAG,CAAC;IAAC,KAAK,CAAC,EAAE,GAAG,CAAC;IAAC,QAAQ,CAAC,EAAE,GAAG,CAAA;CAAC,EAC/E,IAAI,GAAE;IAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAAC,OAAO,CAAC,EAAE,MAAM,CAAA;CAAM,GAC/C,OAAO,CAAC,MAAM,CAAC,CA8BjB;AAED;;;sBAGsB;AACtB,MAAM,MAAM,MAAM,GAAG,CAAC,EAAE,EAAE,UAAU,KAAK,OAAO,CAAC,kBAAkB,CAAC,CAAC;AAErE;;0BAE0B;AAC1B,MAAM,MAAM,SAAS,GACjB;IAAC,IAAI,EAAE,SAAS,CAAC;IAAC,EAAE,EAAE,UAAU,CAAA;CAAC,GACjC;IAAC,IAAI,EAAE,OAAO,CAAC;IAAC,OAAO,EAAE,kBAAkB,CAAC;IAAC,EAAE,EAAE,UAAU,CAAA;CAAC,CAAC;AAEjE;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE;IAClC,MAAM,EAAE,YAAY,CAAC;IACrB,OAAO,EAAE,OAAO,CAAC;IACjB,YAAY,EAAE,YAAY,CAAC;IAC3B,OAAO,CAAC,EAAE,CAAC,CAAC,EAAE,SAAS,KAAK,IAAI,CAAC;CAClC,GAAG,MAAM,CAkCT;AAED;;mDAEmD;AACnD,wBAAsB,WAAW,CAAC,GAAG,EAAE,UAAU,EAAE,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,kBAAkB,EAAE,CAAC,CAIhG"}
|
package/dist/execute.js
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { GasEstimateBelowFloorError, TxRevertedError } from './errors.js';
|
|
2
|
+
/**
|
|
3
|
+
* Wait until `address` has code from THIS client's point of view. A deploy receipt proves the
|
|
4
|
+
* contract exists on chain; it does not prove the node answering the next request has caught up.
|
|
5
|
+
* Bounded — on timeout we proceed and let the gas floor carry it, since a slow RPC is not a reason
|
|
6
|
+
* to refuse to continue a deploy that already spent money.
|
|
7
|
+
*/
|
|
8
|
+
export async function waitForCodeAt(client, address, timeoutMs = 15_000) {
|
|
9
|
+
const deadline = Date.now() + timeoutMs;
|
|
10
|
+
for (;;) {
|
|
11
|
+
const code = await client.getCode({ address }).catch(() => undefined);
|
|
12
|
+
if (code && code !== '0x')
|
|
13
|
+
return true;
|
|
14
|
+
if (Date.now() >= deadline)
|
|
15
|
+
return false;
|
|
16
|
+
await new Promise((r) => setTimeout(r, 750));
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Decide a transaction's gas limit once, here.
|
|
21
|
+
*
|
|
22
|
+
* The important design point, because the obvious approach is wrong: when an estimate looks too low
|
|
23
|
+
* we do **not** substitute a computed number. A caller can only compute the *provable* part of a
|
|
24
|
+
* payload's cost (see `PreparedTx.gasFloor` — code deposit is 200 gas/byte and nothing else is
|
|
25
|
+
* physics); the same setup multicall also carries schema writes, dependency legs, URI legs and
|
|
26
|
+
* mints, whose cost we cannot derive without simulating them. A "probably enough" constant is a
|
|
27
|
+
* number tuned to whatever case was in front of its author: it papers over the symptom, then
|
|
28
|
+
* under-funds the next payload that carries a few more legs, producing the identical
|
|
29
|
+
* `DeploymentFailed()` with a fresh mystery attached.
|
|
30
|
+
*
|
|
31
|
+
* So the floor is used as a **detector**. An estimate below a provable minimum is not "low", it is
|
|
32
|
+
* *impossible* — proof that the node answering us is looking at the wrong state (typically it has
|
|
33
|
+
* not seen the deploy block yet, so the target reads as an account with no code and the estimate
|
|
34
|
+
* comes back as the calldata cost alone). The right response to a broken measurement is to take it
|
|
35
|
+
* again, and if it stays broken, to refuse — sending a transaction we can prove is under-funded
|
|
36
|
+
* would burn the gas AND orphan the contract.
|
|
37
|
+
*
|
|
38
|
+
* When the estimate IS plausible it is trusted, plus headroom for state drift between estimate and
|
|
39
|
+
* inclusion.
|
|
40
|
+
*/
|
|
41
|
+
export async function pinGas(client, tx, opts = {}) {
|
|
42
|
+
const floor = tx.gasFloor ? BigInt(tx.gasFloor) : 0n;
|
|
43
|
+
const attempts = opts.attempts ?? 3;
|
|
44
|
+
let lastEstimate = null;
|
|
45
|
+
let lastError;
|
|
46
|
+
for (let i = 0; i < attempts; i++) {
|
|
47
|
+
try {
|
|
48
|
+
const estimate = await client.estimateGas({
|
|
49
|
+
account: tx.from,
|
|
50
|
+
to: tx.to ?? undefined,
|
|
51
|
+
data: tx.data,
|
|
52
|
+
...(tx.value && tx.value !== '0x0' ? { value: BigInt(tx.value) } : {}),
|
|
53
|
+
});
|
|
54
|
+
// Plausible (or nothing provable to check it against) → trust it.
|
|
55
|
+
if (estimate >= floor)
|
|
56
|
+
return (estimate * 125n) / 100n;
|
|
57
|
+
lastEstimate = estimate;
|
|
58
|
+
}
|
|
59
|
+
catch (err) {
|
|
60
|
+
lastError = err;
|
|
61
|
+
// A revert during estimation is a real answer about the transaction, not a lagging node —
|
|
62
|
+
// surface it immediately, since its message ("caller is not the owner") is the useful part.
|
|
63
|
+
if (floor === 0n)
|
|
64
|
+
throw err;
|
|
65
|
+
}
|
|
66
|
+
if (i < attempts - 1)
|
|
67
|
+
await new Promise((r) => setTimeout(r, opts.delayMs ?? 1_500));
|
|
68
|
+
}
|
|
69
|
+
if (lastEstimate !== null) {
|
|
70
|
+
throw new GasEstimateBelowFloorError(lastEstimate, floor);
|
|
71
|
+
}
|
|
72
|
+
throw lastError instanceof Error ? lastError : new Error('gas estimation failed');
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Build a {@link SendTx} that signs with an env-style hot key and broadcasts — the generalization of
|
|
76
|
+
* what used to be hand-rolled per call site (a deploy-then-setup loop in the CLI, the hot lane in
|
|
77
|
+
* `signer.ts`, the chunk-staging sender in `ownerops.ts`): one function that gets the read-after-
|
|
78
|
+
* write-lag handling right ONCE.
|
|
79
|
+
*
|
|
80
|
+
* Two lags, pinned once at construction / tracked locally per send, rather than re-read at send time:
|
|
81
|
+
* - the NONCE — fetched once here, then incremented locally per tx. Right after a tx is mined a
|
|
82
|
+
* distributed RPC (e.g. Alchemy) can briefly serve a stale `pending` transaction count
|
|
83
|
+
* (read-after-write lag), so re-fetching it for the NEXT send in the same sequence risks reusing
|
|
84
|
+
* the just-spent nonce — the tx it already has in the mempool rejects the duplicate as
|
|
85
|
+
* "replacement transaction underpriced", and that send is silently lost.
|
|
86
|
+
* - GAS, one field over: a tx after the first in a sequence may target a contract an EARLIER tx in
|
|
87
|
+
* this same sequence just created. If the node answering `eth_estimateGas` hasn't seen that
|
|
88
|
+
* block yet, the target looks codeless and the estimate comes back as the calldata cost alone —
|
|
89
|
+
* see `pinGas` above for the full story (and the measured numbers). So every send after the
|
|
90
|
+
* first waits for its target's code to be visible before estimating.
|
|
91
|
+
*
|
|
92
|
+
* Every send: wait for code at the target when it's not the first send in this sender's sequence
|
|
93
|
+
* (mirrors the exact rule the deploy-code hot lane hand-rolled: every tx after the first, since the
|
|
94
|
+
* first is what creates what the rest target), pin the gas (a provable floor detector, never a
|
|
95
|
+
* substitute limit), broadcast, wait for the receipt, and throw a typed {@link TxRevertedError} —
|
|
96
|
+
* carrying the tx hash — on a `reverted` status rather than reporting a burned transaction as
|
|
97
|
+
* "confirmed".
|
|
98
|
+
*/
|
|
99
|
+
export function makeHotSender(args) {
|
|
100
|
+
const { wallet, account, publicClient } = args;
|
|
101
|
+
const notify = args.onEvent ?? (() => { });
|
|
102
|
+
let nonce;
|
|
103
|
+
let sent = 0;
|
|
104
|
+
return async (tx) => {
|
|
105
|
+
if (nonce === undefined) {
|
|
106
|
+
// Fetch once, then increment locally per send — see the read-after-write-lag reasoning above.
|
|
107
|
+
nonce = await publicClient.getTransactionCount({ address: account.address, blockTag: 'pending' });
|
|
108
|
+
}
|
|
109
|
+
// The SAME lag, one field over: every send after the first in this sender's sequence may target
|
|
110
|
+
// what an earlier send just created.
|
|
111
|
+
if (sent > 0 && tx.to)
|
|
112
|
+
await waitForCodeAt(publicClient, tx.to);
|
|
113
|
+
const gas = await pinGas(publicClient, { from: account.address, to: tx.to, data: tx.data, value: tx.value, gasFloor: tx.gasFloor });
|
|
114
|
+
notify({ kind: 'sending', tx });
|
|
115
|
+
const hash = await wallet.sendTransaction({
|
|
116
|
+
to: tx.to ?? undefined,
|
|
117
|
+
data: tx.data,
|
|
118
|
+
value: tx.value && tx.value !== '0x0' ? BigInt(tx.value) : undefined,
|
|
119
|
+
account,
|
|
120
|
+
chain: wallet.chain,
|
|
121
|
+
nonce,
|
|
122
|
+
gas,
|
|
123
|
+
});
|
|
124
|
+
nonce += 1;
|
|
125
|
+
sent += 1;
|
|
126
|
+
const receipt = await publicClient.waitForTransactionReceipt({ hash });
|
|
127
|
+
if (receipt.status !== 'success')
|
|
128
|
+
throw new TxRevertedError(tx.op, hash);
|
|
129
|
+
notify({ kind: 'mined', receipt, tx });
|
|
130
|
+
return receipt;
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
/** Send a list of prepared txs, in order, via `send`; returns their receipts in the same order.
|
|
134
|
+
* A small convenience for the common "just run this sequence" case — narration (if any) rides
|
|
135
|
+
* `send`'s own `onEvent`, not a callback here. */
|
|
136
|
+
export async function runPrepared(txs, send) {
|
|
137
|
+
const receipts = [];
|
|
138
|
+
for (const tx of txs)
|
|
139
|
+
receipts.push(await send(tx));
|
|
140
|
+
return receipts;
|
|
141
|
+
}
|
|
142
|
+
//# sourceMappingURL=execute.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"execute.js","sourceRoot":"","sources":["../src/execute.ts"],"names":[],"mappings":"AAwBA,OAAO,EAAC,0BAA0B,EAAE,eAAe,EAAC,MAAM,aAAa,CAAC;AAExE;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,aAAa,CAAC,MAAoB,EAAE,OAAgB,EAAE,SAAS,GAAG,MAAM;IAC5F,MAAM,QAAQ,GAAG,IAAI,CAAC,GAAG,EAAE,GAAG,SAAS,CAAC;IACxC,SAAS,CAAC;QACR,MAAM,IAAI,GAAG,MAAM,MAAM,CAAC,OAAO,CAAC,EAAC,OAAO,EAAC,CAAC,CAAC,KA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AC;AACJ,CAAC;AAED;;mDAEmD;AACnD,MAAM,CAAC,KAAK,UAAU,WAAW,CAAC,GAAiB,EAAE,IAAY;IAC/D,MAAM,QAAQ,GAAyB,EAAE,CAAC;IAC1C,KAAK,MAAM,EAAE,IAAI,GAAG;QAAE,QAAQ,CAAC,IAAI,CAAC,MAAM,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC;IACpD,OAAO,QAAQ,CAAC;AAClB,CAAC"}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import type { ProjectState } from './types.js';
|
|
2
|
+
/** The locator's network, which decides which gateways can be asked about it. `http` locators
|
|
3
|
+
* carry their own base (they are already an absolute URL) and resolve to `''` here — a caller
|
|
4
|
+
* returns them verbatim before consulting a gateway. */
|
|
5
|
+
export type LocatorNetwork = 'arweave' | 'ipfs' | 'http';
|
|
6
|
+
/** Resolved gateway overrides for the two content-addressed networks — the shape both
|
|
7
|
+
* {@link resolveGatewayBase} and {@link gatewayConfigFromEnv} speak. */
|
|
8
|
+
export interface GatewayOverrides {
|
|
9
|
+
ipfs?: string;
|
|
10
|
+
arweave?: string;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* The gateway BASE for a locator network, given already-resolved `overrides` — PURE, no env
|
|
14
|
+
* read. Defaults to the generic public gateways (`ipfs.io` / `arweave.net`) when `overrides`
|
|
15
|
+
* doesn't name one; any other `network` (e.g. `http`, which carries its own base) resolves to
|
|
16
|
+
* `''`. Callers that want override → env → default precedence compose this with
|
|
17
|
+
* {@link gatewayConfigFromEnv} themselves (see `@artblocks/abx-storage`'s `resolveGatewayBase`
|
|
18
|
+
* for the reference compose) — kept separate so a host with its own config source never has this
|
|
19
|
+
* function reach into `process.env` on its behalf.
|
|
20
|
+
*/
|
|
21
|
+
export declare function resolveGatewayBase(network: 'ipfs' | 'arweave' | string, overrides?: GatewayOverrides): string;
|
|
22
|
+
/**
|
|
23
|
+
* The ONLY env-reading piece of this module: `ABX_IPFS_GATEWAY` / `ABX_ARWEAVE_GATEWAY`, read via
|
|
24
|
+
* {@link readEnv} (safe where `process` doesn't exist). Exported separately from
|
|
25
|
+
* {@link resolveGatewayBase} so a host with its own gateway configuration (a database row, a
|
|
26
|
+
* remote config service) never has to touch `process.env` through this package at all — it just
|
|
27
|
+
* never calls this function and passes its own overrides straight to the pure resolver instead.
|
|
28
|
+
*/
|
|
29
|
+
export declare function gatewayConfigFromEnv(): GatewayOverrides;
|
|
30
|
+
/** Build the URL that asks `gateway` for `id`. A locator that is already an absolute URL is asked
|
|
31
|
+
* verbatim — rewriting someone's URL would answer a question they didn't ask. */
|
|
32
|
+
export declare function gatewayUrlFor(network: LocatorNetwork, id: string, gateway: string): string;
|
|
33
|
+
/** A scheme whose locator is content-addressed, and therefore its own integrity anchor. */
|
|
34
|
+
export type ContentAddressedNetwork = 'ipfs' | 'arweave';
|
|
35
|
+
/**
|
|
36
|
+
* Normalize a gateway HOST (the `ABX_IPFS_GATEWAY` / `ABX_ARWEAVE_GATEWAY` shape, and what
|
|
37
|
+
* {@link resolveGatewayBase} returns) into the on-chain PREFIX shape — trailing path included, so
|
|
38
|
+
* projection is a raw concat. A value that already ends in the scheme's path segment is left
|
|
39
|
+
* alone, so an operator may configure either form.
|
|
40
|
+
*/
|
|
41
|
+
export declare function gatewayPrefixFrom(network: ContentAddressedNetwork, base: string): string;
|
|
42
|
+
/**
|
|
43
|
+
* The gateway prefix this project prefers for a scheme: the collection's reserved
|
|
44
|
+
* `abx_gateway_*` field, else the host's env/override default, else the public floor.
|
|
45
|
+
*
|
|
46
|
+
* The env read is a FLOOR, never an override. When a project has stated a preference on chain,
|
|
47
|
+
* every conforming resolver must serve that one — otherwise the same token renders differently
|
|
48
|
+
* depending on who is serving it, which is the property `abx_provenance` exists to make checkable.
|
|
49
|
+
* When a project has stated nothing, a host with its own gateway is strictly better than
|
|
50
|
+
* `ipfs.io`, and a managed provider serving a token it does not own has no other way to say so.
|
|
51
|
+
*/
|
|
52
|
+
export declare function projectGatewayPrefix(state: Pick<ProjectState, 'collectionFields'>, network: ContentAddressedNetwork, overrides?: GatewayOverrides): string;
|
|
53
|
+
/**
|
|
54
|
+
* Project a content-addressed field value into its https URL, byte-identically to the deployed
|
|
55
|
+
* renderer. `null` when the value locates nothing (`ipfs://` with no CID) — the caller omits an
|
|
56
|
+
* optional field or falls back on a required one, rather than emitting a bare prefix that 404s.
|
|
57
|
+
*
|
|
58
|
+
* Order is load-bearing and mirrors the Solidity exactly: strip the scheme, THEN substitute
|
|
59
|
+
* `{id}`, THEN check for an absolute URL. That last check is what stops a value which already
|
|
60
|
+
* names its own host — precisely what a `backend.locator()` return looks like — from coming back
|
|
61
|
+
* as `https://arweave.net/https://arweave.net/<txid>`.
|
|
62
|
+
*/
|
|
63
|
+
export declare function projectGatewayUrl(network: ContentAddressedNetwork, value: string, prefix: string, tokenId?: string): string | null;
|
|
64
|
+
/**
|
|
65
|
+
* The inverse of {@link gatewayUrlFor} / {@link projectGatewayUrl}: recover the bare content id
|
|
66
|
+
* (CID or txid, plus any path suffix) from whatever form a locator arrived in.
|
|
67
|
+
*
|
|
68
|
+
* This exists because a storage backend's `locator()` returns a **gateway HTTPS URL** — the form an
|
|
69
|
+
* operator can click — while an `ipfs` / `arweave` metadata field must hold IDENTITY, with the
|
|
70
|
+
* gateway supplied at read time. Baking the operator's gateway host into the field is exactly the
|
|
71
|
+
* weld this projection was built to remove: it makes a CID unmigratable and makes the chain report
|
|
72
|
+
* `source: url` for bytes that live on IPFS.
|
|
73
|
+
*
|
|
74
|
+
* `null` when no id can be recovered — notably a subdomain-style gateway
|
|
75
|
+
* (`https://<cid>.ipfs.dweb.link`), which ABX's own backends never produce. A caller that gets
|
|
76
|
+
* `null` should keep the URL as a plain `url` field and SAY so, rather than guess.
|
|
77
|
+
*/
|
|
78
|
+
export declare function contentIdFromLocator(network: ContentAddressedNetwork, locator: string): string | null;
|
|
79
|
+
//# sourceMappingURL=gateways.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
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B,CAAC,OAAO,EAAE,uBAAuB,EAAE,OAAO,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAgBrG"}
|
package/dist/gateways.js
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Gateway resolution for content-addressed locators (`ipfs://…`, `ar://…`) — hoisted from
|
|
3
|
+
* `@artblocks/abx-storage` (the abx-services integrator ask), split into a PURE resolver and a
|
|
4
|
+
* separate env-reading piece so a host with its own gateway config never has to touch
|
|
5
|
+
* `process.env` through this module at all, and so this stays reachable from a browser bundle
|
|
6
|
+
* (see `test/browser-bundle.test.ts` — no `process.env` read outside {@link gatewayConfigFromEnv},
|
|
7
|
+
* which itself goes through {@link readEnv} and degrades to "no override" rather than throwing
|
|
8
|
+
* where `process` doesn't exist).
|
|
9
|
+
*/
|
|
10
|
+
import { readEnv } from './util.js';
|
|
11
|
+
import { GATEWAY_FIELD, GATEWAY_FLOOR, METADATA_REPRESENTATION as R } from './spine.js';
|
|
12
|
+
import { inlineText } from './token.js';
|
|
13
|
+
/**
|
|
14
|
+
* The gateway BASE for a locator network, given already-resolved `overrides` — PURE, no env
|
|
15
|
+
* read. Defaults to the generic public gateways (`ipfs.io` / `arweave.net`) when `overrides`
|
|
16
|
+
* doesn't name one; any other `network` (e.g. `http`, which carries its own base) resolves to
|
|
17
|
+
* `''`. Callers that want override → env → default precedence compose this with
|
|
18
|
+
* {@link gatewayConfigFromEnv} themselves (see `@artblocks/abx-storage`'s `resolveGatewayBase`
|
|
19
|
+
* for the reference compose) — kept separate so a host with its own config source never has this
|
|
20
|
+
* function reach into `process.env` on its behalf.
|
|
21
|
+
*/
|
|
22
|
+
export function resolveGatewayBase(network, overrides) {
|
|
23
|
+
if (network === 'ipfs')
|
|
24
|
+
return overrides?.ipfs || 'https://ipfs.io';
|
|
25
|
+
if (network === 'arweave')
|
|
26
|
+
return overrides?.arweave || 'https://arweave.net';
|
|
27
|
+
return '';
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* The ONLY env-reading piece of this module: `ABX_IPFS_GATEWAY` / `ABX_ARWEAVE_GATEWAY`, read via
|
|
31
|
+
* {@link readEnv} (safe where `process` doesn't exist). Exported separately from
|
|
32
|
+
* {@link resolveGatewayBase} so a host with its own gateway configuration (a database row, a
|
|
33
|
+
* remote config service) never has to touch `process.env` through this package at all — it just
|
|
34
|
+
* never calls this function and passes its own overrides straight to the pure resolver instead.
|
|
35
|
+
*/
|
|
36
|
+
export function gatewayConfigFromEnv() {
|
|
37
|
+
const ipfs = readEnv('ABX_IPFS_GATEWAY');
|
|
38
|
+
const arweave = readEnv('ABX_ARWEAVE_GATEWAY');
|
|
39
|
+
const out = {};
|
|
40
|
+
if (ipfs)
|
|
41
|
+
out.ipfs = ipfs;
|
|
42
|
+
if (arweave)
|
|
43
|
+
out.arweave = arweave;
|
|
44
|
+
return out;
|
|
45
|
+
}
|
|
46
|
+
/** Build the URL that asks `gateway` for `id`. A locator that is already an absolute URL is asked
|
|
47
|
+
* verbatim — rewriting someone's URL would answer a question they didn't ask. */
|
|
48
|
+
export function gatewayUrlFor(network, id, gateway) {
|
|
49
|
+
if (/^https?:\/\//i.test(id))
|
|
50
|
+
return id;
|
|
51
|
+
const base = gateway.replace(/\/+$/, '');
|
|
52
|
+
// A path suffix (a directory manifest entry, e.g. `<txid>/index.html`) rides along untouched.
|
|
53
|
+
return network === 'ipfs' ? `${base}/ipfs/${id}` : `${base}/${id}`;
|
|
54
|
+
}
|
|
55
|
+
const SCHEME = { ipfs: 'ipfs://', arweave: 'ar://' };
|
|
56
|
+
/**
|
|
57
|
+
* Normalize a gateway HOST (the `ABX_IPFS_GATEWAY` / `ABX_ARWEAVE_GATEWAY` shape, and what
|
|
58
|
+
* {@link resolveGatewayBase} returns) into the on-chain PREFIX shape — trailing path included, so
|
|
59
|
+
* projection is a raw concat. A value that already ends in the scheme's path segment is left
|
|
60
|
+
* alone, so an operator may configure either form.
|
|
61
|
+
*/
|
|
62
|
+
export function gatewayPrefixFrom(network, base) {
|
|
63
|
+
const trimmed = base.replace(/\/+$/, '');
|
|
64
|
+
if (!trimmed)
|
|
65
|
+
return GATEWAY_FLOOR[network];
|
|
66
|
+
if (network === 'arweave')
|
|
67
|
+
return `${trimmed}/`;
|
|
68
|
+
return trimmed.endsWith('/ipfs') ? `${trimmed}/` : `${trimmed}/ipfs/`;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* The gateway prefix this project prefers for a scheme: the collection's reserved
|
|
72
|
+
* `abx_gateway_*` field, else the host's env/override default, else the public floor.
|
|
73
|
+
*
|
|
74
|
+
* The env read is a FLOOR, never an override. When a project has stated a preference on chain,
|
|
75
|
+
* every conforming resolver must serve that one — otherwise the same token renders differently
|
|
76
|
+
* depending on who is serving it, which is the property `abx_provenance` exists to make checkable.
|
|
77
|
+
* When a project has stated nothing, a host with its own gateway is strictly better than
|
|
78
|
+
* `ipfs.io`, and a managed provider serving a token it does not own has no other way to say so.
|
|
79
|
+
*/
|
|
80
|
+
export function projectGatewayPrefix(state, network, overrides) {
|
|
81
|
+
const onChain = state.collectionFields.find(
|
|
82
|
+
// Only `inline` counts. A prefix is a short UTF-8 string, and accepting exactly one
|
|
83
|
+
// representation keeps this rule identical on the renderer, the generator and here.
|
|
84
|
+
(f) => f.field === GATEWAY_FIELD[network] && f.representation === R.inline);
|
|
85
|
+
if (onChain) {
|
|
86
|
+
// No `.trim()`: the on-chain `_gatewayPrefix` returns the stored bytes verbatim (any non-empty
|
|
87
|
+
// `inline` value wins), so trimming here would make the two serving planes disagree on a
|
|
88
|
+
// whitespace-padded prefix — the same byte-parity class this projection exists to hold. The field
|
|
89
|
+
// store rejects an empty value, so a set field is always non-empty.
|
|
90
|
+
const stated = inlineText(onChain);
|
|
91
|
+
if (stated)
|
|
92
|
+
return stated;
|
|
93
|
+
}
|
|
94
|
+
const host = overrides?.[network];
|
|
95
|
+
return host ? gatewayPrefixFrom(network, host) : GATEWAY_FLOOR[network];
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Project a content-addressed field value into its https URL, byte-identically to the deployed
|
|
99
|
+
* renderer. `null` when the value locates nothing (`ipfs://` with no CID) — the caller omits an
|
|
100
|
+
* optional field or falls back on a required one, rather than emitting a bare prefix that 404s.
|
|
101
|
+
*
|
|
102
|
+
* Order is load-bearing and mirrors the Solidity exactly: strip the scheme, THEN substitute
|
|
103
|
+
* `{id}`, THEN check for an absolute URL. That last check is what stops a value which already
|
|
104
|
+
* names its own host — precisely what a `backend.locator()` return looks like — from coming back
|
|
105
|
+
* as `https://arweave.net/https://arweave.net/<txid>`.
|
|
106
|
+
*/
|
|
107
|
+
export function projectGatewayUrl(network, value, prefix, tokenId) {
|
|
108
|
+
let id = value;
|
|
109
|
+
const scheme = SCHEME[network];
|
|
110
|
+
if (id.startsWith(scheme))
|
|
111
|
+
id = id.slice(scheme.length);
|
|
112
|
+
if (!id)
|
|
113
|
+
return null;
|
|
114
|
+
// One collection-scope `ipfs` field addresses a whole pinned directory. Skipped on the
|
|
115
|
+
// collection surface, which has no tokenId — the same reason `url-template` is omitted there.
|
|
116
|
+
if (tokenId !== undefined)
|
|
117
|
+
id = id.split('{id}').join(tokenId);
|
|
118
|
+
if (/^https?:\/\//i.test(id))
|
|
119
|
+
return id;
|
|
120
|
+
return prefix + id;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* The inverse of {@link gatewayUrlFor} / {@link projectGatewayUrl}: recover the bare content id
|
|
124
|
+
* (CID or txid, plus any path suffix) from whatever form a locator arrived in.
|
|
125
|
+
*
|
|
126
|
+
* This exists because a storage backend's `locator()` returns a **gateway HTTPS URL** — the form an
|
|
127
|
+
* operator can click — while an `ipfs` / `arweave` metadata field must hold IDENTITY, with the
|
|
128
|
+
* gateway supplied at read time. Baking the operator's gateway host into the field is exactly the
|
|
129
|
+
* weld this projection was built to remove: it makes a CID unmigratable and makes the chain report
|
|
130
|
+
* `source: url` for bytes that live on IPFS.
|
|
131
|
+
*
|
|
132
|
+
* `null` when no id can be recovered — notably a subdomain-style gateway
|
|
133
|
+
* (`https://<cid>.ipfs.dweb.link`), which ABX's own backends never produce. A caller that gets
|
|
134
|
+
* `null` should keep the URL as a plain `url` field and SAY so, rather than guess.
|
|
135
|
+
*/
|
|
136
|
+
export function contentIdFromLocator(network, locator) {
|
|
137
|
+
const trimmed = locator.trim();
|
|
138
|
+
if (!trimmed)
|
|
139
|
+
return null;
|
|
140
|
+
const scheme = SCHEME[network];
|
|
141
|
+
if (trimmed.startsWith(scheme))
|
|
142
|
+
return trimmed.slice(scheme.length) || null;
|
|
143
|
+
if (!/^https?:\/\//i.test(trimmed))
|
|
144
|
+
return trimmed; // already bare
|
|
145
|
+
if (network === 'ipfs') {
|
|
146
|
+
const at = trimmed.lastIndexOf('/ipfs/');
|
|
147
|
+
return at === -1 ? null : trimmed.slice(at + '/ipfs/'.length) || null;
|
|
148
|
+
}
|
|
149
|
+
// Strip the origin by hand rather than via `new URL().pathname`, which percent-encodes: an Arweave
|
|
150
|
+
// directory template `https://arweave.net/TXDIR/{id}.png` came back as `TXDIR/%7Bid%7D.png`, and
|
|
151
|
+
// that value would have been COMMITTED ON CHAIN — where nothing substitutes `%7Bid%7D`, so every
|
|
152
|
+
// token in the collection would resolve to the same missing file.
|
|
153
|
+
const afterOrigin = trimmed.replace(/^https?:\/\/[^/]*\/?/i, '');
|
|
154
|
+
return afterOrigin || null;
|
|
155
|
+
}
|
|
156
|
+
//# sourceMappingURL=gateways.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"gateways.js","sourceRoot":"","sources":["../src/gateways.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,OAAO,EAAC,OAAO,EAAC,MAAM,WAAW,CAAC;AAClC,OAAO,EAAC,aAAa,EAAE,aAAa,EAAE,uBAAuB,IAAI,CAAC,EAAC,MAAM,YAAY,CAAC;AACtF,OAAO,EAAC,UAAU,EAAC,MAAM,YAAY,CAAC;A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AAE,EAAE,CAAC,CAAC;IACjE,OAAO,WAAW,IAAI,IAAI,CAAC;AAC7B,CAAC"}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The generator-document family — the template-mode runtime companion, the inline-safety
|
|
3
|
+
* escapes, and the pure document-shape builders. Hoisted from `@artblocks/abx-token-api`
|
|
4
|
+
* (the abx-services integrator ask): every piece here is a pure string operation with no chain
|
|
5
|
+
* access and no Node dependency, so it belongs in the neutral layer a resolver, a CLI preview,
|
|
6
|
+
* and any third-party provider all consume identically — one definition of the runtime surface
|
|
7
|
+
* and the document shape, so none of them can quietly drift from what the others emit.
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* `abx.js` — the runtime companion (the hl-gen.js analog), served at `/abx.js` and
|
|
11
|
+
* inlined into template-mode documents. Convention, not protocol: a directory build
|
|
12
|
+
* includes it; the generator injects the same surface.
|
|
13
|
+
*
|
|
14
|
+
* tokenData resolution order (the durability chain): the injected `window.abxTokenData`
|
|
15
|
+
* global (template mode) → the `?abx=` query param (base64url canonical JSON — the
|
|
16
|
+
* live-view route) → the `?chainId&contract&tokenId` coordinate floor (a build opened
|
|
17
|
+
* bare from a gateway still knows which token it is; full param reads from RPC are the
|
|
18
|
+
* render-node's job, not this shim's).
|
|
19
|
+
*/
|
|
20
|
+
export declare const ABX_JS = "(function () {\n var abx = (window.abx = window.abx || {});\n function fromQuery() {\n try {\n var q = new URLSearchParams(location.search);\n var packed = q.get('abx');\n if (packed) {\n var b64 = packed.replace(/-/g, '+').replace(/_/g, '/');\n return JSON.parse(new TextDecoder().decode(Uint8Array.from(atob(b64), function (c) { return c.charCodeAt(0); })));\n }\n // coordinate floor: enough for a deterministic piece opened bare from a gateway\n if (q.get('contract') && q.get('tokenId')) {\n return {\n chainId: Number(q.get('chainId') || 1),\n contractAddress: String(q.get('contract')).toLowerCase(),\n tokenId: String(q.get('tokenId')),\n };\n }\n } catch (e) {}\n return null;\n }\n abx.tokenData = window.abxTokenData || fromQuery();\n abx.__traits = null;\n abx.__done = false;\n /** The script reports its computed traits (script-defined features; captured at render). */\n abx.traits = function (t) {\n abx.__traits = t;\n try { document.dispatchEvent(new CustomEvent('abx:traits', {detail: t})); } catch (e) {}\n return t;\n };\n /** Output-complete \u2014 the capture point for the render effect. Optional (timeout fallback). */\n abx.done = function () {\n abx.__done = true;\n try { document.dispatchEvent(new CustomEvent('abx:done')); } catch (e) {}\n };\n})();\n";
|
|
21
|
+
/**
|
|
22
|
+
* Inline-document safety — the one HTML parsing rule that matters when embedding
|
|
23
|
+
* content inside <script> elements: the parser ends the element at the first
|
|
24
|
+
* `</script`, regardless of JS string/comment context. Two content shapes, two
|
|
25
|
+
* semantics-preserving escapes:
|
|
26
|
+
*
|
|
27
|
+
* - JS source (libraries, creator scripts): `</script` → `<\/script`. In valid JS the
|
|
28
|
+
* sequence can only occur inside a string, template, regex, or comment — contexts
|
|
29
|
+
* where `\/` is identical to `/` — so the transform never changes behavior.
|
|
30
|
+
* - JSON (the injected `window.abxTokenData`): every `<` → `\u003c`. `<` only occurs
|
|
31
|
+
* inside JSON strings (never in the syntax), and `\u003c` parses to the same
|
|
32
|
+
* character — the canonical (hashed) serialization is untouched; only the delivery
|
|
33
|
+
* form differs.
|
|
34
|
+
*/
|
|
35
|
+
/** JS source safe to inline inside a <script> element. Case-insensitive because HTML end tags
|
|
36
|
+
* are; the on-chain generator matches this (see `AbxGenerator._escapedScript`). */
|
|
37
|
+
export declare function escapeInlineScript(src: string): string;
|
|
38
|
+
/** A JSON payload safe to inline inside a <script> element (parse-identical). */
|
|
39
|
+
export declare function escapeInlineJson(json: string): string;
|
|
40
|
+
/**
|
|
41
|
+
* Inject `window.abxTokenData` (and, when the document doesn't already carry one, a
|
|
42
|
+
* `<base href="{code root}/">` so relative asset paths keep riding the gateway) at the
|
|
43
|
+
* very top of `<head>` — before any build script can execute; abx.js resolves the global
|
|
44
|
+
* first. String-level and deliberately robust rather than a full HTML parse: no `<head>`
|
|
45
|
+
* → inject right after `<html …>`; neither → prepend. First `<base>` wins in HTML, so an
|
|
46
|
+
* existing one is never doubled.
|
|
47
|
+
*/
|
|
48
|
+
export declare function injectTokenDataIntoHtml(html: string, tokenDataJson: string, codeRoot: string): string;
|
|
49
|
+
/**
|
|
50
|
+
* The template-mode document shape, pure — no chain access, so a caller that already has a
|
|
51
|
+
* script + dep tags in hand (the resolver's own chain-fetched assembly; `abx preview`'s offline
|
|
52
|
+
* studio, local-file + CDN-resolved) can build the byte-identical document. Exported for exactly
|
|
53
|
+
* that reason: **one** definition of the shape, so a caller can never quietly drift from what the
|
|
54
|
+
* generator actually serves — the failure mode a hand-maintained duplicate (with a "keep these in
|
|
55
|
+
* sync" comment and nothing enforcing it) invites.
|
|
56
|
+
*/
|
|
57
|
+
export declare function buildGeneratorDocument(script: string, tokenDataJson: string, depTags: string[]): string;
|
|
58
|
+
//# sourceMappingURL=generator-document.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"generator-document.d.ts","sourceRoot":"","sources":["../src/generator-document.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH;;;;;;;;;;GAUG;AACH,eAAO,MAAM,MAAM,o4CAoClB,CAAC;AAEF;;;;;;;;;;;;;GAaG;AAEH;oFACoF;AACpF,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAEtD;AAED,iFAAiF;AACjF,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAErD;AAED;;;;;;;GAOG;AACH,wBAAgB,uBAAuB,CAAC,IAAI,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,CAcrG;AAED;;;;;;;GAOG;AACH,wBAAgB,sBAAsB,CAAC,MAAM,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,MAAM,CAYvG"}
|