#100DaysOfSolidity 📝 Calling Other Contracts: Unleashing the Power of Solidity Interactions

#100DaysOfSolidity Series 034 “Calling Other Contracts”

Solidity Academy
3 min readJul 10, 2023

🔍 Welcome to a fascinating journey into the world of contract-to-contract communication in Solidity! In this Medium article, we’ll explore the intricacies of “Calling Other Contracts” using the Solidity programming language. Solidity opens up a realm of possibilities for decentralized applications (dApps) by allowing contracts to interact with one another. 🌐🚀

#100DaysOfSolidity 📝 Calling Other Contracts: Unleashing the Power of Solidity Interactions

The Simple Approach: Direct Function Call 📞

One of the easiest ways to call a function in another contract is by directly invoking it. Let’s take a look at the code snippet below to see how it’s done:

pragma solidity ^0.8.17;
contract Callee {
uint public x;
uint public value;
function setX(uint _x) public returns (uint) {
x = _x;
return x;
}
// Additional functions for illustration purposes
// …
}
contract Caller {
function setX(Callee _callee, uint _x) public {
uint x = _callee.setX(_x);
}
// Additional functions for illustration purposes
// …
}

🔍 Understanding the Code:

- The `Callee` contract defines a function `setX` that sets the value of `x` and returns it.
- The `Caller` contract includes a function `setX` which receives an instance of the `Callee` contract and a new value `_x`. It then invokes the `setX` function of the `Callee` contract, passing `_x` as an argument.

By calling functions directly between contracts, we establish a straightforward communication mechanism. However, please note that this approach is limited to contracts residing on the same blockchain. For scenarios involving contracts on different blockchains or networks, we need a different method. Let’s explore another approach to contract interaction that accommodates such situations.

The Low-Level Call: Bridging Contracts across Networks 🌐

Solidity provides a low-level `call` function, which allows contracts to communicate with each other across different networks or blockchains. While this method is not recommended for most scenarios, it becomes essential when integrating with contracts residing on separate chains.

To illustrate the low-level call, let’s examine the code snippet below:

pragma solidity ^0.8.17;
contract Caller {
function setXFromAddress(address _addr, uint _x) public {
Callee callee = Callee(_addr);
callee.setX(_x);
}
function setXandSendEther(Callee _callee, uint _x) public payable {
(uint x, uint value) = _callee.setXandSendEther{value: msg.value}(_x);
}
}

🔍 Understanding the Code:

- The `Caller` contract contains two functions: `setXFromAddress` and `setXandSendEther`.
- The `setXFromAddress` function receives the address of the `Callee` contract `_addr` and a new value `_x`. It then creates an instance of the `Callee` contract using the address `_addr` and invokes its `setX` function, passing `_x` as an argument.
- The `setXandSendEther` function takes an instance of the `Callee` contract `_callee` and a new value `_x`. It also accepts Ether by using the `payable` modifier. The function invokes the `setXandSendEther` function of the `Callee` contract, transferring the received Ether along with the `_x` value. The returned values `x` and `value` are captured in variables for further processing.

Utilizing the low-level call allows us to interact with contracts deployed on different networks or blockchains. However, it’s crucial to exercise caution when employing this approach, as it involves additional complexities and potential security risks. Always perform a thorough analysis of the contracts you intend to interact with and ensure their trustworthiness before making cross-chain calls.

Conclusion 🎉

In this unique Medium article, we embarked on a fascinating exploration of calling other contracts in Solidity. We discovered the simplicity of direct function calls, enabling easy interaction between contracts residing on the same blockchain. Additionally, we delved into the low-level call approach, which bridges the gap between contracts across different networks or blockchains. By harnessing the power of these techniques, developers can unlock a multitude of innovative use cases and fully leverage the potential of decentralized applications. Remember to exercise caution when making cross-chain calls and thoroughly analyze the contracts you interact with. Happy coding! 💻🚀

🔗 Additional Resources:

Disclaimer: The code examples provided in this article are for educational purposes only. Always exercise caution and perform comprehensive testing when deploying and interacting with smart contracts on the blockchain.

--

--

Solidity Academy

Your go-to resource for mastering Solidity programming. Learn smart contract development and blockchain integration in depth. https://heylink.me/solidity/