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

# MPP API 参考

> @monad-crypto/mpp 包的参考文档

本页是 `@monad-crypto/mpp` 的技术参考。如需面向开发者的指南和示例,请参阅 [概述](/zh/reference/mpp/overview)。

## 服务端

### 示例

```ts title="server.ts" highlight={9-13} theme={null}
import { monad } from "@monad-crypto/mpp/server";
import { Mppx } from "mppx";
import { privateKeyToAccount } from "viem/accounts";

const account = privateKeyToAccount(process.env.SERVER_PRIVATE_KEY as `0x${string}`);

const mppx = Mppx.create({
  methods: [
    monad({
      account,
      recipient: account.address,
      // ... 参数见下方
    }),
  ],
});
```

### 参数

`monad` 服务端支付方法接受以下参数。

| 参数                    | 类型                                         | 默认值     | 描述                                                                                |
| --------------------- | ------------------------------------------ | ------- | --------------------------------------------------------------------------------- |
| `amount`              | `string`                                   | —       | 默认支付金额,人类可读格式(例如 `"1.50"`)                                                        |
| `currency`            | `string`                                   | USDC    | ERC-20 代币合约地址                                                                     |
| `decimals`            | `number`                                   | `6`     | 代币小数位数                                                                            |
| `description`         | `string`                                   | —       | 人类可读的描述                                                                           |
| `externalId`          | `string`                                   | —       | 会在收据中回传的外部标识符                                                                     |
| `recipient`           | `string`                                   | —       | 收款地址                                                                              |
| `testnet`             | `boolean`                                  | `false` | 测试网模式                                                                             |
| `waitForConfirmation` | `boolean`                                  | `true`  | 是否等待收款交易在链上确认                                                                     |
| `getClient`           | `(params: { chainId?: number }) => Client` | —       | 根据给定 chain ID 返回 viem Client 的函数                                                  |
| `account`             | `Account \| Address`                       | —       | 用于广播 `receiveWithAuthorization` 交易的服务端账户。接受 `authorization` 载荷时必需。服务端将从此账户支付 gas。 |
| `store`               | `Store`                                    | —       | 用于交易哈希防重放的存储。在多实例部署中请使用共享存储,以便所有服务实例共同看到已消费的哈希。                                   |

## 客户端

### 示例

```ts title="client.ts" highlight={8-11} theme={null}
import { monad } from "@monad-crypto/mpp/client";
import { Mppx } from "mppx/client";
import { privateKeyToAccount } from "viem/accounts";

const account = privateKeyToAccount(process.env.CLIENT_PRIVATE_KEY as `0x${string}`);

const mppx = Mppx.create({
  methods: [monad({ 
    account,
    // ... 参数见下方
  })],
});

// 发起付费请求
const response = await fetch("http://localhost:3000/premium");
const data = await response.json();
console.log(data);
```

### 参数

`monad` 客户端支付方法接受以下参数。

| 参数          | 类型                                         | 默认值 | 描述                                           |
| ----------- | ------------------------------------------ | --- | -------------------------------------------- |
| `account`   | `Account \| Address`                       | —   | 钱包账户。如省略,则从 viem Client 解析。                  |
| `mode`      | `"push" \| "pull"`                         | 自动  | 支付模式。JSON-RPC 账户默认 `"push"`,本地账户默认 `"pull"`。 |
| `getClient` | `(params: { chainId?: number }) => Client` | —   | 自定义 viem Client 解析器。如未提供则使用内置 RPC URL。       |
