merge and default slippage to 2.5%

This commit is contained in:
saml33 2021-12-03 21:39:10 +11:00
commit 2ee67b4693
3 changed files with 55 additions and 16 deletions

View File

@ -25,9 +25,10 @@ const MarketCloseModal: FunctionComponent<MarketCloseModalProps> = ({
const [submitting, setSubmitting] = useState(false)
const actions = useMangoStore((s) => s.actions)
const mangoClient = useMangoStore((s) => s.connection.client)
const orderBookRef = useRef(useMangoStore.getState().selectedMarket.orderBook)
const config = useMangoStore.getState().selectedMarket.config
const orderBookRef = useRef(useMangoStore.getState().selectedMarket.orderBook)
const orderbook = orderBookRef.current
useEffect(
() =>
useMangoStore.subscribe(
@ -38,6 +39,22 @@ const MarketCloseModal: FunctionComponent<MarketCloseModalProps> = ({
[]
)
const markPriceRef = useRef(useMangoStore.getState().selectedMarket.markPrice)
const markPrice = markPriceRef.current
useEffect(
() =>
useMangoStore.subscribe(
(markPrice) => (markPriceRef.current = markPrice as number),
(state) => state.selectedMarket.markPrice
),
[]
)
// The reference price is the book mid if book is double sided; else mark price
const bb = orderbook?.bids?.length > 0 && Number(orderbook.bids[0][0])
const ba = orderbook?.asks?.length > 0 && Number(orderbook.asks[0][0])
const referencePrice = bb && ba ? (bb + ba) / 2 : markPrice
async function handleMarketClose() {
const mangoAccount = useMangoStore.getState().selectedMangoAccount.current
const mangoGroup = useMangoStore.getState().selectedMangoGroup.current
@ -54,11 +71,13 @@ const MarketCloseModal: FunctionComponent<MarketCloseModalProps> = ({
try {
const perpAccount = mangoAccount.perpAccounts[marketIndex]
const side = perpAccount.basePosition.gt(ZERO_BN) ? 'sell' : 'buy'
const price = 1
// send a large size to ensure we are reducing the entire position
const size =
Math.abs(market.baseLotsToNumber(perpAccount.basePosition)) * 2
// hard coded for now; market orders are very dangerous and fault prone
const maxSlippage: number | undefined = 0.025
const txid = await mangoClient.placePerpOrder(
mangoGroup,
mangoAccount,
@ -66,9 +85,9 @@ const MarketCloseModal: FunctionComponent<MarketCloseModalProps> = ({
market,
wallet,
side,
price,
referencePrice * (1 + (side === 'buy' ? 1 : -1) * maxSlippage),
size,
'market',
'ioc',
0, // client order id
side === 'buy' ? askInfo : bidInfo,
true // reduce only

View File

@ -100,6 +100,10 @@ export default function AdvancedTradeForm({
const isTriggerOrder = TRIGGER_ORDER_TYPES.includes(tradeType)
// TODO saml - create a tick box on the UI; Only available on perps
// eslint-disable-next-line
const [postOnlySlide, setPostOnlySlide] = useState(false)
const [postOnly, setPostOnly] = useState(false)
const [ioc, setIoc] = useState(false)
const [submitting, setSubmitting] = useState(false)
@ -108,7 +112,7 @@ export default function AdvancedTradeForm({
const orderbook = orderBookRef.current
const [maxSlippage, setMaxSlippage] = useLocalStorageStringState(
MAX_SLIPPAGE_KEY,
'0.01'
'0.025'
)
const [maxSlippagePercentage, setMaxSlippagePercentage] = useState(
parseFloat(maxSlippage) * 100
@ -368,15 +372,27 @@ export default function AdvancedTradeForm({
}
}
// TODO saml - use
// eslint-disable-next-line
const postOnlySlideOnChange = (checked) => {
if (checked) {
setIoc(false)
setPostOnly(false)
}
setPostOnlySlide(checked)
}
const postOnChange = (checked) => {
if (checked) {
setIoc(false)
setPostOnlySlide(false)
}
setPostOnly(checked)
}
const iocOnChange = (checked) => {
if (checked) {
setPostOnly(false)
setPostOnlySlide(false)
}
setIoc(checked)
}
@ -436,6 +452,11 @@ export default function AdvancedTradeForm({
'close-position'
).toLowerCase()}`
// The reference price is the book mid if book is double sided; else mark price
const bb = orderbook?.bids?.length > 0 && Number(orderbook.bids[0][0])
const ba = orderbook?.asks?.length > 0 && Number(orderbook.asks[0][0])
const referencePrice = bb && ba ? (bb + ba) / 2 : markPrice
let priceImpact
let estimatedPrice = price
if (tradeType === 'Market' && baseSize > 0) {
@ -474,11 +495,6 @@ export default function AdvancedTradeForm({
: baseSize
estimatedPrice = estimateMarketPrice(orderbook, estimatedSize || 0, side)
// The reference price is the book mid if book is double sided; else mark price
const bb = orderbook?.bids?.length > 0 && Number(orderbook.bids[0][0])
const ba = orderbook?.asks?.length > 0 && Number(orderbook.asks[0][0])
const referencePrice = bb && ba ? (bb + ba) / 2 : markPrice
const slippageAbs =
estimatedSize > 0 ? Math.abs(estimatedPrice - referencePrice) : 0
const slippageRel = slippageAbs / referencePrice

View File

@ -21,12 +21,16 @@ export default function useMarkPrice() {
const ba = orderbook?.asks?.length > 0 && Number(orderbook.asks[0][0])
const last = trades && trades.length > 0 && trades[0].price
const newMarkPrice =
bb && ba
? last
? [bb, ba, last].sort((a, b) => a - b)[1]
: (bb + ba) / 2
: null
const priceElements = [bb, ba, last].filter((e) => e).sort((a, b) => a - b)
const newMarkPrice = priceElements
? priceElements[Math.floor(priceElements.length / 2)]
: null
// const newMarkPrice =
// bb && ba
// ? last
// ? [bb, ba, last].sort((a, b) => a - b)[1]
// : (bb + ba) / 2
// : null
if (newMarkPrice !== markPrice) {
setMangoStore((state) => {
state.selectedMarket.markPrice = newMarkPrice?.toFixed(