How to write a smart contact

ICO, or Initial coin offerings, is the new craze now. Blockchain technology provides a unique way for incentivizing early adopters to participate in a project and garner initial funding by issuing “tokens”. One needn’t get scared by the new terms which are used in this domain. Below is a comprehensive guide on what an ICO is, and how someone can learn more about “contracts” which power these ICOs. So let’s dive right in!

What the hell is an ICO?

An Initial Coin Offering, commonly referred to as an ICO, is a fundraising mechanism in which new projects sell their underlying crypto tokens in exchange for Bitcoin and Ether. A Crypto token, or a token in general, is nothing but a term denoting a unit of value issued by a project or company. This unit of value can be used to reward users who participate in the project and perform particular actions, or it can be used as transaction fees for getting a specific service on the network. For example, Ethereum network uses Ether (ETH) as a token which is used to perform computations in the network.

These tokens can be obtained by buying them in an exchange for fiat currencies like USD, or they can be earned by performing specific services on the network (like mining). The beauty of these tokens lie in the fact that their price also appreciates and depreciates based on the demand for these tokens. This behavior is similar to shares in companies, which reflect partial ownership of the company. Tokens reflect both the currency used to pay for services in the network and also as equity in the network of that token.

As the network effect of that token increases, the token appreciates in pricing. The first step in determining whether tokens make sense for your project or not depends upon what your project’s objective is and is there a way that it can be “tokenised.” As discussed above, tokens are not just the currency used in the network, but they are a unit of the business model on which the network is built on.

Top 4 blockchain development courses for any level

We’ve curated a list of the best blockchain development courses for both beginners and experts that will boost your crypto career. Each course will help you to increase your knowledge in this field, regardless of your level of experience.

Bitcoin and Cryptocurrency Technologies - Fundamentals
Learn to Code Ethereum DApps By Building Your Own Game
Ethereum Blockchain Developer: Build Projects Using Solidity
ETH Certified Online Ethereum Developer Course - from b9lab

Do tokens make sense for your project?

For tokens to make sense for a project, the project in question should have the following properties:

  1. There should be a utility value to the token in the network. The token of the Ethereum platform, for instance, is Ether, and it is used as currency for paying transaction fees for any computation performed in the Ethereum blockchain.
  2. There should be crypto-economic incentives for players to buy tokens, and the token issuance mechanism should be designed in such a way that their value should increase as the usage of the token increases. For example, Filecoin is the token for IPFS which is used for storing files in distributed hard drive networks. As more users want to store files in a distributed way, they would need file coin tokens. Generally, these crypto tokens define beforehand how the token supply will evolve over time. These policies are designed in such a way where the value of the token increases as the services provided by the network are used more.

ERC20 is a token standard which makes it very easy to issue new crypto tokens. It is based on Ethereum and describes the functions and events that an Ethereum token contract has to implement. But before we go ahead and learn how one can issue tokens for their project, we must understand what a “smart contract” is. This is the main topic for this post after all.

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:

  1. 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).
  2. 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.
  3. 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.
  4. 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:

  1. 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.
  2. 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.
  3. 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

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. Remix – Remix is a web browser based IDE that allows users to write Solidity smart contracts, then deploy and run the smart contract.

Source: remix.ethereum.org

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:

  1. 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).
  2. 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:

  1. Viper – Viper focuses on security and language and compiler simplicity. It has a python-like indentation scheme.
  2. Lisk –  Lasik uses javascript as a smart contract language.
  3. 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.

pragma solidity ^0.4.0;
 
contract Coin {
 // The keyword "public" makes those variables
 // readable from outside.
 address public minter;
 mapping (address => uint) public balances;
 
 // Events allow light clients to react on
 // changes efficiently.
 event Sent(address from, address to, uint amount);
 
 // This is the constructor whose code is
 // run only when the contract is created.
 function Coin() {
 minter = msg.sender;
 }
 
 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.

address public minter;

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 => 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.

function Coin() {
minter = msg.sender;
}

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.

  1. Ethereum smart contract example
  2. Solidity documentation
  3. IBM Watson IOT smart contract Samples
  4. Ethereum dApp tutorial by Mahesh Murthy

Top slack and gitter channels to connect with experts

The best way to learn about any domain is to talk to the experts in the field. Fortunately, the blockchain community has a very open forum with folks willing to share their knowledge with new members. You can check out the following channels to understand in more details what the community is discussing. You can also ask your queries and the community is more than happy to answer.

If you want to learn more about the general blockchain, Bitcoin’s slack channel is a good place to hang out. For more specific details on Ethereum and smart contracts, check out the Ethereum and solidity gitter channels. Coinfund is an active project where you can ask questions and follow what people are discussing.

Since issuing tokens is becoming an increasingly popular way to monetize your product and generate user participation, smart contracts are getting more and more important. Understanding smart contracts and learning how to write one will enable you to issue tokens and take your projects to where you always dream them to be. All the best!
  • smart contact, ico, inital coin offering
  • 0 Benutzer fanden dies hilfreich
War diese Antwort hilfreich?

Related Articles

How to start an initial coin offering

While acting as an ICO adviser I face a lot of questions with regards to the ICO process. There...

Success ICO step by step

1. Where should I start? Before launching an ICO, you might be interested in what an ICO...