-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateescrow.cpp
More file actions
353 lines (305 loc) · 12.2 KB
/
createescrow.cpp
File metadata and controls
353 lines (305 loc) · 12.2 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
#include "createescrow.hpp"
#include "lib/common.h"
#include "models/accounts.h"
#include "models/balances.h"
#include "models/registry.h"
#include "models/bandwidth.h"
#include "constants.cpp"
#include "createaccounts.cpp"
#include "stakes.cpp"
namespace createescrow
{
using namespace eosio;
using namespace common;
using namespace accounts;
using namespace balance;
using namespace bandwidth;
using namespace registry;
using namespace std;
create_escrow::create_escrow(name s, name code, datastream<const char *> ds) : contract(s, code, ds),
dapps(_self, _self.value),
balances(_self, _self.value),
token(_self, _self.value) {}
void create_escrow::clean()
{
require_auth(_self);
create_escrow::cleanTable<Balances>();
}
void create_escrow::cleanreg()
{
require_auth(_self);
create_escrow::cleanTable<Registry>();
}
void create_escrow::cleantoken()
{
require_auth(_self);
create_escrow::cleanTable<Token>();
}
/***
* Called to specify the following details:
* symbol: the core token of the chain or the token used to pay for new user accounts of the chain
* newAccountContract: the contract to call for new account action
* minimumram: minimum bytes of RAM to put in a new account created on the chain
*/
void create_escrow::init(const symbol &symbol, name newaccountcontract, name newaccountaction, uint64_t minimumram)
{
require_auth(_self);
auto iterator = token.find(symbol.raw());
if (iterator == token.end())
token.emplace(_self, [&](auto &row) {
row.S_SYS = symbol;
row.newaccountcontract = newaccountcontract;
row.newaccountaction = newaccountaction;
row.min_ram = minimumram;
});
else
token.modify(iterator, same_payer, [&](auto &row) {
row.S_SYS = symbol;
row.newaccountcontract = newaccountcontract;
row.newaccountaction = newaccountaction;
row.min_ram = minimumram;
});
}
/***
* Called to define an account name as the owner of a dapp along with the following details:
* owner: account name to be registered as the owner of the dapp
* dapp: the string/account name representing the dapp
* ram_bytes: bytes of ram to put in the new user account created for the dapp
* net: EOS amount to be staked for net
* cpu: EOS amount to be staked for cpu
* airdropcontract: airdropdata struct/json or null
* Only the owner account/whitelisted account will be able to create new user account for the dapp
*/
void create_escrow::define(name &owner, string dapp, uint64_t ram_bytes, asset net, asset cpu, uint64_t pricekey, airdropdata &airdrop, bool use_rex, rexdata &rex)
{
require_auth(dapp != "free" ? owner : _self);
auto iterator = dapps.find(toUUID(dapp));
check(iterator == dapps.end() || (iterator != dapps.end() && iterator->owner == owner),
("the dapp " + dapp + " is already registered by another account").c_str());
uint64_t min_ram = create_escrow::getMinimumRAM();
check(ram_bytes >= min_ram, ("ram for new accounts must be equal to or greater than " + to_string(min_ram) + " bytes.").c_str());
// Creating a new dapp reference
if (iterator == dapps.end())
dapps.emplace(_self, [&](auto &row) {
row.owner = owner;
row.dapp = dapp;
row.pricekey = pricekey;
row.ram_bytes = ram_bytes;
row.net = net;
row.cpu = cpu;
row.airdrop = airdrop;
row.use_rex = use_rex;
row.rex = rex;
});
// Updating an existing dapp reference's configurations
else
dapps.modify(iterator, same_payer, [&](auto &row) {
row.ram_bytes = ram_bytes;
row.pricekey = pricekey;
row.net = net;
row.cpu = cpu;
row.airdrop = airdrop;
row.use_rex = use_rex;
row.rex = rex;
});
}
/***
* Lets the owner account of the dapp to whitelist other accounts.
*/
void create_escrow::whitelist(name owner, name account, string dapp)
{
require_auth(owner);
auto iterator = dapps.find(toUUID(dapp));
if (iterator != dapps.end() && owner == iterator->owner)
dapps.modify(iterator, same_payer, [&](auto &row) {
if (std::find(row.custodians.begin(), row.custodians.end(), account) == row.custodians.end())
{
row.custodians.push_back(account);
}
});
else
check(false, ("the dapp " + dapp + " is not owned by account " + owner.to_string()).c_str());
}
/***
* Transfers the remaining balance of a contributor from createescrow back to the contributor
* reclaimer: account trying to reclaim the balance
* dapp: the dapp name for which the account is trying to reclaim the balance
* sym: symbol of the tokens to be reclaimed. It can have value based on the following scenarios:
* - reclaim the "native" token balance used to create accounts. For ex - EOS/SYS
* - reclaim the remaining airdrop token balance used to airdrop dapp tokens to new user accounts. For ex- IQ/LUM
*/
void create_escrow::reclaim(name reclaimer, string dapp, string sym)
{
require_auth(reclaimer);
asset reclaimer_balance;
bool nocontributor;
// check if the user is trying to reclaim the system tokens
if (sym == create_escrow::getCoreSymbol().code().to_string())
{
auto iterator = balances.find(common::toUUID(dapp));
if (iterator != balances.end())
{
balances.modify(iterator, same_payer, [&](auto &row) {
auto pred = [reclaimer](const contributors &item) {
return item.contributor == reclaimer;
};
auto reclaimer_record = remove_if(std::begin(row.contributors), std::end(row.contributors), pred);
if (reclaimer_record != row.contributors.end())
{
reclaimer_balance = reclaimer_record->balance;
// only erase the contributor row if the cpu and net balances are also 0
if (reclaimer_record->rex_balance == asset(0'0000, create_escrow::getCoreSymbol()))
{
row.contributors.erase(reclaimer_record, row.contributors.end());
}
else
{
reclaimer_record->balance -= reclaimer_balance;
}
row.total_balance -= reclaimer_balance;
}
else
{
check(false, ("no remaining contribution for " + dapp + " by " + reclaimer.to_string()).c_str());
}
nocontributor = row.contributors.empty();
});
// delete the entire balance object if no contributors are there for the dapp
if (nocontributor && iterator->total_balance == asset(0'0000, create_escrow::getCoreSymbol()))
{
balances.erase(iterator);
}
auto msg = reclaimer_balance.to_string() + " reclaimed by " + reclaimer.to_string() + " for " + dapp;
print(msg.c_str());
// transfer the remaining balance for the contributor from the createescrow account to contributor's account
auto memo = "reimburse the remaining balance to " + reclaimer.to_string();
action(
permission_level{_self, "active"_n},
name("eosio.token"),
name("transfer"),
make_tuple(_self, reclaimer, reclaimer_balance, memo))
.send();
}
else
{
check(false, ("no funds given by " + reclaimer.to_string() + " for " + dapp).c_str());
}
}
// user is trying to reclaim custom dapp tokens
else
{
auto iterator = dapps.find(toUUID(dapp));
if (iterator != dapps.end())
dapps.modify(iterator, same_payer, [&](auto &row) {
if (row.airdrop->contract != name("") && row.airdrop->balance.symbol.code().to_string() == sym && row.owner == name(reclaimer))
{
auto memo = "reimburse the remaining airdrop balance for " + dapp + " to " + reclaimer.to_string();
if (row.airdrop->balance != asset(0'0000, row.airdrop->balance.symbol))
{
action(
permission_level{_self, "active"_n},
row.airdrop->contract,
name("transfer"),
make_tuple(_self, reclaimer, row.airdrop->balance, memo))
.send();
row.airdrop->balance = asset(0'0000, row.airdrop->balance.symbol);
}
else
{
check(false, ("No remaining airdrop balance for " + dapp + ".").c_str());
}
}
else
{
check(false, ("the remaining airdrop balance for " + dapp + " can only be claimed by its owner/whitelisted account.").c_str());
}
});
}
}
// to check if createescrow is deployed and functioning
void create_escrow::ping(name &from)
{
print('ping');
}
void create_escrow::transfer(const name &from, const name &to, const asset &quantity, string &memo)
{
if (to != _self)
return;
if (from == name("eosio.stake") || from == name("eosio.rex"))
{
return create_escrow::addTotalUnstaked(quantity);
};
if (quantity.symbol != create_escrow::getCoreSymbol())
return;
if (memo.length() > 64)
return;
create_escrow::addBalance(from, quantity, memo);
}
/***
* Checks if an account is whitelisted for a dapp by the owner of the dapp
* @return
*/
bool create_escrow::checkIfWhitelisted(name account, string dapp)
{
registry::Registry dapps(_self, _self.value);
auto iterator = dapps.find(common::toUUID(dapp));
auto position_in_whitelist = std::find(iterator->custodians.begin(), iterator->custodians.end(), account);
if (position_in_whitelist != iterator->custodians.end())
{
return true;
}
return false;
}
bool create_escrow::checkIfOwner(name account, string dapp)
{
registry::Registry dapps(_self, _self.value);
auto iterator = dapps.find(common::toUUID(dapp));
if (iterator != dapps.end())
{
if (account == iterator->owner)
{
return true;
}
}
return false;
}
bool create_escrow::checkIfOwnerOrWhitelisted(name account, string origin)
{
registry::Registry dapps(_self, _self.value);
auto iterator = dapps.find(common::toUUID(origin));
if (iterator != dapps.end())
{
if (account == iterator->owner)
require_auth(account);
else if (create_escrow::checkIfWhitelisted(account, origin))
require_auth(account);
else if (origin == "free")
print("using globally available free funds to create account");
else
check(false, ("only owner or whitelisted accounts can call this action for " + origin).c_str());
}
else
{
check(false, ("no owner account found for " + origin).c_str());
}
}
extern "C"
{
void apply(uint64_t receiver, uint64_t code, uint64_t action)
{
auto self = receiver;
if (code == self)
switch (action)
{
EOSIO_DISPATCH_HELPER(create_escrow, (init)(clean)(cleanreg)(cleantoken)(create)(define)(whitelist)(reclaim)(refundstakes)(stake)(unstake)(unstakenet)(unstakecpu)(fundnetloan)(fundcpuloan)(rentnet)(rentcpu)(topuploans)(ping))
}
else
{
if (code == name("eosio.token").value && action == name("transfer").value)
{
execute_action(name(receiver), name(code), &create_escrow::transfer);
}
}
}
}
} // namespace createescrow