use shopspring/decimal library everywhere
All checks were successful
Generate check / check-generate (push) Has been skipped
Generate check / check-generate (pull_request) Has been skipped
Tests / tests (pull_request) Has been skipped

This commit is contained in:
2025-11-16 23:03:47 +00:00
parent edc1628674
commit a6d56d7441
14 changed files with 421 additions and 282 deletions

View File

@@ -5,21 +5,21 @@ import (
"encoding/csv"
"fmt"
"io"
"math/big"
"strings"
"time"
"github.com/nmoniz/any2anexoj/internal"
"github.com/shopspring/decimal"
)
type Record struct {
symbol string
side internal.Side
quantity *big.Float
price *big.Float
quantity decimal.Decimal
price decimal.Decimal
timestamp time.Time
fees *big.Float
taxes *big.Float
fees decimal.Decimal
taxes decimal.Decimal
}
func (r Record) Symbol() string {
@@ -30,11 +30,11 @@ func (r Record) Side() internal.Side {
return r.side
}
func (r Record) Quantity() *big.Float {
func (r Record) Quantity() decimal.Decimal {
return r.quantity
}
func (r Record) Price() *big.Float {
func (r Record) Price() decimal.Decimal {
return r.price
}
@@ -42,11 +42,11 @@ func (r Record) Timestamp() time.Time {
return r.timestamp
}
func (r Record) Fees() *big.Float {
func (r Record) Fees() decimal.Decimal {
return r.fees
}
func (r Record) Taxes() *big.Float {
func (r Record) Taxes() decimal.Decimal {
return r.taxes
}
@@ -123,24 +123,23 @@ func (rr RecordReader) ReadRecord(_ context.Context) (internal.Record, error) {
price: price,
timestamp: ts,
fees: convertionFee,
taxes: new(big.Float).Add(stampDutyTax, frenchTxTax),
taxes: stampDutyTax.Add(frenchTxTax),
}, nil
}
}
// parseFloat attempts to parse a string using a standard precision and rounding mode.
// Using this function helps avoid issues around converting values due to sligh parameter changes.
func parseDecimal(s string) (*big.Float, error) {
f, _, err := big.ParseFloat(s, 10, 128, big.ToZero)
return f, err
func parseDecimal(s string) (decimal.Decimal, error) {
return decimal.NewFromString(s)
}
// parseOptinalDecimal behaves the same as parseDecimal but returns 0 when len(s) is 0 instead of
// error.
// Using this function helps avoid issues around converting values due to sligh parameter changes.
func parseOptinalDecimal(s string) (*big.Float, error) {
func parseOptinalDecimal(s string) (decimal.Decimal, error) {
if len(s) == 0 {
return new(big.Float), nil
return decimal.Decimal{}, nil
}
return parseDecimal(s)

View File

@@ -3,11 +3,11 @@ package trading212
import (
"bytes"
"io"
"math/big"
"testing"
"time"
"github.com/nmoniz/any2anexoj/internal"
"github.com/shopspring/decimal"
)
func TestRecordReader_ReadRecord(t *testing.T) {
@@ -136,7 +136,7 @@ func TestRecordReader_ReadRecord(t *testing.T) {
}
}
func ShouldParseDecimal(t testing.TB, sf string) *big.Float {
func ShouldParseDecimal(t testing.TB, sf string) decimal.Decimal {
t.Helper()
bf, err := parseDecimal(sf)