What are Smart Contracts?
Smart contracts are computer programs which allow for
the automatic transfer of digital assets between parties based upon
pre-specified conditions. Smart contracts have been used primarily in
association with cryptocurrencies. The most prominent smart contract
implementation is the Ethereum blockchain platform, which also calls
them
decentralized applications, or
dApps.
Traditionally, we are used to applications which are hosted by a
centralized organization. While the code of the application may be
distributed across multiple physical servers, they are controlled by a
single entity. For example, Facebook is a centralized app which is
controlled by an entity called Facebook Inc. Decentralised apps, on the
other hand, are not controlled by a central entity but are governed by
code and the protocols set within them. These codes are generally open
sourced and anybody can use them to create a new dApp of their own. Some
Ethereum-based dApps that have been successful are Golem, Augur, and
Melonport. These dApps have been able to achieve millions in market cap.
Platforms for writing Smart Contracts
While Ethereum is the most popular platform for writing smart
contracts, it is not the only one. The following are some of the other
platforms used for writing smart contracts:
- Script in Bitcoin
– Script has limited capabilities when processing documents. Bitcoin
features a non-Turing complete scripting language, which allows for
specifying under which conditions a transaction can be redeemed. The
scripting language is quite limited, as it only features some basic
arithmetic, logical, and crypto operations (e.g. hashing and
verification of digital signatures).
- Automated Transactions
is another Turing complete smart contract language, used in
cryptocurrencies like Burstcoin and Qora. An example of its usage is
atomic cross-chain trading. Atomic cross-chain trading enables two
parties, who own coins in different cryptocurrencies, to exchange them
without need for a third, trusted party.
- NXT:
NXT is a public blockchain platform that contains a limited selection
of templates for smart contracts. You have to use what is given, you
can’t write your own code.
- Chain – Chain provides enterprise-grade blockchain infrastructure with SDKs in Java, Ruby, and NodeJS.
Comparison of Smart Contract platforms
|
Advantages |
Disadvantages |
| Script |
Based on Bitcoin platform – which provides stability and security. |
Not Turing complete |
| Automated Transactions (AT) |
Turing complete language. |
Blockchain would need to support AT specifications for a user to be able to create smart contracts in AT. |
| NXT |
Simple and easy with common use cases provided in templates. |
Only contains limited templates. Can’t develop contracts as you wish. |
| Chain |
a. Enterprise grade blockchain platform b. Provides coding in known developer languages like Java, Ruby and Node. |
Not a public blockchain platform. Only available for companies/networks who implement Chain blockchain. |
Limitations of Smart Contracts
Smart contracts are still in their evolution phase and cannot
entirely replace all forms of contracts. They are more effective for
terms which can be objectively defined and are completely in the digital
realm. More specifically:
- Smart contracts should not be making calls to external web services,
APIs, or external databases. This can lead to multiple independent
executions of the same smart contract code having different results.
This could break the consensus of the blockchain. Oracles, which update
data from the real world to the blockchain, are a way to solve this
problem.
- Smart contracts can only solve issues which can be objectively
decided based upon the facts. This constraint makes smart contracts less
valuable for legal contracts, where a dispute arises when there are no
objective facts – but instead the subjective judgement of two parties.
- The most important limitation for “smart contracts” is that (at
present) it is used for simple contract models based on the pattern “if
a, then b” or similar variations. It will be difficult to include more
subjective considerations in the contract like “without undue delay” and
“beyond a reasonable doubt” which is common in our current legal
parlance.
While these limitations currently exist, teams are working to make
these smart contracts more intelligent. With the advent of IoT, it’s
easier to relay real-time, real world data which can be saved on
blockchains by oracles. Many blockchain oracle services, like
Oraclize,
have started up which push real world data into blockchains. Even with
simple “if-then” smart contracts, there are many cases which are set to
be disrupted by smart contracts.
Tools for writing and deploying smart contracts
- Infura –
Infura provides scalable blockchain infrastructure off-loading the
requirement of running a full Ethereum node, and allowing developers to
focus on their code. It has been very well received by the developer
community.
- Mist Browser – It is a tool to browse and use dApps. It is a separate browser that can be used to browse dApps and interact with them.
- Truffle Framework
– Truffle is a popular development framework for Ethereum. It has
built-in smart contract compilation, linking, deployment, and binary
management which greatly simplifies the job of an Ethereum developer.
- Metamask –
MetaMask is a bridge that allows one to visit the distributed web of
tomorrow in their browser today. It allows users to run Ethereum dApps
right in their browser without running a full Ethereum node. It is a
browser plugin that allows users to make Ethereum transactions through
regular websites. As of September, 2017, Metamask is available only as a
plugin in a Chrome browser.
- Remix – Remix is a web browser based IDE that allows users to write Solidity smart contracts, then deploy and run the smart contract.
Languages for writing Ethereum smart-contract
The two primary languages which are used to write Ethereum smart
contracts are Serpent and Solidity. Serpent is the older language which
has become out of date as of September, 2017. Solidity, which is a
javascript based language, has now become the recommended language for
writing smart contracts. Security vulnerabilities have also been found
in Serpent recently, which makes it an unattractive platform for
development. Some key features of these languages are:
- Solidity – Solidity is a contract-oriented, high-level language
whose syntax is similar to that of JavaScript and it is designed to
target the Ethereum Virtual Machine (EVM).
- Serpent – Serpent is a high-level language designed for writing
Ethereum contracts. It is very similar to Python, but as of September,
2017, Solidity is the preferred language of development for Ethereum
developers
Upcoming Smart contract languages
Solidity is currently the most popular language for smart contracts.
There are a few upcoming smart contract languages which can become
important in the future:
- Viper – Viper focuses on security and language and compiler simplicity. It has a python-like indentation scheme.
- Lisk – Lasik uses javascript as a smart contract language.
- Chain – Chain provides enterprise-grade blockchain infrastructure with SDKs in popular languages such as Ruby, Java, and NodeJS.
Smart Contract Example
Let’s walk through an example contract written in Solidity. Solidity
is the most popular language for developing smart contracts. The
following contract is an example on how to create a new cryptocurrency.
New coins can be minted out of thin air but only by the user who has
created the contract. This contract can also be used to send coins from
one address to another.
mapping (address => uint) public balances; |
event Sent(address from, address to, uint amount); |
function mint(address receiver, uint amount) { |
if (msg.sender != minter) return; |
balances[receiver] += amount; |
function send(address receiver, uint amount) { |
if (balances[msg.sender] < amount) return; |
balances[msg.sender] -= amount; |
balances[receiver] += amount; |
Sent(msg.sender, receiver, amount); |
Let’s go through the contract in detail.
defines a public variable of type address which is publicly
accessible. The address type is a 160 bit variable ideal for storing
addresses on the Ethereum network.
| mapping (address =&gt; uint) public balances; |
creates a mapping between address and unit type which stores the coin
balance in each address. You can think of it as a ledger that records
where how many coins are at each address.
this function is the constructor function which is executed as soon
as the contract is deployed. This sets the value of minter to the
address which has deployed the contract. This ensures that only the
owner of the contract can mint new coins and nobody else.
This is
ensured by the following function:
function mint(address receiver, uint amount) |
This function only gets executed if it is called by the minter. The
function sends coin value equal to amount to the receiver address. If it
is called by someone other than the minter, then this function does
nothing.
function send(address receiver, uint amount) |
This function sends an amount of coins to the receiver’s address from
the address calling the function. For example, if Bob calls this
function with Alice’s address and the amount is 1000, then 1000 coins
will be transferred from Bob’s account to Alice’s account.
The above smart contract shows how it can be used to mint coin and transfer it to a specific address.
Deploying smart contract in Ethereum blockchain
Once you have written a contract, you need to test it by actually
deploying it and testing if they are functioning as expected. This is
where testnets come to the rescue.
What is testnet?
Testnets simulate the Ethereum network and EVM. They enable developers to upload and interact with smart contracts
without paying the cost of gas.
Smart contracts must pay
gas for their computations on the
Ethereum network. If you want to run a smart contract on Ethereum
networks, you need to pay “gas” for the transaction to complete.
However, testnets provide environments for developers to test their
contracts without paying any money. Testnet gas is available for free
from many public areas.
What is etherscan and how do I explore smart contracts?
Etherscan is the block explorer for the Ethereum Blockchain. A block
explorer is basically a search engine that allows users to easily
lookup, confirm, and validate transactions that have taken place on the
Ethereum Blockchain. Smart contracts can be verified in etherscan by
using this
link
Few templates to get you started with smart contracts
To get you started with smart contracts, you can check out the
following templates below. The Ethereum and solidity documentation have
simple contracts to get you started. Once you have implemented these,
you can check out more advanced templates given by IBM. The IBM smart
contract templates show how you can use outputs from IoT devices to
control smart contracts. Ethereum dApp tutorial by Mahesh gives an
example from start to finish on how to implement and deploy a voting
dApp on Ethereum.
- Ethereum smart contract example
- Solidity documentation
- IBM Watson IOT smart contract Samples
- Ethereum dApp tutorial by Mahesh Murthy
Resource:howtotoken
Comments
Post a Comment