From fdda87b3da531efc00f70205c2fa4f3fcd7250e5 Mon Sep 17 00:00:00 2001 From: Alexandru Stefan Date: Tue, 5 Nov 2024 13:21:41 +0200 Subject: [PATCH 1/3] feat: expose withdraw stake acc method --- CHANGELOG.md | 6 +++ package.json | 2 +- src/programs/marinade-finance-program.ts | 46 ++++++++++++++++++++ test/marinade-finance-instructions.spec.ts | 50 ++++++++++++++++++++++ 4 files changed, 103 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1ff5897..2ba3e50 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +## v5.0.14 + +### Feat: + + - Expose withdraw stake account method from program + ## v5.0.13 ### Fix: diff --git a/package.json b/package.json index 1e7fd42..f6b1d28 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@marinade.finance/marinade-ts-sdk", - "version": "5.0.13", + "version": "5.0.14", "description": "Marinade SDK for Typescript", "main": "dist/src/index.js", "repository": { diff --git a/src/programs/marinade-finance-program.ts b/src/programs/marinade-finance-program.ts index 1aa5905..1937716 100644 --- a/src/programs/marinade-finance-program.ts +++ b/src/programs/marinade-finance-program.ts @@ -282,4 +282,50 @@ export class MarinadeFinanceProgram { tokenProgram: TOKEN_PROGRAM_ID, }) .instruction() + + withdrawStakeAccount = async ({ + marinadeState, + ownerAddress, + associatedMSolTokenAccountAddress, // burnMsolFrom + stakeAccountAddress, + splitStakeAccountAddress, + splitStakeRentPayer, + stakeIndex, + validatorIndex, + msolAmount, + beneficiary, + }: { + marinadeState: MarinadeState + ownerAddress: web3.PublicKey + associatedMSolTokenAccountAddress: web3.PublicKey + stakeAccountAddress: web3.PublicKey + splitStakeAccountAddress: web3.PublicKey + splitStakeRentPayer: web3.PublicKey + stakeIndex: number + validatorIndex: number + msolAmount: BN + beneficiary: web3.PublicKey + }): Promise => + await this.program.methods + .withdrawStakeAccount(stakeIndex, validatorIndex, msolAmount, beneficiary) + .accountsStrict({ + state: marinadeState.marinadeStateAddress, + msolMint: marinadeState.mSolMintAddress, + burnMsolFrom: associatedMSolTokenAccountAddress, + burnMsolAuthority: ownerAddress, + treasuryMsolAccount: marinadeState.treasuryMsolAccount, + validatorList: + marinadeState.state.validatorSystem.validatorList.account, + stakeList: marinadeState.state.stakeSystem.stakeList.account, + stakeWithdrawAuthority: await marinadeState.stakeWithdrawAuthority(), + stakeDepositAuthority: await marinadeState.stakeDepositAuthority(), + stakeAccount: stakeAccountAddress, + splitStakeAccount: splitStakeAccountAddress, + splitStakeRentPayer, + clock: web3.SYSVAR_CLOCK_PUBKEY, + systemProgram: SYSTEM_PROGRAM_ID, + tokenProgram: TOKEN_PROGRAM_ID, + stakeProgram: STAKE_PROGRAM_ID, + }) + .instruction() } diff --git a/test/marinade-finance-instructions.spec.ts b/test/marinade-finance-instructions.spec.ts index 81230e9..6be8c40 100644 --- a/test/marinade-finance-instructions.spec.ts +++ b/test/marinade-finance-instructions.spec.ts @@ -6,6 +6,8 @@ import { DirectedStakeSdk, findVoteRecords, } from '@marinade.finance/directed-stake-sdk' +import { getAccount } from '@solana/spl-token-3.x' +import { LAMPORTS_PER_SOL } from '@solana/web3.js' describe('Marinade Finance', () => { beforeAll(async () => { @@ -402,4 +404,52 @@ describe('Marinade Finance', () => { } }) }) + + describe('withdraw stake account', () => { + it('withdraw stake account', async () => { + const config = new MarinadeConfig({ + connection: TestWorld.CONNECTION, + publicKey: TestWorld.SDK_USER.publicKey, + }) + const marinade = new Marinade(config) + // for a validator could be deposited, it must be activated for at least 2 epochs + // i.e., "error: Deposited stake could not be activated yet. Wait for #2 epoch" + await TestWorld.waitForStakeAccountActivation({ + stakeAccount: TestWorld.STAKE_ACCOUNT_TO_WITHDRAW.publicKey, + connection: TestWorld.CONNECTION, + activatedAtLeastFor: 2, + }) + const { + transaction: transactionDeposit, + associatedMSolTokenAccountAddress, + } = await marinade.depositStakeAccount( + TestWorld.STAKE_ACCOUNT_TO_WITHDRAW.publicKey + ) + await TestWorld.executeTransaction(transactionDeposit) + const msolTokenBefore = await getAccount( + TestWorld.CONNECTION, + associatedMSolTokenAccountAddress + ) + const { transaction, splitStakeAccountKeypair } = + await marinade.withdrawStakeAccount( + LAMPORTS_PER_SOL, + TestWorld.STAKE_ACCOUNT_TO_WITHDRAW.publicKey + ) + const { executedSlot, executionSlot, err, logs, unitsConsumed } = + await TestWorld.executeTransaction(transaction, [ + splitStakeAccountKeypair, + ]) + expect(err).toBeNull() // no error at simulation + expect(executionSlot).toBeGreaterThanOrEqual(executedSlot) + expect(unitsConsumed).toBeGreaterThan(0) // something has been processed + console.log('Withdraw stake account tx logs:', logs) + const msolTokenAfter = await getAccount( + TestWorld.CONNECTION, + associatedMSolTokenAccountAddress + ) + expect( + (msolTokenBefore?.amount - msolTokenAfter?.amount).toString() + ).toEqual(LAMPORTS_PER_SOL.toString()) + }) + }) }) From 74d39379d0c78a9fd8d665144af5a7a5dc055fbb Mon Sep 17 00:00:00 2001 From: Alexandru Stefan Date: Tue, 5 Nov 2024 13:26:01 +0200 Subject: [PATCH 2/3] chore: remove wrong test --- test/marinade-finance-instructions.spec.ts | 50 ---------------------- 1 file changed, 50 deletions(-) diff --git a/test/marinade-finance-instructions.spec.ts b/test/marinade-finance-instructions.spec.ts index 6be8c40..81230e9 100644 --- a/test/marinade-finance-instructions.spec.ts +++ b/test/marinade-finance-instructions.spec.ts @@ -6,8 +6,6 @@ import { DirectedStakeSdk, findVoteRecords, } from '@marinade.finance/directed-stake-sdk' -import { getAccount } from '@solana/spl-token-3.x' -import { LAMPORTS_PER_SOL } from '@solana/web3.js' describe('Marinade Finance', () => { beforeAll(async () => { @@ -404,52 +402,4 @@ describe('Marinade Finance', () => { } }) }) - - describe('withdraw stake account', () => { - it('withdraw stake account', async () => { - const config = new MarinadeConfig({ - connection: TestWorld.CONNECTION, - publicKey: TestWorld.SDK_USER.publicKey, - }) - const marinade = new Marinade(config) - // for a validator could be deposited, it must be activated for at least 2 epochs - // i.e., "error: Deposited stake could not be activated yet. Wait for #2 epoch" - await TestWorld.waitForStakeAccountActivation({ - stakeAccount: TestWorld.STAKE_ACCOUNT_TO_WITHDRAW.publicKey, - connection: TestWorld.CONNECTION, - activatedAtLeastFor: 2, - }) - const { - transaction: transactionDeposit, - associatedMSolTokenAccountAddress, - } = await marinade.depositStakeAccount( - TestWorld.STAKE_ACCOUNT_TO_WITHDRAW.publicKey - ) - await TestWorld.executeTransaction(transactionDeposit) - const msolTokenBefore = await getAccount( - TestWorld.CONNECTION, - associatedMSolTokenAccountAddress - ) - const { transaction, splitStakeAccountKeypair } = - await marinade.withdrawStakeAccount( - LAMPORTS_PER_SOL, - TestWorld.STAKE_ACCOUNT_TO_WITHDRAW.publicKey - ) - const { executedSlot, executionSlot, err, logs, unitsConsumed } = - await TestWorld.executeTransaction(transaction, [ - splitStakeAccountKeypair, - ]) - expect(err).toBeNull() // no error at simulation - expect(executionSlot).toBeGreaterThanOrEqual(executedSlot) - expect(unitsConsumed).toBeGreaterThan(0) // something has been processed - console.log('Withdraw stake account tx logs:', logs) - const msolTokenAfter = await getAccount( - TestWorld.CONNECTION, - associatedMSolTokenAccountAddress - ) - expect( - (msolTokenBefore?.amount - msolTokenAfter?.amount).toString() - ).toEqual(LAMPORTS_PER_SOL.toString()) - }) - }) }) From 18576523047b77d0e9ccdbc33773c4dda3360281 Mon Sep 17 00:00:00 2001 From: Alexandru Stefan Date: Tue, 5 Nov 2024 14:27:43 +0200 Subject: [PATCH 3/3] fix: method name --- src/programs/marinade-finance-program.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/programs/marinade-finance-program.ts b/src/programs/marinade-finance-program.ts index 1937716..74d2bbc 100644 --- a/src/programs/marinade-finance-program.ts +++ b/src/programs/marinade-finance-program.ts @@ -283,7 +283,7 @@ export class MarinadeFinanceProgram { }) .instruction() - withdrawStakeAccount = async ({ + withdrawStakeAccountInstructionBuilder = async ({ marinadeState, ownerAddress, associatedMSolTokenAccountAddress, // burnMsolFrom