-
Notifications
You must be signed in to change notification settings - Fork 0
/
gravity_hooks.go
executable file
·89 lines (77 loc) · 2.94 KB
/
gravity_hooks.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
package keeper
import (
"fmt"
"strconv"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/common"
gravitytypes "github.com/peggyjv/gravity-bridge/module/v2/x/gravity/types"
swatypes "github.com/swag-eag/swa/v2/x/swa/types"
)
// TODO Implements GravityHooks interface
func (k Keeper) AfterContractCallExecutedEvent(ctx sdk.Context, event gravitytypes.ContractCallExecutedEvent) {
if k.gravityKeeper == nil {
return
}
}
func (k Keeper) AfterERC20DeployedEvent(ctx sdk.Context, event gravitytypes.ERC20DeployedEvent) {
if k.gravityKeeper == nil {
return
}
}
func (k Keeper) AfterSignerSetExecutedEvent(ctx sdk.Context, event gravitytypes.SignerSetTxExecutedEvent) {
if k.gravityKeeper == nil {
return
}
}
func (k Keeper) AfterBatchExecutedEvent(ctx sdk.Context, event gravitytypes.BatchExecutedEvent) {
if k.gravityKeeper == nil {
return
}
}
func (k Keeper) AfterSendToCosmosEvent(ctx sdk.Context, event gravitytypes.SendToCosmosEvent) {
if k.gravityKeeper == nil {
return
}
cacheCtx, commit := ctx.CacheContext()
err := k.doAfterSendToCosmosEvent(cacheCtx, event)
if err == nil {
commit()
} else {
k.Logger(ctx).Error("AfterSendToCosmosEvent hook failed", "error", err)
}
}
func (k Keeper) doAfterSendToCosmosEvent(ctx sdk.Context, event gravitytypes.SendToCosmosEvent) error {
if k.gravityKeeper == nil {
return fmt.Errorf("gravity is not enable")
}
_, denom := k.gravityKeeper.ERC20ToDenomLookup(ctx, common.HexToAddress(event.TokenContract))
coin := sdk.NewCoin(denom, event.Amount)
// TODO: Remove after event is emitted at Gravity module https://github.com/swag-eag/gravity-bridge/pull/12
coins := sdk.Coins{sdk.NewCoin(denom, event.Amount)}
ctx.EventManager().EmitEvent(sdk.NewEvent(
swatypes.EventTypeEthereumSendToCosmosHandled,
sdk.NewAttribute(sdk.AttributeKeyModule, gravitytypes.ModuleName),
sdk.NewAttribute(swatypes.AttributeKeySender, event.GetEthereumSender()),
sdk.NewAttribute(swatypes.AttributeKeyReceiver, event.GetCosmosReceiver()),
sdk.NewAttribute(swatypes.AttributeKeyAmount, coins.String()),
sdk.NewAttribute(gravitytypes.AttributeKeyBridgeChainID, strconv.FormatUint(
k.gravityKeeper.GetParams(ctx).BridgeChainId, 10,
)),
sdk.NewAttribute(swatypes.AttributeKeyEthereumTokenContract, event.GetTokenContract()),
sdk.NewAttribute(gravitytypes.AttributeKeyNonce, strconv.FormatUint(event.GetEventNonce(), 10)),
sdk.NewAttribute(gravitytypes.AttributeKeyEthereumEventVoteRecordID,
string(gravitytypes.MakeEthereumEventVoteRecordKey(event.GetEventNonce(), event.Hash()))),
))
// Try to convert the newly minted native tokens to erc20 contract
cosmosAddr, err := sdk.AccAddressFromBech32(event.CosmosReceiver)
if err != nil {
return err
}
addr := common.BytesToAddress(cosmosAddr.Bytes())
enableAutoDeployment := k.GetParams(ctx).EnableAutoDeployment
err = k.ConvertCoinFromNativeToSWAC21(ctx, addr, coin, enableAutoDeployment)
if err != nil {
return err
}
return nil
}