func (db *DB) Withdraw(pubkey *koblitz.PublicKey, asset *coinparam.Params, amount uint64) (err error) {
	var tx *sql.Tx
	if tx, err = db.DBHandler.Begin(); err != nil {
		return
	}

	defer func() {
		if err != nil {
			tx.Rollback()
			err = fmt.Errorf("Error while withdrawing %s using database: \n%s", asset.Name, err)
			return
		}
		err = tx.Commit()
	}()

	// Use the balance schema

	if _, err = tx.Exec("USE " + db.balanceSchema + ";"); err != nil {
		err = fmt.Errorf("Error trying to use database: \n%s", err)
		return
	}

	getBalanceQuery := fmt.Sprintf("SELECT balance FROM %s WHERE pubkey='%x';", asset.Name, pubkey.SerializeCompressed())
	var rows *sql.Rows
	if rows, err = tx.Query(getBalanceQuery); err != nil {
		return
	}

	var bal uint64
	if rows.Next() {
		if err = rows.Scan(&bal); err != nil {
			return
		}
	} else {
		err = fmt.Errorf("User not registered, no balance")
		return
	}

	if err = rows.Close(); err != nil {
		return
	}

	if bal < amount {
		err = fmt.Errorf("You do not have enough balance to withdraw this amount")
		return
	}

	updatedBalance := bal - amount
	reduceBalanceQuery := fmt.Sprintf("UPDATE %s SET balance=%d WHERE pubkey='%x'", asset.Name, updatedBalance, pubkey.SerializeCompressed())
	if _, err = tx.Exec(reduceBalanceQuery); err != nil {
		return
	}

	return
}
// Withdraw checks that the user has a certain amount of money and removes it if they do
Withdraw checks that the user has a certain amount of money and removes it if they do
// Withdraw checks that the user has a certain amount of money and removes it if they do
func (db *DB) Withdraw(pubkey *koblitz.PublicKey, asset *coinparam.Params, amount uint64) (err error) {
	var tx *sql.Tx
	if tx, err = db.DBHandler.Begin(); err != nil {
		return
	}
	defer func() {
		if err != nil {
			tx.Rollback()
			err = fmt.Errorf("Error while withdrawing %s using database: \n%s", asset.Name, err)
			return
		}
		err = tx.Commit()
	}()
	if _, err = tx.Exec("USE " + db.balanceSchema + ";"); err != nil {
		err = fmt.Errorf("Error trying to use database: \n%s", err)
		return
	}
	getBalanceQuery := fmt.Sprintf("SELECT balance FROM %s WHERE pubkey='%x';", asset.Name, pubkey.SerializeCompressed())
	var rows *sql.Rows
	if rows, err = tx.Query(getBalanceQuery); err != nil {
		return
	}
	var bal uint64
	if rows.Next() {
		if err = rows.Scan(&bal); err != nil {
			return
		}
	} else {
		err = fmt.Errorf("User not registered, no balance")
		return
	}
	if err = rows.Close(); err != nil {
		return
	}
	if bal < amount {
		err = fmt.Errorf("You do not have enough balance to withdraw this amount")
		return
	}
	updatedBalance := bal - amount
	reduceBalanceQuery := fmt.Sprintf("UPDATE %s SET balance=%d WHERE pubkey='%x'", asset.Name, updatedBalance, pubkey.SerializeCompressed())
	if _, err = tx.Exec(reduceBalanceQuery); err != nil {
		return
	}
	return
}
func ( db * DB ) Withdraw ( pubkey * koblitz . PublicKey , asset * coinparam . Params , amount uint64 ) ( err error ) { var tx * sql . Tx if tx , err = db . DBHandler . Begin ( ) ; err != nil { return } defer func ( ) { if err != nil { tx . Rollback ( ) err = fmt . Errorf ( " \n " , asset . Name , err ) return } err = tx . Commit ( ) } ( ) if _ , err = tx . Exec ( " " + db . balanceSchema + " " ) ; err != nil { err = fmt . Errorf ( " \n " , err ) return } getBalanceQuery := fmt . Sprintf ( " " , asset . Name , pubkey . SerializeCompressed ( ) ) var rows * sql . Rows if rows , err = tx . Query ( getBalanceQuery ) ; err != nil { return } var bal uint64 if rows . Next ( ) { if err = rows . Scan ( & bal ) ; err != nil { return } } else { err = fmt . Errorf ( " " ) return } if err = rows . Close ( ) ; err != nil { return } if bal < amount { err = fmt . Errorf ( " " ) return } updatedBalance := bal - amount reduceBalanceQuery := fmt . Sprintf ( " " , asset . Name , updatedBalance , pubkey . SerializeCompressed ( ) ) if _ , err = tx . Exec ( reduceBalanceQuery ) ; err != nil { return } return }
Withdraw checks that the user has a certain amount of money and removes it if they do
Withdraw checks that the user has a certain amount of money and removes it if they do
// Use the balance schema