Exploring Solana Web3.js 2.0: Key New Features and Enhancements

ยท

Solana Web3.js 2.0 represents a major evolution in the JavaScript SDK for building applications on the Solana blockchain. This update introduces significant improvements aimed at enhancing developer experience, reducing bundle sizes, and increasing overall performance.


Enhanced Tree-Shakability for Optimized Builds

One of the most impactful changes in Web3.js 2.0 is its full support for tree-shaking. Unlike the previous object-oriented design, which made it difficult for compilers to eliminate unused code, the new modular structure allows bundlers to exclude any unused functions or modules.

This results in:

The library is now organized into smaller, independent packages under the @solana namespace, such as:

Developers can either use the default configuration from the main @solana/web3.js package or import specific sub-packages to tailor their setup.


Composable Internals for Customization

Web3.js 2.0 exposes more internal APIs, giving developers greater control over functionality such as:

This composability allows for highly customized implementations, enabling developers to optimize their applications for specific use cases and requirements.


Modern JavaScript and Zero Dependencies

The new version leverages modern JavaScript features, resulting in:

These changes not only reduce the library's size but also improve performance and security.


Functional Architecture

Replacing the class-based architecture of Web3.js 1.x, version 2.0 adopts a functional programming approach. This eliminates issues like double-package hazards and reduces unnecessary bloat, ensuring that applications only include the code they actually use.


Performance and Bundle Size Improvements

Web3.js 2.0 offers substantial gains in both performance and efficiency:

Metric1.x (Legacy)2.0Improvement
Minified library size81 KB57.5 KB-29%
Minified size (with Ed25519 support)81 KB53 KB-33%
Bundled size for a lamport transfer111 KB23.9 KB-78%
Bundled size (with Ed25519 support)111 KB18.2 KB-83%
Cryptographic operations performance700 ops/s7000 ops/s+900%

These improvements are largely due to the use of modern JavaScript APIs and optimized internal structures.


Getting Started with Web3.js 2.0

To start using Web3.js 2.0, install the package via npm:

npm install @solana/web3.js@next

Basic RPC Request Example

import { createSolanaRpcFromTransport, createDefaultRpcTransport } from '@solana/web3.js';

const transport = createDefaultRpcTransport({ url: 'https://api.devnet.solana.com' });
const rpc = createSolanaRpcFromTransport(transport);
const slot = await rpc.getSlot().send();

Implementing a Round-Robin Transport

For applications requiring high availability, a round-robin transport can distribute requests across multiple RPC endpoints:

const transports = [
    createDefaultRpcTransport({ url: 'https://mainnet-beta.my-server-1.com' }),
    createDefaultRpcTransport({ url: 'https://mainnet-beta.my-server-2.com' }),
    createDefaultRpcTransport({ url: 'https://mainnet-beta.my-server-3.com' }),
];

let nextTransport = 0;

async function roundRobinTransport(...args) {
    const transport = transports[nextTransport];
    nextTransport = (nextTransport + 1) % transports.length;
    return await transport(...args);
}

const rpc = createSolanaRpcFromTransport(roundRobinTransport);

Sharding Transport Example

A sharding transport can route specific types of requests to dedicated servers:

const transportA = createDefaultRpcTransport({ url: 'https://mainnet-beta.my-server-1.com' });
const transportB = createDefaultRpcTransport({ url: 'https://mainnet-beta.my-server-2.com' });

function selectShard(method) {
    switch (method) {
        case 'getAccountInfo':
        case 'getBalance':
            return transportA;
        case 'getLatestBlockhash':
        case 'getTransaction':
            return transportB;
        default:
            return transportA;
    }
}

async function shardingTransport(...args) {
    const method = args[0].payload.method;
    const selectedTransport = selectShard(method);
    return await selectedTransport(...args);
}

const rpc = createSolanaRpcFromTransport(shardingTransport);

Frequently Asked Questions

What is tree-shaking and why is it important?
Tree-shaking is a build-time optimization that removes unused code from the final bundle. This reduces application size, improves load times, and lowers bandwidth usage, making it crucial for web-based applications.

How does Web3.js 2.0 improve performance?
By leveraging modern JavaScript features like native Ed25519 support and bigint, and by eliminating third-party dependencies, Web3.js 2.0 significantly reduces bundle sizes and accelerates cryptographic operations.

Can I use Web3.js 2.0 with existing projects?
While Web3.js 2.0 introduces breaking changes, its modular design allows for incremental adoption. Developers can start by integrating specific modules like @solana/rpc or @solana/transactions without a full rewrite.

What are the benefits of composable internals?
Composable internals enable developers to customize RPC transports, retry logic, and other low-level behaviors. This flexibility allows for optimizations tailored to specific use cases, such as high-frequency trading or high-availability services.

Is Web3.js 2.0 compatible with all Solana networks?
Yes, Web3.js 2.0 supports all Solana networks, including Mainnet, Devnet, and Testnet. Developers can configure RPC endpoints to point to any compatible network.

Where can I learn more about advanced features?
For a comprehensive guide to the new API, including advanced configurations and use cases, ๐Ÿ‘‰ explore the official documentation.