Skip to content

Commit

Permalink
fix fantom fees
Browse files Browse the repository at this point in the history
  • Loading branch information
0xngmi committed Aug 10, 2023
1 parent 1a48057 commit 867b38b
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
6 changes: 4 additions & 2 deletions fees/fantom.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { Adapter, ProtocolType } from "../adapters/types";
import { CHAIN } from "../helpers/chains";
import { getEtherscanFees } from "../helpers/etherscanFees";
import { queryDune } from "../helpers/dune";
import { getPrices } from "../utils/prices";

const adapter: Adapter = {
adapter: {
[CHAIN.FANTOM]: {
fetch: async (timestamp: number) => {
const usdFees = await getEtherscanFees(timestamp, `https://ftmscan.com/chart/transactionfee?output=csv`, "coingecko:fantom")
const fees = (await queryDune("2843395"))[0]._col0
const usdFees = fees * (await getPrices(["coingecko:fantom"], timestamp))["coingecko:fantom"].price;

return {
timestamp,
Expand Down
56 changes: 56 additions & 0 deletions helpers/dune.ts
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
}
);
}

0 comments on commit 867b38b

Please sign in to comment.