Value Transfer
TxTypeValueTransfer 在用户希望发送 KAIA 时使用。
- 由于 kaia 提供了多种交易类型,使每种交易类型只服务于一个目的,因此 TxTypeValueTransfer 只限于将 KAIA 发送到外部拥有的账户(EOA)。
导入 ethers 和 @kaiachain/ethers-ext 软件包,在 ethers.js 上添加 kaia 功能
定义发件人地址、发件人私钥和收件人地址
使用指定的 kairos 测试网 URL 设置提供程序。 以太坊中的提供者是访问区块链数据的只读抽象。
此外,您还可以将提供商 URL 从 kairos 更改为 quicknode
用私钥和提供者创建发件人钱包
声明包含 ** 类型、来自、至、值** 等字段的事务。
向区块链发送 tx。 函数 "sendTransaction "使用账户的私钥进行内部签名,然后将其传输到区块链网络。
如果已在区块链中完成发送,wait函数将返回发送回执。
txTypeValueTransferTransaction.js
const ethers = require("ethers");const { Wallet, TxType, parseKlay } = require("@kaiachain/ethers-ext/v5");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.jssentTx { 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}