1

In this solidity code the finalizeRequest() is giving a run-time error. Here is the error :

Campaign.finalizeRequest(uint256) 0x692...77b3a

transact to Campaign.finalizeRequest errored: VM error: revert.
revert  The transaction has been reverted to the initial state.
Note: The constructor should be payable if you send value.  Debug the transaction to get more information.

Here is the code:

pragma solidity ^0.5.14;

contract Campaign {
    struct Request {
        string description;
        uint value;
        address payable recipient;
        bool complete;
        uint approvalCount;
        mapping (address => bool) approvals;
    }

    address public manager;
    uint public minimumContribution;
    // address[] public approvers;
    mapping (address => bool) public approvers;
    Request[] public requests;
    uint public approversCount;


    constructor(uint minimum) public {
        manager=msg.sender;
        minimumContribution=minimum;
    }

    function contribute() public payable {
        require(msg.value>minimumContribution);
        // approvers.push(msg.sender);
        approvers[msg.sender]=true;
        approversCount++;
    }

    function createRequest(string memory description,uint value,address payable recipient) public restricted {
        // require (approvers[msg.sender]);    
        Request memory newRequest=Request({description:description,value:value,recipient:recipient,complete:false,approvalCount:0});
        requests.push(newRequest);

    }

    function approveRequest(uint index) public {
        Request storage request=requests[index];

        require(approvers[msg.sender]);
        require(!request.approvals[msg.sender]);
        request.approvals[msg.sender]=true;
        request.approvalCount++;
    } 

    function finalizeRequest(uint index) public restricted payable{
        Request storage request = requests[index];

        require(request.approvalCount>=(approversCount/2));
        require(!request.complete);
        request.recipient.transfer(request.value);
        request.complete=true;

    } 

    modifier restricted() {
        require (msg.sender == manager);
        _;
    }
}

As far as I could debug, when I comment out request.recipient.transfer(request.value); in the finalizeRequest() function no runtime error is thrown. So error seems to be in this line of code ,kindly help resolving this.

1 Answer 1

1

The error message is misleading. You can safely disregard it since you aren't calling the constructor or sending funds.

It's a lot of code to wade through so this won't be very specific.

You say this seems to be the issue:

request.recipient.transfer(request.value);

The most likely cause is the contract doesn't have sufficient funds to make that happen. It cannot transfer ether it doesn't have. So, back up a step and make sure contribute is doing what you think it does. Consider adding:

function contractBalance() public view returns(uint) {
  return address(this).balance;
}

Also watch the request.value. Be sure it isn't more than the contract has. Possibly accounting errors are causing trouble.

You can use a newer compiler (0.4.17 is very old) and add revert reasons:

require(true, "How could that not be true? Something seriously haywire.");

This will not catch the specific transfer error which is protocol error. You might consider commenting out the offending statement, and logging an event again.

Something like:

// request.recipient.transfer(request.value);
emit LogDebug(request.value, address(this).balance);

Hope it helps.

2
  • 1
    Making finalizeRequest() as payable should be at least done as this function involves transaction of ethers. Commented Jan 22, 2020 at 9:40
  • I do not see msg.value in finalizeRequest. What am I missing? You do not need payable for a function that is sending and you should not add it to any function that does not expect to receive ether. Commented Jan 22, 2020 at 16:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.