Exemplo n.º 1
0
        public async Task <UnspentTxOutput[]> GetMempoolUtxos(string address)
        {
            var transactions = await _dcrdClient.SearchRawTransactions(address,
                                                                       count : 100,
                                                                       reverse : true);

            // Check if an outpoint is the input to another known transaction.
            bool IsSpent(string txId, TxVout txOut) =>
            transactions.SelectMany(tx => tx.vin)
            .Any(vin => vin.txid == txId && vin.vout == txOut.n);

            // Filter out transactions that have a spent outpoint
            // Only grab transactions that spend to the provided address.
            return((
                       from transaction in transactions
                       where transaction.confirmations == 0
                       from txOut in transaction.vout
                       where txOut.scriptPubKey.addresses.Contains(address)
                       where !IsSpent(transaction.txid, txOut)
                       select new UnspentTxOutput
            {
                BlockHeight = 0,
                BlockIndex = 4294967295,
                Hash = transaction.txid,
                OutputIndex = (uint)txOut.n,
                OutputValue = (long)(txOut.value * (decimal)Math.Pow(10, 8)),
                OutputVersion = txOut.version,
                PkScript = HexUtil.ToByteArray(txOut.scriptPubKey.hex),
                Tree = 0
            }).ToArray());
        }
Exemplo n.º 2
0
        private async Task <SearchRawTransactionsResult[]> GetMempoolUtxosInternal(string address)
        {
            int skip     = 0;
            int pageSize = 100;
            var results  = new List <SearchRawTransactionsResult>();

            while (true)
            {
                try
                {
                    // Paginate through the results.
                    var response = await _dcrdClient.SearchRawTransactions(
                        address,
                        skip : skip,
                        count : pageSize,
                        reverse : true
                        );

                    // If no results, break.
                    if (response.Result == null || response.Result.Length == 0)
                    {
                        break;
                    }

                    // Only select unconfirmed transactions.
                    // If there are less than 100 unconfirmed transactions, then
                    //   we've started to retrieve records that are out of the mempool
                    //   and we can break.
                    var unconfirmed = response.Result.Where(r => r.Confirmations == 0).ToList();
                    results.AddRange(unconfirmed);
                    if (unconfirmed.Count < pageSize)
                    {
                        break;
                    }

                    skip += response.Result.Length;
                }
                catch (DcrdException)
                {
                    break;
                }
            }

            return(results.ToArray());
        }