func (db *DB) CancelOrder(orderID string) (err error) {

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

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

	didOrderExist := false
	for _, pair := range db.pairsArray {
		// figure out if there is even an order
		getCurrentOrderQuery := fmt.Sprintf("SELECT pubkey, amountHave, amountWant, side FROM %s WHERE orderID='%s';", pair.String(), orderID)
		rows, currOrderErr := tx.Query(getCurrentOrderQuery)
		if err = currOrderErr; err != nil {
			return
		}

		if rows.Next() {
			var pubkeyBytes []byte
			var amtHave uint64
			var amtWant uint64
			var side string

			didOrderExist = true

			// get current values in case of partially filled order

			if err = rows.Scan(&pubkeyBytes, &amtHave, &amtWant, &side); err != nil {
				return
			}

			var pubkey *koblitz.PublicKey
			if pubkey, err = koblitz.ParsePubKey(pubkeyBytes, koblitz.S256()); err != nil {
				return
			}

			// do this so we don't get bad connection / busy buffer issues
			if err = rows.Close(); err != nil {
				return
			}

			// delete order from db
			deleteOrderQuery := fmt.Sprintf("DELETE FROM %s WHERE orderID='%s';", pair.String(), orderID)
			if _, err = tx.Exec(deleteOrderQuery); err != nil {
				return
			}

			// use balance schema for updating balance
			if _, err = tx.Exec("USE " + db.balanceSchema + ";"); err != nil {
				return
			}

			var correctAssetHave *coinparam.Params
			if side == "buy" {
				// make sure that the asset has an associated coinparam. Don't trade assets that you can't settle on a ledger.
				if correctAssetHave, err = util.GetParamFromName(pair.AssetHave.String()); err != nil {
					return
				}
			} else if side == "sell" {
				// make sure that the asset has an associated coinparam. Don't trade assets that you can't settle on a ledger.
				if correctAssetHave, err = util.GetParamFromName(pair.AssetWant.String()); err != nil {
					return
				}
			}

			// update the balance of the client
			if err = db.AddToBalanceWithinTransaction(pubkey, amtHave, tx, correctAssetHave); err != nil {
				return
			}
		}
		// use order schema at end of loop so we go back to where we were
		if _, err = tx.Exec("USE " + db.orderSchema + ";"); err != nil {
			return
		}
	}
	if !didOrderExist {
		err = fmt.Errorf("Order does not exist in any orderbook")
		return
	}

	// credit client with amounthave
	return
}
// CancelOrder runs the queries to cancel an order. Cancelling an individual order is atomic.
CancelOrder runs the queries to cancel an order. Cancelling an individual order is atomic.
// CancelOrder runs the queries to cancel an order . Cancelling an individual order is atomic .
func (db *DB) CancelOrder(orderID string) (err error) {
	tx, err := db.DBHandler.Begin()
	if err != nil {
		err = fmt.Errorf("Error cancelling order: \n%s", err)
		return
	}
	defer func() {
		if err != nil {
			tx.Rollback()
			err = fmt.Errorf("Error while cancelling order: \n%s", err)
			return
		}
		err = tx.Commit()
	}()
	if _, err = tx.Exec("USE " + db.orderSchema + ";"); err != nil {
		return
	}
	didOrderExist := false
	for _, pair := range db.pairsArray {
		getCurrentOrderQuery := fmt.Sprintf("SELECT pubkey, amountHave, amountWant, side FROM %s WHERE orderID='%s';", pair.String(), orderID)
		rows, currOrderErr := tx.Query(getCurrentOrderQuery)
		if err = currOrderErr; err != nil {
			return
		}
		if rows.Next() {
			var pubkeyBytes []byte
			var amtHave uint64
			var amtWant uint64
			var side string
			didOrderExist = true
			if err = rows.Scan(&pubkeyBytes, &amtHave, &amtWant, &side); err != nil {
				return
			}
			var pubkey *koblitz.PublicKey
			if pubkey, err = koblitz.ParsePubKey(pubkeyBytes, koblitz.S256()); err != nil {
				return
			}
			if err = rows.Close(); err != nil {
				return
			}
			deleteOrderQuery := fmt.Sprintf("DELETE FROM %s WHERE orderID='%s';", pair.String(), orderID)
			if _, err = tx.Exec(deleteOrderQuery); err != nil {
				return
			}
			if _, err = tx.Exec("USE " + db.balanceSchema + ";"); err != nil {
				return
			}
			var correctAssetHave *coinparam.Params
			if side == "buy" {
				if correctAssetHave, err = util.GetParamFromName(pair.AssetHave.String()); err != nil {
					return
				}
			} else if side == "sell" {
				if correctAssetHave, err = util.GetParamFromName(pair.AssetWant.String()); err != nil {
					return
				}
			}
			if err = db.AddToBalanceWithinTransaction(pubkey, amtHave, tx, correctAssetHave); err != nil {
				return
			}
		}
		if _, err = tx.Exec("USE " + db.orderSchema + ";"); err != nil {
			return
		}
	}
	if !didOrderExist {
		err = fmt.Errorf("Order does not exist in any orderbook")
		return
	}
	return
}
func ( db * DB ) CancelOrder ( orderID string ) ( err error ) { tx , err := db . DBHandler . Begin ( ) if err != nil { err = fmt . Errorf ( " \n " , err ) return } 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 } didOrderExist := false for _ , pair := range db . pairsArray { getCurrentOrderQuery := fmt . Sprintf ( " " , pair . String ( ) , orderID ) rows , currOrderErr := tx . Query ( getCurrentOrderQuery ) if err = currOrderErr ; err != nil { return } if rows . Next ( ) { var pubkeyBytes [ ] byte var amtHave uint64 var amtWant uint64 var side string didOrderExist = true if err = rows . Scan ( & pubkeyBytes , & amtHave , & amtWant , & side ) ; err != nil { return } var pubkey * koblitz . PublicKey if pubkey , err = koblitz . ParsePubKey ( pubkeyBytes , koblitz . S256 ( ) ) ; err != nil { return } if err = rows . Close ( ) ; err != nil { return } deleteOrderQuery := fmt . Sprintf ( " " , pair . String ( ) , orderID ) if _ , err = tx . Exec ( deleteOrderQuery ) ; err != nil { return } if _ , err = tx . Exec ( " " + db . balanceSchema + " " ) ; err != nil { return } var correctAssetHave * coinparam . Params if side == " " { if correctAssetHave , err = util . GetParamFromName ( pair . AssetHave . String ( ) ) ; err != nil { return } } else if side == " " { if correctAssetHave , err = util . GetParamFromName ( pair . AssetWant . String ( ) ) ; err != nil { return } } if err = db . AddToBalanceWithinTransaction ( pubkey , amtHave , tx , correctAssetHave ) ; err != nil { return } } if _ , err = tx . Exec ( " " + db . orderSchema + " " ) ; err != nil { return } } if ! didOrderExist { err = fmt . Errorf ( " " ) return } return }
CancelOrder runs the queries to cancel an order.
CancelOrder runs the queries to cancel an order .
// figure out if there is even an order // get current values in case of partially filled order // do this so we don't get bad connection / busy buffer issues // delete order from db // use balance schema for updating balance // make sure that the asset has an associated coinparam. Don't trade assets that you can't settle on a ledger. // make sure that the asset has an associated coinparam. Don't trade assets that you can't settle on a ledger. // update the balance of the client // use order schema at end of loop so we go back to where we were // credit client with amounthave