-
Notifications
You must be signed in to change notification settings - Fork 0
/
bank.cpp
324 lines (268 loc) · 9.8 KB
/
bank.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
#include "include/bank.hpp"
auto bank::incrementcounter(name counter_name)
{
auto counter_itr = counters.find(counter_name.value);
if (counter_itr == counters.end()) {
counter_itr = counters.emplace(get_self(), [&](auto &_c) {
_c.counter_name = counter_name;
_c.counter_value = 1;
});
}
else {
counters.modify(counter_itr, get_self(), [&](auto &_c) {
_c.counter_value = counter_itr->counter_value + 1;
});
}
return counter_itr;
}
void bank::setconfig(string key, uint64_t uint_value, int32_t int_value, string string_value, vector<string> arr_value, vector<TOKEN> tkn_value)
{
require_auth(get_self());
auto itr = configs.find(name(key).value);
if (itr != configs.end()) {
configs.modify(itr, get_self(), [&](auto &_config) {
_config.uint_value = uint_value;
_config.int_value = int_value;
_config.string_value = string_value;
_config.arr_value = arr_value;
_config.tkn_value = tkn_value;
});
}
else {
configs.emplace(get_self(), [&](auto &_config) {
_config.key = name(key);
_config.uint_value = uint_value;
_config.int_value = int_value;
_config.string_value = string_value;
_config.arr_value = arr_value;
_config.tkn_value = tkn_value;
});
}
}
void bank::delconfig(string key)
{
require_auth(get_self());
auto itr = configs.require_find(name(key).value, "Config not found");
configs.erase(itr);
}
void bank::savetokentx(name player, asset quantity, string operation)
{
tokentx_t tokentx = get_tokentx(player);
auto counter_itr = incrementcounter(name("tokentx"));
tokentx.emplace(get_self(), [&](auto &_itr) {
_itr.id = counter_itr->counter_value;
_itr.created_at = current_time_point().sec_since_epoch();
_itr.quantity = quantity;
_itr.operation = operation;
});
}
void bank::handletransfer (name from, name to, asset quantity, string memo)
{
if (to != get_self() || from == get_self()) {
print("These are not the droids you are looking for");
return;
}
check(quantity.amount > 0, "Amount has to be > 0");
string symbol = quantity.symbol.code().to_string();
auto allowedtkns = configs.require_find(name("allowedtkns").value, "Allowed tokens not set, contact an administrator");
bool config_found = false;
for (TOKEN tkn : allowedtkns->tkn_value) {
if (symbol == tkn.symbol) {
config_found = true;
}
}
check(config_found, "Symbol not allowed");
check(memo.substr(0, 7) == "deposit", "Token transfer not allowed");
auto itr = balances.find(from.value);
check(itr != balances.end(), "You need to create a bank first");
vector <asset> quantities = itr->quantities;
bool found_token = false;
for (asset &token : quantities) {
if (quantity.symbol == token.symbol) {
found_token = true;
token.amount += quantity.amount;
break;
}
}
if (!found_token) {
quantities.push_back(quantity);
}
balances.modify(itr, get_self(), [&](auto &_balance) {
_balance.quantities = quantities;
});
savetokentx(from, quantity, "IN");
}
void bank::handlenfttransfer (name collection_name, name from, name to, vector<uint64_t> asset_ids, string memo)
{
if (to != get_self() || from == get_self()) {
print("These are not the droids you are looking for");
return;
}
check(memo.substr(0, 7) == "deposit", "NFT transfer not allowed");
atomicassets::assets_t owned_assets = atomicassets::get_assets(to);
auto itr = balances.require_find(from.value, "You need to create a bank first");
vector<NFT> nfts = itr->nfts;
for (uint64_t asset_id: asset_ids) {
auto asset_itr = owned_assets.find(asset_id);
NFT nft;
nft.template_id = asset_itr->template_id;
nft.asset_id = asset_id;
nfts.push_back(nft);
}
balances.modify(itr, get_self(), [&](auto &_balance) {
_balance.nfts = nfts;
});
}
void bank::createbank(name player)
{
require_auth(player);
auto itr = balances.find(player.value);
check(itr == balances.end(), "Bank account already created");
balances.emplace(player, [&](auto &_bank) {
_bank.owner = player;
});
}
void bank::usebanktkn(name player, asset quantity)
{
require_auth(get_self());
auto itr = balances.require_find(player.value, "Bank not found");
auto allowedtkns = configs.require_find(name("allowedtkns").value, "Allowed tokens not set, contact an administrator");
string symbol = quantity.symbol.code().to_string();
vector <asset> quantities = itr->quantities;
bool found = false;
for (asset &token : quantities) {
if (quantity.symbol == token.symbol) {
found = true;
check(token.amount > quantity.amount, "Insufficent balance");
token.amount -= quantity.amount;
auto config_itr = configs.require_find(name("bankacc").value, "No fund account set, contact an administrator");
bool tknconf_found = false;
for (TOKEN tknconf : allowedtkns->tkn_value) {
if (symbol == tknconf.symbol) {
tknconf_found = true;
action(
permission_level{get_self(), name("active")},
name(tknconf.contract),
name("transfer"),
make_tuple(
get_self(),
name(config_itr->string_value),
quantity,
string("bank use")
)
).send();
}
}
check(tknconf_found, "Token conf not found");
break;
}
}
check(found, "Asset not found");
balances.modify(itr, get_self(), [&](auto &_balance) {
_balance.quantities = quantities;
});
savetokentx(player, quantity, "OUT");
}
void bank::usebanknft(name player, int32_t template_id, uint64_t quantity)
{
require_auth(get_self());
auto itr = balances.require_find(player.value, "Bank not found");
uint64_t used = 0;
vector<NFT> left_nfts = itr->nfts;
for (NFT nft : itr->nfts) {
if (nft.template_id == template_id) {
used++;
// Burn the NFT
action(
permission_level{get_self(), name("active")},
name("atomicassets"),
name("burnasset"),
make_tuple(
get_self(),
nft.asset_id
)
).send();
left_nfts.erase(left_nfts.begin());
}
}
check(used == quantity, "Not enough assets in the bank");
balances.modify(itr, get_self(), [&](auto &_balance) {
_balance.nfts = left_nfts;
});
}
void bank::withdrawtkn(name player, asset quantity)
{
require_auth(player);
auto itr = balances.require_find(player.value, "Bank not found");
auto allowedtkns = configs.require_find(name("allowedtkns").value, "Allowed tokens not set, contact an administrator");
string symbol = quantity.symbol.code().to_string();
bool asset_found = false;
vector <asset> quantities = itr->quantities;
for (asset &tkn : quantities) {
if (quantity.symbol == tkn.symbol) {
asset_found = true;
check(tkn.amount >= quantity.amount, "Not enough balance");
bool config_found = false;
for (TOKEN tknconf : allowedtkns->tkn_value) {
if (symbol == tknconf.symbol) {
config_found = true;
action(
permission_level{get_self(), name("active")},
name(tknconf.contract),
name("transfer"),
make_tuple(
get_self(),
player,
quantity,
string("withdraw")
)
).send();
tkn.amount -= quantity.amount;
}
}
check(config_found, "Symbol not allowed");
}
}
check(asset_found, "No balance available for this token");
balances.modify(itr, player, [&](auto &_balance) {
_balance.quantities = quantities;
});
savetokentx(player, quantity, "OUT");
}
void bank::withdrawnft(name player, int32_t template_id, uint64_t quantity)
{
require_auth(player);
auto itr = balances.require_find(player.value, "Bank not found");
vector<uint64_t> assets_totrans;
vector<NFT> nfts = itr->nfts;
auto nft_itr = nfts.begin();
while (nft_itr != nfts.end()) {
if (nft_itr->template_id == template_id) {
assets_totrans.emplace_back(nft_itr->asset_id);
nft_itr = nfts.erase(nft_itr);
if (assets_totrans.size() == quantity) break;
}
}
check(assets_totrans.size() == quantity, "Not enough NFTs");
// Send the NFT
action(
permission_level{get_self(), name("active")},
name("atomicassets"),
name("transfer"),
make_tuple(
get_self(),
player,
assets_totrans,
std::string("")
)
).send();
balances.modify(itr, player, [&](auto &_balance) {
_balance.nfts = nfts;
});
}
void bank::delbank(name player)
{
require_auth(player);
auto itr = balances.require_find(player.value, "Bank not found");
balances.erase(itr);
}