跳至主要内容
本页面使用机器翻译自英语,可能包含错误或不清楚的语言。如需最准确的信息,请参阅英文原文。由于更新频繁,部分内容可能与英文原文有出入。请加入我们在 Crowdin 上的努力,帮助我们改进本页面的翻译。 (Crowdin translation page, Contributing guide)

Value Transfer

TxTypeValueTransfer 用于用户发送 KAIA。

  • 由于 kaia 提供了多种交易类型,使每种交易类型只服务于一个目的,因此 TxTypeValueTransfer 只限于将 KAIA 发送到外部拥有的账户(EOA)。

导入 web3@kaiachain/web3js-ext 软件包,在 web3 上添加 kaia 功能

定义发送方付费方地址和私人密钥

使用指定的 kairos 测试网 URL 设置提供程序。 web3 中的提供者是访问区块链数据的只读抽象。

此外,您还可以将提供商 URL 从 kairos 更改为 quicknode

使用 "web3.eth.accounts.privateKeyToAccount "创建带有私钥的发件人钱包

使用 "web3.eth.accounts.privateKeyToAccount "用私钥创建付费者钱包

定义事务对象。

设置 `type:为交易设置 "TxType.FeeDelegatedValueTransfer",以发送有付费方的价值转移

设置 value: toPeb("0.01") 以定义要转换的值,使用 toPeb 将 Klay 转换为 Peb

设置其他参数,如

使用 signTransaction发送方账户上签署交易

使用 "signTransactionAsFeePayer "以付费方账户签署交易

将交易发送到区块链。 它将返回交易收据

TxTypeFeeDelegatedValueTransfer.js

const { Web3, TxType, toPeb } = require("@kaiachain/web3js-ext");
const senderAddr = "0xa2a8854b1802d8cd5de631e690817c253d6a9153";
const senderPriv = "0x0e4ca6d38096ad99324de0dde108587e5d7c600165ae4cd6c2462c597458c2b8";
const feePayerPriv = "0x9435261ed483b6efa3886d6ad9f64c12078a0e28d8d80715c773e16fc000cff4";
const recieverAddr = "0xc40b6909eb7085590e1c26cb3becc25368e249e9";
const provider = new Web3.providers.HttpProvider("https://public-en-kairos.node.kaia.io");
const web3 = new Web3(provider);
const senderAccount = web3.eth.accounts.privateKeyToAccount(senderPriv);
const feePayerAccount = web3.eth.accounts.privateKeyToAccount(feePayerPriv);
async function main() {
const tx = {
type: TxType.FeeDelegatedValueTransfer,
from: senderAddr,
to: recieverAddr,
value: toPeb("0.01"),
};
// Sign transaction by sender
const signResult1 = await senderAccount.signTransaction(tx);
console.log("senderTxHashRLP", signResult1.rawTransaction);
// Sign and send transaction by fee payer
const signResult2 = await feePayerAccount.signTransactionAsFeePayer(signResult1.rawTransaction);
console.log("signedTx", signResult2.transactionHash);
const receipt = await web3.eth.sendSignedTransaction(signResult2.rawTransaction);
console.log("receipt", receipt);
}
main();

output

❯ node TxTypeFeeDelegatedValueTransfer.js
senderTxHashRLP 0x09f8878203b6850ba43b740082cd1494c40b6909eb7085590e1c26cb3becc25368e249e9872386f26fc1000094a2a8854b1802d8cd5de631e690817c253d6a9153f847f8458207f5a0119485c48b7587a7ba6358f759c0a31e7de94fea77ba28089e20135156af7d94a07c497057653e76b9646bf5cc2024aa6d58cba23d4b2fec932e61e288cb2513f9
signedTx 0xded94a15a62b6d24ac4d0a317f0ed37873d4105b0ff7500e577f6d282fdc4bae
receipt {
blockHash: '0x442774c57650ef18338bea13ad069428acf8a6c8da5e7eb104dd1d961972400a',
blockNumber: 148744776n,
cumulativeGasUsed: 177286n,
effectiveGasPrice: 25000000000n,
from: '0xa2a8854b1802d8cd5de631e690817c253d6a9153',
gasUsed: 31000n,
logs: [],
logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
status: 1n,
to: '0xc40b6909eb7085590e1c26cb3becc25368e249e9',
transactionHash: '0xded94a15a62b6d24ac4d0a317f0ed37873d4105b0ff7500e577f6d282fdc4bae',
transactionIndex: 1n,
type: 0n
}

让这个页面变得更好