The complete transferPunk Function
There are a couple more things to fix/add to our transferPunk()
function. Let's look at the complete code and go through it line by line:
// Transfer ownership of a punk to another user without requiring paymentfunction transferPunk(address to, uint punkIndex) { // If not all punks are handed out, throw an error if (!allPunksAssigned) throw; // 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 Transfer(msg.sender, to, 1); // Emit a Transfer event that's backwards compatible with ERC20 tokens PunkTransfer(msg.sender, to, punkIndex); // Check for the case where there is a bid from the new owner and refund it. // Any other bid can stay in place. Bid bid = punkBids[punkIndex]; if (bid.bidder == to) { // Kill bid and refund value pendingWithdrawals[to] += bid.value; punkBids[punkIndex] = Bid(false, punkIndex, 0x0, 0); }}
There is a couple new points here we haven't discussed:
- Don't allow transfers if not all punks are handed out yet.
- Prevent owners from bidding on their punks (by buying a punk they already bid on).
We'll touch on both of these things in more detail when we cover the initial distribution and Bids in general.
To be continued...