StreamTpslUpdates 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<TpslUpdatesUpdate>
Loading...
time
uint64
Loading...
height
uint64
Loading...
snapshot
bool
Loading...
diffs
array<TpslOrderDiff>
Loading...
Request
1// StreamTpslUpdates Example - Stream TP/SL trigger-order updates 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 streamTpslUpdates(coins []string) error {31fmt.Println(strings.Repeat("=", 60))32fmt.Printf("Streaming TP/SL Updates 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.TpslUpdatesRequest{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.StreamTpslUpdates(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 TP/SL update received!\n")98retryCount = 0 // Reset on success99}100101// Display update102fmt.Println("\n" + strings.Repeat("ā", 60))103snapshotLabel := ""104if update.Snapshot {105snapshotLabel = " | SNAPSHOT"106}107fmt.Printf("Block: %d | Time: %d%s | Diffs: %d\n", update.Height, update.Time, snapshotLabel, len(update.Diffs))108fmt.Println(strings.Repeat("ā", 60))109110for _, diff := range update.Diffs {111side := "ASK"112if diff.Side == "B" {113side = "BID"114}115sz := diff.Sz116if diff.IsPositionTpsl {117sz = "position-sized"118}119120switch diff.DiffType {121case pb.TpslDiffType_TPSL_DIFF_TYPE_ADD:122fmt.Printf(" ADD %s oid: %d | %s | %s sz: %s\n", diff.Coin, diff.Oid, diff.OrderType, side, sz)123fmt.Printf(" trigger: %s | limit px: %s | reduce-only: %t\n", diff.TriggerCondition, diff.LimitPx, diff.ReduceOnly)124case pb.TpslDiffType_TPSL_DIFF_TYPE_REMOVE:125fmt.Printf(" REMOVE %s oid: %d | %s | reason: %s\n", diff.Coin, diff.Oid, diff.OrderType, diff.Reason)126}127}128129fmt.Printf("\n Messages received: %d\n", msgCount)130}131132conn.Close()133134if !shouldRetry {135break136}137}138139return nil140}141142func main() {143coinsFlag := flag.String("coins", "ETH", "Comma-separated coin symbols to stream (e.g., ETH,BTC)")144145flag.Parse()146147coins := strings.Split(*coinsFlag, ",")148149fmt.Println("\n" + strings.Repeat("=", 60))150fmt.Println("Hyperliquid StreamTpslUpdates Example")151fmt.Printf("Endpoint: %s\n", grpcEndpoint)152fmt.Println(strings.Repeat("=", 60))153154if err := streamTpslUpdates(coins); err != nil {155log.Fatal(err)156}157}158
1// StreamTpslUpdates Example - Stream TP/SL trigger-order updates 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 streamTpslUpdates(coins []string) error {31fmt.Println(strings.Repeat("=", 60))32fmt.Printf("Streaming TP/SL Updates 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.TpslUpdatesRequest{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.StreamTpslUpdates(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 TP/SL update received!\n")98retryCount = 0 // Reset on success99}100101// Display update102fmt.Println("\n" + strings.Repeat("ā", 60))103snapshotLabel := ""104if update.Snapshot {105snapshotLabel = " | SNAPSHOT"106}107fmt.Printf("Block: %d | Time: %d%s | Diffs: %d\n", update.Height, update.Time, snapshotLabel, len(update.Diffs))108fmt.Println(strings.Repeat("ā", 60))109110for _, diff := range update.Diffs {111side := "ASK"112if diff.Side == "B" {113side = "BID"114}115sz := diff.Sz116if diff.IsPositionTpsl {117sz = "position-sized"118}119120switch diff.DiffType {121case pb.TpslDiffType_TPSL_DIFF_TYPE_ADD:122fmt.Printf(" ADD %s oid: %d | %s | %s sz: %s\n", diff.Coin, diff.Oid, diff.OrderType, side, sz)123fmt.Printf(" trigger: %s | limit px: %s | reduce-only: %t\n", diff.TriggerCondition, diff.LimitPx, diff.ReduceOnly)124case pb.TpslDiffType_TPSL_DIFF_TYPE_REMOVE:125fmt.Printf(" REMOVE %s oid: %d | %s | reason: %s\n", diff.Coin, diff.Oid, diff.OrderType, diff.Reason)126}127}128129fmt.Printf("\n Messages received: %d\n", msgCount)130}131132conn.Close()133134if !shouldRetry {135break136}137}138139return nil140}141142func main() {143coinsFlag := flag.String("coins", "ETH", "Comma-separated coin symbols to stream (e.g., ETH,BTC)")144145flag.Parse()146147coins := strings.Split(*coinsFlag, ",")148149fmt.Println("\n" + strings.Repeat("=", 60))150fmt.Println("Hyperliquid StreamTpslUpdates Example")151fmt.Printf("Endpoint: %s\n", grpcEndpoint)152fmt.Println(strings.Repeat("=", 60))153154if err := streamTpslUpdates(coins); err != nil {155log.Fatal(err)156}157}158
Don't have an account yet?
Create your Quicknode endpoint in seconds and start building
Get started for free