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 legacy fees and bribes to Shadow Fees #2360

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
103 changes: 91 additions & 12 deletions protocols/shadow-exchange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ import { FetchOptions, SimpleAdapter } from "../adapters/types";
import { CHAIN } from "../helpers/chains";
import request from "graphql-request";

const SHADOW_ADDRESS = "0x3333b97138d4b086720b5ae8a7844b1345a33333"
const XSHADOW_ADDRESS = "0x5050bc082ff4a74fb6b0b04385defddb114b2424"

type TStartTime = {
[key: string]: number;
};

const startTimeV2: TStartTime = {
[CHAIN.SONIC]: 1735129946,
};
Expand All @@ -16,31 +20,106 @@ const v2Endpoints: any = {
};

interface IPool {
id: string;
volumeUSD: string;
feesUSD: string;
}

const fetchBribes = async (options: FetchOptions) => {
const { getLogs } = options
let bribes = 0

const period = Math.floor(options.startOfDay / 604800)
const fromBlock = await options.getFromBlock()
const toBlock = await options.getToBlock()

const gaugeCreatedLogs = await getLogs({
targets: ["0x3aF1dD7A2755201F8e2D6dCDA1a61d9f54838f4f", "0xf914Cc768040B4268A779C3084a3E9cdA6E8a1A8"],
eventAbi: "event GaugeCreated(address indexed pool, address gauge)",
fromBlock: 4028273,
cacheInCloud: true,
})

const gaugeAddresses = gaugeCreatedLogs.map((log) => log[1] as string)

const logs = await getLogs({
targets: gaugeAddresses,
eventAbi: "event NotifyReward(address indexed from, address indexed reward, uint256 amount, uint256 period)",
fromBlock,
toBlock,
})

const periodLogs = logs.filter((log) => Number(log[3]) === period)

const tokens: { [key:string]: bigint } = {
[SHADOW_ADDRESS]: BigInt(0)
}

for (const log of periodLogs) {
const addr = log[1].toLowerCase()

if (!tokens[addr]) {
tokens[addr] = BigInt(0)
}

tokens[addr] += log[2]
}

const query = `
{
tokenDayDatas (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seem the graph not found tokenDayDatas field

where: { token_in: ["${Object.keys(tokens).join('", "')}"] },
startOfDay_lte: ${options.startOfDay},
orderBy: startOfDay
orderDirection: desc
) {
token { id, decimals }
priceUSD
}
}
`

const res = await request(v2Endpoints[options.chain], query)

const shadowPrice = Number(res.tokenDayDatas.find((row: any) => row.token.id === SHADOW_ADDRESS).priceUSD)

for (const [tokenAddress, amount] of Object.entries(tokens)) {
const isXShadow = tokenAddress === XSHADOW_ADDRESS
const tokenDayData = res.tokenDayDatas.find((row: any) => row.token.id === tokenAddress)
const tokenPrice = isXShadow ? shadowPrice : Number(tokenDayData.priceUSD)
const tokenDecimals = isXShadow ? 18 : tokenDayData.token.decimals

bribes += Number(amount) / (10 ** tokenDecimals) * tokenPrice
}

return { bribes }
}

const fetch = async (options: FetchOptions) => {
const query = `
{
clPoolDayDatas(where:{startOfDay: ${options.startOfDay}}) {
startOfDay
volumeUSD
feesUSD
}
{
clPoolDayDatas(where:{startOfDay: ${options.startOfDay}}) {
volumeUSD
feesUSD
}
legacyPoolDayDatas(where:{startOfDay: ${options.startOfDay}}) {
volumeUSD
feesUSD
}
}
`;

const { bribes } = await fetchBribes(options)

const res = await request(v2Endpoints[options.chain], query);
const pools: IPool[] = res.clPoolDayDatas;
const dailyFees = pools.reduce((acc, pool) => acc + Number(pool.feesUSD), 0);
const pools: IPool[] = [...res.clPoolDayDatas, ...res.legacyPoolDayDatas];
const dailyVolume = pools.reduce((acc, pool) => acc + Number(pool.volumeUSD), 0);
const dailyFees = bribes + pools.reduce((acc, pool) => acc + Number(pool.feesUSD), 0)

return {
dailyVolume,
dailyFees,
dailyUserFees: dailyFees,
dailySupplySideRevenue: dailyFees,
dailyProtocolRevenue: dailyFees,
dailyRevenue: dailyFees,
dailyHoldersRevenue: dailyFees,
};

}
Expand Down
Loading