satellite/stripecoinpayments: version is the wrong name

this service exists to do currency conversions, which is
the best I can assume that this was meant to be named.

Change-Id: Ia2416f5475749e8bfe8d05bf491649576f6d77bf
This commit is contained in:
JT Olio 2021-06-28 22:18:48 -06:00 committed by JT Olio
parent d999a963ca
commit cb18dc77fc
3 changed files with 60 additions and 67 deletions

View File

@ -124,10 +124,10 @@ type API struct {
}
Payments struct {
Accounts payments.Accounts
Version *stripecoinpayments.VersionService
Service *stripecoinpayments.Service
Stripe stripecoinpayments.StripeClient
Accounts payments.Accounts
Conversion *stripecoinpayments.ConversionService
Service *stripecoinpayments.Service
Stripe stripecoinpayments.StripeClient
}
Console struct {
@ -533,15 +533,15 @@ func NewAPI(log *zap.Logger, full *identity.FullIdentity, db DB,
peer.Payments.Stripe = stripeClient
peer.Payments.Accounts = peer.Payments.Service.Accounts()
peer.Payments.Version = stripecoinpayments.NewVersionService(
peer.Payments.Conversion = stripecoinpayments.NewConversionService(
peer.Log.Named("payments.stripe:version"),
peer.Payments.Service,
pc.StripeCoinPayments.ConversionRatesCycleInterval)
peer.Services.Add(lifecycle.Item{
Name: "payments.stripe:version",
Run: peer.Payments.Version.Run,
Close: peer.Payments.Version.Close,
Run: peer.Payments.Conversion.Run,
Close: peer.Payments.Conversion.Close,
})
}

View File

@ -4,8 +4,15 @@
package stripecoinpayments
import (
"context"
"math"
"math/big"
"time"
"github.com/zeebo/errs"
"go.uber.org/zap"
"storj.io/common/sync2"
)
// convertToCents convert amount to cents with given rate.
@ -20,3 +27,49 @@ func convertFromCents(rate *big.Float, amount int64) *big.Float {
a = a.Quo(a, new(big.Float).SetInt64(100))
return new(big.Float).Quo(a, rate)
}
// ErrConversion defines version service error.
var ErrConversion = errs.Class("conversion service")
// ConversionService updates conversion rates in a loop.
//
// architecture: Service
type ConversionService struct {
log *zap.Logger
service *Service
Cycle sync2.Cycle
}
// NewConversionService creates new instance of ConversionService.
func NewConversionService(log *zap.Logger, service *Service, interval time.Duration) *ConversionService {
return &ConversionService{
log: log,
service: service,
Cycle: *sync2.NewCycle(interval),
}
}
// Run runs loop which updates conversion rates for service.
func (conversion *ConversionService) Run(ctx context.Context) (err error) {
defer mon.Task()(&ctx)(&err)
return ErrConversion.Wrap(conversion.Cycle.Run(ctx,
func(ctx context.Context) error {
conversion.log.Debug("running conversion rates update cycle")
if err := conversion.service.UpdateRates(ctx); err != nil {
conversion.log.Error("conversion rates update cycle failed", zap.Error(ErrChore.Wrap(err)))
}
return nil
},
))
}
// Close closes underlying cycle.
func (conversion *ConversionService) Close() (err error) {
defer mon.Task()(nil)(&err)
conversion.Cycle.Close()
return nil
}

View File

@ -1,60 +0,0 @@
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package stripecoinpayments
import (
"context"
"time"
"github.com/zeebo/errs"
"go.uber.org/zap"
"storj.io/common/sync2"
)
// ErrVersion defines version service error.
var ErrVersion = errs.Class("version service")
// VersionService updates conversion rates in a loop.
//
// architecture: Service
type VersionService struct {
log *zap.Logger
service *Service
Cycle sync2.Cycle
}
// NewVersionService creates new instance of VersionService.
func NewVersionService(log *zap.Logger, service *Service, interval time.Duration) *VersionService {
return &VersionService{
log: log,
service: service,
Cycle: *sync2.NewCycle(interval),
}
}
// Run runs loop which updates conversion rates for service.
func (version *VersionService) Run(ctx context.Context) (err error) {
defer mon.Task()(&ctx)(&err)
return ErrVersion.Wrap(version.Cycle.Run(ctx,
func(ctx context.Context) error {
version.log.Debug("running conversion rates update cycle")
if err := version.service.UpdateRates(ctx); err != nil {
version.log.Error("conversion rates update cycle failed", zap.Error(ErrChore.Wrap(err)))
}
return nil
},
))
}
// Close closes underlying cycle.
func (version *VersionService) Close() (err error) {
defer mon.Task()(nil)(&err)
version.Cycle.Close()
return nil
}