Using Web3.js

Introduction

This guide walks through the process of using Web3 to manually sign and send a transaction to an Edgeware EVM dev node. For this example, we will use Node.js and straightforward JavaScript code.
Note This tutorial was created using the release of Edgeware EVM. The Edgeware EVM platform, and the Frontier components it relies on for Substrate-based Ethereum compatibility, are still under very active development. We have created this tutorial so you can test out Edgeware's Ethereum compatibility features. Even though we are still in development, we believe it’s important that interested community members and developers have the opportunity to start to try things with Edgeware and provide feedback.

Checking Prerequisites

Installed Nodejs and a particular package manager like yarn or npm, rest we have batteries included in this tutorial. This guide assumes that you have a running local Edgeware EVM node running in --dev mode..
1
git clone https://github.com/edgeware-builders/tutorials tutorials;cd tutorials/web3;yarn
Copied!
It will move to your cloned repository, install required packages and you are ready to go!

Creating Transaction

For this example, we only need a single JavaScript file to create the transaction, which we will run using the node command in the terminal. The script will transfer 1337 ETH from the genesis account to another address. For simplicity, the file is divided into three sections: variable definition, create transactions, and broadcast transaction.
We need to set a couple of values in the variables definitions:
  • Create our Web3 constructor (web3)
  • Define the privKey variable as the private key of our genesis account, which is where all the funds are stored when deploying your local Edgeware EVM node and what is used to sign the transactions
  • Set the "from" and "to" address, making sure to set the value of toAddress to a different address, for example, the one created by Metamask when setting up a local wallet
1
const Web3 = require('web3');
2
3
// genesis private key
4
const privKey = '1111111111111111111111111111111111111111111111111111111111111111';
5
const addressFrom = '0x19e7e376e7c213b7e7e7e46cc70a5dd086daff2a';
6
const addressTo = '0x6bB5423f0Dd01B8C5028a1bc01e1f1bDe4523e72';
7
const web3 = new Web3('http://localhost:9933/');
Copied!
Both the create transaction and deploy transaction sections are wrapped in an asynchronous function that handles the promises from our Web3 instance. To create the transaction, we use the web3.eth.accounts.signTransaction(tx, privKey) command, where we have to define the tx object with some parameters such as: addressFrom, addressTo, number of tokens to send, and the gas limit.
1
const deploy = async () => {
2
console.log(
3
`Attempting to make transaction from ${addressFrom} to ${addressTo}`
4
);
5
6
const createTransaction = await web3.eth.accounts.signTransaction(
7
{
8
from: addressFrom,
9
to: addressTo,
10
value: web3.utils.toWei('1337', 'ether'),
11
gas: 21000,
12
},
13
privKey
14
);
Copied!
Complete createTransaction.js should look like this!
1
const Web3 = require('web3');
2
3
// genesis private key
4
const privKey = '1111111111111111111111111111111111111111111111111111111111111111';
5
const addressFrom = '0x19e7e376e7c213b7e7e7e46cc70a5dd086daff2a';
6
const addressTo = '0x6bB5423f0Dd01B8C5028a1bc01e1f1bDe4523e72';
7
const web3 = new Web3('http://localhost:9933/');
8
9
// Create transaction
10
const deploy = async () => {
11
console.log(
12
`Attempting to make transaction from ${addressFrom} to ${addressTo}`
13
);
14
15
const createTransaction = await web3.eth.accounts.signTransaction(
16
{
17
from: addressFrom,
18
to: addressTo,
19
value: web3.utils.toWei('1337', 'ether'),
20
gas: 21000,
21
},
22
privKey
23
);
24
25
// Deploy transaction
26
const createReceipt = await web3.eth.sendSignedTransaction(
27
createTransaction.rawTransaction
28
);
29
console.log(
30
`Transaction successful with hash: ${createReceipt.transactionHash}`
31
);
32
process.exit(0);
33
};
34
35
deploy();
Copied!

Check balance on accounts

Before running the script, we need another file to check the balances of both addresses before and after the transaction is executed. We can easily do this by leveraging the Ethereum compatibility features of Edgeware.
balances.js looks like this:
1
const Web3 = require('web3');
2
3
// Variables definition
4
const addressFrom = '0x19e7e376e7c213b7e7e7e46cc70a5dd086daff2a';
5
const addressTo = '0x6bB5423f0Dd01B8C5028a1bc01e1f1bDe4523e72';
6
const web3 = new Web3('http://127.0.0.1:9933');
7
8
// Balance call
9
const balances = async () => {
10
const balanceFrom = web3.utils.fromWei(
11
await web3.eth.getBalance(addressFrom),
12
'ether'
13
);
14
const balanceTo = await web3.utils.fromWei(
15
await web3.eth.getBalance(addressTo),
16
'ether'
17
);
18
19
console.log(`The balance of ${addressFrom} is: ${balanceFrom} ETH.`);
20
console.log(`The balance of ${addressTo} is: ${balanceTo} ETH.`);
21
};
22
23
balances();Copy to clipboardErrorCopied
Copied!

Play time

Run node balances.js to check initial balance on accounts
*web3-init-balance
Run node createTransaction.js to transfer some stuff around chain
*web3-make-transaction
Run node balances.js to check result balances
*web3-result-balance

Reach us for more engagement

Glad you've made it through! 🥰 We are eager to guide your more on your exploration through Edgeware Ethereum combability feature. We are keen to hear your experience and suggestions you may have for us.. You can feel free to chat with us in the Edgeware's channels like Discord, Element and Telegram, we can help you out with issues you may have or project you may want to be funded through our Treasury program. Don't hesitate to share your feedback on our channels, there is always space to improve! 🙌
Last modified 2mo ago