Connect a Wallet

The Swing SDK requires a connected wallet provider to initiate signing and sending transactions. A variety of wallets, including Metamask, Coinbase, and Keplr are supported.

Some of the examples below are written using React, but you can use the same methods in any framework.

Wagmi

If you are using wagmi, connect using the wallet client's transport. A full example can be found in the staking-sdk-nextjs example.

import { useConnect } from 'wagmi';
import { SwingSDK } from '@swing.xyz/sdk';
 
const swingSDK = new SwingSDK();
 
function WagmiConnectButton() {
  const { connectAsync, connectors } = useConnect();
 
  return (
    <button
      onClick={async () => {
        // Typically you would render a button for each connector.
        const connector = connectors[0];
 
        const connection = await connectAsync({ connector });
        const walletClient = await connector.getWalletClient();
 
        // Pass Wagmi transport into Swing SDK: https://wagmi.sh/react/ethers-adapters#wallet-client--signer
        await swingSDK.wallet.connect(
          walletClient.transport,
          walletClient.chain.id,
        );
      }}
    >
      Connect Wallet
    </button>
  );
}

Thirdweb

If you are using @thirdweb-dev/react, connect using the wallet signer.

import { useConnect, metamaskWallet } from '@thirdweb-dev/react';
import { SwingSDK } from '@swing.xyz/sdk';
 
const swingSDK = new SwingSDK();
const metamaskConfig = metamaskWallet();
 
function ThirdWebConnectButton() {
  const connect = useConnect();
 
  return (
    <button
      onClick={async () => {
        // Typically you would render a button for each wallet config
        const wallet = await connect(metamaskConfig);
        const signer = await wallet.getSigner();
 
        // Pass wallet signer to Swing SDK
        const walletAddress = await swingSDK.wallet.connect(
          signer,
          defaultTransferParams.fromChain,
        );
      }}
    >
      Connect Wallet
    </button>
  );
}

Web3-React

If you are using web3-react, connect using the active provider.

import { useWeb3React } from '@web3-react/core';
import { SwingSDK } from '@swing.xyz/sdk';
 
const swingSDK = new SwingSDK();
 
function Web3ReactConnectButton() {
  const { connector } = useWeb3React();
 
  return (
    <button
      onClick={async () => {
        // Typically you would render a button for each wallet config
        await connector.activate();
 
        // Pass the active provider to Swing SDK
        const walletAddress = await swingSDK.wallet.connect(
          connector.provider,
          connector.provider.chainId,
        );
      }}
    >
      Connect Wallet
    </button>
  );
}

RainbowKit

If you are using Rainbow with Wagmi, please follow the Wagmi instructions.

If you are using @rainbow-me/rainbowkit, connect using the custom wallet button.

import { WalletButton } from '@rainbow-me/rainbowkit';
import { SwingSDK } from '@swing.xyz/sdk';
 
const swingSDK = new SwingSDK();
 
function RainbowKitConnectButton() {
  return (
    <WalletButton.Custom wallet="rainbow">
      {({ ready, connect, connector }) => {
        return (
          <button
            type="button"
            disabled={!ready}
            onClick={async () => {
              await connect();
 
              const provider = await connector.connector.getProvider();
 
              // Pass the active provider to Swing SDK
              const walletAddress = await swingSDK.wallet.connect(
                connector.provider,
                connector.provider.chainId,
              );
            }}
          >
            Connect Rainbow
          </button>
        );
      }}
    </WalletButton.Custom>
  );
}

Metamask

When connecting to an MetaMask, provide the wallet interface located on the window.

await swingSDK.wallet.connect(window.ethereum, 'ethereum');

Coinbase

When connecting to the Coinbase wallet, provide the wallet interface located on the window.

await swingSDK.wallet.connect(window.CoinbaseWalletProvider, 'ethereum');

Brave

When connecting to an Brave, provide the wallet interface located on the window.

await swingSDK.wallet.connect(window.ethereum, 'ethereum');

WalletConnect

When connecting to WalletConnect, provide the wallet interface located on the window.

await swingSDK.wallet.connect(window.ethereum, 'ethereum');

Phantom EVM

When connecting to Phantom's EVM wallet, provide the wallet interface located on the window.

await swingSDK.wallet.connect(window.phantom.ethereum, 'ethereum');

Phantom Solana

When connecting to Phantom's Solana wallet, provide the wallet interface located on the window.

await swingSDK.wallet.connect(window.phantom.solana, 'solana');

Keplr

When connecting to a Cosmos wallet, provide the OfflineSigner and the SigningStargateClient.

import { SigningStargateClient } from '@cosmjs/stargate';
import { SwingSDK } from '@swing.xyz/sdk';
 
const swingSDK = new SwingSDK();
 
function KeplrConnectButton() {
  const chain = swingSDK.getChain('cosmoshub-4');
 
  return (
    <button
      onClick={async () => {
        // Enable the chain in Keplr
        await window.keplr.enable(chain.slug);
 
        // Get the offline signer from Keplr
        const offlineSigner = window.keplr.getOfflineSigner(chain.slug);
 
        // Create a SigningStargateClient using the offline signer
        const client = await SigningStargateClient.connectWithSigner(
          chain.rpcUrl,
          offlineSigner,
        );
 
        // Pass the offline signer and client to SwingSDK
        await swingSDK.wallet.connect(
          {
            offlineSigner,
            client,
          },
          chain.slug,
        );
      }}
    >
      Connect Wallet
    </button>
  );
}

CosmosKit

If you are using @cosmos-kit/react or @cosmos-kit/react-lite, connect using getOfflineSigner and getSigningStargateClient returned from the useChain hook.

import { useChain } from '@cosmos-kit/react';
import { SwingSDK } from '@swing.xyz/sdk';
 
const swingSDK = new SwingSDK();
 
function CosmosKitConnectButton() {
  const chain = swingSDK.getChain('cosmoshub-4');
  const { connect, getOfflineSigner, getSigningStargateClient } = useChain(
    chain.slug,
  );
 
  return (
    <button
      onClick={async () => {
        // Trigger cosmos-kit to connect to the wallet
        await connect();
 
        const offlineSigner = await getOfflineSigner();
        const client = await getSigningStargateClient();
 
        // Pass the offline signer and client to SwingSDK
        await swingSDK.wallet.connect(
          {
            offlineSigner,
            client,
          },
          chain.slug,
        );
      }}
    >
      Connect Wallet
    </button>
  );
}

Other wallets

Not seeing your wallet? We're constantly adding new wallet integrations. If you need a specific wallet, please reach out in our Discord.