forked from DefiLlama/DefiLlama-Adapters
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Small cleanup dexVolumes folder (DefiLlama#3443)
* Add .vscode folder to .gitignore * Add v0 test script * Fix types * Fix chain blocks type * Change dex volumes readme * Fix ts errors * Remove workbench.colorCustomizations * Finally fix ts errors * Chore test script * Revert "Remove workbench.colorCustomizations" This reverts commit 8e87210. * Revert back since it doesn't solve the issue * Upgrade trilom/file-changes-action from 1.2.3 -> 1.2.4
- Loading branch information
Showing
20 changed files
with
221 additions
and
113 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ jobs: | |
runs-on: ubuntu-latest | ||
steps: | ||
- id: file_changes | ||
uses: trilom/[email protected].3 | ||
uses: trilom/[email protected].4 | ||
with: | ||
output: 'json' | ||
fileOutput: 'json' | ||
|
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 |
---|---|---|
|
@@ -8,3 +8,4 @@ historical-data.js | |
yarn.lock | ||
.DS_Store | ||
projects/pooltogether/index.js | ||
.vscode |
This file was deleted.
Oops, something went wrong.
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,7 @@ | ||
# DEX volumes | ||
|
||
> **_NOTE:_** Under developement. | ||
## Test an adapter | ||
|
||
`npm run test-dex 1inch` |
This file was deleted.
Oops, something went wrong.
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,63 @@ | ||
import * as path from 'path' | ||
import type { ChainBlocks, DexAdapter, VolumeAdapter } from '../dexVolume.type'; | ||
import { chainsForBlocks } from "@defillama/sdk/build/computeTVL/blocks"; | ||
import { Chain } from '@defillama/sdk/build/general'; | ||
import handleError from '../../utils/handleError'; | ||
import { checkArguments, getLatestBlockRetry, printVolumes } from './utils'; | ||
|
||
// Add handler to rejections/exceptions | ||
process.on('unhandledRejection', handleError) | ||
process.on('uncaughtException', handleError) | ||
|
||
// Check if all arguments are present | ||
checkArguments(process.argv) | ||
|
||
// Get path of module import | ||
const passedFile = path.resolve(process.cwd(), `dexVolumes/${process.argv[2]}`); | ||
(async () => { | ||
console.info(`Running ${process.argv[2].toUpperCase()} adapter`) | ||
// Import module to test | ||
let module: DexAdapter = (await import(passedFile)).default | ||
|
||
if ("volume" in module) { | ||
// Get adapter | ||
const volumeAdapter = module.volume | ||
const volumes = await runAdapter(volumeAdapter) | ||
printVolumes(volumes) | ||
} else if ("breakdown" in module) { | ||
const breakdownAdapter = module.breakdown | ||
const allVolumes = await Promise.all(Object.entries(breakdownAdapter).map(async ([version, adapter]) => | ||
await runAdapter(adapter).then(res => ({ version, res })) | ||
)) | ||
allVolumes.forEach((promise) => { | ||
console.info(promise.version) | ||
printVolumes(promise.res) | ||
}) | ||
} else console.info("No compatible adapter found") | ||
})() | ||
|
||
async function runAdapter(volumeAdapter: VolumeAdapter) { | ||
// Get chains to check | ||
const chains = Object.keys(volumeAdapter).filter(item => typeof volumeAdapter[item] === 'object'); | ||
// Get lastest block | ||
const chainBlocks: ChainBlocks = {}; | ||
await Promise.all( | ||
chains.map(async (chainRaw) => { | ||
const chain: Chain = chainRaw === "ava" ? "avax" : chainRaw as Chain | ||
if (chainsForBlocks.includes(chain as Chain) || chain === "ethereum") { | ||
const latestBlock = await getLatestBlockRetry(chain) | ||
if (!latestBlock) throw new Error("latestBlock") | ||
chainBlocks[chain] = latestBlock.number - 10 | ||
} | ||
}) | ||
); | ||
// Get volumes | ||
const unixTimestamp = Math.round(Date.now() / 1000) - 60; | ||
const volumes = await Promise.all(Object.keys(chainBlocks).map( | ||
async chain => volumeAdapter[chain].fetch(unixTimestamp, chainBlocks) | ||
.then(res => { | ||
return { timestamp: unixTimestamp, totalVolume: res.totalVolume, dailyVolume: res.dailyVolume } | ||
}) | ||
)) | ||
return volumes | ||
} |
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,29 @@ | ||
import { getLatestBlock } from "@defillama/sdk/build/util"; | ||
import { FetchResult } from "../dexVolume.type"; | ||
|
||
export function checkArguments(argv: string[]) { | ||
if (argv.length < 3) { | ||
console.error(`Missing argument, you need to provide the filename of the adapter to test. | ||
Eg: ts-node dexVolumes/cli/testAdapter.js dexVolumes/myadapter.js`); | ||
process.exit(1); | ||
} | ||
} | ||
|
||
export async function getLatestBlockRetry(chain: string) { | ||
for (let i = 0; i < 5; i++) { | ||
try { | ||
return await getLatestBlock(chain); | ||
} catch (e) { | ||
throw new Error(`Couln't get block heights for chain "${chain}"\n${e}`); | ||
} | ||
} | ||
} | ||
|
||
export function printVolumes(volumes: FetchResult[]) { | ||
volumes.forEach(element => { | ||
console.info("----------") | ||
console.info(`Daily: ${element.dailyVolume}`) | ||
console.info(`Total: ${element.totalVolume}`) | ||
console.info("----------") | ||
}); | ||
} |
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
export type ChainBlocks = { | ||
[x: string]: number; | ||
[x: string]: number | ||
}; | ||
|
||
export type FetchResult = { | ||
|
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,46 @@ | ||
import { request, gql } from "graphql-request"; | ||
|
||
import { DEFAULT_DAILY_VOLUME_FACTORY, DEFAULT_DAILY_VOLUME_FIELD } from "./getUniSubgraphVolume"; | ||
|
||
interface IGetStartTimestamp { | ||
endpoints: { | ||
[chain: string]: string; | ||
} | ||
chain: string | ||
dailyDataField: string | ||
volumeField: string | ||
dateField?: string | ||
first?: number | ||
} | ||
|
||
const getStartTimestamp = | ||
({ | ||
endpoints, | ||
chain, | ||
dailyDataField = `${DEFAULT_DAILY_VOLUME_FACTORY}s`, | ||
volumeField = DEFAULT_DAILY_VOLUME_FIELD, | ||
dateField = "date", | ||
first = 1000, | ||
}: IGetStartTimestamp) => | ||
async () => { | ||
const query = gql` | ||
{ | ||
${dailyDataField}(first: ${first}) { | ||
${dateField} | ||
${volumeField} | ||
} | ||
} | ||
`; | ||
|
||
const result = await request(endpoints[chain], query); | ||
|
||
const days = result?.[dailyDataField]; | ||
|
||
const firstValidDay = days.find((data: any) => data[volumeField] !== "0"); | ||
|
||
return firstValidDay[dateField]; | ||
}; | ||
|
||
export { | ||
getStartTimestamp, | ||
}; |
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
Oops, something went wrong.