Watch this video free by signing up

Reading Values from Ethereum in JavaScript with Web3

In this video, we’ll write our first Solidity smart contract

Now that we know we have a connection between our browser and our Ethereum node Let’s make a connection to our contract

Remember that in order to create a JavaScript representation of our contract, we need to load the abi

Let’s copy the abi that we compiled earlier into this project. Only I’m going to rename it to counter_sol_Counter_abi.json

In the code, we can require this .json file – this is possible because of the nwb config, if you’re building using another method you could always just copy and paste the abi here in the code t counterAbi = require(‘./counter_sol_Counter_abi.json’);

We can log it out and make sure it loaded. Great nsole.log(counterAbi);

Also remember that our particular instance of the counter contract lives at a specific address on the blockchain. I hope you saved the contract address from last time.

let contractAddr = '0x4575e7fcf12a110060ebc1e3b2b85706ddfbc97e';

This is an important point – when you deploy your website it lives on a specific domain name. When you deploy your contracts, they’ll live at a specific address. So you’ll have these addresses hardcoded (or at least in a config file) committed into your code

Now that we have our ABI and address, we can create a new contract object

var counterContract = new web3.eth.Contract(
  counterAbi,
  contractAddr
);

Now we want to mutate our contract’s state by incrementing the counter. To do this, we’ll need to send a transaction to the blockchain.

Count counterContract object, has functions that help us build this transaction. It looks like this:

 

Subscribe to get the full text of this lesson

and the entire library of videos, sample code, and tutorials.