コード例 #1
0
ファイル: models.go プロジェクト: jaekwon/ftnox-backend
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
}
コード例 #2
0
ファイル: models.go プロジェクト: jaekwon/ftnox-backend
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
}
コード例 #3
0
ファイル: models.go プロジェクト: jaekwon/ftnox-backend
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
}
コード例 #4
0
ファイル: models.go プロジェクト: jaekwon/ftnox-backend
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)
	}
}
コード例 #5
0
ファイル: models.go プロジェクト: jaekwon/ftnox-backend
// 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
}