0

I recently learnt that it is possible to deploy more than one smart contract into the network. This brings the question, can they and how do these smart contracts interact? All the online courses I've covered so far handle deploying one smart contract only.

0

Any smart contract can interact with any other smart contract (unless it's somehow restricted in code). All you need is:

  1. The address of the other contract

  2. Knowledge of the other contract's functionality you want to access. Typically this is provided as an interface.

Here's a minimal example on how to interact with another contract which is already deployed in the blockchain:

pragma solidity ^0.7.0;

interface IOtherContr {
    function doStuff(uint256 some) external;
}

contract Test {
    function doSomething(address otherContr) public {
        IOtherContr ref = IOtherContr(otherContr);
        ref.doStuff(65);
    }
}

If, however, you wish to create a new instance of a contract (instead of referencing an existing one) you can use the new keyword, but then you need the full implementation of the other contract and it will create a new contract in a new address.

2
  • what if the smart contract I want to interact with does not have an interface and I want to call methods on the existing smart contract? – Coreggon 17 hours ago
  • Then you can just create the interface. It doesn't really matter who writes it. – Lauri Peltonen 8 hours ago

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.