Value Transfer Memo
TxTypeValueTransferMemo 用於用戶發送帶有特定信息的 KAIA。
導入 web3 和 @kaiachain/web3js-ext 軟件包,在 web3 上添加 kaia 功能
定義發送方、接收方地址和發送方私人密鑰
使用指定的 kairos 測試網 URL 設置提供程序。 web3js 中的提供者是訪問區塊鏈數據的只讀抽象。
此外,您還可以更改默認提供商。 例如,使用alchemy提供者。
使用提供程序創建KlaytnWeb3實例
定義事務對象。
用發件人賬戶簽署交易。
將已簽名的交易發送到 kaia 網絡。
TxTypeValueTransferMemo.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.ValueTransferMemo, from: senderAddr, to: recieverAddr, value: toPeb("0.01"), gasLimit: 50000, data: "0x1234567890", }; 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 TxTypeValueTransferMemo.jssignedTx 0x237f8d5598bb5235ba87b0e9fc8ded203c6dd270cc71f1add0ae47c63d13da5dreceipt { blockHash: '0x90b6753c8fa1ee401775c57974318bb6c0746834170f4d0a132b8a9d10fb37f5', blockNumber: 148742510n, cumulativeGasUsed: 21500n, effectiveGasPrice: 25000000000n, from: '0xa2a8854b1802d8cd5de631e690817c253d6a9153', gasUsed: 21500n, logs: [], logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', status: 1n, to: '0xc40b6909eb7085590e1c26cb3becc25368e249e9', transactionHash: '0x237f8d5598bb5235ba87b0e9fc8ded203c6dd270cc71f1add0ae47c63d13da5d', transactionIndex: 0n, type: 0n}