-
Notifications
You must be signed in to change notification settings - Fork 12
/
lib.rs
169 lines (153 loc) · 4.66 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#![cfg_attr(not(feature = "std"), no_std, no_main)]
mod data;
mod errors;
mod events;
mod testing;
mod traits;
pub use data::{PSP22Data, PSP22Event};
pub use errors::PSP22Error;
pub use events::{Approval, Transfer};
pub use traits::{PSP22Burnable, PSP22Metadata, PSP22Mintable, PSP22};
// An example code of a smart contract using PSP22Data struct to implement
// the functionality of PSP22 fungible token.
//
// Any contract can be easily enriched to act as PSP22 token by:
// (1) adding PSP22Data to contract storage
// (2) properly initializing it
// (3) implementing PSP22 trait based on PSP22Data methods
// (4) properly emitting resulting events
//
// It is a good practice to also implement the optional PSP22Metadata extension (5)
// and include unit tests (6).
#[ink::contract]
mod token {
use crate::{PSP22Data, PSP22Error, PSP22Event, PSP22Metadata, PSP22};
use ink::prelude::{string::String, vec::Vec};
#[ink(storage)]
pub struct Token {
data: PSP22Data, // (1)
name: Option<String>,
symbol: Option<String>,
decimals: u8,
}
impl Token {
#[ink(constructor)]
pub fn new(
supply: u128,
name: Option<String>,
symbol: Option<String>,
decimals: u8,
) -> Self {
let (data, events) = PSP22Data::new(supply, Self::env().caller()); // (2)
let contract = Self {
data,
name,
symbol,
decimals,
};
contract.emit_events(events);
contract
}
// A helper function emitting events contained in a vector of PSP22Events.
// (4)
fn emit_events(&self, events: Vec<PSP22Event>) {
for event in events {
match event {
PSP22Event::Transfer(e) => self.env().emit_event(e),
PSP22Event::Approval(e) => self.env().emit_event(e),
}
}
}
}
// (3)
impl PSP22 for Token {
#[ink(message)]
fn total_supply(&self) -> u128 {
self.data.total_supply()
}
#[ink(message)]
fn balance_of(&self, owner: AccountId) -> u128 {
self.data.balance_of(owner)
}
#[ink(message)]
fn allowance(&self, owner: AccountId, spender: AccountId) -> u128 {
self.data.allowance(owner, spender)
}
#[ink(message)]
fn transfer(
&mut self,
to: AccountId,
value: u128,
_data: Vec<u8>,
) -> Result<(), PSP22Error> {
let events = self.data.transfer(self.env().caller(), to, value)?;
self.emit_events(events);
Ok(())
}
#[ink(message)]
fn transfer_from(
&mut self,
from: AccountId,
to: AccountId,
value: u128,
_data: Vec<u8>,
) -> Result<(), PSP22Error> {
let events = self
.data
.transfer_from(self.env().caller(), from, to, value)?;
self.emit_events(events);
Ok(())
}
#[ink(message)]
fn approve(&mut self, spender: AccountId, value: u128) -> Result<(), PSP22Error> {
let events = self.data.approve(self.env().caller(), spender, value)?;
self.emit_events(events);
Ok(())
}
#[ink(message)]
fn increase_allowance(
&mut self,
spender: AccountId,
delta_value: u128,
) -> Result<(), PSP22Error> {
let events = self
.data
.increase_allowance(self.env().caller(), spender, delta_value)?;
self.emit_events(events);
Ok(())
}
#[ink(message)]
fn decrease_allowance(
&mut self,
spender: AccountId,
delta_value: u128,
) -> Result<(), PSP22Error> {
let events = self
.data
.decrease_allowance(self.env().caller(), spender, delta_value)?;
self.emit_events(events);
Ok(())
}
}
// (5)
impl PSP22Metadata for Token {
#[ink(message)]
fn token_name(&self) -> Option<String> {
self.name.clone()
}
#[ink(message)]
fn token_symbol(&self) -> Option<String> {
self.symbol.clone()
}
#[ink(message)]
fn token_decimals(&self) -> u8 {
self.decimals
}
}
// (6)
#[cfg(test)]
mod tests {
use super::Token;
crate::tests!(Token, (|supply| Token::new(supply, None, None, 0)));
}
}