1

I am an experienced developer but relatively new to blockchain development. There is a close source contract that I can interact with via the provider's web interface. The screenshot below is the receipt of one of the transactions.

If I can work out what the params are and inject my own data, how do I make a call to this contract from my own contract (in Solidity)?

Transaction Receipt


Update: As pointed out by Alex's answer, the screenshot above is the event emitted by the contract, not the function call metadata. The new screenshot below is of the transaction overview with Input Data.

Transaction made with Contract

I'm now trying to figure out how to make a call to this payable function using the suggested code below:

contract CallsFunctions {
    // for when you want to call any function on any contract
    function callAnything(address contractAddress, bytes memory args) payable external {
        (bool success, ) = contractAddress.call{ gas: 100000, value: msg.value }(args);
        require(success);
    }
}

In Remix, when I attempt to make a transaction, I get the following error:

Gas estimation errored with the following message (see below). The transaction execution will likely fail. Do you want to force sending? Internal JSON-RPC error. { "code": -32000, "message": "execution reverted" }

Making transaction from Remix


I have confirmed that the input data (args) is correct by attempting to make a transaction from the provider's web interface, then copy/paste the pre-signed data into Remix. I tried this on a test net with a custom callee contract and it worked fine.


Update: I tried to deploy a custom callee contract that disallows other contracts, the call returned the same error. I have came to a conclusion that the contract that I'm trying to interact with also blocks contracts.

2 Answers 2

3

You are viewing the logs for that transaction, which are data for the events emitted in response to a function call. Calling those doesn't do anything. If you have the etherscan page, what you should be looking for is the "Input Data" section in the "Overview" tab to see the actual method that was called and its input data (example in image). enter image description here

From there, you can create a smart contract that calls those functions specifically OR one that arbitrarily interacts with functions by passing in the function signature and its arguments as bytes (it will look like the value of the Input Data field from the screenshot). Dummy code for accomplishing both is below:

contract ArbitraryFunctions is IArbitraryFunctions {
    uint public stateVar;
    address public stateAddr;
    function first(uint val, address addr) public override {
        // does something
    }
}

contract CallsFunctions {
    // for when you know the name of the method you want to call
    function callArbitraryFunctionsContract(address contractAddress, uint callVal, address callAddr) public {
        ArbitraryFunctions(contractAddress).first(callVal, callAddr);
    }
    
    // for when you want to call any function on any contract
    function callAnything(address contractAddress, bytes memory args) public {
        (bool success, ) = contractAddress.call(args);
        require(success);
        // do something
    }
}
2
  • Oh, I see. So I would need to know the ABI to decode and encode my own custom input data, don’t I?
    – Fried Rice
    Commented Sep 16, 2021 at 22:35
  • Or I suppose I could trigger a transaction from the web interface and copy the hex data shown in MetaMask
    – Fried Rice
    Commented Sep 16, 2021 at 23:37
2

You would have to build an interface for the contract you want to call, then interact with it through this interface. Just finding out what the functions should take as input and what they're expected to return should be enough for that.

1
  • What would the interface looks like for this contract? Looking at the receipt and from my limited understanding, I know Topics-1 and Topics-2 are function parameters. I don't know what Topics-0 is, nor how to pass the Data 0-3 via this method call.
    – Fried Rice
    Commented Sep 16, 2021 at 12:36

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.