create account

(PT 3) Ethereum Front End - Interacting With A Smart Contract From The Browser (Part 3) by igormuba

View this thread on: hive.blogpeakd.comecency.com
· @igormuba ·
$25.70
(PT 3) Ethereum Front End - Interacting With A Smart Contract From The Browser (Part 3)
# Repository
https://github.com/igormuba/EthereumFrontEnd/tree/master/class3

# What will I learn?

- React consultor and states
- Working with Remix/Ganache/Web3 to import contract ABI and address to React
- Creating and interacting with a contract instance on React with Web3
- Getting and formatting data returned by the blockchain

# Requirements

- Chrome or Brave Browser
- Metamask browser extension
- Node/NPM
- Ganache

# Difficulty

- Intermediary.

# Introduction

On the previous tutorial of the series we saw can we connect our browser with a blockchain node using the framework web3.js, provided by Ethereum. By using Metamask as our node, we can easily switch between blockchains. That means you can test on a local emulated environment (like Ganache) or you can use a regular testnet (like Rinkeby or Ropsten), or even switch to the main Ethereum network!

Now, let us dive a bit on how to "see" contract. We can already connect to any Ethereum blockchain, but we need to know how to work with contracts. For this, I will use the code for an ERC20 contract we have made in another series. You can get the contract for this token [in here](https://github.com/igormuba/DEX/blob/master/Class%2011/contracts/erc20.sol). Just keep in mind that the token is not optimal, not fully ERC20 compatible and has security issues, it is just a very basic token implementation for testing purposes! I have other more in-depth tutorials regarding fully ERC20 compliance and how to improve security, but that is not the goal here.

We will leverage the simplicity of the [Remix online Solidity IDE](http://remix.ethereum.org) with the simulation of Ganache to deploy and run our contract locally.

# Our contract

For web3.js to work with a contract, it needs two things:
- The ABI code of the contract
- The address of the contract.

The ABI is needed because it contains, in JSON, the functions and variables on the contract. Without it, web3.js wouldn't know what to do with the contract data, because they talk "different languages"! And the address is required so that the browser knows "where" in the blockchain to make the calls.

To get the contract ABI, you can use the [Remix online Solidity IDE](http://remix.ethereum.org), this tool allows us to get the ABI and the address, even from our local Ganache environment! Here is the step by step to deploy.

Paste your code on the IDE. Save/compile it with ctrl+c on Windows/Linux/BSD or cmd+s on Mac:
![Captura de Tela 20190410 às 16.37.36.png](https://files.steempeak.com/file/steempeak/igormuba/mj7bHOk7-Captura20de20Tela202019-04-1020aCC80s2016.37.36.png)

On the top right corner select the "run" tab:
![Captura de Tela 20190410 às 16.39.41.png](https://files.steempeak.com/file/steempeak/igormuba/2Dpor0NN-Captura20de20Tela202019-04-1020aCC80s2016.39.41.png)



On the environment setting, choose "Web3 Provider", that allows us to use our local Ganache environment instead of Remix simulated Virtual Machine:
![Captura de Tela 20190410 às 16.40.15.png](https://files.steempeak.com/file/steempeak/igormuba/8xO2Tt3a-Captura20de20Tela202019-04-1020aCC80s2016.40.15.png)

And insert the IP:Port of your local Ganache environment (or any other network you want, as long as it matches your Metamask network):
![Captura de Tela 20190410 às 16.41.04.png](https://files.steempeak.com/file/steempeak/igormuba/E1YuF4yD-Captura20de20Tela202019-04-1020aCC80s2016.41.04.png)
GIF illustrating the process:
<img src="https://media.giphy.com/media/KHKWAFu3CyTxe8ziJi/giphy.gif">

Try to make sure that the address matches the IP/Port of Ganache (and Metamask!) so we can work on the same page:
![Captura de Tela 20190410 às 16.42.00.png](https://files.steempeak.com/file/steempeak/igormuba/h8LchIgB-Captura20de20Tela202019-04-1020aCC80s2016.42.00.png)

Now you can deploy your contract by clicking on "deploy" on the run page:
![Captura de Tela 20190410 às 17.08.26.png](https://files.steempeak.com/file/steempeak/igormuba/3jH0LHGh-Captura20de20Tela202019-04-1020aCC80s2017.08.26.png)

# Storing the contract details

Now, back to our code editor, since our work with Remix is done (don't close it yet! we need to store the data!). On the `src` folder create a new file to store the address and the ABI of the contract. I will call this file "contract.js":
![Captura de Tela 20190410 às 17.11.03.png](https://files.steempeak.com/file/steempeak/igormuba/qoOskkua-Captura20de20Tela202019-04-1020aCC80s2017.11.03.png)

Inside it, I will export the variables with the needed data for the "app" to run on the browser. Here is how you can get that from Remix!

On the Remix compile tab, click on "ABI", this will copy all the ABI data to your clipboard:
![Captura de Tela 20190410 às 17.13.03.png](https://files.steempeak.com/file/steempeak/igormuba/cxw9xXkm-Captura20de20Tela202019-04-1020aCC80s2017.13.03.png)

On the `contract.js` file, paste it on a variable that we can export, for example:

```
export let contract_abi = //PASTE THE ABI HERE!
```

![Captura de Tela 20190410 às 17.14.44.png](https://files.steempeak.com/file/steempeak/igormuba/axo3cfbY-Captura20de20Tela202019-04-1020aCC80s2017.14.44.png)

On the "run" tab on Remix, you can copy the address by clicking on the clipboard icon:

![Captura de Tela 20190410 às 17.15.26.png](https://files.steempeak.com/file/steempeak/igormuba/o5nKUhV9-Captura20de20Tela202019-04-1020aCC80s2017.15.26.png)

You could also get the contract from Ganache on the tab "blocks" on the transaction that created the contract since we deployed it locally on a previous step:
![Captura de Tela 20190410 às 17.16.44.png](https://files.steempeak.com/file/steempeak/igormuba/alT2iruY-Captura20de20Tela202019-04-1020aCC80s2017.16.44.png)

Now, save the address on the save file as the ABI (for simplicity):
```
export let contract_address = 'PASTE THE ADDRESS HERE (WITH SINGLE QUOTES)'
```
The final file should look something like this:
![Captura de Tela 20190410 às 17.18.07.png](https://files.steempeak.com/file/steempeak/igormuba/M5FJL7DV-Captura20de20Tela202019-04-1020aCC80s2017.18.07.png)

# React

Now that we have all that is necessary to interact with the contract, it is time to have fun with React.js! Let us dig on a few basic concepts and apply them to our small test with the contract.

First, we will need to use [states](https://reactjs.org/docs/react-component.html#constructor) to make variables globally available for all of our component. To do this we use a constructor to create a state. A state behaves similarly to a [singleton](https://pt.wikipedia.org/wiki/Singleton). You create one and the variables inside it can be accessed for other methods inside our component.

In practice, to create one, we use the following syntax:
```
constructor(props){
    super(props)
    this.state = {
         //here goes the global variables
    }
}
```
The global variables will go and replace the comment.

In our case, we want to store the token balance of our user (ourselves). So replace the comment with:
```
balance: 0
```

Now, on the render method of the component, we can delete everything. It was showing the default react app webpage, but we don't need it. Instead, we will render:
```
render(){
    return(
      <div>
          <p>Token Balance: {this.state.balance}</p>
      </div>
      );
}
```

The `{this.state.balance}` will grab the `balance` variable inside the `state` object. At the moment, if you run the app, you will see just a zero on the webpage. To fill it with data, we need to our previous `blockchainConnection` method and instantiate the contract and make a connection to it.

# Connecting to the contract

To instantiate the contract, we need the ABI and the address from the `contract.js` file. But they are on different files. So, on the top of the `App.js` we import them with:
```
import {contract_address, contract_abi} from './contract'
```
The above imports both variables.

Now, on the `blockchainConnection` method we can create the instance with:
```
let erc20token = new web3.eth.Contract(contract_abi, contract_address)
```

Now, if you check the contract, there is a method called `balanceOf` that receives a wallet address as an argument. That method returns the token balance of said address. I will show you the full call and then explain the bits of it:

```
let myTokenAmount = await erc20token.methods.balanceOf('YOUR WALLET HERE').call()
```
This saves the returned token balance on the variable `myTokenAmount `.
`await`, as explained in other tutorials, tells JavaScript to hold the execution until we get a response because blockchains are inherently slow!
`erc20token.methods` is where the method names are.
`balanceOf('YOUR WALLET HERE')` receives the wallet of a user as an argument. Change the wallet to your own, remember, this must be the same wallet that deployed the contract! Else we will receive a zero!
`call()` tells it that it is a blockchain call, calls don't consume gas and are free because are read-only!

Now, to store the result on the state, we do:
```
this.setState({
      balance: myTokenAmount
    })
```

The looks ok, but it is not. Let's see why by checking on the browser.
The behavior expected is: To open the page and display the token balance of the user (100).
What we get is:
![Captura de Tela 20190410 às 18.02.12.png](https://files.steempeak.com/file/steempeak/igormuba/FFjEHnGj-Captura20de20Tela202019-04-1020aCC80s2018.02.12.png)

This is because the types the blockchain return are not compatible with the types React expected. The type returned is an object:
![Captura de Tela 20190410 às 18.03.15.png](https://files.steempeak.com/file/steempeak/igormuba/oW9KKsL1-Captura20de20Tela202019-04-1020aCC80s2018.03.15.png)

To solve this, we simply need to parse the number as an int (a native JavaScript function) by adding `parseInt` around the variable:
```
parseInt(myTokenAmount)
```

And that gives us the expected result:
![Captura de Tela 20190410 às 18.04.56.png](https://files.steempeak.com/file/steempeak/igormuba/ZfAOn44n-Captura20de20Tela202019-04-1020aCC80s2018.04.56.png)

That means we've hit gold and we have successfully talked with the contract!

# Series curriculum

-[Part 2](https://steemit.com/utopian-io/@igormuba/pt-2-ethereum-front-end-web3-js-on-browser-react-switching-chains-and-pulling-data-part-2)
-[Part 1](https://steemit.com/utopian-io/@igormuba/pt-1-ethereum-front-end-introduction-to-web3-js-part-1)

# Beneficiaries
This post has as beneficiaries
- [@utopian.pay](https://steemit.com/@utopian.pay) with 5%
- [@steempeak](https://steemit.com/@steempeak) with 5%

using the SteemPeak beneficiary tool:
![captura4.png](https://files.steempeak.com/file/steempeak/igormuba/aLEsximk-captura4.png)
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 125 others
properties (23)
authorigormuba
permlinkpt-3-ethereum-front-end-interacting-with-a-smart-contract-from-the-browser-part-3
categoryutopian-io
json_metadata{"app":"steempeak/1.9.6","format":"markdown","tags":["utopian-io","tutorials","ethereum","front-end","development"],"users":["igormuba","utopian.pay","steempeak"],"links":["https://github.com/igormuba/EthereumFrontEnd/tree/master/class3","https://github.com/igormuba/DEX/blob/master/Class%2011/contracts/erc20.sol","http://remix.ethereum.org","http://remix.ethereum.org","https://reactjs.org/docs/react-component.html#constructor","https://pt.wikipedia.org/wiki/Singleton","/utopian-io/@igormuba/pt-2-ethereum-front-end-web3-js-on-browser-react-switching-chains-and-pulling-data-part-2","/utopian-io/@igormuba/pt-1-ethereum-front-end-introduction-to-web3-js-part-1","/@utopian.pay","/@steempeak"],"image":["https://files.steempeak.com/file/steempeak/igormuba/mj7bHOk7-Captura20de20Tela202019-04-1020aCC80s2016.37.36.png","https://files.steempeak.com/file/steempeak/igormuba/2Dpor0NN-Captura20de20Tela202019-04-1020aCC80s2016.39.41.png","https://files.steempeak.com/file/steempeak/igormuba/8xO2Tt3a-Captura20de20Tela202019-04-1020aCC80s2016.40.15.png","https://files.steempeak.com/file/steempeak/igormuba/E1YuF4yD-Captura20de20Tela202019-04-1020aCC80s2016.41.04.png","https://media.giphy.com/media/KHKWAFu3CyTxe8ziJi/giphy.gif","https://files.steempeak.com/file/steempeak/igormuba/h8LchIgB-Captura20de20Tela202019-04-1020aCC80s2016.42.00.png","https://files.steempeak.com/file/steempeak/igormuba/3jH0LHGh-Captura20de20Tela202019-04-1020aCC80s2017.08.26.png","https://files.steempeak.com/file/steempeak/igormuba/qoOskkua-Captura20de20Tela202019-04-1020aCC80s2017.11.03.png","https://files.steempeak.com/file/steempeak/igormuba/cxw9xXkm-Captura20de20Tela202019-04-1020aCC80s2017.13.03.png","https://files.steempeak.com/file/steempeak/igormuba/axo3cfbY-Captura20de20Tela202019-04-1020aCC80s2017.14.44.png","https://files.steempeak.com/file/steempeak/igormuba/o5nKUhV9-Captura20de20Tela202019-04-1020aCC80s2017.15.26.png","https://files.steempeak.com/file/steempeak/igormuba/alT2iruY-Captura20de20Tela202019-04-1020aCC80s2017.16.44.png","https://files.steempeak.com/file/steempeak/igormuba/M5FJL7DV-Captura20de20Tela202019-04-1020aCC80s2017.18.07.png","https://files.steempeak.com/file/steempeak/igormuba/FFjEHnGj-Captura20de20Tela202019-04-1020aCC80s2018.02.12.png","https://files.steempeak.com/file/steempeak/igormuba/oW9KKsL1-Captura20de20Tela202019-04-1020aCC80s2018.03.15.png","https://files.steempeak.com/file/steempeak/igormuba/ZfAOn44n-Captura20de20Tela202019-04-1020aCC80s2018.04.56.png","https://files.steempeak.com/file/steempeak/igormuba/aLEsximk-captura4.png"]}
created2019-04-10 21:18:48
last_update2019-04-10 21:18:48
depth0
children5
last_payout2019-04-17 21:18:48
cashout_time1969-12-31 23:59:59
total_payout_value19.033 HBD
curator_payout_value6.668 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length10,713
author_reputation128,826,081,446,669
root_title"(PT 3) Ethereum Front End - Interacting With A Smart Contract From The Browser (Part 3)"
beneficiaries
0.
accountsteempeak
weight500
1.
accountutopian.pay
weight500
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id82,836,670
net_rshares46,116,316,038,143
author_curate_reward""
vote details (189)
@mcfarhat ·
$7.11
Thank you for another beautiful tutorial.
- I had no idea you can do that via a browser.
- Good basic intro into the concept of fetching data from contact and communicating with them.
- Very good work on screenshots and illustrations.

Your contribution has been evaluated according to [Utopian policies and guidelines](https://join.utopian.io/guidelines), as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, [click here](https://review.utopian.io/result/8/3-1-1-1-1-1-1-3-).

---- 
Need help? Chat with us on [Discord](https://discord.gg/uTyJkNm).

[[utopian-moderator]](https://join.utopian.io/)
👍  , , , , , , , , , , , , ,
properties (23)
authormcfarhat
permlinkre-igormuba-pt-3-ethereum-front-end-interacting-with-a-smart-contract-from-the-browser-part-3-20190410t220304063z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"links":["https://join.utopian.io/guidelines","https://review.utopian.io/result/8/3-1-1-1-1-1-1-3-","https://discord.gg/uTyJkNm","https://join.utopian.io/"],"app":"steemit/0.1"}
created2019-04-10 22:03:12
last_update2019-04-10 22:03:12
depth1
children1
last_payout2019-04-17 22:03:12
cashout_time1969-12-31 23:59:59
total_payout_value5.412 HBD
curator_payout_value1.693 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length686
author_reputation150,651,671,367,256
root_title"(PT 3) Ethereum Front End - Interacting With A Smart Contract From The Browser (Part 3)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id82,839,482
net_rshares11,796,361,651,730
author_curate_reward""
vote details (14)
@utopian-io ·
Thank you for your review, @mcfarhat! Keep up the good work!
properties (22)
authorutopian-io
permlinkre-re-igormuba-pt-3-ethereum-front-end-interacting-with-a-smart-contract-from-the-browser-part-3-20190410t220304063z-20190413t213517z
categoryutopian-io
json_metadata"{"app": "beem/0.20.17"}"
created2019-04-13 21:35:18
last_update2019-04-13 21:35:18
depth2
children0
last_payout2019-04-20 21:35:18
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length60
author_reputation152,955,367,999,756
root_title"(PT 3) Ethereum Front End - Interacting With A Smart Contract From The Browser (Part 3)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id83,041,904
net_rshares0
@steem-plus ·
SteemPlus upvote
Hi, @igormuba!

You just got a **0.27%** upvote from SteemPlus!
To get higher upvotes, earn more SteemPlus Points (SPP). On your Steemit wallet, check your SPP balance and click on "How to earn SPP?" to find out all the ways to earn.
If you're not using SteemPlus yet, please check our last posts in [here](https://steemit.com/@steem-plus) to see the many ways in which SteemPlus can improve your Steem experience on Steemit and Busy.
properties (22)
authorsteem-plus
permlinkpt-3-ethereum-front-end-interacting-with-a-smart-contract-from-the-browser-part-3---vote-steemplus
categoryutopian-io
json_metadata{}
created2019-04-11 20:55:54
last_update2019-04-11 20:55:54
depth1
children0
last_payout2019-04-18 20:55:54
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length435
author_reputation247,952,188,232,400
root_title"(PT 3) Ethereum Front End - Interacting With A Smart Contract From The Browser (Part 3)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id82,907,205
net_rshares0
@steem-ua ·
#### Hi @igormuba!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your post is eligible for our upvote, thanks to our collaboration with @utopian-io!
**Feel free to join our [@steem-ua Discord server](https://discord.gg/KpBNYGz)**
properties (22)
authorsteem-ua
permlinkre-pt-3-ethereum-front-end-interacting-with-a-smart-contract-from-the-browser-part-3-20190410t225134z
categoryutopian-io
json_metadata"{"app": "beem/0.20.19"}"
created2019-04-10 22:51:36
last_update2019-04-10 22:51:36
depth1
children0
last_payout2019-04-17 22:51:36
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length287
author_reputation23,214,230,978,060
root_title"(PT 3) Ethereum Front End - Interacting With A Smart Contract From The Browser (Part 3)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id82,841,914
net_rshares0
@utopian-io ·
Hey, @igormuba!

**Thanks for contributing on Utopian**.
We’re already looking forward to your next contribution!

**Get higher incentives and support Utopian.io!**
 Simply set @utopian.pay as a 5% (or higher) payout beneficiary on your contribution post (via [SteemPlus](https://chrome.google.com/webstore/detail/steemplus/mjbkjgcplmaneajhcbegoffkedeankaj?hl=en) or [Steeditor](https://steeditor.app)).

**Want to chat? Join us on Discord https://discord.gg/h52nFrV.**

<a href='https://steemconnect.com/sign/account-witness-vote?witness=utopian-io&approve=1'>Vote for Utopian Witness!</a>
properties (22)
authorutopian-io
permlinkre-pt-3-ethereum-front-end-interacting-with-a-smart-contract-from-the-browser-part-3-20190411t024943z
categoryutopian-io
json_metadata"{"app": "beem/0.20.17"}"
created2019-04-11 02:49:45
last_update2019-04-11 02:49:45
depth1
children0
last_payout2019-04-18 02:49:45
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length590
author_reputation152,955,367,999,756
root_title"(PT 3) Ethereum Front End - Interacting With A Smart Contract From The Browser (Part 3)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id82,852,346
net_rshares0