Ethereum Smart Contract Teardown

Niharika Singh
TryCrypto (now Decentology)
10 min readApr 24, 2019


If only contracts were smarter, the world would be a much better place, said no one ever. Yet, here we are inundated with references to blockchain smart contracts in every other technical article (yes, I get the Morissette-grade irony). Most of us have an intuitive sense of how smart contracts work, but it’s challenging to separate hype from reality. What you need is a smart contract de-hyping superpower. And that is exactly what you will have in about 30 minutes as we do a hands-on teardown of a simple smart contract. Let’s go!

The concept of smart contracts dates back to 1994. Nick Szabo, a computer scientist, and a cryptographer is the brain behind smart contracts. (For those of you who don’t know: he’s very active in the blockchain space. You can find him on Twitter- @NickSzabo4.) If you wish to read Szabo’s paper on smart contracts, click here. In his own words:

“A smart contract is a set of promises, specified in digital form, including protocols within which the parties perform on these promises.”

However, the philosophy couldn’t be put into action back then due to lack of know-how and infrastructure. About 14 years later, in 2008, we saw the rise of the Bitcoin blockchain which employed a single smart contract. The contract’s logic was to implement P2P (peer-to-peer) digital payments. Today, the Bitcoin blockchain network happens to be one of the most secure ways to transact value (bitcoin) in a P2P fashion.

About five years later, smart contracts got married to the Ethereum blockchain. Oh, a match made in heaven! Smart contracts make the Ethereum blockchain use-case agnostic. Anyone can deploy any logic (smart contract) to the blockchain and anybody can use it.

This brings infinite autonomy to developers where N use-cases can be deployed on a single blockchain ranging from P2P payments, to supply-chain management, to healthcare, to digital identity management, and beyond! You can check out more use cases on our website.

In this article, we will compose and teardown a simple smart contract to learn the core concepts behind Ethereum smart contracts.


  • Basic knowledge about blockchain technology
  • Minimal understanding of programming concepts
  • Motivation!

This article is best enjoyed with chilled iced-tea.

Let’s get started!

Ethereum is a programmable blockchain. This means that Ethereum does not constrain developers to a finite set of operations. Instead, it gives full freedom to code whatever they wish, regardless of the complexity.

Ethereum smart contracts run on the Ethereum Virtual Machine (EVM). This virtual machine is Turing-complete, which means that any computational problem with any degree of complexity can be run on this machine.

This freedom comes at a price.

Deploying a smart contract and interaction with it requires payment of a fee, formally known as “gas.”

To write smart contracts, you need a Turing-complete language. The most common Turing-complete language to write Ethereum smart contracts is Solidity. It is a high-level language whose syntax is very similar to that of JavaScript.

Let’s write a minimalistic smart contract that adds two numbers in Solidity.

Step 1: Open your browser to

Remix is an open source and cloud-based Integrated Development Environment (IDE) that helps you write, compile, and deploy smart contracts in a high-level, Turing-complete language.

This is what your screen should look like:

Point your browser to You should see something like this.

Step 2: Write and compile the smart contract

Click on the + icon located on the top-left of the screen. This will open a new file in the IDE. You can name it Add.sol. We will define our smart contract in this file.

Note 1: Solidity files end with the extension .sol

pragma solidity ^0.5.7;contract Addition {    uint public sum = 0;    function add (uint x, uint y) public {        sum = x + y;    }}

Pragma is a solidity keyword. In the code above, it is used to specify which versions of solidity compiler can be used.

^0.5.7 means that we would be using compiler whose version is 0.5.7 or greater.

The name of our smart contract is Addition.

The rest of the code is very similar to pretty much any other programming language.

Now that you’ve written the code in the Remix IDE, click Start to compile. You may use any compiler with version 0.5.x.

Note 2: I used 0.5.8-nightly.2019.4.12+commit.31abeb99 compiler version.

Note 3: While coding, always keep in mind that Ethereum is a public blockchain and all code and data is always public. Functions or variables scoped “private” refer to visibility during program execution. Any data that you’d like to keep private should be encrypted off-chain.

Step 3: Deploying the Smart Contract

Go to the Run tab and set the environment to JavaScript VM.

Note 4: JavaScript VM is a blockchain sandbox environment present inside the browser in the current page context. A side-effect of this is that once you reload the page, the state of the sandbox will be reset.

Writing the smart contract.

Notice that our Ethereum account is pre-filled with 100 test ETH. Since we are developing in a sandbox, there is no connection to the Ethereum main or test networks and all funds are imaginary.

Click on the Deploy button.

Smart contract successfully deployed.

Now, you should see that the account balance has been reduced to 99.9999 and under Transactions Recorded, there is one transaction. This refers to the smart contract we deployed. Under Deployed Contracts, you should see the name of our contract with the address where it has been deployed on the JavaScript VM.

In the logs, you should be able to see a table with all the important metadata.

Step 4: Interaction with the Smart Contract

Click on Addition situated under Deployed Contracts and provide two non-negative numbers.

Interacting with the smart contract.
Providing input parameters to the smart contract.

Now click on the reddish button add. This will log another transaction on the blockchain. Notice that the number of Transactions Recorded has been increased to 2. This operation will also consume our test ETH because we use the EVM to calculate the sum of two numbers, 5 and 90. Whenever we want the EVM to execute our code, we need to pay. Now to view the total, click on the purple sum button. This will not cost any ETH because we are reading from the blockchain.

Note 5: Under Deployed Contracts, all functions declared in the smart contract appear red and all variables present in the smart contract appear purple. By clicking the variable button, you can view its value at any time.

Keep in mind that only writes to the blockchain cost ETH. Reads are free.

Viewing result calculated by the smart contract.

Now let’s go back to the Compile tab and click on Details. These Details might look overwhelming at first, but don’t worry! We will go through it step-by-step.


Name of the smart contract under ‘Details’.

This one’s easy. It is the name we gave to our smart contract.


Metadata of the smart contract under ‘Details’.

This is the metadata related to our smart contract Addition.

It states which compiler version we used to compile our smart contract, which language we used, ABI (Application Binary Interface) is used to communicate with the moving parts of a smart contract like functions, variables data structures, etc, devdoc refers to developer documentation of the contract (we didn’t have any), userdoc refers to user documentation of the contract (we didn’t have this either), settings holds data about internal solidity compiler configuration, sources refer to the file names of our smart contracts which are hashed using the keccak256 (erroneously called SHA3) hash function. The hash generated is stored on Swarm (Swarm is a decentralized storage platform. It is a part of Ethereum’s Web 3 stack).

The input space of the keccak256 hash function is infinite and the output generated is a 32-byte hash. If you want to try it out, you can check out this online Keccak256 hash generator. One of the visible applications of the Keccak256 hash functions is to generate raw hexadecimal Ethereum wallet addresses. The wallet addresses uniquely identify each wallet and are derived from the public key using Keccak256 hash function.


Bytecode of the smart contract under ‘Details’.

Since the smart contract is written in a high level language like solidity, the EVM cannot interpret it directly. This code is compiled into a compressed low level bytecode that EVM understands. If you have developed for the Java Virtual Machine or .NET, this concept will be a familiar one.

When we say that a smart contract has been deployed, we technically mean that the bytecode has been deployed. Once the bytecode is deployed on the blockchain, it becomes immutable.

Within bytecode, linkReferences refers to the deployed address of other smart contracts on which the current smart contract has a dependency. Object is our bytecode. It is written in hexadecimal format. Opcodes are operation codes that are human-readable machine level instructions. It is also possible to write smart contracts in opcodes rather than solidity to save resources and do things that solidity can’t do, but it’s not very feasible. The non-blockchain analogy of this is assembly language. SourceMap comes in handy while debugging the smart contract.

ABI (Application Binary Interface)

ABI of the smart contract under ‘Details’.

The ABI enables interaction with the smart contract. In simple terms, it provides the types, function definitions and data structures necessary for a calling entity such as Web3js to interact with the smart contract.


Web3 Deploy of the smart contract under ‘Details’.

This code is auto-generated by the Remix IDE for use by applications that use the Web3.js library for deploying smart contracts to an Ethereum blockchain (for instance, a private ethereum blockchain).


Metadata hash of the smart contract under ‘Details’.

This is the hash of the metadata.


Swarm location of the smart contract under ‘Details’.

Since Remix uses the Swarm protocol for decentralized storage, this is the URL to the metadata of our smart contract. In order to view the file, you have to use a public Swarm gateway or launch the Swarm daemon on your laptop.


Function hashes of the smart contract under ‘Details’.

Each function we write in our smart contract is hashed uniquely.


Gas estimates of the smart contract under ‘Details’.

These gas estimates are in gwei which is a unit of ETH. For more information about the various ETH units click here. The cost to deploy our smart contract on the blockchain is 49,200 gwei (i.e. about 0.0000492 ETH). Gas estimates depend on the complexity of our smart contract and the state of the blockchain; the more efficiently the contract is coded, the less it will cost to deploy the code. The smart contract used to push our smart contract on the blockchain cost us about 5,105 gwei. So the total cost is the sum of the two.

Our add(int256,int256) function and sum() function would cost about 20,288 gwei and 394 gwei respectively. This number might change in the time between submission and execution of the transaction, hence the term “estimates”.


Runtime bytecode of the smart contract under ‘Details’.

Bytecode = Deployment Bytecode + Runtime Bytecode.

Deployment Bytecode contains instructions saying “create a new address, copy all this runtime bytecode there, done”. The runtime bytecode is the code that is actually placed on the Ethereum blockchain.


Assembly of the smart contract under ‘Details’.

This is the assembly language code our smart contract follows. You can calculate the cost of execution of the smart contract by analyzing all assembly level codes. Refer to this spreadsheet to help calculate the cost.

The amount of gas consumed by a smart contract is contingent upon two factors:

  1. Gas limit: This is pre-defined based on opcodes.
  2. Gas price: We fix this on our own. If we want our transaction to get committed quickly, then we pay a higher gas price.

Gas Estimate = Gas limit * Gas price


If we underpay, then our transaction may not be picked up by a miner and our contract may not get deployed. In addition, the spent gas wouldn’t be refunded (you’d have to pay all over again). To carefully set gas price, you can also use the Web3 gas estimate API — web3.eth.estimateGas(callObject [, callback]). Check it out here.

To summarize, there are three key steps to writing a smart contract for the Ethereum blockchain.

  • Write a smart contract in a Turing-complete language supported by the EVM
  • Compile it into bytecode using any EVM compiler
  • Deploy it to the Ethereum blockchain network