0g-chain/tests/util/erc20.go
Robert Pirtle 3366b3b3e3
feat: e2e test eip712 signing & erc20 interactions (#1535)
* add helpers & tests for erc20 eth_call query & transfer

* make encoding config public

* add evm client & raw evm signer to account

* test eip712 signing and broadcast

* update for cosmos v46

* update kvtool

* temporarily disable ibc tests & skip shutdown

* disable all but eip712 test and massively simplify

* add EIP712 tx builder & setup basic MsgSend test

* reenable all tests

* add eip712 test that deposits erc20 into earn

* update kvtool to master branch
2023-04-06 10:51:13 -07:00

41 lines
1.1 KiB
Go

package util
import (
"math/big"
"github.com/ethereum/go-ethereum/common"
"golang.org/x/crypto/sha3"
)
// EvmContractMethodId encodes a method signature to the method id used in eth calldata.
func EvmContractMethodId(signature string) []byte {
transferFnSignature := []byte(signature)
hash := sha3.NewLegacyKeccak256()
hash.Write(transferFnSignature)
return hash.Sum(nil)[:4]
}
func BuildErc20TransferCallData(from common.Address, to common.Address, amount *big.Int) []byte {
methodId := EvmContractMethodId("transfer(address,uint256)")
paddedAddress := common.LeftPadBytes(to.Bytes(), 32)
paddedAmount := common.LeftPadBytes(amount.Bytes(), 32)
var data []byte
data = append(data, methodId...)
data = append(data, paddedAddress...)
data = append(data, paddedAmount...)
return data
}
func BuildErc20BalanceOfCallData(address common.Address) []byte {
methodId := EvmContractMethodId("balanceOf(address)")
paddedAddress := common.LeftPadBytes(address.Bytes(), 32)
var data []byte
data = append(data, methodId...)
data = append(data, paddedAddress...)
return data
}