Search code examples
blockchainethereumsoliditysmartcontractschainlink

How to connect my smart contract with another deployed smart contract?


Assalamualaikum,

I am new to blockchain. So I was thinking of deploying a smart contract as rest api, and use it in my another smart contract. Is it possible? I know oracle helps to fetch data but can it help interacting two deployed contracts? Thank in advance.


Solution

  • You can define an interface of the target contract in the source contract. Example:

    TargetContract, deployed on the 0x123 address:

    pragma solidity ^0.8;
    
    contract TargetContract {
        function foo() external pure returns (bool) {
            return true;
        }
    }
    

    SourceContract, pointing to the 0x123 TargetContract

    pragma solidity ^0.8;
    
    interface ITargetContract {
        function foo() external returns (bool);
    }
    
    contract SourceContract {
        function baz() external {
            ITargetContract targetContract = ITargetContract(address(0x123));
            bool returnedValue = targetContract.foo();
        }
    }