@certen.io/sdk 0.10.0 → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/CHANGELOG.md +80 -0
  2. package/README.md +30 -0
  3. package/dist/address.d.ts +12 -0
  4. package/dist/address.d.ts.map +1 -0
  5. package/dist/address.js +48 -0
  6. package/dist/address.js.map +1 -0
  7. package/dist/browser.d.ts +17 -4
  8. package/dist/browser.d.ts.map +1 -1
  9. package/dist/browser.js +14 -2
  10. package/dist/browser.js.map +1 -1
  11. package/dist/chains.d.ts +20 -0
  12. package/dist/chains.d.ts.map +1 -1
  13. package/dist/chains.js +34 -0
  14. package/dist/chains.js.map +1 -1
  15. package/dist/client.d.ts +15 -1
  16. package/dist/client.d.ts.map +1 -1
  17. package/dist/client.js +16 -2
  18. package/dist/client.js.map +1 -1
  19. package/dist/doctor.d.ts +10 -1
  20. package/dist/doctor.d.ts.map +1 -1
  21. package/dist/doctor.js +6 -4
  22. package/dist/doctor.js.map +1 -1
  23. package/dist/effect.d.ts +99 -0
  24. package/dist/effect.d.ts.map +1 -0
  25. package/dist/effect.js +139 -0
  26. package/dist/effect.js.map +1 -0
  27. package/dist/evm-rpc.d.ts +55 -0
  28. package/dist/evm-rpc.d.ts.map +1 -0
  29. package/dist/evm-rpc.js +126 -0
  30. package/dist/evm-rpc.js.map +1 -0
  31. package/dist/funding.d.ts +171 -0
  32. package/dist/funding.d.ts.map +1 -1
  33. package/dist/funding.js +190 -8
  34. package/dist/funding.js.map +1 -1
  35. package/dist/key-page.d.ts +67 -0
  36. package/dist/key-page.d.ts.map +1 -0
  37. package/dist/key-page.js +154 -0
  38. package/dist/key-page.js.map +1 -0
  39. package/dist/readiness.d.ts +133 -0
  40. package/dist/readiness.d.ts.map +1 -0
  41. package/dist/readiness.js +467 -0
  42. package/dist/readiness.js.map +1 -0
  43. package/dist/receipt.d.ts +233 -0
  44. package/dist/receipt.d.ts.map +1 -0
  45. package/dist/receipt.js +318 -0
  46. package/dist/receipt.js.map +1 -0
  47. package/dist/resources/execute.d.ts +62 -1
  48. package/dist/resources/execute.d.ts.map +1 -1
  49. package/dist/resources/execute.js +102 -9
  50. package/dist/resources/execute.js.map +1 -1
  51. package/dist/shared-proof.d.ts.map +1 -1
  52. package/dist/shared-proof.js +21 -1
  53. package/dist/shared-proof.js.map +1 -1
  54. package/dist/verify.d.ts +126 -2
  55. package/dist/verify.d.ts.map +1 -1
  56. package/dist/verify.js +191 -2
  57. package/dist/verify.js.map +1 -1
  58. package/dist/version.d.ts +1 -1
  59. package/dist/version.js +1 -1
  60. package/package.json +1 -1
@@ -0,0 +1,126 @@
1
+ import { CertenError } from './errors.js';
2
+ import { chainInfo, chainSlug } from './chains.js';
3
+ const DEFAULT_TIMEOUT_MS = 10_000;
4
+ /** Every answer this module asks for is a few hundred bytes. Anything past this is not one, and is not read to the end. */
5
+ const MAX_RESPONSE_CHARS = 200_000;
6
+ function fail(message, code, details) {
7
+ throw new CertenError(message, 0, code, details ? { details } : undefined);
8
+ }
9
+ /**
10
+ * Validate an endpoint and name its host.
11
+ *
12
+ * @throws CertenError `EVM_RPC_INVALID` for something that is not an http(s) URL, or that carries a username or password (a credential in the
13
+ * URL would be sent on to whoever answers). A key in the PATH is allowed: it is how hosted nodes are addressed, and it is why only the host is
14
+ * ever reported.
15
+ */
16
+ export function checkEvmRpcEndpoint(rpc) {
17
+ let u;
18
+ try {
19
+ u = new URL(rpc);
20
+ }
21
+ catch {
22
+ return fail('certen: the RPC endpoint is not a URL.', 'EVM_RPC_INVALID');
23
+ }
24
+ if (u.protocol !== 'https:' && u.protocol !== 'http:') {
25
+ fail(`certen: the RPC endpoint must be http or https, not ${u.protocol}`, 'EVM_RPC_INVALID');
26
+ }
27
+ if (u.username || u.password) {
28
+ fail('certen: the RPC endpoint must not carry a username or password in the URL.', 'EVM_RPC_INVALID');
29
+ }
30
+ return { url: u.toString(), host: u.host };
31
+ }
32
+ /** One JSON-RPC call. The result is whatever the node returned under `result`; an `error` member, a non-200 or a non-JSON reply throws. */
33
+ export async function evmRpcCall(options, method, params) {
34
+ const { url, host } = checkEvmRpcEndpoint(options.rpc);
35
+ const doFetch = options.fetch ?? (typeof fetch === 'function' ? fetch : undefined);
36
+ if (!doFetch)
37
+ fail('certen: this environment has no fetch, so the node cannot be asked.', 'EVM_RPC_UNREADABLE');
38
+ const timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS;
39
+ const controller = new AbortController();
40
+ const timer = setTimeout(() => controller.abort(), timeoutMs);
41
+ let text;
42
+ try {
43
+ const res = await doFetch(url, {
44
+ method: 'POST',
45
+ headers: { 'content-type': 'application/json' },
46
+ body: JSON.stringify({ jsonrpc: '2.0', id: 1, method, params }),
47
+ redirect: 'error',
48
+ signal: controller.signal,
49
+ });
50
+ if (!res.ok)
51
+ fail(`certen: the node at ${host} answered HTTP ${res.status} to ${method}.`, 'EVM_RPC_UNREADABLE', { host, method, status: res.status });
52
+ text = await res.text();
53
+ }
54
+ catch (err) {
55
+ if (err instanceof CertenError)
56
+ throw err;
57
+ // fetch's own messages can contain the URL, and the URL can contain a key. The reason is reduced to what is safe to repeat.
58
+ const why = controller.signal.aborted ? `did not answer within ${timeoutMs / 1000}s` : 'could not be reached';
59
+ return fail(`certen: the node at ${host} ${why} (${method}).`, 'EVM_RPC_UNREADABLE', { host, method });
60
+ }
61
+ finally {
62
+ clearTimeout(timer);
63
+ }
64
+ if (text.length > MAX_RESPONSE_CHARS)
65
+ fail(`certen: the node at ${host} sent a reply too large to be an answer to ${method}.`, 'EVM_RPC_UNREADABLE', { host, method });
66
+ let body;
67
+ try {
68
+ body = JSON.parse(text);
69
+ }
70
+ catch {
71
+ return fail(`certen: the node at ${host} did not answer ${method} with JSON.`, 'EVM_RPC_UNREADABLE', { host, method });
72
+ }
73
+ if (body?.error) {
74
+ const message = typeof body.error.message === 'string' ? body.error.message.slice(0, 200) : 'an error';
75
+ fail(`certen: the node at ${host} could not answer ${method}: ${message}`, 'EVM_RPC_ERROR', { host, method });
76
+ }
77
+ if (body === null || typeof body !== 'object' || !('result' in body)) {
78
+ fail(`certen: the node at ${host} returned no result for ${method}.`, 'EVM_RPC_UNREADABLE', { host, method });
79
+ }
80
+ return body.result;
81
+ }
82
+ const hexToBigInt = (value, what, host) => {
83
+ if (typeof value !== 'string' || !/^0x[0-9a-fA-F]+$/.test(value)) {
84
+ return fail(`certen: the node at ${host} returned ${what} that is not a hex quantity.`, 'EVM_RPC_UNREADABLE', { host });
85
+ }
86
+ return BigInt(value);
87
+ };
88
+ /** The node's chain id (`eth_chainId`). */
89
+ export async function readEvmChainId(options) {
90
+ const { host } = checkEvmRpcEndpoint(options.rpc);
91
+ const id = hexToBigInt(await evmRpcCall(options, 'eth_chainId', []), 'a chain id', host);
92
+ return Number(id);
93
+ }
94
+ /**
95
+ * Refuse a node that is not on the named chain.
96
+ *
97
+ * @throws CertenError `EVM_RPC_CHAIN_UNKNOWN` (the chain is not in the catalogue, so there is nothing to compare with) or
98
+ * `EVM_RPC_WRONG_CHAIN` (the node says it is another network; `details` has `expected` and `actual`).
99
+ */
100
+ export async function assertRpcChain(options, chain) {
101
+ const { host } = checkEvmRpcEndpoint(options.rpc);
102
+ const info = chainInfo(chain);
103
+ if (!info)
104
+ fail(`certen: ${JSON.stringify(String(chain))} is not a chain this client knows, so the node at ${host} cannot be checked to be on it.`, 'EVM_RPC_CHAIN_UNKNOWN');
105
+ const actual = await readEvmChainId(options);
106
+ if (actual !== info.chainId) {
107
+ fail(`certen: the node at ${host} is chain ${actual}, not ${chainSlug(chain)} (${info.chainId}). A balance read from another network would mean nothing here.`, 'EVM_RPC_WRONG_CHAIN', { host, expected: info.chainId, actual });
108
+ }
109
+ }
110
+ /** An address's native balance in base units at a block (`'latest'` or a number). Needs a node that still holds that block's state. */
111
+ export async function readEvmBalance(options, address, block = 'latest') {
112
+ const { host } = checkEvmRpcEndpoint(options.rpc);
113
+ if (!/^0x[0-9a-fA-F]{40}$/.test(address))
114
+ fail(`certen: ${JSON.stringify(address)} is not an EVM address.`, 'INVALID_ADDRESS');
115
+ const tag = block === 'latest' ? 'latest' : `0x${block.toString(16)}`;
116
+ return hexToBigInt(await evmRpcCall(options, 'eth_getBalance', [address, tag]), 'a balance', host);
117
+ }
118
+ /** The header of a block: number, hash and receipts root, as the node reports them. Undefined when the node has no such block. */
119
+ export async function readEvmBlockHeader(options, block) {
120
+ const result = await evmRpcCall(options, 'eth_getBlockByNumber', [`0x${block.toString(16)}`, false]);
121
+ if (result === null || result === undefined)
122
+ return undefined;
123
+ const h = result;
124
+ return { number: h.number ?? `0x${block.toString(16)}`, ...(h.hash ? { hash: h.hash } : {}), ...(h.receiptsRoot ? { receiptsRoot: h.receiptsRoot } : {}) };
125
+ }
126
+ //# sourceMappingURL=evm-rpc.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"evm-rpc.js","sourceRoot":"","sources":["../src/evm-rpc.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AA4BnD,MAAM,kBAAkB,GAAG,MAAM,CAAC;AAClC,2HAA2H;AAC3H,MAAM,kBAAkB,GAAG,OAAO,CAAC;AAEnC,SAAS,IAAI,CAAC,OAAe,EAAE,IAAY,EAAE,OAAiC;IAC5E,MAAM,IAAI,WAAW,CAAC,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,OAAO,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC;AAC7E,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,mBAAmB,CAAC,GAAW;IAC7C,IAAI,CAAM,CAAC;IACX,IAAI,CAAC;QACH,CAAC,GAAG,IAAI,GAAG,CAAC,GAAG,CAAC,CAAC;IACnB,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,IAAI,CAAC,w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W,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,yBAAyB,EAAE,iBAAiB,CAAC,CAAC;IAC/H,MAAM,GAAG,GAAG,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,KAAK,KAAK,CAAC,QAAQ,CAAC,EAAE,CAAC,EAAE,CAAC;IACtE,OAAO,WAAW,CAAC,MAAM,UAAU,CAAC,OAAO,EAAE,gBAAgB,EAAE,CAAC,OAAO,EAAE,GAAG,CAAC,CAAC,EAAE,WAAW,EAAE,IAAI,CAAC,CAAC;AACrG,CAAC;AAED,kIAAkI;AAClI,MAAM,CAAC,KAAK,UAAU,kBAAkB,CACtC,OAAsB,EACtB,KAAa;IAEb,MAAM,MAAM,GAAG,MAAM,UAAU,CAAC,OAAO,EAAE,sBAAsB,EAAE,CAAC,KAAK,KAAK,CAAC,QAAQ,CAAC,EAAE,CAAC,EAAE,EAAE,KAAK,CAAC,CAAC,CAAC;IACrG,IAAI,MAAM,KAAK,IAAI,IAAI,MAAM,KAAK,SAAS;QAAE,OAAO,SAAS,CAAC;IAC9D,MAAM,CAAC,GAAG,MAAmE,CAAC;IAC9E,OAAO,EAAE,MAAM,EAAE,CAAC,CAAC,MAAM,IAAI,KAAK,KAAK,CAAC,QAAQ,CAAC,EAAE,CAAC,EAAE,EAAE,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,EAAE,YAAY,EAAE,CAAC,CAAC,YAAY,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC;AAC7J,CAAC"}
package/dist/funding.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { AxiosInstance } from 'axios';
2
2
  import { CertenError } from './errors.js';
3
+ import type { NativeBalanceReading } from './chains.js';
3
4
  /**
4
5
  * The unfunded abstract account guard.
5
6
  *
@@ -15,6 +16,9 @@ import { CertenError } from './errors.js';
15
16
  * **What it refuses, and what it lets through.**
16
17
  *
17
18
  * - A positively observed zero gas balance is refused ({@link CertenUnfundedAccountError}).
19
+ * - A balance that is positive but cannot cover the amount being moved plus the gas to move it is
20
+ * refused ({@link CertenUnderfundedAccountError}). Zero is the extreme case of this and keeps its
21
+ * own error. The gas half is an ESTIMATE (see {@link assessGas}); the amount half is not.
18
22
  * - A gas balance that cannot be IDENTIFIED is refused by name
19
23
  * ({@link CertenFundingUnverifiableError}): the chain's gas token is unknown to the catalogue,
20
24
  * the gateway reported balances none of which is that token, or it reported the gas row as
@@ -34,7 +38,84 @@ export declare class CertenUnfundedAccountError extends CertenError {
34
38
  /** Never. Funding is a human act; retrying changes nothing. */
35
39
  get isRetryable(): boolean;
36
40
  }
41
+ /**
42
+ * Thrown instead of submitting a value-moving intent from an account that holds some gas, but not enough: either less than the amount
43
+ * being moved, or too little left over once it is.
44
+ *
45
+ * `kind: 'amount'` is a fact (the account holds less than it is about to send). `kind: 'gas'` is judged against an ESTIMATE of what one
46
+ * execution costs, which is why it is overridable and says so.
47
+ */
48
+ export declare class CertenUnderfundedAccountError extends CertenError {
49
+ readonly address: string;
50
+ readonly chain: string;
51
+ readonly kind: 'amount' | 'gas';
52
+ /** Whole native units, as strings, so nothing here is a float. */
53
+ readonly balance: string;
54
+ readonly needed: string;
55
+ readonly symbol: string;
56
+ constructor(address: string, chain: string, shortfall: Extract<GasAssessment, {
57
+ state: 'short';
58
+ }>);
59
+ /** Never. Funding is a human act; retrying changes nothing. */
60
+ get isRetryable(): boolean;
61
+ }
37
62
  export type FundingUnverifiableReason = 'unknown-native' | 'native-not-reported' | 'unreadable';
63
+ /**
64
+ * What the native balance on an abstract account says about a value-moving intent.
65
+ *
66
+ * One function, used by the SDK's guard, the CLI's guard and `client.readiness()`: three copies of "is there enough" would drift, and the
67
+ * place they would drift is the amount of money at which a refusal starts.
68
+ *
69
+ * - `unknown`: the balance could not be read as a number (not reported yet, the wrong token, unreadable). **Never** a refusal on its own
70
+ * authority: an unknown balance does not block a submission, and is never reported as funded.
71
+ * - `empty`: positively zero.
72
+ * - `short`: positive, but below what is needed. `kind: 'amount'` when it cannot even cover the amount; `kind: 'gas'` when it covers the
73
+ * amount but not the amount plus the gas reserve (or leaves nothing at all for gas).
74
+ * - `sufficient`: enough. `estimated` says whether a gas reserve was part of the judgement; false means the chain has no estimate and only
75
+ * the amount was checked.
76
+ *
77
+ * Every quantity is exact integer arithmetic in base units and reported in whole units.
78
+ */
79
+ export type GasAssessment = {
80
+ state: 'unknown';
81
+ reading: NativeBalanceReading;
82
+ } | {
83
+ state: 'empty';
84
+ symbol: string;
85
+ balance: string;
86
+ amount: string;
87
+ reserve: string;
88
+ needed: string;
89
+ } | {
90
+ state: 'short';
91
+ kind: 'amount' | 'gas';
92
+ symbol: string;
93
+ balance: string;
94
+ amount: string;
95
+ reserve: string;
96
+ needed: string;
97
+ /** How much more would clear it, or null when only "something left over for gas" would (the whole balance was being moved). */
98
+ shortfall: string | null;
99
+ } | {
100
+ state: 'sufficient';
101
+ symbol: string;
102
+ balance: string;
103
+ amount: string;
104
+ reserve: string;
105
+ needed: string;
106
+ estimated: boolean;
107
+ };
108
+ export declare function assessGas(p: {
109
+ chain: string | number;
110
+ reading: NativeBalanceReading;
111
+ /** The native amount being moved, in whole units. Omit when nothing native moves (a token transfer, or only a readiness check). */
112
+ amount?: string;
113
+ /**
114
+ * The gas reserve in whole units. Undefined takes the catalogue's estimate for the chain; `null` means "no reserve", which is how a caller
115
+ * says it knows better than the catalogue.
116
+ */
117
+ reserve?: string | null;
118
+ }): GasAssessment;
38
119
  /**
39
120
  * Thrown instead of submitting a value-moving intent whose gas balance could not be identified.
40
121
  *
@@ -76,5 +157,95 @@ export declare function assertFundedForValue(http: AxiosInstance, params: {
76
157
  identityId: string;
77
158
  chain: string | undefined;
78
159
  amount: unknown;
160
+ /**
161
+ * What `amount` is measured in. `whole` (the default) is a transfer's `amount`: "0.0001" ETH. `base` is a contract call's `value`:
162
+ * wei. The same field name has carried both, which is why this is stated rather than guessed from the digits.
163
+ */
164
+ amountUnit?: 'whole' | 'base';
165
+ /** The token being moved. When it is not the chain's native token the amount is not native, and only the gas is judged. */
166
+ tokenSymbol?: string;
167
+ /** Override the catalogue's gas reserve (whole native units); `null` for none. See {@link assessGas}. */
168
+ gasReserve?: string | null;
79
169
  }): Promise<void>;
170
+ /**
171
+ * The part of an intent's amount that is paid in the chain's native token, in whole units, or undefined when none of it is.
172
+ *
173
+ * A token transfer's amount is in the token and does not draw down the gas balance; counting it would refuse an account that holds the
174
+ * token and plenty of gas. A base-unit (wei) value is converted so one comparison serves both spellings.
175
+ */
176
+ export declare function nativeAmountOf(chain: string, p: {
177
+ amount: unknown;
178
+ amountUnit?: 'whole' | 'base';
179
+ tokenSymbol?: string;
180
+ }): string | undefined;
181
+ /**
182
+ * An EIP-681 payment request for a NATIVE transfer: `ethereum:<address>@<chainId>?value=<wei>`.
183
+ *
184
+ * Opens a wallet with the recipient, the network and the amount already set, which is the only way to move a value from a terminal to a wallet
185
+ * without retyping it. A mistyped recipient is the one error in this product that loses money and cannot be undone, so this builds a link only
186
+ * when every part of it is certain, and says `undefined` otherwise:
187
+ *
188
+ * - the chain must be in the catalogue (a link for a guessed network sends value somewhere nobody chose);
189
+ * - the address must be 20 bytes of hex, and is used exactly as given (the gateway's checksum case is preserved, never re-derived);
190
+ * - the amount is whole units and becomes integer wei by BigInt arithmetic. More decimals than the token has is refused rather than rounded:
191
+ * rounding would change what is paid.
192
+ *
193
+ * The value is a plain integer. EIP-681 also allows `1e15`, but a plain integer is read by more wallets.
194
+ */
195
+ export declare function nativeTransferUri(p: {
196
+ chain: string | number;
197
+ address: string;
198
+ amount?: string;
199
+ }): string | undefined;
200
+ /**
201
+ * What to tell someone whose abstract account needs gas, and how to know when it has arrived.
202
+ *
203
+ * Funding was a faucet URL and a guess. This is the account, the least that lets a transaction run, how much to send so the next few do not
204
+ * each need another faucet visit, the faucet, a wallet link, and the alternative for a faucet that will not pay a contract address.
205
+ *
206
+ * **Both amounts are planning figures.** `minimum` is what the readiness gas check accepts: the catalogue's gas reserve (an ESTIMATE, there is
207
+ * no published per-execution cost) plus any amount about to be moved. `suggested` is ten executions' worth, enough to stop thinking about it.
208
+ * A chain with no reserve has neither: it says there is no estimate and asks for no number nobody measured, and any positive balance clears
209
+ * it (the check judges the amount alone there).
210
+ *
211
+ * `balance` is what the account holds now, in whole units. Anything that is not a plain decimal is unknown, and unknown is never satisfied.
212
+ *
213
+ * Words only: this module does not know it is being driven by a CLI, so it names no command. A CLI adds its own.
214
+ */
215
+ export interface GasFundingPlan {
216
+ chain: string;
217
+ symbol: string;
218
+ address: string;
219
+ /** What the account holds now, whole units; null when unknown. */
220
+ balance: string | null;
221
+ /** The least that clears the readiness gas check, whole units; null when the chain has no estimate and no amount was given. */
222
+ minimum: string | null;
223
+ /** What is suggested the account holds: `max(10 x reserve, minimum)`; null when there is no estimate. */
224
+ suggested: string | null;
225
+ /** What to send now to reach `suggested`: the difference from `balance`, or all of it when the balance is unknown. Null when there is no figure. */
226
+ send: string | null;
227
+ /** The least to send now to clear `minimum` (`0` when it is already cleared). Null when there is no figure. */
228
+ least: string | null;
229
+ /** The balance already clears `minimum` (or, with no estimate, is positive). False when unknown. */
230
+ satisfied: boolean;
231
+ /** `minimum` and `suggested` rest on the catalogue's reserve. False when there was none to rest on. */
232
+ estimated: boolean;
233
+ faucet: string | null;
234
+ /** EIP-681 link to send `send` to `address`; the bare address link when there is no figure; null for a chain without an id. */
235
+ paymentUri: string | null;
236
+ /** The instructions, one line each, in words. */
237
+ steps: string[];
238
+ /** The alternative for a faucet that refuses a contract address: a wallet you control, then a transfer from it. Two lines. */
239
+ twoStep: string[];
240
+ }
241
+ export declare function gasFundingPlan(p: {
242
+ chain: string | number;
243
+ address: string;
244
+ /** Whole units as the gateway reports it. Omit, null or anything unreadable for unknown. */
245
+ balance?: string | null;
246
+ /** The native amount about to be moved, whole units. Added to the minimum. */
247
+ amount?: string;
248
+ /** Override the catalogue's gas reserve (whole native units); `null` for none. */
249
+ reserve?: string | null;
250
+ }): GasFundingPlan | undefined;
80
251
  //# sourceMappingURL=funding.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"funding.d.ts","sourceRoot":"","sources":["../src/funding.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,OAAO,CAAC;AACtC,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAI1C;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AAEH,uEAAuE;AACvE,qBAAa,0BAA2B,SAAQ,WAAW;IACzD,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;gBAEX,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM;IAe1C,+DAA+D;IAC/D,IAAI,WAAW,IAAI,OAAO,CAEzB;CACF;AAED,MAAM,MAAM,yBAAyB,GAAG,gBAAgB,GAAG,qBAAqB,GAAG,YAAY,CAAC;AAEhG;;;;;GAKG;AACH,qBAAa,8BAA+B,SAAQ,WAAW;IAC7D,QAAQ,CAAC,OAAO,EAAE,MAAM,GAAG,SAAS,CAAC;IACrC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,MAAM,EAAE,yBAAyB,CAAC;gBAE/B,OAAO,EAAE,MAAM,GAAG,SAAS,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,yBAAyB,EAAE,GAAG,EAAE,MAAM;IActG,iGAAiG;IACjG,IAAI,WAAW,IAAI,OAAO,CAEzB;CACF;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,GAAG,SAAS,GAAG,MAAM,CAElF;AAED,oFAAoF;AACpF,wBAAgB,UAAU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G,SAAS,CAyD7B"}
package/dist/funding.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { CertenError } from './errors.js';
2
- import { chainSlug, nativeSymbolFor, readNativeBalance, describeUnverifiable } from './chains.js';
2
+ import { chainInfo, chainSlug, nativeSymbolFor, readNativeBalance, describeUnverifiable, gasReserveFor, parseUnits, formatUnits, } from './chains.js';
3
3
  /**
4
4
  * The unfunded abstract account guard.
5
5
  *
@@ -15,6 +15,9 @@ import { chainSlug, nativeSymbolFor, readNativeBalance, describeUnverifiable } f
15
15
  * **What it refuses, and what it lets through.**
16
16
  *
17
17
  * - A positively observed zero gas balance is refused ({@link CertenUnfundedAccountError}).
18
+ * - A balance that is positive but cannot cover the amount being moved plus the gas to move it is
19
+ * refused ({@link CertenUnderfundedAccountError}). Zero is the extreme case of this and keeps its
20
+ * own error. The gas half is an ESTIMATE (see {@link assessGas}); the amount half is not.
18
21
  * - A gas balance that cannot be IDENTIFIED is refused by name
19
22
  * ({@link CertenFundingUnverifiableError}): the chain's gas token is unknown to the catalogue,
20
23
  * the gateway reported balances none of which is that token, or it reported the gas row as
@@ -46,6 +49,74 @@ export class CertenUnfundedAccountError extends CertenError {
46
49
  return false;
47
50
  }
48
51
  }
52
+ /**
53
+ * Thrown instead of submitting a value-moving intent from an account that holds some gas, but not enough: either less than the amount
54
+ * being moved, or too little left over once it is.
55
+ *
56
+ * `kind: 'amount'` is a fact (the account holds less than it is about to send). `kind: 'gas'` is judged against an ESTIMATE of what one
57
+ * execution costs, which is why it is overridable and says so.
58
+ */
59
+ export class CertenUnderfundedAccountError extends CertenError {
60
+ address;
61
+ chain;
62
+ kind;
63
+ /** Whole native units, as strings, so nothing here is a float. */
64
+ balance;
65
+ needed;
66
+ symbol;
67
+ constructor(address, chain, shortfall) {
68
+ super(shortfall.kind === 'amount'
69
+ ? `The abstract account ${address} on ${chain} holds ${shortfall.balance} ${shortfall.symbol}, less than the ${shortfall.amount} ${shortfall.symbol} this intent moves. `
70
+ + 'It would be accepted, signed and submitted, and then never execute. Fund it first, or pass skipFundingCheck: true to submit anyway.'
71
+ : `The abstract account ${address} on ${chain} holds ${shortfall.balance} ${shortfall.symbol}; moving ${shortfall.amount} ${shortfall.symbol} and paying for the execution needs about `
72
+ + `${shortfall.needed} ${shortfall.symbol}${shortfall.reserve === '0' ? ' (nothing would be left over for gas)' : ` (the gas share, ${shortfall.reserve} ${shortfall.symbol}, is an estimate, not a quote)`}. `
73
+ + 'Without gas the intent is accepted, signed and submitted, and then parks at "anchoring" forever. Fund it first, or pass skipFundingCheck: true to submit anyway.', 0, 'ABSTRACT_ACCOUNT_UNDERFUNDED');
74
+ this.name = 'CertenUnderfundedAccountError';
75
+ this.address = address;
76
+ this.chain = chain;
77
+ this.kind = shortfall.kind;
78
+ this.balance = shortfall.balance;
79
+ this.needed = shortfall.needed;
80
+ this.symbol = shortfall.symbol;
81
+ }
82
+ /** Never. Funding is a human act; retrying changes nothing. */
83
+ get isRetryable() {
84
+ return false;
85
+ }
86
+ }
87
+ export function assessGas(p) {
88
+ const reading = p.reading;
89
+ if (reading.state !== 'funded' && reading.state !== 'empty')
90
+ return { state: 'unknown', reading };
91
+ const decimals = chainInfo(p.chain)?.nativeDecimals;
92
+ if (decimals === undefined)
93
+ return { state: 'unknown', reading: { state: 'unknown-native', chain: chainSlug(p.chain) } };
94
+ const balance = parseUnits(reading.balance, decimals);
95
+ // A number that is not a plain decimal ("1e-7", "unavailable") is not something to do arithmetic on. Unreadable, not zero.
96
+ if (balance === undefined)
97
+ return { state: 'unknown', reading: { state: 'unreadable', symbol: reading.symbol, balance: reading.balance } };
98
+ // An amount that is not a decimal cannot be compared; the reserve is still judged.
99
+ const amount = p.amount === undefined ? 0n : parseUnits(p.amount, decimals) ?? 0n;
100
+ const reserveText = p.reserve === undefined ? gasReserveFor(p.chain) : p.reserve ?? undefined;
101
+ const reserve = reserveText === undefined ? 0n : parseUnits(reserveText, decimals) ?? 0n;
102
+ const needed = amount + reserve;
103
+ const out = {
104
+ symbol: reading.symbol,
105
+ balance: formatUnits(balance, decimals),
106
+ amount: formatUnits(amount, decimals),
107
+ reserve: formatUnits(reserve, decimals),
108
+ needed: formatUnits(needed, decimals),
109
+ };
110
+ if (balance === 0n)
111
+ return { state: 'empty', ...out };
112
+ const shortfall = needed > balance ? formatUnits(needed - balance, decimals) : null;
113
+ if (balance < amount)
114
+ return { state: 'short', kind: 'amount', ...out, shortfall };
115
+ // Moving the whole balance leaves nothing to pay gas with, estimate or no estimate.
116
+ if (balance < needed || (amount > 0n && balance === amount))
117
+ return { state: 'short', kind: 'gas', ...out, shortfall };
118
+ return { state: 'sufficient', ...out, estimated: reserve > 0n };
119
+ }
49
120
  /**
50
121
  * Thrown instead of submitting a value-moving intent whose gas balance could not be identified.
51
122
  *
@@ -140,17 +211,128 @@ export async function assertFundedForValue(http, params) {
140
211
  }
141
212
  if (address === undefined || reading === undefined)
142
213
  return;
143
- switch (reading.state) {
144
- case 'funded':
214
+ const assessment = assessGas({
215
+ chain: want,
216
+ reading,
217
+ amount: nativeAmountOf(want, params),
218
+ reserve: params.gasReserve,
219
+ });
220
+ switch (assessment.state) {
221
+ case 'sufficient':
145
222
  return;
146
223
  case 'empty':
147
224
  throw new CertenUnfundedAccountError(address, want);
148
- case 'unknown-native':
149
- case 'native-not-reported':
150
- case 'unreadable':
151
- throw new CertenFundingUnverifiableError(address, want, reading.state, describeUnverifiable(want, reading));
152
- default:
225
+ case 'short':
226
+ throw new CertenUnderfundedAccountError(address, want, assessment);
227
+ case 'unknown': {
228
+ const r = assessment.reading;
229
+ if (r.state === 'unknown-native' || r.state === 'native-not-reported' || r.state === 'unreadable') {
230
+ throw new CertenFundingUnverifiableError(address, want, r.state, describeUnverifiable(want, r));
231
+ }
153
232
  return;
233
+ }
234
+ }
235
+ }
236
+ /**
237
+ * The part of an intent's amount that is paid in the chain's native token, in whole units, or undefined when none of it is.
238
+ *
239
+ * A token transfer's amount is in the token and does not draw down the gas balance; counting it would refuse an account that holds the
240
+ * token and plenty of gas. A base-unit (wei) value is converted so one comparison serves both spellings.
241
+ */
242
+ export function nativeAmountOf(chain, p) {
243
+ const info = chainInfo(chain);
244
+ if (!info || p.amount === undefined || p.amount === null)
245
+ return undefined;
246
+ if (p.tokenSymbol && p.tokenSymbol.toUpperCase() !== info.nativeSymbol.toUpperCase())
247
+ return undefined;
248
+ const text = String(p.amount).trim();
249
+ if (p.amountUnit === 'base')
250
+ return /^\d+$/.test(text) ? formatUnits(BigInt(text), info.nativeDecimals) : undefined;
251
+ return text;
252
+ }
253
+ // ── telling someone how to fund an account ──────────────────────────────────────────────────────────
254
+ const ADDRESS_RE = /^0x[0-9a-fA-F]{40}$/;
255
+ /**
256
+ * An EIP-681 payment request for a NATIVE transfer: `ethereum:<address>@<chainId>?value=<wei>`.
257
+ *
258
+ * Opens a wallet with the recipient, the network and the amount already set, which is the only way to move a value from a terminal to a wallet
259
+ * without retyping it. A mistyped recipient is the one error in this product that loses money and cannot be undone, so this builds a link only
260
+ * when every part of it is certain, and says `undefined` otherwise:
261
+ *
262
+ * - the chain must be in the catalogue (a link for a guessed network sends value somewhere nobody chose);
263
+ * - the address must be 20 bytes of hex, and is used exactly as given (the gateway's checksum case is preserved, never re-derived);
264
+ * - the amount is whole units and becomes integer wei by BigInt arithmetic. More decimals than the token has is refused rather than rounded:
265
+ * rounding would change what is paid.
266
+ *
267
+ * The value is a plain integer. EIP-681 also allows `1e15`, but a plain integer is read by more wallets.
268
+ */
269
+ export function nativeTransferUri(p) {
270
+ const info = chainInfo(p.chain);
271
+ if (!info || !ADDRESS_RE.test(p.address))
272
+ return undefined;
273
+ const base = `ethereum:${p.address}@${info.chainId}`;
274
+ if (p.amount === undefined)
275
+ return base;
276
+ const wei = parseUnits(p.amount, info.nativeDecimals);
277
+ return wei === undefined ? undefined : `${base}?value=${wei.toString()}`;
278
+ }
279
+ export function gasFundingPlan(p) {
280
+ const info = chainInfo(p.chain);
281
+ if (!info)
282
+ return undefined;
283
+ const decimals = info.nativeDecimals;
284
+ const symbol = info.nativeSymbol;
285
+ const slug = info.slug;
286
+ const held = p.balance === undefined || p.balance === null ? undefined : parseUnits(p.balance, decimals);
287
+ const reserveText = p.reserve === undefined ? gasReserveFor(slug) : p.reserve ?? undefined;
288
+ const reserve = reserveText === undefined ? undefined : parseUnits(reserveText, decimals);
289
+ const amount = p.amount === undefined ? 0n : parseUnits(p.amount, decimals) ?? 0n;
290
+ const minimumWei = reserve === undefined ? (amount > 0n ? amount : undefined) : reserve + amount;
291
+ const suggestedWei = minimumWei === undefined ? undefined : (reserve !== undefined && reserve * 10n > minimumWei ? reserve * 10n : minimumWei);
292
+ const sendWei = suggestedWei === undefined ? undefined : (held !== undefined && held < suggestedWei ? suggestedWei - held : (held === undefined ? suggestedWei : 0n));
293
+ const leastWei = minimumWei === undefined ? undefined : (held !== undefined && held < minimumWei ? minimumWei - held : (held === undefined ? minimumWei : 0n));
294
+ // With nothing estimated, any positive balance clears it: the readiness check then judges the amount alone.
295
+ const satisfied = held === undefined ? false : minimumWei === undefined ? held > 0n : held >= minimumWei;
296
+ const fmt = (v) => (v === undefined ? null : formatUnits(v, decimals));
297
+ const send = fmt(sendWei);
298
+ const faucet = info.faucet ?? null;
299
+ const paymentUri = nativeTransferUri({ chain: slug, address: p.address, amount: send && sendWei !== 0n ? send : undefined }) ?? null;
300
+ const where = `${p.address} on ${slug}`;
301
+ const estimated = reserve !== undefined;
302
+ const steps = [];
303
+ if (send && sendWei !== 0n && leastWei === 0n) {
304
+ // Already enough to act; this only tops it up.
305
+ steps.push(`${where} already holds ${fmt(held)} ${symbol}, enough to act. To top it up to ${fmt(suggestedWei)} ${symbol}, send ${send} ${symbol}.`);
306
+ }
307
+ else if (send && sendWei !== 0n) {
308
+ // The suggestion is more than the minimum: say both, and which is which. "At least" would contradict "the least that works".
309
+ const split = leastWei !== undefined && sendWei !== undefined && leastWei < sendWei;
310
+ steps.push(split
311
+ ? `Send ${send} ${symbol} to ${where} (${fmt(leastWei)} ${symbol}${held !== undefined && held > 0n ? ' more' : ''} is the least that works; the rest saves a second trip).`
312
+ : `Send at least ${send} ${symbol} to ${where}.`);
313
+ }
314
+ else if (send === null) {
315
+ steps.push(`Send some ${symbol} to ${where}. There is no estimate of what one execution costs on ${slug}, so there is no figure to give: any positive amount lets the account act, and an amount too small will fail at execution.`);
316
+ }
317
+ else {
318
+ steps.push(`${where} already holds ${fmt(held)} ${symbol}.`);
154
319
  }
320
+ if (faucet)
321
+ steps.push(`Testnet gas is free from the faucet: ${faucet}`);
322
+ if (paymentUri)
323
+ steps.push(`A wallet link with the network, the address${send && sendWei !== 0n ? ' and the amount' : ''} already set: ${paymentUri}`);
324
+ if (estimated)
325
+ steps.push(`The amounts are planning figures, not a quote: the gas share of ${fmt(reserve)} ${symbol} is an estimate.`);
326
+ const twoStep = [
327
+ 'Some faucets refuse a contract address, and this account is one. If yours does, send the gas to a wallet you control first.',
328
+ `Then, from that wallet, send ${send && sendWei !== 0n ? `${send} ${symbol}` : symbol} to ${p.address} on ${slug}${paymentUri ? ' (the wallet link opens that transfer pre-filled)' : ''}.`,
329
+ ];
330
+ steps.push(...twoStep);
331
+ return {
332
+ chain: slug, symbol, address: p.address,
333
+ balance: fmt(held),
334
+ minimum: fmt(minimumWei), suggested: fmt(suggestedWei), send, least: fmt(leastWei),
335
+ satisfied, estimated, faucet, paymentUri, steps, twoStep,
336
+ };
155
337
  }
156
338
  //# sourceMappingURL=funding.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"funding.js","sourceRoot":"","sources":["../src/funding.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,EAAE,SAAS,EAAE,eAAe,EAAE,iBAAiB,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAC;AAGlG;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AAEH,uEAAuE;AACvE,MAAM,OAAO,0BAA2B,SAAQ,WAAW;IAChD,OAAO,CAAS;IAChB,KAAK,C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