feat: switch to a standalone script

This commit is contained in:
Sérgio Salgado 2022-06-13 16:57:19 +01:00
parent c16e0ea776
commit 6287464cea
4 changed files with 164 additions and 23 deletions

View file

@ -50,10 +50,9 @@ function isStrictAddress (cryptoCode, toAddress, settings, operatorId) {
return cryptoCode === 'ETH' && util.isValidChecksumAddress(toAddress)
}
function sendCoins (account, tx, settings, operatorId, feeMultiplier, _opts) {
function sendCoins (account, tx, settings, operatorId, feeMultiplier) {
const { toAddress, cryptoAtoms, cryptoCode } = tx
const opts = { ..._opts, includesFee: _.defaultTo(false, _opts?.includesFee) }
return generateTx(toAddress, defaultWallet(account), cryptoAtoms, cryptoCode, opts)
return generateTx(toAddress, defaultWallet(account), cryptoAtoms, false, cryptoCode)
.then(pify(web3.eth.sendRawTransaction))
.then(txid => {
return pify(web3.eth.getTransaction)(txid)
@ -96,7 +95,7 @@ function _balance (includePending, address, cryptoCode) {
.then(balance => balance ? BN(balance) : BN(0))
}
function generateTx (_toAddress, wallet, amount, cryptoCode, opts) {
function generateTx (_toAddress, wallet, amount, includesFee, cryptoCode) {
const fromAddress = '0x' + wallet.getAddress().toString('hex')
const isErc20Token = coins.utils.isErc20Token(cryptoCode)
@ -126,18 +125,18 @@ function generateTx (_toAddress, wallet, amount, cryptoCode, opts) {
.then(([gas, gasPrice, txCount]) => [
BN(gas),
BN(gasPrice),
_.max([1, txCount, lastUsedNonces[fromAddress] + 1])
_.max([0, txCount, lastUsedNonces[fromAddress] + 1])
])
.then(([gas, gasPrice, txCount]) => {
lastUsedNonces[fromAddress] = txCount
const toSend = opts.includesFee
const toSend = includesFee
? amount.minus(gasPrice.times(gas))
: amount
const rawTx = {
chainId: _.defaultTo(1, opts?.chainId),
nonce: _.defaultTo(txCount, opts?.nonce),
chainId: 1,
nonce: txCount,
gasPrice: hex(gasPrice),
gasLimit: hex(gas),
to: toAddress,
@ -173,9 +172,8 @@ function sweep (account, cryptoCode, hdIndex, settings, operatorId) {
return confirmedBalance(fromAddress, cryptoCode)
.then(r => {
if (r.eq(0)) return
const opts = { includesFee: true }
return generateTx(defaultAddress(account), wallet, r, cryptoCode, opts)
return generateTx(defaultAddress(account), wallet, r, true, cryptoCode)
.then(signedTx => pify(web3.eth.sendRawTransaction)(signedTx))
})
}
@ -239,5 +237,4 @@ function checkBlockchainStatus (cryptoCode) {
.then(() => connect(`http://localhost:${coins.utils.getCryptoCurrency(cryptoCode).defaultPort}`))
.then(() => web3.eth.syncing)
.then(res => res === false ? 'ready' : 'syncing')
.catch(() => 'disconnected')
}