Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Metadata serialization issue #20

Open
lucasoares opened this issue Apr 6, 2024 · 4 comments
Open

Metadata serialization issue #20

lucasoares opened this issue Apr 6, 2024 · 4 comments

Comments

@lucasoares
Copy link

lucasoares commented Apr 6, 2024

I'm using the token-metadata client to fetch metadata from tokens. The strings decoded from the data returned by the chain contain additional 0x00 bytes; I don't know if it should.

Example:

	resp, err := client.GetAccountInfoWithOpts(ctx, metadataAddress, &rpc.GetAccountInfoOpts{
		Commitment: rpc.CommitmentType(defaultRpc.Commitment),
		Encoding:   "base64",
	})

	if err != nil {
		return nil, fmt.Errorf("failed to get metadata account info: %w", err)
	}

	metadata := new(token_metadata.Metadata)
	err = bin.NewBorshDecoder(resp.Value.Data.GetBinary()).Decode(metadata)

The result of metadata.Data.Name (or other data, like symbol), for example, will always have a length of 32:

fmt.Printf("Token name from metadata (%d): %s\n", len(metadata.Data.Name), metadata.Data.Name)

Results into:

Token name from metadata (32): Ansem's World

If I print the value with a string interpreter to show 0x00 bytes, it shows as:

name="Ansem's World\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"

What do you think is the best way to deal with it? Is this the default behavior for the NewBorshDecoder?

@SubZzzzzz
Copy link

Did you find an answer ?? am interested by that

@SubZzzzzz
Copy link

SubZzzzzz commented May 2, 2024

mintAddress := solana.MustPublicKeyFromBase58(tokenAddress)

	resp, err := rpcClient.GetAccountInfo(context.Background(), mintAddress)

	if err != nil {
		fmt.Errorf("failed to get metadata account info: %w", err)
	}

	metadata := new(token_metadata.Metadata)
	decoded := bin.NewBorshDecoder(resp.Value.Data.GetBinary())
	err = decoded.Decode(&metadata)
	fmt.Println("Is mutable : ", metadata.IsMutable)
	fmt.Println("Token name : ", metadata.Data.Name)
	fmt.Println("Token Symbol : ", metadata.Data.Symbol)
	fmt.Println("Token Creator : ", metadata.Data.Creators)
	fmt.Println("Token Uri : ", metadata.Data.Uri)

I tried your way and for bonk for example i'd get :

Is mutable :  false
Token name :  
Token Symbol :  
Token Creator :  <nil>
Token Uri :  

If anyone could indicate me what am doing wrong would be lovely

@lucasoares
Copy link
Author

If anyone could indicate me what am doing wrong would be lovely

Do you have issues with getting the metadata by itself or with \x00 bytes in the string values? If you are having issues getting the metadata, follow the examples: https://github.com/gagliardetto/metaplex-go/tree/main/examples

But if it is the same issue as mine (which should be, because you are replying in this github issue) for now I'm manually replacing \x00 bytes in the result.

@SubZzzzzz
Copy link

Thanks it helped.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants