-
Notifications
You must be signed in to change notification settings - Fork 169
/
ibc_module.go
93 lines (82 loc) · 3.36 KB
/
ibc_module.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
package ics20
import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
ibctransfer "github.com/cosmos/ibc-go/v6/modules/apps/transfer"
ibctransfertypes "github.com/cosmos/ibc-go/v6/modules/apps/transfer/types"
channeltypes "github.com/cosmos/ibc-go/v6/modules/core/04-channel/types"
ibcporttypes "github.com/cosmos/ibc-go/v6/modules/core/05-port/types"
ibcexported "github.com/cosmos/ibc-go/v6/modules/core/exported"
ltypes "github.com/umee-network/umee/v4/x/leverage/types"
"github.com/umee-network/umee/v4/x/uibc"
"github.com/umee-network/umee/v4/x/uibc/ics20/keeper"
)
// IBCModule wraps ICS-20 IBC module to limit token transfer inflows.
type IBCModule struct {
// leverage keeper
lkeeper uibc.LeverageKeeper
// embed the ICS-20 transfer's AppModule: ibctransfer.IBCModule
ibcporttypes.IBCModule
keeper keeper.Keeper
}
func NewIBCModule(leverageKeeper uibc.LeverageKeeper, am ibctransfer.IBCModule, k keeper.Keeper) IBCModule {
return IBCModule{
lkeeper: leverageKeeper,
IBCModule: am,
keeper: k,
}
}
// OnRecvPacket delegates the OnRecvPacket call to the embedded ICS-20 transfer
// IBCModule and updates metadata if successful.
func (am IBCModule) OnRecvPacket(
ctx sdk.Context,
packet channeltypes.Packet,
relayer sdk.AccAddress,
) ibcexported.Acknowledgement {
var data ibctransfertypes.FungibleTokenPacketData
if err := ibctransfertypes.ModuleCdc.UnmarshalJSON(packet.GetData(), &data); err != nil {
ackErr := sdkerrors.ErrInvalidType.Wrap("cannot unmarshal ICS-20 transfer packet data")
return channeltypes.NewErrorAcknowledgement(ackErr)
}
// Allowing only registered token for ibc transfer
// TODO: re-enable inflow checks
// isSourceChain := ibctransfertypes.SenderChainIsSource(packet.GetSourcePort(), packet.GetSourceChannel(), data.Denom)
// ackErr := CheckIBCInflow(ctx, packet, am.lkeeper, data.Denom, isSourceChain)
// if ackErr != nil {
// return ackErr
// }
ack := am.IBCModule.OnRecvPacket(ctx, packet, relayer)
if ack.Success() {
var data ibctransfertypes.FungibleTokenPacketData
if err := ibctransfertypes.ModuleCdc.UnmarshalJSON(packet.GetData(), &data); err == nil {
// track metadata
am.keeper.PostOnRecvPacket(ctx, packet, data)
}
}
return ack
}
func CheckIBCInflow(ctx sdk.Context,
packet channeltypes.Packet,
lkeeper uibc.LeverageKeeper,
dataDenom string, isSourceChain bool,
) ibcexported.Acknowledgement {
// if chain is recevier and sender chain is source then we need create ibc_denom (ibc/hash(channel,denom)) to
// check ibc_denom is exists in leverage token registry
if isSourceChain {
// since SendPacket did not prefix the denomination, we must prefix denomination here
sourcePrefix := ibctransfertypes.GetDenomPrefix(packet.GetDestPort(), packet.GetDestChannel())
// NOTE: sourcePrefix contains the trailing "/"
prefixedDenom := sourcePrefix + dataDenom
// construct the denomination trace from the full raw denomination and get the ibc_denom
ibcDenom := ibctransfertypes.ParseDenomTrace(prefixedDenom).IBCDenom()
_, err := lkeeper.GetTokenSettings(ctx, ibcDenom)
if err != nil {
if ltypes.ErrNotRegisteredToken.Is(err) {
return channeltypes.NewErrorAcknowledgement(err)
}
// other leverage keeper error -> log the error and allow the inflow transfer.
ctx.Logger().Error("IBC inflows: can't load token registry", "err", err)
}
}
return nil
}