-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathNRC20.ts
247 lines (205 loc) · 6.96 KB
/
NRC20.ts
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
import { LocalContractStorage, Blockchain, StorageMap, Event } from "./System";
import { BigNumber } from "./bignumber";
import OwnableContract from "./OwnableContract";
const BigNumberStorageDescriptor = {
parse(value: string | number) {
return new BigNumber(value);
},
stringify(o: BigNumber) {
return o.toString(10);
}
}
const objectMapDescriptor = {
parse(value: string) {
return JSON.parse(value)
},
stringify(o: any) {
return JSON.stringify(o)
}
}
interface AllowProfile {
[key: string]: BigNumber;
}
class Allowed {
allowed: AllowProfile;
constructor(obj?: any) {
this.allowed = {};
this.parse(obj);
}
toString() {
return JSON.stringify(this.allowed);
}
parse(obj: any) {
if (typeof obj != "undefined") {
var data = JSON.parse(obj);
for (var key in data) {
this.allowed[key] = new BigNumber(data[key]);
}
}
}
get(key: string): BigNumber {
return this.allowed[key];
}
set(key: string, value: BigNumber | number | string) {
this.allowed[key] = new BigNumber(value);
}
}
type possibleNumber = BigNumber | string | number
class NRC20Token extends OwnableContract {
_name: string;
_symbol: string;
_decimals: number;
_totalSupply: BigNumber;
balances: StorageMap<BigNumber>;
allowed: StorageMap<Allowed>;
constructor() {
super()
LocalContractStorage.defineProperties(this, {
_name: null,
_symbol: null,
_decimals: null,
_totalSupply: BigNumberStorageDescriptor
});
LocalContractStorage.defineMapProperties(this, {
"balances": BigNumberStorageDescriptor,
"allowed": {
parse(value) {
return new Allowed(value);
},
stringify(o) {
return o.toString();
}
}
});
}
// Workaround for
// Property 'init' in type 'The Child Class' is not assignable
// to the same property in base type 'The Father Class'
_initNRC20Token(name: string, symbol: string, decimals: number, totalSupply: string | number) {
super._initOwnableContract()
this._name = name;
this._symbol = symbol;
this._decimals = decimals || 0;
this._totalSupply = new BigNumber(totalSupply).mul(new BigNumber(10).pow(decimals));
var from = Blockchain.transaction.from;
this.balances!.set(from, this._totalSupply);
this.transferEvent(true, from, from, this._totalSupply);
}
// Returns the name of the token
name() {
return this._name || '';
}
// Returns the symbol of the token
symbol() {
return this._symbol;
}
// Returns the number of decimals the token uses
decimals() {
return this._decimals;
}
totalSupply() {
return this._totalSupply!.toString(10);
}
balanceOf(owner: string) {
var balance = this.balances!.get(owner);
if (balance instanceof BigNumber) {
return balance.toString(10);
} else {
return "0";
}
}
transfer(to: string, value: possibleNumber) {
value = new BigNumber(value);
if (value.lt(0)) {
throw new Error("invalid value.");
}
var from = Blockchain.transaction.from;
var balance = this.balances!.get(from) || new BigNumber(0);
if (balance.lt(value)) {
throw new Error("transfer failed.");
}
this.balances!.set(from, balance.sub(value));
var toBalance = this.balances!.get(to) || new BigNumber(0);
this.balances!.set(to, toBalance.add(value));
this.transferEvent(true, from, to, value);
}
transferFrom(from: string, to: string, value: possibleNumber) {
var spender = Blockchain.transaction.from;
var balance = this.balances!.get(from) || new BigNumber(0);
var allowed = this.allowed!.get(from) || new Allowed();
var allowedValue = allowed.get(spender) || new BigNumber(0);
value = new BigNumber(value);
if (value.gte(0) && balance.gte(value) && allowedValue.gte(value)) {
this.balances!.set(from, balance.sub(value));
// update allowed value
allowed.set(spender, allowedValue.sub(value));
this.allowed!.set(from, allowed);
var toBalance = this.balances!.get(to) || new BigNumber(0);
this.balances!.set(to, toBalance.add(value));
this.transferEvent(true, from, to, value);
} else {
throw new Error("transfer failed.");
}
}
approveEvent(status: boolean, from: string, spender: string, value: possibleNumber) {
Event.Trigger(this.name(), {
Status: status,
Action: "Approve",
Approve: {
owner: from,
spender: spender,
value: value
}
});
}
transferEvent(status: boolean, from: string, to: string, value: possibleNumber) {
Event.Trigger(this.name(), {
Status: status,
Action: "Transfer",
Transfer: {
from,
to,
value
}
});
}
approve(spender: string, currentValue: possibleNumber, value: possibleNumber) {
var from = Blockchain.transaction.from;
var oldValue = this.allowance(from, spender);
if (oldValue != currentValue.toString()) {
throw new Error("current approve value mistake.");
}
var balance = new BigNumber(this.balanceOf(from));
value = new BigNumber(value);
if (value.lt(0) || balance.lt(value)) {
throw new Error("invalid value.");
}
var owned = this.allowed!.get(from) || new Allowed();
owned.set(spender, value);
this.allowed!.set(from, owned);
this.approveEvent(true, from, spender, value);
}
allowance(owner: string, spender: string) {
var owned = this.allowed!.get(owner);
if (owned instanceof Allowed) {
let spend: BigNumber = owned.get(spender);
if (typeof spend != "undefined") {
return spend.toString(10);
}
}
return "0";
}
_issue(_to: string, _amount: possibleNumber) {
var amount = new BigNumber(_amount)
var balance = this.balances!.get(_to) || new BigNumber(0)
this._totalSupply = new BigNumber(this._totalSupply!).add(amount)
this.balances!.set(_to, balance.add(amount))
}
_destroy(_from: string, _amount: possibleNumber) {
var amount = new BigNumber(_amount)
var balance = this.balances!.get(_from) || new BigNumber(0)
this._totalSupply = new BigNumber(this._totalSupply!).sub(amount)
this.balances!.set(_from, balance.sub(amount))
}
}
export default NRC20Token;