Inter-Contract Execution
Learn how Solidity smart contracts can interact with each other.
Native interoperability
A major advantage of blockchain development is that smart contracts can natively call each other's functions. Combined with the transparency of the blockchain, this makes Ethereum applications interoperable out of the box. Anyone can build new applications on top of existing ones, even if we don't know their source code.
Application binary interface (ABI)
Ethereum uses a standard format called the application binary interface (ABI) as a lower-level representation of compiled contracts. Just like an application programming interface (API), in traditional web development, it is a lower-level representation of a software’s interface. Using this representation, we can unambiguously refer to the methods and data structures in a contract, in a format that the EVM understands. This way, any deployed contracts can be called both from outside the blockchain (by Web3 clients) and for inter-contract code execution.
Calling external code
Since code execution in Solidity must be initiated by a transaction, we have to use an external call to make our contract call a function defined in another contract, just like we would do to send ETH. There are two ways to make such a call, and the difference between them is fairly simple but important. These are:
CALL: When contract A initiates the execution of a function in contract B using this method, the function is executed using B's storage.
DELEGATECALL: When contract A initiates the execution of a function in contract B using this method, the function is executed using A's storage, while preserving its initial
msg.sender
andmsg.value
.
Let's discuss these two methods in detail.
The CALL method
This is the same method we've used to send ETH out of a contract, with the same syntax, using the .call
(opcode: CALL) command:
Get hands-on with 1200+ tech skills courses.