-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtokencreation.rs
222 lines (186 loc) · 7.15 KB
/
tokencreation.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#![cfg_attr(not(feature = "std"), no_std)]
use ink_lang as ink;
#[ink::contract]
mod token_factory {
use ink_storage::collections::HashMap as StorageHashMap;
#[ink(event)]
pub struct TokenCreated {
#[ink(topic)]
pub token_address: AccountId,
pub name: String,
pub symbol: String,
pub max_supply: Balance,
pub burnable: bool,
}
#[ink(storage)]
pub struct TokenFactory {
owner: AccountId,
created_tokens: StorageHashMap<AccountId, AccountId>,
}
impl TokenFactory {
#[ink(constructor)]
pub fn new() -> Self {
let caller = Self::env().caller();
Self {
owner: caller,
created_tokens: StorageHashMap::new(),
}
}
#[ink(message)]
pub fn create_token(
&mut self,
name: String,
symbol: String,
decimals: u8,
max_supply: Balance,
burnable: bool,
) -> AccountId {
let new_token = CustomToken::new(
name.clone(),
symbol.clone(),
decimals,
max_supply,
burnable,
self.env().caller(),
);
let token_address = new_token.account_id();
self.created_tokens.insert(self.env().caller(), token_address);
self.env().emit_event(TokenCreated {
token_address,
name,
symbol,
max_supply,
burnable,
});
token_address
}
#[ink(message)]
pub fn withdraw_erc20(&mut self, token_address: AccountId, amount: Balance) {
self.only_owner();
let token = ink_env::call::build_call::<Environment>()
.call_type(ink_env::call::Call::FromAccountId)
.call_address(token_address)
.push_arg(amount)
.call();
token.transact();
}
#[ink(message)]
pub fn withdraw_ether(&mut self, amount: Balance) {
self.only_owner();
ink_env::transfer(self.owner, amount).expect("Transfer failed");
}
fn only_owner(&self) {
assert_eq!(self.env().caller(), self.owner, "Only the owner can execute this");
}
}
#[ink::contract]
pub mod custom_token {
use ink_storage::collections::HashMap as StorageHashMap;
#[ink(storage)]
pub struct CustomToken {
name: String,
symbol: String,
decimals: u8,
max_supply: Balance,
total_supply: Balance,
burnable: bool,
owner: AccountId,
balances: StorageHashMap<AccountId, Balance>,
allowances: StorageHashMap<(AccountId, AccountId), Balance>,
}
impl CustomToken {
#[ink(constructor)]
pub fn new(
name: String,
symbol: String,
decimals: u8,
max_supply: Balance,
burnable: bool,
owner: AccountId,
) -> Self {
let mut balances = StorageHashMap::new();
balances.insert(owner, max_supply);
Self {
name,
symbol,
decimals,
max_supply,
total_supply: max_supply,
burnable,
owner,
balances,
allowances: StorageHashMap::new(),
}
}
#[ink(message)]
pub fn total_supply(&self) -> Balance {
self.total_supply
}
#[ink(message)]
pub fn balance_of(&self, account: AccountId) -> Balance {
*self.balances.get(&account).unwrap_or(&0)
}
#[ink(message)]
pub fn transfer(&mut self, recipient: AccountId, amount: Balance) -> bool {
let caller = self.env().caller();
let sender_balance = self.balance_of(caller);
assert!(sender_balance >= amount, "Insufficient balance");
self.balances.insert(caller, sender_balance - amount);
let recipient_balance = self.balance_of(recipient);
self.balances.insert(recipient, recipient_balance + amount);
true
}
#[ink(message)]
pub fn approve(&mut self, spender: AccountId, amount: Balance) -> bool {
let caller = self.env().caller();
self.allowances.insert((caller, spender), amount);
true
}
#[ink(message)]
pub fn transfer_from(
&mut self,
sender: AccountId,
recipient: AccountId,
amount: Balance,
) -> bool {
let caller = self.env().caller();
let sender_balance = self.balance_of(sender);
let allowance = *self.allowances.get(&(sender, caller)).unwrap_or(&0);
assert!(sender_balance >= amount, "Insufficient balance");
assert!(allowance >= amount, "Allowance exceeded");
self.balances.insert(sender, sender_balance - amount);
let recipient_balance = self.balance_of(recipient);
self.balances.insert(recipient, recipient_balance + amount);
self.allowances.insert((sender, caller), allowance - amount);
true
}
#[ink(message)]
pub fn mint(&mut self, to: AccountId, amount: Balance) {
assert_eq!(self.env().caller(), self.owner, "Only the owner can mint");
let new_supply = self.total_supply + amount;
assert!(new_supply <= self.max_supply, "Max supply exceeded");
self.total_supply = new_supply;
let to_balance = self.balance_of(to);
self.balances.insert(to, to_balance + amount);
}
#[ink(message)]
pub fn burn(&mut self, amount: Balance) {
assert_eq!(self.env().caller(), self.owner, "Only the owner can burn");
assert!(self.burnable, "Burning not allowed");
let caller_balance = self.balance_of(self.env().caller());
assert!(caller_balance >= amount, "Insufficient balance to burn");
self.balances.insert(self.env().caller(), caller_balance - amount);
self.total_supply -= amount;
}
#[ink(message)]
pub fn burn_from(&mut self, account: AccountId, amount: Balance) {
assert_eq!(self.env().caller(), self.owner, "Only the owner can burn");
assert!(self.burnable, "Burning not allowed");
let account_balance = self.balance_of(account);
assert!(account_balance >= amount, "Insufficient balance to burn");
self.balances.insert(account, account_balance - amount);
self.total_supply -= amount;
}
}
}
}