-
Notifications
You must be signed in to change notification settings - Fork 1k
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] Get Accounts and Balances for NeoToken #3610
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Copyright (C) 2015-2024 The Neo Project. | ||
// | ||
// NeoTokenExtensions.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; | ||
using System.Collections.Generic; | ||
using System.Numerics; | ||
|
||
namespace Neo.Extensions | ||
{ | ||
public static class NeoTokenExtensions | ||
{ | ||
private const byte Prefix_Account = 20; | ||
|
||
public static IEnumerable<(UInt160 Address, BigInteger Balance)> GetAccounts(this NeoToken neoToken, DataCache snapshot) | ||
{ | ||
if (snapshot is null) | ||
throw new ArgumentNullException(nameof(snapshot)); | ||
|
||
var kb = new KeyBuilder(neoToken.Id, Prefix_Account); | ||
var prefixKey = kb.ToArray(); | ||
|
||
foreach (var (key, value) in snapshot.Seek(prefixKey, SeekDirection.Forward)) | ||
{ | ||
var keyBytes = key.ToArray(); | ||
|
||
if (keyBytes.AsSpan().StartsWith(prefixKey)) | ||
{ | ||
var addressHash = new UInt160(keyBytes.AsSpan(prefixKey.Length)); | ||
yield return new(addressHash, value.GetInteroperable<AccountState>().Balance); | ||
} | ||
else | ||
yield break; | ||
} | ||
} | ||
|
||
public static BigInteger GetAccountBalance(this NeoToken neoToken, DataCache snapshot, UInt160 address) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ERC20 extensions, and use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ERC20? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is for offline mode. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
NEP17 sorry |
||
{ | ||
if (snapshot is null) | ||
throw new ArgumentNullException(nameof(snapshot)); | ||
|
||
if (address is null) | ||
throw new ArgumentNullException(nameof(address)); | ||
|
||
var kb = new KeyBuilder(neoToken.Id, Prefix_Account) | ||
.Add(address); | ||
|
||
var prefixKey = kb.ToArray(); | ||
var account = snapshot.TryGet(prefixKey); | ||
return account.GetInteroperable<AccountState>().Balance; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Copyright (C) 2015-2024 The Neo Project. | ||
// | ||
// UT_NeoTokenExtensions.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 Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Neo.Extensions; | ||
using Neo.SmartContract.Native; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Numerics; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Neo.UnitTests.Extensions | ||
{ | ||
[TestClass] | ||
public class UT_NeoTokenExtensions | ||
{ | ||
private NeoSystem system; | ||
|
||
[TestInitialize] | ||
public void Initialize() | ||
{ | ||
system = TestBlockchain.TheNeoSystem; | ||
} | ||
|
||
[TestCleanup] | ||
public void Clean() | ||
{ | ||
TestBlockchain.ResetStore(); | ||
} | ||
|
||
[TestMethod] | ||
public void TestGetAccounts() | ||
{ | ||
UInt160 expected = "0x9f8f056a53e39585c7bb52886418c7bed83d126b"; | ||
|
||
var accounts = NativeContract.NEO.GetAccounts(system.StoreView); | ||
var actual = accounts.FirstOrDefault(); | ||
|
||
Assert.AreEqual(expected, actual.Address); | ||
Assert.AreEqual(100000000, actual.Balance); | ||
} | ||
|
||
[TestMethod] | ||
public void TestGetAccountBalance() | ||
{ | ||
UInt160 account = "0x9f8f056a53e39585c7bb52886418c7bed83d126b"; | ||
var expected = new BigInteger(100000000); | ||
|
||
var actualBalance = NativeContract.NEO.GetAccountBalance(system.StoreView, account); | ||
|
||
Assert.AreEqual(expected, actualBalance); | ||
} | ||
} | ||
} |
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.
Prefix_Account
has been defined inFungibleToken
, Maybe it doesn't need to be defined twice? (changeFungibleToken. Prefix_Account
to aninternal
const value.