본문으로 건너뛰기

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, fee payer addresses, and their *private keys**

Set up the provider with the specified kaia Baobab testnet URL. A provider in web3 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 with privateKeyToAccount

Define a FeeDelegatedCancel transaction with params: type: TxType.FeeDelegatedCancel, from: senderAddr

Sign the transaction with the sender account

Convert the fee payer's private key to an account

Sign the transaction as a fee payer

Send the signed transaction and log the receipt

TxFeeDelegatedCancel.js

const { KlaytnWeb3, TxType, parseTransaction } = require("@kaiachain/web3js-ext");
const { Web3 } = require("web3");
const senderAddr = "0xa2a8854b1802d8cd5de631e690817c253d6a9153";
const senderPriv = "0x0e4ca6d38096ad99324de0dde108587e5d7c600165ae4cd6c2462c597458c2b8";
const feePayerAddr = "0xcb0eb737dfda52756495a5e08a9b37aab3b271da";
const feePayerPriv = "0x9435261ed483b6efa3886d6ad9f64c12078a0e28d8d80715c773e16fc000cff4";
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.FeeDelegatedCancel,
from: senderAddr,
};
const signResult1 = await senderAccount.signTransaction(tx);
console.log("senderRawTx", signResult1.rawTransaction);
console.log("senderTx", parseTransaction(signResult1.rawTransaction));
// Next step is usually done in the backend by the service provider.
// But for the sake of demonstration, feePayer signature is done here.
const feePayerAccount = web3.eth.accounts.privateKeyToAccount(feePayerPriv);
const signResult2 = await feePayerAccount.signTransactionAsFeePayer(signResult1.rawTransaction);
console.log("rawTx", signResult2.rawTransaction);
console.log("tx", parseTransaction(signResult2.rawTransaction));
const receipt = await web3.eth.sendSignedTransaction(signResult2.rawTransaction);
console.log("receipt", receipt);
}
main();

output

❯ js TxFeeDelegatedCancel.js
senderTxHashRLP 0x39f86b8203ba850ba43b74008302059494a2a8854b1802d8cd5de631e690817c253d6a9153f847f8458207f6a00a348d88278e74688124d4cd3996eed20fba6d5b3e10203d2d52395aab8abfbca031a4213782de0ccaed08a5b08e8e1294e2c8e85f4d758688f96c3e58373c3fed
signedTx 0x8b7fa96dd12a54c077020ddbfdb4114254312bfdbb361cf9479610afe1ba381c
receipt {
blockHash: '0xbb38361be83e6af540b71875824e05a8be986209c0c083eafee07ca35e811334',
blockNumber: 148744957n,
cumulativeGasUsed: 179526n,
effectiveGasPrice: 25000000000n,
from: '0xa2a8854b1802d8cd5de631e690817c253d6a9153',
gasUsed: 31000n,
logs: [],
logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
status: 1n,
to: '0xa2a8854b1802d8cd5de631e690817c253d6a9153',
transactionHash: '0x8b7fa96dd12a54c077020ddbfdb4114254312bfdbb361cf9479610afe1ba381c',
transactionIndex: 1n,
type: 0n
}