0

Ethereum Wallet shows "It seems this transaction will fail ......" even though I've used if/else inside contracts to ensure that transfer value does not go higher than contract balance.`

  function mySales( address a) constant public canWithdraw returns (uint){
    return totalSales[a];
}

function topSalesPerson() constant public returns ( address ){
    return topsales;
}

event bonusDepleted(string);

function submitSales() public payable canWithdraw{

    uint amount = msg.value;
    uint bonus_temp;
    if (this.balance >0){
        if(amount > 50000000000000000000){
            bonus_temp = amount/10; // 10% bonus
        }
        if(bonus_temp > 20000000000000000000){
            bonus_temp = 20000000000000000000;  // max bonus = 20 ether
        }

        if (this.balance > bonus_temp){
             msg.sender.transfer(bonus_temp);  // transfer bonus in case of enough balance
        }
       else{
            msg.sender.transfer(this.balance); // else transfer remaining amount as Bonus 
       }
        owner.transfer(amount);

        Bonus[msg.sender] += bonus_temp;
        totalSales[msg.sender] += msg.value;

        if( totalSales[topsales] < totalSales[msg.sender]){
            topsales = msg.sender;
        }
    }

    if(this.balance == 0){
        bonusDepleted("Bonus has reached its limit and no bonus will be provided for future sales");
    }
}`

So if contract has 3 ethers then for 50 ether it allows txn but for 60 it shows warning and error "intrinsic gas too low" where as it should have just refunded last 3 ether.

Also for Readonly functions like "mySales", how to invoke them from "ethereum -wallet" as it does not return anything on entering address into this function under "Read from Contract Section". i'm able to use it from command-line.

0

I think the issue is here:

if (this.balance > bonus_temp){
    msg.sender.transfer(bonus_temp);  // transfer bonus in case of enough balance
}
else {
    msg.sender.transfer(this.balance); // else transfer remaining amount as Bonus 
}
owner.transfer(amount);

There's no guarantee that there are sufficient funds for the transfer to the owner. In particular, any time amount is positive and you process the else clause, the contract's balance will then be zero, and the line after it will revert the transaction.

A reasonable fix might be to do the transfer to owner before you calculate the bonus, but it really depends on the behavior you want.

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.