Skip to content

Commit 9c73bbb

Browse files
authored
Merge pull request #1958 from pyth-network/ata
feat(staking): create ata upon withdrawal if needed
2 parents bc39480 + 216d016 commit 9c73bbb

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

governance/pyth_staking_sdk/src/pyth-staking-client.ts

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
} from "@solana/spl-governance";
99
import {
1010
type Account,
11+
createAssociatedTokenAccountInstruction,
1112
createTransferInstruction,
1213
getAccount,
1314
getAssociatedTokenAddress,
@@ -477,21 +478,38 @@ export class PythStakingClient {
477478
) {
478479
const globalConfig = await this.getGlobalConfig();
479480
const mint = globalConfig.pythTokenMint;
481+
const instructions = [];
480482

481483
const receiverTokenAccount = await getAssociatedTokenAddress(
482484
mint,
483485
this.wallet.publicKey,
484486
);
485487

486-
const instruction = await this.stakingProgram.methods
487-
.withdrawStake(new BN(amount.toString()))
488-
.accounts({
489-
destination: receiverTokenAccount,
490-
stakeAccountPositions,
491-
})
492-
.instruction();
488+
// Edge case: if the user doesn't have an ATA, create one
489+
try {
490+
await this.getOwnerPythAtaAccount();
491+
} catch {
492+
instructions.push(
493+
createAssociatedTokenAccountInstruction(
494+
this.wallet.publicKey,
495+
receiverTokenAccount,
496+
this.wallet.publicKey,
497+
mint,
498+
),
499+
);
500+
}
493501

494-
return sendTransaction([instruction], this.connection, this.wallet);
502+
instructions.push(
503+
await this.stakingProgram.methods
504+
.withdrawStake(new BN(amount.toString()))
505+
.accounts({
506+
destination: receiverTokenAccount,
507+
stakeAccountPositions,
508+
})
509+
.instruction(),
510+
);
511+
512+
return sendTransaction(instructions, this.connection, this.wallet);
495513
}
496514

497515
public async stakeToPublisher(

0 commit comments

Comments
 (0)