0

I want to interact with a third party contract.

I can interact with (third party) Smart Contract A.

Inside Smart Contract A's code is

import { contractb } from ".../ContractB.sol";

ContractB public contractb; 

some functions inside Contract A interact with contractb by using something like

contractb.function();

I would like to interact with a function that is available inside contractb. Can I do this, or would this only be possible if Contract A is calling the function in one of it's functions?

1 Answer 1

0

Yes, that's right!You can use another contract within a contract.

Solidity is influenced by the C++, Python and Javascript languages,A lot of syntax is similar.

The following code works correctly,You can use it as a reference

contract Test {

    ContractA public contractA = new ContractA();

    function test() public view returns (bool){
        return contractA.contractB().test();
    }

}

contract ContractA {
    ContractB  public contractB;

    constructor() public {
        contractB = new ContractB();
    }
}

contract ContractB {

    constructor() public {}

    function test() public view returns (bool){
        return true;
    }

}
2
  • I am trying to interact with a third party contract. As I said I can interact with Contract A, but I also want to interact with contractb that is inside Contract A. Sadly Contract A's code is not calling the function I want to use, so I was hoping I could call contractb directly. when I interact with Contract A, I type contracta.function() in javascript, I was hoping for something like contracta.contractb.function(), but that did not work.
    – Sebastian
    Commented Aug 2, 2021 at 10:33
  • You can use the interface of contract B to interact with B, and if contract B doesn't define an interface you can write one based on its function.Then instantiate it with InterfaceName(contractB_address), and you can use the contractB functions.
    – zmy
    Commented Aug 3, 2021 at 1:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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