跳至主要內容
本頁面使用機器翻譯自英語,可能包含錯誤或不清楚的語言。如需最準確的信息,請參閱英文原文。由於更新頻繁,部分內容可能與英文原文有出入。請加入我們在 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/v5");
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
}

讓這個頁面變得更好