Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 35 additions & 19 deletions utils/dune.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { httpGet, httpPost } from "./fetchURL";
import { getEnv } from "./env";
import { CliffAdapterResult } from "../types/adapters";
import { elastic } from "@defillama/sdk";
const plimit = require("p-limit");
const limit = plimit(1);

Expand Down Expand Up @@ -111,15 +112,19 @@ export const queryDune = async (
) => {
checkCanRunDuneQuery();

if (Object.keys(query_parameters).length === 0) {
const latest_result = await getLatestData(queryId, run_daily);
if (latest_result !== undefined) return latest_result;
}
const execution_id = await submitQuery(queryId, query_parameters);
const _status = await inquiryStatus(execution_id, queryId);
if (_status === "QUERY_STATE_COMPLETED") {
const API_KEY = API_KEYS[API_KEY_INDEX];
try {
const metadata: any = { application: "dune", query_parameters };
let success = false;
let startTime = +Date.now() / 1e3;

try {
if (Object.keys(query_parameters).length === 0) {
const latest_result = await getLatestData(queryId, run_daily);
if (latest_result !== undefined) return latest_result;
}
const execution_id = await submitQuery(queryId, query_parameters);
const _status = await inquiryStatus(execution_id, queryId);
if (_status === "QUERY_STATE_COMPLETED") {
const API_KEY = API_KEYS[API_KEY_INDEX];
const queryStatus = await limit(() =>
httpGet(
`https://api.dune.com/api/v1/execution/${execution_id}/results?limit=100000`,
Expand All @@ -130,17 +135,28 @@ export const queryDune = async (
},
),
);
return queryStatus.result.rows;
} catch (e: any) {
throw e;
}
} else if (_status === "QUERY_STATE_FAILED") {
if (query_parameters.fullQuery) {
console.log(`Dune query: ${query_parameters.fullQuery}`);
} else {
console.log("Dune parameters", query_parameters);
const rows = queryStatus.result.rows;
success = true;
let endTime = +Date.now() / 1e3;
await elastic.addRuntimeLog({
runtime: endTime - startTime,
success,
metadata: { ...metadata, rows: rows?.length },
});
return rows;
} else if (_status === "QUERY_STATE_FAILED") {
if (query_parameters.fullQuery) {
console.log(`Dune query: ${query_parameters.fullQuery}`);
} else {
console.log("Dune parameters", query_parameters);
}
throw new Error(`Dune query failed: ${queryId}`);
}
throw new Error(`Dune query failed: ${queryId}`);
} catch (e: any) {
let endTime = +Date.now() / 1e3;
await elastic.addRuntimeLog({ runtime: endTime - startTime, success, metadata });
await elastic.addErrorLog({ error: e?.toString(), metadata });
throw e;
}
};

Expand Down
Loading