🎉 [Gate 30 Million Milestone] Share Your Gate Moment & Win Exclusive Gifts!
Gate has surpassed 30M users worldwide — not just a number, but a journey we've built together.
Remember the thrill of opening your first account, or the Gate merch that’s been part of your daily life?
📸 Join the #MyGateMoment# campaign!
Share your story on Gate Square, and embrace the next 30 million together!
✅ How to Participate:
1️⃣ Post a photo or video with Gate elements
2️⃣ Add #MyGateMoment# and share your story, wishes, or thoughts
3️⃣ Share your post on Twitter (X) — top 10 views will get extra rewards!
👉
Solana Web3.js 2.x released: Modular design enhances performance and security.
Solana Web3.js 2.x Version: More Powerful Features, Better Performance
Solana Web3.js, as a feature-rich JavaScript library, officially launched version 2.x in November this year. This new version has undergone significant changes compared to version 1.x, and this article will summarize its main changes.
Although version 2.x has just been released and usage is still low, many widely used libraries have not yet switched, understanding these changes will be helpful for future migration work.
Version Comparison
The 1.x version is relatively simple to use, with only one package: @solana/web3.js, which consolidates all functionalities. It is based on a class design and encapsulates a large number of common operations. For example, the Connection class provides dozens of methods that almost cover all the functionalities required by developers.
However, this design also brings some problems. Although the functionalities actually used by developers may only account for a small part, the entire codebase will be downloaded to the user's device, and due to the large amount of code, it may lead to certain loading times.
In contrast, version 2.x splits the original codebase into multiple smaller modules, such as @solana/accounts, @solana/codecs, @solana/rpc, @solana/signers, @solana/transactions, etc. At the same time, the new version abandons class-based implementations and adopts a more function-oriented approach. This design is beneficial for optimizing JavaScript code during build time, as unused code will be removed and not downloaded to user devices. According to official documentation statistics, DApps using the new version typically achieve a 30% size optimization, and if only a few features are used, the optimization ratio may be even higher.
This change presents new challenges for the Solana team's documentation writing, as how to enable developers to quickly find the required functions has become an important issue. However, it seems that the naming of various packages has good semantic clarity, allowing one to roughly understand their purposes from their names, which to some extent reduces the difficulty of migration for developers.
Since the new version was just released recently, many projects have not yet migrated. There are also 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 of these parts, which may confuse developers.
Another important feature of version 2.x is zero dependencies. This may not be very important to many users, but based on the supply chain attacks that occurred in early December this year on @solana/web3.js versions 1.95.5 and 1.95.6, increased external inputs and dependencies can significantly raise the likelihood of security incidents. With the release of version 2.x, the Web3.js development team has decided to leverage more native functionalities and eliminate 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
connection
In version 1.x, Connection provides a large number of methods. However, its main function is still to create a request sender by configuring the RPC request address, and then using it to send various requests.
The 2.x version 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. The transaction hash will be returned after the transaction is confirmed.
key pair
There are significant changes 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 now use await generateKeyPair() to generate a key pair, whereas before it was generated directly through Keypair.generate().
It is worth noting that the new generateKeyPair returns a Promise instead of directly returning a key pair. This is because the new implementation utilizes the Web Crypto API of JavaScript as much as possible, using the native Ed25519 implementation. Many methods of the Web Crypto API are asynchronous. However, this change is not unacceptable; as we approach the end of 2024, JavaScript developers are already very familiar with Promises.
Send transaction
The 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, so the static methods on the SystemProgram class need to be imported from elsewhere.
For example, the transfer instruction now requires calling the getTransferSolInstruction function in @solana-program/system.
Since classes are no longer provided, Web3.js offers a pipe format commonly used in functional programming. Below is an example of using the pipe function to implement the original 1.x transfer functionality:
javascript import { pipe } from "@solana/web3.js"; import { getTransferSolInstruction } from "@solana-program/system";
const transaction = pipe( createTransaction({ version: 0 }), addInstruction( getTransferSolInstruction({ fromPublicKey, toPublicKey, lamports, }) ) );
It can be seen that transactions are no longer initiated through Connection, but instead a unique function is generated through the RPC Provider we defined, and then this function is called to initiate the transaction. 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. It can be felt that the new method is highly dependent on WSS, and it is believed that the application of WSS will become more and more widespread in the future, which indeed raises higher requirements for the service stability of RPC providers.
React
It is worth mentioning that the @solana/web3.js project also includes a library called @solana/react, which provides some React Hooks with built-in functionalities such as signIn.
Summary
The release of version 2.x of @solana/web3.js fully demonstrates 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.