-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTritokenAutomator.js
More file actions
98 lines (77 loc) · 3.13 KB
/
Copy pathTritokenAutomator.js
File metadata and controls
98 lines (77 loc) · 3.13 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
const ethers = require('ethers');
module.exports = class TritokenAutomator {
constructor(address, wallet, name, project) {
this.address = address;
this.wallet = wallet;
this.name = name;
this.project = project;
}
minEggStake = 0
minFeedStake = 0
stakeEggMethodName = "staking"
eggTokenName() {
return this.project.claimEggMethodSuffix;
}
feedTokenName() {
return this.project.claimFeedMethodSuffix;
}
async getTokenIDs() {
const ids = [];
try {
const contract = new ethers.Contract(this.project.chikn.ADDRESS, this.project.chikn.ABI, this.wallet);
const tokensCount = await contract.balanceOf(this.address);
for (let i=0; i<tokensCount.toNumber(); i++) {
const token = await contract.tokenOfOwnerByIndex(this.address, i);
ids.push(token.toNumber());
}
} catch (e) {
console.log(e);
}
return ids;
}
async claim(tokenIds) {
if (!tokenIds || tokenIds.length==0) return;
try {
const contract = new ethers.Contract(this.project.egg.ADDRESS, this.project.egg.ABI, this.wallet);
const methodName = 'claim' + this.project.claimEggMethodSuffix;
const claimTx = await contract[methodName](tokenIds);
const receipt = await claimTx.wait();
return receipt;
} catch (e) {
console.log(`Failed claiming ${this.project.claimEggMethodSuffix} ${this.name} ${tokenIds.join(",")} ${e}`);
return false;
}
}
async claimFeed() {
try {
const contract = new ethers.Contract(this.project.feed.ADDRESS, this.project.feed.ABI, this.wallet);
const feedBalance = await contract.balanceOf(this.address);
const methodName = 'claim' + this.project.claimFeedMethodSuffix;
const claimTx = await contract[methodName]();
const receipt = await claimTx.wait();
return receipt;
} catch (e) {
console.log(`Failed claiming ${this.project.claimFeedMethodSuffix} ${this.name} ${tokenIds.join(",")} ${e}`);
return false;
}
}
async stake() {
try {
const eggContract = new ethers.Contract(this.project.egg.ADDRESS, this.project.egg.ABI, this.wallet);
const feedContract = new ethers.Contract(this.project.feed.ADDRESS, this.project.feed.ABI, this.wallet);
const eggBalance = await eggContract.balanceOf(this.address);
if (eggBalance>this.minEggStake) {
console.log(`Staking ${ethers.utils.formatUnits(eggBalance)} tokens`);
const stakeTx = await feedContract[this.stakeEggMethodName](eggBalance);
const receipt = await stakeTx.wait();
return {receipt};
} else {
console.log(`No tokens available for ${this.name}`);
return false;
}
} catch (e) {
console.log(`Failed staking tokens ${this.name} ${e}`);
return false;
}
}
}