-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstants.cpp
More file actions
129 lines (111 loc) · 3.43 KB
/
constants.cpp
File metadata and controls
129 lines (111 loc) · 3.43 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
#pragma once
#include "createescrow.hpp"
namespace createescrow
{
using namespace eosio;
using std::string;
using std::vector;
/***
* Returns the symbol of the core token of the chain or the token used to pay for new account creation
* @return
*/
symbol create_escrow::getCoreSymbol()
{
Token token(_self, _self.value);
return token.begin()->S_SYS;
}
/***
* Returns the contract name for new account action
*/
name create_escrow::getNewAccountContract()
{
Token token(_self, _self.value);
return token.begin()->newaccountcontract;
}
name create_escrow::getNewAccountAction()
{
Token token(_self, _self.value);
return token.begin()->newaccountaction;
}
/**
* Returns the minimum bytes of RAM for new account creation
*/
uint64_t create_escrow::getMinimumRAM()
{
Token token(_self, _self.value);
return token.begin()->min_ram;
}
/***
* Returns the price of ram for given bytes
*/
asset create_escrow::getRamCost(uint64_t ram_bytes, uint64_t priceKey)
{
asset ramcost;
if (ram_bytes > 0)
{
RamInfo ramInfo(name("eosio"), name("eosio").value);
auto ramData = ramInfo.find(S_RAM.raw());
symbol coreSymbol = create_escrow::getCoreSymbol();
check(ramData != ramInfo.end(), "Could not get RAM info");
uint64_t base = ramData->base.balance.amount;
uint64_t quote = ramData->quote.balance.amount;
ramcost = asset((((double)quote / base)) * ram_bytes, coreSymbol);
}
else
{ //if account is tier fixed
Token token(_self, _self.value);
name newaccountcontract = create_escrow::getNewAccountContract();
priceTable price(newaccountcontract, newaccountcontract.value);
auto priceItr = price.find(priceKey);
ramcost.amount = priceItr->createprice.amount - (priceItr->netamount.amount + priceItr->cpuamount.amount);
ramcost.symbol = priceItr->createprice.symbol;
}
return ramcost;
}
asset create_escrow::getFixedCpu(uint64_t priceKey)
{
name newaccountcontract = create_escrow::getNewAccountContract();
priceTable price(newaccountcontract, newaccountcontract.value);
auto priceItr = price.find(priceKey);
return priceItr->cpuamount;
}
asset create_escrow::getFixedNet(uint64_t priceKey)
{
name newaccountcontract = create_escrow::getNewAccountContract();
priceTable price(newaccountcontract, newaccountcontract.value);
auto priceItr = price.find(priceKey);
return priceItr->netamount;
}
auto create_escrow::getCpuLoanRecord(name account)
{
rex_cpu_loan_table cpu_loans(name("eosio"), name("eosio").value);
auto cpu_idx = cpu_loans.get_index<"byowner"_n>();
auto loans = cpu_idx.find(_self.value);
auto i = cpu_idx.lower_bound(_self.value);
while (i != cpu_idx.end())
{
if (i->receiver == account)
{
return i;
};
i++;
};
check(false, ("No existing loan found for" + account.to_string()).c_str());
}
auto create_escrow::getNetLoanRecord(name account)
{
rex_net_loan_table net_loans(name("eosio"), name("eosio").value);
auto net_idx = net_loans.get_index<"byowner"_n>();
auto loans = net_idx.find(_self.value);
auto i = net_idx.lower_bound(_self.value);
while (i != net_idx.end())
{
if (i->receiver == account)
{
return i;
};
i++;
};
check(false, ("No existing loan found for" + account.to_string()).c_str());
}
} // namespace createescrow