跳至主要内容
本页面使用机器翻译自英语,可能包含错误或不清楚的语言。如需最准确的信息,请参阅英文原文。由于更新频繁,部分内容可能与英文原文有出入。请加入我们在 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 函数对函数名和参数进行编码

FeeDelegatedSmartContractExecution类型创建交易对象

在交易中输入必要的详细信息

通过发送人的钱包签署交易

使用付费者的钱包将交易发送到区块链上。函数 "sendTransactionAsFeePayer "会在发送者的签名上添加一个带有 FeePayer 私钥的签名,并将其传输到区块链网络。

如果 tx 已成功发送到区块链,wait 函数将返回 tx 收据。

FeeDelegatedSmartContractExecution.js

const ethers = require("ethers");
const { Wallet, TxType } = require("@kaiachain/ethers-ext/v6");
const senderAddr = "0xa2a8854b1802d8cd5de631e690817c253d6a9153";
const senderPriv = "0x0e4ca6d38096ad99324de0dde108587e5d7c600165ae4cd6c2462c597458c2b8";
const feePayerAddr = "0xcb0eb737dfda52756495a5e08a9b37aab3b271da";
const feePayerPriv = "0x9435261ed483b6efa3886d6ad9f64c12078a0e28d8d80715c773e16fc000cff4";
const provider = new ethers.JsonRpcProvider("https://public-en-kairos.node.kaia.io");
const senderWallet = new Wallet(senderPriv, provider);
const feePayerWallet = new Wallet(feePayerPriv, 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.FeeDelegatedSmartContractExecution,
from: senderAddr,
to: contractAddr,
value: 0,
data: data,
};
// Sign transaction by sender
const populatedTx = await senderWallet.populateTransaction(tx);
const senderTxHashRLP = await senderWallet.signTransaction(populatedTx);
console.log("senderTxHashRLP", senderTxHashRLP);
// Sign and send transaction by fee payer
const sentTx = await feePayerWallet.sendTransactionAsFeePayer(senderTxHashRLP);
console.log("sentTx", sentTx.hash);
const receipt = await sentTx.wait();
console.log("receipt", receipt);
}
main();

output

❯ node FeeDelegatedSmartContractExecution.js
senderTxHashRLP 0x31f8a68203ad850ba43b7400830116379495be48607498109030592c08adc9577c7c2dd5058094a2a8854b1802d8cd5de631e690817c253d6a9153a43fb5c1cb0000000000000000000000000000000000000000000000000000000000000123f847f8458207f6a0f23531b148298b6b686af71c9722702732bed89202dd2a22adf9880b716ed205a074c31ec53da203c1421ae71e36e3b720f9550a00771784562b68c1cfa23e19e8
sentTx 0x8ddf463d1e2d5745b9ba71abce52eb02b6680d9699298c463c72180a7dd2c539
receipt {
to: '0x95Be48607498109030592C08aDC9577c7C2dD505',
from: '0xA2a8854b1802D8Cd5De631E690817c253d6a9153',
contractAddress: null,
transactionIndex: 4,
gasUsed: BigNumber { _hex: '0x9659', _isBigNumber: true },
logsBloom: '0x00000000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000001200000002',
blockHash: '0xa466ec90d778b7ea3e2c2cd3ec2a707fcf31a8c5f44d29d1e85f4c690af8ddb4',
transactionHash: '0x8ddf463d1e2d5745b9ba71abce52eb02b6680d9699298c463c72180a7dd2c539',
logs: [
{
transactionIndex: 4,
blockNumber: 148732388,
transactionHash: '0x8ddf463d1e2d5745b9ba71abce52eb02b6680d9699298c463c72180a7dd2c539',
address: '0x95Be48607498109030592C08aDC9577c7C2dD505',
topics: [Array],
data: '0x0000000000000000000000000000000000000000000000000000000000000123',
logIndex: 14,
blockHash: '0xa466ec90d778b7ea3e2c2cd3ec2a707fcf31a8c5f44d29d1e85f4c690af8ddb4'
}
],
blockNumber: 148732388,
confirmations: 3,
cumulativeGasUsed: BigNumber { _hex: '0x0b61a0', _isBigNumber: true },
effectiveGasPrice: BigNumber { _hex: '0x05d21dba00', _isBigNumber: true },
status: 1,
type: 0,
byzantium: true
}

让这个页面变得更好