Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions emitter/emitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (e *HyperLiquidEmitter) Emit(ctx context.Context, w recomma.OrderWork) erro
// TODO: decide if we want to persist the result we get back here, it's not interesting ususally as it just states `resting`
switch w.Action.Type {
case recomma.ActionCreate:
if e.ws.Exists(w.MD) {
if e.ws.Exists(ctx, w.MD) {
logger.Debug("order already exists on Hyperliquid")
return nil
}
Expand All @@ -147,15 +147,15 @@ func (e *HyperLiquidEmitter) Emit(ctx context.Context, w recomma.OrderWork) erro

// we cannot submit these but we must ignore them
if strings.Contains(err.Error(), "Order must have minimum value") {
if err := e.store.RecordHyperliquidOrderRequest(w.MD, *w.Action.Create, w.BotEvent.RowID); err != nil {
if err := e.store.RecordHyperliquidOrderRequest(ctx, w.MD, *w.Action.Create, w.BotEvent.RowID); err != nil {
logger.Warn("could not add to store", slog.String("error", err.Error()))
}
logger.Warn("could not submit (order value), ignoring", slog.String("error", err.Error()))
return nil
}

if strings.Contains(err.Error(), "Reduce only order would increase position") {
if err := e.store.RecordHyperliquidOrderRequest(w.MD, *w.Action.Create, w.BotEvent.RowID); err != nil {
if err := e.store.RecordHyperliquidOrderRequest(ctx, w.MD, *w.Action.Create, w.BotEvent.RowID); err != nil {
logger.Warn("could not add to store", slog.String("error", err.Error()))
}
logger.Warn("could not submit (reduce only order, increase position), ignoring", slog.String("error", err.Error()))
Expand All @@ -172,7 +172,7 @@ func (e *HyperLiquidEmitter) Emit(ctx context.Context, w recomma.OrderWork) erro
return fmt.Errorf("could not place order: %w", err)
}

if err := e.store.RecordHyperliquidOrderRequest(w.MD, *w.Action.Create, w.BotEvent.RowID); err != nil {
if err := e.store.RecordHyperliquidOrderRequest(ctx, w.MD, *w.Action.Create, w.BotEvent.RowID); err != nil {
logger.Warn("could not add to store", slog.String("error", err.Error()))
}
case recomma.ActionCancel:
Expand All @@ -189,7 +189,7 @@ func (e *HyperLiquidEmitter) Emit(ctx context.Context, w recomma.OrderWork) erro
logger.Warn("could not cancel order", slog.String("error", err.Error()), slog.Any("action", w.Action.Cancel))
return fmt.Errorf("could not cancel order: %w", err)
}
if err := e.store.RecordHyperliquidCancel(w.MD, *w.Action.Cancel, w.BotEvent.RowID); err != nil {
if err := e.store.RecordHyperliquidCancel(ctx, w.MD, *w.Action.Cancel, w.BotEvent.RowID); err != nil {
logger.Warn("could not add to store", slog.String("error", err.Error()))
}
case recomma.ActionNone:
Expand All @@ -209,7 +209,7 @@ func (e *HyperLiquidEmitter) Emit(ctx context.Context, w recomma.OrderWork) erro
logger.Warn("could not modify order", slog.String("error", err.Error()), slog.Any("action", w.Action.Modify))
return fmt.Errorf("could not modify order: %w", err)
}
if err := e.store.AppendHyperliquidModify(w.MD, *w.Action.Modify, w.BotEvent.RowID); err != nil {
if err := e.store.AppendHyperliquidModify(ctx, w.MD, *w.Action.Modify, w.BotEvent.RowID); err != nil {
logger.Warn("could not add to store", slog.String("error", err.Error()))
}
}
Expand Down
19 changes: 10 additions & 9 deletions engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (e *Engine) ProduceActiveDeals(ctx context.Context, q Queue) error {
// not a time later else we don't show that deal anymore
lastReq := time.Now().Add(-time.Hour * 24)

_, syncedAt, found, err := e.store.LoadBot(b.Id)
_, syncedAt, found, err := e.store.LoadBot(gctx, b.Id)
if found && err == nil {
lastReq = syncedAt
}
Expand Down Expand Up @@ -136,7 +136,7 @@ func (e *Engine) ProduceActiveDeals(ctx context.Context, q Queue) error {
}
minUpdatedAt = time.UnixMilli(min(minUpdatedAt.UnixMilli(), d.UpdatedAt.UnixMilli()))
q.Add(WorkKey{DealID: uint32(d.Id), BotID: uint32(d.BotId)})
err := e.store.RecordThreeCommasDeal(d)
err := e.store.RecordThreeCommasDeal(gctx, d)
if err != nil {
logger.Warn("could not store deal", slog.String("error", err.Error()))
}
Expand All @@ -154,11 +154,11 @@ func (e *Engine) ProduceActiveDeals(ctx context.Context, q Queue) error {
}
}

if err := e.store.RecordBot(b, b.UpdatedAt); err != nil {
if err := e.store.RecordBot(gctx, b, b.UpdatedAt); err != nil {
logger.Warn("could not record bot", slog.String("error", err.Error()))
}

e.store.TouchBot(b.Id, latest)
e.store.TouchBot(gctx, b.Id, latest)
if len(deals) > 0 {
logger.Info("Enqueued Deals", slog.Int("deals", len(deals)), slog.Duration("elapsed", time.Since(start)))
}
Expand Down Expand Up @@ -214,7 +214,7 @@ func (e *Engine) processDeal(ctx context.Context, wi WorkKey, currency string, e
BotEventID: event.FingerprintAsID(),
}
// we want to store all incoming as a log
_, err := e.store.RecordThreeCommasBotEventLog(md, event)
_, err := e.store.RecordThreeCommasBotEventLog(ctx, md, event)
if err != nil {
if !errors.Is(err, sql.ErrNoRows) {
return fmt.Errorf("record bot event log: %w", err)
Expand All @@ -231,7 +231,7 @@ func (e *Engine) processDeal(ctx context.Context, wi WorkKey, currency string, e
// we only want to act on PLACING, CANCEL and MODIFY
// we assume here that when within the span of 15s (our poll time) a botevent went from PLACING to CANCEL we can ignore it
if event.Action == tc.BotEventActionPlace || event.Action == tc.BotEventActionCancel || event.Action == tc.BotEventActionModify {
lastInsertedId, err := e.store.RecordThreeCommasBotEvent(md, event)
lastInsertedId, err := e.store.RecordThreeCommasBotEvent(ctx, md, event)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
// this means we saw it before, no error
Expand All @@ -246,7 +246,7 @@ func (e *Engine) processDeal(ctx context.Context, wi WorkKey, currency string, e
}

for _, md := range seen {
action, latestEvent, shouldEmit, err := e.reduceOrderEvents(currency, md, logger.With("botevent-id", md.BotEventID))
action, latestEvent, shouldEmit, err := e.reduceOrderEvents(ctx, currency, md, logger.With("botevent-id", md.BotEventID))
if err != nil {
return fmt.Errorf("reduce order %d: %w", md.BotEventID, err)
}
Expand Down Expand Up @@ -276,6 +276,7 @@ func (e *Engine) processDeal(ctx context.Context, wi WorkKey, currency string, e
// on Hyperliquid". It returns the action plus a flag telling the caller whether
// anything needs to be emitted.
func (e *Engine) reduceOrderEvents(
ctx context.Context,
currency string,
md metadata.Metadata,
logger *slog.Logger,
Expand All @@ -284,7 +285,7 @@ func (e *Engine) reduceOrderEvents(
// NB: we actually only care about the PLACING one's

// rows are already sorted by CreatedAt ASC in ListEventsForOrder.
events, err := e.store.ListEventsForOrder(md.BotID, md.DealID, md.BotEventID)
events, err := e.store.ListEventsForOrder(ctx, md.BotID, md.DealID, md.BotEventID)
if err != nil {
return recomma.Action{}, nil, false, fmt.Errorf("load event history: %w", err)
}
Expand All @@ -297,7 +298,7 @@ func (e *Engine) reduceOrderEvents(
prev := previousDistinct(events)

// Did we already create anything for this CLOID on Hyperliquid?
submitted, haveSubmission, err := e.store.LoadHyperliquidSubmission(md)
submitted, haveSubmission, err := e.store.LoadHyperliquidSubmission(ctx, md)
if err != nil {
return recomma.Action{}, nil, false, fmt.Errorf("load submission: %w", err)
}
Expand Down
16 changes: 8 additions & 8 deletions engine/handle_deal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func TestProcessDeal_TableDriven(t *testing.T) {
name: "cancel emitted after local create",
events: []tc.BotEvent{cancelEvent(base.Add(2 * time.Minute))},
prepare: func(t *testing.T, h *harness) {
inserted, err := h.store.RecordThreeCommasBotEvent(activeMD, activeEvent)
inserted, err := h.store.RecordThreeCommasBotEvent(h.ctx, activeMD, activeEvent)
require.NoError(t, err)
require.NotZero(t, inserted)

Expand All @@ -130,7 +130,7 @@ func TestProcessDeal_TableDriven(t *testing.T) {
}

createReq := adapter.ToCreateOrderRequest(h.deal.ToCurrency, be, activeMD)
require.NoError(t, h.store.RecordHyperliquidOrderRequest(activeMD, createReq, inserted))
require.NoError(t, h.store.RecordHyperliquidOrderRequest(h.ctx, activeMD, createReq, inserted))
},
wantActions: []recomma.ActionType{recomma.ActionCancel},
wantStatuses: []tc.MarketOrderStatusString{
Expand All @@ -142,7 +142,7 @@ func TestProcessDeal_TableDriven(t *testing.T) {
name: "modify emitted after local create",
events: []tc.BotEvent{modifyEvent},
prepare: func(t *testing.T, h *harness) {
inserted, err := h.store.RecordThreeCommasBotEvent(activeMD, activeEvent)
inserted, err := h.store.RecordThreeCommasBotEvent(h.ctx, activeMD, activeEvent)
require.NoError(t, err)
require.NotZero(t, inserted)

Expand All @@ -152,7 +152,7 @@ func TestProcessDeal_TableDriven(t *testing.T) {
}

createReq := adapter.ToCreateOrderRequest(h.deal.ToCurrency, be, activeMD)
require.NoError(t, h.store.RecordHyperliquidOrderRequest(activeMD, createReq, inserted))
require.NoError(t, h.store.RecordHyperliquidOrderRequest(h.ctx, activeMD, createReq, inserted))
},
wantActions: []recomma.ActionType{recomma.ActionModify},
wantStatuses: []tc.MarketOrderStatusString{
Expand Down Expand Up @@ -186,7 +186,7 @@ func TestProcessDeal_TableDriven(t *testing.T) {

// For history assertions, use the fingerprint of the last event we fed.
fp := tc.events[len(tc.events)-1].FingerprintAsID()
history, err := h.store.ListEventsForOrder(h.key.BotID, h.key.DealID, fp)
history, err := h.store.ListEventsForOrder(h.ctx, h.key.BotID, h.key.DealID, fp)
require.NoError(t, err)
require.Len(t, history, len(tc.wantStatuses))
for i, want := range tc.wantStatuses {
Expand All @@ -210,7 +210,7 @@ func TestProcessDeal_TakeProfitSizedFromTracker(t *testing.T) {
h := newHarness(t, botID, dealID)
defer h.store.Close()

require.NoError(t, h.store.RecordThreeCommasDeal(tc.Deal{
require.NoError(t, h.store.RecordThreeCommasDeal(h.ctx, tc.Deal{
Id: int(dealID),
BotId: int(botID),
CreatedAt: base,
Expand All @@ -225,9 +225,9 @@ func TestProcessDeal_TakeProfitSizedFromTracker(t *testing.T) {
testutil.WithSize(5),
testutil.WithOrderType(tc.MarketOrderDealOrderTypeBase),
)
_, err := h.store.RecordThreeCommasBotEvent(baseMD, baseEvent)
_, err := h.store.RecordThreeCommasBotEvent(h.ctx, baseMD, baseEvent)
require.NoError(t, err)
require.NoError(t, h.store.RecordHyperliquidStatus(baseMD, makeWsStatus(baseMD, coin, "B", hyperliquid.OrderStatusValueFilled, 5, 0, 10, base.Add(time.Second))))
require.NoError(t, h.store.RecordHyperliquidStatus(h.ctx, baseMD, makeWsStatus(baseMD, coin, "B", hyperliquid.OrderStatusValueFilled, 5, 0, 10, base.Add(time.Second))))

tracker := filltracker.New(h.store, nil)
require.NoError(t, tracker.Rebuild(h.ctx))
Expand Down
8 changes: 4 additions & 4 deletions filltracker/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ func (s *Service) Rebuild(ctx context.Context) error {
}

// UpdateStatus ingests a fresh Hyperliquid status update for a metadata fingerprint.
func (s *Service) UpdateStatus(md metadata.Metadata, status hyperliquid.WsOrder) error {
event, err := s.store.LoadThreeCommasBotEvent(md)
func (s *Service) UpdateStatus(ctx context.Context, md metadata.Metadata, status hyperliquid.WsOrder) error {
event, err := s.store.LoadThreeCommasBotEvent(ctx, md)
if err != nil {
return err
}
Expand Down Expand Up @@ -166,11 +166,11 @@ func (s *Service) reloadDeal(ctx context.Context, dealID uint32) error {
}

for _, md := range mds {
event, err := s.store.LoadThreeCommasBotEvent(md)
event, err := s.store.LoadThreeCommasBotEvent(ctx, md)
if err != nil {
return err
}
status, found, err := s.store.LoadHyperliquidStatus(md)
status, found, err := s.store.LoadHyperliquidStatus(ctx, md)
if err != nil {
return err
}
Expand Down
8 changes: 4 additions & 4 deletions filltracker/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func TestServiceUpdateStatusAdjustsPosition(t *testing.T) {
// Partial fill: remaining 80 of 200.
partialStatus := makeStatus(tpMD, coin, "S", hyperliquid.OrderStatusValueOpen, 200, 80, 0.205, now)
require.NoError(t, recordStatus(store, tpMD, partialStatus))
require.NoError(t, tracker.UpdateStatus(tpMD, partialStatus))
require.NoError(t, tracker.UpdateStatus(ctx, tpMD, partialStatus))

snapshot, ok := tracker.Snapshot(dealID)
require.True(t, ok)
Expand Down Expand Up @@ -323,7 +323,7 @@ func recordDeal(t *testing.T, store *storage.Storage, dealID, botID uint32, coin
t.Helper()

now := time.Now().UTC()
err := store.RecordThreeCommasDeal(tc.Deal{
err := store.RecordThreeCommasDeal(context.Background(), tc.Deal{
Id: int(dealID),
BotId: int(botID),
CreatedAt: now,
Expand All @@ -334,12 +334,12 @@ func recordDeal(t *testing.T, store *storage.Storage, dealID, botID uint32, coin
}

func recordEvent(store *storage.Storage, md metadata.Metadata, evt tc.BotEvent) error {
_, err := store.RecordThreeCommasBotEvent(md, evt)
_, err := store.RecordThreeCommasBotEvent(context.Background(), md, evt)
return err
}

func recordStatus(store *storage.Storage, md metadata.Metadata, status hyperliquid.WsOrder) error {
return store.RecordHyperliquidStatus(md, status)
return store.RecordHyperliquidStatus(context.Background(), md, status)
}

func makeStatus(md metadata.Metadata, coin, side string, status hyperliquid.OrderStatusValue, original, remaining, limit float64, ts time.Time) hyperliquid.WsOrder {
Expand Down
12 changes: 6 additions & 6 deletions hl/ws/ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ func New(ctx context.Context, store *storage.Storage, tracker *filltracker.Servi
continue
}
if c.tracker != nil {
if err := c.tracker.UpdateStatus(*md, o); err != nil {
if err := c.tracker.UpdateStatus(context.Background(), *md, o); err != nil {
c.logger.Warn("could not update fill tracker", slog.String("error", err.Error()))
}
}
if err := c.store.RecordHyperliquidStatus(*md, o); err != nil {
if err := c.store.RecordHyperliquidStatus(context.Background(), *md, o); err != nil {
c.logger.Warn("could not store status", slog.String("error", err.Error()))
continue
}
Expand Down Expand Up @@ -133,8 +133,8 @@ func (c *Client) Close() error {
}

// Exists returns true if we've seen this CLOID via OrderUpdates.
func (c *Client) Exists(md metadata.Metadata) bool {
_, ok, err := c.store.LoadHyperliquidStatus(md)
func (c *Client) Exists(ctx context.Context, md metadata.Metadata) bool {
_, ok, err := c.store.LoadHyperliquidStatus(ctx, md)
if err != nil {
return false
}
Expand All @@ -143,8 +143,8 @@ func (c *Client) Exists(md metadata.Metadata) bool {
}

// Get returns the full WsOrder for a CLOID, if we have it.
func (c *Client) Get(md metadata.Metadata) (*hyperliquid.WsOrder, bool) {
status, ok, err := c.store.LoadHyperliquidStatus(md)
func (c *Client) Get(ctx context.Context, md metadata.Metadata) (*hyperliquid.WsOrder, bool) {
status, ok, err := c.store.LoadHyperliquidStatus(ctx, md)
if err != nil {
return nil, false
}
Expand Down
11 changes: 6 additions & 5 deletions storage/latestHlSafetyStatus_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package storage

import (
"context"
"testing"
"time"

Expand Down Expand Up @@ -49,7 +50,7 @@ func TestStorageListLatestHyperliquidSafetyStatuses(t *testing.T) {
Text: "test safety order",
}

if _, err := store.RecordThreeCommasBotEvent(md, botevent); err != nil {
if _, err := store.RecordThreeCommasBotEvent(context.Background(), md, botevent); err != nil {
t.Fatalf("RecordThreeCommasBotEvent: %v", err)
}

Expand All @@ -58,7 +59,7 @@ func TestStorageListLatestHyperliquidSafetyStatuses(t *testing.T) {

recordStatus := func(t *testing.T, store *Storage, md metadata.Metadata, status hyperliquid.WsOrder) {
t.Helper()
if err := store.RecordHyperliquidStatus(md, status); err != nil {
if err := store.RecordHyperliquidStatus(context.Background(), md, status); err != nil {
t.Fatalf("RecordHyperliquidStatus: %v", err)
}
}
Expand Down Expand Up @@ -314,7 +315,7 @@ func TestStorageListLatestHyperliquidSafetyStatuses(t *testing.T) {
IsMarket: true,
Text: "test take profit",
}
if _, err := store.RecordThreeCommasBotEvent(mdTakeProfit, takeProfit); err != nil {
if _, err := store.RecordThreeCommasBotEvent(context.Background(), mdTakeProfit, takeProfit); err != nil {
t.Fatalf("RecordThreeCommasBotEvent take profit: %v", err)
}

Expand Down Expand Up @@ -344,7 +345,7 @@ func TestStorageListLatestHyperliquidSafetyStatuses(t *testing.T) {
store := newTestStorageWithLogger(t, nil)
want := tc.setup(t, store)

got, err := store.ListLatestHyperliquidSafetyStatuses(tc.dealID)
got, err := store.ListLatestHyperliquidSafetyStatuses(context.Background(), tc.dealID)
require.NoError(t, err)
require.Equal(t, len(want), len(got), "unexpected number of safety statuses")

Expand All @@ -364,7 +365,7 @@ func TestStorageListLatestHyperliquidSafetyStatuses(t *testing.T) {
require.False(t, got[i].HLStatusRecorded.IsZero(), "expected recorded time at index %d", i)
}

filled, err := store.DealSafetiesFilled(tc.dealID)
filled, err := store.DealSafetiesFilled(context.Background(), tc.dealID)
if tc.wantFilled {
require.NoError(t, err)
require.True(t, filled, "expect deal safeties to be filled")
Expand Down
Loading