Skip to content

Commit 13f0c2a

Browse files
committed
Update base apy calc to take into account admin fee
1 parent 67e7e6c commit 13f0c2a

File tree

2 files changed

+56
-7
lines changed

2 files changed

+56
-7
lines changed

routes/v1/getBaseApys/[blockchainId].js

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ import factorypool3BaseCryptoAbi from '#root/constants/abis/factory_crypto_swap.
3232
import { uintToBN } from '#root/utils/Web3/index.js';
3333
import { lc } from '#root/utils/String.js';
3434

35+
const ADMIN_FEE_1_ABI = [{ "name": "admin_fee", "outputs": [{ "type": "uint256", "name": "" }], "inputs": [], "stateMutability": "view", "type": "function" }];
36+
const ADMIN_FEE_2_ABI = [{ "stateMutability": "view", "type": "function", "name": "ADMIN_FEE", "inputs": [], "outputs": [{ "name": "", "type": "uint256" }] }];
37+
3538
const isCryptoPool = ({ registryId }) => registryId.includes('crypto');
3639

3740
// xcp_profit and xcp_profit_a can return '0' when queried for a crypto pool with no activity, whether
@@ -118,7 +121,31 @@ export default fn(async ({ blockchainId }) => {
118121
superSettings: {
119122
fallbackValue: 1e18,
120123
},
121-
}] : [{
124+
}, ...(blockNumber === undefined ? [{
125+
address: pool.address,
126+
abi: ADMIN_FEE_1_ABI,
127+
methodName: 'admin_fee',
128+
metaData: { type: 'adminFee_try_1', pool },
129+
networkSettings: {
130+
...networkSettings,
131+
blockNumber,
132+
},
133+
superSettings: {
134+
fallbackValue: null, // Know when this method hits a dead-end instead of defaulting to valid value
135+
},
136+
}, {
137+
address: pool.address,
138+
abi: ADMIN_FEE_2_ABI,
139+
methodName: 'ADMIN_FEE',
140+
metaData: { type: 'adminFee_try_2', pool },
141+
networkSettings: {
142+
...networkSettings,
143+
blockNumber,
144+
},
145+
superSettings: {
146+
fallbackValue: null, // Know when this method hits a dead-end instead of defaulting to valid value
147+
},
148+
}] : [])] : [{
122149
address: pool.address,
123150
abi: poolAbi,
124151
methodName: 'get_virtual_price',
@@ -149,8 +176,9 @@ export default fn(async ({ blockchainId }) => {
149176
/**
150177
* Calculate base daily and weekly apys
151178
*/
179+
let adminFee;
152180
if (isCryptoPool(pool)) {
153-
const { xcpProfit: [{ data: unsafeXcpProfit }], xcpProfitA: [{ data: unsafeXcpProfitA }] } = currentPoolData;
181+
const { xcpProfit: [{ data: unsafeXcpProfit }], xcpProfitA: [{ data: unsafeXcpProfitA }], adminFee_try_1: [{ data: adminFeeRaw1 }], adminFee_try_2: [{ data: adminFeeRaw2 }] } = currentPoolData;
154182
const { xcpProfit: [{ data: unsafeXcpProfitDayOld }], xcpProfitA: [{ data: unsafeXcpProfitADayOld }] } = dayOldPoolData;
155183
const { xcpProfit: [{ data: unsafeXcpProfitWeekOld }], xcpProfitA: [{ data: unsafeXcpProfitAWeekOld }] } = weekOldPoolData;
156184

@@ -161,9 +189,14 @@ export default fn(async ({ blockchainId }) => {
161189
const xcpProfitWeekOld = safeXcpProfit(unsafeXcpProfitWeekOld);
162190
const xcpProfitAWeekOld = safeXcpProfit(unsafeXcpProfitAWeekOld);
163191

164-
const currentProfit = ((xcpProfit / 2) + (xcpProfitA / 2) + 1e18) / 2;
165-
const dayOldProfit = ((xcpProfitDayOld / 2) + (xcpProfitADayOld / 2) + 1e18) / 2;
166-
const weekOldProfit = ((xcpProfitWeekOld / 2) + (xcpProfitAWeekOld / 2) + 1e18) / 2;
192+
if (adminFeeRaw1 === null && adminFeeRaw2 === null) {
193+
throw new Error(`adminFee could not be retrieved for pool ${pool} because methods used seem invalid`)
194+
}
195+
adminFee = uintToBN(adminFeeRaw1 ?? adminFeeRaw2, 10).toNumber();
196+
197+
const currentProfit = (xcpProfit * (1 - adminFee) / 1e18 + xcpProfitA * adminFee / 1e18 + 1) / 2; // Better calc taking admin fee into account
198+
const dayOldProfit = (xcpProfitDayOld * (1 - adminFee) / 1e18 + xcpProfitADayOld * adminFee / 1e18 + 1) / 2; // Better calc taking admin fee into account
199+
const weekOldProfit = (xcpProfitWeekOld * (1 - adminFee) / 1e18 + xcpProfitAWeekOld * adminFee / 1e18 + 1) / 2; // Better calc taking admin fee into account
167200
const rateDaily = (currentProfit - dayOldProfit) / dayOldProfit;
168201
const rateWeekly = (currentProfit - weekOldProfit) / weekOldProfit;
169202

routes/v1/getVolumes/[blockchainId].js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ import { lc } from '#root/utils/String.js';
2525
import { sumBN } from '#root/utils/Array.js';
2626
import getPricesCurveFiChainsBlockchainId, { PRICES_CURVE_FI_AVAILABLE_CHAIN_IDS } from '#root/utils/data/prices.curve.fi/chains.js';
2727

28+
// Used to temporarily give precedence to internal base apr calcs (vs prices.curve api)
29+
// for yb pools
30+
const YB_POOLS_ADDRESSES = [
31+
'0xf1F435B05D255a5dBdE37333C0f61DA6F69c6127',
32+
'0x83f24023d15d835a213df24fd309c47dAb5BEb32',
33+
'0xD9FF8396554A0d18B2CFbeC53e1979b7ecCe8373',
34+
].map(lc);
35+
2836
const DEFAULT_VOLUME_DATA = {
2937
trading_volume_24h: 0,
3038
liquidity_volume_24h: 0,
@@ -87,8 +95,16 @@ export default fn(async ({ blockchainId }) => {
8795
address,
8896
type,
8997
volumeUSD: BN(tradingVolume).dp(2).toNumber() ?? 0, // Excluding liquidityVolume for consistency for now, may add it later
90-
latestDailyApyPcent: BN(dailyApyWithoutLSTApr ?? latestDailyApyFromApi).plus(additionalApyFromLsts).times(100).dp(2).toNumber(),
91-
latestWeeklyApyPcent: BN(weeklyApyWithoutLSTApr ?? latestWeeklyApyFromApi).plus(additionalApyFromLsts).times(100).dp(2).toNumber(),
98+
latestDailyApyPcent: BN((
99+
YB_POOLS_ADDRESSES.includes(lcAddress) ?
100+
latestDailyApyFromApi :
101+
(dailyApyWithoutLSTApr ?? latestDailyApyFromApi)
102+
)).plus(additionalApyFromLsts).times(100).dp(2).toNumber(),
103+
latestWeeklyApyPcent: BN((
104+
YB_POOLS_ADDRESSES.includes(lcAddress) ?
105+
latestWeeklyApyFromApi :
106+
(weeklyApyWithoutLSTApr ?? latestWeeklyApyFromApi)
107+
)).plus(additionalApyFromLsts).times(100).dp(2).toNumber(),
92108
includedApyPcentFromLsts: additionalApyFromLsts.times(100).dp(2).toNumber(),
93109
virtualPrice,
94110
}

0 commit comments

Comments
 (0)