WeiToGwei copy input big.int (#12370)

This commit is contained in:
terencechain
2023-05-07 08:15:08 -07:00
committed by GitHub
parent 6f383f272a
commit 4bd7e8aefd
2 changed files with 12 additions and 2 deletions

View File

@@ -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()
}

View File

@@ -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())
}