forked from cerc-io/ipld-eth-server
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:
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user