/
exposure_odds.go
129 lines (109 loc) · 3.2 KB
/
exposure_odds.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package keeper
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/sge-network/sge/x/orderbook/types"
)
// SetOrderBookOddsExposure sets a book odds exposure.
func (k Keeper) SetOrderBookOddsExposure(ctx sdk.Context, boe types.OrderBookOddsExposure) {
bookKey := types.GetOrderBookOddsExposureKey(boe.OrderBookUID, boe.OddsUID)
store := k.getOrderBookOddsExposureStore(ctx)
b := k.cdc.MustMarshal(&boe)
store.Set(bookKey, b)
}
// GetOrderBookOddsExposure returns a specific book odds exposure.
func (k Keeper) GetOrderBookOddsExposure(
ctx sdk.Context,
orderBookUID, oddsUID string,
) (val types.OrderBookOddsExposure, found bool) {
marketsStore := k.getOrderBookOddsExposureStore(ctx)
exposureKey := types.GetOrderBookOddsExposureKey(orderBookUID, oddsUID)
b := marketsStore.Get(exposureKey)
if b == nil {
return val, false
}
k.cdc.MustUnmarshal(b, &val)
return val, true
}
// GetOddsExposuresByOrderBook returns all exposures for an order book.
func (k Keeper) GetOddsExposuresByOrderBook(
ctx sdk.Context,
orderBookUID string,
) (list []types.OrderBookOddsExposure, err error) {
store := k.getOrderBookOddsExposureStore(ctx)
iterator := sdk.KVStorePrefixIterator(store, types.GetOrderBookOddsExposuresKey(orderBookUID))
defer func() {
err = iterator.Close()
}()
for ; iterator.Valid(); iterator.Next() {
var val types.OrderBookOddsExposure
k.cdc.MustUnmarshal(iterator.Value(), &val)
list = append(list, val)
}
return
}
// GetAllOrderBookExposures returns all order book exposures used during genesis dump.
func (k Keeper) GetAllOrderBookExposures(
ctx sdk.Context,
) (list []types.OrderBookOddsExposure, err error) {
store := k.getOrderBookOddsExposureStore(ctx)
iterator := sdk.KVStorePrefixIterator(store, []byte{})
defer func() {
err = iterator.Close()
}()
for ; iterator.Valid(); iterator.Next() {
var val types.OrderBookOddsExposure
k.cdc.MustUnmarshal(iterator.Value(), &val)
list = append(list, val)
}
return
}
// initParticipationExposures initialize the odds and participation exposures for the
// participation at index.
func (k Keeper) initParticipationExposures(
ctx sdk.Context,
orderBookUID string,
participationIndex uint64,
) error {
// Update book odds exposures and add participant exposures
boes, err := k.GetOddsExposuresByOrderBook(ctx, orderBookUID)
if err != nil {
return err
}
for _, boe := range boes {
boe.FulfillmentQueue = append(boe.FulfillmentQueue, participationIndex)
k.SetOrderBookOddsExposure(ctx, boe)
pe := types.NewParticipationExposure(
orderBookUID,
boe.OddsUID,
sdk.ZeroInt(),
sdk.ZeroInt(),
participationIndex,
1,
false,
)
k.SetParticipationExposure(ctx, pe)
}
return nil
}
func (k Keeper) removeNotWithdrawableFromFulfillmentQueue(
ctx sdk.Context,
bp types.OrderBookParticipation,
) error {
if !bp.IsLiquidityInCurrentRound() {
boes, err := k.GetOddsExposuresByOrderBook(ctx, bp.OrderBookUID)
if err != nil {
return err
}
for _, boe := range boes {
for i, pn := range boe.FulfillmentQueue {
if pn == bp.Index {
boe.FulfillmentQueue = append(
boe.FulfillmentQueue[:i],
boe.FulfillmentQueue[i+1:]...)
}
}
k.SetOrderBookOddsExposure(ctx, boe)
}
}
return nil
}