2018-01-12 11:49:53 -08:00
|
|
|
package bank
|
2018-01-03 17:20:21 -08:00
|
|
|
|
|
|
|
import (
|
2018-01-17 16:59:40 -08:00
|
|
|
"reflect"
|
2018-01-26 06:22:56 -08:00
|
|
|
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2018-01-03 17:20:21 -08:00
|
|
|
)
|
|
|
|
|
2018-01-17 16:59:40 -08:00
|
|
|
// Handle all "bank" type messages.
|
2018-02-08 23:43:08 -08:00
|
|
|
func NewHandler(ck CoinKeeper) sdk.Handler {
|
2018-01-26 06:22:56 -08:00
|
|
|
return func(ctx sdk.Context, msg sdk.Msg) sdk.Result {
|
2018-01-17 16:59:40 -08:00
|
|
|
switch msg := msg.(type) {
|
|
|
|
case SendMsg:
|
2018-02-08 23:43:08 -08:00
|
|
|
return handleSendMsg(ctx, ck, msg)
|
2018-01-17 16:59:40 -08:00
|
|
|
case IssueMsg:
|
2018-02-08 23:43:08 -08:00
|
|
|
return handleIssueMsg(ctx, ck, msg)
|
2018-01-17 16:59:40 -08:00
|
|
|
default:
|
2018-01-26 06:22:56 -08:00
|
|
|
errMsg := "Unrecognized bank Msg type: " + reflect.TypeOf(msg).Name()
|
2018-01-26 04:19:33 -08:00
|
|
|
return sdk.ErrUnknownRequest(errMsg).Result()
|
2018-01-03 17:20:21 -08:00
|
|
|
}
|
2018-01-17 16:59:40 -08:00
|
|
|
}
|
|
|
|
}
|
2018-01-03 17:20:21 -08:00
|
|
|
|
2018-01-17 16:59:40 -08:00
|
|
|
// Handle SendMsg.
|
2018-02-08 23:43:08 -08:00
|
|
|
func handleSendMsg(ctx sdk.Context, ck CoinKeeper, msg SendMsg) sdk.Result {
|
2018-01-17 16:59:40 -08:00
|
|
|
// NOTE: totalIn == totalOut should already have been checked
|
2018-01-03 17:20:21 -08:00
|
|
|
|
2018-01-17 16:59:40 -08:00
|
|
|
for _, in := range msg.Inputs {
|
2018-02-08 23:43:08 -08:00
|
|
|
_, err := ck.SubtractCoins(ctx, in.Address, in.Coins)
|
2018-01-17 16:59:40 -08:00
|
|
|
if err != nil {
|
2018-01-26 06:22:56 -08:00
|
|
|
return err.Result()
|
2018-01-03 17:20:21 -08:00
|
|
|
}
|
2018-01-17 16:59:40 -08:00
|
|
|
}
|
2018-01-03 17:20:21 -08:00
|
|
|
|
2018-01-17 16:59:40 -08:00
|
|
|
for _, out := range msg.Outputs {
|
2018-02-08 23:43:08 -08:00
|
|
|
_, err := ck.AddCoins(ctx, out.Address, out.Coins)
|
2018-01-17 16:59:40 -08:00
|
|
|
if err != nil {
|
2018-01-26 06:22:56 -08:00
|
|
|
return err.Result()
|
2018-01-03 17:20:21 -08:00
|
|
|
}
|
|
|
|
}
|
2018-01-17 16:59:40 -08:00
|
|
|
|
|
|
|
return sdk.Result{} // TODO
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle IssueMsg.
|
2018-02-08 23:43:08 -08:00
|
|
|
func handleIssueMsg(ctx sdk.Context, ck CoinKeeper, msg IssueMsg) sdk.Result {
|
2018-01-17 16:59:40 -08:00
|
|
|
panic("not implemented yet")
|
2018-01-03 17:20:21 -08:00
|
|
|
}
|