extract borrowAndWithdraw from ui to mango-client pkg
This commit is contained in:
parent
7b8035e960
commit
3db7ed86a5
|
@ -73,12 +73,6 @@ const WithdrawModal = ({ isOpen, onClose }) => {
|
|||
)
|
||||
const liabsAvail = (assetsVal / 1.2 - currentLiabs) * 0.99 - 0.01
|
||||
|
||||
console.log('selected token deposits', getMaxForSelectedAccount())
|
||||
console.log('prices', prices)
|
||||
console.log('assetsVal', assetsVal)
|
||||
console.log('currentLiabs', currentLiabs)
|
||||
console.log('liabsAvail', liabsAvail)
|
||||
|
||||
const amountToWithdraw =
|
||||
liabsAvail / prices[tokenIndex] + getMaxForSelectedAccount()
|
||||
const decimals = mintDecimals[getTokenIndex(mintAddress)]
|
||||
|
@ -99,8 +93,6 @@ const WithdrawModal = ({ isOpen, onClose }) => {
|
|||
if (!marginAccount || !mangoGroup) return
|
||||
|
||||
if (Number(inputAmount) <= getMaxForSelectedAccount()) {
|
||||
console.log('=withdraw without borrow=')
|
||||
|
||||
withdraw(
|
||||
connection,
|
||||
programId,
|
||||
|
@ -129,8 +121,6 @@ const WithdrawModal = ({ isOpen, onClose }) => {
|
|||
onClose()
|
||||
})
|
||||
} else {
|
||||
console.log('-withdraw with borrow-')
|
||||
|
||||
borrowAndWithdraw(
|
||||
connection,
|
||||
programId,
|
||||
|
|
|
@ -26,7 +26,7 @@
|
|||
]
|
||||
},
|
||||
"dependencies": {
|
||||
"@blockworks-foundation/mango-client": "0.1.13",
|
||||
"@blockworks-foundation/mango-client": "0.1.14",
|
||||
"@emotion/react": "^11.1.5",
|
||||
"@emotion/styled": "^11.1.5",
|
||||
"@headlessui/react": "^0.3.2-d950146",
|
||||
|
|
|
@ -27,8 +27,10 @@ import {
|
|||
NUM_TOKENS,
|
||||
} from '@blockworks-foundation/mango-client/lib/layout'
|
||||
import {
|
||||
makeBorrowInstruction,
|
||||
makeSettleBorrowInstruction,
|
||||
makeSettleFundsInstruction,
|
||||
makeWithdrawInstruction,
|
||||
} from '@blockworks-foundation/mango-client/lib/instruction'
|
||||
import { sendTransaction } from './send'
|
||||
import { TOKEN_PROGRAM_ID } from './tokens'
|
||||
|
@ -310,12 +312,9 @@ export async function borrow(
|
|||
quantity: number
|
||||
): Promise<TransactionSignature> {
|
||||
const tokenIndex = mangoGroup.getTokenIndex(token)
|
||||
// const nativeQuantity = uiToNative(
|
||||
// quantity,
|
||||
// mangoGroup.mintDecimals[tokenIndex]
|
||||
// )
|
||||
const nativeQuantity = new BN(
|
||||
Math.floor(quantity * Math.pow(10, mangoGroup.mintDecimals[tokenIndex]))
|
||||
const nativeQuantity = uiToNative(
|
||||
quantity,
|
||||
mangoGroup.mintDecimals[tokenIndex]
|
||||
)
|
||||
|
||||
const keys = [
|
||||
|
@ -371,85 +370,44 @@ export async function borrowAndWithdraw(
|
|||
const tokenIndex = mangoGroup.getTokenIndex(token)
|
||||
const tokenBalance = marginAccount.getUiDeposit(mangoGroup, tokenIndex)
|
||||
const borrowQuantity = withdrawQuantity - tokenBalance
|
||||
console.log('token balance: ', tokenBalance)
|
||||
console.log('withdraw quantity', withdrawQuantity)
|
||||
console.log('borrow quantity', borrowQuantity)
|
||||
|
||||
const nativeBorrowQuantity = uiToNative(
|
||||
borrowQuantity,
|
||||
mangoGroup.mintDecimals[tokenIndex]
|
||||
)
|
||||
console.log('nativeBorrowQuantity', nativeBorrowQuantity)
|
||||
|
||||
const borrowKeys = [
|
||||
{ isSigner: false, isWritable: true, pubkey: mangoGroup.publicKey },
|
||||
{ isSigner: false, isWritable: true, pubkey: marginAccount.publicKey },
|
||||
{ isSigner: true, isWritable: false, pubkey: wallet.publicKey },
|
||||
{ isSigner: false, isWritable: false, pubkey: SYSVAR_CLOCK_PUBKEY },
|
||||
...marginAccount.openOrders.map((pubkey) => ({
|
||||
isSigner: false,
|
||||
isWritable: false,
|
||||
pubkey,
|
||||
})),
|
||||
...mangoGroup.oracles.map((pubkey) => ({
|
||||
isSigner: false,
|
||||
isWritable: false,
|
||||
pubkey,
|
||||
})),
|
||||
]
|
||||
const borrowData = encodeMangoInstruction({
|
||||
Borrow: { tokenIndex: new BN(tokenIndex), quantity: nativeBorrowQuantity },
|
||||
})
|
||||
|
||||
const borrowInstruction = new TransactionInstruction({
|
||||
keys: borrowKeys,
|
||||
data: borrowData,
|
||||
const borrowInstruction = makeBorrowInstruction(
|
||||
programId,
|
||||
})
|
||||
mangoGroup.publicKey,
|
||||
marginAccount.publicKey,
|
||||
wallet.publicKey,
|
||||
tokenIndex,
|
||||
marginAccount.openOrders,
|
||||
mangoGroup.oracles,
|
||||
nativeBorrowQuantity
|
||||
)
|
||||
transaction.add(borrowInstruction)
|
||||
|
||||
const withdrawKeys = [
|
||||
{ isSigner: false, isWritable: true, pubkey: mangoGroup.publicKey },
|
||||
{ isSigner: false, isWritable: true, pubkey: marginAccount.publicKey },
|
||||
{ isSigner: true, isWritable: false, pubkey: wallet.publicKey },
|
||||
{ isSigner: false, isWritable: true, pubkey: tokenAcc },
|
||||
{
|
||||
isSigner: false,
|
||||
isWritable: true,
|
||||
pubkey: mangoGroup.vaults[tokenIndex],
|
||||
},
|
||||
{ isSigner: false, isWritable: false, pubkey: mangoGroup.signerKey },
|
||||
{ isSigner: false, isWritable: false, pubkey: TOKEN_PROGRAM_ID },
|
||||
{ isSigner: false, isWritable: false, pubkey: SYSVAR_CLOCK_PUBKEY },
|
||||
...marginAccount.openOrders.map((pubkey) => ({
|
||||
isSigner: false,
|
||||
isWritable: false,
|
||||
pubkey,
|
||||
})),
|
||||
...mangoGroup.oracles.map((pubkey) => ({
|
||||
isSigner: false,
|
||||
isWritable: false,
|
||||
pubkey,
|
||||
})),
|
||||
]
|
||||
|
||||
// uiToNative() uses Math.round causing
|
||||
// errors so we use Math.floor here instead
|
||||
const nativeWithdrawQuantity = new BN(
|
||||
Math.floor(
|
||||
withdrawQuantity * Math.pow(10, mangoGroup.mintDecimals[tokenIndex])
|
||||
) * 0.98
|
||||
)
|
||||
// const nativeWithdrawQuantity = uiToNative(
|
||||
// withdrawQuantity,
|
||||
// mangoGroup.mintDecimals[tokenIndex]
|
||||
// )
|
||||
const withdrawData = encodeMangoInstruction({
|
||||
Withdraw: { quantity: nativeWithdrawQuantity },
|
||||
})
|
||||
const withdrawInstruction = new TransactionInstruction({
|
||||
keys: withdrawKeys,
|
||||
data: withdrawData,
|
||||
|
||||
const withdrawInstruction = makeWithdrawInstruction(
|
||||
programId,
|
||||
})
|
||||
mangoGroup.publicKey,
|
||||
marginAccount.publicKey,
|
||||
wallet.publicKey,
|
||||
mangoGroup.signerKey,
|
||||
tokenAcc,
|
||||
mangoGroup.vaults[tokenIndex],
|
||||
marginAccount.openOrders,
|
||||
mangoGroup.oracles,
|
||||
nativeWithdrawQuantity
|
||||
)
|
||||
transaction.add(withdrawInstruction)
|
||||
|
||||
const signers = []
|
||||
|
|
12
yarn.lock
12
yarn.lock
|
@ -841,10 +841,10 @@
|
|||
version "0.2.3"
|
||||
resolved "https://registry.yarnpkg.com/@bcoe/v8-coverage/-/v8-coverage-0.2.3.tgz#75a2e8b51cb758a7553d6804a5932d7aace75c39"
|
||||
|
||||
"@blockworks-foundation/mango-client@0.1.13":
|
||||
version "0.1.13"
|
||||
resolved "https://registry.yarnpkg.com/@blockworks-foundation/mango-client/-/mango-client-0.1.13.tgz#fba50904b40ae660b4bc3ef9f77b44a01b96b765"
|
||||
integrity sha512-BIX0P92v1mYhAQYPBfOe9fk35UFAmzdGuop4Kb6vqQ6/VB6RvtxCaBCBRWd4DISVXkrd0CEtu5E3zHctEy5UZg==
|
||||
"@blockworks-foundation/mango-client@0.1.14":
|
||||
version "0.1.14"
|
||||
resolved "https://registry.yarnpkg.com/@blockworks-foundation/mango-client/-/mango-client-0.1.14.tgz#ec113878e8e9a6dbe6598009a7adbb0fb0a8e649"
|
||||
integrity sha512-sRL9Snb6zWwvGnPJ2G8bE8acpq7rK8775TycGIRYbapCP2LxaMWSItIciY0wtTm68lCWpaFImIKH+iAlUeO/3Q==
|
||||
dependencies:
|
||||
"@project-serum/serum" "^0.13.20"
|
||||
"@project-serum/sol-wallet-adapter" "^0.1.4"
|
||||
|
@ -2101,9 +2101,9 @@ boolbase@^1.0.0, boolbase@~1.0.0:
|
|||
version "1.0.0"
|
||||
resolved "https://registry.yarnpkg.com/boolbase/-/boolbase-1.0.0.tgz#68dff5fbe60c51eb37725ea9e3ed310dcc1e776e"
|
||||
|
||||
"borsh@https://github.com/defactojob/borsh-js#field-mapper":
|
||||
"borsh@git+https://github.com/defactojob/borsh-js.git#field-mapper":
|
||||
version "0.3.1"
|
||||
resolved "https://github.com/defactojob/borsh-js#33a0d24af281112c0a48efb3fa503f3212443de9"
|
||||
resolved "git+https://github.com/defactojob/borsh-js.git#33a0d24af281112c0a48efb3fa503f3212443de9"
|
||||
dependencies:
|
||||
"@types/bn.js" "^4.11.5"
|
||||
bn.js "^5.0.0"
|
||||
|
|
Loading…
Reference in New Issue