Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
Merge pull request #6 from exzachlyvv/exzachlyvv/check-for-missing-ac…
Browse files Browse the repository at this point in the history
…count

Check for missing account data
  • Loading branch information
mattstauffer authored Oct 11, 2021
2 parents f99c500 + 1ffb728 commit 0021f09
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 2 deletions.
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@
],
"require": {
"php": "^7.4 || ~8.0",
"guzzlehttp/guzzle": "^7.3",
"illuminate/http": "~8.0"
},
"require-dev": {
"mockery/mockery": "^1.4",
"orchestra/testbench": "^6.0",
"phpunit/phpunit": "^9.0",
"squizlabs/php_codesniffer": "^3.5",
"tightenco/tlint": "^5.0"
Expand Down
10 changes: 10 additions & 0 deletions src/Exceptions/AccountNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Tighten\SolanaPhpSdk\Exceptions;

use Exception;

class AccountNotFoundException extends Exception
{

}
10 changes: 9 additions & 1 deletion src/Solana.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Tighten\SolanaPhpSdk;

use Tighten\SolanaPhpSdk\Exceptions\AccountNotFoundException;

class Solana
{
public const solanaTokenProgramId = 'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA';
Expand All @@ -16,7 +18,13 @@ public function __construct(SolanaRpcClient $client)

public function getAccountInfo(string $pubKey): array
{
return $this->client->call('getAccountInfo', [$pubKey, ["encoding" => "jsonParsed"]])->json()['result']['value'];
$accountResponse = $this->client->call('getAccountInfo', [$pubKey, ["encoding" => "jsonParsed"]])->json()['result']['value'];

if (! $accountResponse) {
throw new AccountNotFoundException("API Error: Account {$pubKey} not found.");
}

return $accountResponse;
}

public function getBalance(string $pubKey): float
Expand Down
8 changes: 8 additions & 0 deletions src/SolanaRpcClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,12 @@ protected function validateResponse(Response $response, string $method, array $p
throw new GenericException('API Error: status code ' . $response->getStatusCode());
}
}

/**
* @return int
*/
public function getRandomKey(): int
{
return $this->randomKey;
}
}
26 changes: 25 additions & 1 deletion tests/SolanaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
namespace Tighten\SolanaPhpSdk\Tests;

use Illuminate\Http\Client\Response;
use Illuminate\Support\Facades\Http;
use Mockery as M;
use PHPUnit\Framework\TestCase;
use Tighten\SolanaPhpSdk\Exceptions\AccountNotFoundException;
use Tighten\SolanaPhpSdk\Solana;
use Tighten\SolanaPhpSdk\SolanaRpcClient;

Expand All @@ -27,6 +28,29 @@ public function it_passes_undefined_calls_through_magically()
$this->assertTrue(true); // Keep PHPUnit from squawking; there must be a better way?
}

/** @test */
public function it_will_throw_exception_when_rpc_account_response_is_null()
{
$client = new SolanaRpcClient(SolanaRpcClient::DEVNET_ENDPOINT);
$expectedIdInHttpResponse = $client->getRandomKey();
$solana = new Solana($client);
Http::fake([
SolanaRpcClient::DEVNET_ENDPOINT => Http::response([
'jsonrpc' => '2.0',
'result' => [
'context' => [
'slot' => 6440
],
'value' => null, // no account data.
],
'id' => $expectedIdInHttpResponse,
]),
]);

$this->expectException(AccountNotFoundException::class);
$solana->getAccountInfo('abc123');
}

protected function fakeResponse(): Response
{
return new Response(new class
Expand Down
10 changes: 10 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Tighten\SolanaPhpSdk\Tests;

use Orchestra\Testbench\TestCase as Orchestra;

class TestCase extends Orchestra
{

}

0 comments on commit 0021f09

Please sign in to comment.