การอ่านข้อมูลสัญญา
เรียกใช้ฟังก์ชัน view
การอ่านข้อมูลสัญญา เป็นบทเรียน Web3 & DApp Development Fundamentals ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Web3 & DApp Development Fundamentals และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Web3 & DApp Development Fundamentals มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Calling View Functions
Reading data from a contract is free — it does not change state, cost gas, or require a signature. These are view and pure functions.
With ethers.js you attach to a deployed contract and call them like normal JavaScript methods.
You Need the ABI
To talk to a contract, ethers needs its ABI — the interface describing each function. You can use the full compiled ABI or a minimal human-readable one:
const abi = [
"function name() view returns (string)",
"function balanceOf(address) view returns (uint256)",
];const abi = [
"function name() view returns (string)",
"function balanceOf(address) view returns (uint256)",
];Creating a Contract Instance
Combine the address, ABI, and a provider to create a read-only contract object:
const contract = new ethers.Contract(
tokenAddress,
abi,
provider
);Because it was given a provider (not a signer), it can only read.
const contract = new ethers.Contract(
tokenAddress,
abi,
provider
);Calling a Read Method
Call any view function and await the result:
const name = await contract.name();
const supply = await contract.totalSupply();
console.log(name, supply);Each call returns a Promise that resolves to the decoded value.
const name = await contract.name();
const supply = await contract.totalSupply();
console.log(name, supply);Passing Arguments
View functions can take parameters. Pass them like normal function arguments:
const balance = await contract.balanceOf(
"0xUserAddress"
);
console.log(ethers.formatUnits(balance, 18));ethers encodes the arguments according to the ABI types automatically.
const balance = await contract.balanceOf(
"0xUserAddress"
);
console.log(ethers.formatUnits(balance, 18));Handling Returned Numbers
Numeric returns are BigInt in ethers v6. To display token amounts, convert with the right decimals:
const raw = await contract.balanceOf(user);
const human = ethers.formatUnits(raw, 18);Use formatUnits/parseUnits for tokens, formatEther/parseEther for ETH (18 decimals).
const raw = await contract.balanceOf(user);
const human = ethers.formatUnits(raw, 18);Returns with Multiple Values
If a function returns several values, ethers gives you a result array you can destructure:
const [reserve0, reserve1, timestamp] =
await pair.getReserves();
console.log(reserve0, reserve1);Named return values are also accessible by name on the result object.
const [reserve0, reserve1, timestamp] =
await pair.getReserves();
console.log(reserve0, reserve1);Reading at a Past Block
You can query historical state by passing a block tag as an override:
const oldBalance = await contract.balanceOf(
user,
{ blockTag: 19000000 }
);This requires an archive node, but lets you reconstruct past state.
const oldBalance = await contract.balanceOf(
user,
{ blockTag: 19000000 }
);Static Calls on Write Functions
Sometimes you want to simulate a state-changing function without sending it, to read its would-be result or check it does not revert:
const result = await contract.transfer
.staticCall(to, amount);
console.log("Would return:", result);This runs the call locally without broadcasting a transaction.
const result = await contract.transfer
.staticCall(to, amount);
console.log("Would return:", result);Reads Are Free
Because view calls execute on the node without changing state:
- They cost no gas for the caller.
- They need no signer or wallet approval.
- They return almost instantly.
Only state changes require a transaction and gas.
Using a Hardhat-Attached Instance
In Hardhat scripts you can attach to a deployed address without writing the ABI by hand, since artifacts are already compiled:
const token = await ethers.getContractAt(
"MyToken",
deployedAddress
);
console.log(await token.totalSupply());ethers loads the ABI from the artifact for you.
const token = await ethers.getContractAt(
"MyToken",
deployedAddress
);
console.log(await token.totalSupply());Quick Check
Test your understanding of reading contract data.
Recap
You learned to read on-chain data.
- Create a contract with address + ABI + provider for read-only access.
- Call view functions like normal methods and
awaitthe result. - Numeric returns are BigInt; convert with
formatUnits/formatEther. - Destructure multiple return values; query the past with
blockTag. - Use
staticCallto simulate write functions; reads are free.
คำถามที่พบบ่อย
บทเรียน “การอ่านข้อมูลสัญญา” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การอ่านข้อมูลสัญญา” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Web3 & DApp Development Fundamentals ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Web3 & DApp Development Fundamentals มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การอ่านข้อมูลสัญญา”
เรียกใช้ฟังก์ชัน view คุณปฏิบัติ Web3 & DApp Development Fundamentals ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Web3 & DApp Development Fundamentals หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Web3 & DApp Development Fundamentals บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “การอ่านข้อมูลสัญญา” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Web3 & DApp Development Fundamentals นี้ได้ไหม
ได้ บทเรียน Web3 & DApp Development Fundamentals ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การเชื่อมต่อกับผู้ให้บริการ
- การอ่านข้อมูลสัญญา
- การส่งธุรกรรม
- การรับฟังเหตุการณ์