forked from Cardinal-Cryptography/PSP22
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
196 lines (178 loc) · 5.33 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#![cfg_attr(not(feature = "std"), no_std, no_main)]
mod data;
mod errors;
mod testing;
mod traits;
pub use data::{PSP22Data, PSP22Event};
pub use errors::PSP22Error;
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) defining the correct Transfer and Approval events
// (4) implementing PSP22 trait based on PSP22Data methods
// (5) properly emitting resulting events
//
// It is a good practice to also implement the optional PSP22Metadata extension (6)
// and include unit tests (7).
#[cfg(feature = "contract")]
#[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 {
Self {
data: PSP22Data::new(supply, Self::env().caller()), // (2)
name,
symbol,
decimals,
}
}
// A helper function translating a vector of PSP22Events into the proper
// ink event types (defined internally in this contract) and emitting them.
// (5)
fn emit_events(&self, events: Vec<PSP22Event>) {
for event in events {
match event {
PSP22Event::Transfer { from, to, value } => {
self.env().emit_event(Transfer { from, to, value })
}
PSP22Event::Approval {
owner,
spender,
amount,
} => self.env().emit_event(Approval {
owner,
spender,
amount,
}),
}
}
}
}
// (3)
#[ink(event)]
pub struct Approval {
#[ink(topic)]
owner: AccountId,
#[ink(topic)]
spender: AccountId,
amount: u128,
}
// (3)
#[ink(event)]
pub struct Transfer {
#[ink(topic)]
from: Option<AccountId>,
#[ink(topic)]
to: Option<AccountId>,
value: u128,
}
// (4)
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(())
}
}
// (6)
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
}
}
// (7)
#[cfg(test)]
mod tests {
crate::tests!(Token, (|supply| Token::new(supply, None, None, 0)));
}
}