Skip to main content

Value Transfer

TxTypeValueTransfer is used when a user wants to send KAIA.

  • As kaia provides multiple transaction types to make each transaction type serve a single purpose, TxTypeValueTransfer is limited to send KAIA to an externally owned account (EOA).

Import the web3 and @kaiachain/web3js-ext packages to add kaia features on web3

Define sender, receiver 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 default provider. For example, using the alchemy provider.

Create a KlaytnWeb3 instance using the provider

Declare transaction params such as type, from, to, value

Sign the transaction with sender account

Send the signed transaction to blockchain and print the receipt

txTypeValueTransferTransaction.js

const { KlaytnWeb3, TxType, toPeb } = require("@kaiachain/web3js-ext");
const { Web3 } = require("web3");
const recieverAddr = "0xc40b6909eb7085590e1c26cb3becc25368e249e9";
const senderAddr = "0xa2a8854b1802d8cd5de631e690817c253d6a9153";
const senderPriv = "0x0e4ca6d38096ad99324de0dde108587e5d7c600165ae4cd6c2462c597458c2b8";
async function main() {
const provider = new Web3.providers.HttpProvider("https://public-en-kairos.node.kaia.io");
const web3 = new KlaytnWeb3(provider);
const senderAccount = web3.eth.accounts.privateKeyToAccount(senderPriv);
const tx = {
type: TxType.ValueTransfer,
from: senderAddr,
to: recieverAddr,
value: toPeb("0.01", "KLAY"),
};
const signResult = await senderAccount.signTransaction(tx);
console.log("rawTx", signResult.rawTransaction);
const receipt = await web3.eth.sendSignedTransaction(signResult.rawTransaction);
console.log("receipt", receipt);
}
main();

output

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