Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
94.29% |
33 / 35 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| SPLTokenInstructions | |
94.29% |
33 / 35 |
|
66.67% |
2 / 3 |
5.00 | |
0.00% |
0 / 1 |
| createAssociatedTokenAccountInstruction | |
84.62% |
11 / 13 |
|
0.00% |
0 / 1 |
3.03 | |||
| buildAssociatedTokenAccountInstruction | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
1 | |||
| createSyncNativeInstruction | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Attestto\SolanaPhpSdk\Programs\SplToken\Instructions; |
| 4 | |
| 5 | use Attestto\SolanaPhpSdk\Exceptions\InputValidationException; |
| 6 | use Attestto\SolanaPhpSdk\Programs\SystemProgram; |
| 7 | use Attestto\SolanaPhpSdk\PublicKey; |
| 8 | use Attestto\SolanaPhpSdk\TransactionInstruction; |
| 9 | use Attestto\SolanaPhpSdk\Util\AccountMeta; |
| 10 | use Attestto\SolanaPhpSdk\Util\Buffer; |
| 11 | |
| 12 | trait SPLTokenInstructions |
| 13 | { |
| 14 | |
| 15 | /** |
| 16 | * @param PublicKey $payer |
| 17 | * @param PublicKey $associatedToken |
| 18 | * @param PublicKey $owner |
| 19 | * @param PublicKey $mint |
| 20 | * @param null $programId |
| 21 | * @param null $associatedTokenProgramId |
| 22 | * @return TransactionInstruction |
| 23 | * @throws InputValidationException |
| 24 | */ |
| 25 | public function createAssociatedTokenAccountInstruction( |
| 26 | PublicKey $payer, |
| 27 | PublicKey $associatedToken, |
| 28 | PublicKey $owner, |
| 29 | PublicKey $mint, |
| 30 | $programId = null, |
| 31 | $associatedTokenProgramId = null |
| 32 | ): TransactionInstruction |
| 33 | { |
| 34 | if (!$programId) { |
| 35 | $programId = $this->SOLANA_TOKEN_PROGRAM_ID; |
| 36 | } |
| 37 | if (!$associatedTokenProgramId) { |
| 38 | $associatedTokenProgramId = $this->SOLANA_TOKEN_PROGRAM_ID; |
| 39 | } |
| 40 | |
| 41 | return $this->buildAssociatedTokenAccountInstruction( |
| 42 | $payer, |
| 43 | $associatedToken, |
| 44 | $owner, |
| 45 | $mint, |
| 46 | new Buffer([]), |
| 47 | $programId, |
| 48 | $associatedTokenProgramId |
| 49 | ); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * @param PublicKey $payer |
| 54 | * @param PublicKey $associatedToken |
| 55 | * @param PublicKey $owner |
| 56 | * @param PublicKey $mint |
| 57 | * @param Buffer $instructionData |
| 58 | * @param string|PublicKey|null $programId |
| 59 | * @param string|PublicKey|null $associatedTokenProgramId |
| 60 | * @return TransactionInstruction |
| 61 | */ |
| 62 | public function buildAssociatedTokenAccountInstruction( |
| 63 | PublicKey $payer, |
| 64 | PublicKey $associatedToken, |
| 65 | PublicKey $owner, |
| 66 | PublicKey $mint, |
| 67 | Buffer $instructionData, |
| 68 | string|PublicKey|null $programId = new PublicKey(self::TOKEN_PROGRAM_ID), |
| 69 | string|PublicKey|null $associatedTokenProgramId = new PublicKey(self::ASSOCIATED_TOKEN_PROGRAM_ID) |
| 70 | ): TransactionInstruction |
| 71 | { |
| 72 | |
| 73 | $keys = [ |
| 74 | new AccountMeta($payer, true, true), |
| 75 | new AccountMeta($associatedToken, false, true), |
| 76 | new AccountMeta($owner, false, false), |
| 77 | new AccountMeta($mint, false, false), |
| 78 | new AccountMeta(SystemProgram::programId(), false, false), |
| 79 | new AccountMeta($programId, false, false), |
| 80 | ]; |
| 81 | |
| 82 | |
| 83 | return new TransactionInstruction( |
| 84 | $associatedTokenProgramId, |
| 85 | $keys, |
| 86 | $instructionData |
| 87 | ); |
| 88 | } |
| 89 | |
| 90 | |
| 91 | /** |
| 92 | * @throws InputValidationException |
| 93 | */ |
| 94 | function createSyncNativeInstruction(PublicKey $owner, string $programId = self::TOKEN_PROGRAM_ID): TransactionInstruction |
| 95 | { |
| 96 | $keys = [ |
| 97 | new AccountMeta($owner, false, true), |
| 98 | ]; |
| 99 | $data = str_repeat("\0", TokenInstruction::SyncNative); |
| 100 | return new TransactionInstruction( |
| 101 | new PublicKey($programId), |
| 102 | $keys, |
| 103 | $data |
| 104 | ); |
| 105 | } |
| 106 | } |