2019-01-24 20:41:30 +00:00
|
|
|
// VulcanizeDB
|
|
|
|
// Copyright © 2018 Vulcanize
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2018-08-16 21:36:35 +00:00
|
|
|
|
2018-09-11 19:01:45 +00:00
|
|
|
package shared
|
2018-08-16 21:36:35 +00:00
|
|
|
|
2018-11-08 15:14:35 +00:00
|
|
|
import (
|
2019-01-28 22:52:47 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2018-11-08 15:14:35 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/transformers/shared/constants"
|
|
|
|
"math/big"
|
|
|
|
)
|
2018-08-16 21:36:35 +00:00
|
|
|
|
2018-11-09 20:53:20 +00:00
|
|
|
var (
|
|
|
|
rayBase = big.NewFloat(1e27)
|
|
|
|
wadBase = big.NewFloat(1e18)
|
|
|
|
rayPrecision = 27
|
|
|
|
wadPrecision = 18
|
|
|
|
ray = "ray"
|
|
|
|
wad = "wad"
|
|
|
|
)
|
|
|
|
|
2018-09-26 14:42:52 +00:00
|
|
|
func BigIntToInt64(value *big.Int) int64 {
|
2018-08-16 21:36:35 +00:00
|
|
|
if value == nil {
|
|
|
|
return int64(0)
|
|
|
|
} else {
|
|
|
|
return value.Int64()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-26 14:42:52 +00:00
|
|
|
func BigIntToString(value *big.Int) string {
|
|
|
|
result := value.String()
|
|
|
|
if result == "<nil>" {
|
2018-08-16 21:36:35 +00:00
|
|
|
return ""
|
|
|
|
} else {
|
2018-09-26 14:42:52 +00:00
|
|
|
return result
|
2018-08-16 21:36:35 +00:00
|
|
|
}
|
|
|
|
}
|
2018-10-04 21:49:09 +00:00
|
|
|
|
|
|
|
func GetDataBytesAtIndex(n int, logData []byte) []byte {
|
|
|
|
switch {
|
|
|
|
case n == -1:
|
2018-11-08 15:14:35 +00:00
|
|
|
return logData[len(logData)-constants.DataItemLength:]
|
2018-10-04 21:49:09 +00:00
|
|
|
case n == -2:
|
2018-11-08 15:14:35 +00:00
|
|
|
return logData[len(logData)-(2*constants.DataItemLength) : len(logData)-constants.DataItemLength]
|
2018-10-04 21:49:09 +00:00
|
|
|
case n == -3:
|
2018-11-08 15:14:35 +00:00
|
|
|
return logData[len(logData)-(3*constants.DataItemLength) : len(logData)-(2*constants.DataItemLength)]
|
2018-10-04 21:49:09 +00:00
|
|
|
}
|
|
|
|
return []byte{}
|
|
|
|
}
|
2018-11-09 20:53:20 +00:00
|
|
|
|
2019-01-28 22:52:47 +00:00
|
|
|
func GetHexWithoutPrefix(raw []byte) string {
|
|
|
|
return common.Bytes2Hex(raw)
|
|
|
|
}
|
|
|
|
|
2018-11-09 20:53:20 +00:00
|
|
|
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)
|
|
|
|
}
|
2019-01-23 18:44:09 +00:00
|
|
|
|
|
|
|
func MinInt64(ints []int64) (min int64) {
|
|
|
|
if len(ints) == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
min = ints[0]
|
|
|
|
for _, i := range ints {
|
|
|
|
if i < min {
|
|
|
|
min = i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|