Solidity Steps
  • Learning Solidity
  • Step 1
    • 1: Introduction
    • 2: Data Types
    • 3: Functions
    • 4: Control Structures
    • 5: State Variable
    • 6: Local Variables
    • 7: Global Variables
    • 8: View Keyword
    • 9: Pure Keyword
  • STEP 2
    • 10: Immunable Keyword
    • 11: Events
    • 12: Condition
    • 13: While Loop
    • 14: Do While Loop
    • 15: For Loop
    • 16: Required
    • 17: Assert
    • 18: Revert
    • 19: Modifier
  • STEP 3
    • 20: Constructor
    • 21: Mapping
    • 22: Array
    • 23: Enum
    • 24: Structs
    • 25: Data Location
    • 26: Inheritance
    • 27: The Shadowing Effect
    • 28: Super Keyword
    • 29: Visibility
  • STEP 4
    • 30: Interface
    • 31: Abstract Contract
    • 32: Payable
    • 33: Using type()
    • 34: Sending Ether
    • 35: Receive
    • 36: Fallback
    • 37: Call
    • 38: DelegateCall
    • 39: Calling Other Contracts
  • STEP 5
    • 40: Factory Contract
    • 41: Proxy Contract
    • 42: Create2
    • 43: Try and Catch
    • 44: Solidity Library
    • 45: ABI Encoded
    • 46: ABI Decoded
    • 47: Keccak256
    • 48: Function Signature Hash
  • TIPS
    • Tips: Solidity by "Immunable"
    • Tips: Truffle Tutorial
    • Tips: Microblog Dapp
    • Tips: Reentrancy
    • Tips: Slither Tutorial
    • Tips: Remix Tutorial
    • Tips: Hardhat Tutorial
  • CAREER
    • 💲Cover Letter
    • 💲Resume
  • ABOUT
    • Contact me
Powered by GitBook
On this page
  • Calling Other Contracts
  • Conclusion
  1. STEP 4

39: Calling Other Contracts

Calling Other Contracts

Calling other contracts is an important part of writing Solidity code. It enables you to interact with smart contracts on the Ethereum blockchain from other contracts. In this article, we'll explore a few examples of contract-to-contract interaction and the various ways of calling other contracts.

  • Example 1: Calling a Function from Another Contract The most basic way to call a function from another contract is to use the delegatecall keyword. This allows you to call a function from another contract and have the code execute in the context of the calling contract.

Let's look at a simple example. We have two contracts, ContractA and ContractB. ContractA wants to call a function in ContractB:

contract ContractA {    
    ContractB b; 
    constructor(ContractB _b) public {    b = _b;    } 
    function callContractB() public {        
        b.delegatecall(bytes4(keccak256("foo()")));    
    } 
} 
contract ContractB {    
    function foo() public {        // Do something    }
}

In this example, ContractA calls a function in ContractB using the delegatecall keyword. The delegatecall keyword takes a single argument, which is the function signature of the function to be called. In this example, we are calling the foo() function, so we pass in the function signature as an argument.

  • Example 2: Calling a Function from Another Contract with a Return Value In the previous example, we saw how to call a function from another contract without a return value. But sometimes you may want to call a function from another contract and get a return value. To do this, we can use the call keyword.

Let's look at a simple example. We have two contracts, ContractA and ContractB. ContractA wants to call a function in ContractB and get a return value:

contract ContractA {    
    ContractB b; 
    constructor(ContractB _b) public {        b = _b;    } 
    function callContractB() public {        
        (bool success, uint256 result) = b.call(
            bytes4(keccak256("foo()"))
        );    
    } 
} 
contract ContractB {    
    function foo() public returns (uint256) {        return 42;    }
}

In this example, ContractA calls the foo() function in ContractB using the call keyword. The call keyword takes two arguments, the function signature and a return value. In this example, we are calling the foo() function, so we pass in the function signature as the first argument and the return value as the second argument.

Conclusion

In this article, we looked at a few examples of contract-to-contract interaction and the various ways of calling other contracts. We saw how to call a function from another contract using the delegatecall keyword and how to call a function from another contract and get a return value using the call keyword.

That's it for the lesson 39! In the next lesson, Factory Contract

Previous38: DelegateCallNext40: Factory Contract

Last updated 1 year ago