124 lines
3.8 KiB
C#
Raw Normal View History

using BlockchainUtils;
using CodexContractsPlugin.Marketplace;
2023-12-20 10:55:29 +01:00
using GethPlugin;
2023-09-19 13:39:24 +02:00
using Logging;
2023-12-20 10:55:29 +01:00
using Nethereum.Hex.HexConvertors.Extensions;
2023-09-19 13:39:24 +02:00
using System.Numerics;
2025-01-16 13:24:57 +01:00
using Utils;
2023-09-19 13:39:24 +02:00
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);
2024-11-21 15:10:53 +01:00
var function = new TokenFunctionBase();
return gethNode.Call<TokenFunctionBase, string>(marketplaceAddress, function);
2023-09-19 13:39:24 +02:00
}
public string GetTokenName(string tokenAddress)
{
try
{
log.Debug(tokenAddress);
2025-07-03 11:03:13 +02:00
var function = new NameFunction();
2025-07-03 11:03:13 +02:00
return gethNode.Call<NameFunction, 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 13:39:24 +02:00
{
log.Debug($"{amount} -> {address} (token: {tokenAddress})");
2023-12-15 11:02:06 +01:00
return MintTokens(address.Address, amount, tokenAddress);
2023-09-19 13:39:24 +02:00
}
public decimal GetBalance(string tokenAddress, string account)
{
log.Debug($"({tokenAddress}) {account}");
2025-07-03 11:03:13 +02:00
var function = new BalanceOfFunction
2023-09-19 13:39:24 +02:00
{
2025-07-03 11:03:13 +02:00
Account = account
2023-09-19 13:39:24 +02:00
};
2025-07-03 11:03:13 +02:00
return gethNode.Call<BalanceOfFunction, BigInteger>(tokenAddress, function).ToDecimal();
}
public string TransferTestTokens(string tokenAddress, string toAccount, BigInteger amount)
2025-07-03 11:03:13 +02:00
{
log.Debug($"({tokenAddress}) {toAccount} {amount}");
var function = new TransferFunction
{
To = toAccount,
Value = amount
};
return gethNode.SendTransaction(tokenAddress, function);
2023-09-19 13:39:24 +02:00
}
2023-12-20 10:55:29 +01: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 13:58:45 +02:00
public bool IsSynced(string marketplaceAddress, string marketplaceAbi)
{
log.Debug();
2023-09-19 13:58:45 +02:00
try
{
return IsBlockNumberOK() && IsContractAvailable(marketplaceAddress, marketplaceAbi);
}
catch
{
return false;
}
}
private string MintTokens(string account, BigInteger amount, string tokenAddress)
2023-09-19 13:39:24 +02:00
{
log.Debug($"({tokenAddress}) {amount} --> {account}");
if (string.IsNullOrEmpty(account)) throw new ArgumentException("Invalid arguments for MintTestTokens");
2025-07-03 11:03:13 +02:00
var function = new MintFunction
2023-09-19 13:39:24 +02:00
{
Holder = account,
Amount = amount
2023-09-19 13:39:24 +02:00
};
2023-12-15 11:02:06 +01:00
return gethNode.SendTransaction(tokenAddress, function);
2023-09-19 13:39:24 +02:00
}
2023-09-19 13:58:45 +02: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 13:39:24 +02:00
}
}