Skip to content

Commit

Permalink
fix: wip version of liquidate stake pool tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexStefan committed Mar 18, 2023
1 parent dc5c031 commit ba2b96f
Show file tree
Hide file tree
Showing 7 changed files with 7,477 additions and 19,339 deletions.
26,708 changes: 7,375 additions & 19,333 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@marinade.finance/marinade-ts-sdk",
"version": "4.0.3",
"version": "4.0.4",
"description": "Marinade SDK for Typescript",
"main": "dist/src/index.js",
"repository": {
Expand Down Expand Up @@ -36,7 +36,8 @@
"license": "ISC",
"dependencies": {
"@project-serum/anchor": "^0.18.2",
"@solana/spl-token": "^0.3.6",
"@solana/spl-stake-pool": "^0.6.5",
"@solana/spl-token-3.x": "npm:@solana/spl-token@^0.3.7",
"bignumber.js": "^9.1.0",
"borsh": "^0.6.0",
"bs58": "^5.0.0"
Expand Down
2 changes: 1 addition & 1 deletion src/marinade-mint/marinade-mint.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Provider, web3, BN } from '@project-serum/anchor'
import { getMint, Mint } from '@solana/spl-token'
import { getMint, Mint } from '@solana/spl-token-3.x'
import { tokenBalanceToNumber } from '../util/conversion'

export class MarinadeMint {
Expand Down
95 changes: 95 additions & 0 deletions src/marinade.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import { MarinadeConfig } from './config/marinade-config'
import { BN, Provider, Wallet, web3 } from '@project-serum/anchor'
import { MarinadeState } from './marinade-state/marinade-state'
Expand All @@ -14,6 +15,19 @@ import { MarinadeReferralGlobalState } from './marinade-referral-state/marinade-
import { assertNotNullAndReturn } from './util/assert'
import { TicketAccount } from './marinade-state/borsh/ticket-account'
import { computeMsolAmount, proportionalBN } from './util'
import { getStakePoolAccount, StakePool, withdrawStake } from '@solana/spl-stake-pool'

export function calcLamportsWithdrawAmount(
stakePool: StakePool,
poolTokens: BN
): BN {
const numerator = poolTokens.mul(stakePool.totalLamports)
const denominator = stakePool.poolTokenSupply
if (numerator.lt(denominator)) {
return new BN(0)
}
return numerator.div(denominator)
}

export class Marinade {
constructor(public readonly config: MarinadeConfig = new MarinadeConfig()) { }
Expand Down Expand Up @@ -352,6 +366,87 @@ export class Marinade {
}
}

/**
* Returns a transaction with the instructions to
* Liquidate an amount of stake pool tokens.
*
* @param {web3.PublicKey} stakePoolTokenAddress - The stake pool token account to be liquidated
* @param {BN} amountToLiquidate - Amount to liquidate
*/
async liquidateStakePoolToken(stakePoolTokenAddress: web3.PublicKey, amountToLiquidate: number): Promise<MarinadeResult.LiquidateStakeAccount> {
const marinadeState = await this.getMarinadeState()
const ownerAddress = assertNotNullAndReturn(this.config.publicKey, ErrorMessage.NO_PUBLIC_KEY)

const transaction = new web3.Transaction({
feePayer: ownerAddress,
})

const withdrawTx = await withdrawStake(
this.provider.connection,
stakePoolTokenAddress,
ownerAddress,
amountToLiquidate
)
const stakePool = await getStakePoolAccount(this.provider.connection, stakePoolTokenAddress)
const solValue = calcLamportsWithdrawAmount(
stakePool.account.data,
new BN(amountToLiquidate)
)
const mSolAmountToReceive = solValue.toNumber() / marinadeState.mSolPrice

transaction.add(...withdrawTx.instructions)

const {
associatedTokenAccountAddress: associatedMSolTokenAccountAddress,
createAssociateTokenInstruction,
} = await getOrCreateAssociatedTokenAccount(this.provider, marinadeState.mSolMintAddress, ownerAddress)

if (createAssociateTokenInstruction) {
transaction.add(createAssociateTokenInstruction)
}

const duplicationFlag = await marinadeState.validatorDuplicationFlag(
new web3.PublicKey(
"GE6atKoWiQ2pt3zL7N13pjNHjdLVys8LinG8qeJLcAiL"
)
)
const { validatorRecords } = await marinadeState.getValidatorRecords()
const validatorLookupIndex = validatorRecords.findIndex(
({ validatorAccount }) => validatorAccount.equals(new web3.PublicKey(
"GE6atKoWiQ2pt3zL7N13pjNHjdLVys8LinG8qeJLcAiL"
))
)
const validatorIndex =
validatorLookupIndex === -1
? marinadeState.state.validatorSystem.validatorList.count
: validatorLookupIndex

const depositTx = await this.marinadeFinanceProgram.depositStakeAccountInstructionBuilder({
validatorIndex,
marinadeState,
duplicationFlag,
ownerAddress,
stakeAccountAddress: withdrawTx.signers[1].publicKey,
authorizedWithdrawerAddress: ownerAddress,
associatedMSolTokenAccountAddress,
})

const liquidUnstakeInstruction = await this.marinadeFinanceProgram.liquidUnstakeInstructionBuilder({
amountLamports: new BN(mSolAmountToReceive),
marinadeState,
ownerAddress,
associatedMSolTokenAccountAddress,
})
transaction.add(depositTx)
transaction.add(liquidUnstakeInstruction)

return {
transaction: transaction,
associatedMSolTokenAccountAddress,
voterAddress: stakePoolTokenAddress,
}
}

/**
* @todo
*/
Expand Down
2 changes: 1 addition & 1 deletion src/programs/marinade-finance-program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { MarinadeFinanceIdl } from './idl/marinade-finance-idl'
import * as marinadeFinanceIdlSchema from './idl/marinade-finance-idl.json'
import { MarinadeState } from '../marinade-state/marinade-state'
import { STAKE_PROGRAM_ID, SYSTEM_PROGRAM_ID, getEpochInfo, getTicketDateInfo, estimateTicketDateInfo } from '../util'
import { TOKEN_PROGRAM_ID } from '@solana/spl-token'
import { TOKEN_PROGRAM_ID } from '@solana/spl-token-3.x'
import { SYSVAR_CLOCK_PUBKEY, SYSVAR_RENT_PUBKEY } from '@solana/web3.js'
import { MARINADE_BORSH_SCHEMA } from '../marinade-state/borsh'
import { deserializeUnchecked } from 'borsh'
Expand Down
2 changes: 1 addition & 1 deletion src/programs/marinade-referral-program.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BN, Idl, Program, Provider, web3 } from '@project-serum/anchor'
import { TOKEN_PROGRAM_ID } from '@solana/spl-token'
import { TOKEN_PROGRAM_ID } from '@solana/spl-token-3.x'
import { SYSVAR_CLOCK_PUBKEY, SYSVAR_RENT_PUBKEY } from '@solana/web3.js'
import { MarinadeState } from '../marinade-state/marinade-state'
import { MarinadeReferralStateResponse } from '../marinade-referral-state/marinade-referral-state.types'
Expand Down
2 changes: 1 addition & 1 deletion src/util/anchor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BN, Provider, utils, web3 } from '@project-serum/anchor'
import { createAssociatedTokenAccountInstruction, getAccount, TokenError } from '@solana/spl-token'
import { createAssociatedTokenAccountInstruction, getAccount, TokenError } from '@solana/spl-token-3.x'
import { ParsedStakeAccountInfo, ProcessedEpochInfo } from './anchor.types'

export const SYSTEM_PROGRAM_ID = new web3.PublicKey('11111111111111111111111111111111')
Expand Down

0 comments on commit ba2b96f

Please sign in to comment.