/
market_resolvers.go
242 lines (209 loc) · 7.26 KB
/
market_resolvers.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
// Copyright (c) 2022 Gobalsky Labs Limited
//
// Use of this software is governed by the Business Source License included
// in the LICENSE.DATANODE file and at https://www.mariadb.com/bsl11.
//
// Change Date: 18 months from the later of the date of the first publicly
// available Distribution of this version of the repository, and 25 June 2022.
//
// On the date above, in accordance with the Business Source License, use
// of this software will be governed by version 3 or later of the GNU General
// Public License.
package gql
import (
"context"
"errors"
"github.com/elysiumstation/fury/logging"
v2 "github.com/elysiumstation/fury/protos/data-node/api/v2"
"github.com/elysiumstation/fury/protos/fury"
types "github.com/elysiumstation/fury/protos/fury"
)
type myMarketResolver FuryResolverRoot
func (r *myMarketResolver) LiquidityProvisionsConnection(
ctx context.Context,
market *types.Market,
party *string,
live *bool,
pagination *v2.Pagination,
) (*v2.LiquidityProvisionsConnection, error) {
var pid string
if party != nil {
pid = *party
}
var marketID string
if market != nil {
marketID = market.Id
}
var l bool
if live != nil {
l = *live
}
req := v2.ListLiquidityProvisionsRequest{
PartyId: &pid,
MarketId: &marketID,
Live: &l,
Pagination: pagination,
}
res, err := r.tradingDataClientV2.ListLiquidityProvisions(ctx, &req)
if err != nil {
r.log.Error("tradingData client", logging.Error(err))
return nil, err
}
return res.LiquidityProvisions, nil
}
func (r *myMarketResolver) Data(ctx context.Context, market *types.Market) (*types.MarketData, error) {
req := v2.GetLatestMarketDataRequest{
MarketId: market.Id,
}
res, err := r.tradingDataClientV2.GetLatestMarketData(ctx, &req)
if err != nil {
r.log.Error("tradingData client", logging.Error(err))
return nil, err
}
return res.MarketData, nil
}
func (r *myMarketResolver) OrdersConnection(
ctx context.Context,
market *types.Market,
pagination *v2.Pagination,
filter *OrderByPartyIdsFilter,
) (*v2.OrderConnection, error) {
req := v2.ListOrdersRequest{
Pagination: pagination,
Filter: &v2.OrderFilter{
MarketIds: []string{market.Id},
},
}
if filter != nil {
req.Filter.PartyIds = filter.PartyIds
if filter.Order != nil {
req.Filter.Statuses = filter.Order.Statuses
req.Filter.Types = filter.Order.Types
req.Filter.TimeInForces = filter.Order.TimeInForces
req.Filter.ExcludeLiquidity = filter.Order.ExcludeLiquidity
req.Filter.Reference = filter.Order.Reference
req.Filter.DateRange = filter.Order.DateRange
req.Filter.LiveOnly = filter.Order.LiveOnly
}
}
res, err := r.tradingDataClientV2.ListOrders(ctx, &req)
if err != nil {
r.log.Error("tradingData client", logging.Error(err))
return nil, err
}
return res.Orders, nil
}
func (r *myMarketResolver) TradesConnection(ctx context.Context, market *types.Market, dateRange *v2.DateRange, pagination *v2.Pagination) (*v2.TradeConnection, error) {
req := v2.ListTradesRequest{
MarketIds: []string{market.Id},
Pagination: pagination,
DateRange: dateRange,
}
res, err := r.tradingDataClientV2.ListTrades(ctx, &req)
if err != nil {
r.log.Error("tradingData client", logging.Error(err))
return nil, err
}
return res.Trades, nil
}
func (r *myMarketResolver) Depth(ctx context.Context, market *types.Market, maxDepth *int) (*types.MarketDepth, error) {
if market == nil {
return nil, errors.New("market missing or empty")
}
req := v2.GetLatestMarketDepthRequest{MarketId: market.Id}
if maxDepth != nil {
if *maxDepth <= 0 {
return nil, errors.New("invalid maxDepth, must be a positive number")
}
reqDepth := uint64(*maxDepth)
req.MaxDepth = &reqDepth
}
// Look for market depth for the given market (will validate market internally)
// Note: Market depth is also known as OrderBook depth within the matching-engine
res, err := r.tradingDataClientV2.GetLatestMarketDepth(ctx, &req)
if err != nil {
r.log.Error("trading data client", logging.Error(err))
return nil, err
}
return &types.MarketDepth{
MarketId: res.MarketId,
Buy: res.Buy,
Sell: res.Sell,
SequenceNumber: res.SequenceNumber,
}, nil
}
func (r *myMarketResolver) AccountsConnection(ctx context.Context, market *types.Market, partyID *string, pagination *v2.Pagination) (*v2.AccountsConnection, error) {
filter := v2.AccountFilter{MarketIds: []string{market.Id}}
ptyID := ""
if partyID != nil {
// get margin account for a party
ptyID = *partyID
filter.PartyIds = []string{ptyID}
filter.AccountTypes = []types.AccountType{types.AccountType_ACCOUNT_TYPE_MARGIN}
} else {
filter.AccountTypes = []types.AccountType{
types.AccountType_ACCOUNT_TYPE_INSURANCE,
types.AccountType_ACCOUNT_TYPE_FEES_LIQUIDITY,
}
}
req := v2.ListAccountsRequest{Filter: &filter, Pagination: pagination}
res, err := r.tradingDataClientV2.ListAccounts(ctx, &req)
if err != nil {
r.log.Error("unable to get market accounts",
logging.Error(err),
logging.String("market-id", market.Id),
logging.String("party-id", ptyID))
return nil, err
}
return res.Accounts, nil
}
func (r *myMarketResolver) DecimalPlaces(_ context.Context, obj *types.Market) (int, error) {
return int(obj.DecimalPlaces), nil
}
func (r *myMarketResolver) PositionDecimalPlaces(_ context.Context, obj *types.Market) (int, error) {
return int(obj.PositionDecimalPlaces), nil
}
func (r *myMarketResolver) OpeningAuction(_ context.Context, obj *types.Market) (*AuctionDuration, error) {
return &AuctionDuration{
DurationSecs: int(obj.OpeningAuction.Duration),
Volume: int(obj.OpeningAuction.Volume),
}, nil
}
func (r *myMarketResolver) PriceMonitoringSettings(_ context.Context, obj *types.Market) (*PriceMonitoringSettings, error) {
return PriceMonitoringSettingsFromProto(obj.PriceMonitoringSettings)
}
func (r *myMarketResolver) LiquidityMonitoringParameters(_ context.Context, obj *types.Market) (*LiquidityMonitoringParameters, error) {
return &LiquidityMonitoringParameters{
TargetStakeParameters: &TargetStakeParameters{
TimeWindow: int(obj.LiquidityMonitoringParameters.TargetStakeParameters.TimeWindow),
ScalingFactor: obj.LiquidityMonitoringParameters.TargetStakeParameters.ScalingFactor,
},
TriggeringRatio: obj.LiquidityMonitoringParameters.TriggeringRatio,
}, nil
}
func (r *myMarketResolver) Proposal(ctx context.Context, obj *types.Market) (*types.GovernanceData, error) {
resp, err := r.tradingDataClientV2.GetGovernanceData(ctx, &v2.GetGovernanceDataRequest{
ProposalId: &obj.Id,
})
// it's possible to not find a proposal as of now.
// some market are loaded at startup, without
// going through the proposal phase
if err != nil {
return nil, nil //nolint:nilerr
}
return resp.Data, nil
}
func (r *myMarketResolver) RiskFactors(ctx context.Context, obj *types.Market) (*types.RiskFactor, error) {
rf, err := r.tradingDataClientV2.GetRiskFactors(ctx, &v2.GetRiskFactorsRequest{
MarketId: obj.Id,
})
if err != nil {
return nil, err
}
return rf.RiskFactor, nil
}
func (r *myMarketResolver) CandlesConnection(ctx context.Context, market *types.Market, sinceRaw string, toRaw *string,
interval fury.Interval, pagination *v2.Pagination,
) (*v2.CandleDataConnection, error) {
return handleCandleConnectionRequest(ctx, r.tradingDataClientV2, market, sinceRaw, toRaw, interval, pagination)
}