-
Notifications
You must be signed in to change notification settings - Fork 6
/
grpc.go
898 lines (774 loc) · 37.2 KB
/
grpc.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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
package provider
import (
"context"
"crypto/tls"
"fmt"
package_info "github.com/bloXroute-Labs/solana-trader-client-go"
"github.com/bloXroute-Labs/solana-trader-client-go/connections"
"github.com/bloXroute-Labs/solana-trader-client-go/transaction"
pb "github.com/bloXroute-Labs/solana-trader-proto/api"
"github.com/bloXroute-Labs/solana-trader-proto/common"
"github.com/gagliardetto/solana-go"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
)
type GRPCClient struct {
pb.UnimplementedApiServer
apiClient pb.ApiClient
privateKey *solana.PrivateKey
recentBlockHashStore *recentBlockHashStore
}
// NewGRPCClient connects to Mainnet Trader API
func NewGRPCClient() (*GRPCClient, error) {
opts := DefaultRPCOpts(MainnetNYGRPC)
opts.UseTLS = true
return NewGRPCClientWithOpts(opts)
}
// NewGRPCTestnet connects to Testnet Trader API
func NewGRPCTestnet() (*GRPCClient, error) {
opts := DefaultRPCOpts(TestnetGRPC)
return NewGRPCClientWithOpts(opts)
}
// NewGRPCDevnet connects to Devnet Trader API
func NewGRPCDevnet() (*GRPCClient, error) {
opts := DefaultRPCOpts(DevnetGRPC)
return NewGRPCClientWithOpts(opts)
}
// NewGRPCLocal connects to local Trader API
func NewGRPCLocal() (*GRPCClient, error) {
opts := DefaultRPCOpts(LocalGRPC)
return NewGRPCClientWithOpts(opts)
}
type blxrCredentials struct {
authorization string
}
func (bc blxrCredentials) GetRequestMetadata(ctx context.Context, uri ...string) (map[string]string, error) {
return map[string]string{
"authorization": bc.authorization,
"x-sdk": package_info.Name,
"x-sdk-version": package_info.Version,
}, nil
}
func (bc blxrCredentials) RequireTransportSecurity() bool {
return false
}
// NewGRPCClientWithOpts connects to custom Trader API
func NewGRPCClientWithOpts(opts RPCOpts, dialOpts ...grpc.DialOption) (*GRPCClient, error) {
var (
conn grpc.ClientConnInterface
err error
grpcOpts = make([]grpc.DialOption, 0)
)
transportOption := grpc.WithTransportCredentials(insecure.NewCredentials())
if opts.UseTLS {
transportOption = grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{}))
}
grpcOpts = append(grpcOpts, transportOption)
if !opts.DisableAuth {
grpcOpts = append(grpcOpts, grpc.WithPerRPCCredentials(blxrCredentials{authorization: opts.AuthHeader}))
}
grpcOpts = append(grpcOpts, dialOpts...)
conn, err = grpc.Dial(opts.Endpoint, grpcOpts...)
if err != nil {
return nil, err
}
client := &GRPCClient{
apiClient: pb.NewApiClient(conn),
privateKey: opts.PrivateKey,
}
client.recentBlockHashStore = newRecentBlockHashStore(
client.GetRecentBlockHash,
client.GetRecentBlockHashStream,
opts,
)
if opts.CacheBlockHash {
go client.recentBlockHashStore.run(context.Background())
}
return client, nil
}
func (g *GRPCClient) RecentBlockHash(ctx context.Context) (*pb.GetRecentBlockHashResponse, error) {
return g.recentBlockHashStore.get(ctx)
}
func (g *GRPCClient) GetRecentBlockHash(ctx context.Context) (*pb.GetRecentBlockHashResponse, error) {
return g.apiClient.GetRecentBlockHash(ctx, &pb.GetRecentBlockHashRequest{})
}
// GetPriorityFee returns a priority fee estimate for a given percentile
func (g *GRPCClient) GetPriorityFee(ctx context.Context, request *pb.GetPriorityFeeRequest) (*pb.GetPriorityFeeResponse, error) {
return g.apiClient.GetPriorityFee(ctx, request)
}
// GetRateLimit returns details of an account rate-limits
func (g *GRPCClient) GetRateLimit(ctx context.Context, request *pb.GetRateLimitRequest) (*pb.GetRateLimitResponse, error) {
return g.apiClient.GetRateLimit(ctx, request)
}
// GetTransaction returns details of a recent transaction
func (g *GRPCClient) GetTransaction(ctx context.Context, request *pb.GetTransactionRequest) (*pb.GetTransactionResponse, error) {
return g.apiClient.GetTransaction(ctx, request)
}
// GetRaydiumPools returns pools on Raydium
func (g *GRPCClient) GetRaydiumPools(ctx context.Context, request *pb.GetRaydiumPoolsRequest) (*pb.GetRaydiumPoolsResponse, error) {
return g.apiClient.GetRaydiumPools(ctx, request)
}
// GetRaydiumQuotes returns the possible amount(s) of outToken for an inToken and the route to achieve it on Raydium
func (g *GRPCClient) GetRaydiumQuotes(ctx context.Context, request *pb.GetRaydiumQuotesRequest) (*pb.GetRaydiumQuotesResponse, error) {
return g.apiClient.GetRaydiumQuotes(ctx, request)
}
// GetRaydiumPrices returns the USDC price of requested tokens on Raydium
func (g *GRPCClient) GetRaydiumPrices(ctx context.Context, request *pb.GetRaydiumPricesRequest) (*pb.GetRaydiumPricesResponse, error) {
return g.apiClient.GetRaydiumPrices(ctx, request)
}
// PostRaydiumSwap returns a partially signed transaction(s) for submitting a swap request on Raydium
func (g *GRPCClient) PostRaydiumSwap(ctx context.Context, request *pb.PostRaydiumSwapRequest) (*pb.PostRaydiumSwapResponse, error) {
return g.apiClient.PostRaydiumSwap(ctx, request)
}
// PostRaydiumRouteSwap returns a partially signed transaction(s) for submitting a swap request on Raydium
func (g *GRPCClient) PostRaydiumRouteSwap(ctx context.Context, request *pb.PostRaydiumRouteSwapRequest) (*pb.PostRaydiumRouteSwapResponse, error) {
return g.apiClient.PostRaydiumRouteSwap(ctx, request)
}
// GetJupiterQuotes returns the possible amount(s) of outToken for an inToken and the route to achieve it on Jupiter
func (g *GRPCClient) GetJupiterQuotes(ctx context.Context, request *pb.GetJupiterQuotesRequest) (*pb.GetJupiterQuotesResponse, error) {
return g.apiClient.GetJupiterQuotes(ctx, request)
}
// GetJupiterPrices returns the USDC price of requested tokens on Jupiter
func (g *GRPCClient) GetJupiterPrices(ctx context.Context, request *pb.GetJupiterPricesRequest) (*pb.GetJupiterPricesResponse, error) {
return g.apiClient.GetJupiterPrices(ctx, request)
}
// PostJupiterSwap returns a partially signed transaction(s) for submitting a swap request on Jupiter
func (g *GRPCClient) PostJupiterSwap(ctx context.Context, request *pb.PostJupiterSwapRequest) (*pb.PostJupiterSwapResponse, error) {
return g.apiClient.PostJupiterSwap(ctx, request)
}
// PostJupiterRouteSwap returns a partially signed transaction(s) for submitting a swap request on Jupiter
func (g *GRPCClient) PostJupiterRouteSwap(ctx context.Context, request *pb.PostJupiterRouteSwapRequest) (*pb.PostJupiterRouteSwapResponse, error) {
return g.apiClient.PostJupiterRouteSwap(ctx, request)
}
// GetOrderbook returns the requested market's orderbook (e.g. asks and bids). Set limit to 0 for all bids / asks.
func (g *GRPCClient) GetOrderbook(ctx context.Context, market string, limit uint32, project pb.Project) (*pb.GetOrderbookResponse, error) {
return g.apiClient.GetOrderbook(ctx, &pb.GetOrderbookRequest{Market: market, Limit: limit, Project: project})
}
// GetMarketDepth returns the requested market's coalesced price data (e.g. asks and bids). Set limit to 0 for all bids / asks.
func (g *GRPCClient) GetMarketDepth(ctx context.Context, market string, limit uint32, project pb.Project) (*pb.GetMarketDepthResponse, error) {
return g.apiClient.GetMarketDepth(ctx, &pb.GetMarketDepthRequest{Market: market, Limit: limit, Project: project})
}
// GetPools returns pools for given projects.
func (g *GRPCClient) GetPools(ctx context.Context, projects []pb.Project) (*pb.GetPoolsResponse, error) {
return g.apiClient.GetPools(ctx, &pb.GetPoolsRequest{Projects: projects})
}
// GetTrades returns the requested market's currently executing trades. Set limit to 0 for all trades.
func (g *GRPCClient) GetTrades(ctx context.Context, market string, limit uint32, project pb.Project) (*pb.GetTradesResponse, error) {
return g.apiClient.GetTrades(ctx, &pb.GetTradesRequest{Market: market, Limit: limit, Project: project})
}
// GetTickers returns the requested market tickets. Set market to "" for all markets.
func (g *GRPCClient) GetTickers(ctx context.Context, market string, project pb.Project) (*pb.GetTickersResponse, error) {
return g.apiClient.GetTickers(ctx, &pb.GetTickersRequest{Market: market, Project: project})
}
// GetOpenOrders returns all open orders by owner address and market
func (g *GRPCClient) GetOpenOrders(ctx context.Context, market string, owner string, openOrdersAddress string, project pb.Project) (*pb.GetOpenOrdersResponse, error) {
return g.apiClient.GetOpenOrders(ctx, &pb.GetOpenOrdersRequest{Market: market, Address: owner, OpenOrdersAddress: openOrdersAddress, Project: project})
}
// GetOrderByID returns an order by id
func (g *GRPCClient) GetOrderByID(ctx context.Context, in *pb.GetOrderByIDRequest) (*pb.GetOrderByIDResponse, error) {
return g.apiClient.GetOrderByID(ctx, in)
}
// GetUnsettled returns all OpenOrders accounts for a given market with the amounts of unsettled funds
func (g *GRPCClient) GetUnsettled(ctx context.Context, market string, ownerAddress string, project pb.Project) (*pb.GetUnsettledResponse, error) {
return g.apiClient.GetUnsettled(ctx, &pb.GetUnsettledRequest{Market: market, OwnerAddress: ownerAddress, Project: project})
}
// GetMarkets returns the list of all available named markets
func (g *GRPCClient) GetMarkets(ctx context.Context) (*pb.GetMarketsResponse, error) {
return g.apiClient.GetMarkets(ctx, &pb.GetMarketsRequest{})
}
// GetAccountBalance returns all tokens associated with the owner address including Serum unsettled amounts
func (g *GRPCClient) GetAccountBalance(ctx context.Context, owner string) (*pb.GetAccountBalanceResponse, error) {
return g.apiClient.GetAccountBalance(ctx, &pb.GetAccountBalanceRequest{OwnerAddress: owner})
}
// GetPrice returns the USDC price of requested tokens
func (g *GRPCClient) GetPrice(ctx context.Context, tokens []string) (*pb.GetPriceResponse, error) {
return g.apiClient.GetPrice(ctx, &pb.GetPriceRequest{Tokens: tokens})
}
// GetQuotes returns the possible amount(s) of outToken for an inToken and the route to achieve it
func (g *GRPCClient) GetQuotes(ctx context.Context, inToken, outToken string, inAmount, slippage float64, limit int32, projects []pb.Project) (*pb.GetQuotesResponse, error) {
return g.apiClient.GetQuotes(ctx, &pb.GetQuotesRequest{InToken: inToken, OutToken: outToken, InAmount: inAmount, Slippage: slippage, Limit: limit, Projects: projects})
}
// SignAndSubmit signs the given transaction and submits it.
func (g *GRPCClient) SignAndSubmit(ctx context.Context, tx *pb.TransactionMessage, skipPreFlight bool, frontRunningProtection bool) (string, error) {
if g.privateKey == nil {
return "", ErrPrivateKeyNotFound
}
txBase64, err := transaction.SignTxWithPrivateKey(tx.Content, *g.privateKey)
if err != nil {
return "", err
}
response, err := g.PostSubmit(ctx, &pb.TransactionMessage{
Content: txBase64,
IsCleanup: tx.IsCleanup,
}, skipPreFlight, frontRunningProtection)
if err != nil {
return "", err
}
return response.Signature, nil
}
// signAndSubmitBatch signs the given transactions and submits them.
func (g *GRPCClient) signAndSubmitBatch(ctx context.Context, transactions []*pb.TransactionMessage, useBundle bool, opts SubmitOpts) (*pb.PostSubmitBatchResponse, error) {
if g.privateKey == nil {
return nil, ErrPrivateKeyNotFound
}
batchRequest, err := buildBatchRequest(transactions, *g.privateKey, useBundle, opts)
if err != nil {
return nil, err
}
return g.PostSubmitBatch(ctx, batchRequest)
}
// PostTradeSwap returns a partially signed transaction for submitting a swap request
func (g *GRPCClient) PostTradeSwap(ctx context.Context, ownerAddress, inToken, outToken string, inAmount, slippage float64, project pb.Project) (*pb.TradeSwapResponse, error) {
return g.apiClient.PostTradeSwap(ctx, &pb.TradeSwapRequest{
OwnerAddress: ownerAddress,
InToken: inToken,
OutToken: outToken,
InAmount: inAmount,
Slippage: slippage,
Project: project,
})
}
// PostRouteTradeSwap returns a partially signed transaction(s) for submitting a swap request
func (g *GRPCClient) PostRouteTradeSwap(ctx context.Context, request *pb.RouteTradeSwapRequest) (*pb.TradeSwapResponse, error) {
return g.apiClient.PostRouteTradeSwap(ctx, request)
}
// PostOrder returns a partially signed transaction for placing a Serum market order. Typically, you want to use SubmitOrder instead of this.
func (g *GRPCClient) PostOrder(ctx context.Context, owner, payer, market string, side pb.Side, types []common.OrderType, amount, price float64, project pb.Project, opts PostOrderOpts) (*pb.PostOrderResponse, error) {
return g.apiClient.PostOrder(ctx, &pb.PostOrderRequest{
OwnerAddress: owner,
PayerAddress: payer,
Market: market,
Side: side,
Type: types,
Amount: amount,
Price: price,
Project: project,
OpenOrdersAddress: opts.OpenOrdersAddress,
ClientOrderID: opts.ClientOrderID,
})
}
// PostSubmit posts the transaction string to the Solana network.
func (g *GRPCClient) PostSubmit(ctx context.Context, tx *pb.TransactionMessage, skipPreFlight bool, frontRunningProtection bool) (*pb.PostSubmitResponse, error) {
return g.apiClient.PostSubmit(ctx, &pb.PostSubmitRequest{Transaction: tx,
SkipPreFlight: skipPreFlight,
FrontRunningProtection: &frontRunningProtection})
}
// PostSubmitBatch posts a bundle of transactions string based on a specific SubmitStrategy to the Solana network.
func (g *GRPCClient) PostSubmitBatch(ctx context.Context, request *pb.PostSubmitBatchRequest) (*pb.PostSubmitBatchResponse, error) {
return g.apiClient.PostSubmitBatch(ctx, request)
}
// PostSubmitV2 posts the transaction string to the Solana network.
func (g *GRPCClient) PostSubmitV2(ctx context.Context, tx *pb.TransactionMessage, skipPreFlight bool, frontRunningProtection bool) (*pb.PostSubmitResponse, error) {
return g.apiClient.PostSubmitV2(ctx, &pb.PostSubmitRequest{Transaction: tx,
SkipPreFlight: skipPreFlight,
FrontRunningProtection: &frontRunningProtection})
}
// PostSubmitBatchV2 posts a bundle of transactions string based on a specific SubmitStrategy to the Solana network.
func (g *GRPCClient) PostSubmitBatchV2(ctx context.Context, request *pb.PostSubmitBatchRequest) (*pb.PostSubmitBatchResponse, error) {
return g.apiClient.PostSubmitBatchV2(ctx, request)
}
// SubmitTradeSwap builds a TradeSwap transaction then signs it, and submits to the network.
func (g *GRPCClient) SubmitTradeSwap(ctx context.Context, ownerAddress, inToken, outToken string, inAmount, slippage float64, project pb.Project, opts SubmitOpts) (*pb.PostSubmitBatchResponse, error) {
resp, err := g.apiClient.PostTradeSwap(ctx, &pb.TradeSwapRequest{
OwnerAddress: ownerAddress,
InToken: inToken,
OutToken: outToken,
InAmount: inAmount,
Slippage: slippage,
Project: project,
})
if err != nil {
return nil, err
}
return g.signAndSubmitBatch(ctx, resp.Transactions, false, opts)
}
// SubmitRouteTradeSwap builds a RouteTradeSwap transaction then signs it, and submits to the network.
func (g *GRPCClient) SubmitRouteTradeSwap(ctx context.Context, request *pb.RouteTradeSwapRequest, opts SubmitOpts) (*pb.PostSubmitBatchResponse, error) {
resp, err := g.PostRouteTradeSwap(ctx, request)
if err != nil {
return nil, err
}
return g.signAndSubmitBatch(ctx, resp.Transactions, false, opts)
}
// SubmitRaydiumSwap builds a Raydium Swap transaction then signs it, and submits to the network.
func (g *GRPCClient) SubmitRaydiumSwap(ctx context.Context, request *pb.PostRaydiumSwapRequest, opts SubmitOpts) (*pb.PostSubmitBatchResponse, error) {
resp, err := g.PostRaydiumSwap(ctx, request)
if err != nil {
return nil, err
}
return g.signAndSubmitBatch(ctx, resp.Transactions, false, opts)
}
// SubmitRaydiumRouteSwap builds a Raydium RouteSwap transaction then signs it, and submits to the network.
func (g *GRPCClient) SubmitRaydiumRouteSwap(ctx context.Context, request *pb.PostRaydiumRouteSwapRequest, opts SubmitOpts) (*pb.PostSubmitBatchResponse, error) {
resp, err := g.PostRaydiumRouteSwap(ctx, request)
if err != nil {
return nil, err
}
return g.signAndSubmitBatch(ctx, resp.Transactions, false, opts)
}
// SubmitJupiterSwap builds a Jupiter Swap transaction then signs it, and submits to the network.
func (g *GRPCClient) SubmitJupiterSwap(ctx context.Context, request *pb.PostJupiterSwapRequest, opts SubmitOpts) (*pb.PostSubmitBatchResponse, error) {
resp, err := g.PostJupiterSwap(ctx, request)
if err != nil {
return nil, err
}
return g.signAndSubmitBatch(ctx, resp.Transactions, false, opts)
}
// SubmitJupiterRouteSwap builds a Jupiter RouteSwap transaction then signs it, and submits to the network.
func (g *GRPCClient) SubmitJupiterRouteSwap(ctx context.Context, request *pb.PostJupiterRouteSwapRequest, opts SubmitOpts) (*pb.PostSubmitBatchResponse, error) {
resp, err := g.PostJupiterRouteSwap(ctx, request)
if err != nil {
return nil, err
}
return g.signAndSubmitBatch(ctx, resp.Transactions, false, opts)
}
// SubmitOrder builds a Serum market order, signs it, and submits to the network.
func (g *GRPCClient) SubmitOrder(ctx context.Context, owner, payer, market string, side pb.Side, types []common.OrderType, amount, price float64, project pb.Project, opts PostOrderOpts) (string, error) {
order, err := g.PostOrder(ctx, owner, payer, market, side, types, amount, price, project, opts)
if err != nil {
return "", err
}
skipPreFlight := true
if opts.SkipPreFlight != nil {
skipPreFlight = *opts.SkipPreFlight
}
return g.SignAndSubmit(ctx, order.Transaction, skipPreFlight, false)
}
// PostCancelOrder builds a Serum cancel order.
func (g *GRPCClient) PostCancelOrder(
ctx context.Context,
orderID string,
side pb.Side,
owner,
market,
openOrders string,
project pb.Project,
) (*pb.PostCancelOrderResponse, error) {
return g.apiClient.PostCancelOrder(ctx, &pb.PostCancelOrderRequest{
OrderID: orderID,
Side: side,
OwnerAddress: owner,
MarketAddress: market,
OpenOrdersAddress: openOrders,
Project: project,
})
}
// SubmitCancelOrder builds a Serum cancel order, signs and submits it to the network.
func (g *GRPCClient) SubmitCancelOrder(
ctx context.Context,
orderID string,
side pb.Side,
owner,
market,
openOrders string,
project pb.Project,
skipPreFlight bool,
) (string, error) {
order, err := g.PostCancelOrder(ctx, orderID, side, owner, market, openOrders, project)
if err != nil {
return "", err
}
return g.SignAndSubmit(ctx, order.Transaction, skipPreFlight, false)
}
// PostCancelByClientOrderID builds a Serum cancel order by client ID.
func (g *GRPCClient) PostCancelByClientOrderID(
ctx context.Context,
clientOrderID uint64,
owner,
market,
openOrders string,
project pb.Project,
) (*pb.PostCancelOrderResponse, error) {
return g.apiClient.PostCancelByClientOrderID(ctx, &pb.PostCancelByClientOrderIDRequest{
ClientOrderID: clientOrderID,
OwnerAddress: owner,
MarketAddress: market,
OpenOrdersAddress: openOrders,
Project: project,
})
}
// SubmitCancelByClientOrderID builds a Serum cancel order by client ID, signs and submits it to the network.
func (g *GRPCClient) SubmitCancelByClientOrderID(
ctx context.Context,
clientOrderID uint64,
owner,
market,
openOrders string,
project pb.Project,
skipPreFlight bool,
) (string, error) {
order, err := g.PostCancelByClientOrderID(ctx, clientOrderID, owner, market, openOrders, project)
if err != nil {
return "", err
}
return g.SignAndSubmit(ctx, order.Transaction, skipPreFlight, false)
}
func (g *GRPCClient) PostCancelAll(ctx context.Context, market, owner string, openOrders []string, project pb.Project) (*pb.PostCancelAllResponse, error) {
return g.apiClient.PostCancelAll(ctx, &pb.PostCancelAllRequest{
Market: market,
OwnerAddress: owner,
OpenOrdersAddresses: openOrders,
Project: project,
})
}
func (g *GRPCClient) SubmitCancelAll(ctx context.Context, market, owner string, openOrdersAddresses []string, project pb.Project, opts SubmitOpts) (*pb.PostSubmitBatchResponse, error) {
orders, err := g.PostCancelAll(ctx, market, owner, openOrdersAddresses, project)
if err != nil {
return nil, err
}
return g.signAndSubmitBatch(ctx, orders.Transactions, false, opts)
}
// PostSettle returns a partially signed transaction for settling market funds. Typically, you want to use SubmitSettle instead of this.
func (g *GRPCClient) PostSettle(ctx context.Context, owner, market, baseTokenWallet, quoteTokenWallet, openOrdersAccount string, project pb.Project) (*pb.PostSettleResponse, error) {
return g.apiClient.PostSettle(ctx, &pb.PostSettleRequest{
OwnerAddress: owner,
Market: market,
BaseTokenWallet: baseTokenWallet,
QuoteTokenWallet: quoteTokenWallet,
OpenOrdersAddress: openOrdersAccount,
Project: project,
})
}
// SubmitSettle builds a market SubmitSettle transaction, signs it, and submits to the network.
func (g *GRPCClient) SubmitSettle(ctx context.Context, owner, market, baseTokenWallet, quoteTokenWallet, openOrdersAccount string, project pb.Project, skipPreflight bool) (string, error) {
order, err := g.PostSettle(ctx, owner, market, baseTokenWallet, quoteTokenWallet, openOrdersAccount, project)
if err != nil {
return "", err
}
return g.SignAndSubmit(ctx, order.Transaction, skipPreflight, false)
}
func (g *GRPCClient) PostReplaceByClientOrderID(ctx context.Context, owner, payer, market string, side pb.Side, types []common.OrderType, amount, price float64, project pb.Project, opts PostOrderOpts) (*pb.PostOrderResponse, error) {
return g.apiClient.PostReplaceByClientOrderID(ctx, &pb.PostOrderRequest{
OwnerAddress: owner,
PayerAddress: payer,
Market: market,
Side: side,
Type: types,
Amount: amount,
Price: price,
Project: project,
OpenOrdersAddress: opts.OpenOrdersAddress,
ClientOrderID: opts.ClientOrderID,
})
}
func (g *GRPCClient) SubmitReplaceByClientOrderID(ctx context.Context, owner, payer, market string, side pb.Side, types []common.OrderType, amount, price float64, project pb.Project, opts PostOrderOpts) (string, error) {
order, err := g.PostReplaceByClientOrderID(ctx, owner, payer, market, side, types, amount, price, project, opts)
if err != nil {
return "", err
}
skipPreFlight := true
if opts.SkipPreFlight != nil {
skipPreFlight = *opts.SkipPreFlight
}
return g.SignAndSubmit(ctx, order.Transaction, skipPreFlight, false)
}
func (g *GRPCClient) PostReplaceOrder(ctx context.Context, orderID, owner, payer, market string, side pb.Side, types []common.OrderType, amount, price float64, project pb.Project, opts PostOrderOpts) (*pb.PostOrderResponse, error) {
return g.apiClient.PostReplaceOrder(ctx, &pb.PostReplaceOrderRequest{
OwnerAddress: owner,
PayerAddress: payer,
Market: market,
Side: side,
Type: types,
Amount: amount,
Price: price,
Project: project,
OpenOrdersAddress: opts.OpenOrdersAddress,
ClientOrderID: opts.ClientOrderID,
OrderID: orderID,
})
}
func (g *GRPCClient) SubmitReplaceOrder(ctx context.Context, orderID, owner, payer, market string, side pb.Side, types []common.OrderType, amount, price float64, project pb.Project, opts PostOrderOpts) (string, error) {
order, err := g.PostReplaceOrder(ctx, orderID, owner, payer, market, side, types, amount, price, project, opts)
if err != nil {
return "", err
}
skipPreFlight := true
if opts.SkipPreFlight != nil {
skipPreFlight = *opts.SkipPreFlight
}
return g.SignAndSubmit(ctx, order.Transaction, skipPreFlight, false)
}
// GetOrderbookStream subscribes to a stream for changes to the requested market updates (e.g. asks and bids. Set limit to 0 for all bids/ asks).
func (g *GRPCClient) GetOrderbookStream(ctx context.Context, markets []string, limit uint32, project pb.Project) (connections.Streamer[*pb.GetOrderbooksStreamResponse], error) {
stream, err := g.apiClient.GetOrderbooksStream(ctx, &pb.GetOrderbooksRequest{
Markets: markets, Limit: limit,
Project: project})
if err != nil {
return nil, err
}
return connections.GRPCStream[pb.GetOrderbooksStreamResponse](stream, fmt.Sprint(markets)), nil
}
// GetMarketDepthsStream subscribes to a stream for changes to the requested market data updates (e.g. asks and bids. Set limit to 0 for all bids/ asks).
func (g *GRPCClient) GetMarketDepthsStream(ctx context.Context, markets []string, limit uint32, project pb.Project) (connections.Streamer[*pb.GetMarketDepthsStreamResponse], error) {
stream, err := g.apiClient.GetMarketDepthsStream(ctx, &pb.GetMarketDepthsRequest{Markets: markets, Limit: limit, Project: project})
if err != nil {
return nil, err
}
return connections.GRPCStream[pb.GetMarketDepthsStreamResponse](stream, fmt.Sprint(markets)), nil
}
// GetTradesStream subscribes to a stream for trades as they execute. Set limit to 0 for all trades.
func (g *GRPCClient) GetTradesStream(ctx context.Context, market string, limit uint32, project pb.Project) (connections.Streamer[*pb.GetTradesStreamResponse], error) {
stream, err := g.apiClient.GetTradesStream(ctx, &pb.GetTradesRequest{Market: market, Limit: limit, Project: project})
if err != nil {
return nil, err
}
return connections.GRPCStream[pb.GetTradesStreamResponse](stream, market), nil
}
// GetOrderStatusStream subscribes to a stream that shows updates to the owner's orders
func (g *GRPCClient) GetOrderStatusStream(ctx context.Context, market, ownerAddress string, project pb.Project) (connections.Streamer[*pb.GetOrderStatusStreamResponse], error) {
stream, err := g.apiClient.GetOrderStatusStream(ctx, &pb.GetOrderStatusStreamRequest{Market: market, OwnerAddress: ownerAddress, Project: project})
if err != nil {
return nil, err
}
return connections.GRPCStream[pb.GetOrderStatusStreamResponse](stream, market), nil
}
// GetRecentBlockHashStream subscribes to a stream for getting recent block hash.
func (g *GRPCClient) GetRecentBlockHashStream(ctx context.Context) (connections.Streamer[*pb.GetRecentBlockHashResponse], error) {
stream, err := g.apiClient.GetRecentBlockHashStream(ctx, &pb.GetRecentBlockHashRequest{})
if err != nil {
return nil, err
}
return connections.GRPCStream[pb.GetRecentBlockHashResponse](stream, ""), nil
}
// GetQuotesStream subscribes to a stream for getting recent quotes of tokens of interest.
func (g *GRPCClient) GetQuotesStream(ctx context.Context, projects []pb.Project, tokenPairs []*pb.TokenPair) (connections.Streamer[*pb.GetQuotesStreamResponse], error) {
stream, err := g.apiClient.GetQuotesStream(ctx, &pb.GetQuotesStreamRequest{
Projects: projects,
TokenPairs: tokenPairs,
})
if err != nil {
return nil, err
}
return connections.GRPCStream[pb.GetQuotesStreamResponse](stream, ""), nil
}
// GetPoolReservesStream subscribes to a stream for getting recent quotes of tokens of interest.
func (g *GRPCClient) GetPoolReservesStream(ctx context.Context, projects []pb.Project) (connections.Streamer[*pb.GetPoolReservesStreamResponse], error) {
stream, err := g.apiClient.GetPoolReservesStream(ctx, &pb.GetPoolReservesStreamRequest{
Projects: projects,
})
if err != nil {
return nil, err
}
return connections.GRPCStream[pb.GetPoolReservesStreamResponse](stream, ""), nil
}
// GetPricesStream subscribes to a stream for getting recent prices of tokens of interest.
func (g *GRPCClient) GetPricesStream(ctx context.Context, projects []pb.Project, tokens []string) (connections.Streamer[*pb.GetPricesStreamResponse], error) {
stream, err := g.apiClient.GetPricesStream(ctx, &pb.GetPricesStreamRequest{
Projects: projects,
Tokens: tokens,
})
if err != nil {
return nil, err
}
return connections.GRPCStream[pb.GetPricesStreamResponse](stream, ""), nil
}
// GetSwapsStream subscribes to a stream for getting recent swaps on projects & markets of interest.
func (g *GRPCClient) GetSwapsStream(
ctx context.Context,
projects []pb.Project,
markets []string,
includeFailed bool,
) (connections.Streamer[*pb.GetSwapsStreamResponse], error) {
stream, err := g.apiClient.GetSwapsStream(ctx, &pb.GetSwapsStreamRequest{
Projects: projects,
Pools: markets,
IncludeFailed: includeFailed,
})
if err != nil {
return nil, err
}
return connections.GRPCStream[pb.GetSwapsStreamResponse](stream, ""), nil
}
// GetNewRaydiumPoolsStream subscribes to a stream for getting recent swaps on projects & markets of interest.
func (g *GRPCClient) GetNewRaydiumPoolsStream(
ctx context.Context,
) (connections.Streamer[*pb.GetNewRaydiumPoolsResponse], error) {
stream, err := g.apiClient.GetNewRaydiumPoolsStream(ctx, &pb.GetNewRaydiumPoolsRequest{})
if err != nil {
return nil, err
}
return connections.GRPCStream[pb.GetNewRaydiumPoolsResponse](stream, ""), nil
}
// GetBlockStream subscribes to a stream for getting recent blocks.
func (g *GRPCClient) GetBlockStream(ctx context.Context) (connections.Streamer[*pb.GetBlockStreamResponse], error) {
stream, err := g.apiClient.GetBlockStream(ctx, &pb.GetBlockStreamRequest{})
if err != nil {
return nil, err
}
return connections.GRPCStream[pb.GetBlockStreamResponse](stream, ""), nil
}
// GetPriorityFeeStream subscribes to a stream of priority fees for a given percentile
func (g *GRPCClient) GetPriorityFeeStream(ctx context.Context, percentile *float64) (connections.Streamer[*pb.GetPriorityFeeResponse], error) {
request := &pb.GetPriorityFeeRequest{}
if percentile != nil {
request.Percentile = percentile
}
stream, err := g.apiClient.GetPriorityFeeStream(ctx, request)
if err != nil {
return nil, err
}
return connections.GRPCStream[pb.GetPriorityFeeResponse](stream, fmt.Sprint(percentile)), nil
}
// V2 Openbook
// GetOrderbookV2 returns the requested market's orderbook (e.g. asks and bids). Set limit to 0 for all bids / asks.
func (g *GRPCClient) GetOrderbookV2(ctx context.Context, market string, limit uint32) (*pb.GetOrderbookResponseV2, error) {
return g.apiClient.GetOrderbookV2(ctx, &pb.GetOrderbookRequestV2{Market: market, Limit: limit})
}
// GetMarketDepthV2 returns the requested market's coalesced price data (e.g. asks and bids). Set limit to 0 for all bids / asks.
func (g *GRPCClient) GetMarketDepthV2(ctx context.Context, market string, limit uint32) (*pb.GetMarketDepthResponseV2, error) {
return g.apiClient.GetMarketDepthV2(ctx, &pb.GetMarketDepthRequestV2{Market: market, Limit: limit})
}
// GetTickersV2 returns the requested market tickets. Set market to "" for all markets.
func (g *GRPCClient) GetTickersV2(ctx context.Context, market string) (*pb.GetTickersResponseV2, error) {
return g.apiClient.GetTickersV2(ctx, &pb.GetTickersRequestV2{Market: market})
}
// GetOpenOrdersV2 returns all open orders by owner address and market
func (g *GRPCClient) GetOpenOrdersV2(ctx context.Context, market string, owner string, openOrdersAddress string, orderID string, clientOrderID uint64) (*pb.GetOpenOrdersResponseV2, error) {
return g.apiClient.GetOpenOrdersV2(ctx, &pb.GetOpenOrdersRequestV2{Market: market, Address: owner, OpenOrdersAddress: openOrdersAddress, OrderID: orderID, ClientOrderID: clientOrderID})
}
// GetUnsettledV2 returns all OpenOrders accounts for a given market with the amounts of unsettled funds
func (g *GRPCClient) GetUnsettledV2(ctx context.Context, market string, ownerAddress string) (*pb.GetUnsettledResponse, error) {
return g.apiClient.GetUnsettledV2(ctx, &pb.GetUnsettledRequestV2{Market: market, OwnerAddress: ownerAddress})
}
// GetMarketsV2 returns the list of all available named markets
func (g *GRPCClient) GetMarketsV2(ctx context.Context) (*pb.GetMarketsResponseV2, error) {
return g.apiClient.GetMarketsV2(ctx, &pb.GetMarketsRequestV2{})
}
// PostOrderV2 returns a partially signed transaction for placing a Serum market order. Typically, you want to use SubmitOrder instead of this.
func (g *GRPCClient) PostOrderV2(ctx context.Context, owner, payer, market string, side string, orderType string, amount, price float64, bundleTip *uint64, opts PostOrderOpts) (*pb.PostOrderResponse, error) {
return g.apiClient.PostOrderV2(ctx, &pb.PostOrderRequestV2{
OwnerAddress: owner,
PayerAddress: payer,
Market: market,
Side: side,
Type: orderType,
Amount: amount,
Price: price,
Tip: bundleTip,
OpenOrdersAddress: opts.OpenOrdersAddress,
ClientOrderID: opts.ClientOrderID,
})
}
// PostOrderV2WithPriorityFee returns a partially signed transaction for placing a Serum market order. Typically, you want to use SubmitOrder instead of this.
func (g *GRPCClient) PostOrderV2WithPriorityFee(ctx context.Context, owner, payer, market string, side string,
orderType string, amount, price float64, computeLimit uint32, computePrice uint64, bundleTip *uint64, opts PostOrderOpts) (*pb.PostOrderResponse, error) {
return g.apiClient.PostOrderV2(ctx, &pb.PostOrderRequestV2{
OwnerAddress: owner,
PayerAddress: payer,
Market: market,
Side: side,
Type: orderType,
Amount: amount,
Price: price,
OpenOrdersAddress: opts.OpenOrdersAddress,
ComputeLimit: computeLimit,
ComputePrice: computePrice,
Tip: bundleTip,
ClientOrderID: opts.ClientOrderID,
})
}
// SubmitOrderV2 builds a Serum market order, signs it, and submits to the network.
func (g *GRPCClient) SubmitOrderV2(ctx context.Context, owner, payer, market string, side string, orderType string, amount, price float64, bundleTip *uint64, opts PostOrderOpts) (string, error) {
order, err := g.PostOrderV2(ctx, owner, payer, market, side, orderType, amount, price, bundleTip, opts)
if err != nil {
return "", err
}
skipPreFlight := true
if opts.SkipPreFlight != nil {
skipPreFlight = *opts.SkipPreFlight
}
return g.SignAndSubmit(ctx, order.Transaction, skipPreFlight, false)
}
// SubmitOrderV2WithPriorityFee builds a Serum market order, signs it, and submits to the network with specified computeLimit and computePrice
func (g *GRPCClient) SubmitOrderV2WithPriorityFee(ctx context.Context, owner, payer, market string, side string,
orderType string, amount, price float64, computeLimit uint32, computePrice uint64, bundleTip *uint64, opts PostOrderOpts) (string, error) {
order, err := g.PostOrderV2WithPriorityFee(ctx, owner, payer, market, side, orderType, amount, price, computeLimit, computePrice, bundleTip, opts)
if err != nil {
return "", err
}
skipPreFlight := true
if opts.SkipPreFlight != nil {
skipPreFlight = *opts.SkipPreFlight
}
return g.SignAndSubmit(ctx, order.Transaction, skipPreFlight, false)
}
// PostCancelOrderV2 builds a Serum cancel order.
func (g *GRPCClient) PostCancelOrderV2(
ctx context.Context,
orderID string,
clientOrderID uint64,
side string,
owner,
market,
openOrders string,
) (*pb.PostCancelOrderResponseV2, error) {
return g.apiClient.PostCancelOrderV2(ctx, &pb.PostCancelOrderRequestV2{
OrderID: orderID,
Side: side,
OwnerAddress: owner,
MarketAddress: market,
OpenOrdersAddress: openOrders,
ClientOrderID: clientOrderID,
})
}
// SubmitCancelOrderV2 builds a Serum cancel order, signs and submits it to the network.
func (g *GRPCClient) SubmitCancelOrderV2(
ctx context.Context,
orderID string,
clientOrderID uint64,
side string,
owner,
market,
openOrders string,
opts SubmitOpts,
) (*pb.PostSubmitBatchResponse, error) {
order, err := g.PostCancelOrderV2(ctx, orderID, clientOrderID, side, owner, market, openOrders)
if err != nil {
return nil, err
}
return g.signAndSubmitBatch(ctx, order.Transactions, false, opts)
}
// PostSettleV2 returns a partially signed transaction for settling market funds. Typically, you want to use SubmitSettle instead of this.
func (g *GRPCClient) PostSettleV2(ctx context.Context, owner, market, baseTokenWallet, quoteTokenWallet, openOrdersAccount string) (*pb.PostSettleResponse, error) {
return g.apiClient.PostSettleV2(ctx, &pb.PostSettleRequestV2{
OwnerAddress: owner,
Market: market,
BaseTokenWallet: baseTokenWallet,
QuoteTokenWallet: quoteTokenWallet,
OpenOrdersAddress: openOrdersAccount,
})
}
// SubmitSettleV2 builds a market SubmitSettle transaction, signs it, and submits to the network.
func (g *GRPCClient) SubmitSettleV2(ctx context.Context, owner, market, baseTokenWallet, quoteTokenWallet, openOrdersAccount string, skipPreflight bool) (string, error) {
order, err := g.PostSettleV2(ctx, owner, market, baseTokenWallet, quoteTokenWallet, openOrdersAccount)
if err != nil {
return "", err
}
return g.SignAndSubmit(ctx, order.Transaction, skipPreflight, false)
}
func (g *GRPCClient) PostReplaceOrderV2(ctx context.Context, orderID, owner, payer, market string, side string, orderType string, amount, price float64, opts PostOrderOpts) (*pb.PostOrderResponse, error) {
return g.apiClient.PostReplaceOrderV2(ctx, &pb.PostReplaceOrderRequestV2{
OwnerAddress: owner,
PayerAddress: payer,
Market: market,
Side: side,
Type: orderType,
Amount: amount,
Price: price,
OpenOrdersAddress: opts.OpenOrdersAddress,
ClientOrderID: opts.ClientOrderID,
OrderID: orderID,
})
}
func (g *GRPCClient) SubmitReplaceOrderV2(ctx context.Context, orderID, owner, payer, market string, side string, orderType string, amount, price float64, opts PostOrderOpts) (string, error) {
order, err := g.PostReplaceOrderV2(ctx, orderID, owner, payer, market, side, orderType, amount, price, opts)
if err != nil {
return "", err
}
skipPreFlight := true
if opts.SkipPreFlight != nil {
skipPreFlight = *opts.SkipPreFlight
}
return g.SignAndSubmit(ctx, order.Transaction, skipPreFlight, false)
}