How to interact with external contracts using interfaces
March 6th, 2023

Solidity interfaces are an essential tool that allows smart contracts to interact with existing smart contracts. In this blog post, we will explore how to use Solidity interfaces to interact with existing smart contracts.

What are Solidity interfaces?

Solidity interfaces are contracts that define the structure of another contract without providing any implementation. They are like a blueprint or a template that describes the functions, variables, and events of an existing smart contract. In particular, you can use interfaces to define the structure of an external contract that you wish to interact with inside your own contracts.

Interfaces are defined using the interface keyword, followed by the name of the interface, and the functions, variables, and events it defines. Here is an example of a simple Solidity interface:

interface ExternalContractInterface {
  function externalFunction() external returns (uint);
}

In this example, we define an interface named ExternalContractInterface that includes a function called externalFunction. The external keyword is used to indicate that this function can be called from outside the external contract, e.g., by the contracts you create yourself. Furthermore, the interface specifies that the function returns an unsigned integer.

How to use Solidity interfaces to interact with external contracts?

Here is an example of how you can use the ExternalContractInterface interface from an external contract inside your own contract code:

pragma solidity ^0.8.0;

interface ExternalContractInterface {
  function externalFunction() external returns (uint);
}

contract MyContract {
  ExternalContractInterface private externalContract;

  constructor(address _addressOfExternalContract) {
    externalContract = ExternalContractInterface(_addressOfExternalContract);
  }

  function callExternalFunction() public view returns (uint) {
    return externalContract.externalFunction();
  }
}

In this example, we first define the ExternalContractInterface interface and subsequently define a contract called MyContract that includes a private variable externalContract of type ExternalContractInterface. We also define a constructor that takes an address parameter _addressOfExternalContract to initialize externalContract.

The callExternalFunction function is a public view function that returns the result of calling the externalFunction function from externalContract.

By using the interface, we can interact with the functions of the external contract in a type-safe manner. This means that we can verify the contract address and the function signatures at compile time, reducing the risk of errors at runtime.

Conclusion

Solidity interfaces are an essential part of your toolbox as a Web3 developer. They enable you to code smart contracts that can interact with other, external contracts that are already live on mainnet. By using interfaces, you can ensure that the contract addresses and function signatures are correct at compile time, reducing the risk of errors at runtime. As you can see, Solidity interfaces are a powerful tool that you can leverage to build complex decentralized applications on any EVM-compatible blockchain.

Subscribe to Marco Besier
Receive the latest updates directly to your inbox.
Mint this entry as an NFT to add it to your collection.
Verification
This entry has been permanently stored onchain and signed by its creator.
More from Marco Besier

Skeleton

Skeleton

Skeleton