带版税的nft分红代币合约
时间: 2023-09-01 20:10:30 浏览: 207
以下是一个带版税的 NFT 分红代币合约示例代码,该合约将以固定比例向 NFT 所有者支付版税,并允许用户持有 NFT 分红代币。
```
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/token/ERC721/IERC721.sol";
contract NFTDividendToken is ERC20 {
address private _nftAddress;
uint256 private _totalSupply;
mapping(address => uint256) private _balances;
mapping(uint256 => uint256) private _dividends;
mapping(uint256 => address) private _owners;
uint256 private _royaltyRate; // 版税比例,0-100
constructor(address nftAddress, uint256 royaltyRate)
ERC20("NFT Dividend Token", "NDT")
{
_nftAddress = nftAddress;
_royaltyRate = royaltyRate;
}
function totalSupply() public view override returns (uint256) {
return _totalSupply;
}
function balanceOf(address account) public view override returns (uint256) {
return _balances[account];
}
function dividendsOf(uint256 tokenId) public view returns (uint256) {
return _dividends[tokenId];
}
function claimDividends(uint256[] memory tokenIds) public {
for (uint256 i = 0; i < tokenIds.length; i++) {
uint256 tokenId = tokenIds[i];
uint256 dividends = _dividends[tokenId];
_dividends[tokenId] = 0;
_balances[msg.sender] += dividends;
_totalSupply += dividends;
emit Transfer(address(0), msg.sender, dividends);
}
}
function addDividends(uint256[] memory tokenIds, uint256 amount) public {
IERC721 nft = IERC721(_nftAddress);
for (uint256 i = 0; i < tokenIds.length; i++) {
uint256 tokenId = tokenIds[i];
address owner = nft.ownerOf(tokenId);
uint256 royalty = (amount * _royaltyRate) / 100; // 计算版税
uint256 dividends = amount - royalty; // 计算分红
_dividends[tokenId] += dividends;
_balances[owner] += dividends;
_totalSupply += dividends;
_owners[tokenId] = owner;
if (royalty > 0) {
// 向版权持有者支付版税
nft.safeTransferFrom(address(this), _owners[tokenId], tokenId);
}
emit Transfer(address(0), owner, dividends);
}
}
}
```
该合约继承了 OpenZeppelin 的 ERC20 合约,允许用户持有 NFT 分红代币。该合约在 `addDividends` 函数中计算版税和分红,并将版税转移给 NFT 版权持有者。版税比例由合约构造函数中的 `royaltyRate` 参数指定。当管理员添加分红时,合约会将每个 NFT tokenId 的分红金额增加,并将所有者的余额增加,同时将总供应量增加。当用户领取分红时,合约会将他们的余额增加并将总供应量增加,并发出一个 ERC20 转账事件。
阅读全文