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

@tangleswap/sdk

v1.1.52

Published

🪐 SDK for building applications with the TangleSwap protocol

Downloads

508

Readme

TangleSwap SDK 🛸

This SDK abstracts core functionality to facilite interactions with the TangleSwap smart contracts. Detailed specifications of all contracts can be found in the Developers Documentation.

Getting Started

Install the latest version of the SDK package from npmjs:

yarn add @tangleswap/sdk

Usage

Import the Tangleship class and create a new instance. On Javascript / Typescript:

import { Tangleship } from '@tangleswap/sdk';

/**
 * @param signer [OPTIONAL] object, ethers.js abstraction of the user's wallet. See: https://docs.ethers.org/v5/api/signer
 * @param chainId [OPTIONAL] number, the ID of the chain (e.g. '1072' for ShimmerEVM testnet)
 * @param provider [OPTIONAL] object, ethers.js abstration of a node connection. See: https://docs.ethers.org/v5/api/providers
 */
const tangleship = new Tangleship(signer, chainId, provider);

 Trade Tokens on TangleSwap

Swapping tokens through the TangleSwap SDK is exceedingly easy.

Approve Swap contract

For each new token, set the allowance to any arbitrary value. The contract will be limited to only spending this maximum amount of the specified token. For a hassle-free UX, you may consider a high value so that users only need to approve once:

/**
 * @param tokenAddress string, address of the token being provided by the user (token0)
 * @param allowanceAmount [OPTIONAL] number, from 0 to 2 ** 256 - 1
 */
tangleship.approveRouter(tokenAddress, allowanceAmount);

Perform an Optimized Trade (recommended)

First, request a trading quote from the Orbit Router module:

/**
 * @param token0Address string, address of token0
 * @param token1Address string, address of token1
 * @param isExactInTrade boolean, true for input=>output & false for output=>input
 * @param amountToTrade number, amount to trade (token0 or token1, based on isExactInTrade)
 * @param recipient string, address of user wallet
 * @param slippageTolerance number, slippage tolerance expressed as e.g. '0.995' for 0.5% tolerance
 * @param deadline number, seconds (UNIX format) before transaction expires
 */
tangleship
  .quoteFromOrbitRouter(
    token0Address,
    token1Address,
    isExactInTrade,
    amountToTrade,
    recipient,
    slippageTolerance,
    deadline,
  )
  .then((quote) => {
    setSwapQuote(quote);
  });

Next, execute the trade using the swapQuote object obtained in the previous step:

/**
 * @param token0Address string, address of token0
 * @param token1Address string, address of token1
 * @param amountIn number, amount of token0 to trade (inferes isExactInTrade == true)
 * @param amountOutEstimated number, estimated amount of token1 desired, safe to ignore (set to 0) if token1 is not native currency
 * @param swapQuote object, output obtained from the 'quoteFromOrbitRouter' function
 * @param recipient string, address of user wallet
 * @param slippageTolerance number, slippage tolerance expressed as e.g. '0.995' for 0.5% tolerance
 * @param deadline number, seconds (UNIX format) before transaction expires
 */
tangleship.multiSwapFromRouter(
  token0Address,
  token1Address,
  amountIn,
  amountOutEstimated,
  swapQuote,
  recipient,
  slippageTolerance,
  deadline,
);

Perform a Manual Trade

Alternatively, advanced users may prefer trading directly with a specific liquidity pool and amount, typically for immediate speed (no need to compute the optimal route) or because the trader is performing off-chain analysis. In that case, after approving the token the process is straightforward:

/**
 * @param token0Address string, address of token0
 * @param token1Address string, address of token1
 * @param feeTier number, corresponding to the pool's fee tier (500 for 0.05%, 3000 for 0.3%, or 10000 for 1%)
 * @param amountIn bigint, amount of token0 to trade (in decimal format, e.g. "1000000" for "1" with 6 decimals)
 * @param amountOutMinimum bigint, minimum amount of token1 willing to receive (in decimal format), used to set slippage tolerance
 * @param recipient string, address of user wallet
 * @param deadline number, seconds (UNIX format) before transaction expires
 */
const swapLog = await tangleship.directSwap(
  token0Address,
  token1Address,
  feeTier,
  amountIn,
  amountOutMinimum,
  recipient,
  deadline,
);

Runnable Example: Trade on TangleSwap

Below we provide a fully-functional example of Typescript code to import the Tangleship class, obtain a quote, and perform the trade between two arbitrary tokens on the Sepolia testnet.

import { Tangleship } from '@tangleswap/sdk';

// Signer must be served from your ethers.js config — optionally, Provider too:
const tangleship = new Tangleship(signer, 11155111, provider);

const token0 = '0x58aA86552d8cbB96a574F46b5bDD270b24f37626';
const token1 = '0x9Ef0C71EA63e29aB62d792245023707C9C3DD2A5';
const maxAllowance = 2 ** 256 - 1; // maximum allowed
const isExactInTrade = True;
const amountToTrade = 150000000;
const recipient = '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045';
const slippageTolerance = 0.995; // equivalent to 0.5% tolerance
const deadline = Math.floor(Date.now() / 1000 + 1800);

await tangleship.approveRouter(token0, maxAllowance);

const swapQuote = await tangleship.quoteFromOrbitRouter(
  token0,
  token1,
  isExactInTrade,
  amountToTrade,
  recipient,
  slippageTolerance,
  deadline,
);

const swapLog = await tangleship.multiSwapFromRouter(
  token0,
  token1,
  amountToTrade,
  isExactInTrade ? swapQuote.quote : amountToTrade,
  swapQuote,
  recipient,
  slippageTolerance,
  deadline,
);

Manage Liquidity on TangleSwap

Coming soon™