-
Notifications
You must be signed in to change notification settings - Fork 31
/
keeper.go
executable file
·80 lines (65 loc) · 1.99 KB
/
keeper.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package price
import (
"time"
dbm "github.com/tendermint/tm-db"
"github.com/tendermint/dex-demo/types"
"github.com/tendermint/dex-demo/types/store"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/store/types"
)
type IteratorCB func(tick Tick) bool
type Keeper struct {
as store.ArchiveStore
cdc *codec.Codec
}
func NewKeeper(db dbm.DB, cdc *codec.Codec) Keeper {
return Keeper{
as: store.NewTable(db, EntityName),
cdc: cdc,
}
}
func (k Keeper) ReverseIteratorByMarket(mktID store.EntityID, cb IteratorCB) {
k.as.PrefixIterator(tickIterKey(mktID), func(_ []byte, v []byte) bool {
var tick Tick
k.cdc.MustUnmarshalBinaryBare(v, &tick)
return cb(tick)
})
}
func (k Keeper) ReverseIteratorByMarketFrom(mktID store.EntityID, from time.Time, cb IteratorCB) {
k.as.ReverseIterator(tickKey(mktID, 0), sdk.PrefixEndBytes(tickKey(mktID, 0)), func(_ []byte, v []byte) bool {
var tick Tick
k.cdc.MustUnmarshalBinaryBare(v, &tick)
return cb(tick)
})
}
func (k Keeper) IteratorByMarketAndInterval(mktID store.EntityID, from time.Time, to time.Time, cb IteratorCB) {
k.as.Iterator(tickKey(mktID, from.Unix()), sdk.PrefixEndBytes(tickKey(mktID, to.Unix())), func(_ []byte, v []byte) bool {
var tick Tick
k.cdc.MustUnmarshalBinaryBare(v, &tick)
return cb(tick)
})
}
func (k Keeper) OnFillEvent(event types.Fill) {
tick := Tick{
MarketID: event.MarketID,
Pair: event.Pair,
BlockNumber: event.BlockNumber,
BlockTime: event.BlockTime,
Price: event.Price,
}
storedB := k.cdc.MustMarshalBinaryBare(tick)
k.as.Set(tickKey(event.MarketID, tick.BlockTime), storedB)
}
func (k Keeper) OnEvent(event interface{}) error {
switch ev := event.(type) {
case types.Fill:
k.OnFillEvent(ev)
}
return nil
}
func tickKey(mktID store.EntityID, blockTime int64) []byte {
return store.PrefixKeyBytes(tickIterKey(mktID), store.Int64Subkey(blockTime))
}
func tickIterKey(mktID store.EntityID) []byte {
return store.PrefixKeyString("tick", mktID.Bytes())
}