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)

Đọc

Bạn có thể gọi các hàm trong Hợp đồng thông minh thông qua API RPC "Gọi". Những gì bạn có thể gọi ở đây bị giới hạn ở các chức năng xem không thay đổi bất kỳ trạng thái nào 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.

Đặ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 readContract được gọi trên máy khách công khai để truy vấn hàm number (hàm xem không sửa đổi trạng thái) từ hợp đồng tại địa chỉ đã chỉ định.

SmartContractView.js

import { http, createPublicClient, kairos } from "@kaiachain/viem-ext";
const publicClient = createPublicClient({
chain: kairos,
transport: http(),
});
// 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 result = await publicClient.readContract({
address,
abi,
functionName: 'number'
})
console.log('Current contract value', result);
})();

output

❯ js SmartContractView.js
Current contract value 123n

Cải thiện trang này