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 }); } }