From 6cf86af3b5cfc109b802662ead3c4a00f2c65a05 Mon Sep 17 00:00:00 2001 From: benbierens Date: Tue, 19 Sep 2023 13:39:24 +0200 Subject: [PATCH] Successful testtoken mint and balance --- CodexContractsPlugin/CodexContractsAccess.cs | 26 ++- CodexContractsPlugin/CodexContractsStarter.cs | 4 +- CodexContractsPlugin/ContractInteractions.cs | 81 +++++++++ GethPlugin/GethNode.cs | 35 ++-- Nethereum/ConversionExtensions.cs | 30 ++++ Nethereum/NethereumInteraction.cs | 157 +++++------------- Tests/BasicTests/ExampleTests.cs | 10 +- 7 files changed, 201 insertions(+), 142 deletions(-) create mode 100644 CodexContractsPlugin/ContractInteractions.cs create mode 100644 Nethereum/ConversionExtensions.cs diff --git a/CodexContractsPlugin/CodexContractsAccess.cs b/CodexContractsPlugin/CodexContractsAccess.cs index ba84a70..f4a4bf5 100644 --- a/CodexContractsPlugin/CodexContractsAccess.cs +++ b/CodexContractsPlugin/CodexContractsAccess.cs @@ -1,14 +1,23 @@ -namespace CodexContractsPlugin +using GethPlugin; +using Logging; + +namespace CodexContractsPlugin { public interface ICodexContracts { string MarketplaceAddress { get; } + + void MintTestTokens(IGethNode gethNode, IEthAddress ethAddress, TestToken testTokens); + TestToken GetTestTokenBalance(IGethNode gethNode, IEthAddress ethAddress); } 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; Abi = abi; TokenAddress = tokenAddress; @@ -17,5 +26,18 @@ public string MarketplaceAddress { get; } public string Abi { 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(); + } } } diff --git a/CodexContractsPlugin/CodexContractsStarter.cs b/CodexContractsPlugin/CodexContractsStarter.cs index 531f0c9..01640c1 100644 --- a/CodexContractsPlugin/CodexContractsStarter.cs +++ b/CodexContractsPlugin/CodexContractsStarter.cs @@ -38,12 +38,12 @@ namespace CodexContractsPlugin var marketplaceAddress = extractor.ExtractMarketplaceAddress(); var abi = extractor.ExtractMarketplaceAbi(); - var interaction = gethNode.StartInteraction(); + var interaction = new ContractInteractions(tools.GetLog(), gethNode); var tokenAddress = interaction.GetTokenAddress(marketplaceAddress); Log("Extract completed. Marketplace deployed."); - return new CodexContractsAccess(marketplaceAddress, abi, tokenAddress); + return new CodexContractsAccess(tools.GetLog(), marketplaceAddress, abi, tokenAddress); } private void Log(string msg) diff --git a/CodexContractsPlugin/ContractInteractions.cs b/CodexContractsPlugin/ContractInteractions.cs new file mode 100644 index 0000000..9e54b67 --- /dev/null +++ b/CodexContractsPlugin/ContractInteractions.cs @@ -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(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(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; + } +} diff --git a/GethPlugin/GethNode.cs b/GethPlugin/GethNode.cs index 99bfbf9..3613904 100644 --- a/GethPlugin/GethNode.cs +++ b/GethPlugin/GethNode.cs @@ -1,4 +1,5 @@ using Logging; +using Nethereum.Contracts; using NethereumWorkflow; namespace GethPlugin @@ -7,12 +8,13 @@ namespace GethPlugin { IGethStartResult StartResult { get; } - NethereumInteraction StartInteraction(); Ether GetEthBalance(); Ether GetEthBalance(IHasEthAddress address); Ether GetEthBalance(IEthAddress address); void SendEth(IHasEthAddress account, Ether eth); void SendEth(IEthAddress account, Ether eth); + TResult Call(string contractAddress, TFunction function) where TFunction : FunctionMessage, new(); + void SendTransaction(string contractAddress, TFunction function) where TFunction : FunctionMessage, new(); } public class GethNode : IGethNode @@ -29,15 +31,6 @@ namespace GethPlugin public IGethStartResult StartResult { 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() { return StartInteraction().GetEthBalance().Eth(); @@ -60,8 +53,26 @@ namespace GethPlugin public void SendEth(IEthAddress account, Ether eth) { - var i = StartInteraction(); - i.SendEth(account.Address, eth.Eth); + StartInteraction().SendEth(account.Address, eth.Eth); + } + + public TResult Call(string contractAddress, TFunction function) where TFunction : FunctionMessage, new() + { + return StartInteraction().Call(contractAddress, function); + } + + public void SendTransaction(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(); } } } diff --git a/Nethereum/ConversionExtensions.cs b/Nethereum/ConversionExtensions.cs new file mode 100644 index 0000000..22daa7c --- /dev/null +++ b/Nethereum/ConversionExtensions.cs @@ -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; + } + } +} diff --git a/Nethereum/NethereumInteraction.cs b/Nethereum/NethereumInteraction.cs index 2e595a6..a682ab4 100644 --- a/Nethereum/NethereumInteraction.cs +++ b/Nethereum/NethereumInteraction.cs @@ -1,10 +1,7 @@ using Logging; -using Nethereum.ABI.FunctionEncoding.Attributes; using Nethereum.Contracts; -using Nethereum.Hex.HexTypes; using Nethereum.RPC.Eth.DTOs; using Nethereum.Web3; -using System.Numerics; using Utils; namespace NethereumWorkflow @@ -37,128 +34,52 @@ namespace NethereumWorkflow return Web3.Convert.FromWei(balance.Value); } - public string GetTokenAddress(string marketplaceAddress) + public TResult Call(string contractAddress, TFunction function) where TFunction : FunctionMessage, new() { - log.Debug(marketplaceAddress); - var function = new GetTokenFunction(); - - var handler = web3.Eth.GetContractQueryHandler(); - return Time.Wait(handler.QueryAsync(marketplaceAddress, function)); + var handler = web3.Eth.GetContractQueryHandler(); + return Time.Wait(handler.QueryAsync(contractAddress, function)); } - public void MintTestTokens(string[] accounts, decimal amount, string tokenAddress) + public void SendTransaction(string contractAddress, TFunction function) where TFunction : FunctionMessage, new() { - if (amount < 1 || accounts.Length < 1) throw new ArgumentException("Invalid arguments for MintTestTokens"); - - var tasks = accounts.Select(a => MintTokens(a, amount, tokenAddress)); - - Task.WaitAll(tasks.ToArray()); + var handler = web3.Eth.GetContractTransactionHandler(); + var receipt = Time.Wait(handler.SendRequestAndWaitForReceiptAsync(contractAddress, function)); + if (!receipt.Succeeded()) throw new Exception("Unable to perform contract transaction."); } - public decimal GetBalance(string tokenAddress, string account) - { - log.Debug($"({tokenAddress}) {account}"); - var function = new GetTokenBalanceFunction - { - Owner = account - }; + //public bool IsSynced(string marketplaceAddress, string marketplaceAbi) + //{ + // try + // { + // return IsBlockNumberOK() && IsContractAvailable(marketplaceAddress, marketplaceAbi); + // } + // catch + // { + // return false; + // } + //} - var handler = web3.Eth.GetContractQueryHandler(); - return ToDecimal(Time.Wait(handler.QueryAsync(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 = number.ToDecimal(); + // return !sync.IsSyncing && numberOfBlocks > 256; + //} - public bool IsSynced(string marketplaceAddress, string marketplaceAbi) - { - try - { - return IsBlockNumberOK() && IsContractAvailable(marketplaceAddress, marketplaceAbi); - } - 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(); - 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; + //private bool IsContractAvailable(string marketplaceAddress, string marketplaceAbi) + //{ + // log.Debug(); + // try + // { + // var contract = web3.Eth.GetContract(marketplaceAbi, marketplaceAddress); + // return contract != null; + // } + // catch + // { + // return false; + // } + //} } } diff --git a/Tests/BasicTests/ExampleTests.cs b/Tests/BasicTests/ExampleTests.cs index a907276..5faaec1 100644 --- a/Tests/BasicTests/ExampleTests.cs +++ b/Tests/BasicTests/ExampleTests.cs @@ -58,14 +58,8 @@ namespace Tests.BasicTests geth.SendEth(node, 10.Eth()); var nodeBalance = geth.GetEthBalance(node); - //contracts.MintTestTokens(geth, node.EthAddress, 100.TestTokens()); - - //geth.GetEthBalance(node.EthAddress); - - //contracts.GetTestTokenBalance(geth, node.EthAddress); - - var i = 0; - + contracts.MintTestTokens(geth, node.EthAddress, 100.TestTokens()); + contracts.GetTestTokenBalance(geth, node.EthAddress); //var sellerInitialBalance = 234.TestTokens(); //var buyerInitialBalance = 1000.TestTokens();