-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.go
More file actions
162 lines (141 loc) · 5.39 KB
/
Copy pathtypes.go
File metadata and controls
162 lines (141 loc) · 5.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//go:build wasip1
package main
import (
"fmt"
"ducat/shared"
"github.com/shopspring/decimal"
)
// Type aliases from shared package
type (
Config = shared.Config
HttpRequestData = shared.HttpRequestData
EvaluateQuotesRequest = shared.EvaluateQuotesRequest
QuoteEvaluationResult = shared.QuoteEvaluationResult
EvaluateQuotesResponse = shared.EvaluateQuotesResponse
EvaluationSummary = shared.EvaluationSummary
GenerateQuotesRequest = shared.GenerateQuotesRequest
GenerateQuotesResponse = shared.GenerateQuotesResponse
PriceEvent = shared.PriceEvent
NostrEvent = shared.NostrEvent
)
// RelayResponse with CRE consensus tags
type RelayResponse struct {
Success bool `json:"success" consensus_aggregation:"identical"`
Message string `json:"message" consensus_aggregation:"identical"`
}
func IsValidTholdHash(hash string) bool {
return shared.IsValidTholdHash(hash)
}
// IsValidQuoteLookupDTag accepts both legacy thold-hash d-tags (40 hex)
// and current commit-hash d-tags (64 hex).
func IsValidQuoteLookupDTag(hash string) bool {
return shared.IsValidTholdHash(hash) || shared.IsValidCommitHash(hash)
}
// PriceData with CRE consensus tags
type PriceData struct {
Price decimal.Decimal `json:"price" consensus_aggregation:"median"`
Origin string `json:"origin" consensus_aggregation:"identical"`
Stamp int64 `json:"stamp" consensus_aggregation:"median"`
}
// KeyDerivation holds derived cryptographic keys
type KeyDerivation struct {
PrivateKey []byte
SchnorrPubkey string
}
// Zero securely zeroes the private key bytes
func (k *KeyDerivation) Zero() {
if k != nil && k.PrivateKey != nil {
for i := range k.PrivateKey {
k.PrivateKey[i] = 0
}
}
}
// PriceContractResponse is the Nostr event content format
type PriceContractResponse struct {
ChainNetwork string `json:"chain_network"`
OraclePubkey string `json:"oracle_pubkey"`
BasePrice int64 `json:"base_price"`
BaseStamp int64 `json:"base_stamp"`
CommitHash string `json:"commit_hash"`
ContractID string `json:"contract_id"`
OracleSig string `json:"oracle_sig"`
TholdHash string `json:"thold_hash"`
TholdKey *string `json:"thold_key"`
TholdPrice int64 `json:"thold_price"`
}
// ToPriceEvent converts to v2.5 PriceEvent format
func (p *PriceContractResponse) ToPriceEvent(network string) *shared.PriceEvent {
origin := "cre"
return &shared.PriceEvent{
SrvNetwork: network,
SrvPubkey: p.OraclePubkey,
QuoteOrigin: origin,
QuotePrice: float64(p.BasePrice),
QuoteStamp: p.BaseStamp,
LatestOrigin: origin,
LatestPrice: float64(p.BasePrice),
LatestStamp: p.BaseStamp,
EventOrigin: nil,
EventPrice: nil,
EventStamp: nil,
EventType: "active",
TholdHash: p.TholdHash,
TholdKey: p.TholdKey,
TholdPrice: float64(p.TholdPrice),
IsExpired: p.TholdKey != nil,
ReqID: p.CommitHash,
ReqSig: p.OracleSig,
}
}
// --- Price Adjustment Types ---
// AdjustmentControlDTag is the NIP-33 d-tag used for the price adjustment control event
const AdjustmentControlDTag = "price_adjustment_control"
// PriceAdjustmentRequest is the HTTP request payload for setting a price adjustment
type PriceAdjustmentRequest struct {
Pct float64 `json:"pct"` // Percentage adjustment (e.g., 5.0 = +5%, -10.0 = -10%)
DurationMinutes int `json:"duration_minutes"` // How long the adjustment lasts
}
// Validate validates the adjustment request
func (r *PriceAdjustmentRequest) Validate() error {
if r == nil {
return fmt.Errorf("request is nil")
}
if r.Pct < MinAdjustmentPct || r.Pct > MaxAdjustmentPct {
return fmt.Errorf("pct must be between %.0f and %.0f, got %.2f", MinAdjustmentPct, MaxAdjustmentPct, r.Pct)
}
if r.DurationMinutes <= 0 {
return fmt.Errorf("duration_minutes must be positive, got %d", r.DurationMinutes)
}
if r.DurationMinutes > MaxAdjustmentDurationMin {
return fmt.Errorf("duration_minutes cannot exceed %d, got %d", MaxAdjustmentDurationMin, r.DurationMinutes)
}
return nil
}
// PriceAdjustmentControl is the content stored in the Nostr control event
type PriceAdjustmentControl struct {
Pct float64 `json:"pct"` // Active percentage adjustment
ExpiresAt int64 `json:"expires_at"` // Unix timestamp when adjustment expires
SetAt int64 `json:"set_at"` // Unix timestamp when adjustment was set
}
// IsActive returns true if the adjustment is still active (not expired)
func (a *PriceAdjustmentControl) IsActive(currentTime int64) bool {
return a.Pct != 0 && currentTime < a.ExpiresAt
}
// PriceAdjustmentResponse is the HTTP response for adjust/clear/status actions
type PriceAdjustmentResponse struct {
Success bool `json:"success"`
Pct float64 `json:"pct"`
ExpiresAt int64 `json:"expires_at"`
SetAt int64 `json:"set_at"`
Active bool `json:"active"`
Message string `json:"message"`
}
// AdjustmentStatusResponse is returned by the status action with CRE consensus tags
type AdjustmentStatusResponse struct {
Success bool `json:"success" consensus_aggregation:"identical"`
Active bool `json:"active" consensus_aggregation:"identical"`
Pct float64 `json:"pct" consensus_aggregation:"median"`
ExpiresAt int64 `json:"expires_at" consensus_aggregation:"median"`
SetAt int64 `json:"set_at" consensus_aggregation:"median"`
Message string `json:"message" consensus_aggregation:"identical"`
}