To send a transaction to ethereum network we first need to sign it using private key and then it needed to be sent on the network. Process for the same in client side code is different than using a private key in backend/node project.
In this smartbook we will take a look at those functions and send test ethers using them. We will be using following two methods to send transaction on ethereum network
This function will sign a transaction. This account needs to be unlocked.
Syntax : web3.eth.signTransaction( transactionObject, address [, callback] )
Parameters :
Object - The transaction data to sign. See web3.eth.sendTransaction() for more.
String - Address to sign transaction with.
Function - (optional) Optional callback, returns an error object as first parameter and the result as second.
Returns : The RLP encoded transaction object is returned.
This function sends an already signed transaction, generated using web3.eth.accounts.signTransaction as discussed in previous section.
Syntax : web3.eth.sendSignedTransaction( signedTransactionData [, callback] )
Parameters :
String - Signed transaction data in HEX format
Function - (optional) Optional callback, returns an error object as first parameter and the result as second.
Returns : A promise combined event emitter. Resolves when the transaction receipt is available.
RPC url is required for our program to interact with a blockchain network. You can check different methods to connect through providers in this smartbook.
For this example, the provder we will be using is Infura via http. All we have to do is create an account and generate a project which will provide us with rpc url for created project's api key.
You can get test tokens from ropsten faucet. Enter your address and click on submit.
First initialise nodejs project and create working file with following commands,
> npm init
> touch index.js
Dependency required is web3, commands for its installation is as follows :
> npm install web3
Now that installation and configuration process is complete lets look at the code.
Let's initialise web3 with RPC provider with rpc url we got from previous steps
const Web3 = require('web3');
const web3 = new Web3("https://ropsten.infura.io/v3/<API>");
const Private_Key = '<Private Key>';
const from_address = '<Public Address>';
const to_address = '<Receiver Public Address>';
Signing a transaction will happen with our private key as follow,
var SingedTransaction = await web3.eth.accounts.signTransaction({
to: to_address,
value: '1000000000',
gas: 2000000
}, Private_Key );
Send transaction will need rawTransaction from signed transaction object we got from previous function.
web3.eth.sendSignedTransaction(SignedTransaction.rawTransaction).then((receipt) => {
console.log(receipt)
});
const Web3 = require('web3');
const web3 = new Web3("https://ropsten.infura.io/v3/<API>");
const Private_Key = '<Private Key>';
const from_address = '<Public Address>';
const to_address = '<Receiver Public Address>';
async function eth_transaction(){
var SingedTransaction = await web3.eth.accounts.signTransaction({
to: to_address,
value: '1000000000',
gas: 2000000
}, Private_Key );
web3.eth.sendSignedTransaction(SignedTransaction.rawTransaction).then((receipt) => {
console.log(receipt);
});
}
eth_transaction();