Untitled

mail@pastecode.io avatar
unknown
plain_text
6 months ago
5.0 kB
0
Indexable
Never
package org.example;

import okhttp3.*;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class BitqueryWebsocketClient {

    private static final String API_KEY = "*************************";
    private static final String WS_URL = "wss://streaming.bitquery.io/graphql";

    public static void main(String[] args) {
        OkHttpClient client = new OkHttpClient.Builder()
                .readTimeout(0, TimeUnit.MILLISECONDS)
                .build();

        Request request = new Request.Builder()
                .url(WS_URL)
                .header("X-API-KEY", "Bearer " + API_KEY)
                .header("Sec-WebSocket-Protocol", "graphql-ws")
                .build();

        // Dynamic parameters
        String network = "eth";
        int limit = 2;

        List<String> excludeList = new ArrayList<>();
        excludeList.add("0xdAC17F958D2ee523a2206206994597C13D831ec7");
        excludeList.add("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2");
        excludeList.add("0xB8c77482e45F1F44dE1745F52C74426C631bDD52");

        WebSocket webSocket = client.newWebSocket(request, new WebSocketListener() {

            @Override
            public void onOpen(WebSocket webSocket, Response response) {
                System.out.println("Opened connection");

                // Initialization message
                String initMessage = "{ \"type\": \"connection_init\" }";
                webSocket.send(initMessage);

                // Wait for a bit (this is just an example, you might want a more structured approach)
                try {
                    Thread.sleep(1000); // Sleep for 1 second
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                // Send the updated subscription message with dynamic parameters
                String payload = createSubscriptionPayload("1", "start", network, limit, excludeList);
                webSocket.send(payload);
            }

            @Override
            public void onMessage(WebSocket webSocket, String text) {
                try {
                    JSONObject jsonObject = new JSONObject(text);
                    if (!"ka".equals(jsonObject.optString("type"))) {
                        System.out.println("Received: " + text);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onClosed(WebSocket webSocket, int code, String reason) {
                System.out.println("Closed: " + reason);
            }

            @Override
            public void onFailure(WebSocket webSocket, Throwable t, Response response) {
                t.printStackTrace();
            }
        });

        // To keep the program running
        try {
            Thread.sleep(100000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private static String createSubscriptionPayload(String id, String type, String network, int limit, List<String> excludeList) {
        JSONObject payloadObject = new JSONObject()
                .put("id", id)
                .put("type", type)
                .put("payload", new JSONObject()
                        .put("query", "subscription MyQueryWithVariables($network: evm_network!, $limit: Int!, $exclude: [String]) {" +
                                "  EVM(network: $network) {" +
                                "    Transfers(" +
                                "      limit: {count: $limit}" +
                                "      orderBy: {descending: Block_Number}" +
                                "      where: {Transfer: {Currency: {SmartContract: {notIn: $exclude}}}}" +
                                "    ) {" +
                                "      Block {" +
                                "        Number" +
                                "        Hash" +
                                "      }" +
                                "      Transfer {" +
                                "        Currency {" +
                                "          Symbol" +
                                "          Name" +
                                "        }" +
                                "        Amount" +
                                "      }" +
                                "    }" +
                                "  }" +
                                "}")
                        .put("variables", new JSONObject()
                                .put("network", network)
                                .put("limit", limit)
                                .put("exclude", excludeList)));

        return payloadObject.toString();
    }
}
Leave a Comment