跳至主要内容

legacy

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

TxTypeLegacyTransaction 代表 kaia 以前存在的一种事务类型。

**这种类型的交易可以创建账户、转移代币、部署智能合约、执行智能合约,或者执行上述交易的混合。

导入可与以太坊区块链及其生态系统交互的ethers软件包。

导入 @kaiachain/ethers-ext 软件包,在 ethers.js 上添加 kaia 功能

定义发送方地址、发送方私人密钥和接收方地址

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

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

使用私钥和提供商启动钱包实例。

声明一个事务,其字段包括from(从)、to(到)、value(值)

向区块链发送 tx。 函数 "sendTransaction "使用账户的私钥进行内部签名,然后将其传输到区块链网络。

如果已在区块链中完成发送,wait函数将返回发送回执。

txTypeLegacyTransaction.js

const ethers = require("ethers");
const { Wallet, 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 = {
// when type is empty it will be automatically set to type 0 or 2
// depending on the gasPrice, maxFeePerGas, maxPriorityFeePerGas fields.
// here, type will be 2 because no gas-related fields are set.
from: senderAddr,
to: recieverAddr,
value: parseKlay("0.01"),
};
const sentTx = await wallet.sendTransaction(tx);
console.log("sentTx", sentTx.hash);
const receipt = await sentTx.wait();
console.log("receipt", receipt);
}
main();

output

❯ node txTypeLegacyTransaction.js
sentTx 0x0693a5398133e80ae462ed957c2f590d4643d8c5fadf3aa6bc4de33b0c3d0da8
receipt {
to: '0xC40B6909EB7085590E1c26Cb3beCC25368e249E9',
from: '0xA2a8854b1802D8Cd5De631E690817c253d6a9153',
contractAddress: null,
transactionIndex: 2,
gasUsed: BigNumber { _hex: '0x5208', _isBigNumber: true },
logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
blockHash: '0xceca715c25ad13f55c4cba62a1c758b2f6731187cbf61d691e84b615dad263ea',
transactionHash: '0x0693a5398133e80ae462ed957c2f590d4643d8c5fadf3aa6bc4de33b0c3d0da8',
logs: [],
blockNumber: 148720917,
confirmations: 1,
cumulativeGasUsed: BigNumber { _hex: '0x055f7b', _isBigNumber: true },
effectiveGasPrice: BigNumber { _hex: '0x05d21dba00', _isBigNumber: true },
status: 1,
type: 2,
byzantium: true
}

让这个页面变得更好