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

Cancel

TxTypeCancel 取消事務池中具有相同 nonce 的事務的執行。 當已提交的交易在一定時間內似乎未得到處理時,這種交易類型就很有用。

導入 ethers@kaiachain/ethers-ext 軟件包,在 ethers.js 上添加 kaia 功能

定義發送方付費方地址和私人密鑰

使用指定的 kairos 測試網 URL 設置提供程序。 以太坊中的提供者是訪問區塊鏈數據的只讀抽象。

此外,您還可以將提供商 URL 從 kairos 更改為 quicknode

用私鑰和提供商創建發送方的錢包

用私鑰和提供商創建付費者的錢包

聲明一個事務,其from字段為發送地址type字段為TxType.FeeDelegatedCancel

發送方錢包簽署交易,並打印出發送方TxHashRLP

使用付費者的錢包將交易發送到區塊鏈上。 函數 "sendTransactionAsFeePayer "會在發送者的簽名上添加一個帶有 FeePayer 私鑰的簽名,並將其傳輸到區塊鏈網絡。

如果已在區塊鏈中完成發送,wait函數將返回發送回執。

TxFeeDelegatedCancelType.js

const ethers = require("ethers");
const { Wallet, TxType, parseKlay } = require("@kaiachain/ethers-ext/v5");
const senderAddr = "0xa2a8854b1802d8cd5de631e690817c253d6a9153";
const senderPriv = "0x0e4ca6d38096ad99324de0dde108587e5d7c600165ae4cd6c2462c597458c2b8";
const feePayerAddr = "0xcb0eb737dfda52756495a5e08a9b37aab3b271da";
const feePayerPriv = "0x9435261ed483b6efa3886d6ad9f64c12078a0e28d8d80715c773e16fc000cff4";
const provider = new ethers.providers.JsonRpcProvider("https://public-en-kairos.node.kaia.io");
const senderWallet = new Wallet(senderPriv, provider);
const feePayerWallet = new Wallet(feePayerPriv, provider);
async function main() {
const tx = {
type: TxType.FeeDelegatedCancel,
from: senderAddr,
};
// Sign transaction by sender
const populatedTx = await senderWallet.populateTransaction(tx);
const senderTxHashRLP = await senderWallet.signTransaction(populatedTx);
console.log("senderTxHashRLP", senderTxHashRLP);
// Sign and send transaction by fee payer
const sentTx = await feePayerWallet.sendTransactionAsFeePayer(senderTxHashRLP);
console.log("sentTx", sentTx.hash);
const receipt = await sentTx.wait();
console.log("receipt", receipt);
}
main();

output

❯ node TxFeeDelegatedCancelType.js
senderTxHashRLP 0x39f86a8203af850ba43b740082cd1494a2a8854b1802d8cd5de631e690817c253d6a9153f847f8458207f5a06c639b8828bb27b36b2575636b4ec9dd4c7de4820327fe158746d03b28b90d8ba0332b43ec974ecb3ea16593760cb6e139a95d4ce03847681fb053a00143eb91a1
sentTx 0xd81cd9190f2fd40f6b2e6472f91b95a1e09e2ad3dfc41a6b3458274eb5c9175c
receipt {
to: '0xA2a8854b1802D8Cd5De631E690817c253d6a9153',
from: '0xA2a8854b1802D8Cd5De631E690817c253d6a9153',
contractAddress: null,
transactionIndex: 1,
gasUsed: BigNumber { _hex: '0x7918', _isBigNumber: true },
logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
blockHash: '0x02d655e716e1a5c8d497f66a6d80683c633d3e4002a9e26026f101983d951c1a',
transactionHash: '0xd81cd9190f2fd40f6b2e6472f91b95a1e09e2ad3dfc41a6b3458274eb5c9175c',
logs: [],
blockNumber: 148732476,
confirmations: 3,
cumulativeGasUsed: BigNumber { _hex: '0x034b1d', _isBigNumber: true },
effectiveGasPrice: BigNumber { _hex: '0x05d21dba00', _isBigNumber: true },
status: 1,
type: 0,
byzantium: true
}

讓這個頁面變得更好