🚣Migrating from v2.x

Guide for migrating your existing project from use-wallet v2 to v3

The release of @txnlab/use-wallet v3.x marks a significant evolution of the library, transitioning from a React-specific solution to a framework-agnostic core with framework-specific adapters. While we've strived to maintain familiarity and ease of use, there are several important changes to be aware of when upgrading from v2.x.

This guide will walk you through the key differences and provide step-by-step instructions to help you migrate your existing React projects to the new version. By following this guide, you'll be able to take advantage of the improved flexibility and expanded capabilities of v3.x while minimizing disruption to your existing codebase.

Step 1: Update Package Installation

circle-info

Commands are provided for NPM, Yarn, and PNPM

The first step in migrating to v3.x is to update your package installation. In v3.x, the core functionality has been separated from the React-specific implementation.

  1. Uninstall the existing package:

npm uninstall @txnlab/use-wallet
yarn remove @txnlab/use-wallet
pnpm remove @txnlab/use-wallet
  1. Install the new React adapter package:

npm install @txnlab/use-wallet-react
yarn add @txnlab/use-wallet-react
pnpm add @txnlab/use-wallet-react

This new package includes both the core library and the React-specific adapter, ensuring compatibility with your existing React project.

Step 2: Update Library Configuration

In v3.x, the way you configure the library at the root level of your app has changed. The custom hook useInitializeProviders is no longer used, and the configuration is now done outside of the root component using a WalletManager instance.

v2.x Configuration (Old)

v3.x Configuration (New)

Step 3: Update useWallet Hook Usage and Types

The useWallet hook has undergone several changes in v3.x. Here are the key differences and how to adapt your code:

  1. Rename providers to wallets:

    • In v2.x: providers was an array of wallet providers.

    • In v3.x: This is now called wallets.

  2. Changes in returned properties:

    • connectedAccounts is now activeWalletAccounts.

    • signer is now transactionSigner.

  3. Network management:

    • New properties: activeNetwork and setActiveNetwork for managing the current network.

  4. Algod client:

    • algodClient and setAlgodClient are now available directly from the hook.

The types exported by the library have changed as well. Some have changed to fit the new implementation, some have been renamed to be more descriptive and consistent, and others that were outside the scope of the library have been removed.

  1. Wallet accounts:

    • In v2.x: Account was an object containing providerId, name, address, authAddr, and email.

    • In v3.x: This is now called WalletAccount and only contains a name and address.

  2. Wallet providers:

    • ProvidersArray is now SupportedWallets[].

    • PROVIDER_ID enum is renamed to WalletId.

    • BaseClient is now BaseWallet.

    • Metadata is now WalletMetadata.

  3. Types that have been removed:

    • Algorand REST API response types such as AccountInfo and Asset.

    • Transaction types including Txn, TxnType, TxnInfo, RawTxnResponse, ConfirmedTxn, and DecodedTransaction.

    • Various other undocumented types that are no longer used with the v3.x implementation.

circle-info

Note: If you were using a type that was removed in v3.x, you can either redefine it in your application or see if an equivalent type exists in the algosdk or @algorandfoundation/algokit-utils NPM packages.

β€’ v2.x types directory: https://github.com/TxnLab/use-wallet/tree/v2/src/typesarrow-up-right

β€’ Algorand JS SDK: https://github.com/algorand/js-algorand-sdkarrow-up-right

β€’ Algokit TypeScript Utilities: https://github.com/algorandfoundation/algokit-utils-tsarrow-up-right

Step 4: Signing and Sending Transactions

Version 3.x introduces improvements in how transactions are signed and sent to the network, aligning more closely with recommended Algorand practices.

Key Changes

  1. Flexible Transaction Formats:

    • v3.x now accepts both Uint8Array encoded transactions and algosdk.Transaction objects.

    • This flexibility simplifies most common use cases.

  2. Improved Atomic Transaction Composer (ATC) Support:

    • While possible in v2.x, ATC usage is now better supported and documented.

    • The implementation is more efficient, avoiding unnecessary transaction encoding.

  3. Streamlined API:

    • Removed several utility functions that were essentially wrappers around algosdk methods.

    • Developers are now encouraged to use algosdk methods directly for manual transaction handling.

  4. ARC-0001 compliance:

    • The optional returnGroup argument has been removed from signTransactions, conforming with ARC-0001's SignTxnsFunction type signature. This also simplifies the method's implementation.

    • Now signTransactions will always return Promise<(Uint8Array | null)[]>, a promise that resolves to an array of signed transactions or null for transactions that were not signed.

Examples

Signing and Sending Transactions using Atomic Transaction Composer

This example demonstrates the recommended method using an Atomic Transaction Composer, which is now well-supported and efficient.

Manually Signing and Sending Transactions

The next example illustrates how to manually handle transactions if needed, though this is generally less common in typical use cases.

circle-info

The signTransactions method will still accept an array of Uint8Array encoded transactions, as was required in v2.x (but is no longer necessary).

By adopting these patterns, developers can create more robust and efficient transaction handling in their applications while aligning with best practices in the Algorand ecosystem.

Resources

Atomic Transaction Composer documentation
ARC-0001 specification

Technical Support

If you encounter any issues during the migration process or have questions about implementing new features:

  1. Discord Support: Join our NFDomains Discord server, where we have a dedicated #use-wallet channel. Our team and community members are available to provide technical support and answer your questions.

  2. GitHub Issues: For potential bugs or feature requests, please open an issue on our GitHub repository.

We're here to ensure your transition to v3.x is as smooth as possible. Don't hesitate to reach out – whether you're stuck on a particular migration step, need clarification on new features, or want to discuss best practices for your specific use case.

Last updated