Exemplo n.º 1
0
        public void SendDustAsFeeTest(
            BitcoinBasedConfig currency,
            decimal available,
            decimal amount,
            decimal fee,
            DustUsagePolicy dustUsagePolicy)
        {
            var change = available - amount - fee;

            var broadcastCallback = new Action <IBlockchainTransaction, int, int, CancellationToken>((tx, attempts, attemptsInterval, token) =>
            {
                var btcBasedTx = (IBitcoinBasedTransaction)tx;
                Assert.True(btcBasedTx.Fees == currency.CoinToSatoshi(fee + change));
            });

            var error = Send(
                currency: currency,
                available: available,
                amount: amount,
                fee: fee,
                dustUsagePolicy: dustUsagePolicy,
                apiSetup: apiMock =>
            {
                apiMock.Setup(a => a.TryBroadcastAsync(It.IsAny <IBlockchainTransaction>(), 3, 1000, CancellationToken.None))
                .Callback(broadcastCallback)
                .Returns(Task.FromResult(new Result <string>("<txid>")));
            },
                repositorySetup: (repositoryMock, fromAddress) =>
            {
                repositoryMock.Setup(r => r.GetWalletAddressAsync(It.IsAny <string>(), fromAddress.Address))
                .Returns(Task.FromResult(fromAddress));
            });

            Assert.Null(error);
        }
Exemplo n.º 2
0
        private Error Send(
            BitcoinBasedConfig currency,
            decimal available,
            decimal amount,
            decimal fee,
            DustUsagePolicy dustUsagePolicy,
            Action <Mock <IBlockchainApi> > apiSetup = null,
            Action <Mock <IAccountDataRepository>, WalletAddress> repositorySetup = null)
        {
            var apiMock = new Mock <IBlockchainApi>();

            apiSetup?.Invoke(apiMock);

            var wallet      = new HdWallet(Network.TestNet);
            var fromAddress = wallet.GetAddress(
                currency: currency,
                account: 0,
                chain: 0,
                index: 0,
                keyType: CurrencyConfig.StandardKey);
            var fromOutputs = GetOutputs(fromAddress.Address, NBitcoin.Network.TestNet, currency.CoinToSatoshi(available)).ToList();

            var repositoryMock = new Mock <IAccountDataRepository>();

            repositorySetup?.Invoke(repositoryMock, fromAddress);

            var currencies = Common.CurrenciesTestNet;

            currencies.GetByName(currency.Name).BlockchainApi = apiMock.Object;

            var account = new BitcoinBasedAccount(
                currency: currency.Name,
                currencies: currencies,
                wallet: wallet,
                dataRepository: repositoryMock.Object);

            return(account
                   .SendAsync(
                       from: fromOutputs,
                       to: currency.TestAddress(),
                       amount: amount,
                       fee: fee,
                       dustUsagePolicy: dustUsagePolicy)
                   .WaitForResult());
        }