Skip to content

Instantly share code, notes, and snippets.

@oak-ib
Forked from earthchie/TLMBot.js
Last active May 13, 2021 12:50
Show Gist options
  • Save oak-ib/b382c2ca9c00b875f8444bdef2a52c14 to your computer and use it in GitHub Desktop.
Save oak-ib/b382c2ca9c00b875f8444bdef2a52c14 to your computer and use it in GitHub Desktop.
class TLMBot {
constructor(minWaitTime = 5000, maxWaitTime = 15000) {
this.minWaitTime = minWaitTime;
this.maxWaitTime = maxWaitTime;
this.isMining = false;
this.isBotRunning = false;
this.startedAt = 0;
this.initialBalance = 0;
this.accumulate = 0;
this.alertCaptcha = false;
}
async start() {
try{
await this.login();
this.isBotRunning = true;
this.startedAt = new Date();
this.initialBalance = await this.getBalance();
console.log(`%c[Bot] TLM Bot Started! Your current balance is ${this.initialBalance} TLM.`, 'color:green');
while (this.isBotRunning) {
await bot.cooldown();
await bot.checkCPU();
await bot.mine();
}
}catch (err) {
unityInstance.SendMessage(
"ErrorHandler",
"Server_Response_SetErrorData",
"error"
);
this.isMining = true;
this.start();
}
}
async stop() {
this.isBotRunning = false;
const current_balance = await this.getBalance();
console.log(
`
%c[Bot] TLM Bot Stopped. \n
You run the bot for ${TLMBot.howLongFrom(this.startedAt)}. Since ${this.startedAt}
Your current balance is ${current_balance} TLM. \n
You earn ${this.accumulate} TLM in total.
`,
'color:green'
);
this.initialBalance = 0;
this.accumulate = 0;
}
getRandomTimeWait() {
return this.minWaitTime + Math.floor(Math.random() * (this.maxWaitTime - this.minWaitTime));
}
enableAlertCaptcha(enable = true) {
this.alertCaptcha = enable;
}
async login() {
this.id = await wax.login();
unityInstance.SendMessage('Controller', 'Server_Response_LoginData', this.id);
console.log(`%c[Bot] You're logged in as ` + this.id, 'color:green');
}
async cooldown() {
return new Promise((resolve) => {
getMineDelay(this.id).then((mineDelay) => {
if (mineDelay <= 0) {
console.log(`%c[Bot] Cooldown finished.`, 'color:green');
resolve();
} else {
const waitTime = mineDelay + this.getRandomTimeWait();
console.log(`%c[Bot] Cooldown for ${Math.ceil((waitTime / 1000)/60)} min`, 'color:green');
TLMBot.delay(waitTime).then((_) => {
if (this.isMining) {
return this.cooldown();
} else {
resolve();
}
});
}
});
});
}
async postData(url = '', data = {}) {
const response = await fetch(url, {
method: 'POST',
mode: 'cors',
cache: 'no-cache',
credentials: 'same-origin',
headers: {
'Content-Type': 'application/json'
},
redirect: 'follow',
referrerPolicy: 'no-referrer',
body: JSON.stringify(data)
});
return response.json();
}
async checkCPU(){
let result = true;
while(result){
try {
const accountDetail = await this.postData('https://api.waxsweden.org/v1/chain/get_account', { account_name: this.id })
if(accountDetail.cpu_limit != null){
const rawPercent = ((accountDetail.cpu_limit.used/accountDetail.cpu_limit.max)*100).toFixed(2)
console.log(`%c[Bot] rawPercent : ${rawPercent}%`, 'color:green')
console.log(`%c[Bot] cpu_limit max:${accountDetail.cpu_limit.max} use:${accountDetail.cpu_limit.used} = ${accountDetail.cpu_limit.max - accountDetail.cpu_limit.used}`, 'color:green')
if(accountDetail.cpu_limit.max > accountDetail.cpu_limit.used){
result = false;
}
}
}catch (err) {
console.log(err.message);
result = false;
}
if(result){
const delayCheckCpu = 120000 + Math.floor(Math.random() * 20001)
console.log(`%c[Bot] delay ${(delayCheckCpu/1000/60)} min check cpu again`, 'color:green')
await TLMBot.delay(delayCheckCpu)
}
}
}
async mine() {
return new Promise((success, fail) => {
background_mine(this.id).then((work) => {
unityInstance.SendMessage('Controller', 'Server_Response_Mine', JSON.stringify(work));
console.log(`%c[Bot] ${work.account} Pushing mine results...`, 'color:green');
const actions = [
{
account: mining_account,
name: 'mine',
authorization: [
{
actor: work.account,
permission: 'active',
},
],
data: {
miner: work.account,
nonce: work.rand_str,
},
},
];
console.log('%c[Bot] transacting: ', 'color:green', actions);
// Start alert captcha.
if (this.alertCaptcha) {
const audio = new Audio('https://media.geeksforgeeks.org/wp-content/uploads/20190531135120/beep.mp3');
audio.play();
}
wax.api
.transact(
{
actions,
},
{
blocksBehind: 3,
expireSeconds: 90,
}
)
.then((result) => {
console.log('%c[Bot] Mining result:', 'color:green', result);
let amounts = new Map();
if (result && result.processed) {
let accumulate = 0;
result.processed.action_traces[0].inline_traces.forEach((item) => {
if (item.act.data.quantity) {
if (amounts.has(item.act.data.to)) {
let obStr = amounts.get(item.act.data.to);
obStr = obStr.substring(0, obStr.length - 4);
let nbStr = item.act.data.quantity;
nbStr = nbStr.substring(0, nbStr.length - 4);
let balance = (parseFloat(obStr) + parseFloat(nbStr)).toFixed(4);
accumulate += balance;
amounts.set(item.act.data.to, balance.toString() + ' TLM');
} else {
accumulate += parseFloat(item.act.data.quantity.split(' ').shift());
amounts.set(item.act.data.to, item.act.data.quantity);
}
}
});
this.accumulate += accumulate;
console.log(
`%c[Bot] Mining success! ${work.account} got ${accumulate.toFixed(4)} TLM. So far you earn ${this.accumulate.toFixed(
4
)} TLM in total.`,
'color:green'
);
unityInstance.SendMessage('Controller', 'Server_Response_Claim', amounts.get(work.account));
}
this.isMining = false;
success();
})
.catch((err) => {
unityInstance.SendMessage('ErrorHandler', 'Server_Response_SetErrorData', err.message);
this.isMining = true;
fail();
});
});
});
}
async getBalance() {
return parseFloat((await getBalance(this.id, wax.api.rpc)).split(' ').shift());
}
static delay(ms) {
return new Promise((r) => {
setTimeout((_) => r(), ms);
});
}
static howLongFrom(start) {
start = start.getTime();
const now = new Date().getTime();
let diff = (now - start) / 1000;
let result = [];
if (diff > 86400) {
result.push(Math.floor(diff / 86400) + ' days');
diff %= 86400;
}
if (diff > 3600) {
result.push(Math.floor(diff / 3600) + ` hours`);
diff %= 3600;
}
if (diff > 60) {
result.push(Math.floor(diff / 60) + ` minutes`);
diff %= 60;
}
if (diff > 0) {
result.push(diff.toFixed(3) + ` seconds`);
}
return result.join(' ');
}
}
let bot = new TLMBot();
// bot.enableAlertCaptcha();
bot.start();
let i = 1;
let interval = setInterval(function () {
console.log(`%c[Bot] interval ${i}`, 'color:green');
bot.stop();
setTimeout(function(){ bot.start(); }, 2000);
i++;
}, 2400000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment