refactor(deps): source BN from anchor and drop unused @types/bn.js - #688
Open
amilz wants to merge 5 commits into
Open
refactor(deps): source BN from anchor and drop unused @types/bn.js#688amilz wants to merge 5 commits into
amilz wants to merge 5 commits into
Conversation
Tests imported BN directly from bn.js while declaring only @types/bn.js, resolving the runtime package transitively. @anchor-lang/core re-exports BN and depends on bn.js itself, so these tests can take BN from the Anchor namespace they already import instead. - Rewrite 17 test files from a direct bn.js import to anchor.BN - Drop the now-dead direct bn.js dependency from two packages - Remove @types/bn.js from 16 packages with no BN in their type surface - Regenerate the affected per-project lockfiles @types/bn.js is kept wherever BN is reachable. bn.js ships no type declarations, so it is the only supplier of the BN type that Anchor maps u64/i64/u128/i128 IDL fields to; removing it there widens those fields to any instead of failing, because every tsconfig sets skipLibCheck.
Contributor
Greptile SummaryThe PR consolidates test-side BN construction on Anchor’s exported
Confidence Score: 5/5The PR appears safe to merge because no blocking failure remains within the eligible follow-up scope. No blocking failure remains. Important Files Changed
Reviews (2): Last reviewed commit: "refactor(escrow): replace borsh helper w..." | Re-trigger Greptile |
Three litesvm tests decoded SPL token accounts with AccountLayout from @solana/spl-token. The official program clients expose a decoder for this, so use getTokenDecoder() from @solana-program/token, or token-2022 for the Token-2022 example. amount is a bigint from both, so assertions are unchanged. @solana/spl-token stays: all three still use its instruction builders. Also drops the unused ethers dependency from external-delegate-token-master. The token clients are pinned to 0.15.0 rather than the 0.14.x used elsewhere in the repo, because 0.14.x imports getMinimumBalanceForRentExemption from @solana/kit, an export kit removed in 7.1.0.
Removes dependencies with no importer in their package:
- nft-operations/anchor: @metaplex-foundation/mpl-token-metadata,
@metaplex-foundation/umi, axios, node-fetch
- cnft-burn/anchor: @metaplex-foundation/umi
- cutils/anchor: @metaplex-foundation/js
- nft-meta-data-pointer/{anchor,app}: @coral-xyz/spl-token,
browserify-sign, crypto-js, @chakra-ui/next-js
@metaplex-foundation/js is kept in cnft-burn, which does import it, and
Chakra's emotion and framer-motion peers are kept because Chakra needs
them. browserify-sign and crypto-js were version-floor pins rather than
real dependencies: crypto-js still resolves to 4.2.0 transitively, and
nothing in the tree depends on browserify-sign at all.
Also replaces the repository's last direct bn.js import, in cnft-burn's
ReadApi wrapper, where new BN(leaf_id).toArray('le', 8) becomes
getU64Encoder().encode(BigInt(leaf_id)).
Both escrow examples wrapped a local borshSerialize(schema, data) helper to build instruction data and to decode the Offer account. Replaces it with getStructEncoder and getStructDecoder, matching the shape already used in basics/close-account/native, and drops the borsh dependency. Field widths and order were checked against each program's Rust source. The decoder's fixed size of 113 matches Offer::LEN, and decoding the pubkey fields with getAddressDecoder yields Address values directly, so the tests no longer round-trip them through a raw byte array.
The remaining native and pinocchio examples hand-rolled borsh schemas inline in their tests to build instruction data and decode their own program accounts. Each example now exposes a ts/ client module built on @solana/kit codecs, following basics/close-account/native: instruction encoders and builders under ts/instructions, account decoders under ts/state, and a discriminant map matching the program's instruction enum. Tests import the builders instead of serializing by hand, and borsh is removed from all twelve packages. Encodings were checked field by field against each program's Rust source. Where a program has no instruction discriminant, as in account-data, processing-instructions and repository-layout, the encoder covers the bare payload rather than inventing a tag. Covers the last of the packages carrying borsh, so the repository no longer depends on it anywhere.
Contributor
|
Too many files changed for review (171 files, 100 file limit). Bypass the limit by tagging |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Completes DEV-839 — all five sections. Five commits, each a self-contained change.
borshis now absent from the entire repository.1.
refactor(deps)— source BN from Anchor, drop unused@types/bn.js17 test files imported
BNdirectly frombn.jswhile declaring only@types/bn.js, resolving the runtime package transitively.@anchor-lang/corere-exportsBNand depends onbn.jsitself, so these now useanchor.BN— already the dominant idiom, since siblinglitesvm.test.tsfiles in the same packages used it whiletest.tsused a bareBN. Two packages also dropped a directbn.jsdependency that no longer has a direct importer.Beyond tidiness this removes a real footgun: under pnpm a test's own
bn.jscopy can be a physically distinct module from the one Anchor's coder uses, breakinginstanceofduring serialization.anchor.BNguarantees class identity.@types/bn.jswas removed from 16 packages and deliberately kept in 12.bn.jsships no type declarations, so@types/bn.jsis the only supplier of theBNtype that Anchor mapsu64/i64/u128/i128IDL fields to, and@anchor-lang/coredeclares it only as adevDependency. Removing it does not fail the build — every tsconfig setsskipLibCheck: true, which swallows the missing declaration and silently widensBNtoany:That would have quietly un-typed every 64-bit IDL field (
basics/counter/anchorcalls.toNumber()on au64). It is retained in the 11 packages whose programs use 64-bit ints, plusallow-block-list-token, which importsBNexplicitly in.tsx.2.
refactor(tokens)— decode token accounts with kit codecsThree litesvm tests used
AccountLayout.decode(...)from@solana/spl-token; they now usegetTokenDecoder()from@solana-program/token(token-2022for the Token-2022 example).amountis abigintfrom both, so no assertion changed.@solana/spl-tokenstays — all three still use its instruction builders. Also drops an unusedethers.Heads-up for reviewers: these are pinned to
@solana-program/token@0.15.0, not the0.14.xused elsewhere.0.14.ximportsgetMinimumBalanceForRentExemptionfrom@solana/kit, an export removed in kit 7.1.0; a fresh resolve of^7.0.0gets 7.1.0 and fails at import time with a hardSyntaxError. Packages still on0.14.xwork only because their committed lockfiles pin kit at 7.0.0. Not introduced here, but worth a follow-up bump repo-wide.3.
refactor— dead dependencies and the last directbn.jsimportRemoves dependencies with no importer across
nft-operations/anchor,cnft-burn/anchor,cutils/anchor, andnft-meta-data-pointer/{anchor,app}. Kept deliberately:@metaplex-foundation/jsincnft-burn(it is imported), and Chakra's@emotion/*/framer-motionpeers.browserify-signandcrypto-jswere version-floor pins rather than real dependencies —crypto-jsstill resolves to 4.2.0 transitively, and nothing in the tree depends onbrowserify-sign.Also replaces the repo's last direct
bn.jsimport:new BN(leaf_id).toArray('le', 8)→getU64Encoder().encode(BigInt(leaf_id)).4.
refactor(escrow)— borsh helper to kit struct codecsBoth escrow examples wrapped a local
borshSerialize(schema, data)helper. Replaced withgetStructEncoder/getStructDecodermatchingbasics/close-account/native, droppingborsh. Field widths and order were checked against each program's Rust source; the decoder's fixed size of 113 matchesOffer::LEN, and the encoder produces00 0100000000000000 0200000000000000 0300000000000000 ff, matching the offsetsmake_offer.rsreads.5.
refactor(basics,tokens)— native instruction data via kit codecsThe remaining 12 native/pinocchio examples hand-rolled borsh schemas inline in their tests. Each now exposes a
ts/client module followingbasics/close-account/native(from DEV-838): encoders and instruction builders underts/instructions, account decoders underts/state, and a discriminant map matching the program's instruction enum. Tests import the builders instead of serializing by hand.Encodings were checked field by field against each program's Rust source. Where a program has no instruction discriminant (
account-data,processing-instructions,repository-layout— they deserialize the whole payload), the encoder covers the bare payload rather than inventing a tag.repository-layout/pinocchiowas verified independently of its native twin rather than assumed identical; it turned out byte-identical with no discriminant split off.Several encoders were additionally checked byte-for-byte against
borsh.serializeof the equivalent schema — e.g.visitPark('Jimmy', 3)→050000004a696d6d7903000000, and the escrow encoder →00 0100000000000000 0200000000000000 0300000000000000 ff, matching the offsetsmake_offer.rsreads.Verification
Local
anchor build --ignore-keys+tsc --noEmit+ test runs:escrow/native,escrow/pinocchiotoken-fundraiser/anchortransfer-switch/anchorexternal-delegate-token-master/anchornft-operations/anchornft-meta-data-pointer/anchoraccount-data,favorites,transfer-sol,pda-rent-payer,transfer-tokens,token-2022/basicspnpm install --frozen-lockfileverified in every package with a modifiedpackage.json, andprettier --checkpasses on every changed file.Seven of the section-5 packages could not be tested on my machine —
cargo build-sbfunder a local solana-cli 4.2.0 produces.sobinaries that fail against the pinned litesvm withAccess violation ... at address 0x3after 44 compute units. I confirmed this is environmental rather than a codec bug by restoringbasics/account-data/nativeentirely tomain, borsh and all, and rebuilding: it fails identically. Section 5 changed only TypeScript; no Rust source moved. CI (solana 3.1.8) builds and tests all 12 and passes.compression/cnft-burn/anchorandcompression/cutils/anchorhave pre-existing type errors (undeclaredbs58/@solana/spl-tokenimports, a staletreeAuthorityIDL reference). I confirmed these reproduce unchanged atmainand are not caused by these commits; both packages are in.github/.ghaignore, so CI does not build them.