Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
Implement Metadata borsh deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielkoerich committed Dec 8, 2021
1 parent f6c1f5d commit d032405
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 1 deletion.
21 changes: 21 additions & 0 deletions src/Accounts/Creator.php
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'],
],
],
];
}
32 changes: 32 additions & 0 deletions src/Accounts/Metadata.php
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);
}
}
32 changes: 32 additions & 0 deletions src/Accounts/MetadataData.php
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;
}
}
11 changes: 11 additions & 0 deletions src/Borsh/BinaryReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Tighten\SolanaPhpSdk\Exceptions\TodoException;
use Tighten\SolanaPhpSdk\Util\Buffer;
use Tighten\SolanaPhpSdk\PublicKey;
use Closure;

class BinaryReader
Expand Down Expand Up @@ -142,6 +143,16 @@ public function readFixedArray(int $length): array
return $this->readBuffer($length)->toArray();
}

public function readPubKey(): PublicKey
{
return new PublicKey($this->readFixedArray(32));
}

public function readPubKeyAsString(): string
{
return $this->readPubKey()->toBase58();
}

/**
* @return array
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Borsh/Borsh.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ protected static function deserializeField(
$fieldType,
BinaryReader $reader
) {
if (is_string($fieldType)) {
if (is_string($fieldType) && ! class_exists($fieldType)) {
return $reader->{'read' . ucfirst($fieldType)}();
}

Expand Down
26 changes: 26 additions & 0 deletions tests/Unit/MetadataTest.php
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];
}
}

0 comments on commit d032405

Please sign in to comment.