Пример #1
0
        public string GetBalance(string address, string tag)
        {
            var addressUint160 = address.HexToUInt160();

            if (tag == "pending")
            {
                // Get all transaction from pool
                var transactions = _transactionPool.Transactions;

                List <TransactionReceipt> txReceipts = new List <TransactionReceipt>();

                foreach (var tx in transactions)
                {
                    var from = tx.Value.Transaction.From.ToHex();

                    if (address == from)
                    {
                        txReceipts.Add(tx.Value);
                    }
                }

                // Sort on the basis of nonce
                txReceipts = txReceipts.OrderBy(receipt => receipt, new ReceiptComparer()).ToList();

                // Get current address nonce
                var transactionRepository = _stateManager.CurrentSnapshot.Transactions;
                var currNonce             = transactionRepository.GetTotalTransactionCount(addressUint160);

                // Virtually execute the txs in nonce order
                var availableBalance = GetSnapshotByTag("latest") !.Balances.GetBalance(addressUint160);

                foreach (var tx in txReceipts)
                {
                    var from = tx.Transaction.From.ToHex();

                    if (currNonce == tx.Transaction.Nonce)
                    {
                        // Executing the transaction
                        var gasp   = new Money(tx.Transaction.GasPrice);
                        var gasl   = new Money(tx.Transaction.GasLimit);
                        var txamnt = new Money(tx.Transaction.Value);

                        if (availableBalance - txamnt - (gasl * gasp) >= Money.Parse("0"))
                        {
                            // If transaction can be executed
                            availableBalance = availableBalance - txamnt - (gasl * gasp);
                            currNonce       += 1;
                        }
                        else if (availableBalance - (gasl * gasp) >= Money.Parse("0"))
                        {
                            // If balance is not enough for transaction
                            availableBalance = availableBalance - (gasl * gasp);
                            currNonce       += 1;
                        }
                    }
                }

                return(Web3DataFormatUtils.Web3Number(availableBalance.ToWei().ToUInt256()));
            }
            else
            {
                var availableBalance = GetSnapshotByTag(tag) !.Balances.GetBalance(addressUint160);
                return(Web3DataFormatUtils.Web3Number(availableBalance.ToWei().ToUInt256()));
            }
        }