Skip to content

Commit

Permalink
feat: fetch sbbtc-apr from dune api
Browse files Browse the repository at this point in the history
  • Loading branch information
ryenguyen7411 committed Jul 25, 2023
1 parent f1b96c5 commit f773da7
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 123 deletions.
18 changes: 5 additions & 13 deletions src/modules/hooks/useGetPoolApr/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,15 @@ export const useGetPoolApr = (): { apr: PoolAprState; isLoading: boolean } => {
query: { bridge: 'btc_skypool' },
});

const farmBsc = stringifyUrl({
url: `${ENDPOINT_YIELD_FARMING}/api/v1/farm-info`,
query: { farm: 'SkyPools' },
});

const results = await Promise.all([
fetcher<{ apy: number }>(sbBtcSkypoolUrl),
fetcher<{ apr: number; swingbyPerBlock: number; farmTvl: number }>(farmBsc),
]);
const results = await Promise.all([fetcher<{ apy: number }>(sbBtcSkypoolUrl)]);

setApr({
btc_skypool: {
sbBtc: results[0].apy,
farm: results[1].apr,
total: results[0].apy + results[1].apr,
swingbyPerBlock: results[1].swingbyPerBlock,
farmTvl: results[1].farmTvl,
farm: 0,
total: results[0].apy,
swingbyPerBlock: 0,
farmTvl: 0,
},
});
} catch (error) {
Expand Down
32 changes: 0 additions & 32 deletions src/modules/scenes/Main/Liquidity/AddLiquidity/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,39 +102,7 @@ export const AddLiquidity = (props: Props) => {
}
};

const [transactionFee, setTransactionFee] = useState(null);
const [isLoading, setIsLoading] = useState(false);
const context = useSdkContext();
const sbBTC = getBridgeSbBtc(bridge);
useEffect(() => {
let cancelled = false;

(async () => {
Number(amount) > 0 && setIsLoading(true);
try {
if (cancelled) return;

const { feeTotal } = await estimateAmountReceiving({
context,
currencyDeposit: currency as CoinSymbol,
currencyReceiving: sbBTC,
amountDesired: amount,
});
if (cancelled) return;
setTransactionFee(feeTotal);
} catch (e) {
if (cancelled) return;
console.log(e);
setTransactionFee('');
}
setIsLoading(false);
})();

return () => {
cancelled = true;
setTransactionFee('');
};
}, [currency, amount, context, bridge, sbBTC]);

const widget = createWidget({
resource: 'pool',
Expand Down
32 changes: 0 additions & 32 deletions src/modules/scenes/Main/Liquidity/Withdraw/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -132,38 +132,6 @@ export const Withdraw = (props: Props) => {
}
}, [dispatch, toCurrency, transactionFees, context, amount, currency, bridge]);

const [transactionFee, setTransactionFee] = useState(null);
const [isLoading, setIsLoading] = useState(false);

useEffect(() => {
let cancelled = false;

(async () => {
Number(amount) > 0 && setIsLoading(true);
try {
if (cancelled) return;
const { feeTotal } = await estimateAmountReceiving({
context,
currencyDeposit: getBridgeSbBtc(bridge),
currencyReceiving: toCurrency,
amountDesired: amount,
});
if (cancelled) return;
setTransactionFee(feeTotal);
} catch (e) {
if (cancelled) return;
console.log(e);
setTransactionFee('');
}
setIsLoading(false);
})();

return () => {
cancelled = true;
setTransactionFee('');
};
}, [toCurrency, amount, context, bridge]);

const currencyItems = (
<>
{poolCurrencies.map((currency) => (
Expand Down
52 changes: 6 additions & 46 deletions src/pages/api/v1/sbbtc-apr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,9 @@ import { aprToApy } from 'apr-tools';
import { NextApiRequest, NextApiResponse } from 'next';

import { corsMiddleware, getParam } from '../../../modules/api';
import { sumArray } from '../../../modules/common';
import { fetcher } from '../../../modules/fetch';
import {
fetchFloatBalances,
fetchVwap,
getFixedBaseEndpoint,
getTransactionFee,
IFloat,
} from '../../../modules/explorer';
import { skypoolsEnabled } from '../../../modules/env';

const getFloatUsd = (bridge: SkybridgeBridge, usdBtc: number, floatBalances: IFloat) => {
const sumFloatUsd = (floatBtc: number, floatPeggedBtc: number, usdBtc: number) =>
(floatBtc + floatPeggedBtc) * usdBtc;

switch (bridge) {
case 'btc_skypool':
return sumFloatUsd(floatBalances.btcSkypool, floatBalances.wbtcSkypool, usdBtc);

default:
return sumFloatUsd(floatBalances.btcSkypool, floatBalances.wbtcSkypool, usdBtc);
}
};

const isBridgeEnabled = (bridge: SkybridgeBridge): boolean => {
switch (bridge) {
case 'btc_skypool': {
Expand All @@ -51,34 +30,15 @@ export default async function handler(
}

try {
const usdBtc = await fetchVwap('btcUsd');

// Memo: 1/3 goes to liquidity provider
const feeGoesLiquidityProvider = 0.33;
const baseUrl = getFixedBaseEndpoint(bridge);

const results = await Promise.all([
fetcher<{ network1mSwapsVolume: number[] }>(`${baseUrl}/api/v1/swaps/stats`),
getTransactionFee(bridge),
fetchFloatBalances(usdBtc, bridge),
fetcher<{ result: { rows: { apr: number; date: string }[] } }>(
'https://api.dune.com/api/v1/query/2705069/results?api_key=6AUVgjoBeq5858W8OVcwIhJz4xM0JWPE',
),
]);

const network1mSwapVolume = results[0].network1mSwapsVolume;
const bridgeFeePercent = Number(results[1].bridgeFeePercent) * 0.01;
const floatBalances = results[2].floats;

const filledMonth = network1mSwapVolume.filter((it: number) => it !== 0).length;
const yearlyVolume = sumArray(network1mSwapVolume);
const averageVolume = yearlyVolume / filledMonth;
const estimatedYearlyVolumeUsd = averageVolume * usdBtc * 12;
const floatUsd = getFloatUsd(bridge, usdBtc, floatBalances);

if (floatUsd > 0) {
// Eg: ((32,356,354 * 0.002 * 0.33) / 470,438) * 100 = 4.5
const estApr =
((estimatedYearlyVolumeUsd * bridgeFeePercent * feeGoesLiquidityProvider) / floatUsd) * 100;

apr = Number(estApr.toFixed(1));
const currentApr = results[0].result.rows.find((row) => row.apr > 0);
if (currentApr) {
apr = Number(currentApr.apr.toFixed(1));
apy = Number(aprToApy(apr).toFixed(1));
}

Expand Down

0 comments on commit f773da7

Please sign in to comment.