npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@keeper-wallet/swap-client

v0.2.3

Published

Get the best asset swap prices from [Keeper Wallet](https://keeper-wallet.app/) swap services aggregator.

Downloads

2,405

Readme

@keeper-wallet/swap-client

Get the best asset swap prices from Keeper Wallet swap services aggregator.

Installation

Install using npm:

npm install @keeper-wallet/swap-client

or yarn:

yarn add @keeper-wallet/swap-client

Usage

First, import SwapClient (to setup connection) and SwapClientErrorCode (to understand what happened in case of an error):

import { SwapClient, SwapClientErrorCode } from '@keeper-wallet/swap-client';

Create an instance of SwapClient class:

const swapClient = new SwapClient();

Note: If you want to receive data for multiple asset pairs simultaneously, you'll need separate instances for each pair.

Subscribe to receive data about possible swaps:

// Call unsubscribe when you no longer interested in receiving swaps.
const unsubscribe = swapClient.subscribe({
  // called in case we can't connect to the service
  onError: () => {
    console.error('Could not connect to the swap service');
  },
  // called with updated data about possible swaps
  onData: (
    // vendor name as a string like 'keeper', 'swopfi' or 'puzzle'
    vendor,
    // see below
    response
  ) => {
    // type equals 'error' if vendor can't perform swap with given parameters
    if (response.type === 'error') {
      if (response.code === SwapClientErrorCode.INVALID_ASSET_PAIR) {
        console.error(`${vendor} cannot perform swap with given asset pair`);
      } else if (response.code === SwapClientErrorCode.UNAVAILABLE) {
        console.error(`${vendor} service is not available at the moment`);
      } else if (response.code === SwapClientErrorCode.UNEXPECTED) {
        console.error(`Something really bad happened with ${vendor}`);
      }
    } else if (response.type === 'data') {
      // we've got data to perform swap!!!

      // priceImpact is a number ranging from 0 to 1, which represents a ratio
      // by which asset price will change after swap
      console.log(response.priceImpact);

      // amountCoins is a string (because JavaScript number can't fit 64-bit
      // integer) showing how much coins we'll receive
      console.log(response.amountCoins);

      // minimumReceivedCoins is also a string (for the same reason)
      // showing minimum amount of coins which user would received according to
      // passed slippage tolerance
      console.log(response.minimumReceivedCoins);

      // contains swapParams for which we got the response
      console.log(response.swapParams);

      // tx contains prepared fields of invoke script transaction, which you can
      // sign and broadcast to actually perform it
      console.log(response.tx);
    }
  },
});

Finally, set parameters for the swap you would like to perform:

swapClient.setSwapParams({
  // amount (in coins) which you're going to swap
  amountCoins: '100000000', // 100 USDN (it has 6 decimal places)
  // asset you'd like to swap (USDN in this case)
  fromAssetId: 'DG2xFkPdDwKUoBkzGAhQtLpSGzfXLiCYPEzeKH2Ad24p',
  // if price goes down by more than this ratio, swap will not be performed
  slippageTolerance: 0.1,
  // asset which you would like to receive
  toAssetId: 'WAVES',
});

Note: to change parameters (e.g. to change assets and/or amount) just call it again. The last call determines parameters with which you want to swap.

And once you don't want to receive swaps anymore, call unsubscribe returned from subscribe method above:

unsubscribe();

And if there are no subscribers left, connection will be closed after 3 seconds.