2018-07-08 22:09:07 +00:00
|
|
|
package paychan
|
|
|
|
|
|
|
|
import (
|
2018-07-10 13:56:04 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2018-07-13 13:27:55 +00:00
|
|
|
"reflect"
|
2018-07-08 22:09:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewHandler returns a handler for "paychan" type messages.
|
2018-07-10 13:56:04 +00:00
|
|
|
// Called when adding routes to a newly created app.
|
2018-07-08 22:09:07 +00:00
|
|
|
func NewHandler(k Keeper) sdk.Handler {
|
|
|
|
return func(ctx sdk.Context, msg sdk.Msg) sdk.Result {
|
|
|
|
switch msg := msg.(type) {
|
2018-07-10 13:56:04 +00:00
|
|
|
case MsgCreate:
|
|
|
|
return handleMsgCreate(ctx, k, msg)
|
2018-08-24 23:18:41 +00:00
|
|
|
case MsgSubmitUpdate:
|
|
|
|
return handleMsgSubmitUpdate(ctx, k, msg)
|
2018-07-08 22:09:07 +00:00
|
|
|
default:
|
|
|
|
errMsg := "Unrecognized paychan Msg type: " + reflect.TypeOf(msg).Name()
|
|
|
|
return sdk.ErrUnknownRequest(errMsg).Result()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-24 23:18:41 +00:00
|
|
|
// Handle MsgCreate
|
2018-07-11 21:02:07 +00:00
|
|
|
// Leaves validation to the keeper methods.
|
2018-07-10 13:56:04 +00:00
|
|
|
func handleMsgCreate(ctx sdk.Context, k Keeper, msg MsgCreate) sdk.Result {
|
2018-08-24 23:18:41 +00:00
|
|
|
tags, err := k.CreateChannel(ctx, msg.Participants[0], msg.Participants[len(msg.Participants)-1], msg.Coins)
|
2018-07-10 13:56:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return err.Result()
|
|
|
|
}
|
|
|
|
// TODO any other information that should be returned in Result?
|
|
|
|
return sdk.Result{
|
2018-07-13 13:27:55 +00:00
|
|
|
Tags: tags,
|
2018-07-10 13:56:04 +00:00
|
|
|
}
|
2018-07-08 22:09:07 +00:00
|
|
|
}
|
|
|
|
|
2018-08-24 23:18:41 +00:00
|
|
|
// Handle MsgSubmitUpdate
|
2018-07-11 21:02:07 +00:00
|
|
|
// Leaves validation to the keeper methods.
|
2018-08-24 23:18:41 +00:00
|
|
|
func handleMsgSubmitUpdate(ctx sdk.Context, k Keeper, msg MsgSubmitUpdate) sdk.Result {
|
|
|
|
|
2018-08-27 21:58:58 +00:00
|
|
|
participants := k.getChannel(ctx, msg.Update.ChannelID).Participants
|
|
|
|
|
|
|
|
// if only sender signed
|
|
|
|
if msg.submitter == participants[0] {
|
|
|
|
tags, err := k.InitCloseChannelBySender()
|
|
|
|
// else if receiver signed
|
|
|
|
} else if msg.submitter == participants[len(participants)-1] {
|
|
|
|
tags, err := k.CloseChannelByReceiver()
|
|
|
|
}
|
2018-08-24 23:18:41 +00:00
|
|
|
|
2018-07-10 13:56:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return err.Result()
|
|
|
|
}
|
2018-08-24 23:18:41 +00:00
|
|
|
// These tags can be used by clients to subscribe to channel close attempts
|
2018-07-10 13:56:04 +00:00
|
|
|
return sdk.Result{
|
2018-07-13 13:27:55 +00:00
|
|
|
Tags: tags,
|
2018-07-10 13:56:04 +00:00
|
|
|
}
|
2018-07-08 22:09:07 +00:00
|
|
|
}
|