Documentation
¶
Overview ¶
Package sailfish provides fast, unsigned, fixed-decimal values for trading and financial protocols.
A value is stored as one scaled integer:
value = units / 10^fractionalDecimalPlaces
The package supports uint8, uint16, uint32, uint64, and uint256.Int units. Types make semantic kind, unit representation, and fractional decimal places explicit. For example, PriceInUint64Units[DecimalPlaces5] represents a price as uint64 units with exactly five digits after the decimal point. Its hot parse, text/CBOR append, strict CBOR decode, compare, and arithmetic paths are allocation-free when caller-owned output buffers have capacity. FixedDecimalCodec converts already-scaled big.Int and uint256.Int units without decimal rescaling; ToBigInt accepts caller-owned storage so repeated wide conversions can also remain allocation-free. Exact BigRat conversion, explicit cross-scale Rescale/AddAs/SubAs operations, and denomination-aware arithmetic are available without implicit rounding or scale inference.
Index ¶
- Constants
- func Compare[VA FixedDecimalFormat[UA], UA Unit, VB FixedDecimalFormat[UB], UB Unit](a FixedDecimal[VA, UA], b FixedDecimal[VB, UB]) int
- type AmountInUint8Units
- type AmountInUint16Units
- type AmountInUint32Units
- type AmountInUint64Units
- type AmountInUint256Units
- type BigRatWorkspace
- type DecimalPlaces
- type DecimalPlaces0
- type DecimalPlaces1
- type DecimalPlaces2
- type DecimalPlaces3
- type DecimalPlaces4
- type DecimalPlaces5
- type DecimalPlaces6
- type DecimalPlaces7
- type DecimalPlaces8
- type DecimalPlaces9
- type DecimalPlaces10
- type DecimalPlaces11
- type DecimalPlaces12
- type DecimalPlaces13
- type DecimalPlaces14
- type DecimalPlaces15
- type DecimalPlaces16
- type DecimalPlaces17
- type DecimalPlaces18
- type DecimalPlaces19
- type DecimalPlaces20
- type Denominated
- func AddDenominatedAs[ResultV FixedDecimalFormat[ResultU], ResultU Unit, D comparable, ...](a Denominated[D, AV, AU], b Denominated[D, BV, BU]) (Denominated[D, ResultV, ResultU], error)
- func NewDenominated[D comparable, V FixedDecimalFormat[U], U Unit](denomination D, value FixedDecimal[V, U]) Denominated[D, V, U]
- func RescaleDenominated[ToV FixedDecimalFormat[ToU], ToU Unit, D comparable, ...](value Denominated[D, FromV, FromU]) (Denominated[D, ToV, ToU], error)
- func SubDenominatedAs[ResultV FixedDecimalFormat[ResultU], ResultU Unit, D comparable, ...](a Denominated[D, AV, AU], b Denominated[D, BV, BU]) (Denominated[D, ResultV, ResultU], error)
- func (d Denominated[D, V, U]) Add(other Denominated[D, V, U]) (Denominated[D, V, U], error)
- func (d Denominated[D, V, U]) Compare(other Denominated[D, V, U]) (int, error)
- func (d Denominated[D, V, U]) Decimal() FixedDecimal[V, U]
- func (d Denominated[D, V, U]) Denomination() D
- func (d Denominated[D, V, U]) Sub(other Denominated[D, V, U]) (Denominated[D, V, U], error)
- type Error
- type FixedDecimal
- func AddAs[ResultV FixedDecimalFormat[ResultU], ResultU Unit, AV FixedDecimalFormat[AU], ...](a FixedDecimal[AV, AU], b FixedDecimal[BV, BU]) (FixedDecimal[ResultV, ResultU], error)
- func NewCompactFixedDecimal[V FixedDecimalFormat[U], U Unit](s string) (FixedDecimal[V, U], error)
- func NewFixedDecimal[V FixedDecimalFormat[U], U Unit](s string) (FixedDecimal[V, U], error)
- func NewFixedDecimalFromBytes[V FixedDecimalFormat[U], U Unit](b []byte) (FixedDecimal[V, U], error)
- func NewFixedDecimalFromUnits[V FixedDecimalFormat[U], U Unit](units U) (FixedDecimal[V, U], error)
- func Rescale[ToV FixedDecimalFormat[ToU], ToU Unit, FromV FixedDecimalFormat[FromU], ...](value FixedDecimal[FromV, FromU]) (FixedDecimal[ToV, ToU], error)
- func SubAs[ResultV FixedDecimalFormat[ResultU], ResultU Unit, AV FixedDecimalFormat[AU], ...](a FixedDecimal[AV, AU], b FixedDecimal[BV, BU]) (FixedDecimal[ResultV, ResultU], error)
- func (d FixedDecimal[V, U]) Add(other FixedDecimal[V, U]) (FixedDecimal[V, U], error)
- func (d *FixedDecimal[V, U]) AddAssign(other FixedDecimal[V, U]) (overflow bool)
- func (d FixedDecimal[V, U]) AddOverflow(other FixedDecimal[V, U]) (FixedDecimal[V, U], bool)
- func (d FixedDecimal[V, U]) AppendCBOR(dst []byte) []byte
- func (d FixedDecimal[V, U]) AppendJSON(dst []byte) []byte
- func (d FixedDecimal[V, U]) AppendText(dst []byte) ([]byte, error)
- func (d FixedDecimal[V, U]) AppendTo(dst []byte) []byte
- func (d FixedDecimal[V, U]) CBORLen() int
- func (d FixedDecimal[V, U]) Canonical() FixedDecimal[V, U]
- func (d FixedDecimal[V, U]) Cmp(other FixedDecimal[V, U]) int
- func (d FixedDecimal[V, U]) Compare(other FixedDecimal[V, U]) int
- func (d FixedDecimal[V, U]) Equal(other FixedDecimal[V, U]) bool
- func (d FixedDecimal[V, U]) HasRepresentation() bool
- func (d FixedDecimal[V, U]) IsZero() bool
- func (d FixedDecimal[V, U]) Len() int
- func (d FixedDecimal[V, U]) Less(other FixedDecimal[V, U]) bool
- func (d FixedDecimal[V, U]) MarshalCBOR() ([]byte, error)
- func (d FixedDecimal[V, U]) MarshalJSON() ([]byte, error)
- func (d FixedDecimal[V, U]) MarshalText() ([]byte, error)
- func (d *FixedDecimal[V, U]) SetUnits(units U)
- func (d FixedDecimal[V, U]) String() string
- func (d FixedDecimal[V, U]) Sub(other FixedDecimal[V, U]) (FixedDecimal[V, U], error)
- func (d *FixedDecimal[V, U]) SubAssign(other FixedDecimal[V, U]) (underflow bool)
- func (d FixedDecimal[V, U]) SubUnderflow(other FixedDecimal[V, U]) (FixedDecimal[V, U], bool)
- func (d FixedDecimal[V, U]) ToBigInt(dst *big.Int) error
- func (d FixedDecimal[V, U]) ToBigRat(dst *big.Rat, workspace *BigRatWorkspace) error
- func (d FixedDecimal[V, U]) ToU256() uint256.Int
- func (d FixedDecimal[V, U]) Units() U
- func (d *FixedDecimal[V, U]) UnmarshalCBOR(raw []byte) error
- func (d *FixedDecimal[V, U]) UnmarshalJSON(data []byte) error
- func (d *FixedDecimal[V, U]) UnmarshalText(text []byte) error
- type FixedDecimalCodec
- func (c FixedDecimalCodec[V, U]) AppendCBOR(dst []byte, d FixedDecimal[V, U]) []byte
- func (c FixedDecimalCodec[V, U]) AppendJSON(dst []byte, d FixedDecimal[V, U]) []byte
- func (c FixedDecimalCodec[V, U]) AppendTo(dst []byte, d FixedDecimal[V, U]) []byte
- func (c FixedDecimalCodec[V, U]) AppendUnits(dst []byte, units U) []byte
- func (c FixedDecimalCodec[V, U]) CBORLen(d FixedDecimal[V, U]) int
- func (c FixedDecimalCodec[V, U]) Canonical(d FixedDecimal[V, U]) FixedDecimal[V, U]
- func (c FixedDecimalCodec[V, U]) FractionalDecimalPlaces() DecimalPlaces
- func (FixedDecimalCodec[V, U]) FromBigInt(source *big.Int) (FixedDecimal[V, U], error)
- func (c FixedDecimalCodec[V, U]) FromBigRat(source *big.Rat) (FixedDecimal[V, U], error)
- func (FixedDecimalCodec[V, U]) FromU256(source uint256.Int) (FixedDecimal[V, U], error)
- func (c FixedDecimalCodec[V, U]) FromUnits(units U) FixedDecimal[V, U]
- func (c FixedDecimalCodec[V, U]) Len(d FixedDecimal[V, U]) int
- func (c FixedDecimalCodec[V, U]) MaxIntegerDigits() int
- func (c FixedDecimalCodec[V, U]) Parse(s string) (FixedDecimal[V, U], error)
- func (c FixedDecimalCodec[V, U]) ParseBytes(b []byte) (FixedDecimal[V, U], error)
- func (c FixedDecimalCodec[V, U]) ParseCBOR(raw []byte) (FixedDecimal[V, U], error)
- func (c FixedDecimalCodec[V, U]) ParseCBORFirst(raw []byte) (FixedDecimal[V, U], []byte, error)
- func (c FixedDecimalCodec[V, U]) ParseCompact(s string) (FixedDecimal[V, U], error)
- func (c FixedDecimalCodec[V, U]) ParseUnits(s string) (U, Error)
- func (c FixedDecimalCodec[V, U]) ParseUnitsBytes(b []byte) (U, Error)
- func (c FixedDecimalCodec[V, U]) String(d FixedDecimal[V, U]) string
- func (c FixedDecimalCodec[V, U]) UnitsLen(units U) int
- type FixedDecimalFormat
- type NativeUnit
- type PriceInUint8Units
- type PriceInUint16Units
- type PriceInUint32Units
- type PriceInUint64Units
- type PriceInUint256Units
- type StaticDecimalPlaces
- type Uint8Units
- type Uint16Units
- type Uint32Units
- type Uint64Units
- type Uint256FixedDecimalCodec
- func (c Uint256FixedDecimalCodec) AppendCBOR(dst []byte, units uint256.Int) []byte
- func (c Uint256FixedDecimalCodec) AppendTo(dst []byte, units uint256.Int) []byte
- func (c Uint256FixedDecimalCodec) CBORLen(units uint256.Int) int
- func (c Uint256FixedDecimalCodec) FractionalDecimalPlaces() DecimalPlaces
- func (c Uint256FixedDecimalCodec) Len(units uint256.Int) int
- func (c Uint256FixedDecimalCodec) MaxIntegerDigits() int
- func (c Uint256FixedDecimalCodec) Parse(input string) (uint256.Int, Error)
- func (c Uint256FixedDecimalCodec) ParseBytes(input []byte) (uint256.Int, Error)
- func (c Uint256FixedDecimalCodec) ParseBytesInto(input []byte, dst *uint256.Int) Error
- func (c Uint256FixedDecimalCodec) ParseCBOR(raw []byte) (uint256.Int, Error)
- func (c Uint256FixedDecimalCodec) ParseCBORFirst(raw []byte) (uint256.Int, []byte, Error)
- func (c Uint256FixedDecimalCodec) ParseCBORFirstInto(raw []byte, dst *uint256.Int) ([]byte, Error)
- func (c Uint256FixedDecimalCodec) ParseCBORInto(raw []byte, dst *uint256.Int) Error
- func (c Uint256FixedDecimalCodec) ParseInto(input string, dst *uint256.Int) Error
- type Uint256Units
- type Unit
Examples ¶
Constants ¶
const MaxCBORSize = 35
MaxCBORSize is the maximum preferred CBOR encoding size of one FixedDecimal. It is tag 2, a one-byte length argument, and a 32-byte uint256 magnitude.
Variables ¶
This section is empty.
Functions ¶
func Compare ¶
func Compare[VA FixedDecimalFormat[UA], UA Unit, VB FixedDecimalFormat[UB], UB Unit]( a FixedDecimal[VA, UA], b FixedDecimal[VB, UB], ) int
Compare compares fixed decimals across fractional decimal-place counts and unit backends exactly. It does not rescale either integer, so comparison cannot overflow.
Types ¶
type AmountInUint8Units ¶ added in v1.0.4
type AmountInUint8Units[S StaticDecimalPlaces] struct { Uint8Units }
AmountInUint8Units through AmountInUint256Units are the amount-kind equivalents. Price and amount formats remain distinct types even with equal fractional decimal places and the same backend.
func (AmountInUint8Units[S]) FractionalDecimalPlaces ¶ added in v1.0.4
func (AmountInUint8Units[S]) FractionalDecimalPlaces() DecimalPlaces
type AmountInUint16Units ¶ added in v1.0.4
type AmountInUint16Units[S StaticDecimalPlaces] struct { Uint16Units }
func (AmountInUint16Units[S]) FractionalDecimalPlaces ¶ added in v1.0.4
func (AmountInUint16Units[S]) FractionalDecimalPlaces() DecimalPlaces
type AmountInUint32Units ¶ added in v1.0.4
type AmountInUint32Units[S StaticDecimalPlaces] struct { Uint32Units }
func (AmountInUint32Units[S]) FractionalDecimalPlaces ¶ added in v1.0.4
func (AmountInUint32Units[S]) FractionalDecimalPlaces() DecimalPlaces
type AmountInUint64Units ¶ added in v1.0.4
type AmountInUint64Units[S StaticDecimalPlaces] struct { Uint64Units }
func (AmountInUint64Units[S]) FractionalDecimalPlaces ¶ added in v1.0.4
func (AmountInUint64Units[S]) FractionalDecimalPlaces() DecimalPlaces
type AmountInUint256Units ¶ added in v1.0.4
type AmountInUint256Units[S StaticDecimalPlaces] struct { Uint256Units }
func (AmountInUint256Units[S]) FractionalDecimalPlaces ¶ added in v1.0.4
func (AmountInUint256Units[S]) FractionalDecimalPlaces() DecimalPlaces
type BigRatWorkspace ¶ added in v1.2.0
type BigRatWorkspace struct {
// contains filtered or unexported fields
}
BigRatWorkspace owns temporary integer words used by ToBigRat. Its zero value is ready for use. Reuse one workspace and destination per goroutine; neither type is safe for concurrent mutation.
type DecimalPlaces ¶ added in v1.0.4
type DecimalPlaces uint8
DecimalPlaces is the exact number of fractional digits represented after the decimal point. For example, DecimalPlaces(5) means raw units 12_331_232 represent the decimal value 123.31232.
type DecimalPlaces0 ¶ added in v1.0.4
type DecimalPlaces0 struct{}
DecimalPlaces0 through DecimalPlaces20 are zero-sized policies that state the exact number of digits represented after the decimal point. Decimal places are independent from the scaled-integer backend: callers choose both the fractional precision and numeric capacity.
func (DecimalPlaces0) FractionalDecimalPlaces ¶ added in v1.0.4
func (DecimalPlaces0) FractionalDecimalPlaces() DecimalPlaces
type DecimalPlaces1 ¶ added in v1.0.4
type DecimalPlaces1 struct{}
func (DecimalPlaces1) FractionalDecimalPlaces ¶ added in v1.0.4
func (DecimalPlaces1) FractionalDecimalPlaces() DecimalPlaces
type DecimalPlaces2 ¶ added in v1.0.4
type DecimalPlaces2 struct{}
func (DecimalPlaces2) FractionalDecimalPlaces ¶ added in v1.0.4
func (DecimalPlaces2) FractionalDecimalPlaces() DecimalPlaces
type DecimalPlaces3 ¶ added in v1.0.4
type DecimalPlaces3 struct{}
func (DecimalPlaces3) FractionalDecimalPlaces ¶ added in v1.0.4
func (DecimalPlaces3) FractionalDecimalPlaces() DecimalPlaces
type DecimalPlaces4 ¶ added in v1.0.4
type DecimalPlaces4 struct{}
func (DecimalPlaces4) FractionalDecimalPlaces ¶ added in v1.0.4
func (DecimalPlaces4) FractionalDecimalPlaces() DecimalPlaces
type DecimalPlaces5 ¶ added in v1.0.4
type DecimalPlaces5 struct{}
func (DecimalPlaces5) FractionalDecimalPlaces ¶ added in v1.0.4
func (DecimalPlaces5) FractionalDecimalPlaces() DecimalPlaces
type DecimalPlaces6 ¶ added in v1.0.4
type DecimalPlaces6 struct{}
func (DecimalPlaces6) FractionalDecimalPlaces ¶ added in v1.0.4
func (DecimalPlaces6) FractionalDecimalPlaces() DecimalPlaces
type DecimalPlaces7 ¶ added in v1.0.4
type DecimalPlaces7 struct{}
func (DecimalPlaces7) FractionalDecimalPlaces ¶ added in v1.0.4
func (DecimalPlaces7) FractionalDecimalPlaces() DecimalPlaces
type DecimalPlaces8 ¶ added in v1.0.4
type DecimalPlaces8 struct{}
func (DecimalPlaces8) FractionalDecimalPlaces ¶ added in v1.0.4
func (DecimalPlaces8) FractionalDecimalPlaces() DecimalPlaces
type DecimalPlaces9 ¶ added in v1.0.4
type DecimalPlaces9 struct{}
func (DecimalPlaces9) FractionalDecimalPlaces ¶ added in v1.0.4
func (DecimalPlaces9) FractionalDecimalPlaces() DecimalPlaces
type DecimalPlaces10 ¶ added in v1.0.4
type DecimalPlaces10 struct{}
func (DecimalPlaces10) FractionalDecimalPlaces ¶ added in v1.0.4
func (DecimalPlaces10) FractionalDecimalPlaces() DecimalPlaces
type DecimalPlaces11 ¶ added in v1.0.4
type DecimalPlaces11 struct{}
func (DecimalPlaces11) FractionalDecimalPlaces ¶ added in v1.0.4
func (DecimalPlaces11) FractionalDecimalPlaces() DecimalPlaces
type DecimalPlaces12 ¶ added in v1.0.4
type DecimalPlaces12 struct{}
func (DecimalPlaces12) FractionalDecimalPlaces ¶ added in v1.0.4
func (DecimalPlaces12) FractionalDecimalPlaces() DecimalPlaces
type DecimalPlaces13 ¶ added in v1.0.4
type DecimalPlaces13 struct{}
func (DecimalPlaces13) FractionalDecimalPlaces ¶ added in v1.0.4
func (DecimalPlaces13) FractionalDecimalPlaces() DecimalPlaces
type DecimalPlaces14 ¶ added in v1.0.4
type DecimalPlaces14 struct{}
func (DecimalPlaces14) FractionalDecimalPlaces ¶ added in v1.0.4
func (DecimalPlaces14) FractionalDecimalPlaces() DecimalPlaces
type DecimalPlaces15 ¶ added in v1.0.4
type DecimalPlaces15 struct{}
func (DecimalPlaces15) FractionalDecimalPlaces ¶ added in v1.0.4
func (DecimalPlaces15) FractionalDecimalPlaces() DecimalPlaces
type DecimalPlaces16 ¶ added in v1.0.4
type DecimalPlaces16 struct{}
func (DecimalPlaces16) FractionalDecimalPlaces ¶ added in v1.0.4
func (DecimalPlaces16) FractionalDecimalPlaces() DecimalPlaces
type DecimalPlaces17 ¶ added in v1.0.4
type DecimalPlaces17 struct{}
func (DecimalPlaces17) FractionalDecimalPlaces ¶ added in v1.0.4
func (DecimalPlaces17) FractionalDecimalPlaces() DecimalPlaces
type DecimalPlaces18 ¶ added in v1.0.4
type DecimalPlaces18 struct{}
func (DecimalPlaces18) FractionalDecimalPlaces ¶ added in v1.0.4
func (DecimalPlaces18) FractionalDecimalPlaces() DecimalPlaces
type DecimalPlaces19 ¶ added in v1.0.4
type DecimalPlaces19 struct{}
func (DecimalPlaces19) FractionalDecimalPlaces ¶ added in v1.0.4
func (DecimalPlaces19) FractionalDecimalPlaces() DecimalPlaces
type DecimalPlaces20 ¶ added in v1.0.4
type DecimalPlaces20 struct{}
func (DecimalPlaces20) FractionalDecimalPlaces ¶ added in v1.0.4
func (DecimalPlaces20) FractionalDecimalPlaces() DecimalPlaces
type Denominated ¶ added in v1.2.0
type Denominated[D comparable, V FixedDecimalFormat[U], U Unit] struct { // contains filtered or unexported fields }
Denominated binds a fixed decimal to a comparable runtime identity. D can be a token identifier, a chain/token pair, or a base/quote market key. The fixed-decimal format continues to own fractional decimal places and unit representation; denomination is never used to infer or normalize scale.
func AddDenominatedAs ¶ added in v1.2.0
func AddDenominatedAs[ ResultV FixedDecimalFormat[ResultU], ResultU Unit, D comparable, AV FixedDecimalFormat[AU], AU Unit, BV FixedDecimalFormat[BU], BU Unit, ]( a Denominated[D, AV, AU], b Denominated[D, BV, BU], ) (Denominated[D, ResultV, ResultU], error)
AddDenominatedAs checks runtime denomination, exactly rescales both values to the selected result format, and adds them.
Example ¶
package main
import (
"fmt"
"github.com/JekaMas/sailfish"
)
func main() {
type Asset struct {
Chain uint32
Token string
}
type Price2 = sailfish.PriceInUint64Units[sailfish.DecimalPlaces2]
type Price5 = sailfish.PriceInUint64Units[sailfish.DecimalPlaces5]
asset := Asset{Chain: 1, Token: "USDC"}
price2, _ := sailfish.NewFixedDecimal[Price2]("1.20")
price5, _ := sailfish.NewFixedDecimal[Price5]("0.00003")
left := sailfish.NewDenominated(asset, price2)
right := sailfish.NewDenominated(asset, price5)
sum, err := sailfish.AddDenominatedAs[Price5](left, right)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(sum.Denomination().Token)
fmt.Println(sum.Decimal().String())
}
Output: USDC 1.20003
func NewDenominated ¶ added in v1.2.0
func NewDenominated[D comparable, V FixedDecimalFormat[U], U Unit]( denomination D, value FixedDecimal[V, U], ) Denominated[D, V, U]
NewDenominated binds value to denomination without changing either value.
func RescaleDenominated ¶ added in v1.2.0
func RescaleDenominated[ ToV FixedDecimalFormat[ToU], ToU Unit, D comparable, FromV FixedDecimalFormat[FromU], FromU Unit, ](value Denominated[D, FromV, FromU]) (Denominated[D, ToV, ToU], error)
RescaleDenominated exactly rescales a value while preserving its runtime denomination. It never derives fractional decimal places from denomination.
func SubDenominatedAs ¶ added in v1.2.0
func SubDenominatedAs[ ResultV FixedDecimalFormat[ResultU], ResultU Unit, D comparable, AV FixedDecimalFormat[AU], AU Unit, BV FixedDecimalFormat[BU], BU Unit, ]( a Denominated[D, AV, AU], b Denominated[D, BV, BU], ) (Denominated[D, ResultV, ResultU], error)
SubDenominatedAs checks runtime denomination, exactly rescales both values to the selected result format, and subtracts them.
func (Denominated[D, V, U]) Add ¶ added in v1.2.0
func (d Denominated[D, V, U]) Add( other Denominated[D, V, U], ) (Denominated[D, V, U], error)
Add adds values only when their denominations match.
func (Denominated[D, V, U]) Compare ¶ added in v1.2.0
func (d Denominated[D, V, U]) Compare(other Denominated[D, V, U]) (int, error)
Compare compares values only when their denominations match.
func (Denominated[D, V, U]) Decimal ¶ added in v1.2.0
func (d Denominated[D, V, U]) Decimal() FixedDecimal[V, U]
Decimal returns the fixed decimal by value.
func (Denominated[D, V, U]) Denomination ¶ added in v1.2.0
func (d Denominated[D, V, U]) Denomination() D
Denomination returns the runtime identity associated with the decimal.
func (Denominated[D, V, U]) Sub ¶ added in v1.2.0
func (d Denominated[D, V, U]) Sub( other Denominated[D, V, U], ) (Denominated[D, V, U], error)
Sub subtracts values only when their denominations match.
type Error ¶
type Error string
Error is an allocation-free, comparable package error.
Exported errors are typed string constants. They work with errors.Is when returned directly or wrapped with fmt.Errorf and %w.
const ( ErrSyntax Error = "sailfish: invalid syntax" ErrRange Error = "sailfish: value does not fit unit type" ErrPrecision Error = "sailfish: too many fractional digits" ErrUnsupportedFractionalDecimalPlaces Error = "sailfish: fractional decimal places are unsupported by unit type" ErrOverflow Error = "sailfish: addition overflow" ErrUnderflow Error = "sailfish: subtraction underflow" ErrDenominationMismatch Error = "sailfish: denomination mismatch" ErrNilSource Error = "sailfish: nil source" ErrNilDestination Error = "sailfish: nil destination" ErrNilWorkspace Error = "sailfish: nil workspace" ErrCBORSyntax Error = "sailfish: invalid CBOR" ErrCBORNonDeterministic Error = "sailfish: non-deterministic CBOR" )
type FixedDecimal ¶ added in v1.0.4
type FixedDecimal[V FixedDecimalFormat[U], U Unit] struct { // contains filtered or unexported fields }
FixedDecimal is an unsigned fixed-scale decimal stored as one scaled integer.
Numeric value = units / 10^fractional-decimal-places. For example, FixedDecimal[PriceInUint64Units[DecimalPlaces5], uint64] stores one uint64; raw units 12_331_232 represent the price 123.31232.
representation is optional immutable wire text. Numeric mutation clears the string header; it never edits string bytes. Clearing the header allocates nothing, and strings previously returned by String remain valid.
Example (IntegerConversions) ¶
package main
import (
"fmt"
"math/big"
"github.com/JekaMas/sailfish"
"github.com/holiman/uint256"
)
type exampleAmountFormat = sailfish.AmountInUint256Units[sailfish.DecimalPlaces18]
func main() {
codec, err := sailfish.NewFixedDecimalCodec[exampleAmountFormat]()
if err != nil {
fmt.Println(err)
return
}
// Integer conversions use already-scaled units. They do not rescale.
value, err := codec.FromU256(uint256.Int{1_250_000_000_000_000_000})
if err != nil {
fmt.Println(err)
return
}
var destination big.Int
if err = value.ToBigInt(&destination); err != nil {
fmt.Println(err)
return
}
fmt.Println(value.String())
fmt.Println(destination.String())
}
Output: 1.250000000000000000 1250000000000000000
Example (RationalConversions) ¶
package main
import (
"fmt"
"math/big"
"github.com/JekaMas/sailfish"
)
type examplePriceFormat = sailfish.PriceInUint64Units[sailfish.DecimalPlaces5]
func main() {
codec, err := sailfish.NewFixedDecimalCodec[examplePriceFormat]()
if err != nil {
fmt.Println(err)
return
}
price, err := codec.FromBigRat(big.NewRat(385_351, 3_125))
if err != nil {
fmt.Println(err)
return
}
var rational big.Rat
var workspace sailfish.BigRatWorkspace
if err = price.ToBigRat(&rational, &workspace); err != nil {
fmt.Println(err)
return
}
fmt.Println(price.String())
fmt.Println(rational.String())
}
Output: 123.31232 385351/3125
Example (Serialization) ¶
package main
import (
"fmt"
"github.com/JekaMas/sailfish"
"github.com/fxamacker/cbor/v2"
json "github.com/goccy/go-json"
"github.com/holiman/uint256"
)
type examplePriceFormat = sailfish.PriceInUint64Units[sailfish.DecimalPlaces5]
type examplePrice = sailfish.FixedDecimal[examplePriceFormat, uint64]
type exampleAmountFormat = sailfish.AmountInUint256Units[sailfish.DecimalPlaces18]
type exampleAmount = sailfish.FixedDecimal[exampleAmountFormat, uint256.Int]
type exampleQuote struct {
_ struct{} `cbor:",toarray"`
Price examplePrice
Amount exampleAmount
}
func main() {
priceCodec, err := sailfish.NewFixedDecimalCodec[examplePriceFormat]()
if err != nil {
fmt.Println(err)
return
}
amountCodec, err := sailfish.NewFixedDecimalCodec[exampleAmountFormat]()
if err != nil {
fmt.Println(err)
return
}
price, err := priceCodec.Parse("123.31232")
if err != nil {
fmt.Println(err)
return
}
amount, err := amountCodec.Parse("1.250000000000000000")
if err != nil {
fmt.Println(err)
return
}
quote := exampleQuote{Price: price, Amount: amount}
jsonRaw, err := json.Marshal(quote.Price)
if err != nil {
fmt.Println(err)
return
}
enc, err := cbor.CanonicalEncOptions().EncMode()
if err != nil {
fmt.Println(err)
return
}
cborRaw, err := enc.Marshal(quote)
if err != nil {
fmt.Println(err)
return
}
dec, err := cbor.DecOptions{}.DecMode()
if err != nil {
fmt.Println(err)
return
}
var decoded exampleQuote
if err := dec.Unmarshal(cborRaw, &decoded); err != nil {
fmt.Println(err)
return
}
fmt.Println(string(jsonRaw))
fmt.Println(decoded.Price.String())
fmt.Println(decoded.Amount.String())
}
Output: "123.31232" 123.31232 1.250000000000000000
func AddAs ¶ added in v1.2.0
func AddAs[ ResultV FixedDecimalFormat[ResultU], ResultU Unit, AV FixedDecimalFormat[AU], AU Unit, BV FixedDecimalFormat[BU], BU Unit, ](a FixedDecimal[AV, AU], b FixedDecimal[BV, BU]) (FixedDecimal[ResultV, ResultU], error)
AddAs exactly rescales both operands to the selected result format and adds them. Use the same-format Add method when no rescaling is required.
func NewCompactFixedDecimal ¶ added in v1.0.4
func NewCompactFixedDecimal[V FixedDecimalFormat[U], U Unit](s string) (FixedDecimal[V, U], error)
NewCompactFixedDecimal parses s without retaining its backing storage.
func NewFixedDecimal ¶ added in v1.0.4
func NewFixedDecimal[V FixedDecimalFormat[U], U Unit](s string) (FixedDecimal[V, U], error)
NewFixedDecimal parses s. It retains s only when s is already canonical fixed-decimal text. Parsing is strict: no whitespace, signs, exponent notation, or excess fractional digits are accepted.
func NewFixedDecimalFromBytes ¶ added in v1.0.4
func NewFixedDecimalFromBytes[V FixedDecimalFormat[U], U Unit](b []byte) (FixedDecimal[V, U], error)
NewFixedDecimalFromBytes parses b without retaining or converting it.
func NewFixedDecimalFromUnits ¶ added in v1.0.4
func NewFixedDecimalFromUnits[V FixedDecimalFormat[U], U Unit](units U) (FixedDecimal[V, U], error)
NewFixedDecimalFromUnits constructs a decimal from already-scaled units.
Example ¶
package main
import (
"fmt"
"github.com/JekaMas/sailfish"
)
func main() {
type AmountFormat = sailfish.AmountInUint32Units[sailfish.DecimalPlaces6]
amount, err := sailfish.NewFixedDecimalFromUnits[AmountFormat](uint32(1_234_567))
if err != nil {
fmt.Println(err)
return
}
fmt.Println(amount.String())
}
Output: 1.234567
func Rescale ¶ added in v1.2.0
func Rescale[ ToV FixedDecimalFormat[ToU], ToU Unit, FromV FixedDecimalFormat[FromU], FromU Unit, ](value FixedDecimal[FromV, FromU]) (FixedDecimal[ToV, ToU], error)
Rescale converts value to an explicitly selected format without rounding. Downscaling rejects discarded nonzero units; upscaling rejects overflow.
func SubAs ¶ added in v1.2.0
func SubAs[ ResultV FixedDecimalFormat[ResultU], ResultU Unit, AV FixedDecimalFormat[AU], AU Unit, BV FixedDecimalFormat[BU], BU Unit, ](a FixedDecimal[AV, AU], b FixedDecimal[BV, BU]) (FixedDecimal[ResultV, ResultU], error)
SubAs exactly rescales both operands to the selected result format and subtracts them. Use the same-format Sub method when no rescaling is required.
func (FixedDecimal[V, U]) Add ¶ added in v1.0.4
func (d FixedDecimal[V, U]) Add(other FixedDecimal[V, U]) (FixedDecimal[V, U], error)
func (*FixedDecimal[V, U]) AddAssign ¶ added in v1.0.4
func (d *FixedDecimal[V, U]) AddAssign(other FixedDecimal[V, U]) (overflow bool)
AddAssign leaves d unchanged on overflow. A value-changing success clears cached text without allocation; adding zero preserves it.
func (FixedDecimal[V, U]) AddOverflow ¶ added in v1.0.4
func (d FixedDecimal[V, U]) AddOverflow(other FixedDecimal[V, U]) (FixedDecimal[V, U], bool)
AddOverflow returns the wrapped sum and reports unit overflow.
func (FixedDecimal[V, U]) AppendCBOR ¶ added in v1.0.4
func (d FixedDecimal[V, U]) AppendCBOR(dst []byte) []byte
AppendCBOR appends the preferred deterministic CBOR encoding. It allocates only when dst has insufficient capacity. When FixedDecimal is a field in a cbor:",toarray" struct, the result is a scalar array element rather than a redundant nested one-element array.
func (FixedDecimal[V, U]) AppendJSON ¶ added in v1.0.4
func (d FixedDecimal[V, U]) AppendJSON(dst []byte) []byte
AppendJSON appends a quoted JSON decimal string. FixedDecimal text contains only ASCII digits and a decimal point, so no escaping pass is needed.
func (FixedDecimal[V, U]) AppendText ¶ added in v1.0.4
func (d FixedDecimal[V, U]) AppendText(dst []byte) ([]byte, error)
AppendText implements the append-style text encoding contract available in current Go versions without requiring a newly owned result slice.
func (FixedDecimal[V, U]) AppendTo ¶ added in v1.0.4
func (d FixedDecimal[V, U]) AppendTo(dst []byte) []byte
AppendTo appends canonical fixed-decimal text. It allocates only when dst has insufficient capacity.
func (FixedDecimal[V, U]) CBORLen ¶ added in v1.0.4
func (d FixedDecimal[V, U]) CBORLen() int
CBORLen returns the exact size of the preferred CBOR encoding. FixedDecimal is encoded as its scaled unsigned integer. Scale and retained source text are type/cache metadata and are intentionally absent from the wire format.
func (FixedDecimal[V, U]) Canonical ¶ added in v1.0.4
func (d FixedDecimal[V, U]) Canonical() FixedDecimal[V, U]
Canonical returns a copy retaining canonical text. It never mutates shared state and is safe to use concurrently with readers of the original value.
func (FixedDecimal[V, U]) Cmp ¶ added in v1.0.4
func (d FixedDecimal[V, U]) Cmp(other FixedDecimal[V, U]) int
func (FixedDecimal[V, U]) Compare ¶ added in v1.0.4
func (d FixedDecimal[V, U]) Compare(other FixedDecimal[V, U]) int
Compare returns -1, 0, or +1.
func (FixedDecimal[V, U]) Equal ¶ added in v1.0.4
func (d FixedDecimal[V, U]) Equal(other FixedDecimal[V, U]) bool
func (FixedDecimal[V, U]) HasRepresentation ¶ added in v1.0.4
func (d FixedDecimal[V, U]) HasRepresentation() bool
HasRepresentation reports whether canonical wire text is currently retained.
func (FixedDecimal[V, U]) IsZero ¶ added in v1.0.4
func (d FixedDecimal[V, U]) IsZero() bool
func (FixedDecimal[V, U]) Len ¶ added in v1.0.4
func (d FixedDecimal[V, U]) Len() int
Len returns the exact canonical text length.
func (FixedDecimal[V, U]) Less ¶ added in v1.0.4
func (d FixedDecimal[V, U]) Less(other FixedDecimal[V, U]) bool
func (FixedDecimal[V, U]) MarshalCBOR ¶ added in v1.0.4
func (d FixedDecimal[V, U]) MarshalCBOR() ([]byte, error)
MarshalCBOR implements the fxamacker/cbor Marshaler contract. The returned owned slice necessarily allocates once; use AppendCBOR on hot paths.
func (FixedDecimal[V, U]) MarshalJSON ¶ added in v1.0.4
func (d FixedDecimal[V, U]) MarshalJSON() ([]byte, error)
func (FixedDecimal[V, U]) MarshalText ¶ added in v1.0.4
func (d FixedDecimal[V, U]) MarshalText() ([]byte, error)
func (*FixedDecimal[V, U]) SetUnits ¶ added in v1.0.4
func (d *FixedDecimal[V, U]) SetUnits(units U)
SetUnits replaces the scaled integer. A value-changing update invalidates cached text without allocation; setting the same value preserves it.
func (FixedDecimal[V, U]) String ¶ added in v1.0.4
func (d FixedDecimal[V, U]) String() string
String returns retained text when available. Otherwise it creates exactly one result string allocation and does not mutate d.
func (FixedDecimal[V, U]) Sub ¶ added in v1.0.4
func (d FixedDecimal[V, U]) Sub(other FixedDecimal[V, U]) (FixedDecimal[V, U], error)
func (*FixedDecimal[V, U]) SubAssign ¶ added in v1.0.4
func (d *FixedDecimal[V, U]) SubAssign(other FixedDecimal[V, U]) (underflow bool)
SubAssign leaves d unchanged on underflow. A value-changing success clears cached text without allocation; subtracting zero preserves it.
func (FixedDecimal[V, U]) SubUnderflow ¶ added in v1.0.4
func (d FixedDecimal[V, U]) SubUnderflow(other FixedDecimal[V, U]) (FixedDecimal[V, U], bool)
SubUnderflow returns the wrapped difference and reports unit underflow.
func (FixedDecimal[V, U]) ToBigInt ¶ added in v1.1.0
func (d FixedDecimal[V, U]) ToBigInt(dst *big.Int) error
ToBigInt writes the already-scaled integer units into caller-owned dst. Reusing a destination with sufficient capacity avoids allocation. A fresh destination may allocate its math/big backing words once.
func (FixedDecimal[V, U]) ToBigRat ¶ added in v1.2.0
func (d FixedDecimal[V, U]) ToBigRat(dst *big.Rat, workspace *BigRatWorkspace) error
ToBigRat writes the exact decimal value into caller-owned dst. Reusing dst and workspace avoids steady-state allocation. The first wide call may grow their math/big backing words once.
func (FixedDecimal[V, U]) ToU256 ¶ added in v1.1.0
func (d FixedDecimal[V, U]) ToU256() uint256.Int
ToU256 returns the already-scaled integer units as one inline four-limb value. Every supported backend fits exactly, so conversion cannot fail.
func (FixedDecimal[V, U]) Units ¶ added in v1.0.4
func (d FixedDecimal[V, U]) Units() U
Units returns the scaled integer by value. uint256.Int is an inline four-limb value, so the returned value owns its storage without allocation.
func (*FixedDecimal[V, U]) UnmarshalCBOR ¶ added in v1.0.4
func (d *FixedDecimal[V, U]) UnmarshalCBOR(raw []byte) error
UnmarshalCBOR implements the fxamacker/cbor Unmarshaler contract. It accepts only RFC 8949 preferred deterministic unsigned encodings and leaves d unchanged on failure. Successful decode clears retained text because CBOR carries numeric units only.
func (*FixedDecimal[V, U]) UnmarshalJSON ¶ added in v1.0.4
func (d *FixedDecimal[V, U]) UnmarshalJSON(data []byte) error
UnmarshalJSON parses ordinary quoted decimals directly without a separate escape scan. go-json handles escaped strings and non-string JSON syntax.
func (*FixedDecimal[V, U]) UnmarshalText ¶ added in v1.0.4
func (d *FixedDecimal[V, U]) UnmarshalText(text []byte) error
type FixedDecimalCodec ¶ added in v1.0.4
type FixedDecimalCodec[V FixedDecimalFormat[U], U Unit] struct { // contains filtered or unexported fields }
FixedDecimalCodec validates a fixed-decimal format once and carries its fractional decimal places through repeated parse and format operations. It is the preferred hot-loop API. Its zero value derives the decimal places from the compile-time format; NewFixedDecimalCodec validates and caches them.
The one-byte decimalPlacesPlusOne encoding reserves zero for zero-value derivation.
Example (ManualPositionalCBOR) ¶
package main
import (
"fmt"
"github.com/JekaMas/sailfish"
"github.com/holiman/uint256"
)
type examplePriceFormat = sailfish.PriceInUint64Units[sailfish.DecimalPlaces5]
type exampleAmountFormat = sailfish.AmountInUint256Units[sailfish.DecimalPlaces18]
func main() {
priceCodec, err := sailfish.NewFixedDecimalCodec[examplePriceFormat]()
if err != nil {
fmt.Println(err)
return
}
amountCodec, err := sailfish.NewFixedDecimalCodec[exampleAmountFormat]()
if err != nil {
fmt.Println(err)
return
}
price := priceCodec.FromUnits(12_331_232)
var amountUnits uint256.Int
amountUnits.SetUint64(1_250_000_000_000_000_000)
amount := amountCodec.FromUnits(amountUnits)
record := make([]byte, 0, 1+2*sailfish.MaxCBORSize)
record = append(record, 0x82) // fixed two-field CBOR array
record = priceCodec.AppendCBOR(record, price)
record = amountCodec.AppendCBOR(record, amount)
raw := record[1:]
decodedPrice, raw, err := priceCodec.ParseCBORFirst(raw)
if err != nil {
fmt.Println(err)
return
}
decodedAmount, raw, err := amountCodec.ParseCBORFirst(raw)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(decodedPrice.String())
fmt.Println(decodedAmount.String())
fmt.Println(len(raw))
}
Output: 123.31232 1.250000000000000000 0
Example (Price) ¶
package main
import (
"fmt"
"github.com/JekaMas/sailfish"
)
type examplePriceFormat = sailfish.PriceInUint64Units[sailfish.DecimalPlaces5]
func main() {
codec, err := sailfish.NewFixedDecimalCodec[examplePriceFormat]()
if err != nil {
fmt.Println(err)
return
}
price, err := codec.Parse("123.31232")
if err != nil {
fmt.Println(err)
return
}
next, err := price.Add(codec.FromUnits(1))
if err != nil {
fmt.Println(err)
return
}
fmt.Println(next.String())
fmt.Println(next.Units())
}
Output: 123.31233 12331233
func NewFixedDecimalCodec ¶ added in v1.0.4
func NewFixedDecimalCodec[V FixedDecimalFormat[U], U Unit]() (FixedDecimalCodec[V, U], error)
NewFixedDecimalCodec validates the format's fractional decimal places and caches them for repeated operations.
func (FixedDecimalCodec[V, U]) AppendCBOR ¶ added in v1.0.4
func (c FixedDecimalCodec[V, U]) AppendCBOR(dst []byte, d FixedDecimal[V, U]) []byte
AppendCBOR appends preferred deterministic CBOR after validating the codec.
func (FixedDecimalCodec[V, U]) AppendJSON ¶ added in v1.0.4
func (c FixedDecimalCodec[V, U]) AppendJSON(dst []byte, d FixedDecimal[V, U]) []byte
func (FixedDecimalCodec[V, U]) AppendTo ¶ added in v1.0.4
func (c FixedDecimalCodec[V, U]) AppendTo(dst []byte, d FixedDecimal[V, U]) []byte
func (FixedDecimalCodec[V, U]) AppendUnits ¶ added in v1.0.4
func (c FixedDecimalCodec[V, U]) AppendUnits(dst []byte, units U) []byte
AppendUnits appends canonical fixed-decimal text directly from raw integer units. It allocates only when dst has insufficient capacity.
func (FixedDecimalCodec[V, U]) CBORLen ¶ added in v1.0.4
func (c FixedDecimalCodec[V, U]) CBORLen(d FixedDecimal[V, U]) int
CBORLen returns the exact preferred CBOR size after validating the codec.
func (FixedDecimalCodec[V, U]) Canonical ¶ added in v1.0.4
func (c FixedDecimalCodec[V, U]) Canonical(d FixedDecimal[V, U]) FixedDecimal[V, U]
func (FixedDecimalCodec[V, U]) FractionalDecimalPlaces ¶ added in v1.0.4
func (c FixedDecimalCodec[V, U]) FractionalDecimalPlaces() DecimalPlaces
FractionalDecimalPlaces returns the exact number of digits represented after the decimal point.
func (FixedDecimalCodec[V, U]) FromBigInt ¶ added in v1.1.0
func (FixedDecimalCodec[V, U]) FromBigInt(source *big.Int) (FixedDecimal[V, U], error)
FromBigInt constructs a fixed decimal from non-negative, already-scaled integer units. It reads source without retaining or modifying it.
func (FixedDecimalCodec[V, U]) FromBigRat ¶ added in v1.2.0
func (c FixedDecimalCodec[V, U]) FromBigRat(source *big.Rat) (FixedDecimal[V, U], error)
FromBigRat constructs a fixed decimal only when source has an exact representation at the format's fractional decimal places. It performs no rounding and does not retain or modify source.
func (FixedDecimalCodec[V, U]) FromU256 ¶ added in v1.1.0
func (FixedDecimalCodec[V, U]) FromU256(source uint256.Int) (FixedDecimal[V, U], error)
FromU256 constructs a fixed decimal from non-negative, already-scaled uint256 units. Narrow backends reject values outside their integer width.
func (FixedDecimalCodec[V, U]) FromUnits ¶ added in v1.0.4
func (c FixedDecimalCodec[V, U]) FromUnits(units U) FixedDecimal[V, U]
func (FixedDecimalCodec[V, U]) Len ¶ added in v1.0.4
func (c FixedDecimalCodec[V, U]) Len(d FixedDecimal[V, U]) int
func (FixedDecimalCodec[V, U]) MaxIntegerDigits ¶ added in v1.0.4
func (c FixedDecimalCodec[V, U]) MaxIntegerDigits() int
MaxIntegerDigits reports how many decimal digits can occur before the point in this backend's maximum value for the configured fractional decimal places. It describes capacity independently from fractional precision; it does not imply every value with that many digits fits the binary backend.
func (FixedDecimalCodec[V, U]) Parse ¶ added in v1.0.4
func (c FixedDecimalCodec[V, U]) Parse(s string) (FixedDecimal[V, U], error)
Parse retains s only when it is already canonical fixed-decimal text.
func (FixedDecimalCodec[V, U]) ParseBytes ¶ added in v1.0.4
func (c FixedDecimalCodec[V, U]) ParseBytes(b []byte) (FixedDecimal[V, U], error)
ParseBytes parses b directly and never retains it.
func (FixedDecimalCodec[V, U]) ParseCBOR ¶ added in v1.0.4
func (c FixedDecimalCodec[V, U]) ParseCBOR(raw []byte) (FixedDecimal[V, U], error)
ParseCBOR decodes preferred deterministic CBOR without retaining raw input.
func (FixedDecimalCodec[V, U]) ParseCBORFirst ¶ added in v1.0.4
func (c FixedDecimalCodec[V, U]) ParseCBORFirst(raw []byte) (FixedDecimal[V, U], []byte, error)
ParseCBORFirst decodes one preferred deterministic CBOR decimal from the start of raw and returns the unconsumed suffix. It is the typed hot-path decoder for decimal fields inside manually encoded positional arrays. ParseCBOR remains the strict whole-item API.
func (FixedDecimalCodec[V, U]) ParseCompact ¶ added in v1.0.4
func (c FixedDecimalCodec[V, U]) ParseCompact(s string) (FixedDecimal[V, U], error)
ParseCompact never retains s.
func (FixedDecimalCodec[V, U]) ParseUnits ¶ added in v1.0.4
func (c FixedDecimalCodec[V, U]) ParseUnits(s string) (U, Error)
ParseUnits parses strict fixed-decimal text directly into the selected unit backend. Use it when a numeric batch stores raw units for the smallest cache footprint and does not need FixedDecimal's optional retained representation. Successful and rejected parses allocate no memory.
func (FixedDecimalCodec[V, U]) ParseUnitsBytes ¶ added in v1.0.4
func (c FixedDecimalCodec[V, U]) ParseUnitsBytes(b []byte) (U, Error)
ParseUnitsBytes is ParseUnits for byte input. It neither converts nor retains b.
func (FixedDecimalCodec[V, U]) String ¶ added in v1.0.4
func (c FixedDecimalCodec[V, U]) String(d FixedDecimal[V, U]) string
func (FixedDecimalCodec[V, U]) UnitsLen ¶ added in v1.0.4
func (c FixedDecimalCodec[V, U]) UnitsLen(units U) int
UnitsLen returns the exact canonical text length of raw integer units.
type FixedDecimalFormat ¶ added in v1.0.4
type FixedDecimalFormat[U Unit] interface { StaticDecimalPlaces // contains filtered or unexported methods }
FixedDecimalFormat binds a semantic decimal kind, exact fractional decimal places, and one scaled-integer backend. Prefer PriceInUint*Units and AmountInUint*Units. A custom format is normally a zero-sized type:
type QuotePriceWith5DecimalPlaces struct{ sailfish.Uint64Units }
func (QuotePriceWith5DecimalPlaces) FractionalDecimalPlaces() sailfish.DecimalPlaces {
return 5
}
type NativeUnit ¶
type NativeUnit interface {
comparable
uint8 | uint16 | uint32 | uint64
}
NativeUnit is the subset backed by Go's native unsigned integer types.
type PriceInUint8Units ¶ added in v1.0.4
type PriceInUint8Units[S StaticDecimalPlaces] struct { Uint8Units }
PriceInUint8Units through PriceInUint256Units identify prices represented by one unsigned scaled integer of the named width. The type parameter states the exact fractional decimal places. For example, PriceInUint64Units[DecimalPlaces5] stores uint64 units and represents numeric value as units / 100000. Backend width controls range; it is not inferred from the decimal places.
func (PriceInUint8Units[S]) FractionalDecimalPlaces ¶ added in v1.0.4
func (PriceInUint8Units[S]) FractionalDecimalPlaces() DecimalPlaces
type PriceInUint16Units ¶ added in v1.0.4
type PriceInUint16Units[S StaticDecimalPlaces] struct { Uint16Units }
func (PriceInUint16Units[S]) FractionalDecimalPlaces ¶ added in v1.0.4
func (PriceInUint16Units[S]) FractionalDecimalPlaces() DecimalPlaces
type PriceInUint32Units ¶ added in v1.0.4
type PriceInUint32Units[S StaticDecimalPlaces] struct { Uint32Units }
func (PriceInUint32Units[S]) FractionalDecimalPlaces ¶ added in v1.0.4
func (PriceInUint32Units[S]) FractionalDecimalPlaces() DecimalPlaces
type PriceInUint64Units ¶ added in v1.0.4
type PriceInUint64Units[S StaticDecimalPlaces] struct { Uint64Units }
func (PriceInUint64Units[S]) FractionalDecimalPlaces ¶ added in v1.0.4
func (PriceInUint64Units[S]) FractionalDecimalPlaces() DecimalPlaces
type PriceInUint256Units ¶ added in v1.0.4
type PriceInUint256Units[S StaticDecimalPlaces] struct { Uint256Units }
func (PriceInUint256Units[S]) FractionalDecimalPlaces ¶ added in v1.0.4
func (PriceInUint256Units[S]) FractionalDecimalPlaces() DecimalPlaces
type StaticDecimalPlaces ¶ added in v1.0.4
type StaticDecimalPlaces interface {
FractionalDecimalPlaces() DecimalPlaces
}
StaticDecimalPlaces supplies an exact compile-time count of fractional decimal places. Implement it on a zero-sized value type with a value receiver.
type Uint8Units ¶
type Uint8Units struct{}
Uint8Units, Uint16Units, and Uint32Units are zero-sized unit providers. Embed one in a custom fixed-decimal format, or use the explicit PriceInUint*Units and AmountInUint*Units formats.
type Uint16Units ¶
type Uint16Units struct{}
type Uint32Units ¶
type Uint32Units struct{}
type Uint64Units ¶
type Uint64Units struct{}
Uint64Units is a zero-sized unit provider. Embed it in a format type.
type Uint256FixedDecimalCodec ¶ added in v1.0.4
type Uint256FixedDecimalCodec struct {
// contains filtered or unexported fields
}
Uint256FixedDecimalCodec is the runtime-decimal-places hot-path codec for scaled uint256 units.
Use FixedDecimalCodec with a FixedDecimalFormat when compile-time semantic identity and decimal places are required. Use Uint256FixedDecimalCodec at boundaries where trusted metadata resolves the fractional decimal places at runtime, such as CEX symbol decoding. Its methods avoid generic format dispatch and return Error directly so successful and rejected parses remain allocation-free.
The zero value represents zero fractional decimal places. The number of decimal places is stored directly in one byte so repeated boundary operations do not decode constructor metadata.
Example ¶
package main
import (
"fmt"
"github.com/JekaMas/sailfish"
"github.com/holiman/uint256"
)
func main() {
codec, err := sailfish.NewUint256FixedDecimalCodec(18)
if err != nil {
fmt.Println(err)
return
}
var units uint256.Int
if parseErr := codec.ParseInto("1.250000000000000000", &units); parseErr != "" {
fmt.Println(parseErr)
return
}
fmt.Println(string(codec.AppendTo(nil, units)))
}
Output: 1.250000000000000000
func NewUint256FixedDecimalCodec ¶ added in v1.0.4
func NewUint256FixedDecimalCodec( fractionalDecimalPlaces DecimalPlaces, ) (Uint256FixedDecimalCodec, error)
NewUint256FixedDecimalCodec validates the exact number of fractional decimal places once for repeated uint256 operations.
func (Uint256FixedDecimalCodec) AppendCBOR ¶ added in v1.0.4
func (c Uint256FixedDecimalCodec) AppendCBOR(dst []byte, units uint256.Int) []byte
AppendCBOR appends the preferred deterministic CBOR encoding for units. It allocates only when dst has insufficient capacity.
func (Uint256FixedDecimalCodec) AppendTo ¶ added in v1.0.4
func (c Uint256FixedDecimalCodec) AppendTo(dst []byte, units uint256.Int) []byte
AppendTo appends canonical fixed-scale text for units. It allocates only when dst has insufficient capacity.
func (Uint256FixedDecimalCodec) CBORLen ¶ added in v1.0.4
func (c Uint256FixedDecimalCodec) CBORLen(units uint256.Int) int
CBORLen returns the exact preferred deterministic CBOR size for units.
func (Uint256FixedDecimalCodec) FractionalDecimalPlaces ¶ added in v1.0.4
func (c Uint256FixedDecimalCodec) FractionalDecimalPlaces() DecimalPlaces
FractionalDecimalPlaces returns the exact number of digits represented after the decimal point.
func (Uint256FixedDecimalCodec) Len ¶ added in v1.0.4
func (c Uint256FixedDecimalCodec) Len(units uint256.Int) int
Len returns the exact canonical text length for units.
func (Uint256FixedDecimalCodec) MaxIntegerDigits ¶ added in v1.0.4
func (c Uint256FixedDecimalCodec) MaxIntegerDigits() int
MaxIntegerDigits reports the maximum integer-part digit count for the configured fractional decimal places.
func (Uint256FixedDecimalCodec) Parse ¶ added in v1.0.4
func (c Uint256FixedDecimalCodec) Parse(input string) (uint256.Int, Error)
Parse parses a strict non-negative decimal string into scaled units.
func (Uint256FixedDecimalCodec) ParseBytes ¶ added in v1.0.4
func (c Uint256FixedDecimalCodec) ParseBytes(input []byte) (uint256.Int, Error)
ParseBytes parses input without converting it to a string.
func (Uint256FixedDecimalCodec) ParseBytesInto ¶ added in v1.0.4
func (c Uint256FixedDecimalCodec) ParseBytesInto(input []byte, dst *uint256.Int) Error
ParseBytesInto parses input into dst without converting it to a string. It leaves dst unchanged on failure.
func (Uint256FixedDecimalCodec) ParseCBOR ¶ added in v1.0.4
func (c Uint256FixedDecimalCodec) ParseCBOR(raw []byte) (uint256.Int, Error)
ParseCBOR decodes preferred deterministic CBOR into scaled units.
func (Uint256FixedDecimalCodec) ParseCBORFirst ¶ added in v1.0.4
ParseCBORFirst decodes one preferred deterministic CBOR uint256 from the start of raw and returns the unconsumed suffix. It is intended for manual positional-array decoders that keep aggregate decoding allocation-free.
func (Uint256FixedDecimalCodec) ParseCBORFirstInto ¶ added in v1.0.4
ParseCBORFirstInto decodes one preferred deterministic CBOR uint256 into dst and returns the unconsumed suffix. It leaves dst unchanged on failure.
func (Uint256FixedDecimalCodec) ParseCBORInto ¶ added in v1.0.4
func (c Uint256FixedDecimalCodec) ParseCBORInto(raw []byte, dst *uint256.Int) Error
ParseCBORInto decodes preferred deterministic CBOR into dst. It leaves dst unchanged on failure.
type Uint256Units ¶
type Uint256Units struct{}
Uint256Units is a zero-sized unit provider. Embed it in a format type.
Source Files
¶
- cbor.go
- codec.go
- decimal.go
- denominated.go
- doc.go
- encoding.go
- errors.go
- fixed_decimal_format.go
- format.go
- format_reverse_swar.go
- formats.go
- integer_conversion.go
- load_eight_native.go
- parse_digits.go
- rational_conversion.go
- raw_digits.go
- rescale.go
- uint256_codec.go
- unit_arithmetic.go
- units_narrow.go
- units_uint256.go
- units_uint64.go