-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathpoorman.token.cpp
218 lines (166 loc) · 7.73 KB
/
poorman.token.cpp
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
/**
* @file
* @copyright defined in eos/LICENSE.txt
*/
#include "include/poorman.token.hpp"
namespace genereos {
void token::create( name issuer, asset maximum_supply )
{
require_auth( _self );
auto sym = maximum_supply.symbol;
check( sym.is_valid(), "Invalid symbol name" );
check( maximum_supply.is_valid(), "Invalid supply" );
check( maximum_supply.amount > 0, "Maximum supply must be positive" );
auto sym_code_raw = sym.code().raw();
stats statstable( _self, sym_code_raw );
auto existing = statstable.find( sym_code_raw );
check( existing == statstable.end(), "Token with that symbol name exists" );
statstable.emplace( _self, [&]( auto& s )
{
s.supply.symbol = maximum_supply.symbol;
s.max_supply = maximum_supply;
s.issuer = issuer;
});
}
void token::issue( name to, asset quantity, string memo )
{
do_issue( to, quantity, memo, true );
}
void token::issuefree( name to, asset quantity, string memo )
{
do_issue( to, quantity, memo, false );
}
void token::burn( name from, asset quantity, string memo )
{
auto sym = quantity.symbol;
check( sym.is_valid(), "Invalid symbol name" );
check( memo.size() <= 256, "Memo must be less than 256 characters" );
auto sym_code_raw = sym.code().raw();
stats statstable( _self, sym_code_raw );
auto existing = statstable.find( sym_code_raw );
check( existing != statstable.end(), "Token with that symbol name does not exist - Please create the token before burning" );
const auto& st = *existing;
require_auth( from );
require_recipient( from );
check( quantity.is_valid(), "Invalid quantity value" );
check( quantity.amount > 0, "Quantity value must be positive" );
check( st.supply.symbol == quantity.symbol, "Symbol precision mismatch" );
check( st.supply.amount >= quantity.amount, "Quantity value cannot exceed the available supply" );
statstable.modify( st, same_payer, [&]( auto& s )
{
s.supply -= quantity;
});
sub_balance( from, quantity );
}
void token::signup( name owner, asset quantity )
{
auto sym = quantity.symbol;
check( sym.is_valid(), "Invalid symbol name" );
auto sym_code_raw = sym.code().raw();
stats statstable( _self, sym_code_raw );
auto existing = statstable.find( sym_code_raw );
check( existing != statstable.end(), "Token with that symbol name does not exist - Please create the token before issuing" );
const auto& st = *existing;
require_auth( owner );
require_recipient( owner );
accounts to_acnts( _self, owner.value );
auto to = to_acnts.find( sym_code_raw );
check( to == to_acnts.end(), "You have already signed up" );
check( quantity.is_valid(), "Invalid quantity value" );
check( quantity.amount == 0, "Quantity exceeds signup allowance" );
check( st.supply.symbol == quantity.symbol, "Symbol precision mismatch" );
check( st.max_supply.amount - st.supply.amount >= quantity.amount, "Quantity value cannot exceed the available supply" );
statstable.modify( st, same_payer, [&]( auto& s ) {
s.supply += quantity;
});
add_balance( owner, quantity, owner );
}
void token::transfer( name from, name to, asset quantity, string memo )
{
do_transfer( from, to, quantity, memo, true );
}
void token::transferfree( name from, name to, asset quantity, string memo )
{
do_transfer( from, to, quantity, memo, false );
}
void token::do_issue( name to, asset quantity, string memo, bool pay_ram )
{
auto sym = quantity.symbol;
check( sym.is_valid(), "Invalid symbol name" );
check( memo.size() <= 256, "Memo must be less than 256 characters" );
auto sym_code_raw = sym.code().raw();
stats statstable( _self, sym_code_raw );
auto existing = statstable.find( sym_code_raw );
check( existing != statstable.end(), "Token with that symbol name does not exist - Please create the token before issuing" );
const auto& st = *existing;
require_auth( st.issuer );
check( quantity.is_valid(), "Invalid quantity value" );
check( quantity.amount > 0, "Quantity value must be positive" );
check( st.supply.symbol == quantity.symbol, "Symbol precision mismatch" );
check( st.max_supply.amount - st.supply.amount >= quantity.amount, "Quantity value cannot exceed the available supply" );
statstable.modify( st, same_payer, [&]( auto& s )
{
s.supply += quantity;
});
add_balance( st.issuer, quantity, st.issuer );
if (to != st.issuer) {
if (pay_ram == true) {
SEND_INLINE_ACTION( *this, transfer, { st.issuer, name("active") }, { st.issuer, to, quantity, memo } );
} else {
SEND_INLINE_ACTION( *this, transferfree, { st.issuer, name("active") }, { st.issuer, to, quantity, memo } );
}
}
}
void token::do_transfer( name from, name to, asset quantity, string memo, bool pay_ram )
{
require_auth( from );
check( from != to, "Cannot transfer to self" );
check( is_account( to ), "to Account does not exist" );
auto sym = quantity.symbol;
stats statstable( _self, sym.code().raw() );
const auto& st = statstable.get( sym.code().raw(), "Token with that symbol name does not exist - Please create the token before transferring" );
require_recipient( from );
require_recipient( to );
check( quantity.is_valid(), "Invalid quantity value" );
check( quantity.amount > 0, "Quantity value must be positive" );
check( st.supply.symbol == quantity.symbol, "Symbol precision mismatch" );
check( memo.size() <= 256, "Memo must be less than 256 characters" );
sub_balance( from, quantity );
add_balance( to, quantity, from, pay_ram );
}
void token::sub_balance( name owner, asset value )
{
accounts from_acnts( _self, owner.value );
auto sym = value.symbol;
auto from = from_acnts.find( sym.code().raw() );
check( from != from_acnts.end(), "No balance object found under the token balance owner's account" );
const auto& from_iter = *from;
check( from_iter.balance.amount >= value.amount, "Overdrawn balance will result in a nonexistent negative balance of the account" );
if ( from_iter.balance.amount == value.amount ) {
from_acnts.erase( from_iter );
} else {
from_acnts.modify( from_iter, owner, [&]( auto& a )
{
a.balance -= value;
});
}
}
void token::add_balance( name owner, asset value, name ram_payer, bool pay_ram )
{
accounts to_acnts( _self, owner.value );
auto to = to_acnts.find( value.symbol.code().raw() );
if ( to == to_acnts.end() ) {
check( pay_ram == true, "Destination account does not have balance" );
to_acnts.emplace( ram_payer, [&]( auto& a )
{
a.balance = value;
});
} else {
to_acnts.modify( to, same_payer, [&]( auto& a )
{
a.balance += value;
});
}
}
} // namespace genereos
EOSIO_DISPATCH( genereos::token, (create)(issue)(issuefree)(burn)(signup)(transfer)(transferfree) )