Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reversible staking options withdraw #1749

Merged
merged 4 commits into from
Aug 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"@dialectlabs/react-sdk-blockchain-solana": "1.0.0-beta.3",
"@dialectlabs/react-ui": "1.1.0-beta.5",
"@dual-finance/airdrop": "0.2.2",
"@dual-finance/staking-options": "0.0.24",
"@dual-finance/staking-options": "0.0.26",
"@emotion/react": "11.9.0",
"@emotion/styled": "11.8.1",
"@everlend/general-pool": "0.0.27",
Expand Down
53 changes: 45 additions & 8 deletions utils/instructions/Dual/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
TransactionInstruction,
} from '@solana/web3.js'

import { StakingOptions } from '@dual-finance/staking-options'
import { DUAL_DAO_WALLET_PK, StakingOptions } from '@dual-finance/staking-options'
import { ConnectionContext } from '@utils/connection'
import { validateInstruction } from '@utils/instructionTools'
import {
Expand Down Expand Up @@ -413,13 +413,17 @@ export async function getWithdrawInstruction({
const serializedInstruction = ''
const additionalSerializedInstructions: string[] = []
const prerequisiteInstructions: TransactionInstruction[] = []
// First is for base token, second is for quote token.
let helperTokenAccount: Keypair | null = null
if (isValid && form.soName && form.baseTreasury && wallet?.publicKey) {
let helperTokenAccount2: Keypair | null = null
if (isValid && form.soName && form.baseTreasury && wallet?.publicKey && form.mintPk) {
const so = getStakingOptionsApi(connection)
const authority = form.baseTreasury.isSol
? form.baseTreasury.extensions.transferAddress
: form.baseTreasury.extensions.token!.account.owner!
let destination = form.baseTreasury.pubkey
let baseDestination = form.baseTreasury.pubkey
let quoteDestination = (await so.getState(form.soName, new PublicKey(form.mintPk))).quoteAccount as PublicKey;

if (form.baseTreasury.isSol) {
const baseMint = form.mintPk
const space = 165
Expand All @@ -446,17 +450,50 @@ export async function getWithdrawInstruction({
owner: form.baseTreasury.governance.pubkey,
})
)
destination = helperTokenAccount.publicKey
baseDestination = helperTokenAccount.publicKey

const quoteMint = (await so.getState(form.soName, new PublicKey(form.mintPk))).quoteMint;
helperTokenAccount2 = new Keypair()
//run as prerequsite instructions payer is connected wallet
prerequisiteInstructions.push(
SystemProgram.createAccount({
fromPubkey: wallet.publicKey,
newAccountPubkey: helperTokenAccount2.publicKey,
lamports: rent,
space: space,
programId: TOKEN_PROGRAM_ID,
}),
//initialized account with same mint as quote
initializeAccount({
account: helperTokenAccount2.publicKey,
mint: new PublicKey(quoteMint!),
owner: form.baseTreasury.governance.pubkey,
})
)
quoteDestination = helperTokenAccount2.publicKey

// Initialize the fee account so the tx succeeds. This happens when there
// is a base token that DUAL DAO has never received before.
const feeAccount = await StakingOptions.getFeeAccount(new PublicKey(quoteMint!));
if (!(await connection.current.getAccountInfo(feeAccount))) {
const [ataIx] = await createAssociatedTokenAccount(
wallet.publicKey,
DUAL_DAO_WALLET_PK,
new PublicKey(quoteMint!)
)
additionalSerializedInstructions.push(serializeInstructionToBase64(ataIx))
}
}

const withdrawInstruction = form.baseTreasury.isSol
? await so.createWithdrawInstructionWithMint(
form.soName,
authority!,
destination,
new PublicKey(form.mintPk!)
baseDestination,
new PublicKey(form.mintPk!),
quoteDestination,
)
: await so.createWithdrawInstruction(form.soName, authority!, destination)
: await so.createWithdrawInstruction(form.soName, authority!, baseDestination, quoteDestination)

additionalSerializedInstructions.push(
serializeInstructionToBase64(withdrawInstruction)
Expand All @@ -466,7 +503,7 @@ export async function getWithdrawInstruction({
serializedInstruction,
prerequisiteInstructions: prerequisiteInstructions,
prerequisiteInstructionsSigners: helperTokenAccount
? [helperTokenAccount]
? [helperTokenAccount, helperTokenAccount2]
: [],
isValid: true,
governance: form.baseTreasury?.governance,
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1013,10 +1013,10 @@
"@solana/web3.js" "^1.73.2"
js-sha3 "^0.8.0"

"@dual-finance/[email protected].24":
version "0.0.24"
resolved "https://registry.yarnpkg.com/@dual-finance/staking-options/-/staking-options-0.0.24.tgz#376568dee9389a4133ef700fd55b3143802fdd7f"
integrity sha512-SMcER1lW2QgcZuHNa9PQmAQu5CyTMXeiZMmbRbEzMmhkSTqVPMauWaiVDeGZ35MxwN0pARGrVl7pf0gOVmHdsw==
"@dual-finance/[email protected].26":
version "0.0.26"
resolved "https://registry.yarnpkg.com/@dual-finance/staking-options/-/staking-options-0.0.26.tgz#37bce685e1b8a2c8a3dca00606899249d897ab62"
integrity sha512-jwtMpoQzIliKlp+Nlu2Js/UaXL7/0tk+NqtMZLdKf997OdfHgxoDJrUC/sha1oZgBEuPjAwThWtwlY+Ku/tc8w==
dependencies:
"@coral-xyz/anchor" "^0.28.0"
"@dual-finance/staking-options" "^0.0.21"
Expand Down