# 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](https://docs.tangleswap.exchange/developers).
## Getting Started
Install the latest version of the [SDK package](https://npmjs.com/package/@tangleswap-sdk/sdk) from npmjs:
```javascript
yarn add @tangleswap-sdk/sdk
```
### Usage
Import the `Tangleship` class and create a new instance. On Javascript / Typescript:
```javascript
import { Tangleship } from '@tangleswap-sdk/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. '1070' 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 on [Shimmer](https://shimmer.network) & [IOTA](https://iota.org) through the TangleSwap protocol is achieved seamlessly through only two different functions.
### 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 optimal UX, high value recommended so there is only need to approve once:
```javascript
/**
* @param tokenAddress string, address of the token
* @param allowanceAmount [OPTIONAL]Â number, from 0 to 2 ** 256 - 1
*/
tangleship.approveRouter(tokenAddress, allowanceAmount)
```
### Get a Quote
Request a trading quote from the [Orbit Router](https://npmjs.com/package/@tangleswap-sdk/orbit-router) module:
```javascript
/**
* @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)
})
```
### Execute the Trade
Perform the trade using the `swapQuote` object obtained in the previous step:
```javascript
/**
* @param token0Address string, address of token0
* @param token1Address string, address of token1
* @param amountIn number, amount of token0 to trade (inferes isExactInTrade == true)
* @param amountOut number, estimated amount of token1 desired, can be set to 0 if not caring about slippage
* @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,
amountOut,
swapQuote,
recipient,
slippageTolerance,
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 Goerli testnet.
```javascript
import { Tangleship } from '@tangleswap/sdk'
// Signer must be served from your ethers.js config — optionally, Provider too:
const tangleship = new Tangleship(signer, 5, 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)
tangleship.approveRouter(
token0,
maxAllowance,
)
tangleship.quoteFromOrbitRouter(
token0,
token1,
isExactInTrade,
amountToTrade,
recipient,
slippageTolerance,
deadline,
)
.then((swapQuote) => {
const amountOut = isExactInTrade
? swapQuote.quote
: amountToTrade
tangleship.multiSwapFromRouter(
token0,
token1,
amountToTrade,
amountOut,
swapQuote,
recipient,
slippageTolerance,
deadline,
)
})
```
## Manage Liquidity on TangleSwap
Coming soonâ„¢