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 FRIEND-ROOM fees adapter #812

Merged
merged 2 commits into from
Sep 7, 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
81 changes: 81 additions & 0 deletions fees/friend-room.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { Adapter, FetchResultFees, SimpleAdapter } from "../adapters/types";
import { CHAIN } from "../helpers/chains";
import * as sdk from "@defillama/sdk";
import { getBlock } from "../helpers/getBlock";
import { getPrices } from "../utils/prices";
import { Chain } from "@defillama/sdk/build/general";
import { ethers } from "ethers";

const friendRoomSharesAddress = '0x9BD0474CC4F118efe56f9f781AA8f0F03D4e7A9c';
const topic0_trade = '0x68e42cc58cdc03b82d6c135e12e98660f43a75ce1ccfb1a625965e253f50d7b0';
const event_trade = 'event Trade(uint256 index, uint256 serverId, address trader, uint256 tokenId, bool isBuy, uint256 shareAmount, uint256 ethAmount, uint256 protocolEthAmount, uint256 subjectEthAmount, uint256 supply)'
const contract_interface = new ethers.utils.Interface([
event_trade
]);

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

interface IFee {
fees: number;
rev: number;
}

const fetch = async (timestamp: number): Promise<FetchResultFees> => {
const fromTimestamp = timestamp - 60 * 60 * 24
const toTimestamp = timestamp

const fromBlock = (await getBlock(fromTimestamp, CHAIN.ETHEREUM, {}));
const toBlock = (await getBlock(toTimestamp, CHAIN.ETHEREUM, {}));
try {
const logs: ILog[] = (await sdk.api.util.getLogs({
target: friendRoomSharesAddress,
topic: '',
toBlock: toBlock,
fromBlock: fromBlock,
keys: [],
chain: CHAIN.ETHEREUM,
topics: [topic0_trade]
})).output as ILog[];

const fees_details: IFee[] = logs.map((e: ILog) => {
const value = contract_interface.parseLog(e);
const protocolEthAmount = Number(value.args.protocolEthAmount._hex) / 10 ** 18;
const subjectEthAmount = Number(value.args.subjectEthAmount._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 ethAddress = "ethereum:0x0000000000000000000000000000000000000000";
const ethPrice = (await getPrices([ethAddress], timestamp))[ethAddress].price;
const dailyFeesUSD = (dailyFees) * ethPrice;
const dailyRevUSD = (dailyRev) * ethPrice;
return {
dailyFees: `${dailyFeesUSD}`,
dailyRevenue: `${dailyRevUSD}`,
timestamp
}
} catch (error) {
console.error(error)
throw error;
}

}


const adapter: Adapter = {
adapter: {
[CHAIN.ETHEREUM]: {
fetch: fetch,
start: async () => 1693731179,
},
}
}

export default adapter;
Loading