Lesson 6: Outline

Lesson 6 introduction and outline

Lesson 6: Smart Contract Interactions

Objective: This lesson focuses on understanding how smart contracts interact with each other on the Ethereum blockchain. Learning about contract interactions is crucial for building decentralized applications (dApps) that require collaboration between multiple contracts.

Part 1: Calling External Contracts

  • Direct Calls:

    • Explain how to interact with other contracts directly using their addresses and function signatures. This method is suitable for simple interactions and when the interface of the external contract is known and stable.

  • Interface Contracts:

    • Introduce the concept of interfaces in Solidity, which define the functions of an external contract without their implementation. Using interfaces is a cleaner and more error-proof method for contract interactions.

  • Example of Direct Call:

pragma solidity ^0.8.0;

contract ExternalContract {
    function externalFunction() public pure returns (string memory) {
        return "External function called";
    }
}

contract Caller {
    function callExternalFunction(address _contractAddress) public view returns (string memory) {
        ExternalContract externalContract = ExternalContract(_contractAddress);
        return externalContract.externalFunction();
    }
}

Part 2: Using Interfaces for Contract Interactions

  • Defining and Implementing an Interface:

    • Show how to define an interface and use it to interact with an external contract. Interfaces allow a contract to call functions of another contract as if it were calling its own functions, promoting loose coupling between contracts.

  • Example Using an Interface:

interface IExternalContract {
    function externalFunction() external view returns (string memory);
}

contract ExternalContract is IExternalContract {
    function externalFunction() external pure override returns (string memory) {
        return "External function called";
    }
}

contract Caller {
    IExternalContract externalContract;

    constructor(address _contractAddress) {
        externalContract = IExternalContract(_contractAddress);
    }

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

Part 3: Handling Contract Dependencies

  • Contract Creation from Another Contract:

    • Discuss how contracts can create other contracts using the new keyword, establishing dependencies between them. This is useful for factory patterns and when deploying multiple instances of a contract from a single contract is needed.

  • Address and ABI:

    • Explain the importance of knowing the address and the Application Binary Interface (ABI) of the external contract for interaction. The ABI is a JSON representation that describes how to call functions in a contract and is automatically generated by the Solidity compiler.

Part 4: Considerations and Best Practices

  • Gas Considerations: Understand the gas implications of external calls and how they can affect the contract's execution cost.

  • Security Implications: Highlight common security considerations such as reentrancy attacks and how to mitigate them using patterns like checks-effects-interactions.

  • Upgradability and Modularity: Discuss strategies for designing contracts with upgradability and modularity in mind, using proxy contracts or the diamond standard (EIP-2535).

Last updated