1

I am trying to transfer some funds using a NodeJS application. The code I am using is:

async function transfer(to, amountToSend) {
    return new Promise(async (resolve, reject) => {
        var nonce = await web3.eth.getTransactionCount(_serverEthAccount.address);

        web3.eth.getBalance(_serverEthAccount.address, async (err, result) => {
            if (err) {
                return reject();
            }
            let balance = web3.utils.fromWei(result, 'ether');
            console.log('Current Server Balance:', balance);

            if (balance < amountToSend) {
                console.error('Insufficient funds!');
                return reject();
            }

            let details = {
                "to": to,
                value: web3.utils.toHex(web3.utils.toWei('0.1', 'ether')),
                gasLimit: web3.utils.toHex(21000),
                gasPrice: web3.utils.toHex(web3.utils.toWei('10', 'gwei')),
                "nonce": nonce,
                "chainId": 4 // EIP 155 chainId - mainnet: 1, rinkeby: 4
            };

            const transaction = new EthereumTx(details, { chain: 'rinkeby' });
            let privateKey = _serverEthAccount.privateKey.split('0x');
            let privKey = Buffer.from(privateKey[1], 'hex');
            transaction.sign(privKey);

            const serializedTransaction = transaction.serialize();

            web3.eth.sendSignedTransaction('0x' + serializedTransaction.toString('hex'))
                .on('transactionHash', (hash) => {
                    const url = `https://rinkeby.etherscan.io/tx/${hash}`; // TODO: Change this for prod
                    console.log('Transaction URL: ', url);
                    console.log('TX: ', hash);
                    resolve(hash);
                })
                .on('receipt', (rec) => {
                    console.log('Receipt', rec);
                    resolve(rec);
                })
                .on('confirmation', (confirmationNum, rec) => {
                    console.log('Confirmation Received: ', confirmationNum);
                    resolve({ confirmationNum, rec });
                })
                .on('error', (err) => {
                    console.error(err);
                    reject();
                });
        });
    });
}

What I am noticing here is that I can successfully execute this code. The on('transactionHash) event is fired and I get that, although there is nothing on Etherscan. I never get a receipt and no transaction is made.

I have spent some time Googling the issue and have also tried changing the line creating the transaction object to const transaction = new EthereumTx(details, { chain: 'rinkeby', hardfork: 'istanbul' });, but I am still getting the exact same result.

Other information that might be relevant is that I am Infura.

const ethNetwork = 'https://rinkeby.infura.io/v3/{key}';
const web3 = new Web3(new Web3.providers.HttpProvider(ethNetwork));

Any help would be greatly appreciated!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.