func (db *DB) ViewOrderBook(pair *match.Pair) (sellOrderBook []*match.LimitOrder, buyOrderBook []*match.LimitOrder, err error) {

	tx, err := db.DBHandler.Begin()
	if err != nil {
		return nil, nil, fmt.Errorf("Error beginning transaction while updating deposits: \n%s", err)
	}
	defer func() {
		if err != nil {
			tx.Rollback()
			err = fmt.Errorf("Error while viewing order book: \n%s", err)
			return
		}
		err = tx.Commit()
	}()

	if _, err = tx.Exec("USE " + db.orderSchema + ";"); err != nil {
		return
	}

	// First get all the prices so we have something to iterate through and match
	getPricesQuery := fmt.Sprintf("SELECT DISTINCT price FROM %s ORDER BY price ASC;", pair.String())
	rows, err := tx.Query(getPricesQuery)
	if err != nil {
		return
	}

	var prices []float64

	for rows.Next() {
		var price float64
		if err = rows.Scan(&price); err != nil {
			return
		}

		prices = append(prices, price)
	}
	if err = rows.Close(); err != nil {
		return
	}

	for _, price := range prices {

		// logging.Infof("Matching all orders with price %f\n", price)

		if _, err = tx.Exec("USE " + db.orderSchema + ";"); err != nil {
			return
		}

		// this will select all sell side, ordered by time ascending so the earliest one will be at the front
		getSellSideQuery := fmt.Sprintf("SELECT pubkey, orderID, side, amountHave, amountWant FROM %s WHERE price=%f AND side='%s' ORDER BY time ASC;", pair.String(), price, "sell")
		sellRows, sellQueryErr := tx.Query(getSellSideQuery)
		if err = sellQueryErr; err != nil {
			return
		}

		var sellOrders []*match.LimitOrder
		for sellRows.Next() {
			var pubkeyBytes []byte
			sellOrder := new(match.LimitOrder)
			if err = sellRows.Scan(&pubkeyBytes, &sellOrder.OrderID, &sellOrder.Side, &sellOrder.AmountHave, &sellOrder.AmountWant); err != nil {
				return
			}

			// we have to do this because ugh they return my byte arrays as hex strings...
			if pubkeyBytes, err = hex.DecodeString(string(pubkeyBytes)); err != nil {
				return
			}

			// copy pubkey bytes into 33 order byte arr
			copy(sellOrder.Pubkey[:], pubkeyBytes)

			// set price to return to clients
			sellOrder.OrderbookPrice = price

			sellOrders = append(sellOrders, sellOrder)
		}
		if err = sellRows.Close(); err != nil {
			return
		}

		sellOrderBook = append(sellOrderBook, sellOrders[:]...)
		getBuySideQuery := fmt.Sprintf("SELECT pubkey, orderID, side, amountHave, amountWant FROM %s WHERE price=%f AND side='%s' ORDER BY time ASC;", pair.String(), price, "buy")
		buyRows, buyQueryErr := tx.Query(getBuySideQuery)
		if err = buyQueryErr; err != nil {
			return
		}

		var buyOrders []*match.LimitOrder
		for buyRows.Next() {
			var pubkeyBytes []byte
			buyOrder := new(match.LimitOrder)
			if err = buyRows.Scan(&pubkeyBytes, &buyOrder.OrderID, &buyOrder.Side, &buyOrder.AmountHave, &buyOrder.AmountWant); err != nil {
				return
			}

			// we have to do this because ugh they return my byte arrays as hex strings...
			if pubkeyBytes, err = hex.DecodeString(string(pubkeyBytes)); err != nil {
				return
			}

			// copy pubkey bytes into 33 order byte arr
			copy(buyOrder.Pubkey[:], pubkeyBytes)

			// set price to return to clients
			buyOrder.OrderbookPrice = price

			buyOrders = append(buyOrders, buyOrder)
		}
		if err = buyRows.Close(); err != nil {
			return
		}

		buyOrderBook = append(buyOrderBook, buyOrders[:]...)

	}

	return
}
// ViewOrderBook returns a list of orders that is the orderbook
ViewOrderBook returns a list of orders that is the orderbook
// ViewOrderBook returns a list of orders that is the orderbook
func (db *DB) ViewOrderBook(pair *match.Pair) (sellOrderBook []*match.LimitOrder, buyOrderBook []*match.LimitOrder, err error) {
	tx, err := db.DBHandler.Begin()
	if err != nil {
		return nil, nil, fmt.Errorf("Error beginning transaction while updating deposits: \n%s", err)
	}
	defer func() {
		if err != nil {
			tx.Rollback()
			err = fmt.Errorf("Error while viewing order book: \n%s", err)
			return
		}
		err = tx.Commit()
	}()
	if _, err = tx.Exec("USE " + db.orderSchema + ";"); err != nil {
		return
	}
	getPricesQuery := fmt.Sprintf("SELECT DISTINCT price FROM %s ORDER BY price ASC;", pair.String())
	rows, err := tx.Query(getPricesQuery)
	if err != nil {
		return
	}
	var prices []float64
	for rows.Next() {
		var price float64
		if err = rows.Scan(&price); err != nil {
			return
		}
		prices = append(prices, price)
	}
	if err = rows.Close(); err != nil {
		return
	}
	for _, price := range prices {
		if _, err = tx.Exec("USE " + db.orderSchema + ";"); err != nil {
			return
		}
		getSellSideQuery := fmt.Sprintf("SELECT pubkey, orderID, side, amountHave, amountWant FROM %s WHERE price=%f AND side='%s' ORDER BY time ASC;", pair.String(), price, "sell")
		sellRows, sellQueryErr := tx.Query(getSellSideQuery)
		if err = sellQueryErr; err != nil {
			return
		}
		var sellOrders []*match.LimitOrder
		for sellRows.Next() {
			var pubkeyBytes []byte
			sellOrder := new(match.LimitOrder)
			if err = sellRows.Scan(&pubkeyBytes, &sellOrder.OrderID, &sellOrder.Side, &sellOrder.AmountHave, &sellOrder.AmountWant); err != nil {
				return
			}
			if pubkeyBytes, err = hex.DecodeString(string(pubkeyBytes)); err != nil {
				return
			}
			copy(sellOrder.Pubkey[:], pubkeyBytes)
			sellOrder.OrderbookPrice = price
			sellOrders = append(sellOrders, sellOrder)
		}
		if err = sellRows.Close(); err != nil {
			return
		}
		sellOrderBook = append(sellOrderBook, sellOrders[:]...)
		getBuySideQuery := fmt.Sprintf("SELECT pubkey, orderID, side, amountHave, amountWant FROM %s WHERE price=%f AND side='%s' ORDER BY time ASC;", pair.String(), price, "buy")
		buyRows, buyQueryErr := tx.Query(getBuySideQuery)
		if err = buyQueryErr; err != nil {
			return
		}
		var buyOrders []*match.LimitOrder
		for buyRows.Next() {
			var pubkeyBytes []byte
			buyOrder := new(match.LimitOrder)
			if err = buyRows.Scan(&pubkeyBytes, &buyOrder.OrderID, &buyOrder.Side, &buyOrder.AmountHave, &buyOrder.AmountWant); err != nil {
				return
			}
			if pubkeyBytes, err = hex.DecodeString(string(pubkeyBytes)); err != nil {
				return
			}
			copy(buyOrder.Pubkey[:], pubkeyBytes)
			buyOrder.OrderbookPrice = price
			buyOrders = append(buyOrders, buyOrder)
		}
		if err = buyRows.Close(); err != nil {
			return
		}
		buyOrderBook = append(buyOrderBook, buyOrders[:]...)
	}
	return
}
func ( db * DB ) ViewOrderBook ( pair * match . Pair ) ( sellOrderBook [ ] * match . LimitOrder , buyOrderBook [ ] * match . LimitOrder , err error ) { tx , err := db . DBHandler . Begin ( ) if err != nil { return nil , nil , fmt . Errorf ( " \n " , err ) } defer func ( ) { if err != nil { tx . Rollback ( ) err = fmt . Errorf ( " \n " , err ) return } err = tx . Commit ( ) } ( ) if _ , err = tx . Exec ( " " + db . orderSchema + " " ) ; err != nil { return } getPricesQuery := fmt . Sprintf ( " " , pair . String ( ) ) rows , err := tx . Query ( getPricesQuery ) if err != nil { return } var prices [ ] float64 for rows . Next ( ) { var price float64 if err = rows . Scan ( & price ) ; err != nil { return } prices = append ( prices , price ) } if err = rows . Close ( ) ; err != nil { return } for _ , price := range prices { if _ , err = tx . Exec ( " " + db . orderSchema + " " ) ; err != nil { return } getSellSideQuery := fmt . Sprintf ( " " , pair . String ( ) , price , " " ) sellRows , sellQueryErr := tx . Query ( getSellSideQuery ) if err = sellQueryErr ; err != nil { return } var sellOrders [ ] * match . LimitOrder for sellRows . Next ( ) { var pubkeyBytes [ ] byte sellOrder := new ( match . LimitOrder ) if err = sellRows . Scan ( & pubkeyBytes , & sellOrder . OrderID , & sellOrder . Side , & sellOrder . AmountHave , & sellOrder . AmountWant ) ; err != nil { return } if pubkeyBytes , err = hex . DecodeString ( string ( pubkeyBytes ) ) ; err != nil { return } copy ( sellOrder . Pubkey [ : ] , pubkeyBytes ) sellOrder . OrderbookPrice = price sellOrders = append ( sellOrders , sellOrder ) } if err = sellRows . Close ( ) ; err != nil { return } sellOrderBook = append ( sellOrderBook , sellOrders [ : ] ... ) getBuySideQuery := fmt . Sprintf ( " " , pair . String ( ) , price , " " ) buyRows , buyQueryErr := tx . Query ( getBuySideQuery ) if err = buyQueryErr ; err != nil { return } var buyOrders [ ] * match . LimitOrder for buyRows . Next ( ) { var pubkeyBytes [ ] byte buyOrder := new ( match . LimitOrder ) if err = buyRows . Scan ( & pubkeyBytes , & buyOrder . OrderID , & buyOrder . Side , & buyOrder . AmountHave , & buyOrder . AmountWant ) ; err != nil { return } if pubkeyBytes , err = hex . DecodeString ( string ( pubkeyBytes ) ) ; err != nil { return } copy ( buyOrder . Pubkey [ : ] , pubkeyBytes ) buyOrder . OrderbookPrice = price buyOrders = append ( buyOrders , buyOrder ) } if err = buyRows . Close ( ) ; err != nil { return } buyOrderBook = append ( buyOrderBook , buyOrders [ : ] ... ) } return }
ViewOrderBook returns a list of orders that is the orderbook
ViewOrderBook returns a list of orders that is the orderbook
// First get all the prices so we have something to iterate through and match // logging.Infof("Matching all orders with price %f\n", price) // this will select all sell side, ordered by time ascending so the earliest one will be at the front // we have to do this because ugh they return my byte arrays as hex strings... // copy pubkey bytes into 33 order byte arr // set price to return to clients // we have to do this because ugh they return my byte arrays as hex strings... // copy pubkey bytes into 33 order byte arr // set price to return to clients