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

[Improvement][tests] in tests project, the version of dependency "@solana/spl-token": "^0.1.8" is too old #3446

Open
yanCode opened this issue Dec 23, 2024 · 0 comments · May be fixed by #3447

Comments

@yanCode
Copy link
Contributor

yanCode commented Dec 23, 2024

The issue

under tests folder, in package.json file, you can find now it still using rather old version dependencies, which were published 4 years ago.

    "@project-serum/common": "^0.0.1-beta.3",
    "@project-serum/serum": "^0.13.60",
    "@solana/spl-token": "^0.1.8",

At this moment of writing this issue, the latest version of @solana/spl-token now is 0.4.9

What the new version of @solana/spl-token brings?

  1. Its functionality can replace @project-serum/common and @project-serum/serum which haven't been updated for 4 years. in current code it uses chunky SPL token client boilerplate code to implement some functionalities now support by the latest version of @solana/spl-token. for instance:

    // SPL token client boilerplate for test initialization. Everything below here is
    // mostly irrelevant to the point of the example.
    const serumCmn = require("@project-serum/common");
    async function getTokenAccount(provider, addr) {
    return await serumCmn.getTokenAccount(provider, addr);
    }
    async function getMintInfo(provider, mintAddr) {
    return await serumCmn.getMintInfo(provider, mintAddr);
    }
    async function createMint(tokenProgram) {
    const mint = anchor.web3.Keypair.generate();
    const authority = tokenProgram.provider.wallet.publicKey;
    const createMintIx = await tokenProgram.account.mint.createInstruction(mint);
    const initMintIx = await tokenProgram.methods
    .initializeMint2(0, authority, null)
    .accounts({ mint: mint.publicKey })
    .instruction();
    const tx = new anchor.web3.Transaction();
    tx.add(createMintIx, initMintIx);
    await tokenProgram.provider.sendAndConfirm(tx, [mint]);
    return mint.publicKey;
    }
    async function createTokenAccount(tokenProgram, mint, owner) {
    const vault = anchor.web3.Keypair.generate();
    const tx = new anchor.web3.Transaction();
    const createTokenAccountIx =
    await tokenProgram.account.account.createInstruction(vault);
    const initTokenAccountIx = await tokenProgram.methods
    .initializeAccount3(owner)
    .accounts({ account: vault.publicKey, mint })
    .instruction();
    tx.add(createTokenAccountIx, initTokenAccountIx);
    await tokenProgram.provider.sendAndConfirm(tx, [vault]);
    return vault.publicKey;
    }

  2. The new version comes with new functionality for Token-2022. for example, in current code when it needs to use ASSOCIATED_TOKEN_PROGRAM_ID and TOKEN_2022_PROGRAM_ID , it has to copy and paste in many parts , which results in many code duplications. like here

    const TOKEN_2022_PROGRAM_ID = new anchor.web3.PublicKey(
    "TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb"
    );

    but in the new version, it can direclty import from the package like

    import { ASSOCIATED_TOKEN_PROGRAM_ID, TOKEN_2022_PROGRAM_ID } from "@solana/spl-token";
  3. another example

    const ix = await addExtraAccountsToInstruction(
    provider.connection,
    createTransferCheckedInstruction(
    source,
    mint.publicKey,

    It uses two functions addExtraAccountsToInstruction & addExtraAccountsToInstruction to generate an instruction for checked transfer with transfer hook. But in the new version of @solana/spl-token you can do it in only 1 function call createTransferCheckedWithTransferHookInstruction

In General

As these tests are used for developers to learn about anchor, it's better to provide examples of latest version and up-to-date usages. rather than show the users the usage of old versions (which containing many broken changes) of 4 years ago. Otherwise the user has to learn again what changes has been made in past 4 years, that might make the learning curve deeper.

I spent a week of my free time to address this upgrade. I will submit the PR and brief the changes I made in the PR soon.

@yanCode yanCode linked a pull request Dec 23, 2024 that will close this issue
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

Successfully merging a pull request may close this issue.

1 participant