您的USDC正在按昨天的利率产生收益Quicknode 会自动将其转移到今日最佳的 Morpho 保险库中。已在 7 条链上上线。
制定您的策略Solana Web3.js 2.0: A New Chapter in Solana Development
The Solana JavaScript SDK 2.0 streamlines developer interactions with the Solana blockchain, offering more incredible speed, modularity, and ease of use.
通过 SOC 2 II 类认证 · ISO 27001

September 9, 2024 — 6 min read

With consistently over 350,000 downloads per week (src: npmtrends.com), the Solana JavaScript SDK (@solana/web3.js) is one of the most popular JavaScript libraries in all of Web3. It's used by thousands of developers to read, write, and execute logic on the Solana blockchain. If you are not a developer, you have likely used an application, wallet, or other tool that uses Solana JavaScript SDK under the hood.
In response to developer feedback on the Solana JavaScript SDK (v1.x), the Anza team (formerly known as Solana Labs) has set out to modernize the library, increase its performance, and improve the developer experience. Solana JavaScript SDK 2.0 is a complete overhaul of the library, designed to address many pain points developers have faced.
Here's a quick summary of the key features and improvements in Solana JavaScript SDK 2.0:
Smaller bundles and faster load times: The new library is tree-shakable and modular, allowing you to import only the functions you need, reducing your bundle size dramatically. This leads to faster load times for your dApps and reduced compute costs for serverless deployments.
Enhanced type safety: The new library leverages TypeScript's advanced features to provide a more type-safe development experience. This means fewer runtime errors and more compile-time checks, better autocomplete and IntelliSense support in your IDE, and easier detection of common mistakes before they make it to production.
Modern JavaScript features: The new library embraces modern JavaScript features, including native BigInt support for handling large numbers accurately, async iterators for working with subscriptions, and native Ed25519 support where available, falling back to a polyfill when necessary.
Flexible RPC methods: The new library provides a more flexible and extensible RPC method API, allowing you to easily add custom RPC methods without modifying the core library. This makes it easier to integrate with your custom programs and tools like Quicknode Marketplace.
If you are used to the 1.x library, you will find that the new library is different and will take some practice to get used to. Though 2.0 will go live in the next few weeks, the legacy 1.x library will continue to be supported for the foreseeable future, so do not feel like you have to update your existing code if you are not ready to.
功能 | Solana JS SDK 1.x | Solana JS SDK 2.0 |
|---|---|---|
Architecture & Modularity | Monolithic, class-based | Modular, functional, tree-shakable |
Performance & Size | Larger bundle, baseline performance | Smaller bundle, improved performance (e.g., 10x faster key operations) |
Developer Experience | Basic TypeScript support, limited IDE assistance | Enhanced type safety, improved autocomplete and IntelliSense |
Modern JavaScript Features | Limited | Native BigInt and Web Crypto API support, AsyncIterators for subscriptions |
Flexibility & Extensibility | Fixed set of methods and features | Extensible RPC methods, custom transports, comprehensive codec system |
Compatibility & Learning Curve | Established ecosystem support, familiar to existing users | Designed for modern environments, requires adaptation for 1.x users |
One of the most significant improvements in 2.0 is its tree-shakable and modular architecture. This means:
You can now import only the functions you need (as opposed to the entire Connections class and all its methods, for example), reducing your bundle size dramatically.
Smaller bundles lead to faster load times for your dApps and reduced compute costs for serverless deployments.
The 2.0 library is made up of several sub-libraries that can be imported individually to handle specific tasks, rather than importing the entire library at once.
For example, instead of importing the entire 1.x library, you can now import specific elements from its smaller, more focused modules:
@solana/accounts: For fetching and decoding accounts
@solana/codecs: For composing data (de)serializers from a set of primitives or building custom ones
@solana/errors: For identifying and refining coded errors thrown in the @solana namespace
@solana/rpc: For sending RPC requests
@solana/rpc-subscriptions: For subscribing to RPC notifications
@solana/signers: For building message and/or transaction signer objects
@solana/sysvars: For fetching and decoding sysvar accounts
@solana/transaction-messages: For building and transforming Solana transaction message objects
@solana/transactions: For compiling and signing transactions for submission to the network
The net impact is a significant reduction in bundle size - in some cases, up to 80% smaller than using the entire 1.x library. In an early test of Solana Explorer, the landing page was reduced from 311KB to 226KB, a 26% reduction in size (source Solana YouTube).
Web3.js 2.0 leverages TypeScript's advanced features to provide a more type-safe development experience. This means:
Fewer runtime errors and more compile-time checks.
Better autocomplete and IntelliSense support in your IDE.
Easier detection of common mistakes before they make it to production.
For instance, the library now uses type refinement to ensure you're using the correct parameters for different RPC methods. Common examples of this might include type errors if:
A transaction is missing a required signature.
A transaction does not have a recent blockhash.
An instruction is missing a required account.
An account lookup table is passed into a legacy transaction.
Web3.js 2.0 embraces modern JavaScript features, including:
Native BigInt support for handling large numbers accurately, which is important in Solana programming because Rust's u64 type can represent numbers up to 2^64-1, while JavaScript's Number type can only represent numbers up to 2^53-1.
AsyncIterators for working with subscriptions.
Native Ed25519 via CryptoKey which results in faster key generation and signing, fewer dependencies, and better compatibility with other libraries. Testing has demonstrated Keygen, Sign, and Verify performs nearly 10 times faster than the original 1.x library.
The new SDK is designed with extensibility in mind, allowing for seamless integration of custom RPC methods. This means you can easily access Quicknode Marketplace add-ons, your own custom Functions, or any other custom HTTP request required for your application. Further, it means you can integrate your own custom transport logic, such as sharding, retries, rate limiting, failover, etc.

This extensibility means that as we roll out new features and optimizations on our RPC endpoints, you'll be able to take advantage of them immediately without waiting for updates to the core Solana web3.js library.
As a developer using Quicknode, the Solana SDK 2.0 brings several benefits:
Faster Applications: Smaller bundle sizes and improved performance mean your dApps will load and run faster.
Improved Developer Experience: Better type safety, modern JavaScript features, and tooling support make development more productive and enjoyable.
Scalable: Integrate custom RPC methods, program clients, and custom transports to take advantage of new features and optimizations as they become available.
Easier Debugging: Improved error handling and more predictable behavior make diagnosing and fixing issues when they occur easier.
To start using web3.js 2.0 with our RPC services, you can install the release candidate:

Then, you can create an RPC client pointing to our endpoint:

The Solana SDK 2.0 represents a significant step forward for Solana development. Its modern design, improved performance, and enhanced developer experience give you the tools you need to build the next generation of Solana applications. We're excited to see what you'll build with this new toolkit.
To help get you started, we have:
Added Sample Code to integrate Quicknode add-ons into your application
Created step-by-step technical Guides to help you get started
Quicknode 成立于 2017 年,Quicknode 为开发者和企业Quicknode 机构级区块链基础设施。凭借 99.99% 的运行时间以及对 80 多条区块链的支持,各团队能够毫无妥协地构建和扩展链上应用。
将最新的工程见解、产品更新和 Web3 资讯直接发送至您的收件箱。