Add support for announcing market hours (#380)

* feat: add market hour configuration for optimal payment timing

This commit adds market hour configuration to help users determine optimal
times for making payments with lower fees. The configuration is managed
through environment variables and exposed via the GetInfo RPC.

Changes:
- Add MarketHour message type to protobuf service definition
- Add market hour configuration fields to Config struct
- Update covenant and covenantless services to handle market hour data
- Extend GetInfo RPC response to include market hour information
- Set default market hour period to 24 hours
- Initialize market hour fields after other service fields

Configuration:
- ARK_FIRST_MARKET_HOUR: Initial market hour timestamp
  (default: current server start time)
- ARK_MARKET_HOUR_PERIOD: Time between market hours in seconds
  (default: 86400)
- ARK_MARKET_HOUR_ROUND_LIFETIME: Round lifetime for market hours
  (default: 0, falls back to ARK_ROUND_LIFETIME)

* feat: add admin RPC for updating market hour configuration

Add new UpdateMarketHour RPC to AdminService for configuring market hour parameters:
- Add request/response messages to admin.proto
- Add UpdateMarketHour method to Service interface
- Implement market hour updates in covenant and covenantless services
- Add validation for market hour parameters
- Implement admin gRPC handler

The RPC allows updating:
- First market hour timestamp
- Market hour period
- Market hour round lifetime (optional, defaults to round lifetime

* feat: add market hour persistence with sqlite

- Add MarketHourRepo interface in domain layer
- Implement market hour persistence using SQLite
- Add market hour queries to sqlc/query.sql
- Update service initialization to load market hours from DB
- Add fallback to config values if no DB entry exists
- Update RepoManager interface with new MarketHourRepo method
This commit is contained in:
Dusan Sekulic
2024-11-22 10:36:51 +01:00
committed by GitHub
parent d6b8508f6d
commit ae3ccb3579
31 changed files with 2464 additions and 827 deletions

View File

@@ -39,6 +39,63 @@ func (q *Queries) DeleteEntityVtxo(ctx context.Context, entityID int64) error {
return err
}
const getLatestMarketHour = `-- name: GetLatestMarketHour :one
SELECT id, start_time, end_time, period, round_interval, updated_at FROM market_hour ORDER BY updated_at DESC LIMIT 1
`
func (q *Queries) GetLatestMarketHour(ctx context.Context) (MarketHour, error) {
row := q.db.QueryRowContext(ctx, getLatestMarketHour)
var i MarketHour
err := row.Scan(
&i.ID,
&i.StartTime,
&i.EndTime,
&i.Period,
&i.RoundInterval,
&i.UpdatedAt,
)
return i, err
}
const insertMarketHour = `-- name: InsertMarketHour :one
INSERT INTO market_hour (
start_time,
end_time,
period,
round_interval,
updated_at
) VALUES (?, ?, ?, ?, ?)
RETURNING id, start_time, end_time, period, round_interval, updated_at
`
type InsertMarketHourParams struct {
StartTime int64
EndTime int64
Period int64
RoundInterval int64
UpdatedAt int64
}
func (q *Queries) InsertMarketHour(ctx context.Context, arg InsertMarketHourParams) (MarketHour, error) {
row := q.db.QueryRowContext(ctx, insertMarketHour,
arg.StartTime,
arg.EndTime,
arg.Period,
arg.RoundInterval,
arg.UpdatedAt,
)
var i MarketHour
err := row.Scan(
&i.ID,
&i.StartTime,
&i.EndTime,
&i.Period,
&i.RoundInterval,
&i.UpdatedAt,
)
return i, err
}
const insertNote = `-- name: InsertNote :exec
INSERT INTO note (id) VALUES (?)
`
@@ -755,6 +812,47 @@ func (q *Queries) SelectVtxosByPoolTxid(ctx context.Context, poolTx string) ([]S
return items, nil
}
const updateMarketHour = `-- name: UpdateMarketHour :one
UPDATE market_hour
SET start_time = ?,
end_time = ?,
period = ?,
round_interval = ?,
updated_at = ?
WHERE id = ?
RETURNING id, start_time, end_time, period, round_interval, updated_at
`
type UpdateMarketHourParams struct {
StartTime int64
EndTime int64
Period int64
RoundInterval int64
UpdatedAt int64
ID int64
}
func (q *Queries) UpdateMarketHour(ctx context.Context, arg UpdateMarketHourParams) (MarketHour, error) {
row := q.db.QueryRowContext(ctx, updateMarketHour,
arg.StartTime,
arg.EndTime,
arg.Period,
arg.RoundInterval,
arg.UpdatedAt,
arg.ID,
)
var i MarketHour
err := row.Scan(
&i.ID,
&i.StartTime,
&i.EndTime,
&i.Period,
&i.RoundInterval,
&i.UpdatedAt,
)
return i, err
}
const updateVtxoExpireAt = `-- name: UpdateVtxoExpireAt :exec
UPDATE vtxo SET expire_at = ? WHERE txid = ? AND vout = ?
`
@@ -786,9 +884,9 @@ func (q *Queries) UpdateVtxoPaymentId(ctx context.Context, arg UpdateVtxoPayment
}
const upsertEntity = `-- name: UpsertEntity :one
INSERT INTO entity (nostr_recipient)
VALUES (?)
ON CONFLICT(nostr_recipient) DO UPDATE SET
INSERT INTO entity (nostr_recipient)
VALUES (?)
ON CONFLICT(nostr_recipient) DO UPDATE SET
nostr_recipient = EXCLUDED.nostr_recipient
RETURNING id
`
@@ -801,9 +899,9 @@ func (q *Queries) UpsertEntity(ctx context.Context, nostrRecipient string) (int6
}
const upsertEntityVtxo = `-- name: UpsertEntityVtxo :exec
INSERT INTO entity_vtxo (entity_id, vtxo_txid, vtxo_vout)
VALUES (?, ?, ?)
ON CONFLICT(entity_id, vtxo_txid, vtxo_vout) DO UPDATE SET
INSERT INTO entity_vtxo (entity_id, vtxo_txid, vtxo_vout)
VALUES (?, ?, ?)
ON CONFLICT(entity_id, vtxo_txid, vtxo_vout) DO UPDATE SET
entity_id = EXCLUDED.entity_id
`