2018-07-08 22:09:07 +00:00
|
|
|
package paychan
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
2018-07-10 13:56:04 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
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)
|
|
|
|
case MsgClose:
|
|
|
|
return handleMsgClose(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-07-10 13:56:04 +00:00
|
|
|
// TODO does validation go here or in the keeper?
|
|
|
|
|
2018-07-08 22:09:07 +00:00
|
|
|
// Handle CreateMsg.
|
2018-07-10 13:56:04 +00:00
|
|
|
func handleMsgCreate(ctx sdk.Context, k Keeper, msg MsgCreate) sdk.Result {
|
|
|
|
// TODO maybe remove tags for first version
|
|
|
|
tags, err := k.CreatePaychan(msg.sender, msg.receiver, msg.amount)
|
|
|
|
if err != nil {
|
|
|
|
return err.Result()
|
|
|
|
}
|
|
|
|
// TODO any other information that should be returned in Result?
|
|
|
|
return sdk.Result{
|
|
|
|
Tags: tags
|
|
|
|
}
|
2018-07-08 22:09:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handle CloseMsg.
|
2018-07-10 13:56:04 +00:00
|
|
|
func handleMsgClose(ctx sdk.Context, k Keeper, msg MsgClose) sdk.Result {
|
|
|
|
// TODO maybe remove tags for first version
|
|
|
|
tags, err := k.ClosePaychan(msg.sender, msg.receiver, msg.id, msg.receiverAmount)
|
|
|
|
if err != nil {
|
|
|
|
return err.Result()
|
|
|
|
}
|
|
|
|
// These tags can be used to subscribe to channel closures
|
|
|
|
return sdk.Result{
|
|
|
|
Tags: tags
|
|
|
|
}
|
2018-07-08 22:09:07 +00:00
|
|
|
}
|