This video is available to students only

How to Call An Ethereum Smart Contract Code From Javascript

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

Now that we have our contract on the blockchain, we can start using it.

To do this, we're going to make a JavaScript object that acts as a proxy to our smart contract. Remember that our contract code doesn't describe, it's own API, so we need to use our ABI file.

The process to get a handle of our contract instance, feels a little odd coming from JavaScript. It looks like this:

  • First we'll parse our ABI into a JSON data structure
  • Second we'll create a contract "class" using that ABI and
  • Third we'll instantiate that class using our address

It's probably easier to just show you in code.

# First we're going to copy our abi file we compiled earlier
cat counter_sol_Counter.abi
cat counter_sol_Counter.abi | pbcopy

Next we'll go back to geth and parse the string with JSON.parse:

var abi = JSON.parse('[{"constant":...';

// take a look

// Next we're going to create, essentially, a JavaScript "class" for our contract
var Counter = eth.contract(abi);

// And finally, we need to tell our code _where_ to find this particular instance of the Counter contract on the blockchain
// So we type ...
var counter =

// looking at counter, we can see we have our address, and our functions, get and increment.

Now we have an handle to our contract instance in JavaScript! This means we can now interact with our contract's code like a JavaScript object. Sort of.


This page is a preview of Intro to Programming Ethereum ĐApps

Start a new discussion. All notification go to the author.