-
Notifications
You must be signed in to change notification settings - Fork 0
/
ibc_module.go
170 lines (152 loc) · 5.25 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
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
package intergamm
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types"
transfertypes "github.com/cosmos/ibc-go/v2/modules/apps/transfer/types"
channeltypes "github.com/cosmos/ibc-go/v2/modules/core/04-channel/types"
ibcexported "github.com/cosmos/ibc-go/v2/modules/core/exported"
"github.com/disperze/ibc-osmo/x/intergamm/types"
)
// OnChanOpenInit implements the IBCModule interface
func (am AppModule) OnChanOpenInit(
ctx sdk.Context,
order channeltypes.Order,
connectionHops []string,
portID string,
channelID string,
chanCap *capabilitytypes.Capability,
counterparty channeltypes.Counterparty,
version string,
) error {
return am.app.OnChanOpenInit(ctx, order, connectionHops, portID, channelID,
chanCap, counterparty, version)
}
// OnChanOpenTry implements the IBCModule interface
func (am AppModule) OnChanOpenTry(
ctx sdk.Context,
order channeltypes.Order,
connectionHops []string,
portID,
channelID string,
chanCap *capabilitytypes.Capability,
counterparty channeltypes.Counterparty,
version,
counterpartyVersion string,
) error {
return am.app.OnChanOpenTry(ctx, order, connectionHops, portID, channelID,
chanCap, counterparty, version, counterpartyVersion)
}
// OnChanOpenAck implements the IBCModule interface
func (am AppModule) OnChanOpenAck(
ctx sdk.Context,
portID,
channelID string,
counterpartyVersion string,
) error {
return am.app.OnChanOpenAck(ctx, portID, channelID, counterpartyVersion)
}
// OnChanOpenConfirm implements the IBCModule interface
func (am AppModule) OnChanOpenConfirm(
ctx sdk.Context,
portID,
channelID string,
) error {
return am.app.OnChanOpenConfirm(ctx, portID, channelID)
}
// OnChanCloseInit implements the IBCModule interface
func (am AppModule) OnChanCloseInit(
ctx sdk.Context,
portID,
channelID string,
) error {
return am.app.OnChanCloseInit(ctx, portID, channelID)
}
// OnChanCloseConfirm implements the IBCModule interface
func (am AppModule) OnChanCloseConfirm(
ctx sdk.Context,
portID,
channelID string,
) error {
return am.app.OnChanCloseConfirm(ctx, portID, channelID)
}
// OnRecvPacket implements the IBCModule interface
func (am AppModule) OnRecvPacket(
ctx sdk.Context,
packet channeltypes.Packet,
relayer sdk.AccAddress,
) ibcexported.Acknowledgement {
var modulePacketData types.IbcPacketData
// uses custom UnmarshalJSON to allow messages from another middleware module
if err := types.SafeUnmarshalJSON(types.ModuleCdc, packet.GetData(), &modulePacketData); err != nil {
return channeltypes.NewErrorAcknowledgement(sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "cannot unmarshal packet data: %s", err.Error()).Error())
}
if modulePacketData.Gamm == nil {
return am.app.OnRecvPacket(ctx, packet, relayer)
}
receiver := types.GetFundAddress(packet.GetDestPort(), packet.GetDestChannel())
newData := transfertypes.NewFungibleTokenPacketData(modulePacketData.Denom, modulePacketData.Amount, modulePacketData.Sender, receiver.String())
newPacket := packet
newPacket.Data = newData.GetBytes()
ics20Ack := am.app.OnRecvPacket(ctx, newPacket, relayer)
if !ics20Ack.Success() {
return ics20Ack
}
var ack channeltypes.Acknowledgement
switch packetData := modulePacketData.Gamm.(type) {
case *types.IbcPacketData_Swap:
packetAck, err := am.keeper.OnRecvSwapPacket(ctx, packet, receiver, newData.Amount, newData.Denom, *packetData.Swap)
if err != nil {
ack = channeltypes.NewErrorAcknowledgement(err.Error())
} else {
// Encode packet acknowledgment
packetAckBytes, err := types.ModuleCdc.MarshalJSON(&packetAck)
if err != nil {
return channeltypes.NewErrorAcknowledgement(sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error()).Error())
}
ack = channeltypes.NewResultAcknowledgement(sdk.MustSortJSON(packetAckBytes))
}
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeSwapPacket,
sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName),
sdk.NewAttribute(types.AttributeKeyAckSuccess, fmt.Sprintf("%t", err != nil)),
),
)
// this line is used by starport scaffolding # ibc/packet/module/recv
default:
errMsg := fmt.Sprintf("unrecognized %s packet type: %T", types.ModuleName, packet)
return channeltypes.NewErrorAcknowledgement(errMsg)
}
// NOTE: acknowledgement will be written synchronously during IBC handler execution.
return ack
}
// OnAcknowledgementPacket implements the IBCModule interface
func (am AppModule) OnAcknowledgementPacket(
ctx sdk.Context,
packet channeltypes.Packet,
acknowledgement []byte,
relayer sdk.AccAddress,
) error {
return am.app.OnAcknowledgementPacket(ctx, packet, acknowledgement, relayer)
}
// OnTimeoutPacket implements the IBCModule interface
func (am AppModule) OnTimeoutPacket(
ctx sdk.Context,
packet channeltypes.Packet,
relayer sdk.AccAddress,
) error {
return am.app.OnTimeoutPacket(ctx, packet, relayer)
}
// NegotiateAppVersion implements the IBCModule interface
func (am AppModule) NegotiateAppVersion(
ctx sdk.Context,
order channeltypes.Order,
connectionID string,
portID string,
counterparty channeltypes.Counterparty,
proposedVersion string,
) (version string, err error) {
return am.app.NegotiateAppVersion(ctx, order, connectionID, portID, counterparty, proposedVersion)
}