forked from txiuqw4/farmersworld-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
work.js
324 lines (279 loc) · 8.07 KB
/
work.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
import query from "./service/wax-query.js";
import historyTransaction from "./service/wax-transaction.js";
import { mine, repair, recover, withdraw } from "./farmersworld.js";
import { toCamelCase } from "./utils.js";
import { createRequire } from "module";
const require = createRequire(import.meta.url);
const accounts = require("./accounts.json");
const MAX_DELAY = 3600; // 1 hour
const {
REPAIR_IF_DURABILITY_LOWER,
RECOVER_IF_ENERGY_LOWER,
LOWEST_ENERGY,
PAYBW,
MINIMUM_FEE,
MINIMUN_WITHDRAW,
TIMEZONE,
WITHDRAWABLE,
} = require("./config.json");
function queryData(wallet) {
return query({
json: true,
code: "farmersworld",
scope: "farmersworld",
table: "tools",
lower_bound: wallet,
upper_bound: wallet,
index_position: 2,
key_type: "i64",
limit: "100",
reverse: false,
show_payer: false,
});
}
/*
* Check withdraw fee is equal target
*
* @param target number
*
* @return bool
*/
async function isWithdrawFeeEqual(target = 5) {
const data = await query({
json: true,
code: "farmersworld",
scope: "farmersworld",
table: "config",
lower_bound: null,
upper_bound: null,
index_position: 1,
key_type: "",
limit: "100",
reverse: false,
show_payer: false,
});
if (!data || data.rows.length === 0) return false;
console.log("Current withdraw fee", data.rows[0].fee);
return data.rows[0].fee === target;
}
/*
* Get reward after claim
*
* @return string
*/
async function logClaim(trxId) {
try {
const trans = await historyTransaction(trxId);
const traces = trans.traces.find((r) => r.act.name === "logclaim");
const rewards = traces.act.data.rewards;
return rewards && rewards instanceof Array ? rewards.join(" - ") : "";
} catch {
return "";
}
}
async function delay(ms = 1000) {
return new Promise((r) => setTimeout(r, ms));
}
async function countdown(seconds) {
for (let i = seconds; i > 0; i--) {
process.stdout.write("\r");
process.stdout.write(` -> ${i} seconds remaining...`);
await delay(1000);
}
process.stdout.write("\n");
}
async function getAccount(wallet) {
const data = await query({
json: true,
code: "farmersworld",
scope: "farmersworld",
table: "accounts",
lower_bound: wallet,
upper_bound: wallet,
index_position: 1,
key_type: "",
limit: "100",
reverse: false,
show_payer: false,
});
return data && data.rows.length > 0 ? data.rows[0] : null;
}
/*
* Fetch tools from contract
*
* @return Array
*/
async function fetchTools() {
let tools = [];
for (const account of accounts) {
console.log("run with wallet ", account.wallet);
const data = await queryData(account.wallet);
tools = tools.concat(
data
? data.rows.map((r) => ({ ...toCamelCase(r), ...account }))
: []
);
}
return tools;
}
function calcNextClaim(tools) {
return tools.reduce(function (min, r) {
return min >= r.nextAvailability ? r.nextAvailability : min;
}, Math.floor(Date.now() / 1000 + MAX_DELAY));
}
function getClaimableTools(tools) {
return tools.filter(
(r) => Math.ceil(r.nextAvailability - Date.now() / 1000) <= 0
);
}
function getRepairTools(tools) {
return tools.filter(
(r) => r.currentDurability <= REPAIR_IF_DURABILITY_LOWER
);
}
async function makeMine(tool, paybw) {
console.log("claim with asset_id", tool.assetId);
const result = await mine(tool, paybw);
await delay(1000);
const reward = await logClaim(result.transaction_id);
console.log("Log claim", reward);
}
function logDurability(tools) {
for (const row of tools) {
let difftime = Math.ceil(row.nextAvailability - Date.now() / 1000);
console.log(
"asset_id",
row.assetId,
"diff",
difftime,
"seconds",
"current durability",
row.currentDurability
);
}
}
/*
* Fetch account infomation
*
* @return array
*/
async function syncAccounts() {
const data = [];
for (const account of accounts) {
const r = await getAccount(account.wallet);
data.push({
...r,
...account,
});
await delay(500);
}
return data;
}
async function fetchBalanceOf(wallet, type) {
const account = await getAccount(wallet);
return parseBalance(
account.balances.find((r) => r.toUpperCase().endsWith(type))
);
}
function parseBalance(value) {
return Number(value.split(" ")[0]);
}
/*
* logic withdraw, repair, recover
*
* @param tools Array[]
* @param paybw object | null
*
* @return void
*/
async function anotherTask(tools, paybw = null) {
try {
// TASK: repair
for (const tool of tools) {
const gold = await fetchBalanceOf(tool.wallet, "GOLD");
const consumed = (tool.durability - tool.currentDurability) / 5;
console.log("Repair", tool.assetId, "gold consumed", consumed);
if (gold >= consumed) await repair(tool, paybw);
else console.log("Not enough gold to repair.");
await delay(500);
}
const canWithdraw = await isWithdrawFeeEqual(MINIMUM_FEE);
const fwAccounts = await syncAccounts();
for (const account of fwAccounts) {
if (account.energy <= RECOVER_IF_ENERGY_LOWER) {
let energy = account.max_energy - account.energy;
let consumed = energy / 5;
const food = parseBalance(
account.balances.find((r) =>
r.toUpperCase().endsWith("FOOD")
)
);
if (account.energy <= LOWEST_ENERGY && food < consumed) {
consumed = Math.floor(food);
energy = consumed * 5;
}
if (food >= consumed) {
console.log("Recover", account.wallet, energy, "energy");
await recover(account, energy, paybw);
} else {
console.log("Not enough food to recover.");
continue;
}
}
if (canWithdraw) {
const quantities = account.balances.filter((r) => {
const amount = parseBalance(r);
const symbol = r.split(" ")[1];
return amount > MINIMUN_WITHDRAW && WITHDRAWABLE.includes(symbol);
});
if (quantities.length > 0) {
console.log("Withdrawing...");
await withdraw(account, quantities, MINIMUM_FEE, paybw);
}
}
}
} catch (e) {
console.log("[ERROR] another task error -", e.message);
}
}
async function main(paybw) {
let tools = await fetchTools();
logDurability(tools);
const nextClaim = calcNextClaim(tools);
const difftime = Math.ceil(nextClaim - Date.now() / 1000);
if (difftime > 120) {
const repairTools = getRepairTools(tools);
await anotherTask(repairTools, paybw);
}
if (difftime > 0) {
console.log(
"Next claim at",
new Date(nextClaim * 1000).toLocaleString("en-US", {
timeZoneName: "short",
timeZone: TIMEZONE,
})
);
// await countdown(difftime);
await delay(difftime * 1000);
}
let claimable = getClaimableTools(tools);
for (const tool of claimable) {
await makeMine(tool, paybw);
await delay(1000);
}
}
export default async function () {
let paybw = null;
if (PAYBW) {
paybw = require("./paybw.json");
}
console.log("working...");
while (true) {
try {
await main(paybw);
} catch (e) {
// an error occus
console.log("[Error] -", e);
}
}
}