-
Notifications
You must be signed in to change notification settings - Fork 0
/
send_to_evm_chain.go
executable file
·181 lines (159 loc) · 5.5 KB
/
send_to_evm_chain.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
package evmhandler
import (
"fmt"
"math/big"
gravitytypes "github.com/peggyjv/gravity-bridge/module/v2/x/gravity/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
swakeeper "github.com/swag-eag/swa/v2/x/swa/keeper"
"github.com/swag-eag/swa/v2/x/swa/types"
)
var _ types.EvmLogHandler = SendToEvmChainHandler{}
const (
SendToEvmChainEventName = "__SwaSendToEvmChain"
SendToEvmChainResponseEventName = "__SwaSendToEvmChainResponse"
)
var (
// SendToEvmChainEvent represent the signature of
// `event __SwaSendToEvmChain(address indexed sender, address indexed recipient, uint256 indexed chain_id, uint256 amount, uint256 bridge_fee, bytes extraData)`
SendToEvmChainEvent abi.Event
// SendToEvmChainResponseEvent represent the signature of
// `event __SwaSendToChainResponse(uint256 id)`
SendToEvmChainResponseEvent abi.Event
)
func init() {
addressType, _ := abi.NewType("address", "", nil)
uint256Type, _ := abi.NewType("uint256", "", nil)
bytesType, _ := abi.NewType("bytes", "", nil)
SendToEvmChainEvent = abi.NewEvent(
SendToEvmChainEventName,
SendToEvmChainEventName,
false,
abi.Arguments{abi.Argument{
Name: "sender",
Type: addressType,
Indexed: true,
}, abi.Argument{
Name: "recipient",
Type: addressType,
Indexed: true,
}, abi.Argument{
Name: "chain_id",
Type: uint256Type,
Indexed: true,
}, abi.Argument{
Name: "amount",
Type: uint256Type,
Indexed: false,
}, abi.Argument{
Name: "bridge_fee",
Type: uint256Type,
Indexed: false,
}, abi.Argument{
Name: "extraData",
Type: bytesType,
Indexed: false,
}},
)
SendToEvmChainResponseEvent = abi.NewEvent(
SendToEvmChainResponseEventName,
SendToEvmChainResponseEventName,
false,
abi.Arguments{abi.Argument{
Name: "id",
Type: uint256Type,
Indexed: false,
}},
)
}
// SendToEvmChainHandler handles `__SwaSendToEvmChain` log
type SendToEvmChainHandler struct {
gravitySrv gravitytypes.MsgServer
bankKeeper types.BankKeeper
swaKeeper swakeeper.Keeper
}
func NewSendToEvmChainHandler(gravitySrv gravitytypes.MsgServer, bankKeeper types.BankKeeper, swaKeeper swakeeper.Keeper) *SendToEvmChainHandler {
return &SendToEvmChainHandler{
gravitySrv: gravitySrv,
bankKeeper: bankKeeper,
swaKeeper: swaKeeper,
}
}
func (h SendToEvmChainHandler) EventID() common.Hash {
return SendToEvmChainEvent.ID
}
// Handle `__SwaSendToChain` log only if gravity is activated.
func (h SendToEvmChainHandler) Handle(
ctx sdk.Context,
contract common.Address,
topics []common.Hash,
data []byte,
addLogToReceipt func(contractAddress common.Address, logSig common.Hash, logData []byte),
) error {
if h.gravitySrv == nil {
return fmt.Errorf("native action %s is not implemented", SendToEvmChainEventName)
}
if len(topics) != 4 {
// log and ignore
h.swaKeeper.Logger(ctx).Info("log signature matches but wrong number of indexed events")
for i, topic := range topics {
h.swaKeeper.Logger(ctx).Debug(fmt.Sprintf("topic index: %d value: %s", i, topic.TerminalString()))
}
return nil
}
unpacked, err := SendToEvmChainEvent.Inputs.Unpack(data)
if err != nil {
// log and ignore
h.swaKeeper.Logger(ctx).Error("log signature matches but failed to decode", "error", err)
return nil
}
denom, found := h.swaKeeper.GetDenomByContract(ctx, contract)
if !found {
return fmt.Errorf("contract %s is not connected to native token", contract)
}
if !types.IsValidGravityDenom(denom) && !types.IsValidSwaDenom(denom) {
return fmt.Errorf("the native token associated with the contract %s is neither a gravity voucher or a swa token", contract)
}
contractCosmosAddr := sdk.AccAddress(contract.Bytes())
// needs to crope the extra bytes in the topic and cast to a cosmos address
senderCosmosAddr := sdk.AccAddress(common.BytesToAddress(topics[1].Bytes()).Bytes())
ethRecipient := common.BytesToAddress(topics[2].Bytes())
chainID := sdk.NewIntFromBigInt(new(big.Int).SetBytes(topics[3].Bytes()))
amount := sdk.NewIntFromBigInt(unpacked[0].(*big.Int))
bridgeFee := sdk.NewIntFromBigInt(unpacked[1].(*big.Int))
if !chainID.Equal(sdk.NewInt(1)) && !chainID.Equal(sdk.NewInt(3)) &&
!chainID.Equal(sdk.NewInt(4)) && !chainID.Equal(sdk.NewInt(5)) {
return fmt.Errorf("only ethereum network is supported")
}
coins := sdk.NewCoins(sdk.NewCoin(denom, amount.Add(bridgeFee)))
if types.IsSourceCoin(denom) {
// it is a source token, we need to mint coins
if err = h.bankKeeper.MintCoins(ctx, types.ModuleName, coins); err != nil {
return err
}
// send the coin to the user
if err = h.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, senderCosmosAddr.Bytes(), coins); err != nil {
return err
}
} else {
// send coins from contract address to user address so that he will be able to cancel later on
if err = h.bankKeeper.SendCoins(ctx, contractCosmosAddr, senderCosmosAddr.Bytes(), coins); err != nil {
return err
}
}
// Initialize a gravity transfer
msg := gravitytypes.MsgSendToEthereum{
Sender: senderCosmosAddr.String(),
EthereumRecipient: ethRecipient.Hex(),
Amount: sdk.NewCoin(denom, amount),
BridgeFee: sdk.NewCoin(denom, bridgeFee),
}
resp, err := h.gravitySrv.SendToEthereum(sdk.WrapSDKContext(ctx), &msg)
if err != nil {
return err
}
logData, _ := SendToEvmChainResponseEvent.Inputs.Pack(big.NewInt(int64(resp.Id)))
addLogToReceipt(contract, SendToEvmChainResponseEvent.ID, logData)
return nil
}