This guide will walk you through the process of setting up Capsule with Ethers.js, a popular Ethereum library. We’ll cover both v5 and v6 of Ethers, as Capsule provides integration packages for both versions.

Installation

First, you’ll need to install the necessary packages. Choose the appropriate package based on your Ethers version:

Setting Up Capsule with Ethers v6

Here’s how to set up Capsule with Ethers v6:

import { CapsuleEthersSigner } from '@usecapsule/ethers-v6-integration';
import { ethers } from 'ethers';
import Capsule from '@usecapsule/web-sdk';

// Initialize Capsule
const capsule = new Capsule(Environment.BETA, YOUR_API_KEY);

// Set up the provider
const provider = new ethers.JsonRpcProvider(YOUR_RPC_URL);

// Create the Capsule Ethers Signer
const ethersSigner = new CapsuleEthersSigner(capsule, provider);

Setting Up Capsule with Ethers v5

For Ethers v5, the setup is similar:

import { CapsuleEthersSigner } from '@usecapsule/ethers-v5-integration';
import { ethers } from 'ethers';
import Capsule from '@usecapsule/web-sdk';

// Initialize Capsule
const capsule = new Capsule(Environment.BETA, YOUR_API_KEY);

// Set up the provider
const provider = new ethers.providers.JsonRpcProvider(YOUR_RPC_URL);

// Create the Capsule Ethers Signer
const ethersSigner = new CapsuleEthersSigner(capsule, provider);

Usage

Once you’ve set up the CapsuleEthersSigner, you can use it just like you would use a regular Ethers signer. Here’s a basic example:

// Get the signer's address
const address = await ethersSigner.getAddress();
console.log('Signer address:', address);

// Get the balance
const balance = await provider.getBalance(address);
console.log('Balance:', ethers.utils.formatEther(balance));

Server-Side Usage

If you need to use Capsule with Ethers on the server-side, please check our Server-Side Signing Guide for specific instructions.

Remember to always handle errors appropriately and ensure your user is authenticated with Capsule before attempting to use the signer.

For more advanced usage and a complete API reference, please refer to the Ethers documentation.