merge and default slippage to 2.5%
This commit is contained in:
commit
2ee67b4693
|
@ -25,9 +25,10 @@ const MarketCloseModal: FunctionComponent<MarketCloseModalProps> = ({
|
||||||
const [submitting, setSubmitting] = useState(false)
|
const [submitting, setSubmitting] = useState(false)
|
||||||
const actions = useMangoStore((s) => s.actions)
|
const actions = useMangoStore((s) => s.actions)
|
||||||
const mangoClient = useMangoStore((s) => s.connection.client)
|
const mangoClient = useMangoStore((s) => s.connection.client)
|
||||||
const orderBookRef = useRef(useMangoStore.getState().selectedMarket.orderBook)
|
|
||||||
const config = useMangoStore.getState().selectedMarket.config
|
const config = useMangoStore.getState().selectedMarket.config
|
||||||
|
|
||||||
|
const orderBookRef = useRef(useMangoStore.getState().selectedMarket.orderBook)
|
||||||
|
const orderbook = orderBookRef.current
|
||||||
useEffect(
|
useEffect(
|
||||||
() =>
|
() =>
|
||||||
useMangoStore.subscribe(
|
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() {
|
async function handleMarketClose() {
|
||||||
const mangoAccount = useMangoStore.getState().selectedMangoAccount.current
|
const mangoAccount = useMangoStore.getState().selectedMangoAccount.current
|
||||||
const mangoGroup = useMangoStore.getState().selectedMangoGroup.current
|
const mangoGroup = useMangoStore.getState().selectedMangoGroup.current
|
||||||
|
@ -54,11 +71,13 @@ const MarketCloseModal: FunctionComponent<MarketCloseModalProps> = ({
|
||||||
try {
|
try {
|
||||||
const perpAccount = mangoAccount.perpAccounts[marketIndex]
|
const perpAccount = mangoAccount.perpAccounts[marketIndex]
|
||||||
const side = perpAccount.basePosition.gt(ZERO_BN) ? 'sell' : 'buy'
|
const side = perpAccount.basePosition.gt(ZERO_BN) ? 'sell' : 'buy'
|
||||||
const price = 1
|
|
||||||
// send a large size to ensure we are reducing the entire position
|
// send a large size to ensure we are reducing the entire position
|
||||||
const size =
|
const size =
|
||||||
Math.abs(market.baseLotsToNumber(perpAccount.basePosition)) * 2
|
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(
|
const txid = await mangoClient.placePerpOrder(
|
||||||
mangoGroup,
|
mangoGroup,
|
||||||
mangoAccount,
|
mangoAccount,
|
||||||
|
@ -66,9 +85,9 @@ const MarketCloseModal: FunctionComponent<MarketCloseModalProps> = ({
|
||||||
market,
|
market,
|
||||||
wallet,
|
wallet,
|
||||||
side,
|
side,
|
||||||
price,
|
referencePrice * (1 + (side === 'buy' ? 1 : -1) * maxSlippage),
|
||||||
size,
|
size,
|
||||||
'market',
|
'ioc',
|
||||||
0, // client order id
|
0, // client order id
|
||||||
side === 'buy' ? askInfo : bidInfo,
|
side === 'buy' ? askInfo : bidInfo,
|
||||||
true // reduce only
|
true // reduce only
|
||||||
|
|
|
@ -100,6 +100,10 @@ export default function AdvancedTradeForm({
|
||||||
|
|
||||||
const isTriggerOrder = TRIGGER_ORDER_TYPES.includes(tradeType)
|
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 [postOnly, setPostOnly] = useState(false)
|
||||||
const [ioc, setIoc] = useState(false)
|
const [ioc, setIoc] = useState(false)
|
||||||
const [submitting, setSubmitting] = useState(false)
|
const [submitting, setSubmitting] = useState(false)
|
||||||
|
@ -108,7 +112,7 @@ export default function AdvancedTradeForm({
|
||||||
const orderbook = orderBookRef.current
|
const orderbook = orderBookRef.current
|
||||||
const [maxSlippage, setMaxSlippage] = useLocalStorageStringState(
|
const [maxSlippage, setMaxSlippage] = useLocalStorageStringState(
|
||||||
MAX_SLIPPAGE_KEY,
|
MAX_SLIPPAGE_KEY,
|
||||||
'0.01'
|
'0.025'
|
||||||
)
|
)
|
||||||
const [maxSlippagePercentage, setMaxSlippagePercentage] = useState(
|
const [maxSlippagePercentage, setMaxSlippagePercentage] = useState(
|
||||||
parseFloat(maxSlippage) * 100
|
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) => {
|
const postOnChange = (checked) => {
|
||||||
if (checked) {
|
if (checked) {
|
||||||
setIoc(false)
|
setIoc(false)
|
||||||
|
setPostOnlySlide(false)
|
||||||
}
|
}
|
||||||
setPostOnly(checked)
|
setPostOnly(checked)
|
||||||
}
|
}
|
||||||
const iocOnChange = (checked) => {
|
const iocOnChange = (checked) => {
|
||||||
if (checked) {
|
if (checked) {
|
||||||
setPostOnly(false)
|
setPostOnly(false)
|
||||||
|
setPostOnlySlide(false)
|
||||||
}
|
}
|
||||||
setIoc(checked)
|
setIoc(checked)
|
||||||
}
|
}
|
||||||
|
@ -436,6 +452,11 @@ export default function AdvancedTradeForm({
|
||||||
'close-position'
|
'close-position'
|
||||||
).toLowerCase()}`
|
).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 priceImpact
|
||||||
let estimatedPrice = price
|
let estimatedPrice = price
|
||||||
if (tradeType === 'Market' && baseSize > 0) {
|
if (tradeType === 'Market' && baseSize > 0) {
|
||||||
|
@ -474,11 +495,6 @@ export default function AdvancedTradeForm({
|
||||||
: baseSize
|
: baseSize
|
||||||
estimatedPrice = estimateMarketPrice(orderbook, estimatedSize || 0, side)
|
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 =
|
const slippageAbs =
|
||||||
estimatedSize > 0 ? Math.abs(estimatedPrice - referencePrice) : 0
|
estimatedSize > 0 ? Math.abs(estimatedPrice - referencePrice) : 0
|
||||||
const slippageRel = slippageAbs / referencePrice
|
const slippageRel = slippageAbs / referencePrice
|
||||||
|
|
|
@ -21,12 +21,16 @@ export default function useMarkPrice() {
|
||||||
const ba = orderbook?.asks?.length > 0 && Number(orderbook.asks[0][0])
|
const ba = orderbook?.asks?.length > 0 && Number(orderbook.asks[0][0])
|
||||||
const last = trades && trades.length > 0 && trades[0].price
|
const last = trades && trades.length > 0 && trades[0].price
|
||||||
|
|
||||||
const newMarkPrice =
|
const priceElements = [bb, ba, last].filter((e) => e).sort((a, b) => a - b)
|
||||||
bb && ba
|
const newMarkPrice = priceElements
|
||||||
? last
|
? priceElements[Math.floor(priceElements.length / 2)]
|
||||||
? [bb, ba, last].sort((a, b) => a - b)[1]
|
: null
|
||||||
: (bb + ba) / 2
|
// const newMarkPrice =
|
||||||
: null
|
// bb && ba
|
||||||
|
// ? last
|
||||||
|
// ? [bb, ba, last].sort((a, b) => a - b)[1]
|
||||||
|
// : (bb + ba) / 2
|
||||||
|
// : null
|
||||||
if (newMarkPrice !== markPrice) {
|
if (newMarkPrice !== markPrice) {
|
||||||
setMangoStore((state) => {
|
setMangoStore((state) => {
|
||||||
state.selectedMarket.markPrice = newMarkPrice?.toFixed(
|
state.selectedMarket.markPrice = newMarkPrice?.toFixed(
|
||||||
|
|
Loading…
Reference in New Issue