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;
}
}
|