-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrex.cpp
More file actions
203 lines (169 loc) · 6.98 KB
/
rex.cpp
File metadata and controls
203 lines (169 loc) · 6.98 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
#include "lib/common.h"
#include "models/registry.h"
#include "createescrow.hpp"
#include "constants.cpp"
namespace createescrow
{
// finds the existing net loan from createescrow to user account and funds it
void create_escrow::fundnetloan(name &from, name &to, asset quantity, string &origin)
{
create_escrow::checkIfOwnerOrWhitelisted(from, origin);
create_escrow::fundloan(to, quantity, origin, "net");
create_escrow::subCpuOrNetBalance(from.to_string(), origin, quantity, true);
}
// finds the existing cpu loan from createescrow to user account and funds it
void create_escrow::fundcpuloan(name &from, name &to, asset quantity, string &origin)
{
create_escrow::checkIfOwnerOrWhitelisted(from, origin);
create_escrow::fundloan(to, quantity, origin, "cpu");
create_escrow::subCpuOrNetBalance(from.to_string(), origin, quantity, true);
}
// creates a new loan from createescrow to the user acount (to)
void create_escrow::rentnet(name &from, name &to, string &origin)
{
create_escrow::checkIfOwnerOrWhitelisted(from, origin);
auto iterator = dapps.find(common::toUUID(origin));
create_escrow::rentrexnet(origin, to);
asset quantity = iterator->rex->net_loan_payment + iterator->rex->net_loan_fund;
create_escrow::subCpuOrNetBalance(from.to_string(), origin, quantity, true);
}
// creates a new loan from createescrow to the user acount (to)
void create_escrow::rentcpu(name &from, name &to, string &origin)
{
create_escrow::checkIfOwnerOrWhitelisted(from, origin);
auto iterator = dapps.find(common::toUUID(origin));
create_escrow::rentrexcpu(origin, to);
asset quantity = iterator->rex->cpu_loan_payment + iterator->rex->cpu_loan_fund;
create_escrow::subCpuOrNetBalance(from.to_string(), origin, quantity, true);
}
// topup existing loan balance (cpu & net) for a user up to the given quantity
// If no existing loan, then create a new loan
void create_escrow::topuploans(name &from, name &to, asset &cpuquantity, asset &netquantity, string &origin)
{
create_escrow::checkIfOwnerOrWhitelisted(from, origin);
asset required_net_bal, required_cpu_bal;
tie(required_net_bal, required_cpu_bal) = create_escrow::rextopup(to, cpuquantity, netquantity, origin);
create_escrow::subCpuOrNetBalance(from.to_string(), origin, required_net_bal + required_cpu_bal, true);
}
void create_escrow::rentrexnet(string dapp, name account)
{
registry::Registry dapps(_self, _self.value);
auto iterator = dapps.find(common::toUUID(dapp));
if (iterator != dapps.end())
dapps.modify(iterator, same_payer, [&](auto &row) {
// check if the dapp has opted for rex
if (row.use_rex == true)
{
action(
permission_level{_self, "active"_n},
create_escrow::getNewAccountContract(),
name("rentnet"),
make_tuple(_self, account, row.rex->net_loan_payment, row.rex->net_loan_fund))
.send();
}
else
{
check(row.use_rex, ("Rex not enabled for" + dapp).c_str());
}
});
}
void create_escrow::rentrexcpu(string dapp, name account)
{
registry::Registry dapps(_self, _self.value);
auto iterator = dapps.find(common::toUUID(dapp));
if (iterator != dapps.end())
dapps.modify(iterator, same_payer, [&](auto &row) {
// check if the dapp has opted for rex
if (row.use_rex == true)
{
action(
permission_level{_self, "active"_n},
create_escrow::getNewAccountContract(),
name("rentcpu"),
make_tuple(_self, account, row.rex->cpu_loan_payment, row.rex->cpu_loan_fund))
.send();
}
else
{
check(row.use_rex, ("Rex not enabled for" + dapp).c_str());
}
});
}
void create_escrow::fundloan(name to, asset quantity, string dapp, string type)
{
registry::Registry dapps(_self, _self.value);
auto iterator = dapps.find(common::toUUID(dapp));
check(iterator->use_rex, ("Rex not enabled for" + dapp).c_str());
if (type == "net")
{
auto loan_record = create_escrow::getNetLoanRecord(to);
action(
permission_level{_self, "active"_n},
create_escrow::getNewAccountContract(),
name("fundnetloan"),
make_tuple(_self, loan_record->loan_num, quantity))
.send();
}
if (type == "cpu")
{
auto loan_record = create_escrow::getCpuLoanRecord(to);
action(
permission_level{_self, "active"_n},
create_escrow::getNewAccountContract(),
name("fundcpuloan"),
make_tuple(_self, loan_record->loan_num, quantity))
.send();
}
}
std::tuple<asset, asset> create_escrow::rextopup(name to, asset cpuquantity, asset netquantity, string dapp)
{
asset required_cpu_loan_bal;
asset required_net_loan_bal;
registry::Registry dapps(_self, _self.value);
auto iterator = dapps.find(common::toUUID(dapp));
check(iterator->use_rex, ("Rex not enabled for" + dapp).c_str());
auto net_loan_record = create_escrow::getNetLoanRecord(to);
auto cpu_loan_record = create_escrow::getCpuLoanRecord(to);
if (net_loan_record->receiver == to)
{
asset existing_net_loan_bal = net_loan_record->balance;
required_net_loan_bal = netquantity - existing_net_loan_bal;
if (required_net_loan_bal > asset(0'0000, create_escrow::getCoreSymbol()))
{
action(
permission_level{_self, "active"_n},
create_escrow::getNewAccountContract(),
name("fundnetloan"),
make_tuple(_self, net_loan_record->loan_num, required_net_loan_bal))
.send();
}
}
else
{
// creates a new rex loan with the loan fund/balance provided in app registration
create_escrow::rentrexnet(dapp, to);
}
if (cpu_loan_record->receiver == to)
{
asset existing_cpu_loan_bal = cpu_loan_record->balance;
required_cpu_loan_bal = cpuquantity - existing_cpu_loan_bal;
if (required_cpu_loan_bal > asset(0'0000, create_escrow::getCoreSymbol()))
{
action(
permission_level{_self, "active"_n},
create_escrow::getNewAccountContract(),
name("fundcpuloan"),
make_tuple(_self, cpu_loan_record->loan_num, required_cpu_loan_bal))
.send();
}
}
else
{
// creates a new rex loan with the loan fund/balance provided in app registration
required_cpu_loan_bal = iterator->rex->cpu_loan_payment + iterator->rex->cpu_loan_fund;
required_net_loan_bal = iterator->rex->net_loan_payment + iterator->rex->net_loan_fund;
create_escrow::rentrexcpu(dapp, to);
}
return make_tuple(required_cpu_loan_bal, required_net_loan_bal);
}
} // namespace createescrow