Sign in
Log inSign up
Writing and deploying your first Ethereum contract

Writing and deploying your first Ethereum contract

Shamsher Ansari's photo
Shamsher Ansari
·Sep 27, 2018

Blockchain... Blockchain.. Blockchain... You have heard it thousands of times and may be reading about it day in and day out but wondering how you can make your hands dirty with Blockchain. So I will jump straight into how we should write something for Blockchain. The best way to write something for Blockchain is to use Ethereum Smart Contract.

A smart contract is a computer code running on top of a blockchain containing a set of rules under which the parties to that smart contract agree to interact with each other. If and when the pre-defined rules are met, the agreement is automatically enforced.

We will be using Remix Editor to write and deploy Ethereum Smart Contract. Without further delay I will show you what we are going to write in snapshot and then we will see line by line what it means and how you can deploy your smart contract on virtual blockchain.

smart-contract.png

Ohh... It looks quite similar to JavaScript, Yes you are right, if you are familiar with JavaScript you will feel right at home.

Solidity is a programming language used to write smart contract. The very first line indicate the version of compiler you want to use to compile your smart contract. The key word contract is similar to that of Class in other programming languages.

Inbox will be identified as the name of the contract. We declare one variable at the beginning of our contract called message. Message will be main storage space in blockchain which will hold our inbox message.

constructor.png

The first function declared on line number 6 is kind of constructor in class which will get instantiated when contract is initialized for the first time. When you declare function, it has 3 parts. Function name, Function type and Return type.

function-declaration.png

As you know if you want to change anything on Blockchain, you have to submit a transaction. When you call getMessage function it simply returns the latest value stored in message. When you want to update Inbox storage, you call setMessage function.

msg.png

When you call getMessage, it does not modify contract's data. So it executes instantly and does not cost you money. But when you call setMessage function, it actually modifies contract's data, so it takes approx 15 seconds to execute hence it cost money to process transaction.

Okk... too much explanation. Lets see how we can deploy our tiny smart contract on Ethereum Virtual Machine (EVM). We are not going deploy our tiny contract on main Ethereum network instead we are going to use Remix Editor which contains in browser fake ethereum network which we will use to test and deploy our tiny contract.

deploye.png

Remix editor is where our contract source code is present, Ethereum compiler will compile contract source to bytecode which is understood by in browser fake network. You can not directly interact with ethereum blockchain data and this is where ABI comes into picture.

The ABI, Application Binary Interface, is basically how you call function in contract and get data back. An ABI is the interface between two program modules one of which is at the level of machine code.

In order to deploy our tiny contract on in browser fake network just click on Run tab in remix editor and select Javscript VM from different available environment. When you select environment you will see one default account is automatically get selected in Account options. This account will be use to deduct money for deploying contract.

contract-deploy.png

We will set some initial message for our inbox and click create button to deploy contract on fake network.

dep1.png

When you click create button you will see something similar in remix editor console.

console.png

After our contract is deployed you will see following screen where you can call getMessage and set new message in inbox.

dep2.png

Remember if you click get message you will get instant output because it does not modify contract data on blockchain. But when you click setMessage you are submitting transaction to modifying contract's data so it will take 15 sec to reflect changes.

dep3.png

Remix editor is good when you want to play around and test your small contract which does not have any other dependency. But if you want to write smart contract that have lot of internal and external dependency, you can use other available test network like Ropsten, Kovan and Rinkeby.

In upcoming articles we will explore more about writing modular smart contract and how we can deploy our contract on different testnets.

Diagram credit: Stephen Grider