Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
64.10% |
25 / 39 |
|
72.73% |
8 / 11 |
CRAP | |
0.00% |
0 / 1 |
| Connection | |
64.10% |
25 / 39 |
|
72.73% |
8 / 11 |
25.41 | |
0.00% |
0 / 1 |
| getAccountInfo | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| getBalance | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getConfirmedTransaction | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getTransaction | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getRecentBlockhash | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getLatestBlockhash | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| sendTransaction | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
3 | |||
| simulateTransaction | |
85.71% |
6 / 7 |
|
0.00% |
0 / 1 |
2.01 | |||
| requestAirdrop | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getProgramAccounts | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
2 | |||
| getMinimumBalanceForRentExemption | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Attestto\SolanaPhpSdk; |
| 4 | |
| 5 | use Attestto\SolanaPhpSdk\Exceptions\AccountNotFoundException; |
| 6 | use Attestto\SolanaPhpSdk\Exceptions\GenericException; |
| 7 | use Attestto\SolanaPhpSdk\Exceptions\InvalidIdResponseException; |
| 8 | use Attestto\SolanaPhpSdk\Exceptions\MethodNotFoundException; |
| 9 | use Attestto\SolanaPhpSdk\Util\Commitment; |
| 10 | use Illuminate\Http\Client\Response; |
| 11 | use Psr\Http\Client\ClientExceptionInterface; |
| 12 | use SodiumException; |
| 13 | |
| 14 | /** |
| 15 | * Class Connection |
| 16 | * @package Attestto\SolanaPhpSdk |
| 17 | * https://solana-labs.github.io/solana-web3.js/v1.x/classes/Connection.html |
| 18 | */ |
| 19 | class Connection extends Program |
| 20 | { |
| 21 | /** |
| 22 | * @param string $pubKey |
| 23 | * @return array |
| 24 | */ |
| 25 | public function getAccountInfo(string $pubKey): array |
| 26 | { |
| 27 | $accountResponse = $this->client->call('getAccountInfo', [$pubKey, ["encoding" => "base64"]])['value']; |
| 28 | |
| 29 | if (! $accountResponse) { |
| 30 | throw new AccountNotFoundException("API Error: Account {$pubKey} not found."); |
| 31 | } |
| 32 | |
| 33 | return $accountResponse; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * @param string $pubKey |
| 38 | * @return float |
| 39 | */ |
| 40 | public function getBalance(string $pubKey): float |
| 41 | { |
| 42 | return $this->client->call('getBalance', [$pubKey])['value']; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * @param string $transactionSignature |
| 47 | * @return array|null |
| 48 | */ |
| 49 | public function getConfirmedTransaction(string $transactionSignature): array|null |
| 50 | { |
| 51 | return $this->client->call('getConfirmedTransaction', [$transactionSignature]); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * NEW: This method is only available in solana-core v1.7 or newer. Please use getConfirmedTransaction for solana-core v1.6 |
| 56 | * |
| 57 | * @param string $transactionSignature |
| 58 | * @return array|null |
| 59 | */ |
| 60 | public function getTransaction(string $transactionSignature): array|null |
| 61 | { |
| 62 | return $this->client->call('getTransaction', [$transactionSignature]); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * @param Commitment|null $commitment |
| 67 | * @return array |
| 68 | * @throws Exceptions\GenericException|Exceptions\MethodNotFoundException|Exceptions\InvalidIdResponseException |
| 69 | * Deprecated: Use getLatestBlockhash instead |
| 70 | */ |
| 71 | public function getRecentBlockhash(?Commitment $commitment = null): array |
| 72 | { |
| 73 | return $this->client->call('getLatestBlockhash', array_filter([$commitment]))['value']; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * @param string|null $commitment |
| 78 | * @return array |
| 79 | * @throws Exceptions\GenericException|Exceptions\MethodNotFoundException|Exceptions\InvalidIdResponseException|\Psr\Http\Client\ClientExceptionInterface |
| 80 | */ |
| 81 | public function getLatestBlockhash(?Commitment $commitment = null): array |
| 82 | { |
| 83 | return $this->client->call('getLatestBlockhash', array_filter([$commitment]))['value']; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * @param Transaction $transaction |
| 88 | * @param Keypair[] $signers |
| 89 | * @param array $params |
| 90 | |
| 91 | * @throws GenericException |
| 92 | * @throws InvalidIdResponseException |
| 93 | * @throws MethodNotFoundException |
| 94 | * @throws ClientExceptionInterface |
| 95 | * @throws SodiumException |
| 96 | * TODO Add Support for Versioned TXns |
| 97 | */ |
| 98 | public function sendTransaction(Transaction $transaction, array $signers, array $params = []) |
| 99 | { |
| 100 | if (! $transaction->recentBlockhash) { |
| 101 | $transaction->recentBlockhash = $this->getLatestBlockhash()['blockhash']; |
| 102 | } |
| 103 | $transaction->sign(...$signers); |
| 104 | |
| 105 | $rawBinaryString = $transaction->serialize(false); |
| 106 | |
| 107 | $hashString = sodium_bin2base64($rawBinaryString, SODIUM_BASE64_VARIANT_ORIGINAL); |
| 108 | |
| 109 | $send_params = ['encoding' => 'base64', 'preflightCommitment' => 'confirmed']; |
| 110 | |
| 111 | foreach ($params as $k=>$v) |
| 112 | $send_params[$k] = $v; |
| 113 | |
| 114 | return $this->client->call('sendTransaction', [$hashString, $send_params]); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * @param Transaction $transaction |
| 119 | * @param Keypair[] $signers |
| 120 | * @param array $params |
| 121 | * @return array|Response |
| 122 | * @throws Exceptions\GenericException |
| 123 | * @throws Exceptions\InvalidIdResponseException |
| 124 | * @throws Exceptions\MethodNotFoundException |
| 125 | */ |
| 126 | public function simulateTransaction(Transaction $transaction, array $signers, array $params = []) |
| 127 | { |
| 128 | $transaction->sign(...$signers); |
| 129 | |
| 130 | $rawBinaryString = $transaction->serialize(false); |
| 131 | |
| 132 | $hashString = sodium_bin2base64($rawBinaryString, SODIUM_BASE64_VARIANT_ORIGINAL); |
| 133 | |
| 134 | $send_params = ['encoding' => 'base64', 'commitment' => 'confirmed', 'sigVerify'=>true]; |
| 135 | |
| 136 | foreach ($params as $k=>$v) |
| 137 | $send_params[$k] = $v; |
| 138 | |
| 139 | return $this->client->call('simulateTransaction', [$hashString, $send_params]); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * @param string $pubKey |
| 144 | * @param array $params |
| 145 | * @return string |
| 146 | */ |
| 147 | public function requestAirdrop(array $params = []): string |
| 148 | { |
| 149 | return $response = $this->client->call('requestAirdrop', $params ); |
| 150 | |
| 151 | } |
| 152 | // https://solana.com/docs/rpc/http/getprogramaccounts |
| 153 | // https://sns.guide/domain-name/all-domains.html |
| 154 | public function getProgramAccounts(string $programIdBs58, $dataSlice, $filters) |
| 155 | { |
| 156 | $params = [ |
| 157 | $programIdBs58, |
| 158 | [ |
| 159 | 'dataSlice' => $dataSlice, |
| 160 | 'filters' => $filters, |
| 161 | 'dataSize' => 108, // 'dataSize' => 108 |
| 162 | 'encoding' => 'base64', |
| 163 | 'page' => 1, |
| 164 | 'limit' => 1000 |
| 165 | |
| 166 | ], |
| 167 | |
| 168 | |
| 169 | ]; |
| 170 | return $this->client->call('getProgramAccounts', $params ); |
| 171 | //return $this->client->call('getAssetsByOwner', $params ); |
| 172 | |
| 173 | } |
| 174 | |
| 175 | public function getMinimumBalanceForRentExemption(int $space = 1024){ |
| 176 | return $this->client->call('getMinimumBalanceForRentExemption', [$space] ); |
| 177 | } |
| 178 | |
| 179 | } |