Getting the "VM Exception while processing transaction: revert" error for a pay() function

by Kevvv   Last Updated May 21, 2020 23:28 PM - source

I'm getting the following error during the testing in Truffle:

 Error: Returned error: VM Exception while processing transaction: revert

It simply says "revert" without the contingency message, which tells me that it's not coming from any of the "require" functions I've defined. I've also tried running the test with everything else commented out, except for the "pay()" function, including all the other test, but still didn't work.

testing

it("can pay the payout from the pending credit to the proper beneficiary", async () => {
    // ARRANGE
    let beneficiary = accounts[2];
    let flightAddress = accounts[4];
    let initialBalance;
    let postBalance;
    let eventEmitted = false;
    // ACT
    try {
      initialBalance = await web3.eth.getBalance(beneficiary);
    } catch (e) {
      throw new Error(e);
    }
    await config.flightSuretyData.buy(beneficiary, 100, flightAddress);
    await config.flightSuretyData.creditInsurees(beneficiary);
    await config.flightSuretyData.pay({ from: beneficiary }); // right here!
    await config.flightSuretyData.contract.events.InsurancePayoutPaid(
      function () {
        eventEmitted = true;
      }
    );
    try {
      postBalance = await web3.eth.getBalance(beneficiary);
    } catch (e) {
      throw new Error(e);
    }
    assert.equal(
      postBalance - initialBalance,
      result,
      "The insurance payout failed"
    );
    assert.isTrue(eventEmitted, "The event InsurancePayoutPaid is not emitted");
  });

contract

    function pay() external payable requireIsOperational {
        require(msg.sender != address(0), "Not a valid address");
        require(
            insurance[msg.sender].isValue == true,
            "The caller is not insured"
        );
        require(
            pendingCredit[msg.sender] != 0,
            "The pending credit amount is 0"
        );
        uint256 payout = pendingCredit[msg.sender];
        pendingCredit[msg.sender] = 0;
        msg.sender.transfer(payout);
        // address(uint160(msg.sender)).transfer(payout);
        emit InsurancePayoutPaid(msg.sender, payout);
    }

I thought maybe it had to do with the gas limit so I tried "await config.flightSuretyData.pay({ from: beneficiary, gasPrice: 0 });" or "await config.flightSuretyData.pay({ from: beneficiary, gas: 0 });", but none worked.

I've tried "web3.utils.fromWei(postBalance) - web3.utils.fromWei(initialBalance)" for the assertion or "web3.utils.fromWei(100)" for the argument for the "buy()" function, but that wasn't it either.

Finally, I've deleted the build, "truffle migrate --reset" and then tried testing again, but still the same result.



Related Questions


Contract state won't change during Truffle tests

Updated September 17, 2018 14:28 PM



Read returned bytes in web3js

Updated October 25, 2017 13:28 PM