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

# AssetOperations

> Handles balance queries, transfers, minting, and merging of CTF tokens and collateral

## Overview

The `AssetOperations` mixin provides core functionality for handling CTF (Conditional Token Framework) and collateral assets. It implements balance fetching, transfers, and CTF-specific operations like minting (splitting) and merging positions.

Source: `src/exchange/mixins/AssetOperations.sol`

## Constants

### `parentCollectionId`

```solidity theme={null}
bytes32 public constant parentCollectionId = bytes32(0)
```

The parent collection ID used for CTF operations. A zero value indicates top-level positions with no parent collection.

## Functions

### `_getBalance`

```solidity theme={null}
function _getBalance(uint256 tokenId) internal override returns (uint256)
```

Gets the contract's balance of either collateral or a conditional token.

#### Parameters

* `tokenId` (uint256):
  * `0` for collateral (ERC20) balance
  * Any other value for the ERC1155 CTF token balance

#### Returns

* `uint256`: The token balance

### `_transfer`

```solidity theme={null}
function _transfer(address from, address to, uint256 id, uint256 value) internal override
```

Transfers a quantity of assets from one address to another. Routes to either `_transferCollateral` or `_transferCTF` based on the token ID.

#### Parameters

* `from` (address): The account to transfer assets from
* `to` (address): The account to transfer assets to
* `id` (uint256):
  * `0` for collateral (ERC20)
  * Any other value for CTF tokens (ERC1155)
* `value` (uint256): The amount of assets to transfer

### `_transferCollateral`

```solidity theme={null}
function _transferCollateral(address from, address to, uint256 value) internal
```

Transfers ERC20 collateral using the `TransferHelper` library. Uses either `transfer` or `transferFrom` depending on whether the `from` address is the contract itself.

#### Parameters

* `from` (address): The account to transfer tokens from
* `to` (address): The account to transfer tokens to
* `value` (uint256): The amount of ERC20 tokens to transfer

### `_transferCTF`

```solidity theme={null}
function _transferCTF(address from, address to, uint256 id, uint256 value) internal
```

Transfers ERC1155 CTF tokens using the `TransferHelper` library.

#### Parameters

* `from` (address): The account to transfer tokens from
* `to` (address): The account to transfer tokens to
* `id` (uint256): The ERC1155 token ID
* `value` (uint256): The amount of tokens to transfer

### `_mint`

```solidity theme={null}
function _mint(bytes32 conditionId, uint256 amount) internal override
```

Mints a full conditional token set from collateral by calling `splitPosition` on the CTF contract. This converts X units of collateral (ERC20) into X units of each complementary outcome token (ERC1155).

For binary outcomes, this creates equal amounts of both outcome tokens (e.g., YES and NO tokens).

#### Parameters

* `conditionId` (bytes32): The ID of the condition to split on
* `amount` (uint256): The quantity of collateral to split (note: collateral and minted conditional tokens use the same number of decimals)

#### Implementation Details

* Uses `parentCollectionId` of zero (bytes32(0))
* Uses a binary partition \[1, 2] for the two outcome tokens
* Calls `IConditionalTokens.splitPosition()` on the CTF contract

<Note>
  Learn more about Gnosis Conditional Tokens in the [official documentation](https://docs.gnosis.io/conditionaltokens/docs/devguide01/).
</Note>

### `_merge`

```solidity theme={null}
function _merge(bytes32 conditionId, uint256 amount) internal override
```

Merges (burns) complete sets of conditional tokens back into collateral by calling `mergePositions` on the CTF contract. This converts X units of each complementary outcome token (ERC1155) into X units of collateral (ERC20).

For binary outcomes, this requires equal amounts of both outcome tokens (e.g., YES and NO tokens).

#### Parameters

* `conditionId` (bytes32): The ID of the condition to merge on
* `amount` (uint256): The quantity of complete sets to burn for their underlying collateral

#### Implementation Details

* Uses `parentCollectionId` of zero (bytes32(0))
* Uses a binary partition \[1, 2] for the two outcome tokens
* Calls `IConditionalTokens.mergePositions()` on the CTF contract

<Note>
  Learn more about Gnosis Conditional Tokens in the [official documentation](https://docs.gnosis.io/conditionaltokens/docs/devguide01/).
</Note>

## Usage in Binary Matching

The `_mint` and `_merge` functions are critical for binary matching:

* **Mint**: When matching buy orders for complementary tokens (A and A'), the exchange mints a full set from collateral, then transfers token A to one buyer and token A' to the other.

* **Merge**: When matching sell orders for complementary tokens (A and A'), the exchange receives both tokens from the sellers, merges them back into collateral, and pays out the sellers in collateral.

This mechanism ensures efficient market making for binary outcome markets while maintaining the 1:1 relationship between complete sets and collateral.
