-
Notifications
You must be signed in to change notification settings - Fork 38
/
erc20_contract_lib.go
113 lines (94 loc) · 2.92 KB
/
erc20_contract_lib.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package erc20contractlib
import (
"math/big"
"strings"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ten-protocol/go-ten/go/ethadapter"
gethcommon "github.com/ethereum/go-ethereum/common"
)
const methodBytesLen = 4
// ERC20ContractLib provides methods for handling erc20 contracts
type ERC20ContractLib interface {
// DecodeTx receives a *types.Transaction and converts it to an common.L1Transaction
// returns nil if the transaction is not convertible
DecodeTx(tx *types.Transaction) ethadapter.L1Transaction
// CreateDepositTx receives an common.L1Transaction and converts it to an eth transaction
CreateDepositTx(tx *ethadapter.L1DepositTx) types.TxData
}
// erc20ContractLibImpl takes a mgmtContractAddr and processes multiple erc20ContractAddrs
// Watches for contract executions that might be deposits towards the Management Contract
type erc20ContractLibImpl struct {
mgmtContractAddr *gethcommon.Address
erc20ContractAddrs []*gethcommon.Address
contractABI abi.ABI
}
func NewERC20ContractLib(mgmtContractAddr *gethcommon.Address, contractAddrs ...*gethcommon.Address) ERC20ContractLib {
contractABI, err := abi.JSON(strings.NewReader(ERC20ContractABI))
if err != nil {
panic(err)
}
return &erc20ContractLibImpl{
mgmtContractAddr: mgmtContractAddr,
erc20ContractAddrs: contractAddrs,
contractABI: contractABI,
}
}
func (c *erc20ContractLibImpl) CreateDepositTx(tx *ethadapter.L1DepositTx) types.TxData {
data, err := c.contractABI.Pack("transfer", &tx.To, tx.Amount)
if err != nil {
panic(err)
}
return &types.LegacyTx{
To: tx.TokenContract,
Data: data,
}
}
func (c *erc20ContractLibImpl) DecodeTx(tx *types.Transaction) ethadapter.L1Transaction {
if !c.isRelevant(tx) {
return nil
}
method, err := c.contractABI.MethodById(tx.Data()[:methodBytesLen])
if err != nil {
panic(err)
}
contractCallData := map[string]interface{}{}
if err := method.Inputs.UnpackIntoMap(contractCallData, tx.Data()[methodBytesLen:]); err != nil {
panic(err)
}
to, found := contractCallData[ToCallData]
if !found {
panic("to address not found for transfer")
}
// only process transfers made to the management contract
toAddr, ok := to.(gethcommon.Address)
if !ok || toAddr.Hex() != c.mgmtContractAddr.Hex() {
return nil
}
amount, found := contractCallData[AmountCallData]
if !found {
panic("amount not found for transfer")
}
signer := types.NewLondonSigner(tx.ChainId())
sender, err := signer.Sender(tx)
if err != nil {
panic(err)
}
return ðadapter.L1DepositTx{
Amount: amount.(*big.Int),
To: &toAddr,
TokenContract: tx.To(),
Sender: &sender,
}
}
func (c *erc20ContractLibImpl) isRelevant(tx *types.Transaction) bool {
if tx.To() == nil || len(tx.Data()) == 0 {
return false
}
for _, addr := range c.erc20ContractAddrs {
if tx.To().Hex() == addr.Hex() {
return true
}
}
return false
}