-
Notifications
You must be signed in to change notification settings - Fork 84
/
market.go
101 lines (89 loc) · 3.05 KB
/
market.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
// This code is available on the terms of the project LICENSE.md file,
// also available online at https://blueoakcouncil.org/license/1.0.0.
package dex
import (
"fmt"
"math"
"strings"
)
// MarketInfo specifies a market that the Archiver must support.
type MarketInfo struct {
Name string
Base uint32
Quote uint32
LotSize uint64
RateStep uint64
EpochDuration uint64 // msec
MarketBuyBuffer float64
MaxUserCancelsPerEpoch uint32
BookedLotLimit uint32
}
func marketName(base, quote string) string {
return base + "_" + quote
}
// MarketName creates the string representation of a DEX market (e.g. "dcr_btc")
// given the base and quote asset indexes defined in BIP-0044. See also
// BipIDSymbol.
func MarketName(base, quote uint32) (string, error) {
baseSymbol := BipIDSymbol(base)
if baseSymbol == "" {
return "", fmt.Errorf("base asset %d not found", base)
}
baseSymbol = strings.ToLower(baseSymbol)
quoteSymbol := BipIDSymbol(quote)
if quoteSymbol == "" {
return "", fmt.Errorf("quote asset %d not found", quote)
}
quoteSymbol = strings.ToLower(quoteSymbol)
return marketName(baseSymbol, quoteSymbol), nil
}
// NewMarketInfo creates a new market configuration (MarketInfo) from the given
// base and quote asset indexes, order lot size, and epoch duration in
// milliseconds. See also MarketName.
func NewMarketInfo(base, quote uint32, lotSize, rateStep, epochDuration uint64, marketBuyBuffer float64) (*MarketInfo, error) {
name, err := MarketName(base, quote)
if err != nil {
return nil, err
}
return &MarketInfo{
Name: name,
Base: base,
Quote: quote,
LotSize: lotSize,
RateStep: rateStep,
EpochDuration: epochDuration,
MarketBuyBuffer: marketBuyBuffer,
MaxUserCancelsPerEpoch: math.MaxUint32,
BookedLotLimit: math.MaxUint32,
}, nil
}
// NewMarketInfoFromSymbols is like NewMarketInfo, but the base and quote assets
// are identified by their symbols as defined in the
// decred.org/dcrdex/server/asset package.
func NewMarketInfoFromSymbols(base, quote string, lotSize, rateStep, epochDuration uint64, marketBuyBuffer float64) (*MarketInfo, error) {
base = strings.ToLower(base)
baseID, found := BipSymbolID(base)
if !found {
return nil, fmt.Errorf(`base asset symbol "%s" unrecognized`, base)
}
quote = strings.ToLower(quote)
quoteID, found := BipSymbolID(quote)
if !found {
return nil, fmt.Errorf(`quote asset symbol "%s" unrecognized`, quote)
}
return &MarketInfo{
Name: marketName(base, quote),
Base: baseID,
Quote: quoteID,
LotSize: lotSize,
RateStep: rateStep,
EpochDuration: epochDuration,
MarketBuyBuffer: marketBuyBuffer,
MaxUserCancelsPerEpoch: math.MaxUint32,
BookedLotLimit: math.MaxUint32,
}, nil
}
// String returns the market's Name.
func (mi *MarketInfo) String() string {
return mi.Name
}