I'm using a very simple crowdsale it just sells tokens in ratio 1ETH=10000 tokens

I would like to change this so every day token would be more expensive for example

Day1: 1ETH = 10 000 Tokens Day2: 1ETH = 9 950 Tokens Day3: 1ETH = 9 900 Tokens

and so on...

heres the code i use now

pragma solidity ^0.4.18;

interface token {
    function transfer(address receiver, uint amount) external;

contract Crowdsale {
    address public beneficiary;
    uint public amountRaised;
    uint public price;
    token public tokenReward;
    mapping(address => uint256) public balanceOf;

    event FundTransfer(address backer, uint amount, bool isContribution);

     * Constructor function
     * Setup the owner
        address ifSuccessfulSendTo,
        uint milietherCostOfEachToken,
        address addressOfTokenUsedAsReward
    ) public {
        beneficiary = ifSuccessfulSendTo;
        price = milietherCostOfEachToken * 0.0001 ether;
        tokenReward = token(addressOfTokenUsedAsReward);

    function () payable public {

        uint amount = msg.value;
        balanceOf[msg.sender] += amount;
        amountRaised += amount;
        tokenReward.transfer(msg.sender, amount * 10**18 / price);
        emit FundTransfer(msg.sender, amount, true);

        // Forward funds immediately to beneficiary

You could store the crowdsale start in the constructor, like

uint crowdsaleStart;

    address ifSuccessfulSendTo,
    uint milietherCostOfEachToken,
    address addressOfTokenUsedAsReward
) public {
    crowdsaleStart = now

and then, with now special variable, use it to calculate how many tokens contributors should get for 1 ETH:

uint16 MAX_REWARD = 10000
uint16 MIN_REWARD = 1000

function () payable public {
    //uint16 should be enough, but check your types max values anyway
    uint16 daysPassed = (now - crowdsaleStart) / 60 / 60 / 24;

    //10k on the first day, 9950 on the second etc.
    uint16 tokensPerETH = MAX_REWARD - 50 * daysPassed;
    //make sure they get at least something
    tokensPerETH = tokensPerETH >= MIN_REWARD ? tokensPerETH : MIN_REWARD;
    //and make sure it's not too much, in case of underflow
    //best be sure it doesn't happen, or limit your daysPassed based price penalty
    tokensPerETH = tokensPerETH <= MAX_REWARD ? tokensPerETH : MAX_REWARD;

    //overflow isn't an issue here, nobody will send you zillions of ETH
    //but still it's a good idea to use OpenZeppelin's SafeMath
    reward = msg.value * tokensPerEth / 1 ether;

    tokenReward.transfer(msg.sender, reward);

or you can use price and change it in a similar way, use any "time to price" formula you like.

Check out SafeMath by OpenZeppelin, almost never hurts to use it when counting ETH and tokens.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.