Skip to content

Instantly share code, notes, and snippets.

@cupOJoseph
Created December 14, 2023 03:38
Show Gist options
  • Save cupOJoseph/910391de3083223f81c80aea005c198c to your computer and use it in GitHub Desktop.
Save cupOJoseph/910391de3083223f81c80aea005c198c to your computer and use it in GitHub Desktop.
white elephant gift exchange 30 minute coding challenge as a solidity smart contract
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
contract Tickets is ERC721, Ownable {
uint ticketNumber = 0;
uint giftingTime = 1703462400;
bool[] giftList;
mapping(uint giftListPresent => address giftAddress) giftsAddresses;
mapping(uint giftListPresent => uint giftListPresentID) giftsOriginIds;
constructor(address initialOwner)
ERC721("tickets", "TICKET")
Ownable(initialOwner)
{}
function _baseURI() internal pure override returns (string memory) {
return "https://i.imgur.com/10m8z79.png";
}
function openPresent(uint burnTicketId) external{
require(block.timestamp > giftingTime);
//make sure the msg sender has a ticket
require(ownerOf(burnTicketId) == msg.sender);
//burn a ticket
safeTransferFrom(msg.sender, address(0), burnTicketId);
//get the last present in the line and take it!
uint giftId = block.number % giftList.length;
//send giftList[giftID]
address giftNFTAddress = giftsAddresses[giftId];
uint giftsOriginId = giftsOriginIds[giftId];
giftList.pop();
//yeet
ERC721(giftNFTAddress).transferFrom(address(this), msg.sender, giftsOriginId);
}
function depositPresent(address nftContract, uint id) external {
require(block.timestamp < giftingTime);
//move erc721 present to this contract
ERC721(nftContract).transferFrom(msg.sender, address(this), id);
//add it to the list
giftsAddresses[giftList.length] = nftContract;
giftsOriginIds[giftList.length] = id;
giftList.push(true);
//give the msg sender a ticket: _safeMint(to, tokenId);
_safeMint(msg.sender, ticketNumber);
ticketNumber += 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment