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

Refactored and added burst #2102

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
80 changes: 80 additions & 0 deletions fees/apexdefi/burst.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { Chain } from "@defillama/sdk/build/general";
import { FetchOptions } from "../../adapters/types";
import { CHAIN } from "../../helpers/chains";
import { Balances } from "@defillama/sdk";

type TAddress = {
[s: string | Chain]: string;
};

type BurstSwap = {
token: string;
sender: string;
amount0In: string; // Token
amount0Out: string; // Token
amount1In: string; // Native
amount1Out: string; // Native
};

type CurveComplete = {
token0: string; // Token
dist: string; // Distributor
launchFee: string;
creatorRewards: string;
};

const BURST_FACTORIES: TAddress = {
[CHAIN.AVAX]: "0x1FE488479873Bc76e5D69F612f155714c20C0DCF",
[CHAIN.BASE]: "0xBc74A3C24d8aA980445ADc889577E29089c07CDD",
};

const scaleFactor = BigInt(10000);
const burstSwapFee = BigInt(25); // 0.25%
const burstFactoryFee = BigInt(75); // 0.75%

export async function burstMetrics(options: FetchOptions): Promise<{
dailyFees: Balances;
dailyProtocolRevenue: Balances;
dailyVolume: Balances;
}> {
const { createBalances } = options;

let dailyFees = createBalances();
let dailyProtocolRevenue = createBalances();
let dailyVolume = createBalances();

const curveCompleteLogs = await options.getLogs({
target: BURST_FACTORIES[options.chain],
eventAbi:
"event CurveCompleted(address indexed token0, address indexed dist, uint256 launchFee, uint256 creatorRewards)",
});

const swapLogs = await options.getLogs({
targets: [BURST_FACTORIES[options.chain]],
eventAbi:
"event BurstSwap(address indexed token, address indexed sender, uint256 amount0In, uint256 amount0Out, uint256 amount1In, uint256 amount1Out)",
});

curveCompleteLogs.map((log: CurveComplete) => {
const launchFeeAmount = BigInt(log.launchFee);
const creatorRewardsAmount = BigInt(log.creatorRewards);
const totalFee = launchFeeAmount + creatorRewardsAmount;
dailyFees.addGasToken(totalFee);
dailyProtocolRevenue.addGasToken(launchFeeAmount);
});

swapLogs.map((log: BurstSwap) => {
const nativeAmount = BigInt(log.amount1In) + BigInt(log.amount1Out);
const fee = (nativeAmount * burstSwapFee) / scaleFactor;
const protocolRevenue = (nativeAmount * burstFactoryFee) / scaleFactor;
dailyFees.addGasToken(fee + protocolRevenue);
dailyProtocolRevenue.addGasToken(protocolRevenue);
dailyVolume.addGasToken(nativeAmount);
});

return {
dailyFees: dailyFees,
dailyProtocolRevenue: dailyProtocolRevenue,
dailyVolume: dailyVolume,
};
}
72 changes: 72 additions & 0 deletions fees/apexdefi/dex.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { Chain } from "@defillama/sdk/build/general";
import { FetchOptions } from "../../adapters/types";
import { CHAIN } from "../../helpers/chains";
import { Balances } from "@defillama/sdk";

const abis = {
allTokens: "address[]:getAllTokens",
};

type TAddress = {
[s: string | Chain]: string;
};

type Swap = {
sender: string;
amountTokenIn: string;
amountNativeIn: string;
amountTokenOut: string;
amountNativeOut: string;
flashSwap: boolean;
};

const FACTORIES: TAddress = {
[CHAIN.AVAX]: "0x754A0c42C35562eE7a41eb824d14bc1259820f01",
[CHAIN.BASE]: "0x10d11Eb1d5aB87E65518458F990311480b321061",
[CHAIN.ETHEREUM]: "0x820c889D5749847217599B43ab86FcC91781019f",
};

const scaleFactor = BigInt(10000);
const factoryFee = BigInt(10); // 0.1%
const lpFee = BigInt(20); // 0.2%

export async function swapMetrics(options: FetchOptions): Promise<{
dailyFees: Balances;
dailySupplySideRevenue: Balances;
dailyProtocolRevenue: Balances;
dailyVolume: Balances;
}> {
const { createBalances } = options;
const dailyFees = createBalances();
const dailySupplySideRevenue = createBalances();
const dailyProtocolRevenue = createBalances();
const dailyVolume = createBalances();

const allTokens = await options.api.call({
target: FACTORIES[options.chain],
abi: abis.allTokens,
});

const logs = await options.getLogs({
targets: allTokens,
eventAbi:
"event Swap(address indexed sender, uint256 amountTokenIn, uint256 amountNativeIn, uint256 amountTokenOut, uint256 amountNativeOut, bool flashSwap)",
});

logs.map((tx: Swap) => {
const nativeAmount = BigInt(tx.amountNativeIn) + BigInt(tx.amountNativeOut);
const fee = (nativeAmount * lpFee) / scaleFactor;
const protocolRevenue = (nativeAmount * factoryFee) / scaleFactor;
dailyFees.addGasToken(fee + protocolRevenue);
dailyProtocolRevenue.addGasToken(protocolRevenue);
dailySupplySideRevenue.addGasToken(fee);
dailyVolume.addGasToken(nativeAmount);
});

return {
dailyFees,
dailyVolume,
dailyProtocolRevenue,
dailySupplySideRevenue,
};
}
76 changes: 24 additions & 52 deletions fees/apexdefi/index.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,7 @@
import { Chain } from "@defillama/sdk/build/general";
import { Adapter, FetchOptions } from "../../adapters/types";
import { CHAIN } from "../../helpers/chains";

const abis = {
allTokens: "address[]:getAllTokens",
};

type TAddress = {
[s: string | Chain]: string;
};

type Swap = {
sender: string;
amountTokenIn: string;
amountNativeIn: string;
amountTokenOut: string;
amountNativeOut: string;
flashSwap: boolean;
};
import { burstMetrics } from "./burst";
import { swapMetrics } from "./dex";

const methodology = {
Fees: "Swap fees paid by users of 0.03%",
Expand All @@ -28,49 +12,37 @@ const methodology = {
"70% of collected swap fees are distributed to liquidity providers",
};

const FACTORIES: TAddress = {
[CHAIN.AVAX]: "0x754A0c42C35562eE7a41eb824d14bc1259820f01",
[CHAIN.BASE]: "0x10d11Eb1d5aB87E65518458F990311480b321061",
[CHAIN.ETHEREUM]: "0x820c889D5749847217599B43ab86FcC91781019f",
};
const fetch = async (options: FetchOptions) => {
const { createBalances } = options;
const dailyFees = createBalances();
const dailySupplySideRevenue = createBalances();
const dailyProtocolRevenue = createBalances();
const dailyVolume = createBalances();

const scaleFactor = BigInt(10000);
const factoryFee = BigInt(10); // 0.1%
const lpFee = BigInt(20); // 0.2%
const swapMetricsResult = await swapMetrics(options);

const fetch = async (options: FetchOptions) => {
const dailyFees = options.createBalances();
const dailySupplySideRevenue = options.createBalances();
const dailyProtocolRevenue = options.createBalances();
const dailyVolume = options.createBalances();
const totalVolume = options.createBalances();
dailyFees.addBalances(swapMetricsResult.dailyFees);

dailySupplySideRevenue.addBalances(swapMetricsResult.dailySupplySideRevenue);

const allTokens = await options.api.call({
target: FACTORIES[options.chain],
abi: abis.allTokens,
});
dailyProtocolRevenue.addBalances(swapMetricsResult.dailyProtocolRevenue);

const logs = await options.getLogs({
targets: allTokens,
eventAbi:
"event Swap(address indexed sender, uint256 amountTokenIn, uint256 amountNativeIn, uint256 amountTokenOut, uint256 amountNativeOut, bool flashSwap)",
});
dailyVolume.addBalances(swapMetricsResult.dailyVolume);

logs.map((tx: Swap) => {
const nativeAmount = BigInt(tx.amountNativeIn) + BigInt(tx.amountNativeOut);
const fee = (nativeAmount * lpFee) / scaleFactor;
const protocolRevenue = (nativeAmount * factoryFee) / scaleFactor;
dailyFees.addGasToken(fee + protocolRevenue);
dailyProtocolRevenue.addGasToken(protocolRevenue);
dailySupplySideRevenue.addGasToken(fee);
dailyVolume.addGasToken(nativeAmount);
});
// No burst metrics for Ethereum
if (options.chain !== CHAIN.ETHEREUM) {
const burstMetricsResult = await burstMetrics(options);

dailyFees.addBalances(burstMetricsResult.dailyFees);
dailyProtocolRevenue.addBalances(burstMetricsResult.dailyProtocolRevenue);
dailyVolume.addBalances(burstMetricsResult.dailyVolume);
}

return {
dailyFees,
dailyVolume,
dailyProtocolRevenue,
dailySupplySideRevenue,
dailyProtocolRevenue,
dailyVolume,
};
};

Expand Down
Loading