forked from DevToothCrew/DevTooth_NFT_Tokens
-
Notifications
You must be signed in to change notification settings - Fork 0
/
devtooth_nft.cpp
390 lines (322 loc) · 14 KB
/
devtooth_nft.cpp
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#include "devtooth_nft.hpp"
namespace eosio {
using std::string;
using eosio::asset;
void devtooth_nft::create( account_name issuer, string sym ) {
require_auth( _self );
// Check if issuer account exists
eosio_assert( is_account( issuer ), "issuer account does not exist");
// Valid symbol
asset supply(0, string_to_symbol(0, sym.c_str()));
auto symbol = supply.symbol;
eosio_assert( symbol.is_valid(), "invalid symbol name" );
eosio_assert( supply.is_valid(), "invalid supply");
// Check if currency with symbol already exists
currency_index currency_table( _self, symbol.name() );
auto existing_currency = currency_table.find( symbol.name() );
eosio_assert( existing_currency == currency_table.end(), "token with symbol already exists" );
// Create new currency
currency_table.emplace( _self, [&]( auto& currency ) {
currency.supply = supply;
currency.issuer = issuer;
});
}
void devtooth_nft::issue(account_name to, asset quantity, uint64_t index)
{
eosio_assert( is_account( to ), "to account does not exist");
// e,g, Get EOS from 3 EOS
symbol_type symbol = quantity.symbol;
eosio_assert( symbol.is_valid(), "invalid symbol name" );
eosio_assert( symbol.precision() == 0, "quantity must be a whole number" );
// Ensure currency has been created
auto symbol_name = symbol.name();
currency_index currency_table( _self, symbol_name );
auto existing_currency = currency_table.find( symbol_name );
eosio_assert( existing_currency != currency_table.end(), "token with symbol does not exist. create token before issue" );
const auto& st = *existing_currency;
// Ensure have issuer authorization and valid quantity
//require_auth( to );
require_auth(to);
eosio_assert( quantity.is_valid(), "invalid quantity" );
eosio_assert( quantity.amount > 0, "must issue positive quantity of uts" );
eosio_assert( symbol == st.supply.symbol, "symbol precision mismatch" );
// Case UTS
if( symbol == S(0, UTS)){
// Get Servant info
servant_table servant(N(unlimittest1), to);
auto servant_iter = servant.get(index, "Not exist Servant");
// Check Token duplication
auto uts_list = s_tokens.get_index<N(byowner)>();
bool not_exist = true;
for(auto it = uts_list.begin(); it != uts_list.end(); ++it){
if(it->t_idx == servant_iter.index){
not_exist = false;
break;
}
}
eosio_assert(not_exist, "Already exist Token");
// Add token with creator paying for RAM
s_tokens.emplace( to, [&]( auto& token ) {
token.idx = s_tokens.available_primary_key();
token.t_idx = index;
token.state = "idle";
token.owner = to;
token.master = to;
token.value = asset{1, symbol};
});
}
// Case UTM
if( symbol == S(0, UTM)){
// Get Monster info
monster_table monster(N(unlimittest1), to);
auto monster_iter = monster.get(index, "Not exist Monster");
// Check Token duplication
auto utm_list = m_tokens.get_index<N(byowner)>();
bool not_exist = true;
for(auto it = utm_list.begin(); it != utm_list.end(); ++it){
if(it->t_idx == monster_iter.index){
not_exist = false;
break;
}
}
eosio_assert(not_exist, "Already exist Token");
// Add token with creator paying for RAM
m_tokens.emplace( to, [&]( auto& token ) {
token.idx = m_tokens.available_primary_key();
token.t_idx = index;
token.state = "idle";
token.owner = to;
token.master = to;
token.value = asset{1, symbol};
});
}
// Case UTI
if( symbol == S(0, UTI)){
// Get Item info
item_table item(N(unlimittest1), to);
auto item_iter = item.get(index, "Not exist Item");
// Check Token duplication
auto uti_list = i_tokens.get_index<N(byowner)>();
bool not_exist = true;
for(auto it = uti_list.begin(); it != uti_list.end(); ++it){
if(it->t_idx == item_iter.index){
not_exist = false;
break;
}
}
eosio_assert(not_exist, "Already exist Token");
// Add token with creator paying for RAM
i_tokens.emplace( to, [&]( auto& token ) {
token.idx = i_tokens.available_primary_key();
token.t_idx = index;
token.state = "idle";
token.owner = to;
token.master = to;
token.value = asset{1, symbol};
});
}
// Increase supply
add_supply( quantity );
// Add balance to account
add_balance( to, quantity, to );
}
void devtooth_nft::transferid( account_name from, account_name to, id_type id, string sym)
{
// Ensure authorized to send from account
eosio_assert( from != to, "cannot transfer to self" );
require_auth( from );
// Ensure 'to' account exists
eosio_assert( is_account( to ), "to account does not exist");
asset token(0, string_to_symbol(0, sym.c_str()));
// UTS
if( token.symbol == S(0, UTS) ){
auto sender_token = s_tokens.find( id );
eosio_assert( sender_token != s_tokens.end(), "token with specified ID does not exist" );
eosio_assert( sender_token->owner == from, "sender does not own token with specified ID");
eosio_assert( sender_token->state == "idle", "a non-tradeable token");
const auto& st = *sender_token;
s_tokens.modify( st, from, [&]( auto& token ) {
token.owner = to;
});
sub_balance( from, st.value );
add_balance( to, st.value, from );
}
// UTM
if( token.symbol == S(0, UTM) ){
auto sender_token = m_tokens.find( id );
eosio_assert( sender_token != m_tokens.end(), "token with specified ID does not exist" );
eosio_assert( sender_token->owner == from, "sender does not own token with specified ID");
eosio_assert( sender_token->state == "idle", "a non-tradeable token");
const auto& st = *sender_token;
m_tokens.modify( st, from, [&]( auto& token ) {
token.owner = to;
});
sub_balance( from, st.value );
add_balance( to, st.value, from );
}
// UTI
if( token.symbol == S(0, UTI) ){
auto sender_token = i_tokens.find( id );
eosio_assert( sender_token != i_tokens.end(), "token with specified ID does not exist" );
eosio_assert( sender_token->owner == from, "sender does not own token with specified ID");
eosio_assert( sender_token->state == "idle", "a non-tradeable token");
const auto& st = *sender_token;
i_tokens.modify( st, from, [&]( auto& token ) {
token.owner = to;
});
sub_balance( from, st.value );
add_balance( to, st.value, from );
}
// Notify both recipients
require_recipient( from );
require_recipient( to );
}
void devtooth_nft::changestate(account_name from, string sym, id_type id){
require_auth(from);
asset token(0, string_to_symbol(0, sym.c_str()));
// UTS
if( token.symbol == S(0, UTS) ){
auto target_token = s_tokens.find( id );
eosio_assert( target_token != s_tokens.end(), "token with specified ID does not exist" );
eosio_assert( target_token->owner == from, "sender does not own token with specified ID");
const auto& st = *target_token;
if(st.state == "idle"){
s_tokens.modify( st, from, [&]( auto& token ) {
token.state = "selling";
});
}
else{
s_tokens.modify( st, from, [&]( auto& token ) {
token.state = "idle";
});
}
}
// UTM
if( token.symbol == S(0, UTM) ){
auto target_token = m_tokens.find( id );
eosio_assert( target_token != m_tokens.end(), "token with specified ID does not exist" );
eosio_assert( target_token->owner == from, "sender does not own token with specified ID");
const auto& st = *target_token;
if(st.state == "idle"){
m_tokens.modify( st, from, [&]( auto& token ) {
token.state = "selling";
});
}
else{
m_tokens.modify( st, from, [&]( auto& token ) {
token.state = "idle";
});
}
}
// UTI
if( token.symbol == S(0, UTI) ){
auto target_token = i_tokens.find( id );
eosio_assert( target_token != i_tokens.end(), "token with specified ID does not exist" );
eosio_assert( target_token->owner == from, "sender does not own token with specified ID");
const auto& st = *target_token;
if(st.state == "idle"){
i_tokens.modify( st, from, [&]( auto& token ) {
token.state = "selling";
});
}
else{
i_tokens.modify( st, from, [&]( auto& token ) {
token.state = "idle";
});
}
}
}
void devtooth_nft::backtogame(account_name from, string sym, id_type id){
require_auth(from);
asset token(0, string_to_symbol(0, sym.c_str()));
// UTS
if( token.symbol == S(0, UTS) ){
auto target_token = s_tokens.find( id );
eosio_assert( target_token != s_tokens.end(), "token with specified ID does not exist" );
eosio_assert( target_token->owner == from, "sender does not own token with specified ID");
eosio_assert( target_token->state == "idle", "Can not back to game in auction");
const auto& st = *target_token;
s_tokens.erase( st );
}
// UTM
if( token.symbol == S(0, UTM) ){
auto target_token = m_tokens.find( id );
eosio_assert( target_token != m_tokens.end(), "token with specified ID does not exist" );
eosio_assert( target_token->owner == from, "sender does not own token with specified ID");
eosio_assert( target_token->state == "idle", "Can not back to game in auction");
const auto& st = *target_token;
m_tokens.erase( st );
}
// UTI
if( token.symbol == S(0, UTI) ){
auto target_token = i_tokens.find( id );
eosio_assert( target_token != i_tokens.end(), "token with specified ID does not exist" );
eosio_assert( target_token->owner == from, "sender does not own token with specified ID");
eosio_assert( target_token->state == "idle", "Can not back to game in auction");
const auto& st = *target_token;
i_tokens.erase( st );
}
}
void devtooth_nft::clean() {
for(auto iter = s_tokens.begin(); iter != s_tokens.end();){
auto token_iter = s_tokens.find(iter->primary_key());
iter++;
s_tokens.erase(token_iter);
}
for(auto iter2 = m_tokens.begin(); iter2 != m_tokens.end();){
auto token_iter2 = m_tokens.find(iter2->primary_key());
iter2++;
m_tokens.erase(token_iter2);
}
for(auto iter3 = i_tokens.begin(); iter3 != i_tokens.end(); ){
auto token_iter3 = i_tokens.find(iter3->primary_key());
iter3++;
i_tokens.erase(token_iter3);
}
}
void devtooth_nft::sub_balance( account_name owner, asset value )
{
account_index from_acnts( _self, owner );
const auto& from = from_acnts.get( value.symbol.name(), "no balance object found" );
eosio_assert( from.balance.amount >= value.amount, "overdrawn balance" );
if( from.balance.amount == value.amount ) {
from_acnts.erase( from );
} else {
from_acnts.modify( from, owner, [&]( auto& a ) {
a.balance -= value;
});
}
}
void devtooth_nft::add_balance( account_name owner, asset value, account_name ram_payer )
{
account_index to_accounts( _self, owner );
auto to = to_accounts.find( value.symbol.name() );
if( to == to_accounts.end() ) {
to_accounts.emplace( ram_payer, [&]( auto& a ){
a.balance = value;
});
} else {
to_accounts.modify( to, 0, [&]( auto& a ) {
a.balance += value;
});
}
}
void devtooth_nft::sub_supply( asset quantity ) {
auto symbol_name = quantity.symbol.name();
currency_index currency_table( _self, symbol_name );
auto current_currency = currency_table.find( symbol_name );
currency_table.modify( current_currency, 0, [&]( auto& currency ) {
currency.supply -= quantity;
});
}
void devtooth_nft::add_supply( asset quantity )
{
auto symbol_name = quantity.symbol.name();
currency_index currency_table( _self, symbol_name );
auto current_currency = currency_table.find( symbol_name );
currency_table.modify( current_currency, 0, [&]( auto& currency ) {
currency.supply += quantity;
});
}
EOSIO_ABI( devtooth_nft, (create)(issue)(transferid)(changestate)(backtogame)(clean) )
} /// namespace eosio