in

How to Write Your First Ethereum Smart Contract

Smart contracts are integral in the functioning of Ethereum as a ‘programmable blockchain’.

These contracts are lines of code that ‘self-execute’ upon fulfilling a specific set of conditions.

Self-execute means executing without any third-party interference (as in on a blockchain).

Vitalik Buterin introduced the idea in the official Ethereum whitepaper in 2013. And, after 2015, independent developers started leveraging smart contracts to create their own tokens and crypto-assets.

Consequentially it led to 2017’s ICO boom. Three years later, writing smart contracts is still a valuable coding skill.

You may be interested in smart contract programming just as a hobbyist, to understand Ethereum in all its beauty.

Or you may be looking to build a career as a professional blockchain developer. If it’s the latter, then you should make it a point to learn smart contract writing.

Especially with Solidity, Ethereum’s native programming language.

Overview of Smart Contracts, Source: DevTeam.Space

Overview of Smart Contracts, Source: DevTeam.Space

But before delving deeper to ‘solidify’ your ‘smart contracting’ skills, let’s take a look at a bit of history and working of smart contracts.

History of Smart Contracts

As per a Harvard Law School forum article, computer scientist and cryptographer Nick Szabo first mentioned the term ‘smart contract’ around 20 years ago.

At that time he was a graduate student at the University of Washington.

Smart contracts also find mention in the Ricardian Contracts theory, an idea postulated by Ian Grigg and Gary Howland in 1996.

The Ricardian Contract Model

The Ricardian Contract Model, Source: Ian Grigg, Systemics Inc.

Ricardian Contracts were to act as a bridge between the usual text contracts and computer code.

Despite such notable mentions by such eminent computer scientists, smart contracts didn’t find takers until recently.

And due to their surging popularity, it makes sense to understand their working.

How do Smart Contracts Work?

Before discussing the technical aspects of how smart contracts work, let’s first look at an example.

Rajesh is a paper pulp processing factory owner in Noida. Naresh is a carton box manufacturer based in Surat. Rajesh and Naresh share a great business relationship. They trust each other and have been working with each other for quite a long time now.

Now imagine a scenario, where Naresh has a contract set up for placing orders to Rajesh for the raw material. Whenever the paper stock runs out at Naresh’s end, the system automatically raises an alert to Rajesh with more orders.

Rajesh fulfills the demand and collects the payment for the same through the same automated system. Business goes on as usual without any bottlenecks. This is just a visualization.

Since Ethereum is a public blockchain, all network participants (nodes) get notified about activities and transactions happening on the distributed ledger.

A positive consensus between nodes on the positive fulfillment of conditions in a certain transaction executes the smart contract.

In Ethereum, smart contract execution happens on the Ethereum Virtual Machine (EVM). And every smart contract execution has to be paid for in ‘gas’.

Gas payments are calculated in terms of Ether (ETH), Ethereum’s native cryptocurrency.

Complex smart contracts (based on the transaction steps) need more gas for execution. The gas prevents overly complex or numerous smart contracts from overwhelming the EVM.

Working done. Now, let’s get to coding smart contracts!

How to Create and Deploy Smart Contracts?

Before progressing further let’s understand a bit about Solidity.

As described by California based, tech solutions firm LeewayHertz, Solidity is similar to Javascript. It is an exclusive smart contract writing programming language.

Typed statically, Solidity supports libraries, inheritance, and complex user-defined types.

A typical Solidity compiler converts a smart contract code into EVM bytecode, which in turn is sent to the Ethereum network as a deployment transaction.

Creating the smart contract

Start by downloading the MetaMask Google Chrome extension. The thing about MetaMask is that it serves both as an Ethereum-centric web browser and a wallet.

By default, your MetaMask wallet is connected to the main Ethereum network.

However, for writing smart contracts you must switch to any one of the displayed ‘test networks’:

  • Ropsten Test Network
  • Kovan Test Network
  • Rinkeby Test Network
  • Goerli Test Network

Check out this video below to set yourself up on the Rinkeby test network:

https://www.youtube.com/watch?v=yNhV4ifFPb0

Next, you can go ahead and start writing your smart contract on Remix IDE (integrated development environment). It is a web-based decentralized application (dApp) and also functions through certain smart contracts.

No need to install it, you can just access it directly here: https://remix.ethereum.org/#optimize=true&version=soljson-v0.4.24+commit.e67f0147.js&evmVersion=null

For more information on writing code in Remix IDE, check out the video below:

Embed: https://www.youtube.com/watch?v=qRYyJq5p76I

Deploying the smart contract

After writing, now it’s time to deploy your smart contract!

For deploying smart contracts on the Rinkeby test network, you will need test Ethers. You can avail test ETH from the ‘Get Ether’ option under ‘Test Faucet’.

You can also use real ETH for deploying your code. To buy Ethereum‘s cryptocurrency download the WazirX app on your phone, set up your account, and purchase ETH directly from the INR funds in your bank account.

After this, deposit the ETH you just bought into your MetaMask wallet.

Go to the Remix IDE, and click on the small ‘+’ sign aka ‘Create’ at the top left of your browser.

This will deploy your smart contract to Rinkeby, and confirm the transaction in MetaMask.

You have now successfully deployed your smart contract in the Rinkeby test network using MetaMask.

This was an overview of smart contract programming. There are lots of resources available on the internet that you can refer to. Here are a couple of them:

Report

What do you think?

Veteran

Written by tarunaarya

Years Of MembershipStory MakerContent Author

One Comment

Leave a Reply

Leave a Reply