const { apiKey, component } = props; const BASE_URL = "https://api-v2.flipsidecrypto.xyz/json-rpc"; State.init({ data: [], hasError: false, isLoading: false, isRunning: false, queryId: "", interval: null, }); if (!apiKey) { return <div>Empty apiKey!</div>; } const getQueryResult = () => { let body = { jsonrpc: "2.0", method: "getQueryRunResults", params: [ { queryRunId: state.queryId, format: "csv", page: { number: 1, size: 1, }, }, ], id: 1, }; asyncFetch(BASE_URL, { method: "POST", headers: { "Content-Type": "application/json", "x-api-key": apiKey }, body: JSON.stringify(body), }).then((res) => { if (!res.ok) { State.update({ isLoading: false, isRunning: false, hasError: true, }); return; } if (!res.body.result) { State.update({ isLoading: false, isRunning: false, hasError: true, }); return; } console.log(res.body); }); }; const getQueryStatus = () => { console.log("getting query status"); let body = { jsonrpc: "2.0", method: "getQueryRun", params: [ { queryRunId: state.queryId, }, ], id: 1, }; asyncFetch(BASE_URL, { method: "POST", headers: { "Content-Type": "application/json", "x-api-key": apiKey }, body: JSON.stringify(body), }).then((res) => { if (!res.ok) { State.update({ isLoading: false, isRunning: false, hasError: true, }); return; } console.log(res.body); if (!res.body.result) { State.update({ isLoading: false, isRunning: false, hasError: true, }); return; } if (res.body.result.queryRun.state == "QUERY_STATE_SUCCESS") { State.update({ isLoading: false, isRunning: false, }); } }); }; const runQuery = (sql) => { if (state.isRunning) { return; } let body = { jsonrpc: "2.0", method: "createQueryRun", params: [ { resultTTLHours: 1, maxAgeMinutes: 0, sql: "SELECT date_trunc('''hour''', block_timestamp) as hourly_datetime, count(distinct tx_hash) as tx_count from ethereum.core.fact_transactions where block_timestamp >= getdate() - interval'''1 month''' group by 1 order by 1 desc", dataSource: "snowflake-default", dataProvider: "flipside", }, ], id: 1, }; State.update({ isRunning: true, isLoading: true }); asyncFetch(BASE_URL, { method: "POST", headers: { "Content-Type": "application/json", "x-api-key": apiKey }, body: JSON.stringify(body), }).then((res) => { if (!res.ok) { State.update({ isLoading: false, isRunning: false, hasError: true, }); return; } if (!res.body.result) { State.update({ isLoading: false, isRunning: false, hasError: true, }); return; } State.update({ queryId: res.body.result.queryRun.id, }); console.log(res.body.result.queryRun.id); getQueryStatus(res.body.result.queryRun.id); }); }; const cancelQuery = async () => {}; return ( <div> <div>Rendering Child: </div> <> {component({ data, hasError, isLoading, isRunning, runQuery, cancelQuery, })} </> </div> );