본문으로 건너뛰기

Cancel

TxTypeCancel cancels the execution of the transaction with the same nonce in the transaction pool. This transaction type is useful when a submitted transaction seems unprocessed for a certain amount of time.

Import the web3 and @kaiachain/web3js-ext packages to add kaia features on web3

Define sender address and sender private key

Set up the provider with the specified kaia Baobab testnet URL. A provider in web3js is a read-only abstraction to access the blockchain data.

Also, you can change the provider URL from baobab to allthatnode

Create a KlaytnWeb3 instance using the provider

Convert the sender's private key to an account using web3.eth.accounts.privateKeyToAccount

Define a transaction object for the Cancel transaction, params type: TxType.Cancel and from: senderAddr are required

Sign the transaction with the sender's account

Send the signed transaction to blockchain and print the receipt

TxCancelType.js

const { KlaytnWeb3, TxType } = require("@kaiachain/web3js-ext");
const { Web3 } = require("web3");
const senderAddr = "0xa2a8854b1802d8cd5de631e690817c253d6a9153";
const senderPriv = "0x0e4ca6d38096ad99324de0dde108587e5d7c600165ae4cd6c2462c597458c2b8";
async function main() {
const provider = new Web3.providers.HttpProvider("https://public-en-kairos.node.kaia.io");
const web3 = new KlaytnWeb3(provider);
const senderAccount = web3.eth.accounts.privateKeyToAccount(senderPriv);
const tx = {
type: TxType.Cancel,
from: senderAddr,
};
const signResult = await senderAccount.signTransaction(tx);
console.log("rawTx", signResult.rawTransaction);
const receipt = await web3.eth.sendSignedTransaction(signResult.rawTransaction);
console.log("receipt", receipt);
}
main();

output

❯ js TxCancelType.js
signedTx 0xe9a22708a547b5db4b1787603bdeed4b530b83ca2ba277dffba265061e675896
receipt {
blockHash: '0x8d3772ae12e4ef79b22631a10d02b2b731e3b099327fd58367e3374639aed4ca',
blockNumber: 148742706n,
cumulativeGasUsed: 21000n,
effectiveGasPrice: 25000000000n,
from: '0xa2a8854b1802d8cd5de631e690817c253d6a9153',
gasUsed: 21000n,
logs: [],
logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
status: 1n,
to: '0xa2a8854b1802d8cd5de631e690817c253d6a9153',
transactionHash: '0xe9a22708a547b5db4b1787603bdeed4b530b83ca2ba277dffba265061e675896',
transactionIndex: 0n,
type: 0n
}