verze-app / solana-php-sdk

Simple PHP SDK for Solana JSON RPC endpoints
MIT License
88 stars 45 forks source link

Implement Metadata borsh deserialization #19

Closed gabrielkoerich closed 2 years ago

gabrielkoerich commented 2 years ago

Hi guys!

This PR creates 3 accounts:

It uses the same schema Metaplex uses to deserialize objects from the buffer array.

I also created a test to show how it works, but basically would be something like this:

use Tighten\SolanaPhpSdk\Accounts\Metadata;

$metadata = Metadata::fromBuffer($this->getBuffer());

dump($metadata);
Tighten\SolanaPhpSdk\Accounts\Metadata^ {#460
  +"key": 4
  +"updateAuthority": "AmivaqHeZBgUZkzEZ44Mi9zQJruExZMP4PVRYduFVkaS"
  +"mint": "FeGDLocrdh5AVH5EMFsAx583hF6f7bqEaVnCE5UEzizg"
  +"data": Tighten\SolanaPhpSdk\Accounts\MetadataData^ {#1385
    +"name": "ROGUE SHARKS #2967"
    +"symbol": "SHARK"
    +"uri": "https://arweave.net/UkM-RbNKE7FARRIozpQ1JLXa5qNvQW7wxrAKtailx5M"
    +"sellerFeeBasisPoints": 420
    +"creators": array:2 [
      0 => Tighten\SolanaPhpSdk\Accounts\Creator^ {#409
        +"address": "HfQgpZzi7Ka241nfCqwoU6A6br8NBjUsdfW2GZ11gHSN"
        +"verified": 1
        +"share": 0
      }
      1 => Tighten\SolanaPhpSdk\Accounts\Creator^ {#986
        +"address": "AmivaqHeZBgUZkzEZ44Mi9zQJruExZMP4PVRYduFVkaS"
        +"verified": 1
        +"share": 100
      }
    ]
  }
  +"primarySaleHappened": 1
  +"isMutable": 1
}

So we finally can deserialize them on PHP! 🚀

mattstauffer commented 2 years ago

This looks great to me, and thank you for your hard work! But I'm also not super experienced with how this should look.

@exzachlyvv do you have time to review?

exzachlyvv commented 2 years ago

Looks solid to me, I say go for it :D

Glad to see the Borsh serialize and deserialize are working for you. It has unit tests with it for the basics but is not very well battle-tested.

neverything commented 2 years ago

Thank you @gabrielkoerich, I got stuck with implementing it, but this looks solid. Will give it a spin in a project I'm working on right now.

neverything commented 2 years ago

Hey @gabrielkoerich tested it on my pet project and it works like a charm. Thank you so much.

Example I used:

$buffer = Tighten\SolanaPhpSdk\Util\Buffer::from(base64_decode("BLUfopsd28JkHxD508khOVqJs0QEtJil6YnQx6TnsfjCvqlhQ8ZBiq6HQQAmaAGcnOT8OZqx345k6w7TwXNMEgkgAAAARGVnZW4gQXBlICM2MDcwAAAAAAAAAAAAAAAAAAAAAAAKAAAAREFQRQAAAAAAAMgAAABodHRwczovL2Fyd2VhdmUubmV0LzYtTlRNQ3V2U1RoenhVa1hGMEJCQnNkMlVkUndleGJQZ1Y3ZURlNnZxWlEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAKQBAQUAAAB5gjzbbOUIc9s/iV4kP5TYIguYOHAu+Xu662lw3SOU/wAnepeo3YmSVXuxd1YIGA0Fx/NBhlrdkN7wihlp80ABbNYAGfMttjlO90iXaxTs+8J22sQvCBdhYhhLS9qYAwYgbdVKABlc/UqP7sYeQijwKGxdKwBRDSYVJjog7Vg/GdoRJ0TtEAAKtR+imx3bwmQfEPnTySE5WomzRAS0mKXpidDHpOex+MIBAQEAAf8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=="));

$metadata = Tighten\SolanaPhpSdk\Accounts\Metadata::fromBuffer($buffer->toArray());

Results in:

=> Tighten\SolanaPhpSdk\Accounts\Metadata {#1352
     +"key": 4,
     +"updateAuthority": "DC2mkgwhy56w3viNtHDjJQmc7SGu2QX785bS4aexojwX",
     +"mint": "DqGGFuiWRiJ6Mim1qafLWn6FZzBGGcSTyFFSJ8RpuqE8",
     +"data": Tighten\SolanaPhpSdk\Accounts\MetadataData {#1351
       +"name": "Degen Ape #6070",
       +"symbol": "DAPE",
       +"uri": "https://arweave.net/6-NTMCuvSThzxUkXF0BBBsd2UdRwexbPgV7eDe6vqZQ",
       +"sellerFeeBasisPoints": 420,
       +"creators": [
         Tighten\SolanaPhpSdk\Accounts\Creator {#1349
           +"address": "9BKWqDHfHZh9j39xakYVMdr6hXmCLHH5VfCpeq2idU9L",
           +"verified": 0,
           +"share": 39,
         },
         Tighten\SolanaPhpSdk\Accounts\Creator {#1348
           +"address": "9FYsKrNuEweb55Wa2jaj8wTKYDBvuCG3huhakEj96iN9",
           +"verified": 0,
           +"share": 25,
         },
         Tighten\SolanaPhpSdk\Accounts\Creator {#1345
           +"address": "HNGVuL5kqjDehw7KR63w9gxow32sX6xzRNgLb8GkbwCM",
           +"verified": 0,
           +"share": 25,
         },
         Tighten\SolanaPhpSdk\Accounts\Creator {#1344
           +"address": "7FzXBBPjzrNJbm9MrZKZcyvP3ojVeYPUG2XkBPVZvuBu",
           +"verified": 0,
           +"share": 10,
         },
         Tighten\SolanaPhpSdk\Accounts\Creator {#1347
           +"address": "DC2mkgwhy56w3viNtHDjJQmc7SGu2QX785bS4aexojwX",
           +"verified": 1,
           +"share": 1,
         },
       ],
     },
     +"primarySaleHappened": 1,
     +"isMutable": 0,
   }
gabrielkoerich commented 2 years ago

@neverything awesome to see it helped!

@exzachlyvv yeah, this wouldn't be possible without your work, thanks!

mattstauffer commented 2 years ago

Fantastic--thanks so much for your work @gabrielkoerich! Tagging now.