In this post, we will show how to write a simple smart contract. If you do a quick google search, you will find numerous articles on writing smart contracts. In my previous post Blockchain, I explained blockchain. But I didn’t talk about the smart contracts in that post. That’s why a separate post.
Smart Contract
In layman’s terms, the contract is nothing but an agreement between two parties, witnessed by a third party to hold both parties accountable for playing out the contract. So what is a Smart Contract then?
In Nick Szabo’s words
“Smart Contracts are a set of promises, specified in digital form, including protocols within which the parties perform on these promises.”
In Web Developer’s terms, a Smart contract is like an application API, but there are few exceptions. Like an API can call another external API, a smart contract can not call external API. A smart contract can call another smart contract. A smart contract comprised of a number of private functions and variables to implement the agreement.
More formal definition of a smart contract is a method of using Bitcoin to form agreements with people via the blockchain.
Ethereum
So how do we write these smart contracts? Ethereum is one such a platform that is used primarily for building and publishing distributed applications. It is a Turing-complete virtual machine built for the purpose of cryptocurrency. It is the fundamental underlying infrastructure platform that can run all blockchains and protocols. Each node in Ethereum runs an Ethereum Virtual Machine. This EVM hosts distributed programs (smart contracts) which get executed seamlessly.
Implementation of smart contracts
To address some basic questions like “How does a smart contract look?”, “What do you use for programming a smart contract?”, I will go over some simple concepts.
There are currently two programming languages that can be used to write a smart contract.
- Solidity – A javascript look-alike language with file extensions .sol
- Serpent – A python look-alike language with file extensions .se
Once a contract is written in either language, you can compile it using a solc compiler. A compiled contract then posted on the network. You can call this contract in your web app by using web3.js Javascript API.
Conclusion
In this post, I tried to explain one of the key concepts of blockchain, a smart contract. This can be used further in building decentralized applications. In the next post, I will show how to write a simple smart contract and run on a node with EVM.
References
- Building a smart contract – Smart Contract Ethereum
- Blockchain for web developers – Blockchain