Loading...
Hello Guys;
In this session we will be looking at how to send ether from server side using nodejs.
We will be needing public,private key pairs of sender and only public key address of receiver.
To correctly excecute this procedure.See the steps here
Then you need web3 and hd wallet to interact from backend.
you can simply type these codes on your command prompt & hit enter
npm install web3
npm install @truffle/hdwallet-provider
you will be needing an rpc url which you can get from here
Just click on the link -> Login to your account ->Click on ethereum on left side bar ->create project ->settings->keys->endpoints
and choose your respective testnet rpc url
rpc url it will look like this - https://ropsten.infura.io/v3/your_api_key
Then in your Project folder create 2 files named package.json & index.js and do necessary changes in index.js
{
"name": "app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"author": "",
"license": "ISC",
"dependencies": {
"@truffle/hdwallet-provider": "^1.4.1",
"express": "^4.17.1",
"web3": "^1.2.9"
}
}
copy paste this code inside package.json file
var express = require('express');
var Web3 = require('web3');
const Provider = require('@truffle/hdwallet-provider');
var app = express();
var port = process.env.PORT || 3000;
var rpcurl = "infura rpc url"; //Put your rpc url here
var senderaddress = "address"; //provide sender's address
var receiveraddress = "addresss"; //provide reciever's address
var senderprivatekey = 'private key' //provide the private key
const sendEther = async () =>{
console.log("in function");
var provider = new Provider(senderprivatekey, rpcurl);
var web3 = new Web3(provider);
console.log("provider set transaction initiated");
web3.eth.sendTransaction({
from: senderaddress,
to: receiveraddress,
value: '100000000000000000' // 0.1 ether 10^17
})
.then(function(receipt){
console.log(receipt);
});
}
// transaction may take a few min to get executed
sendEther();
app.listen(port);
console.log('listening on', port);
then on command prompt run
npm install
This will install all the package dependencies on your computer
then run the index.js file on cmd for the transaction to happen
node index
With that being said let's roll our session.
For test ethers and link tokens. Click Here
For many more exciting tutorials keep following my username @BlockTalks_Raj
!!!!!HAPPY LEARNING!!!!