Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
93.75% covered (success)
93.75%
15 / 16
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
SplTokenProgram
93.75% covered (success)
93.75%
15 / 16
50.00% covered (danger)
50.00%
1 / 2
4.00
0.00% covered (danger)
0.00%
0 / 1
 getTokenAccountsByOwner
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
 getAssociatedTokenAddressSync
85.71% covered (warning)
85.71%
6 / 7
0.00% covered (danger)
0.00%
0 / 1
3.03
1<?php
2
3namespace Attestto\SolanaPhpSdk\Programs;
4
5
6use Attestto\SolanaPhpSdk\Exceptions\InputValidationException;
7use Attestto\SolanaPhpSdk\Exceptions\TokenOwnerOffCurveError;
8use Attestto\SolanaPhpSdk\Program;
9use Attestto\SolanaPhpSdk\Programs\SplToken\Actions\SPLTokenActions;
10use Attestto\SolanaPhpSdk\Programs\SplToken\Instructions\SPLTokenInstructions;
11use Attestto\SolanaPhpSdk\PublicKey;
12
13
14/**
15 * @property $SOLANA_TOKEN_PROGRAM_ID
16 */
17class SplTokenProgram extends Program
18{
19    public const TOKEN_PROGRAM_ID = 'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA';
20    public const NATIVE_MINT = 'So11111111111111111111111111111111111111112';
21    public const ASSOCIATED_TOKEN_PROGRAM_ID = 'ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL';
22    public const TOKEN_2022_PROGRAM_ID ='TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb';
23    public const TOKEN_2022_MINT = '9pan9bMn5HatX4EJdBwg9VgCa7Uz5HL8N1m5D3NdXejP';
24
25    use SPLTokenActions;
26    use SPLTokenInstructions;
27
28    /**
29     * @param string $pubKey
30     * @return mixed
31     */
32    public function getTokenAccountsByOwner(string $pubKey)
33    {
34        return $this->client->call('getTokenAccountsByOwner', [
35            $pubKey,
36            [
37                'programId' => self::TOKEN_PROGRAM_ID,
38            ],
39            [
40                'encoding' => 'jsonParsed',
41            ],
42        ]);
43    }
44
45    /**
46     * @param PublicKey $mint
47     * @param PublicKey $owner
48     * @param bool $allowOwnerOffCurve
49     * @param string|null $programId
50     * @param null $associatedTokenProgramId
51     * @return PublicKey
52     * @throws TokenOwnerOffCurveError
53     * @throws InputValidationException
54     */
55    public function getAssociatedTokenAddressSync(
56        PublicKey $mint,
57        PublicKey $owner,
58        bool      $allowOwnerOffCurve = false,
59        PublicKey    $programId = new PublicKey(self::TOKEN_PROGRAM_ID),
60                  PublicKey $atPid = new PublicKey(self::ASSOCIATED_TOKEN_PROGRAM_ID)
61    ): PublicKey {
62        if (!$allowOwnerOffCurve && !PublicKey::isOnCurve($owner->toBinaryString())) {
63            throw new TokenOwnerOffCurveError();
64        }
65
66
67
68        $address = PublicKey::findProgramAddressSync(
69            [$owner->toBuffer(), $programId->toBuffer(), $mint->toBuffer()],
70            $atPid
71        );
72
73        return $address[0];
74    }
75
76
77
78
79}