Skip to content

Commit

Permalink
Check nep24 compliant (#1059)
Browse files Browse the repository at this point in the history
* Update ContractManifestExtension.cs

* Update ContractManifestExtension.cs

* Update ContractManifestExtension.cs

* Update src/Neo.Compiler.CSharp/ContractManifestExtension.cs

* Update src/Neo.Compiler.CSharp/ContractManifestExtension.cs

Co-authored-by: Shargon <shargon@gmail.com>

* Update ContractManifestExtension.cs

* avoid == true

* Update src/Neo.Compiler.CSharp/ContractManifestExtension.cs

---------

Co-authored-by: Shargon <shargon@gmail.com>
Co-authored-by: Jimmy <jinghui@wayne.edu>
  • Loading branch information
3 people committed Jun 7, 2024
1 parent 5334d70 commit f029a65
Showing 1 changed file with 50 additions and 7 deletions.
57 changes: 50 additions & 7 deletions src/Neo.Compiler.CSharp/ContractManifestExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,35 +38,42 @@ private static void CheckNep11Compliant(this ContractManifest manifest)
var transferMethod1 = manifest.Abi.GetMethod("transfer", 3);
var transferMethod2 = manifest.Abi.GetMethod("transfer", 5);

var symbolValid = symbolMethod != null && symbolMethod.Safe == true &&
var symbolValid = symbolMethod != null && symbolMethod.Safe &&
symbolMethod.ReturnType == ContractParameterType.String;
var decimalsValid = decimalsMethod != null && decimalsMethod.Safe == true &&
var decimalsValid = decimalsMethod != null && decimalsMethod.Safe &&
decimalsMethod.ReturnType == ContractParameterType.Integer;
var totalSupplyValid = totalSupplyMethod != null && totalSupplyMethod.Safe == true &&
var totalSupplyValid = totalSupplyMethod != null && totalSupplyMethod.Safe &&
totalSupplyMethod.ReturnType == ContractParameterType.Integer;
var balanceOfValid1 = balanceOfMethod1 != null && balanceOfMethod1.Safe == true &&
var balanceOfValid1 = balanceOfMethod1 != null && balanceOfMethod1.Safe &&
balanceOfMethod1.ReturnType == ContractParameterType.Integer &&
balanceOfMethod1.Parameters.Length == 1 &&
balanceOfMethod1.Parameters[0].Type == ContractParameterType.Hash160;
var balanceOfValid2 = balanceOfMethod2?.Safe == true &&
balanceOfMethod2?.ReturnType == ContractParameterType.Integer &&
balanceOfMethod2?.Parameters.Length == 2 &&
balanceOfMethod2?.Parameters[0].Type == ContractParameterType.Hash160 &&
balanceOfMethod2?.Parameters[0].Type == ContractParameterType.ByteArray;
var tokensOfValid = tokensOfMethod != null && tokensOfMethod.Safe == true &&
var tokensOfValid = tokensOfMethod != null && tokensOfMethod.Safe &&
tokensOfMethod.ReturnType == ContractParameterType.InteropInterface &&
tokensOfMethod.Parameters.Length == 1 &&
tokensOfMethod.Parameters[0].Type == ContractParameterType.Hash160;
var ownerOfValid1 = ownerOfMethod != null && ownerOfMethod.Safe == true &&
var ownerOfValid1 = ownerOfMethod != null && ownerOfMethod.Safe &&
ownerOfMethod.ReturnType == ContractParameterType.Hash160 &&
ownerOfMethod.Parameters.Length == 1 &&
ownerOfMethod.Parameters[0].Type == ContractParameterType.ByteArray;
var ownerOfValid2 = ownerOfMethod != null && ownerOfMethod.Safe == true &&
var ownerOfValid2 = ownerOfMethod != null && ownerOfMethod.Safe &&
ownerOfMethod.ReturnType == ContractParameterType.InteropInterface &&
ownerOfMethod.Parameters.Length == 1 &&
ownerOfMethod.Parameters[0].Type == ContractParameterType.ByteArray;
var transferValid1 = transferMethod1 != null && transferMethod1.Safe == false &&
transferMethod1.ReturnType == ContractParameterType.Boolean &&
transferMethod1.Parameters.Length == 3 &&
transferMethod1.Parameters[0].Type == ContractParameterType.Hash160 &&
transferMethod1.Parameters[1].Type == ContractParameterType.ByteArray &&
transferMethod1.Parameters[2].Type == ContractParameterType.Any;
var transferValid2 = transferMethod2?.Safe == false &&
transferMethod2?.ReturnType == ContractParameterType.Boolean &&
transferMethod2.Parameters.Length == 5 &&
transferMethod2?.Parameters[0].Type == ContractParameterType.Hash160 &&
transferMethod2?.Parameters[1].Type == ContractParameterType.Hash160 &&
transferMethod2?.Parameters[2].Type == ContractParameterType.Integer &&
Expand Down Expand Up @@ -114,6 +121,33 @@ private static void CheckNep11Compliant(this ContractManifest manifest)
}
}

private static void CheckNep24Compliant(this ContractManifest manifest)
{
try
{
var royaltyInfoMethod = manifest.Abi.GetMethod("royaltyInfo", 0);

var royaltyInfoValid = royaltyInfoMethod != null && royaltyInfoMethod.Safe &&
royaltyInfoMethod.ReturnType == ContractParameterType.Array &&
royaltyInfoMethod.Parameters.Length == 3 &&
royaltyInfoMethod.Parameters[0].Type == ContractParameterType.ByteArray &&
royaltyInfoMethod.Parameters[1].Type == ContractParameterType.Hash160 &&
royaltyInfoMethod.Parameters[2].Type == ContractParameterType.Integer;

if (!royaltyInfoValid) throw new CompilationException(DiagnosticId.IncorrectNEPStandard,
$"Incomplete NEP standard {NepStandard.Nep24.ToStandard()} implementation: royaltyInfo");
}
catch (Exception ex) when (ex is not CompilationException)
{
throw;
}
catch
{
throw new CompilationException(DiagnosticId.IncorrectNEPStandard,
$"Incomplete NEP standard {NepStandard.Nep24.ToStandard()} implementation: Unidentified issue.");
}
}

private static void CheckNep17Compliant(this ContractManifest manifest)
{
try
Expand All @@ -132,9 +166,11 @@ private static void CheckNep17Compliant(this ContractManifest manifest)
totalSupplyMethod.ReturnType == ContractParameterType.Integer;
var balanceOfValid = balanceOfMethod != null && balanceOfMethod.Safe &&
balanceOfMethod.ReturnType == ContractParameterType.Integer &&
balanceOfMethod.Parameters.Length == 1 &&
balanceOfMethod.Parameters[0].Type == ContractParameterType.Hash160;
var transferValid = transferMethod != null && transferMethod.Safe == false &&
transferMethod.ReturnType == ContractParameterType.Boolean &&
transferMethod.Parameters.Length == 4 &&
transferMethod.Parameters[0].Type == ContractParameterType.Hash160 &&
transferMethod.Parameters[1].Type == ContractParameterType.Hash160 &&
transferMethod.Parameters[2].Type == ContractParameterType.Integer &&
Expand Down Expand Up @@ -169,6 +205,7 @@ private static void CheckNep11PayableCompliant(this ContractManifest manifest)
{
var onNEP11PaymentMethod = manifest.Abi.GetMethod("onNEP11Payment", 4);
var onNEP11PaymentValid = onNEP11PaymentMethod is { ReturnType: ContractParameterType.Void } &&
onNEP11PaymentMethod.Parameters.Length == 4 &&
onNEP11PaymentMethod.Parameters[0].Type == ContractParameterType.Hash160 &&
onNEP11PaymentMethod.Parameters[1].Type == ContractParameterType.Integer &&
onNEP11PaymentMethod.Parameters[2].Type == ContractParameterType.String &&
Expand All @@ -189,6 +226,7 @@ private static void CheckNep17PayableCompliant(this ContractManifest manifest)
{
var onNEP17PaymentMethod = manifest.Abi.GetMethod("onNEP17Payment", 3);
var onNEP17PaymentValid = onNEP17PaymentMethod is { ReturnType: ContractParameterType.Void } &&
onNEP17PaymentMethod.Parameters.Length == 3 &&
onNEP17PaymentMethod.Parameters[0].Type == ContractParameterType.Hash160 &&
onNEP17PaymentMethod.Parameters[1].Type == ContractParameterType.Integer &&
onNEP17PaymentMethod.Parameters[2].Type == ContractParameterType.Any;
Expand All @@ -214,6 +252,11 @@ internal static ContractManifest CheckStandards(this ContractManifest manifest)
manifest.CheckNep17Compliant();
}

if (manifest.SupportedStandards.Contains(NepStandard.Nep24.ToStandard()))
{
manifest.CheckNep24Compliant();
}

if (manifest.SupportedStandards.Contains(NepStandard.Nep11Payable.ToStandard()))
{
manifest.CheckNep11PayableCompliant();
Expand Down

0 comments on commit f029a65

Please sign in to comment.