Get solutions for intent

This is an example of how to get solutions for specific intent.

Name: GetIntentSolutions

GetIntentSolutions is a Web Socket method to get a list of intent solutions GetIntentSolutionsRequest arguments:

KeyDescriptionValues

dapp_or_sender_address

DApp or sender ETH address

string

intent_id

intent ID

string

hash

Keccak256Hash of the dapp_or_sender_address + intent_id

byte[]

signature

ECDSA signature of the hash

byte[]

GetIntentSolutionsResponse is a list of objects with the following fields:

KeyDescriptionValues

intent_solution

solution for the specific intent

byte[]

solution_id

UUID of the solution

string

dapp_address

dApp ETH address

string

solver_address

solver ETH address

string

timestamp

time when the solution was first seen (time.RFC3339Nano format)

string

Example

package main

import (
	"encoding/hex"
	"encoding/json"
	"fmt"
	"log"
	"net/http"

	"github.com/ethereum/go-ethereum/crypto"
	"github.com/gorilla/websocket"
)

func main() {
	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 := getSolutionsForIntent(intentID)

	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
	}
}

func getSolutionsForIntent(intentID string) []byte {
	// The sample private key of the solver
	senderPrivateKeyHex := "2b36f5c0317c13e6326e9d2e2ae39badec9d030ba44bc889318e4fa5412ad342"

	// Decode the hex string to a byte slice
	senderPrivateKeyBytes, err := hex.DecodeString(senderPrivateKeyHex)
	if err != nil {
		log.Fatalf("invalid hex string: %v", err)
	}

	// Use the Ethereum crypto package to create an ECDSA private key
	senderPrivateKey, err := crypto.ToECDSA(senderPrivateKeyBytes)
	if err != nil {
		log.Fatalf("failed to create private key: %v", err)
	}

	// The address of the sender
	senderAddress := crypto.PubkeyToAddress(senderPrivateKey.PublicKey).String()

	data := []byte(senderAddress + intentID)

	// Sign the data with the private key
	reqHash := crypto.Keccak256Hash(data).Bytes()      // need a hash to sign, so we're hashing the payload here
	sig, err := crypto.Sign(reqHash, senderPrivateKey) // signing the hash
	if err != nil {
		log.Fatalln("could not sign the message", err)
	}

	m := map[string]interface{}{
		"intent_id":              intentID,
		"dapp_or_sender_address": senderAddress,
		"hash":                   reqHash,
		"signature":              sig,
	}

	req, err := json.Marshal(m)
	if err != nil {
		log.Fatalln("marshal", err)
	}

	return []byte(fmt.Sprintf(`{"id": "1", "method": "blxr_get_intent_solutions", "params": %s}`, req))
}

Last updated