-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy patheosdactoken.cpp
More file actions
373 lines (288 loc) · 12.9 KB
/
eosdactoken.cpp
File metadata and controls
373 lines (288 loc) · 12.9 KB
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/**
* author chenlian@oraclechain.io
* version 1.0
* source reference eosdactoken contract,thanks eos contracts/eosdactoken contract
* this eosdactoken follow the erc2.0 norms(trans,issue,approve,transferfrom,balanceof,allowance)
* @file
* @copyright defined in eos/LICENSE.txt
*/
#include "eosdactoken.hpp"
#include"tool.hpp"
EOSIO_ABI( eosdactoken, (transfer)(create)(issue)(transferfee)(approve)(transferfrom)(balanceof)(allowance)(totalsupply)(copystates)(claim))
using std::string;
using std::array;
using namespace eosio;
void eosdactoken::checkasset(const asset &quantity){
eosio_assert( quantity.symbol.is_valid(), INVALID_SYMBOL_NAME);
auto sym = quantity.symbol.name();
Stats statstable( _self, sym );
const auto& ite = statstable.find( sym );
eosio_assert( ite != statstable.end(), TOKEN_WITH_SYMBOL_DOES_NOT_EXIST_CREATE_TOKEN_BEFORE_ISSUE);
const auto& st = *ite;
eosio_assert( quantity.is_valid(), INVALID_QUANTITY );
eosio_assert( quantity.amount > 0, MUST_ISSUE_POSITIVE_QUANTITY );
eosio_assert( quantity.symbol == st.supply.symbol, SYMBOL_PRECISION_MISMATCH);
checkoutAmount(quantity.amount);
}
void eosdactoken::issue(account_name to,
asset quantity,
string memo) {
checkasset(quantity);
eosio_assert( memo.size() <= 256, MEMO_HAS_MORE_THAN_256_BYTES);
auto sym = quantity.symbol.name();
Stats statstable( _self, sym );
const auto& ite = statstable.find( sym );
const auto& st = *ite;
eosio_assert( quantity.amount <= st.max_supply.amount - st.supply.amount, QUANTITY_EXCEEDS_AVAILABLE_SUPPLY);
require_auth( st.issuer);
statstable.modify( st, 0, [&]( auto& s ) {
s.supply += quantity;
});
add_balance( st.issuer, quantity, st.issuer);
if( to != st.issuer )
{
SEND_INLINE_ACTION( *this, transfer, {st.issuer,N(active)}, {st.issuer, to, quantity, memo} );
}
}
void eosdactoken::transferfee(
account_name from,
account_name to,
asset quantity,
account_name tofeeadmin,
asset feequantity,
string memo){
checkasset(quantity);
checkasset(feequantity);
eosio_assert( quantity.symbol == feequantity.symbol, TOKEN_SYMBOL_SHOULD_EQUAL_FEE_SYMBOL);
require_auth(from);
eosio_assert( from != to, CANNOT_TRANSFER_TO_YOURSELF);
eosio_assert( from != tofeeadmin, CANNOT_TRANSFER_TO_YOURSELF);
eosio_assert( is_account( to ), TO_ACCOUNT_DOES_NOT_EXIST);
eosio_assert( is_account( tofeeadmin ), TO_ACCOUNT_DOES_NOT_EXIST);
auto sym = quantity.symbol.name();
Stats statstable( _self, sym );
const auto& st = statstable.get( sym );
require_recipient( from );
require_recipient( to );
require_recipient( tofeeadmin );
sub_balance( from, quantity, from );
add_balance( to, quantity, from);
sub_balance( from, feequantity, from);
add_balance( tofeeadmin, feequantity, from);
}
void eosdactoken::transfer( account_name from,
account_name to,
asset quantity,
string memo) {
checkasset(quantity);
require_auth(from);
eosio_assert( from != to, CANNOT_TRANSFER_TO_YOURSELF);
eosio_assert( is_account( to ), TO_ACCOUNT_DOES_NOT_EXIST);
auto sym = quantity.symbol.name();
Stats statstable( _self, sym );
const auto& st = statstable.get( sym );
require_recipient( from );
require_recipient( to );
eosio_assert( memo.size() <= 256, MEMO_HAS_MORE_THAN_256_BYTES);
sub_balance( from, quantity, from);
add_balance( to, quantity, from);
}
void eosdactoken::sub_balance( account_name owner, asset value, uint64_t payer) {
require_auth(payer);
Accounts from_acnts( _self, owner );
const auto& from = from_acnts.get( value.symbol.name(), NO_BALANCE_OBJECT_FOUND_FOR_THIS_ACCOUNT);
eosio_assert( from.balance.amount >= value.amount, BLANCE_NOT_ENOUGH );
from_acnts.modify( from, payer, [&]( auto& a ) {
a.balance -= value;
});
}
void eosdactoken::add_balance( account_name owner, asset value, account_name payer )
{
require_auth(payer);
Accounts to_acnts( _self, owner );
auto to = to_acnts.find( value.symbol.name() );
if( to == to_acnts.end() ) {
to_acnts.emplace( payer, [&]( auto& a ){
a.balance = value;
});
} else {
to_acnts.modify( to, 0, [&]( auto& a ) {
a.balance += value;
});
}
}
void eosdactoken::approve(account_name owner,
account_name spender,
asset quantity){
eosio_assert( quantity.symbol.is_valid(), INVALID_SYMBOL_NAME);
auto sym = quantity.symbol.name();
Stats statstable( _self, sym );
const auto& ite = statstable.find( sym );
eosio_assert( ite != statstable.end(), TOKEN_WITH_SYMBOL_DOES_NOT_EXIST_CREATE_TOKEN_BEFORE_ISSUE);
const auto& st = *ite;
eosio_assert( quantity.is_valid(), INVALID_QUANTITY );
eosio_assert( quantity.amount >= 0, MUST_ISSUE_POSITIVE_QUANTITY );
eosio_assert( quantity.symbol == st.supply.symbol, SYMBOL_PRECISION_MISMATCH);
checkoutAmount(quantity.amount);
require_auth(owner);
eosio_assert( is_account( spender ), TO_ACCOUNT_DOES_NOT_EXIST);
Accounts from_acnts( _self, owner );
const auto& from = from_acnts.find( quantity.symbol.name());
eosio_assert(from!=from_acnts.end(), YOU_NOT_HAVE_THIS_TOKEN_NOW);
eosio_assert( from->balance.amount >= quantity.amount, BLANCE_NOT_ENOUGH );
Approves approveobj(_self, owner);
if(approveobj.find(quantity.symbol.name()) != approveobj.end()){
auto &approSymIte = approveobj.get(quantity.symbol.name());
approveobj.modify(approSymIte, owner, [&](auto &a){
auto approvetoPairIte = a.approved.begin();
bool finded = false;
while(approvetoPairIte != a.approved.end()){
if(approvetoPairIte->to == spender){
finded = true;
if(quantity.amount == 0){
a.approved.erase(approvetoPairIte);
}else{
approvetoPairIte->value = quantity.amount;
}
break;
}
approvetoPairIte++;
}
if(!finded && quantity.amount>0){
approvetoPair atp;
atp.to = spender;
atp.value = quantity.amount;
a.approved.push_back(atp);
}
});
}else if(quantity.amount>0){
approvetoPair atp;
atp.to = spender;
atp.value = quantity.amount;
approveobj.emplace(owner, [&](auto &a){
a.symbol_name = quantity.symbol;
a.approved.push_back(atp);
});
}
}
void eosdactoken::balanceof(account_name owner,
std::string symbol){
symbol_name sn = string_to_symbol(4, symbol.c_str());
print("balanceOf[",symbol.c_str(),"]=", get_balance(owner, symbol_type(sn).name()));
}
void eosdactoken::allowance(account_name owner,
account_name spender,
std::string symbol){
Approves approveobj(_self, owner);
symbol_type st = symbol_type(string_to_symbol(4, symbol.c_str()));
if(approveobj.find(st.name()) != approveobj.end()){
const auto &approSymIte = approveobj.get(st.name());
auto approvetoPairIte = approSymIte.approved.begin();
while(approvetoPairIte != approSymIte.approved.end()){
if(approvetoPairIte->to == spender){
print("allowanceof[", account_name(approvetoPairIte->to), "]=", approvetoPairIte->value);
return;
}
approvetoPairIte++;
}
if(approvetoPairIte == approSymIte.approved.end()){
print("allowanceOf[", account_name(spender), "]=", 0);
}
}else{
print("allowanceOf[", (account_name)spender, "]=", 0);
}
}
void eosdactoken::transferfrom(account_name owner,
account_name spender,
asset quantity){
checkasset(quantity);
require_auth(spender);
eosio_assert( is_account( owner ), TO_ACCOUNT_DOES_NOT_EXIST);
eosio_assert( quantity.amount > 0, MUST_ISSUE_POSITIVE_QUANTITY);
Approves approveobj(_self, owner);
if(approveobj.find(quantity.symbol.name()) != approveobj.end()){
const auto &approSymIte = approveobj.get(quantity.symbol.name());
approveobj.modify(approSymIte, owner, [&](auto &a){
a = approSymIte;
auto approvetoPairIte = a.approved.begin();
bool finded = false;
while(approvetoPairIte != a.approved.end()){
if(approvetoPairIte->to == spender){
finded = true;
eosio_assert(approvetoPairIte->value>=quantity.amount, NOT_ENOUGH_ALLOWED_OCT_TO_DO_IT);
eosio_assert(approvetoPairIte->value > approvetoPairIte->value-quantity.amount, NOT_ENOUGH_ALLOWED_OCT_TO_DO_IT);
approvetoPairIte->value -= quantity.amount;
checkoutAmount(approvetoPairIte->value);
if(approvetoPairIte->value == 0){
a.approved.erase(approvetoPairIte);
}
require_recipient( spender );
sub_balance( owner, quantity, spender);
add_balance( spender, quantity, spender);
break;
}
approvetoPairIte++;
}
if(!finded){
eosio_assert(false, NOT_ENOUGH_ALLOWED_OCT_TO_DO_IT);
}
});
}else{
eosio_assert(false, NOT_ENOUGH_ALLOWED_OCT_TO_DO_IT);
}
}
void eosdactoken::create( account_name issuer,
asset currency) {
require_auth( _self );
auto sym = currency.symbol;
eosio_assert( sym.is_valid(), INVALID_SYMBOL_NAME);
eosio_assert( currency.is_valid(), INVALID_QUANTITY);
eosio_assert( currency.amount>0, TOKEN_MAX_SUPPLY_MUST_POSITIVE) ;
Stats statstable( _self, sym.name() );
auto existing = statstable.find( sym.name() );
eosio_assert( existing == statstable.end(), TOKEN_WITH_SYMBOL_ALREADY_EXISTS);
statstable.emplace( _self, [&]( auto& s ) {
s.supply.symbol = currency.symbol;
s.max_supply = currency;
s.issuer = issuer;
});
}
void eosdactoken::copystates(std::string symbol){
require_auth( _self );
auto sym = symbol_type(string_to_symbol(4, symbol.c_str())).name();
Stats statstable( _self, sym );
auto existing = statstable.find( sym );
const auto& st = *existing;
eosio_assert( existing != statstable.end(), TOKEN_WITH_SYMBOL_NOT_EXISTS);
Stat stattable( _self, sym );
const auto& stdest = stattable.find(sym);
if(stdest!=stattable.end()){
stattable.modify( stdest, 0, [&]( auto& s ) {
s.supply = st.supply;
s.max_supply = st.max_supply;
s.issuer = st.issuer;
});
}else{
stattable.emplace( _self, [&]( auto& s ) {
s.supply = st.supply;
s.max_supply = st.max_supply;
s.issuer = st.issuer;
});
}
}
void eosdactoken::claim(account_name claimer, std::string symbol)
{
require_auth( claimer );
auto symt = symbol_type(string_to_symbol(4, symbol.c_str()));
auto sym = symt.name();
Stat statstable( _self, sym );
auto existing = statstable.find( sym );
eosio_assert(existing != statstable.end(), TOKEN_WITH_SYMBOL_NOT_EXISTS);
Accounts acnts( _self, claimer );
auto it = acnts.find( sym );
if( it == acnts.end() ) {
acnts.emplace( claimer, [&]( auto& a ){
a.balance = asset{0, symt};
});
}
}