modify orders

This commit is contained in:
Adrian Brzeziński 2022-12-15 11:27:08 +01:00
parent c2ababe263
commit 0f10b85ac0
1 changed files with 100 additions and 63 deletions

View File

@ -3,6 +3,8 @@ import {
PerpOrder, PerpOrder,
PerpOrderType, PerpOrderType,
Serum3Market, Serum3Market,
Serum3OrderType,
Serum3SelfTradeBehavior,
Serum3Side, Serum3Side,
} from '@blockworks-foundation/mango-v4' } from '@blockworks-foundation/mango-v4'
import Input from '@components/forms/Input' import Input from '@components/forms/Input'
@ -44,33 +46,50 @@ const OpenOrders = () => {
const { width } = useViewport() const { width } = useViewport()
const showTableView = width ? width > breakpoints.md : false const showTableView = width ? width > breakpoints.md : false
const findSerum3MarketPkInOpenOrders = (o: Order): string | undefined => {
const openOrders = mangoStore.getState().mangoAccount.openOrders
let foundedMarketPk: string | undefined = undefined
for (const [marketPk, orders] of Object.entries(openOrders)) {
for (const order of orders) {
if (order.orderId.eq(o.orderId)) {
foundedMarketPk = marketPk
break
}
}
break
}
return foundedMarketPk
}
const handleCancelSerumOrder = useCallback( const handleCancelSerumOrder = useCallback(
async (o: Order) => { async (o: Order) => {
const client = mangoStore.getState().client const client = mangoStore.getState().client
const group = mangoStore.getState().group const group = mangoStore.getState().group
const mangoAccount = mangoStore.getState().mangoAccount.current const mangoAccount = mangoStore.getState().mangoAccount.current
const selectedMarket = mangoStore.getState().selectedMarket.current
const actions = mangoStore.getState().actions const actions = mangoStore.getState().actions
if (!group || !mangoAccount) return if (!group || !mangoAccount) return
const marketPk = findSerum3MarketPkInOpenOrders(o)
if (!marketPk) return
const market = group.getSerum3MarketByExternalMarket(
new PublicKey(marketPk)
)
setCancelId(o.orderId.toString()) setCancelId(o.orderId.toString())
try { try {
if (selectedMarket instanceof Serum3Market) { const tx = await client.serum3CancelOrder(
const tx = await client.serum3CancelOrder( group,
group, mangoAccount,
mangoAccount, market!.serumMarketExternal,
selectedMarket!.serumMarketExternal, o.side === 'buy' ? Serum3Side.bid : Serum3Side.ask,
o.side === 'buy' ? Serum3Side.bid : Serum3Side.ask, o.orderId
o.orderId )
)
actions.fetchOpenOrders() actions.fetchOpenOrders()
notify({ notify({
type: 'success', type: 'success',
title: 'Transaction successful', title: 'Transaction successful',
txid: tx, txid: tx,
}) })
}
} catch (e: any) { } catch (e: any) {
console.error('Error canceling', e) console.error('Error canceling', e)
notify({ notify({
@ -83,47 +102,9 @@ const OpenOrders = () => {
setCancelId('') setCancelId('')
} }
}, },
[t] [t, openOrders]
) )
const handleCancelPerpOrder = useCallback(
async (o: PerpOrder) => {
const client = mangoStore.getState().client
const group = mangoStore.getState().group
const mangoAccount = mangoStore.getState().mangoAccount.current
const selectedMarket = mangoStore.getState().selectedMarket.current
const actions = mangoStore.getState().actions
if (!group || !mangoAccount) return
setCancelId(o.orderId.toString())
try {
if (selectedMarket instanceof PerpMarket) {
const tx = await client.perpCancelOrder(
group,
mangoAccount,
o.perpMarketIndex,
o.orderId
)
actions.fetchOpenOrders()
notify({
type: 'success',
title: 'Transaction successful',
txid: tx,
})
}
} catch (e: any) {
console.error('Error canceling', e)
notify({
title: t('trade:cancel-order-error'),
description: e.message,
txid: e.txid,
type: 'error',
})
} finally {
setCancelId('')
}
},
[t]
)
const modifyOrder = useCallback( const modifyOrder = useCallback(
async (o: PerpOrder | Order) => { async (o: PerpOrder | Order) => {
const client = mangoStore.getState().client const client = mangoStore.getState().client
@ -134,8 +115,9 @@ const OpenOrders = () => {
const price = modifiedOrderPrice ? Number(modifiedOrderPrice) : o.price const price = modifiedOrderPrice ? Number(modifiedOrderPrice) : o.price
if (!group || !mangoAccount) return if (!group || !mangoAccount) return
try { try {
let tx = ''
if (o instanceof PerpOrder) { if (o instanceof PerpOrder) {
const tx = await client.modifyPerpOrder( tx = await client.modifyPerpOrder(
group, group,
mangoAccount, mangoAccount,
o.perpMarketIndex, o.perpMarketIndex,
@ -149,13 +131,32 @@ const OpenOrders = () => {
undefined, undefined,
undefined undefined
) )
actions.fetchOpenOrders() } else {
notify({ const marketPk = findSerum3MarketPkInOpenOrders(o)
type: 'success', if (!marketPk) return
title: 'Transaction successful', const market = group.getSerum3MarketByExternalMarket(
txid: tx, new PublicKey(marketPk)
}) )
tx = await client.modifySerum3Order(
group,
o.orderId,
mangoAccount,
market.serumMarketExternal,
o.side === 'buy' ? Serum3Side.bid : Serum3Side.ask,
price,
baseSize,
Serum3SelfTradeBehavior.decrementTake,
Serum3OrderType.limit,
Date.now(),
10
)
} }
actions.fetchOpenOrders()
notify({
type: 'success',
title: 'Transaction successful',
txid: tx,
})
} catch (e: any) { } catch (e: any) {
console.error('Error canceling', e) console.error('Error canceling', e)
notify({ notify({
@ -171,6 +172,42 @@ const OpenOrders = () => {
[t, modifiedOrderSize, modifiedOrderPrice] [t, modifiedOrderSize, modifiedOrderPrice]
) )
const handleCancelPerpOrder = useCallback(
async (o: PerpOrder) => {
const client = mangoStore.getState().client
const group = mangoStore.getState().group
const mangoAccount = mangoStore.getState().mangoAccount.current
const actions = mangoStore.getState().actions
if (!group || !mangoAccount) return
setCancelId(o.orderId.toString())
try {
const tx = await client.perpCancelOrder(
group,
mangoAccount,
o.perpMarketIndex,
o.orderId
)
actions.fetchOpenOrders()
notify({
type: 'success',
title: 'Transaction successful',
txid: tx,
})
} catch (e: any) {
console.error('Error canceling', e)
notify({
title: t('trade:cancel-order-error'),
description: e.message,
txid: e.txid,
type: 'error',
})
} finally {
setCancelId('')
}
},
[t]
)
const showEditOrderForm = (order: Order | PerpOrder) => { const showEditOrderForm = (order: Order | PerpOrder) => {
setModifyOrderId(order.orderId.toString()) setModifyOrderId(order.orderId.toString())
setModifiedOrderSize(order.size.toString()) setModifiedOrderSize(order.size.toString())