跳至主要內容
本頁面使用機器翻譯自英語,可能包含錯誤或不清楚的語言。如需最準確的信息,請參閱英文原文。由於更新頻繁,部分內容可能與英文原文有出入。請加入我們在 Crowdin 上的努力,幫助我們改進本頁面的翻譯。 (Crowdin translation page, Contributing guide)

使用 TxType 寫入

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

定義發件人的地址私人密鑰

使用指定的 kairos 測試網 URL 設置提供程序。 web3 中的提供者是訪問區塊鏈數據的只讀抽象。

此外,您還可以將提供商 URL 從 kairos 更改為 quicknode

使用 web3.eth.accounts.privateKeyToAccount 創建發件人賬戶

定義與之交互的合同 Abiaddress

使用定義了 contractAbiaddressWeb3.eth.Contract創建合同實例。 您可以通過該實例讀寫合約

更新前獲取編號

使用contract.methods.increment().encodeABI()對合約方法數據進行編碼

創建事務以更新編號的值,其類型為 "FeeDelegatedSmartContractExecution":收費委託智能合約執行

發送方賬戶簽署交易

向網絡發送交易並等待收據

更新後獲取編號

smartContractWriteWithTxType.js

const { Web3, TxType } = require("@kaiachain/web3js-ext");
const senderAddr = "0x24e8efd18d65bcb6b3ba15a4698c0b0d69d13ff7";
const senderPriv = "0x4a72b3d09c3d5e28e8652e0111f9c4ce252e8299aad95bb219a38eb0a3f4da49";
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);
/* compiled in remix.ethereum.org (compiler: 0.8.18, optimizer: false)
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;
contract Counter {
uint256 public number;
event SetNumber(uint256 number);
constructor(uint256 initNumber) {
number = initNumber;
}
function setNumber(uint256 newNumber) public {
number = newNumber;
emit SetNumber(number);
}
function increment() public {
number++;
emit SetNumber(number);
}
}
*/
const contractAbi = JSON.parse('[{"inputs":[{"internalType":"uint256","name":"initNumber","type":"uint256"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"number","type":"uint256"}],"name":"SetNumber","type":"event"},{"inputs":[],"name":"increment","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"number","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"newNumber","type":"uint256"}],"name":"setNumber","outputs":[],"stateMutability":"nonpayable","type":"function"}]');
const contractAddr = "0x95Be48607498109030592C08aDC9577c7C2dD505";
async function main() {
const contract = new web3.eth.Contract(contractAbi, contractAddr);
console.log("number before", (await contract.methods.number().call()).toString());
const data = contract.methods.increment().encodeABI();
const tx = {
type: TxType.SmartContractExecution,
from: senderAddr,
to: contractAddr,
data: data,
};
const signResult = await senderAccount.signTransaction(tx);
console.log("signedTx", signResult.transactionHash);
const receipt = await web3.eth.sendSignedTransaction(signResult.rawTransaction);
console.log("receipt", receipt);
console.log("number after", (await contract.methods.number().call()).toString());
}
main();

output

❯ node smartContractWriteWithTxType.js
number before 295
signedTx 0xb4e2f9d3a6c3d24a318028ff904f2d21e79ca6f2adc41aa099cd1be70f7952b9
receipt {
blockHash: '0xdbf4c6d306da99f7c36500d0ec84d0064b34820f51e827a4f6662f1b734a27cf',
blockNumber: 148743668n,
cumulativeGasUsed: 28014n,
effectiveGasPrice: 25000000000n,
from: '0x24e8efd18d65bcb6b3ba15a4698c0b0d69d13ff7',
gasUsed: 28014n,
logs: [
{
address: '0x95be48607498109030592c08adc9577c7c2dd505',
topics: [Array],
data: '0x0000000000000000000000000000000000000000000000000000000000000128',
blockNumber: 148743668n,
transactionHash: '0xb4e2f9d3a6c3d24a318028ff904f2d21e79ca6f2adc41aa099cd1be70f7952b9',
transactionIndex: 0n,
blockHash: '0xdbf4c6d306da99f7c36500d0ec84d0064b34820f51e827a4f6662f1b734a27cf',
logIndex: 0n,
removed: false
}
],
logsBloom: '0x00000000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000001200000002',
status: 1n,
to: '0x95be48607498109030592c08adc9577c7c2dd505',
transactionHash: '0xb4e2f9d3a6c3d24a318028ff904f2d21e79ca6f2adc41aa099cd1be70f7952b9',
transactionIndex: 0n,
type: 0n
}
number after 296

讓這個頁面變得更好