Skip to content

Commit a9f718f

Browse files
authored
refactor(deps): source BN from anchor and drop unused @types/bn.js (#688)
* refactor(deps): source BN from anchor and drop unused @types/bn.js 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. * refactor(tokens): decode token accounts with kit codecs 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. * refactor: remove dead dependencies and the last direct bn.js import 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)). * refactor(escrow): replace borsh helper with kit struct codecs 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. * refactor(basics,tokens): encode native instruction data with kit codecs 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.
1 parent 56e16c9 commit a9f718f

171 files changed

Lines changed: 5052 additions & 4653 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

basics/account-data/anchor/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
"@solana/web3.js": "^1.98.4"
55
},
66
"devDependencies": {
7-
"@types/bn.js": "^5.1.0",
87
"@types/chai": "^5.2.3",
98
"@types/mocha": "^10.0.10",
109
"chai": "^6.2.2",

basics/account-data/anchor/pnpm-lock.yaml

Lines changed: 0 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

basics/account-data/native/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
},
99
"dependencies": {
1010
"@solana/kit": "^7.0.0",
11-
"@solana-program/system": "^0.13.0",
12-
"borsh": "^2.0.0"
11+
"@solana-program/system": "^0.13.0"
1312
},
1413
"devDependencies": {
1514
"@types/chai": "^5.2.3",

basics/account-data/native/pnpm-lock.yaml

Lines changed: 0 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 9 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import { Buffer } from 'node:buffer';
21
import {
3-
AccountRole,
42
type Address,
53
appendTransactionMessageInstruction,
64
createTransactionMessage,
@@ -11,33 +9,18 @@ import {
119
setTransactionMessageFeePayerSigner,
1210
signTransactionMessageWithSigners,
1311
} from '@solana/kit';
14-
import { SYSTEM_PROGRAM_ADDRESS } from '@solana-program/system';
15-
import * as borsh from 'borsh';
1612
import { assert } from 'chai';
1713
import { FailedTransactionMetadata, LiteSVM } from 'litesvm';
18-
19-
const AddressInfoSchema = {
20-
struct: {
21-
name: 'string',
22-
house_number: 'u8',
23-
street: 'string',
24-
city: 'string',
25-
},
26-
};
27-
28-
type AddressInfo = {
29-
name: string;
30-
house_number: number;
31-
street: string;
32-
city: string;
33-
};
34-
35-
function borshSerialize(schema: borsh.Schema, data: object): Buffer {
36-
return Buffer.from(borsh.serialize(schema, data));
37-
}
14+
import { type AddressInfo, addressInfoDecoder, createCreateAddressInfoInstruction } from '../ts';
3815

3916
describe('Account Data!', () => {
4017
const svm = new LiteSVM();
18+
const addressInfo: AddressInfo = {
19+
name: 'Joe C',
20+
houseNumber: 136,
21+
street: 'Mile High Dr.',
22+
city: 'Solana Beach',
23+
};
4124
let programId: Address;
4225
let payer: KeyPairSigner;
4326
let addressInfoAccount: KeyPairSigner;
@@ -51,30 +34,7 @@ describe('Account Data!', () => {
5134
});
5235

5336
it('Create the address info account', async () => {
54-
console.log(`Program Address : ${programId}`);
55-
console.log(`Payer Address : ${payer.address}`);
56-
console.log(`Address Info Acct : ${addressInfoAccount.address}`);
57-
58-
const ix = {
59-
programAddress: programId,
60-
accounts: [
61-
{
62-
address: addressInfoAccount.address,
63-
role: AccountRole.WRITABLE_SIGNER,
64-
signer: addressInfoAccount,
65-
},
66-
{ address: payer.address, role: AccountRole.WRITABLE_SIGNER, signer: payer },
67-
{ address: SYSTEM_PROGRAM_ADDRESS, role: AccountRole.READONLY },
68-
],
69-
data: new Uint8Array(
70-
borshSerialize(AddressInfoSchema, {
71-
name: 'Joe C',
72-
house_number: 136,
73-
street: 'Mile High Dr.',
74-
city: 'Solana Beach',
75-
}),
76-
),
77-
};
37+
const ix = createCreateAddressInfoInstruction(addressInfoAccount, payer, programId, addressInfo);
7838

7939
const transactionMessage = pipe(
8040
createTransactionMessage({ version: 0 }),
@@ -92,10 +52,6 @@ describe('Account Data!', () => {
9252
const accountInfo = svm.getAccount(addressInfoAccount.address);
9353
assert(accountInfo.exists, 'address info account not found');
9454

95-
const readAddressInfo = borsh.deserialize(AddressInfoSchema, Buffer.from(accountInfo.data)) as AddressInfo;
96-
console.log(`Name : ${readAddressInfo.name}`);
97-
console.log(`House Num: ${readAddressInfo.house_number}`);
98-
console.log(`Street : ${readAddressInfo.street}`);
99-
console.log(`City : ${readAddressInfo.city}`);
55+
assert.deepEqual(addressInfoDecoder.decode(accountInfo.data), addressInfo);
10056
});
10157
});
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './instructions';
2+
export * from './state';
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import {
2+
AccountRole,
3+
addEncoderSizePrefix,
4+
type Address,
5+
getStructEncoder,
6+
getU8Encoder,
7+
getU32Encoder,
8+
getUtf8Encoder,
9+
type TransactionSigner,
10+
} from '@solana/kit';
11+
import { SYSTEM_PROGRAM_ADDRESS } from '@solana-program/system';
12+
import type { AddressInfo } from '../state';
13+
14+
// Instruction data layout: the program deserializes the whole payload as an `AddressInfo`.
15+
export const createAddressInfoEncoder = getStructEncoder([
16+
['name', addEncoderSizePrefix(getUtf8Encoder(), getU32Encoder())],
17+
['houseNumber', getU8Encoder()],
18+
['street', addEncoderSizePrefix(getUtf8Encoder(), getU32Encoder())],
19+
['city', addEncoderSizePrefix(getUtf8Encoder(), getU32Encoder())],
20+
]);
21+
22+
export function createCreateAddressInfoInstruction(
23+
addressInfoAccount: TransactionSigner,
24+
payer: TransactionSigner,
25+
programId: Address,
26+
addressInfo: AddressInfo,
27+
) {
28+
return {
29+
programAddress: programId,
30+
accounts: [
31+
{
32+
address: addressInfoAccount.address,
33+
role: AccountRole.WRITABLE_SIGNER,
34+
signer: addressInfoAccount,
35+
},
36+
{ address: payer.address, role: AccountRole.WRITABLE_SIGNER, signer: payer },
37+
{ address: SYSTEM_PROGRAM_ADDRESS, role: AccountRole.READONLY },
38+
],
39+
data: createAddressInfoEncoder.encode(addressInfo),
40+
};
41+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './create';
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { addDecoderSizePrefix, getStructDecoder, getU8Decoder, getU32Decoder, getUtf8Decoder } from '@solana/kit';
2+
3+
export type AddressInfo = {
4+
name: string;
5+
houseNumber: number;
6+
street: string;
7+
city: string;
8+
};
9+
10+
// Account data layout, matching the program's `AddressInfo` struct.
11+
export const addressInfoDecoder = getStructDecoder([
12+
['name', addDecoderSizePrefix(getUtf8Decoder(), getU32Decoder())],
13+
['houseNumber', getU8Decoder()],
14+
['street', addDecoderSizePrefix(getUtf8Decoder(), getU32Decoder())],
15+
['city', addDecoderSizePrefix(getUtf8Decoder(), getU32Decoder())],
16+
]);

basics/checking-accounts/anchor/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
"@solana/web3.js": "^1.98.4"
1111
},
1212
"devDependencies": {
13-
"@types/bn.js": "^5.1.0",
1413
"@types/chai": "^5.2.3",
1514
"@types/mocha": "^10.0.10",
1615
"anchor-litesvm": "^0.2.1",

0 commit comments

Comments
 (0)