-
Notifications
You must be signed in to change notification settings - Fork 41
/
dgoods.cpp
589 lines (489 loc) · 23.3 KB
/
dgoods.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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
#include <dgoods.hpp>
#include <math.h>
ACTION dgoods::setconfig(const symbol_code& sym, const string& version) {
require_auth( get_self() );
// valid symbol
check( sym.is_valid(), "not valid symbol" );
// can only have one symbol per contract
config_index config_table(get_self(), get_self().value);
auto config_singleton = config_table.get_or_create( get_self(), tokenconfigs{ "dgoods"_n, version, sym, 1, 1 } );
// setconfig will always update version when called
config_singleton.version = version;
config_table.set( config_singleton, get_self() );
}
ACTION dgoods::create(const name& issuer,
const name& rev_partner,
const name& category,
const name& token_name,
const bool& fungible,
const bool& burnable,
const bool& sellable,
const bool& transferable,
const double& rev_split,
const string& base_uri,
const uint32_t& max_issue_days,
const asset& max_supply) {
require_auth( get_self() );
time_point_sec max_issue_window = time_point_sec(0);
// true max supply, not time based supply window
if ( max_issue_days == 0 ) {
// cannot have both max_supply and max_issue_days be 0
_checkasset( max_supply, fungible );
} else {
uint32_t max_issue_seconds = max_issue_days * 24 * 3600;
max_issue_window = time_point_sec(current_time_point()) + max_issue_seconds;
check( max_supply.amount >= 0, "max supply must be 0 or greater" );
if (fungible == false) {
check( max_supply.symbol.precision() == 0, "NFT must have max supply as int, precision of 0" );
}
}
// check if issuer account exists
check( is_account( issuer ), "issuer account does not exist" );
check( is_account( rev_partner), "rev_partner account does not exist" );
// check split frac is between 0 and 1
check( ( rev_split <= 1.0 ) && (rev_split >= 0.0), "rev_split must be between 0 and 1" );
// get category_name_id
config_index config_table( get_self(), get_self().value );
check(config_table.exists(), "Symbol table does not exist, setconfig first");
auto config_singleton = config_table.get();
auto category_name_id = config_singleton.category_name_id;
category_index category_table( get_self(), get_self().value );
auto existing_category = category_table.find( category.value );
// category hasn't been created before, create it
if ( existing_category == category_table.end() ) {
category_table.emplace( get_self(), [&]( auto& cat ) {
cat.category = category;
});
}
asset current_supply = asset( 0, symbol( config_singleton.symbol, max_supply.symbol.precision() ));
asset issued_supply = asset( 0, symbol( config_singleton.symbol, max_supply.symbol.precision() ));
stats_index stats_table( get_self(), category.value );
auto existing_token = stats_table.find( token_name.value );
check( existing_token == stats_table.end(), "Token with category and token_name exists" );
// token type hasn't been created, create it
stats_table.emplace( get_self(), [&]( auto& stats ) {
stats.category_name_id = category_name_id;
stats.issuer = issuer;
stats.rev_partner= rev_partner;
stats.token_name = token_name;
stats.fungible = fungible;
stats.burnable = burnable;
stats.sellable = sellable;
stats.transferable = transferable;
stats.current_supply = current_supply;
stats.issued_supply = issued_supply;
stats.rev_split = rev_split;
stats.base_uri = base_uri;
stats.max_supply = max_supply;
stats.max_issue_window = max_issue_window;
});
// successful creation of token, update category_name_id to reflect
config_singleton.category_name_id++;
config_table.set( config_singleton, get_self() );
}
ACTION dgoods::issue(const name& to,
const name& category,
const name& token_name,
const asset& quantity,
const string& relative_uri,
const string& memo) {
check( is_account( to ), "to account does not exist");
check( memo.size() <= 256, "memo has more than 256 bytes" );
// dgoodstats table
stats_index stats_table( get_self(), category.value );
const auto& dgood_stats = stats_table.get( token_name.value,
"Token with category and token_name does not exist" );
// ensure have issuer authorization and valid quantity
require_auth( dgood_stats.issuer );
_checkasset( quantity, dgood_stats.fungible );
string string_precision = "precision of quantity must be " + to_string( dgood_stats.max_supply.symbol.precision() );
check( quantity.symbol == dgood_stats.max_supply.symbol, string_precision.c_str() );
// time based minting
if ( dgood_stats.max_issue_window != time_point_sec(0) ) {
check(time_point_sec(current_time_point()) <= dgood_stats.max_issue_window, "issue window has closed, cannot issue more");
}
if (dgood_stats.max_supply.amount != 0) {
// check cannot issue more than max supply, careful of overflow of uint
check( quantity.amount <= (dgood_stats.max_supply.amount - dgood_stats.issued_supply.amount), "Cannot issue more than max supply" );
}
if (dgood_stats.fungible == false) {
check( quantity.amount <= 100, "can issue up to 100 at a time");
asset issued_supply = dgood_stats.issued_supply;
asset one_token = asset( 1, dgood_stats.max_supply.symbol);
for ( uint64_t i = 1; i <= quantity.amount; i++ ) {
_mint(to, dgood_stats.issuer, category, token_name,
issued_supply, relative_uri);
// used to keep track of serial number when minting multiple
issued_supply += one_token;
}
}
_add_balance(to, get_self(), category, token_name, dgood_stats.category_name_id, quantity);
// increase current supply
stats_table.modify( dgood_stats, same_payer, [&]( auto& s ) {
s.current_supply += quantity;
s.issued_supply += quantity;
});
}
ACTION dgoods::burnnft(const name& owner,
const vector<uint64_t>& dgood_ids) {
require_auth(owner);
check( dgood_ids.size() <= 20, "max batch size of 20" );
// loop through vector of dgood_ids, check token exists
lock_index lock_table( get_self(), get_self().value );
dgood_index dgood_table( get_self(), get_self().value );
for ( auto const& dgood_id: dgood_ids ) {
const auto& token = dgood_table.get( dgood_id, "token does not exist" );
check( token.owner == owner, "must be token owner" );
stats_index stats_table( get_self(), token.category.value );
const auto& dgood_stats = stats_table.get( token.token_name.value, "dgood stats not found" );
check( dgood_stats.burnable == true, "Not burnable");
check( dgood_stats.fungible == false, "Cannot call burnnft on fungible token, call burnft instead");
// make sure token not locked;
auto locked_nft = lock_table.find( dgood_id );
check(locked_nft == lock_table.end(), "token locked");
asset quantity(1, dgood_stats.max_supply.symbol);
// decrease current supply
stats_table.modify( dgood_stats, same_payer, [&]( auto& s ) {
s.current_supply -= quantity;
});
// lower balance from owner
_sub_balance(owner, dgood_stats.category_name_id, quantity);
// erase token
dgood_table.erase( token );
}
}
ACTION dgoods::burnft(const name& owner,
const uint64_t& category_name_id,
const asset& quantity) {
require_auth(owner);
account_index from_account( get_self(), owner.value );
const auto& acct = from_account.get( category_name_id, "token does not exist in account" );
stats_index stats_table( get_self(), acct.category.value );
const auto& dgood_stats = stats_table.get( acct.token_name.value, "dgood stats not found" );
_checkasset( quantity, true );
string string_precision = "precision of quantity must be " + to_string( dgood_stats.max_supply.symbol.precision() );
check( quantity.symbol == dgood_stats.max_supply.symbol, string_precision.c_str() );
// lower balance from owner
_sub_balance(owner, category_name_id, quantity);
// decrease current supply
stats_table.modify( dgood_stats, same_payer, [&]( auto& s ) {
s.current_supply -= quantity;
});
}
ACTION dgoods::transfernft(const name& from,
const name& to,
const vector<uint64_t>& dgood_ids,
const string& memo ) {
check( dgood_ids.size() <= 20, "max batch size of 20" );
// ensure authorized to send from account
check( from != to, "cannot transfer to self" );
require_auth( from );
// ensure 'to' account exists
check( is_account( to ), "to account does not exist");
// check memo size
check( memo.size() <= 256, "memo has more than 256 bytes" );
_changeowner( from, to, dgood_ids, memo, true );
}
ACTION dgoods::transferft(const name& from,
const name& to,
const name& category,
const name& token_name,
const asset& quantity,
const string& memo ) {
// ensure authorized to send from account
check( from != to, "cannot transfer to self" );
require_auth( from );
// ensure 'to' account exists
check( is_account( to ), "to account does not exist");
// check memo size
check( memo.size() <= 256, "memo has more than 256 bytes" );
require_recipient( from );
require_recipient( to );
stats_index stats_table( get_self(), category.value );
const auto& dgood_stats = stats_table.get( token_name.value, "dgood stats not found" );
check( dgood_stats.transferable == true, "not transferable");
check( dgood_stats.fungible == true, "Must be fungible token");
_checkasset( quantity, true );
string string_precision = "precision of quantity must be " + to_string( dgood_stats.max_supply.symbol.precision() );
check( quantity.symbol == dgood_stats.max_supply.symbol, string_precision.c_str() );
_sub_balance(from, dgood_stats.category_name_id, quantity);
_add_balance(to, get_self(), category, token_name, dgood_stats.category_name_id, quantity);
}
ACTION dgoods::listsalenft(const name& seller,
const vector<uint64_t>& dgood_ids,
const uint32_t sell_by_days,
const asset& net_sale_amount) {
require_auth( seller );
time_point_sec expiration = time_point_sec(0);
if ( sell_by_days != 0 ) {
uint32_t sell_by_seconds = sell_by_days * 24 * 3600;
expiration = time_point_sec(current_time_point()) + sell_by_seconds;
}
check (dgood_ids.size() <= 20, "max batch size of 20");
check( net_sale_amount.amount > .02 * pow(10, net_sale_amount.symbol.precision()), "minimum price of at least 0.02 EOS");
check( net_sale_amount.symbol == symbol( symbol_code("EOS"), 4), "only accept EOS for sale" );
dgood_index dgood_table( get_self(), get_self().value );
for ( auto const& dgood_id: dgood_ids ) {
const auto& token = dgood_table.get( dgood_id, "token does not exist" );
stats_index stats_table( get_self(), token.category.value );
const auto& dgood_stats = stats_table.get( token.token_name.value, "dgood stats not found" );
check( dgood_stats.sellable == true, "not sellable");
check ( seller == token.owner, "not token owner");
// make sure token not locked;
lock_index lock_table( get_self(), get_self().value );
auto locked_nft = lock_table.find( dgood_id );
check(locked_nft == lock_table.end(), "token locked");
// add token to lock table
lock_table.emplace( seller, [&]( auto& l ) {
l.dgood_id = dgood_id;
});
}
ask_index ask_table( get_self(), get_self().value );
// add batch to table of asks
// set id to the first dgood being listed, if only one being listed, simplifies life
ask_table.emplace( seller, [&]( auto& a ) {
a.batch_id = dgood_ids[0];
a.dgood_ids = dgood_ids;
a.seller = seller;
a.amount = net_sale_amount;
a.expiration = expiration;
});
}
ACTION dgoods::closesalenft(const name& seller,
const uint64_t& batch_id) {
ask_index ask_table( get_self(), get_self().value );
const auto& ask = ask_table.get( batch_id, "cannot find sale to close" );
lock_index lock_table( get_self(), get_self().value );
if ( ask.expiration == time_point_sec(0) || time_point_sec(current_time_point()) <= ask.expiration ) {
require_auth( seller );
check( ask.seller == seller, "only the seller can cancel a sale in progress");
}
// sale has expired anyone can call this and ask removed, token removed from asks/lock
for ( auto const& dgood_id: ask.dgood_ids ) {
const auto& locked_nft = lock_table.get( dgood_id, "dgood not found in lock table" );
lock_table.erase( locked_nft );
}
ask_table.erase( ask );
}
void dgoods::buynft(const name& from,
const name& to,
const asset& quantity,
const string& memo) {
// allow EOS to be sent by sending with empty string memo
if ( memo == "deposit" ) return;
// don't allow spoofs
if ( to != get_self() ) return;
if ( from == name("eosio.stake") ) return;
check( quantity.symbol == symbol( symbol_code("EOS"), 4), "Buy only with EOS" );
check( memo.length() <= 32, "memo too long" );
//memo format comma separated
//batch_id,to_account
uint64_t batch_id;
name to_account;
tie( batch_id, to_account ) = parsememo(memo);
ask_index ask_table( get_self(), get_self().value );
const auto& ask = ask_table.get( batch_id, "cannot find listing" );
check ( ask.amount.amount == quantity.amount, "send the correct amount");
check ( ask.expiration == time_point_sec(0) || ask.expiration > time_point_sec(current_time_point()), "sale has expired");
// nft(s) bought, change owner to buyer regardless of transferable
_changeowner( ask.seller, to_account, ask.dgood_ids, "bought by: " + to_account.to_string(), false);
// amounts owed to all parties
map<name, asset> fee_map = _calcfees(ask.dgood_ids, ask.amount, ask.seller);
for(auto const& fee : fee_map) {
auto account = fee.first;
auto amount = fee.second;
// if seller is contract, no need to send EOS again
if ( account != get_self() ) {
// send EOS to account owed
action( permission_level{ get_self(), name("active") },
name("eosio.token"), name("transfer"),
make_tuple( get_self(), account, amount, string("sale of dgood") ) ).send();
}
}
// remove locks, remove from ask table
lock_index lock_table( get_self(), get_self().value );
for ( auto const& dgood_id: ask.dgood_ids ) {
const auto& locked_nft = lock_table.get( dgood_id, "dgood not found in lock table" );
lock_table.erase( locked_nft );
}
SEND_INLINE_ACTION( *this, logsale, { { get_self(), "active"_n } }, { ask.dgood_ids, ask.seller, from, to_account } );
// remove sale listing
ask_table.erase( ask );
}
// method to log dgood_id and match transaction to action
ACTION dgoods::logcall(const uint64_t& dgood_id) {
require_auth( get_self() );
}
// method to logsuccessful sale
ACTION dgoods::logsale(const vector<uint64_t>& dgood_ids, const name& seller, const name& buyer, const name& receiver) {
require_auth( get_self() );
}
ACTION dgoods::freezemaxsup(const name& category, const name& token_name) {
require_auth( get_self() );
// dgoodstats table
stats_index stats_table( get_self(), category.value );
const auto& dgood_stats = stats_table.get( token_name.value,
"Token with category and token_name does not exist" );
check(dgood_stats.max_issue_window != time_point_sec(0), "can't freeze max supply unless time based minting");
check(dgood_stats.issued_supply.amount != 0, "need to issue at least one token before freezing");
stats_table.modify( dgood_stats, same_payer, [&]( auto& s ) {
s.max_supply = dgood_stats.issued_supply;
s.max_issue_window = time_point_sec(0);
});
}
// Private
map<name, asset> dgoods::_calcfees(vector<uint64_t> dgood_ids, asset ask_amount, name seller) {
map<name, asset> fee_map;
dgood_index dgood_table( get_self(), get_self().value );
int64_t tot_fees = 0;
for ( auto const& dgood_id: dgood_ids ) {
const auto& token = dgood_table.get( dgood_id, "token does not exist" );
stats_index stats_table( get_self(), token.category.value );
const auto& dgood_stats = stats_table.get( token.token_name.value, "dgood stats not found" );
name rev_partner = dgood_stats.rev_partner;
if ( dgood_stats.rev_split == 0.0 ) {
continue;
}
double fee = static_cast<double>(ask_amount.amount) * dgood_stats.rev_split / static_cast<double>( dgood_ids.size() );
asset fee_asset(fee, ask_amount.symbol);
auto ret_val = fee_map.insert({rev_partner, fee_asset});
tot_fees += fee_asset.amount;
if ( ret_val.second == false ) {
fee_map[rev_partner] += fee_asset;
}
}
//add seller to fee_map minus fees
asset seller_amount(ask_amount.amount - tot_fees, ask_amount.symbol);
auto ret_val = fee_map.insert({seller, seller_amount});
if ( ret_val.second == false ) {
fee_map[seller] += seller_amount;
}
return fee_map;
}
// Private
void dgoods::_changeowner(const name& from, const name& to, const vector<uint64_t>& dgood_ids, const string& memo, const bool& istransfer) {
check (dgood_ids.size() <= 20, "max batch size of 20");
// loop through vector of dgood_ids, check token exists
dgood_index dgood_table( get_self(), get_self().value );
lock_index lock_table( get_self(), get_self().value );
for ( auto const& dgood_id: dgood_ids ) {
const auto& token = dgood_table.get( dgood_id, "token does not exist" );
stats_index stats_table( get_self(), token.category.value );
const auto& dgood_stats = stats_table.get( token.token_name.value, "dgood stats not found" );
if ( istransfer ) {
check( token.owner == from, "must be token owner" );
check( dgood_stats.transferable == true, "not transferable");
auto locked_nft = lock_table.find( dgood_id );
check( locked_nft == lock_table.end(), "token locked, cannot transfer");
}
// notifiy both parties
require_recipient( from );
require_recipient( to );
dgood_table.modify( token, same_payer, [&] (auto& t ) {
t.owner = to;
});
// amount 1, precision 0 for NFT
asset quantity(1, dgood_stats.max_supply.symbol);
_sub_balance(from, dgood_stats.category_name_id, quantity);
_add_balance(to, get_self(), token.category, token.token_name, dgood_stats.category_name_id, quantity);
}
}
// Private
void dgoods::_checkasset(const asset& amount, const bool& fungible) {
auto sym = amount.symbol;
if (fungible) {
check( amount.amount > 0, "amount must be positive" );
} else {
check( sym.precision() == 0, "NFT must be an int, precision of 0" );
check( amount.amount >= 1, "NFT amount must be >= 1" );
}
config_index config_table(get_self(), get_self().value);
auto config_singleton = config_table.get();
check( config_singleton.symbol.raw() == sym.code().raw(), "Symbol must match symbol in config" );
check( amount.is_valid(), "invalid amount" );
}
// Private
void dgoods::_mint(const name& to,
const name& issuer,
const name& category,
const name& token_name,
const asset& issued_supply,
const string& relative_uri) {
dgood_index dgood_table( get_self(), get_self().value);
auto dgood_id = _nextdgoodid();
if ( relative_uri.empty() ) {
dgood_table.emplace( issuer, [&]( auto& dg) {
dg.id = dgood_id;
dg.serial_number = issued_supply.amount + 1;
dg.owner = to;
dg.category = category;
dg.token_name = token_name;
});
} else {
dgood_table.emplace( issuer, [&]( auto& dg ) {
dg.id = dgood_id;
dg.serial_number = issued_supply.amount + 1;
dg.owner = to;
dg.category = category;
dg.token_name = token_name;
dg.relative_uri = relative_uri;
});
}
SEND_INLINE_ACTION( *this, logcall, { { get_self(), "active"_n } }, { dgood_id } );
}
// available_primary_key() will reuise id's if last minted token is burned -- bad
uint64_t dgoods::_nextdgoodid() {
// get next_dgood_id and increment
config_index config_table( get_self(), get_self().value );
check(config_table.exists(), "dgoods config table does not exist, setconfig first");
auto config_singleton = config_table.get();
auto next_dgood_id = config_singleton.next_dgood_id++;
config_table.set( config_singleton, _self );
return next_dgood_id;
}
// Private
void dgoods::_add_balance(const name& owner, const name& ram_payer, const name& category, const name& token_name,
const uint64_t& category_name_id, const asset& quantity) {
account_index to_account( get_self(), owner.value );
auto acct = to_account.find( category_name_id );
if ( acct == to_account.end() ) {
to_account.emplace( ram_payer, [&]( auto& a ) {
a.category_name_id = category_name_id;
a.category = category;
a.token_name = token_name;
a.amount = quantity;
});
} else {
to_account.modify( acct, same_payer, [&]( auto& a ) {
a.amount += quantity;
});
}
}
// Private
void dgoods::_sub_balance(const name& owner, const uint64_t& category_name_id, const asset& quantity) {
account_index from_account( get_self(), owner.value );
const auto& acct = from_account.get( category_name_id, "token does not exist in account" );
check( acct.amount.amount >= quantity.amount, "quantity is more than account balance");
if ( acct.amount.amount == quantity.amount ) {
from_account.erase( acct );
} else {
from_account.modify( acct, same_payer, [&]( auto& a ) {
a.amount -= quantity;
});
}
}
extern "C" {
void apply (uint64_t receiver, uint64_t code, uint64_t action ) {
auto self = receiver;
if ( code == self ) {
switch( action ) {
EOSIO_DISPATCH_HELPER( dgoods, (setconfig)(create)(issue)(burnnft)(burnft)(transfernft)(transferft)(listsalenft)(closesalenft)(logcall)(freezemaxsup) )
}
}
else {
if ( code == name("eosio.token").value && action == name("transfer").value ) {
execute_action( name(receiver), name(code), &dgoods::buynft );
}
}
}
}