"ICO Contract" Example (Send Token in Exchange for Ether)

In this post i can send Ether to the Creator Account as a donation from another Account
Ive also created a Token Contract where you can send Tokens to Another Account

In this post, i will make use of both concepts with the following assumption:

I have a company and i would like to go ICO.

Initial coin offering (ICO) is an unregulated and controversial means of crowdfunding via use of cryptocurrency, which can be a source of capital for startup companies.
So if someone or a company wants to help me setup my company and get some Initial Coins. My initial Coins are Tokens and i accept Ether in exchange for the Token.

So the process goes as follows.
1. I Deploy my TOKEN contract with an initial of 1000
2. Another Account will send me Ether and i will send back some Coins.
3. My Exchange Rate is 1 to 1. So for Every Ether, i will send 1 Token back to the person who gave me Ether.

In the Code below, i am giving 7 Tokens for any amount



pragma solidity ^0.4.18;

//Used like an Interface in C# or Java
contract Token {

    function totalSupply() public constant returns (uint256 supply) {}
    function balanceOf(address _owner) public constant returns (uint256 balance) {}
    function transfer(address _to, uint256 _value) public returns (bool success) {}
    function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) {}
    function approve(address _spender, uint256 _value) public returns (bool success) {}
    function allowance(address _owner, address _spender) public constant returns (uint256 remaining) {}

    //needed to Log the Events
    event Transfer(address indexed _from, address indexed _to, uint256 _value);
    event Approval(address indexed _owner, address indexed _spender, uint256 _value);
    
}



contract StandardToken is Token {
    mapping (address => uint256) balances;
    mapping (address => mapping (address => uint256)) allowed;
    uint256 public totalSupply;
   
    function transfer(address _to, uint256 _value) public returns (bool success) {
       if (balances[msg.sender] >= _value && _value > 0) {
            balances[msg.sender] -= _value;
            balances[_to] += _value;
            Transfer(msg.sender, _to, _value);
            return true;
        } else { return false; }
    }
    function balanceOf(address _owner) public constant returns (uint256 balance) {
        return balances[_owner];
    }
}


//name this contract whatever you'd like
contract AreshToken is StandardToken {

    function () public{
        revert();
    }

    /* Public variables of the token */

    string public name;                   //Aresh Coin
    uint8 public decimals;               //Standard 18
    string public symbol; 
    string public version = 'H1.0';       //Coin ContractVersion
    address creator;
    function AreshToken(
        ) public {
        creator = msg.sender;
        balances[msg.sender] = 10000;               // Give the creator all initial tokens (100 for example)
        totalSupply = 10000;                        // Update total supply (100 for example)
        name = "ATSCoinng";                                   // Set the name for display purposes
        decimals = 0;                            // Amount of decimals for display purposes
        symbol = "ATSICO";                               // Set the symbol for display purposes
    }
    function getSender() public constant returns (address){
        return creator;
    }
}
//RUN THIS CONTRACT
contract ICOAresh{
    AreshToken atsToken;
    address creator;
    
    function ICOAresh() public{
        creator= msg.sender;
        atsToken = new AreshToken(); //an instance of TokenAresh
    }    
    function getOwner() public constant returns (address)
    {
        //this gets the contract address as the owner
        return atsToken.getSender();
    }
    function giveToken(address _donor, uint256 _amount) private{
        atsToken.transfer(_donor,_amount);
    }
    function getBalanceToken() public constant returns (uint256){
        return atsToken.balanceOf(msg.sender);
    }
    function Donate() payable public{
        
        uint256 convertion = 7; //1 to 1 exchange
        if (msg.value > 0)
        {
            giveToken(msg.sender,convertion); //1 Tokens for every amount donated
            safeMoney(msg.value);
        }
    }
    //send the amount to the creator
    function safeMoney(uint amountRaised) private{
        creator.transfer(amountRaised);
    }
    
}


The next step would be to compute for an exchange rate. instead of a fix amount of 7.you would want to multiply the msg.value with a number

since the msg.value is in Wei. for every 1 value we get 1000000000000000000 as the msg.value
so a value of  3 is 3000000000000000000

given this, in order to create an exchange we will change the following line

uint256 convertion = 7; //1 to 1 exchange

TO

uint256 convertion = 2* (msg.value / 1000000000000000000); //1 to 2 exchange
        

So for every msg.value, we multiply it by 2. this means, a donation of 5Ether will get you 10 Tokens.



Comments

Popular posts from this blog

Is Making the Crypto Space Legally Compliant Paving the Road to Mass (Blockchain) Adoption?

Crypto-Currency "Coin Contract" Example

Solidity Hash of Structs (Testing hash uniqueness)