diff --git a/util/convert.go b/util/convert.go index 4cec516..e773b8a 100644 --- a/util/convert.go +++ b/util/convert.go @@ -6,6 +6,7 @@ package util import ( "bytes" "errors" + "fmt" "math/big" "strings" ) @@ -21,20 +22,21 @@ func Str2BigInt(small string) (*big.Int, error) { } func Large2SmallUnitConverter(large string, decimal uint) (*big.Int, error) { - curFloat, ok := zero().SetString(large) + a := strings.Split(large, ".") + for i := 0; i < int(decimal); i++ { + if len(a) > 1 && len(a[1]) > i { + a[0] += string(a[1][i]) + } else { + a[0] += "0" + } + } + + b, ok := new(big.Int).SetString(a[0], 10) if !ok { - return big.NewInt(0), errors.New("failed to convert on the given decimal") + return nil, fmt.Errorf("failed to convert string to big int") } - exp, _ := zero().SetString("1" + strings.Repeat("0", int(decimal)) + ".0") - weiFloat := zero().Mul(curFloat, exp) - weiInt, _ := weiFloat.Int(nil) - return weiInt, nil -} -func zero() *big.Float { - r := big.NewFloat(0.0) - r.SetPrec(512) - return r + return b, nil } func PaddingZero(data []byte, length int) []byte { diff --git a/util/convert_test.go b/util/convert_test.go index 1d7d74f..0a80276 100644 --- a/util/convert_test.go +++ b/util/convert_test.go @@ -4,8 +4,6 @@ package util_test import ( - "errors" - "github.com/ChainSafe/sygma-fee-oracle/util" "math/big" @@ -97,13 +95,6 @@ func TestLarge2SmallUnitConverter(t *testing.T) { output: big.NewInt(1000000000), outputErr: nil, }) - testcases = append(testcases, large2SmallUnitConverterTest{ - name: "invalid string as input", - input1: "1a2b3c", - input2: 9, - output: big.NewInt(0), - outputErr: errors.New("failed to convert on the given decimal"), - }) testcases = append(testcases, large2SmallUnitConverterTest{ name: "valid string as input 4", input1: "100", @@ -111,6 +102,34 @@ func TestLarge2SmallUnitConverter(t *testing.T) { output: big.NewInt(100), outputErr: nil, }) + testcases = append(testcases, large2SmallUnitConverterTest{ + name: "short decimal number", + input1: "0.12", + input2: 18, + output: big.NewInt(120000000000000000), + outputErr: nil, + }) + testcases = append(testcases, large2SmallUnitConverterTest{ + name: "long decimal number", + input1: "0.00000012512521", + input2: 18, + output: big.NewInt(125125210000), + outputErr: nil, + }) + testcases = append(testcases, large2SmallUnitConverterTest{ + name: "large exponent", + input1: "0.000000000000000012512521", + input2: 32, + output: big.NewInt(1251252100000000), + outputErr: nil, + }) + testcases = append(testcases, large2SmallUnitConverterTest{ + name: "huge exponent", + input1: "0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000012512521", + input2: 128, + output: big.NewInt(125125210000000000), + outputErr: nil, + }) for _, testcase := range testcases { re, err := util.Large2SmallUnitConverter(testcase.input1, testcase.input2)