-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathaward_manage.cc
More file actions
427 lines (379 loc) · 11.9 KB
/
award_manage.cc
File metadata and controls
427 lines (379 loc) · 11.9 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
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
#include "xchain/xchain.h"
const char* BALANCEPRE = "balanceOf_";
const char* ALLOWANCEPRE = "allowanceOf_";
const char* MASTERPRE = "owner";
// 积分管理合约的基类
// 积分管理合约需要实现基类中指定的方法
// 参数由xchain::Contract中的context提供
class AwardBasic {
public:
/*
* func: 初始化积分管理账户以及总发行量
* @param: initiator:交易发起者,也是初始化积分的owner
* @param: totalSupply:发行总量,初始化时,积分全部归initiator
*/
virtual void initialize() = 0;
/*
* func: 增发积分
* @param: initiator:交易发起者,只有交易发起者等于积分owner时,才能增发
* @param: amount:增发容量
*/
virtual void addAward() = 0;
/*
* func: 获取积分总供应量
*/
virtual void totalSupply() = 0;
/*
* func: 获取caller的积分余额
* @param: caller: 合约调用者
*/
virtual void balance() = 0;
/*
* func: 查询to用户能消费from用户的积分数量
* @param: from: 被消费积分的一方
* @param: to: 消费积分的一方
*/
virtual void allowance() = 0;
/*
* func: from账户给to账户转token数量的积分
* @param: from:转移积分的一方
* @param: to:收积分的一方
* @param: token:转移积分数量
*/
virtual void transfer() = 0;
/*
* func: 从授权账户from转移数量为token的积分给to账户
* @param: from:被转积分账户
* @param: caller:合约调用者
* @param: to:收积分账户
* @param: token:转移的积分数量
*/
virtual void transferFrom() = 0;
/*
* func: 允许to账户从from账户转移token数量的积分
* @param: from:
* @param: to:
* @param: token
*/
virtual void approve() = 0;
};
struct Award : public AwardBasic, public xchain::Contract {
public:
void initialize() {
xchain::Context* ctx = this->context();
const std::string& caller = ctx->initiator();
if (caller.empty()) {
ctx->error("missing caller");
return;
}
const std::string& totalSupply = ctx->arg("totalSupply");
if (totalSupply.empty()) {
ctx->error("missing totalSupply");
return;
}
if (atoi(totalSupply.c_str()) <= 0) {
ctx->error("totalSupply is overflow");
return;
}
std::string key = BALANCEPRE + caller;
ctx->put_object("totalSupply", totalSupply);
ctx->put_object(key, totalSupply);
std::string master = MASTERPRE;
ctx->put_object(master, caller);
ctx->ok("initialize success");
}
void addAward() {
xchain::Context* ctx = this->context();
const std::string& caller = ctx->initiator();
if (caller.empty()) {
ctx->error("missing caller");
return;
}
std::string master;
if (!ctx->get_object(MASTERPRE, &master)) {
ctx->error("missing master");
return;
}
if (master != caller) {
ctx->error("only the person who created the contract can addAward");
return;
}
const std::string& increaseSupply = ctx->arg("amount");
if (increaseSupply.empty()) {
ctx->error("missing amount");
return;
}
if (atoi(increaseSupply.c_str()) <= 0) {
ctx->error("amount is overflow");
return;
}
std::string value;
if (!ctx->get_object("totalSupply", &value)) {
ctx->error("get totalSupply error");
return;
}
int increaseSupplyint = atoi(increaseSupply.c_str());
int valueint = atoi(value.c_str());
int totalSupplyint = increaseSupplyint + valueint;
if (totalSupplyint <= 0) {
ctx->error("amount+totalSupply is overflow");
return;
}
char buf[32];
snprintf(buf, 32, "%d", totalSupplyint);
ctx->put_object("totalSupply", buf);
std::string key = BALANCEPRE + caller;
if (!ctx->get_object(key, &value)) {
ctx->error("get caller balance error");
return;
}
valueint = atoi(value.c_str());
int callerint = increaseSupplyint + valueint;
snprintf(buf, 32, "%d", callerint);
ctx->put_object(key, buf);
ctx->ok(buf);
}
void totalSupply() {
xchain::Context* ctx = this->context();
std::string value;
if (ctx->get_object("totalSupply", &value)) {
ctx->ok(value);
} else {
ctx->error("key not found");
}
}
void balance() {
xchain::Context* ctx = this->context();
const std::string& caller = ctx->arg("caller");
if (caller.empty()) {
ctx->error("missing caller");
return;
}
std::string key = BALANCEPRE + caller;
std::string value;
if (ctx->get_object(key, &value)) {
ctx->ok(value);
} else {
ctx->error("key not found");
}
}
void allowance() {
xchain::Context* ctx = this->context();
const std::string& from = ctx->arg("from");
if (from.empty()) {
ctx->error("missing from");
return;
}
const std::string& to = ctx->arg("to");
if (to.empty()) {
ctx->error("missing to");
return;
}
std::string key = ALLOWANCEPRE + from + "_" + to;
std::string value;
if (ctx->get_object(key, &value)) {
ctx->ok(value);
} else {
ctx->error("key not found");
}
}
void transfer() {
xchain::Context* ctx = this->context();
const std::string& from = ctx->initiator());
if (from.empty()) {
ctx->error("missing from");
return;
}
const std::string& to = ctx->arg("to");
if (to.empty()) {
ctx->error("missing to");
return;
}
if (to == from) {
ctx->error("can't transfer to yourself");
return;
}
const std::string& token_str = ctx->arg("token");
if (token_str.empty()) {
ctx->error("missing token");
return;
}
int token = atoi(token_str.c_str());
if (token <= 0) {
ctx->error("token is overflow");
return;
}
std::string from_key = BALANCEPRE + from;
std::string value;
int from_balance = 0;
if (ctx->get_object(from_key, &value)) {
from_balance = atoi(value.c_str());
if (from_balance < token) {
ctx->error("The balance of from not enough");
return;
}
} else {
ctx->error("key not found");
return;
}
std::string to_key = BALANCEPRE + to;
int to_balance = 0;
if (ctx->get_object(to_key, &value)) {
to_balance = atoi(value.c_str());
}
from_balance = from_balance - token;
to_balance = to_balance + token;
char buf[32];
snprintf(buf, 32, "%d", from_balance);
ctx->put_object(from_key, buf);
snprintf(buf, 32, "%d", to_balance);
ctx->put_object(to_key, buf);
ctx->ok("transfer success");
}
void transferFrom() {
xchain::Context* ctx = this->context();
const std::string& from = ctx->arg("from");
if (from.empty()) {
ctx->error("missing from");
return;
}
const std::string& caller = ctx->arg("caller");
if (caller.empty()) {
ctx->error("missing caller");
return;
}
const std::string& to = ctx->arg("to");
if (to.empty()) {
ctx->error("missing to");
return;
}
if (to == from) {
ctx->error("can't transfer to yourself");
return;
}
const std::string& token_str = ctx->arg("token");
if (token_str.empty()) {
ctx->error("missing token");
return;
}
int token = atoi(token_str.c_str());
if (token <= 0) {
ctx->error("token is overflow");
return;
}
std::string allowance_key = ALLOWANCEPRE + from + "_" + caller;
std::string value;
int allowance_balance = 0;
if (ctx->get_object(allowance_key, &value)) {
allowance_balance = atoi(value.c_str());
if (allowance_balance < token) {
ctx->error("The allowance of from_to not enough");
return;
}
} else {
ctx->error("You need to add allowance from_to");
return;
}
std::string from_key = BALANCEPRE + from;
int from_balance = 0;
if (ctx->get_object(from_key, &value)) {
from_balance = atoi(value.c_str());
if (from_balance < token) {
ctx->error("The balance of from not enough");
return;
}
} else {
ctx->error("From no balance");
return;
}
std::string to_key = BALANCEPRE + to;
int to_balance = 0;
if (ctx->get_object(to_key, &value)) {
to_balance = atoi(value.c_str());
}
from_balance = from_balance - token;
to_balance = to_balance + token;
allowance_balance = allowance_balance - token;
char buf[32];
snprintf(buf, 32, "%d", from_balance);
ctx->put_object(from_key, buf);
snprintf(buf, 32, "%d", to_balance);
ctx->put_object(to_key, buf);
snprintf(buf, 32, "%d", allowance_balance);
ctx->put_object(allowance_key, buf);
ctx->ok("transferFrom success");
}
void approve() {
xchain::Context* ctx = this->context();
const std::string& from = ctx->arg("from");
if (from.empty()) {
ctx->error("missing from");
return;
}
const std::string& to = ctx->arg("to");
if (to.empty()) {
ctx->error("missing to");
return;
}
if (to == from) {
ctx->error("can't transfer to yourself");
return;
}
const std::string& token_str = ctx->arg("token");
if (token_str.empty()) {
ctx->error("missing token");
return;
}
int token = atoi(token_str.c_str());
if (token <= 0) {
ctx->error("token is overflow");
return;
}
std::string from_key = BALANCEPRE + from;
std::string value;
if (ctx->get_object(from_key, &value)) {
int from_balance = atoi(value.c_str());
if (from_balance < token) {
ctx->error("The balance of from not enough");
return;
}
} else {
ctx->error("From no balance");
return;
}
std::string allowance_key = ALLOWANCEPRE + from + "_" + to;
int allowance_balance = 0;
if (ctx->get_object(allowance_key, &value)) {
allowance_balance = atoi(value.c_str());
}
allowance_balance = allowance_balance + token;
char buf[32];
snprintf(buf, 32, "%d", allowance_balance);
ctx->put_object(allowance_key, buf);
ctx->ok("approve success");
}
};
DEFINE_METHOD(Award, initialize) {
self.initialize();
}
DEFINE_METHOD(Award, addAward) {
self.addAward();
}
DEFINE_METHOD(Award, totalSupply) {
self.totalSupply();
}
DEFINE_METHOD(Award, balance) {
self.balance();
}
DEFINE_METHOD(Award, allowance) {
self.allowance();
}
DEFINE_METHOD(Award, transfer) {
self.transfer();
}
DEFINE_METHOD(Award, transferFrom) {
self.transferFrom();
}
DEFINE_METHOD(Award, approve) {
self.approve();
}