forked from ten-protocol/go-ten
-
Notifications
You must be signed in to change notification settings - Fork 0
/
obscuro_ERC20_contract_ABI.go
71 lines (59 loc) · 1.75 KB
/
obscuro_ERC20_contract_ABI.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package erc20contractlib
import (
"fmt"
"math/big"
"strings"
gethlog "github.com/ethereum/go-ethereum/log"
"github.com/obscuronet/go-obscuro/go/common/log"
"github.com/ethereum/go-ethereum/accounts/abi"
gethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
)
func init() { //nolint:gochecknoinits
var err error
obscuroERC20ContractABIJSON, err = abi.JSON(strings.NewReader(ERC20ContractABI))
if err != nil {
panic(err)
}
}
var obscuroERC20ContractABIJSON = abi.ABI{}
const (
TransferFunction = "transfer"
BalanceOfFunction = "balanceOf"
AmountField = "amount"
ToField = "to"
)
func DecodeTransferTx(t *types.Transaction, logger gethlog.Logger) (bool, *gethcommon.Address, *big.Int) {
if len(t.Data()) < methodBytesLen {
return false, nil, nil
}
method, err := obscuroERC20ContractABIJSON.MethodById(t.Data()[:methodBytesLen])
if err != nil {
logger.Trace(fmt.Sprintf("Could not decode tx %s", t.Hash().Hex()), log.ErrKey, err)
return false, nil, nil
}
if method.Name != TransferFunction {
return false, nil, nil
}
args := map[string]interface{}{}
if err := method.Inputs.UnpackIntoMap(args, t.Data()[4:]); err != nil {
panic(err)
}
address := args[ToField].(gethcommon.Address)
amount := args[AmountField].(*big.Int)
return true, &address, amount
}
func CreateTransferTxData(address gethcommon.Address, amount *big.Int) []byte {
transferERC20data, err := obscuroERC20ContractABIJSON.Pack(TransferFunction, address, amount)
if err != nil {
panic(err)
}
return transferERC20data
}
func CreateBalanceOfData(address gethcommon.Address) []byte {
balanceData, err := obscuroERC20ContractABIJSON.Pack(BalanceOfFunction, address)
if err != nil {
panic(err)
}
return balanceData
}