- Notifications
You must be signed in to change notification settings - Fork32
Simple PHP SDK for interacting with Transactions, Signatures, Borsh Serialization/Deserialization and RPCs
License
Attestto-com/solana-php-sdk
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
_____________________________________________/__ \////__ \/___// __ \/ / / | / | / / | / ___// __ \/ //_///_///_///_// \__ \///////||/|///||\__\////,</____/__/____/___///_///___/___|//|/___|___///_///||/_//_//_/_//____/\____/_____/_/|_/_/|_/_/|_|/____/_____/_/|_|
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.gitcd solana-php-sdkcomposer installdocker build -t solana-php-sdk.then
docker run -it solana-php-sdk /bin/bash
You can use theConnection class for convenient access to API methods. Some are defined in the code:
useAttestto\SolanaPhpSdk\Connection;useAttestto\SolanaPhpSdk\SolanaRpcClient;// Using a defined method$sdk =newConnection(newSolanaRpcClient(SolanaRpcClient::MAINNET_ENDPOINT));$accountInfo =$sdk->getAccountInfo('4fYNw3dojWmQ4dXtSGE9epjRGy9pFSx62YypT7avPYvA');var_dump($accountInfo);
For all the possible methods, see theAPI documentation.
TheConnection 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 fullResponse object:
useAttestto\SolanaPhpSdk\SolanaRpcClient;$client =newSolanaRpcClient(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 =newSolanaRpcClient(SolanaRpcClient::DEVNET_ENDPOINT);$connection =newConnection($client);$fromPublicKey = KeyPair::fromSecretKey([...]);$toPublicKey =newPublicKey('J3dxNj7nDRRqRRXuEMynDG57DkZK4jYRuv3Garmb1i99');$instruction = SystemProgram::transfer($fromPublicKey->getPublicKey(),$toPublicKey,6);$transaction =newTransaction(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;//traitpublic$keyData;publicconstSCHEMA = [ 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']], ], ], ];publicstaticfunctionfromBuffer(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...'); */staticfunctiondeserializeDidData($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 =newBase58();$base58String =$b58->encode($binaryString);$didData->keyData =$base58String;return$didData; }
- Most of the Magic is done in theBorshDesealizable.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 -Coverage4. [ ] Connection::class5. [x] getLatestBlokchash::class [Source] - [Test] - [Coverage]6. [ ] getMinimumBalanceForRentExemption()7. [ ] getTokenAccountBalance()6. [ ] TransactionMessage::class7. [ ] compileToV0Message()8. [ ] VersionedTransaction::class9. [ ] SPL-TOKEN Program10. [ ] 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
$recentBlockhashwhen sending transactions.
Suggestions? Open anIssue orPull Request :D
WIP -- Working on coverage and deprecations. SeeCoverage Report.
- Configurationphpunit.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" },
composertestOR
/verdor/bin/phpunit tests [options]
- Please seeCONTRIBUTING 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 seeLicense File for more information.
About
Simple PHP SDK for interacting with Transactions, Signatures, Borsh Serialization/Deserialization and RPCs
Topics
Resources
License
Contributing
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
Packages0
Uh oh!
There was an error while loading.Please reload this page.