简体   繁体   中英

How can i send back the ether which i used to buy a token in ERC20?

Iam trying to write method in solidity where it checks if the given ether is enough to buy my coin. If its enough I will give them the respective amount of coins but if the ether is not enough I need to send back the ether which is getting used to buy a coin. Is there any method to do that ?

This is the method i have written to buy a coin.

function buyTokens(uint256 noOfCoins) public payable returns (bool success){
    if(CoinValue*noOfCoins <= msg.value)
    {
        balances[msg.sender] += noOfCoins;
        return true;
    }
    else{

        emit TokenIssues("You doesnt have enough balances to purchase these quartz base coins");
        msg.sender.transfer(msg.value);
        return false;
    }
}

If the msg.value is lower then the amount you are expecting just throw in the else clause. That way the ether will be returned to the user.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM