[Ethereum] Low Level Calls in Solidity

remixsolidity

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?

Best Answer

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.

Related Topic