Skip to content

Commit e5a17da

Browse files
author
Earnwithalee7890
committed
docs: Add complete SIP-010 fungible token implementation example
1 parent 22bd448 commit e5a17da

1 file changed

Lines changed: 182 additions & 0 deletions

File tree

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
# SIP-010 Fungible Token Example
2+
3+
A complete implementation of the SIP-010 standard for fungible tokens on Stacks.
4+
5+
## Overview
6+
7+
This example demonstrates how to create a fungible token that implements the [SIP-010 standard](https://github.com/stacksgov/sips/blob/main/sips/sip-010/sip-010-fungible-token-standard.md), making it compatible with wallets, exchanges, and other ecosystem tools.
8+
9+
## Complete Contract
10+
11+
```clarity
12+
;; SIP-010 Fungible Token Example
13+
;; A simple implementation of the SIP-010 standard
14+
15+
(impl-trait 'SP3FBR2AGK5H9QBDH3EEN6DF8EK8JY7RX8QJ5SVTE.sip-010-trait-ft-standard.sip-010-trait)
16+
17+
(define-fungible-token reward-token u1000000000)
18+
19+
(define-constant contract-owner tx-sender)
20+
(define-constant err-owner-only (err u100))
21+
(define-constant err-not-token-owner (err u101))
22+
23+
;; SIP-010 Functions
24+
25+
(define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34))))
26+
(begin
27+
(asserts! (is-eq tx-sender sender) err-not-token-owner)
28+
(try! (ft-transfer? reward-token amount sender recipient))
29+
(match memo to-print (print to-print) 0x)
30+
(ok true)
31+
)
32+
)
33+
34+
(define-read-only (get-name)
35+
(ok "Reward Token")
36+
)
37+
38+
(define-read-only (get-symbol)
39+
(ok "RWD")
40+
)
41+
42+
(define-read-only (get-decimals)
43+
(ok u6)
44+
)
45+
46+
(define-read-only (get-balance (who principal))
47+
(ok (ft-get-balance reward-token who))
48+
)
49+
50+
(define-read-only (get-total-supply)
51+
(ok (ft-get-supply reward-token))
52+
)
53+
54+
(define-read-only (get-token-uri)
55+
(ok (some u"https://example.com/token-metadata.json"))
56+
)
57+
58+
;; Mint function (only owner)
59+
(define-public (mint (amount uint) (recipient principal))
60+
(begin
61+
(asserts! (is-eq tx-sender contract-owner) err-owner-only)
62+
(ft-mint? reward-token amount recipient)
63+
)
64+
)
65+
```
66+
67+
## Key Features
68+
69+
### 1. Trait Implementation
70+
The contract implements the SIP-010 trait, ensuring compatibility:
71+
```clarity
72+
(impl-trait 'SP3FBR2AGK5H9QBDH3EEN6DF8EK8JY7RX8QJ5SVTE.sip-010-trait-ft-standard.sip-010-trait)
73+
```
74+
75+
### 2. Token Transfer
76+
Allows users to transfer tokens with optional memo:
77+
```clarity
78+
(define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34))))
79+
(begin
80+
(asserts! (is-eq tx-sender sender) err-not-token-owner)
81+
(try! (ft-transfer? reward-token amount sender recipient))
82+
(match memo to-print (print to-print) 0x)
83+
(ok true)
84+
)
85+
)
86+
```
87+
88+
### 3. Read-Only Functions
89+
Required metadata functions for token information:
90+
- `get-name`: Returns token name
91+
- `get-symbol`: Returns token symbol
92+
- `get-decimals`: Returns decimal places
93+
- `get-balance`: Returns balance for an address
94+
- `get-total-supply`: Returns total token supply
95+
- `get-token-uri`: Returns metadata URI
96+
97+
### 4. Access Control
98+
Only the contract owner can mint new tokens:
99+
```clarity
100+
(define-public (mint (amount uint) (recipient principal))
101+
(begin
102+
(asserts! (is-eq tx-sender contract-owner) err-owner-only)
103+
(ft-mint? reward-token amount recipient)
104+
)
105+
)
106+
```
107+
108+
## Usage Examples
109+
110+
### Deploying the Contract
111+
```bash
112+
clarinet contract deploy token
113+
```
114+
115+
### Minting Tokens
116+
```clarity
117+
(contract-call? .reward-token mint u1000000 'ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM)
118+
```
119+
120+
### Transferring Tokens
121+
```clarity
122+
(contract-call? .reward-token transfer
123+
u1000
124+
tx-sender
125+
'ST2CY5V39NHDPWSXMW9QDT3HC3GD6Q6XX4CFRK9AG
126+
none)
127+
```
128+
129+
### Checking Balance
130+
```clarity
131+
(contract-call? .reward-token get-balance tx-sender)
132+
```
133+
134+
## Customization
135+
136+
You can customize this template by:
137+
138+
1. **Changing token properties**:
139+
- Modify `get-name` for your token name
140+
- Modify `get-symbol` for your token symbol
141+
- Adjust `get-decimals` for precision
142+
143+
2. **Adjusting supply**:
144+
- Change max supply in `define-fungible-token`
145+
146+
3. **Adding features**:
147+
- Burn functionality
148+
- Pausable transfers
149+
- Allowance system
150+
151+
## Testing
152+
153+
Test your token with Clarinet:
154+
155+
```toml
156+
# Clarinet.toml
157+
[contracts.reward-token]
158+
path = "contracts/reward-token.clar"
159+
```
160+
161+
Run tests:
162+
```bash
163+
clarinet test
164+
```
165+
166+
## Best Practices
167+
168+
1. **Always validate sender**: Ensure `tx-sender` matches the `sender` parameter
169+
2. **Use error codes**: Define clear error codes for debugging
170+
3. **Implement all trait functions**: Required for SIP-010 compatibility
171+
4. **Add access control**: Protect sensitive functions like `mint`
172+
5. **Test thoroughly**: Test all functions before mainnet deployment
173+
174+
## Resources
175+
176+
- [SIP-010 Specification](https://github.com/stacksgov/sips/blob/main/sips/sip-010/sip-010-fungible-token-standard.md)
177+
- [Clarity Language Reference](https://docs.stacks.co/clarity)
178+
- [Fungible Tokens Guide](https://docs.stacks.co/write-smart-contracts/tokens)
179+
180+
## License
181+
182+
This example is provided as-is for educational purposes.

0 commit comments

Comments
 (0)