____ __ ______ _____ ____ __ ___ _ _____ _____ ____ __ __
/ __ \/ / / / __ \ / ___// __ \/ / / | / | / / | / ___// __ \/ //_/
/ /_/ / /_/ / /_/ / \__ \/ / / / / / /| | / |/ / /| | \__ \/ / / / ,<
/ ____/ __ / ____/ ___/ / /_/ / /___/ ___ |/ /| / ___ | ___/ / /_/ / /| |
/_/ /_/ /_/_/ /____/\____/_____/_/ |_/_/ |_/_/ |_| /____/_____/_/ |_|
Simple PHP SDK for interacting with the Transactions, Signatures, Borsh Serialization/Deserialization and RPCs
Forked from the Verze repo: verze-app/solana-php-sdk
- 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.
You can install the package via composer :
composer require attestto/solana-php-sdk
git clone https://github.com/Attestto-com/solana-php-sdk.git
cd solana-php-sdk
composer install
docker build -t solana-php-sdk .
then
docker run -it solana-php-sdk /bin/bash
You can use the Connection
class for convenient access to API methods. Some are defined in the code:
use Attestto\SolanaPhpSdk\Connection;
use Attestto\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.
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 Attestto\SolanaPhpSdk\SolanaRpcClient;
$client = new SolanaRpcClient(SolanaRpcClient::MAINNET_ENDPOINT);
$accountInfoResponse = $client->call('getAccountInfo', ['4fYNw3dojWmQ4dXtSGE9epjRGy9pFSx62YypT7avPYvA']);
$accountInfoBody = $accountInfoResponse->json();
$accountInfoStatusCode = $accountInfoResponse->getStatusCode();
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);
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);
}
}
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;
}
- 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.
-
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 4. [ ] Connection::class 5. [x] getLatestBlokchash::class [Source] - [Test] - [Coverage] 6. [ ] getMinimumBalanceForRentExemption() 7. [ ] getTokenAccountBalance() 6. [ ] TransactionMessage::class 7. [ ] compileToV0Message() 8. [ ] VersionedTransaction::class 9. [ ] SPL-TOKEN Program 10. [ ] getOrCreateAssociatedTokenAccount() 11. [ ] getAssociatedTokenAddressSync() 12. [ ] createAssociatedTokenAccountInstruction() 11. [ ] createSyncNativeInstruction() - [Test][Coverage]
-
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
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]
- Please see CONTRIBUTING for details.
- I will change my profile pic once we get a 2nd mantainer onboard :-)
If you discover any security related issues, please email the maintainers (see composer.json) instead of using the issue tracker.
- Matt Stauffer (Original creator)
- Zach Vander Velden (Metadata wizard)
- Neverything (Previous Maintainer)
- All Contributors
The MIT License (MIT). Please see License File for more information.