การจัดการธุรกรรมและเหตุการณ์
เรียนรู้การส่งธุรกรรมจาก DApp การจัดการการยืนยันธุรกรรม และการรับฟังเหตุการณ์ของสัญญาอัจฉริยะเพื่ออัปเดตส่วนติดต่อผู้ใช้
การจัดการธุรกรรมและเหตุการณ์ เป็นบทเรียน Web3 & DApp Development Fundamentals ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 3 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Web3 & DApp Development Fundamentals และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Web3 & DApp Development Fundamentals มีบทเรียนทั้งหมด 3 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Making DApps Dynamic
So far, you've learned how to connect your DApp to the blockchain and read data. But what if you want to change data or perform actions?
This lesson teaches you how to send transactions from your DApp to modify blockchain state and how to listen for real-time updates using smart contract events.
What is a Blockchain Transaction?
A blockchain transaction is a signed message that changes the state of the blockchain. Every transaction costs a small fee called gas, paid in Ether (ETH).
Common DApp transactions include:
- Sending Ether to another address.
- Calling a smart contract function that writes or modifies data.
- Deploying a new smart contract.
Sending Transactions from Your DApp
To send a transaction that calls a smart contract function, your DApp needs to:
- Connect to a blockchain provider (e.g., MetaMask).
- Get a signer (the connected user's wallet).
- Call the desired contract function, passing any required parameters.
The user's wallet will then prompt them to confirm and sign the transaction.
Calling a Write Function Example
Here's how you might call a simple setValue function on a smart contract using Ethers.js. Remember, contract refers to your instantiated contract object.
// Assuming 'contract' is an Ethers.js Contract instance
// and 'signer' is obtained from the connected wallet.
async function updateValue(newValue) {
try {
const tx = await contract.connect(signer).setValue(newValue);
console.log("Transaction sent: ", tx.hash);
// Wait for the transaction to be mined
await tx.wait();
console.log("Transaction confirmed!");
} catch (error) {
console.error("Error sending transaction:", error);
}
}Waiting for Transaction Confirmation
After sending a transaction, it's not immediately processed. It goes into a transaction pool and waits to be included in a block by a miner or validator.
The await tx.wait() call is crucial. It pauses your DApp's execution until the transaction is mined and confirmed on the blockchain, ensuring your UI reflects the latest state.
Understanding Transaction Receipts
When tx.wait() resolves, it returns a transaction receipt. This receipt contains important information about the transaction's outcome:
blockHashandblockNumber: Where it was included.gasUsed: The actual gas consumed.status: Indicates if the transaction was successful (1) or failed (0).
Always check the status to ensure the contract call executed as expected!
Introducing Smart Contract Events
Smart contract events are a powerful mechanism for your contracts to 'broadcast' information to external applications (like your DApp) when something significant happens.
Think of them as logs or notifications that DApps can listen to. They are a cost-effective way to communicate data from the blockchain without storing it directly on-chain.
Emitting Events in Solidity
In Solidity, you first define an event with its parameters, then use the emit keyword to trigger it within a function. Here's a simple example:
pragma solidity ^0.8.0;
contract MyContract {
uint public value;
// Define an event
event ValueChanged(address indexed user, uint oldValue, uint newValue);
function setValue(uint _newValue) public {
uint _oldValue = value;
value = _newValue;
// Emit the event
emit ValueChanged(msg.sender, _oldValue, _newValue);
}
}Listening for Events in Your DApp
Your DApp can subscribe to specific events emitted by a contract. When an event occurs, your DApp receives the event data, allowing you to update the UI or trigger other actions in real-time.
This avoids constant 'polling' (repeatedly asking the blockchain for updates), making your DApp more efficient.
// Assuming 'contract' is an Ethers.js Contract instance
contract.on("ValueChanged", (user, oldValue, newValue, event) => {
console.log(`Value changed by ${user}`);
console.log(`From ${oldValue} to ${newValue}`);
// Update your DApp's UI here, e.g., display the new value
updateDisplay(newValue);
});
console.log("Listening for ValueChanged events...");Quick Check: Tx & Events
When calling a smart contract function that modifies state from your DApp, what is the primary purpose of using await tx.wait()?
Recap & Next Steps
You've learned how to make your DApps truly interactive!
- You can now send transactions to modify blockchain state.
- You know how to wait for confirmation and handle transaction receipts.
- You understand how smart contract events provide real-time updates to your DApp.
These skills are fundamental for building dynamic and responsive decentralized applications. Keep practicing to master DApp interaction!
คำถามที่พบบ่อย
บทเรียน “การจัดการธุรกรรมและเหตุการณ์” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การจัดการธุรกรรมและเหตุการณ์” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Web3 & DApp Development Fundamentals ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Web3 & DApp Development Fundamentals มีบทเรียนทั้งหมด 3 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การจัดการธุรกรรมและเหตุการณ์”
เรียนรู้การส่งธุรกรรมจาก DApp การจัดการการยืนยันธุรกรรม และการรับฟังเหตุการณ์ของสัญญาอัจฉริยะเพื่ออัปเดตส่วนติดต่อผู้ใช้ คุณปฏิบัติ Web3 & DApp Development Fundamentals ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Web3 & DApp Development Fundamentals หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Web3 & DApp Development Fundamentals บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 3 บทเรียน
บทเรียน “การจัดการธุรกรรมและเหตุการณ์” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Web3 & DApp Development Fundamentals นี้ได้ไหม
ได้ บทเรียน Web3 & DApp Development Fundamentals ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- React และเฟรมเวิร์ก Web3
- การผสานรวมกระเป๋าเงิน
- การจัดการธุรกรรมและเหตุการณ์