gagliardetto / solana-go

Go SDK library and RPC client for the Solana Blockchain
Apache License 2.0
824 stars 246 forks source link

CreateProgramAddress working tests cases with Golang and TypeScript #33

Closed cryptohazard closed 2 years ago

cryptohazard commented 2 years ago

Hey,

I am trying to mirror the behaviour of a typescript lib for Solana. I need to use CreateProgramAddress to derive a Serum Market address. To have a working example I looked through the tests cases:

I see that the resulting addresses are different and I don't really understand why. I might be missing something. Any idea where the diff comes from? Do you by any chance have a example that work on both lib? I don't know Rust so I also don't know if there are tests cases on the original lib.

gagliardetto commented 2 years ago

Hey @cryptohazard

Thanks for bringing this up!

Those are the tests I ported from the Solana rust code. I now added a note: https://github.com/gagliardetto/solana-go/blob/3c4b307d5a9b95d5a74ab09e922ba9b2a766a064/keys_test.go#L189-L190

I now also ported the tests from the typescript you mentioned above, and they do correspond: https://github.com/gagliardetto/solana-go/blob/3c4b307d5a9b95d5a74ab09e922ba9b2a766a064/keys_test.go#L239-L242

My guess is that it's possible to visually confuse the both tests because the rust one uses program_id := MustPublicKeyFromBase58("BPFLoaderUpgradeab1e11111111111111111111111") and typescript uses program_id := MustPublicKeyFromBase58("BPFLoader1111111111111111111111111111111111") ( I spent a minute figuring out what was wrong too :sweat_smile: )

I also notice that the rust create_program_address (here) removed the if program_id.is_native_program_id() check (see latest here), so I removed it in my go implementation, too.

All this culminated in the release of v1.0.4.

See full changes diff: https://github.com/gagliardetto/solana-go/compare/v1.0.3...v1.0.4

cryptohazard commented 2 years ago

wow thank you. I guess I really shouldn't code things like that in the middle of night 😅. I should now be able to finish my work here: https://github.com/teal-finance/rainbow/tree/psyoptions/pkg/provider/psyoptions I learn a lot about Solana using your libs, especially with the examples. Thanks again!!

gagliardetto commented 2 years ago

:+1:

I should now be able to finish my work here: https://github.com/teal-finance/rainbow/tree/psyoptions/pkg/provider/psyoptions

Please remember that, when dealing with anchor-go, you need to keep the UnmarshalWithDecoder methods that get generated (otherwise you won't be able to decode the accounts data).

You also better use the bin.NewBorshDecoder instead of bin.NewBinDecoder (anchor uses Borsh as encoding format).