Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

♻️ Futures margin types refactor / cleanup and perps v3 prep #2571

Merged
merged 58 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
35005ce
Initial perps v3 sdk service setup
avclarke Jul 4, 2023
ed37116
Margin types refactor part 1
avclarke Jul 7, 2023
4ac3dc0
Update sdk mock to fix tests
avclarke Jul 7, 2023
c2d3966
Perps v3 type updates
avclarke Jul 9, 2023
8324b8f
More futures state refactoring
avclarke Jul 10, 2023
1736aee
Merge branch 'dev' into perps-v3
avclarke Jul 11, 2023
9973be5
Update futures modal types
avclarke Jul 11, 2023
148b72f
Update onboard name
avclarke Jul 11, 2023
f2f4fda
Fix tests
avclarke Jul 11, 2023
e947011
Cross margin deposits
avclarke Jul 13, 2023
a7f893c
Merge branch 'dev' into perps-v3
avclarke Jul 13, 2023
2bb0050
clean up
avclarke Jul 17, 2023
fcecb57
Merge branch 'perps-v3' into perps-v3-part2
avclarke Jul 17, 2023
3b40eb4
Fetch available margin
avclarke Jul 18, 2023
7ff6089
Fix dashboard positions
avclarke Jul 18, 2023
c48e41a
Merge branch 'perps-v3' into perps-v3-part2
avclarke Jul 18, 2023
9a49fcf
Refactor trade inputs
avclarke Jul 18, 2023
c16a4ba
Add TSDoc
koredefashokun Jul 19, 2023
5f0d112
Submit v3 order
avclarke Jul 20, 2023
ce73b65
Merge branch 'dev' into perps-v3
avclarke Jul 20, 2023
b15c967
Merge branch 'perps-v3' into perps-v3-part2
avclarke Jul 20, 2023
73da04d
Core v3 flows
avclarke Jul 26, 2023
3b8aa67
Merge branch 'dev' into perps-v3
avclarke Jul 27, 2023
c92b64f
Merge branch 'perps-v3' into perps-v3-part2
avclarke Jul 27, 2023
42233a8
v3 updates
avclarke Jul 27, 2023
8f22545
Improved v3 account query
avclarke Jul 28, 2023
9cca0cc
Fix deposit / withdraw
avclarke Jul 28, 2023
03f2543
Add value check on number formatting
avclarke Jul 28, 2023
4ebcfd2
Fix smart margin balance
avclarke Jul 28, 2023
b9d0a9a
Merge branch 'dev' into perps-v3
avclarke Jul 28, 2023
6319ddd
Merge branch 'perps-v3' into perps-v3-part2
avclarke Jul 28, 2023
b5a0bdc
Fix smart margin pending orders
avclarke Jul 28, 2023
957ea75
Fix smart margin withdraw
avclarke Jul 28, 2023
d1a5971
Merge pull request #2706 from Kwenta/perps-v3-part2
avclarke Jul 28, 2023
114582e
Merge dev
avclarke Jul 31, 2023
38c0ef0
Merge branch 'dev' into perps-v3
avclarke Jul 31, 2023
9774ed3
Fix dashboard
avclarke Jul 31, 2023
b212e5f
Fix tests
avclarke Jul 31, 2023
b0ebd1f
Remove transfers tab
avclarke Jul 31, 2023
36752d3
Fix leaderboard trader details
avclarke Jul 31, 2023
3c26bbe
Trade panel updates
avclarke Jul 31, 2023
b00a148
Fix trades empty state
avclarke Jul 31, 2023
88c10fd
Fix mobile orders
avclarke Jul 31, 2023
6a30fff
Clean up
avclarke Jul 31, 2023
8d7a73c
Update jest config
avclarke Jul 31, 2023
b3af3b3
Update Jest packages
avclarke Jul 31, 2023
1a30810
Update jest command
avclarke Jul 31, 2023
4cde511
Merge branch 'dev' into perps-v3
avclarke Aug 1, 2023
7ed082b
Disable cross margin in prod
avclarke Aug 1, 2023
d99cdb2
Initial cross margin tests
avclarke Aug 1, 2023
6da76fd
Update v3 contracts
avclarke Aug 1, 2023
85ce9f2
Fix sl tp display on positions table
avclarke Aug 1, 2023
8e75376
Ensure enough ETH is deposited to cover all pending orders
avclarke Aug 2, 2023
dc83797
Merge branch 'dev' into perps-v3
avclarke Aug 2, 2023
0ce98f9
Bump store version
avclarke Aug 2, 2023
9159a32
chore(*): sdk cleanup (#2633)
koredefashokun Aug 2, 2023
271037e
Merge branch 'dev' into perps-v3
platschi Aug 2, 2023
f443831
Update link to smart margin
avclarke Aug 2, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/app/src/__tests__/pages/market.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FuturesMarket } from '@kwenta/sdk/dist/types'
import { FuturesMarket } from '@kwenta/sdk/types'
import { wei } from '@synthetixio/wei'
import { fireEvent, render, waitFor } from '@testing-library/react'
import { ReactNode } from 'react'
Expand Down
6 changes: 3 additions & 3 deletions packages/app/src/components/Nav/FuturesIcon.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FuturesAccountType } from '@kwenta/sdk/utils'
import { FuturesMarginType } from '@kwenta/sdk/types'

import CrossMarginIconDark from 'assets/svg/futures/cross-margin-icon-dark.svg'
import CrossMarginIconLight from 'assets/svg/futures/cross-margin-icon-light.svg'
Expand All @@ -8,7 +8,7 @@ import { useAppSelector } from 'state/hooks'
import { selectCurrentTheme } from 'state/preferences/selectors'

type IconProps = {
type: FuturesAccountType
type: FuturesMarginType
}

export default function FuturesIcon(props: IconProps) {
Expand All @@ -17,7 +17,7 @@ export default function FuturesIcon(props: IconProps) {
const CrossMarginIcon = currentTheme === 'dark' ? CrossMarginIconDark : CrossMarginIconLight
const IsolatedMarginIcon =
currentTheme === 'dark' ? IsolatedMarginIconDark : IsolatedMarginIconLight
return props.type === 'cross_margin' || props.type === 'smart_margin' ? (
return props.type === FuturesMarginType.SMART_MARGIN ? (
<CrossMarginIcon {...props} />
) : (
<IsolatedMarginIcon {...props} />
Expand Down
3 changes: 2 additions & 1 deletion packages/app/src/components/SocketBridge/SocketBridge.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { FuturesMarginType } from '@kwenta/sdk/types'
import { Bridge } from '@socket.tech/plugin'
import { useCallback } from 'react'
import styled, { useTheme } from 'styled-components'
Expand Down Expand Up @@ -42,7 +43,7 @@ const SocketBridge = () => {
enableSameChainSwaps={true}
onBridgeSuccess={onBridgeSuccess}
/>
{accountType === 'isolated_margin' && (
{accountType === FuturesMarginType.CROSS_MARGIN && (
<StyledDiv>
<ArrowIcon />
</StyledDiv>
Expand Down
4 changes: 3 additions & 1 deletion packages/app/src/constants/defaults.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { FuturesMarginType } from '@kwenta/sdk/types'

import { Language } from 'translations/constants'

// app defaults
Expand Down Expand Up @@ -35,6 +37,6 @@ export const DEFAULT_DELAYED_CANCEL_BUFFER = 15

export const CROSS_MARGIN_ENABLED = true

export const DEFAULT_FUTURES_MARGIN_TYPE = 'cross_margin'
export const DEFAULT_FUTURES_MARGIN_TYPE = FuturesMarginType.SMART_MARGIN

export const DEFAULT_LEVERAGE = '1'
6 changes: 6 additions & 0 deletions packages/app/src/constants/futures.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { FuturesMarginType } from '@kwenta/sdk/types'

export const SUPPORTED_PERPS_TYPES =
process.env.NODE_ENV !== 'production'
? [FuturesMarginType.SMART_MARGIN]
: [FuturesMarginType.SMART_MARGIN, FuturesMarginType.CROSS_MARGIN]
21 changes: 1 addition & 20 deletions packages/app/src/constants/queryKeys.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Period } from '@kwenta/sdk/constants'
import { NetworkId, FuturesAccountType, FuturesMarketAsset } from '@kwenta/sdk/types'
import { NetworkId, FuturesMarketAsset } from '@kwenta/sdk/types'

import { CurrencyKey } from './currency'

Expand Down Expand Up @@ -239,25 +239,6 @@ export const QUERY_KEYS = {
TotalLiquidations: ['futures', 'totalLiquidations'],
TotalTrades: (networkId: NetworkId) => ['futures', 'totalTrades', networkId],
TotalVolume: ['futures', 'totalVolume'],
PotentialTrade: (
networkId: NetworkId,
market: string | null,
tradeSize: string,
walletAddress: string,
selectedAccountType: FuturesAccountType,
marginDelta: string,
leverageSide: string
) => [
'futures',
'potentialTrade',
tradeSize,
networkId,
market,
walletAddress,
selectedAccountType,
marginDelta,
leverageSide,
],
MarketLimit: (networkId: NetworkId, market: string | null) => [
'futures',
'marketLimit',
Expand Down
24 changes: 15 additions & 9 deletions packages/app/src/constants/routes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { FuturesAccountType, FuturesMarketAsset } from '@kwenta/sdk/types'
import { FuturesMarketAsset } from '@kwenta/sdk/types'

import { AppFuturesMarginType } from 'state/futures/types'

import { EXTERNAL_LINKS } from './links'

Expand Down Expand Up @@ -32,22 +34,23 @@ export const ROUTES = {
Into: (currencyKey: string) => `/exchange/?quote=${currencyKey}`,
},
Markets: {
Home: (accountType: FuturesAccountType) => formatUrl('/market', { accountType, asset: 'sETH' }),
MarketPair: (asset: FuturesMarketAsset | string, accountType: FuturesAccountType) =>
Home: (accountType: AppFuturesMarginType) =>
formatUrl('/market', { accountType, asset: 'sETH' }),
MarketPair: (asset: FuturesMarketAsset | string, accountType: AppFuturesMarginType) =>
formatUrl('/market', { asset, accountType }),
Position: (asset: FuturesMarketAsset, accountType: FuturesAccountType) =>
Position: (asset: FuturesMarketAsset, accountType: AppFuturesMarginType) =>
formatUrl('/market', {
asset,
accountType,
tab: 'position',
}),
Orders: (asset: FuturesMarketAsset, accountType: FuturesAccountType) =>
Orders: (asset: FuturesMarketAsset, accountType: AppFuturesMarginType) =>
formatUrl('/market', { asset, accountType, tab: 'orders' }),
ConditionalOrders: (asset: FuturesMarketAsset, accountType: FuturesAccountType) =>
ConditionalOrders: (asset: FuturesMarketAsset, accountType: AppFuturesMarginType) =>
formatUrl('/market', { asset, accountType, tab: 'conditional_orders' }),
Trades: (asset: FuturesMarketAsset, accountType: FuturesAccountType) =>
Trades: (asset: FuturesMarketAsset, accountType: AppFuturesMarginType) =>
formatUrl('/market', { asset, accountType, tab: 'trades' }),
Transfers: (asset: FuturesMarketAsset, accountType: FuturesAccountType) =>
Transfers: (asset: FuturesMarketAsset, accountType: AppFuturesMarginType) =>
formatUrl('/market', { asset, accountType, tab: 'transfers' }),
},
Stats: {
Expand Down Expand Up @@ -75,7 +78,10 @@ export const SUB_MENUS = {
],
}

export const setLastVisited = (baseCurrencyPair: string, accountType: FuturesAccountType): void => {
export const setLastVisited = (
baseCurrencyPair: string,
accountType: AppFuturesMarginType
): void => {
localStorage.setItem('lastVisited', ROUTES.Markets.MarketPair(baseCurrencyPair, accountType))
}

Expand Down
44 changes: 20 additions & 24 deletions packages/app/src/pages/market.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { FuturesMarketAsset } from '@kwenta/sdk/types'
import { FuturesMarginType, FuturesMarketAsset } from '@kwenta/sdk/types'
import { MarketKeyByAsset } from '@kwenta/sdk/utils'
import { useRouter } from 'next/router'
import { useEffect, FC, useState, ReactNode } from 'react'
import { useEffect, FC, ReactNode, useMemo } from 'react'
import styled from 'styled-components'

import Loader from 'components/Loader'
import { DesktopOnlyView, MobileOrTabletView } from 'components/Media'
import { SUPPORTED_PERPS_TYPES } from 'constants/futures'
import Connector from 'containers/Connector'
import useIsL2 from 'hooks/useIsL2'
import useWindowSize from 'hooks/useWindowSize'
Expand All @@ -19,26 +20,25 @@ import MarketHead from 'sections/futures/MarketInfo/MarketHead'
import MobileTrade from 'sections/futures/MobileTrade/MobileTrade'
import { TRADE_PANEL_WIDTH_LG, TRADE_PANEL_WIDTH_MD } from 'sections/futures/styles'
import FuturesUnsupportedNetwork from 'sections/futures/Trade/FuturesUnsupported'
import SwitchToSmartMargin from 'sections/futures/Trade/SwitchToSmartMargin'
import TradeIsolatedMargin from 'sections/futures/Trade/TradePanel'
import TradePanel from 'sections/futures/Trade/TradePanel'
import TransferIsolatedMarginModal from 'sections/futures/Trade/TransferIsolatedMarginModal'
import DelayedOrderConfirmationModal from 'sections/futures/TradeConfirmation/DelayedOrderConfirmationModal'
import TradeConfirmationModalCrossMargin from 'sections/futures/TradeConfirmation/TradeConfirmationModalCrossMargin'
import WithdrawSmartMargin from 'sections/futures/TradeCrossMargin/WithdrawSmartMargin'
import WithdrawSmartMargin from 'sections/futures/TradeSmartMargin/WithdrawSmartMargin'
import AppLayout from 'sections/shared/Layout/AppLayout'
import { setOpenModal } from 'state/app/reducer'
import { selectShowModal, selectShowPositionModal } from 'state/app/selectors'
import { clearTradeInputs } from 'state/futures/actions'
import { usePollMarketFuturesData } from 'state/futures/hooks'
import { setFuturesAccountType, setMarketAsset } from 'state/futures/reducer'
import {
selectActiveIsolatedPositionsCount,
selectCMAccountQueryStatus,
selectCrossMarginAccount,
selectSmartMarginAccountQueryStatus,
selectSmartMarginAccount,
selectFuturesType,
selectMarketAsset,
selectShowCrossMarginOnboard,
selectShowSmartMarginOnboard,
} from 'state/futures/selectors'
import { AppFuturesMarginType } from 'state/futures/types'
import { useAppDispatch, useAppSelector } from 'state/hooks'
import { FetchStatus } from 'state/types'
import { PageContent } from 'styles/common'
Expand All @@ -56,14 +56,17 @@ const Market: MarketComponent = () => {
const routerMarketAsset = router.query.asset as FuturesMarketAsset

const setCurrentMarket = useAppSelector(selectMarketAsset)
const showOnboard = useAppSelector(selectShowCrossMarginOnboard)
const showOnboard = useAppSelector(selectShowSmartMarginOnboard)
const openModal = useAppSelector(selectShowModal)
const showPositionModal = useAppSelector(selectShowPositionModal)
const accountType = useAppSelector(selectFuturesType)
const selectedMarketAsset = useAppSelector(selectMarketAsset)

const routerAccountType =
router.query.accountType === 'cross_margin' ? 'cross_margin' : 'isolated_margin'
const routerAccountType = useMemo(() => {
if (SUPPORTED_PERPS_TYPES.includes(router.query.accountType as AppFuturesMarginType))
return router.query.accountType as AppFuturesMarginType
return FuturesMarginType.SMART_MARGIN
}, [router.query.accountType])

useEffect(() => {
if (router.isReady && accountType !== routerAccountType) {
Expand Down Expand Up @@ -142,25 +145,18 @@ function TradePanelDesktop() {
const isL2 = useIsL2()
const { walletAddress } = Connector.useContainer()
const accountType = useAppSelector(selectFuturesType)
const queryStatus = useAppSelector(selectCMAccountQueryStatus)
const crossMarginAccount = useAppSelector(selectCrossMarginAccount)
const isolatedPositionsCount = useAppSelector(selectActiveIsolatedPositionsCount)
const [open, setOpen] = useState(false)

useEffect(
() => setOpen(accountType === 'isolated_margin' && isolatedPositionsCount === 0),
[accountType, isolatedPositionsCount]
)
const queryStatus = useAppSelector(selectSmartMarginAccountQueryStatus)
const smartMarginAccount = useAppSelector(selectSmartMarginAccount)

if (walletAddress && !isL2) {
return <FuturesUnsupportedNetwork />
}

if (
!router.isReady ||
(accountType === 'cross_margin' &&
(accountType === FuturesMarginType.SMART_MARGIN &&
walletAddress &&
!crossMarginAccount &&
!smartMarginAccount &&
queryStatus.status === FetchStatus.Idle)
) {
return (
Expand All @@ -170,7 +166,7 @@ function TradePanelDesktop() {
)
}

return open ? <SwitchToSmartMargin onDismiss={() => setOpen(false)} /> : <TradeIsolatedMargin />
return <TradePanel />
}

Market.getLayout = (page) => <AppLayout>{page}</AppLayout>
Expand Down
5 changes: 0 additions & 5 deletions packages/app/src/queries/futures/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,3 @@ export type FuturesCumulativeStats = {
totalLiquidations: string
averageTradeSize: string
}

export enum FuturesAccountTypes {
ISOLATED_MARGIN = 'isolated_margin',
CROSS_MARGIN = 'cross_margin',
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FuturesAccountType } from '@kwenta/sdk/utils'
import { FuturesMarginType } from '@kwenta/sdk/types'
import Link from 'next/link'
import { useRouter } from 'next/router'
import { FC, useMemo } from 'react'
Expand All @@ -18,19 +18,20 @@
import useIsL2 from 'hooks/useIsL2'
import useNetworkSwitcher from 'hooks/useNetworkSwitcher'
import PositionType from 'sections/futures/PositionType'
import { selectCrossMarginPositions } from 'state/crossMargin/selectors'
import {
selectCrossMarginPositions,
selectIsolatedMarginPositions,
selectSmartMarginPositions,
selectMarkets,
selectPositionHistory,
} from 'state/futures/selectors'
import { AppFuturesMarginType } from 'state/futures/types'
import { useAppSelector } from 'state/hooks'
import { getSynthDescription } from 'utils/futures'

import MobilePositionRow from './MobilePositionRow'

type FuturesPositionTableProps = {
accountType: FuturesAccountType
accountType: AppFuturesMarginType
showCurrentMarket?: boolean
showEmptyTable?: boolean
}
Expand All @@ -46,13 +47,14 @@

const isL2 = useIsL2()

const isolatedPositions = useAppSelector(selectIsolatedMarginPositions)
const crossMarginPositions = useAppSelector(selectCrossMarginPositions)
const smartMarginPositions = useAppSelector(selectSmartMarginPositions)
const positionHistory = useAppSelector(selectPositionHistory)
const futuresMarkets = useAppSelector(selectMarkets)

let data = useMemo(() => {
const positions = accountType === 'cross_margin' ? crossMarginPositions : isolatedPositions
const positions =
accountType === FuturesMarginType.SMART_MARGIN ? smartMarginPositions : crossMarginPositions
return positions
.map((position) => {
const market = futuresMarkets.find((market) => market.asset === position.asset)
Expand All @@ -71,15 +73,15 @@
}
})
.filter(({ position, market }) => !!position && !!market)
}, [accountType, isolatedPositions, crossMarginPositions, futuresMarkets, t, positionHistory])
}, [accountType, crossMarginPositions, smartMarginPositions, futuresMarkets, t, positionHistory])
Fixed Show fixed Hide fixed

return (
<>
<DesktopOnlyView>
<div>
<Table
data={data}
hiddenColumns={accountType === 'isolated_margin' ? ['tp-sl'] : []}
hiddenColumns={accountType === FuturesMarginType.SMART_MARGIN ? ['tp-sl'] : []}
onTableRowClick={(row) =>
router.push(ROUTES.Markets.MarketPair(row.original.market.asset, accountType))
}
Expand All @@ -94,7 +96,7 @@
{!showCurrentMarket ? (
t('dashboard.overview.futures-positions-table.no-result')
) : (
<Link href={ROUTES.Markets.Home('cross_margin')}>
<Link href={ROUTES.Markets.Home(FuturesMarginType.SMART_MARGIN)}>
<div>{t('common.perp-cta')}</div>
</Link>
)}
Expand Down Expand Up @@ -293,7 +295,7 @@
<div style={{ margin: '0 15px' }}>
{data.length === 0 ? (
<NoPositionsText>
<Link href={ROUTES.Markets.Home('cross_margin')}>
<Link href={ROUTES.Markets.Home(FuturesMarginType.SMART_MARGIN)}>
<div>{t('common.perp-cta')}</div>
</Link>
</NoPositionsText>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { FuturesMarginType } from '@kwenta/sdk/types'
import { formatDollars } from '@kwenta/sdk/utils'
import Wei from '@synthetixio/wei'
import { useMemo, useState } from 'react'
Expand All @@ -6,7 +7,6 @@ import styled from 'styled-components'

import TabButton from 'components/Button/TabButton'
import { TabPanel } from 'components/Tab'
import { FuturesAccountTypes } from 'queries/futures/types'
import { SectionHeader, SectionTitle } from 'sections/futures/mobile'
import { selectBalances } from 'state/balances/selectors'
import { selectFuturesPortfolio, selectActiveSmartPositionsCount } from 'state/futures/selectors'
Expand Down Expand Up @@ -47,7 +47,7 @@ const OpenPositions: React.FC<OpenPositionsProps> = ({ exchangeTokens, exchangeT
label: t('dashboard.overview.positions-tabs.smart-margin'),
badge: smartPositionsCount,
active: activePositionsTab === PositionsTab.SMART_MARGIN,
detail: formatDollars(portfolio.crossMarginFutures),
detail: formatDollars(portfolio.smartMargin),
disabled: false,
onClick: () => setActivePositionsTab(PositionsTab.SMART_MARGIN),
},
Expand All @@ -64,7 +64,7 @@ const OpenPositions: React.FC<OpenPositionsProps> = ({ exchangeTokens, exchangeT
t,
activePositionsTab,
smartPositionsCount,
portfolio.crossMarginFutures,
portfolio.smartMargin,
balances.totalUSDBalance,
exchangeTokenBalances,
setActivePositionsTab,
Expand All @@ -86,7 +86,7 @@ const OpenPositions: React.FC<OpenPositionsProps> = ({ exchangeTokens, exchangeT
</div>

<TabPanel name={PositionsTab.SMART_MARGIN} activeTab={activePositionsTab}>
<FuturesPositionsTable accountType={FuturesAccountTypes.CROSS_MARGIN} />
<FuturesPositionsTable accountType={FuturesMarginType.CROSS_MARGIN} />
</TabPanel>

<TabPanel name={MarketsTab.SPOT} activeTab={activePositionsTab}>
Expand Down
Loading
Loading