diff --git a/app.compose.yml b/app.compose.yml index 2e1f913..4e89fb4 100644 --- a/app.compose.yml +++ b/app.compose.yml @@ -23,27 +23,27 @@ services: - "5080:5080" volumes: - dexon_openobserve_data:/data - api: - image: dexon-api - container_name: dexon-api - build: - context: . - dockerfile: Dockerfile - ports: - - 12345:12345 - volumes: - - ./.env:/app/.env - environment: - POSTGRES_URL: postgresql://postgres:postgres@timescaledb:5432/dexon?sslmode=disable - depends_on: - - timescaledb - - openobserve - healthcheck: - test: ["CMD", "curl", "-f", "http://localhost:12345/health"] - interval: 30s - timeout: 10s - retries: 5 - restart: always +# api: +# image: dexon-api +# container_name: dexon-api +# build: +# context: . +# dockerfile: Dockerfile +# ports: +# - 12345:12345 +# volumes: +# - ./.env:/app/.env +# environment: +# POSTGRES_URL: postgresql://postgres:postgres@timescaledb:5432/dexon?sslmode=disable +# depends_on: +# - timescaledb +# - openobserve +# healthcheck: +# test: ["CMD", "curl", "-f", "http://localhost:12345/health"] +# interval: 30s +# timeout: 10s +# retries: 5 +# restart: always volumes: dexon_timescaledb_data: diff --git a/internal/orders/services/order.go b/internal/orders/services/order.go index 3f45028..d18c48c 100644 --- a/internal/orders/services/order.go +++ b/internal/orders/services/order.go @@ -6,7 +6,6 @@ import ( "github.com/jackc/pgx/v5/pgtype" "github.com/jinzhu/copier" "github.com/zuni-lab/dexon-service/pkg/db" - "slices" "time" ) @@ -16,12 +15,7 @@ type ListOrdersByWalletQuery struct { Offset int32 `query:"offset" validate:"gte=0"` } -type ListOrdersByWalletResponseItem struct { - db.Order - Children []db.Order `json:"children"` -} - -func ListOrderByWallet(ctx context.Context, query ListOrdersByWalletQuery) ([]ListOrdersByWalletResponseItem, error) { +func ListOrderByWallet(ctx context.Context, query ListOrdersByWalletQuery) ([]db.Order, error) { var params db.GetOrdersByWalletParams if err := copier.Copy(¶ms, &query); err != nil { return nil, err @@ -32,57 +26,26 @@ func ListOrderByWallet(ctx context.Context, query ListOrdersByWalletQuery) ([]Li return nil, err } - var ( - item ListOrdersByWalletResponseItem - res = []ListOrdersByWalletResponseItem{} - ) - for _, order := range orders { - if idx := slices.IndexFunc(res, func(item ListOrdersByWalletResponseItem) bool { - return item.ID == order.ID - }); idx != -1 { - res[idx].Children = append(res[idx].Children, order.Order) - } - - err = copier.Copy(&item, &order) - if err != nil { - return nil, err - } - res = append(res, item) - } - - return res, nil + return orders, nil } type CreateOrderBody struct { Wallet string `json:"wallet" validate:"eth_addr"` - Token0 string `json:"token0" validate:"eth_addr"` - Token1 string `json:"token1" validate:"eth_addr"` + PoolIDs []string `json:"poolIds" validate:"min=1,dive,eth_addr"` Side db.OrderSide `json:"side" validate:"oneof=BUY SELL"` Type db.OrderType `json:"type" validate:"oneof=MARKET LIMIT STOP TWAP"` Price string `json:"price" validate:"numeric,gt=0"` Amount string `json:"amount" validate:"numeric,gt=0"` - TwapTotalTime *int32 `json:"twap_total_time" validate:"omitempty,gt=0"` + TwapTotalTime *int32 `json:"twapTotalTime" validate:"omitempty,gt=0"` + Slippage float64 `json:"slippage" validate:"gte=0"` } func CreateOrder(ctx context.Context, body CreateOrderBody) (*db.Order, error) { - var ( - pool db.Pool - params db.InsertOrderParams - ) - + var params db.InsertOrderParams if err := copier.Copy(¶ms, &body); err != nil { return nil, err } - pool, err := db.DB.GetPoolByToken(ctx, db.GetPoolByTokenParams{ - Token0ID: body.Token0, - Token1ID: body.Token1, - }) - if err != nil { - return nil, err - } - - params.PoolID = pool.ID if params.Type == db.OrderTypeMARKET { params.Status = db.OrderStatusFILLED _ = params.FilledAt.Scan(time.Now()) @@ -90,6 +53,13 @@ func CreateOrder(ctx context.Context, body CreateOrderBody) (*db.Order, error) { params.Status = db.OrderStatusPENDING } + pools, err := db.DB.GetPoolsByIDs(ctx, body.PoolIDs) + if err != nil { + return nil, err + } else if len(pools) != len(body.PoolIDs) { + return nil, errors.New("invalid pool ids") + } + order, err := db.DB.InsertOrder(ctx, params) if err != nil { return nil, err @@ -99,16 +69,24 @@ func CreateOrder(ctx context.Context, body CreateOrderBody) (*db.Order, error) { } func FillPartialOrder(ctx context.Context, parent db.Order, price, amount string) (*db.Order, error) { - var params db.InsertOrderParams - if err := copier.Copy(¶ms, &parent); err != nil { - return nil, err + params := db.InsertOrderParams{ + PoolIds: parent.PoolIds, + Wallet: parent.Wallet, + Status: db.OrderStatusFILLED, + Side: parent.Side, + Type: db.OrderTypeTWAP, + Amount: parent.Amount, + Slippage: parent.Slippage, + TwapIntervalSeconds: parent.TwapIntervalSeconds, + TwapExecutedTimes: parent.TwapExecutedTimes, + TwapCurrentExecutedTimes: parent.TwapCurrentExecutedTimes, + TwapMinPrice: parent.TwapMinPrice, + TwapMaxPrice: parent.TwapMaxPrice, } - _ = params.ParentID.Scan(parent.ID) - _ = params.FilledAt.Scan(time.Now()) _ = params.Price.Scan(price) _ = params.Amount.Scan(amount) - params.Status = db.OrderStatusFILLED + _ = params.FilledAt.Scan(time.Now()) order, err := db.DB.InsertOrder(ctx, params) if err != nil { @@ -137,11 +115,16 @@ func MatchOrder(ctx context.Context, price string) (*db.Order, error) { } if order.Type == db.OrderTypeLIMIT || order.Type == db.OrderTypeSTOP || - order.Type == db.OrderTypeTWAP && order.TwapAmount.Int.Cmp(order.Amount.Int) == 0 { + order.Type == db.OrderTypeTWAP && order.TwapCurrentExecutedTimes.Int32+1 == order.TwapExecutedTimes.Int32 { _ = params.FilledAt.Scan(time.Now()) params.Status = db.OrderStatusFILLED + + if order.Type == db.OrderTypeTWAP { + order.TwapCurrentExecutedTimes.Int32 = order.TwapExecutedTimes.Int32 + } } else if order.Type == db.OrderTypeTWAP { params.Status = db.OrderStatusPARTIALFILLED + // TODO: create child order } else { return nil, errors.New("invalid order") } diff --git a/pkg/db/migration/000003_update_orders_table.down.sql b/pkg/db/migration/000003_update_orders_table.down.sql new file mode 100644 index 0000000..068faea --- /dev/null +++ b/pkg/db/migration/000003_update_orders_table.down.sql @@ -0,0 +1,15 @@ +ALTER TABLE orders +DROP COLUMN pool_ids, + ADD COLUMN pool_id VARCHAR(42) NOT NULL, + DROP COLUMN slippage, + DROP COLUMN twap_interval_seconds, + DROP COLUMN twap_executed_times, + DROP COLUMN twap_current_executed_times, + DROP COLUMN twap_min_price, + DROP COLUMN twap_max_price, + DROP COLUMN deadline, + DROP COLUMN rejected_at; + DROP COLUMN signature; + +ALTER TABLE orders + ADD CONSTRAINT orders_pool_id_fkey FOREIGN KEY (pool_id) REFERENCES pools(id) ON DELETE CASCADE; \ No newline at end of file diff --git a/pkg/db/migration/000003_update_orders_table.up.sql b/pkg/db/migration/000003_update_orders_table.up.sql new file mode 100644 index 0000000..ec094b0 --- /dev/null +++ b/pkg/db/migration/000003_update_orders_table.up.sql @@ -0,0 +1,15 @@ +ALTER TABLE orders +DROP CONSTRAINT orders_pool_id_fkey; + +ALTER TABLE orders +DROP COLUMN pool_id, + ADD COLUMN pool_ids VARCHAR(42)[] NOT NULL, + ADD COLUMN slippage DOUBLE PRECISION, + ADD COLUMN twap_interval_seconds INT, + ADD COLUMN twap_executed_times INT, + ADD COLUMN twap_current_executed_times INT, + ADD COLUMN twap_min_price NUMERIC(78,18), + ADD COLUMN twap_max_price NUMERIC(78,18), + ADD COLUMN deadline TIMESTAMP, + ADD COLUMN rejected_at TIMESTAMP, + ADD COLUMN signature VARCHAR(256); \ No newline at end of file diff --git a/pkg/db/models.go b/pkg/db/models.go index 97c2416..6808bc8 100644 --- a/pkg/db/models.go +++ b/pkg/db/models.go @@ -143,21 +143,30 @@ func (ns NullOrderType) Value() (driver.Value, error) { } type Order struct { - ID int64 `json:"id"` - PoolID string `json:"pool_id"` - ParentID pgtype.Int8 `json:"parent_id"` - Wallet pgtype.Text `json:"wallet"` - Status OrderStatus `json:"status"` - Side OrderSide `json:"side"` - Type OrderType `json:"type"` - Price pgtype.Numeric `json:"price"` - Amount pgtype.Numeric `json:"amount"` - TwapAmount pgtype.Numeric `json:"twap_amount"` - TwapParts pgtype.Int4 `json:"twap_parts"` - PartialFilledAt pgtype.Timestamptz `json:"partial_filled_at"` - FilledAt pgtype.Timestamptz `json:"filled_at"` - CancelledAt pgtype.Timestamptz `json:"cancelled_at"` - CreatedAt pgtype.Timestamptz `json:"created_at"` + ID int64 `json:"id"` + ParentID pgtype.Int8 `json:"parent_id"` + Wallet pgtype.Text `json:"wallet"` + Status OrderStatus `json:"status"` + Side OrderSide `json:"side"` + Type OrderType `json:"type"` + Price pgtype.Numeric `json:"price"` + Amount pgtype.Numeric `json:"amount"` + TwapAmount pgtype.Numeric `json:"twap_amount"` + TwapParts pgtype.Int4 `json:"twap_parts"` + PartialFilledAt pgtype.Timestamptz `json:"partial_filled_at"` + FilledAt pgtype.Timestamptz `json:"filled_at"` + CancelledAt pgtype.Timestamptz `json:"cancelled_at"` + CreatedAt pgtype.Timestamptz `json:"created_at"` + PoolIds []string `json:"pool_ids"` + Slippage pgtype.Float8 `json:"slippage"` + TwapIntervalSeconds pgtype.Int4 `json:"twap_interval_seconds"` + TwapExecutedTimes pgtype.Int4 `json:"twap_executed_times"` + TwapCurrentExecutedTimes pgtype.Int4 `json:"twap_current_executed_times"` + TwapMinPrice pgtype.Numeric `json:"twap_min_price"` + TwapMaxPrice pgtype.Numeric `json:"twap_max_price"` + Deadline pgtype.Timestamp `json:"deadline"` + RejectedAt pgtype.Timestamp `json:"rejected_at"` + Signature pgtype.Text `json:"signature"` } type Pool struct { diff --git a/pkg/db/orders.sql.go b/pkg/db/orders.sql.go index c14e298..5cd6de9 100644 --- a/pkg/db/orders.sql.go +++ b/pkg/db/orders.sql.go @@ -12,7 +12,7 @@ import ( ) const getMatchedOrder = `-- name: GetMatchedOrder :one -SELECT id, pool_id, parent_id, wallet, status, side, type, price, amount, twap_amount, twap_parts, partial_filled_at, filled_at, cancelled_at, created_at FROM orders +SELECT id, parent_id, wallet, status, side, type, price, amount, twap_amount, twap_parts, partial_filled_at, filled_at, cancelled_at, created_at, pool_ids, slippage, twap_interval_seconds, twap_executed_times, twap_current_executed_times, twap_min_price, twap_max_price, deadline, rejected_at, signature FROM orders WHERE ( (side = 'BUY' AND type = 'LIMIT' AND price <= $1) OR (side = 'SELL' AND type = 'LIMIT' AND price >= $1) @@ -22,7 +22,6 @@ WHERE ( OR (side = 'SELL' AND type = 'TWAP' AND price >= $1) ) AND status IN ('PENDING', 'PARTIAL_FILLED') - AND type <> 'TWAP' ORDER BY created_at ASC LIMIT 1 ` @@ -32,7 +31,6 @@ func (q *Queries) GetMatchedOrder(ctx context.Context, price pgtype.Numeric) (Or var i Order err := row.Scan( &i.ID, - &i.PoolID, &i.ParentID, &i.Wallet, &i.Status, @@ -46,12 +44,22 @@ func (q *Queries) GetMatchedOrder(ctx context.Context, price pgtype.Numeric) (Or &i.FilledAt, &i.CancelledAt, &i.CreatedAt, + &i.PoolIds, + &i.Slippage, + &i.TwapIntervalSeconds, + &i.TwapExecutedTimes, + &i.TwapCurrentExecutedTimes, + &i.TwapMinPrice, + &i.TwapMaxPrice, + &i.Deadline, + &i.RejectedAt, + &i.Signature, ) return i, err } const getOrdersByStatus = `-- name: GetOrdersByStatus :many -SELECT id, pool_id, parent_id, wallet, status, side, type, price, amount, twap_amount, twap_parts, partial_filled_at, filled_at, cancelled_at, created_at FROM orders +SELECT id, parent_id, wallet, status, side, type, price, amount, twap_amount, twap_parts, partial_filled_at, filled_at, cancelled_at, created_at, pool_ids, slippage, twap_interval_seconds, twap_executed_times, twap_current_executed_times, twap_min_price, twap_max_price, deadline, rejected_at, signature FROM orders WHERE status = ANY($1::varchar[]) ` @@ -66,7 +74,6 @@ func (q *Queries) GetOrdersByStatus(ctx context.Context, status []string) ([]Ord var i Order if err := rows.Scan( &i.ID, - &i.PoolID, &i.ParentID, &i.Wallet, &i.Status, @@ -80,6 +87,16 @@ func (q *Queries) GetOrdersByStatus(ctx context.Context, status []string) ([]Ord &i.FilledAt, &i.CancelledAt, &i.CreatedAt, + &i.PoolIds, + &i.Slippage, + &i.TwapIntervalSeconds, + &i.TwapExecutedTimes, + &i.TwapCurrentExecutedTimes, + &i.TwapMinPrice, + &i.TwapMaxPrice, + &i.Deadline, + &i.RejectedAt, + &i.Signature, ); err != nil { return nil, err } @@ -92,10 +109,9 @@ func (q *Queries) GetOrdersByStatus(ctx context.Context, status []string) ([]Ord } const getOrdersByWallet = `-- name: GetOrdersByWallet :many -SELECT o1.id, o1.pool_id, o1.parent_id, o1.wallet, o1.status, o1.side, o1.type, o1.price, o1.amount, o1.twap_amount, o1.twap_parts, o1.partial_filled_at, o1.filled_at, o1.cancelled_at, o1.created_at, o2.id, o2.pool_id, o2.parent_id, o2.wallet, o2.status, o2.side, o2.type, o2.price, o2.amount, o2.twap_amount, o2.twap_parts, o2.partial_filled_at, o2.filled_at, o2.cancelled_at, o2.created_at FROM orders AS o1 -LEFT JOIN orders AS o2 ON o1.id = o2.parent_id AND o2.parent_id IS NOT NULL -WHERE o1.wallet = $1 -ORDER BY o1.created_at DESC +SELECT id, parent_id, wallet, status, side, type, price, amount, twap_amount, twap_parts, partial_filled_at, filled_at, cancelled_at, created_at, pool_ids, slippage, twap_interval_seconds, twap_executed_times, twap_current_executed_times, twap_min_price, twap_max_price, deadline, rejected_at, signature FROM orders +WHERE wallet = $1 +ORDER BY created_at DESC LIMIT $2 OFFSET $3 ` @@ -105,37 +121,17 @@ type GetOrdersByWalletParams struct { Offset int32 `json:"offset"` } -type GetOrdersByWalletRow struct { - ID int64 `json:"id"` - PoolID string `json:"pool_id"` - ParentID pgtype.Int8 `json:"parent_id"` - Wallet pgtype.Text `json:"wallet"` - Status OrderStatus `json:"status"` - Side OrderSide `json:"side"` - Type OrderType `json:"type"` - Price pgtype.Numeric `json:"price"` - Amount pgtype.Numeric `json:"amount"` - TwapAmount pgtype.Numeric `json:"twap_amount"` - TwapParts pgtype.Int4 `json:"twap_parts"` - PartialFilledAt pgtype.Timestamptz `json:"partial_filled_at"` - FilledAt pgtype.Timestamptz `json:"filled_at"` - CancelledAt pgtype.Timestamptz `json:"cancelled_at"` - CreatedAt pgtype.Timestamptz `json:"created_at"` - Order Order `json:"order"` -} - -func (q *Queries) GetOrdersByWallet(ctx context.Context, arg GetOrdersByWalletParams) ([]GetOrdersByWalletRow, error) { +func (q *Queries) GetOrdersByWallet(ctx context.Context, arg GetOrdersByWalletParams) ([]Order, error) { rows, err := q.db.Query(ctx, getOrdersByWallet, arg.Wallet, arg.Limit, arg.Offset) if err != nil { return nil, err } defer rows.Close() - items := []GetOrdersByWalletRow{} + items := []Order{} for rows.Next() { - var i GetOrdersByWalletRow + var i Order if err := rows.Scan( &i.ID, - &i.PoolID, &i.ParentID, &i.Wallet, &i.Status, @@ -149,21 +145,16 @@ func (q *Queries) GetOrdersByWallet(ctx context.Context, arg GetOrdersByWalletPa &i.FilledAt, &i.CancelledAt, &i.CreatedAt, - &i.Order.ID, - &i.Order.PoolID, - &i.Order.ParentID, - &i.Order.Wallet, - &i.Order.Status, - &i.Order.Side, - &i.Order.Type, - &i.Order.Price, - &i.Order.Amount, - &i.Order.TwapAmount, - &i.Order.TwapParts, - &i.Order.PartialFilledAt, - &i.Order.FilledAt, - &i.Order.CancelledAt, - &i.Order.CreatedAt, + &i.PoolIds, + &i.Slippage, + &i.TwapIntervalSeconds, + &i.TwapExecutedTimes, + &i.TwapCurrentExecutedTimes, + &i.TwapMinPrice, + &i.TwapMaxPrice, + &i.Deadline, + &i.RejectedAt, + &i.Signature, ); err != nil { return nil, err } @@ -176,47 +167,70 @@ func (q *Queries) GetOrdersByWallet(ctx context.Context, arg GetOrdersByWalletPa } const insertOrder = `-- name: InsertOrder :one -INSERT INTO orders (parent_id, wallet, pool_id, side, status, type, price, amount, twap_amount, twap_parts, filled_at, partial_filled_at, cancelled_at) -VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13) -RETURNING id, pool_id, parent_id, wallet, status, side, type, price, amount, twap_amount, twap_parts, partial_filled_at, filled_at, cancelled_at, created_at +INSERT INTO orders ( + pool_ids, parent_id, wallet, status, side, type, + price, amount, slippage, twap_interval_seconds, + twap_executed_times, twap_current_executed_times, + twap_min_price, twap_max_price, deadline, + partial_filled_at, filled_at, rejected_at, + cancelled_at, created_at) +VALUES ($1, $2, $3, $4, $5, $6, + $7, $8, $9, $10, + $11, $12, $13, + $14, $15, $16, + $17, $18, $19, $20) +RETURNING id, parent_id, wallet, status, side, type, price, amount, twap_amount, twap_parts, partial_filled_at, filled_at, cancelled_at, created_at, pool_ids, slippage, twap_interval_seconds, twap_executed_times, twap_current_executed_times, twap_min_price, twap_max_price, deadline, rejected_at, signature ` type InsertOrderParams struct { - ParentID pgtype.Int8 `json:"parent_id"` - Wallet pgtype.Text `json:"wallet"` - PoolID string `json:"pool_id"` - Side OrderSide `json:"side"` - Status OrderStatus `json:"status"` - Type OrderType `json:"type"` - Price pgtype.Numeric `json:"price"` - Amount pgtype.Numeric `json:"amount"` - TwapAmount pgtype.Numeric `json:"twap_amount"` - TwapParts pgtype.Int4 `json:"twap_parts"` - FilledAt pgtype.Timestamptz `json:"filled_at"` - PartialFilledAt pgtype.Timestamptz `json:"partial_filled_at"` - CancelledAt pgtype.Timestamptz `json:"cancelled_at"` + PoolIds []string `json:"pool_ids"` + ParentID pgtype.Int8 `json:"parent_id"` + Wallet pgtype.Text `json:"wallet"` + Status OrderStatus `json:"status"` + Side OrderSide `json:"side"` + Type OrderType `json:"type"` + Price pgtype.Numeric `json:"price"` + Amount pgtype.Numeric `json:"amount"` + Slippage pgtype.Float8 `json:"slippage"` + TwapIntervalSeconds pgtype.Int4 `json:"twap_interval_seconds"` + TwapExecutedTimes pgtype.Int4 `json:"twap_executed_times"` + TwapCurrentExecutedTimes pgtype.Int4 `json:"twap_current_executed_times"` + TwapMinPrice pgtype.Numeric `json:"twap_min_price"` + TwapMaxPrice pgtype.Numeric `json:"twap_max_price"` + Deadline pgtype.Timestamp `json:"deadline"` + PartialFilledAt pgtype.Timestamptz `json:"partial_filled_at"` + FilledAt pgtype.Timestamptz `json:"filled_at"` + RejectedAt pgtype.Timestamp `json:"rejected_at"` + CancelledAt pgtype.Timestamptz `json:"cancelled_at"` + CreatedAt pgtype.Timestamptz `json:"created_at"` } func (q *Queries) InsertOrder(ctx context.Context, arg InsertOrderParams) (Order, error) { row := q.db.QueryRow(ctx, insertOrder, + arg.PoolIds, arg.ParentID, arg.Wallet, - arg.PoolID, - arg.Side, arg.Status, + arg.Side, arg.Type, arg.Price, arg.Amount, - arg.TwapAmount, - arg.TwapParts, - arg.FilledAt, + arg.Slippage, + arg.TwapIntervalSeconds, + arg.TwapExecutedTimes, + arg.TwapCurrentExecutedTimes, + arg.TwapMinPrice, + arg.TwapMaxPrice, + arg.Deadline, arg.PartialFilledAt, + arg.FilledAt, + arg.RejectedAt, arg.CancelledAt, + arg.CreatedAt, ) var i Order err := row.Scan( &i.ID, - &i.PoolID, &i.ParentID, &i.Wallet, &i.Status, @@ -230,6 +244,16 @@ func (q *Queries) InsertOrder(ctx context.Context, arg InsertOrderParams) (Order &i.FilledAt, &i.CancelledAt, &i.CreatedAt, + &i.PoolIds, + &i.Slippage, + &i.TwapIntervalSeconds, + &i.TwapExecutedTimes, + &i.TwapCurrentExecutedTimes, + &i.TwapMinPrice, + &i.TwapMaxPrice, + &i.Deadline, + &i.RejectedAt, + &i.Signature, ) return i, err } @@ -238,39 +262,38 @@ const updateOrder = `-- name: UpdateOrder :one UPDATE orders SET status = COALESCE($2, status), - twap_amount = COALESCE($3, twap_amount), + twap_current_executed_times = COALESCE($3, twap_current_executed_times), filled_at = COALESCE($4, filled_at), cancelled_at = COALESCE($5, cancelled_at), - twap_amount = COALESCE($6, twap_amount), - partial_filled_at = COALESCE($7, partial_filled_at) + partial_filled_at = COALESCE($6, partial_filled_at), + rejected_at = COALESCE($7, rejected_at) WHERE id = $1 -RETURNING id, pool_id, parent_id, wallet, status, side, type, price, amount, twap_amount, twap_parts, partial_filled_at, filled_at, cancelled_at, created_at +RETURNING id, parent_id, wallet, status, side, type, price, amount, twap_amount, twap_parts, partial_filled_at, filled_at, cancelled_at, created_at, pool_ids, slippage, twap_interval_seconds, twap_executed_times, twap_current_executed_times, twap_min_price, twap_max_price, deadline, rejected_at, signature ` type UpdateOrderParams struct { - ID int64 `json:"id"` - Status OrderStatus `json:"status"` - TwapAmount pgtype.Numeric `json:"twap_amount"` - FilledAt pgtype.Timestamptz `json:"filled_at"` - CancelledAt pgtype.Timestamptz `json:"cancelled_at"` - TwapAmount_2 pgtype.Numeric `json:"twap_amount_2"` - PartialFilledAt pgtype.Timestamptz `json:"partial_filled_at"` + ID int64 `json:"id"` + Status OrderStatus `json:"status"` + TwapCurrentExecutedTimes pgtype.Int4 `json:"twap_current_executed_times"` + FilledAt pgtype.Timestamptz `json:"filled_at"` + CancelledAt pgtype.Timestamptz `json:"cancelled_at"` + PartialFilledAt pgtype.Timestamptz `json:"partial_filled_at"` + RejectedAt pgtype.Timestamp `json:"rejected_at"` } func (q *Queries) UpdateOrder(ctx context.Context, arg UpdateOrderParams) (Order, error) { row := q.db.QueryRow(ctx, updateOrder, arg.ID, arg.Status, - arg.TwapAmount, + arg.TwapCurrentExecutedTimes, arg.FilledAt, arg.CancelledAt, - arg.TwapAmount_2, arg.PartialFilledAt, + arg.RejectedAt, ) var i Order err := row.Scan( &i.ID, - &i.PoolID, &i.ParentID, &i.Wallet, &i.Status, @@ -284,6 +307,16 @@ func (q *Queries) UpdateOrder(ctx context.Context, arg UpdateOrderParams) (Order &i.FilledAt, &i.CancelledAt, &i.CreatedAt, + &i.PoolIds, + &i.Slippage, + &i.TwapIntervalSeconds, + &i.TwapExecutedTimes, + &i.TwapCurrentExecutedTimes, + &i.TwapMinPrice, + &i.TwapMaxPrice, + &i.Deadline, + &i.RejectedAt, + &i.Signature, ) return i, err } diff --git a/pkg/db/pools.sql.go b/pkg/db/pools.sql.go index 493df86..b05aa73 100644 --- a/pkg/db/pools.sql.go +++ b/pkg/db/pools.sql.go @@ -101,6 +101,36 @@ func (q *Queries) GetPools(ctx context.Context) ([]Pool, error) { return items, nil } +const getPoolsByIDs = `-- name: GetPoolsByIDs :many +SELECT id, token0_id, token1_id, created_at FROM pools +WHERE id = ANY($1::varchar[]) +` + +func (q *Queries) GetPoolsByIDs(ctx context.Context, ids []string) ([]Pool, error) { + rows, err := q.db.Query(ctx, getPoolsByIDs, ids) + if err != nil { + return nil, err + } + defer rows.Close() + items := []Pool{} + for rows.Next() { + var i Pool + if err := rows.Scan( + &i.ID, + &i.Token0ID, + &i.Token1ID, + &i.CreatedAt, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + const poolDetails = `-- name: PoolDetails :one SELECT pools.id, diff --git a/pkg/db/querier.go b/pkg/db/querier.go index d680cf0..1b51c9e 100644 --- a/pkg/db/querier.go +++ b/pkg/db/querier.go @@ -17,10 +17,11 @@ type Querier interface { GetMarketData(ctx context.Context, arg GetMarketDataParams) ([]GetMarketDataRow, error) GetMatchedOrder(ctx context.Context, price pgtype.Numeric) (Order, error) GetOrdersByStatus(ctx context.Context, status []string) ([]Order, error) - GetOrdersByWallet(ctx context.Context, arg GetOrdersByWalletParams) ([]GetOrdersByWalletRow, error) + GetOrdersByWallet(ctx context.Context, arg GetOrdersByWalletParams) ([]Order, error) GetPool(ctx context.Context, id string) (Pool, error) GetPoolByToken(ctx context.Context, arg GetPoolByTokenParams) (Pool, error) GetPools(ctx context.Context) ([]Pool, error) + GetPoolsByIDs(ctx context.Context, ids []string) ([]Pool, error) GetPriceByPoolID(ctx context.Context, poolID string) (Price, error) GetPrices(ctx context.Context, arg GetPricesParams) ([]Price, error) InsertOrder(ctx context.Context, arg InsertOrderParams) (Order, error) diff --git a/pkg/db/query/orders.sql b/pkg/db/query/orders.sql index b010a74..55df927 100644 --- a/pkg/db/query/orders.sql +++ b/pkg/db/query/orders.sql @@ -1,13 +1,22 @@ -- name: InsertOrder :one -INSERT INTO orders (parent_id, wallet, pool_id, side, status, type, price, amount, twap_amount, twap_parts, filled_at, partial_filled_at, cancelled_at) -VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13) +INSERT INTO orders ( + pool_ids, parent_id, wallet, status, side, type, + price, amount, slippage, twap_interval_seconds, + twap_executed_times, twap_current_executed_times, + twap_min_price, twap_max_price, deadline, + partial_filled_at, filled_at, rejected_at, + cancelled_at, created_at) +VALUES ($1, $2, $3, $4, $5, $6, + $7, $8, $9, $10, + $11, $12, $13, + $14, $15, $16, + $17, $18, $19, $20) RETURNING *; -- name: GetOrdersByWallet :many -SELECT o1.*, sqlc.embed(o2) FROM orders AS o1 -LEFT JOIN orders AS o2 ON o1.id = o2.parent_id AND o2.parent_id IS NOT NULL -WHERE o1.wallet = $1 -ORDER BY o1.created_at DESC +SELECT * FROM orders +WHERE wallet = $1 +ORDER BY created_at DESC LIMIT $2 OFFSET $3; -- name: GetOrdersByStatus :many @@ -25,7 +34,6 @@ WHERE ( OR (side = 'SELL' AND type = 'TWAP' AND price >= $1) ) AND status IN ('PENDING', 'PARTIAL_FILLED') - AND type <> 'TWAP' ORDER BY created_at ASC LIMIT 1; @@ -33,10 +41,10 @@ LIMIT 1; UPDATE orders SET status = COALESCE($2, status), - twap_amount = COALESCE($3, twap_amount), + twap_current_executed_times = COALESCE($3, twap_current_executed_times), filled_at = COALESCE($4, filled_at), cancelled_at = COALESCE($5, cancelled_at), - twap_amount = COALESCE($6, twap_amount), - partial_filled_at = COALESCE($7, partial_filled_at) + partial_filled_at = COALESCE($6, partial_filled_at), + rejected_at = COALESCE($7, rejected_at) WHERE id = $1 RETURNING *; diff --git a/pkg/db/query/pools.sql b/pkg/db/query/pools.sql index fa0bc6b..551d352 100644 --- a/pkg/db/query/pools.sql +++ b/pkg/db/query/pools.sql @@ -5,6 +5,10 @@ SELECT * FROM pools; SELECT * FROM pools WHERE id = $1 LIMIT 1; +-- name: GetPoolsByIDs :many +SELECT * FROM pools +WHERE id = ANY(@ids::varchar[]); + -- name: GetPoolByToken :one SELECT * FROM pools WHERE token0_id = $1 AND token1_id = $2 LIMIT 1;