-
Notifications
You must be signed in to change notification settings - Fork 1k
Add GasToken extension GetAccounts for Governance token
#4424
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
Closed
cschuchardt88
wants to merge
4
commits into
neo-project:master
from
cschuchardt88:add/gastrokenextension-back
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
355aae7
Add `GasToken` extension `GetAccounts` for `Governance` token
cschuchardt88 6fa9820
Fixed unit tests
cschuchardt88 497bfdd
Merge branch 'master' into add/gastrokenextension-back
ajara87 f64ee0b
Merge branch 'master' into add/gastrokenextension-back
ajara87 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| // Copyright (C) 2015-2026 The Neo Project. | ||
| // | ||
| // GovernanceExtensions.cs file belongs to the neo project and is free | ||
| // software distributed under the MIT software license, see the | ||
| // accompanying file LICENSE in the main directory of the | ||
| // repository or http://www.opensource.org/licenses/mit-license.php | ||
| // for more details. | ||
| // | ||
| // Redistribution and use in source and binary forms with or without | ||
| // modifications are permitted. | ||
|
|
||
| using Neo.Persistence; | ||
| using Neo.SmartContract; | ||
| using Neo.SmartContract.Native; | ||
| using System.Numerics; | ||
|
|
||
| namespace Neo.Extensions.SmartContract; | ||
|
|
||
| public static class GovernanceExtensions | ||
| { | ||
| public static IEnumerable<(UInt160 Address, BigInteger Balance)> GetAccounts(this Governance gasToken, IReadOnlyStore snapshot) | ||
| { | ||
| ArgumentNullException.ThrowIfNull(gasToken); | ||
|
|
||
| ArgumentNullException.ThrowIfNull(snapshot); | ||
|
|
||
| var kb = new KeyBuilder(TokenManagement.TokenId, TokenManagement.Prefix_AccountState) | ||
| .Add(gasToken.GasTokenId) | ||
| .ToArray(); | ||
| var kbLength = kb.Length; | ||
|
|
||
| foreach (var (key, value) in snapshot.Find(kb, SeekDirection.Forward)) | ||
| { | ||
| var keyBytes = key.ToArray(); | ||
| var accountHash = new UInt160(keyBytes.AsSpan(kb.Length)); | ||
| yield return new(accountHash, value.GetInteroperable<AccountState>().Balance); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
tests/Neo.UnitTests/Extensions/UT_GovernanceTextensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| // Copyright (C) 2015-2026 The Neo Project. | ||
| // | ||
| // UT_GovernanceTextensions.cs file belongs to the neo project and is free | ||
| // software distributed under the MIT software license, see the | ||
| // accompanying file LICENSE in the main directory of the | ||
| // repository or http://www.opensource.org/licenses/mit-license.php | ||
| // for more details. | ||
| // | ||
| // Redistribution and use in source and binary forms with or without | ||
| // modifications are permitted. | ||
|
|
||
| using Neo.Extensions.SmartContract; | ||
| using Neo.SmartContract.Native; | ||
|
|
||
| namespace Neo.UnitTests.Extensions; | ||
|
|
||
| [TestClass] | ||
| public class UT_GovernanceExtensions | ||
| { | ||
| private NeoSystem _system = null!; | ||
|
|
||
| [TestInitialize] | ||
| public void Setup() | ||
| { | ||
| _system = TestBlockchain.GetSystem(); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void TestGetAccounts() | ||
| { | ||
| UInt160 expected = "0x9f8f056a53e39585c7bb52886418c7bed83d126b"; | ||
|
|
||
| var accounts = NativeContract.Governance.GetAccounts(_system.StoreView); | ||
| var (address, balance) = accounts.FirstOrDefault(); | ||
|
|
||
| Assert.AreEqual(expected, address); | ||
| Assert.AreEqual(5200000000000000, balance); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Putting "account" first is a more logical approach. This is because finding all assets in a wallet is a common operation, while finding all accounts for a specific token is a very rare operation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If its done this way than you can't lookup the storage key without knowing an account that has a balance of the asset. So without the account you wont find any assets for a given token. This is basic relational data/database techniques and standard data/database normalization