Nhảy tới nội dung

Value Transfer With Memo

TxTypeValueTransferMemo is used when a user wants to send KAIA with a specific message.

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, data.

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.

TxTypeValueTransferMemo.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.ValueTransferMemo,
from: senderAddr,
to: recieverAddr,
value: parseKlay("0.01"),
data: "0x1234567890",
};
const sentTx = await wallet.sendTransaction(tx);
console.log("sentTx", sentTx.hash);
const receipt = await sentTx.wait();
console.log("receipt", receipt);
}
main();

output

❯ node TxTypeValueTransferMemo.js
sentTx 0x57fdd7fec672b9e66e9bef766aca109babbfbb3c71b62544f10bd9bd3dceee85
receipt {
to: '0xC40B6909EB7085590E1c26Cb3beCC25368e249E9',
from: '0xA2a8854b1802D8Cd5De631E690817c253d6a9153',
contractAddress: null,
transactionIndex: 2,
gasUsed: BigNumber { _hex: '0x53fc', _isBigNumber: true },
logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
blockHash: '0xf223ef09cb8c9bc50f9ec0463a22ffb8a79c179e6ee5d1dccc0467aa23de7fe2',
transactionHash: '0x57fdd7fec672b9e66e9bef766aca109babbfbb3c71b62544f10bd9bd3dceee85',
logs: [],
blockNumber: 148721333,
confirmations: 7,
cumulativeGasUsed: BigNumber { _hex: '0x056a2f', _isBigNumber: true },
effectiveGasPrice: BigNumber { _hex: '0x05d21dba00', _isBigNumber: true },
status: 1,
type: 0,
byzantium: true
}