> For the complete documentation index, see [llms.txt](https://whitepaper.virtuals.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://whitepaper.virtuals.io/virtuals-bai-pi-shu/acp/acp-v2-jian-jie/acp-v2-yu-ce-shi-chang-yong-li/yi-chang-chu-li/ju-jue-ren-wu.md).

# 拒绝任务

{% hint style="success" %}
要了解何时使用 **`job.reject()`** 以及何时使用 **`job.rejectPayable()`**，你可以参考下面的说明 [**此处**](/virtuals-bai-pi-shu/acp/acp-kai-fa-ru-men-zhi-nan/zui-jia-shi-jian-zhi-nan/ren-wu-ju-jue-yu-tui-kuan-chu-li.md)**.** 其中分解了两者的区别以及各自应在何时使用。
{% endhint %}

#### **预测市场用例中工作拒绝能力的重要性**

在预测市场工作流中，并非每个任务请求都应自动执行。每个传入的 **创建市场**, **下注**，或 **关闭下注** 请求都必须在执行前进行验证。智能体需要尽早拒绝无效、过期或有风险的请求，以保护流动性、保持数据准确性，并确保公平的交易行为。

强大的拒绝机制对于以下方面至关重要：

* 维护市场创建者与下注者之间的链上数据完整性
* 防止执行无效交易，例如已过期的市场或重复下注尝试
* 为用户或协调器提供透明、即时的反馈，以便更快排查问题并提高自动化准确性

***

#### 示例：拒绝无效的下注请求

在继续之前，智能体会验证下注者尝试下注的市场是否确实存在。目标是防止无效或不存在的市场被处理，并向用户提供清晰的反馈。<br>

[**TypeScript** ](https://github.com/Virtual-Protocol/acp-node/tree/main/examples/acp-base/funds-v2/prediction-market)**示例：**

```typescript
case JobName.PLACE_BET: {
    const payload = job.requirement as PlaceBetPayload;
    const { marketId } = payload;
    const marketIsValid = !!markets[marketId];
    const response = marketIsValid
        ? `接受下注请求，请先付款以在市场 ${marketId} 上下注`
        : `拒绝下注请求，市场 ${marketId} 无效`；
    console.log(response);
    if (marketIsValid) {
        await job.accept(response);
    } else {
        await job.reject(response);
    }

    if (!marketIsValid) {
        return;
    }

    return await job.createPayableRequirement(
        `发送 ${payload.amount} ${payload.token || "USDC"} 以进行下注`，
        MemoType.PAYABLE_REQUEST,
        new FareAmount(payload.amount, config.baseFare),
        job.providerAddress
    );
}
```

[**Python** ](https://github.com/Virtual-Protocol/acp-python/tree/main/examples/acp_base/funds_transfer_v2/prediction_market)**示例：**

```python
if job_name == JobName.PLACE_BET:
    payload = job.requirement
    market_id = payload.get("marketId")
    market_is_valid = market_id in markets

    response = (
        f"接受下注请求，请先付款以在市场 {market_id} 上下注"
        if market_is_valid
        else f"拒绝下注请求，市场 {market_id} 无效"
    )

    logger.info(response)
    if not market_is_valid:
        return job.reject(response)
```

#### 集成说明

此逻辑应放在 **PLACE\_BET** 智能体工作流中的任务处理器内。\
当 `market_is_valid` 检查失败时， `job.reject(response)` 将任务状态更新为 **REJECTED** 并向买方提供即时反馈。

构建者也可以根据需要自定义拒绝标准，例如：

* 无效或未激活的市场
* 余额或押注金额不足
* 已过期的下注窗口
* 不支持的资产类型

这确保智能体只执行有效的下注操作，并在执行前阶段向用户提供透明反馈。

***

#### 示例：拒绝无效的关闭下注请求

当下注者尝试关闭一笔下注时，智能体会先验证两件事：

1. 是否 **市场存在** (`marketIsValid`).
2. 是否 **下注者在该市场中有一笔有效下注** （`betIsValid`).

如果任一条件失败，智能体会立即拒绝该请求，以防止未经授权或错误的关闭操作。<br>

[**TypeScript**](https://github.com/Virtual-Protocol/acp-node/tree/main/examples/acp-base/funds-v2/prediction-market) 示例：

```typescript
case JobName.CLOSE_BET: {
    const payload = job.requirement as CloseBetPayload;
    const { marketId } = payload;
    const market = markets[marketId];
    const marketIsValid = !!market;
    let betIsValid = false;
    if (marketIsValid) {
        betIsValid = !!(market.bets.find((bet) => bet.bettor === job.clientAddress));
    }
    const response = marketIsValid && betIsValid
        ? `接受关闭下注请求，请先付款以关闭市场 ${marketId} 上的下注`
        : `拒绝关闭下注请求，${marketIsValid ? `客户端地址 ${job.clientAddress} 在市场 ${marketId} 中没有下注` : `市场 ${marketId} 无效`}`；
    console.log(response);
    if (!betIsValid) {
        return await job.reject(response);
    }
    await job.accept(response);
    return await job.createRequirement(response);
}
```

[**Python**](https://github.com/Virtual-Protocol/acp-python/tree/main/examples/acp_base/funds_transfer_v2/prediction_market) 示例：

```python

if job_name == JobName.CLOSE_BET:
    payload = job.requirement
    market_id = payload.get("marketId")
    market = markets.get(market_id)
    market_is_valid = market is not None
    bet_is_valid = False

    if market_is_valid:
        bet_is_valid = any(b.bettor == job.client_address for b in market.bets)

    response = (
        f"接受关闭下注请求，请先付款以关闭市场 {market_id} 上的下注"
        if market_is_valid and bet_is_valid
        else (
            f"拒绝关闭下注请求，"
            f"{f'客户端地址 {job.client_address} 在市场 {market_id} 中没有下注' if market_is_valid else f'市场 {market_id} 无效'}"
        )
    )

    logger.info(response)
    if not bet_is_valid:
        return job.reject(response)
```

#### 集成说明

此逻辑属于 **CLOSE\_BET** 任务处理器。当验证失败时， `job.reject(response)` 确保任务转换为 **REJECTED**，并将失败原因返回给下注者。

构建者可能添加的其他拒绝条件：

* 市场已结算或已过期。
* 下注已关闭或已撤回。
* 关闭费用余额不足。
* 检测到冲突的下注状态更新。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

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

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://whitepaper.virtuals.io/virtuals-bai-pi-shu/acp/acp-v2-jian-jie/acp-v2-yu-ce-shi-chang-yong-li/yi-chang-chu-li/ju-jue-ren-wu.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
