Clearing Marketplace Offers

The Marketplace handles Offers, meaning people offering their punk for sale. We definitely want to clear those on transfer. The new owner might not want to sell the punk...

We looked at the Offer struct in detail in the data types section:

struct Offer {    bool isForSale;    uint punkIndex;    address seller;    uint minValue;    address onlySellTo;}mapping (uint => Offer) public punksOfferedForSale;

Looking at this, it's pretty obvious what we have to implement with what we know so far. We need to clear the sell offer on transfer. Let's add the following code to our function:

// If the current punk is for sale...if (punksOfferedForSale[punkIndex].isForSale) {  // We reset the offer to...  punksOfferedForSale[punkIndex] = Offer(    false,      // isForSale = false    punkIndex,  // punkIndex = the current punk    msg.sender, // seller = the new owner (future seller)    0,          // minValue = 0 (no price since it's not for sale)    0x0         // onlySellTo = no special offer recipient  );  // We let outside applications know the punk is not for sale anymore   // by emitting an Event.  PunkNoLongerForSale(punkIndex);}

Since this code is used in multiple places, Matt and John extracted it to its own function called punkNoLongerForSale(). So in the context of our transfer function it just looks like this:

// Transfer ownership of a punk to another user without requiring paymentfunction transferPunk(address to, uint punkIndex) {  // Throw an error if called by anyone but the punk owner  if (punkIndexToAddress[punkIndex] != msg.sender) throw;  // Our contract only allows managing punks #0 – #9,999  if (punkIndex >= 10000) throw;  // Clear the open sell Offer if there is one  if (punksOfferedForSale[punkIndex].isForSale) {    punkNoLongerForSale(punkIndex);  }  // Assign the new owner  punkIndexToAddress[punkIndex] = to;  // Remove the punk from the total balance of the previous owner  balanceOf[msg.sender]--;  // Add the punk to the total balance of the new owner  balanceOf[to]++;  // Let outside applications know of the transfer by emitting an Event  PunkTransfer(msg.sender, to, punkIndex);}

Congrats for getting this far 👏!