From dc6b0e3bef32c344b726d5f9a9caf5ee76070cb9 Mon Sep 17 00:00:00 2001 From: Luisfc68 Date: Wed, 4 Dec 2024 12:27:58 +0100 Subject: [PATCH] feat: update wei to sat conversion --- internal/entities/wei.go | 9 ++++++++- internal/entities/wei_test.go | 17 ++++++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/internal/entities/wei.go b/internal/entities/wei.go index a3bb2143..aa8fe8a9 100644 --- a/internal/entities/wei.go +++ b/internal/entities/wei.go @@ -63,7 +63,14 @@ func (w *Wei) ToRbtc() *big.Float { } func (w *Wei) ToSatoshi() *big.Float { - return new(big.Float).Quo(new(big.Float).SetInt(w.AsBigInt()), new(big.Float).SetInt(bTenPowTen)) + remainder := new(big.Int) + quotient := new(big.Int) + quotient.QuoRem(w.AsBigInt(), bTenPowTen, remainder) + if remainder.Cmp(big.NewInt(0)) == 0 { + return new(big.Float).SetInt(quotient) + } + quotient.Add(quotient, big.NewInt(1)) + return new(big.Float).SetInt(quotient) } func (w *Wei) String() string { diff --git a/internal/entities/wei_test.go b/internal/entities/wei_test.go index 9a343633..7420f657 100644 --- a/internal/entities/wei_test.go +++ b/internal/entities/wei_test.go @@ -162,7 +162,22 @@ func TestWei_ToSatoshi(t *testing.T) { { name: "1 wei to sat", w: entities.NewWei(1), - want: new(big.Float).Quo(new(big.Float).SetInt64(1), new(big.Float).SetInt(new(big.Int).Exp(big.NewInt(10), big.NewInt(10), nil))), + want: big.NewFloat(1), + }, + { + name: "72160329123080000 wei to 7216033 sat", + w: entities.NewWei(72160329123080000), + want: big.NewFloat(7216033), + }, + { + name: "4360000000000000 wei to 436000 sat", + w: entities.NewWei(4360000000000000), + want: big.NewFloat(436000), + }, + { + name: "1 RBTC to 100000000 sat", + w: entities.NewWei(1000000000000000000), + want: big.NewFloat(100000000), }, } for _, tt := range tests {