Skip to content

Commit

Permalink
Merge pull request #565 from DefiLlama/ambient
Browse files Browse the repository at this point in the history
add ambient
  • Loading branch information
dtmkeng committed Jun 20, 2023
2 parents a0e5cb5 + 8198c9e commit 25f8549
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions dexs/ambient/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import {Adapter, FetchResultVolume} from "../../adapters/types";
import {CHAIN} from "../../helpers/chains";
import {Chain} from "@defillama/sdk/build/general";
import {request, gql} from "graphql-request";
import {getTimestampAtStartOfDayUTC} from "../../utils/date";
import { getPrices } from "../../utils/prices";

type TEndpoint = {
[s: Chain | string]: string;
}
const endpoints: TEndpoint = {
[CHAIN.ETHEREUM]: "https://api.thegraph.com/subgraphs/name/crocswap/croc-mainnet",
}
interface IPool {
quote: string;
}
interface ISwap {
quoteFlow: string;
pool: IPool;
dex: string;
}
const graphs = (chain: Chain) => {
return async (timestamp: number): Promise<FetchResultVolume> => {
const todaysTimestamp = getTimestampAtStartOfDayUTC(timestamp)
const fromTimestamp = todaysTimestamp - 60 * 60 * 24
const toTimestamp = todaysTimestamp
const query = gql`
{
swaps(where: {
time_gte: ${fromTimestamp}
time_lte: ${toTimestamp}
dex: "croc"
}, orderBy:time, orderDirection: desc) {
quoteFlow
pool {
quote
}
dex
}
}
`
const graphRes: ISwap[] = (await request(endpoints[chain], query)).swaps;
const coins = [...new Set(graphRes.map((e: ISwap) => `${chain}:${e.pool.quote.toLowerCase()}`))]
const prices = await getPrices(coins, todaysTimestamp);
const dailyVolume = graphRes.map((e: ISwap) => {
const decimals = prices[`${chain}:${e.pool.quote.toLowerCase()}`].decimals;
const price = prices[`${chain}:${e.pool.quote.toLowerCase()}`].price;
return (Number(e.quoteFlow.replace('-','')) / 10 ** decimals) * price
}).reduce((a: number, b: number) => a + b, 0)
return {
dailyVolume: `${dailyVolume}`,
timestamp,
};
}
}


const adapter: Adapter = {
adapter: {
[CHAIN.ETHEREUM]: {
fetch: graphs(CHAIN.ETHEREUM),
start: async () => 1685232000,
},
}
}

export default adapter;

0 comments on commit 25f8549

Please sign in to comment.