# 现金出入金

## 入金前置授权

入金交易从用户钱包扣除现金 token。调用入金接口前，用户钱包需对当前 chain / product 对应的 spender 完成 ERC-20 `approve`：

```
ERC20(tokenAddress).approve(spender, rawTokenAmount)
```

其中 `rawTokenAmount = tokenAmount × 10^tokenDecimals`。

### Mainnet Spender 地址表

| `x-api-chain-id` | Chain            | `x-api-p`     | Spender                                      |
| ---------------- | ---------------- | ------------- | -------------------------------------------- |
| `1`              | Ethereum Mainnet | `Anchored`    | `0x8c1b182bb0fe4e8407404ffe37c974d6dacef3a9` |
| `143`            | Monad Mainnet    | `Anchored`    | `0x8c1b182bb0fe4e8407404ffe37c974d6dacef3a9` |
| `143`            | Monad Mainnet    | `MondayTrade` | `0xb983787a2882da59bbf9e09cb5a0e47ff0c30141` |

### 检查授权额度

```http
GET /api/v1/users/{userId}/balance?spenderAddress={spender}
```

`tokenBalances[].walletAllowance` 即为用户钱包对该 spender 的授权额度。

{% hint style="warning" %}
授权不足时，`/cash/deposits/calldata` 仍会正常返回 calldata，但后续链上交易会失败。
{% endhint %}

***

## 入金接口

### 构建入金 Calldata

```
POST /api/v1/cash/deposits/calldata
```

| 字段             | 类型              | 必填 | 说明             |
| -------------- | --------------- | -- | -------------- |
| `userAddress`  | string          | 是  | 用户钱包地址。        |
| `tokenAddress` | string          | 是  | 现金 token 合约地址。 |
| `tokenAmount`  | string / number | 是  | 入金数量（人类可读单位）。  |

返回 `method` 为 `deposit`，`value` 为 `"0"`。

### One Click 代发入金

```
POST /api/v1/cash/deposits/send
```

| 字段             | 类型              | 必填 | 说明             |
| -------------- | --------------- | -- | -------------- |
| `tokenAddress` | string          | 是  | 现金 token 合约地址。 |
| `tokenAmount`  | string / number | 是  | 入金数量。          |
| `gasLimit`     | number          | 否  | 指定 gas limit。  |

{% hint style="info" %}
该接口使用 **API Key 绑定的 `userAddress`**，不读取请求体中的 `userAddress`。
{% endhint %}

### 记录入金 TxHash

```
POST /api/v1/cash/deposits/tx
```

| 字段       | 类型     | 必填 | 说明             |
| -------- | ------ | -- | -------------- |
| `txHash` | string | 是  | 已提交的入金交易 hash。 |

### 查询入金记录

```
GET /api/v1/cash/deposits?page=1&limit=10
GET /api/v1/cash/deposits/{operationId}
```

`operationId` 由 indexer 解析链上事件后回填。

***

## 出金接口

### 构建出金 Calldata

```
POST /api/v1/cash/withdrawals/calldata
```

| 字段             | 类型              | 必填 | 说明                    |
| -------------- | --------------- | -- | --------------------- |
| `userAddress`  | string          | 是  | 用户钱包地址。               |
| `tokenAddress` | string          | 是  | 现金 token 合约地址。        |
| `creditAmount` | string / number | 是  | 出金 credit 数量（人类可读单位）。 |

返回 `method` 为 `withdraw`，`value` 为 `"0"`。

### One Click 代发出金

```
POST /api/v1/cash/withdrawals/send
```

### 记录出金 TxHash

```
POST /api/v1/cash/withdrawals/tx
```

### 查询出金记录

```
GET /api/v1/cash/withdrawals?page=1&limit=10
GET /api/v1/cash/withdrawals/{operationId}
```

***

## 出入金详情字段

`TradingCashOperationDetailRespDto`：

| 字段                    | 类型            | 说明        |
| --------------------- | ------------- | --------- |
| `mappingStatus`       | string / null | TX 映射状态。  |
| `operationStatus`     | string / null | 链上现金操作状态。 |
| `mapping`             | object        | 映射记录。     |
| `depositOperation`    | object / null | 入金明细。     |
| `withdrawalOperation` | object / null | 出金明细。     |

### depositOperation 常用字段

| 字段             | 类型            | 说明                 |
| -------------- | ------------- | ------------------ |
| `operationId`  | string        | 入金操作 ID。           |
| `userAddress`  | string        | 用户地址。              |
| `tokenAddress` | string        | Token 地址。          |
| `status`       | string        | 操作状态。              |
| `isInstant`    | boolean       | 是否 instant。        |
| `amount`       | number        | Token 数量（人类可读单位）。  |
| `creditAmount` | number        | Credit 数量（人类可读单位）。 |
| `feeAmount`    | number        | Fee 数量（人类可读单位）。    |
| `createTxHash` | string        | 创建交易 hash。         |
| `settleTxHash` | string / null | 结算交易 hash。         |


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.anchored.finance/trading-api-docs-zh/api-jie-kou/cash-operations.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
