The cookie market is bustling with activity as vendors sell their festive treats and holiday shoppers rush to find the perfect cookies for their loved ones.
As Santa Claus makes his way through the market, he is greeted with warm smiles and cheerful greetings from the vendors and shoppers alike. Children run up to him, excited to see the jolly old man in person, and Santa takes the time to chat with them and hear their holiday wishes.
As he moves from booth to booth, Santa tastes a variety of cookies, from classic sugar cookies decorated with frosting and sprinkles, to more exotic flavors like gingerbread and spiced shortbread. He even tries his hand at decorating a few cookies himself, much to the delight of the children watching.
After he has gathered all the supplies he needs, Santa thanks the vendors and shoppers for their hospitality and heads back to his workshop to begin preparing for his long journey. As he sets off, he is filled with the warmth and joy of the holiday season, knowing that he will bring a little bit of that magic to every child he visits.
// SPDX-License-Identifier: MITpragmasolidity0.8.17;import"./ERC721.sol";contractCookieisERC721("cookie","E"){uint256publiccookieIDX;addresspublicowner;constructor(){cookieIDX=0;}// @dev mints an cookie. Note that there are only 10 cookies in the basket.functionmintcookie()external{require(cookieIDX<10);_mint(msg.sender,cookieIDX);cookieIDX+=1;}}
// SPDX-License-Identifier: MITpragmasolidity0.8.17;import"./IERC721.sol";import"./IERC721Receiver.sol";contractCookieMarketisIERC721Receiver{// mapping that handles ownership of the cookies within the CookieMarket.mapping(uint256=>address)publiccanRedeemcookie;// struct that handles the orders in the marketstructsell_Order{uint256cookie_idx_offered;// the ERC721 idx of the "cookie" token.uint256amount_eth_wanted;// the amount of ETH the seller wants to receive for the cookie.addresscookie_provider;// the address of the seller.}// storing all the sell orders in the market.sell_Order[]publicsellOrders;// cookieIERC721publiccookie;/** @dev cookieMarket constructor. @param _cookie ERC721 contract instance. */constructor(address_cookie){cookie=IERC721(_cookie);}/** @dev Allows a buyer to buy an cookie from the cookieMarket via exhausting its subsequent sell order. @param _idx The ERC721 idx of the cookie. @param _owner The `current` owner of the cookie. */functionexecuteOrder(uint256_idx,address_owner)externalpayable{require(msg.sender!=_owner,"err: no self-exchanges allowed");// find the sellOrder whose cookie_idx_offered == _idxfor(uint256i=0;i<sellOrders.length;i++){if(sellOrders[i].cookie_idx_offered==_idx){// check if the _owner is the sellerrequire(sellOrders[i].cookie_provider==_owner,"err: _owner != seller");// the cookie is for sale.// check if the msg.sender has provided enough ETH to pay for the cookieif(msg.value>=sellOrders[i].amount_eth_wanted){// the _owner has enough ETH to pay for the cookie// paying the seller(current owner) of the cookie(boolsent,bytesmemorydata)=_owner.call{value:msg.value}("");require(sent,"err: transfer failed");// transfer the ownership of the cookie from the seller to the buyercanRedeemcookie[_idx]=msg.sender;// remove the sellOrder from the sellOrders arraysellOrders[i]=sellOrders[sellOrders.length-1];sellOrders.pop();break;}}}}/** @dev Function to retrieve an cookie from the market. @param _idx The index of the cookie in the market. */functionredeemcookies(uint256_idx)external{// check if sender can redeem the cookierequire(canRedeemcookie[_idx]==msg.sender,"err: msg.sender != owner(cookie)");// approve the cookie transfer.cookie.approve(msg.sender,_idx);// transfer the ownership of the cookie.cookie.transferFrom(address(this),msg.sender,_idx);// remove the cookie _idx from the canRedeemcookie mappingdeletecanRedeemcookie[_idx];}/** @dev Function to effectively add a sellOrder for your cookie on the cookieMarket. @param _cookieIDX The index of the ERC721 cookie. @param _ethWanted The amount of ETH the seller wants to receive for the cookie. */functionaddSellOrder(uint256_cookieIDX,uint256_ethWanted)external{// check whether the msg.sender can sell the _cookieIDXrequire(canRedeemcookie[_cookieIDX]==msg.sender,"err: msg.sender != owner(cookie[_cookieIDX])");// create the new sellOrdersell_OrdermemorynewOrder;newOrder.cookie_idx_offered=_cookieIDX;newOrder.amount_eth_wanted=_ethWanted;newOrder.cookie_provider=msg.sender;sellOrders.push(newOrder);}/** @dev Function to effectively remove a sellOrder from the cookieMarket. @param _cookieIDX The index of the ERC721 cookie. */functionremoveSellOrder(uint256_cookieIDX)external{// iterate through all sellOrdersfor(uint256i=0;i<sellOrders.length;i++){// check if the sellOrder is for the _cookieIDXif(sellOrders[i].cookie_idx_offered==_cookieIDX){// check if the msg.sender is the owner of the cookierequire(sellOrders[i].cookie_provider==msg.sender,"err: msg.sender != cookie_provider");// delete the sellOrdersellOrders[i]=sellOrders[sellOrders.length-1];sellOrders.pop();break;}}}/** @dev Inherited from IERC721Receiver. */functiononERC721Received(address,address_from,uint256_tokenId,bytescalldata)externaloverridereturns(bytes4){// we have received an cookie from its owner; mark that in the redeem mappingcanRedeemcookie[_tokenId]=_from;returnthis.onERC721Received.selector;}}