-
Notifications
You must be signed in to change notification settings - Fork 2
/
token.hpp
220 lines (186 loc) · 8.43 KB
/
token.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
#pragma once
#include <eosio/asset.hpp>
#include <eosio/eosio.hpp>
#include <string>
namespace eosiosystem {
class system_contract;
}
namespace eosio {
using std::string;
/**
* @defgroup eosiotoken eosio.token
* @ingroup eosiocontracts
*
* eosio.token contract
*
* @details eosio.token contract defines the structures and actions that allow users to create, issue, and manage
* tokens on eosio based blockchains.
* @{
*/
class [[eosio::contract("token")]] token : public contract {
public:
using contract::contract;
token( eosio::name receiver, eosio::name code, eosio::datastream<const char*> ds ): eosio::contract(receiver, code, ds), acclinks(receiver, code.value)
{}
[[eosio::action]]
void clearout( const name& owner, uint64_t iterations );
[[eosio::action]]
void linktoeth( const name& acc, std::string eth_acc );
[[eosio::action]]
void requestswap( const name& acc, std::string eth_acc, asset quantity, std::string memo );
[[eosio::action]]
void cancelswap( const name& acc );
[[eosio::action]]
void confirmswap( const name& acc );
/**
* Create action.
*
* @details Allows `issuer` account to create a token in supply of `maximum_supply`.
* @param issuer - the account that creates the token,
* @param maximum_supply - the maximum supply set for the token created.
*
* @pre Token symbol has to be valid,
* @pre Token symbol must not be already created,
* @pre maximum_supply has to be smaller than the maximum supply allowed by the system: 1^62 - 1.
* @pre Maximum supply must be positive;
*
* If validation is successful a new entry in statstable for token symbol scope gets created.
*/
[[eosio::action]]
void create( const name& issuer,
const asset& maximum_supply);
/**
* Issue action.
*
* @details This action issues to `to` account a `quantity` of tokens.
*
* @param to - the account to issue tokens to, it must be the same as the issuer,
* @param quntity - the amount of tokens to be issued,
* @memo - the memo string that accompanies the token issue transaction.
*/
[[eosio::action]]
void issue( const name& to, const asset& quantity, const string& memo );
/**
* Retire action.
*
* @details The opposite for create action, if all validations succeed,
* it debits the statstable.supply amount.
*
* @param quantity - the quantity of tokens to retire,
* @param memo - the memo string to accompany the transaction.
*/
[[eosio::action]]
void retire( const asset& quantity, const string& memo );
/**
* Transfer action.
*
* @details Allows `from` account to transfer to `to` account the `quantity` tokens.
* One account is debited and the other is credited with quantity tokens.
*
* @param from - the account to transfer from,
* @param to - the account to be transferred to,
* @param quantity - the quantity of tokens to be transferred,
* @param memo - the memo string to accompany the transaction.
*/
[[eosio::action]]
void transfer( const name& from,
const name& to,
const asset& quantity,
const string& memo );
/**
* Open action.
*
* @details Allows `ram_payer` to create an account `owner` with zero balance for
* token `symbol` at the expense of `ram_payer`.
*
* @param owner - the account to be created,
* @param symbol - the token to be payed with by `ram_payer`,
* @param ram_payer - the account that supports the cost of this action.
*
* More information can be read [here](https://github.com/EOSIO/eosio.contracts/issues/62)
* and [here](https://github.com/EOSIO/eosio.contracts/issues/61).
*/
[[eosio::action]]
void open( const name& owner, const symbol& symbol, const name& ram_payer );
/**
* Close action.
*
* @details This action is the opposite for open, it closes the account `owner`
* for token `symbol`.
*
* @param owner - the owner account to execute the close action for,
* @param symbol - the symbol of the token to execute the close action for.
*
* @pre The pair of owner plus symbol has to exist otherwise no action is executed,
* @pre If the pair of owner plus symbol exists, the balance has to be zero.
*/
[[eosio::action]]
void close( const name& owner, const symbol& symbol );
/**
* Get supply method.
*
* @details Gets the supply for token `sym_code`, created by `token_contract_account` account.
*
* @param token_contract_account - the account to get the supply for,
* @param sym_code - the symbol to get the supply for.
*/
static asset get_supply( const name& token_contract_account, const symbol_code& sym_code )
{
stats statstable( token_contract_account, sym_code.raw() );
const auto& st = statstable.get( sym_code.raw() );
return st.supply;
}
/**
* Get balance method.
*
* @details Get the balance for a token `sym_code` created by `token_contract_account` account,
* for account `owner`.
*
* @param token_contract_account - the token creator account,
* @param owner - the account for which the token balance is returned,
* @param sym_code - the token for which the balance is returned.
*/
static asset get_balance( const name& token_contract_account, const name& owner, const symbol_code& sym_code )
{
accounts accountstable( token_contract_account, owner.value );
const auto& ac = accountstable.get( sym_code.raw() );
return ac.balance;
}
using create_action = eosio::action_wrapper<"create"_n, &token::create>;
using issue_action = eosio::action_wrapper<"issue"_n, &token::issue>;
using retire_action = eosio::action_wrapper<"retire"_n, &token::retire>;
using transfer_action = eosio::action_wrapper<"transfer"_n, &token::transfer>;
using open_action = eosio::action_wrapper<"open"_n, &token::open>;
using close_action = eosio::action_wrapper<"close"_n, &token::close>;
using clearout_action = eosio::action_wrapper<"clearout"_n, &token::clearout>;
using linktoeth_action = eosio::action_wrapper<"linktoeth"_n, &token::linktoeth>;
using requestswap_action = eosio::action_wrapper<"requestswap"_n, &token::requestswap>;
using cancelswap_action = eosio::action_wrapper<"cancelswap"_n, &token::cancelswap>;
using confirmswap_action = eosio::action_wrapper<"confirmswap"_n, &token::confirmswap>;
struct [[eosio::table]] link_account {
name eos_acc;
std::string eth_acc;
asset pending_to_eth;
std::string memo;
uint64_t primary_key()const { return eos_acc.value; }
};
typedef eosio::multi_index< "linkaccount"_n, link_account > links;
links acclinks;
private:
struct [[eosio::table]] account {
asset balance;
uint64_t primary_key()const { return balance.symbol.code().raw(); }
};
struct [[eosio::table]] currency_stats {
asset supply;
asset max_supply;
name issuer;
uint64_t primary_key()const { return supply.symbol.code().raw(); }
};
typedef eosio::multi_index< "accounts"_n, account > accounts;
typedef eosio::multi_index< "stat"_n, currency_stats > stats;
void sub_balance( const name& owner, const asset& value );
void add_balance( const name& owner, const asset& value, const name& ram_payer );
};
/** @}*/ // end of @defgroup eosiotoken eosio.token
} /// namespace eosio