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?


2 Answers 2


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(
        //admin of the sale is the deployer of this contract
        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('')

    const tx = await Factory.deployCollection(

          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()


Use the contractAddress value from the response.

enter image description here

  • 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.