| title |
Non-Fungible Tokens & NEP-11 |
| actions |
|
| material |
| editor |
| language |
startingCode |
answer |
c# |
public class AlienFinder : SmartContract {
...
public class Alien {
public uint Xna { get; set; }
public string AlienName { get; set; }
public BigInteger Id { get; set; }
// Complete (1) HERE
}
public static BigInteger GenerateAlien (string alienName, byte[] owner) {
if (owner.Length != 20 && owner.Length != 33)
throw new InvalidOperationException ("The parameter owner should be a 20-byte address or a 33-byte public key");
if (!Runtime.CheckWitness (owner)) return 0;
uint xna = FindXna (RandomNumber ());
Alien someAlien = new Alien {
Xna = xna,
AlienName = alienName,
Id = updateCounter (),
// Complete (2) HERE
};
StorageMap alienMap = Storage.CurrentContext.CreateMap (nameof (alienMap));
alienMap.Put (someAlien.Id.ToByteArray (), Helper.Serialize (someAlien));
OnAlienGenerated (someAlien.Id);
BigInteger balance = balanceOf(owner);
Storage.Put(owner, balance + 1);
return someAlien.Id;
}
...
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
}
}
|
public class AlienFinder : SmartContract {
...
public class Alien {
public uint Xna { get; set; }
public string AlienName { get; set; }
public BigInteger Id { get; set; }
public byte[] Owner { get; set; }
}
public static BigInteger GenerateAlien (string alienName, byte[] owner) {
if (owner.Length != 20 && owner.Length != 33)
throw new InvalidOperationException ("The parameter owner should be a 20-byte address or a 33-byte public key");
if (!Runtime.CheckWitness (owner)) return 0;
uint xna = FindXna (RandomNumber ());
Alien someAlien = new Alien {
Xna = xna,
AlienName = alienName,
Id = updateCounter (),
Owner = owner
};
StorageMap alienMap = Storage.CurrentContext.CreateMap (nameof (alienMap));
alienMap.Put (someAlien.Id.ToByteArray (), Helper.Serialize (someAlien));
OnAlienGenerated (someAlien.Id);
BigInteger balance = balanceOf(owner);
Storage.Put(owner, balance + 1);
return someAlien.Id;
}
...
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;
}
}
|
|
|
You may have noticed that the NEP-5 standard we have been using does not account for the fact that each of our alien has a unique ID and may have different attributes. What if we want to look up the owner account of a specific Alien? What if we want to transfer the ownership of that Alien?
NEP-11 is a token standard introduced for objects just like Aliens. It is called 'non-fungible' because unlike conventional currencies, where a one-dollar bill is an equal substitued for another (we are not evaluating currencies like currency collectors in this case), one Alien token is not an equal substitute for another due to their uniqueness.
NEP-11 is also built upon the NEP-5 standard, which means that all NEP-5 methods and events are present in NEP-11 tokens. And so we can continue working on the NEP-5 contract we have been building, and have it comply with NEP-11.
Here are the additional methods that NEP-11 standard introduced:
ownerOf () - returns a collection that contains all the co-owners that own the specified token.
tokensOf () - returns a collection that contains all of the token ids owned by the specified address.
properties () - returns a serialized NVM object containing the properties for the given NFT.
NEP-11 introduced the idea of a token's owner, so we will need to adjust the Alien object accordingly.
- Add property Owner with type
byte[]
- In
GenerateAlien () update new Alien declaration accordingly.
- Implement
ownerOf ()
- Find an Alien by its
id. (Use Query ())
- If
a is not null, return its Owner.
- Otherwise return null.