Skip to content

Commit

Permalink
feat: update wei to sat conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
Luisfc68 committed Dec 4, 2024
1 parent fe91517 commit dc6b0e3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
9 changes: 8 additions & 1 deletion internal/entities/wei.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
17 changes: 16 additions & 1 deletion internal/entities/wei_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit dc6b0e3

Please sign in to comment.