-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontributions.cpp
More file actions
367 lines (312 loc) · 12.7 KB
/
contributions.cpp
File metadata and controls
367 lines (312 loc) · 12.7 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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#include <eosio/system.hpp>
#include <cstdlib>
#include "createescrow.hpp"
#include "lib/common.h"
#include "models/accounts.h"
#include "models/balances.h"
#include "models/registry.h"
#include "constants.cpp"
namespace createescrow
{
using namespace eosio;
using namespace std;
/*
* Returns the total balance contributed for a dapp
*/
asset create_escrow::balanceFor(string &memo)
{
balance::Balances balances(_self, _self.value);
uint64_t payerId = common::toUUID(memo);
auto payer = balances.find(payerId);
if (payer == balances.end())
return asset(0'0000, create_escrow::getCoreSymbol());
return payer->total_balance;
}
/*
* Checks whether the balance for an account is greater than the required balance
*/
bool create_escrow::hasBalance(string memo, const asset &quantity)
{
return balanceFor(memo).amount > quantity.amount;
}
/*
* Adds the amount contributed by the contributor for an app to the balances table
* Called by the internal transfer function
*/
void create_escrow::addBalance(const name &from, const asset &quantity, string &memo)
{
vector<string> stats = common::split(memo, ",");
auto dapp = stats[0];
uint64_t id = common::toUUID(dapp);
symbol core_symbol = create_escrow::getCoreSymbol();
int ram = dapp == "free" ? 100 : stoi(stats[1]);
int totalaccounts = stats.size() > 2 ? stoi(stats[2]) : -1;
asset rex_balance = asset(0'0000, core_symbol);
registry::Registry dapps(_self, _self.value);
auto itr = dapps.find(common::toUUID(dapp));
balance::Balances balances(_self, _self.value);
auto iterator = balances.find(id);
name newAccountContract = create_escrow::getNewAccountContract();
// only owner or the whitelisted account are allowed to contribute for cpu and net
// for globally available free funds, anybody can contribute
if (create_escrow::checkIfOwner(from, dapp) || create_escrow::checkIfWhitelisted(from, dapp) || dapp == "free")
{
// cpu or net balance are passed in as 1000000 in memo for a value like 100.0000 SYS
int64_t rex_quantity = stats.size() > 3 ? stoi(stats[3]) : 0'0000;
rex_balance = asset(rex_quantity, core_symbol);
// deposit rex funds for cpu and net
if (rex_balance > asset(0'0000, core_symbol))
{
if (itr->use_rex == true)
{
action(
permission_level{_self, "active"_n},
newAccountContract,
name("deposit"),
make_tuple(_self, rex_balance))
.send();
}
else
{
check(false, ("Rex not enabled for " + dapp).c_str());
}
}
asset balance = quantity - (rex_balance);
if (iterator == balances.end())
balances.emplace(_self, [&](auto &row) {
row.memo = id;
row.contributors.push_back({from, balance, rex_balance, asset(0'0000, core_symbol), asset(0'0000, core_symbol), asset(0'0000, core_symbol), ram, totalaccounts, 0});
row.total_balance = balance;
row.origin = dapp;
row.timestamp = eosio::current_time_point().sec_since_epoch();
});
else
balances.modify(iterator, same_payer, [&](auto &row) {
auto pred = [from](const balance::contributors &item) {
return item.contributor == from;
};
std::vector<balance::contributors>::iterator itr = std::find_if(std::begin(row.contributors), std::end(row.contributors), pred);
if (itr != std::end(row.contributors))
{
itr->balance += balance;
itr->rex_balance += rex_balance;
itr->ram_contribution_percent = ram;
itr->totalaccounts = totalaccounts;
}
else
{
row.contributors.push_back({from, balance, rex_balance, asset(0'0000, core_symbol), asset(0'0000, core_symbol), asset(0'0000, core_symbol), ram, totalaccounts, 0});
row.timestamp = eosio::current_time_point().sec_since_epoch();
}
row.total_balance += balance;
});
}
}
/*
* Subtracts the amount used to create an account from the total amount contributed by a contributor for an app
* Also checks if the memo account is same as one of the dapp contributors. If yes, then only increases the createdaccount field by 1
* Called by the create action
*/
void create_escrow::subBalance(string memo, string &origin, const asset &quantity, bool memoIsDapp)
{
uint64_t id = common::toUUID(origin);
balance::Balances balances(_self, _self.value);
auto iterator = balances.find(id);
check(iterator != balances.end(), "No balance object");
check(iterator->total_balance.amount >= quantity.amount, "overdrawn balance");
balances.modify(iterator, same_payer, [&](auto &row) {
auto pred = [memo](const balance::contributors &item) {
return item.contributor == name(memo);
};
auto itr = std::find_if(std::begin(row.contributors), std::end(row.contributors), pred);
if (itr != std::end(row.contributors))
{
row.total_balance -= quantity;
itr->balance -= quantity;
itr->total_spent += quantity;
if (!memoIsDapp)
{
itr->createdaccounts += 1;
}
if (row.total_balance.amount <= 0 && itr->rex_balance.amount <= 0)
{
row.contributors.erase(itr);
}
}
else
{
check(false, ("The account " + memo + "not found as one of the contributors for " + origin).c_str());
}
});
}
/* subtracts the balance used to stake/rex for cpu */
void create_escrow::subCpuOrNetBalance(string memo, string &origin, const asset &quantity, bool use_rex_balance)
{
uint64_t id = common::toUUID(origin);
balance::Balances balances(_self, _self.value);
auto iterator = balances.find(id);
check(iterator != balances.end(), "No balance object");
balances.modify(iterator, same_payer, [&](auto &row) {
auto pred = [memo](const balance::contributors &item) {
return item.contributor == name(memo);
};
auto itr = std::find_if(std::begin(row.contributors), std::end(row.contributors), pred);
if (itr != std::end(row.contributors))
{
check(itr->balance.amount >= quantity.amount, "overdrawn balance");
itr->balance -= quantity;
if (use_rex_balance)
{
itr->total_spent_rex += quantity;
}
else
{
itr->total_staked += quantity;
}
}
else
{
check(false, ("The account " + memo + " not found as one of the contributors for " + origin).c_str());
}
});
}
/**********************************************/
/*** ***/
/*** Helpers ***/
/*** ***/
/**********************************************/
/***
* Gets the balance of a contributor for a dapp
* @return
*/
asset create_escrow::findContribution(string dapp, name contributor, string type)
{
balance::Balances balances(_self, _self.value);
uint64_t id = common::toUUID(dapp);
auto iterator = balances.find(id);
symbol coreSymbol = create_escrow::getCoreSymbol();
auto msg = "No contribution found for " + dapp + " by " + contributor.to_string() + ".";
// if no record found for the dapp in the balances table, return the balance for the contributor as 0
if (iterator != balances.end())
{
auto pred = [contributor](const balance::contributors &item) {
return item.contributor == contributor;
};
auto itr = std::find_if(std::begin(iterator->contributors), std::end(iterator->contributors), pred);
if (itr != std::end(iterator->contributors))
{
if (type == "ram" || type == "net" || type == "cpu")
{
return itr->balance;
}
if (type == "rex")
{
return itr->rex_balance;
}
}
else
{
print(msg.c_str());
return asset(0'0000, coreSymbol);
}
}
else
{
print(msg.c_str());
return asset(0'0000, coreSymbol);
}
}
/***
* Gets the % RAM contribution of a contributor for a dapp
* @return
*/
int create_escrow::findRamContribution(string dapp, name contributor)
{
balance::Balances balances(_self, _self.value);
uint64_t id = common::toUUID(dapp);
auto iterator = balances.find(id);
symbol coreSymbol = create_escrow::getCoreSymbol();
auto msg = "No contribution found for " + dapp + " by " + contributor.to_string() + ". Checking the globally available free fund.";
// if no record found for the dapp in the balances table, return the balance for the contributor as 0
if (iterator != balances.end())
{
auto pred = [contributor](const balance::contributors &item) {
return item.contributor == contributor;
};
auto itr = std::find_if(std::begin(iterator->contributors), std::end(iterator->contributors), pred);
if (itr != std::end(iterator->contributors))
{
return itr->ram_contribution_percent;
}
else
{
print(msg.c_str());
return 0;
}
}
else
{
print(msg.c_str());
return 0;
}
}
/**
* Randomly select the contributors for a dapp
* It has following steps:
* 1. Generate a random number with the new account name as the seed and the payer account name as the value
* 2. Mod the individual digits in the number by the contributors vector size to get all the indices within the vector size
* 3. Remove the duplicate indices
* 4. Chooses the contributors which have created accounts < total accounts until the max contribution upto 100% is reached
*/
vector<balance::chosen_contributors> create_escrow::getContributors(string origin, string memo, uint64_t seed, uint64_t to, asset ram)
{
balance::Balances balances(_self, _self.value);
auto iterator = balances.find(common::toUUID(origin));
vector<balance::contributors> initial_contributors = iterator->contributors;
vector<balance::contributors> final_contributors;
vector<balance::chosen_contributors> chosen_contributors;
vector<int> chosen_index;
// generate a random number with new account name as the seed
uint64_t number = common::generate_random(seed, to);
int size = initial_contributors.size();
// get the index from the contributors corresponding to individual digits of the random number generated in the above step
while (size > 0 && number > 0)
{
int digit = number % 10;
// modulus the digit of the random number by the initial_contributors to get the randomly generated indices within the size of the vector
int index = digit % initial_contributors.size();
// make sure not the same contributor is chosen twice
if (std::find(chosen_index.begin(), chosen_index.end(), index) == chosen_index.end())
{
chosen_index.push_back(index);
final_contributors.push_back(initial_contributors[index]);
}
number /= 10;
size--;
}
int final_size = final_contributors.size();
int i = 0;
int max_ram_contribution = 100;
int total_ram_contribution = 0;
// choose the contributors to get the total contributions for RAM as close to 100% as possible
while (total_ram_contribution < max_ram_contribution && i < final_size)
{
//check if the total account creation limit has been reached for a contributor
if ((final_contributors[i].createdaccounts < final_contributors[i].totalaccounts || final_contributors[i].totalaccounts == -1) && final_contributors[i].contributor != name(memo))
{
int ram_contribution = findRamContribution(origin, final_contributors[i].contributor);
total_ram_contribution += ram_contribution;
if (total_ram_contribution > max_ram_contribution)
{
ram_contribution -= (total_ram_contribution - max_ram_contribution);
}
asset ram_amount = (ram_contribution * ram) / 100;
chosen_contributors.push_back(
{final_contributors[i].contributor,
ram_amount});
}
i++;
}
return chosen_contributors;
}
} // namespace createescrow