Skip to main content
5 votes
Accepted

Program cannot deserialize borsch serialized instruction from typescript client

First correct your schema declaration: // Instruction variant indexes enum InstructionVariant { InitializeAccount = 0, Deposit, Withdraw, } const DepositSchema = new Map([ [ ...
Frank C.'s user avatar
  • 656
4 votes
Accepted

Recommended serialization library for dynamically sized objects

You wouldn't want to serialize an account within the runtime. For any meaningfully sized account, this will use up more compute units than you have. I have generally gotten away with just looking up ...
Nick Garfield's user avatar
4 votes

Solana Deserialization

If that Payload struct is the only input that your program can receive, then this should work: #[derive(BorshDeserialize, BorshSerialize, Debug)] struct Payload { purchase_qty: u64, ...
Joe C's user avatar
  • 1,116
3 votes
Accepted

Borsh dependency issue with latest versions of crates

You can’t use anchor 28 with mpl token metadata until Metaplex updates their libraries. Downgrade to anchor 26 with Solana 1.14 Check the Anchor issues for more info
Noah's user avatar
  • 286
3 votes
Accepted

Am I allowed to use a BinaryHeap in an account?

You're correct that BinaryHeap is implemented in Borsh: https://github.com/near/borsh-rs/blob/9e6831628442036fe3183f8244da3973bd99e8f7/borsh/src/ser/mod.rs#L305 This means that you can safely use it ...
Jon C's user avatar
  • 19.6k
2 votes
Accepted

Rust Client - Get Serum liquidity pool info

I think you are getting confused as to what Serum pools are. The code in your link is some sort of agnostic pool which doesn't define a swap capability. If you read the spl-token-swap documentation &...
Arowana's user avatar
  • 2,373
2 votes
Accepted

Persistent negative offset span value when serialising a structure in Borsh generated by Anchor

As you noted, that -1 span is due to the variable-length fields in your type, the Strings. Instead, you can change your types to encode a set number of bytes in utf-8 encoding, e.g.: const STRING_LEN:...
Jon C's user avatar
  • 19.6k
2 votes

Program cannot deserialize borsch serialized instruction from typescript client

this is another solution that worked for me function DepositInstructionData(deposit_amount): Buffer { const dataLayout = BufferLayout.struct([ BufferLayout.u8('instruction'), ...
chinepun.sol's user avatar
2 votes

Program cannot deserialize borsch serialized instruction from typescript client

amount is declared as u64 in the Rust program, but only u8 on the client side schema. All type declarations must match in both size and layout on both sides
trent.sol's user avatar
  • 4,585
2 votes

Borsh Deserialization error: Trying to access beyond buffer length

Since your struct has variable-length fields (strings), using the struct type will be very difficult, since it can't calculate the size of each field statically. There should be an easier way, however....
Jon C's user avatar
  • 19.6k
2 votes

What does range full operator do in borsh serialize?

That's just another way of dereferencing to get the internal data in Rust. &mut account.data.borrow_mut()[..] and *account_info.data.borrow_mut() should get the same result. You could write that ...
acheron's user avatar
  • 2,909
2 votes
Accepted

What's the difference of using Borsh and buffer layout to encode/decode?

Borsh is just a particular serialization format, given by the spec at https://borsh.io/. They also provide libraries for serializing and deserializing types based on that spec. Buffer layout is a JS ...
Jon C's user avatar
  • 19.6k
2 votes

how to make a proper schema for the arguments

Pubkey is not part of the borsh specification. It can be represented as a [u8; 32] instead. Should look something like: class MyArgs { decimals: u8; mintAuthority: PublicKey; freezeAuthority: ...
Ademola's user avatar
  • 3,944
2 votes
Accepted

What is the difference between `serialize`,`try_to_vec` and `try_serialize` in anchor?

No, they are not. try_to_vec and serialize are quite similar as try_to_vec calls serialize under the hood. The difference between these two is that serialize requires you to pass a writer where the ...
McBain's user avatar
  • 5,384
2 votes
Accepted

Facing error with array/string serialization in anchor solana

Your Order struct in Rust has token_id defined as [u8; 80], meaning it expects a 80-byte array. If the serialized data for token_id doesn't match this length, Borsh will throw an error since it ...
Jim's user avatar
  • 165
1 vote

How to get same borsh serialize result for same input from both JS and Rust

I found the mistake I have made. SHOULD use BorshSchema.Array(BorshSchema.u8, 32), for PubKey type data 'recipient', not BorshSchema.Vec(BorshSchema.u8)
breeze wang's user avatar
1 vote

Deserializing Anchor Account Data

I haven’t tested anything but I’ve been researching and I think it comes down to anchor putting 8 bytes of discriminator data at the start of account data, so I’m going to slice the first 8 bytes off ...
taborgreat's user avatar
1 vote

In a solana program, how do you go about (Borsh) deserializing account data where the allocated data size is bigger than the actual struct size?

Same issue although I tried using borsh0_10::try_from_slice_unchecked as the tooltip for the deprecated try_from_slice_unchecked suggested. But I get the the error that "the trait bound ...
r4F705's user avatar
  • 11
1 vote
Accepted

Parsing instructions in Rust

The stake program is a bit unique because it doesn't have an IDL. On the other hand, @solana/web3.js provides instruction decoders out of the box for you. Given the instruction data, you can do: const ...
Jon C's user avatar
  • 19.6k
1 vote

Deserializing an Enum with borsh

I'm not sure what version of Borsh-JS you're using, but there's probably a way to "tag" the enum name. In the most recent version, that would look like: const WhitelistMode = borsh.rustEnum([...
Jon C's user avatar
  • 19.6k
1 vote
Accepted

program return data to client error

meta.returnData.data is trimmed all of the least significant bit (LE) to the right from your program returned value. In this case, it's missing 3 zero bytes at the end of the buffer. I think a better ...
BFault's user avatar
  • 281
1 vote

Borsh, pubkey & COption

In my Cargo.toml I was using borsh=0.10.3 and downgrading to borsh=0.9.3 fixed my problem.
Jorge Martinez's user avatar
1 vote

Solana Deserialization

If you want to manually deserialize some struct (without Anchor), I suggest bytemuck, structs are Plain Old Data (POD): pub fn get_payload_from_bytes(v: &[u8]) -> &Payload{ bytemuck::...
Whiteseal's user avatar
  • 3,597
1 vote
Accepted

Data Account Cannot Be Deserialized

createAccountWithSeed just allocates the space and assigns it to the program, and no data is written, so you should always see all zeroes in the program. The failed deserialization likely means ...
Jon C's user avatar
  • 19.6k
1 vote
Accepted

How does borsh serialize updated data back into account?

counter_ai is an AccountInfo object provided by the runtime to the program, and it contains the accounts lamports (SOL) as a mutable reference &mut u64 and data as a mutable slice of bytes &...
Jon C's user avatar
  • 19.6k
1 vote

How do you decode struct numbers for testing in anchor?

You can do state.depositCap.eq(depositCap)
Meow's user avatar
  • 343

Only top scored, non community-wiki answers of a minimum length are eligible