public IResponse <List <BlockchainTransactionModel> > GetTransactionsWithoutHash(long jobId)
        {
            var response = new Response <List <BlockchainTransactionModel> >();
            var blockcahinTransactions = new List <BlockchainTransactionModel>();

            try
            {
                var transactions = _entity.Transaction.Where(t => t.JobTimelineId == jobId && t.TransactionHash == null);
                foreach (var transaction in transactions)
                {
                    var accountModel = new AccountModel()
                    {
                        CreditAddress = transaction.Account.CreditAddress,
                        DebitAddress  = transaction.Account.DebitAddress
                    };

                    var blockchainTransaction = new BlockchainTransactionModel()
                    {
                        Id        = transaction.Id,
                        Account   = accountModel,
                        Statement = (transaction.TransactionTypeId == 1) ? Common.Enums.Statement.Credit : Common.Enums.Statement.Debit,
                        TxStatus  = Common.Enums.TxStatus.Pending,
                        Value     = transaction.TransactionAmount
                    };
                    blockcahinTransactions.Add(blockchainTransaction);
                }
                response.Value  = blockcahinTransactions;
                response.Status = Common.Enums.StatusEnum.Success;
            }
            catch (Exception ex)
            {
                response.Status  = Common.Enums.StatusEnum.Error;
                response.Message = Message.SomethingWentWrong;
                _logger.Information($"TransactionService.GetTransactionsWithoutHash(jobId: {jobId})");
                _logger.Error(ex.Message);
            }

            return(response);
        }
        public async Task <IResponse <NoValue> > CheckTransactionsStatus()
        {
            var response = new Response <NoValue>();

            try
            {
                var pendingTransactions = _entity.Transaction.Where(t => t.TransactionStatus.Name == Common.Enums.TransactionStatus.Pending.ToString() && t.TransactionHash != null).ToList();
                if (pendingTransactions.Count() > 0)
                {
                    foreach (var transaction in pendingTransactions)
                    {
                        var blockchainTransaction = new BlockchainTransactionModel
                        {
                            TxHash = transaction.TransactionHash
                        };
                        var cryptoAdapterId = transaction.JobTimeline.Schedule.JobDefinition.Adapter1.CryptoAdapter.FirstOrDefault().Id;
                        var getTransactionStatusResponse = await _blockchainService.GetTransactionStatus(blockchainTransaction, (long)cryptoAdapterId);

                        if (getTransactionStatusResponse.Succeeded)
                        {
                            transaction.TransactionStatusId = (int)getTransactionStatusResponse.Result.TxStatus;
                        }
                    }
                }
                _entity.SaveChanges();
            }
            catch (Exception ex)
            {
                response.Message = Message.SomethingWentWrong;
                response.Status  = Common.Enums.StatusEnum.Error;
                _logger.Information($"TransactionService.CheckTransactionsStatus()");
                _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);
        }
예제 #4
0
        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);
        }
예제 #5
0
        public async Task <TransactionResponse <BlockchainTransactionModel> > GetTransactionStatus(BlockchainTransactionModel transaction, CryptoAdapterModel cryptoAdapter)
        {
            var web3 = InstantiateWeb3(cryptoAdapter);

            var response = new TransactionResponse <BlockchainTransactionModel>();

            try
            {
                var transactionReceipt = await web3.Eth.Transactions.GetTransactionReceipt.SendRequestAsync(transaction.TxHash);

                var transactionStatus = transactionReceipt.Status?.Value;

                response.Succeeded = true;
                var transactionStatusConverted = ConvertStatus(transactionStatus ?? default(BigInteger));

                response.Result = new BlockchainTransactionModel
                {
                    TxHash   = transaction.TxHash,
                    TxStatus = transactionStatusConverted
                };
            }
            catch (Exception ex)
            {
                response.Succeeded = false;
                response.Message   = ex.Message;

                response.Result = new BlockchainTransactionModel
                {
                    TxHash   = transaction.TxHash,
                    TxStatus = TxStatus.None
                };

                _logger.Information($"EthereumAdapter.GetTransactionStatus(TransactionId: {transaction.Id}, CryptoAdapterId: {cryptoAdapter.Id})");
                _logger.Error(ex.Message);
            }

            return(response);
        }