diff --git a/math/math_helper.go b/math/math_helper.go index 47e225526f..b4442f56db 100644 --- a/math/math_helper.go +++ b/math/math_helper.go @@ -214,11 +214,13 @@ func AddInt(i ...int) (int, error) { } // WeiToGwei converts big int wei to uint64 gwei. +// The input `v` is copied before being modified. func WeiToGwei(v *big.Int) uint64 { if v == nil { return 0 } gweiPerEth := big.NewInt(1e9) - v.Div(v, gweiPerEth) - return v.Uint64() + copied := big.NewInt(0).Set(v) + copied.Div(copied, gweiPerEth) + return copied.Uint64() } diff --git a/math/math_helper_test.go b/math/math_helper_test.go index 9810e8bc1e..d3b5231fb3 100644 --- a/math/math_helper_test.go +++ b/math/math_helper_test.go @@ -567,3 +567,11 @@ func TestWeiToGwei(t *testing.T) { } } } + +func TestWeiToGwei_CopyOk(t *testing.T) { + v := big.NewInt(1e9) + got := math.WeiToGwei(v) + + require.Equal(t, uint64(1), got) + require.Equal(t, big.NewInt(1e9).Uint64(), v.Uint64()) +}