2019-11-15 14:59:39 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package coinpayments
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
"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"
|
|
|
|
|
|
|
|
"storj.io/storj/satellite/payments/monetary"
|
2019-11-15 14:59:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// cmdRates is API command for retrieving currency rate infos.
|
|
|
|
const cmdRates = "rates"
|
|
|
|
|
|
|
|
// ExchangeStatus defines if currency is exchangeable.
|
|
|
|
type ExchangeStatus string
|
|
|
|
|
|
|
|
const (
|
|
|
|
// ExchangeStatusOnline defines exchangeable currency.
|
|
|
|
ExchangeStatusOnline ExchangeStatus = "online"
|
|
|
|
// ExchangeStatusOffline defines currency that can not be convertible at the moment.
|
|
|
|
ExchangeStatusOffline ExchangeStatus = "offline"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CurrencyRateInfo holds currency conversion info.
|
|
|
|
type CurrencyRateInfo struct {
|
|
|
|
IsFiat bool
|
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
|
|
|
RateBTC decimal.Decimal
|
|
|
|
TXFee decimal.Decimal
|
2019-11-15 14:59:39 +00:00
|
|
|
Status ExchangeStatus
|
|
|
|
LastUpdate time.Time
|
|
|
|
}
|
|
|
|
|
2020-07-16 16:27:24 +01:00
|
|
|
// UnmarshalJSON converts JSON string to currency rate info.
|
2019-11-15 14:59:39 +00:00
|
|
|
func (rateInfo *CurrencyRateInfo) UnmarshalJSON(b []byte) error {
|
|
|
|
var rateRaw struct {
|
|
|
|
IsFiat int `json:"is_fiat"`
|
|
|
|
RateBTC string `json:"rate_btc"`
|
|
|
|
TXFee string `json:"tx_fee"`
|
|
|
|
Status string `json:"status"`
|
|
|
|
LastUpdate string `json:"last_update"`
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.Unmarshal(b, &rateRaw); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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
|
|
|
rateBTC, err := decimal.NewFromString(rateRaw.RateBTC)
|
2019-11-15 14:59:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
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
|
|
|
txFee, err := decimal.NewFromString(rateRaw.TXFee)
|
2019-11-15 14:59:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
lastUpdate, err := strconv.ParseInt(rateRaw.LastUpdate, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
*rateInfo = CurrencyRateInfo{
|
|
|
|
IsFiat: rateRaw.IsFiat > 0,
|
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
|
|
|
RateBTC: rateBTC,
|
|
|
|
TXFee: txFee,
|
2019-11-15 14:59:39 +00:00
|
|
|
Status: ExchangeStatus(rateRaw.Status),
|
|
|
|
LastUpdate: time.Unix(lastUpdate, 0),
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CurrencyRateInfos maps currency to currency rate info.
|
2021-09-30 17:20:52 +01:00
|
|
|
type CurrencyRateInfos map[CurrencySymbol]CurrencyRateInfo
|
|
|
|
|
|
|
|
// ForCurrency allows lookup into a CurrencyRateInfos map by currency
|
|
|
|
// object, instead of by its coinpayments.net-specific symbol.
|
|
|
|
func (infos CurrencyRateInfos) ForCurrency(currency *monetary.Currency) (info CurrencyRateInfo, ok bool) {
|
|
|
|
coinpaymentsSymbol, ok := currencySymbols[currency]
|
|
|
|
if !ok {
|
|
|
|
return info, false
|
|
|
|
}
|
|
|
|
info, ok = infos[coinpaymentsSymbol]
|
|
|
|
return info, ok
|
|
|
|
}
|
2019-11-15 14:59:39 +00:00
|
|
|
|
|
|
|
// ConversionRates collection of API methods for retrieving currency
|
|
|
|
// conversion rates.
|
|
|
|
type ConversionRates struct {
|
|
|
|
client *Client
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get returns USD rate for specified currency.
|
|
|
|
func (rates ConversionRates) Get(ctx context.Context) (CurrencyRateInfos, error) {
|
|
|
|
values := make(url.Values)
|
|
|
|
values.Set("short", "1")
|
|
|
|
|
|
|
|
rateInfos := make(CurrencyRateInfos)
|
|
|
|
|
|
|
|
res, err := rates.client.do(ctx, cmdRates, values)
|
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = json.Unmarshal(res, &rateInfos); err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return rateInfos, nil
|
|
|
|
}
|