|
8 | 8 | } from "@solana/spl-governance";
|
9 | 9 | import {
|
10 | 10 | type Account,
|
| 11 | + createAssociatedTokenAccountInstruction, |
11 | 12 | createTransferInstruction,
|
12 | 13 | getAccount,
|
13 | 14 | getAssociatedTokenAddress,
|
@@ -477,21 +478,38 @@ export class PythStakingClient {
|
477 | 478 | ) {
|
478 | 479 | const globalConfig = await this.getGlobalConfig();
|
479 | 480 | const mint = globalConfig.pythTokenMint;
|
| 481 | + const instructions = []; |
480 | 482 |
|
481 | 483 | const receiverTokenAccount = await getAssociatedTokenAddress(
|
482 | 484 | mint,
|
483 | 485 | this.wallet.publicKey,
|
484 | 486 | );
|
485 | 487 |
|
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 | + } |
493 | 501 |
|
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); |
495 | 513 | }
|
496 | 514 |
|
497 | 515 | public async stakeToPublisher(
|
|
0 commit comments