mppx 0.19.0 → 0.19.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.
@@ -0,0 +1,33 @@
1
+ import { type Address } from 'viem';
2
+ import * as Errors from '../../Errors.js';
3
+ /**
4
+ * Returns the revert data of a failed simulation (`eth_estimateGas` or `eth_call`), found anywhere
5
+ * in the error's `cause` chain.
6
+ */
7
+ export declare function getRevertData(error: unknown): `0x${string}` | undefined;
8
+ /**
9
+ * Maps a failed pre-signing simulation to a typed client error.
10
+ *
11
+ * Tempo reverts with `SpendingLimitExceeded()` when the signing access key's remaining limit is
12
+ * below the payment, and with `InsufficientBalance(available, required, token)` when the account
13
+ * holds too little of a token. Both would fail on chain, and a fee payer would pay for the failed
14
+ * transaction, so the client stops before it signs. Returns `undefined` for any other error.
15
+ */
16
+ export declare function toClientError(error: unknown, parameters: {
17
+ account: {
18
+ address: Address;
19
+ accessKeyAddress?: unknown;
20
+ };
21
+ }): Errors.SpendingLimitExceededError | Errors.InsufficientBalanceError | undefined;
22
+ /**
23
+ * Runs a step that simulates the transaction before it is signed, such as
24
+ * `prepareTransactionRequest` (which estimates gas), and rethrows a spending-limit or balance
25
+ * revert as a typed client error.
26
+ */
27
+ export declare function run<value>(parameters: {
28
+ account: {
29
+ address: Address;
30
+ accessKeyAddress?: unknown;
31
+ };
32
+ }, fn: () => Promise<value>): Promise<value>;
33
+ //# sourceMappingURL=simulation.d.ts.map
@@ -0,0 +1,80 @@
1
+ import { decodeErrorResult } from 'viem';
2
+ import { Abis } from 'viem/tempo';
3
+ import * as Errors from '../../Errors.js';
4
+ const abi = [
5
+ ...Abis.accountKeychain.filter((item) => item.type === 'error'),
6
+ ...Abis.tip20.filter((item) => item.type === 'error'),
7
+ ];
8
+ /**
9
+ * Returns the revert data of a failed simulation (`eth_estimateGas` or `eth_call`), found anywhere
10
+ * in the error's `cause` chain.
11
+ */
12
+ export function getRevertData(error) {
13
+ const seen = new Set();
14
+ let current = error;
15
+ while (typeof current === 'object' && current !== null && !seen.has(current)) {
16
+ seen.add(current);
17
+ const data = current.data;
18
+ if (isRevertData(data))
19
+ return data;
20
+ const nested = data?.data;
21
+ if (isRevertData(nested))
22
+ return nested;
23
+ current = current.cause;
24
+ }
25
+ return undefined;
26
+ }
27
+ /**
28
+ * Maps a failed pre-signing simulation to a typed client error.
29
+ *
30
+ * Tempo reverts with `SpendingLimitExceeded()` when the signing access key's remaining limit is
31
+ * below the payment, and with `InsufficientBalance(available, required, token)` when the account
32
+ * holds too little of a token. Both would fail on chain, and a fee payer would pay for the failed
33
+ * transaction, so the client stops before it signs. Returns `undefined` for any other error.
34
+ */
35
+ export function toClientError(error, parameters) {
36
+ const data = getRevertData(error);
37
+ if (!data)
38
+ return undefined;
39
+ const decoded = (() => {
40
+ try {
41
+ return decodeErrorResult({ abi, data });
42
+ }
43
+ catch {
44
+ return undefined;
45
+ }
46
+ })();
47
+ if (decoded?.errorName === 'SpendingLimitExceeded') {
48
+ const accessKey = parameters.account.accessKeyAddress;
49
+ return new Errors.SpendingLimitExceededError({
50
+ accessKey: typeof accessKey === 'string' ? accessKey : undefined,
51
+ account: parameters.account.address,
52
+ cause: error,
53
+ });
54
+ }
55
+ if (decoded?.errorName === 'InsufficientBalance') {
56
+ const [available, required, token] = decoded.args;
57
+ return new Errors.InsufficientBalanceError({
58
+ cause: error,
59
+ reason: `${parameters.account.address} holds ${available} of token ${token}, but the payment requires ${required}`,
60
+ });
61
+ }
62
+ return undefined;
63
+ }
64
+ /**
65
+ * Runs a step that simulates the transaction before it is signed, such as
66
+ * `prepareTransactionRequest` (which estimates gas), and rethrows a spending-limit or balance
67
+ * revert as a typed client error.
68
+ */
69
+ export async function run(parameters, fn) {
70
+ try {
71
+ return await fn();
72
+ }
73
+ catch (error) {
74
+ throw toClientError(error, parameters) ?? error;
75
+ }
76
+ }
77
+ function isRevertData(value) {
78
+ return typeof value === 'string' && /^0x[0-9a-fA-F]{8}/.test(value);
79
+ }
80
+ //# sourceMappingURL=simulation.js.map