0

Let's say I've got a contract called MyContract. My contract has a function which receives calldata and an address for another contract, called OtherContract

I want to be able to take the calldata and address in MyContract, and execute the callData in OtherContract from MyContract.

I don't know anything about the OtherContract, or the function that uses the calldata.

Is this possible?

Thanks for your time!!!

1

Yes, it is possible to call an arbitrary function with only address and data.

function foo(address target, bytes memory data) external {
    // Make the function call
    (bool success, bytes memory result) = target.call(data);

    // success is false if the call reverts, true otherwise
    require(success, "Call failed");

    // result contains whatever has returned the function
    emit ExecutionResult(success, result);
}

You can check if the function call revert or not. Since you don't know anything else about the contract it is not possible to interpret the result but you can return them.

Making a call changes msg.sender. To the target contract it will seem the contract making the call is the sender.

1
  • Perfect, this is exactly what I needed, thankyou!
    – Shayno
    May 8 '21 at 10:50

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.