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

# 库存管理

> 做市的结果代币库存管理

做市商需要双方的结果代币来报价市场。三个核心库存操作是将 pUSD **拆分**为 YES/NO 代币对、将代币对**合并**回 pUSD,以及在判定后**兑换**获胜代币——所有操作都通过 Relayer Client 免 gas 执行。

<Info>
  有关条件代币框架工作原理的完整说明,请参阅 [CTF 概述](/trading/ctf/overview)。本页重点介绍使用 Relayer Client 的做市商工作流程。
</Info>

***

## 将 pUSD 拆分为代币

拆分将 pUSD 转换为等量的 YES 和 NO 代币——创建你报价市场双方所需的库存。

<CodeGroup>
  ```typescript TypeScript theme={null}
  import { ethers } from "ethers";
  import { Interface } from "ethers/lib/utils";
  import { RelayClient, Transaction } from "@polymarket/builder-relayer-client";

  const CTF_ADDRESS = "0x4D97DCd97eC945f40cF65F87097ACe5EA0476045";
  const pUSD_ADDRESS = "0xC011a7E12a19f7B1f670d46F03B03f3342E82DFB";

  const ctfInterface = new Interface([
    "function splitPosition(address collateralToken, bytes32 parentCollectionId, bytes32 conditionId, uint[] partition, uint amount)",
  ]);

  // Split $1000 pUSD into YES/NO tokens
  const amount = ethers.utils.parseUnits("1000", 6); // pUSD has 6 decimals

  const splitTx: Transaction = {
    to: CTF_ADDRESS,
    data: ctfInterface.encodeFunctionData("splitPosition", [
      pUSD_ADDRESS, // collateralToken
      ethers.constants.HashZero, // parentCollectionId (always zero for Polymarket)
      conditionId, // conditionId from market
      [1, 2], // partition: [YES, NO]
      amount,
    ]),
    value: "0",
  };

  const response = await client.execute([splitTx], "Split pUSD into tokens");
  const result = await response.wait();
  console.log("Split completed:", result?.transactionHash);
  ```

  ```python Python theme={null}
  from web3 import Web3

  CTF_ADDRESS = "0x4D97DCd97eC945f40cF65F87097ACe5EA0476045"
  pUSD_ADDRESS = "0xC011a7E12a19f7B1f670d46F03B03f3342E82DFB"

  ctf_abi = [{
      "name": "splitPosition",
      "type": "function",
      "inputs": [
          {"name": "collateralToken", "type": "address"},
          {"name": "parentCollectionId", "type": "bytes32"},
          {"name": "conditionId", "type": "bytes32"},
          {"name": "partition", "type": "uint256[]"},
          {"name": "amount", "type": "uint256"}
      ],
      "outputs": []
  }]

  # Split $1000 pUSD into YES/NO tokens
  amount = 1000 * 10**6  # pUSD has 6 decimals

  split_tx = {
      "to": CTF_ADDRESS,
      "data": Web3().eth.contract(
          address=CTF_ADDRESS, abi=ctf_abi
      ).encode_abi(
          abi_element_identifier="splitPosition",
          args=[
              pUSD_ADDRESS,
              bytes(32),          # parentCollectionId (always zero)
              condition_id,       # conditionId from market
              [1, 2],             # partition: [YES, NO]
              amount,
          ]
      ),
      "value": "0"
  }

  response = client.execute([split_tx], "Split pUSD into tokens")
  response.wait()
  ```
</CodeGroup>

拆分 1000 pUSD 后,你会收到 1000 个 YES 代币和 1000 个 NO 代币。你的 pUSD 余额减少 1000。

***

## 将代币合并为 pUSD

合并将等量的 YES 和 NO 代币转换回 pUSD——适用于减少敞口、退出市场或释放资金。

<CodeGroup>
  ```typescript TypeScript theme={null}
  const ctfInterface = new Interface([
    "function mergePositions(address collateralToken, bytes32 parentCollectionId, bytes32 conditionId, uint[] partition, uint amount)",
  ]);

  // Merge 500 YES + 500 NO back to 500 pUSD
  const amount = ethers.utils.parseUnits("500", 6);

  const mergeTx: Transaction = {
    to: CTF_ADDRESS,
    data: ctfInterface.encodeFunctionData("mergePositions", [
      pUSD_ADDRESS,
      ethers.constants.HashZero,
      conditionId,
      [1, 2],
      amount,
    ]),
    value: "0",
  };

  const response = await client.execute([mergeTx], "Merge tokens to pUSD");
  await response.wait();
  ```

  ```python Python theme={null}
  merge_abi = [{
      "name": "mergePositions",
      "type": "function",
      "inputs": [
          {"name": "collateralToken", "type": "address"},
          {"name": "parentCollectionId", "type": "bytes32"},
          {"name": "conditionId", "type": "bytes32"},
          {"name": "partition", "type": "uint256[]"},
          {"name": "amount", "type": "uint256"}
      ],
      "outputs": []
  }]

  # Merge 500 YES + 500 NO back to 500 pUSD
  amount = 500 * 10**6

  merge_tx = {
      "to": CTF_ADDRESS,
      "data": Web3().eth.contract(
          address=CTF_ADDRESS, abi=merge_abi
      ).encode_abi(
          abi_element_identifier="mergePositions",
          args=[pUSD_ADDRESS, bytes(32), condition_id, [1, 2], amount]
      ),
      "value": "0"
  }

  response = client.execute([merge_tx], "Merge tokens to pUSD")
  response.wait()
  ```
</CodeGroup>

合并各 500 个后,你的 YES 和 NO 余额各减少 500,pUSD 余额增加 500。

***

## 判定后兑换

市场判定后,将获胜代币兑换为 pUSD。每个获胜代币价值 $1——失败代币兑换为 $0。

### 检查判定状态

<CodeGroup>
  ```typescript TypeScript theme={null}
  const market = await clobClient.getMarket(conditionId);
  if (market.closed) {
    const winningToken = market.tokens.find((t) => t.winner);
    console.log("Winning outcome:", winningToken?.outcome);
  }
  ```

  ```python Python theme={null}
  market = clob_client.get_market(condition_id)
  if market.get("closed"):
      winning = next(t for t in market["tokens"] if t.get("winner"))
      print("Winning outcome:", winning["outcome"])
  ```
</CodeGroup>

### 兑换获胜代币

<CodeGroup>
  ```typescript TypeScript theme={null}
  const ctfInterface = new Interface([
    "function redeemPositions(address collateralToken, bytes32 parentCollectionId, bytes32 conditionId, uint[] indexSets)",
  ]);

  const redeemTx: Transaction = {
    to: CTF_ADDRESS,
    data: ctfInterface.encodeFunctionData("redeemPositions", [
      pUSD_ADDRESS,
      ethers.constants.HashZero,
      conditionId,
      [1, 2], // Redeem both YES and NO (only winners pay out)
    ]),
    value: "0",
  };

  const response = await client.execute([redeemTx], "Redeem winning tokens");
  await response.wait();
  ```

  ```python Python theme={null}
  redeem_abi = [{
      "name": "redeemPositions",
      "type": "function",
      "inputs": [
          {"name": "collateralToken", "type": "address"},
          {"name": "parentCollectionId", "type": "bytes32"},
          {"name": "conditionId", "type": "bytes32"},
          {"name": "indexSets", "type": "uint256[]"}
      ],
      "outputs": []
  }]

  redeem_tx = {
      "to": CTF_ADDRESS,
      "data": Web3().eth.contract(
          address=CTF_ADDRESS, abi=redeem_abi
      ).encode_abi(
          abi_element_identifier="redeemPositions",
          args=[pUSD_ADDRESS, bytes(32), condition_id, [1, 2]]
      ),
      "value": "0"
  }

  response = client.execute([redeem_tx], "Redeem winning tokens")
  response.wait()
  ```
</CodeGroup>

***

## Negative Risk 市场

多结果市场使用 Neg Risk CTF Exchange 和 Neg Risk Adapter。拆分和合并的工作方式相同,但使用不同的合约地址:

```typescript theme={null}
const NEG_RISK_CTF_EXCHANGE = "0xe2222d279d744050d28e00520010520000310F59";
const NEG_RISK_ADAPTER = "0xd91E80cF2E7be2e162c6513ceD06f1dD0dA35296";
```

有关多结果代币机制如何不同的详情,请参阅 [Negative Risk 市场](/advanced/neg-risk)。

***

## 库存策略

### 报价前

1. 通过 [Gamma API](/market-data/fetching-markets) 检查市场元数据
2. 拆分足够的 pUSD 以覆盖你的预期报价规模
3. 如果尚未完成,设置代币授权(参见[入门](/market-makers/getting-started))

### 交易期间

* 当库存在某一侧失衡时**倾斜报价**
* **合并多余代币**以释放资金用于其他市场
* 当任一侧库存不足时**拆分更多**

### 判定后

1. 取消市场中的所有未成交订单
2. 等待判定完成
3. 兑换获胜代币
4. 合并任何剩余的 YES/NO 代币对

***

## 批量操作

在单个中继器调用中执行多个库存操作以提高效率:

```typescript theme={null}
const transactions: Transaction[] = [
  // Split on Market A
  {
    to: CTF_ADDRESS,
    data: ctfInterface.encodeFunctionData("splitPosition", [
      pUSD_ADDRESS,
      ethers.constants.HashZero,
      conditionIdA,
      [1, 2],
      ethers.utils.parseUnits("1000", 6),
    ]),
    value: "0",
  },
  // Split on Market B
  {
    to: CTF_ADDRESS,
    data: ctfInterface.encodeFunctionData("splitPosition", [
      pUSD_ADDRESS,
      ethers.constants.HashZero,
      conditionIdB,
      [1, 2],
      ethers.utils.parseUnits("1000", 6),
    ]),
    value: "0",
  },
];

const response = await client.execute(transactions, "Batch inventory setup");
await response.wait();
```

***

## 下一步

<CardGroup cols={2}>
  <Card title="CTF 概述" icon="coins" href="/trading/ctf/overview">
    条件代币框架的底层工作原理
  </Card>

  <Card title="拆分代币" icon="scissors" href="/trading/ctf/split">
    详细的拆分函数参数和前提条件
  </Card>

  <Card title="合并代币" icon="merge" href="/trading/ctf/merge">
    详细的合并函数参数
  </Card>

  <Card title="免 gas 交易" icon="gas-pump" href="/trading/gasless">
    Relayer Client 设置和配置
  </Card>
</CardGroup>
