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

Added validator class for NEP-17 contract #311

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
30 changes: 7 additions & 23 deletions src/neoxp/Node/OfflineNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
using Neo.SmartContract;
using Neo.SmartContract.Manifest;
using Neo.SmartContract.Native;
using Neo.VM;
using Neo.Wallets;
using NeoExpress.Commands;
using NeoExpress.Models;
using NeoExpress.Validators.Tokens;
using System.Numerics;
using static Neo.Ledger.Blockchain;

Expand Down Expand Up @@ -295,30 +295,14 @@ public Task<uint> GetTransactionHeightAsync(UInt256 txHash)
.Where(c => c.standard == TokenStandard.Nep17)
.ToList();

var addressArray = address.ToArray();
using var builder = new ScriptBuilder();
for (var i = contracts.Count; i-- > 0;)
{
builder.EmitDynamicCall(contracts[i].scriptHash, "symbol");
builder.EmitDynamicCall(contracts[i].scriptHash, "decimals");
builder.EmitDynamicCall(contracts[i].scriptHash, "balanceOf", addressArray);
}

List<(TokenContract contract, BigInteger balance)> balances = new();
using var engine = builder.Invoke(neoSystem.Settings, snapshot);
if (engine.State != VMState.FAULT && engine.ResultStack.Count == contracts.Count * 3)
for (int i = contracts.Count; i-- > 0;)
{
var resultStack = engine.ResultStack;
for (var i = 0; i < contracts.Count; i++)
{
var index = i * 3;
var symbol = resultStack.Peek(index + 2).GetString();
if (symbol is null)
continue;
var decimals = (byte)resultStack.Peek(index + 1).GetInteger();
var balance = resultStack.Peek(index).GetInteger();
balances.Add((new TokenContract(symbol, decimals, contracts[i].scriptHash, contracts[i].standard), balance));
}
// Note: this will throw error NotSupportedException if NEP-17 is not compliant.
// A check maybe added for exception in future, if crashes because SupportStandard
// invalid.
var nep17Contract = new Nep17Token(ProtocolSettings, snapshot, contracts[i].scriptHash);
balances.Add((new TokenContract(nep17Contract.Symbol, nep17Contract.Decimals, contracts[i].scriptHash, contracts[i].standard), nep17Contract.BalanceOf(address)));
}

return balances;
Expand Down
24 changes: 24 additions & 0 deletions src/neoxp/Validators/Tokens/Nep17Token.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (C) 2015-2023 The Neo Project.
//
// The neo is free software distributed under the MIT software license,
// see the accompanying file LICENSE in the main directory of the
// project 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;
using Neo.Persistence;

namespace NeoExpress.Validators.Tokens;

internal class Nep17Token : TokenBase
{
public Nep17Token(
ProtocolSettings protocolSettings,
DataCache snapshot,
UInt160 scriptHash) : base(protocolSettings, snapshot, scriptHash)
{
}
}
71 changes: 71 additions & 0 deletions src/neoxp/Validators/Tokens/TokenBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright (C) 2015-2023 The Neo Project.
//
// The neo is free software distributed under the MIT software license,
// see the accompanying file LICENSE in the main directory of the
// project 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;
using Neo.Persistence;
using Neo.SmartContract;
using Neo.VM;
using System.Numerics;

namespace NeoExpress.Validators.Tokens;

internal abstract class TokenBase
{
public UInt160 ScriptHash { get; private init; }
public string Symbol { get; private set; }
public byte Decimals { get; private set; }

protected readonly DataCache _snapshot;
protected readonly ProtocolSettings _protocolSettings;

public TokenBase(
ProtocolSettings protocolSettings,
DataCache snapshot,
UInt160 scriptHash)
{
_protocolSettings = protocolSettings;
_snapshot = snapshot;
ScriptHash = scriptHash;

using var builder = new ScriptBuilder();
builder.EmitDynamicCall(ScriptHash, "decimals");
builder.EmitDynamicCall(ScriptHash, "symbol");

using var engine = builder.Invoke(_protocolSettings, _snapshot);
if (engine.State != VMState.HALT)
throw new NotSupportedException($"{ScriptHash} is not NEP-17 compliant.");
cschuchardt88 marked this conversation as resolved.
Show resolved Hide resolved

var results = engine.ResultStack;
Symbol = results.Pop().GetString()!;
Decimals = checked((byte)results.Pop().GetInteger());
}

public BigInteger TotalSupply()
{
using var builder = new ScriptBuilder();
builder.EmitDynamicCall(ScriptHash, "totalSupply");

using var appEng = builder.Invoke(_protocolSettings, _snapshot);
if (appEng.State == VMState.HALT)
return appEng.ResultStack.Pop().GetInteger();
throw new InvalidOperationException();
}

public BigInteger BalanceOf(UInt160 owner)
{
using var builder = new ScriptBuilder();
builder.EmitDynamicCall(ScriptHash, "balanceOf", owner);

using var appEng = builder.Invoke(_protocolSettings, _snapshot);
if (appEng.State == VMState.HALT)
return appEng.ResultStack.Pop().GetInteger();
throw new InvalidOperationException();
}
}