简体   繁体   English

Solidity:从 Marketplace 合约调用 NFT 合约的一个函数,只有 Marketplace 有权调用它

[英]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.我正在做一个 NFT 市场,每次出售 NFT 时,我都必须实施该图像洗牌。 The Marketplace has the functionallity to buy the NFTs so we have referenciate tha NFTs smart contract.市场具有购买 NFT 的功能,因此我们参考了 NFT 智能合约。 When the function buy() is called, it calls the transfer() and suffle() from NFT smart contract.当调用函数 buy() 时,它会从 NFT 智能合约调用 transfer() 和 suffle()。 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.但我只希望市场调用 suffle(),所以我决定添加一个检查调用的地址是否等于市场地址的要求。 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.问题是,为了部署 Marketplace,我需要 NFT 地址,而对于部署 NFT 合约,我需要 Marketplace 地址。 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智能合约逻辑图

You can use a setter function to set the dependency address after deployment.部署后可以使用setter函数设置依赖地址。

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;
    }
}

I wrote a simple framework that should help you.我写了一个简单的框架,应该可以帮助你。 :) :)

 //SPDX-License-Identifier: UNLICENSED

pragma solidity 0.6.12;


contract NFT {

    //Ensure that only the owner can call important functions
    address owner; 
    

    constructor() public {
        owner = msg.sender;
    }

    mapping (address => bool) public _onlyIfMarketplace;

    function mint() public {
        require(owner == msg.sender);
    }

    function transfer() public {

    }


    function shuffle() public {
        require(_onlyIfMarketplace[msg.sender] == true);
    }

    //You can always add an address that can call this function, 
    //and you can also write another one to remove the address which can call this function
    function setMarketplaceContract(address MarketplaceContract) public {
        require(owner == msg.sender);
        _onlyIfMarketplace[MarketplaceContract] = true;
    }
}


contract Marketplace {

    //Call NFT contract
    NFT nftContract; 

     //Ensure that only the owner can call important functions
    address owner; 
   

    constructor(address nftAddress) public {
        owner = msg.sender;
        nftContract = NFT(nftAddress);
    }

    // You can change the NFT contract you want to call at any time
    function changeNFTAddress(address newNFTAddress) public {
        require(owner == msg.sender);
        nftContract = NFT(newNFTAddress);
    }

    //Call the function of NFT contract
    function buy() public {
        nftContract.transfer();
        nftContract.shuffle();
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM