From c453d57261686f3ad9cf9df7e759b205834738d3 Mon Sep 17 00:00:00 2001 From: Adam Clarke Date: Fri, 4 Aug 2023 13:10:30 +0100 Subject: [PATCH 1/4] fix(app): allow to close position when below 50 usd margin (#2744) Allow to close position when below margin --- packages/sdk/src/contracts/PerpsV2MarketInternalV2.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/sdk/src/contracts/PerpsV2MarketInternalV2.ts b/packages/sdk/src/contracts/PerpsV2MarketInternalV2.ts index a535df99c2..01fccb70dd 100644 --- a/packages/sdk/src/contracts/PerpsV2MarketInternalV2.ts +++ b/packages/sdk/src/contracts/PerpsV2MarketInternalV2.ts @@ -201,8 +201,10 @@ class FuturesMarketInternal { const minInitialMargin = await this._getSetting('minInitialMargin') const positionDecreasing = - oldPos.size.gte(ZERO_BIG_NUM) === newPos.size.gte(ZERO_BIG_NUM) && - newPos.size.abs().lt(oldPos.size.abs()) + newPos.size.eq(ZERO_BIG_NUM) || + (oldPos.size.gte(ZERO_BIG_NUM) === newPos.size.gte(ZERO_BIG_NUM) && + newPos.size.abs().lt(oldPos.size.abs())) + if (!positionDecreasing) { if (newPos.margin.add(fee).lt(minInitialMargin)) { return { From ae7e7289ff033d25515d7276b144b6b5a77670b1 Mon Sep 17 00:00:00 2001 From: Adam Clarke Date: Fri, 4 Aug 2023 13:12:05 +0100 Subject: [PATCH 2/4] fix(*): update eth deposit messaging and fix a test (#2743) --- .../src/__tests__/pages/smartMargin.test.tsx | 11 +++++-- .../TradeConfirmationModal.tsx | 2 +- packages/app/src/state/__mocks__/sdk.ts | 29 ++----------------- packages/app/src/state/futures/selectors.ts | 1 - .../state/futures/smartMargin/selectors.ts | 9 +++--- packages/app/src/translations/en.json | 2 +- 6 files changed, 16 insertions(+), 38 deletions(-) diff --git a/packages/app/src/__tests__/pages/smartMargin.test.tsx b/packages/app/src/__tests__/pages/smartMargin.test.tsx index 3ea217f358..9c3c8be22e 100644 --- a/packages/app/src/__tests__/pages/smartMargin.test.tsx +++ b/packages/app/src/__tests__/pages/smartMargin.test.tsx @@ -149,10 +149,15 @@ describe('Futures market page - smart margin', () => { // Update the mock to return some different data sdk.futures.getMarkets = () => Promise.resolve([{ ...SDK_MARKETS[1], marketLimitUsd: wei(100000) } as PerpsMarketV2]) + sdk.futures.getSmartMarginBalanceInfo = () => + Promise.resolve({ + freeMargin: wei('100000'), + keeperEthBal: wei('0.1'), + walletEthBal: wei('1'), + allowance: wei('1000'), + }) - const store = setupStore( - preloadedStateWithSmartMarginAccount(mockSmartMarginAccount('1000000')) - ) + const store = setupStore(preloadedStateWithSmartMarginAccount(mockSmartMarginAccount('100000'))) const { findByTestId, findByText } = render( diff --git a/packages/app/src/sections/futures/TradeConfirmation/TradeConfirmationModal.tsx b/packages/app/src/sections/futures/TradeConfirmation/TradeConfirmationModal.tsx index bef5950fcf..cebfd20647 100644 --- a/packages/app/src/sections/futures/TradeConfirmation/TradeConfirmationModal.tsx +++ b/packages/app/src/sections/futures/TradeConfirmation/TradeConfirmationModal.tsx @@ -202,7 +202,7 @@ export default function TradeConfirmationModal({ const disabledReason = useMemo(() => { if (showEthBalWarning) { return t('futures.market.trade.confirmation.modal.disabled-eth-bal', { - depositAmount: stripZeros(keeperFee?.toString()), + depositAmount: formatNumber(stripZeros(keeperFee?.toString()), { suggestDecimals: true }), }) } if (positionDetails?.exceedsPriceProtection && !overridePriceProtection) { diff --git a/packages/app/src/state/__mocks__/sdk.ts b/packages/app/src/state/__mocks__/sdk.ts index a7e8b495aa..c72a3e2a0b 100644 --- a/packages/app/src/state/__mocks__/sdk.ts +++ b/packages/app/src/state/__mocks__/sdk.ts @@ -17,34 +17,9 @@ export const mockFuturesService = () => ({ getTradesForMarkets: () => [], getAllTrades: () => [], getConditionalOrders: () => [], - getIsolatedMarginTransfers: () => [], + getMarketMarginTransfers: () => [], getDelayedOrders: () => [], - getSmartMarginTransfers: () => [], - getSmartMarginBalanceInfo: () => ({ - freeMargin: wei('1000'), - keeperEthBal: wei('0.1'), - walletEthBal: wei('1'), - allowance: wei('1000'), - }), - getMarkets: () => { - return [...SDK_MARKETS] - }, - submitCrossMarginOrder: mockSubmitCrossMarginOrder, -}) - -export const mockPerpsService = () => ({ - getSmartMarginAccounts: () => ['0x7bCe4eF9d95129011528E502357C7772'], - getPreviousDayPrices: () => [], - getSmartMarginTradePreview: () => { - return { ...MOCK_TRADE_PREVIEW } - }, - getFuturesPositions: () => [], - getTradesForMarkets: () => [], - getAllTrades: () => [], - getConditionalOrders: () => [], - getIsolatedMarginTransfers: () => [], - getDelayedOrders: () => [], - getSmartMarginTransfers: () => [], + getSmartMarginAccountTransfers: () => [], getSmartMarginBalanceInfo: () => ({ freeMargin: wei('1000'), keeperEthBal: wei('0.1'), diff --git a/packages/app/src/state/futures/selectors.ts b/packages/app/src/state/futures/selectors.ts index 292eb2102d..9a00c2ba8e 100644 --- a/packages/app/src/state/futures/selectors.ts +++ b/packages/app/src/state/futures/selectors.ts @@ -9,7 +9,6 @@ import { truncateTimestamp } from '@kwenta/sdk/utils' import { createSelector } from '@reduxjs/toolkit' import { wei } from '@synthetixio/wei' -import { selectSNXUSDBalance, selectSusdBalance } from 'state/balances/selectors' import { selectAllCrossMarginTrades, selectCrossMarginAccountData, diff --git a/packages/app/src/state/futures/smartMargin/selectors.ts b/packages/app/src/state/futures/smartMargin/selectors.ts index 63be5bf2d0..caf0f8c2d4 100644 --- a/packages/app/src/state/futures/smartMargin/selectors.ts +++ b/packages/app/src/state/futures/smartMargin/selectors.ts @@ -512,7 +512,7 @@ export const selectSmartMarginDepositApproved = createSelector( } ) -export const selectMarginInMarkets = (isSuspended: boolean = false) => +export const selectIdleMarginInMarkets = (isSuspended: boolean = false) => createSelector(selectAllSmartMarginPositions, (positions) => { const idleInMarkets = positions .filter((p) => { @@ -523,9 +523,9 @@ export const selectMarginInMarkets = (isSuspended: boolean = false) => return idleInMarkets }) -export const selectAvailableMarginInMarkets = selectMarginInMarkets() +export const selectAvailableMarginInMarkets = selectIdleMarginInMarkets() -export const selectLockedMarginInMarkets = selectMarginInMarkets(true) +export const selectLockedMarginInMarkets = selectIdleMarginInMarkets(true) export const selectIdleAccountMargin = createSelector( selectAvailableMarginInMarkets, @@ -633,9 +633,8 @@ export const selectKeeperDepositExceedsBal = createSelector( export const selectEditPositionModalInfo = createSelector( selectEditPositionModalMarket, selectSmartMarginActivePositions, - selectV2Markets, selectPrices, - (modalMarketKey, smartPositions, markets, prices) => { + (modalMarketKey, smartPositions, prices) => { const position = smartPositions.find((p) => p.market.marketKey === modalMarketKey) if (!position || position.market.version === 3) return { position: null, market: null, marketPrice: wei(0) } diff --git a/packages/app/src/translations/en.json b/packages/app/src/translations/en.json index 7fcd692cb3..990da697bf 100644 --- a/packages/app/src/translations/en.json +++ b/packages/app/src/translations/en.json @@ -965,7 +965,7 @@ "disabled-min-margin": "Minimum market margin of $50 required", "disabled-eth-bal": "Min ETH {{depositAmount}} balance required", "disabled-exceeds-price-protection": "Price protection exceeded ", - "eth-bal-warning": "The limit order service requires some ETH to cover gas. The 0.01 ETH deposit will cover many orders and you can withdraw any unused balance.", + "eth-bal-warning": "Conditional orders require a small ETH deposit to use for gas when executing", "delayed-disclaimer": "The fee amount will be deposited to cover trade and automated execution fees. If an order is cancelled or not executed, the deposit will be forfeited to the Synthetix debt pool.", "stop-loss-warning": "Stop loss is close to the liquidation price and cannot be guaranteed to execute before you get liquidated, proceed anyway?", "slippage-warning": "This trade incurs high slippage, proceed anyway?" From b19b9388145d7e6c3f9211ca69b4d383d04d90e3 Mon Sep 17 00:00:00 2001 From: platschi Date: Fri, 4 Aug 2023 10:13:25 -0300 Subject: [PATCH 3/4] chore(*): bump version --- package.json | 2 +- packages/app/package.json | 2 +- packages/sdk/package.json | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index a21346eb8e..4945afc9db 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "kwenta", - "version": "7.4.17", + "version": "7.4.19", "description": "Kwenta", "main": "index.js", "scripts": { diff --git a/packages/app/package.json b/packages/app/package.json index 1f90ea2179..87598a9852 100644 --- a/packages/app/package.json +++ b/packages/app/package.json @@ -1,6 +1,6 @@ { "name": "@kwenta/app", - "version": "7.4.17", + "version": "7.4.19", "scripts": { "dev": "next", "build": "next build", diff --git a/packages/sdk/package.json b/packages/sdk/package.json index 262113ef3b..a7344dc28b 100644 --- a/packages/sdk/package.json +++ b/packages/sdk/package.json @@ -1,6 +1,6 @@ { "name": "@kwenta/sdk", - "version": "1.0.5", + "version": "1.0.6", "description": "SDK for headless interaction with Kwenta", "main": "dist/index.js", "directories": { From 75d875e2029924e20e17f828ae9817822c494ab2 Mon Sep 17 00:00:00 2001 From: Adam Clarke Date: Fri, 4 Aug 2023 14:52:13 +0100 Subject: [PATCH 4/4] fix(app): max size button (#2747) --- packages/app/src/state/futures/selectors.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/app/src/state/futures/selectors.ts b/packages/app/src/state/futures/selectors.ts index 9a00c2ba8e..11410529fa 100644 --- a/packages/app/src/state/futures/selectors.ts +++ b/packages/app/src/state/futures/selectors.ts @@ -365,7 +365,7 @@ export const selectTradePrice = createSelector( selectSmartMarginOrderPrice, selectMarketIndexPrice, (type, orderPrice, indexPrice) => { - return type === FuturesMarginType.CROSS_MARGIN ? indexPrice : wei(orderPrice || 0) + return type === FuturesMarginType.CROSS_MARGIN ? indexPrice : wei(orderPrice || indexPrice) } )