We need to write smart contract issue token on ethereum for ico
We will be programming in Solidity , the language of the Ethereum network created for smart contracts. Open up a text editor and create a file with your token name and the. Here, we define the functions of the ERC20 Token standard as mentioned above.
One can see that totalSupply returns a uint , or value of tokens in circulation. The parameters of these functions have an address field which is the ETH public key of a user.
The two event methods emit details of transfers and approvals. Along with our balances array, we also need an allowed array to indicate how much of our token an address can withdraw from another address.
Adding to this contract, we need to implement the rest of our methods. Our transfer function takes two parameters: In our function, we have to deduct the amount of tokens spent by the sender and add the token value to the destination address. However, we will do this only when certain conditions are met. Otherwise, the function will return false. After we complete the transfer, we emit the details of the transaction with the Transfer event.
We also need to check if the number of tokens being transferred is approved. To implement our balanceOf method, we just need to return the value stored in the balances array for the address inputted. There are two remaining methods to develop a ERC20 standard token. The approve method allows allowances to be added for the sender to withdraw tokens from a certain address.
It will call the Approval event to emit the details of the approval. To initialize our token, we can create another contract inherited from the Token contract with our token name. In this contract, we will also be defining our token name, number of decimals to show, and the token symbol. The approveAndCall method runs when the contact is authorized to transfer an amount of tokens from the sender account, and the function receiveApproval is called in the receiving contract where the transfer can be done.
Notice that the contract understands what a token is because we defined it earlier by starting the code with:. This doesn't fully describe how the contract works or all the functions it has, but describes only the ones this contract needs: Put the address of the organization you just created in the field if successful, send to. If you are just doing it for a test or demonstration, put the crowdsale duration as minutes, but if you are really raising funds you can put a larger amount, like 45, 31 days.
The ether cost of each token should be calculated based on how many tokens you are putting up for sale a maximum of how many you added as "initial supply" of your token on the previous step. In this example, put 5 ethers. The address of the token you created should be added to the token reward address. Put a gas price, click deploy and wait for your crowdsale to be created.
Once the crowdsale page is created, you now need to deposit enough rewards so it can pay the rewards back. Click the address of the crowdsale, then deposit and send 50 gadgets to the crowdsale. This is a very important point. The crowdsale we are building will be completely controlled by the token holders.
You can try to create special code on the association contract to prevent these hostile takeovers, or you can instead have all the funds sent to a simple address. To simplify we are simply selling off half of all the gadgets: Once the crowdsale has all the necessary tokens, contributing to it is easy and you can do it from any ethereum wallet: You can see the relevant code bit here:.
The unnamed function is the default function executed whenever a contract receives ether. This function will automatically check if the crowdsale is active, calculate how many tokens the caller bought and send the equivalent. If the crowdsale has ended or if the contract is out of tokens the contract will throw meaning the execution will be stopped and the ether sent will be returned but all the gas will be spent. This has the advantage that the contract prevents falling into a situation that someone will be left without their ether or tokens.
In a previous version of this contract we would also self destruct the contract after the crowdsale ended: By creating a fallback function that throws when the sale is over, we prevent anyone losing money. The contract has a safeWithdrawl function, without any parameters, that can be executed by the beneficiary to access the amount raised or by the funders to get back their funds in the case of a failed fundraise. In our code, only two things can happen: Since the token amount is limited, it means that once the goal has been reached no one else can contribute.
But the history of crowdfunding is full of projects that overshoot their goals in much less time than predicted or that raised many times over the required amount. So we are going to modify our project slightly so that instead of sending a limited set of tokens, the project actually creates a new token out of thin air whenever someone sends them ether. First of all, we need to create a Mintable token. Then modify the crowdsale to rename all mentions of transfer to mintToken:. Once you published the crowdsale contract, get its address and go into your Token Contract to execute a Change Ownership function.
This will allow your crowdsale to call the Mint Token function as much as it wants. This opens you to the danger of hostile takeover. At any point during the crowdsale anyone who donates more than the amount already raised will be able to control the whole pie and steal it. There are many strategies to prevent that, but implementing will be left as an exercise to the reader:.
You have to hire managers, find a trustworthy CFO to handle the accounts, run board meetings and do a bunch of paperwork. Or you can simply leave all that to an Ethereum contract. It will collect proposals from your backers and submit them through a completely transparent voting process. Crowdsale Raising funds from friends without a third party. Crowdfund your idea Sometimes a good idea takes a lot of funds and collective effort.