From a1725bab04edb975bb68d4691a4144ae397d3697 Mon Sep 17 00:00:00 2001 From: Erik Zhang Date: Tue, 5 Jan 2021 11:22:38 +0800 Subject: [PATCH] Add GetKey() in NonfungibleToken --- src/neo/SmartContract/Native/NonfungibleToken.cs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/neo/SmartContract/Native/NonfungibleToken.cs b/src/neo/SmartContract/Native/NonfungibleToken.cs index 45969da8b5..4ad9799269 100644 --- a/src/neo/SmartContract/Native/NonfungibleToken.cs +++ b/src/neo/SmartContract/Native/NonfungibleToken.cs @@ -62,9 +62,11 @@ protected NonfungibleToken() Manifest.Abi.Events = events.ToArray(); } + protected virtual byte[] GetKey(byte[] tokenId) => tokenId; + protected void Mint(ApplicationEngine engine, TokenState token) { - engine.Snapshot.Storages.Add(CreateStorageKey(Prefix_Token).Add(token.Id), new StorageItem(token)); + engine.Snapshot.Storages.Add(CreateStorageKey(Prefix_Token).Add(GetKey(token.Id)), new StorageItem(token)); NFTAccountState account = engine.Snapshot.Storages.GetAndChange(CreateStorageKey(Prefix_Account).Add(token.Owner), () => new StorageItem(new NFTAccountState())).GetInteroperable(); account.Add(token.Id); engine.Snapshot.Storages.GetAndChange(CreateStorageKey(Prefix_TotalSupply), () => new StorageItem(BigInteger.Zero)).Add(1); @@ -73,7 +75,7 @@ protected void Mint(ApplicationEngine engine, TokenState token) protected void Burn(ApplicationEngine engine, byte[] tokenId) { - StorageKey key_token = CreateStorageKey(Prefix_Token).Add(tokenId); + StorageKey key_token = CreateStorageKey(Prefix_Token).Add(GetKey(tokenId)); TokenState token = engine.Snapshot.Storages.TryGet(key_token)?.GetInteroperable(); if (token is null) throw new InvalidOperationException(); engine.Snapshot.Storages.Delete(key_token); @@ -96,13 +98,13 @@ public BigInteger TotalSupply(StoreView snapshot) [ContractMethod(0_01000000, CallFlags.ReadStates)] public UInt160 OwnerOf(StoreView snapshot, byte[] tokenId) { - return snapshot.Storages[CreateStorageKey(Prefix_Token).Add(tokenId)].GetInteroperable().Owner; + return snapshot.Storages[CreateStorageKey(Prefix_Token).Add(GetKey(tokenId))].GetInteroperable().Owner; } [ContractMethod(0_01000000, CallFlags.ReadStates)] public JObject Properties(StoreView snapshot, byte[] tokenId) { - return snapshot.Storages[CreateStorageKey(Prefix_Token).Add(tokenId)].GetInteroperable().ToJson(); + return snapshot.Storages[CreateStorageKey(Prefix_Token).Add(GetKey(tokenId))].GetInteroperable().ToJson(); } [ContractMethod(0_01000000, CallFlags.ReadStates)] @@ -132,7 +134,7 @@ public IIterator TokensOf(StoreView snapshot, UInt160 owner) protected bool Transfer(ApplicationEngine engine, UInt160 to, byte[] tokenId) { if (to is null) throw new ArgumentNullException(nameof(to)); - TokenState token = engine.Snapshot.Storages.GetAndChange(CreateStorageKey(Prefix_Token).Add(tokenId))?.GetInteroperable(); + TokenState token = engine.Snapshot.Storages.GetAndChange(CreateStorageKey(Prefix_Token).Add(GetKey(tokenId)))?.GetInteroperable(); if (token is null) throw new ArgumentException(); UInt160 from = token.Owner; if (!from.Equals(engine.CallingScriptHash) && !engine.CheckWitnessInternal(from))