Created
January 26, 2021 03:20
-
-
Save thanhson1085/9e0da9862a518395af0c085f2605af1f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const web3Rpc = require('../models/web3rpc') | |
const config = require('config') | |
const logger = require('../helpers/logger') | |
const BigNumber = require('bignumber.js') | |
let sleep = (time) => new Promise((resolve) => setTimeout(resolve, time)) | |
var nonce = 0 | |
async function run (pkey, to) { | |
const account = web3Rpc.eth.accounts.privateKeyToAccount('0x' + pkey) | |
let coinbase = account.address | |
web3Rpc.eth.accounts.wallet.add(account) | |
web3Rpc.eth.defaultAccount = coinbase | |
logger.info('Start process at %s', new Date()) | |
try { | |
while(true) { | |
const validatorAbi = require('./TomoValidatorAbi.json') | |
const address = '0x0000000000000000000000000000000000000088' | |
const validator = new web3Rpc.eth.Contract(validatorAbi, | |
address, {gasPrice: 250000000, gas: 2000000 }) | |
await web3Rpc.eth.getBlockNumber().then(blockNumber => { | |
return validator.methods.getWithdrawBlockNumbers().call({ | |
from: coinbase | |
}).then(async result => { | |
let map = result.map(async (it, idx) => { | |
it = it.toString(10) | |
console.log(it, idx) | |
if (parseInt(it) < blockNumber) { | |
let cap = await validator.methods.getWithdrawCap(it).call({from:coinbase}) | |
console.log(cap) | |
if (cap.toString(10) == "0") { | |
return true | |
} | |
validator.methods.withdraw(it, idx).send({ | |
from : coinbase, | |
gas: 2000000, | |
gasPrice: 250000000, | |
chainId:88 | |
}, (error, transactionHash) => { | |
console.log('withdraw', error, transactionHash) | |
}) | |
console.log('sleep witdraw') | |
return sleep(10000) | |
} | |
return false | |
}) | |
return Promise.all(map) | |
}).then((result) => { | |
console.log(result) | |
}) | |
}).catch(e => console.log(e)) | |
nonce = await web3Rpc.eth.getTransactionCount(coinbase) | |
await sendAll(coinbase, to) | |
logger.info('sleep') | |
await sleep(10000) | |
} | |
} catch (e) { | |
logger.error('Cannot start by error %s', String(e)) | |
process.exit(1) | |
} | |
} | |
const send = function (obj) { | |
return new Promise((resolve, reject) => { | |
web3Rpc.eth.sendTransaction({ | |
nonce: obj.nonce, | |
from: obj.from, | |
to: obj.to, | |
value: obj.value, | |
gasLimit: obj.gasLimit, | |
gasPrice: obj.gasPrice, | |
chainId: config.get('blockchain.chainId') | |
}, function (err, hash) { | |
if (err) { | |
return resolve() | |
} else { | |
logger.info('Done %s %s %s %s %s', obj.to, obj.value, hash, 'nonce', obj.nonce) | |
return resolve() | |
} | |
}).catch(e => { logger.error(e) }) | |
}) | |
} | |
async function sendAll (coinbase, addr) { | |
let balance = await web3Rpc.eth.getBalance(coinbase) | |
let value = new BigNumber(balance).minus(21000 * 250000000).minus(1e+17) | |
let item = { | |
nonce: parseInt(nonce), | |
from: coinbase, | |
to: addr, | |
value: value.toString(10), | |
gasLimit: 21000, | |
gasPrice: 250000000 | |
} | |
await send(item) | |
} | |
module.exports = { run } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment