Skip to content

Commit

Permalink
replace usage of getPrices
Browse files Browse the repository at this point in the history
  • Loading branch information
g1nt0ki committed Feb 10, 2024
1 parent 93a5296 commit 36c898f
Show file tree
Hide file tree
Showing 13 changed files with 206 additions and 354 deletions.
1 change: 1 addition & 0 deletions adapters/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export type FetchOptions = {
getLogs: (params: FetchGetLogsOptions) => Promise<any[]>;
toTimestamp: number;
fromTimestamp: number;
startOfDay: number;
getFromBlock: () => Promise<number>;
getToBlock: () => Promise<number>;
chain: string,
Expand Down
7 changes: 5 additions & 2 deletions adapters/utils/runAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Balances, ChainApi, getEventLogs, getProvider } from '@defillama/sdk'
import { BaseAdapter, ChainBlocks, DISABLED_ADAPTER_KEY, FetchGetLogsOptions, FetchResultGeneric, } from '../types'
import { BaseAdapter, ChainBlocks, DISABLED_ADAPTER_KEY, FetchGetLogsOptions, FetchOptions, FetchResultGeneric, } from '../types'
import { getBlock } from "../../helpers/getBlock";
import { getUniqStartOfTodayTimestamp } from '../../helpers/getUniSubgraphFees';

const ONE_DAY_IN_SECONDS = 60 * 60 * 24

Expand Down Expand Up @@ -47,7 +48,7 @@ export default async function runAdapter(volumeAdapter: BaseAdapter, cleanCurren
}
}

async function getOptionsObject(timestamp: number, chain: string, chainBlocks: ChainBlocks) {
async function getOptionsObject(timestamp: number, chain: string, chainBlocks: ChainBlocks): Promise<FetchOptions> {
const withinTwoHours = Math.trunc(Date.now() / 1000) - timestamp < 2 * 60 * 60 // 2 hours
const createBalances: () => Balances = () => {
return new Balances({ timestamp: closeToCurrentTime ? undefined : timestamp, chain })
Expand Down Expand Up @@ -75,6 +76,7 @@ export default async function runAdapter(volumeAdapter: BaseAdapter, cleanCurren
}
const fromApi = new ChainApi({ chain, timestamp: fromTimestamp, block: fromBlock })
const api = new ChainApi({ chain, timestamp: withinTwoHours ? undefined : timestamp, block: toBlock })
const startOfDay = getUniqStartOfTodayTimestamp(new Date(timestamp * 1000))

return {
createBalances,
Expand All @@ -87,6 +89,7 @@ export default async function runAdapter(volumeAdapter: BaseAdapter, cleanCurren
chain,
fromApi,
api,
startOfDay,
}
}

Expand Down
2 changes: 1 addition & 1 deletion cli/interactive.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ async function runAdapter(adapterPath, debugMode) {
}

const startTime = Date.now()
const child = childProcess.spawn('ts-node', ['--transpile-only', 'cli/testAdapter.ts', ...adapterPath.split('/')], {
const child = childProcess.spawn('npx', ['ts-node', '--transpile-only', 'cli/testAdapter.ts', ...adapterPath.split('/')], {
env,
})

Expand Down
98 changes: 34 additions & 64 deletions dexs/ambient/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
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";
import { getBlock } from "../../helpers/getBlock";
import * as sdk from "@defillama/sdk";
import { type } from "os";
import { Adapter, ChainBlocks, FetchOptions, FetchResultVolume } from "../../adapters/types";
import { CHAIN } from "../../helpers/chains";
import { Chain } from "@defillama/sdk/build/general";
import { request, } from "graphql-request";

type TEndpoint = {
[s: Chain | string]: string;
}
const endpoints: TEndpoint = {
[CHAIN.ETHEREUM]: "https://api.thegraph.com/subgraphs/name/crocswap/croc-mainnet",
[CHAIN.ETHEREUM]: "https://api.thegraph.com/subgraphs/name/crocswap/croc-mainnet",
}
interface IPool {
quote: string;
Expand All @@ -22,12 +17,12 @@ interface ISwap {
pool: IPool;
dex: string;
}

const toPositive = (n: any) => +n > 0 ? +n : n * -1

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`
return async (timestamp: number, _: ChainBlocks, { fromTimestamp, toTimestamp, createBalances, }: FetchOptions): Promise<FetchResultVolume> => {
const query = `
{
swaps(where: {
time_gte: ${fromTimestamp}
Expand All @@ -42,19 +37,13 @@ const graphs = (chain: Chain) => {
}
}
`
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 || 0;
const price = prices[`${chain}:${e.pool.quote.toLowerCase()}`]?.price || 0;
return (Number(e.quoteFlow.replace('-','')) / 10 ** decimals) * price
}).reduce((a: number, b: number) => a + b, 0)
return {
dailyVolume: `${dailyVolume}`,
timestamp,
};
}
const graphRes: ISwap[] = (await request(endpoints[chain], query)).swaps;
const dailyVolume = createBalances()
graphRes.map((e: ISwap) => {
dailyVolume.add(e.pool.quote, toPositive(e.quoteFlow))
})
return { dailyVolume, timestamp, }
}
}

const swapEvent = 'event CrocSwap (address indexed base, address indexed quote, uint256 poolIdx, bool isBuy, bool inBaseQty, uint128 qty, uint16 tip, uint128 limitPrice, uint128 minOut, uint8 reserveFlags, int128 baseFlow, int128 quoteFlow)';
Expand All @@ -67,48 +56,29 @@ interface ILog {
quoteFlow: string;
}
const contract_address: TContractAddress = {
[CHAIN.SCROLL]: '0xaaaaaaaacb71bf2c8cae522ea5fa455571a74106'
[CHAIN.SCROLL]: '0xaaaaaaaacb71bf2c8cae522ea5fa455571a74106',
}

const fetchVolume = (chain: Chain) => {
return async (timestamp: number): Promise<FetchResultVolume> => {
const toTimestamp = getTimestampAtStartOfDayUTC(timestamp)
const fromTimestamp = toTimestamp - 60 * 60 * 24
const balances = new sdk.Balances({ chain, timestamp })
const fromBlock = await getBlock(fromTimestamp, chain, {})
const toBlock = await getBlock(toTimestamp, chain, {})

const logs: ILog[] = (await sdk.getEventLogs({
target: contract_address[chain],
toBlock: toBlock,
fromBlock: fromBlock,
chain,
eventAbi: swapEvent,
flatten: false,
onlyArgs: true,
})) as ILog[];
logs.forEach((log: ILog) => {
balances.add(log.quote, log.quoteFlow)
});
return {
dailyVolume: await balances.getUSDString(),
timestamp,
}
}
return async (timestamp: number, _: ChainBlocks, { getLogs, createBalances, }: FetchOptions): Promise<FetchResultVolume> => {
const dailyVolume = createBalances()
const logs: ILog[] = await getLogs({ target: contract_address[chain], eventAbi: swapEvent, })
logs.forEach((log: ILog) => dailyVolume.add(log.quote, Number(log.quoteFlow) < 0 ? 0 : log.quoteFlow));
return { dailyVolume, timestamp, }
}
}


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

export default adapter;
36 changes: 12 additions & 24 deletions dexs/aux-exchange/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FetchResultVolume, SimpleAdapter } from "../../adapters/types";
import { ChainBlocks, FetchOptions, FetchResultVolume, SimpleAdapter } from "../../adapters/types";
import { CHAIN } from "../../helpers/chains";
import { getPrices } from "../../utils/prices";
import { httpGet } from "../../utils/fetchURL";
Expand Down Expand Up @@ -30,9 +30,8 @@ const getResources = async (account: string): Promise<any[]> => {
return data
}

const fetchVolume = async (timestamp: number): Promise<FetchResultVolume> => {
const fromTimestamp = timestamp - 86400;
const toTimestamp = timestamp;
const fetchVolume = async (timestamp: number, _: ChainBlocks, { fromTimestamp, toTimestamp, createBalances, }: FetchOptions): Promise<FetchResultVolume> => {
const dailyVolume = createBalances();
const account_resource: any[] = (await getResources(account))
const pools = account_resource.filter(e => e.type?.includes('amm::Pool'))
.map((e: any) => {
Expand All @@ -49,24 +48,13 @@ const fetchVolume = async (timestamp: number): Promise<FetchResultVolume> => {
}
})

const logs_swap: ISwapEventData[] = (await Promise.all(pools.map(p => getSwapEvent(p, fromTimestamp)))).flat()
.filter(e => toUnixTime(e.timestamp) > fromTimestamp && toUnixTime(e.timestamp) < toTimestamp)
const coins = [...new Set([...logs_swap.map(p => `${CHAIN.APTOS}:${p.in_coin_type}`), ...logs_swap.map(p => `${CHAIN.APTOS}:${p.out_coin_type}`)])]
const price = (await getPrices(coins, timestamp));
const dailyVolume = logs_swap.map((e: ISwapEventData) => {
const token0Price = price[`${CHAIN.APTOS}:${e.in_coin_type}`]?.price || 0;
const token1Price = price[`${CHAIN.APTOS}:${e.out_coin_type}`]?.price || 0;
const token0Decimals = price[`${CHAIN.APTOS}:${e.in_coin_type}`]?.decimals || 0;
const token1Decimals = price[`${CHAIN.APTOS}:${e.out_coin_type}`]?.decimals || 0;
const in_au = (Number(e.in_au) / 10 ** token0Decimals) * token0Price;
const out_au = (Number(e.out_au) / 10 ** token1Decimals) * token1Price;
return token0Price ? in_au : out_au;
}).reduce((a: number, b: number) => a + b, 0)
const logs_swap: ISwapEventData[] = (await Promise.all(pools.map(p => getSwapEvent(p, fromTimestamp)))).flat()
.filter(e => toUnixTime(e.timestamp) > fromTimestamp && toUnixTime(e.timestamp) < toTimestamp)
logs_swap.map((e: ISwapEventData) => {
dailyVolume.add(e.out_coin_type, e.out_au)
})

return {
timestamp,
dailyVolume: dailyVolume.toString(),
}
return { timestamp, dailyVolume, }
}

const getSwapEvent = async (pool: any, fromTimestamp: number): Promise<ISwapEventData[]> => {
Expand All @@ -77,12 +65,12 @@ const getSwapEvent = async (pool: any, fromTimestamp: number): Promise<ISwapEven
const getEventByCreation = `${APTOS_PRC}/v1/accounts/${account}/events/${pool.swap_events.creation_num}?start=${start}&limit=25`;
try {
const event: any[] = (await httpGet(getEventByCreation));
const listSequence: number[] = event.map(e => Number(e.sequence_number))
const listSequence: number[] = event.map(e => Number(e.sequence_number))
swap_events.push(...event)
const lastMin = Math.min(...listSequence)
if (lastMin >= Infinity || lastMin <= -Infinity) break;
const lastTimestamp = event.find(e => Number(e.sequence_number) === lastMin)?.data.timestamp
const lastTimestampNumber = Number((Number(lastTimestamp)/1e6).toString().split('.')[0])
const lastTimestampNumber = Number((Number(lastTimestamp) / 1e6).toString().split('.')[0])
if (lastTimestampNumber < fromTimestamp) break;
start = lastMin - 26 > 0 ? lastMin - 26 : 0;
} catch {
Expand All @@ -92,7 +80,7 @@ const getSwapEvent = async (pool: any, fromTimestamp: number): Promise<ISwapEven
}
return swap_events.map(e => e.data)
}
const toUnixTime = (timestamp: string) => Number((Number(timestamp)/1e6).toString().split('.')[0])
const toUnixTime = (timestamp: string) => Number((Number(timestamp) / 1e6).toString().split('.')[0])

const adapter: SimpleAdapter = {
adapter: {
Expand Down
32 changes: 14 additions & 18 deletions dexs/bisq/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import fetchURL from "../../utils/fetchURL"
import { SimpleAdapter } from "../../adapters/types";
import { ChainBlocks, FetchOptions, SimpleAdapter } from "../../adapters/types";
import { CHAIN } from "../../helpers/chains";
import { getUniqStartOfTodayTimestamp } from "../../helpers/getUniSubgraphVolume";
import { getPrices } from "../../utils/prices";

const historicalVolumeEndpoint = "https://bisq.markets/bisq/api/markets/volumes?interval=day"

Expand All @@ -11,24 +9,22 @@ interface IVolumeall {
period_start: number;
}

const fetch = async (timestamp: number) => {
const dayTimestamp = getUniqStartOfTodayTimestamp(new Date(timestamp * 1000))
const historicalVolume: IVolumeall[] = (await fetchURL(historicalVolumeEndpoint));
const totalVolume = historicalVolume
.filter(volItem => volItem.period_start <= dayTimestamp)
.reduce((acc, { volume }) => acc + Number(volume), 0)
const fetch = async (timestamp: number, _: ChainBlocks, {startOfDay, createBalances, }: FetchOptions) => {
const totalVolume = createBalances()
const dailyVolume = createBalances()

const dailyVolume = historicalVolume
.find(dayItem => dayItem.period_start === dayTimestamp)?.volume
const historicalVolume: IVolumeall[] = (await fetchURL(historicalVolumeEndpoint));
historicalVolume
.filter(volItem => volItem.period_start <= startOfDay)
.map(({ volume }) => totalVolume.addCGToken('bitcoin', +volume))

const coinId = "coingecko:bitcoin";
const prices = await getPrices([coinId], dayTimestamp)
const dailyVol = historicalVolume
.find(dayItem => dayItem.period_start === startOfDay)?.volume
dailyVolume.addCGToken('bitcoin', +(dailyVol as any))

return {
totalVolume: totalVolume ? String(Number(totalVolume) * prices[coinId].price) : "0",
dailyVolume: dailyVolume ? String(Number(dailyVolume) * prices[coinId].price) : "0",
timestamp: dayTimestamp,
};
return {
// totalVolume,
dailyVolume, timestamp: startOfDay };
};

const adapter: SimpleAdapter = {
Expand Down
Loading

0 comments on commit 36c898f

Please sign in to comment.