github.com/ChainSafe/chainbridge-core@v1.4.2/chains/evm/calls/util.go (about) 1 package calls 2 3 import ( 4 "errors" 5 "math" 6 gomath "math" 7 "math/big" 8 "strings" 9 10 "github.com/ethereum/go-ethereum" 11 "github.com/ethereum/go-ethereum/common/hexutil" 12 "github.com/ethereum/go-ethereum/crypto" 13 ) 14 15 func GetSolidityFunctionSig(in []byte) [4]byte { 16 var res [4]byte 17 hash := crypto.Keccak256Hash(in) 18 copy(res[:], hash[:]) 19 return res 20 } 21 22 func SliceTo32Bytes(in []byte) [32]byte { 23 var res [32]byte 24 copy(res[:], in) 25 return res 26 } 27 28 // ToCallArg is the function that converts ethereum.CallMsg into more abstract map 29 // This is done for matter of making EVMClient more abstract since some go-ethereum forks uses different messages types 30 func ToCallArg(msg ethereum.CallMsg) map[string]interface{} { 31 arg := map[string]interface{}{ 32 "from": msg.From, 33 "to": msg.To, 34 } 35 if len(msg.Data) > 0 { 36 arg["data"] = hexutil.Bytes(msg.Data) 37 } 38 if msg.Value != nil { 39 arg["value"] = (*hexutil.Big)(msg.Value) 40 } 41 if msg.Gas != 0 { 42 arg["gas"] = hexutil.Uint64(msg.Gas) 43 } 44 if msg.GasPrice != nil { 45 arg["gasPrice"] = (*hexutil.Big)(msg.GasPrice) 46 } 47 return arg 48 } 49 50 // UserAmountToWei converts decimal user friendly representation of token amount to 'Wei' representation with provided amount of decimal places 51 // eg UserAmountToWei(1, 5) => 100000 52 func UserAmountToWei(amount string, decimal *big.Int) (*big.Int, error) { 53 amountFloat, ok := big.NewFloat(0).SetString(amount) 54 if !ok { 55 return nil, errors.New("wrong amount format") 56 } 57 ethValueFloat := new(big.Float).Mul(amountFloat, big.NewFloat(math.Pow10(int(decimal.Int64())))) 58 ethValueFloatString := strings.Split(ethValueFloat.Text('f', int(decimal.Int64())), ".") 59 60 i, ok := big.NewInt(0).SetString(ethValueFloatString[0], 10) 61 if !ok { 62 return nil, errors.New(ethValueFloat.Text('f', int(decimal.Int64()))) 63 } 64 65 return i, nil 66 } 67 68 func WeiAmountToUser(amount *big.Int, decimals *big.Int) (*big.Float, error) { 69 amountFloat, ok := big.NewFloat(0).SetString(amount.String()) 70 if !ok { 71 return nil, errors.New("wrong amount format") 72 } 73 return new(big.Float).Quo(amountFloat, big.NewFloat(gomath.Pow10(int(decimals.Int64())))), nil 74 }