-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtradingbook2.cpp
More file actions
90 lines (77 loc) · 2.32 KB
/
tradingbook2.cpp
File metadata and controls
90 lines (77 loc) · 2.32 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
#include<eosio/eosio.hpp>
#include<eosio/asset.hpp>
using namespace eosio;
CONTRACT tradingbook2: public contract {
public:
using contract::contract;
[[eosio::on_notify("eosio.token::transfer")]]
void ontransfer(name from, name to, asset quantity, std::string memo) {
if(from == get_self()) {
ReceiveScope forSaveScope(get_self(), get_self().value);
auto itr = forSaveScope.find(to.value);
if(itr == forSaveScope.end()) {
forSaveScope.emplace(get_self(), [&](auto& row) {
row.myScope = to.value;
});
}
Receive forReceiver(get_self(), to.value);
forReceiver.emplace(get_self(), [&](auto& row) {
row.mykey = forReceiver.available_primary_key();
row.user = to;
row.balance = quantity;
});
} else {
SendScope forSaveScope(get_self(), get_self().value);
auto itr = forSaveScope.find(from.value);
if(itr == forSaveScope.end()) {
forSaveScope.emplace(get_self(), [&](auto& row) {
row.myScope = from.value;
});
}
Send forSender(get_self(), from.value);
forSender.emplace(get_self(), [&](auto& row) {
row.mykey = forSender.available_primary_key();
row.user = from;
row.balance = quantity;
});
}
}
ACTION eraseall() {
require_auth(get_self());
SendScope forSendScope(get_self(), get_self().value);
auto itrSend = forSendScope.begin();
while(itrSend != forSendScope.end()) {
Send forEraseAll(get_self(), itrSend->myScope);
auto itr = forEraseAll.begin();
while(itr != forEraseAll.end()) {
itr = forEraseAll.erase(itr);
}
itrSend = forSendScope.erase(itrSend);
}
ReceiveScope forReceiveScope(get_self(), get_self().value);
auto itrReceive = forReceiveScope.begin();
while(itrSend != forSendScope.end()) {
Receive forEraseAll(get_self(), itrReceive->myScope);
auto itr = forEraseAll.begin();
while(itr != forEraseAll.end()) {
itr = forEraseAll.erase(itr);
}
itrSend = forSendScope.erase(itrSend);
}
}
private:
TABLE tradingbook_struct {
uint64_t mykey;
name user;
asset balance;
uint64_t primary_key() const { return mykey; }
};
TABLE scopebook_struct {
uint64_t myScope;
uint64_t primary_key() const { return myScope; }
};
typedef multi_index<"sendbook"_n, tradingbook_struct> Send;
typedef multi_index<"receivebook"_n, tradingbook_struct> Receive;
typedef multi_index<"sendscope"_n, scopebook_struct> SendScope;
typedef multi_index<"receivescope"_n, scopebook_struct> ReceiveScope;
};