[Ethereum] Warning: The use of low level “call” should be avoided whenever possible

contract-developmentcontract-invocationdelegatecallremixsolidity

I am writing my own smart contract and I want to call a function from a contract that has already been deployed. The fragment of my code:

function myFunc(address _contractAddress, address _user, uint _price) onlyOwner {
        //... some code ...

        require(_contractAddress.call(bytes4(sha3("func(address, uint256)")), _user, _price));

        //... some code ...
    }

I am using Remix IDE. It shows me this warning:

use of "call": the use of low level "call" should be avoided whenever possible. It can lead to unexpected behavior if return value is not handled properly. Please use Direct Calls via specifying the called contract's interface.

How should I solve this issue? Delegatecall produces similar warning. Maybe there are other ways on how to call other contract's function?

Best Answer

If you know the method to call you can use an abstract contract

contract OtherContract {
    function otherMethod(address _to, uint _price);
}

contract MyContract {
    uint public unitPrice = 100;

    function myMethod(address _destination, uint _count) {
        // _destination is a contract that implements OtherContract
        OtherContract oc = OtherContract(_destination);
        // call method from other contract
        oc.otherMethod(address(this), _count * unitPrice);
    }
}
Related Topic