Solana Web3.js 2.0 Upgrade Analysis: Modular Design Improves Performance and Security

Solana Web3.js 2.x Version: A Feature-Rich Upgrade to the JavaScript Library

Solana Web3.js, as a feature-rich JavaScript library, officially released version 2.x in November this year. Compared to version 1.x, the new version brings many significant changes. This article will summarize and analyze these main changes.

Although version 2.x has just been released and its usage is currently low, many widely used libraries have not yet switched, but understanding these changes is crucial for future migration work.

Version Comparison

The usage of version 1.x is relatively simple. It only contains one package: @solana/web3.js, where all functionalities are concentrated. The class-based design encapsulates a large number of commonly used operations, such as the Connection class which provides dozens of methods, almost covering all the functionalities required by developers.

However, this design also brings some issues. Although developers usually only use a small portion of the features, the entire codebase will be downloaded to the user's device, which may take some time due to the large amount of code in the library.

The 2.x version adopts a different approach. The official team has split 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 implementation and adopts a more function-based approach. This change is beneficial for the optimization of JavaScript code during construction, as unused code will be removed and will not be downloaded to the user's device. According to official documentation statistics, DApps using the new version can generally achieve a 30% optimization in code size, and if only a small number of features are used, the optimization ratio may be even higher.

Web3 Beginner Series: Now Upgrade @solana/web3.js 2.x to Enable Functional Programming

This change raises higher demands for the quality of documentation from the Solana team. Helping developers quickly find the features they need has become a key issue. Currently, the package names have good semantics, and their purposes can be roughly understood from their names, which alleviates the difficulty of migration for developers to some extent.

However, due to the recent release, 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 functions (such as generating key pairs), but the documentation lacks detailed descriptions of these parts, which causes confusion for developers.

Another important feature of version 2.x is zero dependencies. While this may not be the most important aspect for many users, the supply chain attack that occurred in early December this year on versions 1.95.5 and 1.95.6 of @solana/web3.js shows that more external inputs and dependencies significantly increase the likelihood of security incidents. With the release of version 2.x, the Web3.js development team has decided to rely more on native features, eliminating external dependencies and the introduction of Polyfills. Although changes may occur in the future, version 2.x has currently eliminated all external dependencies.

Important Changes

connection

In version 1.x, the Connection class provides a large number of methods. Although 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 implement this feature:

javascript import { createSolanaRpc } from "@solana/web3.js";

const rpc = createSolanaRpc("");

When calling sendAndConfirmTransaction to send a transaction, the system will automatically initiate an HTTPS request and establish a WSS connection to subscribe to the transaction status, returning the transaction hash after the transaction is confirmed.

key pair

The parts related to public keys and private keys have also undergone significant changes. The commonly used Keypair and PublicKey classes 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, instead of the previous 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 leverages the JavaScript Web Crypto API as much as possible, utilizing the native Ed25519 implementation. Many methods of the Web Crypto API are asynchronous. However, this change is not unacceptable, as JavaScript developers have become very familiar with Promises by the end of 2024.

Send Transaction

Users of version 1.x should be very familiar with the Transaction and VersionedTransaction classes. In version 2.x, these two classes no longer exist.

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.

Due to the discontinuation of classes, Web3.js provides a pipe format commonly used in functional programming. Below is an example of using the pipe function to implement the original 1.x transfer function:

javascript import { pipe } from '@solana/functional'; import { generateKeyPair } from '@solana/web3.js'; import { getTransferSolInstruction } from '@solana/system-program'; import { createTransaction } from '@solana/transactions';

const transaction = pipe( createTransaction(), addInstruction(getTransferSolInstruction({ fromPubkey: sender.publicKey, toPubkey: recipient, lamports: amount, })), setRecentBlockhash(blockhash), addSigners([sender]) );

const signature = await rpc.sendAndConfirmTransaction(transaction);

It can be seen that transactions are no longer initiated through Connection, but rather by generating a specific function through our defined RPC Provider, and then calling that function to initiate the transaction. Compared to version 1.x, the amount of code has increased, but the customizability has become stronger.

Transactions are initiated via HTTPS RPC and then confirmed through subscriptions to WSS RPC. It can be felt that the new method relies heavily on WSS, and it is believed that the application of WSS will become increasingly widespread in the future, which also places higher demands on the service stability of RPC providers.

React

Interestingly, the @solana/web3.js project also includes a library called @solana/react, which provides some React Hooks with built-in features like signIn.

Summary

The release of version 2.x of @solana/web3.js fully 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. Although current usage is low, over time, it is believed that more and more projects will migrate to this new version.

Web3 Beginner Series: Upgrade @solana/web3.js 2.x Now to Enable Functional Programming

SOL0.63%
View Original
This page may contain third-party content, which is provided for information purposes only (not representations/warranties) and should not be considered as an endorsement of its views by Gate, nor as financial or professional advice. See Disclaimer for details.
  • Reward
  • 4
  • Share
Comment
0/400
BearMarketSurvivorvip
· 16h ago
Frontline report: The upgrade seems to be the first shot fired in an empty nest.
View OriginalReply0
OPsychologyvip
· 16h ago
Don't rush to switch if the upgrade isn't stable yet.
View OriginalReply0
RugResistantvip
· 16h ago
hmm potential security hotspots detected... need thorough audit tbh
Reply0
mev_me_maybevip
· 16h ago
Ah, this Solana has also started playing with modularization.
View OriginalReply0
Trade Crypto Anywhere Anytime
qrCode
Scan to download Gate app
Community
English
  • 简体中文
  • English
  • Tiếng Việt
  • 繁體中文
  • Español
  • Русский
  • Français (Afrique)
  • Português (Portugal)
  • Bahasa Indonesia
  • 日本語
  • بالعربية
  • Українська
  • Português (Brasil)