/ ProjectPlugins / CodexContractsPlugin / ContractInteractions.cs
ContractInteractions.cs
  1  using BlockchainUtils;
  2  using CodexContractsPlugin.Marketplace;
  3  using GethPlugin;
  4  using Logging;
  5  using Nethereum.Hex.HexConvertors.Extensions;
  6  using System.Numerics;
  7  using Utils;
  8  
  9  namespace CodexContractsPlugin
 10  {
 11      public class ContractInteractions
 12      {
 13          private readonly ILog log;
 14          private readonly IGethNode gethNode;
 15  
 16          public ContractInteractions(ILog log, IGethNode gethNode)
 17          {
 18              this.log = log;
 19              this.gethNode = gethNode;
 20          }
 21  
 22          public string GetTokenAddress(string marketplaceAddress)
 23          {
 24              log.Debug(marketplaceAddress);
 25              var function = new TokenFunctionBase();
 26              return gethNode.Call<TokenFunctionBase, string>(marketplaceAddress, function);
 27          }
 28  
 29          public string GetTokenName(string tokenAddress)
 30          {
 31              try
 32              {
 33                  log.Debug(tokenAddress);
 34                  var function = new NameFunction();
 35  
 36                  return gethNode.Call<NameFunction, string>(tokenAddress, function);
 37              }
 38              catch (Exception ex)
 39              {
 40                  log.Log("Failed to get token name: " + ex);
 41                  return string.Empty;
 42              }
 43          }
 44  
 45          public string MintTestTokens(EthAddress address, BigInteger amount, string tokenAddress)
 46          {
 47              log.Debug($"{amount} -> {address} (token: {tokenAddress})");
 48              return MintTokens(address.Address, amount, tokenAddress);
 49          }
 50  
 51          public decimal GetBalance(string tokenAddress, string account)
 52          {
 53              log.Debug($"({tokenAddress}) {account}");
 54              var function = new BalanceOfFunction
 55              {
 56                  Account = account
 57              };
 58  
 59              return gethNode.Call<BalanceOfFunction, BigInteger>(tokenAddress, function).ToDecimal();
 60          }
 61  
 62          public string TransferTestTokens(string tokenAddress, string toAccount, BigInteger amount)
 63          {
 64              log.Debug($"({tokenAddress}) {toAccount} {amount}");
 65              var function = new TransferFunction
 66              {
 67                  To = toAccount,
 68                  Value = amount
 69              };
 70  
 71              return gethNode.SendTransaction(tokenAddress, function);
 72          }
 73  
 74          public GetRequestOutputDTO GetRequest(string marketplaceAddress, byte[] requestId)
 75          {
 76  
 77              log.Debug($"({marketplaceAddress}) {requestId.ToHex(true)}");
 78              var func = new GetRequestFunction
 79              {
 80                  RequestId = requestId
 81              };
 82              return gethNode.Call<GetRequestFunction, GetRequestOutputDTO>(marketplaceAddress, func);
 83          }
 84  
 85          public bool IsSynced(string marketplaceAddress, string marketplaceAbi)
 86          {
 87              log.Debug();
 88              try
 89              {
 90                  return IsBlockNumberOK() && IsContractAvailable(marketplaceAddress, marketplaceAbi);
 91              }
 92              catch
 93              {
 94                  return false;
 95              }
 96          }
 97  
 98          private string MintTokens(string account, BigInteger amount, string tokenAddress)
 99          {
100              log.Debug($"({tokenAddress}) {amount} --> {account}");
101              if (string.IsNullOrEmpty(account)) throw new ArgumentException("Invalid arguments for MintTestTokens");
102  
103              var function = new MintFunction
104              {
105                  Holder = account,
106                  Amount = amount
107              };
108  
109              return gethNode.SendTransaction(tokenAddress, function);
110          }
111  
112          private bool IsBlockNumberOK()
113          {
114              var n = gethNode.GetSyncedBlockNumber();
115              return n != null && n > 256;
116          }
117  
118          private bool IsContractAvailable(string marketplaceAddress, string marketplaceAbi)
119          {
120              return gethNode.IsContractAvailable(marketplaceAbi, marketplaceAddress);
121          }
122      }
123  }