From fcf9823570822d99319af71f332a77a0894800b6 Mon Sep 17 00:00:00 2001 From: 0xgnek <0xgnek@gmail.com> Date: Wed, 18 Oct 2023 14:10:37 +0000 Subject: [PATCH] fix stars arena --- fees/stars-arena.ts | 88 +++++++++++++++++++++++++++++++++------------ 1 file changed, 66 insertions(+), 22 deletions(-) diff --git a/fees/stars-arena.ts b/fees/stars-arena.ts index 3dd55e7243..b1ef69f673 100644 --- a/fees/stars-arena.ts +++ b/fees/stars-arena.ts @@ -1,40 +1,84 @@ -import { FetchResultFees, SimpleAdapter } from "../adapters/types"; +import { Adapter, FetchResultFees } from "../adapters/types"; import { CHAIN } from "../helpers/chains"; -import { queryDune } from "../helpers/dune"; +import * as sdk from "@defillama/sdk"; +import { getBlock } from "../helpers/getBlock"; +import { getPrices } from "../utils/prices"; +import { ethers } from "ethers"; + +const address = '0x9C8574779468125975F370b7746fb2aF7CB13fdb'; +const topic0_trade = '0xc9d4f93ded9b42fa24561e02b2a40f720f71601eb1b3f7b3fd4eff20877639ee'; +const event_trade = 'event Trade(address trader,address subject,bool isBuy,uint256 shareAmount,uint256 amount,uint256 protocolAmount,uint256 subjectAmount,uint256 referralAmount,uint256 supply,uint256 buyPrice,uint256 myShares)' +const contract_interface = new ethers.utils.Interface([ + event_trade +]); + +interface ILog { + data: string; + transactionHash: string; + topics: string[]; +} interface IFee { - day: string; - fees_usd: number; - rev_usd: number; + fees: number; + rev: number; } +const fetch = async (timestamp: number): Promise => { + const fromTimestamp = timestamp - 60 * 60 * 24 + const toTimestamp = timestamp -const fetchFees = async (timestamp: number): Promise => { + const fromBlock = (await getBlock(fromTimestamp, CHAIN.AVAX, {})); + const toBlock = (await getBlock(toTimestamp, CHAIN.AVAX, {})); try { - const fees: IFee[] = (await queryDune("3083702")); - // const fees = temp; - const dateStr = new Date(timestamp * 1000).toISOString().split('T')[0]; - const daily = fees.find((e: IFee) => e.day.split(' ')[0] === dateStr); - const dailyFees = daily?.fees_usd || 0; - const dailyRevenue = daily?.rev_usd || 0; + let _logs: ILog[] = []; + for(let i = fromBlock; i < toBlock; i += 5000) { + const logs: ILog[] = (await sdk.api.util.getLogs({ + target: address, + topic: '', + toBlock: i + 5000, + fromBlock: i, + keys: [], + chain: CHAIN.AVAX, + topics: [topic0_trade] + })).output as ILog[]; + _logs = _logs.concat(logs); + } + + const fees_details: IFee[] = _logs.map((e: ILog) => { + const value = contract_interface.parseLog(e); + const protocolEthAmount = Number(value.args.protocolAmount._hex) / 10 ** 18; + const subjectEthAmount = Number(value.args.subjectAmount._hex) / 10 ** 18; + return { + fees: protocolEthAmount + subjectEthAmount, + rev: protocolEthAmount + } as IFee + }) + const dailyFees = fees_details.reduce((a: number, b: IFee) => a+b.fees, 0) + const dailyRev = fees_details.reduce((a: number, b: IFee) => a+b.rev, 0) + const avaxPrice = "avax:0xB31f66AA3C1e785363F0875A1B74E27b85FD66c7"; + const ethPrice = (await getPrices([avaxPrice], timestamp))[avaxPrice].price; + const dailyFeesUSD = (dailyFees) * ethPrice; + const dailyRevUSD = (dailyRev) * ethPrice; return { - dailyFees: `${dailyFees}`, - dailyRevenue: `${dailyRevenue}`, + dailyFees: `${dailyFeesUSD}`, + dailyRevenue: `${dailyRevUSD}`, timestamp } - } catch (e) { - console.error(e) - throw e; + } catch (error) { + console.error(error) + throw error; } } -const adapters: SimpleAdapter = { + + +const adapter: Adapter = { adapter: { [CHAIN.AVAX]: { - fetch: fetchFees, - start: async () => 1695081600, - } + fetch: fetch, + start: async () => 1695081600, + }, } } -export default adapters; +export default adapter;