blockchainethereumsoliditynftmarketplace

Solidity: call a function of NFT contract from Marketplace contract, and only Marketplace has access to call it


I'am doing a NFT marketplace, and i have to implement that everytime a NFT is sold the images shuffle. The Marketplace has the functionallity to buy the NFTs so we have referenciate tha NFTs smart contract. When the function buy() is called, it calls the transfer() and suffle() from NFT smart contract. But i want only the marketplace to call the suffle(), so I decided to add a require that checks if the address that is calling is equal to the Marketplace address. The problem is that for deploying the Marketplace i need the NFT address and the same for deployig the NFT contract i need the Marketplace address. How can i solve this? Is a problem of the logic of the smart contract or there is a way to solve it in this way? Thank you!!

Smart Contracts Logic Diagram


Solution

  • You can use a setter function to set the dependency address after deployment.

    Example:

    contract NFT {
        address marketplace;
    
        function setMarketplace(address _marketplace) public onlyOwner {
            marketplace = _marketplace;
        }
    }
    
    contract Marketplace {
        address nft;
    
        function setNft(address _nft) public onlyOwner {
            nft = _nft;
        }
    }