Beispiel #1
0
        /// <summary>
        /// Adds a new transaction to the database.
        /// </summary>
        /// <param name="tx">The transaction to add.</param>
        /// <param name="blockIndex">Index of the block containing the given transaction.</param>
        /// <param name="positionIndex">Index of the transaction in the block.</param>
        /// <param name="needSave">If true, the database will be saved immediately.</param>
        /// <returns>Index object for the given transaction.</returns>
        /// <remarks>
        /// In scenarios when there are multiple calls to this method,
        /// passing false to <paramref name="needSave"/> and then calling
        /// <see cref="Save"/> in the end would be a more efficient approach.
        /// </remarks>
        public TransactionIndex AddTransaction(Transaction tx, int blockIndex, int positionIndex, bool needSave = true)
        {
            if (_transactions.ContainsKey(tx.Id) || _transactionIndices.ContainsKey(tx.Id))
            {
                throw new InvalidOperationException("Duplicate transaction ID");
            }

            var txIndex = new TransactionIndex
            {
                Id            = tx.Id,
                BlockIndex    = blockIndex,
                PositionIndex = positionIndex,
            };

            _transactionIndices.Add(tx.Id, txIndex);
            _transactions.Add(tx.Id, tx);

            for (int i = 0, c = tx.Inputs.Count; i < c; ++i)
            {
                var txIn = tx.Inputs[i];
                spendTxOut(txIn.TxId, txIn.TxOutIndex);
            }

            for (int i = 0, c = tx.Outputs.Count; i < c; ++i)
            {
                var txOut = tx.Outputs[i];
                var utxo  = new UnspentTxOut
                {
                    TxId       = tx.Id,
                    TxOutIndex = i,
                    Address    = txOut.Address,
                };
                _unspentTxOuts.Add(utxo);
            }

            for (int i = 0, c = _pendingTransactions.Count; i < c; ++i)
            {
                var ptx = _pendingTransactions[i];
                if (ptx.Tx.Id == tx.Id)
                {
                    _pendingTransactions.RemoveAt(i);
                    break;
                }
            }

            if (needSave)
            {
                Save();
            }

            return(txIndex);
        }
Beispiel #2
0
 public object ToJson()
 => new
 {
     removed          = Removed,
     logIndex         = LogIndex?.ToJson(),
     transactionIndex = TransactionIndex?.ToJson(),
     transactionHash  = TransactionHash?.ToJson(),
     blockHash        = BlockHash?.ToJson(),
     blockNumber      = BlockNumber?.ToJson(),
     address          = Address?.ToJson(),
     data             = Data?.ToJson(),
     topics           = Topics?.Select(t => t.ToJson()).ToArray()
 };
 public object ToJson()
 {
     return(new
     {
         transactionHash = TransactionHash?.ToJson(),
         transactionIndex = TransactionIndex?.ToJson(),
         blockHash = BlockHash?.ToJson(),
         blockNumber = BlockNumber?.ToJson(),
         cumulativeGasUsed = CumulativeGasUsed?.ToJson(),
         gasUsed = GasUsed?.ToJson(),
         contractAddress = ContractAddress?.ToJson(),
         logs = Logs?.Select(x => x.ToJson()).ToArray(),
         logsBloom = LogsBloom?.ToJson(),
         root = Root?.ToJson(),
         status = Status?.ToJson()
     });
 }
Beispiel #4
0
 public object ToJson()
 {
     return(new
     {
         hash = Hash.ToJson(),
         nonce = Nonce.ToJson(),
         blockHash = BlockHash.ToJson(),
         blockNumber = BlockNumber.ToJson(),
         transactionIndex = TransactionIndex.ToJson(),
         from = From.ToJson(),
         to = To.ToJson(),
         value = Value.ToJson(),
         gasPrice = GasPrice.ToJson(),
         gas = Gas.ToJson(),
         input = Data.ToJson(),
     });
 }
        public async Task <int> Handle(CreateTransactionCommand request, CancellationToken cancellationToken)
        {
            if (!_context.CartIndexs.Any(x => x.Id == request.CartIndexId))
            {
                throw new NotFoundException(nameof(CartIndex), request.CartIndexId);
            }

            if (!_context.CartIndexs.Any(x => x.PaymentId > 0))
            {
                throw new NotFoundException("Payment still not been choose");
            }

            if (_context.CartIndexs.Any(x => x.ShipmentId == 0))
            {
                throw new NotFoundException("Shipment still not been choose");
            }

            var indexAsset = await _context.CartIndexs.FindAsync(request.CartIndexId);

            //Create TransactionIndex
            var transactionIndexEntity = new TransactionIndex
            {
                UserPropertyId = indexAsset.UserPropertyId,
                PaymentId      = indexAsset.PaymentId,
                ShipmentId     = indexAsset.ShipmentId,
                StoreId        = indexAsset.StoreId,
                Status         = Status.WaitingForPayment
            };

            _context.TransactionIndexs.Add(transactionIndexEntity);
            await _context.SaveChangesAsync(cancellationToken);

            //Moving Cart to Transaction Method
            var count = _context.Carts
                        .Where(x => x.CartIndexId == indexAsset.Id)
                        .Count();

            var index = _context.TransactionIndexs
                        .Where(x => x.UserPropertyId == indexAsset.UserPropertyId)
                        .OrderByDescending(x => x.Id)
                        .FirstOrDefault();

            //Loop for multiple data
            while (count > 0)
            {
                //Getting Cart Asset
                var assetCart = await _context.Carts
                                .Where(x => x.CartIndexId == indexAsset.Id)
                                .FirstOrDefaultAsync();

                var assetProduct = await _context.Products
                                   .Where(x => x.Id == assetCart.ProductId)
                                   .FirstOrDefaultAsync();

                //Add entity Transaction Table
                var transactionEntity = new Transaction
                {
                    ProductId          = assetCart.ProductId,
                    ProductName        = assetProduct.Name,
                    ImageUrl           = assetProduct.ImageUrl,
                    Description        = assetProduct.Description,
                    Price              = assetProduct.Price,
                    Quantity           = assetCart.Quantity,
                    TotalPrice         = assetCart.TotalPrice,
                    TransactionIndexId = index.Id,
                };

                _context.Transactions.Add(transactionEntity);

                //Removing Cart
                _context.Carts.Remove(assetCart);

                await _context.SaveChangesAsync(cancellationToken);

                count--;
            }

            _context.CartIndexs.Remove(indexAsset);
            await _context.SaveChangesAsync(cancellationToken);

            return(1);
        }