github.com/InjectiveLabs/sdk-go@v1.53.0/client/exchange/exchange.go (about) 1 package exchange 2 3 import ( 4 "context" 5 "fmt" 6 7 "google.golang.org/grpc/credentials/insecure" 8 9 "github.com/InjectiveLabs/sdk-go/client/common" 10 accountPB "github.com/InjectiveLabs/sdk-go/exchange/accounts_rpc/pb" 11 auctionPB "github.com/InjectiveLabs/sdk-go/exchange/auction_rpc/pb" 12 derivativeExchangePB "github.com/InjectiveLabs/sdk-go/exchange/derivative_exchange_rpc/pb" 13 insurancePB "github.com/InjectiveLabs/sdk-go/exchange/insurance_rpc/pb" 14 metaPB "github.com/InjectiveLabs/sdk-go/exchange/meta_rpc/pb" 15 oraclePB "github.com/InjectiveLabs/sdk-go/exchange/oracle_rpc/pb" 16 portfolioExchangePB "github.com/InjectiveLabs/sdk-go/exchange/portfolio_rpc/pb" 17 spotExchangePB "github.com/InjectiveLabs/sdk-go/exchange/spot_exchange_rpc/pb" 18 log "github.com/InjectiveLabs/suplog" 19 "github.com/pkg/errors" 20 "google.golang.org/grpc" 21 ) 22 23 type ExchangeClient interface { 24 QueryClient() *grpc.ClientConn 25 GetDerivativeMarket(ctx context.Context, marketId string) (*derivativeExchangePB.MarketResponse, error) 26 GetDerivativeOrderbookV2(ctx context.Context, marketId string) (*derivativeExchangePB.OrderbookV2Response, error) 27 GetDerivativeOrderbooksV2(ctx context.Context, marketIDs []string) (*derivativeExchangePB.OrderbooksV2Response, error) 28 // StreamDerivativeOrderbook deprecated API 29 StreamDerivativeOrderbookV2(ctx context.Context, marketIDs []string) (derivativeExchangePB.InjectiveDerivativeExchangeRPC_StreamOrderbookV2Client, error) 30 StreamDerivativeOrderbookUpdate(ctx context.Context, marketIDs []string) (derivativeExchangePB.InjectiveDerivativeExchangeRPC_StreamOrderbookUpdateClient, error) 31 StreamDerivativeMarket(ctx context.Context, marketIDs []string) (derivativeExchangePB.InjectiveDerivativeExchangeRPC_StreamMarketClient, error) 32 GetDerivativeOrders(ctx context.Context, req *derivativeExchangePB.OrdersRequest) (*derivativeExchangePB.OrdersResponse, error) 33 GetDerivativeMarkets(ctx context.Context, req *derivativeExchangePB.MarketsRequest) (*derivativeExchangePB.MarketsResponse, error) 34 GetDerivativePositions(ctx context.Context, req *derivativeExchangePB.PositionsRequest) (*derivativeExchangePB.PositionsResponse, error) 35 GetDerivativePositionsV2(ctx context.Context, req *derivativeExchangePB.PositionsV2Request) (*derivativeExchangePB.PositionsV2Response, error) 36 GetDerivativeLiquidablePositions(ctx context.Context, req *derivativeExchangePB.LiquidablePositionsRequest) (*derivativeExchangePB.LiquidablePositionsResponse, error) 37 StreamDerivativePositions(ctx context.Context, req *derivativeExchangePB.StreamPositionsRequest) (derivativeExchangePB.InjectiveDerivativeExchangeRPC_StreamPositionsClient, error) 38 StreamDerivativeOrders(ctx context.Context, req *derivativeExchangePB.StreamOrdersRequest) (derivativeExchangePB.InjectiveDerivativeExchangeRPC_StreamOrdersClient, error) 39 GetDerivativeTrades(ctx context.Context, req *derivativeExchangePB.TradesRequest) (*derivativeExchangePB.TradesResponse, error) 40 GetDerivativeTradesV2(ctx context.Context, req *derivativeExchangePB.TradesV2Request) (*derivativeExchangePB.TradesV2Response, error) 41 StreamDerivativeTrades(ctx context.Context, req *derivativeExchangePB.StreamTradesRequest) (derivativeExchangePB.InjectiveDerivativeExchangeRPC_StreamTradesClient, error) 42 StreamDerivativeV2Trades(ctx context.Context, req *derivativeExchangePB.StreamTradesV2Request) (derivativeExchangePB.InjectiveDerivativeExchangeRPC_StreamTradesV2Client, error) 43 GetSubaccountDerivativeOrdersList(ctx context.Context, req *derivativeExchangePB.SubaccountOrdersListRequest) (*derivativeExchangePB.SubaccountOrdersListResponse, error) 44 GetSubaccountDerivativeTradesList(ctx context.Context, req *derivativeExchangePB.SubaccountTradesListRequest) (*derivativeExchangePB.SubaccountTradesListResponse, error) 45 GetHistoricalDerivativeOrders(ctx context.Context, req *derivativeExchangePB.OrdersHistoryRequest) (*derivativeExchangePB.OrdersHistoryResponse, error) 46 StreamHistoricalDerivativeOrders(ctx context.Context, req *derivativeExchangePB.StreamOrdersHistoryRequest) (derivativeExchangePB.InjectiveDerivativeExchangeRPC_StreamOrdersHistoryClient, error) 47 GetDerivativeFundingPayments(ctx context.Context, req *derivativeExchangePB.FundingPaymentsRequest) (*derivativeExchangePB.FundingPaymentsResponse, error) 48 GetDerivativeFundingRates(ctx context.Context, req *derivativeExchangePB.FundingRatesRequest) (*derivativeExchangePB.FundingRatesResponse, error) 49 GetPrice(ctx context.Context, baseSymbol string, quoteSymbol string, oracleType string, oracleScaleFactor uint32) (*oraclePB.PriceResponse, error) 50 GetOracleList(ctx context.Context) (*oraclePB.OracleListResponse, error) 51 StreamPrices(ctx context.Context, baseSymbol string, quoteSymbol string, oracleType string) (oraclePB.InjectiveOracleRPC_StreamPricesClient, error) 52 GetAuction(ctx context.Context, round int64) (*auctionPB.AuctionEndpointResponse, error) 53 GetAuctions(ctx context.Context) (*auctionPB.AuctionsResponse, error) 54 StreamBids(ctx context.Context) (auctionPB.InjectiveAuctionRPC_StreamBidsClient, error) 55 GetSubaccountsList(ctx context.Context, accountAddress string) (*accountPB.SubaccountsListResponse, error) 56 GetSubaccountBalance(ctx context.Context, subaccountId string, denom string) (*accountPB.SubaccountBalanceEndpointResponse, error) 57 StreamSubaccountBalance(ctx context.Context, subaccountId string) (accountPB.InjectiveAccountsRPC_StreamSubaccountBalanceClient, error) 58 GetSubaccountBalancesList(ctx context.Context, subaccountId string) (*accountPB.SubaccountBalancesListResponse, error) 59 GetSubaccountHistory(ctx context.Context, req *accountPB.SubaccountHistoryRequest) (*accountPB.SubaccountHistoryResponse, error) 60 GetSubaccountOrderSummary(ctx context.Context, req *accountPB.SubaccountOrderSummaryRequest) (*accountPB.SubaccountOrderSummaryResponse, error) 61 GetOrderStates(ctx context.Context, req *accountPB.OrderStatesRequest) (*accountPB.OrderStatesResponse, error) 62 GetPortfolio(ctx context.Context, accountAddress string) (*accountPB.PortfolioResponse, error) 63 GetRewards(ctx context.Context, req *accountPB.RewardsRequest) (*accountPB.RewardsResponse, error) 64 GetSpotOrders(ctx context.Context, req *spotExchangePB.OrdersRequest) (*spotExchangePB.OrdersResponse, error) 65 GetSpotOrderbookV2(ctx context.Context, marketId string) (*spotExchangePB.OrderbookV2Response, error) 66 GetSpotOrderbooksV2(ctx context.Context, marketIDs []string) (*spotExchangePB.OrderbooksV2Response, error) 67 // StreamSpotOrderbook deprecated API 68 StreamSpotOrderbookV2(ctx context.Context, marketIDs []string) (spotExchangePB.InjectiveSpotExchangeRPC_StreamOrderbookV2Client, error) 69 StreamSpotOrderbookUpdate(ctx context.Context, marketIDs []string) (spotExchangePB.InjectiveSpotExchangeRPC_StreamOrderbookUpdateClient, error) 70 GetSpotMarkets(ctx context.Context, req *spotExchangePB.MarketsRequest) (*spotExchangePB.MarketsResponse, error) 71 GetSpotMarket(ctx context.Context, marketId string) (*spotExchangePB.MarketResponse, error) 72 StreamSpotMarket(ctx context.Context, marketIDs []string) (spotExchangePB.InjectiveSpotExchangeRPC_StreamMarketsClient, error) 73 StreamSpotOrders(ctx context.Context, req *spotExchangePB.StreamOrdersRequest) (spotExchangePB.InjectiveSpotExchangeRPC_StreamOrdersClient, error) 74 GetSpotTrades(ctx context.Context, req *spotExchangePB.TradesRequest) (*spotExchangePB.TradesResponse, error) 75 GetSpotTradesV2(ctx context.Context, req *spotExchangePB.TradesV2Request) (*spotExchangePB.TradesV2Response, error) 76 StreamSpotTrades(ctx context.Context, req *spotExchangePB.StreamTradesRequest) (spotExchangePB.InjectiveSpotExchangeRPC_StreamTradesClient, error) 77 StreamSpotTradesV2(ctx context.Context, req *spotExchangePB.StreamTradesV2Request) (spotExchangePB.InjectiveSpotExchangeRPC_StreamTradesV2Client, error) 78 GetSubaccountSpotOrdersList(ctx context.Context, req *spotExchangePB.SubaccountOrdersListRequest) (*spotExchangePB.SubaccountOrdersListResponse, error) 79 GetSubaccountSpotTradesList(ctx context.Context, req *spotExchangePB.SubaccountTradesListRequest) (*spotExchangePB.SubaccountTradesListResponse, error) 80 GetHistoricalSpotOrders(ctx context.Context, req *spotExchangePB.OrdersHistoryRequest) (*spotExchangePB.OrdersHistoryResponse, error) 81 StreamHistoricalSpotOrders(ctx context.Context, req *spotExchangePB.StreamOrdersHistoryRequest) (spotExchangePB.InjectiveSpotExchangeRPC_StreamOrdersHistoryClient, error) 82 GetInsuranceFunds(ctx context.Context, req *insurancePB.FundsRequest) (*insurancePB.FundsResponse, error) 83 GetRedemptions(ctx context.Context, req *insurancePB.RedemptionsRequest) (*insurancePB.RedemptionsResponse, error) 84 85 GetAccountPortfolio(ctx context.Context, accountAddress string) (*portfolioExchangePB.AccountPortfolioResponse, error) 86 GetAccountPortfolioBalances(ctx context.Context, accountAddress string) (*portfolioExchangePB.AccountPortfolioBalancesResponse, error) 87 StreamAccountPortfolio(ctx context.Context, accountAddress string, subaccountId, balanceType string) (portfolioExchangePB.InjectivePortfolioRPC_StreamAccountPortfolioClient, error) 88 89 StreamKeepalive(ctx context.Context) (metaPB.InjectiveMetaRPC_StreamKeepaliveClient, error) 90 GetInfo(ctx context.Context, req *metaPB.InfoRequest) (*metaPB.InfoResponse, error) 91 GetVersion(ctx context.Context, req *metaPB.VersionRequest) (*metaPB.VersionResponse, error) 92 Ping(ctx context.Context, req *metaPB.PingRequest) (*metaPB.PingResponse, error) 93 GetNetwork() common.Network 94 Close() 95 } 96 97 func NewExchangeClient(network common.Network, options ...common.ClientOption) (ExchangeClient, error) { 98 // process options 99 opts := common.DefaultClientOptions() 100 if network.ChainTLSCert != nil { 101 options = append(options, common.OptionTLSCert(network.ExchangeTLSCert)) 102 } 103 for _, opt := range options { 104 if err := opt(opts); err != nil { 105 err = errors.Wrap(err, "error in client option") 106 return nil, err 107 } 108 } 109 110 // create grpc client 111 var conn *grpc.ClientConn 112 var err error 113 if opts.TLSCert != nil { 114 conn, err = grpc.Dial(network.ExchangeGrpcEndpoint, grpc.WithTransportCredentials(opts.TLSCert), grpc.WithContextDialer(common.DialerFunc)) 115 } else { 116 117 conn, err = grpc.Dial(network.ExchangeGrpcEndpoint, grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithContextDialer(common.DialerFunc)) 118 } 119 if err != nil { 120 err := errors.Wrapf(err, "failed to connect to the gRPC: %s", network.ExchangeGrpcEndpoint) 121 return nil, err 122 } 123 124 // build client 125 cc := &exchangeClient{ 126 opts: opts, 127 network: network, 128 conn: conn, 129 130 metaClient: metaPB.NewInjectiveMetaRPCClient(conn), 131 accountClient: accountPB.NewInjectiveAccountsRPCClient(conn), 132 auctionClient: auctionPB.NewInjectiveAuctionRPCClient(conn), 133 oracleClient: oraclePB.NewInjectiveOracleRPCClient(conn), 134 insuranceClient: insurancePB.NewInjectiveInsuranceRPCClient(conn), 135 spotExchangeClient: spotExchangePB.NewInjectiveSpotExchangeRPCClient(conn), 136 derivativeExchangeClient: derivativeExchangePB.NewInjectiveDerivativeExchangeRPCClient(conn), 137 portfolioExchangeClient: portfolioExchangePB.NewInjectivePortfolioRPCClient(conn), 138 139 logger: log.WithFields(log.Fields{ 140 "module": "sdk-go", 141 "svc": "exchangeClient", 142 }), 143 } 144 145 return cc, nil 146 } 147 148 type exchangeClient struct { 149 opts *common.ClientOptions 150 network common.Network 151 conn *grpc.ClientConn 152 logger log.Logger 153 154 metaClient metaPB.InjectiveMetaRPCClient 155 accountClient accountPB.InjectiveAccountsRPCClient 156 auctionClient auctionPB.InjectiveAuctionRPCClient 157 oracleClient oraclePB.InjectiveOracleRPCClient 158 insuranceClient insurancePB.InjectiveInsuranceRPCClient 159 spotExchangeClient spotExchangePB.InjectiveSpotExchangeRPCClient 160 derivativeExchangeClient derivativeExchangePB.InjectiveDerivativeExchangeRPCClient 161 portfolioExchangeClient portfolioExchangePB.InjectivePortfolioRPCClient 162 } 163 164 func (c *exchangeClient) QueryClient() *grpc.ClientConn { 165 return c.conn 166 } 167 168 // Derivatives RPC 169 170 func (c *exchangeClient) GetDerivativeOrders(ctx context.Context, req *derivativeExchangePB.OrdersRequest) (*derivativeExchangePB.OrdersResponse, error) { 171 res, err := common.ExecuteCall(ctx, c.network.ExchangeCookieAssistant, c.derivativeExchangeClient.Orders, req) 172 if err != nil { 173 fmt.Println(err) 174 return &derivativeExchangePB.OrdersResponse{}, err 175 } 176 177 return res, nil 178 } 179 180 // Deprecated: Use GetDerivativePositionsV2 instead. 181 func (c *exchangeClient) GetDerivativePositions(ctx context.Context, req *derivativeExchangePB.PositionsRequest) (*derivativeExchangePB.PositionsResponse, error) { 182 res, err := common.ExecuteCall(ctx, c.network.ExchangeCookieAssistant, c.derivativeExchangeClient.Positions, req) 183 if err != nil { 184 fmt.Println(err) 185 return &derivativeExchangePB.PositionsResponse{}, err 186 } 187 188 return res, nil 189 } 190 191 func (c *exchangeClient) GetDerivativePositionsV2(ctx context.Context, req *derivativeExchangePB.PositionsV2Request) (*derivativeExchangePB.PositionsV2Response, error) { 192 res, err := common.ExecuteCall(ctx, c.network.ExchangeCookieAssistant, c.derivativeExchangeClient.PositionsV2, req) 193 if err != nil { 194 fmt.Println(err) 195 return &derivativeExchangePB.PositionsV2Response{}, err 196 } 197 198 return res, nil 199 } 200 201 func (c *exchangeClient) GetDerivativeLiquidablePositions(ctx context.Context, req *derivativeExchangePB.LiquidablePositionsRequest) (*derivativeExchangePB.LiquidablePositionsResponse, error) { 202 res, err := common.ExecuteCall(ctx, c.network.ExchangeCookieAssistant, c.derivativeExchangeClient.LiquidablePositions, req) 203 if err != nil { 204 fmt.Println(err) 205 return &derivativeExchangePB.LiquidablePositionsResponse{}, err 206 } 207 208 return res, nil 209 } 210 211 func (c *exchangeClient) GetDerivativeOrderbookV2(ctx context.Context, marketId string) (*derivativeExchangePB.OrderbookV2Response, error) { 212 req := derivativeExchangePB.OrderbookV2Request{ 213 MarketId: marketId, 214 } 215 216 res, err := common.ExecuteCall(ctx, c.network.ExchangeCookieAssistant, c.derivativeExchangeClient.OrderbookV2, &req) 217 if err != nil { 218 fmt.Println(err) 219 return &derivativeExchangePB.OrderbookV2Response{}, err 220 } 221 222 return res, nil 223 } 224 225 func (c *exchangeClient) GetDerivativeOrderbooksV2(ctx context.Context, marketIDs []string) (*derivativeExchangePB.OrderbooksV2Response, error) { 226 req := derivativeExchangePB.OrderbooksV2Request{ 227 MarketIds: marketIDs, 228 } 229 230 res, err := common.ExecuteCall(ctx, c.network.ExchangeCookieAssistant, c.derivativeExchangeClient.OrderbooksV2, &req) 231 if err != nil { 232 fmt.Println(err) 233 return &derivativeExchangePB.OrderbooksV2Response{}, err 234 } 235 236 return res, nil 237 } 238 239 func (c *exchangeClient) StreamDerivativeOrderbookV2(ctx context.Context, marketIDs []string) (derivativeExchangePB.InjectiveDerivativeExchangeRPC_StreamOrderbookV2Client, error) { 240 req := derivativeExchangePB.StreamOrderbookV2Request{ 241 MarketIds: marketIDs, 242 } 243 244 stream, err := common.ExecuteStreamCall(ctx, c.network.ExchangeCookieAssistant, c.derivativeExchangeClient.StreamOrderbookV2, &req) 245 246 if err != nil { 247 fmt.Println(err) 248 return nil, err 249 } 250 251 return stream, nil 252 } 253 254 func (c *exchangeClient) StreamDerivativeOrderbookUpdate(ctx context.Context, marketIDs []string) (derivativeExchangePB.InjectiveDerivativeExchangeRPC_StreamOrderbookUpdateClient, error) { 255 req := derivativeExchangePB.StreamOrderbookUpdateRequest{ 256 MarketIds: marketIDs, 257 } 258 259 stream, err := common.ExecuteStreamCall(ctx, c.network.ExchangeCookieAssistant, c.derivativeExchangeClient.StreamOrderbookUpdate, &req) 260 261 if err != nil { 262 fmt.Println(err) 263 return nil, err 264 } 265 266 return stream, nil 267 } 268 269 func (c *exchangeClient) GetDerivativeMarkets(ctx context.Context, req *derivativeExchangePB.MarketsRequest) (*derivativeExchangePB.MarketsResponse, error) { 270 res, err := common.ExecuteCall(ctx, c.network.ExchangeCookieAssistant, c.derivativeExchangeClient.Markets, req) 271 if err != nil { 272 fmt.Println(err) 273 return &derivativeExchangePB.MarketsResponse{}, err 274 } 275 276 return res, nil 277 } 278 279 func (c *exchangeClient) GetDerivativeMarket(ctx context.Context, marketId string) (*derivativeExchangePB.MarketResponse, error) { 280 req := derivativeExchangePB.MarketRequest{ 281 MarketId: marketId, 282 } 283 284 res, err := common.ExecuteCall(ctx, c.network.ExchangeCookieAssistant, c.derivativeExchangeClient.Market, &req) 285 if err != nil { 286 fmt.Println(err) 287 return &derivativeExchangePB.MarketResponse{}, err 288 } 289 290 return res, nil 291 } 292 293 func (c *exchangeClient) StreamDerivativeMarket(ctx context.Context, marketIDs []string) (derivativeExchangePB.InjectiveDerivativeExchangeRPC_StreamMarketClient, error) { 294 req := derivativeExchangePB.StreamMarketRequest{ 295 MarketIds: marketIDs, 296 } 297 298 stream, err := common.ExecuteStreamCall(ctx, c.network.ExchangeCookieAssistant, c.derivativeExchangeClient.StreamMarket, &req) 299 300 if err != nil { 301 fmt.Println(err) 302 return nil, err 303 } 304 305 return stream, nil 306 } 307 308 func (c *exchangeClient) StreamDerivativePositions(ctx context.Context, req *derivativeExchangePB.StreamPositionsRequest) (derivativeExchangePB.InjectiveDerivativeExchangeRPC_StreamPositionsClient, error) { 309 stream, err := common.ExecuteStreamCall(ctx, c.network.ExchangeCookieAssistant, c.derivativeExchangeClient.StreamPositions, req) 310 311 if err != nil { 312 fmt.Println(err) 313 return nil, err 314 } 315 316 return stream, nil 317 } 318 319 func (c *exchangeClient) StreamDerivativeOrders(ctx context.Context, req *derivativeExchangePB.StreamOrdersRequest) (derivativeExchangePB.InjectiveDerivativeExchangeRPC_StreamOrdersClient, error) { 320 stream, err := common.ExecuteStreamCall(ctx, c.network.ExchangeCookieAssistant, c.derivativeExchangeClient.StreamOrders, req) 321 322 if err != nil { 323 fmt.Println(err) 324 return nil, err 325 } 326 327 return stream, nil 328 } 329 330 func (c *exchangeClient) GetDerivativeTrades(ctx context.Context, req *derivativeExchangePB.TradesRequest) (*derivativeExchangePB.TradesResponse, error) { 331 res, err := common.ExecuteCall(ctx, c.network.ExchangeCookieAssistant, c.derivativeExchangeClient.Trades, req) 332 if err != nil { 333 fmt.Println(err) 334 return &derivativeExchangePB.TradesResponse{}, err 335 } 336 337 return res, nil 338 } 339 340 func (c *exchangeClient) GetDerivativeTradesV2(ctx context.Context, req *derivativeExchangePB.TradesV2Request) (*derivativeExchangePB.TradesV2Response, error) { 341 res, err := common.ExecuteCall(ctx, c.network.ExchangeCookieAssistant, c.derivativeExchangeClient.TradesV2, req) 342 if err != nil { 343 fmt.Println(err) 344 return &derivativeExchangePB.TradesV2Response{}, err 345 } 346 347 return res, nil 348 } 349 350 func (c *exchangeClient) StreamDerivativeTrades(ctx context.Context, req *derivativeExchangePB.StreamTradesRequest) (derivativeExchangePB.InjectiveDerivativeExchangeRPC_StreamTradesClient, error) { 351 stream, err := common.ExecuteStreamCall(ctx, c.network.ExchangeCookieAssistant, c.derivativeExchangeClient.StreamTrades, req) 352 353 if err != nil { 354 fmt.Println(err) 355 return nil, err 356 } 357 358 return stream, nil 359 } 360 361 func (c *exchangeClient) StreamDerivativeV2Trades(ctx context.Context, req *derivativeExchangePB.StreamTradesV2Request) (derivativeExchangePB.InjectiveDerivativeExchangeRPC_StreamTradesV2Client, error) { 362 stream, err := common.ExecuteStreamCall(ctx, c.network.ExchangeCookieAssistant, c.derivativeExchangeClient.StreamTradesV2, req) 363 364 if err != nil { 365 fmt.Println(err) 366 return nil, err 367 } 368 369 return stream, nil 370 } 371 372 func (c *exchangeClient) GetSubaccountDerivativeOrdersList(ctx context.Context, req *derivativeExchangePB.SubaccountOrdersListRequest) (*derivativeExchangePB.SubaccountOrdersListResponse, error) { 373 res, err := common.ExecuteCall(ctx, c.network.ExchangeCookieAssistant, c.derivativeExchangeClient.SubaccountOrdersList, req) 374 375 if err != nil { 376 fmt.Println(err) 377 return &derivativeExchangePB.SubaccountOrdersListResponse{}, err 378 } 379 380 return res, nil 381 } 382 383 func (c *exchangeClient) GetSubaccountDerivativeTradesList(ctx context.Context, req *derivativeExchangePB.SubaccountTradesListRequest) (*derivativeExchangePB.SubaccountTradesListResponse, error) { 384 res, err := common.ExecuteCall(ctx, c.network.ExchangeCookieAssistant, c.derivativeExchangeClient.SubaccountTradesList, req) 385 386 if err != nil { 387 fmt.Println(err) 388 return &derivativeExchangePB.SubaccountTradesListResponse{}, err 389 } 390 391 return res, nil 392 } 393 394 func (c *exchangeClient) GetHistoricalDerivativeOrders(ctx context.Context, req *derivativeExchangePB.OrdersHistoryRequest) (*derivativeExchangePB.OrdersHistoryResponse, error) { 395 res, err := common.ExecuteCall(ctx, c.network.ExchangeCookieAssistant, c.derivativeExchangeClient.OrdersHistory, req) 396 if err != nil { 397 return &derivativeExchangePB.OrdersHistoryResponse{}, err 398 } 399 400 return res, nil 401 } 402 403 func (c *exchangeClient) StreamHistoricalDerivativeOrders(ctx context.Context, req *derivativeExchangePB.StreamOrdersHistoryRequest) (derivativeExchangePB.InjectiveDerivativeExchangeRPC_StreamOrdersHistoryClient, error) { 404 stream, err := common.ExecuteStreamCall(ctx, c.network.ExchangeCookieAssistant, c.derivativeExchangeClient.StreamOrdersHistory, req) 405 406 if err != nil { 407 fmt.Println(err) 408 return nil, err 409 } 410 411 return stream, nil 412 } 413 414 func (c *exchangeClient) GetDerivativeFundingPayments(ctx context.Context, req *derivativeExchangePB.FundingPaymentsRequest) (*derivativeExchangePB.FundingPaymentsResponse, error) { 415 res, err := common.ExecuteCall(ctx, c.network.ExchangeCookieAssistant, c.derivativeExchangeClient.FundingPayments, req) 416 if err != nil { 417 fmt.Println(err) 418 return &derivativeExchangePB.FundingPaymentsResponse{}, err 419 } 420 421 return res, nil 422 } 423 424 func (c *exchangeClient) GetDerivativeFundingRates(ctx context.Context, req *derivativeExchangePB.FundingRatesRequest) (*derivativeExchangePB.FundingRatesResponse, error) { 425 res, err := common.ExecuteCall(ctx, c.network.ExchangeCookieAssistant, c.derivativeExchangeClient.FundingRates, req) 426 if err != nil { 427 fmt.Println(err) 428 return &derivativeExchangePB.FundingRatesResponse{}, err 429 } 430 431 return res, nil 432 } 433 434 // Oracle RPC 435 436 func (c *exchangeClient) GetPrice(ctx context.Context, baseSymbol, quoteSymbol, oracleType string, oracleScaleFactor uint32) (*oraclePB.PriceResponse, error) { 437 req := oraclePB.PriceRequest{ 438 BaseSymbol: baseSymbol, 439 QuoteSymbol: quoteSymbol, 440 OracleType: oracleType, 441 OracleScaleFactor: oracleScaleFactor, 442 } 443 444 res, err := common.ExecuteCall(ctx, c.network.ExchangeCookieAssistant, c.oracleClient.Price, &req) 445 446 if err != nil { 447 fmt.Println(err) 448 return &oraclePB.PriceResponse{}, err 449 } 450 451 return res, nil 452 } 453 454 func (c *exchangeClient) GetOracleList(ctx context.Context) (*oraclePB.OracleListResponse, error) { 455 req := oraclePB.OracleListRequest{} 456 457 res, err := common.ExecuteCall(ctx, c.network.ExchangeCookieAssistant, c.oracleClient.OracleList, &req) 458 if err != nil { 459 fmt.Println(err) 460 return &oraclePB.OracleListResponse{}, err 461 } 462 463 return res, nil 464 } 465 466 func (c *exchangeClient) StreamPrices(ctx context.Context, baseSymbol, quoteSymbol, oracleType string) (oraclePB.InjectiveOracleRPC_StreamPricesClient, error) { 467 req := oraclePB.StreamPricesRequest{ 468 BaseSymbol: baseSymbol, 469 QuoteSymbol: quoteSymbol, 470 OracleType: oracleType, 471 } 472 473 stream, err := common.ExecuteStreamCall(ctx, c.network.ExchangeCookieAssistant, c.oracleClient.StreamPrices, &req) 474 475 if err != nil { 476 fmt.Println(err) 477 return nil, err 478 } 479 480 return stream, nil 481 } 482 483 // Auction RPC 484 485 func (c *exchangeClient) GetAuction(ctx context.Context, round int64) (*auctionPB.AuctionEndpointResponse, error) { 486 req := auctionPB.AuctionEndpointRequest{ 487 Round: round, 488 } 489 490 res, err := common.ExecuteCall(ctx, c.network.ExchangeCookieAssistant, c.auctionClient.AuctionEndpoint, &req) 491 if err != nil { 492 fmt.Println(err) 493 return &auctionPB.AuctionEndpointResponse{}, err 494 } 495 496 return res, nil 497 } 498 499 func (c *exchangeClient) GetAuctions(ctx context.Context) (*auctionPB.AuctionsResponse, error) { 500 req := auctionPB.AuctionsRequest{} 501 502 res, err := common.ExecuteCall(ctx, c.network.ExchangeCookieAssistant, c.auctionClient.Auctions, &req) 503 if err != nil { 504 fmt.Println(err) 505 return &auctionPB.AuctionsResponse{}, err 506 } 507 508 return res, nil 509 } 510 511 func (c *exchangeClient) StreamBids(ctx context.Context) (auctionPB.InjectiveAuctionRPC_StreamBidsClient, error) { 512 req := auctionPB.StreamBidsRequest{} 513 514 stream, err := common.ExecuteStreamCall(ctx, c.network.ExchangeCookieAssistant, c.auctionClient.StreamBids, &req) 515 516 if err != nil { 517 fmt.Println(err) 518 return nil, err 519 } 520 521 return stream, nil 522 } 523 524 // Accounts RPC 525 526 func (c *exchangeClient) GetSubaccountsList(ctx context.Context, accountAddress string) (*accountPB.SubaccountsListResponse, error) { 527 req := accountPB.SubaccountsListRequest{ 528 AccountAddress: accountAddress, 529 } 530 531 res, err := common.ExecuteCall(ctx, c.network.ExchangeCookieAssistant, c.accountClient.SubaccountsList, &req) 532 533 if err != nil { 534 fmt.Println(err) 535 return &accountPB.SubaccountsListResponse{}, err 536 } 537 538 return res, nil 539 } 540 541 func (c *exchangeClient) GetSubaccountBalance(ctx context.Context, subaccountId, denom string) (*accountPB.SubaccountBalanceEndpointResponse, error) { 542 req := accountPB.SubaccountBalanceEndpointRequest{ 543 SubaccountId: subaccountId, 544 Denom: denom, 545 } 546 547 res, err := common.ExecuteCall(ctx, c.network.ExchangeCookieAssistant, c.accountClient.SubaccountBalanceEndpoint, &req) 548 549 if err != nil { 550 fmt.Println(err) 551 return &accountPB.SubaccountBalanceEndpointResponse{}, err 552 } 553 554 return res, nil 555 } 556 557 func (c *exchangeClient) StreamSubaccountBalance(ctx context.Context, subaccountId string) (accountPB.InjectiveAccountsRPC_StreamSubaccountBalanceClient, error) { 558 req := accountPB.StreamSubaccountBalanceRequest{ 559 SubaccountId: subaccountId, 560 } 561 562 stream, err := common.ExecuteStreamCall(ctx, c.network.ExchangeCookieAssistant, c.accountClient.StreamSubaccountBalance, &req) 563 564 if err != nil { 565 fmt.Println(err) 566 return nil, err 567 } 568 569 return stream, nil 570 } 571 572 func (c *exchangeClient) GetSubaccountBalancesList(ctx context.Context, subaccountId string) (*accountPB.SubaccountBalancesListResponse, error) { 573 req := accountPB.SubaccountBalancesListRequest{ 574 SubaccountId: subaccountId, 575 } 576 577 res, err := common.ExecuteCall(ctx, c.network.ExchangeCookieAssistant, c.accountClient.SubaccountBalancesList, &req) 578 579 if err != nil { 580 fmt.Println(err) 581 return &accountPB.SubaccountBalancesListResponse{}, err 582 } 583 584 return res, nil 585 } 586 587 func (c *exchangeClient) GetSubaccountHistory(ctx context.Context, req *accountPB.SubaccountHistoryRequest) (*accountPB.SubaccountHistoryResponse, error) { 588 res, err := common.ExecuteCall(ctx, c.network.ExchangeCookieAssistant, c.accountClient.SubaccountHistory, req) 589 590 if err != nil { 591 fmt.Println(err) 592 return &accountPB.SubaccountHistoryResponse{}, err 593 } 594 595 return res, nil 596 } 597 598 func (c *exchangeClient) GetSubaccountOrderSummary(ctx context.Context, req *accountPB.SubaccountOrderSummaryRequest) (*accountPB.SubaccountOrderSummaryResponse, error) { 599 res, err := common.ExecuteCall(ctx, c.network.ExchangeCookieAssistant, c.accountClient.SubaccountOrderSummary, req) 600 601 if err != nil { 602 fmt.Println(err) 603 return &accountPB.SubaccountOrderSummaryResponse{}, err 604 } 605 606 return res, nil 607 } 608 609 func (c *exchangeClient) GetOrderStates(ctx context.Context, req *accountPB.OrderStatesRequest) (*accountPB.OrderStatesResponse, error) { 610 res, err := common.ExecuteCall(ctx, c.network.ExchangeCookieAssistant, c.accountClient.OrderStates, req) 611 if err != nil { 612 fmt.Println(err) 613 return &accountPB.OrderStatesResponse{}, err 614 } 615 616 return res, nil 617 } 618 619 func (c *exchangeClient) GetPortfolio(ctx context.Context, accountAddress string) (*accountPB.PortfolioResponse, error) { 620 req := accountPB.PortfolioRequest{ 621 AccountAddress: accountAddress, 622 } 623 624 res, err := common.ExecuteCall(ctx, c.network.ExchangeCookieAssistant, c.accountClient.Portfolio, &req) 625 626 if err != nil { 627 fmt.Println(err) 628 return &accountPB.PortfolioResponse{}, err 629 } 630 631 return res, nil 632 } 633 634 func (c *exchangeClient) GetRewards(ctx context.Context, req *accountPB.RewardsRequest) (*accountPB.RewardsResponse, error) { 635 res, err := common.ExecuteCall(ctx, c.network.ExchangeCookieAssistant, c.accountClient.Rewards, req) 636 637 if err != nil { 638 fmt.Println(err) 639 return &accountPB.RewardsResponse{}, err 640 } 641 642 return res, nil 643 } 644 645 // Spot RPC 646 647 func (c *exchangeClient) GetSpotOrders(ctx context.Context, req *spotExchangePB.OrdersRequest) (*spotExchangePB.OrdersResponse, error) { 648 res, err := common.ExecuteCall(ctx, c.network.ExchangeCookieAssistant, c.spotExchangeClient.Orders, req) 649 650 if err != nil { 651 fmt.Println(err) 652 return &spotExchangePB.OrdersResponse{}, err 653 } 654 655 return res, nil 656 } 657 658 func (c *exchangeClient) GetSpotOrderbookV2(ctx context.Context, marketId string) (*spotExchangePB.OrderbookV2Response, error) { 659 req := spotExchangePB.OrderbookV2Request{ 660 MarketId: marketId, 661 } 662 663 res, err := common.ExecuteCall(ctx, c.network.ExchangeCookieAssistant, c.spotExchangeClient.OrderbookV2, &req) 664 665 if err != nil { 666 fmt.Println(err) 667 return &spotExchangePB.OrderbookV2Response{}, err 668 } 669 670 return res, nil 671 } 672 673 func (c *exchangeClient) GetSpotOrderbooksV2(ctx context.Context, marketIDs []string) (*spotExchangePB.OrderbooksV2Response, error) { 674 req := spotExchangePB.OrderbooksV2Request{ 675 MarketIds: marketIDs, 676 } 677 678 res, err := common.ExecuteCall(ctx, c.network.ExchangeCookieAssistant, c.spotExchangeClient.OrderbooksV2, &req) 679 680 if err != nil { 681 fmt.Println(err) 682 return &spotExchangePB.OrderbooksV2Response{}, err 683 } 684 685 return res, nil 686 } 687 688 func (c *exchangeClient) StreamSpotOrderbookUpdate(ctx context.Context, marketIDs []string) (spotExchangePB.InjectiveSpotExchangeRPC_StreamOrderbookUpdateClient, error) { 689 req := spotExchangePB.StreamOrderbookUpdateRequest{ 690 MarketIds: marketIDs, 691 } 692 693 stream, err := common.ExecuteStreamCall(ctx, c.network.ExchangeCookieAssistant, c.spotExchangeClient.StreamOrderbookUpdate, &req) 694 695 if err != nil { 696 fmt.Println(err) 697 return nil, err 698 } 699 700 return stream, nil 701 } 702 703 func (c *exchangeClient) StreamSpotOrderbookV2(ctx context.Context, marketIDs []string) (spotExchangePB.InjectiveSpotExchangeRPC_StreamOrderbookV2Client, error) { 704 req := spotExchangePB.StreamOrderbookV2Request{ 705 MarketIds: marketIDs, 706 } 707 708 stream, err := common.ExecuteStreamCall(ctx, c.network.ExchangeCookieAssistant, c.spotExchangeClient.StreamOrderbookV2, &req) 709 710 if err != nil { 711 fmt.Println(err) 712 return nil, err 713 } 714 715 return stream, nil 716 } 717 718 func (c *exchangeClient) GetSpotMarkets(ctx context.Context, req *spotExchangePB.MarketsRequest) (*spotExchangePB.MarketsResponse, error) { 719 res, err := common.ExecuteCall(ctx, c.network.ExchangeCookieAssistant, c.spotExchangeClient.Markets, req) 720 721 if err != nil { 722 fmt.Println(err) 723 return &spotExchangePB.MarketsResponse{}, err 724 } 725 726 return res, nil 727 } 728 729 func (c *exchangeClient) GetSpotMarket(ctx context.Context, marketId string) (*spotExchangePB.MarketResponse, error) { 730 req := spotExchangePB.MarketRequest{ 731 MarketId: marketId, 732 } 733 734 res, err := common.ExecuteCall(ctx, c.network.ExchangeCookieAssistant, c.spotExchangeClient.Market, &req) 735 736 if err != nil { 737 fmt.Println(err) 738 return &spotExchangePB.MarketResponse{}, err 739 } 740 741 return res, nil 742 } 743 744 func (c *exchangeClient) StreamSpotMarket(ctx context.Context, marketIDs []string) (spotExchangePB.InjectiveSpotExchangeRPC_StreamMarketsClient, error) { 745 req := spotExchangePB.StreamMarketsRequest{ 746 MarketIds: marketIDs, 747 } 748 749 stream, err := common.ExecuteStreamCall(ctx, c.network.ExchangeCookieAssistant, c.spotExchangeClient.StreamMarkets, &req) 750 751 if err != nil { 752 fmt.Println(err) 753 return nil, err 754 } 755 756 return stream, nil 757 } 758 759 func (c *exchangeClient) StreamSpotOrders(ctx context.Context, req *spotExchangePB.StreamOrdersRequest) (spotExchangePB.InjectiveSpotExchangeRPC_StreamOrdersClient, error) { 760 stream, err := common.ExecuteStreamCall(ctx, c.network.ExchangeCookieAssistant, c.spotExchangeClient.StreamOrders, req) 761 762 if err != nil { 763 fmt.Println(err) 764 return nil, err 765 } 766 767 return stream, nil 768 } 769 770 func (c *exchangeClient) GetSpotTrades(ctx context.Context, req *spotExchangePB.TradesRequest) (*spotExchangePB.TradesResponse, error) { 771 res, err := common.ExecuteCall(ctx, c.network.ExchangeCookieAssistant, c.spotExchangeClient.Trades, req) 772 773 if err != nil { 774 fmt.Println(err) 775 return &spotExchangePB.TradesResponse{}, err 776 } 777 778 return res, nil 779 } 780 781 func (c *exchangeClient) GetSpotTradesV2(ctx context.Context, req *spotExchangePB.TradesV2Request) (*spotExchangePB.TradesV2Response, error) { 782 res, err := common.ExecuteCall(ctx, c.network.ExchangeCookieAssistant, c.spotExchangeClient.TradesV2, req) 783 784 if err != nil { 785 fmt.Println(err) 786 return &spotExchangePB.TradesV2Response{}, err 787 } 788 789 return res, nil 790 } 791 792 func (c *exchangeClient) StreamSpotTrades(ctx context.Context, req *spotExchangePB.StreamTradesRequest) (spotExchangePB.InjectiveSpotExchangeRPC_StreamTradesClient, error) { 793 stream, err := common.ExecuteStreamCall(ctx, c.network.ExchangeCookieAssistant, c.spotExchangeClient.StreamTrades, req) 794 795 if err != nil { 796 fmt.Println(err) 797 return nil, err 798 } 799 800 return stream, nil 801 } 802 803 func (c *exchangeClient) StreamSpotTradesV2(ctx context.Context, req *spotExchangePB.StreamTradesV2Request) (spotExchangePB.InjectiveSpotExchangeRPC_StreamTradesV2Client, error) { 804 stream, err := common.ExecuteStreamCall(ctx, c.network.ExchangeCookieAssistant, c.spotExchangeClient.StreamTradesV2, req) 805 806 if err != nil { 807 fmt.Println(err) 808 return nil, err 809 } 810 811 return stream, nil 812 } 813 814 func (c *exchangeClient) GetSubaccountSpotOrdersList(ctx context.Context, req *spotExchangePB.SubaccountOrdersListRequest) (*spotExchangePB.SubaccountOrdersListResponse, error) { 815 res, err := common.ExecuteCall(ctx, c.network.ExchangeCookieAssistant, c.spotExchangeClient.SubaccountOrdersList, req) 816 817 if err != nil { 818 fmt.Println(err) 819 return &spotExchangePB.SubaccountOrdersListResponse{}, err 820 } 821 822 return res, nil 823 } 824 825 func (c *exchangeClient) GetSubaccountSpotTradesList(ctx context.Context, req *spotExchangePB.SubaccountTradesListRequest) (*spotExchangePB.SubaccountTradesListResponse, error) { 826 res, err := common.ExecuteCall(ctx, c.network.ExchangeCookieAssistant, c.spotExchangeClient.SubaccountTradesList, req) 827 828 if err != nil { 829 fmt.Println(err) 830 return &spotExchangePB.SubaccountTradesListResponse{}, err 831 } 832 833 return res, nil 834 } 835 836 func (c *exchangeClient) GetHistoricalSpotOrders(ctx context.Context, req *spotExchangePB.OrdersHistoryRequest) (*spotExchangePB.OrdersHistoryResponse, error) { 837 res, err := common.ExecuteCall(ctx, c.network.ExchangeCookieAssistant, c.spotExchangeClient.OrdersHistory, req) 838 if err != nil { 839 return &spotExchangePB.OrdersHistoryResponse{}, err 840 } 841 842 return res, nil 843 } 844 845 func (c *exchangeClient) StreamHistoricalSpotOrders(ctx context.Context, req *spotExchangePB.StreamOrdersHistoryRequest) (spotExchangePB.InjectiveSpotExchangeRPC_StreamOrdersHistoryClient, error) { 846 stream, err := common.ExecuteStreamCall(ctx, c.network.ExchangeCookieAssistant, c.spotExchangeClient.StreamOrdersHistory, req) 847 848 if err != nil { 849 fmt.Println(err) 850 return nil, err 851 } 852 853 return stream, nil 854 } 855 856 func (c *exchangeClient) GetInsuranceFunds(ctx context.Context, req *insurancePB.FundsRequest) (*insurancePB.FundsResponse, error) { 857 res, err := common.ExecuteCall(ctx, c.network.ExchangeCookieAssistant, c.insuranceClient.Funds, req) 858 if err != nil { 859 fmt.Println(err) 860 return &insurancePB.FundsResponse{}, err 861 } 862 863 return res, nil 864 } 865 866 func (c *exchangeClient) GetRedemptions(ctx context.Context, req *insurancePB.RedemptionsRequest) (*insurancePB.RedemptionsResponse, error) { 867 res, err := common.ExecuteCall(ctx, c.network.ExchangeCookieAssistant, c.insuranceClient.Redemptions, req) 868 869 if err != nil { 870 fmt.Println(err) 871 return &insurancePB.RedemptionsResponse{}, err 872 } 873 874 return res, nil 875 } 876 877 func (c *exchangeClient) Ping(ctx context.Context, req *metaPB.PingRequest) (*metaPB.PingResponse, error) { 878 res, err := common.ExecuteCall(ctx, c.network.ExchangeCookieAssistant, c.metaClient.Ping, req) 879 880 if err != nil { 881 fmt.Println(err) 882 return &metaPB.PingResponse{}, err 883 } 884 885 return res, nil 886 } 887 888 func (c *exchangeClient) GetVersion(ctx context.Context, req *metaPB.VersionRequest) (*metaPB.VersionResponse, error) { 889 res, err := common.ExecuteCall(ctx, c.network.ExchangeCookieAssistant, c.metaClient.Version, req) 890 891 if err != nil { 892 fmt.Println(err) 893 return &metaPB.VersionResponse{}, err 894 } 895 896 return res, nil 897 } 898 899 func (c *exchangeClient) GetInfo(ctx context.Context, req *metaPB.InfoRequest) (*metaPB.InfoResponse, error) { 900 res, err := common.ExecuteCall(ctx, c.network.ExchangeCookieAssistant, c.metaClient.Info, req) 901 if err != nil { 902 fmt.Println(err) 903 return &metaPB.InfoResponse{}, err 904 } 905 906 return res, nil 907 } 908 909 func (c *exchangeClient) StreamKeepalive(ctx context.Context) (metaPB.InjectiveMetaRPC_StreamKeepaliveClient, error) { 910 req := metaPB.StreamKeepaliveRequest{} 911 912 stream, err := common.ExecuteStreamCall(ctx, c.network.ExchangeCookieAssistant, c.metaClient.StreamKeepalive, &req) 913 914 if err != nil { 915 fmt.Println(err) 916 return nil, err 917 } 918 919 return stream, nil 920 } 921 922 // Deprecated: Use GetAccountPortfolioBalances instead. 923 func (c *exchangeClient) GetAccountPortfolio(ctx context.Context, accountAddress string) (*portfolioExchangePB.AccountPortfolioResponse, error) { 924 req := &portfolioExchangePB.AccountPortfolioRequest{ 925 AccountAddress: accountAddress, 926 } 927 res, err := common.ExecuteCall(ctx, c.network.ExchangeCookieAssistant, c.portfolioExchangeClient.AccountPortfolio, req) 928 if err != nil { 929 fmt.Println(err) 930 return &portfolioExchangePB.AccountPortfolioResponse{}, err 931 } 932 933 return res, nil 934 } 935 936 func (c *exchangeClient) GetAccountPortfolioBalances(ctx context.Context, accountAddress string) (*portfolioExchangePB.AccountPortfolioBalancesResponse, error) { 937 req := &portfolioExchangePB.AccountPortfolioBalancesRequest{ 938 AccountAddress: accountAddress, 939 } 940 res, err := common.ExecuteCall(ctx, c.network.ExchangeCookieAssistant, c.portfolioExchangeClient.AccountPortfolioBalances, req) 941 if err != nil { 942 fmt.Println(err) 943 return &portfolioExchangePB.AccountPortfolioBalancesResponse{}, err 944 } 945 946 return res, nil 947 } 948 949 func (c *exchangeClient) StreamAccountPortfolio(ctx context.Context, accountAddress, subaccountId, balanceType string) (portfolioExchangePB.InjectivePortfolioRPC_StreamAccountPortfolioClient, error) { 950 req := &portfolioExchangePB.StreamAccountPortfolioRequest{ 951 AccountAddress: accountAddress, 952 SubaccountId: subaccountId, 953 Type: balanceType, 954 } 955 stream, err := common.ExecuteStreamCall(ctx, c.network.ExchangeCookieAssistant, c.portfolioExchangeClient.StreamAccountPortfolio, req) 956 957 if err != nil { 958 fmt.Println(err) 959 return nil, err 960 } 961 962 return stream, nil 963 } 964 965 func (c *exchangeClient) GetNetwork() common.Network { 966 return c.network 967 } 968 969 func (c *exchangeClient) Close() { 970 c.conn.Close() 971 }