-
Notifications
You must be signed in to change notification settings - Fork 0
/
orderbook.go
116 lines (96 loc) · 3.62 KB
/
orderbook.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
package keeper
import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/furychain/fury/x/strategicreserve/types"
)
// SetOrderBook sets an order book.
func (k Keeper) SetOrderBook(ctx sdk.Context, book types.OrderBook) {
bookKey := types.GetOrderBookKey(book.UID)
store := k.getOrderBookStore(ctx)
b := k.cdc.MustMarshal(&book)
store.Set(bookKey, b)
}
// GetOrderBook returns a specific order book by its uid.
func (k Keeper) GetOrderBook(ctx sdk.Context, orderBookUID string) (val types.OrderBook, found bool) {
marketsStore := k.getOrderBookStore(ctx)
bookKey := types.GetOrderBookKey(orderBookUID)
b := marketsStore.Get(bookKey)
if b == nil {
return val, false
}
k.cdc.MustUnmarshal(b, &val)
return val, true
}
// GetAllOrderBooks returns all order books used during genesis dump.
func (k Keeper) GetAllOrderBooks(ctx sdk.Context) (list []types.OrderBook, err error) {
store := k.getOrderBookStore(ctx)
iterator := sdk.KVStorePrefixIterator(store, []byte{})
defer func() {
err = iterator.Close()
}()
for ; iterator.Valid(); iterator.Next() {
var val types.OrderBook
k.cdc.MustUnmarshal(iterator.Value(), &val)
list = append(list, val)
}
return
}
// InitiateOrderBook initiates an order book for a given market.
func (k Keeper) InitiateOrderBook(ctx sdk.Context, marketUID string, srContribution sdk.Int, oddsUIDs []string) (err error) {
// book and market have one-to-one relationship
orderBookUID := marketUID
// check for existing orderBook with uid
orderBook, found := k.GetOrderBook(ctx, orderBookUID)
if found {
return sdkerrors.Wrapf(types.ErrOrderBookAlreadyPresent, "%s", orderBook.UID)
}
// create new active book object
orderBook = types.NewOrderBook(
orderBookUID,
2, // sr participation is made in two tranches to solve the reordering issue
uint64(len(oddsUIDs)),
types.OrderBookStatus_ORDER_BOOK_STATUS_STATUS_ACTIVE,
)
// Transfer sr contribution from sr to `sr_book_liquidity_pool` Account
err = k.transferFundsFromModuleToModule(ctx, types.SRPoolName, types.OrderBookLiquidityName, srContribution)
if err != nil {
return
}
// Add book participations
tranch1SRContribution := srContribution.Quo(sdk.NewInt(2))
tranch2SRContribution := srContribution.Sub(tranch1SRContribution)
fulfillmentQueue := []uint64{}
var i uint
for i = 0; i <= 1; i++ {
var tranchSRContribution sdk.Int
if i == 0 {
tranchSRContribution = tranch1SRContribution
} else {
tranchSRContribution = tranch2SRContribution
}
srParticipation := types.NewOrderBookParticipation(
types.SrparticipationIndex+uint64(i), orderBook.UID, k.accountKeeper.GetModuleAddress(types.SRPoolName).String(), orderBook.OddsCount, true, tranchSRContribution, tranchSRContribution,
sdk.ZeroInt(), sdk.ZeroInt(), sdk.ZeroInt(), sdk.Int{}, "", sdk.ZeroInt(),
)
_, found = k.GetOrderBookParticipation(ctx, orderBook.UID, srParticipation.Index)
if found {
err = sdkerrors.Wrapf(types.ErrOrderBookAlreadyPresent, "%d", srParticipation.Index)
return
}
k.SetOrderBookParticipation(ctx, srParticipation)
fulfillmentQueue = append(fulfillmentQueue, srParticipation.Index)
// Add participation exposures
for _, oddsUID := range oddsUIDs {
pe := types.NewParticipationExposure(srParticipation.OrderBookUID, oddsUID, sdk.ZeroInt(), sdk.ZeroInt(), srParticipation.Index, types.RoundStart, false)
k.SetParticipationExposure(ctx, pe)
}
}
// Add book exposures
for _, oddsUID := range oddsUIDs {
boe := types.NewOrderBookOddsExposure(orderBook.UID, oddsUID, fulfillmentQueue)
k.SetOrderBookOddsExposure(ctx, boe)
}
k.SetOrderBook(ctx, orderBook)
return nil
}