I use this "buy" function in a smart contract, to receive Eth. for a token. I would like to extend the function, so if the Eth amount in a transaction is higher than X Eth. then send Y% to smart contract owner.

I'm not sure about what would be the best way to do this, so I'm looking for any suggestions.

Thanks in advance :-)

 @dev Buy tokens with ether. 
 @param minPurchaseReturn The minimum number of tokens you will accept.
function buy(uint minPurchaseReturn) public payable {
    uint amount = formulaContract.calculatePurchaseReturn(
        (tokenContract.totalSupply() - uncirculatedSupplyCount) - tokenContract.balanceOf(this),
        (address(this).balance + virtualReserveBalance) - msg.value,
    amount = (amount - ((amount * fee) / 1000000));

    //Now do the trade if conditions are met
    require (enabled);
    require (amount >= minPurchaseReturn);
    require (tokenContract.balanceOf(this) >= amount);

    //Accounting - so we can pull the fees out without changing the balance
    collectedFees += (msg.value * fee) / 1000000;

    emit Buy(msg.sender, msg.value, amount);
    tokenContract.transfer(msg.sender, amount);
  • if the Eth amount in a transaction is higher than X Eth - there is no if in your code, to begin with. Sep 24, 2018 at 12:53
  • Also, it looks extremely similar to Bancor Contracts. Have you tried consulting with them via GitHub? Sep 24, 2018 at 12:59

1 Answer 1


You can see the amount of Eth coming in msg.value, so use it in your calculations. Add something like this:

uint threshold = 1 ether;
uint percentage = 5;

function buy(uint minPurchaseReturn) public payable {
    if(msg.value > threshold){
        //the following line introduces no overflow risk
        //since there's not enough Ether to overflow uint256 
        //by multiplying msg.value by 100
        uint myPrecious = msg.value * percentage / 100; 

You can inherit Ownable.sol to get _owner value.

  • msg.value / 100 * percentage is a bad idea. You probably want msg.value * percentage / 100. Sep 24, 2018 at 15:22
  • @goodvibration well, just multiplying by 100 isn't very likely to cause an overflow, but then again - you never know ;) So it may require additional checks. With / 100 * percentage you get less precision, but no overflow risk. Is precision loss the reason why you don't like it?
    – Utgarda
    Sep 24, 2018 at 15:54
  • The value of percentage is at most 100, right? In order for msg.value * percentage to overflow, msg.value would need to be at least 2 ^ 256 / 100 wei, which means 2 ^ 256 / 10 ^ 16 Ether. That amount of Ether doesn't even exist, so no overflow is ever likely there. Sep 24, 2018 at 16:30
  • 1
    Thank you all, and that you @utgarda for providing me with an example. I've got my contract working now :-)
    – Dyvel
    Sep 25, 2018 at 12:54

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.