> ## Documentation Index
> Fetch the complete documentation index at: https://docs.powfi.alephium.org/llms.txt
> Use this file to discover all available pages before exploring further.

# 集成方手续费

> 对通过你的应用路由的 CPMM 和 CLMM 兑换收取手续费。

export const VersionBanner = ({network = 'mainnet', products = [], lang = 'en'}) => {
  const SDK_VERSION = '1.0.2';
  const ADDRESSES = {
    mainnet: {
      cpmm: {
        Router: 'ze15rnAwTyVCPnDhN7J8An3SvNeWBsZHAVMeBx1ehXeP',
        TokenPairFactory: '24CXRLvj1QiDH6riwr1EPERG7BcduHKZtktygbq97MfYb'
      },
      clmm: {
        PoolFactory: '24gqd4DMQXVzjm5QDAGxUtYupx3WM2PSu3vZUseuBCV9h',
        PositionManager: 'vFyKSqnJHS9MicrojBcLgztyRFqhS4hB349Ci3LJCELw'
      },
      staking: {
        XAlphToken: '225WevmFp5ZgzPsyVJTvyp2v2uyKrvp329HfrVmzffnWj',
        RewardFeeCollector: '22CE7vG6u64zjjsLZLxeJqQuPDydjPvbP9iG1d861j2G7'
      }
    },
    testnet: {
      cpmm: {
        Router: 'xaueT8kPMvpnaFJEfHv6CJLgqsSPpoNmHjF1QgTxoF9R',
        TokenPairFactory: '29hdd9b9Gp7oXuamwHKfUqBaRXP487HoeTcutS3RhZJEj'
      },
      clmm: {
        PoolFactory: 'z2QFT7qBQrfm8UwAYG3VHWoHs7B56ZDFNdSCcDu5j53m',
        PositionManager: '21p6egW8d6FEyVVuH17WqvHCARsCsD61GcBmeCr75XdGT'
      },
      staking: {
        XAlphToken: '23psDicimC5VdpM56wrgqgCzQzcyXWGCkQGSYWbtZAPRq',
        RewardFeeCollector: '29qQ1LPJ5uqawRviXLVDMeo19JUygRrNNTQy5RUBh817D'
      }
    }
  };
  const EXPLORERS = {
    mainnet: 'https://explorer.alephium.org/addresses/',
    testnet: 'https://testnet.alephium.org/addresses/'
  };
  const LABELS = {
    en: {
      sdk: 'SDK',
      network: 'Network',
      contracts: 'Contracts',
      all: 'All addresses',
      allNetworks: 'mainnet · testnet · devnet',
      devnet: 'Local deployment (addresses vary per devnet)'
    },
    zh: {
      sdk: 'SDK 版本',
      network: '网络',
      contracts: '合约',
      all: '全部地址',
      allNetworks: 'mainnet · testnet · devnet',
      devnet: '本地部署（每个 devnet 的地址不同）'
    },
    fr: {
      sdk: 'SDK',
      network: 'Réseau',
      contracts: 'Contrats',
      all: 'Toutes les adresses',
      allNetworks: 'mainnet · testnet · devnet',
      devnet: 'Déploiement local (adresses propres à chaque devnet)'
    }
  };
  const t = LABELS[lang] ?? LABELS.en;
  const contractsPage = lang === 'en' ? '/reference/contracts' : `/${lang}/reference/contracts`;
  const short = a => `${a.slice(0, 6)}…${a.slice(-4)}`;
  const entries = [];
  const addrs = ADDRESSES[network];
  if (addrs) {
    for (const p of products) {
      for (const [name, address] of Object.entries(addrs[p] ?? ({}))) entries.push({
        name,
        address
      });
    }
  }
  let contractsCell;
  if (network === 'devnet' && products.length > 0) {
    contractsCell = <span>{t.devnet}</span>;
  } else if (entries.length > 0) {
    contractsCell = entries.map(({name, address}, i) => <span key={name}>
        {i > 0 && <span className="mx-1 opacity-50">·</span>}
        {name}{' '}
        <a href={EXPLORERS[network] + address} target="_blank" rel="noreferrer" title={address}>
          <code>{short(address)}</code>
        </a>
      </span>);
  }
  const row = (label, value) => <div className="flex flex-wrap gap-x-2">
      <span className="font-semibold min-w-[5.5rem]">{label}</span>
      <span className="flex-1 min-w-0 break-words">{value}</span>
    </div>;
  return <div className="not-prose mb-6 rounded-xl border border-zinc-950/10 dark:border-white/10 bg-zinc-50 dark:bg-white/5 px-4 py-3 text-sm leading-6 text-zinc-700 dark:text-zinc-300">
      {row(t.sdk, <code>@alephium/powfi-sdk@{SDK_VERSION}</code>)}
      {row(t.network, network === 'all' ? t.allNetworks : network)}
      {contractsCell && row(t.contracts, <span>
            {contractsCell}
            <span className="mx-1 opacity-50">·</span>
            <a href={contractsPage}>{t.all} →</a>
          </span>)}
    </div>;
};

<VersionBanner network="mainnet" products={['cpmm', 'clmm']} lang="zh" />

<Note>
  本页讲的是**兑换**上的手续费。如果你想通过为 Powfi 带来质押用户来分享质押收益，请看[推荐质押与渠道分润](/zh/guides/referral-staking)。这是两套独立的机制。
</Note>

钱包和聚合器可以对每笔兑换收取手续费。手续费以**输入代币**支付，在同一笔交易中发送给 `feeRecipient`，并且是在兑换输入**之外额外**收取的。

## 用法

<CodeGroup>
  ```ts CPMM theme={null}
  await powfi.cpmm.swap({
    tokenInId, tokenOutId,
    amountIn: 1_000n * 10n ** 18n,
    slippageBps: 50n,
    sender: address,
    fee: 3n * 10n ** 18n,          // 3 tokens of tokenIn (0.3%)
    feeRecipient: 'your-fee-address'
  })
  ```

  ```ts CLMM theme={null}
  await powfi.clmm.swap({
    token0: tokenIn, token1: tokenOut,
    amount: amountIn, amountIn,
    slippage: 50n,
    routePlan: [configIndex],
    fee: (amountIn * 30n) / 10_000n,
    feeRecipient: 'your-fee-address'
  })
  ```
</CodeGroup>

用户支付 `amountIn + fee`（精确输入），或 `maximalTokenInAmount + fee`（CPMM 精确输出）。设置了手续费时，SDK 使用 `SwapMinOutWithFee` / `SwapMaxInWithFee`（CPMM）或 `SwapWithoutAccountWithFee`（CLMM）脚本。

## 校验规则

`validateIntegratorFee` 在签名前运行，以下情况会抛出普通 `Error`：

| 规则 | 消息 |
| - | - |
| `fee` / `feeRecipient` 只设置了一个 | `fee and feeRecipient must be provided together` |
| `fee <= 0` | `fee must be greater than 0` |
| `feeRecipient` 与发送方相同 | `feeRecipient must differ from the sender` |
| 输入是 ALPH 且 `fee < DUST_AMOUNT` | `an ALPH fee must be at least the dust amount …` |
| `fee >= amountIn`（精确输出时为 `>=` 最大输入） | `fee must be less than the swapped amount …` |

当输入是非 ALPH 代币时，SDK 会为手续费输出额外附加一个 `DUST_AMOUNT` 的 ALPH。这笔 dust（0.001 ALPH）由用户在 `amountIn + fee` 和 gas 之外额外支付，最后会和手续费一起进入 `feeRecipient` 地址。向用户展示总花费时，记得把它算进去。

无分组的 `feeRecipient` 地址会自动规范化到协议所在分组。


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.