Nhảy tới nội dung
This page uses machine translation from English, which may contain errors or unclear language. For the most accurate information, please see the original English version. Some content may be in the original English due to frequent updates. Help us improve this page's translation by joining our effort on Crowdin. (Crowdin translation page, Contributing guide)

Viết

Bạn có thể thực hiện "giao dịch" tương tác với Hợp đồng thông minh khi bạn cần cập nhật một số trạng thái trong hợp đồng.

Nhập các mô-đun @kaiachain/ethers-ext để thêm các tính năng kaia vào web3.

Khởi tạo một máy khách công khai để tương tác chỉ đọc với blockchain Kaia.

Thiết lập ứng dụng ví bằng createWalletClient, được cấu hình bằng chuỗi Kairos, phương thức truyền tải HTTPkhóa riêng của người gửi được chuyển đổi thành tài khoản.

Đặt Abi được tạo từ mã solidity

Xác định địa chỉ hợp đồng để tương tác với

Phương thức writeContract được sử dụng để gọi hàm setNumber trên hợp đồng, truyền Date.now() (dấu thời gian hiện tại tính bằng mili giây) làm đối số. Thao tác này sẽ tạo và gửi một giao dịch để cập nhật biến số của hợp đồng.

Sử dụng máy khách công khai để truy vấn hàm số (hàm xem không sửa đổi trạng thái) từ hợp đồng. Lệnh này sẽ lấy giá trị hiện tại của biến số, giá trị này sẽ phản ánh dấu thời gian được thiết lập bởi giao dịch trước đó (nếu thành công).

smartContractWrite.js

import {
createPublicClient,
http,
kairos,
privateKeyToAccount,
createWalletClient
} from "@kaiachain/viem-ext";
const publicClient = createPublicClient({
chain: kairos,
transport: http(),
});
const walletClient = createWalletClient({
chain: kairos,
transport: http(),
account: privateKeyToAccount(
// using legacy account only for this example
"0x71c5a2d04d744d76492640fb3e5cf2650efae106655f27baffc482c53f57dca2"
),
});
// Example usage
(async () => {
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 address = "0x95Be48607498109030592C08aDC9577c7C2dD505";
const txHash = await walletClient.writeContract({
address,
abi,
functionName: 'setNumber',
args: [Date.now()]
})
console.log('tx hash', txHash);
const result = await publicClient.readContract({
address,
abi,
functionName: 'number'
})
console.log('Current contract value', result);
})();

output

❯ node smartContractWrite.js
tx hash 0xf890d27d3cb4670755391257477c4f942ecb3fd0974e1863c2cd0bc72bfae0d4
Current contract value 123n

Cải thiện trang này