githubEdit

Middleware

Learn how to intercept and enhance wallet operations with middleware.

Middleware allows you to intercept wallet operations. You can use this to add logging, implement retry logic, or handle failovers for RPC providers.

Register Middleware

When registering middleware, you should reference a specific chain. The middleware function runs every time an account is instantiated or an operation is performed, depending on the implementation.

Logging

This simple middleware logs a message whenever a new account is accessed.

Logging Middleware
wdk.registerMiddleware('ethereum', async (account) => {
  const address = await account.getAddress()
  console.log('Accessed Ethereum account:', address)
  
  // You can also attach custom properties or wrap methods here
})

Failover Protection with Provider Failover

The @tetherto/wdk-provider-failoverarrow-up-right package provides a resilient wrapper for wallet instances. Unlike standard middleware, you wrap your wallet class instantiation directly.

Install @tetherto/wdk-provider-failover

You can install the @tetherto/wdk-provider-failover using npm with the following command:

Use createFallbackWallet

You can import the createFallbackWallet function to ensure that if your primary RPC fails, the wallet automatically retries with the fallback providers.

With this configuration, if sendTransaction fails due to a network error, the WDK will automatically retry using the fallback providers without throwing an error to your application.

Next Steps

Learn about error handling and best practices to ensure your application is robust and secure.