-
Notifications
You must be signed in to change notification settings - Fork 2
/
contract_example.js
120 lines (115 loc) · 3.53 KB
/
contract_example.js
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
'use strict';
BigNumber.config({ ERRORS: false });
var Contract = function () {
LocalContractStorage.defineProperty(this, "creator", null);
LocalContractStorage.defineProperty(this, "users", null);
LocalContractStorage.defineMapProperty(this, "paymentMap");
};
Contract.prototype = {
init: function () {
this.creator = Blockchain.transaction.from;
this.users = {};
},
//支付
payment: function (itemId, limit) {
let txhash = Blockchain.transaction.hash;
let value = Blockchain.transaction.value;
let from = Blockchain.transaction.from;
if (limit != value) {
throw new Error("Invalid parameters");
}
var payments = this.paymentMap.get(from);
if (payments == null)
payments = {};
payments[txhash] = { "itemId": itemId, "value": value };
this.paymentMap.set(from, payments);
},
//查询用户的付费记录
queryPayment: function () {
let from = Blockchain.transaction.from;
var payments = this.paymentMap.get(from);
return payments;
},
//记录分数
record: function (nickname, score) {
this._positiveIntCheck(score);
this._nickNameCheck(nickname);
let from = Blockchain.transaction.from;
var users = this.users;
users[from] = [from, nickname, score];
this.users = users;
return users[from];
},
//正整数的检验
_positiveIntCheck(num) {
let regex = /^[1-9]\d*$/;
if (!regex.test(num))
throw new Error("Invalid Integer: " + num);
},
//昵称检验,不含特殊字符的数字、字母、汉字 2-8位
_nickNameCheck(name) {
let regex = /^[\u4E00-\u9FA5A-Za-z0-9]{1,8}$/
if (!regex.test(name))
throw new Error("Invalid Nick Name: " + name);
},
_auth() {
if (this.creator != from)
throw new Error("Insufficient permissions");
},
//清空排行榜
clear: function () {
this._auth();
this.users = {};
},
//查询排行榜所有记录
queryAll: function () {
let users = this.users;
let userArr = Object.keys(users).map(function (key) {
return users[key];
});
userArr.sort((a, b) => {
return b[2] - a[2];
});
return userArr;
},
//获取当前用户的记录
queryOwn: function () {
var from = Blockchain.transaction.from;
if (from in this.users) {
return this.users[from];
} else {
throw new Error("Can't find user: " + from);
}
},
takeout: function (amount) {
this._mustCreator();
var from = Blockchain.transaction.from;
let value = new BigNumber(amount);
var result = this._transfer(from, value);
if (!result) {
throw new Error("transfer failed.");
}
},
_mustCreator: function () {
if (!this._isCreator()) {
throw new Error("Insufficient permissions.");
}
},
_isCreator: function () {
return Blockchain.transaction.from == this.creator;
}
,
_transfer: function (address, value) {
var result = Blockchain.transfer(address, value);
console.log('_transfer', address, value, result);
Event.Trigger("transfer", {
Transfer: {
from: Blockchain.transaction.to,
to: address,
value: value
}
});
return result;
},
};
module.exports = Contract;