2019-11-21 13:23:16 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package stripecoinpayments
|
|
|
|
|
|
|
|
import (
|
2021-06-29 05:18:48 +01:00
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
satellite/payments: specialized type for monetary amounts
Why: big.Float is not an ideal type for dealing with monetary amounts,
because no matter how high the precision, some non-integer decimal
values can not be represented exactly in base-2 floating point. Also,
storing gob-encoded big.Float values in the database makes it very hard
to use those values in meaningful queries, making it difficult to do
any sort of analysis on billing.
For better accuracy, then, we can just represent monetary values as
integers (in whatever base units are appropriate for the currency). For
example, STORJ tokens or Bitcoins can not be split into pieces smaller
than 10^-8, so we can store amounts of STORJ or BTC with precision
simply by moving the decimal point 8 digits to the right. For USD values
(assuming we don't want to deal with fractional cents), we can move the
decimal point 2 digits to the right.
To make it easier and less error-prone to deal with the math involved, I
introduce here a new type, monetary.Amount, instances of which have an
associated value _and_ a currency.
Change-Id: I03395d52f0e2473cf301361f6033722b54640265
2021-08-10 23:29:50 +01:00
|
|
|
"github.com/shopspring/decimal"
|
2021-06-29 05:18:48 +01:00
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2022-09-06 13:43:09 +01:00
|
|
|
"storj.io/common/currency"
|
2021-06-29 05:18:48 +01:00
|
|
|
"storj.io/common/sync2"
|
2019-11-21 13:23:16 +00:00
|
|
|
)
|
|
|
|
|
satellite/payments: specialized type for monetary amounts
Why: big.Float is not an ideal type for dealing with monetary amounts,
because no matter how high the precision, some non-integer decimal
values can not be represented exactly in base-2 floating point. Also,
storing gob-encoded big.Float values in the database makes it very hard
to use those values in meaningful queries, making it difficult to do
any sort of analysis on billing.
For better accuracy, then, we can just represent monetary values as
integers (in whatever base units are appropriate for the currency). For
example, STORJ tokens or Bitcoins can not be split into pieces smaller
than 10^-8, so we can store amounts of STORJ or BTC with precision
simply by moving the decimal point 8 digits to the right. For USD values
(assuming we don't want to deal with fractional cents), we can move the
decimal point 2 digits to the right.
To make it easier and less error-prone to deal with the math involved, I
introduce here a new type, monetary.Amount, instances of which have an
associated value _and_ a currency.
Change-Id: I03395d52f0e2473cf301361f6033722b54640265
2021-08-10 23:29:50 +01:00
|
|
|
// convertToCents convert amount to USD cents with given rate.
|
2022-09-06 13:43:09 +01:00
|
|
|
func convertToCents(rate decimal.Decimal, amount currency.Amount) int64 {
|
satellite/payments: specialized type for monetary amounts
Why: big.Float is not an ideal type for dealing with monetary amounts,
because no matter how high the precision, some non-integer decimal
values can not be represented exactly in base-2 floating point. Also,
storing gob-encoded big.Float values in the database makes it very hard
to use those values in meaningful queries, making it difficult to do
any sort of analysis on billing.
For better accuracy, then, we can just represent monetary values as
integers (in whatever base units are appropriate for the currency). For
example, STORJ tokens or Bitcoins can not be split into pieces smaller
than 10^-8, so we can store amounts of STORJ or BTC with precision
simply by moving the decimal point 8 digits to the right. For USD values
(assuming we don't want to deal with fractional cents), we can move the
decimal point 2 digits to the right.
To make it easier and less error-prone to deal with the math involved, I
introduce here a new type, monetary.Amount, instances of which have an
associated value _and_ a currency.
Change-Id: I03395d52f0e2473cf301361f6033722b54640265
2021-08-10 23:29:50 +01:00
|
|
|
amountDecimal := amount.AsDecimal()
|
|
|
|
usd := amountDecimal.Mul(rate)
|
|
|
|
usdCents := usd.Shift(2)
|
|
|
|
return usdCents.Round(0).IntPart()
|
2019-11-21 14:25:37 +00:00
|
|
|
}
|
|
|
|
|
satellite/payments: specialized type for monetary amounts
Why: big.Float is not an ideal type for dealing with monetary amounts,
because no matter how high the precision, some non-integer decimal
values can not be represented exactly in base-2 floating point. Also,
storing gob-encoded big.Float values in the database makes it very hard
to use those values in meaningful queries, making it difficult to do
any sort of analysis on billing.
For better accuracy, then, we can just represent monetary values as
integers (in whatever base units are appropriate for the currency). For
example, STORJ tokens or Bitcoins can not be split into pieces smaller
than 10^-8, so we can store amounts of STORJ or BTC with precision
simply by moving the decimal point 8 digits to the right. For USD values
(assuming we don't want to deal with fractional cents), we can move the
decimal point 2 digits to the right.
To make it easier and less error-prone to deal with the math involved, I
introduce here a new type, monetary.Amount, instances of which have an
associated value _and_ a currency.
Change-Id: I03395d52f0e2473cf301361f6033722b54640265
2021-08-10 23:29:50 +01:00
|
|
|
// convertFromCents convert amount in cents to a StorjTokenAmount with given rate.
|
2022-09-06 13:43:09 +01:00
|
|
|
func convertFromCents(rate decimal.Decimal, usdCents int64) currency.Amount {
|
satellite/payments: specialized type for monetary amounts
Why: big.Float is not an ideal type for dealing with monetary amounts,
because no matter how high the precision, some non-integer decimal
values can not be represented exactly in base-2 floating point. Also,
storing gob-encoded big.Float values in the database makes it very hard
to use those values in meaningful queries, making it difficult to do
any sort of analysis on billing.
For better accuracy, then, we can just represent monetary values as
integers (in whatever base units are appropriate for the currency). For
example, STORJ tokens or Bitcoins can not be split into pieces smaller
than 10^-8, so we can store amounts of STORJ or BTC with precision
simply by moving the decimal point 8 digits to the right. For USD values
(assuming we don't want to deal with fractional cents), we can move the
decimal point 2 digits to the right.
To make it easier and less error-prone to deal with the math involved, I
introduce here a new type, monetary.Amount, instances of which have an
associated value _and_ a currency.
Change-Id: I03395d52f0e2473cf301361f6033722b54640265
2021-08-10 23:29:50 +01:00
|
|
|
usd := decimal.NewFromInt(usdCents).Shift(-2)
|
|
|
|
numStorj := usd.Div(rate)
|
2022-09-06 13:43:09 +01:00
|
|
|
return currency.AmountFromDecimal(numStorj, currency.USDollars)
|
2019-11-21 13:23:16 +00:00
|
|
|
}
|
2021-06-29 05:18:48 +01:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|