Contract Name:
KrownMarketplace
Contract Source Code:
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (access/Ownable.sol)
pragma solidity ^0.8.20;
import {Context} from "../utils/Context.sol";
/**
* @dev Contract module which provides a basic access control mechanism, where
* there is an account (an owner) that can be granted exclusive access to
* specific functions.
*
* The initial owner is set to the address provided by the deployer. This can
* later be changed with {transferOwnership}.
*
* This module is used through inheritance. It will make available the modifier
* `onlyOwner`, which can be applied to your functions to restrict their use to
* the owner.
*/
abstract contract Ownable is Context {
address private _owner;
/**
* @dev The caller account is not authorized to perform an operation.
*/
error OwnableUnauthorizedAccount(address account);
/**
* @dev The owner is not a valid owner account. (eg. `address(0)`)
*/
error OwnableInvalidOwner(address owner);
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
/**
* @dev Initializes the contract setting the address provided by the deployer as the initial owner.
*/
constructor(address initialOwner) {
if (initialOwner == address(0)) {
revert OwnableInvalidOwner(address(0));
}
_transferOwnership(initialOwner);
}
/**
* @dev Throws if called by any account other than the owner.
*/
modifier onlyOwner() {
_checkOwner();
_;
}
/**
* @dev Returns the address of the current owner.
*/
function owner() public view virtual returns (address) {
return _owner;
}
/**
* @dev Throws if the sender is not the owner.
*/
function _checkOwner() internal view virtual {
if (owner() != _msgSender()) {
revert OwnableUnauthorizedAccount(_msgSender());
}
}
/**
* @dev Leaves the contract without owner. It will not be possible to call
* `onlyOwner` functions. Can only be called by the current owner.
*
* NOTE: Renouncing ownership will leave the contract without an owner,
* thereby disabling any functionality that is only available to the owner.
*/
function renounceOwnership() public virtual onlyOwner {
_transferOwnership(address(0));
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Can only be called by the current owner.
*/
function transferOwnership(address newOwner) public virtual onlyOwner {
if (newOwner == address(0)) {
revert OwnableInvalidOwner(address(0));
}
_transferOwnership(newOwner);
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Internal function without access restriction.
*/
function _transferOwnership(address newOwner) internal virtual {
address oldOwner = _owner;
_owner = newOwner;
emit OwnershipTransferred(oldOwner, newOwner);
}
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.1.0) (token/ERC721/IERC721.sol)
pragma solidity ^0.8.20;
import {IERC165} from "../../utils/introspection/IERC165.sol";
/**
* @dev Required interface of an ERC-721 compliant contract.
*/
interface IERC721 is IERC165 {
/**
* @dev Emitted when `tokenId` token is transferred from `from` to `to`.
*/
event Transfer(address indexed from, address indexed to, uint256 indexed tokenId);
/**
* @dev Emitted when `owner` enables `approved` to manage the `tokenId` token.
*/
event Approval(address indexed owner, address indexed approved, uint256 indexed tokenId);
/**
* @dev Emitted when `owner` enables or disables (`approved`) `operator` to manage all of its assets.
*/
event ApprovalForAll(address indexed owner, address indexed operator, bool approved);
/**
* @dev Returns the number of tokens in ``owner``'s account.
*/
function balanceOf(address owner) external view returns (uint256 balance);
/**
* @dev Returns the owner of the `tokenId` token.
*
* Requirements:
*
* - `tokenId` must exist.
*/
function ownerOf(uint256 tokenId) external view returns (address owner);
/**
* @dev Safely transfers `tokenId` token from `from` to `to`.
*
* Requirements:
*
* - `from` cannot be the zero address.
* - `to` cannot be the zero address.
* - `tokenId` token must exist and be owned by `from`.
* - If the caller is not `from`, it must be approved to move this token by either {approve} or {setApprovalForAll}.
* - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon
* a safe transfer.
*
* Emits a {Transfer} event.
*/
function safeTransferFrom(address from, address to, uint256 tokenId, bytes calldata data) external;
/**
* @dev Safely transfers `tokenId` token from `from` to `to`, checking first that contract recipients
* are aware of the ERC-721 protocol to prevent tokens from being forever locked.
*
* Requirements:
*
* - `from` cannot be the zero address.
* - `to` cannot be the zero address.
* - `tokenId` token must exist and be owned by `from`.
* - If the caller is not `from`, it must have been allowed to move this token by either {approve} or
* {setApprovalForAll}.
* - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon
* a safe transfer.
*
* Emits a {Transfer} event.
*/
function safeTransferFrom(address from, address to, uint256 tokenId) external;
/**
* @dev Transfers `tokenId` token from `from` to `to`.
*
* WARNING: Note that the caller is responsible to confirm that the recipient is capable of receiving ERC-721
* or else they may be permanently lost. Usage of {safeTransferFrom} prevents loss, though the caller must
* understand this adds an external call which potentially creates a reentrancy vulnerability.
*
* Requirements:
*
* - `from` cannot be the zero address.
* - `to` cannot be the zero address.
* - `tokenId` token must be owned by `from`.
* - If the caller is not `from`, it must be approved to move this token by either {approve} or {setApprovalForAll}.
*
* Emits a {Transfer} event.
*/
function transferFrom(address from, address to, uint256 tokenId) external;
/**
* @dev Gives permission to `to` to transfer `tokenId` token to another account.
* The approval is cleared when the token is transferred.
*
* Only a single account can be approved at a time, so approving the zero address clears previous approvals.
*
* Requirements:
*
* - The caller must own the token or be an approved operator.
* - `tokenId` must exist.
*
* Emits an {Approval} event.
*/
function approve(address to, uint256 tokenId) external;
/**
* @dev Approve or remove `operator` as an operator for the caller.
* Operators can call {transferFrom} or {safeTransferFrom} for any token owned by the caller.
*
* Requirements:
*
* - The `operator` cannot be the address zero.
*
* Emits an {ApprovalForAll} event.
*/
function setApprovalForAll(address operator, bool approved) external;
/**
* @dev Returns the account approved for `tokenId` token.
*
* Requirements:
*
* - `tokenId` must exist.
*/
function getApproved(uint256 tokenId) external view returns (address operator);
/**
* @dev Returns if the `operator` is allowed to manage all of the assets of `owner`.
*
* See {setApprovalForAll}
*/
function isApprovedForAll(address owner, address operator) external view returns (bool);
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.1.0) (token/ERC721/IERC721Receiver.sol)
pragma solidity ^0.8.20;
/**
* @title ERC-721 token receiver interface
* @dev Interface for any contract that wants to support safeTransfers
* from ERC-721 asset contracts.
*/
interface IERC721Receiver {
/**
* @dev Whenever an {IERC721} `tokenId` token is transferred to this contract via {IERC721-safeTransferFrom}
* by `operator` from `from`, this function is called.
*
* It must return its Solidity selector to confirm the token transfer.
* If any other value is returned or the interface is not implemented by the recipient, the transfer will be
* reverted.
*
* The selector can be obtained in Solidity with `IERC721Receiver.onERC721Received.selector`.
*/
function onERC721Received(
address operator,
address from,
uint256 tokenId,
bytes calldata data
) external returns (bytes4);
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.1) (utils/Context.sol)
pragma solidity ^0.8.20;
/**
* @dev Provides information about the current execution context, including the
* sender of the transaction and its data. While these are generally available
* via msg.sender and msg.data, they should not be accessed in such a direct
* manner, since when dealing with meta-transactions the account sending and
* paying for execution may not be the actual sender (as far as an application
* is concerned).
*
* This contract is only required for intermediate, library-like contracts.
*/
abstract contract Context {
function _msgSender() internal view virtual returns (address) {
return msg.sender;
}
function _msgData() internal view virtual returns (bytes calldata) {
return msg.data;
}
function _contextSuffixLength() internal view virtual returns (uint256) {
return 0;
}
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.1.0) (utils/introspection/IERC165.sol)
pragma solidity ^0.8.20;
/**
* @dev Interface of the ERC-165 standard, as defined in the
* https://eips.ethereum.org/EIPS/eip-165[ERC].
*
* Implementers can declare support of contract interfaces, which can then be
* queried by others ({ERC165Checker}).
*
* For an implementation, see {ERC165}.
*/
interface IERC165 {
/**
* @dev Returns true if this contract implements the interface defined by
* `interfaceId`. See the corresponding
* https://eips.ethereum.org/EIPS/eip-165#how-interfaces-are-identified[ERC section]
* to learn more about how these ids are created.
*
* This function call must use less than 30 000 gas.
*/
function supportsInterface(bytes4 interfaceId) external view returns (bool);
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (utils/Pausable.sol)
pragma solidity ^0.8.20;
import {Context} from "../utils/Context.sol";
/**
* @dev Contract module which allows children to implement an emergency stop
* mechanism that can be triggered by an authorized account.
*
* This module is used through inheritance. It will make available the
* modifiers `whenNotPaused` and `whenPaused`, which can be applied to
* the functions of your contract. Note that they will not be pausable by
* simply including this module, only once the modifiers are put in place.
*/
abstract contract Pausable is Context {
bool private _paused;
/**
* @dev Emitted when the pause is triggered by `account`.
*/
event Paused(address account);
/**
* @dev Emitted when the pause is lifted by `account`.
*/
event Unpaused(address account);
/**
* @dev The operation failed because the contract is paused.
*/
error EnforcedPause();
/**
* @dev The operation failed because the contract is not paused.
*/
error ExpectedPause();
/**
* @dev Initializes the contract in unpaused state.
*/
constructor() {
_paused = false;
}
/**
* @dev Modifier to make a function callable only when the contract is not paused.
*
* Requirements:
*
* - The contract must not be paused.
*/
modifier whenNotPaused() {
_requireNotPaused();
_;
}
/**
* @dev Modifier to make a function callable only when the contract is paused.
*
* Requirements:
*
* - The contract must be paused.
*/
modifier whenPaused() {
_requirePaused();
_;
}
/**
* @dev Returns true if the contract is paused, and false otherwise.
*/
function paused() public view virtual returns (bool) {
return _paused;
}
/**
* @dev Throws if the contract is paused.
*/
function _requireNotPaused() internal view virtual {
if (paused()) {
revert EnforcedPause();
}
}
/**
* @dev Throws if the contract is not paused.
*/
function _requirePaused() internal view virtual {
if (!paused()) {
revert ExpectedPause();
}
}
/**
* @dev Triggers stopped state.
*
* Requirements:
*
* - The contract must not be paused.
*/
function _pause() internal virtual whenNotPaused {
_paused = true;
emit Paused(_msgSender());
}
/**
* @dev Returns to normal state.
*
* Requirements:
*
* - The contract must be paused.
*/
function _unpause() internal virtual whenPaused {
_paused = false;
emit Unpaused(_msgSender());
}
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.1.0) (utils/ReentrancyGuard.sol)
pragma solidity ^0.8.20;
/**
* @dev Contract module that helps prevent reentrant calls to a function.
*
* Inheriting from `ReentrancyGuard` will make the {nonReentrant} modifier
* available, which can be applied to functions to make sure there are no nested
* (reentrant) calls to them.
*
* Note that because there is a single `nonReentrant` guard, functions marked as
* `nonReentrant` may not call one another. This can be worked around by making
* those functions `private`, and then adding `external` `nonReentrant` entry
* points to them.
*
* TIP: If EIP-1153 (transient storage) is available on the chain you're deploying at,
* consider using {ReentrancyGuardTransient} instead.
*
* TIP: If you would like to learn more about reentrancy and alternative ways
* to protect against it, check out our blog post
* https://blog.openzeppelin.com/reentrancy-after-istanbul/[Reentrancy After Istanbul].
*/
abstract contract ReentrancyGuard {
// Booleans are more expensive than uint256 or any type that takes up a full
// word because each write operation emits an extra SLOAD to first read the
// slot's contents, replace the bits taken up by the boolean, and then write
// back. This is the compiler's defense against contract upgrades and
// pointer aliasing, and it cannot be disabled.
// The values being non-zero value makes deployment a bit more expensive,
// but in exchange the refund on every call to nonReentrant will be lower in
// amount. Since refunds are capped to a percentage of the total
// transaction's gas, it is best to keep them low in cases like this one, to
// increase the likelihood of the full refund coming into effect.
uint256 private constant NOT_ENTERED = 1;
uint256 private constant ENTERED = 2;
uint256 private _status;
/**
* @dev Unauthorized reentrant call.
*/
error ReentrancyGuardReentrantCall();
constructor() {
_status = NOT_ENTERED;
}
/**
* @dev Prevents a contract from calling itself, directly or indirectly.
* Calling a `nonReentrant` function from another `nonReentrant`
* function is not supported. It is possible to prevent this from happening
* by making the `nonReentrant` function external, and making it call a
* `private` function that does the actual work.
*/
modifier nonReentrant() {
_nonReentrantBefore();
_;
_nonReentrantAfter();
}
function _nonReentrantBefore() private {
// On the first call to nonReentrant, _status will be NOT_ENTERED
if (_status == ENTERED) {
revert ReentrancyGuardReentrantCall();
}
// Any calls to nonReentrant after this point will fail
_status = ENTERED;
}
function _nonReentrantAfter() private {
// By storing the original value once again, a refund is triggered (see
// https://eips.ethereum.org/EIPS/eip-2200)
_status = NOT_ENTERED;
}
/**
* @dev Returns true if the reentrancy guard is currently set to "entered", which indicates there is a
* `nonReentrant` function in the call stack.
*/
function _reentrancyGuardEntered() internal view returns (bool) {
return _status == ENTERED;
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
interface IEIP2981 {
/**
* @dev Returns how much royalty is owed and to whom, based on a sale price that may be denominated in any unit of
* exchange. The royalty amount is denominated and should be paid in that same unit of exchange.
* @param tokenId The NFT token ID being sold
* @param salePrice The price of the NFT asset specified
* @return receiver Address of who should be sent the royalty payment
* @return royaltyAmount The royalty payment amount for salePrice
*/
function royaltyInfo(uint256 tokenId, uint256 salePrice)
external
view
returns (address receiver, uint256 royaltyAmount);
}
// SPDX-License-Identifier: MIT
/*
/$$ /$$ /$$$$$$$ /$$$$$$ /$$ /$$ /$$ /$$ /$$ /$$$$$$ /$$$$$$$ /$$$$$$$
| $$ /$$/| $$__ $$ /$$__ $$| $$ /$ | $$| $$$ | $$ | $$ /$$__ $$| $$__ $$ /$$__ $$
| $$ /$$/ | $$ \ $$| $$ \ $$| $$ /$$$| $$| $$$$| $$ | $$ | $$ \ $$| $$ \ $$| $$ \__/
| $$$$$/ | $$$$$$$/| $$ | $$| $$/$$ $$ $$| $$ $$ $$ | $$ | $$$$$$$$| $$$$$$$ | $$$$$$
| $$ $$ | $$__ $$| $$ | $$| $$$$_ $$$$| $$ $$$$ | $$ | $$__ $$| $$__ $$ \____ $$
| $$\ $$ | $$ \ $$| $$ | $$| $$$/ \ $$$| $$\ $$$ | $$ | $$ | $$| $$ \ $$ /$$ \ $$
| $$ \ $$| $$ | $$| $$$$$$/| $$/ \ $$| $$ \ $$ | $$$$$$$$| $$ | $$| $$$$$$$/| $$$$$$/
|__/ \__/|__/ |__/ \______/ |__/ \__/|__/ \__/ |________/|__/ |__/|_______/ \______/
krownlabs.app
x.com/krownlabs
discord.gg/KTU4krfhrG
*/
pragma solidity ^0.8.20;
import "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/Pausable.sol";
import "@openzeppelin/contracts/token/ERC721/IERC721.sol";
import "@openzeppelin/contracts/token/ERC721/IERC721Receiver.sol";
import "@openzeppelin/contracts/utils/introspection/IERC165.sol";
import "./IEIP2981.sol";
interface IERC721WithOwner is IERC721 {
function owner() external view returns (address);
}
contract KrownMarketplace is
Ownable,
ReentrancyGuard,
Pausable,
IERC721Receiver
{
/* ========== CONSTANTS ========== */
uint256 public constant MAX_ROYALTY = 1000; // 10%
uint256 public constant MIN_ROYALTY = 50; // 0,5%
uint256 public constant MAX_PRICE = 1000000 ether; // 1m ether
uint256 public constant MIN_PRICE = 0.001 ether;
uint256 public constant BID_TIMEOUT = 24 hours;
uint256 public constant MIN_EXPIRATION_TIME = 1 hours;
uint256 public constant MAX_EXPIRATION_TIME = 90 days;
uint256 public constant CLEANUP_BATCH_SIZE = 50;
uint256 public constant PRICE_UPDATE_DELAY = 5 minutes;
uint256 public constant MIN_BID_INCREMENT = 5; // 5%
/* ========== STRUCTS ========== */
struct Listing {
address seller;
address nftContract;
uint256 tokenId;
uint96 price;
bool active;
uint96 highestBid;
address highestBidder;
bool acceptingBids;
uint40 listingTime;
uint40 expirationTime;
uint40 lastPriceUpdate;
}
struct CollectionListings {
Listing[] listings;
mapping(uint256 => uint256) tokenIdToIndex;
mapping(uint256 => bool) isListed;
uint256 activeListingsCount;
}
struct Bid {
address bidder;
uint96 amount;
uint40 timestamp;
}
struct RoyaltyInfo {
address receiver;
uint96 percentage;
}
/* ========== STATE VARIABLES ========== */
uint256 public marketplaceFee;
mapping(address => CollectionListings) public collectionListings;
mapping(address => mapping(uint256 => Bid[])) public bidsHistory;
mapping(address => uint256) private pendingPayments;
mapping(address => RoyaltyInfo) public collectionRoyalties;
mapping(address => bool) public collectionOwners;
/* ========== EVENTS ========== */
event Listed(
address indexed seller,
address indexed nftContract,
uint256 indexed tokenId,
uint256 price,
bool acceptingBids,
uint256 expirationTime
);
event Sale(
address indexed seller,
address indexed buyer,
address indexed nftContract,
uint256 tokenId,
uint256 price
);
event Cancelled(
address indexed seller,
address indexed nftContract,
uint256 indexed tokenId
);
event BidPlaced(
address indexed bidder,
address indexed nftContract,
uint256 indexed tokenId,
uint256 amount
);
event BidAccepted(
address indexed seller,
address indexed bidder,
address indexed nftContract,
uint256 tokenId,
uint256 amount
);
event PaymentPending(
address indexed recipient,
uint256 amount
);
event MarketplaceFeeUpdated(uint256 newFee);
event EmergencyWithdraw(address indexed recipient, uint256 amount);
event CollectionListingsCleaned(address indexed nftContract, uint256 count);
event RoyaltySet(
address indexed nftContract,
address indexed receiver,
uint256 percentage
);
/* ========== ERRORS ========== */
error ZeroAddress();
error UnauthorizedCaller();
error InvalidPrice();
error ListingNotActive();
error InvalidExpiration();
error BidTooLow();
error ListingExpired();
error InvalidInterface();
error NotListed();
error TransferFailed();
error RoyaltyTooHigh();
error BidNotAllowed();
error NoValidBid();
error BatchSizeTooLarge();
error PriceUpdateTooSoon();
error InsufficientPendingPayment();
error PriceExceedsMaximum();
error RoyaltyTooLow();
/* ========== MODIFIERS ========== */
modifier checkPrice(uint256 price) {
if (price < MIN_PRICE || price > MAX_PRICE) revert InvalidPrice();
_;
}
modifier onlyCollectionOwner(address nftContract) {
if (!collectionOwners[nftContract] ||
msg.sender != IERC721WithOwner(nftContract).owner()) {
revert UnauthorizedCaller();
}
_;
}
/* ========== CONSTRUCTOR ========== */
constructor(
uint256 _marketplaceFee
) Ownable(msg.sender) {
if(_marketplaceFee > 1000) revert InvalidPrice();
marketplaceFee = _marketplaceFee;
}
/* ========== MAIN FUNCTIONS ========== */
function listNFT(
address _nftContract,
uint256 _tokenId,
uint256 _price,
bool _acceptingBids,
uint256 _expirationTime
) external
whenNotPaused
nonReentrant
checkPrice(_price)
{
if(collectionListings[_nftContract].isListed[_tokenId]) revert NotListed();
if(_expirationTime < block.timestamp + MIN_EXPIRATION_TIME ||
_expirationTime > block.timestamp + MAX_EXPIRATION_TIME) {
revert InvalidExpiration();
}
// Interface and ownership checks
IERC721 nft = IERC721(_nftContract);
if(!_checkInterface(nft)) revert InvalidInterface();
if(nft.ownerOf(_tokenId) != msg.sender) revert UnauthorizedCaller();
if(nft.getApproved(_tokenId) != address(this) &&
!nft.isApprovedForAll(msg.sender, address(this))) {
revert UnauthorizedCaller();
}
Listing memory listing = Listing({
seller: msg.sender,
nftContract: _nftContract,
tokenId: _tokenId,
price: uint96(_price),
active: true,
highestBid: 0,
highestBidder: address(0),
acceptingBids: _acceptingBids,
listingTime: uint40(block.timestamp),
expirationTime: uint40(_expirationTime),
lastPriceUpdate: uint40(block.timestamp)
});
CollectionListings storage collection = collectionListings[_nftContract];
collection.listings.push(listing);
collection.tokenIdToIndex[_tokenId] = collection.listings.length - 1;
collection.isListed[_tokenId] = true;
collection.activeListingsCount++;
emit Listed(msg.sender, _nftContract, _tokenId, _price, _acceptingBids, _expirationTime);
}
function buyNFT(
address _nftContract,
uint256 _tokenId,
uint256 maxPrice
) external payable nonReentrant whenNotPaused {
CollectionListings storage collection = collectionListings[_nftContract];
if(!collection.isListed[_tokenId]) revert NotListed();
Listing storage listing = collection.listings[collection.tokenIdToIndex[_tokenId]];
if(!listing.active) revert ListingNotActive();
if(msg.value != listing.price) revert InvalidPrice();
if(listing.price > maxPrice) revert PriceExceedsMaximum();
if(listing.seller == msg.sender) revert UnauthorizedCaller();
if(block.timestamp >= listing.expirationTime) revert ListingExpired();
// Verify current ownership
if(IERC721(_nftContract).ownerOf(_tokenId) != listing.seller) {
revert UnauthorizedCaller();
}
address seller = listing.seller;
uint256 price = listing.price;
// Remove listing first
_removeListingFromCollection(_nftContract, _tokenId);
// Handle payments through pending payments
_distributePendingPayments(price, seller, _nftContract);
// Transfer NFT last
IERC721(_nftContract).safeTransferFrom(seller, msg.sender, _tokenId);
emit Sale(seller, msg.sender, _nftContract, _tokenId, price);
}
function placeBid(
address _nftContract,
uint256 _tokenId
) external payable nonReentrant whenNotPaused {
CollectionListings storage collection = collectionListings[_nftContract];
if(!collection.isListed[_tokenId]) revert NotListed();
Listing storage listing = collection.listings[collection.tokenIdToIndex[_tokenId]];
if(!listing.active) revert ListingNotActive();
if(!listing.acceptingBids) revert BidNotAllowed();
if(msg.sender == listing.seller) revert UnauthorizedCaller();
// Minimum bid increment check
uint256 minBidRequired = listing.highestBid + (listing.highestBid * MIN_BID_INCREMENT / 100);
if(msg.value <= minBidRequired) revert BidTooLow();
if(msg.value < listing.price) revert BidTooLow();
if(block.timestamp >= listing.expirationTime) revert ListingExpired();
// Verify current ownership
if(IERC721(listing.nftContract).ownerOf(listing.tokenId) != listing.seller) {
revert UnauthorizedCaller();
}
// Return funds to previous highest bidder through pending payments
if(listing.highestBidder != address(0)) {
pendingPayments[listing.highestBidder] += listing.highestBid;
emit PaymentPending(listing.highestBidder, listing.highestBid);
}
// Update bid information
listing.highestBid = uint96(msg.value);
listing.highestBidder = msg.sender;
// Record bid in history
bidsHistory[_nftContract][_tokenId].push(Bid({
bidder: msg.sender,
amount: uint96(msg.value),
timestamp: uint40(block.timestamp)
}));
emit BidPlaced(msg.sender, _nftContract, _tokenId, msg.value);
}
function acceptBid(
address _nftContract,
uint256 _tokenId
) external nonReentrant whenNotPaused {
CollectionListings storage collection = collectionListings[_nftContract];
if(!collection.isListed[_tokenId]) revert NotListed();
Listing storage listing = collection.listings[collection.tokenIdToIndex[_tokenId]];
if(msg.sender != listing.seller) revert UnauthorizedCaller();
if(!listing.active || !listing.acceptingBids) revert ListingNotActive();
if(listing.highestBidder == address(0)) revert NoValidBid();
if(block.timestamp >= listing.expirationTime) revert ListingExpired();
// Verify current ownership
if(IERC721(_nftContract).ownerOf(_tokenId) != msg.sender) {
revert UnauthorizedCaller();
}
address buyer = listing.highestBidder;
uint256 salePrice = listing.highestBid;
// Remove listing
_removeListingFromCollection(_nftContract, _tokenId);
// Handle payments
_distributePendingPayments(salePrice, msg.sender, _nftContract);
// Transfer NFT
IERC721(_nftContract).safeTransferFrom(msg.sender, buyer, _tokenId);
emit BidAccepted(msg.sender, buyer, _nftContract, _tokenId, salePrice);
}
function cancelListing(
address _nftContract,
uint256 _tokenId
) external whenNotPaused {
CollectionListings storage collection = collectionListings[_nftContract];
if(!collection.isListed[_tokenId]) revert NotListed();
Listing storage listing = collection.listings[collection.tokenIdToIndex[_tokenId]];
if(listing.seller != msg.sender && owner() != msg.sender) {
revert UnauthorizedCaller();
}
if(!listing.active) revert ListingNotActive();
if(listing.highestBidder != address(0)) {
pendingPayments[listing.highestBidder] += listing.highestBid;
emit PaymentPending(listing.highestBidder, listing.highestBid);
}
_removeListingFromCollection(_nftContract, _tokenId);
emit Cancelled(msg.sender, _nftContract, _tokenId);
}
/* ========== COLLECTION MANAGEMENT ========== */
function registerAsCollectionOwner(address nftContract) external {
if(nftContract == address(0)) revert ZeroAddress();
if(!_checkInterface(IERC721(nftContract))) revert InvalidInterface();
if(msg.sender != IERC721WithOwner(nftContract).owner()) {
revert UnauthorizedCaller();
}
collectionOwners[nftContract] = true;
}
function setRoyalty(
address nftContract,
address receiver,
uint256 percentage
) external onlyCollectionOwner(nftContract) {
if(percentage > MAX_ROYALTY) revert RoyaltyTooHigh();
if(percentage < MIN_ROYALTY) revert RoyaltyTooLow();
if(receiver == address(0)) revert ZeroAddress();
collectionRoyalties[nftContract] = RoyaltyInfo({
receiver: receiver,
percentage: uint96(percentage)
});
emit RoyaltySet(nftContract, receiver, percentage);
}
/* ========== INTERNAL FUNCTIONS ========== */
function _removeListingFromCollection(address nftContract, uint256 tokenId) internal {
CollectionListings storage collection = collectionListings[nftContract];
uint256 index = collection.tokenIdToIndex[tokenId];
uint256 lastIndex = collection.listings.length - 1;
if (index != lastIndex) {
Listing memory lastListing = collection.listings[lastIndex];
collection.listings[index] = lastListing;
collection.tokenIdToIndex[lastListing.tokenId] = index;
}
collection.listings.pop();
delete collection.tokenIdToIndex[tokenId];
delete collection.isListed[tokenId];
if (collection.activeListingsCount > 0) {
collection.activeListingsCount--;
}
}
function _distributePendingPayments(
uint256 amount,
address seller,
address nftContract
) internal {
uint256 marketplaceFeeAmount = (amount * marketplaceFee) / 10000;
uint256 remainingAmount = amount - marketplaceFeeAmount;
// Handle royalties
(address royaltyReceiver, uint256 royaltyAmount) = _getRoyalty(nftContract, amount);
if (royaltyReceiver != address(0)) {
remainingAmount -= royaltyAmount;
// Direct transfer of royalties
(bool royaltySent,) = payable(royaltyReceiver).call{value: royaltyAmount}("");
if(!royaltySent) revert TransferFailed();
emit PaymentPending(royaltyReceiver, 0);
}
// Direct transfer of marketplace fee
(bool feeSent,) = payable(owner()).call{value: marketplaceFeeAmount}("");
if(!feeSent) revert TransferFailed();
emit PaymentPending(address(this), 0);
// Direct transfer to seller
(bool sellerSent,) = payable(seller).call{value: remainingAmount}("");
if(!sellerSent) revert TransferFailed();
emit PaymentPending(seller, 0);
}
function _getRoyalty(
address nftContract,
uint256 amount
) internal view returns (address receiver, uint256 royaltyAmount) {
// First check if there's a manually set royalty
RoyaltyInfo memory royalty = collectionRoyalties[nftContract];
if (royalty.receiver != address(0)) {
return (royalty.receiver, (amount * royalty.percentage) / 10000);
}
// If no manual royalty, check if contract supports EIP2981 interface
try IERC165(nftContract).supportsInterface(0x2a55205a) returns (bool supported) {
// Only try to get royalty info if interface is supported
if (supported) {
try IEIP2981(nftContract).royaltyInfo(0, amount) returns (
address _receiver,
uint256 _royaltyAmount
) {
if (_receiver != address(0)) {
// Limit royalty to 10%
uint256 maxRoyalty = (amount * MAX_ROYALTY) / 10000;
return (_receiver, _royaltyAmount > maxRoyalty ? maxRoyalty : _royaltyAmount);
}
} catch {
return (address(0), 0);
}
}
} catch {
return (address(0), 0);
}
return (address(0), 0);
}
function _checkInterface(IERC721 nft) internal view returns (bool) {
try nft.supportsInterface(type(IERC721).interfaceId) returns (bool supported) {
return supported;
} catch {
return false;
}
}
/* ========== PAYMENT FUNCTIONS ========== */
function withdrawPendingPayments() external nonReentrant whenNotPaused {
uint256 amount = pendingPayments[msg.sender];
if(amount == 0) revert InsufficientPendingPayment();
// Update state before transfer
pendingPayments[msg.sender] = 0;
// Execute transfer
(bool sent,) = payable(msg.sender).call{value: amount}("");
if(!sent) revert TransferFailed();
emit PaymentPending(msg.sender, 0); // Update pending amount to 0
}
/* ========== ADMIN FUNCTIONS ========== */
function updateMarketplaceFee(uint256 _newFee) external onlyOwner {
if(_newFee > 1000) revert InvalidPrice();
marketplaceFee = _newFee;
emit MarketplaceFeeUpdated(_newFee);
}
function emergencyWithdraw() external onlyOwner {
uint256 balance = address(this).balance;
if(balance > 0) {
(bool success,) = payable(owner()).call{value: balance}("");
if(!success) revert TransferFailed();
emit EmergencyWithdraw(owner(), balance);
}
}
function pause() external onlyOwner {
_pause();
}
function unpause() external onlyOwner {
_unpause();
}
/* ========== MAINTENANCE FUNCTIONS ========== */
function cleanupExpiredListings(
address nftContract,
uint256 batchSize
) external nonReentrant {
if(batchSize > CLEANUP_BATCH_SIZE) revert BatchSizeTooLarge();
CollectionListings storage collection = collectionListings[nftContract];
uint256 length = collection.listings.length;
uint256 processed = 0;
uint256 cleanedCount = 0;
for (uint256 i = 0; i < length && processed < batchSize; i++) {
Listing memory listing = collection.listings[i];
if (listing.active && block.timestamp >= listing.expirationTime) {
if (listing.highestBidder != address(0)) {
pendingPayments[listing.highestBidder] += listing.highestBid;
emit PaymentPending(listing.highestBidder, listing.highestBid);
}
_removeListingFromCollection(nftContract, listing.tokenId);
cleanedCount++;
}
processed++;
}
if (cleanedCount > 0) {
emit CollectionListingsCleaned(nftContract, cleanedCount);
}
}
/* ========== VIEW FUNCTIONS ========== */
function getPendingPayment(address user) external view returns (uint256) {
return pendingPayments[user];
}
function getCollectionListings(
address nftContract,
uint256 offset,
uint256 limit
) external view returns (Listing[] memory, uint256 total) {
CollectionListings storage collection = collectionListings[nftContract];
uint256 length = collection.listings.length;
if (offset >= length) {
return (new Listing[](0), length);
}
uint256 remaining = length - offset;
uint256 currentLimit = remaining < limit ? remaining : limit;
Listing[] memory result = new Listing[](currentLimit);
for (uint256 i = 0; i < currentLimit; i++) {
result[i] = collection.listings[offset + i];
}
return (result, length);
}
function getCollectionRoyalty(
address _nftContract
) external view returns (RoyaltyInfo memory) {
return collectionRoyalties[_nftContract];
}
/* ========== IERC721Receiver ========== */
function onERC721Received(
address,
address,
uint256,
bytes calldata
) external pure override returns (bytes4) {
return this.onERC721Received.selector;
}
}