我在做NFT市场,而且每次NFT被出售时,我都要实现图像洗牌。市场有购买NFT的功能,所以我们有参考NFT智能契约。当调用函数buy()时,它将调用NFT智能契约中的transfer()和Su显()。但是我只想让市场调用Su显(),所以我决定添加一个要求,检查正在调用的地址是否等于marketplace地址。问题是,对于部署市场,我需要NFT地址,对于部署NFT契约,我也需要市场地址。我怎么才能解决这个问题?是智能契约的逻辑问题,还是用这种方式解决的方法?谢谢你!!
发布于 2022-06-13 11:45:13
部署后,可以使用setter函数设置依赖地址。
示例:
contract NFT {
address marketplace;
function setMarketplace(address _marketplace) public onlyOwner {
marketplace = _marketplace;
}
}
contract Marketplace {
address nft;
function setNft(address _nft) public onlyOwner {
nft = _nft;
}
}
发布于 2022-06-13 12:01:36
我写了一个简单的框架,应该对你有帮助。:)
//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();
}
}
https://stackoverflow.com/questions/72601947
复制相似问题