Skip to content

Commit

Permalink
Add skipCacheRead option to getLogs function
Browse files Browse the repository at this point in the history
  • Loading branch information
g1nt0ki committed Feb 6, 2024
1 parent 3bae5a8 commit 12e06dd
Show file tree
Hide file tree
Showing 29 changed files with 314 additions and 1,686 deletions.
1 change: 1 addition & 0 deletions adapters/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export type FetchGetLogsOptions = {
toBlock?: number,
flatten?: boolean,
cacheInCloud?: boolean,
skipCacheRead?: boolean,
topics?: string[],
}

Expand Down
4 changes: 2 additions & 2 deletions adapters/utils/runAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ export default async function runAdapter(volumeAdapter: BaseAdapter, cleanCurren
const fromChainBlocks = {}
const getFromBlock = async () => await getBlock(fromTimestamp, chain, fromChainBlocks)
const getToBlock = async () => await getBlock(toTimestamp, chain, chainBlocks)
const getLogs = async ({ target, targets, onlyArgs = true, fromBlock, toBlock, flatten = true, eventAbi, topics, topic, cacheInCloud = false, }: FetchGetLogsOptions) => {
const getLogs = async ({ target, targets, onlyArgs = true, fromBlock, toBlock, flatten = true, eventAbi, topics, topic, cacheInCloud = false, skipCacheRead = false,}: FetchGetLogsOptions) => {
fromBlock = fromBlock ?? await getFromBlock()
toBlock = toBlock ?? await getToBlock()

return getEventLogs({ fromBlock, toBlock, chain, target, targets, onlyArgs, flatten, eventAbi, topics, topic, cacheInCloud, })
return getEventLogs({ fromBlock, toBlock, chain, target, targets, onlyArgs, flatten, eventAbi, topics, topic, cacheInCloud, skipCacheRead, })
}

return {
Expand Down
39 changes: 8 additions & 31 deletions aggregators/aggre/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { Chain } from "@defillama/sdk/build/general";
import { FetchResultAggregators, SimpleAdapter } from "../../adapters/types";
import { getBlock } from "../../helpers/getBlock";
import { FetchOptions, FetchResultAggregators, SimpleAdapter } from "../../adapters/types";
import { CHAIN } from "../../helpers/chains";
import * as sdk from "@defillama/sdk";

let abi = ["event SwapExecuted(address indexed user, address tokenIn, address tokenOut, uint amountIn, uint amountOut, uint swapType)"];

Expand All @@ -14,42 +12,21 @@ const contract: IContract = {
[CHAIN.SCROLL]: '0xcf8bcaCb401C31774EA39296b367B9DaB4F72267',
}

const fetch = (chain: Chain) => {
return async (timestamp: number): Promise<FetchResultAggregators> => {
const fromTimestamp = timestamp - 60 * 60 * 24;
const toTimestamp = timestamp;


const api = new sdk.ChainApi({ chain, timestamp });
const fromBlock = (await getBlock(fromTimestamp, chain, {}));
const toBlock = (await getBlock(toTimestamp, chain, {}));
const logs = (await api.getLogs({
target: contract[chain],
toBlock: toBlock,
fromBlock: fromBlock,
chain,
eventAbi: abi[0],
onlyArgs: true,
}))

logs.map((parsed: any) => {
api.add(parsed.tokenOut, parsed.amountOut)
});
const VUSD = Number(await api.getUSDValue()).toFixed(0);
const fetch: any = async (timestamp: number, _, { getLogs, createBalances, chain, }: FetchOptions): Promise<FetchResultAggregators> => {
const dailyVolume = createBalances();
const logs = (await getLogs({ target: contract[chain], eventAbi: abi[0], }))

return {
dailyVolume: VUSD,
timestamp,
};
};
logs.map((log: any) => dailyVolume.add(log.tokenOut, log.amountOut));

return { dailyVolume, timestamp, };
};

const adapter: SimpleAdapter = {
adapter: Object.keys(contract).reduce((acc, chain) => {
return {
...acc,
[chain]: {
fetch: fetch(chain),
fetch,
start: 1698660910,
}
}
Expand Down
43 changes: 10 additions & 33 deletions aggregators/jumper-exchange/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Chain } from "@defillama/sdk/build/general";
import { FetchResultVolume, SimpleAdapter } from "../../adapters/types";
import { FetchOptions, FetchResultVolume, SimpleAdapter } from "../../adapters/types";
import { CHAIN } from "../../helpers/chains";
import * as sdk from "@defillama/sdk";

type IContract = {
[c: string | Chain]: string;
Expand All @@ -18,44 +17,22 @@ const contract: IContract = {
[CHAIN.POLYGON_ZKEVM]: '0x1231deb6f5749ef6ce6943a275a1d3e7486f4eae',
[CHAIN.FANTOM]: '0x1231deb6f5749ef6ce6943a275a1d3e7486f4eae'
}
interface IData {
integrator: string;
fromAssetId: string;
toAssetId: string;
fromAmount: number;
toAmount: number;
}

const fetch = (chain: Chain) => {
return async (timestamp: number): Promise<FetchResultVolume> => {
const fromTimestamp = timestamp - 60 * 60 * 24;
const toTimestamp = timestamp;
const api = new sdk.ChainApi({ chain, timestamp });
const data: IData[] = (await api.getLogs({
target: contract[chain],
fromTimestamp, toTimestamp,
chain: chain,
onlyArgs: true,
eventAbi: 'event LiFiGenericSwapCompleted(bytes32 indexed transactionId, string integrator, string referrer, address receiver, address fromAssetId, address toAssetId, uint256 fromAmount, uint256 toAmount)'
})) as any
console.log(data.length, chain)
data.forEach((e: IData) => api.add(e.toAssetId, e.toAmount));
const { usdTvl } = await api.getUSDJSONs()
return {
dailyVolume: Number(usdTvl).toFixed(0),
timestamp,
} as any;
};
const fetch: any = async (timestamp: number, _, { chain, getLogs, createBalances, }: FetchOptions): Promise<FetchResultVolume> => {
const dailyVolume = createBalances();
const data: any[] = await getLogs({
target: contract[chain],
eventAbi: 'event LiFiGenericSwapCompleted(bytes32 indexed transactionId, string integrator, string referrer, address receiver, address fromAssetId, address toAssetId, uint256 fromAmount, uint256 toAmount)'
})
data.forEach((e: any) => dailyVolume.add(e.toAssetId, e.toAmount));
return { dailyVolume, timestamp, } as any;
};

const adapter: SimpleAdapter = {
adapter: Object.keys(contract).reduce((acc, chain) => {
return {
...acc,
[chain]: {
fetch: fetch(chain),
start: 1691625600,
}
[chain]: { fetch, start: 1691625600, }
}
}, {})
};
Expand Down
75 changes: 8 additions & 67 deletions dexs/hummus/index.ts
Original file line number Diff line number Diff line change
@@ -1,85 +1,26 @@
import { DISABLED_ADAPTER_KEY, FetchResult, SimpleAdapter } from "../../adapters/types";
import { DISABLED_ADAPTER_KEY, FetchOptions, FetchResult, SimpleAdapter } from "../../adapters/types";
import { CHAIN } from "../../helpers/chains";
import disabledAdapter from "../../helpers/disabledAdapter";
import { getBlock } from "../../helpers/getBlock";
import { getUniqStartOfTodayTimestamp } from "../../helpers/getUniSubgraphVolume";
import * as sdk from "@defillama/sdk";
import * as ethers from "ethers";

interface ILog {
data: string;
transactionHash: string;
topics: string[];
}

const abi_event = {
swap: "event Swap(address indexed sender, address fromToken, address toToken, uint256 fromAmount, uint256 toAmount, address indexed to)",
};

const abi_event_interface = new ethers.Interface(
Object.values(abi_event)
);

const swap_topic =
"0x54787c404bb33c88e86f4baf88183a3b0141d0a848e6a9f7a13b66ae3a9b73d1";

const tokens = [
"0xEA32A96608495e54156Ae48931A7c20f0dcc1a21",
"0xbB06DCA3AE6887fAbF931640f67cab3e3a16F4dC",
"0x4c078361FC9BbB78DF910800A991C7c3DD2F6ce0",
];
const decimals = [6, 6, 18];

const fetch = async (timestamp: number): Promise<FetchResult> => {
const dayTimestamp = getUniqStartOfTodayTimestamp(new Date(timestamp * 1000));
const dayID = dayTimestamp / 86400;

const fromBlock = await getBlock((dayID - 1) * 86400, CHAIN.METIS, {});
const toBlock = await getBlock(dayID * 86400, CHAIN.METIS, {});

const logs: ILog[] = (
await Promise.resolve(
sdk.getEventLogs({
target: "0x248fD66e6ED1E0B325d7b80F5A7e7d8AA2b2528b",
toBlock: toBlock,
fromBlock: fromBlock,
chain: CHAIN.METIS,
topics: [swap_topic],
})
)
) as ILog[];

let dailyVolume = 0;

logs.forEach((log) => {
const args = abi_event_interface.parseLog(log)!.args;
let vol = 0;
let tokenIndex = -1;

if (args.fromAmount < args.toAmount) {
tokenIndex = tokens.findIndex((address) => address === args.fromToken);
vol = args.fromAmount / 10 ** decimals[tokenIndex];
} else {
tokenIndex = tokens.findIndex((address) => address === args.toToken);
vol = args.toAmount / 10 ** decimals[tokenIndex];
}
if (!isNaN(vol)) {
dailyVolume += vol;
}
});
const fetch: any = async (timestamp: number, _, { getLogs, createBalances, toTimestamp }: FetchOptions) => {
const dailyVolume = createBalances();

const logs: any[] = await getLogs({ target: "0x248fD66e6ED1E0B325d7b80F5A7e7d8AA2b2528b", eventAbi: abi_event.swap, })
console.log(logs, logs.length)
logs.forEach((log: any) => dailyVolume.add(log.toToken, Number(log.toAmount)));

return {
dailyVolume: dailyVolume ? `${dailyVolume}` : undefined,
timestamp: dayTimestamp,
};
return { dailyVolume, timestamp: toTimestamp, };
};

const adapter: SimpleAdapter = {
adapter: {
[DISABLED_ADAPTER_KEY]: disabledAdapter,
[CHAIN.METIS]: {
fetch: fetch,
fetch,
start: 1661900400,
},
},
Expand Down
121 changes: 27 additions & 94 deletions dexs/joe-v2.1/index.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,7 @@
import { SimpleAdapter } from "../../adapters/types";
import { FetchOptions, SimpleAdapter } from "../../adapters/types";
import { CHAIN } from "../../helpers/chains";
import * as sdk from "@defillama/sdk";
import { getBlock } from "../../helpers/getBlock";
import { getPrices } from "../../utils/prices";
import { Chain } from "@defillama/sdk/build/general";
import { ethers, } from "ethers";

interface ILog {
data: string;
transactionHash: string;
topics: string[];
}
interface IAmount {
amountInX: number;
amountInY: number;
}
const event_swap = 'event Swap(address indexed sender,address indexed to,uint24 id,bytes32 amountsIn,bytes32 amountsOut,uint24 volatilityAccumulator,bytes32 totalFees,bytes32 protocolFees)';
const topic0 = '0xad7d6f97abf51ce18e17a38f4d70e975be9c0708474987bb3e26ad21bd93ca70';

const contract_interface = new ethers.Interface([
event_swap
]);

type TPool = {
[c: string]: string[];
Expand Down Expand Up @@ -66,86 +47,38 @@ const pools: TPool = {
]
}

const graph = (chain: Chain) => {
return async (timestamp: number) => {
const fromTimestamp = timestamp - 60 * 60 * 24
const toTimestamp = timestamp
const lpTokens = pools[chain]
const [underlyingToken0, underlyingToken1] = await Promise.all(
['address:getTokenX', 'address:getTokenY'].map((abi: string) =>
sdk.api2.abi.multiCall({
abi,
calls: lpTokens,
chain: chain
})
)
);

const tokens0 = underlyingToken0;
const tokens1 = underlyingToken1;
const fromBlock = (await getBlock(fromTimestamp, chain, {}));
const toBlock = (await getBlock(toTimestamp, chain, {}));

const logs: ILog[][] = (await Promise.all(lpTokens.map((address: string) => sdk.getEventLogs({
target: address,
toBlock: toBlock,
fromBlock: fromBlock,
chain: chain,
topics: [topic0]
})))) as ILog[][];
const fetch: any = async (timestamp: number, _, { api, chain, getLogs, createBalances, }: FetchOptions) => {
const dailyVolume = createBalances();
const lpTokens = pools[chain]
const [tokens0, tokens1] = await Promise.all(
['address:getTokenX', 'address:getTokenY'].map((abi: string) =>
api.multiCall({ abi, calls: lpTokens, }) )
);

const rawCoins = [...tokens0, ...tokens1].map((e: string) => `${chain}:${e}`);
const coins = [...new Set(rawCoins)]
const prices = await getPrices(coins, timestamp);
const logs: any[][] = (await getLogs({
targets: lpTokens,
eventAbi: event_swap,
flatten: false,
}))


const untrackVolumes: number[] = lpTokens.map((_: string, index: number) => {
const token0Decimals = prices[`${chain}:${tokens0[index]}`]?.decimals || 0
const token1Decimals = prices[`${chain}:${tokens1[index]}`]?.decimals || 0
const log: IAmount[] = logs[index]
.map((e: ILog) => { return { ...e } })
.map((p: ILog) => {
const value = contract_interface.parseLog(p);
const amountInX = Number('0x' + '0'.repeat(32) + value!.args.amountsIn.replace('0x', '').slice(0, 32)) / 10 ** token1Decimals
const amountInY = Number('0x' + '0'.repeat(32) + value!.args.amountsIn.replace('0x', '').slice(32, 64)) / 10 ** token0Decimals
return {
amountInX,
amountInY,
} as IAmount
}) as IAmount[];

const token0Price = (prices[`${chain}:${tokens0[index]}`]?.price || 0);
const token1Price = (prices[`${chain}:${tokens1[index]}`]?.price || 0);
const totalAmountInX = log
.reduce((a: number, b: IAmount) => Number(b.amountInX) + a, 0) * token1Price;
const totalAmountInY = log
.reduce((a: number, b: IAmount) => Number(b.amountInY) + a, 0) * token0Price;
const untrackAmountUSD = (totalAmountInX + totalAmountInY); // counted only we have price data
return untrackAmountUSD;
});
const dailyVolume = untrackVolumes.reduce((a: number, b: number) => a + b, 0);
return {
dailyVolume: `${dailyVolume}`,
timestamp,
};
}
logs.map((log: any, index: number) => {
const token0 = tokens0[index];
const token1 = tokens1[index];
log.forEach((i: any) => {
const amountInX = Number('0x' + '0'.repeat(32) + i.amountsIn.replace('0x', '').slice(0, 32))
const amountInY = Number('0x' + '0'.repeat(32) + i.amountsIn.replace('0x', '').slice(32, 64))
dailyVolume.add(token0, amountInY);
dailyVolume.add(token1, amountInX);
})
});
return { dailyVolume, timestamp, };
}


const adapter: SimpleAdapter = {
adapter: {
[CHAIN.ARBITRUM]: {
fetch: graph(CHAIN.ARBITRUM),
start: 1682121600,
},
[CHAIN.BSC]: {
fetch: graph(CHAIN.BSC),
start: 1681084800,
},
[CHAIN.AVAX]: {
fetch: graph(CHAIN.AVAX),
start: 1682467200,
},
[CHAIN.ARBITRUM]: { fetch, start: 1682121600, },
[CHAIN.BSC]: { fetch, start: 1681084800, },
[CHAIN.AVAX]: { fetch, start: 1682467200, },
}
};

Expand Down
Loading

0 comments on commit 12e06dd

Please sign in to comment.