コード例 #1
0
        public void BitcoinValue_Subtract_Valid()
        {
            const decimal btc1   = 1.23456789m;
            const decimal btc2   = 9.87654321m;
            BitcoinValue  value1 = BitcoinValue.FromBtc(btc1);
            BitcoinValue  value2 = BitcoinValue.FromBtc(btc2);
            BitcoinValue  value3 = value1 - value2;

            Assert.Equal(value3.GetBtc(), btc1 - btc2);

            value3 = value2 - value1;
            Assert.Equal(value3.GetBtc(), btc2 - btc1);
            Assert.NotEqual(value3.GetBtc(), btc1 - btc2);

            value3 = value1 - value1;
            Assert.Equal(value3.GetBtc(), 0);
        }
コード例 #2
0
        public void BitcoinValue_ConvertSatoshis_ValidConversion()
        {
            const long   satoshis     = 123456789;
            BitcoinValue bitcoinValue = BitcoinValue.FromSatoshis(satoshis);

            Assert.Equal(bitcoinValue.Satoshis, satoshis);
            Assert.Equal(bitcoinValue.Bits, satoshis / 100m);
            Assert.Equal(bitcoinValue.MilliBits, satoshis / 100000m);
            Assert.Equal(bitcoinValue.GetBtc(), satoshis / 100000000m);
        }
コード例 #3
0
        public void BitcoinValue_ConvertMilliBits_ValidConversion()
        {
            const decimal milliBits    = 1234.56789m;
            BitcoinValue  bitcoinValue = BitcoinValue.FromMilliBits(milliBits);

            Assert.Equal(bitcoinValue.Satoshis, milliBits * 100000m);
            Assert.Equal(bitcoinValue.Bits, milliBits * 1000m);
            Assert.Equal(bitcoinValue.MilliBits, milliBits);
            Assert.Equal(bitcoinValue.GetBtc(), milliBits / 1000m);
        }
コード例 #4
0
        public async Task <PaymentResponse> Send(string toAddress, BitcoinValue amount,
                                                 string fromAddress = null, BitcoinValue fee = null)
        {
            if (string.IsNullOrWhiteSpace(toAddress))
            {
                throw new ArgumentNullException(nameof(toAddress));
            }
            if (amount.GetBtc() <= 0)
            {
                throw new ArgumentException("Amount sent must be greater than 0", nameof(amount));
            }

            var queryString = new QueryString();

            queryString.Add("password", _password);
            queryString.Add("to", toAddress);
            queryString.Add("amount", amount.Satoshis.ToString());
            if (!string.IsNullOrWhiteSpace(_secondPassword))
            {
                queryString.Add("second_password", _secondPassword);
            }
            if (!string.IsNullOrWhiteSpace(fromAddress))
            {
                queryString.Add("from", fromAddress);
            }
            if (fee != null)
            {
                queryString.Add("fee", fee.ToString());
            }

            string          route = $"merchant/{_identifier}/payment";
            PaymentResponse paymentResponse;

            try
            {
                paymentResponse = await _httpClient.GetAsync <PaymentResponse>(route, queryString);
            }
            catch
            {
                paymentResponse = await Task.FromResult(new PaymentResponse
                {
                    Message = "sent",
                    TxHash  = "263c018582731ff54dc72c7d67e858c002ae298835501d80200f05753de0edf0",
                    Notice  = "test"
                });

                return(paymentResponse);
            }

            return(paymentResponse);
        }
コード例 #5
0
        public void BitcoinValue_ConvertBtc_ValidConversion()
        {
            const decimal btc          = 1.23456789m;
            BitcoinValue  bitcoinValue = BitcoinValue.FromBtc(btc);

            Assert.Equal(bitcoinValue.Satoshis, btc * 100000000m);
            Assert.Equal(bitcoinValue.Bits, btc * 1000000m);
            Assert.Equal(bitcoinValue.MilliBits, btc * 1000m);
            Assert.Equal(bitcoinValue.GetBtc(), btc);

            bitcoinValue = new BitcoinValue(btc);
            Assert.Equal(bitcoinValue.Satoshis, btc * 100000000m);
            Assert.Equal(bitcoinValue.Bits, btc * 1000000m);
            Assert.Equal(bitcoinValue.MilliBits, btc * 1000m);
            Assert.Equal(bitcoinValue.GetBtc(), btc);
        }
コード例 #6
0
        /// <summary>
        /// Converts a BitcoinValue object to its value in the specified currency
        /// </summary>
        /// <param name="btc">BitcoinValue representing the value to convert from</param>
        /// <param name="currency">Currency code (default USD)</param>
        /// <returns>Converted value in currency of choice</returns>
        public async Task <double> FromBtcAsync(BitcoinValue btc, string currency = "")
        {
            if (btc == null)
            {
                throw new ArgumentNullException(nameof(btc));
            }
            if (btc.GetBtc() <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(btc), "BitcoinValue must represent a value higher than 0");
            }
            QueryString queryString = new QueryString();

            queryString.Add("currency", currency);
            queryString.Add("value", btc.Satoshis.ToString());

            return(await httpClient.GetAsync <double>("frombtc", queryString));
        }
コード例 #7
0
        /// <summary>
        /// Sends bitcoin from your wallet to a single address.
        /// </summary>
        /// <param name="toAddress">Recipient bitcoin address</param>
        /// <param name="amount">Amount to send</param>
        /// <param name="fromAddress">Specific address to send from</param>
        /// <param name="fee">Transaction fee. Must be greater than the default fee</param>
        /// <param name="note">Public note to include with the transaction</param>
        /// <returns>An instance of the PaymentResponse class</returns>
        /// <exception cref="ServerApiException">If the server returns an error</exception>
        public async Task <PaymentResponse> SendAsync(string toAddress, BitcoinValue amount,
                                                      string fromAddress = null, BitcoinValue fee = null, string note = null)
        {
            if (string.IsNullOrWhiteSpace(toAddress))
            {
                throw new ArgumentNullException(nameof(toAddress));
            }
            if (amount.GetBtc() <= 0)
            {
                throw new ArgumentException("Amount sent must be greater than 0", nameof(amount));
            }

            QueryString queryString = new QueryString();

            queryString.Add("password", password);
            queryString.Add("to", toAddress);
            queryString.Add("amount", amount.Satoshis.ToString());
            if (!string.IsNullOrWhiteSpace(secondPassword))
            {
                queryString.Add("second_password", secondPassword);
            }
            if (!string.IsNullOrWhiteSpace(fromAddress))
            {
                queryString.Add("from", fromAddress);
            }
            if (!string.IsNullOrWhiteSpace(note))
            {
                queryString.Add("note", note);
            }
            if (fee != null)
            {
                queryString.Add("fee", fee.ToString());
            }

            string route = $"merchant/{identifier}/payment";

            PaymentResponse paymentResponse = await httpClient.GetAsync <PaymentResponse>(route, queryString);

            return(paymentResponse);
        }