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

智能合约执行

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

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

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

定义合同地址和 abi

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

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

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

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

定义 contractAddrcontractAbi,您可以从编译后的 solidity 代码或区块链资源管理器中获取合约地址和 Abi。

使用定义了 contractAbiaddressWeb3.eth.Contract创建合约实例。 您可以通过该实例读写合约

setNumber 函数编码数据,encodeABI 将数据转换为字节码

创建交易对象与合约交互

设置 type:TxType.FeeDelegatedSmartContractExecution 用于更改与费用支付方的合约状态的交易

设置 data: data 以获取较早编码的函数数据

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

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

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

FeeDelegatedSmartContractExecution.js

const { Web3, TxType } = require("@kaiachain/web3js-ext");
const senderAddr = "0xa2a8854b1802d8cd5de631e690817c253d6a9153";
const senderPriv = "0x0e4ca6d38096ad99324de0dde108587e5d7c600165ae4cd6c2462c597458c2b8";
const feePayerAddr = "0xcb0eb737dfda52756495a5e08a9b37aab3b271da";
const feePayerPriv = "0x9435261ed483b6efa3886d6ad9f64c12078a0e28d8d80715c773e16fc000cff4";
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);
const contractAddr = "0xD7fA6634bDDe0B2A9d491388e2fdeD0fa25D2067";
const contractAbi = JSON.parse('[{"inputs":[{"internalType":"uint256","name":"newNumber","type":"uint256"}],"name":"setNumber","outputs":[],"stateMutability":"nonpayable","type":"function"}]');
async function main() {
const contract = new web3.eth.Contract(contractAbi, contractAddr);
const data = contract.methods.setNumber(0x123).encodeABI();
const tx = {
type: TxType.FeeDelegatedSmartContractExecution,
from: senderAddr,
to: contractAddr,
data: data,
};
// 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 FeeDelegatedSmartContractExecution.js
senderTxHashRLP 0x31f8a68203b8850ba43b74008301087994d7fa6634bdde0b2a9d491388e2fded0fa25d20678094a2a8854b1802d8cd5de631e690817c253d6a9153a43fb5c1cb0000000000000000000000000000000000000000000000000000000000000123f847f8458207f5a07d40391ae45f7d515becb1aff3b99e7d8b05299b268daa623fb045d981d89c44a0117c23ab37da35ce5206525852ce27f7edc9401e0ee8069a9a396daa64d0c85d
signedTx 0xfa69f712a8d748e294caa1b95df51f8974a0bcf620af6c1b8a7614999a20b447
receipt {
blockHash: '0x4f852e6a2d320287f20cf34c24546f51f53ebdbbc4a4cacee93525e678abf275',
blockNumber: 148744853n,
cumulativeGasUsed: 36990n,
effectiveGasPrice: 25000000000n,
from: '0xa2a8854b1802d8cd5de631e690817c253d6a9153',
gasUsed: 36990n,
logs: [],
logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
status: 1n,
to: '0xd7fa6634bdde0b2a9d491388e2fded0fa25d2067',
transactionHash: '0xfa69f712a8d748e294caa1b95df51f8974a0bcf620af6c1b8a7614999a20b447',
transactionIndex: 0n,
type: 0n
}

让这个页面变得更好