Skip to content

Commit

Permalink
fix: baseLimit check + trade form selector
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelxuwu authored May 25, 2023
1 parent 3d92791 commit 8678817
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,14 @@ const TradeFormCollateralSection = ({

const max = getSoftMaxCollateral(trade, collateral)
const min = getSoftMinCollateral(collateral)

const liqPrice = collateral.liquidationPrice
const spotPrice = trade.market().spotPrice
const liqDistToSpot = liqPrice ? getDistToLiqPrice(spotPrice, liqPrice) : null
const absLiqDistToSpot = liqDistToSpot ? Math.abs(liqDistToSpot) : null

const sliderDist = max.sub(min)
const step = fromBigNumber(sliderDist) / NUM_STEPS
const isRange = !min.gte(max)
const isRange = max.gt(min)

// HACK: When min = max collateral i.e. there is no range, lock collateral to max
useEffect(() => {
Expand Down Expand Up @@ -102,7 +101,7 @@ const TradeFormCollateralSection = ({

return (
<CardSection {...styleProps}>
{isCall && !position ? (
{isCall && trade.market().isBaseCollateralEnabled && !position ? (
<RowItem
mb={5}
label="Sell With"
Expand Down
3 changes: 1 addition & 2 deletions app/src/containers/trade/TradeForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import { SLIPPAGE } from '@/app/constants/contracts'
import { TRADE_CARD_MIN_HEIGHT } from '@/app/constants/layout'
import TradeFormSizeInput from '@/app/containers/trade/TradeForm/TradeFormSizeInput'
import useAccountBalances from '@/app/hooks/account/useAccountBalances'
import useNetwork from '@/app/hooks/account/useNetwork'
import useWalletAccount from '@/app/hooks/account/useWalletAccount'
import withSuspense from '@/app/hooks/data/withSuspense'
import useTradeSync from '@/app/hooks/market/useTradeSync'
Expand All @@ -40,7 +39,6 @@ type Props = {
const TradeForm = withSuspense(
({ isBuy, option, position, onTrade, hideTitle }: Props) => {
const account = useWalletAccount()
const network = useNetwork()
const market = option.market()

// TODO: @dappbeast parallelize requests
Expand Down Expand Up @@ -144,6 +142,7 @@ const TradeForm = withSuspense(
{!isLong && trade.collateral ? (
<>
<TradeFormCollateralSection
key={fromBigNumber(collateralAmount)}
trade={trade}
collateral={trade.collateral}
collateralAmount={collateralAmount}
Expand Down
15 changes: 10 additions & 5 deletions sdk/src/market/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ export class Market {
spotPrice: BigNumber
contractAddresses: MarketContractAddresses
params: MarketParameters
isBaseCollateralEnabled: boolean

constructor(
lyra: Lyra,
Expand All @@ -216,7 +217,8 @@ export class Market {
block: Block,
hedgerView?: PoolHedgerView,
adapterView?: ExchangeAdapterView,
poolHedgerParams?: PoolHedgerParams
poolHedgerParams?: PoolHedgerParams,
baseLimit?: BigNumber | null
) {
this.lyra = lyra
this.block = block
Expand All @@ -233,7 +235,7 @@ export class Market {
poolHedgerParams
)
this.address = fields.address

this.isBaseCollateralEnabled = !baseLimit || baseLimit.gt(0)
this.isPaused = fields.isPaused
this.spotPrice = fields.spotPrice
this.quoteToken = fields.quoteToken
Expand Down Expand Up @@ -276,7 +278,8 @@ export class Market {
tokenPrice: BigNumber,
hedgerView?: PoolHedgerView,
adapterView?: ExchangeAdapterView,
poolHedgerParams?: PoolHedgerParams
poolHedgerParams?: PoolHedgerParams,
baseLimit?: BigNumber | null
) {
const address = marketView.marketAddresses.optionMarket
const isPaused = marketView.isPaused ?? isGlobalPaused
Expand Down Expand Up @@ -349,6 +352,7 @@ export class Market {
(marketView as AvalonOptionMarketViewer.MarketViewWithBoardsStructOutput).marketParameters.poolHedgerParams,
hedgerView: hedgerView ?? null,
adapterView: adapterView ?? null,
baseLimit,
}

if (version === Version.Avalon) {
Expand Down Expand Up @@ -478,7 +482,7 @@ export class Market {
lyra.provider.getBlock('latest'),
])
const markets = marketViews.map(
({ marketView, hedgerView, adapterView, poolHedgerParams, tokenPrice }) =>
({ marketView, hedgerView, adapterView, poolHedgerParams, tokenPrice, baseLimit }) =>
new Market(
lyra,
marketView,
Expand All @@ -488,7 +492,8 @@ export class Market {
block,
hedgerView,
adapterView,
poolHedgerParams
poolHedgerParams,
baseLimit
)
)
return markets
Expand Down
2 changes: 2 additions & 0 deletions sdk/src/utils/fetchNewportMarketViews.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ export default async function fetchNewportMarketViews(lyra: Lyra): Promise<{
adapterView: GMXAdapter.GMXAdapterStateStructOutput
poolHedgerParams: PoolHedgerParams
tokenPrice: BigNumber
baseLimit: null
}[]
isGlobalPaused: boolean
owner: string
Expand Down Expand Up @@ -225,6 +226,7 @@ export default async function fetchNewportMarketViews(lyra: Lyra): Promise<{
adapterView: adapterViews[i],
poolHedgerParams: !isTestnet(lyra) ? poolHedgerParams[i] : TESTNET_POOL_HEDGER_PARAMS,
tokenPrice: tokenPrices[i],
baseLimit: null,
}
})

Expand Down
21 changes: 21 additions & 0 deletions sdk/src/utils/fetchNewportOptimismMarketViews.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { PoolHedgerParams } from '../admin'
import { MAX_BN, ZERO_BN } from '../constants/bn'
import { LyraContractId, LyraMarketContractId } from '../constants/contracts'
import { LyraContractMap, LyraMarketContractMap } from '../constants/mappings'
import { NewportOptionMarket } from '../contracts/newport/optimism/typechain'
import { OptionMarketViewer } from '../contracts/newport/typechain/NewportOptionMarketViewer'
import { SNXPerpsV2PoolHedger } from '../contracts/newport/typechain/NewportSNXPerpsV2PoolHedger'
import { SNXPerpV2Adapter } from '../contracts/newport/typechain/NewportSNXPerpV2Adapter'
Expand Down Expand Up @@ -31,6 +32,7 @@ type RequestGetTokenPrice = MulticallRequest<
LyraMarketContractMap<Version.Newport, LyraMarketContractId.LiquidityPool>,
'getTokenPrice'
>
type RequestGetBaseLimit = MulticallRequest<NewportOptionMarket, 'baseLimit'>

const TESTNET_POOL_HEDGER_PARAMS: PoolHedgerParams = {
interactionDelay: ZERO_BN,
Expand Down Expand Up @@ -85,6 +87,7 @@ export default async function fetchNewportOptimismMarketViews(lyra: Lyra): Promi
adapterView: SNXPerpV2Adapter.MarketAdapterStateStructOutput
poolHedgerParams: PoolHedgerParams
tokenPrice: BigNumber
baseLimit: BigNumber
}[]
isGlobalPaused: boolean
owner: string
Expand Down Expand Up @@ -155,6 +158,21 @@ export default async function fetchNewportOptimismMarketViews(lyra: Lyra): Promi
}
})
: []

const baseLimitRequests: RequestGetBaseLimit[] = allMarketAddresses.map(marketAddresses => {
const optionMarket = getLyraMarketContract(
lyra,
marketAddresses,
Version.Newport,
LyraMarketContractId.OptionMarket
) as NewportOptionMarket
return {
contract: optionMarket,
function: 'baseLimit',
args: [],
}
})

const {
returnData: [owner, marketViewsRes, ...hedgerAndAdapterViews],
blockNumber,
Expand All @@ -177,6 +195,8 @@ export default async function fetchNewportOptimismMarketViews(lyra: Lyra): Promi
...tokenPriceRequests,
])

const { returnData: baseLimits } = await multicall<RequestGetBaseLimit[]>(lyra, baseLimitRequests)

const hedgerViews: SNXPerpsV2PoolHedger.HedgerStateStructOutput[] = hedgerAndAdapterViews.slice(
0,
hedgerRequests.length
Expand Down Expand Up @@ -204,6 +224,7 @@ export default async function fetchNewportOptimismMarketViews(lyra: Lyra): Promi
adapterView: adapterViews[i],
poolHedgerParams: !isTestnet(lyra) ? poolHedgerParams[i] : TESTNET_POOL_HEDGER_PARAMS,
tokenPrice: tokenPrices[i],
baseLimit: baseLimits[i],
}
})

Expand Down

0 comments on commit 8678817

Please sign in to comment.