Back to Blog

Web3.js is here

Table of Contents

Highlights

Now Available: Solana JavaScript SDK 2.0 Release Candidate

Written By

Anza Developers

August 1, 2024

If you build JavaScript applications on Solana, it’s likely that you’ve worked with @solana/web3.js or a library powered by it. With 350K+ weekly downloads on npm, it’s the most-used library in the ecosystem for building program clients, web applications, backend services, mobile apps, and more.

In response to feedback from developers, we began a process of modernizing the library to prepare for the next generation of Solana applications. A Release Candidate of the new web3.js is now available.

This is an open invitation to test it out before the final release. Your feedback in these final moments will help us catch any bugs and rough edges that we missed during development.

What’s new?

Speed

Cryptographic operations like generating keypairs, signing transactions, and verifying messages are up to 10x faster in the new web3.js, thanks to its use of native Ed25519 cryptography APIs built into modern JavaScript runtimes like Node and Safari 17.

Efficiency

Applications that you develop with the new web3.js will be smaller, faster to load, and will have fewer dependencies. The new library itself is zero-dependency and can be tree-shaken by an optimizing compiler to reduce your bundle to only the parts of web3.js that you actually use.

Flexibility

Wherever your requirements deviate from the defaults we’ve set, you can compose our new networking, transaction confirmation, codec, and signing primitives with your own custom code. It’s newly possible to define RPC instances with custom methods, specialized network transports, custom transaction signers, and more, and we hope that you will open source them when you do.

Program Clients

New TypeScript clients for core on-chain programs are now available in the @solana-program org on GitHub. These clients are autogenerated from Kinobi IDLs meaning you can also use Kinobi to generate clients for your own programs. To get started, we recommend using the brand new pnpm create solana-program script that spins up a new program with autogenerated clients out-of-the-box.

Quick Start

Fetch Data

import { fetchMint } from "@solana-program/token";
import { address, createSolanaRpc, mainnet } from "@solana/web3.js";

// Create an RPC instance
const rpc = createSolanaRpc(mainnet("https://api.mainnet-beta.solana.com"));

// Use it to fetch and decode a mint account
const mint = await fetchMint(
    rpc,
    address("EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v" /* USDC */),
);

console.log(mint.data.supply); // 2747191815831469n

Create a Transaction

import { getAddMemoInstruction } from "@solana-program/memo";
import {
    appendTransactionMessageInstruction,
    createKeyPairSignerFromBytes,
    createTransactionMessage,
    pipe,
    setTransactionMessageFeePayerSigner,
    setTransactionMessageLifetimeUsingBlockhash,
    signTransactionMessageWithSigners,
} from "@solana/web3.js";

// Fetch a recent blockhash
const { value: latestBlockhash } = await rpc
    .getLatestBlockhash({ commitment: "confirmed" })
    .send();

// Create a transaction signer
const signer = await createKeyPairSignerFromBytes(
    new Uint8Array(JSON.parse(process.env.KEY_PAIR_BYTES)),
);

// Build up a transaction message
const message = pipe(
    createTransactionMessage({ version: 0 }),
    (m) => setTransactionMessageFeePayerSigner(signer, m),
    (m) => setTransactionMessageLifetimeUsingBlockhash(latestBlockhash, m),
    (m) =>
        appendTransactionMessageInstruction(
            getAddMemoInstruction({ memo: "Hello from the new web3.js!" }),
            m,
        ),
);

// Self-sign the message using its included signers
const signedTransaction = await signTransactionMessageWithSigners(message);

console.log(signedTransaction);
// {
//     lifetimeConstraint: {
//         blockhash: 'GrWXyubEeZgdv5khuHtJ1KaPskSQ34dFME4dsZrBJDSv',
//         lastValidBlockHeight: 259936038n
//     },
//     messageBytes: Uint8Array(133) [ ... ],
//     signatures: {
//         '5CJ13VfdDCE5EyqzobnYptjgGtMdH3rqkqLHNWsJi51W': Uint8Array(64) [ ... ],
//     }
// }

Send And Confirm a Transaction

import {
    createSolanaRpc,
    createSolanaRpcSubscriptions,
    getSignatureFromTransaction,
    mainnet,
    sendAndConfirmTransactionFactory,
} from "@solana/web3.js";

// Create an RPC instance to send the transaction, and a RPC
// subscriptions instance to listen for signature confirmation
const rpc = createSolanaRpc(mainnet("https://api.mainnet-beta.solana.com"));
const rpcSubscriptions = createSolanaRpcSubscriptions(
    mainnet("wss://api.mainnet-beta.solana.com"),
);

// Create a transaction sender with the default confirmation strategy
const sendAndConfirmTransaction = sendAndConfirmTransactionFactory({
    rpc,
    rpcSubscriptions,
});

/* ...build and sign a transaction as above... */

console.info(
    "Sending transaction: https://explorer.solana.com/tx/" +
        getSignatureFromTransaction(signedTransaction),
);

try {
    await sendAndConfirmTransaction(signedTransaction, {
        commitment: "confirmed",
    });
    console.info("Transaction confirmed!");
} catch (e) {
    console.error("Transaction failed", e);
}

Try It

Install the Release Candidate using the rc tag.

> npm install @solana/web3.js@rc

Give Feedback

Before the general release of web3.js 2.0, shortly before Breakpoint 2024 in September, we want to collect as much feedback as possible from you. If you find a bug, are missing a feature, or would like an API modified, file a GitHub Issue.

Learn More

Watch the original announcement of the web3.js rewrite at Breakpoint 2023.