Interface called Delegate call failed

16 views Asked by At

I am attempting to build a proxy in Solidity and get the following error:
transact to Wallet.sum errored: Error occurred: revert.

revert The transaction has been reverted to the initial state. Reason provided by the contract: "Delegate call failed".

The offending line is per the debugger: uint returnValue = proxy.callSum(x, y);
It appears that the IProxy private proxy; is zero address (see comments in code).
How can I initialize it?
Steps to reproduce:

For simplicity use the same wallet for all three files
1. Deploy calculator.sol
2. Deploy proxy.sol 
3. Deploy wallet.sol 
4. Set proxy address in deployed wallet.sol
5. Set calculator address in deployed proxy.sol
Call wallet.swap()

The contracts are:

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

interface IProxy {
    function callSum(uint x, uint y) external returns(uint);
}

contract Wallet{
    address private owner;
    address private proxyAddress;
    IProxy private proxy;

    constructor(){
        owner = msg.sender;
    }

    modifier onlyOwner() {
        require(msg.sender == owner, "Only owner can perform this action");
        _;
    }

    function getOwner() public view returns(address){
        return owner;
    }

    function setProxyAddress(address _proxyAddress) public onlyOwner {
        proxyAddress = _proxyAddress;
    }

    function getProxyAddress() public view onlyOwner returns(address){
        return proxyAddress;
    }

    function sum(uint x, uint y) public returns(uint){
        require(proxyAddress != address(0), "proxyAddress not set");
        // STATE VARIABLE proxyAddress:  0xEF9F1ACE83DFBB8F559DA621F4AEA72C6EB10EBF
        // interface IProxy is IProxy
        // contract IProxy private proxy
        // STATE VARIABLE proxy:  0x0000000000000000000000000000000000000000
        proxy = IProxy(proxyAddress);
        uint returnValue = proxy.callSum(x, y);
        return returnValue;
    }

    function getAddress() public view returns(address){
        return address(this);
    }
}

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

contract Proxy{
    address private calculatorAddress;
    address private walletAddress;

    function setCalculatorAddressAddress(address _calculatorAddress) public{
        calculatorAddress = _calculatorAddress;
    }

    function getCalculatorAddress() public view returns(address){
        return calculatorAddress;
    }

    function callSum(uint x, uint y) public returns (uint) {
        require(calculatorAddress != address(0), "calculatorAddressnot set");
        bytes memory data = abi.encodeWithSignature("sum(uint,uint)", x, y);
        (bool success, bytes memory returnData) = calculatorAddress.delegatecall(data);
        require(success, "Delegate call failed");

        return abi.decode(returnData, (uint));
    }

    function getAddress() public view returns(address){
        return address(this);
    }
}

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

contract Calculator{
    function sum(uint x, uint y) public returns(uint){
        return x+y;
    }
}
0

There are 0 answers