forked from DefiLlama/dimension-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.
Merge pull request DefiLlama#1254 from sbalcin/master
blastfutures volume adapter
- Loading branch information
Showing
1 changed file
with
51 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import fetchURL from "../../utils/fetchURL" | ||
import { FetchResultVolume, SimpleAdapter } from "../../adapters/types"; | ||
|
||
const historicalVolumeEndpoint = "https://api.bfx.trade/markets" | ||
const candles = (market: string, timestampFrom: number, timestampTo: number) => { | ||
const url = `https://api.bfx.trade/candles?market_id=${market}×tamp_from=${timestampFrom}×tamp_to=${timestampTo}&period=1440`; | ||
return url; | ||
|
||
} | ||
|
||
interface IVolumeall { | ||
volume: string; | ||
time: string; | ||
close: string; | ||
} | ||
|
||
const fetchVolume = async (timestamp: number): Promise<FetchResultVolume> => { | ||
const fromTimestamp = timestamp - 60 * 60 * 24 | ||
const toTimestamp = timestamp | ||
|
||
// Get market data | ||
const response = await fetchURL(historicalVolumeEndpoint); | ||
const marketsData = response.result; | ||
|
||
// Fetch candles for each USD market | ||
const historical: IVolumeall[] = (await Promise.all(marketsData.map((market: any) => fetchURL(candles(market.id, fromTimestamp, toTimestamp))))) | ||
.map((e: any) => e.result) | ||
.flat(); | ||
|
||
// Calculate daily volume | ||
const dailyVolume = historical | ||
.filter((e: IVolumeall) => Number(e.time) >= fromTimestamp ) | ||
.filter((e: IVolumeall) => Number(e.time) <= toTimestamp) | ||
.reduce((a: number, b: IVolumeall) => a + Number(b.volume), 0) | ||
|
||
return { | ||
dailyVolume: dailyVolume ? `${dailyVolume}` : undefined, | ||
timestamp: timestamp, | ||
}; | ||
}; | ||
|
||
const adapter: SimpleAdapter = { | ||
adapter: { | ||
"blast": { | ||
fetch: fetchVolume, | ||
start: 1700179200, // Replace with actual start timestamp | ||
}, | ||
}, | ||
}; | ||
|
||
export default adapter; |