Fix position

This commit is contained in:
2025-04-25 01:16:53 +07:00
parent 6c385ea309
commit a62b610f88

View File

@@ -1,28 +1,35 @@
import fp from 'fastify-plugin' import fp from 'fastify-plugin'
import { FastifyReply, FastifyRequest } from 'fastify' import {FastifyReply, FastifyRequest} from 'fastify'
import { z } from 'zod' import {z} from 'zod'
import { GmxSdk } from '../../generated/gmxsdk/index.js' import {GmxSdk} from '../../generated/gmxsdk/index.js'
import { arbitrum } from 'viem/chains'; import {arbitrum} from 'viem/chains';
import { getTokenBySymbol } from '../../generated/gmxsdk/configs/tokens.js'; import {getTokenBySymbol} from '../../generated/gmxsdk/configs/tokens.js';
import { Trade, TradeDirection, TradeStatus, TradeType, Position, PositionStatus } from '../../generated/ManagingApiTypes.js'; import {
import { MarketInfo, MarketsInfoData } from '../../generated/gmxsdk/types/markets.js'; Trade,
import { MarketConfig, MARKETS } from '../../generated/gmxsdk/configs/markets.js' TradeDirection,
import { ARBITRUM } from '../../generated/gmxsdk/configs/chains.js' TradeStatus,
import { TokenData, TokensData } from '../../generated/gmxsdk/types/tokens.js'; TradeType,
import { getByKey } from '../../generated/gmxsdk/utils/objects.js'; Position,
import { GmxSdkConfig } from '../../generated/gmxsdk/types/sdk.js'; PositionStatus
import { PositionIncreaseParams } from '../../generated/gmxsdk/modules/orders/helpers.js'; } from '../../generated/ManagingApiTypes.js';
import { numberToBigint, PRECISION_DECIMALS } from '../../generated/gmxsdk/utils/numbers.js'; import {MarketInfo, MarketsInfoData} from '../../generated/gmxsdk/types/markets.js';
import { DecreasePositionSwapType, PositionOrderInfo } from '../../generated/gmxsdk/types/orders.js'; import {MarketConfig, MARKETS} from '../../generated/gmxsdk/configs/markets.js'
import { OrderType } from '../../generated/gmxsdk/types/orders.js'; import {ARBITRUM} from '../../generated/gmxsdk/configs/chains.js'
import { DecreasePositionAmounts } from '../../generated/gmxsdk/types/trade.js'; import {TokenData, TokensData} from '../../generated/gmxsdk/types/tokens.js';
import { encodeReferralCode } from '../../generated/gmxsdk/utils/referrals.js'; import {getByKey} from '../../generated/gmxsdk/utils/objects.js';
import { convertToUsd } from '../../generated/gmxsdk/utils/tokens.js'; import {GmxSdkConfig} from '../../generated/gmxsdk/types/sdk.js';
import { toNumber } from 'ethers'; import {PositionIncreaseParams} from '../../generated/gmxsdk/modules/orders/helpers.js';
import { bigintToNumber } from '../../generated/gmxsdk/utils/numbers.js'; import {numberToBigint, PRECISION_DECIMALS} from '../../generated/gmxsdk/utils/numbers.js';
import { formatUsd } from '../../generated/gmxsdk/utils/numbers/formatting.js'; import {DecreasePositionSwapType, PositionOrderInfo} from '../../generated/gmxsdk/types/orders.js';
import { calculateDisplayDecimals } from '../../generated/gmxsdk/utils/numbers/index.js'; import {OrderType} from '../../generated/gmxsdk/types/orders.js';
import {DecreasePositionAmounts} from '../../generated/gmxsdk/types/trade.js';
import {encodeReferralCode} from '../../generated/gmxsdk/utils/referrals.js';
import {convertToUsd} from '../../generated/gmxsdk/utils/tokens.js';
import {toNumber} from 'ethers';
import {bigintToNumber} from '../../generated/gmxsdk/utils/numbers.js';
import {formatUsd} from '../../generated/gmxsdk/utils/numbers/formatting.js';
import {calculateDisplayDecimals} from '../../generated/gmxsdk/utils/numbers/index.js';
/** /**
* GMX Plugin * GMX Plugin
@@ -131,7 +138,7 @@ export const openGmxPositionImpl = async (
): Promise<string> => { ): Promise<string> => {
try { try {
// Get markets and tokens data from GMX SDK // Get markets and tokens data from GMX SDK
const { marketsInfoData, tokensData } = await sdk.markets.getMarketsInfo(); 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");
@@ -161,7 +168,7 @@ export const openGmxPositionImpl = async (
allowedSlippageBps: 100, // 1% slippage allowedSlippageBps: 100, // 1% slippage
leverage: leverageBps, leverage: leverageBps,
skipSimulation: true, skipSimulation: true,
limitPrice: null, limitPrice: limitPrice,
referralCodeForTxn: encodeReferralCode("kaigen_ai"), referralCodeForTxn: encodeReferralCode("kaigen_ai"),
stopLossPrice: stopLossPrice ? numberToBigint(stopLossPrice, 30) : undefined, stopLossPrice: stopLossPrice ? numberToBigint(stopLossPrice, 30) : undefined,
takeProfitPrice: takeProfitPrice ? numberToBigint(takeProfitPrice, 30) : undefined takeProfitPrice: takeProfitPrice ? numberToBigint(takeProfitPrice, 30) : undefined
@@ -211,7 +218,7 @@ export async function openGmxPosition(
leverage?: number, leverage?: number,
stopLossPrice?: number, stopLossPrice?: number,
takeProfitPrice?: number takeProfitPrice?: number
) { ) {
try { try {
// Validate the request parameters // Validate the request parameters
openPositionSchema.parse({ openPositionSchema.parse({
@@ -255,7 +262,7 @@ export async function openGmxPosition(
error: error instanceof Error ? error.message : 'An unknown error occurred' error: error instanceof Error ? error.message : 'An unknown error occurred'
}; };
} }
} }
/** /**
* Implementation function to cancel all GMX orders for a ticker * Implementation function to cancel all GMX orders for a ticker
@@ -270,7 +277,7 @@ export const cancelGmxOrdersImpl = async (
): Promise<boolean> => { ): Promise<boolean> => {
try { try {
// Get markets and tokens data first // Get markets and tokens data first
const { marketsInfoData, tokensData } = await sdk.markets.getMarketsInfo(); 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");
@@ -405,7 +412,7 @@ export const closeGmxPositionImpl = async (
): Promise<string> => { ): Promise<string> => {
try { try {
// Get markets and tokens data from GMX SDK // Get markets and tokens data from GMX SDK
const { marketsInfoData, tokensData } = await sdk.markets.getMarketsInfo(); 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");
@@ -434,7 +441,7 @@ export const closeGmxPositionImpl = async (
const position = positionsInfo[positionKey]; const position = positionsInfo[positionKey];
const decreaseAmounts: DecreasePositionAmounts ={ const decreaseAmounts: DecreasePositionAmounts = {
isFullClose: true, isFullClose: true,
sizeDeltaUsd: position.sizeInUsd, sizeDeltaUsd: position.sizeInUsd,
sizeDeltaInTokens: position.sizeInTokens, sizeDeltaInTokens: position.sizeInTokens,
@@ -545,7 +552,7 @@ export const getGmxTradeImpl = async (
ticker: string ticker: string
): Promise<Trade[]> => { ): Promise<Trade[]> => {
const { marketsInfoData, tokensData } = await sdk.markets.getMarketsInfo(); const {marketsInfoData, tokensData} = await sdk.markets.getMarketsInfo();
const orders = await sdk.orders.getOrders({ const orders = await sdk.orders.getOrders({
account: sdk.account, account: sdk.account,
@@ -668,7 +675,7 @@ export async function getGmxTrade(
export const getGmxPositionsImpl = async ( export const getGmxPositionsImpl = async (
sdk: GmxSdk sdk: GmxSdk
): Promise<Position[]> => { ): Promise<Position[]> => {
const { marketsInfoData, tokensData } = await sdk.markets.getMarketsInfo(); const {marketsInfoData, tokensData} = await sdk.markets.getMarketsInfo();
const positionsInfo = await sdk.positions.getPositionsInfo({ const positionsInfo = await sdk.positions.getPositionsInfo({
marketsInfoData, marketsInfoData,
@@ -829,5 +836,5 @@ export default fp(async (fastify) => {
fastify.decorateRequest('closeGmxPosition', closeGmxPosition) fastify.decorateRequest('closeGmxPosition', closeGmxPosition)
fastify.decorateRequest('getGmxTrade', getGmxTrade) fastify.decorateRequest('getGmxTrade', getGmxTrade)
fastify.decorateRequest('getGmxPositions', getGmxPositions) fastify.decorateRequest('getGmxPositions', getGmxPositions)
}) })