-
Notifications
You must be signed in to change notification settings - Fork 226
/
send_to_chain.go
165 lines (144 loc) · 4.85 KB
/
send_to_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
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"
cronoskeeper "github.com/crypto-org-chain/cronos/x/cronos/keeper"
"github.com/crypto-org-chain/cronos/x/cronos/types"
)
var _ types.EvmLogHandler = SendToChainHandler{}
const (
SendToChainEventName = "__CronosSendToChain"
SendToChainResponseEventName = "__CronosSendToChainResponse"
)
var (
// SendToChainEvent represent the signature of
// `event __CronosSendToChain(address recipient, uint256 amount, uint256 bridge_fee)`
SendToChainEvent abi.Event
// SendToChainResponseEvent represent the signature of
// `event __CronosSendToChainResponse(uint256 id)`
SendToChainResponseEvent abi.Event
)
func init() {
addressType, _ := abi.NewType("address", "", nil)
uint256Type, _ := abi.NewType("uint256", "", nil)
SendToChainEvent = abi.NewEvent(
SendToChainEventName,
SendToChainEventName,
false,
abi.Arguments{abi.Argument{
Name: "sender",
Type: addressType,
Indexed: false,
}, abi.Argument{
Name: "recipient",
Type: addressType,
Indexed: false,
}, abi.Argument{
Name: "amount",
Type: uint256Type,
Indexed: false,
}, abi.Argument{
Name: "bridge_fee",
Type: uint256Type,
Indexed: false,
}, abi.Argument{
Name: "chain_id",
Type: uint256Type,
Indexed: false,
}},
)
SendToChainResponseEvent = abi.NewEvent(
SendToChainResponseEventName,
SendToChainResponseEventName,
false,
abi.Arguments{abi.Argument{
Name: "id",
Type: uint256Type,
Indexed: false,
}},
)
}
// SendToChainHandler handles `__CronosSendToChain` log
type SendToChainHandler struct {
gravitySrv gravitytypes.MsgServer
bankKeeper types.BankKeeper
cronosKeeper cronoskeeper.Keeper
}
func NewSendToChainHandler(gravitySrv gravitytypes.MsgServer, bankKeeper types.BankKeeper, cronosKeeper cronoskeeper.Keeper) *SendToChainHandler {
return &SendToChainHandler{
gravitySrv: gravitySrv,
bankKeeper: bankKeeper,
cronosKeeper: cronosKeeper,
}
}
func (h SendToChainHandler) EventID() common.Hash {
return SendToChainEvent.ID
}
// Handle `__CronosSendToChain` log only if gravity is activated.
func (h SendToChainHandler) Handle(
ctx sdk.Context,
contract common.Address,
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", SendToChainEventName)
}
unpacked, err := SendToChainEvent.Inputs.Unpack(data)
if err != nil {
// log and ignore
h.cronosKeeper.Logger(ctx).Info("log signature matches but failed to decode")
return nil
}
denom, found := h.cronosKeeper.GetDenomByContract(ctx, contract)
if !found {
return fmt.Errorf("contract %s is not connected to native token", contract)
}
if !types.IsValidGravityDenom(denom) && !types.IsValidCronosDenom(denom) {
return fmt.Errorf("the native token associated with the contract %s is neither a gravity voucher or a cronos token", contract)
}
contractCosmosAddr := sdk.AccAddress(contract.Bytes())
senderCosmosAddr := sdk.AccAddress(unpacked[0].(common.Address).Bytes())
ethRecipient := unpacked[1].(common.Address)
amount := sdk.NewIntFromBigInt(unpacked[2].(*big.Int))
bridgeFee := sdk.NewIntFromBigInt(unpacked[3].(*big.Int))
chainID := sdk.NewIntFromBigInt(unpacked[4].(*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 not 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, _ := SendToChainResponseEvent.Inputs.Pack(big.NewInt(int64(resp.Id)))
addLogToReceipt(contract, SendToChainResponseEvent.ID, logData)
return nil
}