This repository has been archived by the owner on May 1, 2024. It is now read-only.
generated from tighten/php-package-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from gabrielkoerich/main
Implement Metadata borsh deserialization
- Loading branch information
Showing
6 changed files
with
123 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace Tighten\SolanaPhpSdk\Accounts; | ||
|
||
use Tighten\SolanaPhpSdk\Borsh; | ||
|
||
class Creator | ||
{ | ||
use Borsh\BorshDeserializable; | ||
|
||
public const SCHEMA = [ | ||
self::class => [ | ||
'kind' => 'struct', | ||
'fields' => [ | ||
['address', 'pubkeyAsString'], | ||
['verified', 'u8'], | ||
['share', 'u8'], | ||
], | ||
], | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace Tighten\SolanaPhpSdk\Accounts; | ||
|
||
use Tighten\SolanaPhpSdk\Borsh\Borsh; | ||
use Tighten\SolanaPhpSdk\Borsh\BorshDeserializable; | ||
|
||
class Metadata | ||
{ | ||
use BorshDeserializable; | ||
|
||
public const SCHEMA = [ | ||
Creator::class => Creator::SCHEMA[Creator::class], | ||
MetadataData::class => MetadataData::SCHEMA[MetadataData::class], | ||
self::class => [ | ||
'kind' => 'struct', | ||
'fields' => [ | ||
['key', 'u8'], | ||
['updateAuthority', 'pubkeyAsString'], | ||
['mint', 'pubkeyAsString'], | ||
['data', MetadataData::class], | ||
['primarySaleHappened', 'u8'], // bool | ||
['isMutable', 'u8'], // bool | ||
], | ||
], | ||
]; | ||
|
||
public static function fromBuffer(array $buffer): self | ||
{ | ||
return Borsh::deserialize(self::SCHEMA, self::class, $buffer); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace Tighten\SolanaPhpSdk\Accounts; | ||
|
||
use Tighten\SolanaPhpSdk\Borsh; | ||
|
||
class MetadataData | ||
{ | ||
use Borsh\BorshDeserializable; | ||
|
||
public const SCHEMA = [ | ||
Creator::class => Creator::SCHEMA[Creator::class], | ||
self::class => [ | ||
'kind' => 'struct', | ||
'fields' => [ | ||
['name', 'string'], | ||
['symbol', 'string'], | ||
['uri', 'string'], | ||
['sellerFeeBasisPoints', 'u16'], | ||
['creators', [ | ||
'kind' => 'option', | ||
'type' => [Creator::class] | ||
]] | ||
], | ||
], | ||
]; | ||
|
||
public function __set($name, $value): void | ||
{ | ||
$this->{$name} = is_string($value) ? preg_replace('/[[:cntrl:]]/', '', $value) : $value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace Tighten\SolanaPhpSdk\Tests\Unit; | ||
|
||
use Tighten\SolanaPhpSdk\Account; | ||
use Tighten\SolanaPhpSdk\Keypair; | ||
use Tighten\SolanaPhpSdk\Tests\TestCase; | ||
use Tighten\SolanaPhpSdk\Accounts\Metadata; | ||
|
||
class MetadataTest extends TestCase | ||
{ | ||
/** @test */ | ||
public function it_deserializes_metadata() | ||
{ | ||
$metadata = Metadata::fromBuffer($this->getBuffer()); | ||
|
||
$this->assertEquals($metadata->key, 4); | ||
$this->assertEquals($metadata->mint, 'FeGDLocrdh5AVH5EMFsAx583hF6f7bqEaVnCE5UEzizg'); | ||
$this->assertEquals($metadata->data->sellerFeeBasisPoints, 420); | ||
} | ||
|
||
private function getBuffer() | ||
{ | ||
return [4,145,46,36,49,209,155,219,75,110,199,128,138,23,42,82,195,151,97,65,119,209,223,11,140,138,148,52,90,254,192,90,223,217,143,78,220,158,99,114,245,24,144,126,240,164,219,101,56,181,231,17,68,20,221,78,43,174,57,170,131,79,186,82,237,32,0,0,0,82,79,71,85,69,32,83,72,65,82,75,83,32,35,50,57,54,55,0,0,0,0,0,0,0,0,0,0,0,0,0,0,10,0,0,0,83,72,65,82,75,0,0,0,0,0,200,0,0,0,104,116,116,112,115,58,47,47,97,114,119,101,97,118,101,46,110,101,116,47,85,107,77,45,82,98,78,75,69,55,70,65,82,82,73,111,122,112,81,49,74,76,88,97,53,113,78,118,81,87,55,119,120,114,65,75,116,97,105,108,120,53,77,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,164,1,1,2,0,0,0,247,145,216,18,238,207,44,106,94,249,103,180,163,74,72,155,203,8,247,230,68,183,76,198,206,170,252,13,134,51,164,151,1,0,145,46,36,49,209,155,219,75,110,199,128,138,23,42,82,195,151,97,65,119,209,223,11,140,138,148,52,90,254,192,90,223,1,100,1,1,1,254,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]; | ||
} | ||
} |