wip: perp position settle funds

This commit is contained in:
tjs 2022-12-08 00:06:06 -05:00
parent 3dd54d26fe
commit 20af1810b0
4 changed files with 102 additions and 38 deletions

View File

@ -23,7 +23,7 @@ function Modal({
className="relative z-50 overflow-y-auto"
>
<div
className={`fixed inset-0 backdrop-blur-sm backdrop-brightness-75 ${
className={`fixed inset-0 backdrop-brightness-75 ${
disableOutsideClose ? 'pointer-events-none' : ''
}`}
aria-hidden="true"

View File

@ -138,9 +138,9 @@ const PerpPositions = () => {
<Td className="text-right">
<div>
$
{numberFormat.format(
{/* {numberFormat.format(
position.perpSpotTransfers.toNumber()
)}
)} */}
</div>
</Td>
</TrBody>

View File

@ -12,7 +12,7 @@ import { breakpoints } from 'utils/theme'
import MarketLogos from './MarketLogos'
import { Table, Td, Th, TrBody, TrHead } from '@components/shared/TableElements'
import useMangoGroup from 'hooks/useMangoGroup'
import { PerpPosition } from '@blockworks-foundation/mango-v4'
import { PerpMarket, PerpPosition } from '@blockworks-foundation/mango-v4'
import { useWallet } from '@solana/wallet-adapter-react'
const UnsettledTrades = ({
@ -29,7 +29,7 @@ const UnsettledTrades = ({
const { width } = useViewport()
const showTableView = width ? width > breakpoints.md : false
const handleSettleFunds = useCallback(async (mktAddress: string) => {
const handleSettleSerumFunds = useCallback(async (mktAddress: string) => {
const client = mangoStore.getState().client
const group = mangoStore.getState().group
const mangoAccount = mangoStore.getState().mangoAccount.current
@ -37,6 +37,7 @@ const UnsettledTrades = ({
if (!group || !mangoAccount) return
setSettleMktAddress(mktAddress)
try {
const txid = await client.serum3SettleFunds(
group,
@ -63,6 +64,75 @@ const UnsettledTrades = ({
}
}, [])
const handleSettlePerpFunds = useCallback(async (market: PerpMarket) => {
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
setSettleMktAddress(market.publicKey.toString())
try {
const mangoAccounts = await client.getAllMangoAccounts(group)
const perpPosition = mangoAccount.getPerpPosition(market.perpMarketIndex)
const mangoAccountPnl = perpPosition?.getEquityUi(market)
if (mangoAccountPnl === undefined)
throw new Error('Unable to get account P&L')
const sign = Math.sign(mangoAccountPnl)
const filteredAccounts = mangoAccounts
.map((m) => ({
mangoAccount: m,
pnl:
m?.getPerpPosition(market.perpMarketIndex)?.getEquityUi(market) ||
0,
}))
.sort((a, b) => sign * (a.pnl - b.pnl))
console.log(
'pnl',
filteredAccounts.map((m) => [
m.mangoAccount.publicKey.toString(),
m.pnl,
])
)
const profitableAccount =
mangoAccountPnl >= 0 ? mangoAccount : filteredAccounts[0].mangoAccount
const unprofitableAccount =
mangoAccountPnl < 0 ? mangoAccount : filteredAccounts[0].mangoAccount
// const profitableAccount = mangoAccount
// const unprofitableAccount =
// filteredAccounts[filteredAccounts.length - 1].mangoAccount
// console.log('unprofitableAccount', unprofitableAccount)
const txid = await client.perpSettlePnl(
group,
profitableAccount,
unprofitableAccount,
mangoAccount,
market.perpMarketIndex
)
actions.reloadMangoAccount()
notify({
type: 'success',
title: 'Successfully settled P&L',
txid,
})
} catch (e: any) {
notify({
type: 'error',
title: 'Settle P&L error',
description: e?.message,
txid: e?.txid,
})
console.error('Settle P&L error:', e)
} finally {
setSettleMktAddress('')
}
}, [])
if (!group) return null
return connected ? (
@ -110,7 +180,7 @@ const UnsettledTrades = ({
<div className="flex justify-end">
<Tooltip content={t('trade:settle-funds')}>
<IconButton
onClick={() => handleSettleFunds(mktAddress)}
onClick={() => handleSettleSerumFunds(mktAddress)}
size="small"
>
{settleMktAddress === mktAddress ? (
@ -141,15 +211,13 @@ const UnsettledTrades = ({
<span></span>
</Td>
<Td className="text-right font-mono">
{position.getUnsettledFunding(market).toNumber()}
{position.getEquityUi(market)}
</Td>
<Td>
<div className="flex justify-end">
<Tooltip content={t('trade:settle-funds')}>
<IconButton
onClick={() =>
handleSettleFunds(market.publicKey.toString())
}
onClick={() => handleSettlePerpFunds(market)}
size="small"
>
{settleMktAddress === market.publicKey.toString() ? (
@ -201,7 +269,9 @@ const UnsettledTrades = ({
</span>
</span>
) : null}
<IconButton onClick={() => handleSettleFunds(mktAddress)}>
<IconButton
onClick={() => handleSettleSerumFunds(mktAddress)}
>
{settleMktAddress === mktAddress ? (
<Loading className="h-4 w-4" />
) : (

View File

@ -395,9 +395,9 @@
integrity sha512-Rk4SkJFaXZiznFyC/t77Q0NKS4FL7TLJJsVG2V2oiEq3kJVeTdxysEe/yRWSpnWMe808XRDJ+VFh5pt/FN5plw==
"@noble/hashes@^1.1.2":
version "1.1.3"
resolved "https://registry.yarnpkg.com/@noble/hashes/-/hashes-1.1.3.tgz#360afc77610e0a61f3417e497dcf36862e4f8111"
integrity sha512-CE0FCR57H2acVI5UOzIGSSIYxZ6v/HOhDR0Ro9VLyhnzLwx0o8W1mmgaqlEUx4049qJDlIBRztv5k+MM8vbO3A==
version "1.1.4"
resolved "https://registry.yarnpkg.com/@noble/hashes/-/hashes-1.1.4.tgz#2611ebf5764c1bf754da7c7794de4fb30512336d"
integrity sha512-+PYsVPrTSqtVjatKt2A/Proukn2Yrz61OBThOCKErc5w2/r1Fh37vbDv0Eah7pyNltrmacjwTvdw3JoR+WE4TA==
"@noble/secp256k1@^1.6.3":
version "1.7.0"
@ -1098,9 +1098,9 @@
"@wallet-standard/base" "^1.0.0"
"@solana/web3.js@^1.17.0", "@solana/web3.js@^1.21.0", "@solana/web3.js@^1.32.0", "@solana/web3.js@^1.36.0", "@solana/web3.js@^1.63.1":
version "1.69.0"
resolved "https://registry.yarnpkg.com/@solana/web3.js/-/web3.js-1.69.0.tgz#1756b1a26087172291c0b5163d3b44d24eef8aa7"
integrity sha512-iU2Q0IG25RITsxBkY1Vkk74LffRokViEcSblz4CGxyt+/V7xSkC2DNM0n0rB3aY/9+FvMiz4l5wHnD9UC4Ac/w==
version "1.70.0"
resolved "https://registry.yarnpkg.com/@solana/web3.js/-/web3.js-1.70.0.tgz#14ad207f431861397db85921aad8df4e8374e7c8"
integrity sha512-HwdI9LaHaszfpzgxJI44iP68mJWUeqK1TeSheKQsGkH5zlVyGWGmim50MyDWu2vXiuL8Akf2xEMSrDYyLordgg==
dependencies:
"@babel/runtime" "^7.12.5"
"@noble/ed25519" "^1.7.0"
@ -1558,9 +1558,9 @@
integrity sha512-evMDG1bC4rgQg4ku9tKpuMh5iBNEwNa3tf9zRHdP1qlv+1WUg44xat4IxCE14gIpZRGUUWAx2VhItCZc25NfMA==
"@types/node@*":
version "18.11.10"
resolved "https://registry.yarnpkg.com/@types/node/-/node-18.11.10.tgz#4c64759f3c2343b7e6c4b9caf761c7a3a05cee34"
integrity sha512-juG3RWMBOqcOuXC643OAdSA525V44cVgGV6dUDuiFtss+8Fk5x1hI93Rsld43VeJVIeqlP9I7Fn9/qaVqoEAuQ==
version "18.11.11"
resolved "https://registry.yarnpkg.com/@types/node/-/node-18.11.11.tgz#1d455ac0211549a8409d3cdb371cd55cc971e8dc"
integrity sha512-KJ021B1nlQUBLopzZmPBVuGU9un7WJd/W4ya7Ih02B4Uwky5Nja0yGYav2EfYIk0RR2Q9oVhf60S2XR1BCWJ2g==
"@types/node@17.0.23":
version "17.0.23"
@ -2268,9 +2268,9 @@ bigint-buffer@^1.1.5:
bindings "^1.3.0"
bignumber.js@^9.0.1:
version "9.1.0"
resolved "https://registry.yarnpkg.com/bignumber.js/-/bignumber.js-9.1.0.tgz#8d340146107fe3a6cb8d40699643c302e8773b62"
integrity sha512-4LwHK4nfDOraBCtst+wOWIHbu1vhvAPJK8g8nROd4iuc3PSEjWif/qwbkh8jwCJz6yDBvtU4KPynETgrfh7y3A==
version "9.1.1"
resolved "https://registry.yarnpkg.com/bignumber.js/-/bignumber.js-9.1.1.tgz#c4df7dc496bd849d4c9464344c1aa74228b4dac6"
integrity sha512-pHm4LsMJ6lzgNGVfZHjMoO8sdoRhOzOH4MLmY65Jg70bpxCKu5iOHNJyfF6OyvYw7t8Fpf35RuzUyqnQsj8Vig==
bignumber.js@^9.0.2:
version "9.0.2"
@ -2861,10 +2861,10 @@ dayjs@^1.11.3:
resolved "https://registry.yarnpkg.com/dayjs/-/dayjs-1.11.3.tgz#4754eb694a624057b9ad2224b67b15d552589258"
integrity sha512-xxwlswWOlGhzgQ4TKzASQkUhqERI3egRNqgV4ScR8wlANA/A9tZ7miXa44vTTKEq5l7vWoL5G57bG3zA+Kow0A==
debug@4.3.2:
version "4.3.2"
resolved "https://registry.yarnpkg.com/debug/-/debug-4.3.2.tgz#f0a49c18ac8779e31d4a0c6029dfb76873c7428b"
integrity sha512-mOp8wKcvj7XxC78zLgw/ZA+6TSgkoE2C/ienthhRD298T7UNwAg9diBpLRxC0mOezLl4B0xV7M0cCO6P/O0Xhw==
debug@4.3.4, debug@^4.1.1, debug@^4.3.2, debug@^4.3.4, debug@~4.3.1, debug@~4.3.2:
version "4.3.4"
resolved "https://registry.yarnpkg.com/debug/-/debug-4.3.4.tgz#1319f6579357f2338d3337d2cdd4914bb5dcc865"
integrity sha512-PRWFHuSU3eDtQJPvnNY7Jcket1j0t5OuOsFzPPzsekD52Zl8qUfFIPEiswXqIvHWGVHOgX+7G/vCNNhehwxfkQ==
dependencies:
ms "2.1.2"
@ -2882,13 +2882,6 @@ debug@^3.2.7:
dependencies:
ms "^2.1.1"
debug@^4.1.1, debug@^4.3.2, debug@^4.3.4, debug@~4.3.1, debug@~4.3.2:
version "4.3.4"
resolved "https://registry.yarnpkg.com/debug/-/debug-4.3.4.tgz#1319f6579357f2338d3337d2cdd4914bb5dcc865"
integrity sha512-PRWFHuSU3eDtQJPvnNY7Jcket1j0t5OuOsFzPPzsekD52Zl8qUfFIPEiswXqIvHWGVHOgX+7G/vCNNhehwxfkQ==
dependencies:
ms "2.1.2"
decamelize@^1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/decamelize/-/decamelize-1.2.0.tgz#f6534d15148269b20352e7bee26f501f9a191290"
@ -5801,13 +5794,14 @@ split@0.3:
through "2"
start-server-and-test@^1.14.0:
version "1.14.0"
resolved "https://registry.yarnpkg.com/start-server-and-test/-/start-server-and-test-1.14.0.tgz#c57f04f73eac15dd51733b551d775b40837fdde3"
integrity sha512-on5ELuxO2K0t8EmNj9MtVlFqwBMxfWOhu4U7uZD1xccVpFlOQKR93CSe0u98iQzfNxRyaNTb/CdadbNllplTsw==
version "1.15.1"
resolved "https://registry.yarnpkg.com/start-server-and-test/-/start-server-and-test-1.15.1.tgz#8e28e48a31884b7c7b19950502e92161eec6293b"
integrity sha512-ixhMbUAmym+7k3kqhjhTt6/0eUwE3dbNZPWgcfEUJ5uStaHbT0m8VB+049V5V9X0ueEyxAkwnkkR2SEhCKst5g==
dependencies:
arg "^5.0.2"
bluebird "3.7.2"
check-more-types "2.24.0"
debug "4.3.2"
debug "4.3.4"
execa "5.1.1"
lazy-ass "1.6.0"
ps-tree "1.2.0"