Watch this video free by signing up

Compiling Solidity Code

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

Creating .bin

To compile our Solidity smart contract, we need a compiler. The main solidity compiler is solc.

https://github.com/ethereum/solidity

It’s written in C++ and it’s a good idea to use a formal release build for your production smart contracts.

That said, getting the C++ version of solc installed can sometimes be a pain. My recommendation is that you install solc before you deploy your first contracts on the production network, but for now, we can use the JavaScript version solcjs.

# this installs solcjs (not the c++ version of solc)
npm install -g solc

Solcjs’s core code is generated using Emscripten to convert the C++ version of solc to JavaScript. This means it will work just fine, but know that the command-line options are different.

We can compile our contract by typing this:

solcjs --bin counter.sol

And let’s take a look at the output:

ls
cat counter_sol_Counter.bin

Well, that’s not exactly enlightening, but this is a hex encoded version of the bytecode of our script. There are other ways of viewing the opcodes that are clearer, but we don’t need them right now.

Deploying in Geth

Start geth like did in the earlier video:

./bin/geth --datadir=./datadir console

Now we’re in a JavaScript console, so we can declare variables, functions, etc. As a reminder, in an earlier video we created some accounts and mined some eth. We’ll need that now, so go back to that video if you haven’t watched it already.

The first thing that we need to do is assign our contract hex code to a variable. It will help if you have two tabs open. I have geth console in one, and a shell in the other.

 

Subscribe to get the full text of this lesson

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