cosmos-sdk/x/coinstore/handler.go

41 lines
841 B
Go
Raw Normal View History

2018-01-07 12:49:58 -08:00
package coinstore
2018-01-03 17:20:21 -08:00
import (
"github.com/cosmos/cosmos-sdk/types"
)
2018-01-07 12:27:56 -08:00
func TransferHandlerFn(newAccStore func(types.KVStore) types.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-07 12:49:58 -08:00
cs := 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
}
}