跳至主要內容
本頁面使用機器翻譯自英語,可能包含錯誤或不清楚的語言。如需最準確的信息,請參閱英文原文。由於更新頻繁,部分內容可能與英文原文有出入。請加入我們在 Crowdin 上的努力,幫助我們改進本頁面的翻譯。 (Crowdin translation page, Contributing guide)

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.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
}

讓這個頁面變得更好