Skip to content
Open
Changes from 1 commit
Commits
Show all changes
71 commits
Select commit Hold shift + click to select a range
0ce5e68
Create readme.md
Mosamorphing Oct 15, 2024
9001155
Update readme.md
Mosamorphing Oct 15, 2024
968cdb0
Create next-init-page.png
Mosamorphing Oct 15, 2024
5a1d099
Add files via upload
Mosamorphing Oct 15, 2024
49b9984
Merge branch 'WTFAcademy:main' into main
Mosamorphing Oct 31, 2024
32a7327
Update readme.md
Mosamorphing Nov 7, 2024
c8c0e2e
Add files via upload
Mosamorphing Nov 7, 2024
6c821e0
Update readme.md
Mosamorphing Nov 7, 2024
b397f2a
Update readme.md
Mosamorphing Nov 7, 2024
28bb3ae
Add files via upload
Mosamorphing Nov 7, 2024
e69c433
Update readme.md
Mosamorphing Nov 7, 2024
695c590
Create Web3.tsx
Mosamorphing Nov 7, 2024
b201f12
Create Web3.tsx
Mosamorphing Nov 7, 2024
72286cd
Create readme.md
Mosamorphing Nov 7, 2024
57793c4
Create faucet.png
Mosamorphing Nov 7, 2024
c47196b
Add files via upload
Mosamorphing Nov 7, 2024
86c3159
Create readme.md
Mosamorphing Nov 7, 2024
42c7ea4
Create connect.png
Mosamorphing Nov 7, 2024
7ad6344
Add files via upload
Mosamorphing Nov 7, 2024
8688fd4
Create Web3.tsx
Mosamorphing Nov 7, 2024
e48759d
Create readme.md
Mosamorphing Nov 7, 2024
1719674
Create metamask.png
Mosamorphing Nov 7, 2024
dc8244c
Add files via upload
Mosamorphing Nov 7, 2024
3576a8c
Create readme.md
Mosamorphing Nov 7, 2024
7df8b7d
Create Web3.tsx
Mosamorphing Nov 7, 2024
4ca95b5
Create Web3.tsx
Mosamorphing Nov 7, 2024
0e1310b
Add files via upload
Mosamorphing Nov 7, 2024
6471fa4
Update readme.md
Mosamorphing Nov 7, 2024
801b2ea
Update readme.md
Mosamorphing Nov 7, 2024
a91bfdd
Update readme.md
Mosamorphing Nov 7, 2024
4fdacac
Update readme.md
Mosamorphing Nov 7, 2024
0e42342
Create readme.md
Mosamorphing Nov 7, 2024
59aa147
Update readme.md
Mosamorphing Nov 7, 2024
bca70dc
Create createnew.png
Mosamorphing Nov 7, 2024
576e25f
Add files via upload
Mosamorphing Nov 7, 2024
fd553fd
Update readme.md
Mosamorphing Nov 7, 2024
e956587
Create readme.md
Mosamorphing Nov 7, 2024
8b9e281
Create remix.png
Mosamorphing Nov 7, 2024
d5d50d7
Add files via upload
Mosamorphing Nov 7, 2024
aca0921
Update readme.md
Mosamorphing Nov 7, 2024
655a975
Update readme.md
Mosamorphing Nov 7, 2024
93709f8
Create MyToken.sol
Mosamorphing Nov 7, 2024
705cd61
Update MyToken.sol
Mosamorphing Nov 7, 2024
4a0db7d
Create readme.md
Mosamorphing Nov 8, 2024
f26ae02
Create connect1.png
Mosamorphing Nov 8, 2024
086b794
Add files via upload
Mosamorphing Nov 8, 2024
a442acc
Create Web3.tsx
Mosamorphing Nov 8, 2024
c213e51
Create readme.md
Mosamorphing Nov 8, 2024
cdf62a5
Create Web3.tsx
Mosamorphing Nov 8, 2024
8d419b2
Create demo.png
Mosamorphing Nov 8, 2024
6e10641
Add files via upload
Mosamorphing Nov 8, 2024
fdb6e7b
Create readme.md
Mosamorphing Nov 8, 2024
773127c
Create walletconnect.png
Mosamorphing Nov 8, 2024
d42c522
Add files via upload
Mosamorphing Nov 8, 2024
a6c3a51
Create Web3.tsx
Mosamorphing Nov 8, 2024
0c8eed4
Update readme.md
Mosamorphing Nov 8, 2024
812cd05
Create readme.md
Mosamorphing Nov 8, 2024
ab6b551
Create Web3.tsx
Mosamorphing Nov 8, 2024
8a17bd4
Create image.png
Mosamorphing Nov 8, 2024
4e7b65e
Add files via upload
Mosamorphing Nov 8, 2024
fd138c0
Create readme.md
Mosamorphing Nov 8, 2024
7e32b05
Create demo.png
Mosamorphing Nov 11, 2024
8dc6b77
Add files via upload
Mosamorphing Nov 11, 2024
85c92af
Create readme.md
Mosamorphing Nov 11, 2024
ec96757
Update readme.md
Mosamorphing Nov 11, 2024
da04bc5
Create send.png
Mosamorphing Nov 11, 2024
b542ecb
Add files via upload
Mosamorphing Nov 11, 2024
81dd076
Create readme.md
Mosamorphing Nov 11, 2024
06247a9
Create localnode.png
Mosamorphing Nov 11, 2024
5ba4c01
Add files via upload
Mosamorphing Nov 11, 2024
0bebb85
Create readme.md
Mosamorphing Nov 11, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Create readme.md
  • Loading branch information
Mosamorphing authored Nov 7, 2024
commit 3576a8c724f905f4b60d40f44f637acbc9467768
98 changes: 98 additions & 0 deletions languages/en/05_Events/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
Translated by:[@Mofasasi](https://x.com/mofasasi)

In this lecture, we will guide you to quickly create a React project and display an NFT image in it.

---

This section will explain how to monitor contract events within a DApp and update its interface in real-time.

## Introduction

In blockchain smart contracts, the concept of events is somewhat distinct from how events are understood in traditional application development. Blockchain lacks a built-in messaging system to send events to applications. Instead, events are essentially abstractions of logs generated on the Ethereum Virtual Machine (EVM).

Compared to direct state changes in smart contracts, these log-based events are more cost-effective, serving as an economical method for data storage. An event typically uses about 2000 gas, while storing a new variable on the blockchain requires at least 20,000 gas. Consequently, events are often used in smart contracts to document significant state changes. Moreover, by leveraging the RPC interfaces provided by node services, the frontend of a DApp can listen to these contract events and update the interface in near real-time.

## How to Add Events in Smart Contracts

In smart contracts, events are defined with the `event` keyword and activated using the `emit` command. Here's a straightforward example to illustrate this concept. We will offer a more detailed explanation in our upcoming contract development courses.

```diff
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/access/Ownable.sol";

contract MyToken is ERC721, Ownable {
uint256 private _nextTokenId = 0;
+ event Minted(address minter, uint256 amount);
constructor()
ERC721("MyToken", "MTK")
Ownable(msg.sender)
{}
function mint(uint256 quantity) public payable {
require(quantity == 1, "quantity must be 1");
require(msg.value == 0.01 ether, "must pay 0.01 ether");
uint256 tokenId = _nextTokenId++;
_mint(msg.sender, tokenId);
+ emit Minted(msg.sender, quantity);
}
}
```

## How to Listen to Events in a DApp

To listen to contract events in a DApp's front-end, we can utilize the RPC interface provided by the node service. For this example, we'll continue using `wagmi` for our development needs.

We'll start by implementing the [useWatchContractEvent](https://wagmi.sh/react/api/hooks/useWatchContractEvent#abi) hook.

```diff
import {
createConfig,
http,
useReadContract,
useWriteContract,
+ useWatchContractEvent,
} from "wagmi";
```

To monitor contract events, use `useWatchContractEvent`.

```ts
useWatchContractEvent({
address: "0xEcd0D12E21805803f70de03B72B1C162dB0898d9",
abi: [
{
anonymous: false,
inputs: [
{
indexed: false,
internalType: "address",
name: "minter",
type: "address",
},
{
indexed: false,
internalType: "uint256",
name: "amount",
type: "uint256",
},
],
name: "Minted",
type: "event",
},
],
eventName: "Minted",
onLogs() {
message.success("new minted!");
},
});
```

When using an SDK like wagmi, the approach is quite similar to traditional front-end development. You start by entering the contract address and the event you wish to monitor. Then, you can handle these events through the `onLogs` callback function.

It's important to note that the code provided here is for demonstration purposes only and may not encompass all practical applications. In a real-world scenario, you would need to determine the specific details of the event within `onLogs` and update your page accordingly.

Typically, the `abi` parameter is generated automatically when compiling the contract. Instead of manually writing it each time, it's best to maintain a complete version within your project and use it as needed.

Since events only occur after a contract is invoked, we can't debug this right now. You should continue with the course material and return to test this once you're able to deploy a test contract in a testing environment.