/
markets_service.go
123 lines (103 loc) · 3.35 KB
/
markets_service.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
package api
import (
"encoding/json"
"github.com/novaprotocolio/backend/models"
"github.com/novaprotocolio/sdk-backend/common"
"github.com/novaprotocolio/sdk-backend/utils"
"github.com/shopspring/decimal"
"time"
)
type SnapshotV2 struct {
Sequence uint64 `json:"sequence"`
Bids [][2]string `json:"bids"`
Asks [][2]string `json:"asks"`
}
func GetOrderbook(p Param) (interface{}, error) {
params := p.(*OrderbookReq)
marketID := params.MarketID
var snapshot SnapshotV2
orderBookStr, err := CacheService.Get(common.GetMarketOrderbookSnapshotV2Key(marketID))
if err == common.KVStoreEmpty {
orderBookStr = utils.ToJsonString(&SnapshotV2{
Sequence: 0,
Bids: [][2]string{},
Asks: [][2]string{},
})
} else if err != nil {
return nil, err
}
err = json.Unmarshal([]byte(orderBookStr), &snapshot)
if err != nil {
return nil, err
}
return map[string]interface{}{
"orderBook": snapshot,
}, nil
}
func GetMarkets(_ Param) (interface{}, error) {
var markets []Market
dbMarkets := models.MarketDao.FindPublishedMarkets()
for _, dbMarket := range dbMarkets {
marketStatus := GetMarketStatus(dbMarket.ID)
gasFeeAmount := getGasFeeAmount(dbMarket)
markets = append(markets, Market{
ID: dbMarket.ID,
BaseToken: dbMarket.BaseTokenSymbol,
BaseTokenName: dbMarket.BaseTokenName,
BaseTokenDecimals: dbMarket.BaseTokenDecimals,
BaseTokenAddress: dbMarket.BaseTokenAddress,
QuoteToken: dbMarket.QuoteTokenSymbol,
QuoteTokenDecimals: dbMarket.QuoteTokenDecimals,
QuoteTokenAddress: dbMarket.QuoteTokenAddress,
MinOrderSize: dbMarket.MinOrderSize,
PricePrecision: dbMarket.PricePrecision,
PriceDecimals: dbMarket.PriceDecimals,
AmountDecimals: dbMarket.AmountDecimals,
AsMakerFeeRate: dbMarket.MakerFeeRate,
AsTakerFeeRate: dbMarket.TakerFeeRate,
GasFeeAmount: gasFeeAmount,
SupportedOrderTypes: []string{"limit", "market"},
MarketOrderMaxSlippage: utils.StringToDecimal("0.1"),
MarketStatus: *marketStatus,
})
}
return map[string]interface{}{
"markets": markets,
}, nil
}
func GetMarketStatus(marketID string) *MarketStatus {
yesterday := time.Now().UTC().Add(-time.Hour * 24)
trades := models.TradeDao.FindTradesByMarket(marketID, yesterday, time.Now())
lastPrice := decimal.Zero
lastPriceIncrease := decimal.Zero
price24h := decimal.Zero
amount24h := decimal.Zero
quoteTokenVolume24h := decimal.Zero
if len(trades) == 0 {
return &MarketStatus{
LastPrice: lastPrice,
LastPriceIncrease: lastPriceIncrease,
Price24h: price24h,
Amount24h: amount24h,
QuoteTokenVolume24h: quoteTokenVolume24h,
}
}
lastPrice = trades[0].Price
if len(trades) > 1 {
lastPriceIncrease = trades[0].Price.Sub(trades[1].Price)
}
price24h = trades[0].Price.Sub(trades[len(trades)-1].Price).Div(trades[len(trades)-1].Price)
amount24h = decimal.Zero
quoteTokenVolume24h = decimal.Zero
for _, trade := range trades {
amount24h.Add(trade.Price.Mul(trade.Amount))
quoteTokenVolume24h.Add(trade.Amount)
}
return &MarketStatus{
LastPrice: lastPrice,
LastPriceIncrease: lastPriceIncrease,
Price24h: price24h,
Amount24h: amount24h,
QuoteTokenVolume24h: quoteTokenVolume24h,
}
}