示例#1
0
        public static async Task <Fee> GetOneFeeCoin()
        {
            using (BlockchainStateManagerContext context = new BlockchainStateManagerContext())
            {
                var selectedFee = (from f in context.Fees
                                   where f.Consumed == false
                                   select f).FirstOrDefault();

                if (selectedFee == null)
                {
                    throw new Exception("No proper fee was found.");
                }

                selectedFee.Consumed = true;

                await context.SaveChangesAsync();

                return(selectedFee);
            }
        }
示例#2
0
        public async Task <Error> GenerateFees(BitcoinSecret sourceSecret, BitcoinSecret destinationSecret, int feeCount)
        {
            var feeAmount = Settings.Constants.BTCToSathoshiMultiplicationFactor / 100;
            var coins     = await explorerHelper.GetCoinsForWallet(sourceSecret.GetAddress().ToString(), 10, 0, null, null,
                                                                   null, true) as GetOrdinaryCoinsForWalletReturnType;

            if (coins.Error == null)
            {
                var selectedCoin = coins.Coins.Where(c => c.Amount >= (ulong)feeCount
                                                     * Settings.Constants.BTCToSathoshiMultiplicationFactor).FirstOrDefault();
                if (selectedCoin == null)
                {
                    Error retError = new Error();
                    retError.Message = "Could not find the proper coin to spend.";
                    return(retError);
                }

                try
                {
                    TransactionBuilder builder = new TransactionBuilder();
                    builder.AddKeys(sourceSecret).AddCoins(selectedCoin);
                    for (int i = 0; i < feeCount; i++)
                    {
                        builder.Send(destinationSecret.GetAddress(),
                                     new Money(feeAmount));
                    }
                    builder.SetChange(sourceSecret.GetAddress());
                    builder.SendFees(new Money(feeCount * 100000));

                    var tx = builder.BuildTransaction(true);
                    await transactionBroadcaster.BroadcastTransactionToBlockchain(tx.ToHex());

                    using (BlockchainStateManagerContext context = new BlockchainStateManagerContext())
                    {
                        IList <Fee> fees   = new List <Fee>();
                        var         txHash = tx.GetHash().ToString();
                        for (uint i = 0; i < feeCount; i++)
                        {
                            if (tx.Outputs[i].Value != feeAmount)
                            {
                                continue;
                            }

                            fees.Add(new Fee
                            {
                                Consumed      = false,
                                TransactionId = txHash,
                                OutputNumber  = (int)i,
                                Satoshi       = (long)feeAmount,
                                PrivateKey    = destinationSecret.ToString(),
                                Script        = tx.Outputs[i].ScriptPubKey.ToString()
                            });
                        }
                        context.Fees.AddRange(fees);

                        await context.SaveChangesAsync();
                    }
                }
                catch (Exception exp)
                {
                    Error retError = new Error();
                    retError.Message = string.Format("An exception occured {0}.", exp.ToString());
                    return(retError);
                }

                return(null); // No error
            }

            return(coins.Error);
        }