From 837eaf781fe2d0523bb163118af0454c664bbdf5 Mon Sep 17 00:00:00 2001 From: 0xgnek <0xgnek@gmail.com> Date: Thu, 29 Jun 2023 18:05:24 +0000 Subject: [PATCH] fix get price from version --- fees/cryptex-v2.ts | 68 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 64 insertions(+), 4 deletions(-) diff --git a/fees/cryptex-v2.ts b/fees/cryptex-v2.ts index 2d82db1777..81a31a21d3 100644 --- a/fees/cryptex-v2.ts +++ b/fees/cryptex-v2.ts @@ -4,6 +4,7 @@ import * as sdk from "@defillama/sdk"; import { getBlock } from "../helpers/getBlock"; import { ethers } from "ethers"; import { getPrices } from "../utils/prices"; +import { type } from "os"; interface ILog { data: string; @@ -69,8 +70,47 @@ const abis: any = { ], "stateMutability": "view", "type": "function" + }, + atVersion: { + "inputs": [ + { + "internalType": "uint256", + "name": "oracleVersion", + "type": "uint256" + } + ], + "name": "atVersion", + "outputs": [ + { + "components": [ + { + "internalType": "uint256", + "name": "version", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "timestamp", + "type": "uint256" + }, + { + "internalType": "Fixed18", + "name": "price", + "type": "int256" + } + ], + "internalType": "struct IOracleProvider.OracleVersion", + "name": "", + "type": "tuple" + } + ], + "stateMutability": "view", + "type": "function" } } +type IPrice = { + [s: string]: number; +} const fetch = async (timestamp: number): Promise => { @@ -142,8 +182,28 @@ const fetch = async (timestamp: number): Promise => { const makerFees = makerFee.output.map((res: any) => Number(res.output) / 10 ** 18); const takerFees = takerFee.output.map((res: any) => Number(res.output) / 10 ** 18); - const coins = [...new Set(Object.values(coinsId))] - const prices = await getPrices(coins, timestamp); + const all: ILog[] = [ + ...make_closed_topic0_logs, + ...make_opened_topic0_logs, + ...take_closed_topic0_logs, + ...take_opened_topic0_logs + ] + const versions = [...new Set(all.map(e => contract_interface.parseLog(e)).map(e => Number(e.args.version._hex)))]; + const price_ = (await sdk.api.abi.multiCall({ + abi: abis.atVersion, + calls: versions.map((version: number) => ({ + target: products[0], + params: [version] + })), + chain: CHAIN.ARBITRUM + })).output + const _prices: IPrice = {} + price_.forEach((e: any) => { + const raw_price: string = e.output.price; + const version: string = e.output.version; + const price = Number(raw_price.replace('-', '')) / 10 ** 18; + _prices[version] = price; + }); const maker_logs: ILog[] = [ ...make_closed_topic0_logs, @@ -156,7 +216,7 @@ const fetch = async (timestamp: number): Promise => { const makerFeesAmount = maker_logs.map((a: ILog) => { const value = contract_interface.parseLog(a); - const price = prices[coinsId[a.address.toLowerCase()]].price + const price = _prices[value.args.version] const findIndex = products.findIndex(p => p.toLowerCase() === a.address.toLowerCase()); const fees = makerFees[findIndex] return ((Number(value.args.amount) / 1e18) * price) * fees; @@ -164,7 +224,7 @@ const fetch = async (timestamp: number): Promise => { const takerFeesAmount = taker_logs.map((a: ILog) => { const value = contract_interface.parseLog(a); - const price = prices[coinsId[a.address.toLowerCase()]].price + const price = _prices[value.args.version] const findIndex = products.findIndex(p => p.toLowerCase() === a.address.toLowerCase()); const fees = takerFees[findIndex] return ((Number(value.args.amount) / 1e18) * price) * fees;