-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patheosdactoken.hpp
247 lines (209 loc) · 8.9 KB
/
eosdactoken.hpp
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
/**
This contract follows the erc20 standard
contract ERC20Interface {
function totalSupply() public constant returns (uint);
function balanceOf(address tokenOwner) public constant returns (uint balance);
function allowance(address tokenOwner, address spender) public constant returns (uint remaining);
function transfer(address to, uint tokens) public returns (bool success);
function approve(address spender, uint tokens) public returns (bool success);
function transferFrom(address from, address to, uint tokens) public returns (bool success);
}
*/
#pragma once
#include <eosiolib/eosio.hpp>
#include <eosiolib/asset.hpp>
#include <eosiolib/multi_index.hpp>
#include <vector>
using std::string;
using std::array;
using namespace eosio;
using boost::container::flat_map;
//one cheque
struct approvetoPair
{
account_name to;//cheque to who
int64_t value;//cheque value
approvetoPair() {}
EOSLIB_SERIALIZE(approvetoPair, (to)(value))
};
/*
* cheques list of someone
@abi table approves
*/
struct approveto {
symbol_type symbol_name;//name of symbol
std::vector<approvetoPair> approved;//list of cheque to others
uint64_t primary_key()const { return symbol_name.name(); }
EOSLIB_SERIALIZE( approveto, (symbol_name)(approved))
};
/*
account info
*/
struct account {
asset balance;//account balance
uint64_t primary_key()const { return balance.symbol.name(); }
EOSLIB_SERIALIZE( account, (balance))
};
/*
status of one currency
*/
struct curstats {
asset supply;//amount of money already supplied
asset max_supply;//Maximum supply of money
account_name issuer;//Currency issuer
uint64_t primary_key()const { return supply.symbol.name(); }
EOSLIB_SERIALIZE( curstats, (supply)(max_supply)(issuer))
};
class eosdactoken : public contract {
public:
eosdactoken( account_name self )
:contract(self)
{}
struct fee_schedule {
uint64_t primary_key()const { return 0; }
array<extended_asset,7> fee_per_length;
EOSLIB_SERIALIZE( fee_schedule, (fee_per_length) )
};
// ------------------------------------------------------------------------
// get amount of `spender` can withdraw from your account
// ------------------------------------------------------------------------
uint64_t allowanceOf( account_name owner,
account_name spender,
symbol_name symbol){
Approves approveobj(get_self(), owner);
if(approveobj.find(symbol) != approveobj.end()){
const auto &approSymIte = approveobj.get(symbol);
auto approvetoPairIte = approSymIte.approved.begin();
while(approvetoPairIte != approSymIte.approved.end()){
if(approvetoPairIte->to == spender){
print("allowanceOf[", account_name(approvetoPairIte->to), "]=", approvetoPairIte->value);
return approvetoPairIte->value;
}
approvetoPairIte++;
}
if(approvetoPairIte == approSymIte.approved.end()){
print("allowanceOf[", account_name(spender), "]=", 0);
}
}else{
print("allowanceOf[", (account_name)spender, "]=", 0);
}
return 0;
}
typedef eosio::multi_index<N(accounts), account> Accounts;
typedef eosio::multi_index<N(stats), curstats> Stats;
typedef eosio::multi_index<N(stat), curstats> Stat;
typedef eosio::multi_index<N(approves), approveto> Approves;
asset get_balance( account_name owner, symbol_name symbol )const {
Accounts t( _self, owner );
if(t.find(symbol) == t.end()){
asset as;
as.amount = 0;
as.symbol = symbol_type(symbol);
return as;
}
return t.get(symbol).balance;
}
asset get_supply( symbol_name symbol )const {
Stats statstable( _self, symbol_type(symbol).name());
const auto& st = statstable.get(symbol_type(symbol).name());
return st.max_supply;
}
// ------------------------------------------------------------------------
//A currency is issued, the issuer is ‘issuer’
// ------------------------------------------------------------------------
/// @abi action
void create( account_name issuer,
asset currency
);
// ------------------------------------------------------------------------
// Transfer the balance from token owner's account to `to` account
// - from's account must have sufficient balance to transfer
// ------------------------------------------------------------------------
/// @abi action
void transfer(
account_name from,
account_name to,
asset quantity,
string memo);
// ------------------------------------------------------------------------
// Transfer the balance from token owner's account to `to` account,and transfer 'feequantity' to 'tofeeadmin' account at the same time
// - from's account must have sufficient balance to transfer
// ------------------------------------------------------------------------
/// @abi action
void transferfee( account_name from,
account_name to,
asset quantity,
account_name tofeeadmin,
asset feequantity,
string memo);
// ------------------------------------------------------------------------
//Issue money to users
// ------------------------------------------------------------------------
/// @abi action
void issue(
account_name to,
asset quantity,
string memo);
// ------------------------------------------------------------------------
//for standard:cleos get currency command
// ------------------------------------------------------------------------
/// @abi action
void copystates(std::string symbol);
// ------------------------------------------------------------------------
// Allow `spender` to withdraw from your account, multiple times, up to the `quantity` amount.
// If this function is called again it overwrites the current allowance with value.
// ------------------------------------------------------------------------
/// @abi action
void approve(
account_name owner,
account_name spender,
asset quantity);
// ------------------------------------------------------------------------
// Returns the amount of tokens approved by the owner that can be
// transferred to the spender's account
// ------------------------------------------------------------------------
/// @abi action
void allowance(
account_name owner,
account_name spender,
std::string symbol);
// ------------------------------------------------------------------------
// Transfer `tokens` from the `from` account to the `to` account
//
// The calling account must already have sufficient tokens approve(...)-d
// for spending from the `from` account and
// - From account must have sufficient balance to transfer
// - Spender must have sufficient allowance to transfer
// ------------------------------------------------------------------------
/// @abi action
void transferfrom(
account_name from,
account_name to,
asset quantity);
// ------------------------------------------------------------------------
//Get the token on symbol balance for account `owner`
// ------------------------------------------------------------------------
/// @abi action
void balanceof(
account_name owner,
std::string symbol);
// ------------------------------------------------------------------------
// Total supply of token symbol
// ------------------------------------------------------------------------
//@abi action
void totalsupply(std::string symbol){
symbol_name sn = string_to_symbol(4, symbol.c_str());
print("totalsupply[",symbol.c_str(),"]", get_supply(sn).amount);
}
// ------------------------------------------------------------------------
// claim token
// ------------------------------------------------------------------------
//@abi action
void claim(account_name claimer, std::string symbol);
private:
//Sub currency to the 'owner' account,and the cost of 'bindwidth ram、cpu' is paid by payer
void sub_balance( account_name owner, asset currency, uint64_t payer);
//Add currency to the 'owner' account,and the cost of 'bindwidth ram、cpu' is paid by payer
void add_balance( account_name owner, asset currency, account_name payer );
void checkasset(const asset &quantity);
};