-
Notifications
You must be signed in to change notification settings - Fork 0
/
erc20.go
40 lines (31 loc) · 1.07 KB
/
erc20.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
}