StreamBboBook gRPC Method
Please note that this method is metered based on data consumption at 0.0165 MB = 10 API credits.
Parameters
coins
repeated string
Loading...
Returns
stream
stream<BboBookUpdate>
Loading...
coin
string
Loading...
time
uint64
Loading...
block_number
uint64
Loading...
bid
L2Level
Loading...
ask
L2Level
Loading...
Request
1// StreamBboBook Example - Stream top-of-book best bid/ask changes via gRPC2package main34import (5"context"6"flag"7"fmt"8"io"9"log"10"math"11"strings"12"time"1314"google.golang.org/grpc"15"google.golang.org/grpc/codes"16"google.golang.org/grpc/credentials"17"google.golang.org/grpc/metadata"18"google.golang.org/grpc/status"1920pb "hyperliquid-orderbook-example/proto"21)2223const (24grpcEndpoint = "your-endpoint.hype-mainnet.quiknode.pro:10000"25authToken = "your-auth-token"26maxRetries = 1027baseDelay = 2 * time.Second28)2930func streamBboBook(coins []string) error {31fmt.Println(strings.Repeat("=", 60))32fmt.Printf("Streaming BBO Book for %s\n", strings.Join(coins, ", "))33fmt.Println("Auto-reconnect: true")34fmt.Println(strings.Repeat("=", 60) + "\n")3536retryCount := 03738for retryCount < maxRetries {39creds := credentials.NewClientTLSFromCert(nil, "")40conn, err := grpc.Dial(grpcEndpoint,41grpc.WithTransportCredentials(creds),42grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(100*1024*1024)))43if err != nil {44return fmt.Errorf("failed to connect: %w", err)45}4647client := pb.NewOrderBookStreamingClient(conn)48ctx := metadata.AppendToOutgoingContext(context.Background(), "x-token", authToken)4950request := &pb.BboBookRequest{51Coins: coins,52}5354if retryCount > 0 {55fmt.Printf("\nš Reconnecting (attempt %d/%d)...\n", retryCount+1, maxRetries)56} else {57fmt.Printf("Connecting to %s...\n", grpcEndpoint)58}5960stream, err := client.StreamBboBook(ctx, request)61if err != nil {62conn.Close()63return fmt.Errorf("failed to start stream: %w", err)64}6566msgCount := 067shouldRetry := false6869for {70update, err := stream.Recv()71if err == io.EOF {72break73}74if err != nil {75st, ok := status.FromError(err)76if ok && st.Code() == codes.DataLoss {77fmt.Printf("\nā ļø Server reinitialized: %s\n", st.Message())78retryCount++79if retryCount < maxRetries {80delay := baseDelay * time.Duration(math.Pow(2, float64(retryCount-1)))81fmt.Printf("ā³ Waiting %v before reconnecting...\n", delay)82time.Sleep(delay)83shouldRetry = true84break85} else {86fmt.Printf("\nā Max retries (%d) reached. Giving up.\n", maxRetries)87conn.Close()88return nil89}90}91conn.Close()92return fmt.Errorf("stream error: %w", err)93}9495msgCount++96if msgCount == 1 {97fmt.Println("ā First BBO update received!\n")98retryCount = 0 // Reset on success99}100101// Display BBO update102fmt.Println("\n" + strings.Repeat("ā", 60))103fmt.Printf("Block: %d | Time: %d | Coin: %s\n", update.BlockNumber, update.Time, update.Coin)104fmt.Println(strings.Repeat("ā", 60))105106// Display best ask107if update.Ask != nil {108fmt.Printf(" BEST ASK: %12s | %12s | (%d orders)\n", update.Ask.Px, update.Ask.Sz, update.Ask.N)109} else {110fmt.Println(" BEST ASK: (none)")111}112113// Display best bid114if update.Bid != nil {115fmt.Printf(" BEST BID: %12s | %12s | (%d orders)\n", update.Bid.Px, update.Bid.Sz, update.Bid.N)116} else {117fmt.Println(" BEST BID: (none)")118}119120// Display spread121if update.Bid != nil && update.Ask != nil {122fmt.Printf(" SPREAD: (best bid: %s, best ask: %s)\n", update.Bid.Px, update.Ask.Px)123}124125fmt.Printf("\n Messages received: %d\n", msgCount)126}127128conn.Close()129130if !shouldRetry {131break132}133}134135return nil136}137138func main() {139coinsFlag := flag.String("coins", "BTC", "Comma-separated coin symbols to stream (e.g., BTC,ETH)")140141flag.Parse()142143coins := strings.Split(*coinsFlag, ",")144145fmt.Println("\n" + strings.Repeat("=", 60))146fmt.Println("Hyperliquid StreamBboBook Example")147fmt.Printf("Endpoint: %s\n", grpcEndpoint)148fmt.Println(strings.Repeat("=", 60))149150if err := streamBboBook(coins); err != nil {151log.Fatal(err)152}153}154
1// StreamBboBook Example - Stream top-of-book best bid/ask changes via gRPC2package main34import (5"context"6"flag"7"fmt"8"io"9"log"10"math"11"strings"12"time"1314"google.golang.org/grpc"15"google.golang.org/grpc/codes"16"google.golang.org/grpc/credentials"17"google.golang.org/grpc/metadata"18"google.golang.org/grpc/status"1920pb "hyperliquid-orderbook-example/proto"21)2223const (24grpcEndpoint = "your-endpoint.hype-mainnet.quiknode.pro:10000"25authToken = "your-auth-token"26maxRetries = 1027baseDelay = 2 * time.Second28)2930func streamBboBook(coins []string) error {31fmt.Println(strings.Repeat("=", 60))32fmt.Printf("Streaming BBO Book for %s\n", strings.Join(coins, ", "))33fmt.Println("Auto-reconnect: true")34fmt.Println(strings.Repeat("=", 60) + "\n")3536retryCount := 03738for retryCount < maxRetries {39creds := credentials.NewClientTLSFromCert(nil, "")40conn, err := grpc.Dial(grpcEndpoint,41grpc.WithTransportCredentials(creds),42grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(100*1024*1024)))43if err != nil {44return fmt.Errorf("failed to connect: %w", err)45}4647client := pb.NewOrderBookStreamingClient(conn)48ctx := metadata.AppendToOutgoingContext(context.Background(), "x-token", authToken)4950request := &pb.BboBookRequest{51Coins: coins,52}5354if retryCount > 0 {55fmt.Printf("\nš Reconnecting (attempt %d/%d)...\n", retryCount+1, maxRetries)56} else {57fmt.Printf("Connecting to %s...\n", grpcEndpoint)58}5960stream, err := client.StreamBboBook(ctx, request)61if err != nil {62conn.Close()63return fmt.Errorf("failed to start stream: %w", err)64}6566msgCount := 067shouldRetry := false6869for {70update, err := stream.Recv()71if err == io.EOF {72break73}74if err != nil {75st, ok := status.FromError(err)76if ok && st.Code() == codes.DataLoss {77fmt.Printf("\nā ļø Server reinitialized: %s\n", st.Message())78retryCount++79if retryCount < maxRetries {80delay := baseDelay * time.Duration(math.Pow(2, float64(retryCount-1)))81fmt.Printf("ā³ Waiting %v before reconnecting...\n", delay)82time.Sleep(delay)83shouldRetry = true84break85} else {86fmt.Printf("\nā Max retries (%d) reached. Giving up.\n", maxRetries)87conn.Close()88return nil89}90}91conn.Close()92return fmt.Errorf("stream error: %w", err)93}9495msgCount++96if msgCount == 1 {97fmt.Println("ā First BBO update received!\n")98retryCount = 0 // Reset on success99}100101// Display BBO update102fmt.Println("\n" + strings.Repeat("ā", 60))103fmt.Printf("Block: %d | Time: %d | Coin: %s\n", update.BlockNumber, update.Time, update.Coin)104fmt.Println(strings.Repeat("ā", 60))105106// Display best ask107if update.Ask != nil {108fmt.Printf(" BEST ASK: %12s | %12s | (%d orders)\n", update.Ask.Px, update.Ask.Sz, update.Ask.N)109} else {110fmt.Println(" BEST ASK: (none)")111}112113// Display best bid114if update.Bid != nil {115fmt.Printf(" BEST BID: %12s | %12s | (%d orders)\n", update.Bid.Px, update.Bid.Sz, update.Bid.N)116} else {117fmt.Println(" BEST BID: (none)")118}119120// Display spread121if update.Bid != nil && update.Ask != nil {122fmt.Printf(" SPREAD: (best bid: %s, best ask: %s)\n", update.Bid.Px, update.Ask.Px)123}124125fmt.Printf("\n Messages received: %d\n", msgCount)126}127128conn.Close()129130if !shouldRetry {131break132}133}134135return nil136}137138func main() {139coinsFlag := flag.String("coins", "BTC", "Comma-separated coin symbols to stream (e.g., BTC,ETH)")140141flag.Parse()142143coins := strings.Split(*coinsFlag, ",")144145fmt.Println("\n" + strings.Repeat("=", 60))146fmt.Println("Hyperliquid StreamBboBook Example")147fmt.Printf("Endpoint: %s\n", grpcEndpoint)148fmt.Println(strings.Repeat("=", 60))149150if err := streamBboBook(coins); err != nil {151log.Fatal(err)152}153}154
Don't have an account yet?
Create your Quicknode endpoint in seconds and start building
Get started for free