Skip to content

Commit 1b74920

Browse files
committed
added events docs
1 parent d02eff8 commit 1b74920

File tree

2 files changed

+55
-4
lines changed

2 files changed

+55
-4
lines changed

.vitepress/config.mts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,8 @@ export default defineConfig({
3535
{ text: 'Cheat Sheet', link: '/smart-contracts/contract-cheat-sheet' },
3636
{ text: 'Context', link: '/smart-contracts/context' },
3737
{ text: 'Functions', link: '/smart-contracts/functions' },
38-
{
39-
text: 'Modules',
40-
collapsed: true,
41-
items: [
38+
{ text: 'Events', link: '/smart-contracts/events' },
39+
{ text: 'Modules', collapsed: true, items: [
4240
{ text: 'Crypto', link: '/smart-contracts/modules/crypto-stdlib' },
4341
{ text: 'Hashlib', link: '/smart-contracts/modules/hashlib-stdlib' },
4442
{ text: 'Random', link: '/smart-contracts/modules/random-stdlib' },

src/smart-contracts/events.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
LogEvents are used to track & index when your smart contract performs an operation.
2+
3+
Emitted events are automatically recorded & indexed by the `Blockchain Data Service Node (BDS)` & queryable via the
4+
5+
# Use-cases
6+
7+
In many situations in your app, you will want to track when something happens in your smart contract.
8+
9+
Here are some example situations when you may want to emit an event from your contract :
10+
11+
- When a swap occurs on a DEX.
12+
- When an NFT is minted.
13+
- When a transfer is received by a specific account.
14+
- When a transfer is sent from a specific account.
15+
- When an account approves tokens to be used by a smart contract.
16+
17+
# Usage
18+
19+
### Smart Contract
20+
21+
```python
22+
"""
23+
Declare the event at the top of the contract code.
24+
"""
25+
26+
TransferEvent = LogEvent(
27+
event="Transfer",
28+
params={
29+
"from": {'type': str, 'idx': True},
30+
"to": {'type': str, 'idx': True},
31+
"amount": {'type': (int, float, decimal)}
32+
}
33+
)
34+
35+
"""
36+
At the end of the function in the operation you want to track,
37+
call the event.
38+
"""
39+
40+
@export
41+
def transfer(amount: float, to: str):
42+
...
43+
balances[ctx.caller] -= amount
44+
balances[to] += amount
45+
46+
TransferEvent({
47+
"from": ctx.caller,
48+
"to": to,
49+
"amount": amount
50+
})
51+
```
52+
53+
For info on how to query events, see the [GraphQL](../node/interfaces/graphql) docs.

0 commit comments

Comments
 (0)