@swing.xyz/sdk
TypeScript icon, indicating that this package has built-in type declarations

0.16.7 • Public • Published

Swing.xyx

Swing.xyx



The Swing SDK provides you with utilities for interacting with Swing's API and web3 wallets, which will enable you to build your own any-to-any cross-chain swap UI.

Looking for a fully featured, embeddable widget instead? Check out @swing.xyz/ui

Getting started

yarn add @swing.xyz/sdk

Usage

Setup

import SwingSDK, { TransferParams } from '@swing.xyz';

// Create a new SwingSDK client
const sdk = new SwingSDK({
  // Optionally enable debug mode for verbose logging
  debug: true,
});

// Initialize the SDK
await sdk.init();

// Connect to a wallet
await sdk.wallet.connect(window.ethereum || window.CoinbaseWalletProvider);

Cross-Chain Transfer

// Add a listener to update your UI as the transfer status changes
sdk.on('TRANSFER', (transfer) => {
  switch (transfer.status) {
    case 'PENDING':
      console.log(`Creating a transaction for the ${transfer.step} step`);
      break;
    case 'CHAIN_SWITCH_REQUIRED':
      // Handle switching chains or alert the user to do it manually
      console.log('Please switch to the chain:', transfer.chain)
      break;
    case 'WALLET_CONNECTION_REQUIRED':
      console.log('Please connect to the wallet for address:', transfer.address)
      break;
    case 'ACTION_REQUIRED':
      console.log('Please complete the required action within your connected wallet');
      break;
    case 'CONFIRMING':
      console.log(`Waiting for the transaction from the ${transfer.step} step to complete`);
      break;
    case 'SUCCESS':
      console.log(`Transfer has completed the ${transfer.step} step`);
      break;
    case 'FAILED':
      console.log(`Transfer failed at the ${transfer.step} step:`, transfer.error);
      break;
  }
});


// Setup parameters for a new transfer
const transferParams: TransferParams = {
  fromChain: 'ethereum',
  toChain: 'polygon',

  fromToken: 'USDC',
  toToken: 'USDC',

  amount: '100', // 100 USDC

  fromUserAddress: '0x0000',
  toUserAddress: '0x0000',
};

// Get a quote to find the best possible routes
const quote = await sdk.getQuote(transferParams);

// Select a route from the quote
const transferRoute = quote.routes[0];

// Start the transfer
await sdk.transfer(transferRoute, transferParams);

Claim Transaction

// Get a history of transactions for the connected wallet
const transactions = await sdk.wallet.getTransactions();

// Alert your user to take action on any transactions require a claim
for (const tx of transactions) {
  if (tx.status === 'Pending Destination Chain' && tx.needClaim) {
    // This is only required if the user closes the browser before claiming their tokens during the transfer process
    await sdk.claim(tx);
  }
}

Keywords

none

Install

npm i @swing.xyz/sdk

DownloadsWeekly Downloads

306

Version

0.16.7

License

none

Unpacked Size

2.55 MB

Total Files

23

Last publish

Collaborators

  • yuri-pramos
  • swing_gabriel
  • swing_chuqiao
  • lottamus