@aztec/ethereum 0.0.0-test.0 → 0.0.1-commit.001888fc
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/dest/account.d.ts +2 -0
- package/dest/account.d.ts.map +1 -0
- package/dest/account.js +4 -0
- package/dest/chain.d.ts +1 -1
- package/dest/client.d.ts +14 -4
- package/dest/client.d.ts.map +1 -1
- package/dest/client.js +27 -3
- package/dest/config.d.ts +70 -23
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +155 -34
- package/dest/constants.d.ts +1 -1
- package/dest/contracts/empire_base.d.ts +27 -8
- package/dest/contracts/empire_base.d.ts.map +1 -1
- package/dest/contracts/empire_base.js +75 -2
- package/dest/contracts/empire_slashing_proposer.d.ts +69 -0
- package/dest/contracts/empire_slashing_proposer.d.ts.map +1 -0
- package/dest/contracts/empire_slashing_proposer.js +216 -0
- package/dest/contracts/errors.d.ts +7 -0
- package/dest/contracts/errors.d.ts.map +1 -0
- package/dest/contracts/errors.js +12 -0
- package/dest/contracts/fee_asset_handler.d.ts +20 -0
- package/dest/contracts/fee_asset_handler.d.ts.map +1 -0
- package/dest/contracts/fee_asset_handler.js +59 -0
- package/dest/contracts/fee_asset_price_oracle.d.ts +101 -0
- package/dest/contracts/fee_asset_price_oracle.d.ts.map +1 -0
- package/dest/contracts/fee_asset_price_oracle.js +651 -0
- package/dest/contracts/fee_juice.d.ts +6 -7
- package/dest/contracts/fee_juice.d.ts.map +1 -1
- package/dest/contracts/fee_juice.js +27 -20
- package/dest/contracts/governance.d.ts +45 -32
- package/dest/contracts/governance.d.ts.map +1 -1
- package/dest/contracts/governance.js +98 -85
- package/dest/contracts/governance_proposer.d.ts +19 -13
- package/dest/contracts/governance_proposer.d.ts.map +1 -1
- package/dest/contracts/governance_proposer.js +441 -26
- package/dest/contracts/gse.d.ts +32 -0
- package/dest/contracts/gse.d.ts.map +1 -0
- package/dest/contracts/gse.js +72 -0
- package/dest/contracts/inbox.d.ts +47 -0
- package/dest/contracts/inbox.d.ts.map +1 -0
- package/dest/contracts/inbox.js +80 -0
- package/dest/contracts/index.d.ts +12 -3
- package/dest/contracts/index.d.ts.map +1 -1
- package/dest/contracts/index.js +11 -2
- package/dest/contracts/log.d.ts +13 -0
- package/dest/contracts/log.d.ts.map +1 -0
- package/dest/contracts/log.js +1 -0
- package/dest/contracts/multicall.d.ts +21 -0
- package/dest/contracts/multicall.d.ts.map +1 -0
- package/dest/contracts/multicall.js +157 -0
- package/dest/contracts/outbox.d.ts +41 -0
- package/dest/contracts/outbox.d.ts.map +1 -0
- package/dest/contracts/outbox.js +86 -0
- package/dest/contracts/registry.d.ts +12 -5
- package/dest/contracts/registry.d.ts.map +1 -1
- package/dest/contracts/registry.js +74 -17
- package/dest/contracts/rollup.d.ts +311 -53
- package/dest/contracts/rollup.d.ts.map +1 -1
- package/dest/contracts/rollup.js +1180 -116
- package/dest/contracts/slasher_contract.d.ts +44 -0
- package/dest/contracts/slasher_contract.d.ts.map +1 -0
- package/dest/contracts/slasher_contract.js +75 -0
- package/dest/contracts/tally_slashing_proposer.d.ts +140 -0
- package/dest/contracts/tally_slashing_proposer.d.ts.map +1 -0
- package/dest/contracts/tally_slashing_proposer.js +320 -0
- package/dest/contracts/utils.d.ts +3 -0
- package/dest/contracts/utils.d.ts.map +1 -0
- package/dest/contracts/utils.js +11 -0
- package/dest/deploy_aztec_l1_contracts.d.ts +259 -0
- package/dest/deploy_aztec_l1_contracts.d.ts.map +1 -0
- package/dest/deploy_aztec_l1_contracts.js +413 -0
- package/dest/deploy_l1_contract.d.ts +68 -0
- package/dest/deploy_l1_contract.d.ts.map +1 -0
- package/dest/deploy_l1_contract.js +312 -0
- package/dest/eth-signer/eth-signer.d.ts +21 -0
- package/dest/eth-signer/eth-signer.d.ts.map +1 -0
- package/dest/eth-signer/eth-signer.js +5 -0
- package/dest/eth-signer/index.d.ts +2 -0
- package/dest/eth-signer/index.d.ts.map +1 -0
- package/dest/eth-signer/index.js +1 -0
- package/dest/forwarder_proxy.d.ts +32 -0
- package/dest/forwarder_proxy.d.ts.map +1 -0
- package/dest/forwarder_proxy.js +93 -0
- package/dest/generated/l1-contracts-defaults.d.ts +30 -0
- package/dest/generated/l1-contracts-defaults.d.ts.map +1 -0
- package/dest/generated/l1-contracts-defaults.js +30 -0
- package/dest/l1_artifacts.d.ts +81744 -0
- package/dest/l1_artifacts.d.ts.map +1 -0
- package/dest/l1_artifacts.js +166 -0
- package/dest/l1_contract_addresses.d.ts +24 -4
- package/dest/l1_contract_addresses.d.ts.map +1 -1
- package/dest/l1_contract_addresses.js +25 -21
- package/dest/l1_reader.d.ts +6 -2
- package/dest/l1_reader.d.ts.map +1 -1
- package/dest/l1_reader.js +20 -9
- package/dest/l1_tx_utils/config.d.ts +65 -0
- package/dest/l1_tx_utils/config.d.ts.map +1 -0
- package/dest/l1_tx_utils/config.js +109 -0
- package/dest/l1_tx_utils/constants.d.ts +12 -0
- package/dest/l1_tx_utils/constants.d.ts.map +1 -0
- package/dest/l1_tx_utils/constants.js +39 -0
- package/dest/l1_tx_utils/factory.d.ts +32 -0
- package/dest/l1_tx_utils/factory.d.ts.map +1 -0
- package/dest/l1_tx_utils/factory.js +22 -0
- package/dest/l1_tx_utils/fee-strategies/index.d.ts +10 -0
- package/dest/l1_tx_utils/fee-strategies/index.d.ts.map +1 -0
- package/dest/l1_tx_utils/fee-strategies/index.js +12 -0
- package/dest/l1_tx_utils/fee-strategies/p75_competitive.d.ts +8 -0
- package/dest/l1_tx_utils/fee-strategies/p75_competitive.d.ts.map +1 -0
- package/dest/l1_tx_utils/fee-strategies/p75_competitive.js +129 -0
- package/dest/l1_tx_utils/fee-strategies/p75_competitive_blob_txs_only.d.ts +23 -0
- package/dest/l1_tx_utils/fee-strategies/p75_competitive_blob_txs_only.d.ts.map +1 -0
- package/dest/l1_tx_utils/fee-strategies/p75_competitive_blob_txs_only.js +191 -0
- package/dest/l1_tx_utils/fee-strategies/types.d.ts +51 -0
- package/dest/l1_tx_utils/fee-strategies/types.d.ts.map +1 -0
- package/dest/l1_tx_utils/fee-strategies/types.js +3 -0
- package/dest/l1_tx_utils/forwarder_l1_tx_utils.d.ts +41 -0
- package/dest/l1_tx_utils/forwarder_l1_tx_utils.d.ts.map +1 -0
- package/dest/l1_tx_utils/forwarder_l1_tx_utils.js +42 -0
- package/dest/l1_tx_utils/index-blobs.d.ts +3 -0
- package/dest/l1_tx_utils/index-blobs.d.ts.map +1 -0
- package/dest/l1_tx_utils/index-blobs.js +2 -0
- package/dest/l1_tx_utils/index.d.ts +13 -0
- package/dest/l1_tx_utils/index.d.ts.map +1 -0
- package/dest/l1_tx_utils/index.js +13 -0
- package/dest/l1_tx_utils/interfaces.d.ts +76 -0
- package/dest/l1_tx_utils/interfaces.d.ts.map +1 -0
- package/dest/l1_tx_utils/interfaces.js +4 -0
- package/dest/l1_tx_utils/l1_fee_analyzer.d.ts +233 -0
- package/dest/l1_tx_utils/l1_fee_analyzer.d.ts.map +1 -0
- package/dest/l1_tx_utils/l1_fee_analyzer.js +506 -0
- package/dest/l1_tx_utils/l1_tx_utils.d.ts +105 -0
- package/dest/l1_tx_utils/l1_tx_utils.d.ts.map +1 -0
- package/dest/l1_tx_utils/l1_tx_utils.js +639 -0
- package/dest/l1_tx_utils/readonly_l1_tx_utils.d.ts +83 -0
- package/dest/l1_tx_utils/readonly_l1_tx_utils.d.ts.map +1 -0
- package/dest/l1_tx_utils/readonly_l1_tx_utils.js +327 -0
- package/dest/l1_tx_utils/signer.d.ts +4 -0
- package/dest/l1_tx_utils/signer.d.ts.map +1 -0
- package/dest/l1_tx_utils/signer.js +16 -0
- package/dest/l1_tx_utils/tx_delayer.d.ts +56 -0
- package/dest/l1_tx_utils/tx_delayer.d.ts.map +1 -0
- package/dest/l1_tx_utils/tx_delayer.js +221 -0
- package/dest/l1_tx_utils/types.d.ts +67 -0
- package/dest/l1_tx_utils/types.d.ts.map +1 -0
- package/dest/l1_tx_utils/types.js +26 -0
- package/dest/l1_tx_utils/utils.d.ts +4 -0
- package/dest/l1_tx_utils/utils.d.ts.map +1 -0
- package/dest/l1_tx_utils/utils.js +14 -0
- package/dest/l1_types.d.ts +6 -0
- package/dest/l1_types.d.ts.map +1 -0
- package/dest/l1_types.js +1 -0
- package/dest/publisher_manager.d.ts +16 -0
- package/dest/publisher_manager.d.ts.map +1 -0
- package/dest/publisher_manager.js +88 -0
- package/dest/queries.d.ts +5 -3
- package/dest/queries.d.ts.map +1 -1
- package/dest/queries.js +61 -12
- package/dest/test/chain_monitor.d.ts +94 -0
- package/dest/test/chain_monitor.d.ts.map +1 -0
- package/dest/test/chain_monitor.js +244 -0
- package/dest/test/eth_cheat_codes.d.ts +229 -0
- package/dest/test/eth_cheat_codes.d.ts.map +1 -0
- package/dest/test/eth_cheat_codes.js +560 -0
- package/dest/test/eth_cheat_codes_with_state.d.ts +2 -2
- package/dest/test/eth_cheat_codes_with_state.d.ts.map +1 -1
- package/dest/test/eth_cheat_codes_with_state.js +1 -1
- package/dest/test/index.d.ts +4 -3
- package/dest/test/index.d.ts.map +1 -1
- package/dest/test/index.js +3 -2
- package/dest/test/rollup_cheat_codes.d.ts +90 -0
- package/dest/test/rollup_cheat_codes.d.ts.map +1 -0
- package/dest/test/rollup_cheat_codes.js +292 -0
- package/dest/test/start_anvil.d.ts +23 -2
- package/dest/test/start_anvil.d.ts.map +1 -1
- package/dest/test/start_anvil.js +133 -24
- package/dest/test/upgrade_utils.d.ts +6 -5
- package/dest/test/upgrade_utils.d.ts.map +1 -1
- package/dest/test/upgrade_utils.js +23 -16
- package/dest/types.d.ts +62 -8
- package/dest/types.d.ts.map +1 -1
- package/dest/types.js +3 -1
- package/dest/utils.d.ts +17 -3
- package/dest/utils.d.ts.map +1 -1
- package/dest/utils.js +111 -88
- package/dest/zkPassportVerifierAddress.d.ts +15 -0
- package/dest/zkPassportVerifierAddress.d.ts.map +1 -0
- package/dest/zkPassportVerifierAddress.js +11 -0
- package/package.json +51 -25
- package/src/account.ts +5 -0
- package/src/client.ts +51 -5
- package/src/config.ts +222 -43
- package/src/contracts/README.md +157 -0
- package/src/contracts/empire_base.ts +79 -7
- package/src/contracts/empire_slashing_proposer.ts +265 -0
- package/src/contracts/errors.ts +13 -0
- package/src/contracts/fee_asset_handler.ts +66 -0
- package/src/contracts/fee_asset_price_oracle.ts +280 -0
- package/src/contracts/fee_juice.ts +29 -15
- package/src/contracts/governance.ts +90 -78
- package/src/contracts/governance_proposer.ts +81 -25
- package/src/contracts/gse.ts +88 -0
- package/src/contracts/inbox.ts +115 -0
- package/src/contracts/index.ts +11 -2
- package/src/contracts/log.ts +13 -0
- package/src/contracts/multicall.ts +158 -0
- package/src/contracts/outbox.ts +98 -0
- package/src/contracts/registry.ts +81 -26
- package/src/contracts/rollup.ts +940 -89
- package/src/contracts/slasher_contract.ts +89 -0
- package/src/contracts/tally_slashing_proposer.ts +322 -0
- package/src/contracts/utils.ts +14 -0
- package/src/deploy_aztec_l1_contracts.ts +650 -0
- package/src/deploy_l1_contract.ts +362 -0
- package/src/eth-signer/eth-signer.ts +25 -0
- package/src/eth-signer/index.ts +1 -0
- package/src/forwarder_proxy.ts +108 -0
- package/src/generated/l1-contracts-defaults.ts +32 -0
- package/src/l1_artifacts.ts +254 -0
- package/src/l1_contract_addresses.ts +49 -34
- package/src/l1_reader.ts +30 -10
- package/src/l1_tx_utils/README.md +177 -0
- package/src/l1_tx_utils/config.ts +181 -0
- package/src/l1_tx_utils/constants.ts +29 -0
- package/src/l1_tx_utils/factory.ts +64 -0
- package/src/l1_tx_utils/fee-strategies/index.ts +22 -0
- package/src/l1_tx_utils/fee-strategies/p75_competitive.ts +163 -0
- package/src/l1_tx_utils/fee-strategies/p75_competitive_blob_txs_only.ts +245 -0
- package/src/l1_tx_utils/fee-strategies/types.ts +56 -0
- package/src/l1_tx_utils/forwarder_l1_tx_utils.ts +108 -0
- package/src/l1_tx_utils/index-blobs.ts +2 -0
- package/src/l1_tx_utils/index.ts +15 -0
- package/src/l1_tx_utils/interfaces.ts +86 -0
- package/src/l1_tx_utils/l1_fee_analyzer.ts +803 -0
- package/src/l1_tx_utils/l1_tx_utils.ts +766 -0
- package/src/l1_tx_utils/readonly_l1_tx_utils.ts +423 -0
- package/src/l1_tx_utils/signer.ts +28 -0
- package/src/l1_tx_utils/tx_delayer.ts +294 -0
- package/src/l1_tx_utils/types.ts +85 -0
- package/src/l1_tx_utils/utils.ts +16 -0
- package/src/l1_types.ts +6 -0
- package/src/publisher_manager.ts +108 -0
- package/src/queries.ts +82 -16
- package/src/test/chain_monitor.ts +302 -0
- package/src/test/eth_cheat_codes.ts +588 -0
- package/src/test/eth_cheat_codes_with_state.ts +1 -1
- package/src/test/index.ts +3 -2
- package/src/test/rollup_cheat_codes.ts +330 -0
- package/src/test/start_anvil.ts +166 -22
- package/src/test/upgrade_utils.ts +30 -21
- package/src/types.ts +71 -7
- package/src/utils.ts +137 -93
- package/src/zkPassportVerifierAddress.ts +15 -0
- package/dest/contracts/forwarder.d.ts +0 -24
- package/dest/contracts/forwarder.d.ts.map +0 -1
- package/dest/contracts/forwarder.js +0 -101
- package/dest/contracts/slashing_proposer.d.ts +0 -21
- package/dest/contracts/slashing_proposer.d.ts.map +0 -1
- package/dest/contracts/slashing_proposer.js +0 -47
- package/dest/deploy_l1_contracts.d.ts +0 -21210
- package/dest/deploy_l1_contracts.d.ts.map +0 -1
- package/dest/deploy_l1_contracts.js +0 -687
- package/dest/eth_cheat_codes.d.ts +0 -147
- package/dest/eth_cheat_codes.d.ts.map +0 -1
- package/dest/eth_cheat_codes.js +0 -303
- package/dest/index.d.ts +0 -14
- package/dest/index.d.ts.map +0 -1
- package/dest/index.js +0 -13
- package/dest/l1_tx_utils.d.ts +0 -192
- package/dest/l1_tx_utils.d.ts.map +0 -1
- package/dest/l1_tx_utils.js +0 -641
- package/dest/l1_tx_utils_with_blobs.d.ts +0 -12
- package/dest/l1_tx_utils_with_blobs.d.ts.map +0 -1
- package/dest/l1_tx_utils_with_blobs.js +0 -64
- package/dest/test/delayed_tx_utils.d.ts +0 -8
- package/dest/test/delayed_tx_utils.d.ts.map +0 -1
- package/dest/test/delayed_tx_utils.js +0 -21
- package/dest/test/tx_delayer.d.ts +0 -25
- package/dest/test/tx_delayer.d.ts.map +0 -1
- package/dest/test/tx_delayer.js +0 -116
- package/src/contracts/forwarder.ts +0 -132
- package/src/contracts/slashing_proposer.ts +0 -51
- package/src/deploy_l1_contracts.ts +0 -948
- package/src/eth_cheat_codes.ts +0 -314
- package/src/index.ts +0 -13
- package/src/l1_tx_utils.ts +0 -847
- package/src/l1_tx_utils_with_blobs.ts +0 -86
- package/src/test/delayed_tx_utils.ts +0 -24
- package/src/test/tx_delayer.ts +0 -163
|
@@ -0,0 +1,639 @@
|
|
|
1
|
+
import { maxBigint } from '@aztec/foundation/bigint';
|
|
2
|
+
import { merge, pick } from '@aztec/foundation/collection';
|
|
3
|
+
import { InterruptError, TimeoutError } from '@aztec/foundation/error';
|
|
4
|
+
import { createLogger } from '@aztec/foundation/log';
|
|
5
|
+
import { retryUntil } from '@aztec/foundation/retry';
|
|
6
|
+
import { sleep } from '@aztec/foundation/sleep';
|
|
7
|
+
import { DateProvider } from '@aztec/foundation/timer';
|
|
8
|
+
import { RollupAbi } from '@aztec/l1-artifacts/RollupAbi';
|
|
9
|
+
import pickBy from 'lodash.pickby';
|
|
10
|
+
import { formatGwei, serializeTransaction } from 'viem';
|
|
11
|
+
import { formatViemError } from '../utils.js';
|
|
12
|
+
import { l1TxUtilsConfigMappings } from './config.js';
|
|
13
|
+
import { MAX_L1_TX_LIMIT } from './constants.js';
|
|
14
|
+
import { ReadOnlyL1TxUtils } from './readonly_l1_tx_utils.js';
|
|
15
|
+
import { createDelayer, wrapClientWithDelayer } from './tx_delayer.js';
|
|
16
|
+
import { DroppedTransactionError, TerminalTxUtilsState, TxUtilsState, UnknownMinedTxError } from './types.js';
|
|
17
|
+
const MAX_L1_TX_STATES = 32;
|
|
18
|
+
export class L1TxUtils extends ReadOnlyL1TxUtils {
|
|
19
|
+
client;
|
|
20
|
+
address;
|
|
21
|
+
signer;
|
|
22
|
+
store;
|
|
23
|
+
metrics;
|
|
24
|
+
txs;
|
|
25
|
+
/** Last nonce successfully sent to the chain. Used as a lower bound when a fallback RPC node returns a stale count. */ lastSentNonce;
|
|
26
|
+
/** Tx delayer for testing. Only set when enableDelayer config is true. */ delayer;
|
|
27
|
+
/** KZG instance for blob operations. */ kzg;
|
|
28
|
+
constructor(client, address, signer, logger = createLogger('ethereum:publisher'), dateProvider = new DateProvider(), config, debugMaxGasLimit = false, store, metrics, kzg, delayer){
|
|
29
|
+
super(client, logger, dateProvider, config, debugMaxGasLimit), this.client = client, this.address = address, this.signer = signer, this.store = store, this.metrics = metrics, this.txs = [];
|
|
30
|
+
this.kzg = kzg;
|
|
31
|
+
// Set up delayer: use provided one or create new
|
|
32
|
+
if (config?.enableDelayer && config?.ethereumSlotDuration) {
|
|
33
|
+
this.delayer = delayer ?? this.createDelayer({
|
|
34
|
+
ethereumSlotDuration: config.ethereumSlotDuration
|
|
35
|
+
}, logger.getBindings());
|
|
36
|
+
this.client = wrapClientWithDelayer(this.client, this.delayer);
|
|
37
|
+
if (config.txDelayerMaxInclusionTimeIntoSlot !== undefined) {
|
|
38
|
+
this.delayer.setMaxInclusionTimeIntoSlot(config.txDelayerMaxInclusionTimeIntoSlot);
|
|
39
|
+
}
|
|
40
|
+
} else if (delayer) {
|
|
41
|
+
// Delayer provided but enableDelayer not set — just store it without wrapping
|
|
42
|
+
logger.warn('Delayer provided but enableDelayer config is not set; delayer will not be used');
|
|
43
|
+
this.delayer = delayer;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
get state() {
|
|
47
|
+
return this.txs.at(-1)?.status ?? TxUtilsState.IDLE;
|
|
48
|
+
}
|
|
49
|
+
get lastMinedAtBlockNumber() {
|
|
50
|
+
const minedBlockNumbers = this.txs.map((tx)=>tx.receipt?.blockNumber).filter((bn)=>bn !== undefined);
|
|
51
|
+
return minedBlockNumbers.length === 0 ? undefined : maxBigint(...minedBlockNumbers);
|
|
52
|
+
}
|
|
53
|
+
async updateState(l1TxState, newState, l1Timestamp) {
|
|
54
|
+
const oldState = l1TxState.status;
|
|
55
|
+
l1TxState.status = newState;
|
|
56
|
+
const sender = this.getSenderAddress().toString();
|
|
57
|
+
this.logger.debug(`Tx state changed from ${TxUtilsState[oldState]} to ${TxUtilsState[newState]} for nonce ${l1TxState.nonce} account ${sender}`);
|
|
58
|
+
// Record metrics
|
|
59
|
+
if (newState === TxUtilsState.MINED && l1Timestamp !== undefined) {
|
|
60
|
+
this.metrics?.recordMinedTx(l1TxState, new Date(l1Timestamp));
|
|
61
|
+
} else if (newState === TxUtilsState.NOT_MINED) {
|
|
62
|
+
this.metrics?.recordDroppedTx(l1TxState);
|
|
63
|
+
// The tx was dropped: the chain nonce reverted to l1TxState.nonce, so our lower bound is
|
|
64
|
+
// no longer valid. Clear it so the next send fetches the real nonce from the chain.
|
|
65
|
+
if (this.lastSentNonce === l1TxState.nonce) {
|
|
66
|
+
this.lastSentNonce = undefined;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
// Update state in the store
|
|
70
|
+
await this.store?.saveState(sender, l1TxState).catch((err)=>this.logger.error('Failed to persist L1 tx state', err));
|
|
71
|
+
}
|
|
72
|
+
updateConfig(newConfig) {
|
|
73
|
+
this.config = merge(this.config, newConfig);
|
|
74
|
+
this.logger.info('Updated L1TxUtils config', pickBy(newConfig, (_, key)=>key in l1TxUtilsConfigMappings));
|
|
75
|
+
}
|
|
76
|
+
getSenderAddress() {
|
|
77
|
+
return this.address;
|
|
78
|
+
}
|
|
79
|
+
getSenderBalance() {
|
|
80
|
+
return this.client.getBalance({
|
|
81
|
+
address: this.getSenderAddress().toString()
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Rehydrates transaction states from the store and resumes monitoring for pending transactions.
|
|
86
|
+
* This should be called on startup to restore state and resume monitoring of any in-flight transactions.
|
|
87
|
+
*/ async loadStateAndResumeMonitoring() {
|
|
88
|
+
if (!this.store) {
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
const account = this.getSenderAddress().toString();
|
|
92
|
+
const loadedStates = await this.store.loadStates(account);
|
|
93
|
+
if (loadedStates.length === 0) {
|
|
94
|
+
this.logger.debug(`No states to rehydrate for account ${account}`);
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
// Clean up excess states if we have more than MAX_L1_TX_STATES
|
|
98
|
+
if (loadedStates.length > MAX_L1_TX_STATES) {
|
|
99
|
+
this.logger.warn(`Found ${loadedStates.length} tx states for account ${account}, pruning to most recent ${MAX_L1_TX_STATES}`);
|
|
100
|
+
// Keep only the most recent MAX_L1_TX_STATES
|
|
101
|
+
const statesToKeep = loadedStates.slice(-MAX_L1_TX_STATES);
|
|
102
|
+
const statesToDelete = loadedStates.slice(0, -MAX_L1_TX_STATES);
|
|
103
|
+
// Batch delete old states in a transaction for efficiency
|
|
104
|
+
const idsToDelete = statesToDelete.map((s)=>s.id);
|
|
105
|
+
await this.store.deleteState(account, ...idsToDelete);
|
|
106
|
+
this.txs = statesToKeep;
|
|
107
|
+
this.logger.info(`Cleaned up ${statesToDelete.length} old tx states, kept ${statesToKeep.length} for account ${account}`);
|
|
108
|
+
} else {
|
|
109
|
+
// Convert loaded states (which have id) to the txs format
|
|
110
|
+
this.txs = loadedStates;
|
|
111
|
+
this.logger.info(`Rehydrated ${loadedStates.length} tx states for account ${account}`);
|
|
112
|
+
}
|
|
113
|
+
// Find all pending states and resume monitoring
|
|
114
|
+
const pendingStates = this.txs.filter((state)=>!TerminalTxUtilsState.includes(state.status));
|
|
115
|
+
if (pendingStates.length === 0) {
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
this.logger.info(`Resuming monitoring for ${pendingStates.length} pending transactions for account ${account}`, {
|
|
119
|
+
txs: pendingStates.map((s)=>({
|
|
120
|
+
id: s.id,
|
|
121
|
+
nonce: s.nonce,
|
|
122
|
+
status: TxUtilsState[s.status]
|
|
123
|
+
}))
|
|
124
|
+
});
|
|
125
|
+
for (const state of pendingStates){
|
|
126
|
+
void this.monitorTransaction(state).catch((err)=>{
|
|
127
|
+
this.logger.error(`Error monitoring rehydrated tx with nonce ${state.nonce} for account ${account}`, formatViemError(err), {
|
|
128
|
+
nonce: state.nonce,
|
|
129
|
+
account
|
|
130
|
+
});
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
async signTransaction(txRequest) {
|
|
135
|
+
const signature = await this.signer(txRequest, this.getSenderAddress());
|
|
136
|
+
return serializeTransaction(txRequest, signature);
|
|
137
|
+
}
|
|
138
|
+
async prepareSignedTransaction(txData) {
|
|
139
|
+
const txRequest = await this.client.prepareTransactionRequest(txData);
|
|
140
|
+
return await this.signTransaction(txRequest);
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Sends a transaction with gas estimation and pricing
|
|
144
|
+
* @param request - The transaction request (to, data, value)
|
|
145
|
+
* @param gasConfig - Optional gas configuration
|
|
146
|
+
* @returns The transaction hash and parameters used
|
|
147
|
+
*/ async sendTransaction(request, gasConfigOverrides, blobInputs, stateChange = TxUtilsState.SENT) {
|
|
148
|
+
if (this.interrupted) {
|
|
149
|
+
throw new InterruptError(`Transaction sending is interrupted`);
|
|
150
|
+
}
|
|
151
|
+
try {
|
|
152
|
+
const gasConfig = merge(this.config, gasConfigOverrides);
|
|
153
|
+
const account = this.getSenderAddress().toString();
|
|
154
|
+
let gasLimit;
|
|
155
|
+
if (this.debugMaxGasLimit) {
|
|
156
|
+
gasLimit = MAX_L1_TX_LIMIT;
|
|
157
|
+
} else if (gasConfig.gasLimit) {
|
|
158
|
+
gasLimit = gasConfig.gasLimit;
|
|
159
|
+
} else {
|
|
160
|
+
gasLimit = await this.estimateGas(account, request, gasConfig);
|
|
161
|
+
}
|
|
162
|
+
this.logger.trace(`Computed gas limit ${gasLimit}`, {
|
|
163
|
+
gasLimit,
|
|
164
|
+
...request
|
|
165
|
+
});
|
|
166
|
+
const gasPrice = await this.getGasPrice(gasConfig, !!blobInputs);
|
|
167
|
+
if (this.interrupted) {
|
|
168
|
+
throw new InterruptError(`Transaction sending is interrupted`);
|
|
169
|
+
}
|
|
170
|
+
const now = new Date(await this.getL1Timestamp());
|
|
171
|
+
if (gasConfig.txTimeoutAt && now > gasConfig.txTimeoutAt) {
|
|
172
|
+
throw new TimeoutError(`Transaction timed out before sending (now ${now.toISOString()} > timeoutAt ${gasConfig.txTimeoutAt.toISOString()})`);
|
|
173
|
+
}
|
|
174
|
+
const chainNonce = await this.client.getTransactionCount({
|
|
175
|
+
address: account,
|
|
176
|
+
blockTag: 'pending'
|
|
177
|
+
});
|
|
178
|
+
// If a fallback RPC node returns a stale count (lower than what we last sent), use our
|
|
179
|
+
// local lower bound to avoid sending a duplicate of an already-pending transaction.
|
|
180
|
+
const nonce = this.lastSentNonce !== undefined && chainNonce <= this.lastSentNonce ? this.lastSentNonce + 1 : chainNonce;
|
|
181
|
+
const baseState = {
|
|
182
|
+
request,
|
|
183
|
+
gasLimit,
|
|
184
|
+
blobInputs,
|
|
185
|
+
gasPrice,
|
|
186
|
+
nonce
|
|
187
|
+
};
|
|
188
|
+
const txData = this.makeTxData(baseState, {
|
|
189
|
+
isCancelTx: false
|
|
190
|
+
});
|
|
191
|
+
// Send the new tx
|
|
192
|
+
const signedRequest = await this.prepareSignedTransaction(txData);
|
|
193
|
+
const txHash = await this.client.sendRawTransaction({
|
|
194
|
+
serializedTransaction: signedRequest
|
|
195
|
+
});
|
|
196
|
+
// Update after tx is sent successfully
|
|
197
|
+
this.lastSentNonce = nonce;
|
|
198
|
+
// Create the new state for monitoring
|
|
199
|
+
const l1TxState = {
|
|
200
|
+
...baseState,
|
|
201
|
+
id: await this.store?.consumeNextStateId(account) ?? Math.max(...this.txs.map((tx)=>tx.id), 0),
|
|
202
|
+
txHashes: [
|
|
203
|
+
txHash
|
|
204
|
+
],
|
|
205
|
+
cancelTxHashes: [],
|
|
206
|
+
status: TxUtilsState.IDLE,
|
|
207
|
+
txConfigOverrides: gasConfigOverrides ?? {},
|
|
208
|
+
sentAtL1Ts: now,
|
|
209
|
+
lastSentAtL1Ts: now
|
|
210
|
+
};
|
|
211
|
+
// And persist it
|
|
212
|
+
await this.updateState(l1TxState, stateChange);
|
|
213
|
+
await this.store?.saveBlobs(account, l1TxState.id, blobInputs);
|
|
214
|
+
this.txs.push(l1TxState);
|
|
215
|
+
// Clean up stale states
|
|
216
|
+
if (this.txs.length > MAX_L1_TX_STATES) {
|
|
217
|
+
const removed = this.txs.shift();
|
|
218
|
+
if (removed && this.store) {
|
|
219
|
+
await this.store.deleteState(account, removed.id);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
this.logger.info(`Sent L1 transaction ${txHash}`, {
|
|
223
|
+
to: request.to,
|
|
224
|
+
value: request.value,
|
|
225
|
+
nonce,
|
|
226
|
+
account,
|
|
227
|
+
sentAt: now,
|
|
228
|
+
gasLimit,
|
|
229
|
+
maxFeePerGas: formatGwei(gasPrice.maxFeePerGas),
|
|
230
|
+
maxPriorityFeePerGas: formatGwei(gasPrice.maxPriorityFeePerGas),
|
|
231
|
+
...gasPrice.maxFeePerBlobGas && {
|
|
232
|
+
maxFeePerBlobGas: formatGwei(gasPrice.maxFeePerBlobGas)
|
|
233
|
+
},
|
|
234
|
+
isBlobTx: !!blobInputs,
|
|
235
|
+
txConfig: gasConfigOverrides
|
|
236
|
+
});
|
|
237
|
+
return {
|
|
238
|
+
txHash,
|
|
239
|
+
state: l1TxState
|
|
240
|
+
};
|
|
241
|
+
} catch (err) {
|
|
242
|
+
const viemError = formatViemError(err, request.abi);
|
|
243
|
+
this.logger.error(`Failed to send L1 transaction: ${viemError.message}`, viemError, {
|
|
244
|
+
request: pick(request, 'to', 'value')
|
|
245
|
+
});
|
|
246
|
+
throw viemError;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
async tryGetTxReceipt(txHashes, nonce, isCancelTx) {
|
|
250
|
+
for (const hash of txHashes){
|
|
251
|
+
try {
|
|
252
|
+
const receipt = await this.client.getTransactionReceipt({
|
|
253
|
+
hash
|
|
254
|
+
});
|
|
255
|
+
if (receipt) {
|
|
256
|
+
const account = this.getSenderAddress().toString();
|
|
257
|
+
const what = isCancelTx ? 'Cancellation L1 transaction' : 'L1 transaction';
|
|
258
|
+
if (receipt.status === 'reverted') {
|
|
259
|
+
this.logger.warn(`${what} ${hash} with nonce ${nonce} reverted`, {
|
|
260
|
+
receipt,
|
|
261
|
+
nonce,
|
|
262
|
+
account
|
|
263
|
+
});
|
|
264
|
+
} else {
|
|
265
|
+
this.logger.info(`${what} ${hash} with nonce ${nonce} mined`, {
|
|
266
|
+
receipt,
|
|
267
|
+
nonce,
|
|
268
|
+
account
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
return receipt;
|
|
272
|
+
}
|
|
273
|
+
} catch (err) {
|
|
274
|
+
if (err instanceof Error && err.name === 'TransactionReceiptNotFoundError') {
|
|
275
|
+
continue;
|
|
276
|
+
} else {
|
|
277
|
+
this.logger.error(`Error getting receipt for tx ${hash}`, err);
|
|
278
|
+
continue;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* Returns whether a given tx should be considered as timed out and no longer monitored.
|
|
285
|
+
* Relies on the txTimeout setting for user txs, and on the txCancellationTimeout for cancel txs.
|
|
286
|
+
* @remarks We check against the latestBlockTimestamp as opposed to the current time to avoid a race condition where
|
|
287
|
+
* the tx is mined in a block with the same timestamp as txTimeoutAt, but our execution node has not yet processed it,
|
|
288
|
+
* or the loop here has not yet checked the tx before that timeout.
|
|
289
|
+
*/ isTxTimedOut(state, l1Timestamp) {
|
|
290
|
+
const account = this.getSenderAddress().toString();
|
|
291
|
+
const { nonce } = state;
|
|
292
|
+
const txConfig = merge(this.config, state.txConfigOverrides);
|
|
293
|
+
const { txTimeoutAt, txTimeoutMs, maxSpeedUpAttempts } = txConfig;
|
|
294
|
+
const isCancelTx = state.cancelTxHashes.length > 0;
|
|
295
|
+
this.logger.trace(`Tx timeout check for ${account} with nonce ${nonce}`, {
|
|
296
|
+
nonce,
|
|
297
|
+
account,
|
|
298
|
+
l1Timestamp,
|
|
299
|
+
lastAttemptSent: state.lastSentAtL1Ts.getTime(),
|
|
300
|
+
initialTxTime: state.sentAtL1Ts.getTime(),
|
|
301
|
+
now: this.dateProvider.now(),
|
|
302
|
+
txTimeoutAt,
|
|
303
|
+
txTimeoutMs,
|
|
304
|
+
interrupted: this.interrupted
|
|
305
|
+
});
|
|
306
|
+
if (this.interrupted) {
|
|
307
|
+
this.logger.warn(`Tx monitoring interrupted during nonce ${nonce} for ${account} check`, {
|
|
308
|
+
nonce,
|
|
309
|
+
account
|
|
310
|
+
});
|
|
311
|
+
return true;
|
|
312
|
+
}
|
|
313
|
+
if (isCancelTx) {
|
|
314
|
+
// Note that we check against the lastSentAt time for cancellations, since we time them out
|
|
315
|
+
// after the last attempt to submit them, not the initial time.
|
|
316
|
+
const attempts = state.cancelTxHashes.length;
|
|
317
|
+
return attempts > maxSpeedUpAttempts && state.lastSentAtL1Ts.getTime() + txConfig.txCancellationFinalTimeoutMs <= l1Timestamp;
|
|
318
|
+
}
|
|
319
|
+
return txTimeoutAt !== undefined && l1Timestamp >= txTimeoutAt.getTime() || txTimeoutMs !== undefined && txTimeoutMs > 0 && l1Timestamp - state.sentAtL1Ts.getTime() >= txTimeoutMs;
|
|
320
|
+
}
|
|
321
|
+
/**
|
|
322
|
+
* Monitors a transaction until completion, handling speed-ups if needed
|
|
323
|
+
*/ async monitorTransaction(state) {
|
|
324
|
+
const { nonce, gasLimit, blobInputs, txConfigOverrides: gasConfigOverrides } = state;
|
|
325
|
+
const gasConfig = merge(this.config, gasConfigOverrides);
|
|
326
|
+
const { maxSpeedUpAttempts, stallTimeMs } = gasConfig;
|
|
327
|
+
const isCancelTx = state.cancelTxHashes.length > 0;
|
|
328
|
+
const txHashes = isCancelTx ? state.cancelTxHashes : state.txHashes;
|
|
329
|
+
const isBlobTx = !!blobInputs;
|
|
330
|
+
const account = this.getSenderAddress().toString();
|
|
331
|
+
const initialTxHash = txHashes[0];
|
|
332
|
+
let currentTxHash = txHashes.at(-1);
|
|
333
|
+
let l1Timestamp;
|
|
334
|
+
while(true){
|
|
335
|
+
l1Timestamp = await this.getL1Timestamp();
|
|
336
|
+
try {
|
|
337
|
+
const timePassed = l1Timestamp - state.lastSentAtL1Ts.getTime();
|
|
338
|
+
const [currentNonce, pendingNonce] = await Promise.all([
|
|
339
|
+
this.client.getTransactionCount({
|
|
340
|
+
address: account,
|
|
341
|
+
blockTag: 'latest'
|
|
342
|
+
}),
|
|
343
|
+
this.client.getTransactionCount({
|
|
344
|
+
address: account,
|
|
345
|
+
blockTag: 'pending'
|
|
346
|
+
})
|
|
347
|
+
]);
|
|
348
|
+
// If the current nonce on our account is greater than our transaction's nonce then a tx with the same nonce has been mined.
|
|
349
|
+
if (currentNonce > nonce) {
|
|
350
|
+
// We try getting the receipt twice, since sometimes anvil fails to return it if the tx has just been mined
|
|
351
|
+
const receipt = await this.tryGetTxReceipt(state.cancelTxHashes, nonce, true) ?? await this.tryGetTxReceipt(state.txHashes, nonce, false) ?? await sleep(500) ?? await this.tryGetTxReceipt(state.cancelTxHashes, nonce, true) ?? await this.tryGetTxReceipt(state.txHashes, nonce, false);
|
|
352
|
+
if (receipt) {
|
|
353
|
+
state.receipt = receipt;
|
|
354
|
+
await this.updateState(state, TxUtilsState.MINED, l1Timestamp);
|
|
355
|
+
return receipt;
|
|
356
|
+
}
|
|
357
|
+
// If we get here then we have checked all of our tx versions and not found anything.
|
|
358
|
+
// We should consider the nonce as MINED
|
|
359
|
+
await this.updateState(state, TxUtilsState.MINED, l1Timestamp);
|
|
360
|
+
throw new UnknownMinedTxError(nonce, account);
|
|
361
|
+
}
|
|
362
|
+
// If this is a cancel tx and its nonce is no longer on the mempool, we consider it dropped and stop monitoring
|
|
363
|
+
// If it is a regular tx, we let the loop speed it up after the stall time
|
|
364
|
+
if (isCancelTx && pendingNonce <= nonce && timePassed >= gasConfig.txUnseenConsideredDroppedMs) {
|
|
365
|
+
this.logger.warn(`Cancellation tx with nonce ${nonce} for account ${account} has been dropped from the visible mempool`, {
|
|
366
|
+
nonce,
|
|
367
|
+
account,
|
|
368
|
+
pendingNonce,
|
|
369
|
+
timePassed
|
|
370
|
+
});
|
|
371
|
+
await this.updateState(state, TxUtilsState.NOT_MINED);
|
|
372
|
+
throw new DroppedTransactionError(nonce, account);
|
|
373
|
+
}
|
|
374
|
+
// Break if the tx has timed out (ie expired)
|
|
375
|
+
if (this.isTxTimedOut(state, l1Timestamp)) {
|
|
376
|
+
break;
|
|
377
|
+
}
|
|
378
|
+
// Speed up the transaction if it appears to be stuck (exceeded stall time and still have retry attempts)
|
|
379
|
+
const attempts = txHashes.length;
|
|
380
|
+
if (timePassed >= stallTimeMs && attempts <= maxSpeedUpAttempts) {
|
|
381
|
+
const newGasPrice = await this.getGasPrice(gasConfig, isBlobTx, attempts, state.gasPrice);
|
|
382
|
+
state.gasPrice = newGasPrice;
|
|
383
|
+
this.logger.debug(`Tx ${currentTxHash} with nonce ${nonce} from ${account} appears stuck. ` + `Attempting speed-up ${attempts}/${maxSpeedUpAttempts} ` + `with new priority fee ${formatGwei(newGasPrice.maxPriorityFeePerGas)} gwei.`, {
|
|
384
|
+
account,
|
|
385
|
+
nonce,
|
|
386
|
+
maxFeePerGas: formatGwei(newGasPrice.maxFeePerGas),
|
|
387
|
+
maxPriorityFeePerGas: formatGwei(newGasPrice.maxPriorityFeePerGas),
|
|
388
|
+
...newGasPrice.maxFeePerBlobGas && {
|
|
389
|
+
maxFeePerBlobGas: formatGwei(newGasPrice.maxFeePerBlobGas)
|
|
390
|
+
}
|
|
391
|
+
});
|
|
392
|
+
const txData = this.makeTxData(state, {
|
|
393
|
+
isCancelTx
|
|
394
|
+
});
|
|
395
|
+
const signedRequest = await this.prepareSignedTransaction(txData);
|
|
396
|
+
const newHash = await this.client.sendRawTransaction({
|
|
397
|
+
serializedTransaction: signedRequest
|
|
398
|
+
});
|
|
399
|
+
this.logger.verbose(`Sent L1 speed-up tx ${newHash} replacing ${currentTxHash} for nonce ${nonce} from ${account}`, {
|
|
400
|
+
nonce,
|
|
401
|
+
account,
|
|
402
|
+
gasLimit,
|
|
403
|
+
maxFeePerGas: formatGwei(newGasPrice.maxFeePerGas),
|
|
404
|
+
maxPriorityFeePerGas: formatGwei(newGasPrice.maxPriorityFeePerGas),
|
|
405
|
+
txConfig: state.txConfigOverrides,
|
|
406
|
+
...newGasPrice.maxFeePerBlobGas && {
|
|
407
|
+
maxFeePerBlobGas: formatGwei(newGasPrice.maxFeePerBlobGas)
|
|
408
|
+
}
|
|
409
|
+
});
|
|
410
|
+
currentTxHash = newHash;
|
|
411
|
+
txHashes.push(currentTxHash);
|
|
412
|
+
state.lastSentAtL1Ts = new Date(l1Timestamp);
|
|
413
|
+
await this.updateState(state, isCancelTx ? TxUtilsState.CANCELLED : TxUtilsState.SPEED_UP);
|
|
414
|
+
await sleep(gasConfig.checkIntervalMs);
|
|
415
|
+
continue;
|
|
416
|
+
}
|
|
417
|
+
this.logger.debug(`Tx ${currentTxHash} from ${account} with nonce ${nonce} still pending after ${timePassed}ms`, {
|
|
418
|
+
account,
|
|
419
|
+
nonce,
|
|
420
|
+
pendingNonce,
|
|
421
|
+
attempts,
|
|
422
|
+
timePassed,
|
|
423
|
+
isBlobTx,
|
|
424
|
+
isCancelTx,
|
|
425
|
+
...pick(state.gasPrice, 'maxFeePerGas', 'maxPriorityFeePerGas', 'maxFeePerBlobGas'),
|
|
426
|
+
...pick(gasConfig, 'txUnseenConsideredDroppedMs', 'txCancellationFinalTimeoutMs', 'maxSpeedUpAttempts', 'stallTimeMs', 'txTimeoutAt', 'txTimeoutMs')
|
|
427
|
+
});
|
|
428
|
+
await sleep(gasConfig.checkIntervalMs);
|
|
429
|
+
} catch (err) {
|
|
430
|
+
if (err instanceof DroppedTransactionError || err instanceof UnknownMinedTxError) {
|
|
431
|
+
throw err;
|
|
432
|
+
}
|
|
433
|
+
const viemError = formatViemError(err);
|
|
434
|
+
this.logger.error(`Error while monitoring L1 tx ${currentTxHash}`, viemError, {
|
|
435
|
+
nonce,
|
|
436
|
+
account
|
|
437
|
+
});
|
|
438
|
+
await sleep(gasConfig.checkIntervalMs);
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
// Oh no, the transaction has timed out!
|
|
442
|
+
if (isCancelTx || !gasConfig.cancelTxOnTimeout) {
|
|
443
|
+
await this.updateState(state, TxUtilsState.NOT_MINED);
|
|
444
|
+
} else {
|
|
445
|
+
// Otherwise we fire the cancellation without awaiting to avoid blocking the caller,
|
|
446
|
+
// and monitor it in the background so we can speed it up as needed.
|
|
447
|
+
void this.attemptTxCancellation(state).catch(async (err)=>{
|
|
448
|
+
await this.updateState(state, TxUtilsState.NOT_MINED);
|
|
449
|
+
this.logger.error(`Failed to send cancellation for timed out tx ${initialTxHash} with nonce ${nonce}`, err, {
|
|
450
|
+
account,
|
|
451
|
+
nonce,
|
|
452
|
+
initialTxHash
|
|
453
|
+
});
|
|
454
|
+
});
|
|
455
|
+
}
|
|
456
|
+
const what = isCancelTx ? 'Cancellation L1' : 'L1';
|
|
457
|
+
this.logger.warn(`${what} transaction ${initialTxHash} with nonce ${nonce} from ${account} timed out`, {
|
|
458
|
+
initialTxHash,
|
|
459
|
+
currentTxHash,
|
|
460
|
+
nonce,
|
|
461
|
+
account,
|
|
462
|
+
txTimeoutAt: gasConfig.txTimeoutAt?.getTime(),
|
|
463
|
+
txTimeoutMs: gasConfig.txTimeoutMs,
|
|
464
|
+
txInitialTime: state.sentAtL1Ts.getTime(),
|
|
465
|
+
l1Timestamp,
|
|
466
|
+
now: this.dateProvider.now(),
|
|
467
|
+
attempts: txHashes.length - 1,
|
|
468
|
+
isInterrupted: this.interrupted
|
|
469
|
+
});
|
|
470
|
+
throw new TimeoutError(`L1 transaction ${initialTxHash} timed out`);
|
|
471
|
+
}
|
|
472
|
+
/**
|
|
473
|
+
* Creates tx data to be signed by viem signTransaction method, using the state as input.
|
|
474
|
+
* If isCancelTx is true, creates a 0-value tx to self with 21k gas and no data instead,
|
|
475
|
+
* and an empty blob input if the original tx also had blobs.
|
|
476
|
+
*/ makeTxData(state, opts) {
|
|
477
|
+
const { request, gasLimit, blobInputs, gasPrice, nonce } = state;
|
|
478
|
+
const isBlobTx = blobInputs !== undefined;
|
|
479
|
+
const baseTxOpts = {
|
|
480
|
+
nonce,
|
|
481
|
+
...pick(gasPrice, 'maxFeePerGas', 'maxPriorityFeePerGas')
|
|
482
|
+
};
|
|
483
|
+
if (opts.isCancelTx) {
|
|
484
|
+
const baseTxData = {
|
|
485
|
+
to: this.getSenderAddress().toString(),
|
|
486
|
+
value: 0n,
|
|
487
|
+
data: '0x',
|
|
488
|
+
gas: 21_000n,
|
|
489
|
+
...baseTxOpts
|
|
490
|
+
};
|
|
491
|
+
return isBlobTx ? {
|
|
492
|
+
...baseTxData,
|
|
493
|
+
...this.makeEmptyBlobInputs(gasPrice.maxFeePerBlobGas)
|
|
494
|
+
} : baseTxData;
|
|
495
|
+
}
|
|
496
|
+
const baseTxData = {
|
|
497
|
+
...request,
|
|
498
|
+
...baseTxOpts,
|
|
499
|
+
gas: gasLimit
|
|
500
|
+
};
|
|
501
|
+
return blobInputs ? {
|
|
502
|
+
...baseTxData,
|
|
503
|
+
...blobInputs,
|
|
504
|
+
maxFeePerBlobGas: gasPrice.maxFeePerBlobGas
|
|
505
|
+
} : baseTxData;
|
|
506
|
+
}
|
|
507
|
+
/** Returns when all monitor loops have stopped. */ async waitMonitoringStopped(timeoutSeconds = 10) {
|
|
508
|
+
const account = this.getSenderAddress().toString();
|
|
509
|
+
await retryUntil(()=>this.txs.every((tx)=>TerminalTxUtilsState.includes(tx.status)), `monitoring stopped for ${account}`, timeoutSeconds, 0.1).catch(()=>this.logger.warn(`Timeout waiting for monitoring loops to stop for ${account}`));
|
|
510
|
+
}
|
|
511
|
+
/**
|
|
512
|
+
* Sends a transaction and monitors it until completion
|
|
513
|
+
* @param request - The transaction request (to, data, value)
|
|
514
|
+
* @param gasConfig - Optional gas configuration
|
|
515
|
+
* @returns The receipt of the successful transaction
|
|
516
|
+
*/ async sendAndMonitorTransaction(request, gasConfig, blobInputs) {
|
|
517
|
+
const { state } = await this.sendTransaction(request, gasConfig, blobInputs);
|
|
518
|
+
const receipt = await this.monitorTransaction(state);
|
|
519
|
+
return {
|
|
520
|
+
receipt,
|
|
521
|
+
state
|
|
522
|
+
};
|
|
523
|
+
}
|
|
524
|
+
async simulate(request, _blockOverrides = {}, stateOverrides = [], abi = RollupAbi, _gasConfig) {
|
|
525
|
+
const blockOverrides = {
|
|
526
|
+
..._blockOverrides
|
|
527
|
+
};
|
|
528
|
+
const gasConfig = merge(this.config, _gasConfig);
|
|
529
|
+
const gasPrice = await this.getGasPrice(gasConfig, false);
|
|
530
|
+
const call = {
|
|
531
|
+
to: request.to,
|
|
532
|
+
data: request.data,
|
|
533
|
+
from: request.from ?? this.getSenderAddress().toString(),
|
|
534
|
+
maxFeePerGas: gasPrice.maxFeePerGas,
|
|
535
|
+
maxPriorityFeePerGas: gasPrice.maxPriorityFeePerGas,
|
|
536
|
+
gas: request.gas ?? MAX_L1_TX_LIMIT
|
|
537
|
+
};
|
|
538
|
+
if (!request.gas && !gasConfig.ignoreBlockGasLimit) {
|
|
539
|
+
// MAX_L1_TX_LIMIT is set as call.gas, ensure block gasLimit is sufficient
|
|
540
|
+
blockOverrides.gasLimit = MAX_L1_TX_LIMIT;
|
|
541
|
+
}
|
|
542
|
+
return this._simulate(call, blockOverrides, stateOverrides, gasConfig, abi);
|
|
543
|
+
}
|
|
544
|
+
/**
|
|
545
|
+
* Attempts to cancel a transaction by sending a 0-value tx to self with same nonce but higher gas prices
|
|
546
|
+
* Only sends the cancellation if the original tx is still pending, not if it was dropped
|
|
547
|
+
* @returns The hash of the cancellation transaction
|
|
548
|
+
*/ async attemptTxCancellation(state) {
|
|
549
|
+
const isBlobTx = state.blobInputs !== undefined;
|
|
550
|
+
const { nonce, gasPrice: previousGasPrice } = state;
|
|
551
|
+
const account = this.getSenderAddress().toString();
|
|
552
|
+
// Do not send cancellation if interrupted
|
|
553
|
+
if (this.interrupted) {
|
|
554
|
+
this.logger.warn(`Not sending cancellation for L1 tx from account ${account} with nonce ${nonce} as interrupted`, {
|
|
555
|
+
nonce,
|
|
556
|
+
account
|
|
557
|
+
});
|
|
558
|
+
await this.updateState(state, TxUtilsState.NOT_MINED);
|
|
559
|
+
return;
|
|
560
|
+
}
|
|
561
|
+
// Check if the original tx is still pending
|
|
562
|
+
const currentNonce = await this.client.getTransactionCount({
|
|
563
|
+
address: account,
|
|
564
|
+
blockTag: 'pending'
|
|
565
|
+
});
|
|
566
|
+
if (currentNonce < nonce) {
|
|
567
|
+
this.logger.verbose(`Not sending cancellation for L1 tx from account ${account} with nonce ${nonce} as it is dropped`, {
|
|
568
|
+
nonce,
|
|
569
|
+
account,
|
|
570
|
+
currentNonce
|
|
571
|
+
});
|
|
572
|
+
await this.updateState(state, TxUtilsState.NOT_MINED);
|
|
573
|
+
return;
|
|
574
|
+
}
|
|
575
|
+
// Get gas price with higher priority fee for cancellation
|
|
576
|
+
const cancelGasPrice = await this.getGasPrice({
|
|
577
|
+
...this.config,
|
|
578
|
+
// Use high bump for cancellation to ensure it replaces the original tx
|
|
579
|
+
priorityFeeRetryBumpPercentage: 150
|
|
580
|
+
}, isBlobTx, state.txHashes.length, previousGasPrice);
|
|
581
|
+
const { maxFeePerGas, maxPriorityFeePerGas, maxFeePerBlobGas } = cancelGasPrice;
|
|
582
|
+
this.logger.verbose(`Attempting to cancel L1 ${isBlobTx ? 'blob' : 'vanilla'} transaction from account ${account} with nonce ${nonce} after time out`, {
|
|
583
|
+
maxFeePerGas: formatGwei(maxFeePerGas),
|
|
584
|
+
maxPriorityFeePerGas: formatGwei(maxPriorityFeePerGas),
|
|
585
|
+
...maxFeePerBlobGas && {
|
|
586
|
+
maxFeePerBlobGas: formatGwei(maxFeePerBlobGas)
|
|
587
|
+
}
|
|
588
|
+
});
|
|
589
|
+
// Send 0-value tx to self with higher gas price
|
|
590
|
+
state.gasPrice = cancelGasPrice;
|
|
591
|
+
state.lastSentAtL1Ts = new Date(await this.getL1Timestamp());
|
|
592
|
+
const txData = this.makeTxData(state, {
|
|
593
|
+
isCancelTx: true
|
|
594
|
+
});
|
|
595
|
+
const signedRequest = await this.prepareSignedTransaction(txData);
|
|
596
|
+
const cancelTxHash = await this.client.sendRawTransaction({
|
|
597
|
+
serializedTransaction: signedRequest
|
|
598
|
+
});
|
|
599
|
+
state.cancelTxHashes.push(cancelTxHash);
|
|
600
|
+
await this.updateState(state, TxUtilsState.CANCELLED);
|
|
601
|
+
this.logger.warn(`Sent cancellation tx ${cancelTxHash} for timed out tx from ${account} with nonce ${nonce}`, {
|
|
602
|
+
nonce,
|
|
603
|
+
cancelGasPrice,
|
|
604
|
+
isBlobTx,
|
|
605
|
+
txHashes: state.txHashes
|
|
606
|
+
});
|
|
607
|
+
// Do not await the cancel tx to be mined
|
|
608
|
+
void this.monitorTransaction(state).catch((err)=>{
|
|
609
|
+
this.logger.error(`Failed to mine cancellation tx ${cancelTxHash} for nonce ${nonce} account ${account}`, err, {
|
|
610
|
+
nonce,
|
|
611
|
+
account,
|
|
612
|
+
cancelTxHash
|
|
613
|
+
});
|
|
614
|
+
});
|
|
615
|
+
}
|
|
616
|
+
/** Returns L1 timestamps in milliseconds */ async getL1Timestamp() {
|
|
617
|
+
const { timestamp } = await this.client.getBlock({
|
|
618
|
+
blockTag: 'latest',
|
|
619
|
+
includeTransactions: false
|
|
620
|
+
});
|
|
621
|
+
return Number(timestamp) * 1000;
|
|
622
|
+
}
|
|
623
|
+
/** Makes empty blob inputs for the cancellation tx. */ makeEmptyBlobInputs(maxFeePerBlobGas) {
|
|
624
|
+
if (!this.kzg) {
|
|
625
|
+
throw new Error('Cannot make empty blob inputs for cancellation without kzg');
|
|
626
|
+
}
|
|
627
|
+
const blobData = new Uint8Array(131072).fill(0);
|
|
628
|
+
return {
|
|
629
|
+
blobs: [
|
|
630
|
+
blobData
|
|
631
|
+
],
|
|
632
|
+
kzg: this.kzg,
|
|
633
|
+
maxFeePerBlobGas
|
|
634
|
+
};
|
|
635
|
+
}
|
|
636
|
+
/** Creates a new delayer instance. */ createDelayer(opts, bindings) {
|
|
637
|
+
return createDelayer(this.dateProvider, opts, bindings);
|
|
638
|
+
}
|
|
639
|
+
}
|