@uniblock/1inch v0.0.1-2
uniblock-sdk-1inch
This is a wrapper sdk around 1inch sdk.
Setup
Follow this step to add uniblock/rarible to your repository
npm i @uniblock/1inch
or
yarn add @uniblock/1inch
General Overview
This sdk is a wrapper around 1inch sdk. Through this sdk, developers can programatically call the api of 1inch aggregation router at the frontend. The 1inch aggregation protocol can find efficient paths for token swap and offer crypto asset exchanges at the best rates in market. Therefore, developers can use this sdk to provide service for users to check appropriate path for cryptocurrency exchange or directly exchange the cryptocurrencies.
Quick Start
1. Get available swap tokens
get list of available tokens for swap in 1inch protocol:
import { initializeOneInch } from "@uniblock/1inch"
const chainId = 1
const uniblockApiKey = YOUR_UNIBLOCK_API_KEY
const oneInch = initializeOneInch(uniblockApiKey)
const result = await oneInch.get1inchSwapTokens(chainId)
// Get the list of available crypto tokens at ethereum main net
2. Get available liquidity pools
get list of available liquidity pools:
import { initializeOneInch } from "@uniblock/1inch"
const chainId = 1
const uniblockApiKey = YOUR_UNIBLOCK_API_KEY
const oneInch = initializeOneInch(uniblockApiKey)
const result = await oneInch.getLiquiditySources(chainId)
// Get the list of available liquidity pools at ethereum main net
3. Get allowance
get the number of given tokens that the 1inch router is allowed to spend
import { initializeOneInch } from "@uniblock/1inch"
const chainId = 1
const tokenAddress = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2" //WETH address
const walletAddress = YOUR_WALLET_ADDRESS
const uniblockApiKey = YOUR_UNIBLOCK_API_KEY
const oneInch = initializeOneInch(uniblockApiKey)
const result = await oneInch.checkAllowance(chainId, tokenAddress, walletAddress)
// get the number of WETH that 1inch router is allowed to spend from given walletAddress
4. Approve allowance
Approve the given user to spend given amount of given token
import { initializeOneInch } from "@uniblock/1inch"
const tokenAddress = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2" //WETH
const signer = new ethers.providers.Web3Provider(ethereum).getSigner()
const amount = 5;
const uniblockApiKey = YOUR_UNIBLOCK_API_KEY
const oneInch = initializeOneInch(uniblockApiKey)
const result = await oneInch.approveAllowance(tokenAddress, signer, amount)
// Approve from the user at tokenAddress to spend 5 WETH
5. Get best swap price
Get the best quote price to exchange via 1inch router:
- Only provides the mandatory inputs
import { initializeOneInch } from "@uniblock/1inch"
const chainId = 1
const fromTokenAddress = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2" //WETH
const toTokenAddress = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48" //USDC
const amount = 100
const uniblockApiKey = YOUR_UNIBLOCK_API_KEY
const oneInch = initializeOneInch(uniblockApiKey)
const result = await oneInch.swapQuote(chainId, fromTokenAddress, toTokenAddress, amount)
// get the price of swapping 100 WETH to USDC on ethereum chain
6. Swap
Swap the given source tokens to target tokens using the path provided by quote
- Only provides the mandtory inputs
import { initializeOneInch } from "@uniblock/1inch"
const chainId = 1
const fromTokenAddress = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2" //WETH
const toTokenAddress = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48" //USDC
const amount = 100
const fromAddress = YUOUR_WALLET_ADDRESS
const slippage = 1
const signer = new ethers.providers.Web3Provider(ethereum).getSigner()
const uniblockApiKey = YOUR_UNIBLOCK_API_KEY
const oneInch = initializeOneInch(uniblockApiKey)
const result = await oneInch.swap(chainId, fromTokenAddress, toTokenAddress, amount, fromAddress, slippage, signer)