Fix convertion when getting positon

This commit is contained in:
2025-08-17 00:35:41 +07:00
parent 0aafab82b3
commit e38ad95a8b
4 changed files with 21 additions and 14 deletions

View File

@@ -285,7 +285,7 @@ export const openGmxPositionImpl = async (
): Promise<string> => {
try {
// Get markets and tokens data from GMX SDK with cache
const {marketsInfoData, tokensData} = await getMarketsInfoWithCache(sdk);
const {marketsInfoData, tokensData} = await sdk.markets.getMarketsInfo();
if (!marketsInfoData || !tokensData) {
throw new Error("No markets or tokens info data");
@@ -735,6 +735,7 @@ export const getGmxTradeImpl = async (
if (initialCollateral !== 0n) {
// Convert values to regular numbers but preserve precision
// Convert BigInt to Number before arithmetic operations
const size = Number(sizeDelta) / 1e30; // Convert from PRECISION_DECIMALS
const collateral = Number(initialCollateral) / 1e6; // USDC has 6 decimals
@@ -753,6 +754,7 @@ export const getGmxTradeImpl = async (
const numericPrice = Number(displayPrice.replace(/[^0-9.]/g, ''));
// Calculate size in tokens instead of USD based on the collateral and the trigger price
// Convert BigInt to Number before arithmetic operations
const sizeInTokens = Number(sizeDelta) / Number(triggerPrice);
// Apply proper scaling for token amount
@@ -768,7 +770,7 @@ export const getGmxTradeImpl = async (
leverage: leverage, // Fixed leverage value
status: TradeStatus.PendingOpen, // Assuming these are pending orders
tradeType: order.orderType == OrderType.LimitIncrease ? TradeType.Limit : TradeType.Market,
date: new Date(Number(order.updatedAtTime) * 1000),
date: new Date(Number(order.updatedAtTime) * 1000), // Convert BigInt to Number first
exchangeOrderId: order.key
} as Trade;
});
@@ -831,7 +833,13 @@ export const getGmxPositionsImpl = async (
// For positions, we already have the leverage value from GMX, but we want to ensure it's properly formatted
if (pos.sizeInUsd > 0n && pos.collateralAmount > 0n) {
// Use a simplified approach to calculate leverage directly
leverage = Math.round(Number(pos.sizeInUsd) / Number(pos.collateralAmount));
// Convert BigInt to Number before division to avoid mixing types
const sizeInUsdNumber = Number(pos.sizeInUsd);
const collateralAmountNumber = Number(pos.collateralAmount);
if (collateralAmountNumber > 0) {
leverage = Math.round(sizeInUsdNumber / collateralAmountNumber);
}
}
}
@@ -898,7 +906,7 @@ export const getGmxPositionsImpl = async (
leverage: leverage, // Fixed leverage value
status: PositionStatus.Filled, // Represents an open/active position
tradeType: TradeType.Market, // Placeholder
date: new Date(Number(pos.increasedAtTime) * 1000), // Position open time
date: new Date(Number(pos.increasedAtTime) * 1000), // Position open time - convert BigInt to Number first
exchangeOrderId: pos.key, // Position key
pnl: bigintToNumber(pos.pnlAfterFees, PRECISION_DECIMALS), // PnL after fees (USD), assuming 30 decimals
collateral: bigintToNumber(pos.collateralAmount, collateralDecimals), // Collateral (in Collateral Token units)

View File

@@ -508,9 +508,8 @@ export const initAddressImpl = async (
const {tokensData} = await sdk.tokens.getTokensData();
const usdcTokenData = getTokenDataFromTicker(Ticker.USDC, tokensData);
const wrapperEtherData = getTokenDataFromTicker("WETH", tokensData);
let approveAmount = usdcTokenData.prices.maxPrice; // Large enough amount for trading
// Use max uint256 for unlimited approvals (more gas efficient than frequent approvals)
let approveAmount = BigInt("115792089237316195423570985008687907853269984665640564039457584007913129639935");
// Check approval for USDC (this first check is for general token approval, keeping original logic)
const usdcToken = GetToken('USDC');

View File

@@ -1,6 +1,6 @@
import {test} from 'node:test'
import assert from 'node:assert'
import { getClientForAddress, cancelGmxOrdersImpl } from '../../src/plugins/custom/gmx'
import {cancelGmxOrdersImpl, getClientForAddress} from '../../src/plugins/custom/gmx'
import {Ticker} from '../../src/generated/ManagingApiTypes'
test('GMX Orders Closing', async (t) => {
@@ -9,7 +9,7 @@ test('GMX Orders Closing', async (t) => {
const result = await cancelGmxOrdersImpl(
sdk,
Ticker.BTC
Ticker.ETH
)
console.log('Orders closing result:', result)
assert.ok(result, 'Orders closing result should be defined')

View File

@@ -13,9 +13,9 @@ test('GMX Position Opening', async (t) => {
TradeDirection.Long,
0.00678,
2,
4400,
6000,
3500
4410,
3500,
6000
)
console.log('Position opening result:', result)
assert.ok(result, 'Position opening result should be defined')