2020-06-25 22:16:39 +01:00
|
|
|
// Copyright (C) 2020 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2021-02-19 14:42:10 +00:00
|
|
|
package coinpayments_test
|
2020-06-25 22:16:39 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
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"
|
2020-06-25 22:16:39 +01:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2022-09-06 13:43:09 +01:00
|
|
|
"storj.io/common/currency"
|
2020-06-25 22:16:39 +01:00
|
|
|
"storj.io/common/testcontext"
|
2021-02-19 14:42:10 +00:00
|
|
|
"storj.io/storj/satellite"
|
|
|
|
"storj.io/storj/satellite/payments/coinpayments"
|
|
|
|
"storj.io/storj/satellite/satellitedb/satellitedbtest"
|
2020-06-25 22:16:39 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestListInfos(t *testing.T) {
|
2020-10-13 13:47:55 +01:00
|
|
|
// This test is deliberately skipped as it requires credentials to coinpayments.net
|
2020-06-25 22:16:39 +01:00
|
|
|
t.SkipNow()
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
2021-02-19 14:42:10 +00:00
|
|
|
payments := coinpayments.NewClient(coinpayments.Credentials{
|
2020-06-25 22:16:39 +01:00
|
|
|
PublicKey: "ask-littleskunk-on-keybase",
|
|
|
|
PrivateKey: "ask-littleskunk-on-keybase",
|
|
|
|
}).Transactions()
|
|
|
|
|
2020-10-13 13:47:55 +01:00
|
|
|
// verify that bad ids fail
|
2021-02-19 14:42:10 +00:00
|
|
|
infos, err := payments.ListInfos(ctx, coinpayments.TransactionIDList{"an_unlikely_id"})
|
2020-06-25 22:16:39 +01:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Len(t, infos, 0)
|
|
|
|
|
2020-10-13 13:47:55 +01:00
|
|
|
// verify that ListInfos can handle more than 25 good ids
|
2021-02-19 14:42:10 +00:00
|
|
|
ids := coinpayments.TransactionIDList{}
|
2020-06-25 22:16:39 +01:00
|
|
|
for x := 0; x < 27; x++ {
|
|
|
|
tx, err := payments.Create(ctx,
|
2021-02-19 14:42:10 +00:00
|
|
|
&coinpayments.CreateTX{
|
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
|
|
|
Amount: decimal.NewFromInt(100),
|
2022-09-06 13:43:09 +01:00
|
|
|
CurrencyIn: currency.StorjToken,
|
|
|
|
CurrencyOut: currency.StorjToken,
|
2020-06-25 22:16:39 +01:00
|
|
|
BuyerEmail: "test@test.com",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
ids = append(ids, tx.ID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
infos, err = payments.ListInfos(ctx, ids)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, infos, 27)
|
|
|
|
}
|
2021-02-19 14:42:10 +00:00
|
|
|
|
|
|
|
func TestUpdateSameAppliesDoesNotExplode(t *testing.T) {
|
|
|
|
satellitedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db satellite.DB) {
|
|
|
|
tdb := db.StripeCoinPayments().Transactions()
|
|
|
|
assert.NoError(t, tdb.Update(ctx, nil, coinpayments.TransactionIDList{"blah", "blah"}))
|
|
|
|
assert.NoError(t, tdb.Update(ctx, nil, coinpayments.TransactionIDList{"blah", "blah"}))
|
|
|
|
})
|
|
|
|
}
|