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;
}
}