-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler.go
45 lines (38 loc) · 1.21 KB
/
handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package utxo
import (
sdk "github.com/cosmos/cosmos-sdk/types"
)
// Return the next position for handler to store newly created UTXOs
// Secondary is true if NextPosition is meant to return secondary output positions for a single multioutput transaction
// If false, NextPosition will increment position to accomadate outputs for a new transaction
type NextPosition func(ctx sdk.Context, secondary bool) Position
// User-defined fee update function
type FeeUpdater func([]Output) sdk.Error
// Handler handles spends of arbitrary utxo implementation
func NewSpendHandler(um Mapper, nextPos NextPosition) sdk.Handler {
return func(ctx sdk.Context, msg sdk.Msg) sdk.Result {
spendMsg, ok := msg.(SpendMsg)
if !ok {
panic("Msg does not implement SpendMsg")
}
// Add outputs from store
for i, o := range spendMsg.Outputs() {
var next Position
if i == 0 {
next = nextPos(ctx, false)
} else {
next = nextPos(ctx, true)
}
utxo := NewUTXO(o.Owner, o.Amount, o.Denom, next)
um.ReceiveUTXO(ctx, utxo)
}
// Spend inputs from store
for _, i := range spendMsg.Inputs() {
err := um.SpendUTXO(ctx, i.Owner, i.Position)
if err != nil {
return err.Result()
}
}
return sdk.Result{}
}
}