Alberto Beiz
Albertobeiz

Albertobeiz

Hello World in Ethereum dev with ethers.js and hardhat

Hello World in Ethereum dev with ethers.js and hardhat

Subscribe to my newsletter and never miss my upcoming articles

Are you interested in blockchain development like me? Then follow me in my learning journey! I'll set up a local dev environment using hardhat and make an ETH transaction between 2 accounts.

First things first, we need to have Node.js version 12 or above installed. We'll init a new project and install hardhat as a dependency:

mkdir local-blockchain
cd local-blockchain
npm init
npm i hardhat

We create an empty hardhat configuration:

npx hardhat

hh.png

We're ready to start a node:

npx hardhat node

node.png

Ok so we have a working node and 20 test accounts with 10.000 ETH each. We're ready to transfer funds between accounts.

We need a (surprise) node package to interact with the blockchain:

npm i ethers

And now a quick node script to send 10 ETH from one account to another:

// index.js

const { ethers } = require("ethers");

const provider = new ethers.providers.JsonRpcProvider();
const signer = provider.getSigner();

const from = '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266';
const to = '0x70997970c51812dc3a010c7d01b50e0d17dc79c8'


sendTransactionAndShowBalances();


async function sendTransactionAndShowBalances() {
  await signer.sendTransaction({
    from,
    to,
    value: ethers.utils.parseEther("10.0")
  });

  let balance = await provider.getBalance(from);
  console.log('From account balance:', ethers.utils.formatEther(balance));

  balance = await provider.getBalance(to);
  console.log('To account balance:', ethers.utils.formatEther(balance));
}

That's it! Send a transaction and print account balances. Let's run it:

node index.js

acc.png

🀨 WAIT....WHAT? The sending account is missing some ETH!

Correct, to send a transaction you need to pay a small (or big πŸ˜…) fee , GAS, to the network, it's what keeps everything working and secure.

You are not giving your data to any central player now (they'll make money later selling it), and to keep the system decentralized some πŸ’ΈπŸ’ΈπŸ’Έ is needed.

OK WE ARE DONE! Now it will be a good idea to code a frontend and use it to send our transactions. If you want to get part 2 pleeeeeeease leave a comment or reaction πŸ™πŸ™πŸ™

AND

If you have a blockchain project and need a dev, I'm available for hiring as freelance dev. Just contact me on LinkedIn or Twitter

linkedin.com/in/albertobeiz twitter.com/albertobeiz

Did you find this article valuable?

Support Alberto Beiz by becoming a sponsor. Any amount is appreciated!

Learn more about Hashnode Sponsors
Β 
Share this