@depay/web3-exchanges

中文文档
15.7.3发布于 2025/09/21MITESM + CJS零依赖近 30 天下载 3608

JavaScript library simplifying decentralized web3 exchange routing for multiple blockchains and exchanges.

原始使用文档
文档目录16 节
  1. Quickstart
  2. Support
  3. Platform specific packaging
  4. EVM (Ethereum Virtual Machine) platform specific packaging
  5. SVM (Solana Virtual Machine) platform specific packaging
  6. Data Structures
  7. Exchange
  8. Swap
  9. Route
  10. Slippage
  11. Auto Slippage
  12. Functionalities
  13. Access exchange directly
  14. Access exchange scoped for a given blockchain
  15. route: Routes a Swap configuration and returns routes to pe…
  16. Development

Quickstart

yarn add @depay/web3-exchanges

or

npm install --save @depay/web3-exchanges
import Exchanges from '@depay/web3-exchanges'

Exchanges.uniswap_v2
Exchanges.orca

// scoped

Exchanges.ethereum.uniswap_v2 // Ethereum scoped uniswap_v2
Exchanges.solana.orca

let routes

routes = await Exchanges.route({
  blockchain: 'ethereum',
  tokenIn: '0xa0bEd124a09ac2Bd941b10349d8d224fe3c955eb',
  tokenOut: '0xdAC17F958D2ee523a2206206994597C13D831ec7',
  amountIn: 1
})

routes = await Exchanges.route({
  blockchain: 'solana',
  tokenIn: 'Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB',
  tokenOut: '4k3Dyjzvzp8eMZWUXbBCjEvwSkkk59S5iCNLY3QrkX6R',
  amountIn: 1
})

import { getWallets } from '@depay/web3-wallets'

const wallet = getWallets()[0]
const account = await wallet.account()

// check if prep is required to facilitate swap/exchange
const preparation = await route.getPrep({ account })

let permit2
if(prep?.transaction) {
  await wallet.sendTransaction(prep.transaction)
} else if (prep?.signature) {
  let signature = await wallet.sign(prep.signature)
  permit2 = {...prep.signature.message, signature}
}

// use connected wallet to sign and send the swap transaction
const transaction = await route.getTransaction({ account, permit2 })
wallet.sendTransaction(transaction)

Support

This library supports the following blockchains:

This library supports the following decentralized exchanges:

Ethereum:

BNB Smart Chain:

Polygon:

Solana:

  • Orca
  • Raydium, only CP (Constant Product) and CL (Concentrated Liquidity) pools

Optimism:

Base:

Arbitrum:

Fantom:

Avalanche:

Gnosis:

Platform specific packaging

In case you want to use and package only specific platforms, use the platform-specific package:

EVM (Ethereum Virtual Machine) platform specific packaging

import Exchanges from '@depay/web3-exchanges-evm'

SVM (Solana Virtual Machine) platform specific packaging

import Exchanges from '@depay/web3-exchanges-svm'

Data Structures

Exchange

Decentralized exchange data is provided in the following structure:

{
  name: String (e.g. uniswap_v2)
  label: String (e.g. Uniswap v2)
  logo: String (base64 encoded PNG)
  protocol: String (uniswap_v2, uniswap_v3 etc.)

  fee: Array (e.g. [100, 500, 3000, 10000]; available fee teirs on the exchange)
  slippage: Boolean (indicates if exchange has slippage)

  blockchains: String (e.g. ['ethereum'])
  [blockchain]: {
    router: Object (contains 'address' and 'api' to interact with the exchange router)
    factory: Object (contains 'address' and 'api' to interact with the exchange factory)
    pair: Object (contains 'api' to interact with an exchange pair)
    quoter: Object (contains 'address' and 'api' to interact with the exchange quoter)
  }
}

Swap

A Swap configuration is fed into the route function:

{
  blockchain: String (e.g. 'ethereum')
  tokenIn: String (e.g. '0xa0bEd124a09ac2Bd941b10349d8d224fe3c955eb')
  tokenOut: String (e.g. '0xdAC17F958D2ee523a2206206994597C13D831ec7')
  amountIn: Human Readable Number (e.g. 1.2 and converted internally to BigNumber) or BigNumber if passed as string
  amountInMax: Human Readable Number (e.g. 1.2 and converted internally to BigNumber) or BigNumber if passed as string
  amountOut: Human Readable Number (e.g. 1.2 and converted internally to BigNumber) or BigNumber if passed as string
  amountOutMin: Human Readable Number (e.g. 1.2 and converted internally to BigNumber) or BigNumber if passed as string
}
  • tokenIn: The token put into the swap (out of wallet)
  • amountIn: The exact amount of tokenIn put into the swap (out of wallet)
  • amountInMax: The max. amount of tokenIn put into the swap (out of wallet)
  • tokenOut: The token expected to come out of the swap (into wallet)
  • amountOut: The exact amount of tokenOut expected to come out of the swap (into wallet)
  • amountOutMin: The min. amount of tokenOut expected to come out of the swap (into wallet)

The following combinations of provided amounts are possible:

  • Pass amountOutMin. Swap will return at least amountOutMin into the wallet. amountIn will be calculated automatically and can vary.
  • Pass amountOut. Swap will take at max amountInMax out of the wallet (calculated based on provided, exact amountOut). amountInMax will be calculated automatically and can vary.
  • Pass amountInMax. Swap will take at max amountInMax out of the wallet. amountOut will be calculated automatically and can vary.
  • Pass amountIn. Swap will return at least amountOutMin into the the wallet (calculated based on provided, exact amountIn). amountOutMin will be calculated automatically and can vary.

Route

Routes are returned by calling route on Exchanges or a single exchange.

A single Route has the following structure:

{
  blockchain: String (e.g. 'ethereum')
  tokenIn: String (e.g. '0xa0bEd124a09ac2Bd941b10349d8d224fe3c955eb')
  decimalsIn: Integer (e.g. 18)
  tokenOut: String (e.g. '0xdAC17F958D2ee523a2206206994597C13D831ec7')
  decimalsOut: Integer (e.g. 18)
  path: Array (e.g. ['0xa0bEd124a09ac2Bd941b10349d8d224fe3c955eb', '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2', '0xdAC17F958D2ee523a2206206994597C13D831ec7'])
  pools: Object ([{ pool }])
  amounts: [BigNumber] (e.g. ['1000000000000000000', '2300000000000000000', '9900000000000000000'])
  amountIn: BigNumber (e.g. '1000000000000000000')
  amountOutMin: BigNumber (e.g. '32000000000000000000')
  amountOut: BigNumber (e.g. '32000000000000000000')
  amountInMax: BigNumber (e.g. '1000000000000000000')
  exchange: Exchange (see [Exchange data structure](#exchange))
  getPrep: async function (returns transaction object for approvals or signature request for permit2)
  getTransaction: async function (returns transaction object –> see @depay/web3-wallets for details)
}

See @depay/web3-wallets for details about the transaction format.

Slippage

This library applies slippage strategies to amounts for the following combinations:

  • If amountOutMin is provided, slippage is applied to amountIn.
  • If amountOut is provided, slippage is applied to amountInMax.
  • If amountInMax is provided, slippage is applied to amountOut.
  • If amountIn is provided, slippage is applied to amountOutMax.

Auto Slippage

Auto slippage applies 0.5% default slippage.

For blockchains that allow to receive quotes for previous blocks (EVM), auto slippage additionally checks for:

  • Extreme Direction

  • Extreme Base Volatility

And applies a higher than default slippage (0.5%) if required.

Extreme Directional Price Change

If there is a clear directional change of the price for the last 3 blocks, the target price will be projected according to the velocity of the last 3 blocks.

Example Extreme Directional Price Change

Current Price: $1'500

Last 3 Blocks: $1'500, $1'470, $1'440

Velocity: $30 per block

Projection: $1'530

Slippage 2%

Slippage of 2% will be applied, because it's higher than default slippage.

For downwards projections slippage is not required as the transaction would not fail.

Extreme Base Volatility

If there is extreme base volatility in the last 3 blocks, none-directional, the target price will be the highest price over the last 3 blocks plus the smallest price change over the last 3 blocks.

Example Extreme Base Volatility

Current Price: $1'500

Last 3 Blocks: $1'500, $1'490, $1'520

Smallest Change: $10

Highest Price: $1'520

Projection: $1'530 ($1'520 + $10)

Slippage 2%

Slippage of 2% will be applied, because it's higher than default slippage.

Functionalities

Access exchange directly

import Exchanges from '@depay/web3-exchanges'

Exchanges.uniswap_v2

Access exchange scoped for a given blockchain

import Exchanges from '@depay/web3-exchanges'

Exchanges.arbitrum.uniswap_v3

route: Routes a Swap configuration and returns routes to perform the Swap

import Exchanges from '@depay/web3-exchanges'

let routes = Exchanges.route({
  blockchain: 'ethereum'
  tokenIn: '0xa0bEd124a09ac2Bd941b10349d8d224fe3c955eb',
  tokenOut: '0xdAC17F958D2ee523a2206206994597C13D831ec7',
  amountOutMin: 2
})

route can also be called on an exchange:

import Exchanges from '@depay/web3-exchanges'

let route = await Exchanges.ethereum.uniswap_v2.route({
  tokenIn: '0xa0bEd124a09ac2Bd941b10349d8d224fe3c955eb',
  tokenOut: '0xdAC17F958D2ee523a2206206994597C13D831ec7',
  amountIn: 1
})

Development

yarn install
yarn dev

版本与发布记录

@depay/web3-exchanges 共发布 123 个版本,首次发布于 2021/11/26;过去 12 个月发布了 0 个版本。

版本发布日期距今
15.7.3latest2025/09/211.1 年前
15.7.22025/08/201.2 年前
15.7.12025/04/291.5 年前
15.7.02025/04/121.6 年前
15.6.32025/04/111.6 年前
15.6.22025/04/101.6 年前

关于 @depay/web3-exchanges 的常见问题

@depay/web3-exchanges 是什么?

JavaScript library simplifying decentralized web3 exchange routing for multiple blockchains and exchanges.

如何安装 @depay/web3-exchanges?

在项目目录运行 npm install @depay/web3-exchanges,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@depay/solana-web3.js(^1.98.0)、@depay/web3-blockchains(^9.7.9)、@depay/web3-client(^11.0.2)、@depay/web3-tokens(^11.0.0)、decimal.js(^10.4.3)、ethers(^5.7.1)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@depay/web3-exchanges 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/depay__web3-exchanges 是否存在。

@depay/web3-exchanges 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@depay/web3-exchanges 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=14。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@depay/web3-exchanges 可以商用吗?

@depay/web3-exchanges 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@depay/web3-exchanges 的下载量有多少?

根据 npm 官方下载统计,@depay/web3-exchanges 在 2026/10/12 – 2026/11/10 这 30 天内被下载 3,608 次,日均约 120 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@depay/web3-exchanges 还在维护吗?

最新版本 15.7.3 发布于 2025/09/21(1.1 年前);至今共发布 123 个版本,其中过去 12 个月发布了 0 个;首次发布于 2021/11/26。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。