Marketplace has launched, further enabling blockchain developers! Learn more

How to call another smart contract from your solidity code

September 23, 2022


Smart contracts are the most popular feature of the Ethereum network. Smart contracts have functions that sometimes need to be accessed by other smart contracts. Before understanding smart contracts' interactions, let's brush up on our basics about smart contracts. 

In this guide, we'll learn about smart contracts, understand why we want to call other smart contracts from our smart contract and deploy two separate contracts that interact with each other.

What is a Smart Contract?

Smart contracts are computer code, otherwise known as apps, deployed on the Blockchain network. Just like any other app, these smart contracts have information and conditions or rules. The main difference is that a smart contract's functions can be executed by anyone, anytime for any reason.

In the real-life, whenever an agreement between various stakeholders is supposed to occur, a third-party examines and validates it. For instance, a notary will ensure the identity of both signers on a contract. This makes it a time consuming and complicated process. Smart contracts eliminate the need for a third-party and enable the stakeholders to transact directly, and in this way, the process gets automated and more secure.

The smart contract gives the ability to transact with anyone globally without worrying about the trustworthiness and a middleman. Smart contracts inherit many Blockchain properties as they are designed and implemented on the blockchain, Being immutable is one of them which means smart contracts can never be changed and no one can break a contract.

Solidity is the standard language to write a smart contract, Learn more about Solidity, and write your first Solidity Smart Contract.

The following are the benefits of a smart contract:

Accuracy – Filling out forms manually can result in some errors, whereas working with automated contracts can be faster, cheaper, and accurate.

Trust – The contracts are deployed and stored on the blockchain nodes' network in an encoded format. They can never be lost.

Autonomy – Using smart contracts, you are the one making agreements; there's no need to be dependent on a broker, lawyer, or any third-party intermediaries.

Speed – Processing documents manually can require a considerable amount of paperwork and demand time. Since smart contracts are actually just computer programs, they can be automated - which is fast.

Backup – Imagine if a financial firm's data centre crashes and all the data of its customer's assets are gone. On the Ethereum Blockchain, each and everyone on the network has a copy of the data and smart contracts. In this way, the data is backed up and never lost.

Savings – Whenever a third-party is involved in the agreement, a notable amount is paid to them for their services, but since smart contracts eliminate the need for a third-party, the cost of transacting is reduced.

Why do Contracts need to interact with other contracts?

Just like an application or computer program can access other applications data through an API, a smart contract can make use of functions in other smart contracts that have been deployed on the blockchain. This reusability can save time and space on the blockchain since you don't need to write or copy and paste a function and you don't need to store a duplicate of the function on chain.

Getting some testnet ETH

We’ll deploy our contracts on the Ropsten testnet. To get started, you will need the Metamask browser extension to create an ETH wallet and some test ETH, which you can get by going to Ropsten faucet. You'll need to select Ropsten Test Network on your Metamask wallet and copy-paste the wallet address into the text field in the faucet then click Send me test Ether.

Deploying our contracts (Counter.sol & Interface.sol)

Now to achieve our goal we'll make two contracts in Solidity.

  1. 'Counter.sol' will be deployed first, a short counter incrementing script.
  2. 'Interface.sol' we will use the Solidity interface pattern and supply the function signature of the counter from the deployed contract 'Counter.sol'.

Head over to the Ethereum Remix IDE and make a new Solidity file Counter.sol

Paste the following code into your new Solidity script:

deploying our contracts counter sol interface sol

pragma solidity ^0.6.8;

// SPDX-License-Identifier: MIT

contract Counter {
    uint public count;
    function increment() external {
        count += 1;

Explanation of the code above: 

Line 1: Declaring the solidity version

Line 3: Specifying SPDX license type, which is an addition after Solidity ^0.6.8, Whenever the source code of a smart contract is made available to the public these licenses can help resolve/avoid copyright issues. If you do not wish to specify any license type, you can use a special value UNLICENSED or simply skip the whole comment (it won’t result in an error, just a warning).

Line 5: Starting our Contract named Counter

Line 6: Creating a public variable count of unsigned integer type, which will store the value of increment.

Line 8-10: Creating a function increment with an external modifier, increasing the value of count by one. External functions are part of the contract interface, which means they can be called from other contracts and via transactions - this is key for our goal of calling one contract from another.

Compile the smart-contract and deploy it using Injected Web3, you might also want to change the version of the compiler to 0.6.0 using the menu on the left.

Now that we have our 1st contract deployed, let's create another contract to use the Counter.sol contract's functions.

Create a new Solidity file Interface.sol, Paste the following code into your new Solidity script:

deploying our contracts counter sol interface sol

pragma solidity ^0.6.0;

// SPDX-License-Identifier: MIT

interface ICounter {
    function count() external view returns (uint);
    function increment() external;

contract Interaction {
    address counterAddr;

    function setCounterAddr(address _counter) public payable {
       counterAddr = _counter;

    function getCount() external view returns (uint) {
        return ICounter(counterAddr).count();

Explanation of the code above: 

Line 1: Declaring the solidity version.

Line 3: Specifying SPDX license type, which is an addition after Solidity ^0.6.8, Whenever the source code of a smart contract is made available to the public these licenses can help resolve/avoid copyright issues. If you do not wish to specify any license type, you can use a special value UNLICENSED or simply skip the whole comment (it won’t result in an error, just a warning).

Line 5-8: Creating an Interface ICounter with the function signatures of the contract Counter.sol, supplying two function signatures count and increment.

Line 10: Starting our contract named Interaction.

Line 11: Declaring a private variable `counterAddr` to store the address of the contract we want to call a function on.

Line 13-15: Creating a function setCounterAddr which accepts `_counter`  as an argument. `_counter` will be the address we want to set  `counterAddr` to - which will be stored on chain. 

Line 17-19: Declaring the `getCount` function which is the reason we started this journey in the first place, instantiating the interface with the `counterAddr` address and immediately calling the `count` function on it to fetch the count from the deployed `Counter.sol` contract. getCount is a view type method and has external state, view and pure methods (which includes getters for public state variables) can be called without making a transaction.

Compile the smart-contract and deploy it using Injected Web3. 

Note: make sure to select Interaction under the Contract section from the left menu before deploying

Setting the counter contract address

Now you will see two contracts under the "Deployed Contracts" section in Remix. Copy the address of deployed contract COUNTER from the copy button beside the contract's name, expand the INTERACTION contract, paste the address in the input box near the setCounterAddr button, and click on setCounterAddr - this will write the address of the previously deployed smart contract to the variable counterAddr on the blockchain. This transaction will require some gas.

Reading the count from Counter.sol via Interface.sol

Now click on count button under the COUNTER contract then click on getCount button under the INTERACTON contract, You'll see the same value of count in both the outputs. This shows an interaction between the contracts, and the value of count was passed between contracts.

Updating the count in Counter.sol

Now let's update the value of count on the COUNTER contract, click on increment; this will send a write request to the chain and require some gas. Then click on count. You must see the value of count increased by one.

Note: You will need to wait for the transaction to be confirmed on the blockchain (which can take up to 45 seconds or so) before clicking count.

Seeing the updated value

Let's check if the INTERFACE contract can fetch the updated value of count, click on getCount button under the INTERACTION contract and you must see the updated value.

Note: This assumes the transaction from the previous section is confirmed.

Use of interface allows users to use a deployed contract’s functionality. If someone wishes to use a contract’s function, they can get the interface of that contract from its creator and integrate it into their contract without implementing everything from scratch.


Here we successfully saw how we can call another smart contract's function using Solidity.

Subscribe to our newsletter for more articles and guides on Ethereum. If you have any feedback, feel free to reach out to us via Twitter. You can always chat with us on our Discord community server, featuring some of the coolest developers you'll ever meet :)

Related articles 18

Solidity vs Vyper
Originally Published On: Aug 18, 2021
Updated On: Sep 23, 2022

With the introduction to smart... When we think of blockchains,... Introduction IntroductionVyper is... Now, the million dollar question...

Continue reading
How to write an Ethereum smart contract using Solidity
Originally Published On: Apr 5, 2020
Updated On: Sep 23, 2022

This article is intended for... Before getting started with... The smart contract term was... As we saw that smart contracts... Now, let's write a simple smart... After writing a smart contract... So we have successfully created...

Continue reading
How to Setup Local Development Environment for Solidity
Originally Published On: Jul 10, 2021
Updated On: Sep 23, 2022

The success story of blockchain... According to the Now that you are geared up with... Installation Now that our contract is ready... Now, let's write some test... The last thing, we need to do is... For deploying using hardhat, you... Here we saw how to work with...

Continue reading
How to Deploy a Smart Contract on MATIC/Polygon
Originally Published On: Oct 27, 2021
Updated On: Sep 23, 2022

Ethereum is a very lovely... With the rapid adoption of... We'll deploy our contract on... After creating your QuickNode... We will need some test MATIC to... Time to write our smart... Now click on the Solidity logo... A big thank you and kudos if you...

Continue reading
How to Create a BEP20 Token
Originally Published On: Jul 3, 2021
Updated On: Sep 23, 2022

BEP20 is the Binance Smart Chain... The first thing you will want to... The Next thing we will need to... Because BSC uses the EVM, the... With the initial BEP20.sol... In order to view this in your... Congratulations! You made it to...

Continue reading
What is an ABI?
Originally Published On: Mar 20, 2021
Updated On: Sep 23, 2022

While interacting with a smart... ABI (Application Binary... The JSON format of a contract’s... One of the most common ways is... Now that you know what ABI is,...

Continue reading
How to create and deploy an ERC-721 (NFT)
Originally Published On: Mar 6, 2021
Updated On: Sep 23, 2022

Digital collectibles compatible... Fungible means to be the same or... ERC stands for Ethereum Request... Digital art (or... Now that we know what ERC-721... Before writing our NFT contract,... For ease and security, we’ll use... Congratulations on creating your...

Continue reading
How to deploy a smart contract with Brownie
Originally Published On: Jul 23, 2021
Updated On: Sep 23, 2022

Python is one of the most... Smart contract development is... Brownie is built on top of... First of all, lets cd into the... We will deploy our contract on... We need to set up our QuickNode... As our contract will be deployed... Before deploying the contract,... So, today we learned brownies...

Continue reading
How to Write a Secure Smart Contract in Vyper
Originally Published On: Feb 2, 2022
Updated On: Sep 23, 2022

Hey readers! If you are familiar... In this project we will be... First things first, if we are... We can begin by writing our... Here we will write the code... Congrats on making it to the...

Continue reading
Como crear un Token BEP20
Originally Published On: Jan 10, 2022
Updated On: Sep 23, 2022

BEP20 es en Binance Smart Chain... La primera cosa que hay que... El próximo paso que deberás... Como BSC usa la EVM, el código... Con el contrato BEP20.sol... Para poder ver esto en la... ¡Felicidades! Acabas de terminar...

Continue reading
How to Create a Dutch Auction Smart Contract
Originally Published On: Feb 11, 2022
Updated On: Sep 23, 2022

Often to perform any transaction... Auctions are platforms for... A Dutch auction, also known as... We will deploy our contract on... Before moving further, it is... Now that our Dutch auction... If you made it here,...

Continue reading