写一个完整的带版税nft合约
时间: 2024-01-24 07:16:53 浏览: 110
当然,我可以帮您回答这个问题。下面是一个例子:
```
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
contract MyNFT is ERC721 {
using Counters for Counters.Counter;
Counters.Counter private _tokenIds;
uint256 public constant ROYALTY_FEE = 10; // 10%
uint256 public royaltyAmount;
address public royaltyBeneficiary;
constructor(address _royaltyBeneficiary) ERC721("MyNFT", "NFT") {
royaltyBeneficiary = _royaltyBeneficiary;
}
function mint(address _to, string memory _tokenURI) public returns (uint256) {
_tokenIds.increment();
uint256 newItemId = _tokenIds.current();
_safeMint(_to, newItemId);
_setTokenURI(newItemId, _tokenURI);
return newItemId;
}
function setRoyaltyBeneficiary(address _newBeneficiary) public {
require(msg.sender == royaltyBeneficiary, "Only current beneficiary can change.");
royaltyBeneficiary = _newBeneficiary;
}
function transferFrom(
address _from,
address _to,
uint256 _tokenId
) public payable override {
require(_checkRoyaltyFee(msg.value), "Not enough Ether sent.");
super.transferFrom(_from, _to, _tokenId);
royaltyAmount = msg.value;
}
function _checkRoyaltyFee(uint256 _amount) internal view returns (bool) {
return _amount * 100 / ROYALTY_FEE == royaltyAmount * 100;
}
function withdrawRoyalty() public {
require(msg.sender == royaltyBeneficiary, "Only royalty beneficiary can withdraw.");
uint256 amount = royaltyAmount;
royaltyAmount = 0;
payable(msg.sender).transfer(amount);
}
}
```
上面的合约可以让您创建一个 ERC721 标准的 NFT 合约,并包含版税功能。当合约中的 NFT 进行转移时,合约会收取 10% 的转移费用,并分给版税接收人。
阅读全文