PHP code example of sandermuller / laravel-solana-sdk
1. Go to this page and download the library: Download sandermuller/laravel-solana-sdk library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
sandermuller / laravel-solana-sdk example snippets
use SanderMuller\LaravelSolanaSdk\Facades\Solana;
$balance = Solana::getBalance('SomeWalletAddressBase58'); // lamports as float
$blockhash = Solana::latestBlockhash(); // typed BlockhashInfo
$status = Solana::sendAndConfirmTransaction($tx, [$payer]);
use SanderMuller\LaravelSolanaSdk\Facades\Solana;
use SanderMuller\LaravelSolanaSdk\Facades\SolanaRpc;
$balance = Solana::getBalance('SomeWalletAddressBase58');
$info = Solana::accountInfo('SomeWalletAddressBase58'); // typed AccountInfo
$blockhash = Solana::latestBlockhash(); // typed BlockhashInfo
// Send + poll until confirmed in a single call:
$status = Solana::sendAndConfirmTransaction($tx, [$payer]);
// Low-level JSON-RPC escape hatch
$result = SolanaRpc::call('getSlot');
use SanderMuller\SolanaPhpSdk\Connection;
class WalletController
{
public function show(Connection $solana, string $address): array
{
return ['balance' => $solana->getBalance($address)];
}
}
use SanderMuller\SolanaPhpSdk\TransactionBuilder;
use SanderMuller\SolanaPhpSdk\Programs\SystemProgram;
use SanderMuller\LaravelSolanaSdk\Facades\Solana;
$blockhash = Solana::latestBlockhash();
$tx = TransactionBuilder::make()
->feePayer($payer->publicKey)
->recentBlockhash($blockhash)
->addInstruction(SystemProgram::transfer($payer->publicKey, $to, $lamports))
->addSigner($payer)
->build();
$status = Solana::sendAndConfirmTransaction($tx, [$payer]);
use SanderMuller\SolanaPhpSdk\Services\SolanaPubSubClient;
class WatchSignatures
{
public function handle(SolanaPubSubClient $pubsub, string $signature): void
{
$pubsub->enableAutoReconnect();
$pubsub->signatureSubscribe($signature);
foreach ($pubsub->listen() as $event) {
// …
}
}
}
use SanderMuller\LaravelSolanaSdk\Facades\Solana;
it('reads the balance', function (): void {
Solana::fake()->script([
'getBalance' => ['value' => 5_000_000],
]);
expect(Solana::getBalance($address))->toBe(5_000_000.0);
expect(Solana::fakedStub()?->methodCalls())->toContain('getBalance');
});
use SanderMuller\SolanaPhpSdk\Testing\PestExpectations;
PestExpectations::register();