Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion ocp/worker/currency/exchangerate/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@ import (

currency_lib "github.com/code-payments/ocp-server/currency"
ocp_data "github.com/code-payments/ocp-server/ocp/data"
"github.com/code-payments/ocp-server/ocp/data/currency"
"github.com/code-payments/ocp-server/ocp/worker"
)

type exchangeRateRuntime struct {
log *zap.Logger
data ocp_data.Provider

lastRates *currency.MultiRateRecord
}

func New(log *zap.Logger, data ocp_data.Provider) worker.Runtime {
Expand Down Expand Up @@ -70,7 +73,21 @@ func (p *exchangeRateRuntime) Start(runtimeCtx context.Context, interval time.Du
func (p *exchangeRateRuntime) GetCurrentExchangeRates(ctx context.Context) error {
data, err := p.data.GetCurrentExchangeRatesFromExternalProviders(ctx)
if err != nil {
return errors.Wrap(err, "failed to get current rate data")
p.log.With(zap.Error(err)).Warn("failed to get current rate data, persisting last known rates")

lastRates := p.lastRates
if lastRates == nil {
lastRates, _ = p.data.GetAllExchangeRates(ctx, time.Now())
}

if lastRates == nil {
return errors.Wrap(err, "failed to get current rate data")
}

data = &currency.MultiRateRecord{
Time: time.Now(),
Rates: lastRates.Rates,
}
}

delete(data.Rates, string(currency_lib.BTC))
Expand All @@ -97,5 +114,7 @@ func (p *exchangeRateRuntime) GetCurrentExchangeRates(ctx context.Context) error
return errors.Wrap(err, "failed to store rate data")
}

p.lastRates = data

return nil
}
Loading