Ethereum crowdsale contract example


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.

Tokens and DAOs In this example we will make a better crowdfunding by solving two important problems: If you are just testing, switch the wallet to the testnet and start mining. The code Now copy this code and let's create the crowdsale: The following line will instantiate a contract at a given address: How to use Go to contracts and then deploy contract: Put ethers as the funding goal 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 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. I have gadgets. Why not sell them all? Raise funds 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: Extending the crowdsale What if the crowdsale overshoots its target?

Unlimited crowdsale 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. Then modify the crowdsale to rename all mentions of transfer to mintToken: There are many strategies to prevent that, but implementing will be left as an exercise to the reader: A virtual organization where members vote on issues A transparent association based on shareholder voting Your own country with an unchangeable constitution A better delegative democracy.

But you should be happy, there are a few of tools that can help you with its implementation. You can install it with NPM package-manager. The second one is flexible deploy tool called truffle. So, the simplest token can be created with the simplest code.

This is mintable token, it means that you can mint new tokens without any problems. Crowdsale contract is needed for the token sale. That contract takes ether and give tokens.

Those few lines describe one of the easiest crowdsale contract ever. When people send you money, fallback function name for the function without name and with payable modificator will raise and send them tokens. Now you are ready for the your first ICO. I strongly recommend you to use truffle for deploying contracts. The tool can be installed by NPM and can make your deploying life much better.

A bit of theory What is ICO?