3

I have seen here and several other places that the correct way to send ether from a contract to an address is to call the .call{}() method.

Then here I have seen the method being used with arguments. Like so:

addr.call{value: msg.value, gas: 5000}(
    abi.encodeWithSignature("foo(string,uint256)", "call foo", 123)
);

I understand that the arguments in the curly braces. value: is the eth you want to send and gas: is the amount of gas you are willing to pay for the transaction to be mined. (correct me if I am wrong).

What I do not understand are the arguements in brackets.

  1. What is the abi object insside the contract and what does it do? Is it the same as the one you get when you compile the contract?
  2. What does abi.encodeWithSignature()` method do?
  3. How many arguments can the brackets take and what do they mean? And do you have to provide arguments to it or can you leave it empty?
2
  • address.call(bytes memory msg) accepts a single input parameter: the message to sent to a contract (usually a function call).
  • abi.encodeWithSignature(functionSignature, param1, param2, ...) accepts any number of parameters but the first one is required. It creates a function call message that encodes the function and the parameters.

The code can be written like:

interface IFoo {
    function foo(string, uint256) external payable;
}

IFoo(addr).foo{value: msg.value, gas: 5000}("call foo", 123)

For more details check the documentation https://docs.soliditylang.org/en/v0.8.3/types.html#address

1

After doing some research I finally have the answers to the questions.

1. What is the abi object inside the contract and what does it do? Is it the same as the one you get when you compile the contract?

According to the solidity docs:

The Contract Application Binary Interface (ABI) is the standard way to interact with contracts in the Ethereum ecosystem, both from outside the blockchain and for contract-to-contract interaction. Data is encoded according to its type, as described in this specification. The encoding is not self describing and thus requires a schema in order to decode.

So the abi object that contains encodeWithSignature() method is he standard way to interact with contracts in the Ethereum ecosystem.

2. What does abi.encodeWithSignature() method do?

This method takes either an empty sting or a function signature as it first argument. The function signature is usually a function in the called contract. Although when it is not there are ways solidity deals with that. When it is a function in the called contract, that function is called when the abi.encodeWithSignature() method is called. So in the code example in the question, the called contract has a function:function foo(string var1, uint256 var2){} that is called every time abi.encodeWithSignature("foo(string,uint256)", "call foo", 123) method is called in the caller contract. The other two arguments in the abi.encodeWithSignature() are arguments passed to the foo() function when it is being called.

3. How many arguments can the brackets take and what do they mean? And do you have to provide arguments to it or can you leave it empty?

The brackets in the call method, i.e :

addr.call{value: msg.value, gas: 5000}(/** I am talking about this bracket*/);

requires a single bytes arguement. So if you do not want to pass the abi.encodeWithSignature() method you should pass an empty string or the compiler will throw an error.

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.