Get fees to claims

This commit is contained in:
2025-06-13 14:22:38 +07:00
parent 5188b5faec
commit 3f34c56968
13 changed files with 580 additions and 20 deletions

View File

@@ -1122,10 +1122,10 @@ export const getClaimableFundingFeesImpl = async (
sdk: GmxSdk
): Promise<ClaimableFundingData> => {
try {
const { marketsInfoData } = await getMarketsInfoWithCache(sdk);
const { marketsInfoData, tokensData } = await getMarketsInfoWithCache(sdk);
if (!marketsInfoData) {
throw new Error("No markets info data available");
if (!marketsInfoData || !tokensData) {
throw new Error("No markets info data or tokens data available");
}
const marketAddresses = Object.keys(marketsInfoData);
@@ -1173,7 +1173,7 @@ export const getClaimableFundingFeesImpl = async (
const result = await sdk.executeMulticall(multicallRequest);
// Parse the response
// Parse the response and convert to USD
return Object.entries(result.data).reduce((claimableFundingData, [marketAddress, callsResult]: [string, any]) => {
const market = marketsInfoData[marketAddress];
@@ -1181,12 +1181,30 @@ export const getClaimableFundingFeesImpl = async (
return claimableFundingData;
}
// Get market divisor for proper decimal conversion
const marketDivisor = 1; // You might need to implement getMarketDivisor function
// Get token data for price conversion
const longTokenData = tokensData[market.longToken.address];
const shortTokenData = tokensData[market.shortToken.address];
if (!longTokenData || !shortTokenData) {
console.warn(`Missing token data for market ${marketAddress}`);
return claimableFundingData;
}
// Convert from wei to token units and then to USD
const longAmount = Number(callsResult.claimableFundingAmountLong.returnValues[0]);
const shortAmount = Number(callsResult.claimableFundingAmountShort.returnValues[0]);
// Convert from wei to token units using decimals
const longTokenUnits = longAmount / Math.pow(10, longTokenData.decimals);
const shortTokenUnits = shortAmount / Math.pow(10, shortTokenData.decimals);
// Convert to USD using token prices
const longUsdValue = longTokenUnits * (Number(longTokenData.prices.minPrice) / Math.pow(10, 30)); // GMX prices are in 30 decimals
const shortUsdValue = shortTokenUnits * (Number(shortTokenData.prices.minPrice) / Math.pow(10, 30));
claimableFundingData[marketAddress] = {
claimableFundingAmountLong: Number(callsResult.claimableFundingAmountLong.returnValues[0]) / marketDivisor,
claimableFundingAmountShort: Number(callsResult.claimableFundingAmountShort.returnValues[0]) / marketDivisor,
claimableFundingAmountLong: longUsdValue,
claimableFundingAmountShort: shortUsdValue,
};
return claimableFundingData;

View File

@@ -1,6 +1,11 @@
import {FastifyPluginAsyncTypebox} from '@fastify/type-provider-typebox'
import {Type} from '@sinclair/typebox'
import {TradeDirection} from '../../../generated/ManagingApiTypes'
import {
getClaimableFundingFeesImpl,
getClaimableUiFeesImpl,
getGmxRebateStatsImpl
} from '../../../plugins/custom/gmx.js'
const plugin: FastifyPluginAsyncTypebox = async (fastify) => {
// Define route to open a position
@@ -149,6 +154,93 @@ const plugin: FastifyPluginAsyncTypebox = async (fastify) => {
account
)
})
// Define route to get all claimable fees and rebate stats
fastify.get('/claimable-summary', {
schema: {
querystring: Type.Object({
account: Type.String()
}),
response: {
200: Type.Object({
success: Type.Boolean(),
data: Type.Optional(Type.Object({
claimableFundingFees: Type.Object({
totalUsdc: Type.Number()
}),
claimableUiFees: Type.Object({
totalUsdc: Type.Number()
}),
rebateStats: Type.Object({
totalRebateUsdc: Type.Number(),
discountUsdc: Type.Number(),
rebateFactor: Type.Number(),
discountFactor: Type.Number()
})
})),
error: Type.Optional(Type.String())
})
}
}
}, async (request, reply) => {
try {
const { account } = request.query
// Get GMX client for the account
const sdk = await request.getClientForAddress(account)
// Call all three implementation functions in parallel
const [fundingFeesData, uiFeesData, rebateStatsData] = await Promise.all([
getClaimableFundingFeesImpl(sdk),
getClaimableUiFeesImpl(sdk),
getGmxRebateStatsImpl(sdk)
])
// Process funding fees data - only calculate totals
let totalFundingLong = 0
let totalFundingShort = 0
if (fundingFeesData) {
Object.values(fundingFeesData).forEach(marketData => {
totalFundingLong += marketData.claimableFundingAmountLong
totalFundingShort += marketData.claimableFundingAmountShort
})
}
// Process UI fees data - only calculate totals
let totalUiFees = 0
if (uiFeesData) {
Object.values(uiFeesData).forEach(marketData => {
totalUiFees += marketData.claimableUiFeeAmount
})
}
return {
success: true,
data: {
claimableFundingFees: {
totalUsdc: totalFundingLong + totalFundingShort
},
claimableUiFees: {
totalUsdc: totalUiFees
},
rebateStats: {
totalRebateUsdc: rebateStatsData?.totalRebateUsd || 0,
discountUsdc: rebateStatsData?.discountUsd || 0,
rebateFactor: rebateStatsData?.rebateFactor || 0,
discountFactor: rebateStatsData?.discountFactor || 0
}
}
}
} catch (error) {
console.error('Error getting claimable summary:', error)
return {
success: false,
error: `Failed to get claimable summary: ${error instanceof Error ? error.message : 'Unknown error'}`
}
}
})
}
export default plugin

View File

@@ -16,9 +16,9 @@ describe('swap tokens implementation', () => {
try {
const result = await swapGmxTokensImpl(
sdk,
Ticker.GMX, // fromTicker
Ticker.BTC, // fromTicker
Ticker.USDC, // toTicker
2.06 // amount
0.000056 // amount
)
assert.strictEqual(typeof result, 'string')