forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
56 lines (45 loc) · 1.48 KB
/
handler.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
package ibc
import (
sdk "github.com/cosmos/cosmos-sdk/types"
)
func NewHandler(ibcm Mapper, ck BankKeeper) sdk.Handler {
return func(ctx sdk.Context, msg sdk.Msg) sdk.Result {
switch msg := msg.(type) {
case MsgIBCTransfer:
return handleIBCTransferMsg(ctx, ibcm, ck, msg)
case MsgIBCReceive:
return handleIBCReceiveMsg(ctx, ibcm, ck, msg)
default:
errMsg := "Unrecognized IBC Msg type: " + msg.Type()
return sdk.ErrUnknownRequest(errMsg).Result()
}
}
}
// MsgIBCTransfer deducts coins from the account and creates an egress IBC packet.
func handleIBCTransferMsg(ctx sdk.Context, ibcm Mapper, ck BankKeeper, msg MsgIBCTransfer) sdk.Result {
packet := msg.IBCPacket
_, _, err := ck.SubtractCoins(ctx, packet.SrcAddr, packet.Coins)
if err != nil {
return err.Result()
}
err = ibcm.PostIBCPacket(ctx, packet)
if err != nil {
return err.Result()
}
return sdk.Result{}
}
// MsgIBCReceive adds coins to the destination address and creates an ingress IBC packet.
func handleIBCReceiveMsg(ctx sdk.Context, ibcm Mapper, ck BankKeeper, msg MsgIBCReceive) sdk.Result {
packet := msg.IBCPacket
seq := ibcm.GetIngressSequence(ctx, packet.SrcChain)
if msg.Sequence != seq {
return ErrInvalidSequence(ibcm.codespace).Result()
}
// XXX Check that packet.Coins is valid and positive (nonzero)
_, _, err := ck.AddCoins(ctx, packet.DestAddr, packet.Coins)
if err != nil {
return err.Result()
}
ibcm.SetIngressSequence(ctx, packet.SrcChain, seq+1)
return sdk.Result{}
}