# @cowprotocol/permit-utils 中文文档

规范地址：https://npmcn.com/package/%40cowprotocol/permit-utils/

版本：3\.13\.1
npm 元数据更新时间：2026-11-11T14:14:21.039Z
中文译文更新时间：2026-11-14T16:38:27.165Z
软件包许可：ISC

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40cowprotocol/permit-utils
原文对照：https://npmcn.com/package/%40cowprotocol/permit-utils/?lang=original
项目主页：https://github.com/cowprotocol/cowswap#readme
源码仓库：https://github.com/cowprotocol/cowswap

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：80.7 kB，25 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：包内自带
- 运行依赖：6 个
- 首次发布：2023-10-30T17:14:53.519Z
- 最新版本发布：2026-11-11T14:14:20.199Z
- 近 30 天下载量：1978（2026-10-11 至 2026-11-09，npm 官方下载统计）
- 已发布版本数：53（近 12 个月 38 个）

## 中文简介

收集用于处理象征性许可的提款机.

## 安装

```sh
npm install @cowprotocol/permit-utils
```

也可以使用：`pnpm add @cowprotocol/permit-utils`、`yarn add @cowprotocol/permit-utils`、`bun add @cowprotocol/permit-utils`

## README

# @cowprotocol/许可证使用

收集用于处理象征性许可的提款机.

## 安装

```bash
npm install @cowprotocol/permit-utils
```

## 使用量

### `getTokenPermitInfo`

```typescript
import { getTokenPermitInfo } from "@cowprotocol/permit-utils"

const permitInfo = await getTokenPermitInfo({
  spender, tokenAddress, tokenName, chainId, provider
})
```

### `getPermitUtilsInstance`

```typescript
import { getPermitUtilsInstance } from "@cowprotocol/permit-utils"

// Using a static account defined in the library
const staticEip2612PermitUtils = await getPermitUtilsInstance(chainId, provider)

// Using a provided account address
const accountEip2612PermitUtils = await getPermitUtilsInstance(chainId, provider, account)
```

### `generatePermitHook`

```typescript
import { generatePermitHook } from "@cowprotocol/permit-utils"

const hookData = await generatePermitHook({
  chainId,
  inputToken,
  spender,
  provider,
  permitInfo,
  eip2612Utils,
  account,
  nonce
})
```

### `checkIsCallDataAValidPermit`

```typescript
import { checkIsCallDataAValidPermit } from "@cowprotocol/permit-utils"

const isCallDataAValidPermit = await checkIsCallDataAValidPermit(
  account,
  chainId,
  eip2612Utils,
  tokenAddress,
  tokenName,
  callData,
  permitInfo
)
```

**全流程示例**

说明一下,我们将展示使用这种液态的电动装置,用许可钩下订单的流程.

1.  检查给定的令牌是否是许可表
2.  如果是的话, 生成一个许可钩
3.  添加许可钩子以订购 appData
4.  定期检查许可证是否仍然有效

```typescript
import { checkIsCallDataAValidPermit, generatePermitHook, getPermitUtilsInstance, getTokenPermitInfo } from '@cowprotocol/permit-utils'
import { stringifyDeterministic, OrderBookApi } from '@cowprotocol/cow-sdk'


// Check whether token is permittable.
// No account info is necessary.
// `spender` could in theory be any address
// `tokenName` must always match the contract
const permitInfo = await getTokenPermitInfo({
  spender, tokenAddress, tokenName, chainId, provider
})

// Not able to tell or not permittable
if (!permitInfo) {
  return
}

// Pass in an account address as we'll need the user to sign the actual permit
const eip2612Utils = await getPermitUtilsInstance(chainId, provider, account)

// Need to know what the current permit nonce is
const nonce = await eip2612Utils.getTokenNonce(inputToken.address, account)

// Calling this fn should trigger the signature in the user's wallet
const hookData = await generatePermitHook({
  chainId,
  inputToken,
  spender, // Now `spender` must be the real one
  provider,
  permitInfo,
  eip2612Utils,
  account, // `account` should also be the user's
  nonce
})

// Add the hookData to the order's appData
// See the full reference on https://www.npmjs.com/package/@cowprotocol/sdk-app-data?activeTab=code
const appData = { version: '0.10.0', metadata: { hooks: { pre: [hookData] } } }

// The order expects the stringified JSON doc
const fullAppData = await stringifyDeterministic(appData)

// Build cow-sdk orderBookApi instance
const orderBookApi = new OrderBookApi()

// Note: price quoting is a required step before placing the order but has been left out of this example for brevity

// Place order
const orderId = await orderBookApi.sendOrder(
  {
    ...orderParameters,
    quoteId, // Must be fetched before hand, and also include the appData in the params
    appData: fullAppData // <-- The order will be placed with the permit info
  },
  { chainId }
)

// Now we check whether the permit included in the order is still valid
// It'll become invalid when another permit is executed
// This check does not consider whether the `address` has enough allowance, it purelly checks the permit validity.

// CallData is part of the hookData
const callData = hookData.callData

// All properties must match what was used when the hookData was generated
const isCallDataAValidPermit = await checkIsCallDataAValidPermit(
  account,
  chainId,
  eip2612Utils,
  tokenAddress,
  tokenName,
  callData,
  permitInfo
)
```

## 常见问题

### @cowprotocol/permit-utils 是什么？

收集用于处理象征性许可的提款机\.

### 如何安装 @cowprotocol/permit-utils？

在项目目录运行 npm install @cowprotocol/permit-utils，或使用 pnpm add / yarn add / bun add。

### @cowprotocol/permit-utils 支持 TypeScript 吗？

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

### @cowprotocol/permit-utils 支持 ESM 还是 CommonJS？

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

### @cowprotocol/permit-utils 可以商用吗？

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

### @cowprotocol/permit-utils 的下载量有多少？

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

### @cowprotocol/permit-utils 还在维护吗？

最新版本 3\.13\.1 发布于 2026/11/11（3 天前）；至今共发布 53 个版本，其中过去 12 个月发布了 38 个；首次发布于 2023/10/31。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

