Skip to content

Commit

Permalink
Merge pull request #5 from stasundr/wei_converter
Browse files Browse the repository at this point in the history
Fix convert to wei function. Now without lost precision
  • Loading branch information
yakud authored Apr 4, 2022
2 parents ed0b2c0 + 3a46fec commit ad81cdf
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
8 changes: 8 additions & 0 deletions decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@ func (d *Decimal) GetMantissa() uint8 {
}

func (d *Decimal) FromString(value string) bool {
if d == nil {
*d = *NewDecimalZero()
}

if value == "" {
d.value = uint256.NewInt(0)
return true
Expand Down Expand Up @@ -266,6 +270,10 @@ func (d *Decimal) Rescale(mantissa uint8) *Decimal {
}

func (d *Decimal) ToBig() *big.Int {
if d.value == nil {
d.value = uint256.NewInt(0)
}

return d.value.ToBig()
}

Expand Down
3 changes: 2 additions & 1 deletion wei.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ 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).Rescale(mantissa).ToBig()
return NewDecimal(dec).ToBig()
}
46 changes: 46 additions & 0 deletions wei_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package decimal

import (
"github.com/stretchr/testify/assert"
"math/big"
"testing"
)

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

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

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

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

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

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

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

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

0 comments on commit ad81cdf

Please sign in to comment.