@spectratools/tx-shared 0.4.3 → 0.5.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.
@@ -1,130 +0,0 @@
1
- import {
2
- TxError
3
- } from "./chunk-6T4D5UCR.js";
4
-
5
- // src/execute-tx.ts
6
- async function executeTx(options) {
7
- const {
8
- publicClient,
9
- walletClient,
10
- account,
11
- address,
12
- abi,
13
- functionName,
14
- chain,
15
- args,
16
- value,
17
- gasLimit,
18
- maxFeePerGas,
19
- nonce,
20
- dryRun = false
21
- } = options;
22
- let estimatedGas;
23
- try {
24
- estimatedGas = await publicClient.estimateContractGas({
25
- account,
26
- address,
27
- abi,
28
- functionName,
29
- args,
30
- value
31
- });
32
- } catch (error) {
33
- throw mapError(error, "estimation");
34
- }
35
- let simulationResult;
36
- try {
37
- const sim = await publicClient.simulateContract({
38
- account,
39
- address,
40
- abi,
41
- functionName,
42
- args,
43
- value
44
- });
45
- simulationResult = sim.result;
46
- } catch (error) {
47
- throw mapError(error, "simulation");
48
- }
49
- if (dryRun) {
50
- return {
51
- status: "dry-run",
52
- estimatedGas,
53
- simulationResult
54
- };
55
- }
56
- let hash;
57
- try {
58
- hash = await walletClient.writeContract({
59
- account,
60
- address,
61
- abi,
62
- functionName,
63
- args,
64
- value,
65
- chain,
66
- gas: gasLimit ?? estimatedGas,
67
- maxFeePerGas,
68
- nonce
69
- });
70
- } catch (error) {
71
- throw mapError(error, "submit");
72
- }
73
- let receipt;
74
- try {
75
- receipt = await publicClient.waitForTransactionReceipt({ hash });
76
- } catch (error) {
77
- throw mapError(error, "receipt");
78
- }
79
- if (receipt.status === "reverted") {
80
- throw new TxError("TX_REVERTED", `Transaction ${hash} reverted on-chain`);
81
- }
82
- return receiptToTxResult(receipt);
83
- }
84
- function receiptToTxResult(receipt) {
85
- return {
86
- hash: receipt.transactionHash,
87
- blockNumber: receipt.blockNumber,
88
- gasUsed: receipt.gasUsed,
89
- status: receipt.status === "success" ? "success" : "reverted",
90
- from: receipt.from,
91
- to: receipt.to,
92
- effectiveGasPrice: receipt.effectiveGasPrice
93
- };
94
- }
95
- function mapError(error, phase) {
96
- const msg = errorMessage(error);
97
- if (matchesInsufficientFunds(msg)) {
98
- return new TxError("INSUFFICIENT_FUNDS", `Insufficient funds: ${msg}`, error);
99
- }
100
- if (matchesNonceConflict(msg)) {
101
- return new TxError("NONCE_CONFLICT", `Nonce conflict: ${msg}`, error);
102
- }
103
- if (phase === "estimation" || phase === "simulation") {
104
- return new TxError("GAS_ESTIMATION_FAILED", `Gas estimation/simulation failed: ${msg}`, error);
105
- }
106
- if (matchesRevert(msg)) {
107
- return new TxError("TX_REVERTED", `Transaction reverted: ${msg}`, error);
108
- }
109
- return new TxError("TX_REVERTED", `Transaction failed (${phase}): ${msg}`, error);
110
- }
111
- function errorMessage(error) {
112
- if (error instanceof Error) return error.message;
113
- return String(error);
114
- }
115
- function matchesInsufficientFunds(msg) {
116
- const lower = msg.toLowerCase();
117
- return lower.includes("insufficient funds") || lower.includes("insufficient balance") || lower.includes("sender doesn't have enough funds");
118
- }
119
- function matchesNonceConflict(msg) {
120
- const lower = msg.toLowerCase();
121
- return lower.includes("nonce too low") || lower.includes("nonce has already been used") || lower.includes("already known") || lower.includes("replacement transaction underpriced");
122
- }
123
- function matchesRevert(msg) {
124
- const lower = msg.toLowerCase();
125
- return lower.includes("revert") || lower.includes("execution reverted") || lower.includes("transaction failed");
126
- }
127
-
128
- export {
129
- executeTx
130
- };