diff --git a/wei.go b/wei.go index df00f41..4b0c347 100644 --- a/wei.go +++ b/wei.go @@ -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() } diff --git a/wei_test.go b/wei_test.go index 0b86617..c608693 100644 --- a/wei_test.go +++ b/wei_test.go @@ -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)) }