跳至主要內容

legacy

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

TxTypeLegacyTransaction 代表 kaia 以前存在的一種事務類型。

**這種類型的交易可以創建賬戶、轉移代幣、部署智能合約、執行智能合約,或者執行上述交易的混合。

導入可與以太坊區塊鏈及其生態系統交互的ethers軟件包。

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

定義發送方地址、發送方私人密鑰和接收方地址

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

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

使用私鑰和提供商啟動錢包實例。

聲明一個事務,其字段包括from(從)、to(到)、value(值)

向區塊鏈發送 tx。 函數 "sendTransaction "使用賬戶的私鑰進行內部簽名,然後將其傳輸到區塊鏈網絡。

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

txTypeLegacyTransaction.js

const ethers = require("ethers");
const { Wallet, parseKlay } = require("@kaiachain/ethers-ext/v5");
const recieverAddr = "0xc40b6909eb7085590e1c26cb3becc25368e249e9";
const senderAddr = "0xa2a8854b1802d8cd5de631e690817c253d6a9153";
const senderPriv = "0x0e4ca6d38096ad99324de0dde108587e5d7c600165ae4cd6c2462c597458c2b8";
const provider = new ethers.providers.JsonRpcProvider("https://public-en-kairos.node.kaia.io");
const wallet = new Wallet(senderPriv, provider);
async function main() {
const tx = {
// when type is empty it will be automatically set to type 0 or 2
// depending on the gasPrice, maxFeePerGas, maxPriorityFeePerGas fields.
// here, type will be 2 because no gas-related fields are set.
from: senderAddr,
to: recieverAddr,
value: parseKlay("0.01"),
};
const sentTx = await wallet.sendTransaction(tx);
console.log("sentTx", sentTx.hash);
const receipt = await sentTx.wait();
console.log("receipt", receipt);
}
main();

output

❯ node txTypeLegacyTransaction.js
sentTx 0x0693a5398133e80ae462ed957c2f590d4643d8c5fadf3aa6bc4de33b0c3d0da8
receipt {
to: '0xC40B6909EB7085590E1c26Cb3beCC25368e249E9',
from: '0xA2a8854b1802D8Cd5De631E690817c253d6a9153',
contractAddress: null,
transactionIndex: 2,
gasUsed: BigNumber { _hex: '0x5208', _isBigNumber: true },
logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
blockHash: '0xceca715c25ad13f55c4cba62a1c758b2f6731187cbf61d691e84b615dad263ea',
transactionHash: '0x0693a5398133e80ae462ed957c2f590d4643d8c5fadf3aa6bc4de33b0c3d0da8',
logs: [],
blockNumber: 148720917,
confirmations: 1,
cumulativeGasUsed: BigNumber { _hex: '0x055f7b', _isBigNumber: true },
effectiveGasPrice: BigNumber { _hex: '0x05d21dba00', _isBigNumber: true },
status: 1,
type: 2,
byzantium: true
}

讓這個頁面變得更好