Skip to content

Commit

Permalink
[Framework] add interfaces (#952)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jim8y authored Feb 26, 2024
1 parent 6f230f4 commit fd14cb2
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 3 deletions.
8 changes: 7 additions & 1 deletion src/Neo.Compiler.CSharp/CompilationEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,17 @@ private List<CompilationContext> CompileProjectContracts(Compilation compilation
return Contexts.Select(p => p.Value).ToList();
}

/// <summary>
/// Sort the classes based on their topological dependencies
/// </summary>
/// <param name="dependencies">Contract dependencies map</param>
/// <returns>List of sorted contracts</returns>
/// <exception cref="InvalidOperationException"></exception>
private static List<INamedTypeSymbol> TopologicalSort(Dictionary<INamedTypeSymbol, List<INamedTypeSymbol>> dependencies)
{
var sorted = new List<INamedTypeSymbol>();
var visited = new HashSet<INamedTypeSymbol>(SymbolEqualityComparer.Default);
var visiting = new HashSet<INamedTypeSymbol>(SymbolEqualityComparer.Default); // 添加中间状态以检测循环依赖
var visiting = new HashSet<INamedTypeSymbol>(SymbolEqualityComparer.Default); // for detecting cycles

void Visit(INamedTypeSymbol classSymbol)
{
Expand Down
18 changes: 18 additions & 0 deletions src/Neo.SmartContract.Framework/Interfaces/INEP11Payment.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Numerics;

namespace Neo.SmartContract.Framework.Interfaces;

/// <summary>
/// Interface of method that indicate a contract receive NEP-11 Payment
/// </summary>
public interface INep11Payment
{
/// <summary>
/// NonFungibleToken contracts should implement the <see cref="OnNEP11Payment"/> method
/// to receive assets and modify the Manifest file to trust the received asset contract.
/// </summary>
/// <param name="from">The address of the payer</param>
/// <param name="amount">The amount of token to be transferred</param>
/// <param name="data">Additional payment description data</param>
public void OnNEP11Payment(UInt160 from, BigInteger amount, object? data = null);

Check warning on line 17 in src/Neo.SmartContract.Framework/Interfaces/INEP11Payment.cs

View workflow job for this annotation

GitHub Actions / Test

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 17 in src/Neo.SmartContract.Framework/Interfaces/INEP11Payment.cs

View workflow job for this annotation

GitHub Actions / PublishPackage

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
}
18 changes: 18 additions & 0 deletions src/Neo.SmartContract.Framework/Interfaces/INEP17Payment.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Numerics;

namespace Neo.SmartContract.Framework.Interfaces;

/// <summary>
/// Interface of method that indicate a contract receive NEP-17 Payment
/// </summary>
public interface INep17Payment
{
/// <summary>
/// The Token contract should implement the <see cref="OnNEP17Payment"/> method
/// to receive assets and modify the Manifest file to trust the received asset contract.
/// </summary>
/// <param name="from">The address of the payer</param>
/// <param name="amount">The amount of token to be transferred</param>
/// <param name="data">Additional payment description data</param>
public void OnNEP17Payment(UInt160 from, BigInteger amount, object? data = null);

Check warning on line 17 in src/Neo.SmartContract.Framework/Interfaces/INEP17Payment.cs

View workflow job for this annotation

GitHub Actions / Test

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 17 in src/Neo.SmartContract.Framework/Interfaces/INEP17Payment.cs

View workflow job for this annotation

GitHub Actions / PublishPackage

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
using System.Numerics;
using Neo.SmartContract.Framework.Attributes;
using Neo.SmartContract.Framework.Interfaces;

namespace Neo.SmartContract.Framework.UnitTests.TestClasses
{
[SupportedStandards(NEPStandard.NEP11)]
public class Contract_SupportedStandard11Enum : Nep11Token<Nep11TokenState>
public class Contract_SupportedStandard11Enum : Nep11Token<Nep11TokenState>, INep11Payment
{
public static bool TestStandard()
{
return true;
}

public override string Symbol { [Safe] get; }

Check warning on line 15 in tests/Neo.SmartContract.Framework.TestContracts/Contract_SupportedStandard11Enum.cs

View workflow job for this annotation

GitHub Actions / Test

Non-nullable property 'Symbol' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

public void OnNEP11Payment(UInt160 from, BigInteger amount, object? data = null)
{
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Neo.SmartContract.Framework.Attributes;
using System.ComponentModel;
using System.Numerics;
using Neo.SmartContract.Framework.Interfaces;

namespace Neo.SmartContract.Framework.UnitTests.TestClasses
{
Expand All @@ -11,9 +13,13 @@ namespace Neo.SmartContract.Framework.UnitTests.TestClasses
[ContractSourceCode("https://github.com/neo-project/neo-devpack-dotnet/tree/master/src/Neo.SmartContract.Template")]
[ContractPermission("*", "*")]
[SupportedStandards(NEPStandard.NEP17)]
public class Contract_SupportedStandard17Enum : Nep17Token
public class Contract_SupportedStandard17Enum : Nep17Token, INep17Payment
{
public override string Symbol { [Safe] get; }

Check warning on line 18 in tests/Neo.SmartContract.Framework.TestContracts/Contract_SupportedStandard17Enum.cs

View workflow job for this annotation

GitHub Actions / Test

Non-nullable property 'Symbol' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
public override byte Decimals { [Safe] get; }

public void OnNEP17Payment(UInt160 from, BigInteger amount, object? data = null)
{
}
}
}

0 comments on commit fd14cb2

Please sign in to comment.