Successful testtoken mint and balance

This commit is contained in:
benbierens 2023-09-19 13:39:24 +02:00
parent 58b1c1e03c
commit 6cf86af3b5
No known key found for this signature in database
GPG Key ID: FE44815D96D0A1AA
7 changed files with 201 additions and 142 deletions

View File

@ -1,14 +1,23 @@
namespace CodexContractsPlugin using GethPlugin;
using Logging;
namespace CodexContractsPlugin
{ {
public interface ICodexContracts public interface ICodexContracts
{ {
string MarketplaceAddress { get; } string MarketplaceAddress { get; }
void MintTestTokens(IGethNode gethNode, IEthAddress ethAddress, TestToken testTokens);
TestToken GetTestTokenBalance(IGethNode gethNode, IEthAddress ethAddress);
} }
public class CodexContractsAccess : ICodexContracts public class CodexContractsAccess : ICodexContracts
{ {
public CodexContractsAccess(string marketplaceAddress, string abi, string tokenAddress) private readonly ILog log;
public CodexContractsAccess(ILog log, string marketplaceAddress, string abi, string tokenAddress)
{ {
this.log = log;
MarketplaceAddress = marketplaceAddress; MarketplaceAddress = marketplaceAddress;
Abi = abi; Abi = abi;
TokenAddress = tokenAddress; TokenAddress = tokenAddress;
@ -17,5 +26,18 @@
public string MarketplaceAddress { get; } public string MarketplaceAddress { get; }
public string Abi { get; } public string Abi { get; }
public string TokenAddress { get; } public string TokenAddress { get; }
public void MintTestTokens(IGethNode gethNode, IEthAddress ethAddress, TestToken testTokens)
{
var interaction = new ContractInteractions(log, gethNode);
interaction.MintTestTokens(ethAddress, testTokens.Amount, TokenAddress);
}
public TestToken GetTestTokenBalance(IGethNode gethNode, IEthAddress ethAddress)
{
var interaction = new ContractInteractions(log, gethNode);
var balance = interaction.GetBalance(TokenAddress, ethAddress.Address);
return balance.TestTokens();
}
} }
} }

View File

@ -38,12 +38,12 @@ namespace CodexContractsPlugin
var marketplaceAddress = extractor.ExtractMarketplaceAddress(); var marketplaceAddress = extractor.ExtractMarketplaceAddress();
var abi = extractor.ExtractMarketplaceAbi(); var abi = extractor.ExtractMarketplaceAbi();
var interaction = gethNode.StartInteraction(); var interaction = new ContractInteractions(tools.GetLog(), gethNode);
var tokenAddress = interaction.GetTokenAddress(marketplaceAddress); var tokenAddress = interaction.GetTokenAddress(marketplaceAddress);
Log("Extract completed. Marketplace deployed."); Log("Extract completed. Marketplace deployed.");
return new CodexContractsAccess(marketplaceAddress, abi, tokenAddress); return new CodexContractsAccess(tools.GetLog(), marketplaceAddress, abi, tokenAddress);
} }
private void Log(string msg) private void Log(string msg)

View File

@ -0,0 +1,81 @@
using GethPlugin;
using Logging;
using Nethereum.ABI.FunctionEncoding.Attributes;
using Nethereum.Contracts;
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 void MintTestTokens(IEthAddress address, decimal amount, string tokenAddress)
{
MintTokens(address.Address, amount, tokenAddress);
}
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();
}
private void MintTokens(string account, decimal amount, string tokenAddress)
{
log.Debug($"({tokenAddress}) {amount} --> {account}");
if (string.IsNullOrEmpty(account)) throw new ArgumentException("Invalid arguments for MintTestTokens");
var function = new MintTokensFunction
{
Holder = account,
Amount = amount.ToBig()
};
gethNode.SendTransaction(tokenAddress, function);
}
}
[Function("token", "address")]
public class GetTokenFunction : FunctionMessage
{
}
[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;
}
}

View File

@ -1,4 +1,5 @@
using Logging; using Logging;
using Nethereum.Contracts;
using NethereumWorkflow; using NethereumWorkflow;
namespace GethPlugin namespace GethPlugin
@ -7,12 +8,13 @@ namespace GethPlugin
{ {
IGethStartResult StartResult { get; } IGethStartResult StartResult { get; }
NethereumInteraction StartInteraction();
Ether GetEthBalance(); Ether GetEthBalance();
Ether GetEthBalance(IHasEthAddress address); Ether GetEthBalance(IHasEthAddress address);
Ether GetEthBalance(IEthAddress address); Ether GetEthBalance(IEthAddress address);
void SendEth(IHasEthAddress account, Ether eth); void SendEth(IHasEthAddress account, Ether eth);
void SendEth(IEthAddress account, Ether eth); void SendEth(IEthAddress account, Ether eth);
TResult Call<TFunction, TResult>(string contractAddress, TFunction function) where TFunction : FunctionMessage, new();
void SendTransaction<TFunction>(string contractAddress, TFunction function) where TFunction : FunctionMessage, new();
} }
public class GethNode : IGethNode public class GethNode : IGethNode
@ -29,15 +31,6 @@ namespace GethPlugin
public IGethStartResult StartResult { get; } public IGethStartResult StartResult { get; }
public GethAccount Account { get; } public GethAccount Account { get; }
public NethereumInteraction StartInteraction()
{
var address = StartResult.RunningContainer.Address;
var account = Account;
var creator = new NethereumInteractionCreator(log, address.Host, address.Port, account.PrivateKey);
return creator.CreateWorkflow();
}
public Ether GetEthBalance() public Ether GetEthBalance()
{ {
return StartInteraction().GetEthBalance().Eth(); return StartInteraction().GetEthBalance().Eth();
@ -60,8 +53,26 @@ namespace GethPlugin
public void SendEth(IEthAddress account, Ether eth) public void SendEth(IEthAddress account, Ether eth)
{ {
var i = StartInteraction(); StartInteraction().SendEth(account.Address, eth.Eth);
i.SendEth(account.Address, eth.Eth); }
public TResult Call<TFunction, TResult>(string contractAddress, TFunction function) where TFunction : FunctionMessage, new()
{
return StartInteraction().Call<TFunction, TResult>(contractAddress, function);
}
public void SendTransaction<TFunction>(string contractAddress, TFunction function) where TFunction : FunctionMessage, new()
{
StartInteraction().SendTransaction(contractAddress, function);
}
private NethereumInteraction StartInteraction()
{
var address = StartResult.RunningContainer.Address;
var account = Account;
var creator = new NethereumInteractionCreator(log, address.Host, address.Port, account.PrivateKey);
return creator.CreateWorkflow();
} }
} }
} }

View File

@ -0,0 +1,30 @@
using Nethereum.Hex.HexTypes;
using System.Numerics;
namespace NethereumWorkflow
{
public static class ConversionExtensions
{
public static HexBigInteger ToHexBig(this decimal amount)
{
var bigint = ToBig(amount);
var str = bigint.ToString("X");
return new HexBigInteger(str);
}
public static BigInteger ToBig(this decimal amount)
{
return new BigInteger(amount);
}
public static decimal ToDecimal(this HexBigInteger hexBigInteger)
{
return ToDecimal(hexBigInteger.Value);
}
public static decimal ToDecimal(this BigInteger bigInteger)
{
return (decimal)bigInteger;
}
}
}

View File

@ -1,10 +1,7 @@
using Logging; using Logging;
using Nethereum.ABI.FunctionEncoding.Attributes;
using Nethereum.Contracts; using Nethereum.Contracts;
using Nethereum.Hex.HexTypes;
using Nethereum.RPC.Eth.DTOs; using Nethereum.RPC.Eth.DTOs;
using Nethereum.Web3; using Nethereum.Web3;
using System.Numerics;
using Utils; using Utils;
namespace NethereumWorkflow namespace NethereumWorkflow
@ -37,128 +34,52 @@ namespace NethereumWorkflow
return Web3.Convert.FromWei(balance.Value); return Web3.Convert.FromWei(balance.Value);
} }
public string GetTokenAddress(string marketplaceAddress) public TResult Call<TFunction, TResult>(string contractAddress, TFunction function) where TFunction : FunctionMessage, new()
{ {
log.Debug(marketplaceAddress); var handler = web3.Eth.GetContractQueryHandler<TFunction>();
var function = new GetTokenFunction(); return Time.Wait(handler.QueryAsync<TResult>(contractAddress, function));
var handler = web3.Eth.GetContractQueryHandler<GetTokenFunction>();
return Time.Wait(handler.QueryAsync<string>(marketplaceAddress, function));
} }
public void MintTestTokens(string[] accounts, decimal amount, string tokenAddress) public void SendTransaction<TFunction>(string contractAddress, TFunction function) where TFunction : FunctionMessage, new()
{ {
if (amount < 1 || accounts.Length < 1) throw new ArgumentException("Invalid arguments for MintTestTokens"); var handler = web3.Eth.GetContractTransactionHandler<TFunction>();
var receipt = Time.Wait(handler.SendRequestAndWaitForReceiptAsync(contractAddress, function));
var tasks = accounts.Select(a => MintTokens(a, amount, tokenAddress)); if (!receipt.Succeeded()) throw new Exception("Unable to perform contract transaction.");
Task.WaitAll(tasks.ToArray());
} }
public decimal GetBalance(string tokenAddress, string account) //public bool IsSynced(string marketplaceAddress, string marketplaceAbi)
{ //{
log.Debug($"({tokenAddress}) {account}"); // try
var function = new GetTokenBalanceFunction // {
{ // return IsBlockNumberOK() && IsContractAvailable(marketplaceAddress, marketplaceAbi);
Owner = account // }
}; // catch
// {
// return false;
// }
//}
var handler = web3.Eth.GetContractQueryHandler<GetTokenBalanceFunction>(); //private bool IsBlockNumberOK()
return ToDecimal(Time.Wait(handler.QueryAsync<BigInteger>(tokenAddress, function))); //{
} // log.Debug();
// var sync = Time.Wait(web3.Eth.Syncing.SendRequestAsync());
// var number = Time.Wait(web3.Eth.Blocks.GetBlockNumber.SendRequestAsync());
// var numberOfBlocks = number.ToDecimal();
// return !sync.IsSyncing && numberOfBlocks > 256;
//}
public bool IsSynced(string marketplaceAddress, string marketplaceAbi) //private bool IsContractAvailable(string marketplaceAddress, string marketplaceAbi)
{ //{
try // log.Debug();
{ // try
return IsBlockNumberOK() && IsContractAvailable(marketplaceAddress, marketplaceAbi); // {
} // var contract = web3.Eth.GetContract(marketplaceAbi, marketplaceAddress);
catch // return contract != null;
{ // }
return false; // catch
} // {
} // return false;
// }
private Task MintTokens(string account, decimal amount, string tokenAddress) //}
{
log.Debug($"({tokenAddress}) {amount} --> {account}");
if (string.IsNullOrEmpty(account)) throw new ArgumentException("Invalid arguments for MintTestTokens");
var function = new MintTokensFunction
{
Holder = account,
Amount = ToBig(amount)
};
var handler = web3.Eth.GetContractTransactionHandler<MintTokensFunction>();
return handler.SendRequestAndWaitForReceiptAsync(tokenAddress, function);
}
private bool IsBlockNumberOK()
{
log.Debug();
var sync = Time.Wait(web3.Eth.Syncing.SendRequestAsync());
var number = Time.Wait(web3.Eth.Blocks.GetBlockNumber.SendRequestAsync());
var numberOfBlocks = ToDecimal(number);
return !sync.IsSyncing && numberOfBlocks > 256;
}
private bool IsContractAvailable(string marketplaceAddress, string marketplaceAbi)
{
log.Debug();
try
{
var contract = web3.Eth.GetContract(marketplaceAbi, marketplaceAddress);
return contract != null;
}
catch
{
return false;
}
}
private HexBigInteger ToHexBig(decimal amount)
{
var bigint = ToBig(amount);
var str = bigint.ToString("X");
return new HexBigInteger(str);
}
private BigInteger ToBig(decimal amount)
{
return new BigInteger(amount);
}
private decimal ToDecimal(HexBigInteger hexBigInteger)
{
return ToDecimal(hexBigInteger.Value);
}
private decimal ToDecimal(BigInteger bigInteger)
{
return (decimal)bigInteger;
}
}
[Function("token", "address")]
public class GetTokenFunction : FunctionMessage
{
}
[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;
} }
} }

View File

@ -58,14 +58,8 @@ namespace Tests.BasicTests
geth.SendEth(node, 10.Eth()); geth.SendEth(node, 10.Eth());
var nodeBalance = geth.GetEthBalance(node); var nodeBalance = geth.GetEthBalance(node);
//contracts.MintTestTokens(geth, node.EthAddress, 100.TestTokens()); contracts.MintTestTokens(geth, node.EthAddress, 100.TestTokens());
contracts.GetTestTokenBalance(geth, node.EthAddress);
//geth.GetEthBalance(node.EthAddress);
//contracts.GetTestTokenBalance(geth, node.EthAddress);
var i = 0;
//var sellerInitialBalance = 234.TestTokens(); //var sellerInitialBalance = 234.TestTokens();
//var buyerInitialBalance = 1000.TestTokens(); //var buyerInitialBalance = 1000.TestTokens();