private async Task <List <ICoin> > FilterCoins(List <ICoin> coins) { var unspentOutputs = await _spentOutputRepository.GetUnspentOutputs(coins.Select(o => new Output(o.Outpoint))); var unspentSet = new HashSet <OutPoint>(unspentOutputs.Select(x => new OutPoint(uint256.Parse(x.TransactionHash), x.N))); return(coins.Where(o => unspentSet.Contains(o.Outpoint)).ToList()); }
// Not tested after NBitcoin version upgarde public async Task <IEnumerable <ICoin> > GetUnspentOutputs(string walletAddress, int confirmationsCount = 0) { var outputResponse = await _qBitNinjaApiCaller.GetAddressBalance(walletAddress); var coins = outputResponse.Operations .Where(x => x.Confirmations >= Math.Max(1, confirmationsCount)) .SelectMany(o => o.ReceivedCoins).ToList(); //get unique saved coins if (confirmationsCount == 0) { var internalSavedOutputs = (await _broadcastedOutputRepository.GetOutputs(walletAddress)) .Where(o => !coins.Any(c => c.Outpoint.Hash.ToString() == o.TransactionHash && c.Outpoint.N == o.N)); coins.AddRange(internalSavedOutputs.Select(o => { var coin = new Coin(new OutPoint(uint256.Parse(o.TransactionHash), o.N), new TxOut(new Money(o.Amount, MoneyUnit.Satoshi), o.ScriptPubKey.ToScript())); if (o.AssetId != null) { return ((ICoin) coin.ToColoredCoin(new BitcoinAssetId(o.AssetId, _connectionParams.Network).AssetId, (ulong)o.Quantity)); } return(coin); })); } var unspentOutputs = await _spentOutputRepository.GetUnspentOutputs(coins.Select(o => new Output(o.Outpoint))); coins = coins.Where(o => unspentOutputs.Any(un => un.N == o.Outpoint.N && un.TransactionHash == o.Outpoint.Hash.ToString())).ToList(); var address = BitcoinAddress.Create(walletAddress); if (address is BitcoinPubKeyAddress) { return(coins); } if (address is BitcoinScriptAddress) { var redeem = await _walletAddressRepository.GetRedeemScript(walletAddress); return(coins.OfType <Coin>().Select(x => new ScriptCoin(x, new Script(redeem))) .Concat( coins.OfType <ColoredCoin>().Select(x => new ScriptCoin(x, new Script(redeem)).ToColoredCoin(x.Amount)) .Cast <ICoin>())); } throw new NotImplementedException(); }
private async Task <List <ICoin> > FilterCoins(List <ICoin> coins, bool useInternalSpentOutputs, IPerformanceMonitor monitor) { monitor?.Step("Get unspent outputs"); var unspentOutputs = await _spentOutputRepository.GetUnspentOutputs(coins.Select(o => new Output(o.Outpoint))); var unspentSet = new HashSet <OutPoint>(unspentOutputs.Select(x => new OutPoint(uint256.Parse(x.TransactionHash), x.N))); if (useInternalSpentOutputs) { monitor?.Step("Get internal spent outputs"); var internalSpentOuputs = new HashSet <OutPoint>( (await _internalSpentOutputRepository.GetInternalSpentOutputs()).Select(x => new OutPoint(uint256.Parse(x.TransactionHash), x.N))); unspentSet.ExceptWith(internalSpentOuputs); } monitor?.Step("Filter ouputs"); return(coins.Where(o => unspentSet.Contains(o.Outpoint)).ToList()); }