Untitled

mail@pastecode.io avatar
unknown
java
a month ago
4.9 kB
2
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 BEARER_TOKEN = "****************************************";
    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("Sec-WebSocket-Protocol", "graphql-ws")
                .addHeader("Authorization", "Bearer " + BEARER_TOKEN)
                .build();

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

        List<String> includeList = new ArrayList<>();
        includeList.add("0xdAC17F958D2ee523a2206206994597C13D831ec7");
        includeList.add("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2");
        includeList.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);


                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, includeList);
                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> includeList) {
        JSONObject payloadObject = new JSONObject()
                .put("id", id)
                .put("type", type)
                .put("payload", new JSONObject()
                        .put("query", "subscription MyQueryWithVariables($network: evm_network!, $limit: Int!, $include: [String]) {" +
                                "  EVM(network: $network) {" +
                                "    Transfers(" +
                                "      limit: {count: $limit}" +
                                "      orderBy: {descending: Block_Number}" +
                                "      where: {Transfer: {Currency: {SmartContract: {in: $include}}}}" +
                                "    ) {" +
                                "      Block {" +
                                "        Number" +
                                "      }" +
                                "      Transfer {" +
                                "        Currency {" +
                                "          Symbol" +
                                "          Name" +
                                "          SmartContract" +
                                "        }" +
                                "        Amount" +
                                "      }" +
                                "    }" +
                                "  }" +
                                "}")
                        .put("variables", new JSONObject()
                                .put("network", network)
                                .put("limit", limit)
                                .put("include", includeList)));

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