Пример #1
0
        protected 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()));
        }
Пример #2
0
        protected bool Transfer(ApplicationEngine engine, UInt160 to, byte[] tokenId)
        {
            if (to is null)
            {
                throw new ArgumentNullException(nameof(to));
            }
            StorageKey key_token = CreateStorageKey(Prefix_Token).Add(GetKey(tokenId));
            TokenState token     = engine.Snapshot.TryGet(key_token)?.GetInteroperable <TokenState>();
            UInt160    from      = token.Owner;

            if (!from.Equals(engine.CallingScriptHash) && !engine.CheckWitnessInternal(from))
            {
                return(false);
            }
            if (!from.Equals(to))
            {
                token = engine.Snapshot.GetAndChange(key_token).GetInteroperable <TokenState>();
                StorageKey      key_from = CreateStorageKey(Prefix_Account).Add(from);
                NFTAccountState account  = engine.Snapshot.GetAndChange(key_from).GetInteroperable <NFTAccountState>();
                account.Remove(tokenId);
                if (account.Balance.IsZero)
                {
                    engine.Snapshot.Delete(key_from);
                }
                token.Owner = to;
                StorageKey key_to = CreateStorageKey(Prefix_Account).Add(to);
                account = engine.Snapshot.GetAndChange(key_to, () => new StorageItem(new NFTAccountState())).GetInteroperable <NFTAccountState>();
                account.Add(tokenId);
                OnTransferred(engine, from, token);
            }
            PostTransfer(engine, from, to, tokenId);
            return(true);
        }
Пример #3
0
        protected void Mint(ApplicationEngine engine, TokenState token)
        {
            engine.Snapshot.Add(CreateStorageKey(Prefix_Token).Add(GetKey(token.Id)), new StorageItem(token));
            NFTAccountState account = engine.Snapshot.GetAndChange(CreateStorageKey(Prefix_Account).Add(token.Owner), () => new StorageItem(new NFTAccountState())).GetInteroperable <NFTAccountState>();

            account.Add(token.Id);
            engine.Snapshot.GetAndChange(CreateStorageKey(Prefix_TotalSupply)).Add(1);
            PostTransfer(engine, null, token.Owner, token.Id);
        }
Пример #4
0
 public IIterator TokensOf(StoreView snapshot, UInt160 owner)
 {
     if (owner is null)
     {
         var results = snapshot.Storages.Find(new[] { Prefix_Token }).GetEnumerator();
         return(new StorageIterator(results, FindOptions.ValuesOnly | FindOptions.DeserializeValues | FindOptions.PickField1, null));
     }
     else
     {
         NFTAccountState        account = snapshot.Storages.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()));
     }
 }
Пример #5
0
        private protected void Burn(ApplicationEngine engine, StorageKey key)
        {
            TokenState token = engine.Snapshot.TryGet(key)?.GetInteroperable <TokenState>();

            if (token is null)
            {
                throw new InvalidOperationException();
            }
            engine.Snapshot.Delete(key);
            StorageKey      key_account = CreateStorageKey(Prefix_Account).Add(token.Owner);
            NFTAccountState account     = engine.Snapshot.GetAndChange(key_account).GetInteroperable <NFTAccountState>();

            account.Remove(token.Id);
            if (account.Balance.IsZero)
            {
                engine.Snapshot.Delete(key_account);
            }
            engine.Snapshot.GetAndChange(CreateStorageKey(Prefix_TotalSupply)).Add(-1);
            PostTransfer(engine, token.Owner, null, token.Id);
        }