| Code Coverage
| Lines | Functions and Methods | Classes and Traits
Total | 0.00% covered (danger) | 0.00% | 0 / 32 | 0.00% covered (danger) | 0.00% | 0 / 2 | CRAP | 0.00% covered (danger) | 0.00% | 0 / 1
SPLToken | 0.00% covered (danger) | 0.00% | 0 / 32 | 0.00% covered (danger) | 0.00% | 0 / 2 | 110 | 0.00% covered (danger) | 0.00% | 0 / 1
getOrCreateAssociatedTokenAccount | 0.00% covered (danger) | 0.00% | 0 / 31 | 0.00% covered (danger) | 0.00% | 0 / 1 | 90 |
createAssociatedTokenAccountInstruction | 0.00% covered (danger) | 0.00% | 0 / 1 | 0.00% covered (danger) | 0.00% | 0 / 1 | 2 |

1 | <?php
2 |
3 | namespace Attestto\SolanaPhpSdk\Programs\SplToken;
4 |
5 | use Attestto\SolanaPhpSdk\Connection;
6 | use Attestto\SolanaPhpSdk\Exceptions\TokenAccountNotFoundError;
7 | use Attestto\SolanaPhpSdk\Exceptions\TokenInvalidAccountOwnerError;
8 | use Attestto\SolanaPhpSdk\Exceptions\TokenInvalidMintError;
9 | use Attestto\SolanaPhpSdk\Exceptions\TokenOwnerOffCurveError;
10 | use Attestto\SolanaPhpSdk\PublicKey;
11 | use Attestto\SolanaPhpSdk\Transaction;
12 | use Attestto\SolanaPhpSdk\Util\Commitment;
13 | use Attestto\SolanaPhpSdk\Util\ConfirmOptions;
14 | use Attestto\SolanaPhpSdk\Util\Signer;
15 | use Exception;
16 |
17 | trait SPLToken {
18 |
19 | /**
20 | * @param Connection $connection
21 | * @param Signer $payer
22 | * @param PublicKey $mint
23 | * @param PublicKey $owner
24 | * @param false $allowOwnerOffCurve
25 | * @param Commitment|null $commitment
26 | * @param ConfirmOptions|null $confirmOptions
27 | * @param $programId
28 | * @param $associatedTokenProgramId
29 | * @return mixed
30 | * @throws Exception
31 | */
32 | public function getOrCreateAssociatedTokenAccount(
33 | Connection $connection,
34 | Signer $payer,
35 | PublicKey $mint,
36 | PublicKey $owner,
37 | false $allowOwnerOffCurve = false,
38 | Commitment $commitment = null,
39 | ConfirmOptions $confirmOptions = null,
40 | $programId = null,
41 | $associatedTokenProgramId = null
42 | ): mixed
43 | {
44 | if (!$programId){
45 | $programId = $this->SOLANA_TOKEN_PROGRAM_ID;
46 | }
47 | if (!$associatedTokenProgramId){
48 | $associatedTokenProgramId = $this->SOLANA_TOKEN_PROGRAM_ID;
49 | }
50 |
51 |
52 | $associatedToken = $this->getAssociatedTokenAddressSync(
53 | $mint,
54 | $owner,
55 | $allowOwnerOffCurve,
56 | $programId,
57 | $associatedTokenProgramId
58 | );
59 |
60 | try {
61 | $account = parent::getAccount($connection, $associatedToken, $commitment, $programId);
62 | } catch (Exception $error) {
63 | if ($error instanceof TokenAccountNotFoundError || $error instanceof TokenInvalidAccountOwnerError) {
64 | try {
65 | $transaction = new Transaction();
66 | $transaction->add(
67 | $this->createAssociatedTokenAccountInstruction(
68 | $payer->publicKey,
69 | $associatedToken,
70 | $owner,
71 | $mint,
72 | $programId,
73 | $associatedTokenProgramId
74 | )
75 | );
76 | // TODO Send and confirm transaction
77 | //sendAndConfirmTransaction($connection, $transaction, [$payer], $confirmOptions);
78 | } catch (Exception $error) {
79 | // Ignore all errors
80 | }
81 |
82 | $account = getAccount($connection, $associatedToken, $commitment, $programId);
83 | } else {
84 | throw $error;
85 | }
86 | }
87 |
88 | if (!$account->mint->equals($mint)) throw new TokenInvalidMintError();
89 | if (!$account->owner->equals($owner)) throw new TokenInvalidAccountOwnerError();
90 |
91 | return $account;
92 | }
93 |
94 | public function createAssociatedTokenAccountInstruction(PublicKey $publicKey, $associatedToken, PublicKey $owner, PublicKey $mint, mixed $programId, mixed $associatedTokenProgramId): true
95 | {
96 | return true;
97 | }
98 |
99 | /**
100 | * @param PublicKey $mint
101 | * @param PublicKey $owner
102 | * @param bool $allowOwnerOffCurve
103 | * @param $programId
104 | * @param $associatedTokenProgramId
105 | * @return PublicKey
106 | */
107 |
108 |
109 |
110 | }