Build Your First Smart Contract

A Tutorial for Beginners

Gerald Nash
Crypto Currently
9 min readDec 7, 2017

--

If you’ve seen technology news lately, you’ve likely noticed that Bitcoin and other cryptocurrencies have skyrocketed in price. In just 2017 alone, Bitcoin has seen a 2035.3% increase in price at the time of this writing. And, because many coins in the cryptocurrency market are highly correlated with Bitcoin, currencies such as Ethereum and Litecoin have seen equally spectacular jumps in price. Ethereum, for example, has seen a 6365.3% increase and Litecoin a 2775.49% increase.

We know that Bitcoin is the king of crypto as of today, but its primary use at the moment is solely that of a currency. This is great for day-to-day payments, but we want more functionality; we want the ability to write software that will run ontop of a large, decentralized network like Bitcoin’s. To do this, we could create Bitcoin smart contracts, but Bitcoin’s smart contract language is limited and not very extensible.

To get our smart contracts running on a vast decentralized network, we’ll create smart contracts for the Ethereum blockchain. Ethereum is special, because it’s the first blockchain implementation to have a Turing Complete virtual machine built ontop of it. This means that an Ethereum smart contract can (theoretically) be used to accomplish any computational task. In more simpler terms, nearly any program can be run on of Ethereum.

In this post, we’ll walk through making a simple counter smart contract. The contract will be written in a programming language called Solidity, a language similar to JavaScript. Before we get started, you must have Ganache installed, which will be the tool we’ll use to create a private blockchain that runs on our machine only. You’ll also need to download your own copy of MyEtherWallet (the URL-hosted website won’t work in this case). Note: Download etherwallet-v3.xx.x.x.zip.

Once those are installed, we’ll get started by going to https://remix.ethereum.org/ in our browser, where we’ll see the following screen.

Remix IDE

This is Remix, an online compiler for Solidity. It’s what we’ll use to write our smart contract code. When you first visit the page, the text editor is preloaded with some code. But, we don’t need what’s given, so we’ll erase all of that text and replace it with the following.

Counter Code (in Solidity)

This is the code for our counter. As you can see, it has one variable and three functions. The variable, count , is an integer that’s private, which means that it can’t be accessed by anyone outside of the contract itself. The first function, incrementCounter() , changes, or mutates, the value of count by incrementing its value. The second function, decrementCounter() , mutates the value of count by decrementing its value. And, the third function, getCount() , accesses count and returns its value to whoever or whatever called the function.

When the counter code is pasted into Remix, it should look like the following and automatically compile.

Remix with Contract Code

We’ll leave that tab open so that we can return to it later. Now, open Ganache and you’ll see something like this.

Near the top of the screen, you can see text that says “RPC SERVER”. We’ll need it soon.

Now, unzip your MyEtherWallet download and open the folder. Then, open the index.html file in your browser to see the following screen.

In the top right corner, you can see a dropdown that tells MyEtherWallet what Ethereum network to connect to. By default, it connects to the Ethereum (ETH) main network (mainnet). We want to change this by clicking the dropdown.

Click “Add Custom Node”.

Now you can input the RPC Server information that Ganache gave us earlier. Note: You can name your node whatever you’d like.

MyEtherWallet is now connected to your self-hosted blockchain through Ganache. Let’s use MyEtherWallet to upload our Counter smart contract to our blockchain. To do this, we’ll click “Contracts” in MyEtherWallet’s top navigation bar and select “Deploy Contract”.

As you can see, MyEtherWallet asks us for the contract’s byte code. To locate this, we’ll go back to our Remix IDE and click the “Details” button.

You should now see a dialog with a lot of information about our Counter contract.

To copy the byte code, we’ll click the clipboard icon next to “BYTECODE”.

Then, we’ll go back to MyEtherWallet and paste the byte code into the dialog.

Now we can scroll down and import an account to upload the contract with. Luckily, Ganache gave us 5 addresses that we can use to interact with our private blockchain. To use one of them to upload this contract, we can go back to Ganache and click the key icon for any of the addresses.

It’ll show us the private key associated with this account.

This money is fake, so I’m comfortable showing the private key, but always be careful!

We can copy this private key and paste it into MyEtherWallet.

Now if we click “Unlock”, MyEtherWallet will ask us if we want to sign this transaction and deploy our contract. We do.

Click Yes!

If the transaction occurred successfully, then Ganache will increment its “Current Block” value and the transaction count of the account that we used to deploy the contract also increment.

Our contract is now uploaded to our blockchain! To interact with it by incrementing and decrementing the counter, we can go back to MyEtherWallet and select “Interact With Contract”.

MyEtherWallet now asks for the address at which our newly deployed contract resides and the Application Binary Interface (ABI) of our contract. To find the contract address, we can go back to Ganache and view our transactions log.

This page shows us the transaction that we’d created earlier when we deployed our contract. As you can see, Ganache tells us the address we’d used to deploy the contract, the address of the contract on our blockchain, and more information about the transaction. Let’s click the transaction, copy the created contract address, and paste it into MyEtherWallet.

All that’s left that we need is the ABI. This is what tells MyEtherWallet how to interact with our contract. To get it, we’ll go back to Remix and click the clipboard icon next to “INTERFACE - ABI” to copy it.

Now we can go back to MyEtherWallet, paste the ABI into its text box, and click the “Access” button.

Awesome! Now we can interact with our contract by clicking the “Select a function” dropdown.

In our code, we set count ‘s initial value to 0. To confirm that the contract is working properly, let’s call the getCount() function.

It worked! Our contract returned 0 when getting the value of count before changing it. But, we also made two other functions, incrementCounter() and decrementCounter() . Let’s call incrementCounter() to test it. We’ll do this by selecting the function dropdown again, selecting incrementCounter , and creating a new transaction.

This just incremented the value of count . Now we can call getCount() again to confirm whether the value actually changed.

As you can see, count is now equal to 1! So, our incrementCount() function works. You are free to test decrementCount() , yourself, and explore even more!

Throughout this post, we launched a blockchain on our own machine, deployed a smart contract to it, and interacted with the contract. This is very similar to the workflow of professional Ethereum smart contract developers, albeit a little different. This process works extremely well during early development of smart contracts, but what happens if you want others to interact with your smart contract without using real money by deploying it to mainnet? In that case, you can upload your contract to the Ethereum test network, or testnet, so that the contract is copied onto every machine in Ethereum’s global network but users can still use fake money to interact with it. In a later post, I’ll cover how to deploy smart contracts to testnet using a similar process to the one mentioned in this post.

Thank you very much for reading this article! Please feel free to follow Crypto Currently on Medium and Twitter, and follow me on Medium, Twitter, and Github to stay tuned for more content. Also, if you have any questions, don’t be afraid to reply below this post, DM me on Twitter, or shoot me an email.

--

--