Intelli TOken moved to old repos

This commit is contained in:
Luis Fernando Molina committed 2020-11-20 18:50:35 +01:00
1 parent 287023f978
commit c08e7b5430
153 files changed

No files matched your search

@@ -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>
@@ -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);
}
}
}
@@ -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;
}
}
}
}
@@ -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);
}
}
@@ -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);
}
}
}
@@ -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();
}
}
@@ -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();
}
}
}
}
@@ -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);
}
}
}
}