I am a beginner to solidity and I came across this concept called "Low-Level Calls". What exactly is a Low-level function? and How is it different from others?
2 Answers
In Solidity, the address
object has a method named call()
through which you can call every function you want, and if the address implements it, it will be triggered.
For example:
contract SomeContract {
function any() public {
(bool success, bytes memory result) = addr.call(abi.encodeWithSignature("myFunction(uint,address)", 10, msg.sender);
// if success is `true` the function exists and it had returned some result
(uint a, uint b) = abi.decode(result, (uint, uint)); // This is an example of how the result might have been decoded
}
}
You can also use this syntax:
address.call.gas(50000).value(1 ether)(data);
Here, you forward some ETH and gas. The variable data
is something like this:
0x38ed1739000000000000000000000000000000000000000000000000127126c0ab345650000000000000000000000000000000000000000000000000000000012a8ef05e00000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000e1751fb76a2f4a8288c0af347def0d5fd5313a7000000000000000000000000000000000000000000000000000000006168497f00000000000000000000000000000000000000000000000000000000000000020000000000000000000000007ceb23fd6bc0add59e62ac25578270cff1b9f6190000000000000000000000002791bca1f2de4661ed88a30c99a7a9449aa84174
The first 4 bytes after the 0x
symbol represent the function signature - the first 4 bytes of the function signature after encoding it with the keccak256 hash function. In this case, it is the first 4 bytes of
abi.keccak256("swapExactTokensForTokens(uint256,uint256,address[],address,uint256)")
Everything after that is the representation of the calldata which will be parsed as the function arguments.
-
-
Some of the new solidity version introduced it. solidity-by-example.org/call Jan 11 at 18:24
Low level interactions are used to send funds or calldata or funds & calldata to a contract through the receive() or fallback() function.
Examples of low level functions are call and delegate call.
The difference between low level functions to a regular solidity function is that low level functions only works when fallback/receive are implemented.
Also when a call is being made from a low level function to a regular function, we display "transact to ... (fallback)" instead of writing the name of the function.
For in-depth info check out https://medium.com/remix-ide/low-level-interactions-on-remix-ide-5f79b05ac86