Vdb 67 convert numbers to ray and wad (#105)

* Add method to convert values to ray or wad units

* Convert data to ray or wad for cat_file_chop_lump

* Use shared convert functions in price feed conversion

* Pull common ray/wad values into vars

* Fix after rebase with staging
This commit is contained in:
Elizabeth
2018-11-09 14:53:20 -06:00
committed by GitHub
parent 68464d375a
commit c1e10f09fb
10 changed files with 153 additions and 35 deletions
+31
View File
@@ -19,6 +19,15 @@ import (
"math/big"
)
var (
rayBase = big.NewFloat(1e27)
wadBase = big.NewFloat(1e18)
rayPrecision = 27
wadPrecision = 18
ray = "ray"
wad = "wad"
)
func BigIntToInt64(value *big.Int) int64 {
if value == nil {
return int64(0)
@@ -47,3 +56,25 @@ func GetDataBytesAtIndex(n int, logData []byte) []byte {
}
return []byte{}
}
func ConvertToRay(value string) string {
return convert(ray, value, rayPrecision)
}
func ConvertToWad(value string) string {
return convert(wad, value, wadPrecision)
}
func convert(conversion string, value string, precision int) string {
result := big.NewFloat(0.0)
bigFloat := big.NewFloat(0.0)
bigFloat.SetString(value)
switch conversion {
case ray:
result.Quo(bigFloat, rayBase)
case wad:
result.Quo(bigFloat, wadBase)
}
return result.Text('f', precision)
}
+16
View File
@@ -45,5 +45,21 @@ var _ = Describe("Shared utilities", func() {
Expect(expected[:]).To(Equal(actual))
})
It("converts values to rays", func() {
rayOne := shared.ConvertToRay("123456789012345678901234567890")
Expect(rayOne).To(Equal("123.456789012345680589533003513"))
rayTwo := shared.ConvertToRay("1234567890123456790123567890")
Expect(rayTwo).To(Equal("1.234567890123456912476740399"))
})
It("converts values to wads", func() {
wadOne := shared.ConvertToWad("12345678901234567890123")
Expect(wadOne).To(Equal("12345.678901234567092615"))
wadTwo := shared.ConvertToWad("1234567890123456789")
Expect(wadTwo).To(Equal("1.234567890123456690"))
})
})
})