Files
ark/server/internal/interface/grpc/handlers/adminservice.go
Dusan Sekulic ae3ccb3579 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
2024-11-22 10:36:51 +01:00

174 lines
4.9 KiB
Go

package handlers
import (
"context"
"fmt"
"google.golang.org/protobuf/types/known/durationpb"
"google.golang.org/protobuf/types/known/timestamppb"
arkv1 "github.com/ark-network/ark/api-spec/protobuf/gen/ark/v1"
"github.com/ark-network/ark/server/internal/core/application"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
type adminHandler struct {
adminService application.AdminService
aspService application.Service
noteUriPrefix string
}
func NewAdminHandler(
adminService application.AdminService, aspService application.Service, noteUriPrefix string,
) arkv1.AdminServiceServer {
return &adminHandler{adminService, aspService, noteUriPrefix}
}
func (a *adminHandler) GetRoundDetails(ctx context.Context, req *arkv1.GetRoundDetailsRequest) (*arkv1.GetRoundDetailsResponse, error) {
id := req.GetRoundId()
if len(id) == 0 {
return nil, status.Error(codes.InvalidArgument, "missing round id")
}
details, err := a.adminService.GetRoundDetails(ctx, id)
if err != nil {
return nil, err
}
return &arkv1.GetRoundDetailsResponse{
RoundId: details.RoundId,
Txid: details.TxId,
ForfeitedAmount: convertSatoshis(details.ForfeitedAmount),
TotalVtxosAmount: convertSatoshis(details.TotalVtxosAmount),
TotalExitAmount: convertSatoshis(details.TotalExitAmount),
FeesAmount: convertSatoshis(details.FeesAmount),
InputsVtxos: details.InputsVtxos,
OutputsVtxos: details.OutputsVtxos,
ExitAddresses: details.ExitAddresses,
}, nil
}
func (a *adminHandler) GetRounds(ctx context.Context, req *arkv1.GetRoundsRequest) (*arkv1.GetRoundsResponse, error) {
startAfter := req.GetAfter()
startBefore := req.GetBefore()
if startAfter < 0 {
return nil, status.Error(codes.InvalidArgument, "invalid after (must be >= 0)")
}
if startBefore < 0 {
return nil, status.Error(codes.InvalidArgument, "invalid before (must be >= 0)")
}
if startAfter >= startBefore {
return nil, status.Error(codes.InvalidArgument, "invalid range")
}
rounds, err := a.adminService.GetRounds(ctx, startAfter, startBefore)
if err != nil {
return nil, err
}
return &arkv1.GetRoundsResponse{Rounds: rounds}, nil
}
func (a *adminHandler) GetScheduledSweep(ctx context.Context, _ *arkv1.GetScheduledSweepRequest) (*arkv1.GetScheduledSweepResponse, error) {
scheduledSweeps, err := a.adminService.GetScheduledSweeps(ctx)
if err != nil {
return nil, err
}
sweeps := make([]*arkv1.ScheduledSweep, 0)
for _, sweep := range scheduledSweeps {
outputs := make([]*arkv1.SweepableOutput, 0)
for _, output := range sweep.SweepableOutputs {
outputs = append(outputs, &arkv1.SweepableOutput{
Txid: output.TxId,
Vout: output.Vout,
ScheduledAt: output.ScheduledAt,
Amount: convertSatoshis(output.Amount),
})
}
sweeps = append(sweeps, &arkv1.ScheduledSweep{
RoundId: sweep.RoundId,
Outputs: outputs,
})
}
return &arkv1.GetScheduledSweepResponse{Sweeps: sweeps}, nil
}
func (a *adminHandler) CreateNote(ctx context.Context, req *arkv1.CreateNoteRequest) (*arkv1.CreateNoteResponse, error) {
amount := req.GetAmount()
quantity := req.GetQuantity()
if quantity == 0 {
quantity = 1
}
if amount == 0 {
return nil, status.Error(codes.InvalidArgument, "amount must be greater than 0")
}
notes, err := a.adminService.CreateNotes(ctx, amount, int(quantity))
if err != nil {
return nil, err
}
if len(a.noteUriPrefix) > 0 {
notesWithURI := make([]string, 0, len(notes))
for _, note := range notes {
notesWithURI = append(notesWithURI, fmt.Sprintf("%s://%s", a.noteUriPrefix, note))
}
return &arkv1.CreateNoteResponse{Notes: notesWithURI}, nil
}
return &arkv1.CreateNoteResponse{Notes: notes}, nil
}
func (a *adminHandler) GetMarketHourConfig(
ctx context.Context,
request *arkv1.GetMarketHourConfigRequest,
) (*arkv1.GetMarketHourConfigResponse, error) {
config, err := a.aspService.GetMarketHourConfig(ctx)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &arkv1.GetMarketHourConfigResponse{
Config: &arkv1.MarketHourConfig{
StartTime: timestamppb.New(config.StartTime),
EndTime: timestamppb.New(config.EndTime),
Period: durationpb.New(config.Period),
RoundInterval: durationpb.New(config.RoundInterval),
},
}, nil
}
func (a *adminHandler) UpdateMarketHourConfig(
ctx context.Context,
req *arkv1.UpdateMarketHourConfigRequest,
) (*arkv1.UpdateMarketHourConfigResponse, error) {
if err := a.aspService.UpdateMarketHourConfig(
ctx,
req.GetConfig().GetStartTime().AsTime(),
req.GetConfig().GetEndTime().AsTime(),
req.GetConfig().GetPeriod().AsDuration(),
req.GetConfig().GetRoundInterval().AsDuration(),
); err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &arkv1.UpdateMarketHourConfigResponse{}, nil
}
// convert sats to string BTC
func convertSatoshis(sats uint64) string {
btc := float64(sats) * 1e-8
return fmt.Sprintf("%.8f", btc)
}