-
Notifications
You must be signed in to change notification settings - Fork 14
/
market.go
509 lines (444 loc) · 12.8 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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
package domain
import (
"encoding/hex"
"fmt"
"math"
"github.com/btcsuite/btcd/btcutil"
"github.com/shopspring/decimal"
"github.com/tdex-network/tdex-daemon/pkg/marketmaking"
"github.com/tdex-network/tdex-daemon/pkg/marketmaking/formula"
"github.com/tdex-network/tdex-daemon/pkg/mathutil"
)
type MarketFee struct {
BaseAsset uint64
QuoteAsset uint64
}
// MarketPrice represents base and quote market price
type MarketPrice struct {
// how much 1 base asset is valued in quote asset.
BasePrice string
// how much 1 quote asset is valued in base asset
QuotePrice string
}
func (mp MarketPrice) IsZero() bool {
empty := MarketPrice{}
if mp == empty {
return true
}
return mp.GetBasePrice().IsZero() && mp.GetQuotePrice().IsZero()
}
func (mp MarketPrice) GetBasePrice() decimal.Decimal {
p, _ := decimal.NewFromString(mp.BasePrice)
return p
}
func (mp MarketPrice) GetQuotePrice() decimal.Decimal {
p, _ := decimal.NewFromString(mp.QuotePrice)
return p
}
// PreviewInfo contains info about a price preview based on the market's current
// strategy.
type PreviewInfo struct {
Price MarketPrice
Amount uint64
Asset string
FeeAsset string
FeeAmount uint64
}
// Market defines the Market entity data structure for holding an asset pair state.
type Market struct {
// Base asset in hex format.
BaseAsset string
// Quote asset in hex format.
QuoteAsset string
// Name of the market.
Name string
// Precison of the base asset.
BaseAssetPrecision uint
// Precison of the quote asset.
QuoteAssetPrecision uint
// Percentage fee expressed in basis points for both assets.
PercentageFee MarketFee
// Fixed fee amount expressed in satoshis for both assets.
FixedFee MarketFee
// if curretly open for trades
Tradable bool
// Market Making strategy type
StrategyType int
// Pluggable Price of the asset pair.
Price MarketPrice
}
// NewMarket returns a new market with an account index, the asset pair and the
// percentage fee set.
func NewMarket(
baseAsset, quoteAsset, name string,
basePercentageFee, quotePercentageFee, baseFixedFee, quoteFixedFee uint64,
baseAssetPrecision, quoteAssetPrecision, strategyType uint,
) (*Market, error) {
if !isValidAsset(baseAsset) {
return nil, ErrMarketInvalidBaseAsset
}
if !isValidAsset(quoteAsset) {
return nil, ErrMarketInvalidQuoteAsset
}
if !isValidPercentageFee(
int64(basePercentageFee), int64(quotePercentageFee),
) {
return nil, ErrMarketInvalidPercentageFee
}
if !isValidFixedFee(
int64(baseFixedFee), int64(quoteFixedFee),
) {
return nil, ErrMarketInvalidFixedFee
}
if !isValidPrecision(baseAssetPrecision) {
return nil, ErrMarketInvalidBaseAssetPrecision
}
if !isValidPrecision(quoteAssetPrecision) {
return nil, ErrMarketInvalidQuoteAssetPrecision
}
if !isValidStrategy(strategyType) {
return nil, ErrMarketUnknownStrategy
}
if strategyType == StrategyTypeUndefined {
strategyType = StrategyTypeBalanced
}
if name == "" {
name = makeAccountName(baseAsset, quoteAsset)
}
return &Market{
BaseAsset: baseAsset,
QuoteAsset: quoteAsset,
Name: name,
StrategyType: int(strategyType),
BaseAssetPrecision: baseAssetPrecision,
QuoteAssetPrecision: quoteAssetPrecision,
PercentageFee: MarketFee{
BaseAsset: basePercentageFee,
QuoteAsset: quotePercentageFee,
},
FixedFee: MarketFee{
BaseAsset: baseFixedFee,
QuoteAsset: quoteFixedFee,
},
}, nil
}
// IsTradable returns true if the market is available for trading
func (m *Market) IsTradable() bool {
return m.Tradable
}
func (m *Market) IsStrategyBalanced() bool {
return m.StrategyType == StrategyTypeBalanced
}
// IsStrategyPluggable returns true if the strategy isn't automated.
func (m *Market) IsStrategyPluggable() bool {
return m.StrategyType == StrategyTypePluggable
}
// MakeTradable updates the status of the market to tradable.
func (m *Market) MakeTradable() error {
if m.IsStrategyPluggable() && m.Price.IsZero() {
return ErrMarketNotPriced
}
m.Tradable = true
return nil
}
// MakeNotTradable updates the status of the market to not tradable.
func (m *Market) MakeNotTradable() {
m.Tradable = false
}
// MakeStrategyPluggable makes the current market using a given price
// (ie. set via UpdateMarketPrice rpc either manually or a price feed plugin)
func (m *Market) MakeStrategyPluggable() error {
if m.IsTradable() {
// We need the market be switched off before making this change
return ErrMarketIsOpen
}
m.StrategyType = StrategyTypePluggable
m.Price = MarketPrice{}
return nil
}
// MakeStrategyBalanced makes the current market using a balanced AMM formula 50/50
func (m *Market) MakeStrategyBalanced() error {
if m.IsTradable() {
// We need the market be switched off before making this change
return ErrMarketIsOpen
}
m.StrategyType = StrategyTypeBalanced
return nil
}
// ChangePercentageFee updates market's perentage fee to the given one.
func (m *Market) ChangePercentageFee(baseFee, quoteFee int64) error {
if m.IsTradable() {
return ErrMarketIsOpen
}
if !isValidPercentageFee(baseFee, quoteFee) {
return ErrMarketInvalidPercentageFee
}
if baseFee >= 0 {
m.PercentageFee.BaseAsset = uint64(baseFee)
}
if quoteFee >= 0 {
m.PercentageFee.QuoteAsset = uint64(quoteFee)
}
return nil
}
// ChangeFixedFee updates market's fixed fee to those given.
func (m *Market) ChangeFixedFee(baseFee, quoteFee int64) error {
if m.IsTradable() {
return ErrMarketIsOpen
}
if !isValidFixedFee(baseFee, quoteFee) {
return ErrMarketInvalidFixedFee
}
if baseFee >= 0 {
m.FixedFee.BaseAsset = uint64(baseFee)
}
if quoteFee >= 0 {
m.FixedFee.QuoteAsset = uint64(quoteFee)
}
return nil
}
// ChangeBasePrice updates the price of market's base asset.
func (m *Market) ChangePrice(basePrice, quotePrice decimal.Decimal) error {
if basePrice.LessThanOrEqual(decimal.Zero) {
return ErrMarketInvalidBasePrice
}
if quotePrice.LessThanOrEqual(decimal.Zero) {
return ErrMarketInvalidQuotePrice
}
m.Price.BasePrice = basePrice.String()
m.Price.QuotePrice = quotePrice.String()
return nil
}
func (m *Market) ChangeAssetPrecision(
baseAssetPrecision, quoteAssetPrecision int,
) error {
if m.IsTradable() {
return ErrMarketIsOpen
}
if baseAssetPrecision >= 0 {
if !isValidPrecision(uint(baseAssetPrecision)) {
return ErrMarketInvalidBaseAssetPrecision
}
}
if quoteAssetPrecision >= 0 {
if !isValidPrecision(uint(quoteAssetPrecision)) {
return ErrMarketInvalidQuoteAssetPrecision
}
}
if baseAssetPrecision >= 0 {
m.BaseAssetPrecision = uint(baseAssetPrecision)
}
if quoteAssetPrecision >= 0 {
m.QuoteAssetPrecision = uint(quoteAssetPrecision)
}
return nil
}
func (m *Market) Preview(
baseBalance, quoteBalance, amount uint64,
asset, feeAsset string, isBuy bool,
) (*PreviewInfo, error) {
if !m.IsTradable() {
return nil, ErrMarketIsClosed
}
if asset != m.BaseAsset && asset != m.QuoteAsset {
return nil, fmt.Errorf("asset must be either base or quote asset")
}
if feeAsset != m.BaseAsset && feeAsset != m.QuoteAsset {
return nil, fmt.Errorf("fee asset must be either base or quote asset")
}
isBaseAsset := asset == m.BaseAsset
formula := m.formula(isBaseAsset, isBuy)
var args interface{}
if m.IsStrategyPluggable() {
args = m.formulaOptsForPluggable(baseBalance, quoteBalance, isBaseAsset, isBuy)
} else {
args = m.formulaOptsForBalanced(baseBalance, quoteBalance, isBaseAsset, isBuy)
}
price, err := m.priceForStrategy(baseBalance, quoteBalance)
if err != nil {
return nil, err
}
assetPrecision := math.Pow10(int(m.BaseAssetPrecision))
if !isBaseAsset {
assetPrecision = math.Pow10(int(m.QuoteAssetPrecision))
}
amountDecimal := mathutil.Div(amount, uint64(assetPrecision))
previewAmount, err := formula(args, amountDecimal)
if err != nil {
return nil, err
}
previewAsset := m.BaseAsset
previewAssetPrecision := math.Pow10(int(m.BaseAssetPrecision))
if isBaseAsset {
previewAsset = m.QuoteAsset
previewAssetPrecision = math.Pow10(int(m.QuoteAssetPrecision))
}
previewAmountInSats := previewAmount.Mul(
decimal.NewFromFloat(previewAssetPrecision),
).BigInt().Uint64()
if previewAmountInSats == 0 {
return nil, ErrMarketPreviewAmountTooLow
}
amountsByAsset := map[string]uint64{
asset: amount,
previewAsset: previewAmountInSats,
}
amountForFees := amountsByAsset[feeAsset]
previewFeeAmount, err := m.previewFees(amountForFees, feeAsset, isBuy)
if err != nil {
return nil, err
}
return &PreviewInfo{
Price: price,
Amount: previewAmountInSats,
Asset: previewAsset,
FeeAsset: feeAsset,
FeeAmount: previewFeeAmount,
}, nil
}
func (m *Market) SpotPrice(
baseBalance, quoteBalance uint64,
) (MarketPrice, error) {
return m.priceForStrategy(baseBalance, quoteBalance)
}
func (m *Market) strategy() marketmaking.MakingFormula {
switch m.StrategyType {
case StrategyTypePluggable:
return marketmaking.NewPluggableFormula()
case StrategyTypeBalanced:
fallthrough
default:
return marketmaking.NewBalancedReservedFormula()
}
}
func (m *Market) formula(
isBaseAsset, isBuy bool,
) func(interface{}, decimal.Decimal) (decimal.Decimal, error) {
strategy := m.strategy()
if isBuy {
if isBaseAsset {
return strategy.InGivenOut
}
return strategy.OutGivenIn
}
if isBaseAsset {
return strategy.OutGivenIn
}
return strategy.InGivenOut
}
func (m *Market) formulaOptsForPluggable(
baseBalance, quoteBalance uint64, isBaseAsset, isBuy bool,
) interface{} {
bp := uint64(math.Pow10(int(m.BaseAssetPrecision)))
qp := uint64(math.Pow10(int(m.QuoteAssetPrecision)))
balanceIn := mathutil.Div(baseBalance, bp)
balanceOut := mathutil.Div(quoteBalance, qp)
if isBuy {
balanceIn, balanceOut = balanceOut, balanceIn
}
price := m.Price.GetBasePrice()
if isBaseAsset {
price = m.Price.GetQuotePrice()
}
return formula.PluggableOpts{
BalanceIn: balanceIn,
BalanceOut: balanceOut,
Price: price,
}
}
func (m *Market) formulaOptsForBalanced(
baseBalance, quoteBalance uint64, isBaseAsset, isBuy bool,
) interface{} {
bp := uint64(math.Pow10(int(m.BaseAssetPrecision)))
qp := uint64(math.Pow10(int(m.QuoteAssetPrecision)))
balanceIn := mathutil.Div(baseBalance, bp)
balanceOut := mathutil.Div(quoteBalance, qp)
if isBuy {
balanceIn, balanceOut = balanceOut, balanceIn
}
return formula.BalancedReservesOpts{
BalanceIn: balanceIn,
BalanceOut: balanceOut,
}
}
func (m *Market) priceForStrategy(
baseBalance, quoteBalance uint64,
) (MarketPrice, error) {
if m.IsStrategyPluggable() {
return m.Price, nil
}
return m.priceFromBalances(baseBalance, quoteBalance)
}
func (m *Market) priceFromBalances(
baseBalance, quoteBalance uint64,
) (price MarketPrice, err error) {
bp := uint64(math.Pow10(int(m.BaseAssetPrecision)))
qp := uint64(math.Pow10(int(m.QuoteAssetPrecision)))
balanceIn := mathutil.Div(baseBalance, bp)
balanceOut := mathutil.Div(quoteBalance, qp)
quotePrice, err := m.strategy().SpotPrice(
formula.BalancedReservesOpts{
BalanceIn: balanceIn,
BalanceOut: balanceOut,
},
)
if err != nil {
return
}
basePrice := decimal.NewFromInt(1).Div(quotePrice)
price = MarketPrice{
BasePrice: basePrice.String(),
QuotePrice: quotePrice.String(),
}
return
}
func (m *Market) previewFees(amount uint64, asset string, isBuy bool) (uint64, error) {
percentageFee := m.PercentageFee.BaseAsset
fixedFee := m.FixedFee.BaseAsset
if asset == m.QuoteAsset {
percentageFee = m.PercentageFee.QuoteAsset
fixedFee = m.FixedFee.QuoteAsset
}
fee := decimal.NewFromInt(int64(percentageFee)).Div(decimal.NewFromInt(10000))
feeAmount := decimal.NewFromInt(int64(amount)).Mul(fee).BigInt().Uint64()
feeAmount += fixedFee
// Fees must always be added on the amount sent by the proposer and
// subtracted on the amount he receives, ie. fees are going to be subtracted
// if he's BUYing and wants fees on base amount of the preview, or if he's
// SELLing and wants fees on quote amount.
feesToSubtract := (isBuy && asset == m.BaseAsset) ||
(!isBuy && asset == m.QuoteAsset)
if feesToSubtract && feeAmount >= amount {
return 0, ErrMarketPreviewAmountTooLow
}
return feeAmount, nil
}
func makeAccountName(baseAsset, quoteAsset string) string {
buf, _ := hex.DecodeString(baseAsset + quoteAsset)
return hex.EncodeToString(btcutil.Hash160(buf))[:5]
}
func isValidAsset(asset string) bool {
buf, err := hex.DecodeString(asset)
if err != nil {
return false
}
return len(buf) == 32
}
func isValidPercentageFee(baseFee, quoteFee int64) bool {
isValid := func(v int64) bool {
return (v >= 0 && v <= 9999)
}
return isValid(baseFee) && isValid(quoteFee)
}
func isValidFixedFee(baseFee, quoteFee int64) bool {
isValid := func(v int64) bool {
return v >= -1
}
return isValid(baseFee) && isValid(quoteFee)
}
func isValidPrecision(precision uint) bool {
return int(precision) >= 0 && precision <= 8
}
func isValidStrategy(strategy uint) bool {
return strategy >= StrategyTypeUndefined && strategy <= StrategyTypeBalanced
}