diff --git a/src/Managing.Application.Tests/PositionTests.cs b/src/Managing.Application.Tests/PositionTests.cs index 934b929..7feabcb 100644 --- a/src/Managing.Application.Tests/PositionTests.cs +++ b/src/Managing.Application.Tests/PositionTests.cs @@ -21,7 +21,7 @@ public class PositionTests : BaseTests "test", MoneyManagement, TradeDirection.Long, - Ticker.BTC, + Ticker.ETH, PositionInitiator.User, DateTime.UtcNow, _account.User, diff --git a/src/Managing.Web3Proxy/src/plugins/custom/gmx.ts b/src/Managing.Web3Proxy/src/plugins/custom/gmx.ts index 707d909..f9db439 100644 --- a/src/Managing.Web3Proxy/src/plugins/custom/gmx.ts +++ b/src/Managing.Web3Proxy/src/plugins/custom/gmx.ts @@ -26,6 +26,7 @@ import {DecreasePositionAmounts} from '../../generated/gmxsdk/types/trade.js'; import {encodeReferralCode} from '../../generated/gmxsdk/utils/referrals.js'; import {formatUsd} from '../../generated/gmxsdk/utils/numbers/formatting.js'; import {calculateDisplayDecimals} from '../../generated/gmxsdk/utils/numbers/index.js'; +import {handleError} from '../../utils/errorHandler.js'; /** * GMX Plugin @@ -162,7 +163,6 @@ export const openGmxPositionImpl = async ( allowedSlippageBps: 100, // 1% slippage leverage: leverageBps, skipSimulation: true, - limitPrice: limitPrice, referralCodeForTxn: encodeReferralCode("kaigen_ai"), stopLossPrice: stopLossPrice ? numberToBigint(stopLossPrice, 30) : undefined, takeProfitPrice: takeProfitPrice ? numberToBigint(takeProfitPrice, 30) : undefined @@ -247,14 +247,7 @@ export async function openGmxPosition( hash }; } catch (error) { - this.log.error(error); - - // Return appropriate error response - reply.status(error instanceof z.ZodError ? 400 : 500); - return { - success: false, - error: error instanceof Error ? error.message : 'An unknown error occurred' - }; + return handleError(this, reply, error, 'gmx/open-position'); } } @@ -345,14 +338,7 @@ export async function cancelGmxOrders( success }; } catch (error) { - this.log.error(error); - - // Return appropriate error response - reply.status(error instanceof z.ZodError ? 400 : 500); - return { - success: false, - error: error instanceof Error ? error.message : 'An unknown error occurred' - }; + return handleError(this, reply, error, 'gmx/cancel-orders'); } } @@ -524,14 +510,7 @@ export async function closeGmxPosition( hash }; } catch (error) { - this.log.error(error); - - // Return appropriate error response - reply.status(500); - return { - success: false, - error: error instanceof Error ? error.message : 'An unknown error occurred' - }; + return handleError(this, reply, error, 'gmx/close-position'); } } @@ -650,14 +629,7 @@ export async function getGmxTrade( trades }; } catch (error) { - this.log.error(error); - - // Return appropriate error response - reply.status(500); - return { - success: false, - error: error instanceof Error ? error.message : 'An unknown error occurred' - }; + return handleError(this, reply, error, 'gmx/trades'); } } @@ -807,12 +779,7 @@ export async function getGmxPositions( positions, }; } catch (error) { - this.log.error(error); - reply.status(500); - return { - success: false, - error: error instanceof Error ? error.message : 'An unknown error occurred', - }; + return handleError(this, reply, error, 'gmx/positions'); } } diff --git a/src/Managing.Web3Proxy/src/routes/api/gmx/index.ts b/src/Managing.Web3Proxy/src/routes/api/gmx/index.ts index 5496866..9ba4995 100644 --- a/src/Managing.Web3Proxy/src/routes/api/gmx/index.ts +++ b/src/Managing.Web3Proxy/src/routes/api/gmx/index.ts @@ -1,7 +1,6 @@ import {FastifyPluginAsyncTypebox} from '@fastify/type-provider-typebox' import {Type} from '@sinclair/typebox' -import { TradeDirection } from '../../../generated/ManagingApiTypes' -import { handleError } from '../../../utils/errorHandler.js' +import {TradeDirection} from '../../../generated/ManagingApiTypes' const plugin: FastifyPluginAsyncTypebox = async (fastify) => { // Define route to open a position @@ -29,25 +28,19 @@ const plugin: FastifyPluginAsyncTypebox = async (fastify) => { }, async (request, reply) => { const { account, tradeType, ticker, direction, price, quantity, leverage, stopLossPrice, takeProfitPrice } = request.body - try { - // Call the openPosition method from the GMX plugin - const result = await request.openGmxPosition( - reply, - account, - tradeType, - ticker, - direction as TradeDirection, - price, - quantity, - leverage, - stopLossPrice, - takeProfitPrice - ) - - return result - } catch (error) { - return handleError(request, reply, error, 'gmx/open-position'); - } + // Call the openPosition method from the GMX plugin + return request.openGmxPosition( + reply, + account, + tradeType, + ticker, + direction as TradeDirection, + price, + quantity, + leverage, + stopLossPrice, + takeProfitPrice + ) }) // Define route to cancel orders @@ -67,18 +60,12 @@ const plugin: FastifyPluginAsyncTypebox = async (fastify) => { }, async (request, reply) => { const { account, ticker } = request.body - try { - // Call the cancelGmxOrders method from the GMX plugin - const result = await request.cancelGmxOrders( - reply, - account, - ticker, - ) - - return result - } catch (error) { - return handleError(request, reply, error, 'gmx/cancel-orders'); - } + // Call the cancelGmxOrders method from the GMX plugin + return request.cancelGmxOrders( + reply, + account, + ticker, + ) }) // Define route to close a position @@ -100,19 +87,13 @@ const plugin: FastifyPluginAsyncTypebox = async (fastify) => { }, async (request, reply) => { const { account, ticker, direction } = request.body - try { - // Call the closePosition method from the GMX plugin - const result = await request.closeGmxPosition( - reply, - account, - ticker, - direction as TradeDirection - ); - - return result; - } catch (error) { - return handleError(request, reply, error, 'gmx/close-position'); - } + // Call the closePosition method from the GMX plugin + return request.closeGmxPosition( + reply, + account, + ticker, + direction as TradeDirection + ) }) // Define route to get a trade @@ -133,20 +114,16 @@ const plugin: FastifyPluginAsyncTypebox = async (fastify) => { }, async (request, reply) => { const { account, ticker } = request.query - try { - // Call the getGmxTrade method from the GMX plugin - const result = await request.getGmxTrade( - reply, - account, - ticker - ) + // Call the getGmxTrade method from the GMX plugin + const result = await request.getGmxTrade( + reply, + account, + ticker + ) - console.log('result', result) - - return result - } catch (error) { - return handleError(request, reply, error, 'gmx/trades'); - } + console.log('result', result) + + return result }) // Define route to get positions @@ -166,17 +143,11 @@ const plugin: FastifyPluginAsyncTypebox = async (fastify) => { }, async (request, reply) => { const { account } = request.query - try { - // Call the getGmxPositions method from the GMX plugin - const result = await request.getGmxPositions( - reply, - account - ) - - return result - } catch (error) { - return handleError(request, reply, error, 'gmx/positions'); - } + // Call the getGmxPositions method from the GMX plugin + return request.getGmxPositions( + reply, + account + ) }) } diff --git a/src/Managing.WebApp/src/components/organism/Trading/TradeChart/TradeChart.tsx b/src/Managing.WebApp/src/components/organism/Trading/TradeChart/TradeChart.tsx index 075e5a3..2833513 100644 --- a/src/Managing.WebApp/src/components/organism/Trading/TradeChart/TradeChart.tsx +++ b/src/Managing.WebApp/src/components/organism/Trading/TradeChart/TradeChart.tsx @@ -9,7 +9,7 @@ import type { Time, UTCTimestamp, } from 'lightweight-charts' -import {LineStyle, createChart, CrosshairMode} from 'lightweight-charts' +import {createChart, CrosshairMode, LineStyle} from 'lightweight-charts' import moment from 'moment' import * as React from 'react' import {useEffect, useRef, useState} from 'react' @@ -22,10 +22,7 @@ import type { StrategiesResultBase, StrategyType, } from '../../../../generated/ManagingApi' -import { - PositionStatus, - TradeDirection, -} from '../../../../generated/ManagingApi' +import {PositionStatus, TradeDirection,} from '../../../../generated/ManagingApi' import useTheme from '../../../../hooks/useTheme' // var customTheme = { @@ -143,8 +140,8 @@ const TradeChart = ({ let color = 'mintcream' if (position == undefined) return color - const negativeColor = 'palevioletred' - const positiveColor = 'lightgreen' + const negativeColor = theme.error + const positiveColor = theme.success const status = position.status const realized = position.profitAndLoss?.realized ?? 0 @@ -158,13 +155,11 @@ const TradeChart = ({ } else { color = negativeColor } + }else if (status == PositionStatus.Filled) { + color = theme.warning } } - if (position.profitAndLoss?.realized == null) { - color = 'yellow' - } - return color }