Skip to content

Commit

Permalink
remove while loop
Browse files Browse the repository at this point in the history
  • Loading branch information
dtmkeng committed Feb 28, 2024
1 parent 3bfb5b4 commit 86c7737
Showing 1 changed file with 9 additions and 69 deletions.
78 changes: 9 additions & 69 deletions helpers/dune.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import retry from "async-retry";
import { IJSON } from "../adapters/types";
import { httpGet, httpPost } from "../utils/fetchURL";
import { getEnv } from "./env";
Expand All @@ -9,7 +8,6 @@ const token = {} as IJSON<string>
const API_KEYS =getEnv('DUNE_API_KEYS')?.split(',') ?? ["L0URsn5vwgyrWbBpQo9yS1E3C1DBJpZh"]
let API_KEY_INDEX = 0;

const MAX_RETRIES = 6 * API_KEYS.length + 3;
const NOW_TIMESTAMP = Math.trunc((Date.now()) / 1000)

const getLatestData = async (queryId: string) => {
Expand All @@ -28,13 +26,13 @@ const getLatestData = async (queryId: string) => {
}
return undefined
} catch (e: any) {
console.error("GetLatestData Dune: error query data: " + e.message)
return null
throw e;
}
}

const inquiryStatus = async (queryId: string) => {
let _status = undefined;
if (token[queryId] === undefined) throw new Error("execution is not undefined.")
do {
try {
_status = (await limit(() => httpGet(`https://api.dune.com/api/v1/execution/${token[queryId]}/status`, {
Expand All @@ -43,9 +41,8 @@ const inquiryStatus = async (queryId: string) => {
}
}))).state
} catch (e: any) {
return 'QUERY_STATE_FAILED';
throw e;
}

} while (_status !== 'QUERY_STATE_COMPLETED' && _status !== 'QUERY_STATE_FAILED');
return _status
}
Expand All @@ -62,76 +59,23 @@ const submitQuery = async (queryId: string, query_parameters = {}) => {
}))
if (query?.execution_id) {
token[queryId] = query?.execution_id
return query
} else {
console.log("error query data", query)
return null
throw new Error("error query data: " + query)
}
} catch (e: any) {
return null;
throw e;
}
}
return 'ALREADY_SUBMITTED';
}


export const queryDune = async (queryId: string, query_parameters = {}) => {
return await retry(async (bail: any, attempt: number) => {
if (Object.keys(query_parameters).length === 0) {
let latest_result = undefined
while (latest_result === undefined) {
latest_result = await getLatestData(queryId)
if (latest_result === undefined) break;
if (latest_result === null) {
if (API_KEY_INDEX < API_KEYS.length - 1) {
console.error('api key out of limit waiting retry next key')
API_KEY_INDEX = API_KEY_INDEX + 1
latest_result = undefined
} else {
bail(new Error("GetLatestData Dune: there is no more api key"))
}
}
}
const latest_result = await getLatestData(queryId)
if (latest_result !== undefined) return latest_result
}

let execute = undefined;
while (execute === undefined) {
execute = await submitQuery(queryId, query_parameters)
if (execute === null) {
if (API_KEY_INDEX < API_KEYS.length - 1) {
console.error('api key out of limit waiting retry next key')
API_KEY_INDEX = API_KEY_INDEX + 1
execute = undefined
} else {
bail(new Error("SubmitQuery Dune: there is no more api key"))
}
}
if (execute === 'ALREADY_SUBMITTED') {
break;
}
}

let _status = undefined
while (_status === undefined) {
_status = await inquiryStatus(queryId)
if (_status === 'QUERY_STATE_FAILED') {
if (API_KEY_INDEX < API_KEYS.length - 1) {
console.error('api key out of limit waiting retry next key')
API_KEY_INDEX = API_KEY_INDEX + 1
_status = undefined
} else {
bail(new Error("InquiryStatus Dune: there is no more api key"))
}
}
if (_status === 'QUERY_STATE_COMPLETED') {
break;
}
if (MAX_RETRIES === attempt) {
bail(new Error("Dune query cancelled"))
}
}

await submitQuery(queryId, query_parameters)
const _status = await inquiryStatus(queryId)
if (_status === 'QUERY_STATE_COMPLETED') {
const API_KEY = API_KEYS[API_KEY_INDEX]
try {
Expand All @@ -142,11 +86,7 @@ export const queryDune = async (queryId: string, query_parameters = {}) => {
}))
return queryStatus.result.rows
} catch (e: any) {
bail(new Error("Execution Dune: error query data: " + e.message))
throw e;
}
}
}, {
retries: MAX_RETRIES,
maxTimeout: 1000 * 60 * 5
})
}

0 comments on commit 86c7737

Please sign in to comment.