I have main address: const fromAddress
There is also an array of addresses and their keys:foreth/keys
I need to send all erc20 tokens from: foreth
array to the: fromAddress
Tokens are sent only from the first address of the foreth
array, a hash is generated for all subsequent addresses, which is not on etherscan with error: Sorry, We are unable to locate this TxnHash
It is necessary to make sure that tokens are sent from all addresses that are in the array foreth
let tokenAddress = "erc20_contract_addr";
let fromAddress = 'main_addr'
let privateKey = Buffer.from('key_main_addr', 'hex')
const contractInstance = new web3.eth.Contract(minABI, tokenAddress)
let foreth = ["addr1", "addr2", "addr3", "addr4"];
let keys = ['addr1_key', 'addr2_key', 'addr3_key', 'addr4_key']
function nonce_addr(address) {
return web3.eth.getTransactionCount(address)
}
foreth.forEach(async function(recipient, i) {
const balance = await contractInstance.methods.balanceOf(recipient).call(function(err, result) {
let contract = new web3.eth.Contract(minABI, tokenAddress, {
from: recipient
})
if (result < 0) return
web3.eth.getTransactionCount(fromAddress)
.then((count) => {
let rawTransaction = {
'from': fromAddress,
'gasPrice': web3.utils.toHex(20 * 1e9),
'gasLimit': web3.utils.toHex(21000),
'to': recipient,
'value': web3.utils.toHex(web3.utils.toWei("0.0008")),
'nonce': web3.utils.toHex(count + i)
}
let transaction = new Tx(rawTransaction)
transaction.sign(privateKey)
return new Promise((resolve, reject) => {
web3.eth.sendSignedTransaction('0x' + transaction.serialize().toString('hex'))
.on('transactionHash', console.log)
.on('receipt', resolve)
.on('error', reject)
})
}).then(receipt => {
console.log("eth was send")
keys.forEach(async function(keys) {
let nonce = await web3.eth.getTransactionCount(recipient, 'pending');
let rawTransaction = {
'from': recipient,
'gasPrice': web3.utils.toHex(20 * 1e9),
'gasLimit': web3.utils.toHex(100000),
'to': tokenAddress,
'value': 0x0,
'data': contract.methods.transfer(fromAddress, web3.utils.toHex(result)).encodeABI(),
'nonce': web3.utils.toHex(nonce)
}
nonce++
let transaction = new Tx(rawTransaction)
transaction.sign(Buffer.from(keys, 'hex'))
let serializedTx = '0x' + transaction.serialize().toString('hex')
return new Promise(resolve => {
web3.eth.sendSignedTransaction(serializedTx)
.on('transactionHash', console.log)
})
})
})
})
})
rawTransaction
s - this might help get to the bottom of it?