跳至主要內容
本頁面使用機器翻譯自英語,可能包含錯誤或不清楚的語言。如需最準確的信息,請參閱英文原文。由於更新頻繁,部分內容可能與英文原文有出入。請加入我們在 Crowdin 上的努力,幫助我們改進本頁面的翻譯。 (Crowdin translation page, Contributing guide)

價值轉移備忘錄

TxTypeValueTransferMemo 用於用戶發送帶有特定信息的 KAIA。

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

定義發送方付費方地址和私人密鑰

使用指定的 kairos 測試網 URL 設置提供程序。 web3 中的提供者是訪問區塊鏈數據的只讀抽象。

此外,您還可以將提供商 URL 從 kairos 更改為 quicknode

使用 "web3.eth.accounts.privateKeyToAccount "創建帶有私鑰的發件人錢包

使用 "web3.eth.accounts.privateKeyToAccount "用私鑰創建付費者錢包

定義事務對象。

設置 `type:TxType.FeeDelegatedValueTransferMemo ",用於發送有費用支付方的備忘值轉賬交易

設置 value: toPeb("0.01") 以定義要轉換的值,使用 toPeb 將 Klay 轉換為 Peb

設置 data:"0x1234567890",以定義交易附帶的備忘值

設置其他參數,如

使用 signTransaction發送方賬戶上籤署交易

使用 "signTransactionAsFeePayer "以付費方賬戶簽署交易

將交易發送到區塊鏈。 它將返回交易收據

TxTypeFeeDelegatedValueTransferMemo.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.FeeDelegatedValueTransferMemo,
from: senderAddr,
to: recieverAddr,
value: toPeb("0.01"),
data: "0x1234567890",
};
// 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 TxTypeFeeDelegatedValueTransferMemo.js
senderTxHashRLP 0x11f88d8203b7850ba43b740082d1f694c40b6909eb7085590e1c26cb3becc25368e249e9872386f26fc1000094a2a8854b1802d8cd5de631e690817c253d6a9153851234567890f847f8458207f5a0fbd13725c0e913f564469d43e6e928514caca92ca5d5fcc18c493445d2bd3f00a065980c9c0ea0bcfa4324c23b21d45acaa8a64d1d47d45a6730774756be794e86
signedTx 0xb17d3bc44eb466cf0870926eb4b7a8e90a3ca348df1e32227ab1eaf2bd7f98c8
receipt {
blockHash: '0x51586607f9d282deede4a9325d6ecdb2abb675fca0350ac6778549f2e7bfed73',
blockNumber: 148744817n,
cumulativeGasUsed: 31500n,
effectiveGasPrice: 25000000000n,
from: '0xa2a8854b1802d8cd5de631e690817c253d6a9153',
gasUsed: 31500n,
logs: [],
logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
status: 1n,
to: '0xc40b6909eb7085590e1c26cb3becc25368e249e9',
transactionHash: '0xb17d3bc44eb466cf0870926eb4b7a8e90a3ca348df1e32227ab1eaf2bd7f98c8',
transactionIndex: 0n,
type: 0n
}

讓這個頁面變得更好