Lesson 4

Running locally

2

Writing simple smart contracts in Remix is fine, but for more serious projects, you'll want to have a full Ethereum environment set up on your computer. Let's install everything we need for that.

Node & NPM

If you come from a web development background, you're probably already familiar with the Node.js ecosystem. The Node environment and its package manager NPM will allow you to download everything else that's related to Ethereum.


If you're not sure whether or not you have Node and NPM installed on your computer, try typing node -v or npm -v in your terminal.


Don't worry if your version numbers differ a bit from mine.


If you don't see version numbers at all, then you need to install Node. If you're on a Mac, I recommend using homebrew and run the following command (if you're using another OS on the other hand, just follow the instructions on the official website).

We're going to use Node.js quite extensively while developing in Ethereum so if your JavaScript skills are a bit rusty, I suggest you pause to get a quick refresher.

Get a blockchain client

To communicate with the Ethereum blockchain, you will need to install a client that can connect to it. There are many choices out there written in different languages, but Geth is generally considered to be the official, best-maintained implementation.


However, while developing, it would be wasteful to download the entire Ethereum blockchain and then be forced to pay real money (ether) to interact with our smart contracts. That's why we'll instead use a blockchain simulator client, called TestRPC. You can think of it as similar to a localhost server, it's a private blockchain that only runs on your computer.


Run the following command to install TestRPC globally on your computer:

Once it's installed, you can check if it works by simply running the global testrpc command in a new terminal window:



If you see something similar to the above screenshot, congratulations! You are now running a local Ethereum node with some (fake) wallets that you can send money from. We'll see how it works in more detail soon. For now, just keep this terminal window open in the background.

Creating your project

We're now going to re-create the "HelloWorld" project from last chapter in a local environment. Go ahead and create a new folder called hello-solidity anywhere on your computer, navigate to that folder in your terminal and initialise a new NPM project from it:


NPM will guide you through a list of configurations. You can just keep pressing enter until it's done.


Inside the hello-solidity folder, create a new file called HelloWorld.sol and open it in your favourite text editor.


To make it easier to read, I strongly recommend using a syntax highlighter plugin for Solidity. There are some great ones for Atom, Sublime Text and Vim for example.


In case you had forgotten, this is what should go into your Solidity file (the same code that we wrote in the last chapter):

HelloWorld.sol

Next, we're going to create an index.js file in the same folder, which is where we'll write all of our Node.js code.

Compiling our Solidity program

In our index.js file, we want to start by just importing the HelloWorld.sol code and compile it. For that we're going to need a Node-based Solidity compiler called solc.


To install solc for your project, run the following NPM command in the same folder:

Once it's done installing, you can start coding your index.js file:

index.js

Now go back to the terminal and run node index.js to see the result!


What the compiler output looks like.


Install web3

The next and most important dependency that we're going to need is web3.js. Web3 is a JavaScript API that sends requests to the Ethereum blockchain (either the real one or your local one). It acts as the middleman between your frontend and your smart contracts.



To install Web3 for your project, run the following NPM command in the same folder:

We're now going to use Web3 to connect to our local Ethereum node (you're still running the testrpc command in the background, right?).


When starting testrpc, you should've seen the message Listening on localhost:8545. Using the Web3 library, we can use the concept of a provider to connect to that network.

index.js

You can remove the console.log function that we added earlier at this point.

Deploying our contract

Now that our file can connect to the (local) Ethereum network via web3, we want to upload our newly-created contract to an address on the network.


In order to do that, we first need to use web3 to create a Contract object out of our compiled code. This will make it much easier to interact with our contract using JavaScript.

index.js


If you run the program again with "node index.js", you can see what our contract object looks like.


Now, in order to deploy this contract to an address, we need to use gas and spend some ether by following these three steps:

  1. Get an address from our test wallet.

  2. Get the current gas price

  3. Spend some (fake) ether from our address to deploy our HelloWorld contract to a new random address.

  4. Get the deployed contract instance and call its getMyName function.

Since a lot of web3's API is promise-based, we're going to use Node's async/await functionality to make the code more readable (so make sure that your Node version is 7.6 or higher)!

index.js

In order to ensure that we spend enough gas to get our contract deployed, we can just choose an arbitrary high number, like one million (1,000,0001,000,000) gas.


This is what your final codebase should look like:

index.js

If you run this code one last time with node index.js, you should finally get the satisfactory result that you've been waiting for:


It says "Tristan" – the contract works!


What you've done might not seem very remarkable, but it's actually pretty cool. You have:

  1. Created an compiled your first Solidity contract.

  2. Deployed the contract to your (local) Ethereum network

  3. Called one of the functions of the uploaded contract in order to read data stored on the network

If this contract was on the real Ethereum network, anyone would be able to call getMyName from their computer and see the value "Tristan". Pretty cool, huh?


In the next chapter, we'll use the well-known Truffle framework to see how we can automate many of the steps that we had to write code for now.

Comments

profile/avatar/default
Yahya

can you please update the link? it seems that this link is not working anymore! thanks

Tristan Edwards

@yahya: Ah, seems like the website doesn't exist anymore. Luckily you can still find the contents of it on its GitHub repo.

profile/avatar/default
Kmatth 007

This code does not work! I updated the latest version of node to version 10. When I run it says contract deploy is not a function and callback is not a function.

profile/avatar/default
Kmatth 007

It has a problem with this snippet of code..... try { const contractInstance = await contract.deploy({ data: bytecode }).send({ from: addresses[0], gas: 1000000, gasPrice, });......I would like to order the full version of the course.

profile/avatar/default
Yahya

at line 25: if you say something more about the methods available for the contractInstance or may be you can a put a reference for further study about that. Thanks

profile/avatar/default
Yahya

at line 17: contract.deploy is from solc? can you please say a few words about this? thanks