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