-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontract.js
452 lines (395 loc) · 12.5 KB
/
contract.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
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/**
* LinkIdol Contract, copyright is owned by Andoromeda Foundation
* @author: Frank Wei <[email protected]>
* Last updated: 12:00 AM, May 30th
* Test Net Contract Address: n1oecF9SK8wUKxAcTVCYfvsvG3P6TmHWdzW
* @version: 0.9 beta - need to find the potential bug
*/
"use strict"
class Operator {
constructor(obj) {
this.operator = {}
this.parse(obj)
}
toString() {
return JSON.stringify(this.operator)
}
parse(obj) {
if (typeof obj != "undefined") {
var data = JSON.parse(obj)
for (var key in data) {
this.operator[key] = data[key]
}
}
}
get(key) {
return this.operator[key]
}
set(key, value) {
this.operator[key] = value
}
}
class StandardNRC721Token {
constructor() {
// Contract Need to store on-chain data in LocalContractStorage
LocalContractStorage.defineProperties(this, { _name: null, })
LocalContractStorage.defineMapProperties(this, {
"tokenOwner": null,
"tokenPrice": null,
"tokenClaimed": null,
"ownedTokensCount": null,
"tokenApprovals": null,
"tokenToChara": null,
"operatorApprovals": {
parse(value) {
return new Operator(value)
},
stringify(o) {
return o.toString()
}
},
})
}
init(name) {
this._name = name
}
name() {
return this._name
}
balanceOf(_owner) {
var balance = this.ownedTokensCount.get(_owner)
return balance
}
ownerOf(_tokenId) {
return this.tokenOwner.get(_tokenId)
}
priceOf(_tokenId) {
return this.tokenPrice.get(_tokenId)
}
approve(_to, _tokenId) {
var from = Blockchain.transaction.from
var owner = this.ownerOf(_tokenId)
if (_to == owner) {
throw new Error("invalid address in approve.")
}
// msg.sender == owner || isApprovedForAll(owner, msg.sender)
if (owner == from || this.isApprovedForAll(owner, from)) {
this.tokenApprovals.set(_tokenId, _to)
this.approveEvent(true, owner, _to, _tokenId)
} else {
throw new Error("permission denied in approve.")
}
}
getApproved(_tokenId) {
return this.tokenApprovals.get(_tokenId)
}
setApprovalForAll(_to, _approved) {
var from = Blockchain.transaction.from
if (from == _to) {
throw new Error("invalid address in setApprovalForAll.")
}
var operator = this.operatorApprovals.get(from) || new Operator()
operator.set(_to, _approved)
this.operatorApprovals.set(from, operator)
}
isApprovedForAll(_owner, _operator) {
var operator = this.operatorApprovals.get(_owner)
if (operator != null) {
if (operator.get(_operator) === "true") {
return true
} else {
return false
}
}
}
isApprovedOrOwner(_spender, _tokenId) {
var owner = this.ownerOf(_tokenId)
return _spender == owner || this.getApproved(_tokenId) == _spender || this.isApprovedForAll(owner, _spender)
}
rejectIfNotApprovedOrOwner(_tokenId) {
var from = Blockchain.transaction.from
if (!this.isApprovedOrOwner(from, _tokenId)) {
throw new Error("permission denied in transferFrom.")
}
}
transferFrom(_from, _to, _tokenId) {
var from = Blockchain.transaction.from
if (this.isApprovedOrOwner(from, _tokenId)) {
this.clearApproval(_from, _tokenId)
this.removeTokenFrom(_from, _tokenId)
this._addTokenTo(_to, _tokenId)
this.transferEvent(true, _from, _to, _tokenId)
} else {
throw new Error("permission denied in transferFrom.")
}
}
clearApproval(_owner, _tokenId) {
var owner = this.ownerOf(_tokenId)
if (_owner != owner) {
throw new Error("permission denied in clearApproval.")
}
this.tokenApprovals.del(_tokenId)
}
removeTokenFrom(_from, _tokenId) {
if (_from != this.ownerOf(_tokenId)) {
throw new Error("permission denied in removeTokenFrom.")
}
var tokenCount = this.ownedTokensCount.get(_from)
if (tokenCount < 1) {
throw new Error("Insufficient account balance in removeTokenFrom.")
}
this.tokenOwner.delete(_tokenId)
this.tokenPrice.delete(_tokenId)
this.ownedTokensCount.set(_from, tokenCount - 1)
}
// These function can be directly called without underscore in the first letter
_addTokenTo(_to, _tokenId) {
this.tokenOwner.set(_tokenId, _to)
this.tokenPrice.set(_tokenId, 100 * this._nasToWei())
var tokenCount = this.ownedTokensCount.get(_to) || 0
this.ownedTokensCount.set(_to, tokenCount + 1)
}
_mint(_to, _tokenId) {
this._addTokenTo(_to, _tokenId)
this.transferEvent(true, "", _to, _tokenId)
}
_burn(_owner, _tokenId) {
this.clearApproval(_owner, _tokenId)
this.removeTokenFrom(_owner, _tokenId)
this.transferEvent(true, _owner, "", _tokenId)
}
transferEvent(status, _from, _to, _tokenId) {
Event.Trigger(this.name(), {
Status: status,
Transfer: {
from: _from,
to: _to,
tokenId: _tokenId
}
})
}
approveEvent(status, _owner, _spender, _tokenId) {
Event.Trigger(this.name(), {
Status: status,
Approve: {
owner: _owner,
spender: _spender,
tokenId: _tokenId
}
})
}
}
class LinkIdolToken extends StandardNRC721Token {
constructor() {
super()
LocalContractStorage.defineProperties(this, {
_length: null,
girlsList: null,
// Changed `totalQty` before deploy!!!
totalQty: null
})
LocalContractStorage.defineMapProperties(this, { "admins": null })
}
init(name = "linkidol", symbol = "idl", initialGirlsList = []) {
super.init(name, symbol)
this._length = 0
this.totalQty = 100
this.girlsList = initialGirlsList
}
_issue(_to, _girlId) {
var tokenId = this._length
if (this.isSoldOut()) {
throw new Error("Sorry, the card pool is empty now.")
} else {
this._mint(_to, tokenId)
this.tokenToChara.set(tokenId, _girlId)
this.totalQty -= 1;
this._length += 1;
return tokenId
}
}
isSoldOut() {
return this.totalQty <= 0
}
getCardsLeft() {
return this.totalQty;
}
getCardIdByTokenId(_tokenId) {
return this.tokenToChara.get(_tokenId)
}
getCardNameByTokenId(_tokenId) {
const cardId = this.getCardIdByTokenId(_tokenId)
return this.girlsList[cardId]
}
getGirlsList() {
return this.girlsList
}
getTokenIDsByAddress(_address) {
var result = []
for (let id = 0; id < this._length; id += 1) {
if (this.ownerOf(id) === _address) {
result.push(id)
}
}
return result
}
getTotalSupply() {
return this._length
}
}
class LinkIdolContract extends LinkIdolToken {
constructor() {
super()
LocalContractStorage.defineProperties(this, {
cardPrice: null,
owner: null,
referCut: null
})
}
init(name, symbol, initialPrice, initialGirlsList) {
const { from } = Blockchain.transaction
super.init(name, symbol, initialGirlsList)
this.admins.set(from, "true")
this.cardPrice = initialPrice
this.owner = from
this.referCutPercentage = 5
}
onlyAdmins() {
const { from } = Blockchain.transaction
if (!this.admins.get(from)) {
throw new Error("Sorry, You don't have the permission as admins.")
}
}
setAdmins(address) {
this.onlyContractOwner()
this.admins.set(address, "true")
}
onlyContractOwner() {
const { from } = Blockchain.transaction
if (this.owner !== from) {
throw new Error("Sorry, But you don't have the permission as owner.")
}
}
onlyTokenOwner(_tokenId) {
const { from } = Blockchain.transaction
if (this.ownerOf(_tokenId) !== from) {
throw new Error("Sorry, But you don't have the permission as the owner of the token.")
}
}
setTokenPrice(_tokenId, _value) {
this.onlyTokenOwner(_tokenId)
this.tokenPrice = parseInt(_value) * this._nasToWei()
}
countHerosBy(tokens) {
const { getCardIdByTokenId } = this
var tag = []
var count = 0
for (const i in tokens) {
if (tag[getCardIdByTokenId(i)] == false) {
count += 1
tag[getCardIdByTokenId(i)] = true
}
}
return {
count,
tag
}
}
claim() {
const { from } = Blockchain.transaction
const { getCardIdByTokenId, countHerosBy, getTokenIDsByAddress, tokenClaimed } = this
var tokens = getTokenIDsByAddress(from)
const { count, tag } = countHerosBy(tokens)
if (count !== 108) {
throw new Error("Sorry, you don't have enough token.")
}
for (const i in tokens) {
if (tag[getCardIdByTokenId(i)] == 1) {
tokenClaimed[i] = true
}
}
this.cardPrice -= 0.0108 * this._nasToWei()
}
buyToken(_tokenId) {
var value = new BigNumber(Blockchain.transaction.value);
if (value < this.priceOf(_tokenId)) {
throw new Error("Sorry, insufficient bid.")
}
const { from } = Blockchain.transaction
this.tokenOwner.set(_tokenId, from)
this.tokenPrice.set(_tokenId, 100 * this._nasToWei())
}
_nasToWei() {
return 1000000000000000000
}
getPrice() {
return this.cardPrice
}
// For keeping price to fiat
changePrice(value) {
this.onlyAdmins()
this.cardPrice = value
}
changeReferPercentage(value) {
this.onlyAdmins()
if (value > 100) {
throw new Error("Refer Percentage above 100 is ridiculous, we are not selling for lost")
} else {
this.referCutPercentage = value
}
}
withdraw(value) {
this.onlyAdmins()
// Only the owner can have the withdraw fund
return Blockchain.transfer(this.owner, new BigNumber(value))
}
getReferPercentage() {
return this.referCutPercentage
}
luckyDraw(referer) {
var randomGirlId = parseInt(Math.random() * (this.girlsList.length + 1))
var { from, value } = Blockchain.transaction
if (value.eq(this.cardPrice)) {
var tokenId = this._issue(from, randomGirlId)
if (referer !== "") {
Blockchain.transfer(referer, new BigNumber(value).dividedToIntegerBy(100 / this.referCutPercentage))
}
this.cardPrice += 0.0001 * this._nasToWei()
return tokenId
} else {
throw new Error("Price is not matching, please check your transaction details.")
}
}
_issueMultipleCard(from, qty) {
const resultArray = []
for (let i = 0; i < qty; i += 1) {
var randomGirlId = parseInt(Math.random() * this.girlsList.length)
var tokenId = this._issue(from, randomGirlId)
resultArray.push(tokenId)
this.cardPrice += 0.0001 * this._nasToWei()
}
return resultArray
}
multiDraw(referer) {
var {
from,
value
} = Blockchain.transaction
const {
cardPrice,
referCutPercentage
} = this
const qty = value.dividedToIntegerBy(cardPrice)
if (value.gt(0)) {
const result = this._issueMultipleCard(from, qty)
if (referer !== "") {
const referCut = value.dividedToIntegerBy(100 / referCutPercentage)
Blockchain.transfer(referer, referCut)
}
return result
} else {
throw new Error("You don't have enough token, try again with more.")
}
}
}
module.exports = LinkIdolContract