0

Lets say i've deployed an contract using a function in another contract:

function createCollection(
    uint256 _price, uint256 _maxSupply, string memory _name, string memory _symbol, address _requsetFrom, string memory _uri, bytes32 salt
    ) external payable returns(address)  {
        if (msg.value < s_createFee) {
            revert CollectionCreator__InsufficientAmount();
        }
        // CollectionV2 newNft = new CollectionV2(_name,_symbol,_price,_maxSupply,_requsetFrom, _uri);
        CollectionV2 newNft = (new CollectionV2){salt: salt}(_name,_symbol,_price,_maxSupply,_requsetFrom, _uri);
        address _contractAddress = address(newNft);
        ownerToCollection[_requsetFrom].push(_contractAddress);
    emit NewCollection(_requsetFrom, _contractAddress);
    return(_contractAddress);
}

And this is my backend code:

  const Collection = await ERC721Creator.createCollection(1,100,'alireza','arz',deployer.address,"chert")
  const tx = await Collection.wait(1)
  const CollectionV2 = tx.events[0].args[1]

The question is that, how i can create an instance for this contract to interact with? Because i can't create instance by getContract() like another contracts.

1 Answer 1

1

You need to use getContractAt() like this:

const Collection = await ERC721Creator.createCollection(1,100,'alireza','arz',deployer.address,"chert")
const tx = await Collection.wait(1)
const CollectionV2Address = tx.events[0].args[1]
const CollectionV2 = await ethers.getContractAt("CollectionV2", CollectionV2Address)

Make sure that "CollectionV2" is the right artifact name, so getContractAt() has the correct ABI. If CollectionV2 uses a different artifact name, use it instead in the getContractAt() call.

1
  • Thank you, it works...
    – Alireza
    Feb 15, 2023 at 12:26

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.