forked from SunTzuSpace/bsc-eth-swap
-
Notifications
You must be signed in to change notification settings - Fork 1
/
eth_executor.go
180 lines (151 loc) · 4.83 KB
/
eth_executor.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package executor
import (
"context"
"math/big"
"strings"
"time"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/accounts/abi"
ethcmm "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
agent "github.com/binance-chain/bsc-eth-swap/abi"
contractabi "github.com/binance-chain/bsc-eth-swap/abi"
"github.com/binance-chain/bsc-eth-swap/common"
"github.com/binance-chain/bsc-eth-swap/util"
)
type EthExecutor struct {
Chain string
Config *util.Config
SwapAgentAddr ethcmm.Address
ethSwapAgentInst *contractabi.ETHSwapAgent
SwapAgentAbi abi.ABI
Client *ethclient.Client
}
func toBlockNumArg(number *big.Int) string {
if number == nil {
return "latest"
}
pending := big.NewInt(-1)
if number.Cmp(pending) == 0 {
return "pending"
}
return hexutil.EncodeBig(number)
}
func NewEthExecutor(ethClient *ethclient.Client, swapAddr string, config *util.Config) *EthExecutor {
agentAbi, err := abi.JSON(strings.NewReader(agent.ETHSwapAgentABI))
if err != nil {
panic("marshal abi error")
}
ethSwapAgentInst, err := contractabi.NewETHSwapAgent(ethcmm.HexToAddress(swapAddr), ethClient)
if err != nil {
panic(err.Error())
}
return &EthExecutor{
Chain: common.ChainETH,
Config: config,
SwapAgentAddr: ethcmm.HexToAddress(swapAddr),
ethSwapAgentInst: ethSwapAgentInst,
SwapAgentAbi: agentAbi,
Client: ethClient,
}
}
func (e *EthExecutor) GetChainName() string {
return e.Chain
}
func (e *EthExecutor) GetBlockAndTxEvents(height int64) (*common.BlockAndEventLogs, error) {
ctxWithTimeout, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
header, err := e.Client.HeaderByNumber(ctxWithTimeout, big.NewInt(height))
if err != nil {
return nil, err
}
packageLogs, err := e.GetLogs(header, height)
if err != nil {
return nil, err
}
return &common.BlockAndEventLogs{
Height: height,
Chain: e.Chain,
BlockHash: header.Hash().String(),
ParentBlockHash: header.ParentHash.String(),
BlockTime: int64(header.Time),
Events: packageLogs,
}, nil
}
func (e *EthExecutor) GetLogs(header *types.Header, height int64) ([]interface{}, error) {
startEvs, err := e.GetSwapStartLogs(header, height)
if err != nil {
return nil, err
}
regiserEvs, err := e.GetSwapPairRegisterLogs(header, height)
if err != nil {
return nil, err
}
var res = make([]interface{}, 0, len(startEvs)+len(regiserEvs))
res = append(append(res, startEvs...), regiserEvs...)
return res, nil
}
func (e *EthExecutor) GetSwapPairRegisterLogs(header *types.Header, height int64) ([]interface{}, error) {
topics := [][]ethcmm.Hash{{SwapPairRegisterEventHash}}
blockHash := header.Hash()
ctxWithTimeout, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
logs, err := e.Client.FilterLogs(ctxWithTimeout, ethereum.FilterQuery{
BlockHash: &blockHash,
Topics: topics,
Addresses: []ethcmm.Address{e.SwapAgentAddr},
})
if err != nil {
return nil, err
}
eventModels := make([]interface{}, 0, len(logs))
for _, log := range logs {
event, err := ParseSwapPairRegisterEvent(&e.SwapAgentAbi, &log)
if err != nil {
util.Logger.Errorf("parse event log error, er=%s", err.Error())
continue
}
if event == nil {
continue
}
eventModel := event.ToSwapPairRegisterLog(&log)
eventModel.Chain = e.Chain
util.Logger.Debugf("Found register event, erc20 address: %d, name: %s, symbol: %s, decimals: %d",
eventModel.ERC20Addr, eventModel.Name, eventModel.Symbol, eventModel.Decimals)
eventModels = append(eventModels, eventModel)
}
return eventModels, nil
}
func (e *EthExecutor) GetSwapStartLogs(header *types.Header, height int64) ([]interface{}, error) {
topics := [][]ethcmm.Hash{{ETH2BSCSwapStartedEventHash}}
blockHash := header.Hash()
ctxWithTimeout, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
logs, err := e.Client.FilterLogs(ctxWithTimeout, ethereum.FilterQuery{
BlockHash: &blockHash,
Topics: topics,
Addresses: []ethcmm.Address{e.SwapAgentAddr},
})
if err != nil {
return nil, err
}
eventModels := make([]interface{}, 0, len(logs))
for _, log := range logs {
event, err := ParseETH2BSCSwapStartEvent(&e.SwapAgentAbi, &log)
if err != nil {
util.Logger.Errorf("parse event log error, er=%s", err.Error())
continue
}
if event == nil {
continue
}
eventModel := event.ToSwapStartTxLog(&log)
eventModel.Chain = e.Chain
util.Logger.Debugf("Found ETH2BSC swap, txHash: %s, token address: %s, amount: %s, fee amount: %s",
eventModel.TxHash, eventModel.TokenAddr, eventModel.Amount, eventModel.FeeAmount)
eventModels = append(eventModels, eventModel)
}
return eventModels, nil
}