mirror of
https://github.com/aljazceru/ark.git
synced 2025-12-17 12:14:21 +01:00
* 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
92 lines
2.6 KiB
Go
92 lines
2.6 KiB
Go
package application
|
|
|
|
import (
|
|
"github.com/stretchr/testify/require"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestNextMarketHour(t *testing.T) {
|
|
marketHourStartTime := parseTime(t, "2023-10-10 13:00:00")
|
|
marketHourEndTime := parseTime(t, "2023-10-10 14:00:00")
|
|
period := 3 * time.Hour
|
|
|
|
testCases := []struct {
|
|
now time.Time
|
|
expectedStart time.Time
|
|
expectedEnd time.Time
|
|
expectError bool
|
|
description string
|
|
}{
|
|
{
|
|
now: parseTime(t, "2023-10-10 13:00:00"),
|
|
expectedStart: parseTime(t, "2023-10-10 13:00:00"),
|
|
expectedEnd: parseTime(t, "2023-10-10 14:00:00"),
|
|
expectError: false,
|
|
description: "Now is exactly at the initial market hour start time",
|
|
},
|
|
{
|
|
now: parseTime(t, "2023-10-10 13:55:00"),
|
|
expectedStart: parseTime(t, "2023-10-10 13:00:00"),
|
|
expectedEnd: parseTime(t, "2023-10-10 14:00:00"),
|
|
expectError: false,
|
|
description: "Now is during the market period, equals to delta",
|
|
},
|
|
{
|
|
now: parseTime(t, "2023-10-10 13:56:00"),
|
|
expectedStart: parseTime(t, "2023-10-10 16:00:00"),
|
|
expectedEnd: parseTime(t, "2023-10-10 17:00:00"),
|
|
expectError: false,
|
|
description: "Now is during the market period, but after delta",
|
|
},
|
|
{
|
|
now: parseTime(t, "2023-10-10 14:06:00"),
|
|
expectedStart: parseTime(t, "2023-10-10 16:00:00"),
|
|
expectedEnd: parseTime(t, "2023-10-10 17:00:00"),
|
|
expectError: false,
|
|
description: "Now is after market period",
|
|
},
|
|
{
|
|
now: parseTime(t, "2023-10-10 23:06:00"),
|
|
expectedStart: parseTime(t, "2023-10-11 01:00:00"),
|
|
expectedEnd: parseTime(t, "2023-10-11 02:00:00"),
|
|
expectError: false,
|
|
description: "More periods, return next round",
|
|
},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.description, func(t *testing.T) {
|
|
startTime, endTime, err := calcNextMarketHour(
|
|
marketHourStartTime,
|
|
marketHourEndTime,
|
|
period,
|
|
marketHourDelta,
|
|
tc.now,
|
|
)
|
|
if tc.expectError {
|
|
if err == nil {
|
|
t.Errorf("Expected an error but got none")
|
|
}
|
|
} else {
|
|
if err != nil {
|
|
t.Errorf("Did not expect an error but got: %v", err)
|
|
}
|
|
if !startTime.Equal(tc.expectedStart) {
|
|
t.Errorf("Expected start time %v, got %v", tc.expectedStart.UTC(), startTime.UTC())
|
|
}
|
|
if !endTime.Equal(tc.expectedEnd) {
|
|
t.Errorf("Expected end time %v, got %v", tc.expectedEnd.UTC(), endTime.UTC())
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func parseTime(t *testing.T, value string) time.Time {
|
|
layout := "2006-01-02 15:04:05"
|
|
tm, err := time.ParseInLocation(layout, value, time.UTC)
|
|
require.NoError(t, err)
|
|
return tm
|
|
}
|