mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-11-24 06:25:55 +00:00
Fix off by small amounts in sent amount kaspa
Floating point math causes inconsistencies when converting kas to sompi. Use a method that parses the amount as a string, the converts it to sompi then parse back to uint64
This commit is contained in:
parent
c417c8b525
commit
471b7b2f16
@ -4,13 +4,13 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/kaspanet/kaspad/cmd/kaspawallet/daemon/client"
|
"github.com/kaspanet/kaspad/cmd/kaspawallet/daemon/client"
|
||||||
"github.com/kaspanet/kaspad/cmd/kaspawallet/daemon/pb"
|
"github.com/kaspanet/kaspad/cmd/kaspawallet/daemon/pb"
|
||||||
"github.com/kaspanet/kaspad/cmd/kaspawallet/keys"
|
"github.com/kaspanet/kaspad/cmd/kaspawallet/keys"
|
||||||
"github.com/kaspanet/kaspad/cmd/kaspawallet/libkaspawallet"
|
"github.com/kaspanet/kaspad/cmd/kaspawallet/libkaspawallet"
|
||||||
"github.com/kaspanet/kaspad/domain/consensus/utils/constants"
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -35,7 +35,7 @@ func send(conf *sendConfig) error {
|
|||||||
|
|
||||||
var sendAmountSompi uint64
|
var sendAmountSompi uint64
|
||||||
if !conf.IsSendAll {
|
if !conf.IsSendAll {
|
||||||
sendAmountSompi = uint64(conf.SendAmount * constants.SompiPerKaspa)
|
sendAmountSompi = kasToSompi(conf.SendAmount)
|
||||||
}
|
}
|
||||||
|
|
||||||
createUnsignedTransactionsResponse, err :=
|
createUnsignedTransactionsResponse, err :=
|
||||||
@ -99,3 +99,15 @@ func send(conf *sendConfig) error {
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*/
|
||||||
|
func kasToSompi(amount float64) uint64 {
|
||||||
|
amountInStr := fmt.Sprintf("%.8f", amount)
|
||||||
|
|
||||||
|
parts := strings.Split(amountInStr, ".")
|
||||||
|
|
||||||
|
convertedAmount, _ := strconv.ParseUint(strings.Join(parts, ""), 10, 64)
|
||||||
|
|
||||||
|
return convertedAmount
|
||||||
|
}
|
||||||
|
|||||||
25
cmd/kaspawallet/send_test.go
Normal file
25
cmd/kaspawallet/send_test.go
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestKasToSompi(t *testing.T) {
|
||||||
|
type testVector struct {
|
||||||
|
originalAmount float64
|
||||||
|
convertedAmount uint64
|
||||||
|
}
|
||||||
|
|
||||||
|
testVectors := []testVector{
|
||||||
|
{originalAmount: 0, convertedAmount: 0},
|
||||||
|
{originalAmount: 1, convertedAmount: 100000000},
|
||||||
|
{originalAmount: 33184.1489732, convertedAmount: 3318414897320},
|
||||||
|
{originalAmount: 21.35808032, convertedAmount: 2135808032},
|
||||||
|
{originalAmount: 184467440737.09551615, convertedAmount: 18446744073709551615},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, currentTestVector := range testVectors {
|
||||||
|
if kasToSompi(currentTestVector.originalAmount) != currentTestVector.convertedAmount {
|
||||||
|
t.Fail()
|
||||||
|
t.Logf("Expected %.8f, to convert to %d. Got: %d", currentTestVector.originalAmount, currentTestVector.convertedAmount, kasToSompi(currentTestVector.originalAmount))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user