📢 Gate Square #Creator Campaign Phase 1# is now live – support the launch of the PUMP token sale!
The viral Solana-based project Pump.Fun ($PUMP) is now live on Gate for public sale!
Join the Gate Square Creator Campaign, unleash your content power, and earn rewards!
📅 Campaign Period: July 11, 18:00 – July 15, 22:00 (UTC+8)
🎁 Total Prize Pool: $500 token rewards
✅ Event 1: Create & Post – Win Content Rewards
📅 Timeframe: July 12, 22:00 – July 15, 22:00 (UTC+8)
📌 How to Join:
Post original content about the PUMP project on Gate Square:
Minimum 100 words
Include hashtags: #Creator Campaign
Solana Web3.js version 2.x released: Function splitting and performance optimization
Solana Web3.js 2.x Version: Feature Split and Performance Optimization
Solana Web3.js, as a feature-rich JavaScript library, officially released version 2.x in November. Compared to version 1.x, the new version has undergone significant changes. This article will summarize its main changes.
Although version 2.x has just been released and its usage is not yet widespread, and many widely used libraries have not been migrated, understanding these changes will be helpful for future migration work.
Version Comparison
The old version is relatively simple to use, containing only one @solana/web3.js package, with all functions concentrated in it. It is based on a class design, encapsulating a large number of commonly used operations. For example, the Connection class provides dozens of methods that cover almost all the functions developers need. In addition, the large number of example codes provided in the Solana cookbook also offers convenience to developers.
However, this design also brings some issues. Although the features actually used by developers may only account for a small part, the entire codebase will be downloaded to the user's device. Due to the large amount of code in the library, this may take some time.
The 2.x version has adopted a different strategy. The official team has split the original codebase into several smaller modules, such as @solana/accounts, @solana/codecs, @solana/rpc, @solana/signers, @solana/transactions, etc. At the same time, it has abandoned class-based implementations in favor of a more functional approach. This change is beneficial for optimizing JavaScript code during build time, as unused code will be removed and will not be downloaded to the user's device. According to statistics from the official documentation, DApps using the new version can achieve approximately 30% size optimization, and if only a few functions are used, the optimization ratio may be even higher.
This change has raised higher demands for the quality of the Solana team's documentation, making it an important topic on how to help developers quickly find the features they need. Currently, package names have good semantic meaning, allowing users to roughly understand their purposes from their names, which to some extent reduces the difficulty of migration for developers.
Due to the recent release of the new version, many projects have not yet migrated. There are also relatively few examples of version 2.x on the Solana Cookbook. In addition, the new version tends to use runtime built-in features (such as generating key pairs), but the documentation lacks adequate descriptions, which may leave developers confused in certain areas.
One important feature of version 2.x is zero dependencies. This may not be as significant for many users, but considering the supply chain attacks that occurred in early December on @solana/web3.js versions 1.95.5 and 1.95.6, more external inputs and dependencies would greatly increase the likelihood of security incidents. With the release of version 2.x, the Web3.js development team decided to rely more on native functionalities, eliminating the introduction of external dependencies and polyfills. While there may be changes in the future, version 2.x has currently removed all external dependencies.
Important Changes
Connect
In version 1.x, the Connection class provides a large number of methods. Its main function is to create a request sender by configuring the RPC request address, and then use it to send various requests.
Version 2.x adopts a more functional approach to implementation:
javascript import { createSolanaRpc } from "@solana/web3.js";
const rpc = createSolanaRpc("");
When calling sendAndConfirmTransaction to send a transaction, an HTTPS request will be automatically initiated, and a WSS connection will be established to subscribe to the transaction status, returning the transaction hash after the transaction is confirmed.
key pair
There have also been significant changes to the parts related to public keys and private keys. The Keypair and PublicKey classes commonly used in version 1.x no longer exist and have been replaced by some functions.
For example, you can use await generateKeyPair() to generate a key pair, instead of the previous Keypair.generate().
The new generateKeyPair returns a Promise because the new implementation takes advantage of the JavaScript Web Crypto API as much as possible, using the native Ed25519 implementation. Many methods of the Web Crypto API are asynchronous. However, this change should not cause too much trouble for JavaScript developers familiar with Promises.
Send transaction
The commonly used Transaction and VersionedTransaction classes in version 1.x no longer exist in version 2.x.
The methods related to the System Program provided in the old version no longer exist, and the static methods on the SystemProgram class need to be imported from elsewhere.
For example, the transfer instruction needs to call the getTransferSolInstruction function in @solana-program/system.
Due to the discontinuation of the class, Web3.js provides a pipe format commonly used in functional programming. Below is an example of implementing the original 1.x transfer function using the pipe function:
javascript import { pipe } from "@solana/functional"; import { getTransferSolInstruction } from "@solana/system"; import { createSolanaRpc, sendTransaction, setTransactionFeePayer, createTransaction, appendTransactionInstruction, } from "@solana/web3.js";
const rpc = createSolanaRpc("");
const transaction = pipe( createTransaction(), setTransactionFeePayer(payer.address), appendTransactionInstruction( getTransferSolInstruction({ fromAddress: payer.address, toAddress: recipient, amount, }) ) );
const signature = await sendTransaction(rpc, transaction, [payer]);
Compared to version 1.x, the amount of code has increased, but the customizability is stronger.
Transactions are initiated via HTTPS RPC and then confirmed through subscription to WSS RPC. The new method has a higher dependency on WSS, which also places greater demands on the service stability of RPC providers.
React
The @solana/web3.js project also includes a library called @solana/react, which provides some React Hooks with built-in functions like signIn.
Summary
The release of version 2.x of @solana/web3.js reflects the Solana team's commitment to continuous development and improvement. It provides developers with an efficient, flexible, and customizable way to interact with the Solana network, helping to drive the adoption and growth of the platform.