Skip to content

Commit

Permalink
ABIInfo InMemory Storage and SignatureValuesEquality extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
juanfranblanco committed Aug 12, 2023
1 parent 657390b commit 04e9494
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 0 deletions.
114 changes: 114 additions & 0 deletions src/Nethereum.ABI/ABIRepository/ABIInfoInMemoryStorage.cs
@@ -0,0 +1,114 @@
using Nethereum.ABI.Model;
using Nethereum.Hex.HexConvertors.Extensions;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;

namespace Nethereum.ABI.ABIRepository
{
public class ABIInfoInMemoryStorage
{
private List<ABIInfo> _abiInfos = new List<ABIInfo>();
private IDictionary<string, List<FunctionABI>> _signatureToFunctionABIDictionary { get; set; } = new Dictionary<string, List<FunctionABI>>();
private IDictionary<string, List<ErrorABI>> _signatureToErrorABIDictionary { get; set; } = new Dictionary<string, List<ErrorABI>>();
private IDictionary<string, List<EventABI>> _signatureToEventABIDictionary { get; set; } = new Dictionary<string, List<EventABI>>();

public ABIInfo GetABIInfo(BigInteger chainId, string contractAddress)
{
contractAddress = contractAddress.ToLowerInvariant();
return _abiInfos.FirstOrDefault(x => x.Address == contractAddress && chainId == x.ChainId);
}

public FunctionABI FindFunctionABI(BigInteger chainId, string contractAddress, string signature)
{
var abiInfo = GetABIInfo(chainId, contractAddress);
if(abiInfo == null) return null;
return abiInfo.ContractABI.FindFunctionABI(signature);
}

public ErrorABI FindErrorABI(BigInteger chainId, string contractAddress, string signature)
{
var abiInfo = GetABIInfo(chainId, contractAddress);
if (abiInfo == null) return null;
return abiInfo.ContractABI.FindErrorABI(signature);
}

public EventABI FindEventABI(BigInteger chainId, string contractAddress, string signature)
{
var abiInfo = GetABIInfo(chainId, contractAddress);
if (abiInfo == null) return null;
return abiInfo.ContractABI.FindEventABI(signature);
}

public List<FunctionABI> FindFunctionABI(string signature)
{
return _signatureToFunctionABIDictionary[signature];
}

public List<ErrorABI> FindErrorABI(string signature)
{
return _signatureToErrorABIDictionary[signature];
}

public List<EventABI> FindEventABI(string signature)
{
return _signatureToEventABIDictionary[signature];
}

public void AddABIInfo(ABIInfo abiInfo)
{
_abiInfos.Add(abiInfo);
abiInfo.InitialiseContractABI();

foreach(var functionABI in abiInfo.ContractABI.Functions)
{
if (_signatureToFunctionABIDictionary.ContainsKey(functionABI.Sha3Signature))
{
var functionABIs = _signatureToFunctionABIDictionary[functionABI.Sha3Signature];
if(!functionABIs.Any(x => x.HasTheSameSignatureValues(functionABI)))
{
functionABIs.Add(functionABI);
}
}
else
{
this._signatureToFunctionABIDictionary.Add(functionABI.Sha3Signature, new List<FunctionABI>{functionABI});
}

}

foreach (var errorABI in abiInfo.ContractABI.Errors)
{
if (_signatureToErrorABIDictionary.ContainsKey(errorABI.Sha3Signature))
{
var errorABIs = _signatureToErrorABIDictionary[errorABI.Sha3Signature];
if (!errorABIs.Any(x => x.HasTheSameSignatureValues(errorABI)))
{
errorABIs.Add(errorABI);
}
}
else
{
this._signatureToErrorABIDictionary.Add(errorABI.Sha3Signature, new List<ErrorABI> { errorABI });
}

}

foreach (var eventABI in abiInfo.ContractABI.Events)
{
if (_signatureToEventABIDictionary.ContainsKey(eventABI.Sha3Signature))
{
var eventABIs = _signatureToEventABIDictionary[eventABI.Sha3Signature];
if (!eventABIs.Any(x => x.HasTheSameSignatureValues(eventABI)))
{
eventABIs.Add(eventABI);
}
}
else
{
this._signatureToEventABIDictionary.Add(eventABI.Sha3Signature, new List<EventABI> { eventABI });
}
}
}
}
}
46 changes: 46 additions & 0 deletions src/Nethereum.ABI/Model/ModelExtensions.cs
Expand Up @@ -2,6 +2,7 @@
using Nethereum.Hex.HexConvertors.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Nethereum.ABI.Model
Expand All @@ -20,6 +21,51 @@ public static FunctionABI FindFunctionABI(this ContractABI contractABI, string s
return null;
}

public static bool HasTheSameSignatureValues(this FunctionABI first, FunctionABI other)
{
if (first.Sha3Signature.ToLowerInvariant() != other.Sha3Signature.ToLowerInvariant()) return false;
if(first.Name != other.Name) return false;
if(!first.InputParameters.AreTheSameSignatureValues(other.InputParameters)) return false;
if (!first.OutputParameters.AreTheSameSignatureValues(other.OutputParameters)) return false;
return true;
}

public static bool HasTheSameSignatureValues(this EventABI first, EventABI other)
{
if (first.Sha3Signature.ToLowerInvariant() != other.Sha3Signature.ToLowerInvariant()) return false;
if (first.Name != other.Name) return false;
if (!first.InputParameters.AreTheSameSignatureValues(other.InputParameters)) return false;
return true;
}

public static bool HasTheSameSignatureValues(this ErrorABI first, ErrorABI other)
{
if (first.Sha3Signature.ToLowerInvariant() != other.Sha3Signature.ToLowerInvariant()) return false;
if (first.Name != other.Name) return false;
if (!first.InputParameters.AreTheSameSignatureValues(other.InputParameters)) return false;
return true;
}

public static bool AreTheSameSignatureValues(this IEnumerable<Parameter> first, IEnumerable<Parameter> other)
{
if (first.Count() != other.Count()) return false;
foreach (var parameter in first)
{
var otherParameter = other.FirstOrDefault(x => x.Name == parameter.Name);
if(otherParameter == null) return false;
if(!parameter.HasTheSameSignatureValues(otherParameter)) return false;
}
return true;
}

public static bool HasTheSameSignatureValues(this Parameter parameter, Parameter other)
{
if (parameter.Order != other.Order) return false;
if (parameter.ABIType != other.ABIType) return false;
if (parameter.Indexed != other.Indexed) return false;
return true;
}

public static FunctionABI FindFunctionABIFromInputData(this ContractABI contractABI, string inputData)
{
if (string.IsNullOrEmpty(inputData)) return null;
Expand Down

0 comments on commit 04e9494

Please sign in to comment.