cosmos-sdk/x/sendtx/handler.go

42 lines
884 B
Go
Raw Normal View History

2018-01-03 17:20:21 -08:00
package sendtx
import (
"github.com/cosmos/cosmos-sdk/types"
2018-01-06 14:22:21 -08:00
"github.com/cosmos/cosmos-sdk/x/store"
2018-01-03 17:20:21 -08:00
)
2018-01-06 14:22:21 -08:00
func TransferHandlerFn(newAccStore func(types.KVStore) store.AccountStore) types.Handler {
2018-01-03 17:20:21 -08:00
return func(ctx types.Context, ms types.MultiStore, tx types.Tx) types.Result {
accStore := newAccStore(ms.GetKVStore("main"))
2018-01-06 14:22:21 -08:00
cs := store.CoinStore{accStore}
2018-01-03 17:20:21 -08:00
sendTx, ok := tx.(SendTx)
if !ok {
panic("tx is not SendTx") // ?
}
// NOTE: totalIn == totalOut should already have been checked
for _, in := range sendTx.Inputs {
_, err := cs.SubtractCoins(in.Address, in.Coins)
if err != nil {
return types.Result{
Code: 1, // TODO
}
}
}
for _, out := range sendTx.Outputs {
_, err := cs.AddCoins(out.Address, out.Coins)
if err != nil {
return types.Result{
Code: 1, // TODO
}
}
}
return types.Result{} // TODO
}
}