Skip to content

Commit

Permalink
fix: join llc before delegating (#1978)
Browse files Browse the repository at this point in the history
* go

* cleanup

* ci

* go

* refactor
  • Loading branch information
guibescos committed Sep 27, 2024
1 parent fa13b92 commit 275248e
Showing 1 changed file with 71 additions and 21 deletions.
92 changes: 71 additions & 21 deletions governance/pyth_staking_sdk/src/pyth-staking-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -572,16 +582,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(
Expand Down Expand Up @@ -861,6 +881,36 @@ export class PythStakingClient {
.instruction();
}

public async hasJoinedDaoLlc(
stakeAccountPositions: PublicKey,
): Promise<boolean> {
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<TransactionInstruction> {
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);
Expand Down

0 comments on commit 275248e

Please sign in to comment.