Cancel
TxTypeCancel 取消事务池中具有相同 nonce 的事务的执行。 当已提交的交易在一定时间内似乎未得到处理时,这种交易类型就很有用。
导入 web3 和 @kaiachain/web3js-ext 软件包,在 web3 上添加 kaia 功能
定义发件人、缴费人地址,以及他们的(私钥)(
使用指定的 kairos 测试网 URL 设置提供程序。 web3 中的提供者是访问区块链数据的只读抽象。
此外,您还可以将提供商 URL 从 kairos 更改为 quicknode
使用提供程序创建 "KlaytnWeb3 "实例
使用 privateKeyToAccount
将发件人的私人密钥转换为一个账户
Define a FeeDelegatedCancel transaction with params: type: TxType.FeeDelegatedCancel
, from: senderAddr
用发送方账户签署交易
将缴费人的私人密钥转换为账户
作为付费者签署交易
发送已签名的交易并记录收据
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.jssenderTxHashRLP 0x39f86b8203ba850ba43b74008302059494a2a8854b1802d8cd5de631e690817c253d6a9153f847f8458207f6a00a348d88278e74688124d4cd3996eed20fba6d5b3e10203d2d52395aab8abfbca031a4213782de0ccaed08a5b08e8e1294e2c8e85f4d758688f96c3e58373c3fedsignedTx 0x8b7fa96dd12a54c077020ddbfdb4114254312bfdbb361cf9479610afe1ba381creceipt { 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}