// mustacoin ICO pragma solidity >=0.4.11; contract hadcoin_ico { // Introducing the mazimum number of hadcoin avaliable for sale uint public max_hadcoins = 1000000; // Introducing the USD to hadcoin conversion relocatable uint public usd_to_hadcoins = 1000; // Introdicing the total number of hadcoin that have been bought by the investors uint public total_hadcoins_bought = 0; // Mapping fropm the investor address to its equity in hadcoins and USD/ the two mapping take the address of the investor and return how many coins and usd value it has mapping(address => uint) equity_hadcoins; mapping(address => uint) equity_usd; // checking if an investor can buy hadcoins, calculate if the investor request is less then the total max amount of couns plus the couns alredy sold // inside the modifier we use the require function that first convert the usd in hadcoins and add the sold one and check if the total is <= to the max one //the underscore says that the following code will be applied only if the above condition is met modifier can_buy_hadcoins(uint usd_invested){ require (usd_invested * usd_to_hadcoins + total_hadcoins_bought <= max_hadcoins); _; } // Getting the equity in hadcoin of an investor // fuction that takes an adress variable named investor ..specify is external constant and that returns a uint type function equity_in_hadcoins(address investor) external constant returns (uint) { return equity_hadcoins[investor]; ///calling the method above that tekex the invesor address (equity_hadcoins) } // Getting the equity in USD of an investor function equiity_in_uds(address investor )external constant returns (uint){ return equity_usd[investor]; } // Buying hadcoins // we need before the breakets check if the investor is allow to buy or not using the previus modifier function buy_hadcoins(address investor, uint usd_invested) external can_buy_hadcoins(usd_invested){ uint hadcoins_bought = usd_invested * usd_to_hadcoins; equity_hadcoins[investor] += hadcoins_bought; ///gives the total amount of hadcouns held by the investor equity_usd[investor] = equity_hadcoins[investor] /1000; total_hadcoins_bought+= hadcoins_bought; //update the total number of hadcoins bought by all investors } // Selling Hadcoings //this function give the option to the investors to sell the hadcoins and for us to buy it back function sell_hadcoins(address investor, uint hadcoins_sold) external { equity_hadcoins[investor] -= hadcoins_sold; ///remove the hadcoins from investor wallet equity_usd[investor] = equity_hadcoins[investor] / 1000; total_hadcoins_bought -= hadcoins_sold; /// update number of hadcoins of total investors sold } }
0.4.18