Token flow tracing

From PlamenTSV/plamen · View interactive version

Token flow tracing is an AI agent skill from the GitHub repository PlamenTSV/plamen. Trigger Pattern BALANCE_DEPENDENT flag (required) - Inject Into Depth-token-flow, breadth agents

How to use this skill

Skill file

TOKEN_FLOW_TRACING Skill (Sui)

Trigger Pattern: BALANCE_DEPENDENT flag (required)

Inject Into: Depth-token-flow, breadth agents

Purpose: Trace all Coin<T> and Balance<T> flows through Sui Move protocols to identify accounting desync, unsolicited deposit vectors, type confusion, and token lifecycle issues

For every token the protocol handles:

1. Asset Inventory

Enumerate ALL Coin<T> and Balance<T> types the protocol handles:

| Token Type | Representation | Location (module::struct field) | Owned or Shared? | Entry Functions | Exit Functions |

|------------|---------------|-------------------------------|-------------------|-----------------|----------------|

| {e.g., SUI} | Balance<SUI> | pool::Pool.balance | Shared object | deposit() | withdraw() |

| {e.g., USDC} | Coin<USDC> | (function parameter) | Owned (user) | swap_in() | swap_out() |

Sui-specific representations:

2. Token Entry Points

Where can tokens enter the protocol?

| Entry Path | Function | Token Form | Accounting Update | Validated? |

|------------|----------|-----------|-------------------|------------|

| Standard deposit | {module::deposit} | Coin<T> parameter | {state variable updated} | YES/NO |

| PTB coin splitting | (PTB splits user coin) | Coin<T> from split | {same as above?} | N/A |

| Direct transfer | transfer::public_transfer | Coin<T> to address | NONE -- creates new owned object | NO |

| Balance merge | balance::join | Balance<T> | {state variable updated?} | YES/NO |

| Mint | coin::from_balance / coin::mint | TreasuryCap | {supply tracking} | YES/NO |

| Side-effect receipt | {external call returns coin} | Coin<T> | {handled?} | YES/NO |

Sui-specific entry analysis:

3. Token Exit Points

Where can tokens leave the protocol?

| Exit Path | Function | Token Form | Accounting Update | Authorized? |

|-----------|----------|-----------|-------------------|-------------|

| Standard withdraw | {module::withdraw} | Coin<T> returned | {state variable decremented} | {access check} |

| Transfer out | transfer::public_transfer | Coin<T> | {accounting updated?} | {access check} |

| Balance extraction | balance::split | Balance<T> | {state variable decremented?} | {access check} |

| Burn | coin::burn / balance::decrease_supply | TreasuryCap | {supply tracking} | {cap holder} |

| Fee distribution | {fee function} | Coin<T> or Balance<T> | {fee accounting} | {access check} |

| Emergency withdraw | {emergency function} | Coin<T> | {does it clear ALL state?} | {admin cap?} |

For each exit: does the tracked balance decrease BEFORE or AFTER the actual balance extraction?

For each transfer/withdrawal: can the source be underfunded at execution time? (funds deployed externally, locked, or lent out → transfer aborts)

Check for:

3b. Self-Transfer Accounting

For each transfer function: can the sender and recipient be the same address/object?

If YES: does a self-transfer update accounting state (fees credited, rewards claimed, snapshots updated, share ratios changed) without net token movement? Flag as FINDING.

4. Balance Tracking and Desync Analysis

For each token in the protocol:

| Token | Internal Tracking Variable | Actual Balance Source | Can They Desync? | Desync Vector |

|-------|---------------------------|---------------------|-----------------|---------------|

| {token} | {e.g., pool.total_deposited} | balance::value(&pool.balance) | YES/NO | {how} |

Red flags:

Sui-specific desync vectors:

5. Unsolicited Deposit Analysis

Can tokens be added to the protocol's balance without going through deposit logic?

Sui object model considerations:

| Donation Path | Possible? | Changes Protocol Balance? | Breaks Accounting? | Impact |

|---------------|-----------|--------------------------|-------------------|--------|

| transfer::public_transfer to pool address | YES (creates owned obj) | NO (not auto-merged) | NO (unless protocol sweeps) | {analysis} |

| Public function accepting Coin<T> without accounting | {YES/NO} | YES | YES | {analysis} |

| Dynamic field injection | {YES/NO -- needs module API} | {YES/NO} | {YES/NO} | {analysis} |

| Reward/fee distribution to protocol address | {YES/NO} | {YES/NO} | {YES/NO} | {analysis} |

5b. Unsolicited Transfer Matrix (All Token Types)

For EVERY external token type the protocol holds, queries, or receives as side effects -- not just the protocol's primary token:

| Token Type | Can Be Sent to Protocol? | Changes Protocol Accounting? | Blocks Operations? | Triggers Side Effects? |

|------------|--------------------------|-----------------------------|--------------------|----------------------|

| {token_a} | YES/NO | YES/NO | YES/NO | YES/NO |

RULE: If ANY token type can enter the protocol's balance AND affects state -> analyze each consequence:

6. Token Type Confusion

Can the wrong Coin<T> type be passed to protocol functions?

| Function | Expected Type Parameter | Validated? | What if Wrong Type? |

|----------|------------------------|-----------|---------------------|

| {function} | Coin<USDC> | {by Move type system / runtime check} | {impact} |

Sui Move type safety: Move's type system provides strong static guarantees -- Coin<USDC> and Coin<SUI> are different types at compile time. However:

7. Coin Splitting and Merging

Analyze coin::split() and coin::join() / balance::split() and balance::join() operations:

| Operation | Location | Amount Source | Validated? | Edge Cases |

|-----------|----------|-------------|-----------|------------|

| coin::split(&mut coin, amount, ctx) | {location} | {user input / computed} | {amount <= coin.value?} | {amount = 0? amount = full value?} |

| coin::join(&mut coin_a, coin_b) | {location} | {coin_b.value} | {overflow check?} | {coin_b is zero?} |

| balance::split(&mut bal, amount) | {location} | {computed} | {amount <= bal.value?} | {amount = 0?} |

| balance::join(&mut bal_a, bal_b) | {location} | {bal_b.value} | {overflow check?} | {bal_b is zero?} |

Check:

8. Zero-Value Operations

What happens with zero-value tokens?

| Operation | Zero Input Behavior | Impact |

|-----------|-------------------|--------|

| deposit(coin::zero<T>()) | {aborts / succeeds / mints zero shares} | {accounting impact} |

| withdraw(0) | {aborts / succeeds / burns zero shares} | {accounting impact} |

| swap(coin::zero<T>()) | {aborts / succeeds} | {state change without value?} |

| claim_rewards() when rewards = 0 | {aborts / succeeds} | {side effects?} |

Check: Can zero-value operations be used to:

9. Cross-Token Interactions

For protocols with multiple token types:

| Interaction | Token A | Token B | Dependency | Impact |

|-------------|---------|---------|-----------|--------|

| Exchange rate | {A type} | {B type} | {A balance affects B's rate?} | {if A manipulated, B price changes?} |

| Collateral/debt | {collateral type} | {debt type} | {collateral value gates borrowing} | {if collateral inflated, excess borrowing} |

| LP composition | {A type} | {B type} | {ratio determines share value} | {imbalance vector} |

Finding Template

**ID**: [TF-N]
**Severity**: [based on fund impact]
**Step Execution**: check1,2,3,4,5,6,7,8,9 | x(reasons) | ?(uncertain)
**Rules Applied**: [R1:check, R11:check, R4:check, R10:check]
**Location**: module::function:LineN
**Title**: [Token type] can enter/exit via [path] without [expected accounting update]
**Description**: [Trace the token flow and where it diverges from expected]
**Impact**: [What breaks: exchange rates, user balances, protocol insolvency]

Instantiation Parameters

{CONTRACTS}           -- Move modules to analyze
{TOKEN_TYPES}         -- Coin<T>/Balance<T> types handled
{SHARED_OBJECTS}      -- Shared objects holding balances
{ENTRY_FUNCTIONS}     -- Token deposit/entry functions
{EXIT_FUNCTIONS}      -- Token withdraw/exit functions
{GENERIC_FUNCTIONS}   -- Functions with type parameter <T>

Output Schema

| Field | Required | Description |

|-------|----------|-------------|

| asset_inventory | yes | All Coin<T> and Balance<T> types |

| entry_points | yes | All token entry paths |

| exit_points | yes | All token exit paths |

| balance_tracking | yes | Internal vs actual balance analysis |

| unsolicited_analysis | yes | Donation/unsolicited deposit vectors |

| type_confusion | yes | Type parameter validation |

| finding | yes | CONFIRMED / REFUTED / CONTESTED |

| evidence | yes | Code locations with line numbers |

| step_execution | yes | Status for each step |


Step Execution Checklist (MANDATORY)

| Section | Required | Completed? | Notes |

|---------|----------|------------|-------|

| 1. Asset Inventory | YES | check/x/? | |

| 2. Token Entry Points | YES | check/x/? | |

| 3. Token Exit Points | YES | check/x/? | |

| 4. Balance Tracking and Desync | YES | check/x/? | |

| 5. Unsolicited Deposit Analysis | YES | check/x/? | |

| 5b. Unsolicited Transfer Matrix (All Types) | YES | check/x/? | MANDATORY -- never skip |

| 6. Token Type Confusion | YES | check/x/? | |

| 7. Coin Splitting and Merging | YES | check/x/? | |

| 8. Zero-Value Operations | YES | check/x/? | |

| 9. Cross-Token Interactions | IF multi-token | check/x(N/A)/? | |

Cross-Reference Markers

After Section 5 (Unsolicited Deposit Analysis):

After Section 7 (Coin Splitting and Merging):

After Section 8 (Zero-Value Operations):

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.