Gmx v2 - Funding rates (#6)
* Setup GMX v2 * Add get markets * Map token with service * Add get market info data * Add get markets * Add get market token prices * Get markets infos multicall * Try call datastore * Add some tests to figure out why datastore call dont work * Update funding rates * clean
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
using System.Numerics;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using Nethereum.ABI;
|
||||
using Nethereum.Util;
|
||||
|
||||
namespace Managing.Infrastructure.Evm.Models.Gmx.v2;
|
||||
|
||||
public class GmxV2Helpers
|
||||
{
|
||||
private readonly MemoryCache _dataCache = new MemoryCache(new MemoryCacheOptions { SizeLimit = 10000 });
|
||||
private readonly MemoryCache _stringCache = new MemoryCache(new MemoryCacheOptions { SizeLimit = 10000 });
|
||||
private readonly ABIEncode _abiEncode = new ABIEncode();
|
||||
private readonly MemoryCacheEntryOptions _absoluteExpiration = new MemoryCacheEntryOptions { Size = 1 };
|
||||
|
||||
|
||||
public byte[] HashData(string[] dataTypes, object[] dataValues)
|
||||
{
|
||||
var key = GenerateKey(dataTypes, dataValues);
|
||||
|
||||
if (_dataCache.TryGetValue(key, out byte[] cachedHash))
|
||||
{
|
||||
return cachedHash;
|
||||
}
|
||||
|
||||
var abiValues = new List<ABIValue>();
|
||||
foreach (var dataType in dataTypes)
|
||||
{
|
||||
var abiValue = new ABIValue(dataType, dataValues[dataTypes.ToList().IndexOf(dataType)]);
|
||||
abiValues.Add(abiValue);
|
||||
}
|
||||
|
||||
var abiEncode = new ABIEncode();
|
||||
var encoded = abiEncode.GetABIEncoded(abiValues.ToArray());
|
||||
var hash = new Sha3Keccack().CalculateHash(encoded);
|
||||
_dataCache.Set(key, hash, _absoluteExpiration);
|
||||
|
||||
return hash;
|
||||
}
|
||||
|
||||
|
||||
public byte[] HashString(string input)
|
||||
{
|
||||
if (_stringCache.TryGetValue(input, out byte[] cachedHash))
|
||||
{
|
||||
return cachedHash;
|
||||
}
|
||||
|
||||
// Adjusted to directly work with byte arrays
|
||||
var hash = HashData(new[] { "string" }, new string[] { input });
|
||||
_stringCache.Set(input, hash, _absoluteExpiration);
|
||||
|
||||
return hash;
|
||||
}
|
||||
|
||||
private string GenerateKey(string[] dataTypes, object[] dataValues)
|
||||
{
|
||||
var combined = new List<string>();
|
||||
for (int i = 0; i < dataTypes.Length; i++)
|
||||
{
|
||||
combined.Add($"{dataTypes[i]}:{(dataValues[i] is BigInteger bigInt ? bigInt.ToString() : dataValues[i])}");
|
||||
}
|
||||
|
||||
return string.Join(",", combined);
|
||||
}
|
||||
|
||||
public static bool SameAddress(string address, string address2)
|
||||
{
|
||||
return address.ToLower() == address2.ToLower();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user