Merge Complete
This commit is contained in:
1 parent
482ef7a81a
commit
dabe23d505
6483 files changed
No files matched your search
+17
@@ -0,0 +1,17 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp2.1</TargetFramework>
|
||||
<TargetLatestRuntimePatch>true</TargetLatestRuntimePatch>
|
||||
<LangVersion>latest</LangVersion>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Nethereum.Web3" Version="2.5.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\AdvancedAlgos.AlgoToken.Framework.Ethereum\AdvancedAlgos.AlgoToken.Framework.Ethereum.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
+78
@@ -0,0 +1,78 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Numerics;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Nethereum.Hex.HexTypes;
|
||||
using Nethereum.RPC.Accounts;
|
||||
using Nethereum.Util;
|
||||
using Nethereum.Web3;
|
||||
using Nethereum.Web3.Accounts;
|
||||
|
||||
namespace AdvancedAlgos.AlgoToken.Framework.Ethereum.IntegrationTest
|
||||
{
|
||||
public class EthNetwork
|
||||
{
|
||||
public const int TRANSACTION_BASE_FEE = 21000;
|
||||
|
||||
public static EthNetwork Instance { get; private set; }
|
||||
|
||||
private SemaphoreSlim _refillSync = new SemaphoreSlim(1, 1);
|
||||
|
||||
public string Url { get; private set; }
|
||||
public string PrefundedAddress { get; private set; }
|
||||
public string PrefundedPrivateKey { get; private set; }
|
||||
public IGasPriceProvider GasPriceProvider { get; private set; }
|
||||
public BigInteger TxBaseFeeWei => GasPriceProvider.GetGasPrice() * TRANSACTION_BASE_FEE;
|
||||
|
||||
public static void UseDefaultTestNet() => UseGanacheTestNet();
|
||||
|
||||
public static void UseGanacheTestNet()
|
||||
{
|
||||
Instance = new EthNetwork
|
||||
{
|
||||
Url = "http://127.0.0.1:8545",
|
||||
PrefundedAddress = "0x06bB4674A4b08d07186b721378C7e241eD85443b",
|
||||
PrefundedPrivateKey = "0936af475d2701538aad321f87e0a51f2b297634653393e8cab7290a674009a5",
|
||||
GasPriceProvider = new StaticGasPriceProvider(2000000000)
|
||||
};
|
||||
}
|
||||
|
||||
public static void UseGethTestNet()
|
||||
{
|
||||
Instance = new EthNetwork
|
||||
{
|
||||
Url = "http://10.4.0.4:8545",
|
||||
PrefundedAddress = "0x88474Ec3cfddfC943d3E42C94763D43f181A5a83",
|
||||
PrefundedPrivateKey = "0x69970bd6c1b6cfbffea16656c27ff7b330b751ed5e46c1644b54a642871f4bbb",
|
||||
GasPriceProvider = new StaticGasPriceProvider(2000000000)
|
||||
};
|
||||
}
|
||||
|
||||
public Web3 GetWeb3() => new Web3(Url);
|
||||
public Web3 GetWeb3(IAccount account) => new Web3(account, Url);
|
||||
|
||||
public Task RefillAsync(IAccount account)
|
||||
{
|
||||
return RefillAsync(account, UnitConversion.Convert.ToWei(1));
|
||||
}
|
||||
|
||||
public async Task RefillAsync(IAccount account, BigInteger weis)
|
||||
{
|
||||
await _refillSync.WaitAsync();
|
||||
try
|
||||
{
|
||||
var prefundedAccount = new Account(PrefundedPrivateKey);
|
||||
var web3 = new Web3(prefundedAccount, Url);
|
||||
var recepit = await web3.TransactionManager.TransactionReceiptService.SendRequestAndWaitForReceiptAsync(() =>
|
||||
web3.TransactionManager.SendTransactionAsync(prefundedAccount.Address, account.Address, new HexBigInteger(weis))
|
||||
);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_refillSync.Release();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Numerics;
|
||||
using System.Text;
|
||||
|
||||
namespace AdvancedAlgos.AlgoToken.Framework.Ethereum.IntegrationTest
|
||||
{
|
||||
public class StaticGasPriceProvider : IGasPriceProvider
|
||||
{
|
||||
private BigInteger _gasPrice;
|
||||
|
||||
public StaticGasPriceProvider(BigInteger gasPrice)
|
||||
{
|
||||
_gasPrice = gasPrice;
|
||||
}
|
||||
|
||||
public BigInteger GetGasPrice() => _gasPrice;
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp2.1</TargetFramework>
|
||||
<TargetLatestRuntimePatch>true</TargetLatestRuntimePatch>
|
||||
<LangVersion>latest</LangVersion>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Nethereum.Web3" Version="2.5.1" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using Nethereum.Hex.HexConvertors.Extensions;
|
||||
using Nethereum.Signer;
|
||||
using Nethereum.Web3.Accounts;
|
||||
|
||||
namespace AdvancedAlgos.AlgoToken.Framework.Ethereum
|
||||
{
|
||||
public static class EthAccountFactory
|
||||
{
|
||||
public static Account Create()
|
||||
{
|
||||
var accountEcKey = EthECKey.GenerateKey();
|
||||
var accountPrivateKey = accountEcKey.GetPrivateKeyAsBytes().ToHex();
|
||||
return new Account(accountPrivateKey);
|
||||
}
|
||||
}
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using Nethereum.Hex.HexConvertors.Extensions;
|
||||
using Nethereum.Util;
|
||||
|
||||
namespace AdvancedAlgos.AlgoToken.Framework.Ethereum
|
||||
{
|
||||
public static class EthAddressParser
|
||||
{
|
||||
private static readonly AddressUtil _addressUtil = new AddressUtil();
|
||||
|
||||
public static bool ValidateAddress(string address)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!_addressUtil.IsValidAddressLength(address)) return false;
|
||||
address = _addressUtil.ConvertToChecksumAddress(address);
|
||||
return _addressUtil.IsChecksumAddress(address);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
#pragma warning disable RCS1194 // Implement exception constructors.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using Nethereum.RPC.Eth.DTOs;
|
||||
|
||||
namespace AdvancedAlgos.AlgoToken.Framework.Ethereum.Exceptions
|
||||
{
|
||||
public class TransactionRejectedException : Exception
|
||||
{
|
||||
private const string ERROR_MESSAGE = "The transaction was rejected.";
|
||||
|
||||
public TransactionRejectedException(TransactionReceipt transactionReceipt)
|
||||
: base(ERROR_MESSAGE)
|
||||
{
|
||||
TransactionReceipt = transactionReceipt;
|
||||
}
|
||||
|
||||
public TransactionReceipt TransactionReceipt { get; }
|
||||
|
||||
public static void Throw(TransactionReceipt transactionReceipt) => throw new TransactionRejectedException(transactionReceipt);
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using Nethereum.RPC.Eth.DTOs;
|
||||
using AdvancedAlgos.AlgoToken.Framework.Ethereum.Exceptions;
|
||||
|
||||
namespace AdvancedAlgos.AlgoToken.Framework.Ethereum.Extensions
|
||||
{
|
||||
public static class TransactionReceiptExtensions
|
||||
{
|
||||
public static void EnsureSucceededStatus(this TransactionReceipt transactionReceipt)
|
||||
{
|
||||
if (transactionReceipt.Status.Value != 1) TransactionRejectedException.Throw(transactionReceipt);
|
||||
}
|
||||
}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Numerics;
|
||||
using System.Text;
|
||||
|
||||
namespace AdvancedAlgos.AlgoToken.Framework.Ethereum
|
||||
{
|
||||
public interface IGasPriceProvider
|
||||
{
|
||||
BigInteger GetGasPrice();
|
||||
}
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
|
||||
namespace AdvancedAlgos.AlgoToken.Framework.Ethereum.Reflection
|
||||
{
|
||||
public static class AssemblyExtensions
|
||||
{
|
||||
public static string GetResourceString(this Assembly assembly, string resourceName, Encoding encoding = null)
|
||||
{
|
||||
using (var stream = assembly.GetManifestResourceStream(assembly.GetName().Name + "." + resourceName))
|
||||
using (var reader = new StreamReader(stream, encoding ?? Encoding.UTF8))
|
||||
{
|
||||
return reader.ReadToEnd();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+109
@@ -0,0 +1,109 @@
|
||||
using System;
|
||||
using System.Numerics;
|
||||
using System.Threading.Tasks;
|
||||
using AdvancedAlgos.AlgoToken.Framework.Ethereum.Extensions;
|
||||
using AdvancedAlgos.AlgoToken.Framework.Ethereum.Reflection;
|
||||
using Nethereum.Contracts;
|
||||
using Nethereum.Hex.HexTypes;
|
||||
using Nethereum.RPC.Eth.DTOs;
|
||||
using Nethereum.Web3;
|
||||
|
||||
namespace AdvancedAlgos.AlgoToken.Framework.Ethereum
|
||||
{
|
||||
public abstract class SmartContract<TContract> where TContract : SmartContract<TContract>
|
||||
{
|
||||
private static readonly object _syncForResourceLoader = new object();
|
||||
|
||||
private static string _abi;
|
||||
private static string _bin;
|
||||
|
||||
public SmartContract(Web3 web3, IGasPriceProvider gasPriceProvider)
|
||||
{
|
||||
Web3 = web3 ?? throw new ArgumentNullException(nameof(web3));
|
||||
GasPriceProvider = gasPriceProvider ?? throw new ArgumentNullException(nameof(gasPriceProvider));
|
||||
|
||||
LoadAbiAndBin();
|
||||
}
|
||||
|
||||
public SmartContract(string contractAddress, Web3 web3, IGasPriceProvider gasPriceProvider)
|
||||
{
|
||||
Web3 = web3 ?? throw new ArgumentNullException(nameof(web3));
|
||||
GasPriceProvider = gasPriceProvider ?? throw new ArgumentNullException(nameof(gasPriceProvider));
|
||||
ContractAddress = contractAddress ?? throw new ArgumentNullException(nameof(contractAddress));
|
||||
|
||||
LoadAbiAndBin();
|
||||
|
||||
var contractDescriptor = web3.Eth.GetContract(_abi, contractAddress);
|
||||
|
||||
Initialize(contractDescriptor);
|
||||
}
|
||||
|
||||
public Web3 Web3 { get; private set; }
|
||||
public IGasPriceProvider GasPriceProvider { get; }
|
||||
public string ContractAddress { get; private set; }
|
||||
|
||||
protected abstract string AbiResourceName { get; }
|
||||
protected abstract string BinResourceName { get; }
|
||||
protected abstract BigInteger DeploymentGasUnits { get; }
|
||||
|
||||
public void Bind(Web3 web3)
|
||||
{
|
||||
Web3 = web3 ?? throw new ArgumentNullException(nameof(web3));
|
||||
|
||||
var contractDescriptor = Web3.Eth.GetContract(_abi, ContractAddress);
|
||||
|
||||
Initialize(contractDescriptor);
|
||||
}
|
||||
|
||||
protected virtual void Initialize(Contract contractDescriptor) { }
|
||||
|
||||
protected async Task<TransactionReceipt> DeployAsync(params object[] constructorParameters)
|
||||
{
|
||||
var transactionReceipt = await Web3.Eth.DeployContract.SendRequestAndWaitForReceiptAsync(
|
||||
_abi,
|
||||
_bin,
|
||||
Web3.TransactionManager.Account.Address,
|
||||
new HexBigInteger(DeploymentGasUnits),
|
||||
new HexBigInteger(GasPriceProvider.GetGasPrice()),
|
||||
null,
|
||||
null,
|
||||
constructorParameters);
|
||||
|
||||
transactionReceipt.EnsureSucceededStatus();
|
||||
|
||||
var contractDescriptor = Web3.Eth.GetContract(_abi, transactionReceipt.ContractAddress);
|
||||
|
||||
Initialize(contractDescriptor);
|
||||
|
||||
ContractAddress = transactionReceipt.ContractAddress;
|
||||
|
||||
return transactionReceipt;
|
||||
}
|
||||
|
||||
protected async Task<TransactionReceipt> InvokeAsync(Function function, BigInteger gasUnits, params object[] parameters)
|
||||
{
|
||||
var transactionReceipt = await function.SendTransactionAndWaitForReceiptAsync(
|
||||
Web3.TransactionManager.Account.Address,
|
||||
new HexBigInteger(gasUnits),
|
||||
new HexBigInteger(GasPriceProvider.GetGasPrice()),
|
||||
null,
|
||||
null,
|
||||
parameters);
|
||||
|
||||
transactionReceipt.EnsureSucceededStatus();
|
||||
|
||||
return transactionReceipt;
|
||||
}
|
||||
|
||||
private void LoadAbiAndBin()
|
||||
{
|
||||
lock (_syncForResourceLoader)
|
||||
{
|
||||
if (_abi != null) return;
|
||||
|
||||
_abi = GetType().Assembly.GetResourceString(AbiResourceName);
|
||||
_bin = GetType().Assembly.GetResourceString(BinResourceName);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user