Skip to content

Commit

Permalink
liquidateStakeAccount fix, move to v4.0.1
Browse files Browse the repository at this point in the history
liquidateStakeAccount: referral program deposit operation fee calculated to available mSOL for unstake
  • Loading branch information
luciotato authored Sep 28, 2022
2 parents 9e6938d + beda08d commit 19e9a33
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 9 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,9 @@
v4.0.0

Uses Referral-Program V2

v4.0.1

Fix incorrect mSOL amount calculation in function liquidateStakeAccount, when using a referral code


4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion 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.0",
"version": "4.0.1",
"description": "Marinade SDK for Typescript",
"main": "dist/src/index.js",
"repository": {
Expand Down
23 changes: 17 additions & 6 deletions src/marinade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { MarinadeReferralPartnerState } from './marinade-referral-state/marinade
import { MarinadeReferralGlobalState } from './marinade-referral-state/marinade-referral-global-state'
import { assertNotNullAndReturn } from './util/assert'
import { TicketAccount } from './marinade-state/borsh/ticket-account'
import { computeMsolAmount } from './util'
import { computeMsolAmount, proportionalBN } from './util'

export class Marinade {
constructor(public readonly config: MarinadeConfig = new MarinadeConfig()) { }
Expand Down Expand Up @@ -38,8 +38,12 @@ export class Marinade {
this,
)

private isReferralProgram(): boolean {
return this.config.referralCode != null
}

private provideReferralOrMainProgram(): MarinadeFinanceProgram | MarinadeReferralProgram {
return this.config.referralCode ? this.marinadeReferralProgram : this.marinadeFinanceProgram
return this.isReferralProgram() ? this.marinadeReferralProgram : this.marinadeFinanceProgram
}

/**
Expand Down Expand Up @@ -301,11 +305,18 @@ export class Marinade {
const stakeBalance = new BN(totalBalance - rent)
const marinadeState = await this.getMarinadeState()

const { transaction: depositTx, associatedMSolTokenAccountAddress, voterAddress } = await this.depositStakeAccount(stakeAccountAddress)
const { transaction: depositTx, associatedMSolTokenAccountAddress, voterAddress } =
await this.depositStakeAccount(stakeAccountAddress)

const mSolAmountToReceive = computeMsolAmount(stakeBalance, marinadeState)
// when working with referral partner the costs of the deposit operation is subtracted from the mSOL amount the user receives
if (this.isReferralProgram()) {
const partnerOperationFee = (await this.marinadeReferralProgram.getReferralStateData()).operationDepositStakeAccountFee
mSolAmountToReceive.sub(proportionalBN(mSolAmountToReceive, new BN(partnerOperationFee), new BN(10_000)))
}

const availableMsol = computeMsolAmount(stakeBalance, marinadeState)
const unstakeAmount = availableMsol.sub(mSolToKeep ?? new BN(0))
const { transaction: unstakeTx } = await this.liquidUnstake(unstakeAmount, associatedMSolTokenAccountAddress)
const unstakeAmountMSol = mSolAmountToReceive.sub(mSolToKeep ?? new BN(0))
const { transaction: unstakeTx } = await this.liquidUnstake(unstakeAmountMSol, associatedMSolTokenAccountAddress)

return {
transaction: depositTx.add(unstakeTx),
Expand Down

0 comments on commit 19e9a33

Please sign in to comment.