Skip to main content

legacy

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 web3 and @kaiachain/web3js-ext packages to add kaia features on web3

Define sender, reciver address and sender private key

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

Also, you can change the provider URL from baobab to allthatnode

Create a KlaytnWeb3 instance using the provider

Define transaction data with from (the sender address), to (receiver address), value (amount to transfer, use toPeb to set the value in Klay then it will be converted)

The privateKeyToAccount method create account instance from sender's private key.

The signTransaction method signs transaction with sender account.

The sendSignedTransaction method send the signed transaction into blockchain.

The getTransactionReceipt method returns the receipt of transaction.

Finally, get the result.

txTypeLegacyTransaction.js

const { Web3, KlaytnWeb3, toPeb } = require("@kaiachain/web3js-ext");
const senderAddr = "0x24e8efd18d65bcb6b3ba15a4698c0b0d69d13ff7";
const senderPriv = "0x4a72b3d09c3d5e28e8652e0111f9c4ce252e8299aad95bb219a38eb0a3f4da49";
const receiverAddr = "0xc40b6909eb7085590e1c26cb3becc25368e249e9";
async function main() {
const provider = new Web3.providers.HttpProvider("https://public-en-kairos.node.kaia.io");
const web3 = new KlaytnWeb3(provider);
const tx = {
to: receiverAddr,
value: toPeb("0.01", "KLAY"),
from: senderAddr,
};
const senderAccount = web3.eth.accounts.privateKeyToAccount(senderPriv);
const senderTx = await web3.eth.accounts.signTransaction(tx, senderAccount.privateKey);
console.log(senderTx);
const sendResult = await web3.eth.sendSignedTransaction(senderTx.rawTransaction);
const receipt = await web3.eth.getTransactionReceipt(sendResult.transactionHash);
console.log({ receipt });
}
main();

output

❯ node txTypeLegacyTransaction.js
signedTx 0x96b41f32f35f38ddd3e21aed8e8aa929ea6514ecf9f0b898014b00734056cc47
receipt {
blockHash: '0x5899dcdd7346e6b98872f93d9d74d39a118db628e8b75c08a5094b5ae2ef6314',
blockNumber: 148742598n,
cumulativeGasUsed: 205837n,
effectiveGasPrice: 25000000000n,
from: '0xa2a8854b1802d8cd5de631e690817c253d6a9153',
gasUsed: 21000n,
logs: [],
logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
status: 1n,
to: '0xc40b6909eb7085590e1c26cb3becc25368e249e9',
transactionHash: '0x96b41f32f35f38ddd3e21aed8e8aa929ea6514ecf9f0b898014b00734056cc47',
transactionIndex: 1n,
type: 2n
}