Blockchain Stories
Published in

Blockchain Stories

The Story Of An Ethereum Smart Contract

By Sumi Maria Abraham, Research and Development Engineer Kerala Blockchain Academy

In 2008, when Satoshi Nakamoto introduced bitcoin- the decentralized currency, it raised some red flags. It experienced some significant highs and lows before settling itself in the seat of a preeminent cryptocurrency in the world. Now 13 years old, bitcoin is the most popular and lucrative digital currency. All thanks to its supply and demand economics. The success led to the evolution of many new cryptocurrencies and products, making bitcoin supercilious. With time, people began experimenting with the underlying technology of bitcoin- the blockchain, for many different purposes.

Ethereum is one such kind of thought that eagerly utilizes blockchain technology to maintain decentralized payment networks and store computer code that powers tamper-proof decentralized financial contracts and applications.

The credit goes to Vitalik for his deep sense of understanding of the blockchain technology. Vitalik proposed the concept of a programmable blockchain that can be tailored to the need. He coined the fond idea of decentralized computer applications ( DApps ) that run on a blockchain network of computers.

How can computer applications be recorded in blockchain?

Let us examine the features needed if we want to include programmability in the blockchain.

The distributed ledger should be equipped to record computer programs. Whenever a user executes a DApp, the execution should happen at every peer/computer holding the ledger copy. The nodes (network participants holding the blockchain copy) should have an execution environment to execute the DApp. The system should have a mechanism to uniquely identify a program, its associated data and its executions. Finally, these changes should be recorded as transactions on the chain.

What makes Ethereum programmable?

The secret is that the Ethereum blockchain institutes a decentralized world computer. It allows the deployment of computer programs called smart contracts onto the blockchain. Smart contracts run on an emulated computer called the Ethereum Virtual Machine (EVM). Each node on the Ethereum network runs a local copy of the EVM to validate contract execution. At the same time, the Ethereum blockchain records the changing state of this world computer as it processes transactions and smart contracts. The EVM operates like a global, single-instance computer, running everywhere.

Each smart contract is uniquely identified by its 20-byte contract address and has a smart contract program code. But a contract account does not have a private key. Instead, it is owned and controlled by the logic of its smart contract program code that is recorded on the Ethereum blockchain at the contract account’s creation and executed by the EVM. Therefore, no one can steal the funds of a smart contract since it does not have a private key to be stolen. Contract funds are accessed only by programming the transfers in the smart contract code, which is publicly auditable. On the other hand, Externally Owned Accounts or EOAs are controlled by users using their private keys. They are often managed by wallet applications external to the Ethereum platform.

Smart Contract

Cryptographer Nick Szabo coined the term Smart Contract in the 1990s. In the Ethereum context, the smart contracts refer to computer programs deployed on the blockchain. They usually define the basic logic of an application. The word contract has no legal meaning in this context.

The Smart contract deployment is also a blockchain transaction. It is immutable. So once you deploy a smart contract on the blockchain, you cannot apply any changes to the code. It must be deployed as a new contract if any changes are required.This makes the smart contract design and deployment a little risky.

Smart contracts are written in high-level languages like Solidity, Vyper, Yul etc. Below is an example of a smart contract written in Solidity.

MyContract is a simple smart contract storing a data item named message on the Ethereum blockchain. It allows Ethereum users to read(getMessage function) the message data and assign data to it(setMessage function).

What’s the cost of deploying your smart contract ?

Smart contracts have a unique fee structure. Apart from paying for the computational resources, transaction fees here have some extra functionalities. Ethereum world computer allows anonymous users to deploy smart contracts on the Ethereum blockchain. But it should have some mechanism to ensure that these smart contracts are terminable and do not exhaust the computational resources of the Ethereum ecosystem.

To ensure this, Ethereum introduces a metering mechanism called gas. As the EVM executes a smart contract, it carefully accounts for every instruction (computation, data access, etc.). Each instruction has a predetermined cost in units of gas. When a transaction triggers the execution of a smart contract, it must include an amount of gas that sets the upper limit of what can be consumed running the smart contract. The EVM will terminate execution if the amount of gas consumed by computation exceeds the gas available in the transaction. Gas is the mechanism Ethereum uses to allow Turing-complete computation while limiting the resources that any program can consume.[2]

Smart Contract Deployment

Let us see how to deploy our MyContract on the Ethereum blockchain.

Our smart contract is written in Solidity, a high-level language. The Solidity compiler (solc) converts the smart contract to EVM bytecode as the Ethereum Virtual Machine only executes byte code representation.

The compiler also generates an Application Binary Interface (ABI). ABI serves as an interface between two program modules. It defines how data structures and functions are accessed in machine code.

If the compilation is successful, the contract can be deployed using a particular contract deployment transaction. Once the transaction is included in a block, the contract can be referred to by its address. The address of a smart contract is calculated as a hash function of the originating account and account nonce (number of transactions originated from an account).

An Ethereum user who wants to interact with a smart contract can use the contract address and ABI to interact with it anytime.

MyContract is deployed in Ropsten testnet at contract address 0x0dFA0638dd508bFb4E13A06359FAF666677D7F4d. You can check the contract status at the block explorer.

Conclusion

The world is quickly transforming into a sci-fi drama! With increasing adoption and confidence rate in blockchain technology, smart contracts will very soon be far more mainstream. The programmability feature will be active as long as the blockchain network remains live. Inscribed to an immutable log, no changes are edible in smart contract giving a notion of safe conduct.

References

[1] Wood, G., Ethereum: A secure decentralized generalized transaction ledger.

[2] Andreas M Antonopoulos; Gavin Wood, Dr., Mastering Ethereum: building smart contracts and DApps

--

--

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
Kerala Blockchain Academy

Kerala Blockchain Academy

One-stop solution for quality blockchain education and research. Offers best in class blockchain certification programs in multiple blockchain domains.