Solana Web3.js version 2.x: Major Upgrade in Modular Design and Functional Programming

robot
Abstract generation in progress

Solana Web3.js 2.x Version: A Brand New Functional Programming Experience

Solana Web3.js, as a feature-rich JavaScript library, officially released version 2.x this November. Compared to version 1.x, the new version brings a series of significant changes, and this article will summarize its main changes.

Although the 2.x version has just been released and its usage is not yet widespread, many commonly used libraries have not yet migrated. However, understanding these changes in advance will greatly benefit future migration efforts.

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

Version Comparison

It is undeniable that the old version was simpler and more straightforward to use. The 1.x version only includes a single @solana/web3.js package, with all functions concentrated within 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, covering almost all the functionalities that developers need. In addition, the rich example code in the Solana cookbook also provides great convenience for 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, and due to the large amount of code in the library, this may lead to certain loading times.

In contrast, version 2.x adopts a modular design approach. 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, the new version has abandoned class-based implementations in favor of a single function approach, which greatly aids in optimizing JavaScript code construction. Unused code will be removed and will not be downloaded to the user's device. According to official documentation, DApps using the new version generally achieve a 30% reduction in size, and if only a small number of features are used, the optimization ratio may be even higher.

This change also places higher demands on the quality of the Solana team's documentation. How to enable developers to quickly locate the required functions has become a key issue. Currently, the naming of various packages has good semantic meaning, allowing one to roughly infer their purposes from the names, which has, to some extent, reduced the difficulty of migration for developers.

However, since the new version was just released not long ago, many projects have not yet completed the migration. The examples on the Solana Cookbook for version 2.x are also relatively limited. In addition, the new version tends to use runtime built-in features (such as generating key pairs), but the documentation does not provide detailed descriptions of these parts, which may confuse developers.

Another important feature of version 2.x is zero dependencies. This may not have a significant impact on ordinary users, but given the supply chain attacks that occurred in early December this year on versions 1.95.5 and 1.95.6 of @solana/web3.js, excessive external inputs and dependencies can significantly increase the risk of security incidents. With the release of version 2.x, the Web3.js development team has decided to leverage native features more and reduce the introduction of external dependencies and polyfills. While changes may occur in the future, the current version 2.x has eliminated all external dependencies.

Important Changes

connection

In version 1.x, the Connection class provides a large number of methods. However, its core functionality remains to create a request sender by configuring the RPC request address, and then sending various requests through it.

The 2.x version adopts a more functional approach to achieve this feature:

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

const rpc = createSolanaRpc("");

When we call sendAndConfirmTransaction to send a transaction, it automatically initiates an HTTPS request and establishes a WSS connection to subscribe to the transaction status, returning the transaction hash after the transaction is confirmed.

key pair

There have also been significant changes related to public keys and private keys. The commonly used Keypair and PublicKey classes in version 1.x no longer exist, replaced by some functions.

For example, you can 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 the key pair. This is because the new implementation leverages JavaScript's Web Crypto API as much as possible, using the native Ed25519 implementation. Many methods of the Web Crypto API are asynchronous. However, for JavaScript developers who are familiar with Promises, this change should not be difficult to adapt to.

Send transaction

The Transaction and VersionedTransaction classes in version 1.x no longer exist in 2.x.

The methods related to System Program provided in the old version are no longer available, and 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 form 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/web3.js"; import { getTransferSolInstruction } from "@solana/system-program";

const transaction = pipe( createTransaction({ version: 0 }), addInstruction(getTransferSolInstruction({ fromPubkey: sender, toPubkey: recipient, lamports: amount })) );

const signature = await sendAndConfirmTransaction(rpc, transaction, [senderKeypair]);

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 improved.

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

React support

It is worth mentioning that the @solana/web3.js project also includes a library called @solana/react, which provides several React Hooks with built-in functionalities such as signIn.

Summary

The release of version 2.x of @solana/web3.js demonstrates the Solana team's commitment to continuous development and improvement. The new version provides developers with an efficient, flexible, and customizable way to interact with the Solana network, which is expected to drive the adoption and growth of the platform.

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

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
  • 5
  • Share
Comment
0/400
RugPullSurvivorvip
· 07-12 23:48
Changing it is equivalent to not changing it at all... it would be better to rewrite it completely.
View OriginalReply0
Degen4Breakfastvip
· 07-12 23:48
Finally got it up and running.
View OriginalReply0
digital_archaeologistvip
· 07-12 23:40
drop reliance on positive reviews
View OriginalReply0
AirdropHunterWangvip
· 07-12 23:32
Is this it? Are there still fewer features?
View OriginalReply0
metaverse_hermitvip
· 07-12 23:31
This migration is really a headache.
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)