Esempio n. 1
0
        private async Task <AddressBalance> GetBalance(string paymentAddress, bool includeTransactionIds, Stopwatch stopWatch = null)
        {
            statsGetAddressHistory[1] = stopWatch?.ElapsedMilliseconds ?? -1;

            using (var address = new PaymentAddress(paymentAddress))
                using (var getAddressHistoryResult = await chain_.FetchHistoryAsync(address, UInt64.MaxValue, 0))
                {
                    statsGetAddressHistory[2] = stopWatch?.ElapsedMilliseconds ?? -1;

                    Utils.CheckBitprimApiErrorCode(getAddressHistoryResult.ErrorCode, "FetchHistoryAsync(" + paymentAddress + ") failed, check error log.");

                    var history = getAddressHistoryResult.Result;

                    UInt64 received       = 0;
                    UInt64 addressBalance = 0;
                    var    txs            = new OrderedSet <string>();

                    foreach (IHistoryCompact compact in history)
                    {
                        if (compact.PointKind == PointKind.Output)
                        {
                            received += compact.ValueOrChecksum;

                            using (var outPoint = new OutputPoint(compact.Point.Hash, compact.Point.Index))
                            {
                                var getSpendResult = await chain_.FetchSpendAsync(outPoint);

                                if (getSpendResult.ErrorCode == ErrorCode.NotFound)
                                {
                                    addressBalance += compact.ValueOrChecksum;
                                }
                            }
                        }
                        txs.Add(Binary.ByteArrayToHexString(compact.Point.Hash));
                    }



                    statsGetAddressHistory[3] = stopWatch?.ElapsedMilliseconds ?? -1;

                    UInt64 totalSent = received - addressBalance;
                    return(new AddressBalance
                    {
                        Balance = addressBalance,
                        Received = received,
                        Sent = totalSent,
                        Transactions = includeTransactionIds ? txs : null,
                        TxCount = (uint)txs.Count
                    });
                }
        }
        private async Task SetOutputSpendInfo(TransactionOutputSummary jsonOutput, byte[] txHash, UInt32 index)
        {
            using (var outPoint = new OutputPoint(txHash, index))
            {
                var fetchSpendResult = await chain_.FetchSpendAsync(outPoint);

                if (fetchSpendResult.ErrorCode == ErrorCode.NotFound)
                {
                    jsonOutput.spentTxId   = null;
                    jsonOutput.spentIndex  = null;
                    jsonOutput.spentHeight = null;
                }
                else
                {
                    Utils.CheckBitprimApiErrorCode(fetchSpendResult.ErrorCode, "FetchSpendAsync failed, check error log");
                    var spend = fetchSpendResult.Result;
                    jsonOutput.spentTxId  = Binary.ByteArrayToHexString(spend.Hash);
                    jsonOutput.spentIndex = spend.Index;
                    using (var getTxResult = await chain_.FetchTransactionAsync(spend.Hash, false))
                    {
                        Utils.CheckBitprimApiErrorCode(getTxResult.ErrorCode, "FetchTransactionAsync(" + Binary.ByteArrayToHexString(spend.Hash) + "), check error log");
                        jsonOutput.spentHeight = getTxResult.Result.TxPosition.BlockHeight;
                    }
                }
            }
        }