Solidity different token price

0 votes

I'm just checking the Solidity code for my school project. I have a questions about this code:

price = etherCostOfEachToken * 1 ether;

Can I add something like:

(if they send more then 2 ether they have a bonus like the maximum 5000)

 If (ether contribution > 2 ether) {
    tokenReward.transfer(msg.sender, 5000);
    }else{
    tokenReward.transfer(msg.sender, amount / price);
    }

Is this possible?

pragma solidity ^0.4.2;
contract token { function transfer(address receiver, uint amount){  } }

contract Crowdsale {
    address public beneficiary;
    uint public fundingGoal; uint public amountRaised; uint public deadline; uint public price;
    token public tokenReward;
    mapping(address => uint256) public balanceOf;
    bool fundingGoalReached = false;
    event GoalReached(address beneficiary, uint amountRaised);
    event FundTransfer(address backer, uint amount, bool isContribution);
    bool crowdsaleClosed = false;

    /* data structure to hold information about campaign contributors */

    /*  at initialization, setup the owner */
    function Crowdsale(
        address ifSuccessfulSendTo,
        uint fundingGoalInEthers,
        uint durationInMinutes,
        uint etherCostOfEachToken,
        token addressOfTokenUsedAsReward
    ) {
        beneficiary = ifSuccessfulSendTo;
        fundingGoal = fundingGoalInEthers * 1 ether;
        deadline = now + durationInMinutes * 1 minutes;
        price = etherCostOfEachToken * 1 ether;
        tokenReward = token(addressOfTokenUsedAsReward);
    }

    /* The function without name is the default function that is called whenever anyone sends funds to a contract */
    function () payable {
        if (crowdsaleClosed) throw;
        uint amount = msg.value;
        balanceOf[msg.sender] = amount;
        amountRaised += amount;
        tokenReward.transfer(msg.sender, amount / price);
        FundTransfer(msg.sender, amount, true);
    }

    modifier afterDeadline() { if (now >= deadline) _; }

    /* checks if the goal or time limit has been reached and ends the campaign */
    function checkGoalReached() afterDeadline {
        if (amountRaised >= fundingGoal){
            fundingGoalReached = true;
            GoalReached(beneficiary, amountRaised);
        }
        crowdsaleClosed = true;
    }


    function safeWithdrawal() afterDeadline {
        if (!fundingGoalReached) {
            uint amount = balanceOf[msg.sender];
            balanceOf[msg.sender] = 0;
            if (amount > 0) {
                if (msg.sender.send(amount)) {
                    FundTransfer(msg.sender, amount, false);
                } else {
                    balanceOf[msg.sender] = amount;
                }
            }
        }

        if (fundingGoalReached && beneficiary == msg.sender) {
            if (beneficiary.send(amountRaised)) {
                FundTransfer(beneficiary, amountRaised, false);
            } else {
                //If we fail to send the funds to beneficiary, unlock funders balance
                fundingGoalReached = false;
            }
        }
    }
}

Oct 8, 2018 in Blockchain by digger
• 26,740 points
614 views

1 answer to this question.

0 votes

Yes you can do this by modifying your payable method to be something like:

//You can add a constant to save the bonus amount:
uint constant bonus = 5000;

//Inside this method check if amount is larger than 2:
function () payable {
    if (crowdsaleClosed) throw;
    uint amount = msg.value;
    balanceOf[msg.sender] = amount;
    amountRaised += amount;
    if (amount > 2) {
        tokenReward.transfer(msg.sender, amount / price + bonus);
    } else {
        tokenReward.transfer(msg.sender, amount / price);
    }
    tokenReward.transfer(msg.sender, amount / price);
    FundTransfer(msg.sender, amount, true);
}
answered Oct 8, 2018 by Omkar
• 69,210 points

Related Questions In Blockchain

0 votes
1 answer

How to set token price in solidity?

We know that 1 Ether = 1018 wei, ...READ MORE

answered Sep 25, 2018 in Blockchain by slayer
• 29,350 points
8,250 views
0 votes
1 answer

How to set the ERC20 token price in respect to ether in solidity?

Hi, @Jas, I would suggest you go through ...READ MORE

answered Nov 9, 2020 in Blockchain by anonymous
• 10,520 points
4,929 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,689 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,230 views
0 votes
1 answer

Solidity geth: Error encountered during contract execution [Bad instruction]

recipes is a dynamic storage array. You need ...READ MORE

answered Oct 15, 2018 in Blockchain by Omkar
• 69,210 points
1,247 views
+1 vote
2 answers

How is Blockchain programming languages different than usual programming?

The programming syntax of most of the ...READ MORE

answered Mar 30, 2018 in Blockchain by Christine
• 15,790 points
875 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP