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

@firefly-dex/universal-router-sdk

v1.6.1

Published

sdk for integrating with the Universal Router contracts

Downloads

1

Readme

universal-router-sdk

This SDK facilitates interactions with the contracts in Universal Router

Usage

Install latest version of universal-router-sdk. Then import the corresponding Trade class and Data object for each protocol you'd like to interact with.

Trading NFTs

warning: swapNFTCallParameters() to be deprecated in favor of swapCallParameters()

import {
  LooksRareTrade,
  LooksRareData,
  SeaportTrade,
  SeaportData
} from "@firefly-dex/universal-router-sdk";

// Each protocol data object contains 1 call to that protocol. Some protocols can fit
// many NFT purchase within 1 call, like seaport. Others require multiple calls per NFT (like LooksRare).
const looksRareTrades = new LooksRareTrade([looksrareData1, looksrareData2])
const seaportTrades = new SeaportTrade([seaportData1])

// Use the raw calldata and value returned to call into Universal Swap Router contracts
// Trades will happen in the order that they are handed in
const { calldata, value } = SwapRouter.swapCallParameters([looksRareTrades, seaportTrades])

Trading ERC20s on Firefly

warning: swapERC20CallParameters() to be deprecated in favor of swapCallParameters()

import { TradeType } from '@firefly-dex/sdk-core'
import { Trade as V2TradeSDK } from '@firefly-dex/v2-sdk'
import { Trade as V3TradeSDK } from '@firefly-dex/v3-sdk'
import { MixedRouteTrade, MixedRouteSDK, Trade as RouterTrade } from '@firefly-dex/router-sdk'

const options = { slippageTolerance, recipient }
const routerTrade = new FireflyTrade(
  new RouterTrade({ v2Routes, v3Routes, mixedRoutes, tradeType: TradeType.EXACT_INPUT },
  options
)
// Use the raw calldata and value returned to call into Universal Swap Router contracts
const { calldata, value } = SwapRouter.swapCallParameters(routerTrade)

Using Firefly for ERC20 NFT Trades

Send ETH to the router by trading an ERC20 for ETH with a Firefly Trade and encoding the swap recipient as ROUTER_AS_RECIPIENT in the trade. Then subsequently list the NFT trades to use the ETH output to buy NFTs. Trades happen in the order they are listed.

Use trade_type: TradeType.EXACT_OUTPUT to cover the entire NFT price, alternatively the transaction will send supplemental ETH to fulfill the entire price if the swap does not cover it in full. Keep in mind that TradeType.EXACT_INPUT trades are subject to slippage on output, and ETH will be sent to cover potential slippage and any remaining ETH will be returned to sender.

import { TradeType } from '@firefly-dex/sdk-core'
import { Trade as V2TradeSDK } from '@firefly-dex/v2-sdk'
import { Trade as V3TradeSDK } from '@firefly-dex/v3-sdk'
import { MixedRouteTrade, MixedRouteSDK, Trade as RouterTrade } from '@firefly-dex/router-sdk'
import {
  ROUTER_AS_RECIPIENT,
  FireflyTrade,
  LooksRareTrade,
  LooksRareData,
  SeaportTrade,
  SeaportData
} from "@firefly-dex/universal-router-sdk";

const looksRareTrades = new LooksRareTrade([looksrareData1, looksrareData2])
const seaportTrades = new SeaportTrade([seaportData1])
// WARNING: never send funds to ROUTER_AS_RECIPIENT unless it is ETH that will be used in NFT trades, otherwise funds are lost.
const fireflyTrade = new FireflyTrade(
  new RouterTrade({ v2Routes, v3Routes, mixedRoutes, tradeType: TradeType.EXACT_OUTPUT }),
  { slippageTolerance, recipient:  ROUTER_AS_RECIPIENT}
)
// Use the raw calldata and value returned to call into Universal Swap Router contracts
const { calldata, value } = SwapRouter.swapCallParameters([fireflyTrade, seaportTrades, looksRareTrades])

Using WETH for NFT Trades

The current router purchases all NFTs with ETH, but you can send WETH to the router to be unwrapped for ETH right before the NFT commands. Similar to ERC20 Firefly Trades for NFTs, supplemental ETH will be sent in the transaction if the WETH amount will not cover the NFT buys. You can also use ERC20s and WETH to cover the transaction by including both commands before the NFT purchase.

import {
  ROUTER_AS_RECIPIENT,
  FireflyTrade,
  LooksRareTrade,
  LooksRareData,
  SeaportTrade,
  SeaportData
} from "@firefly-dex/universal-router-sdk";

const looksRareTrades = new LooksRareTrade([looksrareData1, looksrareData2])
const seaportTrades = new SeaportTrade([seaportData1])
// if no Permit needed, omit the third var of type Permit2Permit
const unwrapWETH = new UnwrapWETH(amountWETH, chainId, optionalPermit2Params)

// Use the raw calldata and value returned to call into Universal Swap Router contracts
const { calldata, value } = SwapRouter.swapCallParameters([unwrapWETH, seaportTrades, looksRareTrades])

Running this package

Make sure you are running node v16 Install dependencies and run typescript unit tests

yarn install
yarn test:hardhat

Run forge integration tests

yarn symlink # must install git submodules
forge install
yarn test:forge