instrument usecases & repository methods

This commit is contained in:
yash 2026-02-25 22:33:52 +03:00
parent 39b89fc404
commit 16d38bb3cf
7 changed files with 100 additions and 13 deletions

View file

@ -1,6 +1,9 @@
package entities
type Pair struct {
type InstrumentID string
type Instrument struct {
ID InstrumentID
BaseCurrency string // base currency of the pair. e.g. BTC.
QuoteCurrency string // quote currency of the pair. e.g. USDT.
}

View file

@ -3,8 +3,8 @@ package entities
import "github.com/shopspring/decimal"
type Price struct {
Ask decimal.Decimal // limit seller / market buyer. ask > bid.
Bid decimal.Decimal // limit buyer / market seller. bid < ask.
Spread decimal.Decimal // delta between ask and bid.
Pair Pair // trading pair
Ask decimal.Decimal // limit seller / market buyer. ask > bid.
Bid decimal.Decimal // limit buyer / market seller. bid < ask.
Spread decimal.Decimal // delta between ask and bid.
Instrument Instrument // trading pair
}

View file

@ -36,17 +36,17 @@ func New(log *slog.Logger, cfg *config.Bybit) provider.Provider {
}
}
func (b *Bybit) symbol(pair *entities.Pair) string {
func (b *Bybit) symbol(pair *entities.Instrument) string {
return fmt.Sprintf("%s%s", pair.BaseCurrency, pair.QuoteCurrency)
}
// Price returns the current price of the pair (base currency / quote currency).
// e.g. BTC/USDT.
func (b *Bybit) Price(ctx context.Context, pair entities.Pair) (*entities.Price, error) {
func (b *Bybit) Price(ctx context.Context, instrument entities.Instrument) (*entities.Price, error) {
// build request
req := marketOrderbookReq{
Category: categorySpot,
Symbol: b.symbol(&pair),
Symbol: b.symbol(&instrument),
}
var resp marketOrderbookResp
// make request
@ -67,9 +67,9 @@ func (b *Bybit) Price(ctx context.Context, pair entities.Pair) (*entities.Price,
spread := askPrice.Sub(bidPrice)
return &entities.Price{
Ask: askPrice,
Bid: bidPrice,
Spread: spread,
Pair: pair,
Ask: askPrice,
Bid: bidPrice,
Spread: spread,
Instrument: instrument,
}, nil
}

View file

@ -9,5 +9,5 @@ import (
type Provider interface {
// Price returns the current price of the pair (base currency / quote currency).
// e.g. BTC/USDT.
Price(ctx context.Context, pair entities.Pair) (*entities.Price, error)
Price(ctx context.Context, instrument entities.Instrument) (*entities.Price, error)
}

View file

@ -1 +1,54 @@
package postgresql
import (
"context"
"fmt"
"gitea.computernetthings.ru/yash/crypto_alert_bot/internal/entities"
)
const instrumentListQuery = `
select i.id, c_base.symbol, c_quote.symbol
from instrument i
join currency c_base on c_base.id = i.base_currency_id
join currency c_quote on c_quote.id = i.quoted_currency_id
order by i.id
offset $1 limit $2`
func (p *Postgresql) InstrumentList(ctx context.Context, offset, limit int) ([]entities.Instrument, error) {
rows, err := p.db.Query(ctx, instrumentListQuery, offset, limit)
if err != nil {
return nil, fmt.Errorf("failed to exec instrumentListQuery: %w", err)
}
defer rows.Close()
var instruments []entities.Instrument
for rows.Next() {
var inst entities.Instrument
if err := rows.Scan(&inst.ID, &inst.BaseCurrency, &inst.QuoteCurrency); err != nil {
return nil, fmt.Errorf("failed to scan instrument row: %w", err)
}
instruments = append(instruments, inst)
}
return instruments, nil
}
const createInstrumentQuery = `
insert into instrument(base_currency_id, quoted_currency_id)
values (
(select id from currency where symbol = $1),
(select id from currency where symbol = $2)
)
returning id`
func (p *Postgresql) CreateInstrument(ctx context.Context, instrument *entities.Instrument) (entities.InstrumentID, error) {
var id entities.InstrumentID
err := p.db.QueryRow(ctx, createInstrumentQuery, instrument.BaseCurrency, instrument.QuoteCurrency).Scan(&id)
if err != nil {
return "", fmt.Errorf("failed to exec createInstrumentQuery: %w", err)
}
return id, nil
}

View file

@ -10,4 +10,7 @@ type Storage interface {
SaveUser(ctx context.Context, user *entities.User) (entities.UserID, error)
UserByID(ctx context.Context, id entities.UserID) (*entities.User, error)
UserByTelegramID(ctx context.Context, tgID entities.TelegramID) (*entities.User, error)
InstrumentList(ctx context.Context, offset, limit int) ([]entities.Instrument, error)
CreateInstrument(ctx context.Context, instrument *entities.Instrument) (entities.InstrumentID, error)
}

View file

@ -0,0 +1,28 @@
package usecase
import (
"context"
"fmt"
"gitea.computernetthings.ru/yash/crypto_alert_bot/internal/entities"
)
func (uc *Usecase) InstrumentList(ctx context.Context, offset, limit int) ([]entities.Instrument, error) {
instruments, err := uc.storage.InstrumentList(ctx, offset, limit)
if err != nil {
uc.log.Error("failed to list instruments", "offset", offset, "limit", limit, "err", err)
return nil, fmt.Errorf("failed to list instruments: %w", err)
}
return instruments, nil
}
func (uc *Usecase) CreateInstrument(ctx context.Context, instrument *entities.Instrument) (entities.InstrumentID, error) {
id, err := uc.storage.CreateInstrument(ctx, instrument)
if err != nil {
uc.log.Error("failed to create instrument", "instrument", instrument, "err", err)
return "", fmt.Errorf("failed to create instrument: %w", err)
}
return id, nil
}