# Tokenlon v5 SDK

SDK for Tokenlon v5

# Get started

# Installing

yarn add @tokenlon/sdk ethers

# Importing

import { Market, Protocol, Quoter, Signer } from '@tokenlon/sdk'
import ethers from 'ethers'

# Creating an Exchange

# Connecting to Ethereum with the MetaMask Web3 provider

const provider = new ethers.providers.Web3Provider(window.ethereum)

# Initializing a Tokenlon Signer instance

const tokenlonSigner = await new Signer(provider)
const signer = await tokenlonSigner.getSigner()
const address = await signer.getAddress()
const chainId = await signer.getChainId()

# Initializing a Tokenlon Market instance and get all tradable tokens

const market = new Market(chainId)
const tokens = await market.getTradableTokens([Protocol.AMMV1])

# Initializing a Tokenlon Quoter instance and connecting to the quoter server

const quoter = new Quoter(chainId)

// get your account's signature from the Tokenlon signer
// then use the signature to connect to the Tokenlon quoter server
const authInfo = await tokenlonSigner.signAuthToken()
const accessToken = await quoter.auth(authInfo)
quoter.connect(accessToken)

# Selecting one taker token and one maker token, and subscribing to get dynamic exchange rate

const takerToken = 'LON' // should be in the list of tradable tokens
const makerToken = 'ETH' // should be a opposite token of the taker token
const { exchangeable, maxAmount, minAmount, rate } = await quoter.getRateAsync({
  base: takerToken,
  quote: makerToken,
  side: 'SELL',
  amount: 1,
})

# Requesting a new order with selected taker token and maker token

const { exchangeable, order } = await quoter.getNewOrderAsync({
  base: takerToken,
  quote: makerToken,
  side: 'SELL',
  amount: 1,
  protocols: [Protocol.AMMV1],
})

# Previewing the order and transaction fee

const slippagePercent = 1 // 1% slippage tolerance
const { feeAmount, feeCurrency, minimalReceivedAmount } = await quoter.previewOrder(
  order,
  slippagePercent,
)

# Granting withdrawal and automate transactions permission to Tokenlon

// Exchanging ETH to other tokens does not require authorization,
// but using ERC 20 tokens to exchange others, authorization is needed.
const rawTx = await signer.setUnlimitedAllowance(order.takerAssetAddress)
const authorizationTx = await signer.sendTransaction(rawTx)
await authorizationTx.wait()

# Signing and sending out the order

const signResult = await signer.signOrder(order, {
  receiverAddress: address,
  slippagePercent,
})
const orderResult = await signer.sendOrder(signResult, option)

# Starting a Staking App

# LON Staking

const stakeAmount = 100
const rawTx = await signer.sendStakeWithPermit(stakeAmount)
const stakeResult = await signer.sendTransaction(rawTx)

# Unstaking all LONs

const rawTx = await signer.sendUnstake()
const unstakeResult = await signer.sendTransaction(rawTx)

# Performing rage exit

const rawTx = await signer.sendRageExit()
const rageExitResult = await signer.sendTransaction(rawTx)

# Redeeming your xLONs

const redeemAmount = 100
const rawTx = await signer.sendRedeem(redeemAmount)
const redeemResult = await signer.sendTransaction(rawTx)

# Playground

For more details, checkout the demo (opens new window)

git clone https://github.com/consenlabs/tokenlon-v5-sdk.git
cd tokenlon-v5-sdk/frontend
yarn
yarn start

# Errors

You may run into the following errors:

Type Description
ESCAPE_DEAL_ORDER Transferring tokens out before send the order to the blockchain.
LONG_TIME_HOLD Hold off on sending orders and resend later by self when there is a arbitrage opportunity.

# API references