web3js – How to Initiate a Transaction with Address and PrivateKey

accountstransactionsweb3js

I have a list of ethereum addresses and private keys, I need to write an automation script which checks for the balance in each address and transfer the balance funds to another ethereum address.
My question is how I perform the fund transfer with address and privateKey using web3js

Thanks in Advance

Best Answer

If you want to programmatically create transactions for accounts where you have the private key, you can call the web3.eth.accounts.signTransaction function to create a transaction using web3.js:

web3.eth.accounts.signTransaction(tx, privateKey [, callback]);

This will return a rawTransaction string which can be directly sent to the network using web3.eth.sendSignedTransaction:

web3.eth.sendSignedTransaction(signedTransactionData [, callback])

Getting the balance of the account should be even simpler. Just call web3.eth.getBalance for the public address you have for that account:

web3.eth.getBalance(address [, defaultBlock] [, callback])

I hope this helps!

Related Topic