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

add chronos v2 #850

Merged
merged 1 commit into from
Sep 25, 2023
Merged
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
126 changes: 126 additions & 0 deletions dexs/chronos-v2/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import { eth } from "@defillama/sdk/build/api";
import { FetchResultFees, FetchResultVolume, SimpleAdapter } from "../../adapters/types"
import { CHAIN } from "../../helpers/chains"
import { getBlock } from "../../helpers/getBlock";
import * as sdk from "@defillama/sdk";
import { ethers } from "ethers";
import { getPrices } from "../../utils/prices";

const topic0_create_pool = '0x783cca1c0412dd0d695e784568c96da2e9c22ff989357a2e8b1d9b2b4e6b7118';
const topic0_event_pool_create = 'event PoolCreated(address indexed token0,address indexed token1,uint24 indexed fee,int24 tickSpacing,address pool)';
const topic0_swap = '0xc42079f94a6350d7e6235f29174924f928cc2ac818eb64fed8004e115fbcca67';
const topic0_event_swap = 'event Swap(address indexed sender,address indexed recipient,int256 amount0,int256 amount1,uint160 sqrtPriceX96,uint128 liquidity,int24 tick)'
const contract_interface = new ethers.utils.Interface([
topic0_event_pool_create,
topic0_event_swap
])
const poolFactoryAddress = '0x4Db9D624F67E00dbF8ef7AE0e0e8eE54aF1dee49';

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

const PAIR_TOKEN_ABI = (token: string): object => {
return {
"constant": true,
"inputs": [],
"name": token,
"outputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
}
};


const fetchVolume = async (timestamp: number): Promise<FetchResultVolume> => {
const toTimestamp = timestamp;
const fromTimestamp = timestamp - 60 * 60 * 24;
try {
const fromBlock = (await getBlock(fromTimestamp, CHAIN.ARBITRUM, {}));
const toBlock = (await getBlock(toTimestamp, CHAIN.ARBITRUM, {}));

const logs: ILog[] = (await sdk.api.util.getLogs({
target: poolFactoryAddress,
topic: '',
fromBlock: 114041129,
toBlock: toBlock,
chain: CHAIN.ARBITRUM,
topics: [topic0_create_pool],
keys: []
})).output as ILog[];
const poolAddresses = logs.map((e: ILog) => contract_interface.parseLog(e).args.pool);

const [underlyingToken0, underlyingToken1] = await Promise.all(
['token0', 'token1'].map((method) =>
sdk.api.abi.multiCall({
abi: PAIR_TOKEN_ABI(method),
calls: poolAddresses.map((address: string) => ({
target: address,
})),
chain: CHAIN.ARBITRUM
})
)
);
const tokens0 = underlyingToken0.output.map((res: any) => res.output);
const tokens1 = underlyingToken1.output.map((res: any) => res.output);

const coins: string[] = [...new Set([...tokens0.concat(tokens1).map((e: string) => `${CHAIN.ARBITRUM}:${e}`)])];

const logsSwap: ILog[] = (await Promise.all(poolAddresses.map((address: string) => sdk.api.util.getLogs({
target: address,
topic: '',
toBlock: toBlock,
fromBlock: fromBlock,
keys: [],
chain: CHAIN.ARBITRUM,
topics: [topic0_swap]
}))))
.map((p: any) => p)
.map((a: any) => a.output).flat();

const prices = await getPrices(coins, timestamp);

const dailyVolume = logsSwap.map((e: ILog) => {
const parsed = contract_interface.parseLog(e);
const amount0 = Math.abs(Number(parsed.args.amount0._hex.replace('-', '')));
const amount1 = Math.abs(Number(parsed.args.amount1._hex.replace('-', '')));
const index = poolAddresses.indexOf(e.address);
const token0 = tokens0[index];
const token1 = tokens1[index];
const price0 = prices[`${CHAIN.ARBITRUM}:${token0}`].price;
const price1 = prices[`${CHAIN.ARBITRUM}:${token1}`].price;
const decimals0 = prices[`${CHAIN.ARBITRUM}:${token0}`].decimals;
const decimals1 = prices[`${CHAIN.ARBITRUM}:${token1}`].decimals;
return price0 ? (amount0 / 10 ** decimals0) * price0 : (amount1/10**decimals1) * price1;
}).reduce((a: number, b: number) => a + b, 0)

return {
dailyVolume: dailyVolume.toString(),
timestamp
}
} catch(error) {
console.error(error)
throw error;
}
}

const adapters: SimpleAdapter = {
adapter: {
[CHAIN.ARBITRUM]: {
fetch: fetchVolume,
start: async () => 1690070400,
}
}
}

export default adapters;
Loading