|
| 1 | +//SPDX-License-Identifier: MIT |
| 2 | +pragma solidity >=0.8.0 <0.9.0; |
| 3 | + |
| 4 | +import { IListingType } from "./IListingType.sol"; |
| 5 | + |
| 6 | +interface IERC20 { |
| 7 | + function transfer(address to, uint256 value) external returns (bool); |
| 8 | + function transferFrom(address from, address to, uint256 value) external returns (bool); |
| 9 | +} |
| 10 | + |
| 11 | +contract SimpleListings is IListingType { |
| 12 | + // Custom errors |
| 13 | + error PriceZero(); |
| 14 | + error IpfsHashEmpty(); |
| 15 | + error NotActive(); |
| 16 | + error IncorrectEth(); |
| 17 | + error NoEthWithErc20(); |
| 18 | + error Erc20TransferFailed(); |
| 19 | + error NotCreator(); |
| 20 | + error AlreadyClosed(); |
| 21 | + error NotMarketplace(); |
| 22 | + error MarketplaceZeroAddress(); |
| 23 | + error EthSendFailed(); |
| 24 | + error BeforeCloseFailed(); |
| 25 | + error OnCloseFailed(); |
| 26 | + error AfterCloseFailed(); |
| 27 | + struct SimpleListing { |
| 28 | + address creator; |
| 29 | + address paymentToken; // address(0) for ETH, ERC20 otherwise |
| 30 | + uint256 price; |
| 31 | + string ipfsHash; |
| 32 | + bool active; |
| 33 | + } |
| 34 | + |
| 35 | + address public immutable marketplace; |
| 36 | + uint256 public simpleListingCount; |
| 37 | + mapping(uint256 => SimpleListing) public listings; |
| 38 | + |
| 39 | + event SimpleListingCreated( |
| 40 | + uint256 indexed listingId, |
| 41 | + address indexed creator, |
| 42 | + address paymentToken, |
| 43 | + uint256 price, |
| 44 | + string ipfsHash |
| 45 | + ); |
| 46 | + event SimpleListingSold(uint256 indexed listingId, address indexed buyer, uint256 price, address paymentToken); |
| 47 | + event SimpleListingClosed(uint256 indexed listingId, address indexed caller); |
| 48 | + |
| 49 | + constructor(address _marketplace) { |
| 50 | + if (_marketplace == address(0)) revert MarketplaceZeroAddress(); |
| 51 | + marketplace = _marketplace; |
| 52 | + } |
| 53 | + |
| 54 | + modifier onlyMarketplace() { |
| 55 | + if (msg.sender != marketplace) revert NotMarketplace(); |
| 56 | + _; |
| 57 | + } |
| 58 | + |
| 59 | + // View helpers |
| 60 | + function getListing(uint256 listingId) external view returns (bytes memory data) { |
| 61 | + SimpleListing memory l = listings[listingId]; |
| 62 | + return abi.encode(l.creator, l.paymentToken, l.price, l.ipfsHash, l.active); |
| 63 | + } |
| 64 | + |
| 65 | + // Creation lifecycle |
| 66 | + function beforeCreate(bytes calldata data) external view onlyMarketplace returns (bool) { |
| 67 | + // data encoding: abi.encode(address paymentToken, uint256 price, string ipfsHash) |
| 68 | + (, uint256 price, string memory ipfsHash) = abi.decode(data, (address, uint256, string)); |
| 69 | + if (price == 0) revert PriceZero(); |
| 70 | + if (bytes(ipfsHash).length == 0) revert IpfsHashEmpty(); |
| 71 | + // paymentToken can be zero for ETH or any ERC20 address |
| 72 | + return true; |
| 73 | + } |
| 74 | + |
| 75 | + function onCreate(address creator, bytes calldata data) external onlyMarketplace returns (uint256 listingId) { |
| 76 | + (address paymentToken, uint256 price, string memory ipfsHash) = abi.decode(data, (address, uint256, string)); |
| 77 | + listingId = ++simpleListingCount; |
| 78 | + listings[listingId] = SimpleListing({ |
| 79 | + creator: creator, |
| 80 | + paymentToken: paymentToken, |
| 81 | + price: price, |
| 82 | + ipfsHash: ipfsHash, |
| 83 | + active: true |
| 84 | + }); |
| 85 | + emit SimpleListingCreated(listingId, creator, paymentToken, price, ipfsHash); |
| 86 | + } |
| 87 | + |
| 88 | + function afterCreate(uint256 /*listingId*/, bytes calldata /*data*/) external view onlyMarketplace returns (bool) { |
| 89 | + return true; |
| 90 | + } |
| 91 | + |
| 92 | + // Sale lifecycle |
| 93 | + function beforeSale( |
| 94 | + uint256 listingId, |
| 95 | + address /*buyer*/, |
| 96 | + bytes calldata /*data*/ |
| 97 | + ) external view onlyMarketplace returns (bool) { |
| 98 | + SimpleListing memory l = listings[listingId]; |
| 99 | + if (!l.active) revert NotActive(); |
| 100 | + return true; |
| 101 | + } |
| 102 | + |
| 103 | + function onSale( |
| 104 | + uint256 listingId, |
| 105 | + address buyer, |
| 106 | + bytes calldata /*data*/ |
| 107 | + ) external payable onlyMarketplace returns (bool) { |
| 108 | + SimpleListing storage l = listings[listingId]; |
| 109 | + if (!l.active) revert NotActive(); |
| 110 | + |
| 111 | + if (l.paymentToken == address(0)) { |
| 112 | + if (msg.value != l.price) revert IncorrectEth(); |
| 113 | + // forward ETH directly to creator |
| 114 | + (bool sent, ) = l.creator.call{ value: msg.value }(""); |
| 115 | + if (!sent) revert EthSendFailed(); |
| 116 | + } else { |
| 117 | + if (msg.value != 0) revert NoEthWithErc20(); |
| 118 | + // buyer must approve this contract to spend price amount |
| 119 | + bool ok = IERC20(l.paymentToken).transferFrom(buyer, l.creator, l.price); |
| 120 | + if (!ok) revert Erc20TransferFailed(); |
| 121 | + } |
| 122 | + |
| 123 | + emit SimpleListingSold(listingId, buyer, l.price, l.paymentToken); |
| 124 | + return true; |
| 125 | + } |
| 126 | + |
| 127 | + function afterSale( |
| 128 | + uint256 listingId, |
| 129 | + address /*buyer*/, |
| 130 | + bytes calldata /*data*/ |
| 131 | + ) external onlyMarketplace returns (bool) { |
| 132 | + SimpleListing storage l = listings[listingId]; |
| 133 | + if (!l.active) revert NotActive(); |
| 134 | + l.active = false; |
| 135 | + return true; |
| 136 | + } |
| 137 | + |
| 138 | + // Pre-buy lifecycle (no-op for simple listings) |
| 139 | + function beforePreBuy( |
| 140 | + uint256 /*listingId*/, |
| 141 | + address /*buyer*/, |
| 142 | + bytes calldata /*data*/ |
| 143 | + ) external view onlyMarketplace returns (bool) { |
| 144 | + return true; |
| 145 | + } |
| 146 | + |
| 147 | + function onPreBuy( |
| 148 | + uint256 /*listingId*/, |
| 149 | + address /*buyer*/, |
| 150 | + bytes calldata /*data*/ |
| 151 | + ) external payable onlyMarketplace returns (bool) { |
| 152 | + return true; |
| 153 | + } |
| 154 | + |
| 155 | + function afterPreBuy( |
| 156 | + uint256 /*listingId*/, |
| 157 | + address /*buyer*/, |
| 158 | + bytes calldata /*data*/ |
| 159 | + ) external view onlyMarketplace returns (bool) { |
| 160 | + return true; |
| 161 | + } |
| 162 | + |
| 163 | + // Admin lifecycle |
| 164 | + function beforeClose( |
| 165 | + uint256 listingId, |
| 166 | + address caller, |
| 167 | + bytes calldata /*data*/ |
| 168 | + ) external view onlyMarketplace returns (bool) { |
| 169 | + SimpleListing memory l = listings[listingId]; |
| 170 | + if (l.creator != caller) revert NotCreator(); |
| 171 | + if (!l.active) revert AlreadyClosed(); |
| 172 | + return true; |
| 173 | + } |
| 174 | + |
| 175 | + function onClose( |
| 176 | + uint256 listingId, |
| 177 | + address caller, |
| 178 | + bytes calldata /*data*/ |
| 179 | + ) external onlyMarketplace returns (bool) { |
| 180 | + SimpleListing storage l = listings[listingId]; |
| 181 | + if (l.creator != caller) revert NotCreator(); |
| 182 | + if (!l.active) revert AlreadyClosed(); |
| 183 | + l.active = false; |
| 184 | + return true; |
| 185 | + } |
| 186 | + |
| 187 | + function afterClose( |
| 188 | + uint256 listingId, |
| 189 | + address caller, |
| 190 | + bytes calldata /*data*/ |
| 191 | + ) external onlyMarketplace returns (bool) { |
| 192 | + emit SimpleListingClosed(listingId, caller); |
| 193 | + return true; |
| 194 | + } |
| 195 | + |
| 196 | + // Backward compatibility |
| 197 | + function closeListing(uint256 listingId, address caller) external onlyMarketplace returns (bool) { |
| 198 | + if (!this.beforeClose(listingId, caller, "")) revert BeforeCloseFailed(); |
| 199 | + if (!this.onClose(listingId, caller, "")) revert OnCloseFailed(); |
| 200 | + if (!this.afterClose(listingId, caller, "")) revert AfterCloseFailed(); |
| 201 | + return true; |
| 202 | + } |
| 203 | +} |
0 commit comments