Skip to content

Latest commit

 

History

History
174 lines (132 loc) · 5.97 KB

File metadata and controls

174 lines (132 loc) · 5.97 KB
title Transferring Tokens - Part 2
actions
checkAnswer
hints
material
editor
language startingCode answer
c#
public class AlienFinder : SmartContract { ... public static event Action<byte[], byte[], byte[]> Transfer; public static void OnTransfer (byte[] from, byte[] to, byte[] tokenId) { if (Transfer != null) Transfer (from, to, tokenId); } public static string name () => "Alien"; public static string symbol () => "ALI"; public static BigInteger totalSupply () => getCounter (); public static byte decimals () => 0; public static BigInteger balanceOf (byte[] addr) { if (addr.Length != 20) throw new InvalidOperationException ("The parameter owner should be a 20-byte address"); BigInteger balance = 0; byte[] value = Storage.Get(addr); if (value != null) balance = value.ToBigInteger(); return balance; } public static byte[] ownerOf (byte[] id) { Alien a = Query (id); if (a != null) return a.Owner; return null; } public static Alien[] tokensOf (byte[] owner) { if (owner.Length != 20) throw new InvalidOperationException ("The parameter owner should be a 20-byte address"); int balance = (int) balanceOf (owner); Alien[] tokens = new Alien[balance + 1]; int idx = 0; for (BigInteger id = 1; id <= totalSupply (); id = id + 1) { Alien token = Query (id.ToByteArray ()); if (token != null && token.Owner == owner) { tokens[idx] = token; idx = idx + 1; } } return tokens; } public static bool transfer (byte[] to, byte[] tokenid) { if (to.Length != 20) throw new InvalidOperationException ("The parameter owner should be a 20-byte address"); Alien token = Query (tokenid); if (token == null) throw new InvalidOperationException ("Invalid Alien token id"); if (!Runtime.CheckWitness (token.Owner)) return false; // Complete (1) HERE byte[] from = // Complete (2) HERE StorageMap alienMap = // Complete (3) HERE } }
public class AlienFinder : SmartContract { ... public static event Action<byte[], byte[], byte[]> Transfer; public static void OnTransfer (byte[] from, byte[] to, byte[] tokenId) { if (Transfer != null) Transfer (from, to, tokenId); } public static string name () => "Alien"; public static string symbol () => "ALI"; public static BigInteger totalSupply () => getCounter (); public static byte decimals () => 0; public static BigInteger balanceOf (byte[] addr) { if (addr.Length != 20) throw new InvalidOperationException ("The parameter owner should be a 20-byte address"); BigInteger balance = 0; byte[] value = Storage.Get(addr); if (value != null) balance = value.ToBigInteger(); return balance; } public static byte[] ownerOf (byte[] id) { Alien a = Query (id); if (a != null) return a.Owner; return null; } public static Alien[] tokensOf (byte[] owner) { if (owner.Length != 20) throw new InvalidOperationException ("The parameter owner should be a 20-byte address"); int balance = (int) balanceOf (owner); Alien[] tokens = new Alien[balance + 1]; int idx = 0; for (BigInteger id = 1; id <= totalSupply (); id = id + 1) { Alien token = Query (id.ToByteArray ()); if (token != null && token.Owner == owner) { tokens[idx] = token; idx = idx + 1; } } return tokens; } public static bool transfer (byte[] to, byte[] tokenid) { if (to.Length != 20) throw new InvalidOperationException ("The parameter owner should be a 20-byte address"); Alien token = Query (tokenid); if (token == null) throw new InvalidOperationException ("Invalid Alien token id"); if (!Runtime.CheckWitness (token.Owner)) return false; byte[] from = token.Owner; Storage.Put(from, balanceOf(from) - 1); Storage.Put(to, balanceOf(to) + 1); token.Owner = to; StorageMap alienMap = Storage.CurrentContext.CreateMap(nameof(alienMap)); alienMap.Put(tokenid, Helper.Serialize(token)); OnTransfer (token.Owner, to, tokenid); return true; } }

When a transfer takes place, the following data needs to be modified:

  • The balance of the original owner
  • The balance of the new owner
  • The Owner property of the token

When a transfer is performed, an event must also be fired and the transfer () method must return true.

Instructions

Based on the requirements above, complete the rest of transfer ()

  1. Perform the transfer

    • Update the balance of from and to using Storage.Put. Each in a single line.
    • Update the Owner of token.
  2. Store the modified token

  3. Fire event and return true.