This example demonstrates how to subscribe to quotes from the bloXroute gateway.
Name: Quotes
Quotes is a GRPC stream of new quote that match the dappAddress of the subscription as they are propagated in the BDN.\
QuotesRequest arguments:
Key
Description
Values
dappAddress
ETH address of DApp that should receive quotes
string
QuotesReply fields (stream message):
Key
Description
Values
quoteId
UUID of the quote
string
quote
quote
byte[]
dappAddress
ETH address of the DApp that should receive quote
string
solverAddress
ETH address of the quote solver
string
Examples
packagemainimport ("context""encoding/hex""fmt""log" pb "github.com/bloXroute-Labs/gateway/v2/protobuf""github.com/ethereum/go-ethereum/crypto""google.golang.org/grpc""google.golang.org/grpc/credentials")// gatewayHost is the address of the gateway to which the subscription is being madeconst gatewayHost ="127.0.0.1:5002"// header is the authorization header of your bloXroute Accountconst header ="<YOUR-AUTHORIZATION-HEADER>"funcmain() {// this will use localhost CA to verify the certificate creds := credentials.NewClientTLSFromCert(nil, "")// Dial the gateway conn, err := grpc.Dial(gatewayHost, grpc.WithTransportCredentials(creds), grpc.WithPerRPCCredentials(blxrCredentials{authorization: header}))if err !=nil { log.Fatalln("dial grpc", err) }// Create a client client := pb.NewGatewayClient(conn)// Subscribe to quotes stream, err := client.Quotes(context.Background(), genQuotesRequest())if err !=nil { log.Fatalln("subscribe to quotes", err) }for { fmt.Printf("listening for quotes from %s ...\n", gatewayHost)// Receive the quotes from the stream until the stream is closed msg, err := stream.Recv()if err !=nil { log.Fatalln("receive from stream", err) } fmt.Println("got quote:") fmt.Println("- quote_id:", msg.QuoteId) fmt.Println("- dapp_addr:", msg.DappAddress) fmt.Println("- solver_addr:", msg.SolverAddress) fmt.Println("- quote:", hex.EncodeToString(msg.Quote)) }}funcgenQuotesRequest() *pb.QuotesRequest {// Return the quote requestreturn&pb.QuotesReply{ DappAddress: "<dapp addr wanted>", }}// blxrCredentials is an implementation of PerRPCCredentialstypeblxrCredentialsstruct { authorization string}// GetRequestMetadata sets the authorization headerfunc (bc blxrCredentials) GetRequestMetadata(ctx context.Context, uri ...string) (map[string]string, error) {returnmap[string]string{"authorization": bc.authorization, }, nil}// RequireTransportSecurity is a method of the PerRPCCredentials interfacefunc (bc blxrCredentials) RequireTransportSecurity() bool {returnfalse}
packagemainimport ("encoding/json""fmt""log""net/http""github.com/ethereum/go-ethereum/crypto""github.com/gorilla/websocket")funcmain() { dialer := websocket.DefaultDialer// Add the following lines if you work with IP instead of DNS// tlsConfig := &tls.Config{// Certificates: []tls.Certificate{cert},// InsecureSkipVerify: true,// }// dialer.TLSClientConfig = tlsConfig req :=generateQuoteRequest() wsSubscriber, _, err := dialer.Dial("wss://virginia.eth.blxrbdn.com/ws", http.Header{"Authorization": []string{"YOUR-AUTHORIZATION-HEADER"}})if err !=nil { fmt.Println(err)return } err = wsSubscriber.WriteMessage(websocket.TextMessage, req)if err !=nil { fmt.Println(err)return }for { _, nextNotification, err := wsSubscriber.ReadMessage()if err !=nil { fmt.Println(err) } fmt.Println(string(nextNotification)) // or process it generally }}funcgenerateQuoteRequest() []byte { dappAddress :="<dApp addr wanted>" m :=map[string]interface{}{"dapp_address": dappAddress, } req, err := json.Marshal(m)if err !=nil { log.Fatalln("marshal", err) }return []byte(fmt.Sprintf(`{ "id": "2", "method": "subscribe", "params": [ "quotesFeed", %s]}`, req))}