• Stars
    star
    114
  • Rank 308,031 (Top 7 %)
  • Language
    HTML
  • Created about 6 years ago
  • Updated about 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Create An Ethereum Dapp with Ethersjs

This is a step-by-step tutorial on how to create a front end, deploy a Solidity smart contract, and connect them together. We will use Metamask, Remix IDE and Ethersjs.

By the end of this tutorial you will be able to create a simple HTML front end with buttons that can interact with smart contract functions. The tutorial takes place in 3 stages

  • Create a basic html web page
  • Create a basic solidity smart contract
  • Connect the web page with the smart contracts using Ethersjs.

Preparation

  1. Download and Install MetaMask
    • Never used Metamask? Watch this explainer video

      The important bits for us are: 1:06 to 4:14

    • Change to the Ropsten Tesnet and get a Copy an account's wallet public address

  1. Request some Ropsten Tesnet Ether from a faucet loaded into your Metamask Wallet.

  2. Install a http server. Use any you like, but we recommend lite-server for beginners:

    npm install -g lite-server #install lite-server globally

Create and Serve a Simple Webpage

The first step is to create a basic html page.

  1. Create a new folder (directory) in your terminal using mkdir <directory name>
  2. In a code editor (e.g. atom, or visual studio), open the folder
  3. Create a new file called index.html
  4. Open index.html
  5. Create html and body tags
 <html>
    <body>
    </body>
 </html>

We will create an app that simply reads and writes a value to the blockchain. We will need a label, an input, and buttons. 6. Inside the body tag, add some text, a label and input.

    <body>
      <h1>This is my dApp!</h1>
      <p> Here we can set or get the mood: </p>
      <label for="mood">Input Mood:</label> <br>
      <input type="text" id="mood">
    </body>
  1. Inside the body tag add some buttons.
      <div>
        <button onclick="getMood()"> get Mood </button>
      </div>
      <div>
        <button onclick="setMood()"> set Mood</button>
      </div>
  1. Serve the webpage via terminal/command prompt from the directory that has index.html in it and run:

    lite-server
  2. Go to http://127.0.0.1:3000/ in your browser to see your page!

  3. Your front end is now complete!


Create a Basic Smart Contract

Now it is time to create a solidity smart contract.

  1. You can use any editor you like to make the contract. For this tutorial we recommend the online IDE [remix.ethereum.org]

  2. Go to remix.ethereum.org

  3. Check out the "Solidity Compiler", and "Deploy and Run Transactions" tabs. If they are not present, enable them in the plugin manager

  4. Create a new solidity file in remix, named mood.sol

  5. Write the contract

    • Specify the solidity version
     pragma solidity ^0.8.1;
    
    • Define the contract
     contract MoodDiary{
     
     }
    
    • Inside the contract create a variable called mood
     string mood;
    
    • Next, create Read and Write functions
     //create a function that writes a mood to the smart contract
     function setMood(string memory _mood) public{
         mood = _mood;
     }
     
     //create a function the reads the mood from the smart contract
     function getMood() public view returns(string memory){
         return mood;
     }
    
    • And that's it! your code like this
  6. Deploy the contract on the Ropsten Testnet.

    • Make sure your Metamask is connected to the Ropsten Testnet.
    • Make sure you select the right compiler version to match the solidity contract. (In the compile tab)
    • Compile the code using the "Solidity Compiler" tab. Note that it may take a moment to load the compiler
    • Deploy the contract under the "Deploy and Run Transactions" tab
    • Under the Deployed Contracts section, you can test out your functions on the Remix Run tab to make sure your contract works as expected!

<script async src="//s.imgur.com/min/embed.js" charset="utf-8"></script>

Be sure to deploy on Ropsten via Remix under the Injected Web3 environment and confirm the deployment transaction in Metamask

Make a new temporary file to hold:

  • The deployed contract's address
    • Copy it via the copy button next to the deployed contracts pulldown in remix's Run tab
  • The contract ABI (what is that?)
    • Copy it via the copy button under to the contract in remix's Compile tab (also in Details)

Connect Your Webpage to Your Smart Contract

Back in your local text editor in index.html, add the following code to your html page:

  1. Import the Ethersjs source into your index.html page inside a new set of script tags:
<script charset="utf-8"
        src="https://cdn.ethers.io/scripts/ethers-v4.min.js"
        type="text/javascript">
 </script>

<script
  ////////////////////
  //ADD YOUR CODE HERE
  ////////////////////      
</script>
  1. Inside a new script tag, ensure ethereum is enabled:
window.ethereum.enable();
  1. Now, define an ethers provider. In our case it is Ropsten:
var provider = new ethers.providers.Web3Provider(web3.currentProvider,'ropsten');
  1. Import the contract ABI (what is that?) and specify the contract address on our provider's blockchain:
  var MoodContractAddress = "<contract address>";
  var MoodContractABI = <contract ABI>
  var MoodContract
  var signer
  1. Connect the signer to your metamask account (we use [0] as the defalut), and define the contract object using your contract address, ABI, and signer.
provider.listAccounts().then(function(accounts) {
      signer = provider.getSigner(accounts[0]);
      MoodContract = new ethers.Contract(MoodContractAddress, MoodContractABI, signer);
    })
  1. Create asynchronous functions to call your smart contract functions
  async function getMood(){
    getMoodPromise = MoodContract.getMood();
    var Mood = await getMoodPromise;
    console.log(Mood);
  }

  async function setMood(){
    let mood = document.getElementById("mood").value
    setMoodPromise = MoodContract.setMood("patient");
    await setMoodPromise;
  }
  1. Connect your functions to your html buttons
<button onclick="getMood()"> get Mood </button>
<button onclick = "setMood()"> set Mood</button>

Test Your Work Out!

  1. Got your webserver up? Go to http://127.0.0.1:1337/ in your browser to see your page!
  2. Test your functions and approve the transactions as needed through Metamask. Note block times are ~15 seconds... so wait a bit to read the state of the blockchain 😎
  3. See your contract and transaction info via [https://ropsten.etherscan.io/]
  4. Open a console (Ctrl + Shift + i) in the browser to see see the magic happen as you press those buttons 🌠

🎉🎊🎉🎊🎉 DONE! 🎉🎊🎉🎊🎉

Celebrate! :bowtie: You just made a webpage that interacted with a real live Ethereum testnet on the internet! That is not something many folks can say they have done! 🚀


If you had trouble with the tutorial, you can try out the example app provided.

git clone https://github.com/BlockDevsUnited/BasicFrontEndTutorial.git
cd BasicFrontEndTutorial
lite-server

Try and use the following information to interact with an existing contract we published on the Roptsen testnet:

  • We have a MoodDiary contract instance created at this transaction

  • Here is the contract (on etherscan)

    • We also verified our source code to ropsten.etherscan.io as an added measure for you to verify what the contract is exactly, and also the ABI is available to the world! 😁
  • The ABI is also in this file

This illustrates an important point: you can also build a dApp without needing to write the Ethereum contract yourself! If you want to use an existing contract written and already on Ethereum!

More Repositories

1

NFT-Tutorial

Deploy an NFT project on Ethereum
Solidity
85
star
2

TokenTutorial

Solidity
68
star
3

l-earn

SCSS
45
star
4

NFT-COURSE

JavaScript
30
star
5

DEX-Course

Month long course on creating a DEX
23
star
6

dev-punks

BDU Generative Art Collaboration
Solidity
10
star
7

DAO-COURSE

Month Long Course on DAO Development
9
star
8

Developer-Learning-Paths

Follow the learning path and become a Blockchain Developer
9
star
9

text-my-bits

Send Smart Bitcoin via SMS using RSK
JavaScript
7
star
10

Projects

Summary of all official BDU Projects, project managers, and bounty allocations.
7
star
11

Bitcoin-Miami-Hackathon

SMS based Bitcoin payments using RSK
JavaScript
6
star
12

Whitepaper

5
star
13

DevCash-ERC20

4
star
14

zksync-tutorial

How to use the zksync javascript sdk
JavaScript
4
star
15

Devchain

An Ethereum compatible Substrate blockchain for bounties and governance for the Devcash community.
Rust
4
star
16

React-dApp-Tutorial

JavaScript
3
star
17

Gnosis-Multisig-

CSS
3
star
18

Hello-world-

In case any new BDU groups were popping up and a little nervous about finding content. This could be a perfect first lesson
3
star
19

Aion-AVM-Starter-Guide

How to create a full stack Dapp using Aion's AVM testnet!
JavaScript
2
star
20

Airdrop-Script

Solidity
2
star
21

ipfs-tutorial

Tutorial notes for a meetup on IPFS
HTML
1
star
22

DevCash-DEX

Smart contract DEX to trade DevCash against other assets
1
star
23

pre-IBO

The DevCash pre-IBO
JavaScript
1
star
24

Devcash-Bounty-Platform

Devcash Frontend
Vue
1
star
25

devcash-projects

List of Devcash Projects, links and essential details.
1
star
26

DEX-Course-SwappitySwap-sdk

TypeScript
1
star
27

Read-More-Solidity

Slides for a Blockchain Developers United meetup
JavaScript
1
star
28

DEX-Course-SwappitySwap-core

TypeScript
1
star
29

ide.blockchaindevsunited.com

The IDE for devcash
CSS
1
star
30

NFT-WordOfMouth

Solidity
1
star
31

ENS-Tutorial

How to create a basic dapp using the ENS
JavaScript
1
star
32

Ethereum-Tutorials

1
star
33

NEARbounties

A bounty contract on the NEAR
Rust
1
star