diff --git a/governance/pyth_staking_sdk/src/pyth-staking-client.ts b/governance/pyth_staking_sdk/src/pyth-staking-client.ts index 3da87a3e3c..8f9c60339c 100644 --- a/governance/pyth_staking_sdk/src/pyth-staking-client.ts +++ b/governance/pyth_staking_sdk/src/pyth-staking-client.ts @@ -246,19 +246,29 @@ export class PythStakingClient { stakeAccountPositions: PublicKey, amount: bigint, ) { - const instruction = await this.stakingProgram.methods - .createPosition( - { - voting: {}, - }, - new BN(amount.toString()), - ) - .accounts({ - stakeAccountPositions, - }) - .instruction(); + const instructions = []; - return sendTransaction([instruction], this.connection, this.wallet); + if (!(await this.hasJoinedDaoLlc(stakeAccountPositions))) { + instructions.push( + await this.getJoinDaoLlcInstruction(stakeAccountPositions), + ); + } + + instructions.push( + await this.stakingProgram.methods + .createPosition( + { + voting: {}, + }, + new BN(amount.toString()), + ) + .accounts({ + stakeAccountPositions, + }) + .instruction(), + ); + + return sendTransaction(instructions, this.connection, this.wallet); } public async unstakeFromGovernance( @@ -532,16 +542,26 @@ export class PythStakingClient { publisher: PublicKey, amount: bigint, ) { - const instruction = await this.integrityPoolProgram.methods - .delegate(convertBigIntToBN(amount)) - .accounts({ - owner: this.wallet.publicKey, - publisher, - stakeAccountPositions, - }) - .instruction(); + const instructions = []; - return sendTransaction([instruction], this.connection, this.wallet); + if (!(await this.hasJoinedDaoLlc(stakeAccountPositions))) { + instructions.push( + await this.getJoinDaoLlcInstruction(stakeAccountPositions), + ); + } + + instructions.push( + await this.integrityPoolProgram.methods + .delegate(convertBigIntToBN(amount)) + .accounts({ + owner: this.wallet.publicKey, + publisher, + stakeAccountPositions, + }) + .instruction(), + ); + + return sendTransaction(instructions, this.connection, this.wallet); } public async getUnlockSchedule( @@ -821,6 +841,36 @@ export class PythStakingClient { .instruction(); } + public async hasJoinedDaoLlc( + stakeAccountPositions: PublicKey, + ): Promise { + const config = await this.getGlobalConfig(); + const stakeAccountMetadataAddress = getStakeAccountMetadataAddress( + stakeAccountPositions, + ); + const stakeAccountMetadata = + await this.stakingProgram.account.stakeAccountMetadataV2.fetch( + stakeAccountMetadataAddress, + ); + + return ( + JSON.stringify(stakeAccountMetadata.signedAgreementHash) === + JSON.stringify(config.agreementHash) + ); + } + + public async getJoinDaoLlcInstruction( + stakeAccountPositions: PublicKey, + ): Promise { + const config = await this.getGlobalConfig(); + return this.stakingProgram.methods + .joinDaoLlc(config.agreementHash) + .accounts({ + stakeAccountPositions, + }) + .instruction(); + } + public async getMainStakeAccount(owner?: PublicKey) { const stakeAccountPositions = await this.getAllStakeAccountPositions(owner); const currentEpoch = await getCurrentEpoch(this.connection);