Skip to content
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

icpswap #638

Merged
merged 1 commit into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions dexs/icpswap/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { Adapter, FetchResultVolume } from "../../adapters/types"
import { CHAIN } from "../../helpers/chains";
import { getUniqStartOfTodayTimestamp } from "../../helpers/getUniSubgraphVolume";
import { getTimestampAtStartOfDayUTC } from "../../utils/date";
import fetchURL from "../../utils/fetchURL";

const pairsURL = "https://uvevg-iyaaa-aaaak-ac27q-cai.raw.ic0.app/pairs";
const volumeURL = (pool_id: string) => `https://uvevg-iyaaa-aaaak-ac27q-cai.raw.ic0.app/totalVolumeUSD?poolId=${pool_id}&limit=1000`;

interface IPairs {
pool_id: string;
}

interface IVolume {
day: string;
totalVolumeUSD: string;
volumeUSDChange: string;
volumeUSD: string;
}

const fetch = async (timestamp: number): Promise<FetchResultVolume> => {
const pairs: IPairs[] = (await fetchURL(pairsURL)).data;
const pools = pairs.map((e: IPairs) => e.pool_id);
const todayTimestamp = getUniqStartOfTodayTimestamp(new Date(timestamp * 1000));
const dateId = Math.floor(getTimestampAtStartOfDayUTC(todayTimestamp) / 86400)
const historicalVolume: IVolume[] = (await Promise.all(pools.map((e: string) => fetchURL(volumeURL(e))))).map((e: any) => e.data).flat();
const dailyVolume = historicalVolume.filter((e: IVolume) => Number(e.day) === dateId)
.reduce((a: number, b: IVolume) => a + Number(b.volumeUSD), 0)
const totalVolume = historicalVolume.filter((e: IVolume) => Number(e.day) <= dateId)
.reduce((a: number, b: IVolume) => a + Number(b.totalVolumeUSD), 0)
return {
dailyVolume: `${dailyVolume}`,
totalVolume: `${totalVolume}`,
timestamp
}
}


const adapter: Adapter = {
adapter: {
[CHAIN.ICP]: {
fetch: fetch,
start: async () => 1689465600,
},
}
}

export default adapter;
3 changes: 2 additions & 1 deletion helpers/chains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ export enum CHAIN {
PULSECHAIN = "pulse",
ONUS = "onus",
OASIS = "oasis",
MANTLE = "mantle"
MANTLE = "mantle",
ICP="icp"
}

// Don´t use
Expand Down
Loading