@@ -6,74 +6,88 @@ import (
66 "sync"
77 "time"
88
9- "github.com/code-payments/code-server/pkg/database/query"
109 "github.com/code-payments/code-server/pkg/code/data/currency"
10+ "github.com/code-payments/code-server/pkg/database/query"
1111)
1212
1313const (
1414 dateFormat = "2006-01-02"
1515)
1616
1717type 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 ()
2132}
2233
23- type ByTime []* currency.ExchangeRateRecord
34+ type ReserveByTime []* currency.ReserveRecord
2435
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 {
2839 // DESC order (most recent first)
2940 return a [i ].Time .Unix () > a [j ].Time .Unix ()
3041}
3142
3243func New () currency.Store {
3344 return & store {
34- currencyStore : make ([]* currency.ExchangeRateRecord , 0 ),
35- lastIndex : 1 ,
45+ exchangeRateRecords : make ([]* currency.ExchangeRateRecord , 0 ),
46+ lastExchangeRateIndex : 1 ,
3647 }
3748}
3849
3950func (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 ()
4357}
4458
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 ()
4862
4963 // Not ideal but fine for testing the currency store
50- for _ , item := range s .currencyStore {
64+ for _ , item := range s .exchangeRateRecords {
5165 if item .Time .Unix () == data .Time .Unix () {
5266 return currency .ErrExists
5367 }
5468 }
5569
5670 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 ,
5973 Rate : item ,
6074 Time : data .Time ,
6175 Symbol : symbol ,
6276 })
63- s .lastIndex = s .lastIndex + 1
77+ s .lastExchangeRateIndex = s .lastExchangeRateIndex + 1
6478 }
6579
6680 return nil
6781}
6882
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 ()
7286
7387 // Not ideal but fine for testing the currency store
7488 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 ) {
7791 results = append (results , item )
7892 }
7993 }
@@ -82,24 +96,25 @@ func (s *store) Get(ctx context.Context, symbol string, t time.Time) (*currency.
8296 return nil , currency .ErrNotFound
8397 }
8498
85- sort .Sort (ByTime (results ))
99+ sort .Sort (RateByTime (results ))
86100
87101 return results [0 ], nil
88102}
89103
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 ()
93107
94108 // Not ideal but fine for testing the currency store
95- sort .Sort (ByTime (s .currencyStore ))
109+ sort .Sort (RateByTime (s .exchangeRateRecords ))
96110
97111 result := currency.MultiRateRecord {
98112 Rates : make (map [string ]float64 ),
99113 }
100- for _ , item := range s .currencyStore {
114+ for _ , item := range s .exchangeRateRecords {
101115 if item .Time .Unix () <= t .Unix () && item .Time .Format (dateFormat ) == t .Format (dateFormat ) {
102116 result .Rates [item .Symbol ] = item .Rate
117+ result .Time = item .Time
103118 }
104119 }
105120
@@ -110,15 +125,15 @@ func (s *store) GetAll(ctx context.Context, t time.Time) (*currency.MultiRateRec
110125 return & result , nil
111126}
112127
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 ()
116131
117- sort .Sort (ByTime (s .currencyStore ))
132+ sort .Sort (RateByTime (s .exchangeRateRecords ))
118133
119134 // Not ideal but fine for testing the currency store
120135 var all []* currency.ExchangeRateRecord
121- for _ , item := range s .currencyStore {
136+ for _ , item := range s .exchangeRateRecords {
122137 if item .Symbol == symbol && item .Time .Unix () >= start .Unix () && item .Time .Unix () <= end .Unix () {
123138 all = append (all , item )
124139 }
@@ -138,3 +153,47 @@ func (s *store) GetRange(ctx context.Context, symbol string, interval query.Inte
138153
139154 return all , nil
140155}
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