-
Notifications
You must be signed in to change notification settings - Fork 0
/
cancel_send_to_evm_chain.go
executable file
·146 lines (125 loc) · 4.4 KB
/
cancel_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
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 = CancelSendToEvmChainHandler{}
const CancelSendToEvmChainEventName = "__SwaCancelSendToEvmChain"
// CancelSendToEvmChainEvent represent the signature of
// `event __SwaCancelSendToEvmChain(uint256 id)`
var CancelSendToEvmChainEvent abi.Event
func init() {
addressType, _ := abi.NewType("address", "", nil)
uint256Type, _ := abi.NewType("uint256", "", nil)
CancelSendToEvmChainEvent = abi.NewEvent(
CancelSendToEvmChainEventName,
CancelSendToEvmChainEventName,
false,
abi.Arguments{abi.Argument{
Name: "sender",
Type: addressType,
Indexed: true,
}, abi.Argument{
Name: "id",
Type: uint256Type,
Indexed: false,
}},
)
}
// CancelSendToEvmChainHandler handles `__SwaCancelSendToEvmChain` log
type CancelSendToEvmChainHandler struct {
gravitySrv gravitytypes.MsgServer
swaKeeper swakeeper.Keeper
gravityKeeper types.GravityKeeper
}
func NewCancelSendToEvmChainHandler(
gravitySrv gravitytypes.MsgServer,
swaKeeper swakeeper.Keeper,
gravityKeeper types.GravityKeeper,
) *CancelSendToEvmChainHandler {
return &CancelSendToEvmChainHandler{
gravitySrv: gravitySrv,
swaKeeper: swaKeeper,
gravityKeeper: gravityKeeper,
}
}
func (h CancelSendToEvmChainHandler) EventID() common.Hash {
return CancelSendToEvmChainEvent.ID
}
// Handle `__SwaCancelSendToChain` log only if gravity is activated.
func (h CancelSendToEvmChainHandler) Handle(
ctx sdk.Context,
contract common.Address,
topics []common.Hash,
data []byte,
_ func(contractAddress common.Address, logSig common.Hash, logData []byte),
) error {
if h.gravitySrv == nil {
return fmt.Errorf("native action %s is not implemented", CancelSendToEvmChainEventName)
}
if len(topics) != 2 {
// 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 := CancelSendToEvmChainEvent.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
}
// needs to crope the extra bytes in the topic and cast to a cosmos address
senderCosmosAddr := sdk.AccAddress(common.BytesToAddress(topics[1].Bytes()).Bytes())
id := sdk.NewIntFromBigInt(unpacked[0].(*big.Int))
// Need to retrieve the batch to get the amount to refund
var unbatched []*gravitytypes.SendToEthereum
h.gravityKeeper.IterateUnbatchedSendToEthereums(ctx, func(ste *gravitytypes.SendToEthereum) bool {
unbatched = append(unbatched, ste)
return false
})
var send *gravitytypes.SendToEthereum
for _, ste := range unbatched {
if ste.Id == id.Uint64() {
send = ste
}
}
if send == nil {
return fmt.Errorf("id not found or the transaction is already included in a batch")
}
_, denom := h.gravityKeeper.ERC20ToDenomLookup(ctx, common.HexToAddress(send.Erc20Token.Contract))
if !types.IsValidGravityDenom(denom) {
return fmt.Errorf("the native token associated with the contract %s is not a gravity voucher", send.Erc20Token.Contract)
}
// check that the event is emitted from the contract address that manage this token
swac20Address, found := h.swaKeeper.GetContractByDenom(ctx, denom)
if !found {
return fmt.Errorf("the native token %s is not associated with any contract address on swa", denom)
}
if swac20Address != contract {
return fmt.Errorf("cannot cancel a transfer of the native token %s from the contract address %s", denom, contract)
}
msg := gravitytypes.MsgCancelSendToEthereum{
Sender: senderCosmosAddr.String(),
Id: id.Uint64(),
}
_, err = h.gravitySrv.CancelSendToEthereum(sdk.WrapSDKContext(ctx), &msg)
if err != nil {
return err
}
refundAmount := sdk.NewCoins(sdk.NewCoin(denom, send.Erc20Token.Amount.Add(send.Erc20Fee.Amount)))
// If cancel has no error, we need to convert back the native token to evm tokens
err = h.swaKeeper.ConvertVouchersToEvmCoins(ctx, senderCosmosAddr.String(), refundAmount)
if err != nil {
return err
}
return nil
}