cs-codex-dist-tests/ProjectPlugins/CodexContractsPlugin/ContractInteractions.cs

141 lines
4.1 KiB
C#
Raw Permalink Normal View History

2023-12-20 09:55:29 +00:00
using CodexContractsPlugin.Marketplace;
using GethPlugin;
2023-09-19 11:39:24 +00:00
using Logging;
using Nethereum.ABI.FunctionEncoding.Attributes;
using Nethereum.Contracts;
2023-12-20 09:55:29 +00:00
using Nethereum.Hex.HexConvertors.Extensions;
2023-09-19 11:39:24 +00:00
using NethereumWorkflow;
using System.Numerics;
namespace CodexContractsPlugin
{
public class ContractInteractions
{
private readonly ILog log;
private readonly IGethNode gethNode;
public ContractInteractions(ILog log, IGethNode gethNode)
{
this.log = log;
this.gethNode = gethNode;
}
public string GetTokenAddress(string marketplaceAddress)
{
log.Debug(marketplaceAddress);
var function = new GetTokenFunction();
return gethNode.Call<GetTokenFunction, string>(marketplaceAddress, function);
}
public string GetTokenName(string tokenAddress)
{
try
{
log.Debug(tokenAddress);
var function = new GetTokenNameFunction();
return gethNode.Call<GetTokenNameFunction, string>(tokenAddress, function);
}
catch (Exception ex)
{
log.Log("Failed to get token name: " + ex);
return string.Empty;
}
}
public string MintTestTokens(EthAddress address, BigInteger amount, string tokenAddress)
2023-09-19 11:39:24 +00:00
{
log.Debug($"{amount} -> {address} (token: {tokenAddress})");
2023-12-15 10:02:06 +00:00
return MintTokens(address.Address, amount, tokenAddress);
2023-09-19 11:39:24 +00:00
}
public decimal GetBalance(string tokenAddress, string account)
{
log.Debug($"({tokenAddress}) {account}");
var function = new GetTokenBalanceFunction
{
Owner = account
};
return gethNode.Call<GetTokenBalanceFunction, BigInteger>(tokenAddress, function).ToDecimal();
}
2023-12-20 09:55:29 +00:00
public GetRequestOutputDTO GetRequest(string marketplaceAddress, byte[] requestId)
{
log.Debug($"({marketplaceAddress}) {requestId.ToHex(true)}");
var func = new GetRequestFunction
{
RequestId = requestId
};
return gethNode.Call<GetRequestFunction, GetRequestOutputDTO>(marketplaceAddress, func);
}
2023-09-19 11:58:45 +00:00
public bool IsSynced(string marketplaceAddress, string marketplaceAbi)
{
log.Debug();
2023-09-19 11:58:45 +00:00
try
{
return IsBlockNumberOK() && IsContractAvailable(marketplaceAddress, marketplaceAbi);
}
catch
{
return false;
}
}
private string MintTokens(string account, BigInteger amount, string tokenAddress)
2023-09-19 11:39:24 +00:00
{
log.Debug($"({tokenAddress}) {amount} --> {account}");
if (string.IsNullOrEmpty(account)) throw new ArgumentException("Invalid arguments for MintTestTokens");
var function = new MintTokensFunction
{
Holder = account,
Amount = amount
2023-09-19 11:39:24 +00:00
};
2023-12-15 10:02:06 +00:00
return gethNode.SendTransaction(tokenAddress, function);
2023-09-19 11:39:24 +00:00
}
2023-09-19 11:58:45 +00:00
private bool IsBlockNumberOK()
{
var n = gethNode.GetSyncedBlockNumber();
return n != null && n > 256;
}
private bool IsContractAvailable(string marketplaceAddress, string marketplaceAbi)
{
return gethNode.IsContractAvailable(marketplaceAbi, marketplaceAddress);
}
2023-09-19 11:39:24 +00:00
}
[Function("token", "address")]
public class GetTokenFunction : FunctionMessage
{
}
[Function("name", "string")]
public class GetTokenNameFunction : FunctionMessage
{
}
2023-09-19 11:39:24 +00:00
[Function("mint")]
public class MintTokensFunction : FunctionMessage
{
[Parameter("address", "holder", 1)]
public string Holder { get; set; } = string.Empty;
[Parameter("uint256", "amount", 2)]
public BigInteger Amount { get; set; }
}
[Function("balanceOf", "uint256")]
public class GetTokenBalanceFunction : FunctionMessage
{
[Parameter("address", "owner", 1)]
public string Owner { get; set; } = string.Empty;
}
}