Solana Web3.js is a very feature-rich JavaScript library, and version 2.x was officially released in November this year. Compared with 1.x, the new version has changed a lot, and this article attempts to summarize its main changes.

Currently, since version 2.x has just been released, the usage is not high, and many highly used libraries have not been switched. However, we can learn about it first and prepare for future migration.

Version comparison

I have to admit that the old version is actually easier to use. First of all, the old version actually has only one package: @solana/web3.js. All the content is in it. And it is based on classes, encapsulating a lot of commonly used operations. For example, the Connection class has dozens of methods, which basically covers the functions that developers want. And Solana cookbook provides a lot of sample code, developers can always find what they need here.

However, this also creates other problems: although developers often only use a small part of the features, the entire code base will eventually be downloaded to the user's device. Due to the large amount of code in the entire library, this may take a short time.

In contrast, in version 2.x, the official team split the original code base into several small modules, such as @solana/accounts, @solana/codecs, @solana/rpc, @solana/signers, @solana/transactions, etc. And abandoned the class-based implementation, and more single functions were used, which is very helpful for optimizing JavaScript code construction. Code not used by DApp will be deleted and will not actually be downloaded to the user's device. According to official documentation, using the new version of DApp can basically achieve 30% size optimization. If only a small part of the functions are used, a higher optimization ratio can be achieved ( https://solana-labs.github.io/solana-web3.js/#statistics ).

This puts the Solana team's documentation level to the test. How to enable developers to quickly find the functions they need will be an important issue. However, at least the package names have good semantics, and you can roughly know what they can be used for from the names. This should reduce the difficulty of developer migration to a certain extent.

Of course, since it was just released, many projects have not yet migrated. There are also relatively few examples for version 2.x in the Solana Cookbook. And because the new version tends to use built-in runtime functions (such as generating key pairs), the documentation lacks descriptions of this, which makes developers feel a little confused in some places.

2.x also has a very important feature: zero dependency. This may not be important to many users, but judging from the supply chain attack on @solana/web3.js 1.95.5 and 1.95.6 in early December this year, more external inputs and dependencies will rapidly increase the possibility of security incidents. With the release of version 2.x, the Web3.js development team decided to use more native functions and cancel the introduction of external dependencies and Polyfills. There may be changes in the future, but at least for now, version 2.x eliminates all external dependencies.

Important changes

connect

As mentioned above, 1.x has a lot of methods provided by Connection. However, its main function is to create a request sender by configuring the RPC request address, and then send various requests through it.

In 2.x, this is done in a more functional way:

Web3 Beginner Series: Upgrade to @solana/web3.js 2.x now to start functional programming

In the above code, when we call "sendAndConfirmTransaction" to send a transaction, an HTTPS request will be automatically initiated, a WSS connection will be established, the transaction status will be subscribed, and the transaction hash will be returned after the transaction is confirmed.

Key Pair

There are also major changes in public and private key related areas. The two most commonly used Keypair and PublicKey classes in version 1.x no longer exist and have been replaced by some functions.

For example, you can use "await generateKeyPair()" to generate a key pair, but before that you could directly generate a key pair through "Keypair.generate()".

You may have noticed that the new generateKeyPair returns a Promise, instead of returning the required key pair directly as before. This is because the new implementation makes use of JavaScript's Web Crypto API 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 2024 is coming to an end, JavaScript developers have already regarded Promise as a very familiar friend.

Sending transactions

Users of 1.x should be familiar with the two classes "Transaction" and "VersionedTransaction". When I first learned about Solana, I was also very confused about the relationship between them.

In version 2.x, these two classes no longer exist.

The System Program related methods provided in the old version no longer exist, so the static methods on the "SystemProgram" class need to be introduced from other places.

For example, the "transfer" instruction needs to call the "getTransferSolInstruction" function in "@solana-program/system".

Since classes are no longer provided, Web3.js provides the "pipe" form commonly used in functional programming capabilities. The following uses the pipe function to implement the original 1.x transfer function:

Web3 Beginner Series: Upgrade to @solana/web3.js 2.x now to start functional programming

It can be found that transactions are no longer initiated through Connection, but through the RPC Provider we defined, generating a unique function, and then calling this function to initiate transactions. Compared with version 1.x, the amount of code has increased a bit, but the advantage is that it is more customizable.

Transactions are initiated through HTTPS RPC, and then the transaction results are confirmed by subscribing to WSS RPC. It can be felt that the new method is very dependent on WSS. I believe that the application of WSS will become more and more extensive in the future, which does put higher requirements on the service stability of RPC providers. If you are looking for a reliable WSS node provider, ZAN Node ( https://zan.top/home/node-service?chInfo=ch_WZ ) is a good choice. As Solana's leading service provider in the Asia-Pacific region, we are committed to providing stable and efficient connection performance. In addition to popular chains such as Ethereum and Solana, we also support RPC services for more than 20 mainstream chains to meet the needs of different scenarios.

React

Interestingly, the @solana/web3.js project also includes a library called @solana/react that provides some React Hooks with built-in functions such as signIn.

Summarize

The release of @solana/web3.js 2.x 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 and helps drive adoption and growth of the platform.

This article was written by gin-lsl from ZAN Team (X account @zan_team ).