@@ -6,74 +6,88 @@ import (
6
6
"sync"
7
7
"time"
8
8
9
- "github.com/code-payments/code-server/pkg/database/query"
10
9
"github.com/code-payments/code-server/pkg/code/data/currency"
10
+ "github.com/code-payments/code-server/pkg/database/query"
11
11
)
12
12
13
13
const (
14
14
dateFormat = "2006-01-02"
15
15
)
16
16
17
17
type store struct {
18
- currencyStoreMu sync.Mutex
19
- currencyStore []* currency.ExchangeRateRecord
20
- lastIndex uint64
18
+ mu sync.Mutex
19
+ exchangeRateRecords []* currency.ExchangeRateRecord
20
+ lastExchangeRateIndex uint64
21
+ reserveRecords []* currency.ReserveRecord
22
+ lastReserveIndex uint64
23
+ }
24
+
25
+ type RateByTime []* currency.ExchangeRateRecord
26
+
27
+ func (a RateByTime ) Len () int { return len (a ) }
28
+ func (a RateByTime ) Swap (i , j int ) { a [i ], a [j ] = a [j ], a [i ] }
29
+ func (a RateByTime ) Less (i , j int ) bool {
30
+ // DESC order (most recent first)
31
+ return a [i ].Time .Unix () > a [j ].Time .Unix ()
21
32
}
22
33
23
- type ByTime []* currency.ExchangeRateRecord
34
+ type ReserveByTime []* currency.ReserveRecord
24
35
25
- func (a ByTime ) Len () int { return len (a ) }
26
- func (a ByTime ) Swap (i , j int ) { a [i ], a [j ] = a [j ], a [i ] }
27
- func (a ByTime ) Less (i , j int ) bool {
36
+ func (a ReserveByTime ) Len () int { return len (a ) }
37
+ func (a ReserveByTime ) Swap (i , j int ) { a [i ], a [j ] = a [j ], a [i ] }
38
+ func (a ReserveByTime ) Less (i , j int ) bool {
28
39
// DESC order (most recent first)
29
40
return a [i ].Time .Unix () > a [j ].Time .Unix ()
30
41
}
31
42
32
43
func New () currency.Store {
33
44
return & store {
34
- currencyStore : make ([]* currency.ExchangeRateRecord , 0 ),
35
- lastIndex : 1 ,
45
+ exchangeRateRecords : make ([]* currency.ExchangeRateRecord , 0 ),
46
+ lastExchangeRateIndex : 1 ,
36
47
}
37
48
}
38
49
39
50
func (s * store ) reset () {
40
- s .currencyStoreMu .Lock ()
41
- s .currencyStore = make ([]* currency.ExchangeRateRecord , 0 )
42
- s .currencyStoreMu .Unlock ()
51
+ s .mu .Lock ()
52
+ s .exchangeRateRecords = make ([]* currency.ExchangeRateRecord , 0 )
53
+ s .lastExchangeRateIndex = 1
54
+ s .reserveRecords = make ([]* currency.ReserveRecord , 0 )
55
+ s .lastReserveIndex = 1
56
+ s .mu .Unlock ()
43
57
}
44
58
45
- func (s * store ) Put (ctx context.Context , data * currency.MultiRateRecord ) error {
46
- s .currencyStoreMu .Lock ()
47
- defer s .currencyStoreMu .Unlock ()
59
+ func (s * store ) PutExchangeRates (ctx context.Context , data * currency.MultiRateRecord ) error {
60
+ s .mu .Lock ()
61
+ defer s .mu .Unlock ()
48
62
49
63
// Not ideal but fine for testing the currency store
50
- for _ , item := range s .currencyStore {
64
+ for _ , item := range s .exchangeRateRecords {
51
65
if item .Time .Unix () == data .Time .Unix () {
52
66
return currency .ErrExists
53
67
}
54
68
}
55
69
56
70
for symbol , item := range data .Rates {
57
- s .currencyStore = append (s .currencyStore , & currency.ExchangeRateRecord {
58
- Id : s .lastIndex ,
71
+ s .exchangeRateRecords = append (s .exchangeRateRecords , & currency.ExchangeRateRecord {
72
+ Id : s .lastExchangeRateIndex ,
59
73
Rate : item ,
60
74
Time : data .Time ,
61
75
Symbol : symbol ,
62
76
})
63
- s .lastIndex = s .lastIndex + 1
77
+ s .lastExchangeRateIndex = s .lastExchangeRateIndex + 1
64
78
}
65
79
66
80
return nil
67
81
}
68
82
69
- func (s * store ) Get (ctx context.Context , symbol string , t time.Time ) (* currency.ExchangeRateRecord , error ) {
70
- s .currencyStoreMu .Lock ()
71
- defer s .currencyStoreMu .Unlock ()
83
+ func (s * store ) GetExchangeRate (ctx context.Context , symbol string , t time.Time ) (* currency.ExchangeRateRecord , error ) {
84
+ s .mu .Lock ()
85
+ defer s .mu .Unlock ()
72
86
73
87
// Not ideal but fine for testing the currency store
74
88
var results []* currency.ExchangeRateRecord
75
- for _ , item := range s .currencyStore {
76
- if item .Symbol == symbol && item .Time .Unix () <= t .Unix () {
89
+ for _ , item := range s .exchangeRateRecords {
90
+ if item .Symbol == symbol && item .Time .Unix () <= t .Unix () && item . Time . Format ( dateFormat ) == t . Format ( dateFormat ) {
77
91
results = append (results , item )
78
92
}
79
93
}
@@ -82,24 +96,25 @@ func (s *store) Get(ctx context.Context, symbol string, t time.Time) (*currency.
82
96
return nil , currency .ErrNotFound
83
97
}
84
98
85
- sort .Sort (ByTime (results ))
99
+ sort .Sort (RateByTime (results ))
86
100
87
101
return results [0 ], nil
88
102
}
89
103
90
- func (s * store ) GetAll (ctx context.Context , t time.Time ) (* currency.MultiRateRecord , error ) {
91
- s .currencyStoreMu .Lock ()
92
- defer s .currencyStoreMu .Unlock ()
104
+ func (s * store ) GetAllExchangeRates (ctx context.Context , t time.Time ) (* currency.MultiRateRecord , error ) {
105
+ s .mu .Lock ()
106
+ defer s .mu .Unlock ()
93
107
94
108
// Not ideal but fine for testing the currency store
95
- sort .Sort (ByTime (s .currencyStore ))
109
+ sort .Sort (RateByTime (s .exchangeRateRecords ))
96
110
97
111
result := currency.MultiRateRecord {
98
112
Rates : make (map [string ]float64 ),
99
113
}
100
- for _ , item := range s .currencyStore {
114
+ for _ , item := range s .exchangeRateRecords {
101
115
if item .Time .Unix () <= t .Unix () && item .Time .Format (dateFormat ) == t .Format (dateFormat ) {
102
116
result .Rates [item .Symbol ] = item .Rate
117
+ result .Time = item .Time
103
118
}
104
119
}
105
120
@@ -110,15 +125,15 @@ func (s *store) GetAll(ctx context.Context, t time.Time) (*currency.MultiRateRec
110
125
return & result , nil
111
126
}
112
127
113
- func (s * store ) GetRange (ctx context.Context , symbol string , interval query.Interval , start time.Time , end time.Time , ordering query.Ordering ) ([]* currency.ExchangeRateRecord , error ) {
114
- s .currencyStoreMu .Lock ()
115
- defer s .currencyStoreMu .Unlock ()
128
+ func (s * store ) GetExchangeRatesInRange (ctx context.Context , symbol string , interval query.Interval , start time.Time , end time.Time , ordering query.Ordering ) ([]* currency.ExchangeRateRecord , error ) {
129
+ s .mu .Lock ()
130
+ defer s .mu .Unlock ()
116
131
117
- sort .Sort (ByTime (s .currencyStore ))
132
+ sort .Sort (RateByTime (s .exchangeRateRecords ))
118
133
119
134
// Not ideal but fine for testing the currency store
120
135
var all []* currency.ExchangeRateRecord
121
- for _ , item := range s .currencyStore {
136
+ for _ , item := range s .exchangeRateRecords {
122
137
if item .Symbol == symbol && item .Time .Unix () >= start .Unix () && item .Time .Unix () <= end .Unix () {
123
138
all = append (all , item )
124
139
}
@@ -138,3 +153,47 @@ func (s *store) GetRange(ctx context.Context, symbol string, interval query.Inte
138
153
139
154
return all , nil
140
155
}
156
+
157
+ func (s * store ) PutReserveRecord (ctx context.Context , data * currency.ReserveRecord ) error {
158
+ s .mu .Lock ()
159
+ defer s .mu .Unlock ()
160
+
161
+ // Not ideal but fine for testing the currency store
162
+ for _ , item := range s .reserveRecords {
163
+ if item .Mint == data .Mint && item .Time .Unix () == data .Time .Unix () {
164
+ return currency .ErrExists
165
+ }
166
+ }
167
+
168
+ s .reserveRecords = append (s .reserveRecords , & currency.ReserveRecord {
169
+ Id : s .lastReserveIndex ,
170
+ Mint : data .Mint ,
171
+ SupplyFromBonding : data .SupplyFromBonding ,
172
+ CoreMintLocked : data .CoreMintLocked ,
173
+ Time : data .Time ,
174
+ })
175
+ s .lastReserveIndex = s .lastReserveIndex + 1
176
+
177
+ return nil
178
+ }
179
+
180
+ func (s * store ) GetReserveAtTime (ctx context.Context , mint string , t time.Time ) (* currency.ReserveRecord , error ) {
181
+ s .mu .Lock ()
182
+ defer s .mu .Unlock ()
183
+
184
+ // Not ideal but fine for testing the currency store
185
+ var results []* currency.ReserveRecord
186
+ for _ , item := range s .reserveRecords {
187
+ if item .Mint == mint && item .Time .Unix () <= t .Unix () && item .Time .Format (dateFormat ) == t .Format (dateFormat ) {
188
+ results = append (results , item )
189
+ }
190
+ }
191
+
192
+ if len (results ) == 0 {
193
+ return nil , currency .ErrNotFound
194
+ }
195
+
196
+ sort .Sort (ReserveByTime (results ))
197
+
198
+ return results [0 ], nil
199
+ }
0 commit comments