Nhảy tới nội dung

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 ethers and @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 kaia Baobab testnet URL. A provider in ethers is a read-only abstraction to access the blockchain data.

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

Create a sender's wallet with the private key and provider

Declare a transaction with the fields such as type, 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.

txTypeValueTransferTransaction.js

const ethers = require("ethers");
const { Wallet, TxType, parseKlay } = require("@kaiachain/ethers-ext");
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 = {
type: TxType.ValueTransfer,
from: senderAddr,
to: recieverAddr,
value: parseKlay("0.01"),
};
const sentTx = await wallet.sendTransaction(tx);
console.log("sentTx", sentTx);
const receipt = await sentTx.wait();
console.log("receipt", receipt);
}
main();

output

❯ node txTypeValueTransferTransaction.js
sentTx {
hash: '0x7b6c638d8dba310d348ad80d55fbbf6024b286b982fd68b08bdb85004cace46b',
type: 0,
accessList: null,
blockHash: null,
blockNumber: null,
transactionIndex: null,
confirmations: 0,
from: '0xA2a8854b1802D8Cd5De631E690817c253d6a9153',
gasPrice: BigNumber { _hex: '0x0ba43b7400', _isBigNumber: true },
gasLimit: BigNumber { _hex: '0xcd14', _isBigNumber: true },
to: '0xC40B6909EB7085590E1c26Cb3beCC25368e249E9',
value: BigNumber { _hex: '0x2386f26fc10000', _isBigNumber: true },
nonce: 759,
data: '0x',
r: '0xd49028d36e64df5ad7e00e5163740cb734133dde320fb7e5fde1c75df38b6bb8',
s: '0x4a7a7a7950dd6c2d905217cd6cb61948dbdeb5842c13b8376d9e3a231f266667',
v: 2037,
creates: null,
chainId: 1001,
}
receipt {
to: '0xC40B6909EB7085590E1c26Cb3beCC25368e249E9',
from: '0xA2a8854b1802D8Cd5De631E690817c253d6a9153',
contractAddress: null,
transactionIndex: 1,
gasUsed: BigNumber { _hex: '0x5208', _isBigNumber: true },
logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
blockHash: '0x9831848ad564cb4c165bc48b3fa76b38ea08314b1fbf28fa388bec888acd50ed',
transactionHash: '0x7b6c638d8dba310d348ad80d55fbbf6024b286b982fd68b08bdb85004cace46b',
logs: [],
blockNumber: 143449789,
confirmations: 3,
cumulativeGasUsed: BigNumber { _hex: '0x03240d', _isBigNumber: true },
effectiveGasPrice: BigNumber { _hex: '0x05d21dba00', _isBigNumber: true },
status: 1,
type: 0,
byzantium: true
}