-
Notifications
You must be signed in to change notification settings - Fork 55
/
ordersource.go
87 lines (75 loc) 路 2.24 KB
/
ordersource.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
package amm
import (
sdk "github.com/cosmos/cosmos-sdk/types"
)
var _ OrderSource = (*mergedOrderSource)(nil)
// OrderView is the interface which provides a view of orders.
type OrderView interface {
HighestBuyPrice() (price sdk.Dec, found bool)
LowestSellPrice() (price sdk.Dec, found bool)
BuyAmountOver(price sdk.Dec) sdk.Int // Includes the price
SellAmountUnder(price sdk.Dec) sdk.Int // Includes the price
}
// OrderSource is the interface which provides a view of orders and also
// provides a way to extract orders from it.
type OrderSource interface {
OrderView
BuyOrdersOver(price sdk.Dec) []Order // Includes the price
SellOrdersUnder(price sdk.Dec) []Order // Includes the price
}
// mergedOrderSource is a merged order source of multiple sources.
type mergedOrderSource struct {
sources []OrderSource
}
// MergeOrderSources returns a merged order source of multiple sources.
func MergeOrderSources(sources ...OrderSource) OrderSource {
return &mergedOrderSource{sources: sources}
}
func (os *mergedOrderSource) HighestBuyPrice() (price sdk.Dec, found bool) {
for _, source := range os.sources {
p, f := source.HighestBuyPrice()
if f && (price.IsNil() || p.GT(price)) {
price = p
found = true
}
}
return
}
func (os *mergedOrderSource) LowestSellPrice() (price sdk.Dec, found bool) {
for _, source := range os.sources {
p, f := source.LowestSellPrice()
if f && (price.IsNil() || p.LT(price)) {
price = p
found = true
}
}
return
}
func (os *mergedOrderSource) BuyAmountOver(price sdk.Dec) sdk.Int {
amt := sdk.ZeroInt()
for _, source := range os.sources {
amt = amt.Add(source.BuyAmountOver(price))
}
return amt
}
func (os *mergedOrderSource) SellAmountUnder(price sdk.Dec) sdk.Int {
amt := sdk.ZeroInt()
for _, source := range os.sources {
amt = amt.Add(source.SellAmountUnder(price))
}
return amt
}
func (os *mergedOrderSource) BuyOrdersOver(price sdk.Dec) []Order {
var orders []Order
for _, source := range os.sources {
orders = append(orders, source.BuyOrdersOver(price)...)
}
return orders
}
func (os *mergedOrderSource) SellOrdersUnder(price sdk.Dec) []Order {
var orders []Order
for _, source := range os.sources {
orders = append(orders, source.SellOrdersUnder(price)...)
}
return orders
}