-
Notifications
You must be signed in to change notification settings - Fork 0
/
evm_log_handler.go
222 lines (189 loc) · 6.73 KB
/
evm_log_handler.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package keeper
import (
"bytes"
"fmt"
"math/big"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
sdk "github.com/evoblockchain/evochain/libs/cosmos-sdk/types"
tmtypes "github.com/evoblockchain/evochain/libs/tendermint/types"
"github.com/evoblockchain/evochain/x/erc20/types"
evmtypes "github.com/evoblockchain/evochain/x/evm/types"
)
var (
_ evmtypes.EvmLogHandler = SendToIbcEventHandler{}
)
const (
SendToIbcEventName = "__EVOSendToIbc"
SendNative20ToIbcEventName = "__EVOSendNative20ToIbc"
SendToWasmEventName = "__EVOSendToWasm"
)
// SendToIbcEvent represent the signature of
// `event __EVOSendToIbc(string recipient, uint256 amount)`
var SendToIbcEvent abi.Event
// SendNative20ToIbcEvent represent the signature of
// `event __EVOSendNative20ToIbc(string recipient, uint256 amount, string portID, string channelID)`
var SendNative20ToIbcEvent abi.Event
func init() {
addressType, _ := abi.NewType("address", "", nil)
uint256Type, _ := abi.NewType("uint256", "", nil)
stringType, _ := abi.NewType("string", "", nil)
SendToIbcEvent = abi.NewEvent(
SendToIbcEventName,
SendToIbcEventName,
false,
abi.Arguments{abi.Argument{
Name: "sender",
Type: addressType,
Indexed: false,
}, abi.Argument{
Name: "recipient",
Type: stringType,
Indexed: false,
}, abi.Argument{
Name: "amount",
Type: uint256Type,
Indexed: false,
}},
)
SendNative20ToIbcEvent = abi.NewEvent(
SendNative20ToIbcEventName,
SendNative20ToIbcEventName,
false,
abi.Arguments{abi.Argument{
Name: "sender",
Type: addressType,
Indexed: false,
}, abi.Argument{
Name: "recipient",
Type: stringType,
Indexed: false,
}, abi.Argument{
Name: "amount",
Type: uint256Type,
Indexed: false,
}, abi.Argument{
Name: "portID",
Type: stringType,
Indexed: false,
}, abi.Argument{
Name: "channelID",
Type: stringType,
Indexed: false,
}},
)
}
type SendToIbcEventHandler struct {
Keeper
}
func NewSendToIbcEventHandler(k Keeper) *SendToIbcEventHandler {
return &SendToIbcEventHandler{k}
}
// EventID Return the id of the log signature it handles
func (h SendToIbcEventHandler) EventID() common.Hash {
return SendToIbcEvent.ID
}
// Handle Process the log
func (h SendToIbcEventHandler) Handle(ctx sdk.Context, contract common.Address, data []byte) error {
h.Logger(ctx).Info("trigger evm event", "event", SendToIbcEvent.Name, "contract", contract)
// first confirm that the contract address and denom are registered,
// to avoid unpacking any contract '__EVOSendToIbc' event, which consumes performance
denom, found := h.Keeper.GetDenomByContract(ctx, contract)
if !found {
return fmt.Errorf("contract %s is not connected to native token", contract)
}
if !types.IsValidIBCDenom(denom) {
return fmt.Errorf("the native token associated with the contract %s is not an ibc voucher", contract)
}
unpacked, err := SendToIbcEvent.Inputs.Unpack(data)
if err != nil {
// log and ignore
h.Keeper.Logger(ctx).Error("log signature matches but failed to decode", "error", err)
return nil
}
contractAddr := sdk.AccAddress(contract.Bytes())
sender := sdk.AccAddress(unpacked[0].(common.Address).Bytes())
recipient := unpacked[1].(string)
amount := sdk.NewIntFromBigInt(unpacked[2].(*big.Int))
amountDec := sdk.NewDecFromIntWithPrec(amount, sdk.Precision)
vouchers := sdk.NewCoins(sdk.NewCoin(denom, amountDec))
// 1. transfer IBC coin to user so that he will be the refunded address if transfer fails
if err = h.bankKeeper.SendCoins(ctx, contractAddr, sender, vouchers); err != nil {
return err
}
// 2. Initiate IBC transfer from sender account
if err = h.Keeper.IbcTransferVouchers(ctx, sender.String(), recipient, vouchers); err != nil {
return err
}
if !ctx.IsCheckTx() && !ctx.IsTraceTx() {
txHash := tmtypes.Tx(ctx.TxBytes()).Hash(ctx.BlockHeight())
ethTxHash := common.BytesToHash(txHash)
ibcEvents := eventStr(ctx.EventManager().Events())
h.Keeper.addSendToIbcInnerTx(ethTxHash.Hex(), contract.String(), sender.String(), recipient, vouchers.String(), ibcEvents)
}
return nil
}
type SendNative20ToIbcEventHandler struct {
Keeper
}
func NewSendNative20ToIbcEventHandler(k Keeper) *SendNative20ToIbcEventHandler {
return &SendNative20ToIbcEventHandler{k}
}
// EventID Return the id of the log signature it handles
func (h SendNative20ToIbcEventHandler) EventID() common.Hash {
return SendNative20ToIbcEvent.ID
}
// Handle Process the log
func (h SendNative20ToIbcEventHandler) Handle(ctx sdk.Context, contract common.Address, data []byte) error {
h.Logger(ctx).Info("trigger evm event", "event", SendNative20ToIbcEvent.Name, "contract", contract)
// first confirm that the contract address and denom are registered,
// to avoid unpacking any contract '__EVOSendNative20ToIbc' event, which consumes performance
denom, found := h.Keeper.GetDenomByContract(ctx, contract)
if !found {
return fmt.Errorf("contract %s is not connected to native token", contract)
}
if err := sdk.ValidateDenom(denom); err != nil {
return fmt.Errorf("the native token associated with the contract %s is not an valid token", contract)
}
unpacked, err := SendNative20ToIbcEvent.Inputs.Unpack(data)
if err != nil {
// log and ignore
h.Keeper.Logger(ctx).Error("log signature matches but failed to decode", "error", err)
return nil
}
sender := sdk.AccAddress(unpacked[0].(common.Address).Bytes())
recipient := unpacked[1].(string)
amount := sdk.NewIntFromBigInt(unpacked[2].(*big.Int))
portID := unpacked[3].(string)
channelID := unpacked[4].(string)
amountDec := sdk.NewDecFromIntWithPrec(amount, sdk.Precision)
native20s := sdk.NewCoins(sdk.NewCoin(denom, amountDec))
// 1. mint new tokens to user so that he will be the refunded address if transfer fails
if err = h.supplyKeeper.MintCoins(ctx, types.ModuleName, native20s); err != nil {
return err
}
if err = h.supplyKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, sender, native20s); err != nil {
return err
}
// 2. Initiate IBC transfer from sender account
if err = h.Keeper.IbcTransferNative20(ctx, sender.String(), recipient, native20s, portID, channelID); err != nil {
return err
}
if !ctx.IsCheckTx() && !ctx.IsTraceTx() {
txHash := tmtypes.Tx(ctx.TxBytes()).Hash(ctx.BlockHeight())
ethTxHash := common.BytesToHash(txHash)
ibcEvents := eventStr(ctx.EventManager().Events())
h.Keeper.addSendNative20ToIbcInnerTx(ethTxHash.Hex(), types.ModuleName, sender.String(), recipient, native20s.String(), ibcEvents)
}
return nil
}
func eventStr(events sdk.Events) string {
if len(events) == 0 {
return ""
}
var buf bytes.Buffer
buf.WriteString(`{"events":`)
sdk.StringifyEvents(events).MarshalJsonToBuffer(&buf)
buf.WriteString("}")
return buf.String()
}