@tangleswap/sdk
v1.1.52
Published
🪐 SDK for building applications with the TangleSwap protocol
Downloads
508
Maintainers
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™