Your Very First Ethereum Smart Contract — A Full Walkthrough

As a product manager with an engineering — but not computer science — background, smart contracts on the blockchain is an incredibly exciting technology but simultaneously one difficult to easily experiment with. Unlike a JavaScript game that can run in the browser, or a simple Node.JS app which can run locally, interacting with a smart contract requires either running an Ethereum node and creating a private chain, or deploying to an active test chain or mainnet.

In this guide I’ll go through the basics of deploying and interacting with a simple smart contract including:

  • An overview of a simple smart contract
  • How to compile and deploy the contract
  • Interacting with the smart contract and saving information to the blockchain
  • Interacting with the smart contract and reading information from the blockchain
  • BONUS: Verifying the contract code for the public
  • BONUS: Using the Kovan testnet and getting test ETH to power your wallet

The Smart Contract

I chose to use this simple contract (courtesy of Loom Network via CryptoZombies) instead of a typical “Hello, World!” as it allows users to interact with data on the blockchain.

The smart contract in EthFiddle.

In brief, the contract has two functions, one which allows users to set their own lucky number (Solidity uses msg.sender to retrieve the address of the wallet interacting with the contract), and a second function which accepts any address as an input, and returns that address’s lucky number.

Compiling and Deploying the Contract

Now that we have a smart contract, we need to deploy it to the blockchain. For this walkthrough, I’ll be using the Deploy Contract functionality built into MyEtherWallet (MEW). I used the Kovan testnet, but the procedure is the same for the mainnet.

On MEW, navigate to the Contracts tab at the top, and click Deploy Contract. MEW will ask for the bytecode of the contract, as well as the gas limit.

How do we get the bytecode? Well, we need to compile the Solidity contract. To do so, I used Remix — a browser-based Ethereum IDE. We copy in the Solidity code, and Remix spits out the bytecode (you’ll need to click Details on the right-hand side, and use the object value of the bytecode).

Image for post
Image for post

We copy this into MEW, and are ready to deploy — you’ll notice the Gas Limit auto-populates based on the bytecode. The last step is to open the wallet that will deploy the contract. Like any transaction, deploying a smart contract costs gas. Luckily, since this is a simple contract, the gas cost is minimal. If you want to avoid using ETH while experimenting, review the section on receiving ETH on the Kovan testnet below. I unlock my wallet, and hit Deploy. The contract has now gone out as a transaction waiting to be mined. A few seconds later it is mined, and our transaction is live! We need to look at the transaction and find the contract to see at which address it has been deployed.

Saving Our Lucky Number to the Blockchain

Now that our contract is live, we can now interact with it — first by calling the setNumber function to store our lucky number. In MEW, we go back to the Contract tab, and input the contract address. Unfortunately, it’s asking for one more thing — the ABI. The ABI, Application Binary Interface, is basically how you call functions in a contract and get data back. An ABI is necessary so that you can specify which function in the contract to invoke, as well as get a guarantee that the function will return data in the format you are expecting. Luckily, it’s very easy to find out the contract’s ABI, we can go back to Remix and copy it in. Now, we hit the Access button, and…. voilà: MEW pulls up the two functions available to use, setNumber, and getNumber.

We’ll invoke the setNumber function first, setting our lucky number to 23. As we saw before, the contract will take the number 23, and set it against our address. We’ll unlock our wallet once again to sign the contract and pay the gas fee, and the transaction is sent off to be mined.

Image for post
Image for post

Reading from the Blockchain

Once our previous transaction is mined, we’re ready to invoke the second function getNumber. As you may recall, this function accepts an address as an input, and outputs that address’s lucky number. Back to MEW, we invoke the getNumber function, and use our own wallet address in the address field. We click Read and MEW spits back the answer in an instant: 23!

Image for post
Image for post

You’ll notice that we didn’t need to create a transaction to read the data, only when we wrote it. This is because once on the blockchain, any block explorer is able to read the data readily, whereas the setNumber function actually involves writing new data to the blockchain.

So there we have it — a live, working contract on the blockchain! Feel free to experiment with this contract or with your own! You can experiment with my instance of the contract which can be found here. Now that you understand the basics of deploying and interacting with a smart contract, you’re ready to start writing and deploying more advanced contracts! A great place to start learning about Solidity is with CryptoZombies — think Rails for Zombies meets CryptoKitties.

Bonus Sections:

As you may have noticed in our previous steps, you had to tell MEW the ABI for the contract before you were able to interact with it. But wait, isn’t the contract on the blockchain visible and thus viewable by anyone? Well — like most things — yes and no. When we deploy a contract, we deployed the compiled bytecode. This bytecode cannot be used by others to engineer the original contract, but much like hashing, given a contract you can easily verify that its compiled bytecode is the same.

That said, if you want others to interact with the contract, you need to share the ABI with them. Further, if you’re providing a product or service and you want users to trust you, you may need to display your full contract code so that it can be audited to make sure your contract isn’t doing anything malicious such as stealing funds.

Image for post
Image for post

EtherScan supports Contract Source verification, by which the contract creator shares the source. When you do, EtherScan compiles the contract to make sure it matches the contract deployed, and then shares the ABI for anyone to use.

Image for post
Image for post

Additionally, as we saw in the MEW getNumber example, EtherScan can read data from the contract without any transactions neeeded!

Kovan is one of Ethereum Proof of Authority testnet. As such, you wouldn’t mine for ETH as on the Ropsten testnet. Opening a wallet is as easy as going to MEW, selecting the Kovan network, and creating a wallet through the standard procedure.

Image for post
Image for post

Once you’ve done that, go to Kovan’s Gitter and ask the team to send ETH to your address. By default, only 5 ETH is sent to addresses (much more than the 0.005 ETH I needed for this contract) to prevent abuse.

Use my referral link on Coinbase to claim $10 in free Bitcoin when you buy your first $100 in any cryptocurrency using the link here.

Product Manager, Technology Enthusiast, Human Being; Contributor to Towards Data Science, PS I Love You, The Startup, and more. Check out my pub Life with Barry

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store