-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
60 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import axios, { AxiosResponse } from "axios" | ||
import retry from "async-retry"; | ||
import { IJSON } from "../adapters/types"; | ||
|
||
const token = {} as IJSON<string> | ||
const API_KEYS = process.env.DUNE_API_KEYS?.split(',') ?? ["L0URsn5vwgyrWbBpQo9yS1E3C1DBJpZh"] | ||
let API_KEY_INDEX = 0; | ||
const MAX_RETRIES = 20; | ||
|
||
export async function queryDune(queryId: string) { | ||
return await retry( | ||
async (_bail, _attempt: number) => { | ||
const API_KEY = API_KEYS[API_KEY_INDEX] | ||
let query: undefined | AxiosResponse<any, any> = undefined | ||
if (!token[queryId]) { | ||
try{ | ||
query = await axios.post(`https://api.dune.com/api/v1/query/${queryId}/execute`, {}, { | ||
headers: { | ||
"x-dune-api-key": API_KEY, | ||
'Content-Type': 'application/json' | ||
} | ||
}) | ||
if(query?.data?.execution_id){ | ||
token[queryId] = query?.data.execution_id | ||
} else { | ||
console.log("error query data", query?.data) | ||
throw query?.data | ||
} | ||
} catch(e:any){ | ||
console.log("make query dune", e) | ||
throw e.error | ||
} | ||
} | ||
|
||
if (!token[queryId]) { | ||
throw new Error("Couldn't get a token from dune") | ||
} | ||
|
||
const queryStatus = await axios.get(`https://api.dune.com/api/v1/execution/${token[queryId]}/results`, { | ||
headers: { | ||
"x-dune-api-key": API_KEY | ||
} | ||
}) | ||
|
||
const status = queryStatus.data.state | ||
if (status === "QUERY_STATE_COMPLETED") { | ||
return queryStatus.data.result.rows | ||
} | ||
throw new Error("Still running") | ||
}, | ||
{ | ||
retries: MAX_RETRIES, | ||
maxTimeout: 1000 * 60 * 5 | ||
} | ||
); | ||
} |