storj/satellite/payments/stripecoinpayments/transactions.go
paul cannon a16aecfa96 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-09-28 23:27:44 +00:00

101 lines
3.6 KiB
Go

// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package stripecoinpayments
import (
"context"
"time"
"github.com/shopspring/decimal"
"github.com/zeebo/errs"
"storj.io/common/uuid"
"storj.io/storj/satellite/payments/coinpayments"
"storj.io/storj/satellite/payments/monetary"
)
// ErrTransactionConsumed is thrown when trying to consume already consumed transaction.
var ErrTransactionConsumed = errs.New("error transaction already consumed")
// TransactionsDB is an interface which defines functionality
// of DB which stores coinpayments transactions.
//
// architecture: Database
type TransactionsDB interface {
// Insert inserts new coinpayments transaction into DB.
Insert(ctx context.Context, tx Transaction) (*Transaction, error)
// Update updates status and received for set of transactions.
Update(ctx context.Context, updates []TransactionUpdate, applies coinpayments.TransactionIDList) error
// Consume marks transaction as consumed, so it won't participate in apply account balance loop.
Consume(ctx context.Context, id coinpayments.TransactionID) error
// LockRate locks conversion rate for transaction.
LockRate(ctx context.Context, id coinpayments.TransactionID, rate decimal.Decimal) error
// GetLockedRate returns locked conversion rate for transaction or error if non exists.
GetLockedRate(ctx context.Context, id coinpayments.TransactionID) (decimal.Decimal, error)
// ListAccount returns all transaction for specific user.
ListAccount(ctx context.Context, userID uuid.UUID) ([]Transaction, error)
// ListPending returns TransactionsPage with pending transactions.
ListPending(ctx context.Context, offset int64, limit int, before time.Time) (TransactionsPage, error)
// List Unapplied returns TransactionsPage with completed transaction that should be applied to account balance.
ListUnapplied(ctx context.Context, offset int64, limit int, before time.Time) (TransactionsPage, error)
}
// Transaction defines coinpayments transaction info that is stored in the DB.
type Transaction struct {
ID coinpayments.TransactionID
AccountID uuid.UUID
Address string
Amount monetary.Amount
Received monetary.Amount
Status coinpayments.Status
Key string
Timeout time.Duration
CreatedAt time.Time
}
// TransactionUpdate holds transaction update info.
type TransactionUpdate struct {
TransactionID coinpayments.TransactionID
Status coinpayments.Status
Received monetary.Amount
}
// TransactionsPage holds set of transaction and indicates if
// there are more transactions to fetch.
type TransactionsPage struct {
Transactions []Transaction
Next bool
NextOffset int64
}
// IDList returns transaction id list of page's transactions.
func (page *TransactionsPage) IDList() TransactionAndUserList {
var ids = make(TransactionAndUserList)
for _, tx := range page.Transactions {
ids[tx.ID] = tx.AccountID
}
return ids
}
// CreationTimes returns a map of creation times of page's transactions.
func (page *TransactionsPage) CreationTimes() map[coinpayments.TransactionID]time.Time {
creationTimes := make(map[coinpayments.TransactionID]time.Time)
for _, tx := range page.Transactions {
creationTimes[tx.ID] = tx.CreatedAt
}
return creationTimes
}
// TransactionAndUserList is a composite type for storing userID and txID.
type TransactionAndUserList map[coinpayments.TransactionID]uuid.UUID
// IDList returns transaction id list.
func (idMap TransactionAndUserList) IDList() coinpayments.TransactionIDList {
var list coinpayments.TransactionIDList
for transactionID := range idMap {
list = append(list, transactionID)
}
return list
}