Solidity – How to Use address.call{}() in Solidity Smart Contracts

abiaddressessolidity

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 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 arguments in brackets.

  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?
  2. What does the 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?

Best Answer

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 the encodeWithSignature() method is the standard way to interact with contracts in the Ethereum ecosystem.

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

This method takes either an empty sting or a function signature as its 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 the 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 argument. 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.