2

I am trying to simulate send ether fail in KingOfEther contract: The contract is specified below.

contract KingOfEther {
    address public king;
    uint public claimPrice = 20;
    address owner;

    function KingOfEther() {
        owner = msg.sender;
        king = msg.sender;
    }
    function ()
    {

    }
    function claimthrone(uint amount)  {
        if (amount < claimPrice) throw;
        uint value_paid=amount;
        uint compensation =value_paid-claimPrice;
        if(!king.send(compensation))
          throw;
        king = msg.sender;
        claimPrice = value_paid;
    }
    /* other functions below */
    /*function calculateCompensation() returns (uint){
        return 0;
    }
    function calculateNewPrice() returns (uint){
        return 0;
    }*/
}

In test folder of truffle i have added the following test, not complete but just trying to interact with testrpc:

var KingOfEther = artifacts.require("./KingOfEther.sol");

contract("KingOfEtherContract",function(accounts){
   it("first test", function(){
      return KingOfEther.deployed().then(function(instance){
      var koet=instance;
      var account_two=accounts[1];
      var account_one=accounts[0];
      //console.log(KingOfEther.deployed());
      return koet.king.call().then(function(val) {
        console.log(val);
        console.log(account_one);
        console.log(web3.fromWei(web3.eth.getBalance(account_one).toNumber()));
        console.log(web3.fromWei(web3.eth.getBalance(account_two).toNumber()));
          console.log(web3.fromWei(web3.eth.getBalance(val).toNumber()));



        return koet.claimthrone(22,{from:account_two});
      }).then(function(x){
        return koet.king.call().then(function(val) {

          console.log(web3.fromWei(web3.eth.getBalance(val).toNumber()));
          console.log(web3.fromWei(web3.eth.getBalance(account_one).toNumber()));
          console.log(web3.fromWei(web3.eth.getBalance(account_two).toNumber()));
        });
      });

    });});});

On executing i am getting exception on send call.

Contract: KingOfEtherContract first test:
     Error: VM Exception while processing transaction: invalid opcode
      at Object.InvalidResponse (/home/utkarash/.nvm/versions/node/v8.2.1/lib/node_modules/truffle/build/cli.bundled.js:37295:16)
      at /home/utkarash/.nvm/versions/node/v8.2.1/lib/node_modules/truffle/build/cli.bundled.js:224765:36
      at /home/utkarash/.nvm/versions/node/v8.2.1/lib/node_modules/truffle/build/cli.bundled.js:208348:9
      at XMLHttpRequest.request.onreadystatechange (/home/utkarash/.nvm/versions/node/v8.2.1/lib/node_modules/truffle/build/cli.bundled.js:209773:13)
      at XMLHttpRequestEventTarget.dispatchEvent (/home/utkarash/.nvm/versions/node/v8.2.1/lib/node_modules/truffle/build/cli.bundled.js:67130:18)
      at XMLHttpRequest._setReadyState (/home/utkarash/.nvm/versions/node/v8.2.1/lib/node_modules/truffle/build/cli.bundled.js:67420:12)
      at XMLHttpRequest._onHttpResponseEnd (/home/utkarash/.nvm/versions/node/v8.2.1/lib/node_modules/truffle/build/cli.bundled.js:67575:12)
      at IncomingMessage.<anonymous> (/home/utkarash/.nvm/versions/node/v8.2.1/lib/node_modules/truffle/build/cli.bundled.js:67535:24)

Can anyone please help where i am doing wrong? Thanks in advance

1

Is that because:

uint compensation =value_paid-claimPrice;

and since value_paid < claimPrice, so compensation would be a very large number (as it is uint, "overflow"). And contract doesn't have so high balance, so send() returns false and triggers throw().

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.