AuditBase
Sign InGet Started
infoG220

Calling External Function With this Keyword

Learn about the Solidity security issue of "Calling External Function With this Keyword" and discover how to optimize gas usage by changing the function from external to public in this informative blog post.

Category

gas

Languages

solidity

Analysis Layer

static

Severity

info

Calling an external function internally, through the use of this wastes the gas overhead of calling an external function (100 gas). Instead, you can change the function from externalto public and remove the this keyword.

Consider the following example:

contract Example {
    function externalFunction() external {
        // Do something
    }
    
    function internalFunction() public {
        // Call external function using 'this'
        this.externalFunction();
    }
}

In the above code, the internalFunction calls the externalFunctionusing the thiskeyword. This creates unnecessary gas overhead.

To avoid this gas overhead, we can modify the code as follows:

contract Example {
    function externalFunction() public {
        // Do something
    }
    
    function internalFunction() public {
        // Call external function directly
        externalFunction();
    }
}

By changing the externalFunction from external to public, we can now call it directly without using the this keyword.

This simple change can save gas costs and improve the efficiency of your contract execution.

Remember to always consider gas optimization techniques when writing Solidity code.