Open position for bots

This commit is contained in:
2025-04-23 10:02:04 +02:00
parent 8d191e8d14
commit ad5996ca61
10 changed files with 410 additions and 208 deletions

View File

@@ -13,6 +13,7 @@ import { getSwapAmountsByFromValue, getSwapAmountsByToValue } from "../../utils/
import { EntryField, SidecarSlTpOrderEntryValid } from "../../types/sidecarOrders.js";
import { bigMath } from "../../utils/bigmath.js";
const ALLOWED_SLIPPAGE_BPS = BigInt(100);
/** Base Optional params for helpers, allows to avoid calling markets, tokens and uiFeeFactor methods if they are already passed */
interface BaseOptionalParams {
marketsInfoData?: MarketsInfoData;
@@ -174,7 +175,7 @@ export async function increaseOrderHelper(
indexPrice: 0n,
collateralPrice: 0n,
acceptablePrice: params.stopLossPrice,
acceptablePriceDeltaBps: 0n,
acceptablePriceDeltaBps: ALLOWED_SLIPPAGE_BPS,
recommendedAcceptablePriceDeltaBps: 0n,
estimatedPnl: 0n,
estimatedPnlPercentage: 0n,
@@ -194,8 +195,8 @@ export async function increaseOrderHelper(
payedRemainingCollateralUsd: 0n,
receiveTokenAmount: 0n,
receiveUsd: 0n,
decreaseSwapType: DecreasePositionSwapType.NoSwap,
triggerOrderType: OrderType.LimitDecrease,
decreaseSwapType: DecreasePositionSwapType.SwapPnlTokenToCollateralToken,
triggerOrderType: OrderType.StopLossDecrease,
triggerPrice: params.stopLossPrice,
}
@@ -239,7 +240,7 @@ export async function increaseOrderHelper(
indexPrice: 0n,
collateralPrice: 0n,
acceptablePrice: params.takeProfitPrice,
acceptablePriceDeltaBps: 0n,
acceptablePriceDeltaBps: ALLOWED_SLIPPAGE_BPS,
recommendedAcceptablePriceDeltaBps: 0n,
estimatedPnl: 0n,
estimatedPnlPercentage: 0n,
@@ -259,7 +260,7 @@ export async function increaseOrderHelper(
payedRemainingCollateralUsd: 0n,
receiveTokenAmount: 0n,
receiveUsd: 0n,
decreaseSwapType: DecreasePositionSwapType.NoSwap,
decreaseSwapType: DecreasePositionSwapType.SwapPnlTokenToCollateralToken,
triggerOrderType: OrderType.LimitDecrease,
triggerPrice: params.takeProfitPrice,
}

View File

@@ -126,8 +126,8 @@ export const openGmxPositionImpl = async (
quantity: number,
leverage: number,
price?: number,
takeProfitPrice?: number,
stopLossPrice?: number
stopLossPrice?: number,
takeProfitPrice?: number
): Promise<string> => {
try {
// Get markets and tokens data from GMX SDK
@@ -138,6 +138,8 @@ export const openGmxPositionImpl = async (
}
console.log('price', price)
console.log('stopLossPrice', stopLossPrice)
console.log('takeProfitPrice', takeProfitPrice)
const marketInfo = getMarketInfoFromTicker(ticker, marketsInfoData);
const collateralToken = getTokenDataFromTicker("USDC", tokensData); // Using USDC as collateral
@@ -167,7 +169,6 @@ export const openGmxPositionImpl = async (
console.log('params', params)
// Execute the main position order
if (direction === TradeDirection.Long) {
await sdk.orders.long(params);
} else {
@@ -208,8 +209,8 @@ export async function openGmxPosition(
price?: number,
quantity?: number,
leverage?: number,
takeProfitPrice?: number,
stopLossPrice?: number
stopLossPrice?: number,
takeProfitPrice?: number
) {
try {
// Validate the request parameters
@@ -221,13 +222,13 @@ export async function openGmxPosition(
price,
quantity,
leverage,
takeProfitPrice,
stopLossPrice
stopLossPrice,
takeProfitPrice
});
// Get client for the address
const sdk = await this.getClientForAddress(account);
// Call the implementation function
const hash = await openGmxPositionImpl(
sdk,
@@ -236,8 +237,8 @@ export async function openGmxPosition(
quantity,
leverage,
price,
takeProfitPrice,
stopLossPrice
stopLossPrice,
takeProfitPrice
);
return {