Skip to content

Commit

Permalink
added FromWei(…)
Browse files Browse the repository at this point in the history
- added `FromWei(…)` example
- upgraded `github.com/google/go-cmp` v0.5.6 -> v0.5.7
  • Loading branch information
lmittmann committed Jan 22, 2022
1 parent 1b0e204 commit 49980d1
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 3 deletions.
8 changes: 8 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ func ExampleI() {
// 31415000000 wei
}

func ExampleFromWei() {
wei := big.NewInt(1_230000000_000000000)
fmt.Printf("%s Ether\n", w3.FromWei(wei, 18))

// Output:
// 1.23 Ether
}

func ExampleNewFunc() {
// ABI binding to the balanceOf function of an ERC20 Token.
funcBalanceOf, _ := w3.NewFunc("balanceOf(address)", "uint256")
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.17

require (
github.com/ethereum/go-ethereum v1.10.15
github.com/google/go-cmp v0.5.6
github.com/google/go-cmp v0.5.7
)

require (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.4.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
github.com/google/gofuzz v1.1.1-0.20200604201612-c04b05f3adfa/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
Expand Down
23 changes: 23 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package w3

import (
"fmt"
"math/big"
"strings"

Expand Down Expand Up @@ -154,3 +155,25 @@ Outer:
func Keccak(data []byte) common.Hash {
return crypto.Keccak256Hash(data)
}

// FromWei returns the given Wei as decimal with the given number of decimals.
func FromWei(wei *big.Int, decimals uint8) string {
if wei == nil {
return fmt.Sprint(nil)
}

d := new(big.Int).Exp(big.NewInt(10), big.NewInt(int64(decimals)), nil)

sign := ""
if wei.Sign() < 0 {
sign = "-"
}
wei = new(big.Int).Abs(wei)

z, m := new(big.Int).DivMod(wei, d, new(big.Int))
if m.Cmp(new(big.Int)) == 0 {
return sign + z.String()
}
s := strings.TrimRight(fmt.Sprintf("%0*s", decimals, m.String()), "0")
return sign + z.String() + "." + s
}
43 changes: 43 additions & 0 deletions util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,46 @@ func BenchmarkI(b *testing.B) {
})
}
}

func TestFromWei(t *testing.T) {
t.Parallel()

tests := []struct {
Wei *big.Int
Decimals uint8
Want string
}{
{big.NewInt(0), 0, "0"},
{big.NewInt(1), 0, "1"},
{big.NewInt(0), 18, "0"},
{big.NewInt(1), 18, "0.000000000000000001"},
{big.NewInt(1000), 18, "0.000000000000001"},
{big.NewInt(1000000), 18, "0.000000000001"},
{big.NewInt(1000000000), 18, "0.000000001"},
{big.NewInt(1000000000000), 18, "0.000001"},
{big.NewInt(1000000000000000), 18, "0.001"},
{big.NewInt(1000000000000000000), 18, "1"},
{big.NewInt(-1), 18, "-0.000000000000000001"},
{big.NewInt(-1000), 18, "-0.000000000000001"},
{big.NewInt(-1000000), 18, "-0.000000000001"},
{big.NewInt(-1000000000), 18, "-0.000000001"},
{big.NewInt(-1000000000000), 18, "-0.000001"},
{big.NewInt(-1000000000000000), 18, "-0.001"},
{big.NewInt(-1000000000000000000), 18, "-1"},
{big.NewInt(1000000000000000000), 15, "1000"},
{big.NewInt(1000000000000000000), 12, "1000000"},
{big.NewInt(1000000000000000000), 9, "1000000000"},
{big.NewInt(1000000000000000000), 6, "1000000000000"},
{big.NewInt(1000000000000000000), 3, "1000000000000000"},
{big.NewInt(1000000000000000000), 0, "1000000000000000000"},
}

for i, test := range tests {
t.Run(strconv.Itoa(i), func(t *testing.T) {
got := FromWei(test.Wei, test.Decimals)
if got != test.Want {
t.Fatalf("%q != %q", got, test.Want)
}
})
}
}

0 comments on commit 49980d1

Please sign in to comment.