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.
- 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?
- What does the
abi.encodeWithSignature()
method do? - 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:
So the
abi
object that contains theencodeWithSignature()
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 theabi.encodeWithSignature("foo(string,uint256)", "call foo", 123)
method is called in the caller contract. The other two arguments in theabi.encodeWithSignature()
are arguments passed to thefoo()
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.:
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.