🎉 Gate Square Growth Points Summer Lucky Draw Round 1️⃣ 2️⃣ Is Live!
🎁 Prize pool over $10,000! Win Huawei Mate Tri-fold Phone, F1 Red Bull Racing Car Model, exclusive Gate merch, popular tokens & more!
Try your luck now 👉 https://www.gate.com/activities/pointprize?now_period=12
How to earn Growth Points fast?
1️⃣ Go to [Square], tap the icon next to your avatar to enter [Community Center]
2️⃣ Complete daily tasks like posting, commenting, liking, and chatting to earn points
100% chance to win — prizes guaranteed! Come and draw now!
Event ends: August 9, 16:00 UTC
More details: https://www
Solana Web3.js version 2.x released, optimizing code size drop by 30%
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.
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.