Update intro-to-anchor-frontend.mdx #735
Open
+1
−1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What I did
Corrected BN (Big Number) usage in memcmp filters to ensure proper 8-byte little-endian encoding for
u64
valuesProblem
The current documentation shows:
This is problematic for two reasons:
BN
is the numeric base, not the endianness. Passing"le"
is misleading and may behave inconsistently acrossbn.js
versions..toArray()
without an explicit length produces a variable number of bytes. For example,new BN(5).toArray("le")
yields[5]
, but Solana accounts storeu64
values in 8-byte little-endian format. Using shorter arrays may still match as a prefix inmemcmp
filters, but it’s unsafe and can produce false positives or fail unexpectedly.Fixes
Updated example to:
Ensures numbers are serialized as fixed-length 8-byte little-endian arrays, consistent with Solana’s on-chain
u64
layout. This makesmemcmp
filters deterministic and avoids misleading or unsafe BN usage.Example Failure
Suppose an on-chain account has a
u64
field set to65536
.On-chain bytes (u64 LE):
Current doc version (
new BN(65536, "le").toArray()
):→ Encodes incorrectly, does not match the on-chain value.
Correct version (
new BN(65536).toArray("le", 8)
):→ Matches exactly as expected.