Nhảy tới nội dung

legacy

This page uses machine translation from English, which may contain errors or unclear language. For the most accurate information, please see the original English version. Some content may be in the original English due to frequent updates. Help us improve this page's translation by joining our effort on Crowdin. (Crowdin translation page, Contributing guide)

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.

Import the ethers package that can interact with the Ethereum Blockchain and its ecosystem.

Import @kaiachain/ethers-ext packages to add kaia features on ethers.js

Define sender address, sender private key and reciever address

Set up the provider with the specified kairos testnet URL. A provider in ethers is a read-only abstraction to access the blockchain data.

Also, you can change the provider URL from kairos to quicknode

Init Wallet instance with the private key and provider.

Declare a transaction with the fields such as from, to, value.

Send the tx to the blockchain. Function sendTransaction internally signs with the private key of the account and then transmits it to the blockchain network.

The wait function returns the tx receipt if it is completed in the blockchain.

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
}

Cải thiện trang này