Is it possible to call method from another contract by just having its address?

Let's say I've already published a Smart Contract with ERC721, and I have its contract address.
Now I need to create a new smart contract, which will check if msg.sender is the owner of the previously published contract, and if the sender does, also get the token number.

Is it even possible ?

1 Like

You need to import the code of your erc722 contract to your second contract in order for it to be able to interact with functions in the erc721 contract.

You can create an object in the second contract like so: ERCContract myContract = ERCContract(addr) where addr is the address of your deployed erc721 contract.

Admin Edit: Don't use the new keyword here.

1 Like

You can create a proxy using only the address which would serve as the implementation, but it would only inherit from functions from the other contract

1 Like

Yes! Calling one contract from another happens all the time. Contract B can call contract A on the same chain. @EgyptianCactus is correct that importing an interface that references the right function signatures will make that much easier for you. Since you already have the original contract in your codebase, should be relatively easy to include in your contract.

Composing these contracts to work with each other is what makes the technology so powerful!

Is there a good practice to get Contract details by only having its address?

For Example, I'm creating a new smart contract for my NFT collection, and all users who have AZUKI NFT can mint the same token from the newly created smart contract. For that, correct me if I'm wrong, I should check whether or not the msg.sender address is the owner of that NFT with ownerOf method for example right?
And for that, I should get the contract details and use its method.

I solve the issue this way. I'll be very thankful if you could give me some hints regarding the proper solution :slight_smile:

interface IContractName {
  function isActive() external view returns (bool);
  function balanceOf(address owner) external view returns (uint256 balance);
}

Then, on newly created contract I did the following:

address nftAddress = 0x1776383133aAF2fcE1C5EC5Dd3f5e19707BB8766;

  function checkOwner() external view returns(uint256) {
      return IContractName(nftAddress).balanceOf(msg.sender);
  }
1 Like

Is this the best way to get the already published smart contract and use its methods?

Everyone I want to thank you for your help :slight_smile: Appreciate it a lot

Of you want to do that you have to create an interface for the azuki nft before you can use some of it's contract function to check if the user is ownerof any azuki nft

1 Like