Aori Integration Example
This page gives you the code example of using Intent Network to work with project Aori. We will guide you through the following steps.
Connect to Intent Network without local Gateway
Location
Endpoint
Subscribe to Aori Intents
package main
import (
"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/insecure"
)
// gatewayHost is the address of the gateway to which the subscription is being made. Can be changed to cloud-API ip
const gatewayHost = "virginia-intents.blxrbdn.com:5005"
// authHeader is the authorization header of your bloXroute Account
const authHeader = "<YOUR-AUTHORIZATION-HEADER>"
func main() {
// 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: authHeader})
)
if err != nil {
log.Fatalln("dial grpc", err)
}
// Create a client
client := pb.NewGatewayClient(conn)
// Subscribe to intents
stream, err := client.Intents(context.Background(), genIntentsRequest())
if err != nil {
log.Fatalln("subscribe to intents", err)
}
for {
fmt.Printf("listening for intents from %s ...\n", gatewayHost)
// Receive the intent from the stream until the stream is closed
msg, err := stream.Recv()
if err != nil {
log.Fatalln("receive from stream", err)
}
fmt.Println("------------------")
fmt.Println("got intent:"
fmt.Println("- dapp addr: ", msg.DappAddress)
fmt.Println("- sender addr:", msg.SenderAddress)
fmt.Println("- id: ", msg.IntentId)
fmt.Println("- timestamp: ", msg.Timestamp.String())
fmt.Println("- intent: ", hex.EncodeToString(msg.Intent))
}
}
// blxrCredentials is an implementation of PerRPCCredentials
type blxrCredentials struct {
authorization string
}
// GetRequestMetadata sets the authorization header
func (bc blxrCredentials) GetRequestMetadata(ctx context.Context, uri ...string) (map[string]string, error) {
return map[string]string{
"authorization": bc.authorization,
}, nil
}
// RequireTransportSecurity is a method of the PerRPCCredentials interface
func (bc blxrCredentials) RequireTransportSecurity() bool {
return false
}