Running locally - Discover Ethereum & Solidity | Ludu

Running locally

by Tristan Edwards Tristan Edwards

2 thanks

  • 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).

    +
  • Loading code...
    +
  • 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.

    2
    Tristan Edwards
  • 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:

    +
  • Loading code...
    +
  • 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:

    +
  • Loading code...
    +
  • 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):

    +
  • Loading code...
    +
  • 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:

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

    +
  • Loading code...
    +
  • 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:

    +
  • Loading code...
    +
  • 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.

    +
  • Loading code...
    +
  • 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.

    +
  • Loading code...
    +
  • 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. 1

      Get an address from our test wallet.

    2. 2

      Get the current gas price

    3. 3

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

    4. 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)!

    +
  • Loading code...
    4
  • 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:

    +
  • Loading code...
    +
  • 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. 1

      Created an compiled your first Solidity contract.

    2. 2

      Deployed the contract to your (local) Ethereum network

    3. 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.

    +

Did it help you? Click to say "thanks" to the author!

Next lesson: The Truffle framework

Want to create your own course on Ludu?

Back to lesson