Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
AssociatedTokenAccount
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 2
42
0.00% covered (danger)
0.00%
0 / 1
 createAssociatedTokenAccountInstruction
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
12
 buildAssociatedTokenAccountInstruction
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3use Attestto\SolanaPhpSdk\Exceptions\InputValidationException;
4use Attestto\SolanaPhpSdk\Programs\SystemProgram;
5use Attestto\SolanaPhpSdk\PublicKey;
6use Attestto\SolanaPhpSdk\TransactionInstruction;
7use Attestto\SolanaPhpSdk\Util\Buffer;
8
9trait AssociatedTokenAccount
10{
11
12    /**
13     * @param PublicKey $payer
14     * @param PublicKey $associatedToken
15     * @param PublicKey $owner
16     * @param PublicKey $mint
17     * @param null $programId
18     * @param null $associatedTokenProgramId
19     * @return TransactionInstruction
20     * @throws InputValidationException
21     */
22    public function createAssociatedTokenAccountInstruction(
23        PublicKey $payer,
24        PublicKey $associatedToken,
25        PublicKey $owner,
26        PublicKey $mint,
27                  $programId = null,
28                  $associatedTokenProgramId = null
29    ): TransactionInstruction {
30        if (!$programId){
31            $programId = $this->SOLANA_TOKEN_PROGRAM_ID;
32        }
33        if (!$associatedTokenProgramId){
34            $associatedTokenProgramId = $this->SOLANA_TOKEN_PROGRAM_ID;
35        }
36
37        return $this->buildAssociatedTokenAccountInstruction(
38            $payer,
39            $associatedToken,
40            $owner,
41            $mint,
42            new Buffer([]),
43            $programId,
44            $associatedTokenProgramId
45        );
46    }
47
48    /**
49     * @param PublicKey $payer
50     * @param PublicKey $associatedToken
51     * @param PublicKey $owner
52     * @param PublicKey $mint
53     * @param Buffer    $instructionData
54     * @param string|null $programId
55     * @param string|null $associatedTokenProgramId
56     * @return TransactionInstruction
57     */
58    public function buildAssociatedTokenAccountInstruction(
59        PublicKey $payer,
60        PublicKey $associatedToken,
61        PublicKey $owner,
62        PublicKey $mint,
63        Buffer    $instructionData,
64                  $programId = null,
65                  $associatedTokenProgramId = null
66    ): TransactionInstruction
67    {
68        if (!$programId) {
69            $programId = $this->SOLANA_TOKEN_PROGRAM_ID;
70        }
71        if (!$associatedTokenProgramId) {
72            $associatedTokenProgramId = $this->SOLANA_TOKEN_PROGRAM_ID;
73        }
74
75        $keys = [
76            ['pubkey' => $payer, 'isSigner' => true, 'isWritable' => true],
77            ['pubkey' => $associatedToken, 'isSigner' => false, 'isWritable' => true],
78            ['pubkey' => $owner, 'isSigner' => false, 'isWritable' => false],
79            ['pubkey' => $mint, 'isSigner' => false, 'isWritable' => false],
80            ['pubkey' => SystemProgram::programId(), 'isSigner' => false, 'isWritable' => false],
81            ['pubkey' => $programId, 'isSigner' => false, 'isWritable' => false],
82        ];
83
84        return new TransactionInstruction(
85            $associatedTokenProgramId,
86            $keys,
87            $instructionData->data
88        );
89    }
90}