Solana Web3.js version 2.x released, optimizing code size drop by 30%

robot
Abstract generation in progress

Solana Web3.js 2.x Version: Major Upgrade of the Feature-Rich JavaScript Library

Solana Web3.js, as a feature-rich JavaScript library, officially released version 2.x this November. Compared to version 1.x, the new version has significant changes in structure and usage. This article will provide an overview of these major changes to help developers prepare for future migration.

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

Version Comparison

The 1.x version of the Web3.js library is relatively simple and straightforward to use. It has only one main package: @solana/web3.js, which centralizes all functionalities. It adopts a class-based design and encapsulates a large number of commonly used operations. For example, the Connection class provides dozens of methods, almost covering all the functionalities needed by developers.

However, this design also brings some issues. Although the features that developers actually use may only be a small part of the library, the entire codebase will be downloaded to the user's device, which may lead to longer loading times.

The 2.x version adopts a different approach. The development team has split the original codebase into several smaller modules, such as @solana/accounts, @solana/codecs, @solana/rpc, @solana/signers, and @solana/transactions. The new version also abandons class-based implementations in favor of a more function-based approach. This method is beneficial for optimizing JavaScript code during build time, as unused code can be removed and will not be downloaded to the user's device.

According to statistics, using the new version of decentralized applications (DApps) can usually achieve about a 30% optimization in code size. If only a small number of functions are used, the optimization ratio may be even higher.

Another important feature of version 2.x is zero dependencies. This characteristic may not be significant for many users, but considering the recent supply chain attacks on @solana/web3.js versions 1.95.5 and 1.95.6, reducing external dependencies can significantly lower security risks. The Web3.js development team decided to rely more on native features and eliminate the introduction of external dependencies and polyfills.

Important Changes

Connect

In version 2.x, the connection method has become more functional:

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

In version 2.x, the original Keypair and PublicKey classes have been replaced by a series of functions. For example, you can use await generateKeyPair() to generate a key pair, instead of the previous Keypair.generate(). The new implementation leverages the JavaScript Web Crypto API and utilizes the native Ed25519 implementation.

Send the transaction

The 2.x version no longer uses the Transaction and VersionedTransaction classes. Methods related to the System Program also need to be imported from elsewhere. For example, the transfer instruction now requires calling the getTransferSolInstruction function from @solana-program/system.

The new version provides the commonly used pipe format in functional programming. Below is an example of implementing the transfer function using the pipe function:

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

const transaction = pipe( createTransaction({ version: 0 }), addInstruction(getTransferSolInstruction({ from: sender, to: recipient, amount: transferAmount, })), setTransactionFeePayer(sender), );

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

React support

It is worth noting that 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 showcases the Solana team's commitment to continuous development and improvement. The new version provides developers with a more efficient, flexible, and customizable way to interact with the Solana network, which is expected to drive further adoption and development of the platform.

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

SOL-4.23%
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
rekt_but_resilientvip
· 07-11 16:59
Finally upgraded, development has been so painful.
View OriginalReply0
ThatsNotARugPullvip
· 07-08 18:57
Finally succeeded in losing weight.
View OriginalReply0
CompoundPersonalityvip
· 07-08 18:57
Finally, I don't have to code so much anymore.
View OriginalReply0
BtcDailyResearchervip
· 07-08 18:45
Reliable modularization is the way to go.
View OriginalReply0
gas_fee_traumavip
· 07-08 18:30
Solana has become stronger again.
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)