fix slippage + jlp withdraw, change exactout to exactIn

This commit is contained in:
Adrian Brzeziński 2024-02-28 02:40:37 +01:00
parent a954da011c
commit c1b285f50d
2 changed files with 22 additions and 14 deletions

View File

@ -38,6 +38,7 @@ import Decimal from 'decimal.js'
import { Disclosure } from '@headlessui/react'
import { sleep } from 'utils'
import useIpAddress from 'hooks/useIpAddress'
import { AnchorProvider } from '@project-serum/anchor'
const set = mangoStore.getState().set
@ -209,8 +210,11 @@ function UnstakeForm({ token: selectedToken }: UnstakeFormProps) {
type: 'success',
txid: tx,
})
await sleep(500)
await actions.fetchMangoAccounts(mangoAccount.owner)
await sleep(100)
await actions.fetchMangoAccounts(
(client.program.provider as AnchorProvider).wallet.publicKey,
)
await actions.reloadMangoAccount()
await actions.fetchWalletTokens(publicKey)
mangoAccount = mangoStore.getState().mangoAccount.current
notify({
@ -233,8 +237,11 @@ function UnstakeForm({ token: selectedToken }: UnstakeFormProps) {
})
setSubmitting(false)
setInputAmount('')
await sleep(500)
await actions.fetchMangoAccounts(mangoAccount.owner)
await sleep(100)
await actions.fetchMangoAccounts(
(client.program.provider as AnchorProvider).wallet.publicKey,
)
await actions.reloadMangoAccount()
await actions.fetchWalletTokens(publicKey)
} catch (e) {
console.error('Error withdrawing:', e)

View File

@ -59,7 +59,7 @@ export const withdrawAndClose = async (
const withdrawHealthRemainingAccounts: PublicKey[] =
client.buildHealthRemainingAccounts(group, [mangoAccount], [], [], [])
const withdrawMax =
amount == floorToDecimal(stakeBalance, stakeBank.mintDecimals).toNumber()
amount >= floorToDecimal(stakeBalance, stakeBank.mintDecimals).toNumber()
console.log('withdrawMax: ', withdrawMax)
const nativeWithdrawAmount = toNative(
@ -131,10 +131,8 @@ export const unstakeAndSwap = async (
if (borrowed.toNumber() < 0) {
const toRepay = Math.ceil(
(amountToRepay
? toNativeI80F48(amountToRepay, stakeBank.mintDecimals).mul(
stakeBank.getAssetPrice(),
)
: borrowed.abs()
? toNativeI80F48(amountToRepay, stakeBank.mintDecimals)
: borrowed.abs().div(stakeBank.getAssetPrice())
)
.add(I80F48.fromNumber(100))
.toNumber(),
@ -142,19 +140,22 @@ export const unstakeAndSwap = async (
console.log('borrowedSol amount: ', borrowed.toNumber())
console.log('borrow needed to repay for withdraw', toRepay)
const slippage = 1
const { bestRoute: selectedRoute } = await fetchJupiterRoutes(
stakeMintPk.toString(),
borrowBank.mint.toString(),
toRepay,
500,
'ExactOut',
Math.ceil(toRepay),
slippage,
'ExactIn',
0,
false,
)
console.log(selectedRoute)
if (!selectedRoute) {
throw Error('Unable to find a swap route')
}
const slippage = 500 // bips
const [jupiterIxs, jupiterAlts] = await fetchJupiterTransaction(
client.program.provider.connection,
@ -744,7 +745,7 @@ const fetchJupiterRoutes = async (
inputMint: string,
outputMint: string,
amount = 0,
slippage = 50,
slippage = 5,
swapMode = 'ExactIn',
feeBps = 0,
onlyDirectRoutes = true,