contract PixelSale { // Constants uint constant WIDTH = 1920; uint constant HEIGHT = 1080; uint constant PIXEL_PRICE = 1 ether; uint constant MAX_DURATION = 86400; // 24 hours uint constant RESALE_FEE = 75; // percentage of original price // Events event PixelBought(uint indexed x, uint indexed y, address indexed buyer, uint duration, string color, string link); event PixelResold(uint indexed x, uint indexed y, address indexed buyer, uint duration, string color, string link, uint price, address originalBuyer); // Variables mapping(uint => mapping(uint => Pixel)) private pixels; uint private totalEarnings; struct Pixel { address owner; uint duration; string color; string link; uint originalPrice; } // Public functions function buyPixel(uint x, uint y, uint duration, string calldata color, string calldata link) public payable { require(x < WIDTH && y < HEIGHT, "Pixel coordinates out of bounds"); require(msg.value == duration * PIXEL_PRICE, "Incorrect payment amount"); require(duration <= MAX_DURATION, "Duration too long"); Pixel storage pixel = pixels[x][y]; require(pixel.owner == address(0), "Pixel already owned"); pixel.owner = msg.sender; pixel.duration = duration; pixel.color = color; pixel.link = link; pixel.originalPrice = msg.value; totalEarnings += msg.value; emit PixelBought(x, y, msg.sender, duration, color, link); } function resellPixel(uint x, uint y) public payable { Pixel storage pixel = pixels[x][y]; require(pixel.owner != address(0), "Pixel not owned"); require(msg.sender != pixel.owner, "You already own this pixel"); uint resellPrice = pixel.originalPrice * RESALE_FEE / 100; require(msg.value == resellPrice, "Incorrect payment amount"); uint originalOwnerEarnings = pixel.originalPrice * 50 / 100; uint topUpAmount = resellPrice - originalOwnerEarnings; totalEarnings += topUpAmount; pixel.owner.transfer(originalOwnerEarnings); pixel.owner = msg.sender; pixel.duration = 0; pixel.color = ""; pixel.link = ""; emit PixelResold(x, y, msg.sender, pixel.originalPrice, pixel.color, pixel.link, resellPrice, pixel.owner); } // View functions function getPixel(uint x, uint y) public view returns(address owner, uint duration, string memory color, string memory link) { Pixel storage pixel = pixels[x][y]; owner = pixel.owner; duration = pixel.duration; color = pixel.color; link = pixel.link; } function getEarnings() public view returns(uint) { return totalEarnings; } }
0.7.1