Fix convertion when getting positon
This commit is contained in:
@@ -285,7 +285,7 @@ export const openGmxPositionImpl = async (
|
|||||||
): Promise<string> => {
|
): Promise<string> => {
|
||||||
try {
|
try {
|
||||||
// Get markets and tokens data from GMX SDK with cache
|
// 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) {
|
if (!marketsInfoData || !tokensData) {
|
||||||
throw new Error("No markets or tokens info data");
|
throw new Error("No markets or tokens info data");
|
||||||
@@ -735,6 +735,7 @@ export const getGmxTradeImpl = async (
|
|||||||
|
|
||||||
if (initialCollateral !== 0n) {
|
if (initialCollateral !== 0n) {
|
||||||
// Convert values to regular numbers but preserve precision
|
// 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 size = Number(sizeDelta) / 1e30; // Convert from PRECISION_DECIMALS
|
||||||
const collateral = Number(initialCollateral) / 1e6; // USDC has 6 decimals
|
const collateral = Number(initialCollateral) / 1e6; // USDC has 6 decimals
|
||||||
|
|
||||||
@@ -752,7 +753,8 @@ export const getGmxTradeImpl = async (
|
|||||||
// Remove currency symbol and convert to number
|
// Remove currency symbol and convert to number
|
||||||
const numericPrice = Number(displayPrice.replace(/[^0-9.]/g, ''));
|
const numericPrice = Number(displayPrice.replace(/[^0-9.]/g, ''));
|
||||||
|
|
||||||
// Calculate size in tokens instead of USD based on the collateral and the trigger price
|
// 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);
|
const sizeInTokens = Number(sizeDelta) / Number(triggerPrice);
|
||||||
|
|
||||||
// Apply proper scaling for token amount
|
// Apply proper scaling for token amount
|
||||||
@@ -768,7 +770,7 @@ export const getGmxTradeImpl = async (
|
|||||||
leverage: leverage, // Fixed leverage value
|
leverage: leverage, // Fixed leverage value
|
||||||
status: TradeStatus.PendingOpen, // Assuming these are pending orders
|
status: TradeStatus.PendingOpen, // Assuming these are pending orders
|
||||||
tradeType: order.orderType == OrderType.LimitIncrease ? TradeType.Limit : TradeType.Market,
|
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
|
exchangeOrderId: order.key
|
||||||
} as Trade;
|
} 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
|
// 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) {
|
if (pos.sizeInUsd > 0n && pos.collateralAmount > 0n) {
|
||||||
// Use a simplified approach to calculate leverage directly
|
// 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
|
leverage: leverage, // Fixed leverage value
|
||||||
status: PositionStatus.Filled, // Represents an open/active position
|
status: PositionStatus.Filled, // Represents an open/active position
|
||||||
tradeType: TradeType.Market, // Placeholder
|
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
|
exchangeOrderId: pos.key, // Position key
|
||||||
pnl: bigintToNumber(pos.pnlAfterFees, PRECISION_DECIMALS), // PnL after fees (USD), assuming 30 decimals
|
pnl: bigintToNumber(pos.pnlAfterFees, PRECISION_DECIMALS), // PnL after fees (USD), assuming 30 decimals
|
||||||
collateral: bigintToNumber(pos.collateralAmount, collateralDecimals), // Collateral (in Collateral Token units)
|
collateral: bigintToNumber(pos.collateralAmount, collateralDecimals), // Collateral (in Collateral Token units)
|
||||||
|
|||||||
@@ -508,9 +508,8 @@ export const initAddressImpl = async (
|
|||||||
const {tokensData} = await sdk.tokens.getTokensData();
|
const {tokensData} = await sdk.tokens.getTokensData();
|
||||||
const usdcTokenData = getTokenDataFromTicker(Ticker.USDC, tokensData);
|
const usdcTokenData = getTokenDataFromTicker(Ticker.USDC, tokensData);
|
||||||
const wrapperEtherData = getTokenDataFromTicker("WETH", 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)
|
// Check approval for USDC (this first check is for general token approval, keeping original logic)
|
||||||
const usdcToken = GetToken('USDC');
|
const usdcToken = GetToken('USDC');
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { test } from 'node:test'
|
import {test} from 'node:test'
|
||||||
import assert from 'node:assert'
|
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'
|
import {Ticker} from '../../src/generated/ManagingApiTypes'
|
||||||
|
|
||||||
test('GMX Orders Closing', async (t) => {
|
test('GMX Orders Closing', async (t) => {
|
||||||
await t.test('should close all orders for BTC', async () => {
|
await t.test('should close all orders for BTC', async () => {
|
||||||
@@ -9,7 +9,7 @@ test('GMX Orders Closing', async (t) => {
|
|||||||
|
|
||||||
const result = await cancelGmxOrdersImpl(
|
const result = await cancelGmxOrdersImpl(
|
||||||
sdk,
|
sdk,
|
||||||
Ticker.BTC
|
Ticker.ETH
|
||||||
)
|
)
|
||||||
console.log('Orders closing result:', result)
|
console.log('Orders closing result:', result)
|
||||||
assert.ok(result, 'Orders closing result should be defined')
|
assert.ok(result, 'Orders closing result should be defined')
|
||||||
|
|||||||
@@ -13,9 +13,9 @@ test('GMX Position Opening', async (t) => {
|
|||||||
TradeDirection.Long,
|
TradeDirection.Long,
|
||||||
0.00678,
|
0.00678,
|
||||||
2,
|
2,
|
||||||
4400,
|
4410,
|
||||||
6000,
|
3500,
|
||||||
3500
|
6000
|
||||||
)
|
)
|
||||||
console.log('Position opening result:', result)
|
console.log('Position opening result:', result)
|
||||||
assert.ok(result, 'Position opening result should be defined')
|
assert.ok(result, 'Position opening result should be defined')
|
||||||
|
|||||||
Reference in New Issue
Block a user