/// <summary> /// ATM calls this service to send BTC /// </summary> /// <param name="address"></param> /// <param name="btcAmount"></param> /// <returns></returns> public DC.Common.Models.Order CreateOrder(String address, Decimal btcAmount) { if (btcAmount > 0 && BitcoinHelper.IsValidAddress(address)) { //Providers.IWallet wallet = Helpers.Factory.GetWallet(); Providers.IWallet wallet = new Providers.BlockChainInfo() { MerchantId = "4ea10592-e7cb-4170-9b5b-6e94e3236bb1", Password = "******" }; //ADD YOUR BLOCK CHAIN INFO CREDS HERE! //Move to a factory to spit out order model. DC.Common.Models.Order order = new DC.Common.Models.Order() { Address = address, BTC = btcAmount }; order.Txn = wallet.Send(address, btcAmount); return(order); } else { throw new ArgumentOutOfRangeException("Check the bitcoin address is valid and amount is greater than zero."); } }
/// <summary> /// ATM calls this service to send BTC /// </summary> /// <param name="address"></param> /// <param name="btcAmount"></param> /// <returns></returns> public DC.Common.Models.Order CreateOrder(String address, Decimal btcAmount) { if (btcAmount > 0 && BitcoinHelper.IsValidAddress(address)) { // Buy Bitcoin- https://blockchain.info/address/1QJQMFhgyoiLLUZXpr913T2TaEaX7pNFaF DigitalBTC digitalbtc = new DigitalBTC(); PriceData pricedata = digitalbtc.GetSpotPrice2(btcAmount, "USD"); if (pricedata.Price > 0) { pricedata.Price += 1M; float price = (float)pricedata.Price; float btcorderamount = (float)btcAmount; OrderData orderdata = digitalbtc.Order(price, btcorderamount); } // Sell Bitcoin //Providers.IWallet wallet = Helpers.Factory.GetWallet(); Providers.IWallet wallet = new Providers.BlockChainInfo() { MerchantId = "4ea10592-e7cb-4170-9b5b-6e94e3236bb1", Password = "******" }; //ADD YOUR BLOCK CHAIN INFO CREDS HERE! //Move to a factory to spit out order model. DC.Common.Models.Order order = new DC.Common.Models.Order() { Address = address, BTC = btcAmount }; order.Txn = wallet.Send(address, btcAmount); return(order); } else { throw new ArgumentOutOfRangeException("Check the bitcoin address is valid and amount is greater than zero."); } }
/// <summary> /// Sign and send bitcoins using bitcoin core /// </summary> /// <param name="privateKey">Private key as decimal</param> /// <param name="sourceAddress">Bitcoin address to receive change</param> /// <param name="destinationAddress"></param> /// <param name="amount"></param> private String SignAndTransfer(String privateKey, String destinationAddress, Decimal amount) { DC.Common.Models.Address sourceAddress = new Common.Models.Address(privateKey); DC.Common.Models.UnspentResponse utxoResponse = Helpers.Factory.GetTransactionProvider().GetUnspentTxns (sourceAddress.BTCAddress); Decimal fee = Convert.ToDecimal(ConfigurationManager.AppSettings["fee"]); fee = 0.0001M; //Get unspent txs, largest to smallest IEnumerable <DC.Common.Models.UnspentOutput> outputs = (from u in utxoResponse.UnspentOutputs where u.Confirmations > 0 select u).OrderByDescending(u => u.Value); //There needs to be sufficient balance for the amount plus the fee Int64 target = Convert.ToInt64((amount + fee) * DC.Common.Math.SATOSHI); if (outputs.Count() > 0) { bool sufficientFunds = false; IList <DC.Common.Models.UnspentOutput> outputsToSpend = new List <Common.Models.UnspentOutput>(); foreach (DC.Common.Models.UnspentOutput output in outputs) { outputsToSpend.Add(output); Int64 i = outputsToSpend.Sum(o => o.Value); if (i >= target) { sufficientFunds = true; break; } } if (!sufficientFunds) { string message; if (outputsToSpend.Sum(o => o.Value) == amount * DC.Common.Math.SATOSHI) { message = "There are insufficient funds to cover the transaction amount plus the bitcoin miner fee of 0.0001 Bitcoin"; } else { message = "There are insufficient funds to complete this transaction"; } throw new InvalidOperationException(message); } DC.Providers.IAddress provider = new DC.Providers.BlockChainInfo(); String destinationHash160 = provider.GetHash160(destinationAddress); Helpers.Transaction Transaction = new Helpers.Transaction(outputsToSpend, sourceAddress, destinationAddress, destinationHash160, amount, fee); Byte[] signedTransaction = Transaction.CreateAndSignTransaction(sourceAddress, destinationAddress, destinationHash160, amount, fee); String signature = DC.Common.StringHelper.ByteArrayToHexString(signedTransaction); // Providers.IWallet wallet = Helpers.Factory.GetWallet(); Providers.IWallet wallet = new Providers.BlockChainInfo() { MerchantId = "4ea10592-e7cb-4170-9b5b-6e94e3236bb1", Password = "******" }; //ADD YOUR BLOCK CHAIN INFO CREDS HERE! String txnHash = wallet.SendRawTransaction(signature); return(txnHash); } else { throw new ArgumentException("The address does not have any unspent transactions"); } }