Fix get Gas fees + position direction list
This commit is contained in:
@@ -82,7 +82,7 @@ function checkMemoryUsage() {
|
||||
* @param estimatedGasFee The estimated gas fee in wei
|
||||
* @returns Object with balance check result and details
|
||||
*/
|
||||
async function checkGasFeeBalance(
|
||||
export async function checkGasFeeBalance(
|
||||
sdk: GmxSdk,
|
||||
estimatedGasFee: bigint
|
||||
): Promise<{
|
||||
@@ -151,9 +151,8 @@ async function checkGasFeeBalance(
|
||||
* @param params The position increase parameters
|
||||
* @returns Estimated gas fee in wei
|
||||
*/
|
||||
async function estimatePositionGasFee(
|
||||
export async function estimatePositionGasFee(
|
||||
sdk: GmxSdk,
|
||||
params: PositionIncreaseParams
|
||||
): Promise<bigint> {
|
||||
try {
|
||||
// Estimate gas for the position opening transaction
|
||||
@@ -440,6 +439,7 @@ declare module 'fastify' {
|
||||
getGmxTrade: typeof getGmxTrade;
|
||||
getGmxPositions: typeof getGmxPositions;
|
||||
swapGmxTokens: typeof swapGmxTokens;
|
||||
estimatePositionGasFee: typeof estimatePositionGasFee;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -633,7 +633,7 @@ export const openGmxPositionImpl = async (
|
||||
|
||||
// Check gas fees before opening position
|
||||
console.log('⛽ Checking gas fees before opening position...');
|
||||
const estimatedGasFee = await estimatePositionGasFee(sdk, params);
|
||||
const estimatedGasFee = await estimatePositionGasFee(sdk);
|
||||
const gasFeeCheck = await checkGasFeeBalance(sdk, estimatedGasFee);
|
||||
|
||||
if (!gasFeeCheck.hasSufficientBalance) {
|
||||
@@ -1417,6 +1417,7 @@ export default fp(async (fastify) => {
|
||||
fastify.decorateRequest('claimGmxUiFees', claimGmxUiFees)
|
||||
fastify.decorateRequest('swapGmxTokens', swapGmxTokens)
|
||||
fastify.decorateRequest('checkGmxTokenAllowances', checkGmxTokenAllowances)
|
||||
fastify.decorateRequest('estimatePositionGasFee', estimatePositionGasFee)
|
||||
|
||||
// Set up cache refresh without blocking plugin registration
|
||||
setupCacheRefresh();
|
||||
|
||||
@@ -2,11 +2,15 @@ import {FastifyPluginAsyncTypebox} from '@fastify/type-provider-typebox'
|
||||
import {Type} from '@sinclair/typebox'
|
||||
import {TradeDirection} from '../../../generated/ManagingApiTypes.js'
|
||||
import {
|
||||
checkGasFeeBalance,
|
||||
estimatePositionGasFee,
|
||||
getClaimableFundingFeesImpl,
|
||||
getClaimableUiFeesImpl,
|
||||
getGmxRebateStatsImpl
|
||||
} from '../../../plugins/custom/gmx.js'
|
||||
|
||||
const MAX_GAS_FEE_USD = 1.5; // Maximum gas fee in USD
|
||||
|
||||
const plugin: FastifyPluginAsyncTypebox = async (fastify) => {
|
||||
// Define route to open a position
|
||||
fastify.post('/open-position', {
|
||||
@@ -194,6 +198,67 @@ const plugin: FastifyPluginAsyncTypebox = async (fastify) => {
|
||||
)
|
||||
})
|
||||
|
||||
// Define route to get gas fee estimation for opening a position
|
||||
fastify.get('/gas-fee', {
|
||||
schema: {
|
||||
response: {
|
||||
200: Type.Object({
|
||||
success: Type.Boolean(),
|
||||
data: Type.Optional(Type.Object({
|
||||
estimatedGasFeeWei: Type.String(),
|
||||
estimatedGasFeeEth: Type.String(),
|
||||
estimatedGasFeeUsd: Type.Number(),
|
||||
ethBalance: Type.String(),
|
||||
ethPrice: Type.Number(),
|
||||
hasSufficientBalance: Type.Boolean(),
|
||||
errorMessage: Type.Optional(Type.String()),
|
||||
maxAllowedUsd: Type.Number(),
|
||||
gasPrice: Type.String(),
|
||||
gasLimit: Type.String()
|
||||
})),
|
||||
error: Type.Optional(Type.String())
|
||||
})
|
||||
}
|
||||
}
|
||||
}, async (request, reply) => {
|
||||
try {
|
||||
// Use a default address for gas fee estimation
|
||||
const defaultAccount = "0x0000000000000000000000000000000000000000"
|
||||
|
||||
// Get GMX client for the default account
|
||||
const sdk = await request.getClientForAddress(defaultAccount)
|
||||
|
||||
// Call the two methods as mentioned
|
||||
const estimatedGasFee = await estimatePositionGasFee(sdk);
|
||||
const gasFeeCheck = await checkGasFeeBalance(sdk, estimatedGasFee);
|
||||
|
||||
// Format the response data
|
||||
const data = {
|
||||
estimatedGasFeeWei: estimatedGasFee.toString(),
|
||||
estimatedGasFeeEth: (Number(estimatedGasFee) / 1e18).toFixed(6),
|
||||
estimatedGasFeeUsd: gasFeeCheck.estimatedGasFeeUsd,
|
||||
ethBalance: gasFeeCheck.ethBalance,
|
||||
ethPrice: gasFeeCheck.ethPrice,
|
||||
hasSufficientBalance: gasFeeCheck.hasSufficientBalance,
|
||||
errorMessage: gasFeeCheck.errorMessage,
|
||||
maxAllowedUsd: MAX_GAS_FEE_USD,
|
||||
gasPrice: (Number(estimatedGasFee) / 500000).toString(), // Approximate gas price
|
||||
gasLimit: "500000" // Base gas limit used in estimation
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error estimating gas fee:', error)
|
||||
return {
|
||||
success: false,
|
||||
error: `Failed to estimate gas fee: ${error instanceof Error ? error.message : 'Unknown error'}`
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
// Define route to get all claimable fees and rebate stats
|
||||
fastify.get('/claimable-summary', {
|
||||
schema: {
|
||||
|
||||
Reference in New Issue
Block a user