TxTypeLegacyTransaction represents a type of transactions existed previously in kaia.
This type of transaction can create an account, transfer tokens, deploy a smart contract, execute a smart contract, or perform a mix of aforementioned.
導入 web3 和 @kaiachain/web3js-ext 軟件包,在 web3 上添加 kaia 功能
使用指定的 kairos 測試網 URL 設置提供程序。 web3js 中的提供者是訪問區塊鏈數據的只讀抽象。
此外,您還可以將提供商 URL 從 kairos 更改為 quicknode
定義交易數據,包括從(發送方地址)、到(接收方地址)、值(轉賬金額,使用 toPeb 在 Klay 中設置值,然後進行轉換)
使用 privateKeyToAccount 方法,可以用發送者的私鑰創建賬戶實例。
signTransaction 方法與發件人賬戶簽署交易。
sendSignedTransaction 方法將已簽名的交易發送到區塊鏈中。
getTransactionReceipt 方法返回交易收據。
txTypeLegacyTransaction.js
const { Web3, KlaytnWeb3, toPeb } = require("@kaiachain/web3js-ext");
const senderAddr = "0x24e8efd18d65bcb6b3ba15a4698c0b0d69d13ff7";
const senderPriv = "0x4a72b3d09c3d5e28e8652e0111f9c4ce252e8299aad95bb219a38eb0a3f4da49";
const receiverAddr = "0xc40b6909eb7085590e1c26cb3becc25368e249e9";
const provider = new Web3.providers.HttpProvider("https://public-en-kairos.node.kaia.io");
const web3 = new KlaytnWeb3(provider);
value: toPeb("0.01", "KLAY"),
const senderAccount = web3.eth.accounts.privateKeyToAccount(senderPriv);
const senderTx = await web3.eth.accounts.signTransaction(tx, senderAccount.privateKey);
const sendResult = await web3.eth.sendSignedTransaction(senderTx.rawTransaction);
const receipt = await web3.eth.getTransactionReceipt(sendResult.transactionHash);
console.log({ receipt });
❯ node txTypeLegacyTransaction.js
signedTx 0x96b41f32f35f38ddd3e21aed8e8aa929ea6514ecf9f0b898014b00734056cc47
blockHash: '0x5899dcdd7346e6b98872f93d9d74d39a118db628e8b75c08a5094b5ae2ef6314',
cumulativeGasUsed: 205837n,
effectiveGasPrice: 25000000000n,
from: '0xa2a8854b1802d8cd5de631e690817c253d6a9153',
logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
to: '0xc40b6909eb7085590e1c26cb3becc25368e249e9',
transactionHash: '0x96b41f32f35f38ddd3e21aed8e8aa929ea6514ecf9f0b898014b00734056cc47',