Skip to content

Commit

Permalink
Add tokens to NFT native contract (#2271)
Browse files Browse the repository at this point in the history
  • Loading branch information
erikzhang authored Jan 27, 2021
1 parent 5c88a99 commit 7938b93
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions src/neo/SmartContract/Native/NonfungibleToken.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#pragma warning disable IDE0051

using Neo.IO;
using Neo.Persistence;
using Neo.SmartContract.Iterators;
Expand Down Expand Up @@ -121,19 +123,18 @@ public BigInteger BalanceOf(DataCache snapshot, UInt160 owner)
}

[ContractMethod(0_01000000, CallFlags.ReadStates)]
public IIterator TokensOf(DataCache snapshot, UInt160 owner)
private IIterator Tokens(DataCache snapshot)
{
if (owner is null)
{
var results = snapshot.Find(new[] { Prefix_Token }).GetEnumerator();
return new StorageIterator(results, FindOptions.ValuesOnly | FindOptions.DeserializeValues | FindOptions.PickField1, null);
}
else
{
NFTAccountState account = snapshot.TryGet(CreateStorageKey(Prefix_Account).Add(owner))?.GetInteroperable<NFTAccountState>();
IReadOnlyList<byte[]> tokens = account?.Tokens ?? (IReadOnlyList<byte[]>)System.Array.Empty<byte[]>();
return new ArrayWrapper(tokens.Select(p => (StackItem)p).ToArray());
}
var results = snapshot.Find(new[] { Prefix_Token }).GetEnumerator();
return new StorageIterator(results, FindOptions.ValuesOnly | FindOptions.DeserializeValues | FindOptions.PickField1, null);
}

[ContractMethod(0_01000000, CallFlags.ReadStates)]
private IIterator TokensOf(DataCache snapshot, UInt160 owner)
{
NFTAccountState account = snapshot.TryGet(CreateStorageKey(Prefix_Account).Add(owner))?.GetInteroperable<NFTAccountState>();
IReadOnlyList<byte[]> tokens = account?.Tokens ?? (IReadOnlyList<byte[]>)System.Array.Empty<byte[]>();
return new ArrayWrapper(tokens.Select(p => (StackItem)p).ToArray());
}

[ContractMethod(0_09000000, CallFlags.WriteStates | CallFlags.AllowNotify)]
Expand Down

0 comments on commit 7938b93

Please sign in to comment.