File size: 2,088 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 | // 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 {IERC1155} from "@openzeppelin/contracts/token/ERC1155/IERC1155.sol";
contract Exchange15 {
IERC20 public immutable paymentToken;
IERC721 public immutable collection;
IERC1155 public immutable items;
address public immutable buyer;
address public immutable seller;
uint256 public immutable payment;
uint256 public units;
bool public funded;
bool public filled;
constructor(address payment_, address collection_, address items_, address buyer_, address seller_, uint256 price_) {
paymentToken = IERC20(payment_);
collection = IERC721(collection_);
items = IERC1155(items_);
buyer = buyer_;
seller = seller_;
payment = price_;
}
function configure(uint256 value) external {
require(msg.sender == buyer && !filled, "only buyer");
units = value;
}
function fund() external {
require(msg.sender == buyer && !funded, "only buyer");
require(paymentToken.transferFrom(buyer, address(this), payment), "payment required");
funded = true;
}
function displayText() external pure returns (string memory) { return "Offer O for NFT #7"; }
function itemCount() external view returns (uint256) { return units == 0 ? 1 : 2; }
function itemAt(uint256 index) external view returns (address asset, uint256 id, uint256 amount) {
require(index < (units == 0 ? 1 : 2), "index range");
return index == 0 ? (address(collection), 7, 1) : (address(items), 5, units);
}
function accept() external {
require(msg.sender == seller && funded && !filled, "order unavailable");
filled = true;
collection.transferFrom(seller, buyer, 7);
if (units > 0) items.safeTransferFrom(seller, buyer, 5, units, "");
require(paymentToken.transfer(seller, payment), "payment required");
}
}
|