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

fix stars arena #901

Merged
merged 1 commit into from
Oct 18, 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
88 changes: 66 additions & 22 deletions fees/stars-arena.ts
Original file line number Diff line number Diff line change
@@ -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<FetchResultFees> => {
const fromTimestamp = timestamp - 60 * 60 * 24
const toTimestamp = timestamp

const fetchFees = async (timestamp: number): Promise<FetchResultFees> => {
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;
Loading