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

Trigger Pattern: BALANCE_DEPENDENT flag (required)

Inject Into: Depth-token-flow, breadth agents

Purpose: Trace all token flows through Aptos Move protocols using FungibleAsset and Coin<T> models, identifying accounting desync, unsolicited deposit vectors, type confusion, and dispatchable hook side effects

For every token the protocol handles:

1. Asset Inventory

Enumerate ALL asset types the protocol handles:

| Asset | Model | Type Parameter / Metadata | Decimals | Entry Modules | Exit Modules |

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

| {name} | Coin<T> / FungibleAsset | {CoinType or metadata Object} | {decimals} | {list} | {list} |

Aptos dual token model:

2. Token Entry Points

Where can tokens enter the protocol?

| Entry Path | Function | Asset Model | Accounting Updated? | Access Control |

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

| Standard deposit | {deposit_fn} | {Coin/FA} | YES/NO | {who can call} |

| primary_fungible_store::deposit() | External | FA | NO (protocol unaware) | Permissionless |

| coin::deposit<T>() | External | Coin<T> | NO (protocol unaware) | Permissionless (if CoinStore registered) |

| Direct fungible_asset::deposit() | Via store ref | FA | NO (protocol unaware) | Requires FungibleStore reference |

| move_to<T>() | Internal | Resource | {depends} | Module only |

| Side-effect receipts | External call returns | {varies} | {depends} | {depends} |

Red flags:

3. Token Exit Points

Where can tokens leave the protocol?

| Exit Path | Function | Asset Model | Accounting Updated? | Access Control |

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

| Standard withdraw | {withdraw_fn} | {Coin/FA} | YES/NO | {who can call} |

| primary_fungible_store::withdraw() | Via signer | FA | {depends} | Requires signer capability |

| coin::withdraw<T>() | Via signer | Coin<T> | {depends} | Requires signer |

| fungible_asset::withdraw() | Via store ref | FA | {depends} | Requires store &mut ref or TransferRef |

| Fee distribution | {fee_fn} | {varies} | YES/NO | {access} |

| Emergency withdraw | {emergency_fn} | {varies} | YES/NO | {admin} |

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

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

3b. Self-Transfer Accounting

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

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 Analysis

For each asset type:

| Asset | Internal Tracking Variable | On-Chain Balance Query | Can Desync? | Desync Vector |

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

| {name} | {e.g., total_deposited in resource} | fungible_asset::balance(store) or coin::balance<T>(addr) | YES/NO | {if YES: how} |

Critical question: Does the protocol use internal accounting or direct on-chain balance queries?

Red flags:

5. Unsolicited Deposit Analysis

Can tokens be deposited to the protocol without calling its deposit function?

If YES (most cases on Aptos):

5a. Unsolicited Deposit Vectors

| Vector | Asset Model | Protocol Aware? | Impact |

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

| primary_fungible_store::deposit(protocol_addr, fa) | FA | NO | {impact} |

| coin::deposit<T>(protocol_addr, coin) | Coin<T> | NO | {impact} |

| Direct transfer to object-owned store | FA | NO | {impact} |

5b. Unsolicited Transfer Matrix (All Token Types) -- R11 Five Dimensions

For EVERY external token type the protocol holds, queries, or receives as side effects:

| Token Type | Can Deposit Unsolicited? | Accounting Distortion? | Share Inflation? | Threshold Manipulation? | Reward Dilution? | Fee Calculation Impact? |

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

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

RULE: If ANY token type is unsolicited-depositable AND affects state -> analyze each consequence:

If NO:

6. Token Type Confusion

Can the wrong asset type be used where another is expected?

| Check | Location | Status | Impact |

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

| FungibleAsset metadata validated on deposit? | {fn} | YES/NO | Wrong FA type accepted |

| Coin<T> type parameter constrains to expected type? | {fn} | YES/NO (compile-time) | N/A for Coin (type-safe) |

| Paired Coin/FA confusion? | {fn} | YES/NO | Same asset counted twice |

| Metadata address hardcoded or validated? | {fn} | {which} | Spoofed metadata |

Aptos-specific type confusion vectors:

7. Dispatchable Hook Impact

If any token in scope uses the Aptos FungibleAsset dispatchable hooks (DispatchFunctionStore):

| Token | Hook Type | Hook Function | Side Effect | Protocol Handles? |

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

| {token} | deposit | {module::fn} | {effect} | YES/NO |

| {token} | withdraw | {module::fn} | {effect} | YES/NO |

| {token} | derived_balance | {module::fn} | {effect} | YES/NO |

Analysis questions:

8. Zero-Value Operations

What happens with zero-amount deposits/withdrawals?

| Operation | Zero Amount Behavior | Accounting Impact | Shares Issued/Burned? |

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

| deposit(0) | {aborts/succeeds} | {state change?} | {0 shares / abort?} |

| withdraw(0) | {aborts/succeeds} | {state change?} | {0 shares burned?} |

| transfer(0) | {aborts/succeeds} | {state change?} | N/A |

Red flags:

9. Cross-Token Interactions

For protocols handling multiple token types:

| Token A | Token B | Interaction | Can A Affect B? | Impact |

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

| {tokenA} | {tokenB} | {rate dependency / collateral relationship / swap} | YES/NO | {impact} |

10. Token Flow Checklist

For each token identified:

| Token | Entry Points | Exit Points | Tracking Var | Direct Balance Query Used? | Unsolicited Possible? |

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

| {name} | {list} | {list} | {var} | YES/NO | YES/NO |

Instantiation Parameters

{CONTRACTS}           -- Move modules to analyze
{ASSET_TYPES}         -- FungibleAsset metadata and Coin<T> types in scope
{ENTRY_FUNCTIONS}     -- Functions where tokens enter the protocol
{EXIT_FUNCTIONS}      -- Functions where tokens leave the protocol
{BALANCE_VARS}        -- Internal balance tracking variables
{EXTERNAL_TOKENS}     -- External token types the protocol interacts with

Finding Template

**ID**: [TF-N]
**Severity**: [based on fund impact]
**Step Execution**: checkmark1,2,3,4,5,6,7,8,9,10 | x(reasons) | ?(uncertain)
**Rules Applied**: [R1:Y, R4:Y, R10:Y, R11:Y]
**Location**: module::function:LineN
**Title**: [Asset 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]

Output Schema

| Field | Required | Description |

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

| asset_inventory | yes | All asset types and their models |

| entry_points | yes | Token entry paths with accounting status |

| exit_points | yes | Token exit paths with accounting status |

| balance_tracking | yes | Internal vs on-chain balance analysis |

| unsolicited_vectors | yes | Unsolicited deposit analysis (R11 5 dimensions) |

| type_confusion | yes | Token type validation issues |

| 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 | Y/x/? | |

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

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

| 4. Balance Tracking Analysis | YES | Y/x/? | |

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

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

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

| 7. Dispatchable Hook Impact | IF dispatchable tokens | Y/x(N/A)/? | |

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

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

| 10. Token Flow Checklist | YES | Y/x/? | |

Cross-Reference Markers

After Section 5 (Unsolicited Deposit Analysis):

After Section 6 (Token Type Confusion):

After Section 7 (Dispatchable Hook Impact):

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.