Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
47.22% covered (danger)
47.22%
17 / 36
28.57% covered (danger)
28.57%
2 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
SystemProgram
47.22% covered (danger)
47.22%
17 / 36
28.57% covered (danger)
28.57%
2 / 7
17.41
0.00% covered (danger)
0.00%
0 / 1
 programId
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getAccountInfo
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
2.06
 getBalance
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getConfirmedTransaction
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getTransaction
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 transfer
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
1
 createAccount
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace Attestto\SolanaPhpSdk\Programs;
4
5use Attestto\SolanaPhpSdk\Exceptions\AccountNotFoundException;
6use Attestto\SolanaPhpSdk\Program;
7use Attestto\SolanaPhpSdk\PublicKey;
8use Attestto\SolanaPhpSdk\TransactionInstruction;
9use Attestto\SolanaPhpSdk\Util\AccountMeta;
10
11class SystemProgram extends Program
12{
13    const PROGRAM_INDEX_CREATE_ACCOUNT = 0;
14    const PROGRAM_INDEX_TRANSFER = 2;
15    const PROGRAM_ID = '11111111111111111111111111111111';
16
17    /**
18     * Public key that identifies the System program
19     *
20     * @return PublicKey
21     */
22    static function programId(): PublicKey
23    {
24        return new PublicKey('11111111111111111111111111111111');
25    }
26
27    /**
28     * @param string $pubKey
29     * @return array
30     */
31    public function getAccountInfo(string $pubKey): array
32    {
33        $accountResponse = $this->client->call('getAccountInfo', [$pubKey, ["encoding" => "jsonParsed"]])['value'];
34
35        if (! $accountResponse) {
36            throw new AccountNotFoundException("API Error: Account {$pubKey} not found.");
37        }
38
39        return $accountResponse;
40    }
41
42    /**
43     * @param string $pubKey
44     * @return float
45     */
46    public function getBalance(string $pubKey): float
47    {
48        return $this->client->call('getBalance', [$pubKey])['value'];
49    }
50
51    /**
52     * @param string $transactionSignature
53     * @return array
54     */
55    public function getConfirmedTransaction(string $transactionSignature): array
56    {
57        return $this->client->call('getConfirmedTransaction', [$transactionSignature]);
58    }
59
60    /**
61     * NEW: This method is only available in solana-core v1.7 or newer. Please use getConfirmedTransaction for solana-core v1.6
62     *
63     * @param string $transactionSignature
64     * @return array
65     */
66    public function getTransaction(string $transactionSignature): array
67    {
68        return $this->client->call('getTransaction', [$transactionSignature]);
69    }
70
71    /**
72     * Generate a transaction instruction that transfers lamports from one account to another
73     *
74     * @param PublicKey $fromPubkey
75     * @param PublicKey $toPublicKey
76     * @param int $lamports
77     * @return TransactionInstruction
78     */
79    static public function transfer(
80        PublicKey $fromPubkey,
81        PublicKey $toPublicKey,
82        int $lamports
83    ): TransactionInstruction
84    {
85        // 4 byte instruction index + 8 bytes lamports
86        // look at https://www.php.net/manual/en/function.pack.php for formats.
87        $data = [
88            // uint32
89            ...unpack("C*", pack("V", self::PROGRAM_INDEX_TRANSFER)),
90            // int64
91            ...unpack("C*", pack("P", $lamports)),
92        ];
93        $keys = [
94            new AccountMeta($fromPubkey, true, true),
95            new AccountMeta($toPublicKey, false, true),
96        ];
97
98        return new TransactionInstruction(
99            static::programId(),
100            $keys,
101            $data
102        );
103    }
104
105    static public function createAccount(
106        PublicKey $fromPubkey,
107        PublicKey $newAccountPublicKey,
108        int $lamports,
109        int $space,
110        PublicKey $programId
111    ): TransactionInstruction
112    {
113        // look at https://www.php.net/manual/en/function.pack.php for formats.
114        $data = [
115            // uint32
116            ...unpack("C*", pack("V", self::PROGRAM_INDEX_CREATE_ACCOUNT)),
117            // int64
118            ...unpack("C*", pack("P", $lamports)),
119            // int64
120            ...unpack("C*", pack("P", $space)),
121            //
122            ...$programId->toBytes(),
123        ];
124        $keys = [
125            new AccountMeta($fromPubkey, true, true),
126            new AccountMeta($newAccountPublicKey, true, true),
127        ];
128
129        return new TransactionInstruction(
130            static::programId(),
131            $keys,
132            $data
133        );
134    }
135}