Esempio n. 1
0
        /// <summary>	Refund bitshares deposit. </summary>
        ///
        /// <remarks>	Paul, 15/01/2015. </remarks>
        ///
        /// <param name="fromAccount">	from account. </param>
        /// <param name="deposit">	    The deposit. </param>
        /// <param name="depositId">    Identifier for the deposit. </param>
        /// <param name="memo">		    The memo. </param>
        protected virtual void RefundBitsharesDeposit(string fromAccount, ulong larimers, string depositId, string memo)
        {
            // make sure failures after this point don't result in multiple refunds
            MarkTransactionAsRefundedStart(depositId);

            BitsharesTransactionResponse response;
            decimal amount = m_asset.GetAmountFromLarimers(larimers);

            try
            {
                BitsharesAccount account = GetAccountFromLedger(fromAccount);
                response = m_bitshares.WalletTransfer(amount, m_asset.symbol, m_bitsharesAccount, fromAccount, memo);
            }
            catch (BitsharesRpcException)
            {
                BitsharesTransaction t = m_bitshares.BlockchainGetTransaction(depositId);

                // get the sender's address from the balance id
                BitsharesOperation op = t.trx.operations.First(o => o.type == BitsharesTransactionOp.withdraw_op_type);

                BitsharesBalanceRecord balance = m_bitshares.GetBalance(op.data.balance_id);
                string senderAddress           = balance.condition.data.owner;


                response = m_bitshares.WalletTransferToAddress(amount, m_asset.symbol, m_bitsharesAccount, senderAddress, memo);
            }

            MarkTransactionAsRefundedEnd(depositId, response.record_id, amount, DaemonTransactionType.bitsharesRefund, memo);
        }
Esempio n. 2
0
        /// <summary>	Calculates the inventory ratio. </summary>
        ///
        /// <remarks>	Paul, 25/02/2015. </remarks>
        ///
        /// <param name="allBitsharesBalances">	all bitshares balances. </param>
        /// <param name="bitcoinBalance">	    The bitcoin balance. </param>
        ///
        /// <returns>	The calculated inventory ratio. </returns>
        decimal ComputeInventoryRatio(Dictionary <int, ulong> allBitsharesBalances, decimal bitcoinBalance)
        {
            decimal bitsharesBalance = m_asset.GetAmountFromLarimers(allBitsharesBalances[m_asset.id]);

            // convert bitshares into bitcoin value
            bitsharesBalance *= m_lastFeedPrice;

            decimal inventoryRatio = (bitcoinBalance - bitsharesBalance) / (bitsharesBalance + bitcoinBalance);

            return(inventoryRatio / 2 + 0.5M);
        }
Esempio n. 3
0
        /// <summary>	Refund bitshares deposit. </summary>
        ///
        /// <remarks>	Paul, 05/02/2015. </remarks>
        ///
        /// <param name="fromAccount">	from account. </param>
        /// <param name="larimers">     The larimers. </param>
        /// <param name="depositId">    Identifier for the deposit. </param>
        /// <param name="memo">		    The memo. </param>
        /// <param name="asset">	    The asset. </param>
        protected void RefundBitsharesDeposit(string fromAccount, ulong larimers, string depositId, string memo, BitsharesAsset asset, string depositAddress, MetaOrderType orderType)
        {
            decimal amount = asset.GetAmountFromLarimers(larimers);

            // make sure failures after this point don't result in multiple refunds
            m_daemon.MarkTransactionAsRefundedStart(depositId, depositAddress, m_market.symbol_pair, orderType);

            BitsharesTransactionResponse response = SendBitAssets(amount, asset, fromAccount, memo, false);

            //BitsharesTransactionResponse response = m_bitshares.WalletTransfer(amount, asset.symbol, m_bitsharesAccount, fromAccount, memo);

            m_daemon.MarkTransactionAsRefundedEnd(depositId, response.record_id, MetaOrderStatus.refunded, amount, memo);
        }
Esempio n. 4
0
        /// <summary>	Sends the bitcoins to depositor. </summary>
        ///
        /// <remarks>	Paul, 05/02/2015. </remarks>
        ///
        /// <exception cref="RefundBitsharesException">	Thrown when a Refund Bitshares error condition
        ///                                             occurs. </exception>
        ///
        /// <param name="btcAddress">	The btc address. </param>
        /// <param name="trxId">	    Identifier for the trx. </param>
        /// <param name="amount">	    The amount. </param>
        /// <param name="asset">	    The asset. </param>
        ///
        /// <returns>	A string. </returns>
        protected virtual string SendBitcoinsToDepositor(string btcAddress, string trxId, ulong amount, BitsharesAsset asset,
                                                         string depositAddress, MetaOrderType orderType, bool burnUia)
        {
            // make sure failures after this point dont result in multiple credits
            m_daemon.MarkDespositAsCreditedStart(trxId, depositAddress, m_market.symbol_pair, orderType);

            decimal bitAssetAmount = asset.GetAmountFromLarimers(amount);

            if (bitAssetAmount > m_market.bid_max)
            {
                throw new RefundBitsharesException("Over " + Numeric.SerialisedDecimal(m_market.bid_max) + " " + asset.symbol + "!");
            }

            // get the BTC amount we need to transfer
            decimal btcNoFee;

            if (m_flipped)
            {
                // they're sending us bitAssets, not BTC because the market is flipped, this is
                // equivelent to the opposite order type, so we have to use ask here
                btcNoFee = bitAssetAmount / m_market.ask;
            }
            else
            {
                btcNoFee = bitAssetAmount * m_market.bid;
            }

            // when selling, the fee is charged in BTC,
            // the amount recorded in the transaction is the amount of bitAssets sans fee, obv

            decimal fee      = (m_market.bid_fee_percent / 100) * btcNoFee;
            decimal btcTotal = Numeric.TruncateDecimal(btcNoFee - fee, 8);

            // do the transfer
            string txid = m_bitcoin.SendToAddress(btcAddress, btcTotal, "mX: " + orderType + " " + asset.symbol);

            // mark this in our records
            m_daemon.MarkDespositAsCreditedEnd(trxId, txid, MetaOrderStatus.completed, bitAssetAmount, m_market.bid, fee);

            if (burnUia)
            {
                // make sure we were the issuer for this asset before we start burning it!
                BitsharesAccount account = m_bitshares.WalletGetAccount(m_bitsharesAccount);
                if (asset.issuer_id == account.id)
                {
                    m_bitshares.WalletBurn(bitAssetAmount, asset.symbol, m_bitsharesAccount, BurnForOrAgainst.@for, m_bitsharesAccount);
                }
            }

            return(txid);
        }
Esempio n. 5
0
        /// <summary>	Calculates the market prices and limits. </summary>
        ///
        /// <remarks>	Paul, 05/02/2015. </remarks>
        ///
        /// <param name="market">				[in,out] The market. </param>
        /// <param name="bitsharesBalances">	The bitshares balances. </param>
        /// <param name="bitcoinBalance">       The bitcoin balance. </param>
        public override void ComputeMarketPricesAndLimits(ref MarketRow market,
                                                          Dictionary <int, ulong> bitsharesBalances,
                                                          decimal bitcoinBalance)
        {
            base.ComputeMarketPricesAndLimits(ref market, bitsharesBalances, bitcoinBalance);

            decimal baseBalance  = 0;
            decimal quoteBalance = bitcoinBalance;

            if (bitsharesBalances.ContainsKey(m_asset.id))
            {
                // only non zero balances return data, so this guard is necessary
                baseBalance = m_asset.GetAmountFromLarimers(bitsharesBalances[m_asset.id]);
            }

            decimal maxTransactionFactor;

            if (m_currency.uia)
            {
                // with UIA we got to handle the maximum buy size differently
                BitsharesAccount account = m_bitshares.WalletGetAccount(m_bitsharesAccount);
                if (m_asset.issuer_id == account.id)
                {
                    // we are the issuer!

                    // refresh the asset
                    m_asset = m_bitshares.BlockchainGetAsset(m_asset.symbol);

                    // this is how much we can issue, so lets stick that in there
                    baseBalance = m_asset.GetAmountFromLarimers(m_asset.max_supply - m_asset.current_supply);
                }
                else
                {
                    throw new UnexpectedCaseException();
                }

                maxTransactionFactor = 1;
                //maxTransactionFactor = kMaxTransactionFactor;
            }
            else
            {
                maxTransactionFactor = kMaxTransactionFactor;
            }

            decimal newAskMax, newBidMax;

            // askMax is in BITCOINS
            // bidMax is in BITASSETS

            if (m_flipped)
            {
                // BTC_bitUSD

                // baseBalance = 10 bitUSD
                // ask = 240
                // askMax = 10 / 240 = 0.04 BTC

                newAskMax = Numeric.TruncateDecimal((baseBalance / m_market.ask) * maxTransactionFactor, 8);
                newBidMax = Numeric.TruncateDecimal((quoteBalance * m_market.bid) * maxTransactionFactor, 8);
            }
            else
            {
                // BTS_BTC
                //
                // baseBalance = 1 BTS
                // ask = 0.00004
                // askMax = 1 * 0.0004 = 0.0004 BTC

                newAskMax = Numeric.TruncateDecimal((baseBalance * m_market.ask) * maxTransactionFactor, 8);
                newBidMax = Numeric.TruncateDecimal((quoteBalance / m_market.bid) * maxTransactionFactor, 8);
            }

            m_isDirty |= newAskMax != m_market.ask_max || newBidMax != m_market.bid_max;

            market.ask_max = newAskMax;
            market.bid_max = newBidMax;

            if (m_market.price_discovery)
            {
                //
                // update price discovery engine
                //

                decimal bitsharesBalance = m_asset.GetAmountFromLarimers(bitsharesBalances[m_asset.id]);

                if (m_asset.symbol == CurrencyHelpers.kBtcSymbol)
                {
                    m_lastFeedPrice = 1;
                }
                else
                {
                    m_lastFeedPrice = RecomputeFeedPriceInBtc();
                }

                decimal inventoryRatio = ComputeInventoryRatio(bitsharesBalances, bitcoinBalance);

                if (m_prices == null)
                {
                    //
                    // initialise the price discovery engine
                    //

                    m_prices = new PriceDiscovery(market.spread_percent, market.window_percent, m_lastFeedPrice, inventoryRatio);
                }

                decimal oldBid = m_market.bid;
                decimal oldAsk = m_market.ask;
                m_prices.UpdateParameters(m_lastFeedPrice, inventoryRatio, m_market.spread_percent, m_market.window_percent, out m_market.bid, out m_market.ask);

                m_isDirty |= oldBid != m_market.bid || oldAsk != m_market.ask;
            }
        }
Esempio n. 6
0
        /// <summary>	Sends the bitcoins to depositor. </summary>
        ///
        /// <remarks>	Paul, 05/02/2015. </remarks>
        ///
        /// <exception cref="RefundBitsharesException">	Thrown when a Refund Bitshares error condition
        /// 											occurs. </exception>
        ///
        /// <param name="btcAddress">	The btc address. </param>
        /// <param name="trxId">	 	Identifier for the trx. </param>
        /// <param name="amount">	 	The amount. </param>
        /// <param name="asset">	 	The asset. </param>
        ///
        /// <returns>	A string. </returns>
        protected virtual string SendBitcoinsToDepositor(	string btcAddress, string trxId, ulong amount, BitsharesAsset asset,
															string depositAddress, MetaOrderType orderType, bool burnUia)
        {
            // make sure failures after this point dont result in multiple credits
            m_daemon.MarkDespositAsCreditedStart(trxId, depositAddress, m_market.symbol_pair, orderType);

            decimal bitAssetAmount = asset.GetAmountFromLarimers(amount);

            if (bitAssetAmount > m_market.bid_max)
            {
                throw new RefundBitsharesException("Over " + Numeric.SerialisedDecimal(m_market.bid_max) + " " + asset.symbol + "!");
            }

            // get the BTC amount we need to transfer
            decimal btcNoFee;

            if (m_flipped)
            {
                // they're sending us bitAssets, not BTC because the market is flipped, this is
                // equivelent to the opposite order type, so we have to use ask here
                btcNoFee = bitAssetAmount / m_market.ask;
            }
            else
            {
                btcNoFee = bitAssetAmount * m_market.bid;
            }

            // when selling, the fee is charged in BTC,
            // the amount recorded in the transaction is the amount of bitAssets sans fee, obv

            decimal fee = (m_market.bid_fee_percent / 100) * btcNoFee;
            decimal btcTotal = Numeric.TruncateDecimal(btcNoFee - fee, 8);

            // do the transfer
            string txid = m_bitcoin.SendToAddress(btcAddress, btcTotal, "mX: " + orderType + " " + asset.symbol);

            // mark this in our records
            m_daemon.MarkDespositAsCreditedEnd(trxId, txid, MetaOrderStatus.completed, bitAssetAmount, m_market.bid, fee);

            if (burnUia)
            {
                // make sure we were the issuer for this asset before we start burning it!
                BitsharesAccount account = m_bitshares.WalletGetAccount(m_bitsharesAccount);
                if (asset.issuer_id == account.id)
                {
                    m_bitshares.WalletBurn(bitAssetAmount, asset.symbol, m_bitsharesAccount, BurnForOrAgainst.@for, m_bitsharesAccount);
                }
            }

            return txid;
        }
Esempio n. 7
0
        /// <summary>	Refund bitshares deposit. </summary>
        ///
        /// <remarks>	Paul, 05/02/2015. </remarks>
        ///
        /// <param name="fromAccount">	from account. </param>
        /// <param name="larimers">   	The larimers. </param>
        /// <param name="depositId">  	Identifier for the deposit. </param>
        /// <param name="memo">		  	The memo. </param>
        /// <param name="asset">	  	The asset. </param>
        protected void RefundBitsharesDeposit(string fromAccount, ulong larimers, string depositId, string memo, BitsharesAsset asset, string depositAddress, MetaOrderType orderType)
        {
            decimal amount = asset.GetAmountFromLarimers(larimers);

            // make sure failures after this point don't result in multiple refunds
            m_daemon.MarkTransactionAsRefundedStart(depositId, depositAddress, m_market.symbol_pair, orderType);

            BitsharesTransactionResponse response = SendBitAssets(amount, asset, fromAccount, memo, false);
            //BitsharesTransactionResponse response = m_bitshares.WalletTransfer(amount, asset.symbol, m_bitsharesAccount, fromAccount, memo);

            m_daemon.MarkTransactionAsRefundedEnd(depositId, response.record_id, MetaOrderStatus.refunded, amount, memo);
        }