Skip to content

Instantly share code, notes, and snippets.

@stakemepro
Created December 29, 2022 16:13
Show Gist options
  • Save stakemepro/8f9bb374ac3dda1764b3268708c75720 to your computer and use it in GitHub Desktop.
Save stakemepro/8f9bb374ac3dda1764b3268708c75720 to your computer and use it in GitHub Desktop.
Auto-redelegate script
const shell = require("shelljs");
const readlineSync = require('readline-sync');
function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
(async function() {
console.log('[STAKEME] Auto redelegate. https://t.me/stakeme_channel');
console.log('* Please write command BINARY (example okp4d)');
const PROJECT = readlineSync.question();
console.log('* Please write CHAIN (example okp4-nemeton-1)');
const CHAIN_ID = readlineSync.question();
console.log('* Please write DENOM (example uknow)');
const DENOM = readlineSync.question();
console.log('* Please write your name WALLET');
const WALLETNAME = readlineSync.question();
console.log('* Please write PASSWORD from wallet');
const PASSWORD = readlineSync.question();
console.log('* Please FEES for TX (example 0)');
const FEES = readlineSync.question();
function getBalance() {
const cmdGetBalance = `${PROJECT} q bank balances ${DELEGATOR_ADDRESS} -o json | jq -r '.balances | .[].amount'`
const result = shell.exec(cmdGetBalance, {shell: '/bin/bash', silent: true});
const data = result.stdout + result.stderr;
return data.split('\n')[0];
}
function getDelegator() {
const cmdGetDelegator = `echo -e "${PASSWORD}\\ny\\n" | ${PROJECT} keys show ${WALLETNAME} --bech acc -a`;
const result = shell.exec(cmdGetDelegator, {shell: '/bin/bash', silent: true});
return (result.stdout + result.stderr).trim();
}
function getValoper() {
const cmdGetValoper = `echo -e "${PASSWORD}\\ny\\n" | ${PROJECT} keys show ${WALLETNAME} --bech val -a`;
const result = shell.exec(cmdGetValoper, {shell: '/bin/bash', silent: true});
return (result.stdout + result.stderr).trim();
}
const DELEGATOR_ADDRESS = getDelegator();
const VALOPER_ADDRESS = getValoper();
console.log('* Start');
while (true) {
const cmdGetReward = `echo -e "${PASSWORD}\\ny\\n" | ${PROJECT} tx distribution withdraw-rewards ${VALOPER_ADDRESS} --chain-id ${CHAIN_ID} --from ${WALLETNAME} --commission --fees ${FEES}${DENOM} -y`
console.log('* Get reward');
const reward = shell.exec(cmdGetReward, {shell: '/bin/bash', silent: true});
console.log(reward.stdout + reward.stderr);
console.log('Balance', getBalance());
await sleep(1000 * 20);
const cmdGetAllReward = `echo -e "${PASSWORD}\\n${PASSWORD}\\n" | ${PROJECT} tx distribution withdraw-all-rewards --from ${DELEGATOR_ADDRESS} --chain-id ${CHAIN_ID} --fees ${FEES}${DENOM} -y`;
console.log('* Get all reward');
const allReward = shell.exec(cmdGetAllReward, {shell: '/bin/bash', silent: true});
console.log(allReward.stdout + allReward.stderr);
console.log('Balance', getBalance());
await sleep(1000 * 20);
const balance = Number.parseInt(getBalance()) - 50000;
const cmdStakeAll = `echo -e "${PASSWORD}\\n${PASSWORD}\\n" | ${PROJECT} tx staking delegate ${VALOPER_ADDRESS} ${balance}${DENOM} --from ${DELEGATOR_ADDRESS} --chain-id ${CHAIN_ID} --fees ${FEES}${DENOM} -y`;
console.log('* Stake all');
const stakeAll = shell.exec(cmdStakeAll, {shell: '/bin/bash', silent: true});
console.log(stakeAll.stdout + stakeAll.stderr);
await sleep(1000 * 60);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment