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

写入(智能合约执行 TxType)

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

初始化公共客户端,用于与 Kaia 区块链进行只读交互。

使用createWalletClient建立钱包客户端,配置Kairos链HTTP传输和转换为账户的发送者私钥

设置从 solidity 代码生成的 Abi

定义与之交互的**合同地址

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

使用 prepareTransactionRequest创建转账交易请求,指定发送方账户、接收方地址、转账金额(本例中为 0 KLAY)和交易类型**(TxType.SmartContractExecution)**。

使用钱包客户端的 sendTransaction 方法将交易发送到 Kaia 区块链,并记录交易哈希值。

使用公共客户端从合约中查询 number 函数(一个不修改状态的视图函数)。 这将检索数字变量的当前值,该值应反映上一个事务(如果成功)所设置的时间戳。

writeTxType.js

import {
http,
encodeFunctionData,
createWalletClient, createPublicClient, kairos,
TxType,
privateKeyToAccount
} from "@kaiachain/viem-ext";
const publicClient = createPublicClient({
chain: kairos,
transport: http(),
});
const senderWallet = createWalletClient({
chain: kairos,
transport: http(),
account: privateKeyToAccount(
"0x0e4ca6d38096ad99324de0dde108587e5d7c600165ae4cd6c2462c597458c2b8"
),
});
// Example usage
(async () => {
const contractAddr = "0x95Be48607498109030592C08aDC9577c7C2dD505";
const abi = [{ "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 data = encodeFunctionData({
abi,
args: [Date.now()],
functionName: "setNumber",
});
const tx = await senderWallet.prepareTransactionRequest({
type: TxType.SmartContractExecution,
account: senderWallet.account,
to: contractAddr,
value: 0,
data,
});
console.log("preparedTx", tx);
const sentTx = await senderWallet.sendTransaction(tx);
console.log("contract interaction tx", sentTx);
const result = await publicClient.readContract({
address: contractAddr,
abi,
functionName: 'number'
})
console.log('Current contract value', result);
})();

output

❯ node writeTxType.js
preparedTx {
type: 48,
account: {
address: '0xA2a8854b1802D8Cd5De631E690817c253d6a9153',
nonceManager: undefined,
sign: [AsyncFunction: sign],
signAuthorization: [AsyncFunction: signAuthorization],
signMessage: [AsyncFunction: signMessage],
signTransaction: [AsyncFunction: signTransaction],
signTypedData: [AsyncFunction: signTypedData],
source: 'privateKey',
type: 'local',
publicKey: '0x04dc9dccbd788c00fa98f7f4082f2f714e799bc0c29d63f04d48b54fe6250453cdaf06ca34ae8714cf3dae06bacdb78c7c2d4054bd38961d40853cd5f15955da79'
},
to: '0x95Be48607498109030592C08aDC9577c7C2dD505',
value: 0,
data: '0x3fb5c1cb000000000000000000000000000000000000000000000000000001977cfdd996',
from: '0xA2a8854b1802D8Cd5De631E690817c253d6a9153',
nonce: 2399,
chainId: 1001,
gas: 27953n,
gasPrice: '0x66720b300',
gasLimit: 69882
}
contract interaction tx 0x1834bf6fb6fe30bbf1799b03db6dd5448b0bb445e503f38496845b995f909baa
Current contract value 1750147159253n

让这个页面变得更好