public async Task <Response <AccountModel> > NewAccount(CryptoAdapterModel cryptoAdapter, string newAccountPassword) { var web3 = InstantiateWeb3(cryptoAdapter); var response = new Response <AccountModel>(); try { var newDebitAccount = await web3.Personal.NewAccount.SendRequestAsync(newAccountPassword); var newCreditAccount = await web3.Personal.NewAccount.SendRequestAsync(newAccountPassword); response.Status = StatusEnum.Success; response.Value = new AccountModel { DebitAddress = newDebitAccount, CreditAddress = newCreditAccount }; } catch (Exception ex) { response.Status = StatusEnum.Error; response.Message = Message.SomethingWentWrong; _logger.Information($"EthereumAdapter.NewAccount(CryptoAdapterId: {cryptoAdapter.Id}, newAccountPassword: {newAccountPassword})"); _logger.Error(ex.Message); } return(response); }
private async Task <IResponse <NoValue> > TestConnection(CryptoAdapterModel cryptoAdapter) { var response = new Response <NoValue>(); var nodeType = cryptoAdapter.NodeType; switch (nodeType) { case AdapterTypeItemEnum.Ethereum: var adapter = new EthereumAdapter(_logger); if (cryptoAdapter.Direction == DirectionEnum.Destination) { response = await adapter.TestConnectionDestination(cryptoAdapter); } else { response = await adapter.TestConnectionSource(cryptoAdapter); } break; case AdapterTypeItemEnum.Cardano: break; case AdapterTypeItemEnum.EOS: break; case AdapterTypeItemEnum.NEO: var neoAdapter = new NEOAdapter(_logger); response = await neoAdapter.TestConnectionSource(cryptoAdapter); break; case AdapterTypeItemEnum.Bitcoin: var btcAdapter = new BitcoinAdapter(_logger); var btcUsername = cryptoAdapter.Properties.SelectMany(p => p.SourceProperties).FirstOrDefault(sp => sp.Id == (long)PropertyEnum.RpcUsername).Value; var btcPassword = cryptoAdapter.Properties.SelectMany(p => p.SourceProperties).FirstOrDefault(sp => sp.Id == (long)PropertyEnum.RpcPassword).Value; response = btcAdapter.TestConnectionSource(cryptoAdapter, btcUsername, btcPassword); break; case AdapterTypeItemEnum.Litecoin: var ltcAdapter = new LitecoinAdapter(_logger); var ltcUsername = cryptoAdapter.Properties.SelectMany(p => p.SourceProperties).FirstOrDefault(sp => sp.Id == (long)PropertyEnum.RpcUsername).Value; var ltcPassword = cryptoAdapter.Properties.SelectMany(p => p.SourceProperties).FirstOrDefault(sp => sp.Id == (long)PropertyEnum.RpcPassword).Value; response = ltcAdapter.TestConnectionSource(cryptoAdapter, ltcUsername, ltcPassword); break; } return(response); }
public async Task <Response <int> > GetCurrentBlockNumber(CryptoAdapterModel cryptoAdapter) { var response = new Response <int>(); try { var client = InstantiateRpcClient(cryptoAdapter); var neoApiService = new NeoApiService(client); var blockNumber = await neoApiService.Blocks.GetBlockCount.SendRequestAsync(); response.Status = StatusEnum.Success; response.Value = blockNumber; } catch (Exception ex) { response.Status = StatusEnum.Error; response.Message = ex.Message; _logger.Information($"NEOAdapter.GetCurrentBlockNumber(cryptoAdapter: {cryptoAdapter.Id}"); _logger.Error(ex.Message); } return(response); }
private static Web3 InstantiateWeb3(CryptoAdapterModel cryptoAdapter) { Web3 web3; web3 = new Web3("http://" + cryptoAdapter.RpcAddr + ":" + cryptoAdapter.RpcPort + "/"); return(web3); }
public async Task <IResponse <AccountModel> > NewAccount(long cryptoAdapterId) { var response = new Response <AccountModel>(); try { var cryptoAdapter = _entities.CryptoAdapter.Find(cryptoAdapterId); var cryptoAdapterModel = new CryptoAdapterModel { RpcAddr = cryptoAdapter.RpcAddr, }; if (cryptoAdapter.RpcPort != null) { cryptoAdapterModel.RpcPort = UInt16.Parse(cryptoAdapter.RpcPort); } var newAccountPassword = cryptoAdapter.CryptoAdapterProperty.FirstOrDefault(cap => cap.PropertyId == (long)PropertyEnum.NewAccountPassword).Value; var nodeType = (AdapterTypeItemEnum)cryptoAdapter.Adapter.AdapterTypeItemId; switch (nodeType) { case AdapterTypeItemEnum.Ethereum: var adapter = new EthereumAdapter(_logger); response = await adapter.NewAccount(cryptoAdapterModel, newAccountPassword); break; case AdapterTypeItemEnum.Cardano: break; case AdapterTypeItemEnum.EOS: break; case AdapterTypeItemEnum.NEO: break; default: //add logger throw new Exception(); } } catch (Exception ex) { response.Status = StatusEnum.Error; response.Message = Message.SomethingWentWrong; _logger.Information($"BlockchainService.NewAccount(cryptoAdapterId: {cryptoAdapterId})"); _logger.Error(ex.Message); } return(response); }
public async Task <TransactionResponse <BlockchainTransactionModel> > GetTransactionStatus(BlockchainTransactionModel transaction, long cryptoAdapterId) { var response = new TransactionResponse <BlockchainTransactionModel>(); try { var cryptoAdapter = _entities.CryptoAdapter.Find(cryptoAdapterId); var cryptoAdapterModel = new CryptoAdapterModel { RpcAddr = cryptoAdapter.RpcAddr }; if (cryptoAdapter.RpcPort != null) { cryptoAdapterModel.RpcPort = UInt16.Parse(cryptoAdapter.RpcPort); } var nodeType = (AdapterTypeItemEnum)cryptoAdapter.Adapter.AdapterTypeItemId; switch (nodeType) { case AdapterTypeItemEnum.Ethereum: var adapter = new EthereumAdapter(_logger); response = await adapter.GetTransactionStatus(transaction, cryptoAdapterModel); break; case AdapterTypeItemEnum.Cardano: break; case AdapterTypeItemEnum.EOS: break; case AdapterTypeItemEnum.NEO: break; default: //add logger throw new Exception(); } } catch (Exception ex) { response.Succeeded = false; response.Message = Message.SomethingWentWrong; _logger.Information($"BlockchainService.GetTransactionStatus(transaction: {transaction}, cryptoAdapterId: {cryptoAdapterId})"); _logger.Error(ex.Message); } return(response); }
public async Task <IActionResult> Edit(CryptoAdapterModel model) { if (ModelState.IsValid) { var response = await _cryptoAdapterService.UpdateCryptoAdapter(model); return(Json(response)); } return(View(model)); }
public async Task <IActionResult> Create(CryptoAdapterModel model) { var userProfileId = User.Identity.GetUserProfileId(); if (ModelState.IsValid) { var response = await _cryptoAdapterService.CreateCryptoAdapter(model, (long)userProfileId); return(Json(response)); } return(View(model)); }
public CryptoAdapterModel GetInitModel() { var result = new CryptoAdapterModel { Properties = new List <NodeModel>() }; foreach (var node in Enum.GetValues(typeof(CryptoAdapterType)).Cast <CryptoAdapterType>()) { result.Properties.Add(new NodeModel { NodeType = node, SourceProperties = GetProperties(node, DirectionEnum.Source), DestinationProperties = GetProperties(node, DirectionEnum.Destination) }); } return(result); }
public Response <NoValue> TestConnectionSource(CryptoAdapterModel cryptoAdapter, string username, string password) { var response = new Response <NoValue>(); try { var client = InstantiateRpcClient(cryptoAdapter, username, password); client.GetBlockCount(); response.Status = StatusEnum.Success; } catch (Exception ex) { response.Status = StatusEnum.Error; response.Message = Message.TestConnectionFailed; _logger.Information($"LitecoinAdapter.TestConnectionSource(cryptoAdapter: {cryptoAdapter.Id}, username:{username}, password:{password}"); _logger.Error(ex.Message); } return(response); }
public async Task <Response <NoValue> > TestConnectionSource(CryptoAdapterModel cryptoAdapter) { var response = new Response <NoValue>(); var web3 = InstantiateWeb3(cryptoAdapter); try { var blockNumber = await web3.Eth.Blocks.GetBlockNumber.SendRequestAsync(); response.Status = StatusEnum.Success; } catch (Exception ex) { response.Message = Message.TestConnectionFailed; response.Status = StatusEnum.Error; _logger.Information($"EthereumAdapter.TestConnectionSource(CryptoAdapterId: {cryptoAdapter.Id})"); _logger.Error(ex.Message); } return(response); }
public async Task <Response <int> > GetCurrentBlockNumber(CryptoAdapterModel cryptoAdapter) { var response = new Response <int>(); var web3 = InstantiateWeb3(cryptoAdapter); try { var blockNumber = await web3.Eth.Blocks.GetBlockNumber.SendRequestAsync(); response.Value = (int)(blockNumber.Value); response.Status = StatusEnum.Success; } catch (Exception ex) { response.Message = Message.SomethingWentWrong; response.Status = StatusEnum.Error; _logger.Information($"EthereumAdapter.GetCurrentBlockNumber(CryptoAdapterId: {cryptoAdapter.Id})"); _logger.Error(ex.Message); } return(response); }
public Response <int> GetCurrentBlockNumber(CryptoAdapterModel cryptoAdapter, string username, string password) { var response = new Response <int>(); try { var client = InstantiateRpcClient(cryptoAdapter, username, password); var blockNumber = client.GetBlockCount(); response.Value = blockNumber; response.Status = Common.Enums.StatusEnum.Success; } catch (Exception ex) { response.Status = Common.Enums.StatusEnum.Error; response.Message = ex.Message; _logger.Information($"LiteAdapter.GetCurrentBlockNumber(cryptoAdapter: {cryptoAdapter.Id}, username:{username}, password:{password}"); _logger.Error(ex.Message); } return(response); }
public async Task <Response <NoValue> > TestConnectionSource(CryptoAdapterModel cryptoAdapter) { var response = new Response <NoValue>(); try { var client = InstantiateRpcClient(cryptoAdapter); var neoApiService = new NeoApiService(client); await neoApiService.Blocks.GetBlockCount.SendRequestAsync(); response.Status = StatusEnum.Success; } catch (Exception ex) { response.Message = Message.TestConnectionFailed; response.Status = StatusEnum.Error; _logger.Information($"NEOAdapter.TestConnectionSource(CryptoAdapterId: {cryptoAdapter.Id})"); _logger.Error(ex.Message); } return(response); }
public async Task <Response <List <NeoBlockModel> > > GetBlocksWithTransactions(CryptoAdapterModel cryptoAdapter, int fromBlock, int toBlock, string address) { var response = new Response <List <NeoBlockModel> >() { Value = new List <NeoBlockModel>() }; try { var client = InstantiateRpcClient(cryptoAdapter); var neoApiService = new NeoApiService(client); if (toBlock < fromBlock) { _logger.Information($"NEOAdapter.GetBlocksWithTransactions(cryptoAdapter: {cryptoAdapter.Id}, fromBlock: {fromBlock}, toBlock: {toBlock}, address: {address}"); _logger.Error($"FromBlock value {fromBlock} is greater then ToBlock value {toBlock}"); response.Status = StatusEnum.Error; return(response); } var stopwatch = Stopwatch.StartNew(); for (int i = fromBlock; i <= toBlock; i++) { var block = await neoApiService.Blocks.GetBlock.SendRequestAsync(i); var blockModel = new NeoBlockModel() { BlockNumber = i, Time = DateTimeOffset.FromUnixTimeSeconds(block.Time).LocalDateTime }; foreach (var transaction in block.Transactions) { var blockTransactionModel = new NeoBlockTransactionModel() { TransactionId = transaction.Txid, TransactionType = transaction.Type }; foreach (var transactionInput in transaction.Vin) { var transactionInModel = new NeoTransactionInputModel() { TransactionId = transactionInput.TransactionId }; blockTransactionModel.TransactionInputs.Add(transactionInModel); } foreach (var transactionOutput in transaction.Vout) { var transactionOutModel = new NeoTransactionOutputModel() { Address = transactionOutput.Address, Asset = transactionOutput.Asset, Value = Decimal.Parse(transactionOutput.Value, CultureInfo.InvariantCulture) }; if (address == String.Empty || (String.Equals(address, transactionOutModel.Address))) { blockTransactionModel.TransactionOutputs.Add(transactionOutModel); } } if (address == String.Empty || blockTransactionModel.TransactionOutputs.Exists(to => String.Equals(to.Address, address))) { blockModel.TransactionList.Add(blockTransactionModel); } } response.Value.Add(blockModel); } stopwatch.Stop(); _logger.Information($"NEOAdapter.GetBlocksWithTransactions(cryptoAdapter: {cryptoAdapter.Id}, fromBlock: {fromBlock}, toBlock: {toBlock}, address: {address}). Time elapsed: {stopwatch.Elapsed.TotalSeconds} seconds."); response.Status = StatusEnum.Success; } catch (Exception ex) { response.Status = StatusEnum.Error; response.Message = ex.Message; _logger.Information($"NEOAdapter.GetBlocksWithTransactions(cryptoAdapter: {cryptoAdapter.Id}, fromBlock: {fromBlock}, toBlock: {toBlock}, address: {address}"); _logger.Error(ex.Message); } return(response); }
private static RpcClient InstantiateRpcClient(CryptoAdapterModel cryptoAdapter) => new RpcClient(new Uri($"http://{cryptoAdapter.RpcAddr}:{cryptoAdapter.RpcPort}"));
public async Task <Response <NoValue> > TestConnectionDestination(CryptoAdapterModel cryptoAdapter) { var response = new Response <NoValue>(); Web3 web3; var coinbase = cryptoAdapter.Properties.FirstOrDefault().DestinationProperties.FirstOrDefault(dp => dp.Id == (long)PropertyEnum.Coinbase).Value; var password = cryptoAdapter.Properties.FirstOrDefault().DestinationProperties.FirstOrDefault(dp => dp.Id == (long)PropertyEnum.CoinbasePassword).Value; web3 = InstantiateWeb3(cryptoAdapter); try { var unlockAccount = await web3.Personal.UnlockAccount.SendRequestAsync(coinbase, password, 1); response.Status = StatusEnum.Success; } catch (Exception ex) { _logger.Information($"EthereumAdapter.TestConnection(Ip: {cryptoAdapter.RpcAddr}, Port: {cryptoAdapter.RpcPort}, Coinbase: {coinbase}, Password: {password})"); if (ex.Message.Equals("could not decrypt key with given passphrase", StringComparison.InvariantCultureIgnoreCase)) { response.Message = "Test connection failed. Reason: Incorrect address or password."; _logger.Error(ex.Message); } else if (ex.Message.Equals("invalid argument 0: json: cannot unmarshal hex string of odd length into Go value of type common.Address", StringComparison.InvariantCultureIgnoreCase) || ex.Message.Equals("no key for given address or file", StringComparison.InvariantCultureIgnoreCase)) { response.Message = "Test connection failed. Reason: Incorrect coinbase address."; _logger.Error(ex.Message); } else if (ex.InnerException != null) { if (ex.InnerException.InnerException != null) { if (ex.InnerException.InnerException.Message.Equals("Unable to connect to the remote server", StringComparison.InvariantCultureIgnoreCase)) { response.Message = "Test connection failed. Reason: Incorrect IP address or port."; _logger.Error(ex.InnerException.InnerException.Message); } else { response.Message = Message.TestConnectionFailed; _logger.Error(ex.InnerException.InnerException.Message); } } else { response.Message = Message.TestConnectionFailed; _logger.Error(ex.InnerException.Message); } } else { response.Message = Message.TestConnectionFailed; _logger.Error(ex.Message); } response.Status = StatusEnum.Error; } return(response); }
public IResponse <CryptoAdapterModel> GetCryptoAdapter(long cryptoAdapterId) { var response = new Response <CryptoAdapterModel>(); try { var cryptoAdapter = _entity.CryptoAdapter.Find(cryptoAdapterId); var cryptoAdapterModel = new CryptoAdapterModel { Id = cryptoAdapter.Id, Direction = (DirectionEnum)cryptoAdapter.Adapter.DirectionId, Name = cryptoAdapter.Adapter.Name, NodeType = (AdapterTypeItemEnum)cryptoAdapter.Adapter.AdapterTypeItemId, RpcAddr = cryptoAdapter.RpcAddr, RpcPort = UInt16.Parse(cryptoAdapter.RpcPort), Properties = new List <NodeModel>() }; cryptoAdapterModel.Properties.Add(new NodeModel() { NodeType = (CryptoAdapterType)cryptoAdapter.Adapter.AdapterTypeItem.AdapterType.Id, SourceProperties = new List <PropertyModel>(), DestinationProperties = new List <PropertyModel>() }); var properties = _entity.CryptoAdapterProperty .Where(cap => cap.CryptoAdapterId == cryptoAdapter.Id) .Select(s => new PropertyModel { Id = s.Property.Id, Name = s.Property.Name, PropertyType = (PropertyTypeEnum)s.Property.PropertyTypeId, Value = s.Property.PropertyTypeId == (int)PropertyTypeEnum.Password ? String.Empty : s.Value }) .ToList(); switch (cryptoAdapterModel.Direction) { case DirectionEnum.Source: cryptoAdapterModel.Properties.First().SourceProperties = properties; break; case DirectionEnum.Destination: cryptoAdapterModel.Properties.First().DestinationProperties = properties; break; default: break; } response.Status = StatusEnum.Success; response.Value = cryptoAdapterModel; } catch (Exception ex) { response.Message = Message.SomethingWentWrong; response.Status = StatusEnum.Error; _logger.Information($"CryptoAdapterService.GetCryptoAdapter(cryptoAdapterId: {cryptoAdapterId})"); _logger.Error(ex.Message); } return(response); }
public async Task <TransactionResponse <BlockchainTransactionModel> > SendTransaction(BlockchainTransactionModel transaction, CryptoAdapterModel cryptoAdapter) { var response = new TransactionResponse <BlockchainTransactionModel>(); var web3 = InstantiateWeb3(cryptoAdapter); var coinbase = cryptoAdapter.Properties.FirstOrDefault().DestinationProperties.FirstOrDefault(dp => dp.Id == (long)PropertyEnum.Coinbase).Value; var password = cryptoAdapter.Properties.FirstOrDefault().DestinationProperties.FirstOrDefault(dp => dp.Id == (long)PropertyEnum.CoinbasePassword).Value; try { if (String.IsNullOrEmpty(coinbase)) { coinbase = await web3.Eth.CoinBase.SendRequestAsync(); } var unlockAccount = await web3.Personal.UnlockAccount.SendRequestAsync(coinbase, password, 0); var statement = transaction.Statement; var address = String.Empty; switch (statement) { case Statement.Credit: address = transaction.Account.CreditAddress; break; case Statement.Debit: address = transaction.Account.DebitAddress; break; } var value = transaction.Value; var weiValue = Web3.Convert.ToWei(value, Nethereum.Util.UnitConversion.EthUnit.Ether); var hexValue = new HexBigInteger(weiValue); var gasPrice = await web3.Eth.GasPrice.SendRequestAsync(); CallInput callInput = new CallInput { From = coinbase, To = address, Value = hexValue }; var gas = await web3.Eth.Transactions.EstimateGas.SendRequestAsync(callInput); web3.TransactionManager.DefaultGasPrice = gasPrice; web3.TransactionManager.DefaultGas = gas; TransactionInput transactionInput = new TransactionInput { From = coinbase, To = address, Value = hexValue }; var transactionHash = await web3.Eth.Transactions.SendTransaction.SendRequestAsync(transactionInput); response.Succeeded = true; response.Result = new BlockchainTransactionModel { Account = new AccountModel { CreditAddress = transaction.Account.CreditAddress, DebitAddress = transaction.Account.DebitAddress }, Id = transaction.Id, Value = transaction.Value, Statement = transaction.Statement, TxHash = transactionHash }; var lockAccount = await web3.Personal.LockAccount.SendRequestAsync(coinbase); } catch (Exception ex) { response.Succeeded = false; response.Message = ex.Message; _logger.Information($"EthereumAdapter.SendTransaction(TransactionId: {transaction.Id}, CryptoAdapterId: {cryptoAdapter.Id})"); _logger.Error(ex.Message); } return(response); }
private static RPCClient InstantiateRpcClient(CryptoAdapterModel cryptoAdapter, string username, string password) { return(new RPCClient($"{username}:{password}", $"{cryptoAdapter.RpcAddr}:{cryptoAdapter.RpcPort}", Network.Main)); }
public async Task <Response <List <EthereumBlockModel> > > GetBlocksWithTransactions(CryptoAdapterModel cryptoAdapter, int fromBlock, int toBlock, string address) { var response = new Response <List <EthereumBlockModel> >() { Value = new List <EthereumBlockModel>() }; var web3 = InstantiateWeb3(cryptoAdapter); try { if (fromBlock > toBlock) { _logger.Information($"EthereumAdapter.GetBlocksWithTransactions(cryptoAdapter: {cryptoAdapter.Id}, fromBlock: {fromBlock}, toBlock: {toBlock}, address: {address}"); _logger.Error($"FromBlock value {fromBlock} is greater than ToBlock value {toBlock}"); response.Status = StatusEnum.Error; return(response); } var stopwatch = Stopwatch.StartNew(); for (int i = fromBlock; i <= toBlock; i++) { var block = await web3.Eth.Blocks.GetBlockWithTransactionsByNumber.SendRequestAsync(new HexBigInteger(i)); var blockModel = new EthereumBlockModel { BlockNumber = (int)block.Number.Value, TimeStamp = UnixTimeStampToDateTime((double)block.Timestamp.Value), BlockTransactions = new List <EthereumBlockTransactionModel>() }; foreach (var transaction in block.Transactions) { var blockTransactionModel = new EthereumBlockTransactionModel { Hash = transaction.TransactionHash, Value = Web3.Convert.FromWei(transaction.Value), From = transaction.From, To = transaction.To }; address = address.ToLower(); if (address == String.Empty || (String.Equals(transaction.From.ToLower(), address) || String.Equals(transaction.To?.ToLower(), address))) { var transactionReceipt = await web3.Eth.Transactions.GetTransactionReceipt.SendRequestAsync(transaction.TransactionHash); var status = transactionReceipt.Status?.Value; blockTransactionModel.Status = ConvertStatus(status); blockModel.BlockTransactions.Add(blockTransactionModel); } } response.Value.Add(blockModel); } stopwatch.Stop(); _logger.Information($"EthereumAdapter.GetBlocksWithTransactions(cryptoAdapter: {cryptoAdapter.Id}, fromBlock: {fromBlock}, toBlock: {toBlock}, address: {address}). Time elapsed: {stopwatch.Elapsed.TotalSeconds} seconds."); response.Status = StatusEnum.Success; } catch (Exception ex) { _logger.Information($"EthereumAdapter.GetBlocksWithTransactions(cryptoAdapter: {cryptoAdapter.Id}, fromBlock: {fromBlock}, toBlock: {toBlock}, address: {address}"); _logger.Error(ex.Message); response.Status = StatusEnum.Error; response.Message = ex.Message; } return(response); }
private static RPCClient InstantiateRpcClient(CryptoAdapterModel cryptoAdapter, string username, string password) => new RPCClient($"{username}:{password}", $"{cryptoAdapter.RpcAddr}:{cryptoAdapter.RpcPort}", Litecoin.Instance.Mainnet);
public async Task <Response <List <T> > > GetBlocksWithTransactions <T>(long adapterId, int fromBlock, int toBlock, string address) { var response = new Response <List <T> >() { Value = new List <T>() }; try { var cryptoAdapter = _entities.Adapter.Find(adapterId).CryptoAdapter.FirstOrDefault(); var cryptoAdapterModel = new CryptoAdapterModel { Id = cryptoAdapter.Id, RpcAddr = cryptoAdapter.RpcAddr, RpcPort = Convert.ToUInt16(cryptoAdapter.RpcPort) }; if (typeof(T) == typeof(EthereumBlockModel)) { var ethereumResponse = new Response <List <EthereumBlockModel> >() { Value = new List <EthereumBlockModel>() }; var adapter = new EthereumAdapter(_logger); ethereumResponse = await adapter.GetBlocksWithTransactions(cryptoAdapterModel, fromBlock, toBlock, address); response = (Response <List <T> >)Convert.ChangeType(ethereumResponse, typeof(Response <List <T> >)); } else if (typeof(T) == typeof(BitcoinBlockModel)) { var bitcoinResponse = new Response <List <BitcoinBlockModel> >() { Value = new List <BitcoinBlockModel>() }; var username = cryptoAdapter.CryptoAdapterProperty.FirstOrDefault(cap => cap.PropertyId == (long)PropertyEnum.RpcUsername).Value; var password = cryptoAdapter.CryptoAdapterProperty.FirstOrDefault(cap => cap.PropertyId == (long)PropertyEnum.RpcPassword).Value; var adapter = new BitcoinAdapter(_logger); bitcoinResponse = adapter.GetBlocksWithTransactions(cryptoAdapterModel, username, password, fromBlock, toBlock, address); response = (Response <List <T> >)Convert.ChangeType(bitcoinResponse, typeof(Response <List <T> >)); } else if (typeof(T) == typeof(LitecoinBlockModel)) { var litecoinResponse = new Response <List <LitecoinBlockModel> >() { Value = new List <LitecoinBlockModel>() }; var username = cryptoAdapter.CryptoAdapterProperty.FirstOrDefault(cap => cap.PropertyId == (long)PropertyEnum.RpcUsername).Value; var password = cryptoAdapter.CryptoAdapterProperty.FirstOrDefault(cap => cap.PropertyId == (long)PropertyEnum.RpcPassword).Value; var adapter = new LitecoinAdapter(_logger); litecoinResponse = adapter.GetBlocksWithTransactions(cryptoAdapterModel, username, password, fromBlock, toBlock, address); response = (Response <List <T> >)Convert.ChangeType(litecoinResponse, typeof(Response <List <T> >)); } else if (typeof(T) == typeof(NeoBlockModel)) { var neoResponse = new Response <List <NeoBlockModel> >() { Value = new List <NeoBlockModel>() }; var adapter = new NEOAdapter(_logger); neoResponse = await adapter.GetBlocksWithTransactions(cryptoAdapterModel, fromBlock, toBlock, address); response = (Response <List <T> >)Convert.ChangeType(neoResponse, typeof(Response <List <T> >)); } } catch (Exception ex) { response.Status = StatusEnum.Error; response.Message = Message.SomethingWentWrong; _logger.Information($"BlockchainService.GetBlocksWithTransactions(adapterId: {adapterId}, fromBlock: {fromBlock}), toBlock: {toBlock}, address: {address}"); _logger.Error(ex.Message); } return(response); }
public async Task <Response <int> > GetCurrentBlockNumber(long adapterId, AdapterTypeItemEnum adapterType) { var response = new Response <int>(); try { var cryptoAdapter = _entities.Adapter.Find(adapterId).CryptoAdapter.FirstOrDefault(); var cryptoAdapterModel = new CryptoAdapterModel { RpcAddr = cryptoAdapter.RpcAddr }; if (cryptoAdapter.RpcPort != null) { cryptoAdapterModel.RpcPort = UInt16.Parse(cryptoAdapter.RpcPort); } switch (adapterType) { case AdapterTypeItemEnum.Ethereum: var ethAdapter = new EthereumAdapter(_logger); response = await ethAdapter.GetCurrentBlockNumber(cryptoAdapterModel); break; case AdapterTypeItemEnum.Bitcoin: var btcUsername = cryptoAdapter.CryptoAdapterProperty.FirstOrDefault(cap => cap.PropertyId == (long)PropertyEnum.RpcUsername).Value; var btcPassword = cryptoAdapter.CryptoAdapterProperty.FirstOrDefault(cap => cap.PropertyId == (long)PropertyEnum.RpcPassword).Value; var btcAdapter = new BitcoinAdapter(_logger); response = btcAdapter.GetCurrentBlockNumber(cryptoAdapterModel, btcUsername, btcPassword); break; case AdapterTypeItemEnum.NEO: var neoAdapter = new NEOAdapter(_logger); response = await neoAdapter.GetCurrentBlockNumber(cryptoAdapterModel); break; case AdapterTypeItemEnum.Litecoin: var ltcUsername = cryptoAdapter.CryptoAdapterProperty.FirstOrDefault(cap => cap.PropertyId == (long)PropertyEnum.RpcUsername).Value; var ltcPassword = cryptoAdapter.CryptoAdapterProperty.FirstOrDefault(cap => cap.PropertyId == (long)PropertyEnum.RpcPassword).Value; var ltcAdapter = new LitecoinAdapter(_logger); response = ltcAdapter.GetCurrentBlockNumber(cryptoAdapterModel, ltcUsername, ltcPassword); break; default: break; } } catch (Exception ex) { response.Status = StatusEnum.Error; response.Message = Message.SomethingWentWrong; _logger.Information($"BlockchainService.GetCurrentBlockNumber(adapterId: {adapterId}, adapterType: {adapterType}"); _logger.Error(ex.Message); } return(response); }
public Response <List <LitecoinBlockModel> > GetBlocksWithTransactions(CryptoAdapterModel cryptoAdapter, string username, string password, int fromBlock, int toBlock, string address) { var response = new Response <List <LitecoinBlockModel> >() { Value = new List <LitecoinBlockModel>() }; try { var client = InstantiateRpcClient(cryptoAdapter, username, password); if (toBlock < fromBlock) { _logger.Information($"LitecoinAdapter.GetBlocksWithTransactions(cryptoAdapter: {cryptoAdapter.Id}, username:{username}, password:{password}, fromBlock: {fromBlock}, toBlock: {toBlock}, address: {address}"); _logger.Error($"FromBlock value {fromBlock} is greater than ToBlock value {toBlock}"); response.Status = Common.Enums.StatusEnum.Error; return(response); } var stopwatch = Stopwatch.StartNew(); for (int i = fromBlock; i <= toBlock; i++) { var block = client.GetBlock(i); var blockModel = new LitecoinBlockModel() { BlockNumber = i, Time = UnixTimeStampToDateTime(Convert.ToDouble(block.Header.BlockTime.ToUnixTimeSeconds())) }; foreach (var transaction in block.Transactions) { var blockTransactionModel = new LitecoinBlockTransactionModel { TransactionHash = transaction.GetHash().ToString(), TotalOutValue = transaction.TotalOut.ToDecimal(MoneyUnit.BTC) }; foreach (var transactionInput in transaction.Inputs) { var transactionInModel = new LitecoinTransactionInModel(); if (transactionInput.ScriptSig.GetSignerAddress(Litecoin.Instance.Mainnet) != null) { transactionInModel.Address = transactionInput.ScriptSig.GetSignerAddress(Litecoin.Instance.Mainnet).ToString(); } else { transactionInModel.Address = "Address could not be retrieved."; } if (address == String.Empty || (String.Equals(address, transactionInModel.Address))) { blockTransactionModel.TransactionInputs.Add(transactionInModel); } } blockTransactionModel.TransactionInputs = blockTransactionModel.TransactionInputs.GroupBy(ti => ti.Address).Select(ti => ti.FirstOrDefault()).ToList(); foreach (var transactionOutput in transaction.Outputs) { var transactionOutModel = new LitecoinTransactionOutModel { Value = transactionOutput.Value.ToDecimal(MoneyUnit.BTC) }; if (transactionOutput.ScriptPubKey.GetDestinationAddress(Litecoin.Instance.Mainnet) != null) { transactionOutModel.Address = transactionOutput.ScriptPubKey.GetDestinationAddress(Litecoin.Instance.Mainnet).ToString(); } else { transactionOutModel.Address = "Address could not be retrieved."; } if (address == String.Empty || (String.Equals(address, transactionOutModel.Address))) { blockTransactionModel.TransactionOutputs.Add(transactionOutModel); } } if (address == String.Empty || blockTransactionModel.TransactionInputs.Exists(ti => String.Equals(ti.Address, address)) || blockTransactionModel.TransactionOutputs.Exists(to => String.Equals(to.Address, address))) { blockModel.TransactionList.Add(blockTransactionModel); } } response.Value.Add(blockModel); } stopwatch.Stop(); _logger.Information($"LitecoinAdapter.GetBlocksWithTransactions(cryptoAdapter: {cryptoAdapter.Id}, username:{username}, password:{password}, fromBlock: {fromBlock}, toBlock: {toBlock}, address: {address}). Time elapsed: {stopwatch.Elapsed.TotalSeconds} seconds."); response.Status = Common.Enums.StatusEnum.Success; } catch (Exception ex) { response.Status = Common.Enums.StatusEnum.Error; response.Message = ex.Message; _logger.Information($"LitecoinAdapter.GetBlocksWithTransactions(cryptoAdapter: {cryptoAdapter.Id}, username:{username}, password:{password}, fromBlock: {fromBlock}, toBlock: {toBlock}, address: {address}"); _logger.Error(ex.Message); } return(response); }
public async Task <IResponse <NoValue> > CreateCryptoAdapter(CryptoAdapterModel model, long userProfileId) { var result = new Response <NoValue>(); try { var connection = await TestConnection(model); if (connection.Status != StatusEnum.Success) { result.Status = StatusEnum.Error; result.Message = connection.Message; return(result); } Adapter newAdapter = new Adapter { Name = model.Name, AdapterTypeItemId = (int)model.NodeType, DirectionId = (int)model.Direction, UserProfileId = userProfileId, StatusId = (int)Statuses.Active }; _entity.Adapter.Add(newAdapter); CryptoAdapter cryptoAdapter = new CryptoAdapter { RpcAddr = model.RpcAddr, RpcPort = model.RpcPort.ToString(), AdapterId = newAdapter.Id }; _entity.CryptoAdapter.Add(cryptoAdapter); switch (model.Direction) { case DirectionEnum.Source: foreach (var prop in model.Properties.First().SourceProperties ?? new List <PropertyModel>()) { var cryptoAdapterProperty = new CryptoAdapterProperty { CryptoAdapter = cryptoAdapter, PropertyId = prop.Id, Value = prop.Value ?? String.Empty }; _entity.CryptoAdapterProperty.Add(cryptoAdapterProperty); } break; case DirectionEnum.Destination: foreach (var prop in model.Properties.First().DestinationProperties ?? new List <PropertyModel>()) { var cryptoAdapterProperty = new CryptoAdapterProperty { CryptoAdapter = cryptoAdapter, PropertyId = prop.Id, Value = prop.Value ?? String.Empty }; _entity.CryptoAdapterProperty.Add(cryptoAdapterProperty); } break; default: break; } _entity.SaveChanges(); result.Status = StatusEnum.Success; result.Message = "Connection test succeeded. Crypto adapter added successfully."; } catch (Exception ex) { result.Status = StatusEnum.Error; result.Message = Message.SomethingWentWrong; _logger.Information($"CryptoAdapterService.CreateCryptoAdapter(model: {model}, userProfileId: {userProfileId})"); _logger.Error(ex.Message); } return(result); }
public async Task <IResponse <CryptoAdapterModel> > UpdateCryptoAdapter(CryptoAdapterModel model) { var result = new Response <CryptoAdapterModel>(); try { var connection = await TestConnection(model); if (connection.Status != StatusEnum.Success) { result.Status = StatusEnum.Error; result.Message = connection.Message; return(result); } var cryptoAdapter = _entity.CryptoAdapter.Find(model.Id); var jobDefinition = _entity.JobDefinition.Any(jd => jd.From == cryptoAdapter.Adapter.Id || jd.To == cryptoAdapter.Adapter.Id); if (jobDefinition) { result.Status = StatusEnum.Error; result.Message = "Unable to update crypto adapter if it is used in job definition."; return(result); } var rpcPort = (model.RpcPort).ToString(); if (rpcPort == "") { rpcPort = null; } cryptoAdapter.Adapter.Name = model.Name; cryptoAdapter.Adapter.DirectionId = (int)model.Direction; cryptoAdapter.RpcAddr = model.RpcAddr; cryptoAdapter.Adapter.AdapterTypeItemId = (int)model.NodeType; cryptoAdapter.RpcPort = rpcPort; switch (model.Direction) { case DirectionEnum.Source: foreach (var prop in model.Properties.First().SourceProperties ?? new List <PropertyModel>()) { if (String.IsNullOrEmpty(prop.Value) && prop.PropertyType == PropertyTypeEnum.Password) { continue; } var cryptoAdapterProperty = _entity.CryptoAdapterProperty.Where(cap => cap.CryptoAdapterId == cryptoAdapter.Id && cap.PropertyId == prop.Id).SingleOrDefault(); cryptoAdapterProperty.Value = prop.Value ?? String.Empty; } break; case DirectionEnum.Destination: foreach (var prop in model.Properties.First().DestinationProperties ?? new List <PropertyModel>()) { if (String.IsNullOrEmpty(prop.Value) && prop.PropertyType == PropertyTypeEnum.Password) { continue; } var cryptoAdapterProperty = _entity.CryptoAdapterProperty.Where(cap => cap.CryptoAdapterId == cryptoAdapter.Id && cap.PropertyId == prop.Id).SingleOrDefault(); cryptoAdapterProperty.Value = prop.Value ?? String.Empty; } break; default: break; } _entity.SaveChanges(); result.Status = StatusEnum.Success; result.Message = Message.ChangesSaved; } catch (Exception ex) { result.Status = StatusEnum.Error; result.Message = Message.SomethingWentWrong; _logger.Information($"CryptoAdapterService.UpdateCryptoAdapter(model: {model})"); _logger.Error(ex.Message); } return(result); }
public async Task <List <TransactionResponse <BlockchainTransactionModel> > > GetTransactionsStatus(List <BlockchainTransactionModel> transactions, CryptoAdapterModel cryptoAdapter) { var web3 = InstantiateWeb3(cryptoAdapter); var response = new List <TransactionResponse <BlockchainTransactionModel> >(); foreach (var transaction in transactions) { var responseItem = new TransactionResponse <BlockchainTransactionModel>(); try { var transactionReceipt = await web3.Eth.Transactions.GetTransactionReceipt.SendRequestAsync(transaction.TxHash); var transactionStatus = transactionReceipt.Status?.Value; responseItem.Succeeded = true; var transactionStatusConverted = ConvertStatus(transactionStatus ?? default(BigInteger)); responseItem.Result = new BlockchainTransactionModel { TxHash = transaction.TxHash, TxStatus = transactionStatusConverted }; } catch (Exception ex) { responseItem.Succeeded = false; responseItem.Message = ex.Message; responseItem.Result = new BlockchainTransactionModel { TxHash = transaction.TxHash, TxStatus = TxStatus.None }; _logger.Information($"EthereumAdapter.GetTransactionsStatus(TransactionId: {transaction.Id}, CryptoAdapterId: {cryptoAdapter.Id})"); _logger.Error(ex.Message); } response.Add(responseItem); } return(response); }