func (db *DB) MatchAuction(auctionID [32]byte) (height uint64, err error) {

	var tx *sql.Tx
	if tx, err = db.DBHandler.Begin(); err != nil {
		err = fmt.Errorf("Error when beginning transaction for NewAuction: %s", err)
		return
	}

	defer func() {
		if err != nil {
			tx.Rollback()
			err = fmt.Errorf("Error while creating new auction: \n%s", err)
			return
		}
		err = tx.Commit()
	}()

	// Do this for every pair we have

	// We define the rows, etc here so we don't waste a bunch of stack space
	// ughhhh scanning is so tedious
	var rows *sql.Rows

	var pubkeyBytes []byte
	var auctionIDBytes []byte
	var nonceBytes []byte

	var thisOrder *match.AuctionOrder

	var book map[float64][]*match.AuctionOrder
	book = make(map[float64][]*match.AuctionOrder)

	for _, pair := range db.pairsArray {

		// So we get the orders, and they are supposed to be all valid.
		queryAuctionOrders := fmt.Sprintf("SELECT pubkey, side, price, amountHave, amountWant, auctionID, nonce FROM %s WHERE auctionID='%x';", pair, auctionID)
		if rows, err = tx.Query(queryAuctionOrders); err != nil {
			err = fmt.Errorf("Error querying for orders in auction: %s", err)
			return
		}

		for rows.Next() {
			thisOrder = new(match.AuctionOrder)
			if err = rows.Scan(&pubkeyBytes, &thisOrder.Side, &thisOrder.OrderbookPrice, &thisOrder.AmountHave, &thisOrder.AmountWant, &auctionIDBytes, &nonceBytes); err != nil {
				err = fmt.Errorf("Error scanning row to bytes: %s", err)
				return
			}

			// Now we do the dumb decoding thing
			for _, byteArray := range [][]byte{pubkeyBytes, auctionIDBytes, nonceBytes} {
				// just for everything in this list of things we want to decode from weird words to real bytes
				if _, err = hex.Decode(byteArray, byteArray); err != nil {
					err = fmt.Errorf("Error decoding bytes for auction matching: %s", err)
					return
				}
			}

			// Now we put the data into the auction order
			copy(thisOrder.Pubkey[:], pubkeyBytes)
			copy(thisOrder.AuctionID[:], auctionIDBytes)
			copy(thisOrder.Nonce[:], nonceBytes)

			// okay cool now add it to the list
			book[thisOrder.OrderbookPrice] = append(book[thisOrder.OrderbookPrice], thisOrder)
		}

	}

	return
}
/*
 MatchAuction matches the auction with a specific auctionID. This is meant to be the implementation of pro-rata for just the stuff in the auction. We assume that there are orders in the auction orderbook that are ALL valid.

To understand Pro-rata matching on a batch of orders, here is an example of an orderbook, where the "Buy" list represents all of the buy orders
and the "Sell" list represents all of the sell orders.

The pair is BTC/LTC, you "buy" BTC with LTC and "sell" BTC for LTC.

	"Sell": [
		so1: {
			amountWant: 300 LTC,
			amountHave: 100 BTC,
			// price  : 0.33
		},
		so2: {
			amountWant: 400 LTC,
			amountHave: 100 BTC,
			// price  : 0.25
		},
		so3: {
			amountWant: 600 LTC,
			amountHave: 100 BTC,
			// price  : 0.17
		},
		so4: {
			amountWant: 70 LTC,
			amountHave: 10 BTC,
			// price  : 0.15
		},
	]
	"Buy":  [
		bo1: {
			amountWant: 100 BTC,
			amountHave: 100 LTC,
			// price  : 1.00
		},
		bo2: {
			amountWant: 100 BTC,
			amountHave: 300 LTC,
			// price  : 0.33
		},
		bo3: {
			amountWant: 100 BTC,
			amountHave: 500 LTC,
			// price  : 0.20
		},
		bo4: {
			amountWant: 10 BTC,
			amountHave: 50 LTC,
			// price  : 0.20
		},
	]

We can see here that there's no "nice" way to match these orders, the high/low prices on either end are competitive, nor are there many orders
that are the same price. Pro-rata matching for a single price is trivial.

*/
MatchAuction matches the auction with a specific auctionID. This is meant to be the implementation of pro-rata for just the stuff in the auction. We assume that there are orders in the auction orderbook that are ALL valid.

To understand Pro-rata matching on a batch of orders, here is an example of an orderbook, where the "Buy" list represents all of the buy orders
and the "Sell" list represents all of the sell orders.





We can see here that there's no "nice" way to match these orders, the high/low prices on either end are competitive, nor are there many orders
that are the same price. Pro-rata matching for a single price is trivial.
/ * MatchAuction matches the auction with a specific auctionID . This is meant to be the implementation of pro - rata for just the stuff in the auction . We assume that there are orders in the auction orderbook that are ALL valid . To understand Pro - rata matching on a batch of orders here is an example of an orderbook where the " Buy " list represents all of the buy orders and the " Sell " list represents all of the sell orders . The pair is BTC / LTC you " buy " BTC with LTC and " sell " BTC for LTC . " Sell " : [ so1 : { amountWant : 300 LTC amountHave : 100 BTC // price : 0 . 33 } so2 : { amountWant : 400 LTC amountHave : 100 BTC // price : 0 . 25 } so3 : { amountWant : 600 LTC amountHave : 100 BTC // price : 0 . 17 } so4 : { amountWant : 70 LTC amountHave : 10 BTC // price : 0 . 15 } ] " Buy " : [ bo1 : { amountWant : 100 BTC amountHave : 100 LTC // price : 1 . 00 } bo2 : { amountWant : 100 BTC amountHave : 300 LTC // price : 0 . 33 } bo3 : { amountWant : 100 BTC amountHave : 500 LTC // price : 0 . 20 } bo4 : { amountWant : 10 BTC amountHave : 50 LTC // price : 0 . 20 } ] We can see here that there ' s no " nice " way to match these orders the high / low prices on either end are competitive nor are there many orders that are the same price . Pro - rata matching for a single price is trivial . * /
func (db *DB) MatchAuction(auctionID [32]byte) (height uint64, err error) {
	var tx *sql.Tx
	if tx, err = db.DBHandler.Begin(); err != nil {
		err = fmt.Errorf("Error when beginning transaction for NewAuction: %s", err)
		return
	}
	defer func() {
		if err != nil {
			tx.Rollback()
			err = fmt.Errorf("Error while creating new auction: \n%s", err)
			return
		}
		err = tx.Commit()
	}()
	var rows *sql.Rows
	var pubkeyBytes []byte
	var auctionIDBytes []byte
	var nonceBytes []byte
	var thisOrder *match.AuctionOrder
	var book map[float64][]*match.AuctionOrder
	book = make(map[float64][]*match.AuctionOrder)
	for _, pair := range db.pairsArray {
		queryAuctionOrders := fmt.Sprintf("SELECT pubkey, side, price, amountHave, amountWant, auctionID, nonce FROM %s WHERE auctionID='%x';", pair, auctionID)
		if rows, err = tx.Query(queryAuctionOrders); err != nil {
			err = fmt.Errorf("Error querying for orders in auction: %s", err)
			return
		}
		for rows.Next() {
			thisOrder = new(match.AuctionOrder)
			if err = rows.Scan(&pubkeyBytes, &thisOrder.Side, &thisOrder.OrderbookPrice, &thisOrder.AmountHave, &thisOrder.AmountWant, &auctionIDBytes, &nonceBytes); err != nil {
				err = fmt.Errorf("Error scanning row to bytes: %s", err)
				return
			}
			for _, byteArray := range [][]byte{pubkeyBytes, auctionIDBytes, nonceBytes} {
				if _, err = hex.Decode(byteArray, byteArray); err != nil {
					err = fmt.Errorf("Error decoding bytes for auction matching: %s", err)
					return
				}
			}
			copy(thisOrder.Pubkey[:], pubkeyBytes)
			copy(thisOrder.AuctionID[:], auctionIDBytes)
			copy(thisOrder.Nonce[:], nonceBytes)
			book[thisOrder.OrderbookPrice] = append(book[thisOrder.OrderbookPrice], thisOrder)
		}
	}
	return
}
func ( db * DB ) MatchAuction ( auctionID [ 32 ] byte ) ( height uint64 , err error ) { var tx * sql . Tx if tx , err = db . DBHandler . Begin ( ) ; err != nil { err = fmt . Errorf ( " " , err ) return } defer func ( ) { if err != nil { tx . Rollback ( ) err = fmt . Errorf ( " \n " , err ) return } err = tx . Commit ( ) } ( ) var rows * sql . Rows var pubkeyBytes [ ] byte var auctionIDBytes [ ] byte var nonceBytes [ ] byte var thisOrder * match . AuctionOrder var book map [ float64 ] [ ] * match . AuctionOrder book = make ( map [ float64 ] [ ] * match . AuctionOrder ) for _ , pair := range db . pairsArray { queryAuctionOrders := fmt . Sprintf ( " " , pair , auctionID ) if rows , err = tx . Query ( queryAuctionOrders ) ; err != nil { err = fmt . Errorf ( " " , err ) return } for rows . Next ( ) { thisOrder = new ( match . AuctionOrder ) if err = rows . Scan ( & pubkeyBytes , & thisOrder . Side , & thisOrder . OrderbookPrice , & thisOrder . AmountHave , & thisOrder . AmountWant , & auctionIDBytes , & nonceBytes ) ; err != nil { err = fmt . Errorf ( " " , err ) return } for _ , byteArray := range [ ] [ ] byte { pubkeyBytes , auctionIDBytes , nonceBytes } { if _ , err = hex . Decode ( byteArray , byteArray ) ; err != nil { err = fmt . Errorf ( " " , err ) return } } copy ( thisOrder . Pubkey [ : ] , pubkeyBytes ) copy ( thisOrder . AuctionID [ : ] , auctionIDBytes ) copy ( thisOrder . Nonce [ : ] , nonceBytes ) book [ thisOrder . OrderbookPrice ] = append ( book [ thisOrder . OrderbookPrice ] , thisOrder ) } } return }
MatchAuction matches the auction with a specific auctionID.
MatchAuction matches the auction with a specific auctionID .
// Do this for every pair we have // We define the rows, etc here so we don't waste a bunch of stack space // ughhhh scanning is so tedious // So we get the orders, and they are supposed to be all valid. // Now we do the dumb decoding thing // just for everything in this list of things we want to decode from weird words to real bytes // Now we put the data into the auction order // okay cool now add it to the list