Skip to content

Commit

Permalink
fix get price from version
Browse files Browse the repository at this point in the history
  • Loading branch information
dtmkeng committed Jun 29, 2023
1 parent 8c1bd8c commit 837eaf7
Showing 1 changed file with 64 additions and 4 deletions.
68 changes: 64 additions & 4 deletions fees/cryptex-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<FetchResultFees> => {

Expand Down Expand Up @@ -142,8 +182,28 @@ const fetch = async (timestamp: number): Promise<FetchResultFees> => {
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,
Expand All @@ -156,15 +216,15 @@ const fetch = async (timestamp: number): Promise<FetchResultFees> => {

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;
}).reduce((a: number,b: number) => a + b, 0)

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;
Expand Down

0 comments on commit 837eaf7

Please sign in to comment.