@balancer-labs/balancer-maths

中文文档
0.0.41发布于 2026/07/01MITTypeScriptESM + CJS零依赖近 30 天下载 1.6万

Balancer V3 Maths

原始使用文档
文档目录5 节
  1. Hooks Support
  2. Examples
  3. Swap
  4. Add Liquidity
  5. Remove Liquidity

Balancer Maths TS

Typescript implementation of maths for Balancer pools. Also published as a package on NPM.

Hooks Support

Hooks are supported on a case by case basis.

When a pool has a hook type included in the PoolState a HookState must also be passed as an input to any Vault operation. See Remove Liquidity example below.

Currently supported hooks:

  • ExitFeeHook
    • This hook implements the ExitFeeHookExample found in mono-repo

Examples

Swap

const vault = new Vault();

const pool = {
  chainId: '11155111',
  blockNumber: '5955146',
  poolType: 'WEIGHTED',
  poolAddress: '0x204d4194e4e42364e3d1841d0a9b1ef857879c31',
  tokens: [
    '0x7b79995e5f793A07Bc00c21412e50Ecae098E7f9',
    '0xb19382073c7A0aDdbb56Ac6AF1808Fa49e377B75'
  ],
  scalingFactors: [ 1000000000000000000n, 1000000000000000000n ],
  weights: [ 500000000000000000n, 500000000000000000n ],
  swapFee: 1000000000000000n,
  balances: [ 64604926441576011n, 46686842105263157924n ],
  tokenRates: [ 1000000000000000000n, 1000000000000000000n ],
  totalSupply: 1736721048412749353n
};

const calculatedAmount = vault.swap(
    {
        amountRaw,
        tokenIn,
        tokenOut,
        swapKind,
    },
    pool,
);

Add Liquidity

const vault = new Vault();

const pool = {
  chainId: '11155111',
  blockNumber: '5955145',
  poolType: 'WEIGHTED',
  poolAddress: '0x204d4194e4e42364e3d1841d0a9b1ef857879c31',
  tokens: [
    '0x7b79995e5f793A07Bc00c21412e50Ecae098E7f9',
    '0xb19382073c7A0aDdbb56Ac6AF1808Fa49e377B75'
  ],
  scalingFactors: [ 1000000000000000000n, 1000000000000000000n ],
  weights: [ 500000000000000000n, 500000000000000000n ],
  swapFee: 0n,
  balances: [ 64604926441576011n, 46686842105263157924n ],
  tokenRates: [ 1000000000000000000n, 1000000000000000000n ],
  totalSupply: 1736721048412749353n
}

const calculatedAmounts = vault.addLiquidity(
    {
        pool: pool.poolAddress,
        maxAmountsIn: inputAmountsRaw,
        minBptAmountOut: bptOutRaw,
        kind,
    },
    pool,
);

Remove Liquidity

This example shows how to calculate the result of a remove liqudity operation when the pool is registered with the ExitFee hook.

const vault = new Vault();

const poolState = {
    poolType: 'WEIGHTED',
    hookType: 'ExitFee',
    chainId: '11155111',
    blockNumber: '5955145',
    poolAddress: '0x03722034317d8fb16845213bd3ce15439f9ce136',
    tokens: [
        '0x7b79995e5f793A07Bc00c21412e50Ecae098E7f9',
        '0xb19382073c7A0aDdbb56Ac6AF1808Fa49e377B75',
    ],
    scalingFactors: [1000000000000000000n, 1000000000000000000n],
    weights: [500000000000000000n, 500000000000000000n],
    swapFee: 100000000000000000n,
    aggregateSwapFee: 0n,
    balancesLiveScaled18: [5000000000000000n, 5000000000000000000n],
    tokenRates: [1000000000000000000n, 1000000000000000000n],
    totalSupply: 158113883008415798n,
};

const inputHookState = {
    removeLiquidityHookFeePercentage: 0n,
    tokens: poolState.tokens,
};

const removeLiquidityInput = {
    pool: '0xb2456a6f51530053bc41b0ee700fe6a2c37282e8',
    minAmountsOutRaw: [1n, 1n],
    maxBptAmountInRaw: 10000000000000n,
    kind: RemoveKind.PROPORTIONAL,
};

const outPutAmount = vault.removeLiquidity(
    {
        pool: pool.poolAddress,
        minAmountsOutRaw: amountsOutRaw,
        maxBptAmountInRaw: bptInRaw,
        kind,
    },
    pool,
    inputHookState
);

版本与发布记录

@balancer-labs/balancer-maths 共发布 41 个版本,首次发布于 2024/06/03;过去 12 个月发布了 4 个版本。

版本发布日期距今
0.0.41latest2026/07/014 个月前
0.0.402026/03/177 个月前
0.0.392026/03/177 个月前
0.0.382025/12/1610 个月前
0.0.372025/08/281.2 年前
0.0.362025/07/101.3 年前

关于 @balancer-labs/balancer-maths 的常见问题

@balancer-labs/balancer-maths 是什么?

Balancer V3 Maths

如何安装 @balancer-labs/balancer-maths?

在项目目录运行 npm install @balancer-labs/balancer-maths,或使用 pnpm add / yarn add / bun add。

@balancer-labs/balancer-maths 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@balancer-labs/balancer-maths 支持 ESM 还是 CommonJS?

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

@balancer-labs/balancer-maths 需要什么版本的 Node.js?

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

@balancer-labs/balancer-maths 可以商用吗?

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

@balancer-labs/balancer-maths 的下载量有多少?

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

@balancer-labs/balancer-maths 还在维护吗?

最新版本 0.0.41 发布于 2026/07/01(4 个月前);至今共发布 41 个版本,其中过去 12 个月发布了 4 个;首次发布于 2024/06/03。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。