1

I am trying to deploy a contract from within another contract.

function createCollection(string memory _name, string memory _tokenName) external returns(address nftAddress)
{
    
    return address(new NFT(_name, _tokenName));
}

When I call this function with etherjs on the frontend I get an object back with parameters like txhash, from, to and so on. How can I get back just the address of the newly deployed contract?

Thanks

2 Answers 2

0

Here's an example of NFT factory contract that returns the address of a newest deployed NFT:


event SaleDeployed(address, address);

    function deployCollection(
        string memory nftName,
        string memory nftSymbol,
        string memory baseTokenURI,
        address _royaltyRecipient,
        bool _random
    ) public canDeployCollection(msg.sender) returns (address collection) {
        NFT nft = new NFT(
            nftName,
            nftSymbol,
            baseTokenURI,
            _royaltyRecipient,
            _random
        );
        //admin of the sale is the deployer of this contract
        nft.initialize(owner);
        nftCollections.push(nft);
        collectionsCreatedByUser[msg.sender].push(address(nft));
        emit CollectionDeployed(msg.sender, address(nft));
        return address(nft);
    }

With this test you can verify the address of the new collection by listening to the SalesDeployed event:


  context("newCollection", async () => {
    it("Should return an address", async function () {
      const _nft_name = "testname";
      const _nft_symbol = "testsymbol";
      const _base_token_uri = "testbaseuri";
      const _royaltyRecipient = "0x3d337D74851fdE1965e797521f2eE299549c2349";
      const _random = true;

    const provider = new ethers.providers.JsonRpcProvider('http://127.0.0.1:8545/')


    const tx = await Factory.deployCollection(
      _nft_name,
      _nft_symbol,
      _base_token_uri,
      _royaltyRecipient,
      _random
    );

          const rc = await tx.wait()
          const event = rc.events.find(event => event.event === 'CollectionDeployed')
          const [deployer, contractAddress] = event.args
          
          console.log(deployer, contractAddress, 'deployer & contractAddress')

          const MintUpNFT = new ethers.Contract(contractAddress, NFTABI.abi, provider)
          const name = await MintUpNFT.name()
          const symbol = await MintUpNFT.symbol()
          expect(name).to.equal(_nft_name)
          expect(symbol).to.equal(_nft_symbol)
  })
})

0

Use the contractAddress value from the response.

enter image description here

2
  • This returns 'undefined'.
    – PVit
    Jan 2, 2023 at 7:14
  • Not sure what version of Solidity / how you've implemented tested it & it works. Share your code if you want a review. Added image to OP.
    – VX3
    Jan 6, 2023 at 20:33

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.