Watch this video free by signing up

Calling Deployed Smart Contract Code

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`:

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

// take a look
abi

// 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 = Counter.at(contractAddr)

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.

Remember that all reads can be performed locally, for free, as long as they don’t change any state. Let’s try our read function get.

 

Subscribe to get the full text of this lesson

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