如果我想实现一个小的NFT市场,并希望用户能够将项目出售,而其他用户购买这些,我应该怎么做呢?我是否需要在每次有人提出要出售的东西时,签订一份得到批准的市场合同?当有人购买该商品时,合同将执行safeTransferFrom函数吗?
发布于 2021-05-12 18:29:20
是的,这是人们通常做的一种方式。
如果您的市场只适用于一个特定的令牌,则可以使用更好的选项。
发布于 2021-07-17 03:26:33
我用这个合同:
// SPDX-License-Identifier: MIT
pragma solidity >=0.4.21 <0.9.0;
import '@openzeppelin-contracts/contracts/token/ERC721/ERC721.sol';
import '@openzeppelin-contracts/contracts/finance/PaymentSplitter.sol';
contract BALANCE is ERC721 {
event NftBought(address _seller, address _buyer, uint256 _price);
mapping (uint256 => uint256) public tokenIdToPrice;
uint public nextTokenId;
address public admin;
constructor() ERC721('Balance', 'BALANCE') {
admin = msg.sender;
}
function mint(address to) external {
require(msg.sender == admin, 'only admin');
if(nextTokenId < 2){_safeMint(to, nextTokenId);
nextTokenId++;
}
}
function _baseURI() internal view override returns (string memory) {
return 'https://.herokuapp.com/';
}
function allowBuy(uint256 _tokenId, uint256 _price) external {
require(msg.sender == ownerOf(_tokenId), 'Not owner of this token');
require(_price > 0, 'Price zero');
tokenIdToPrice[_tokenId] = _price;
}
function disallowBuy(uint256 _tokenId) external {
require(msg.sender == ownerOf(_tokenId), 'Not owner of this token');
tokenIdToPrice[_tokenId] = 0;
}
function buy(uint256 _tokenId) external payable {
uint256 price = tokenIdToPrice[_tokenId];
require(price > 0, 'This token is not for sale');
require(msg.value == price, 'Incorrect value');
address seller = ownerOf(_tokenId);
_transfer(seller, msg.sender, _tokenId);
tokenIdToPrice[_tokenId] = 0; // not for sale anymore
payable(seller).transfer(msg.value); // send the ETH to the seller
emit NftBought(seller, msg.sender, msg.value);
}
}我叫allowBuy('tokenId','prince_in_WEI')来设置id价格,然后发送buy('tokenId'),尽管我对它有很大的异议。
https://ethereum.stackexchange.com/questions/98826
复制相似问题