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.
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).
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:
hello-solidity folder, create a new file called
HelloWorld.sol and open it in your favourite text editor.
In case you had forgotten, this is what should go into your Solidity file (the same code that we wrote in the last chapter):
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
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
Now go back to the terminal and run
node index.js to see the result!
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.
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.
Now, in order to deploy this contract to an address, we need to use gas and spend some ether by following these three steps:
Get an address from our test wallet.
Get the current gas price
Spend some (fake) ether from our address to deploy our HelloWorld contract to a new random address.
Get the deployed contract instance and call its
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 () gas.
This is what your final codebase should look like:
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:
What you've done might not seem very remarkable, but it's actually pretty cool. You have:
Created an compiled your first Solidity contract.
Deployed the contract to your (local) Ethereum network
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.