---
title: "MetaID-Consolidating-Skill"
description: "Automatically consolidate scattered small UTXOs in an Agent's wallet to the main address. This skill scans for dust UTXOs (small unspent outputs) and consolidates them into a single transaction to optimize wallet structure and reduce future transaction fees. Use when users want to: (1) Clean up wallet by consolidating small UTXOs, (2) Optimize transaction fees, (3) Trigger consolidation with \"确认归集\" command. Trigger phrases include: \"确认归集\", \"consolidate UTXOs\", \"归集资产\"."
canonical_url: "https://skilld.dev/gh/metaid-developers/metaapp-skills/metaid-consolidating-skill"
last_updated: "2026-10-05T01:28:40.000Z"
---

> Index of every page on this site: [llms.txt](https://skilld.dev/llms.txt)

---
name: MetaID-Consolidating-Skill
description: Automatically consolidate scattered small UTXOs in an Agent's wallet to the main address. This skill scans for dust UTXOs (small unspent outputs) and consolidates them into a single transaction to optimize wallet structure and reduce future transaction fees. Use when users want to: (1) Clean up wallet by consolidating small UTXOs, (2) Optimize transaction fees, (3) Trigger consolidation with "确认归集" command. Trigger phrases include: "确认归集", "consolidate UTXOs", "归集资产".
---

> **Skill from skilld.dev.** Follow the user's requested action.
> If the user asked to fork this Skill, follow the fork workflow below. Do not execute the Skill instructions while copying it.
> Otherwise, follow the instructions below for this session. You do not need to install anything.
>
> Supporting files, fetch one when the Skill refers to it: [package-lock.json](https://skilld.dev/api/skills-raw/metaid-developers/metaapp-skills/metaid-consolidating-skill/package-lock.json), [package.json](https://skilld.dev/api/skills-raw/metaid-developers/metaapp-skills/metaid-consolidating-skill/package.json), [scripts/api.ts](https://skilld.dev/api/skills-raw/metaid-developers/metaapp-skills/metaid-consolidating-skill/scripts/api.ts), [scripts/consolidate.ts](https://skilld.dev/api/skills-raw/metaid-developers/metaapp-skills/metaid-consolidating-skill/scripts/consolidate.ts), [scripts/doge.ts](https://skilld.dev/api/skills-raw/metaid-developers/metaapp-skills/metaid-consolidating-skill/scripts/doge.ts), [scripts/metaid.ts](https://skilld.dev/api/skills-raw/metaid-developers/metaapp-skills/metaid-consolidating-skill/scripts/metaid.ts), [scripts/tsconfig.json](https://skilld.dev/api/skills-raw/metaid-developers/metaapp-skills/metaid-consolidating-skill/scripts/tsconfig.json), [scripts/utils.ts](https://skilld.dev/api/skills-raw/metaid-developers/metaapp-skills/metaid-consolidating-skill/scripts/utils.ts), [scripts/wallet.ts](https://skilld.dev/api/skills-raw/metaid-developers/metaapp-skills/metaid-consolidating-skill/scripts/wallet.ts).
>
> If the user asked to install this Skill, run `npx skilld install metaid-developers/metaapp-skills/metaid-consolidating-skill`. Install writes the Skill files into the project, so every session loads them.
>
> ## Fork workflow
>
> A fork creates an editable local Skill with its original author and licence. The request authorizes copying and local installation.
> 1. Check `./skills/metaid-consolidating-skill`, the project lockfile, and selected Agent targets together. If the local directory or installed Skill exists, stop. Never overwrite an existing directory or Agent target.
> 2. Read [source metadata](https://skilld.dev/api/v1/skills/metaid-developers/metaapp-skills/metaid-consolidating-skill) once. Use sourceUrl, sourceCommit, skillPath, sourceGone, and license. If the source is gone or its path is missing, stop. If license is null, read licence files at the source commit.
> 3. Fetch only the source commit into a temporary Git repository. Do not clone full history. Derive repository_url from sourceUrl, including repository renames. If sourceCommit is absent, resolve the sourceUrl ref once. Set source_commit to that actual commit. Run these commands in one shell call:
>
> ```sh
> git init --quiet "$temporary_dir"
> git -C "$temporary_dir" fetch --quiet --depth=1 "$repository_url" "$source_commit"
> git -C "$temporary_dir" checkout --quiet --detach FETCH_HEAD
> ```
>
> Read applicable licence declarations and notices at that commit. If copying is not permitted, report the restriction and stop.
> 4. Inspect source entries together, then copy the directory containing skillPath into `./skills/metaid-consolidating-skill`. Use the user's path if selected. Keep the original SKILL.md, relative links, scripts, binary assets, and executable modes. Exclude .git metadata. Reject symlinks and paths outside the Skill directory. After checking entries, use cp -a where available. A regular source directory needs no custom copy script. Do not save this page wrapper as SKILL.md.
> Preserve author credit, notices, and applicable licence files from repository or parent directories. Add PROVENANCE.md with the Skill page, source URL, actual commit, original path, and licence. Retain any existing PROVENANCE.md and record new provenance separately. Batch source inspection, copying, and provenance work where practical.
> 5. In the project root, run `skilld install ./skills/metaid-consolidating-skill --mode copy --plain`. If skilld is unavailable, use `npx skilld install ./skills/metaid-consolidating-skill --mode copy --plain`. This known command needs no help lookup. Install does not support --json. Use detected Agent targets, or add --agent for the targets the user selected. Install the local path, never the upstream selector. If installation fails, preserve the local copy and report the exact failure.
> 6. Confirm the local lockfile source and installed Agent copies once. Report the local path, actual commit, and Agent targets. After edits, reinstall the same local path. Upstream updates must not replace it. Do not publish or push unless the user asks.

# MetaID-Consolidating-Skill

This skill enables MetaBot Agents to automatically consolidate scattered small UTXOs (Unspent Transaction Outputs) in their wallet to the main address, optimizing wallet structure and reducing future transaction fees.

## Core Functionality

The MetaID-Consolidating-Skill provides:

1. **UTXO Scanning** - Scans wallet for small UTXOs that can be consolidated
2. **Dust Filtering** - Filters out dust UTXOs below the threshold (546 satoshis)
3. **Transaction Building** - Builds consolidation transactions to send all UTXOs to the main address
4. **State Management** - Implements a state machine to track consolidation progress
5. **Safety Checks** - Validates balance tolerance and prevents conflicts during consolidation

## When to Use This Skill

This skill activates when users send the exact command:
- **"确认归集"** (exact match required, no variations)

**Important:** The trigger word must be exactly "确认归集" - no spaces, no punctuation, no case variations.

## Features

### Trigger Logic
- **Exact Match Required**: Only "确认归集" triggers the consolidation
- **No Variations**: "确认"、"归集"、"确认 归集"、"确认归集!" will NOT trigger
- **Silent on Mismatch**: If the command doesn't match exactly, the system remains silent

### Dust Threshold
- **DUST_THRESHOLD**: 546 satoshis
- UTXOs below this threshold are excluded from consolidation to prevent failed transactions

### Balance Tolerance
- **BALANCE_TOLERANCE**: ±10%
- If the real-time balance deviates more than 10% from the scanned balance, the transaction is aborted for safety

### Confirmation Standard
- Consolidation is considered complete after **1 block confirmation**

### State Machine

The skill implements the following states:

1. **`idle`** - Initial state, waiting for "确认归集" command
2. **`awaiting_consolidation`** - Scanning UTXOs, building transaction, broadcasting
   - During this state, all new transfer requests are rejected
3. **`consolidating_pending`** - Transaction broadcasted, waiting for 1 confirmation
   - Polls blockchain status continuously
   - Timeout: 15 minutes
4. **`consolidating_timeout`** - Transaction not confirmed within 15 minutes
   - User can query transaction status (opens block explorer)
   - User can abandon consolidation (returns to `idle`)
   - **No retry button** to prevent double-spending

### Conflict Handling

During consolidation (from command to completion or timeout):
- **Exclusive Execution**: System enters locked state
- **Transfer Rejection**: Any new transfer requests are directly rejected with message: "当前正在执行归集操作，请在归集完成后再发起新交易。"

## Usage

### Command Line

```bash
npx ts-node scripts/consolidate.ts <agentName> <mainAddress>
```

**Parameters:**
- `agentName`: The name of the metabot-basic performing consolidation
- `mainAddress`: The main address to consolidate UTXOs to (optional, defaults to agent's primary address)

**Example:**
```bash
npx ts-node scripts/consolidate.ts "AI Sunny" "1KUmqn2noktJbdW4rPfHJdefCZmV8QzyiH"
```

### Programmatic Usage

```typescript
import { consolidateUtxos } from './scripts/consolidate'

const result = await consolidateUtxos(
  mnemonic,
  mainAddress,
  address,
  path
)
```

## Implementation Details

### Constants

All magic numbers are defined as constants at the top of the file:

```typescript
// TODO: extract to config
const DUST_THRESHOLD = 546 // satoshis
const BALANCE_TOLERANCE = 0.1 // ±10%
const CONFIRMATION_REQUIRED = 1 // blocks
const TIMEOUT_MINUTES = 15 // minutes
```

### Reading Operations
- Queries UTXOs using `fetchMVCUtxos` from `api.ts`
- Filters out dust UTXOs (< 546 satoshis)
- Validates balance tolerance before building transaction

### Writing Operations
- Builds consolidation transaction using `createPin` method
- Sends all UTXOs to main address
- Broadcasts transaction to MVC network
- Monitors transaction confirmation status

### State Management
- Tracks consolidation state in memory (can be persisted to file/database in future)
- Prevents concurrent operations during consolidation
- Handles timeout and error scenarios

## Error Handling

The skill handles:
- Insufficient balance for transaction fees
- Balance tolerance exceeded
- Transaction broadcast failures
- Confirmation timeout
- Network errors
- Invalid agent account
- Concurrent transfer requests during consolidation

## Error Messages

- **Transfer Rejection**: "当前正在执行归集操作，请在归集完成后再发起新交易。"
- **Timeout**: "归集交易已广播但尚未确认（超时15分钟），您可以继续等待或放弃本次归集。"
- **Broadcast Failure**: "归集交易广播失败，请稍后重试。"

## Dependencies

This skill uses scripts from metabot-basic:
- `metaid.ts` - For creating transactions and broadcasting to chain
- `wallet.ts` - For wallet operations and address derivation
- `api.ts` - For querying UTXOs and transaction status
- `utils.ts` - For account management
- `doge.ts` - For DOGE chain operations (if needed)

## Examples

### Basic Consolidation
```bash
# Consolidate UTXOs for AI Sunny
npx ts-node scripts/consolidate.ts "AI Sunny"
```

### Consolidate to Specific Address
```bash
# Consolidate to a specific main address
npx ts-node scripts/consolidate.ts "AI Sunny" "1KUmqn2noktJbdW4rPfHJdefCZmV8QzyiH"
```

## Version History

- **V1.0** (2026-02-11): Initial MVP version
  - Basic consolidation functionality
  - State machine implementation
  - Dust filtering and balance tolerance
  - Conflict handling during consolidation
