Skip links

Table of Contents

Low Level Call vs High Level Call in Solidity [Simplified]

In Solidity, contracts can interact with other contracts in two main ways: through high level calls using the contract’s interface or low level calls using methods like call, delegatecall, and staticcall. Though both approaches use the CALL opcode at the Ethereum Virtual Machine (EVM) level, their handling and behavior in Solidity differ significantly.

Understanding High Level and Low Level Calls

High Level Calls:

High-level calls in Solidity are made using the contract’s interface. This method is user-friendly and includes automatic error handling. For example:

contractInstance.someFunction();

When this call is made, Solidity generates the appropriate bytecode to handle success and failure cases, ensuring that any errors in the called function result in a revert of the transaction.

Low Level Calls:

Low-level calls provide more control but require explicit handling of errors. They are made using methods like call:

(bool success, bytes memory data) = targetAddress.call(abi.encodeWithSignature("someFunction()"));

This method returns a boolean indicating the success of the call and any returned data, but it does not automatically revert on failure.

low level call vs high level call

Detailed Comparison

Behavior on Failure: When a high-level call fails, Solidity automatically reverts the transaction, bubbling up the error. This behavior simplifies error handling for developers:

contractInstance.someFunction(); // This will revert if someFunction fails

In contrast, a low-level call requires manual checking of the success value:

(bool success, ) = targetAddress.call(abi.encodeWithSignature("someFunction()"));<br>require(success, "Call failed");

If the low-level call fails, the success value will be false, and the transaction will continue unless explicitly handled.

Calling Non-Existent Contracts: High-level calls check if the target address contains a contract before executing the call. If the address does not contain code (checked via the EXTCODESIZE opcode), the call will revert:

interface IContract {
    function someFunction() external;
}

IContract(targetAddress).someFunction(); // Reverts if targetAddress has no contract

Low-level calls, however, do not perform this check and will return false if the address has no contract:

(bool success, ) = targetAddress.call(abi.encodeWithSignature("someFunction()"));
require(success, "Call to non-existent contract failed");

Practical Example

Here’s a practical comparison of high-level and low-level calls within a Solidity contract:

High-Level Call Example:

pragma solidity ^0.8.0;

contract HighLevelCaller {
    function callFunction(address _target) public {
        ITarget(_target).targetFunction(); // Reverts if targetFunction fails
    }
}

interface ITarget {
    function targetFunction() external;
}

Low-Level Call Example:

pragma solidity ^0.8.0;

contract LowLevelCaller {
    function callFunction(address _target) public {
        (bool success, ) = _target.call(abi.encodeWithSignature("targetFunction()"));
        require(success, "Low-level call failed");
    }
}

Key Takeaways

  1. Ease of Use vs. Control: High-level calls offer ease of use and built-in safety, making them ideal for standard interactions. Low-level calls provide greater control and flexibility, useful for advanced scenarios requiring manual handling of call results.
  2. Error Handling: High-level calls automatically revert on failure, simplifying error management. Low-level calls require explicit success checks and error handling.
  3. Contract Existence Check: High-level calls check for the existence of the contract at the target address, reverting if none is found. Low-level calls do not perform this check, requiring manual verification if necessary.

By understanding the differences between high-level and low-level calls, Solidity developers can choose the appropriate method for their specific use cases, balancing ease of use with the need for control and flexibility.

faq

FAQs

What is a low level call in Solidity?

  • A low level call in Solidity refers to using functions like calldelegatecallstaticcall, or callcode to interact with other contracts. These are more flexible but riskier.

What is a high level call in Solidity?

  • A high level call uses Solidity’s function calls to interact with other contracts, which are safer and easier to use than low-level calls.

When should I use low-level calls in Solidity?

  • Low-level calls should be used when you need more control over the call process or when interacting with contracts that don’t expose an ABI.

What are the risks of using low-level calls in Solidity?

  • Low-level calls can fail silently, making them vulnerable to security issues such as reentrancy attacks if not handled correctly.

How can I make low-level calls safer in Solidity?

  • To make low-level calls safer, always check the return value, use appropriate security patterns, and consider using high-level calls when possible.

Why are high-level calls preferred in Solidity development?

  • High-level calls are preferred because they are simpler to use, provide better error handling, and integrate seamlessly with Solidity’s type system.

What are common use cases for high-level calls in Solidity?

  • High-level calls are commonly used for standard contract interactions, such as transferring tokens or interacting with DeFi protocols.

Can I combine high-level and low-level calls in a Solidity contract?

  • Yes, combining both types of calls can be useful. High-level calls offer simplicity, while low-level calls provide flexibility when needed.

What are some best practices for using calls in Solidity?

  • Best practices include favoring high-level calls, handling all call returns, implementing checks-effects-interactions patterns, and avoiding unnecessary complexity.

How do high-level calls handle errors in Solidity?

  • High-level calls automatically revert on failure, providing built-in error handling which simplifies debugging and improves contract reliability.

Metana Guarantees a Job 💼

Plus Risk Free 2-Week Refund Policy ✨

You’re guaranteed a new job in web3—or you’ll get a full tuition refund. We also offer a hassle-free two-week refund policy. If you’re not satisfied with your purchase for any reason, you can request a refund, no questions asked.

Web3 Solidity Bootcamp

The most advanced Solidity curriculum on the internet!

Full Stack Web3 Beginner Bootcamp

Learn foundational principles while gaining hands-on experience with Ethereum, DeFi, and Solidity.

You may also like

Metana Guarantees a Job 💼

Plus Risk Free 2-Week Refund Policy

You’re guaranteed a new job in web3—or you’ll get a full tuition refund. We also offer a hassle-free two-week refund policy. If you’re not satisfied with your purchase for any reason, you can request a refund, no questions asked.

Web3 Solidity Bootcamp

The most advanced Solidity curriculum on the internet

Full Stack Web3 Beginner Bootcamp

Learn foundational principles while gaining hands-on experience with Ethereum, DeFi, and Solidity.

Learn foundational principles while gaining hands-on experience with Ethereum, DeFi, and Solidity.

Events by Metana

Dive into the exciting world of Web3 with us as we explore cutting-edge technical topics, provide valuable insights into the job market landscape, and offer guidance on securing lucrative positions in Web3.

Subscribe to Lettercamp

We help you land your dream job! Subscribe to find out how

Start Your Application

Secure your spot now. Spots are limited, and we accept qualified applicants on a first come, first served basis..

Career Track(Required)

The application is free and takes just 3 minutes to complete.

What is included in the course?

Expert-curated curriculum

Weekly 1:1 video calls with your mentor

Weekly group mentoring calls

On-demand mentor support

Portfolio reviews by Design hiring managers

Resume & LinkedIn profile reviews

Active online student community

1:1 and group career coaching calls

Access to our employer network

Job Guarantee