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

Smart Contract Execution

TxTypeSmartContractExecution 使用给定数据执行智能合约。 只有当 "to "是智能合约账户时,才接受TxTypeSmartContractExecution

导入 ethers@kaiachain/ethers-ext 模块,在 ethers.js 上添加 kaia 功能。

定义发件人地址和发件人私人密钥

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

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

使用发件人的私人密钥和提供商创建发件人钱包

在至字段中设置要执行的合约地址,并设置ABI

使用 ethers.Contract创建合约实例,填写参数 contractAddrabiprovider。 您可以通过该实例读写合约

使用 encodeFunctionData 函数对函数名和参数进行编码

定义交易对象

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

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

smartContractExecution.js

const ethers = require("ethers");
const { Wallet, TxType } = require("@kaiachain/ethers-ext");
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);
const contractAddr = "0x95Be48607498109030592C08aDC9577c7C2dD505";
const abi = ["function setNumber(uint256 newNumber)"];
async function main() {
const contract = new ethers.Contract(contractAddr, abi, provider);
const data = contract.interface.encodeFunctionData("setNumber", ["0x123"]);
const tx = {
type: TxType.SmartContractExecution,
from: senderAddr,
to: contractAddr,
value: 0,
data: data,
};
const sentTx = await wallet.sendTransaction(tx);
console.log("sentTx", sentTx.hash);
const receipt = await sentTx.wait();
console.log("receipt", receipt);
}
main();

output

❯ node smartContractExecution.js
sentTx 0x6ee58de9d1fd46da6f595112cc6ce060ef560796f78650e8a18fb32f20ec5343
receipt {
to: '0x95Be48607498109030592C08aDC9577c7C2dD505',
from: '0xA2a8854b1802D8Cd5De631E690817c253d6a9153',
contractAddress: null,
transactionIndex: 3,
gasUsed: BigNumber { _hex: '0x6f49', _isBigNumber: true },
logsBloom: '0x00000000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000001200000002',
blockHash: '0xb71bcb74a6772501913302fb30d754bdf82cf2996ba9418b509bfd48b275bb0c',
transactionHash: '0x6ee58de9d1fd46da6f595112cc6ce060ef560796f78650e8a18fb32f20ec5343',
logs: [
{
transactionIndex: 3,
blockNumber: 148721006,
transactionHash: '0x6ee58de9d1fd46da6f595112cc6ce060ef560796f78650e8a18fb32f20ec5343',
address: '0x95Be48607498109030592C08aDC9577c7C2dD505',
topics: [Array],
data: '0x0000000000000000000000000000000000000000000000000000000000000123',
logIndex: 9,
blockHash: '0xb71bcb74a6772501913302fb30d754bdf82cf2996ba9418b509bfd48b275bb0c'
}
],
blockNumber: 148721006,
confirmations: 2,
cumulativeGasUsed: BigNumber { _hex: '0x094000', _isBigNumber: true },
effectiveGasPrice: BigNumber { _hex: '0x05d21dba00', _isBigNumber: true },
status: 1,
type: 0,
byzantium: true
}

让这个页面变得更好