forked from 0xCatBox/TRON-Grand-Hackathon-Season-3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
446 lines (357 loc) · 12.5 KB
/
app.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
const TronWeb = require('tronweb')
const HttpProvider = TronWeb.providers.HttpProvider;
const fullNode = new HttpProvider("https://nile.trongrid.io");
const solidityNode = new HttpProvider("https://nile.trongrid.io");
const eventServer = new HttpProvider("https://nile.trongrid.io");
////////////////////////////////////////////////////////////////// 세팅을 위한 함수들
async function j_setStakeContract() {
try {
let contract = await tronWeb_owner.contract().at(tokenContract);
let result = await contract.setStakeContract(
stakeContract
).send({
feeLimit: 10000000000
}).then(output => {
console.log('Output:', output);
});
} catch(error) {
console.error("error1", error);
}
}
async function owner_setApprovalForAll() {
try {
let contract = await tronWeb_owner.contract().at(tokenContract);
let result = await contract.setApprovalForAll(
stakeContract, true
).send({
feeLimit: 10000000000
}).then(output => {
console.log('Output:', output);
});
} catch(error) {
console.error("error2", error);
}
}
async function j_chargeMaintoken() {
try {
let contract = await tronWeb_owner.contract().at(stakeContract);
let result = await contract.chargeMaintoken(
100000
).send({
feeLimit: 10000000000
}).then(output => {
console.log('Output:', output);
});
} catch(error) {
console.error("error3", error);
}
}
async function j_transferToStealer() {
try {
let contract = await tronWeb_owner.contract().at(tokenContract);
let result = await contract.safeTransferFrom(
ownerAddress, stealerAddress, 0, 100000, "0x00"
).send({
feeLimit: 10000000000
}).then(output => {
console.log('Output:', output);
});
} catch(error) {
console.error("error4", error);
}
}
//j_setStakeContract(); // 세팅을 위해 필요한 함수, 이미 세팅함
//owner_setApprovalForAll(); // 세팅을 위해 필요한 함수, 이미 세팅함
//j_chargeMaintoken(); // 세팅을 위해 필요한 함수, 이미 세팅함
//j_transferToStealer(); // 세팅을 위해 필요한 함수, 이미 세팅함
///////////////////////////////////////////////////////////////// 여기까지 세팅의 영역
async function j_mintMushInit() {
try {
let contract = await tronWeb_owner.contract().at(stakeContract);
let tokenRate = 15; // 튜토리얼 버섯은 B등급으로 고정
let result = await contract.mintMushInit(
userAddress,
"ran0", //uri
tokenRate
).send({
feeLimit: 10000000000
}).then(output => {
console.log('Output:', output);
});
} catch(error) {
console.error("mintMush error", error);
}
}
async function user_setApprovalForAll() {
try {
let contract = await tronWeb_user.contract().at(tokenContract);
let result = await contract.setApprovalForAll(
stakeContract, true
).send({
feeLimit: 10000000000
}).then(output => {
console.log('Output:', output);
});
} catch(error) {
console.error("error2", error);
}
}
async function j_viewMyNFTs(_tokenId) { // 주석으로 바꿔야 함
try {
let tokencontract = await tronWeb_user.contract().at(tokenContract);
let contract = await tronWeb_user.contract().at(stakeContract)
let result = new Array();
// let counter = await contract.perTotalNFTs().call();
// let NFTs = new Array(counter);
// for (i = 0; i < counter; i++) {
// NFTs[i] = await contract.TotalNFTbalance(userAddress, i).call();
// }
// for (i = 0; i < counter; i++){
// let data = new Object();
// data.tokenId = NFTs[i];
// data.uri = await tokencontract.uri(NFTs[i]).call();
// let _type = await tokencontract.NFTinfo(NFTs[i]).call();
// data.type = _type.staking;
// result.push(data);
// }
// let jsonData = JSON.stringify(result);
// return jsonData;
let _balance = await tokencontract.balanceOf(userAddress, _tokenId).call();
let balance = parseInt(_balance, 16);
let _uri = await tokencontract.uri(_tokenId).call();
let _type = await tokencontract.NFTinfo(_tokenId).call();
let data = new Object();
data.tokenId = _tokenId;
data.uri = _uri;
data.type = _type.staking;
data.balance = balance;
result.push(data);
let jsonData = JSON.stringify(result);
console.log('balanceOf\n', jsonData);
return jsonData;
} catch(error) {
console.error("balanceOf error", error);
}
}
async function j_viewMyMainToken(Id) {
let tokencontract;
let Address;
if (Id == 1) {
tokencontract = await tronWeb_user.contract().at(tokenContract);
Address = userAddress;
} else if(Id == 2) {
tokencontract = await tronWeb_stealer.contract().at(tokenContract);
Address = stealerAddress;
}
try {
let _balance = await tokencontract.balanceOf(Address, 0).call();
let balance = Number(_balance.toString())
console.log('balanceOf(Main token) : ', balance);
return balance;
} catch(error) {
console.error("balanceOf error", error);
}
}
async function j_mintStealerNFT() {
try {
let contract = await tronWeb_owner.contract().at(stakeContract);
let tokenRate = 15; // 튜토리얼 버섯은 A등급으로 고정
let result = await contract.mintStealerNFT(
stealerAddress,
"ran100" // uri 수정 필요
).send({
feeLimit: 10000000000
}).then(output => {
console.log('Output:', output);
});
} catch(error) {
console.error("mintStealer error", error);
}
}
async function stealer_setApprovalForAll() {
try {
let contract = await tronWeb_stealer.contract().at(tokenContract);
let result = await contract.setApprovalForAll(
stakeContract, true
).send({
feeLimit: 10000000000
}).then(output => {
console.log('Output:', output);
});
} catch(error) {
console.error("error2", error);
}
}
async function CalProbability(sporeRate, randNum) {
if (sporeRate == 50) {
if (randNum <= 400)
return 50;
else if (randNum <= 1600 && randNum > 400)
return 30;
else if (randNum <= 4000 && randNum > 1600)
return 15;
else if (randNum <= 7200 && randNum > 4000)
return 10;
else
return 5;
} else if (sporeRate == 30) {
if (randNum <= 200)
return 50;
else if (randNum <= 800 && randNum > 200)
return 30;
else if (randNum <= 2800 && randNum > 800)
return 15;
else if (randNum <= 5600 && randNum > 2800)
return 10;
else
return 5;
} else if (sporeRate == 15) {
if (randNum <= 100)
return 50;
else if (randNum <= 400 && randNum > 100)
return 30;
else if (randNum <= 1600 && randNum > 400)
return 15;
else if (randNum <= 4100 && randNum > 1600)
return 10;
else
return 5;
} else if (sporeRate == 10) {
if (randNum <= 50)
return 50;
else if (randNum <= 200 && randNum > 50)
return 30;
else if (randNum <= 1000 && randNum > 200)
return 15;
else if (randNum <= 3000 && randNum > 1000)
return 10;
else
return 5;
} else if (sporeRate == 5) {
if (randNum <= 1)
return 50;
else if (randNum <= 100 && randNum > 1)
return 30;
else if (randNum <= 500 && randNum > 100)
return 15;
else if (randNum <= 1500 && randNum > 500)
return 10;
else
return 5;
}
}
async function j_MushToSpore(_tokenId) {
try {
let contract = await tronWeb_user.contract().at(stakeContract);
let tokencontract = await tronWeb_user.contract().at(tokenContract);
let randNum = Math.floor(Math.random() * 10000);
let MushInfo = await tokencontract.NFTinfo(_tokenId).call();
let Rate = MushInfo.tokenRate;
let SporeRate = await CalProbability(Rate, randNum);
let result = await contract.MushToSpore(
_tokenId, SporeRate, "ran100" // uri 수정 필요
).send({
feeLimit: 10000000000
}).then(output => {
console.log('Output:', output);
});
} catch(error) {
console.error("MushToSpore error", error);
}
}
async function j_SporeToMush(_tokenId) {
try {
let contract = await tronWeb_user.contract().at(stakeContract);
let tokencontract = await tronWeb_user.contract().at(tokenContract);
let randNum = Math.floor(Math.random() * 10000);
let SporeInfo = await tokencontract.NFTinfo(_tokenId).call();
let Rate = SporeInfo.tokenRate;
let MushRate = await CalProbability(Rate, randNum);
let result = await contract.SporeToMush(
_tokenId, MushRate, "ran101" // uri 수정 필요
).send({
feeLimit: 10000000000
}).then(output => {
console.log('Output:', output);
});
} catch(error) {
console.error("MushToSpore error", error);
}
}
async function j_stake(_tokenId) {
try {
let contract = await tronWeb_owner.contract().at(stakeContract);
let result = await contract.stake(
_tokenId, userAddress
).send({
feeLimit: 10000000000
}).then(output => {
console.log('Output:', output);
});
} catch(error) {
console.error("stake error", error);
}
}
async function j_viewStakeToken() {
try {
let contract = await tronWeb_user.contract().at(stakeContract);
let stakeInfo = await contract.stakes(userAddress).call();
let data = new Object();
data.tokenId = Number(stakeInfo[0].toString());
data.rate = Number(stakeInfo[1].toString());
data.timestamp = Number(stakeInfo[2].toString());
let jsonData = JSON.stringify(data);
console.log('Stake Info \n', jsonData);
return data;
} catch(error) {
console.error("viewStakeToken", error);
}
}
async function j_unstake() {
try {
let contract = await tronWeb_owner.contract().at(stakeContract);
let result = await contract.unstake(
userAddress
).send({
feeLimit: 10000000000
}).then(output => {
console.log('Output:', output);
});
} catch(error) {
console.error("stake error", error);
}
}
async function j_steal(_stealTokenId, _amount) {
try {
let contract = await tronWeb_stealer.contract().at(stakeContract);
let result = await contract.steal(
userAddress, stealerAddress, _amount, _stealTokenId
).send({
feeLimit: 10000000000
}).then(output => {
console.log('Output:', output);
});
} catch(error) {
console.error("stake error", error);
}
}
//////////////////////////////////////////////////////////////////// demo start
//j_mintMushInit(); // 1번째
//user_setApprovalForAll(); // 2번째
//j_viewMyNFTs(3); // 3번째, 인자 3은 하드코딩, jsonData 리턴, 수정 필요
//j_viewMyMainToken(1); // 유저의 MainToken 양 확인, 숫자 리턴
//j_mintStealerNFT(); // Steal 1번쨰, stealer에게 stealerNFT minting
//stealer_setApprovalForAll(); // Steal 2번째
//j_MushToSpore(3); // 3은 하드코딩,
//j_viewMyNFTs(3); // 바뀐거 확인
//j_SporeToMush(3); // 3은 하드코딩,
//j_viewMyNFTs(3); // 바뀐거 확인
//j_stake(3);
//j_viewStakeToken(); // Json 데이터로 리턴, tokenId, rate, timestamp 정보가 있다.
//j_unstake(); //
//j_viewMyMainToken(1) // unstake 후에 리워드 확인
//j_stake(3);
//j_viewStakeToken(); // Json 데이터로 리턴, tokenId, rate, timestamp 정보가 있다.
//j_viewMyMainToken(2); // steal 전에 FT 양 확인
//j_steal(4, 10); // 아직 테스트 x
//j_viewMyMainToken(2); // steal 후에 FT 양 확인