Skip to content

Commit

Permalink
Merge pull request #6 from stasundr/wei_converter
Browse files Browse the repository at this point in the history
Rollback wei converter due another bugs
  • Loading branch information
yakud authored Apr 5, 2022
2 parents ad81cdf + ed10939 commit 0aa91c8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 19 deletions.
3 changes: 1 addition & 2 deletions wei.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import (
"math/big"
)

// ConvertToWei deprecated. Instead of ConvertToWei just use decimal.ToBig()
func ConvertToWei(dec *Decimal, mantissa uint8) *big.Int {
if dec == nil || dec.value == nil {
return new(big.Int).SetUint64(0)
}

return NewDecimal(dec).ToBig()
return NewDecimal(dec).Rescale(mantissa).ToBig()
}
47 changes: 30 additions & 17 deletions wei_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,53 @@ import (
)

func TestConvertToWei(t *testing.T) {
d, ok := NewDecimalFromString("1")
actual, ok := NewDecimalFromString("1")
assert.True(t, ok)
assert.Equal(t, ConvertToWei(d, 0), new(big.Int).SetUint64(1))
assert.Equal(t, new(big.Int).SetUint64(1), ConvertToWei(actual, 0))

d, ok = NewDecimalFromString("1")
actual, ok = NewDecimalFromString("1")
assert.True(t, ok)
assert.Equal(t, ConvertToWei(d, 10), new(big.Int).SetUint64(1))
assert.Equal(t, new(big.Int).SetUint64(10000000000), ConvertToWei(actual, 10))

d, ok = NewDecimalFromString("999999999999999999")
actual, ok = NewDecimalFromString("999999999999999999")
assert.True(t, ok)
assert.Equal(t, ConvertToWei(d, 10), new(big.Int).SetUint64(999999999999999999))
expected, ok := new(big.Int).SetString("9999999999999999990000000000", 10)
assert.True(t, ok)
assert.Equal(t, expected, ConvertToWei(actual, 10))

d, ok = NewDecimalFromString("115792089237316195423570985008687907853269984665640564039457584007913129639935")
actual, ok = NewDecimalFromString("115792089237316195423570985008687907853269984665640564039457584007913129639935")
assert.True(t, ok)
expected, ok = new(big.Int).SetString("115792089237316195423570985008687907853269984665640564039457584007913129639935", 10)
assert.True(t, ok)
expected, ok := new(big.Int).SetString("115792089237316195423570985008687907853269984665640564039457584007913129639935", 10)
assert.Equal(t, ConvertToWei(d, 0), expected)
assert.Equal(t, expected, ConvertToWei(actual, 0))

d, ok = NewDecimalFromString("115792089237316195423570985008687907853269984665640564039457584007913129639935")
actual, ok = NewDecimalFromString("115792089237316195423570985008687907853269984665640564039457584007913129639935")
assert.True(t, ok)
expected, ok = new(big.Int).SetString("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16)
assert.Equal(t, ConvertToWei(d, 0), expected)
assert.True(t, ok)
assert.Equal(t, expected, ConvertToWei(actual, 0))

d, ok = NewDecimalFromString("10.10001")
actual, ok = NewDecimalFromString("10.10001")
assert.True(t, ok)
expected, ok = new(big.Int).SetString("1010001", 10)
assert.Equal(t, ConvertToWei(d, 0), expected)
assert.True(t, ok)
assert.Equal(t, expected, ConvertToWei(actual, 5))

d, ok = NewDecimalFromString("10.10001111111999999999901")
actual, ok = NewDecimalFromString("10.10001111111999999999901")
assert.True(t, ok)
expected, ok = new(big.Int).SetString("1010001111111999999999901", 10)
assert.Equal(t, ConvertToWei(d, 0), expected)
assert.True(t, ok)
assert.Equal(t, expected, ConvertToWei(actual, 23))

d, ok = NewDecimalFromString("10.1000111111199999999990112312313123123123")
actual, ok = NewDecimalFromString("10.1000111111199999999990112312313123123123")
assert.True(t, ok)
expected, ok = new(big.Int).SetString("101000111111199999999990112312313123123123", 10)
assert.Equal(t, ConvertToWei(d, 0), expected)
assert.True(t, ok)
assert.Equal(t, expected, ConvertToWei(actual, 40))

actual, ok = NewDecimalFromString("115792089237316195423570985008687907853269984665640564039457.584007913129639935")
assert.True(t, ok)
expected, ok = new(big.Int).SetString("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16)
assert.True(t, ok)
assert.Equal(t, expected, ConvertToWei(actual, 18))
}

0 comments on commit 0aa91c8

Please sign in to comment.