Skip to content

Commit

Permalink
Merge pull request #4 from Attestto-com/feature/spl-token
Browse files Browse the repository at this point in the history
Feature/spl token
  • Loading branch information
chongkan authored May 4, 2024
2 parents 7d562c1 + bacb51f commit 27cd5e7
Show file tree
Hide file tree
Showing 26 changed files with 713 additions and 50 deletions.
Binary file modified .DS_Store
Binary file not shown.
6 changes: 6 additions & 0 deletions .run/tests.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="tests" type="PHPUnitRunConfigurationType" factoryName="PHPUnit" editBeforeRun="true">
<TestRunner configuration_file="$PROJECT_DIR$/phpunit.xml" directory="$PROJECT_DIR$/tests" use_alternative_configuration_file="true" />
<method v="2" />
</configuration>
</component>
21 changes: 0 additions & 21 deletions LICENSE

This file was deleted.

15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,20 @@ example usage _**(This will be improved, WIP)**_:

1. Borsh serialize and deserialize. [Done](https://github.com/Attestto-com/solana-php-sdk/tree/master/src/Borsh) - [Test(s)](https://github.com/Attestto-com/solana-php-sdk/blob/master/tests/Unit/BorshTest.php) - [Coverage](https://app.codecov.io/github/Attestto-com/solana-php-sdk/tree/master/src%2FBorsh)
2. Improved documentation. [WIP](#) - This document + [Documentation Index](https://github.com/Attestto-com/solana-php-sdk/tree/master/docs) (https://github.com/Attestto-com/solana-php-sdk/tree/master/docs)
3. Build out more of the Connection, SystemProgram, TokenProgram, MetaplexProgram classes. [WIP](https://github.com/Attestto-com/solana-php-sdk/tree/master/src) - [Tests](https://github.com/Attestto-com/solana-php-sdk/tree/master/tests/Unit) - [Coverage](https://app.codecov.io/github/Attestto-com/solana-php-sdk/tree/master/src)
3. Build out more of the Connection, Message, SystemProgram, TokenProgram, MetaplexProgram classes. [WIP](https://github.com/Attestto-com/solana-php-sdk/tree/master/src) - [Tests](https://github.com/Attestto-com/solana-php-sdk/tree/master/tests/Unit) - [Coverage](https://app.codecov.io/github/Attestto-com/solana-php-sdk/tree/master/src)
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]

4. Improve abstractions around working with binary data. [Done?](https://github.com/Attestto-com/solana-php-sdk/tree/master/src/Borsh) - [Test(s)](https://github.com/Attestto-com/solana-php-sdk/blob/master/tests/Unit/BorshTest.php) - [Coverage](https://app.codecov.io/github/Attestto-com/solana-php-sdk/tree/master/src%2FBorsh)
5. Optimizations:
1. Leverage PHP more.
Expand Down
60 changes: 60 additions & 0 deletions config/solana-sdk.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

return [

'TOKEN_PROGRAM_ID' => env('TOKEN_PROGRAM_ID') ?? 'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA',
//-------
'devnet' => [
'mints' => [
'usdc' => env('USDC_MINT_DEVNET'),
'helium' => env('MAINNET_HELIUM_MINT'),
'solana' => env('MAINNET_SOLANA_MINT'),
],
'network' => [
'type' => env('NETWORK'),
'attesto_rpc' => env('ATTESTO_RPC'),
'fallback_rpc_1' => env('FALLBACK_RPC_1'),
'fallback_rpc_2' => env('FALLBACK_RPC_2'),
],
'api_keys' => [
'helius' => env('HELIUS_API_KEY'),
],
'data_source' => env('DATA_SOURCE'),
'keys' => [
'dapp_pk' => env('DAPP_PK'), // Signer
'burn_address' => env('SOLANA_BURN_ADDRESS'), // Not in use
'treasury_pk' => env('TREASURY_PK'), // Not in use
'fees_pk' => env('FEES_PK'), // Fee Taker
],
],
'network' => [
'type' => env('NETWORK'),
'attesto_rpc' => env('ATTESTO_RPC'),
'fallback_rpc_1' => env('FALLBACK_RPC_1'),
'fallback_rpc_2' => env('FALLBACK_RPC_2'),
],
'api_keys' => [
'helius' => env('HELIUS_API_KEY'),
],
'data_source' => env('DATA_SOURCE'),
'keys' => [
'dapp_pk' => env('DAPP_PK'), // Signer
'burn_address' => env('SOLANA_BURN_ADDRESS'), // Not in use
'treasury_pk' => env('TREASURY_PK'), // Not in use
'fees_pk' => env('FEES_PK'), // Fee Taker
],
'fees' => [
'key_registration' => env('KEY_REGISTRATION_FEE'),
'did_registration' => env('DID_REGISTRATION_FEE'),
'did_update' => env('DID_UPDATE_FEE'),
'vc_issuing' => env('VC_ISSUING_FEE'),
'vc_proofing' => env('VC_PROOFING_FEE'),
'vc_verifying' => env('VC_VERIFYING_FEE'),
'vc_revoking' => env('VC_REVOKING_FEE'),
],
'subscription_prices' => [
'monthly' => env('KEY_REGISTRATION_FEE', 0.01),
'anual' => env('DID_REGISTRATION_FEE', 0.1),
],

];
Binary file removed deprecated.png
Binary file not shown.
34 changes: 34 additions & 0 deletions docs/Connection.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Connection Class

The `Connection` class is part of the `Attestto\SolanaPhpSdk` namespace and extends the `Program` class. It provides methods to interact with the Solana network.

## Methods

### `getAccountInfo(string $pubKey): array`

This method retrieves the account information for a given public key. It throws an `AccountNotFoundException` if the account is not found.

### `getBalance(string $pubKey): float`

This method retrieves the balance for a given public key.

### `getConfirmedTransaction(string $transactionSignature): array|null`

This method retrieves a confirmed transaction using the transaction signature.

### `getTransaction(string $transactionSignature): array|null`

This method retrieves a transaction using the transaction signature. This method is only available in solana-core v1.7 or newer.

### `getLatestBlockhash(?Commitment $commitment): array`

This method retrieves the latest blockhash.

### `sendTransaction(Transaction $transaction, array $signers, array $params = []): array|Response`

This method sends a transaction. It signs the transaction with the provided signers and serializes it to a binary string.

### `simulateTransaction(Transaction $transaction, array $signers, array $params = []): array|Response`

This method simulates a transaction. It signs the transaction with the provided signers and serializes it to a binary string.

59 changes: 59 additions & 0 deletions docs/SplToken/SplTokenTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace SplToken;

use Attestto\SolanaPhpSdk\Programs\SplTokenProgram;
use Attestto\SolanaPhpSdk\Tests\TestCase;

class SPLTokenTest extends TestCase
{
private $splTokenProgram;

public function setUp(): void
{
$client = $this->assembleClient('POST', []);
$this->splTokenProgram = new SplTokenProgram($client);
}

// public function testGetAssociatedTokenAddressSync()
// {
// $mockMint = $this->createMock(PublicKey::class);
// $mockOwner = $this->createMock(PublicKey::class);
// $mockOwner->method('toBuffer')->willReturn('buffer');
// $mockOwner->method('isOnCurve')->willReturn(true);
//
// $mockProgramId = 'programId';
// $mockAssociatedTokenProgramId = 'associatedTokenProgramId';
//
// $result = $this->splToken->getAssociatedTokenAddressSync(
// $mockMint,
// $mockOwner,
// false,
// $mockProgramId,
// $mockAssociatedTokenProgramId
// );
//
// $this->assertInstanceOf(PublicKey::class, $result);
// }
//
// public function testGetAssociatedTokenAddressSyncThrowsException()
// {
// $this->expectException(TokenOwnerOffCurveError::class);
//
// $mockMint = $this->createMock(PublicKey::class);
// $mockOwner = $this->createMock(PublicKey::class);
// $mockOwner->method('toBuffer')->willReturn('buffer');
// $mockOwner->method('isOnCurve')->willReturn(false);
//
// $mockProgramId = 'programId';
// $mockAssociatedTokenProgramId = 'associatedTokenProgramId';
//
// $this->splToken->getAssociatedTokenAddressSync(
// $mockMint,
// $mockOwner,
// false,
// $mockProgramId,
// $mockAssociatedTokenProgramId
// );
// }
}
22 changes: 11 additions & 11 deletions phpUnit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.0/phpunit.xsd"
bootstrap="vendor/autoload.php"

executionOrder="depends,defects"


beStrictAboutOutputDuringTests="true"
failOnRisky="true"
failOnWarning="true">
<testsuites>
<testsuite name="default">
<directory>tests</directory>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
</testsuites>
<coverage>
<!-- <include>-->
<!-- <directory suffix=".php">src</directory>-->
<!-- </include>-->
<!-- <report>-->
<!-- <html output="coverage" />-->
<!-- </report>-->
<include>
<directory suffix=".php">src</directory>
</include>
<report>
<html output="coverage" />
</report>
</coverage>
<source restrictDeprecations="true" restrictNotices="true" restrictWarnings="true">
<include>
Expand Down
43 changes: 33 additions & 10 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,19 @@
namespace Attestto\SolanaPhpSdk;

use Attestto\SolanaPhpSdk\Exceptions\AccountNotFoundException;
use Attestto\SolanaPhpSdk\Exceptions\GenericException;
use Attestto\SolanaPhpSdk\Exceptions\InvalidIdResponseException;
use Attestto\SolanaPhpSdk\Exceptions\MethodNotFoundException;
use Attestto\SolanaPhpSdk\Util\Commitment;
use Illuminate\Http\Client\Response;
use Psr\Http\Client\ClientExceptionInterface;
use SodiumException;

/**
* Class Connection
* @package Attestto\SolanaPhpSdk
* https://solana-labs.github.io/solana-web3.js/classes/Connection.html
*/
class Connection extends Program
{
/**
Expand Down Expand Up @@ -55,27 +66,40 @@ public function getTransaction(string $transactionSignature): array|null
* @param Commitment|null $commitment
* @return array
* @throws Exceptions\GenericException|Exceptions\MethodNotFoundException|Exceptions\InvalidIdResponseException
* Deprecated: Use getLatestBlockhash instead
*/
public function getRecentBlockhash(?Commitment $commitment = null): array
{
return $this->client->call('getRecentBlockhash', array_filter([$commitment]))['value'];
}

/**
* @param Commitment|null $commitment CONFIRMED | 'finalized'
* @return array
* @throws Exceptions\GenericException|Exceptions\MethodNotFoundException|Exceptions\InvalidIdResponseException|\Psr\Http\Client\ClientExceptionInterface
*/
public function getLatestBlockhash(?Commitment $commitment): array
{
return $this->client->call('getLatestBlockhash', array_filter([$commitment]))['value'];
}

/**
* @param Transaction $transaction
* @param Keypair[] $signers
* @param array $params
* @return array|\Illuminate\Http\Client\Response
* @throws Exceptions\GenericException
* @throws Exceptions\InvalidIdResponseException
* @throws Exceptions\MethodNotFoundException
* @return array|Response
* @throws GenericException
* @throws InvalidIdResponseException
* @throws MethodNotFoundException
* @throws ClientExceptionInterface
* @throws SodiumException
* TODO Add Support for Versiones TXns
*/
public function sendTransaction(Transaction $transaction, array $signers, array $params = [])
public function sendTransaction(Transaction $transaction, array $signers, array $params = []): array|Response
{
if (! $transaction->recentBlockhash) {
$transaction->recentBlockhash = $this->getRecentBlockhash()['blockhash'];
$transaction->recentBlockhash = $this->getLatestBlockhash()['blockhash'];
}

$transaction->sign(...$signers);

$rawBinaryString = $transaction->serialize(false);
Expand All @@ -90,13 +114,12 @@ public function sendTransaction(Transaction $transaction, array $signers, array

return $this->client->call('sendTransaction', [$hashString, $send_params]);
}



/**
* @param Transaction $transaction
* @param Keypair[] $signers
* @param array $params
* @return array|\Illuminate\Http\Client\Response
* @return array|Response
* @throws Exceptions\GenericException
* @throws Exceptions\InvalidIdResponseException
* @throws Exceptions\MethodNotFoundException
Expand Down
9 changes: 9 additions & 0 deletions src/Exceptions/TokenAccountNotFoundError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Attestto\SolanaPhpSdk\Exceptions;

use Exception;
class TokenAccountNotFoundError extends Exception
{

}
9 changes: 9 additions & 0 deletions src/Exceptions/TokenInvalidAccountOwnerError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Attestto\SolanaPhpSdk\Exceptions;

use Exception;
class TokenInvalidAccountOwnerError extends Exception
{

}
10 changes: 10 additions & 0 deletions src/Exceptions/TokenInvalidMintError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Attestto\SolanaPhpSdk\Exceptions;

use Exception;

class TokenInvalidMintError extends Exception
{

}
9 changes: 9 additions & 0 deletions src/Exceptions/TokenOwnerOffCurveError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Attestto\SolanaPhpSdk\Exceptions;

use Exception;
class TokenOwnerOffCurveError extends Exception
{

}
Loading

0 comments on commit 27cd5e7

Please sign in to comment.