safebits / solana-php-sdk
Solana PHP SDK for interacting with the Solana blockchain
Requires
- php: ~8.2
- ext-sodium: *
- guzzlehttp/guzzle: ^7.3
- paragonie/sodium_compat: ^2.1
- stephenhill/base58: ^1.1
- vlucas/phpdotenv: ^5.6
Requires (Dev)
- mockery/mockery: ^1.4
- orchestra/testbench: ^9.0
- phpunit/php-code-coverage: ^10.0
- phpunit/phpunit: ^10.0
- squizlabs/php_codesniffer: ^3.5
- tightenco/tlint: *
This package is not auto-updated.
Last update: 2025-10-06 20:39:09 UTC
README
____ __ ______ _____ ____ __ ___ _ _____ _____ ____ __ __
/ __ \/ / / / __ \ / ___// __ \/ / / | / | / / | / ___// __ \/ //_/
/ /_/ / /_/ / /_/ / \__ \/ / / / / / /| | / |/ / /| | \__ \/ / / / ,<
/ ____/ __ / ____/ ___/ / /_/ / /___/ ___ |/ /| / ___ | ___/ / /_/ / /| |
/_/ /_/ /_/_/ /____/\____/_____/_/ |_/_/ |_/_/ |_| /____/_____/_/ |_|
Simple PHP SDK for interacting with the Transactions, Signatures, Borsh Serialization/Deserialization and RPCs
Forked from the Verze repo: verze-app/solana-php-sdk
Motivations
- To protect RPC API Keys acting as a Hosted Proxy.
- To enable Background Data Processing.
- To enable Descentralized Data & Query Caching.
- To empower PHP Applications to interact with the Solana Network
- To enable Async Jobs and Queries in the Background.
- To enable Decetralized Websockets (Push Notifications & Subscriptions)
- To reduce Client-Sice RPC Polling, aiming at decongesting the mainnet and devnets.
Installation
You can install the package via composer :
composer require attestto/solana-php-sdk
From this Repository
git clone https://github.com/Attestto-com/solana-php-sdk.git
cd solana-php-sdk
composer install
With Docker
docker build -t solana-php-sdk .
then
docker run -it solana-php-sdk /bin/bash
Usage
Using the Solana simple client
You can use the Connection
class for convenient access to API methods. Some are defined in the code:
use Safebits\SolanaPhpSdk\Connection;
use Safebits\SolanaPhpSdk\SolanaRpcClient;
// Using a defined method
$sdk = new Connection(new SolanaRpcClient(SolanaRpcClient::MAINNET_ENDPOINT));
$accountInfo = $sdk->getAccountInfo('4fYNw3dojWmQ4dXtSGE9epjRGy9pFSx62YypT7avPYvA');
var_dump($accountInfo);
For all the possible methods, see the API documentation.
Directly using the RPC client
The Connection
class is just a light convenience layer on top of the RPC client. You can, if you want, use the client directly, which allows you to work with the full Response
object:
use Safebits\SolanaPhpSdk\SolanaRpcClient;
$client = new SolanaRpcClient(SolanaRpcClient::MAINNET_ENDPOINT);
$accountInfoResponse = $client->call('getAccountInfo', ['4fYNw3dojWmQ4dXtSGE9epjRGy9pFSx62YypT7avPYvA']);
$accountInfoBody = $accountInfoResponse->json();
$accountInfoStatusCode = $accountInfoResponse->getStatusCode();
Transactions
Here is working example of sending a transfer instruction to the Solana blockchain, you may overrride the Endpoint with a custom RPC endpoint.:
$client = new SolanaRpcClient(SolanaRpcClient::DEVNET_ENDPOINT);
$connection = new Connection($client);
$fromPublicKey = KeyPair::fromSecretKey([...]);
$toPublicKey = new PublicKey('J3dxNj7nDRRqRRXuEMynDG57DkZK4jYRuv3Garmb1i99');
$instruction = SystemProgram::transfer(
$fromPublicKey->getPublicKey(),
$toPublicKey,
6
);
$transaction = new Transaction(null, null, $fromPublicKey->getPublicKey());
$transaction->add($instruction);
$txHash = $connection->sendTransaction($transaction, $fromPublicKey);
Borsh Deserialize & Deserialize
For Borsh serialization/deseralization to work, a class::SCHEMA object reflecting the Program Structs, based on the program IDL must be passed or defined. e.g.
class DidData
{
use BorshObject; //trait
public $keyData;
public const SCHEMA = [
VerificationMethodStruct::class => VerificationMethodStruct::SCHEMA[VerificationMethodStruct::class],
ServiceStruct::class => ServiceStruct::SCHEMA[ServiceStruct::class],
self::class => [
'kind' => 'struct',
'fields' => [
['offset', 'u64'],
['version', 'u8'],
['bump', 'u8'],
['nonce', 'u64'],
['initialVerificationMethod', 'string'],
['flags', 'u16'],
['methodType', 'u8'],
['keyData', ['u8']],
['verificationMethods', [VerificationMethodStruct::class]],
['services', [ServiceStruct::class]],
['nativeControllers', ['pubKey']],
['otherControllers', ['string']],
],
],
];
public static function fromBuffer(array $buffer): self
{
return Borsh::deserialize(self::SCHEMA, self::class, $buffer);
}
}
BORSH USAGE (PHP implementation)
To get a better understanding on the implementation and usage, please refer to the following references:
example usage (This will be improved, WIP):
/**
* deserializeDidData
*
* @param string $dataBase64 The base64 encoded data of the DID data account
* @return DidData The deserialized DID data object
* @example DidSolProgram::deserializeDidData('TVjvjfsd7fMA/gAAAA...');
*/
static function deserializeDidData($dataBase64)
{
$base64String = base64_decode($dataBase64);
$uint8Array = array_values(unpack('C*', $base64String));
$didData = DidData::fromBuffer($uint8Array); // See above code block
$keyData = $didData->keyData;
$binaryString = pack('C*', ...$keyData);
$b58 = new Base58();
$base58String = $b58->encode($binaryString);
$didData->keyData = $base58String;
return $didData;
}
Notes:
- Most of the Magic is done in the BorshDesealizable.php Trait.
- This project is in alpha, the code to generate instructions is still being worked on
$instruction = SystemProgram::abc()
- This project is maintained by a single dev, so any feedback, ideas, comments are appreciated.
Roadmap (WIP)
- Borsh serialize and deserialize. Done - Test(s) - Coverage
- Improved documentation. WIP - This document + Documentation Index (https://github.com/Attestto-com/solana-php-sdk/tree/master/docs)
Build out more of the Connection, Message, SystemProgram, TokenProgram, MetaplexProgram classes. WIP - Tests - Coverage
- [ ] Connection::class
- [x] getLatestBlokchash::class [Source] - [Test] - [Coverage]
- [ ] getMinimumBalanceForRentExemption()
- [ ] getTokenAccountBalance()
- [ ] TransactionMessage::class
- [ ] compileToV0Message()
- [ ] VersionedTransaction::class
- [ ] SPL-TOKEN Program
- [ ] getOrCreateAssociatedTokenAccount()
- [ ] getAssociatedTokenAddressSync()
- [ ] createAssociatedTokenAccountInstruction()
- [ ] createSyncNativeInstruction() - [Test][Coverage]
- [ ] Connection::class
Improve abstractions around working with binary data. Done? - Test(s) - Coverage
- Optimizations:
- Leverage PHP more.
- Better cache
$recentBlockhash
when sending transactions.
- Suggestions? Open an Issue or Pull Request :D
Testing & Code Coverage
WIP -- Working on coverage and deprecations. See Coverage Report.
- Configuration phpunit.xml
- composer.json
"scripts": { "test": "vendor/bin/phpunit tests --coverage-clover=coverage.xml --coverage-filter src/", "format": "vendor/bin/php-cs-fixer fix --allow-risk=yes" },
composer test
OR
/verdor/bin/phpunit tests [options]
Contributing - Yes Please! :-P
- Please see CONTRIBUTING for details.
- I will change my profile pic once we get a 2nd mantainer onboard :-)
Security
If you discover any security related issues, please email the maintainers (see composer.json) instead of using the issue tracker.
Credits
- Matt Stauffer (Original creator)
- Zach Vander Velden (Metadata wizard)
- Neverything (Previous Maintainer)
- All Contributors
License
The MIT License (MIT). Please see License File for more information.