Getting Started With ICO Smart Contracts — Part 3

Akarsh Agarwal
MLG Blockchain
Published in
5 min readSep 29, 2017

In Part 1 of this series we learnt how to develop a Token Contract and in Part 2 we learnt some of the very basic functionalities and variables required to develop a Crowdsale Smart Contract. In this article, we continue our dive into the ocean of ICO smart contracts and will explore some of the more complex functions required for the ICO.

Now, that we have got our total tokens and statistics, investors will need a buy function to execute the payment. So, the below function does just that. Let’s take a more detailed look into it:

function() nonZero payable { }
function buytokens(address _recipient, uint256 _value) internal returns (bool success) { }

The function with no name, is called the default function, which is called whenever someone transfers money into the contract due to the “payable” keyword. This function handles some basic checks and then forwards the call to “buyTokens” function which takes care of storing the amount transferred into the sender’s balance.

There are 3 scenarios where a person can transfer the money into the contract:

  • During the Presale Period: As there are separate prices for tokens in Pre-Sale and ICO Sale, we need to make sure that if the investor is sending the money during Pre-Sale, they get the appropriate amount of tokens.
  • During the ICO Sale Period: Similar to the above reason, we need to make sure that we provide adequate number of tokens to the sender, in accordance with the ICO Sale prices.
  • Other than 1 or 2: This is when the contract should return an error and refund the ethers, as the ICO has either not begun or has already concluded, so there is no need of sending the ethers at that very moment.
function isPreSalePeriod() constant returns (bool) { }
function isICOPeriod() constant returns (bool) { }

These functions are used to check whether the ICO has concluded or not, if not, whether we reached our maximum ether cap each for Presale and ICO Sale. This is helpful as we would need to check these conditions every time a transaction is made. If we have reached our maximum cap, we need to change the state. To reflect the same in the above functions, isCrowdSaleStatePreSale or isCrowdSaleStateICO, affect the buying of tokens. Once the state is changed, an investor cannot buy tokens as they are either out of supply or the ICO has reached its end.

Both of these functions may seem similar in their nomenclature, but do entirely different functions. The first only checks for the state and the return variables and latter looks at the timestamps, changes the state accordingly and then returns. This helps make our contract more comprehensive and immune to failures or faulty transactions, which may be made outside the ICO Period.

Now that we have raised our Ether and the ICO Period has completed, the owner of the contract has to call the endCrowdSale function. This will change the state of the variables and will avoid any further transactions. What if the owner delays the function call? Will we still be able to receive the Ethers?

The answer to the question lies in the function’s body. Whenever we create a transaction, we not only check for the state, but also whether the timestamp of the transaction which is included in the block. This ensures that even if the owner doesn’t change the state of the ICO, there is no chance of anyone buying tokens otherwise, as the timestamp wouldn’t match or fall under the range of the ICO Period.

Below is the structure of the function to end the Crowd Sale:

function endCrowdSale() onlyOwner { }

While changing the state of the ICO, we need to check whether we could raise the minimum ethers, we’d set up while deploying the contract. If the amount of ethers raised is above the minimum cap, we set the state of the ICO to “Success”, or else “Failure”.

With the state set to “Success”, the total raised in ether during the Presale and ICO Sale will be transferred to the beneficiary of the ICO, which was declared when deploying the contract. This is done so that no one can exploit the contract to withdraw the funds. Also, it makes it easier for the beneficiary to pay for the development of the project for which the ICO was conducted.

With the “Failure” state, we would need to refund the ethers to their respective investors. So we use “Pull Rather Push” strategy for the refund. What this means is that we don’t send the ethers back to the investors. Instead, we ask them to transfer the ethers by themselves. This ensures that we are not sending any ether to a wrong address or one-time address, as the sender and recipient may be different while buying the tokens.

Hence, the function below:

function getRefund(address _recipient) returns (bool) { }

This will first reduce the amount of the recipient to 0 and then transfer the money to the respective account. This is considered good practice, as it would disable the recipient to ask for refunds again and again, while the transfer is being executed.

So, this was a fairly simple ICO SC, which does a couple of functions and is comprehensive. Many ICOs have multiple functionalities such as halting the ICO if something goes wrong or some faulty transaction occurs. It can be implemented in this same contract, considering the requirements of the company conducting the ICO. Also, we can add the functionality of whitelisted investors, which can buy the tokens in the Presale period. This ensures that trusted people buy their tokens at a lucrative price to attract more investor for the ICO Sale. And the list goes on….

About MLG Blockchain

MLG Blockchain is a global blockchain consulting and development firm headquartered in Toronto with a distributed team across North America, Europe and Asia that is focused on building next generation applications using blockchain and smart contract technology. We speed up your team’s understanding of the blockchain and its potential opportunities for your business and help you to create a blockchain strategy you can use today.

If you are part of the executive team of a new or existing token, please reach out at hello@mlgblockchain.com to be profiled.

--

--

Akarsh Agarwal
MLG Blockchain

All about Distributed Systems and Stakeholder Management. #golang #distributedsystems #management