Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GetKey() in NonfungibleToken #2199

Merged
merged 2 commits into from
Jan 5, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/neo/SmartContract/Native/NonfungibleToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<NFTAccountState>();
account.Add(token.Id);
engine.Snapshot.Storages.GetAndChange(CreateStorageKey(Prefix_TotalSupply), () => new StorageItem(BigInteger.Zero)).Add(1);
Expand All @@ -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<TokenState>();
if (token is null) throw new InvalidOperationException();
engine.Snapshot.Storages.Delete(key_token);
Expand All @@ -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<TokenState>().Owner;
return snapshot.Storages[CreateStorageKey(Prefix_Token).Add(GetKey(tokenId))].GetInteroperable<TokenState>().Owner;
}

[ContractMethod(0_01000000, CallFlags.ReadStates)]
public JObject Properties(StoreView snapshot, byte[] tokenId)
{
return snapshot.Storages[CreateStorageKey(Prefix_Token).Add(tokenId)].GetInteroperable<TokenState>().ToJson();
return snapshot.Storages[CreateStorageKey(Prefix_Token).Add(GetKey(tokenId))].GetInteroperable<TokenState>().ToJson();
}

[ContractMethod(0_01000000, CallFlags.ReadStates)]
Expand Down Expand Up @@ -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>();
TokenState token = engine.Snapshot.Storages.GetAndChange(CreateStorageKey(Prefix_Token).Add(GetKey(tokenId)))?.GetInteroperable<TokenState>();
if (token is null) throw new ArgumentException();
UInt160 from = token.Owner;
if (!from.Equals(engine.CallingScriptHash) && !engine.CheckWitnessInternal(from))
Expand Down