-
Notifications
You must be signed in to change notification settings - Fork 120
add ethereum, add dune utils, add interactive chart viewer #117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2d94fb6
add ethereum, add dune utils, add interactive chart viewer
ReynardoEW 54a5075
remove async-retry
ReynardoEW e00b495
remove chartjs
ReynardoEW 7a40e36
removed unused keys, added back chartjs and added unc rewards
ReynardoEW 4f3898a
unc compress
ReynardoEW File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -2,4 +2,5 @@ | |
| **/**/*.js | ||
| /utils/testCharts | ||
| result.txt | ||
| /utils/result.png | ||
| /utils/result.png | ||
| .env | ||
This file contains hidden or 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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,113 @@ | ||
| import { uncle } from "../adapters/ethereum"; | ||
| import { manualCliff, manualLinear } from "../adapters/manual"; | ||
| import { CliffAdapterResult, LinearAdapterResult, Protocol } from "../types/adapters"; | ||
| import { GAS_TOKEN } from "../utils/constants"; | ||
| import { queryDune } from "../utils/dune"; | ||
| import { periodToSeconds } from "../utils/time"; | ||
|
|
||
| const chain = "ethereum"; | ||
| const start = 1438300800; | ||
|
|
||
| const genesis = 1438269988; // block 1 5eth reward | ||
| const byzantiumFork = 1508131331; // block 4370000 3eth reward | ||
| const constantinopleFork = 1551383524; // block 7280000 2eth reward | ||
| const merge = 1663224162; // block 15537393 0eth reward | ||
|
|
||
| interface BurnDataPoint { | ||
| eth_burn: number; | ||
| timestamp: number; | ||
| } | ||
|
|
||
| const burnData = async (type: 'pos' | 'pow'): Promise<LinearAdapterResult[]> => { | ||
| const result: LinearAdapterResult[] = []; | ||
| const burnData = await queryDune("5041563") | ||
|
|
||
| // Filter data based on type | ||
| let filteredData = burnData; | ||
| if (type === 'pow') { | ||
| // Pre-merge data | ||
| filteredData = burnData.filter((d: BurnDataPoint) => d.timestamp < merge); | ||
| } else if (type === 'pos') { | ||
| // Post-merge data | ||
| filteredData = burnData.filter((d: BurnDataPoint) => d.timestamp >= merge); | ||
| } | ||
|
|
||
| for (let i = 0; i < filteredData.length - 1; i++) { | ||
| result.push({ | ||
| type: "linear", | ||
| start: filteredData[i + 1].timestamp, | ||
| end: filteredData[i].timestamp, | ||
| amount: -filteredData[i].eth_burn | ||
| }); | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| const foundationOutflow = async (): Promise<CliffAdapterResult[]> => { | ||
| const result: CliffAdapterResult[] = []; | ||
| const foundationOutflowData = await queryDune("5041975") | ||
| for (let i = 0; i < foundationOutflowData.length; i++) { | ||
| result.push({ | ||
| type: "cliff", | ||
| start: foundationOutflowData[i].start, | ||
| amount: foundationOutflowData[i].amount, | ||
| }) | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| const stakingRewards = async (): Promise<LinearAdapterResult[]> => { | ||
| const result: LinearAdapterResult[] = []; | ||
| const issuanceData = await queryDune("5041721") | ||
|
|
||
| for (let i = 0; i < issuanceData.length - 1; i++) { | ||
| result.push({ | ||
| type: "linear", | ||
| start: issuanceData[i + 1].timestamp, | ||
| end: issuanceData[i].timestamp, | ||
| amount: issuanceData[i].eth_issued | ||
| }) | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
|
|
||
| const ethereum: Protocol = { | ||
| "Crowd Sale": manualCliff(start, 6e7), | ||
| "Early Contributors": manualLinear( | ||
| start, | ||
| start + periodToSeconds.year * 4, | ||
| 6e6, | ||
| ), | ||
| "Ethereum Foundation": foundationOutflow, | ||
| "Issuance": [ | ||
| manualLinear(genesis, byzantiumFork, 4369999 * 5), | ||
| manualLinear(byzantiumFork, constantinopleFork, 2910000 * 3), | ||
| manualLinear(constantinopleFork, merge, 8257393 * 2), | ||
| uncle, | ||
| stakingRewards, | ||
| burnData | ||
| ], | ||
| meta: { | ||
| token: `${chain}:${GAS_TOKEN}`, | ||
| notes: [ | ||
| `Information on the Early Contributor vesting schedule structure could not be found, here we have assumed it as linearly unlocked over 4 years.`, | ||
| `The Ethereum Foundation supply is assumed to be unlocked when there's an outflow from EthDev address.`, | ||
| `Issuance is combination of PoW and PoS issuance, with Uncle Rewards and EIP-1559 burning included`, | ||
| ], | ||
| sources: [ | ||
| "https://dune.com/21co/ethereum-key-metrics", | ||
| "https://www.galaxy.com/insights/research/breakdown-of-ethereum-supply-distribution-since-genesis/", | ||
| "https://fastercapital.com/topics/common-token-vesting-strategies-for-airdrop-cryptocurrency.html", | ||
| ], | ||
| protocolIds: ["4488"], | ||
| }, | ||
| categories: { | ||
| farming: ["Issuance"], | ||
| insiders: ["Ethereum Foundation"], | ||
| publicSale: ["Crowd Sale"], | ||
| }, | ||
| }; | ||
| export default ethereum; |
This file contains hidden or 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,136 @@ | ||
| import { httpGet, httpPost } from "./fetchURL"; | ||
| import { getEnv } from "./env"; | ||
| const plimit = require('p-limit'); | ||
| const limit = plimit(1); | ||
|
|
||
| const isRestrictedMode = getEnv('DUNE_RESTRICTED_MODE') === 'true' | ||
| const API_KEYS = getEnv('DUNE_API_KEYS')?.split(',') ?? ["L0URsn5vwgyrWbBpQo9yS1E3C1DBJpZh"] | ||
| let API_KEY_INDEX = 0; | ||
|
|
||
| const NOW_TIMESTAMP = Math.trunc((Date.now()) / 1000) | ||
|
|
||
| const getLatestData = async (queryId: string) => { | ||
| checkCanRunDuneQuery() | ||
|
|
||
| const url = `https://api.dune.com/api/v1/query/${queryId}/results` | ||
| try { | ||
| const latest_result = (await limit(() => httpGet(url, { | ||
| headers: { | ||
| "x-dune-api-key": API_KEYS[API_KEY_INDEX] | ||
| } | ||
| }))) | ||
| const submitted_at = latest_result.submitted_at | ||
| const submitted_at_timestamp = Math.trunc(new Date(submitted_at).getTime() / 1000) | ||
| const diff = NOW_TIMESTAMP - submitted_at_timestamp | ||
| if (diff < 60 * 60 * 3) { | ||
| return latest_result.result.rows | ||
| } | ||
| return undefined | ||
| } catch (e: any) { | ||
| throw e; | ||
| } | ||
| } | ||
|
|
||
|
|
||
| async function randomDelay() { | ||
| const delay = Math.floor(Math.random() * 5) + 2 | ||
| return new Promise((resolve) => setTimeout(resolve, delay * 1000)) | ||
| } | ||
|
|
||
| const inquiryStatus = async (execution_id: string, queryId: string) => { | ||
| checkCanRunDuneQuery() | ||
|
|
||
| let _status = undefined; | ||
| do { | ||
| try { | ||
| _status = (await limit(() => httpGet(`https://api.dune.com/api/v1/execution/${execution_id}/status`, { | ||
| headers: { | ||
| "x-dune-api-key": API_KEYS[API_KEY_INDEX] | ||
| } | ||
| }))).state | ||
| if (['QUERY_STATE_PENDING', 'QUERY_STATE_EXECUTING'].includes(_status)) { | ||
| console.info(`waiting for query id ${queryId} to complete...`) | ||
| await randomDelay() // 1 - 4s | ||
| } | ||
| } catch (e: any) { | ||
| throw e; | ||
| } | ||
| } while (_status !== 'QUERY_STATE_COMPLETED' && _status !== 'QUERY_STATE_FAILED'); | ||
| return _status | ||
| } | ||
|
|
||
| const submitQuery = async (queryId: string, query_parameters = {}) => { | ||
| checkCanRunDuneQuery() | ||
|
|
||
| let query: undefined | any = undefined | ||
| try { | ||
| query = await limit(() => httpPost(`https://api.dune.com/api/v1/query/${queryId}/execute`, { query_parameters }, { | ||
| headers: { | ||
| "x-dune-api-key": API_KEYS[API_KEY_INDEX], | ||
| 'Content-Type': 'application/json' | ||
| } | ||
| })) | ||
| if (query?.execution_id) { | ||
| return query?.execution_id | ||
| } else { | ||
| throw new Error("error query data: " + query) | ||
| } | ||
| } catch (e: any) { | ||
| throw e; | ||
| } | ||
| } | ||
|
|
||
|
|
||
| export const queryDune = async (queryId: string, query_parameters: any = {}) => { | ||
| checkCanRunDuneQuery() | ||
|
|
||
| if (Object.keys(query_parameters).length === 0) { | ||
| const latest_result = await getLatestData(queryId) | ||
| 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 queryStatus = await limit(() => httpGet(`https://api.dune.com/api/v1/execution/${execution_id}/results?limit=100000`, { | ||
| headers: { | ||
| "x-dune-api-key": API_KEY | ||
| } | ||
| })) | ||
| 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) | ||
| } | ||
| throw new Error(`Dune query failed: ${queryId}`) | ||
| } | ||
| } | ||
|
|
||
| const tableName = { | ||
| bsc: "bnb", | ||
| ethereum: "ethereum", | ||
| base: "base", | ||
| avax: "avalanche_c" | ||
| } as any | ||
|
|
||
| export const queryDuneSql = (options: any, query: string) => { | ||
| checkCanRunDuneQuery() | ||
|
|
||
| return queryDune("3996608", { | ||
| fullQuery: query.replace("CHAIN", tableName[options.chain] ?? options.chain).split("TIME_RANGE").join(`block_time >= from_unixtime(${options.startTimestamp}) | ||
| AND block_time <= from_unixtime(${options.endTimestamp})`) | ||
| }) | ||
| } | ||
|
|
||
| export function checkCanRunDuneQuery() { | ||
| if (!isRestrictedMode) return; | ||
| const currentHour = new Date().getUTCHours(); | ||
| if (currentHour >= 1 && currentHour <= 3) return; // 1am - 3am - any time other than this, throw error | ||
| throw new Error(`Current hour is ${currentHour}. In restricted mode, can run dune queries only between 1am - 3am UTC`); | ||
| } |
This file contains hidden or 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,25 @@ | ||
| const BOOL_KEYS = [ | ||
| '' | ||
| ] | ||
|
|
||
| const DEFAULTS: any = { | ||
| BITLAYER_RPC: "https://rpc.bitlayer.org,https://rpc.ankr.com/bitlayer,https://rpc.bitlayer-rpc.com,https://rpc-bitlayer.rockx.com", | ||
| } | ||
|
|
||
| export const ENV_KEYS = new Set([ | ||
| ...BOOL_KEYS, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wont this cause a crash?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it doesn't because i put '' in the array |
||
| ...Object.keys(DEFAULTS), | ||
| 'DUNE_API_KEYS', | ||
| 'DUNE_RESTRICTED_MODE' | ||
| ]) | ||
|
|
||
| Object.keys(DEFAULTS).forEach(i => { | ||
| if (!process.env[i]) process.env[i] = DEFAULTS[i] // this is done to set the chain RPC details in @defillama/sdk | ||
| }) | ||
|
|
||
|
|
||
| export function getEnv(key: string): any { | ||
| if (!ENV_KEYS.has(key)) throw new Error(`Unknown env key: ${key}`) | ||
| const value = process.env[key] ?? DEFAULTS[key] | ||
| return BOOL_KEYS.includes(key) ? !!value : value | ||
| } | ||
This file contains hidden or 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, { AxiosRequestConfig } from "axios" | ||
|
|
||
| export default async function fetchURL(url: string, retries = 3): Promise<any> { | ||
| try { | ||
| const res = await httpGet(url) | ||
| return res | ||
| } catch (error) { | ||
| if (retries > 0) return fetchURL(url, retries - 1) | ||
| throw error | ||
| } | ||
| } | ||
|
|
||
| export async function postURL(url: string, data: any, retries = 3, options?: AxiosRequestConfig): Promise<any> { | ||
| try { | ||
| const res = await httpPost(url, data, options) | ||
| return res | ||
| } catch (error) { | ||
| if (retries > 0) return postURL(url, data, retries - 1, options) | ||
| throw error | ||
| } | ||
| } | ||
|
|
||
| function formAxiosError(url: string, error: any, options?: any) { | ||
| let e = new Error((error as any)?.message) | ||
| const axiosError = (error as any)?.response?.data?.message || (error as any)?.response?.data?.error || (error as any)?.response?.statusText || (error as any)?.response?.data; | ||
| (e as any).url = url; | ||
| Object.keys(options || {}).forEach((key) => (e as any)[key] = options[key]); | ||
| if (axiosError) (e as any).axiosError = axiosError; | ||
| delete (e as any).stack | ||
| return e | ||
| } | ||
|
|
||
| const successCodes: number[] = [200, 201, 202, 203, 204, 205, 206, 207, 208, 226]; | ||
| export async function httpGet(url: string, options?: AxiosRequestConfig, { withMetadata = false } = {}) { | ||
| try { | ||
| const res = await axios.get(url, options) | ||
| if (!successCodes.includes(res.status)) throw new Error(`Error fetching ${url}: ${res.status} ${res.statusText}`) | ||
| if (!res.data) throw new Error(`Error fetching ${url}: no data`) | ||
| if (withMetadata) return res | ||
| return res.data | ||
| } catch (error) { | ||
| throw formAxiosError(url, error, { method: 'GET' }) | ||
| } | ||
| } | ||
|
|
||
| export async function httpPost(url: string, data: any, options?: AxiosRequestConfig, { withMetadata = false } = {}) { | ||
| try { | ||
| const res = await axios.post(url, data, options) | ||
| if (!successCodes.includes(res.status)) throw new Error(`Error fetching ${url}: ${res.status} ${res.statusText}`) | ||
| if (!res.data) throw new Error(`Error fetching ${url}: no data`) | ||
| return res.data | ||
| } catch (error) { | ||
| if (withMetadata) throw error | ||
| throw formAxiosError(url, error, { method: 'POST' }) | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.