0

I have a contract called Marketplace which gets deployed as a standalone contract with ethers.js. This contract instantiates another contract called NFT, therefore it's supposedly deployed implicitly by ethers.js. Here's how it's instanciated:

import "./NFT.sol";

contract Marketplace {
   NFT nft = new NFT();
   ...

As mentioned, I only deploy Marketplace, and this is how I do it in the deploy.js script:

const Marketplace = await ethers.getContractFactory("Marketplace");
const marketplace = await Marketplace.deploy();

I read that in Solidity when a contract inherits another contract, the code from both gets merged into one contract, but does this also happen when a contract instantiates another contract, like in my case above?

Initially I thought so, but looking around etherscan I found out that two contract addresses were deployed, and I wasn't expecting to have two standalone contracts but one, since I am explicitly only deploying Marketplace.

Am I supposed to change Hardhat's configuration or do something else in order to achieve that both Marketplace and NFT are deployed under one contract address, or that's not how the compiler works?

1
  • You are not using inheritance in that contract.
    – Ismael
    Commented Aug 1, 2022 at 13:38

1 Answer 1

1

These contracts will not be deployed at the same address - each address only contains a maximum of one contract. Let's break down what's happening here.

You're deploying one contract using Ethers (the marketplace). The marketplace then deploys an NFT via Solidity. Solidity works with the actual bytecode of the contract on-chain, which means that the marketplace's deployed bytecode will contain the bytecode of the NFT contract you will be deploying, but not as a separate contract, just as something it can deploy. When it does deploy the contract, it will use the same process as for deploying a new contract, which will then be deployed to a new address.

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.