File size: 7,053 Bytes
cc183a1 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | // SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {IERC721} from "@openzeppelin/contracts/token/ERC721/IERC721.sol";
import {IERC165} from "@openzeppelin/contracts/utils/introspection/IERC165.sol";
import {IERC2981} from "@openzeppelin/contracts/interfaces/IERC2981.sol";
contract Market {
struct Listing {
address seller;
uint256 price;
}
IERC20 public immutable paymentToken;
uint256 public immutable feeBps;
bool public enforceRoyalties;
mapping(address => mapping(uint256 => Listing)) private _listings;
mapping(address => mapping(uint256 => mapping(address => uint256))) private _offers;
event Listed(address indexed collection, uint256 indexed tokenId, address indexed seller, uint256 price);
event PriceUpdated(address indexed collection, uint256 indexed tokenId, uint256 newPrice);
event Cancelled(address indexed collection, uint256 indexed tokenId, address indexed seller);
event Bought(address indexed collection, uint256 indexed tokenId, address indexed buyer, address seller, uint256 price);
event OfferCreated(address indexed collection, uint256 indexed tokenId, address indexed buyer, uint256 amount);
event OfferAccepted(address indexed collection, uint256 indexed tokenId, address indexed buyer, address seller, uint256 amount);
constructor(address paymentToken_, uint256 feeBps_, bool enforceRoyalties_) {
require(feeBps_ <= 10000, "fee exceeds 100%");
paymentToken = IERC20(paymentToken_);
feeBps = feeBps_;
enforceRoyalties = enforceRoyalties_;
}
function list(address collection, uint256 tokenId, uint256 price) external {
require(IERC721(collection).ownerOf(tokenId) == msg.sender, "caller must own token");
require(_listings[collection][tokenId].seller != msg.sender, "already listed");
require(price > 0, "price must be positive");
_listings[collection][tokenId] = Listing(msg.sender, price);
emit Listed(collection, tokenId, msg.sender, price);
}
function setPrice(address collection, uint256 tokenId, uint256 newPrice) external {
require(_listings[collection][tokenId].seller == msg.sender, "only seller");
require(newPrice > 0, "price must be positive");
_listings[collection][tokenId].price = newPrice;
emit PriceUpdated(collection, tokenId, newPrice);
}
function cancel(address collection, uint256 tokenId) external {
require(_listings[collection][tokenId].seller == msg.sender, "only seller");
delete _listings[collection][tokenId];
emit Cancelled(collection, tokenId, msg.sender);
}
function listingOf(address collection, uint256 tokenId) external view returns (address seller, uint256 price) {
Listing memory listing = _listings[collection][tokenId];
return (listing.seller, listing.price);
}
function quoteProceeds(
address collection,
uint256 tokenId,
uint256 price
) public view returns (uint256 fee, uint256 royalty, uint256 sellerProceeds) {
fee = (price * feeBps) / 10000;
if (enforceRoyalties) {
try IERC165(collection).supportsInterface(type(IERC2981).interfaceId) returns (bool supported) {
if (supported) {
try IERC2981(collection).royaltyInfo(tokenId, price) returns (address receiver, uint256 amount) {
if (receiver != address(0)) {
royalty = amount;
}
} catch {}
}
} catch {}
}
require(price >= fee + royalty, "fees exceed price");
sellerProceeds = price - fee - royalty;
}
function buy(address collection, uint256 tokenId) external {
Listing memory listing = _listings[collection][tokenId];
require(listing.price > 0, "not listed");
address seller = listing.seller;
uint256 price = listing.price;
require(IERC721(collection).ownerOf(tokenId) == seller, "seller no longer owns token");
require(
IERC721(collection).getApproved(tokenId) == address(this) ||
IERC721(collection).isApprovedForAll(seller, address(this)),
"market not approved"
);
delete _listings[collection][tokenId];
(, uint256 royalty, uint256 sellerProceeds) = quoteProceeds(collection, tokenId, price);
address royaltyReceiver = address(0);
if (enforceRoyalties && royalty > 0) {
(royaltyReceiver,) = IERC2981(collection).royaltyInfo(tokenId, price);
}
paymentToken.transferFrom(msg.sender, address(this), price);
if (royalty > 0 && royaltyReceiver != address(0)) {
paymentToken.transfer(royaltyReceiver, royalty);
}
paymentToken.transfer(seller, sellerProceeds);
IERC721(collection).transferFrom(seller, msg.sender, tokenId);
emit Bought(collection, tokenId, msg.sender, seller, price);
}
function makeOffer(address collection, uint256 tokenId, uint256 amount) external {
require(amount > 0, "amount must be positive");
require(paymentToken.allowance(msg.sender, address(this)) >= amount, "insufficient allowance");
_offers[collection][tokenId][msg.sender] = amount;
emit OfferCreated(collection, tokenId, msg.sender, amount);
}
function offerOf(address collection, uint256 tokenId, address buyer) external view returns (uint256) {
return _offers[collection][tokenId][buyer];
}
function acceptOffer(address collection, uint256 tokenId, address buyer) external {
require(IERC721(collection).ownerOf(tokenId) == msg.sender, "only owner");
uint256 amount = _offers[collection][tokenId][buyer];
require(amount > 0, "no offer");
require(
IERC721(collection).getApproved(tokenId) == address(this) ||
IERC721(collection).isApprovedForAll(msg.sender, address(this)),
"market not approved"
);
delete _offers[collection][tokenId][buyer];
if (_listings[collection][tokenId].seller == msg.sender) {
delete _listings[collection][tokenId];
}
(, uint256 royalty, uint256 sellerProceeds) = quoteProceeds(collection, tokenId, amount);
address royaltyReceiver = address(0);
if (enforceRoyalties && royalty > 0) {
(royaltyReceiver,) = IERC2981(collection).royaltyInfo(tokenId, amount);
}
paymentToken.transferFrom(buyer, address(this), amount);
if (royalty > 0 && royaltyReceiver != address(0)) {
paymentToken.transfer(royaltyReceiver, royalty);
}
paymentToken.transfer(msg.sender, sellerProceeds);
IERC721(collection).transferFrom(msg.sender, buyer, tokenId);
emit OfferAccepted(collection, tokenId, buyer, msg.sender, amount);
}
}
|