0

I am unable to deposit ether to a payee in an escrow contract. The error is:

transact to Escrow.deposit errored: VM error: revert. revert The transaction has been reverted to the initial state. Note: The called function should be payable if you send value and the value you send should be less than your current balance. Debug the transaction to get more information.

How do I solve this? With thanks.

pragma solidity ^0.5.0;

contract Escrow {
    
    address agent;
    mapping(address => uint256) public deposits;
    
    modifier onlyAgent() {
        require(msg.sender == agent);
        _;
    }
    
    constructor () public {
        agent == msg.sender;
    }
    
    function deposit(address payee) public onlyAgent payable {
        uint256 amount = msg.value;
        deposits[payee] = deposits[payee] + amount;
    }
    
    function withdraw(address payable payee) public onlyAgent {
        uint256 payment = deposits[payee];
        deposits[payee] = 0;
        payee.transfer(payment);
    }
}
1
  • Please paste any errors as Markdown formatted text as screenshots are unreadable. Also Also your question does not have a question in it. – Mikko Ohtamaa Apr 23 at 8:29
0

The problem is in the constructor. Since, you are assigning agent with msg.sender, you must use assignment operator and not equal to (==) operator.

Below is the correct code for Escrow.

pragma solidity ^0.5.0;
contract Escrow {

address agent;
mapping(address => uint256) public deposits;


modifier onlyAgent() {
    require(agent == msg.sender);
    _;
}

constructor () public {
    agent = msg.sender;
}

function deposit(address payee) external payable onlyAgent {
    uint256 amount = msg.value;
    deposits[payee] = deposits[payee] + amount;
}

function withdraw(address payable payee) public onlyAgent {
    uint256 payment = deposits[payee];
    deposits[payee] = 0;
    payee.transfer(payment);
}
}
2
  • Thank you so much!! :)) – Clifton Aurelius Apr 23 at 17:30
  • No problem, If the problem is solved, please mark this answer as correct as that will help other visitors too. – Yash Attal Apr 24 at 5:13

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.