forked from GetScatter/CreateBridge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateaccounts.cpp
More file actions
150 lines (120 loc) · 5.41 KB
/
createaccounts.cpp
File metadata and controls
150 lines (120 loc) · 5.41 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
#include <eosiolib/eosio.hpp>
#include "lib/common.h"
#include "models/accounts.h"
#include "models/balances.h"
#include "models/registry.h"
#include "contributions.cpp"
#include "airdrops.cpp"
using namespace eosio;
using namespace std;
class createaccounts : public contributions, public airdrops{
public:
name createbridge = common::createbridge;
/***
* Checks if an account is whitelisted for a dapp by the owner of the dapp
* @return
*/
void createJointAccount(string& memo, name& account, string& origin, accounts::authority& ownerAuth, accounts::authority& activeAuth){
// memo is the account that pays the remaining balance i.e
// balance needed for new account creation - (balance contributed by the contributors)
vector<balances::chosen_contributors> contributors;
name freeContributor;
asset balance;
asset requiredBalance;
symbol coreSymbol = common::getCoreSymbol();
asset ramFromDapp = asset(0'0000, coreSymbol);
balances::Balances balances(createbridge, createbridge.value);
registry::Registry dapps(createbridge, createbridge.value);
// gets the ram, net and cpu requirements for the new user accounts from the dapp registry
auto iterator = dapps.find(common::toUUID(origin));
uint64_t ram_bytes = iterator->ram_bytes;
// cost of required ram
asset ram = common::getRamCost(ram_bytes);
asset ramFromPayer = ram;
asset net = iterator->net;
asset cpu = iterator->cpu;
if(memo != origin && contributions::hasBalance(origin, ram)){
uint64_t originId = common::toUUID(origin);
auto dapp = balances.find(originId);
if(dapp != balances.end()){
uint64_t seed = account.value;
uint64_t value = name(memo).value;
contributors = getContributors(origin, memo, seed, value, ram);
for(std::vector<balances::chosen_contributors>::iterator itr = contributors.begin(); itr != contributors.end(); ++itr){
ramFromDapp += itr->rampay;
}
ramFromPayer -= ramFromDapp;
}
}
// find the balance of the "memo" account for the origin and check if it has balance >= total balance for RAM, CPU and net - (balance payed by the contributors)
if(ramFromPayer > asset(0'0000, coreSymbol)){
asset balance = contributions::findContribution(origin, name(memo));
requiredBalance = ramFromPayer + cpu + net;
// if the "memo" account doesn't have enough fund, check globally available "free" pool
if(balance < requiredBalance){
eosio_assert(false, ("Not enough balance in " + memo + " or donated by the contributors for " + origin + " to pay for account creation.").c_str());
}
}
createAccount(account, ownerAuth, activeAuth, ram, net, cpu);
// subtract the used balance
if(ramFromPayer.amount > 0)
{
subBalance(memo, origin, requiredBalance);
}
if(ramFromDapp.amount > 0){
for(std::vector<balances::chosen_contributors>::iterator itr = contributors.begin(); itr != contributors.end(); ++itr){
// check if the memo account and the dapp contributor is the same. If yes, only increament accounts created by 1
if(itr->contributor == name{memo} && ramFromPayer.amount > 0){
subBalance(itr->contributor.to_string(), origin, itr->rampay,true);
} else {
subBalance(itr->contributor.to_string(), origin, itr->rampay);
}
}
}
// airdrop dapp tokens if requested
airdrop(origin, account);
}
/***
* Checks if an account is whitelisted for a dapp by the owner of the dapp
* @return
*/
bool checkIfWhitelisted(name account, string dapp){
registry::Registry dapps(createbridge, createbridge.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;
}
/***
* Calls the chain to create a new account
*/
void createAccount(name& account, accounts::authority& ownerauth, accounts::authority& activeauth, asset& ram, asset& net, asset& cpu){
accounts::newaccount new_account = accounts::newaccount{
.creator = createbridge,
.name = account,
.owner = ownerauth,
.active = activeauth
};
name newAccountContract = common::getNewAccountContract();
action(
permission_level{ createbridge, "active"_n },
newAccountContract,
name("newaccount"),
new_account
).send();
action(
permission_level{ createbridge, "active"_n },
newAccountContract,
name("buyram"),
make_tuple(createbridge, account, ram)
).send();
action(
permission_level{ createbridge, "active"_n },
newAccountContract,
name("delegatebw"),
make_tuple(createbridge, account, net, cpu, true)
).send();
};
};