Ejemplo n.º 1
0
func SavePriceLog(tx *db.ModelTx, plog *PriceLog) *PriceLog {
	err := tx.QueryRow(
		`INSERT INTO exchange_price_log (`+PriceLogModel.FieldsInsert+`)
         VALUES (`+PriceLogModel.Placeholders+`)
         RETURNING id`,
		plog,
	).Scan(&plog.Id)
	if err != nil {
		panic(err)
	}
	return plog
}
Ejemplo n.º 2
0
func SaveTrade(tx *db.ModelTx, trade *Trade) *Trade {
	if trade.Time == 0 {
		trade.Time = time.Now().Unix()
	}
	err := tx.QueryRow(
		`INSERT INTO exchange_trade (`+TradeModel.FieldsInsert+`)
         VALUES (`+TradeModel.Placeholders+`)
         RETURNING id`,
		trade,
	).Scan(&trade.Id)
	if err != nil {
		panic(err)
	}
	return trade
}
Ejemplo n.º 3
0
func SaveOrder(tx *db.ModelTx, order *Order) *Order {
	if order.Time == 0 {
		order.Time = time.Now().Unix()
	}
	err := tx.QueryRow(
		`INSERT INTO exchange_order (`+OrderModel.FieldsInsert+`)
         VALUES (`+OrderModel.Placeholders+`)
         RETURNING id`,
		order,
	).Scan(&order.Id)
	if err != nil {
		panic(err)
	}
	return order
}
Ejemplo n.º 4
0
func SaveOrUpdatePriceLog(tx *db.ModelTx, plog *PriceLog) {
	var exists int
	err := tx.QueryRow(
		`SELECT 1 FROM exchange_price_log
         WHERE market=? AND interval=? AND time=?`,
		plog.Market, plog.Interval, plog.Time,
	).Scan(&exists)
	switch db.GetErrorType(err) {
	case sql.ErrNoRows:
		SavePriceLog(tx, plog)
	case nil:
		UpdatePriceLog(tx, plog)
	default:
		panic(err)
	}
}
Ejemplo n.º 5
0
// Adds or subtracts an amount to a user's wallet.
// nonnegative: panics with INSUFFICIENT_FUNDS_ERROR if resulting balance is negative.
// Returns the new balance
func UpdateBalanceByWallet(tx *db.ModelTx, userId int64, wallet string, coin string, diff int64, nonnegative bool) *Balance {
	var balance Balance

	// Get existing balance.
	err := tx.QueryRow(
		`SELECT `+BalanceModel.FieldsSimple+`
         FROM account_balance WHERE
         user_id=? AND wallet=? AND coin=?`,
		userId, wallet, coin,
	).Scan(&balance)

	// Ensure nonnegative
	if nonnegative && balance.Amount+diff < 0 {
		panic(INSUFFICIENT_FUNDS_ERROR)
	}

	// First time balance?
	if err == sql.ErrNoRows {
		// Create new balance
		balance := Balance{UserId: userId, Wallet: wallet, Coin: coin, Amount: diff}
		SaveBalance(tx, &balance)
		return &balance
	}

	// Update balance
	balance.Amount += diff
	_, err = tx.Exec(
		`UPDATE account_balance
         SET amount=?
         WHERE user_id=? AND wallet=? AND coin=?`,
		balance.Amount, userId, wallet, coin,
	)
	if err != nil {
		panic(err)
	}
	return &balance
}