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 SPL token CPI transfers, token_account.amount, invoke/invoke_signed, Transfer/TransferChecked - Inject Into Lifecycle, External-Env agents

How to use this skill

Skill file

TOKEN_FLOW_TRACING Skill (Solana)

Trigger Pattern: SPL token CPI transfers, token_account.amount, invoke/invoke_signed, Transfer/TransferChecked

Inject Into: Lifecycle, External-Env agents

For every token the protocol handles:

1. Token Entry Points

Where can tokens enter?

2. Token State Tracking

For each entry point:

Red flags:

3. Token Exit Points

Where can tokens leave?

For each exit: does the tracked balance decrease BEFORE or AFTER the CPI 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 instruction: can the source and destination token accounts belong to the same owner/authority, or be the same account?

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. Note: this check targets accounting manipulation from self-transfers, distinct from the account key uniqueness validation in ACCOUNT_VALIDATION.md's Self-Transfer Risk column.

4. Token Type Separation (Multi-Token Protocols)

For protocols handling multiple token types:

Check: If instruction A handles MintX and instruction B handles MintY, can MintX accounts be passed to instruction B's logic?

5. Unsolicited Transfer Analysis

Can tokens be sent to the protocol's token accounts without calling deposit()?

If YES (almost always YES on Solana):

If NO:

5b. Unsolicited Transfer Matrix (All Token Types)

For EVERY token type the protocol holds, queries, or receives - including SOL lamports:

| Token Type | Can Transfer To Protocol? | Changes Accounting? | Blocks Operations? | Triggers Side Effects? |

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

| SOL (lamports) | YES (always) | YES/NO | YES/NO | YES/NO |

| {spl_token_a} | YES/NO | YES/NO | YES/NO | YES/NO |

RULE: If ANY transferable token affects state -> analyze: accounting divergence, rent impact, operation blocking, side effect chains.

6. Token Flow Checklist

For each token identified:

| Token | Entry Points | Exit Points | Tracking Var | token_account.amount Used? | Unsolicited Possible? |

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

| [Name/Mint] | deposit, CPI return | withdraw, claim | total_deposited | YES/NO | YES/NO |

7. Cross-Token Interactions

For protocols with multiple tokens:

8. CPI Return Type Verification

For every CPI call that returns tokens or modifies accounts:

8a. CPI Target Verification

Common mismatches:

Check: invoke_signed(&instruction, &[accounts...]) - verify the target program and returned account state match expectations.

8b. Return Value / Account State Validation

9. Transfer Side Effects Analysis

For every CPI transfer to external programs:

9a. On-Transfer Behavior

9b. Side Effect Inventory

| Token / Mint | On Transfer Side Effect | Impact on Protocol |

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

| [Token] | TransferHook executes arbitrary CPI | Potential reentrancy / state corruption |

| [Token] | TransferFee withholds portion | Accounting mismatch (sent != received) |

| [Token] | PermanentDelegate can move tokens | Tokens can leave without protocol consent |

9c. Token-2022 Extension Checks

9d. Side Effect Token Type Analysis

| CPI Call / Event | Side Effect | Token Type Produced | Protocol Handles This Type? | Mismatch? |

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

| {cpi_call} | {side_effect} | {token_type_or_UNKNOWN} | YES/NO | YES/NO |

RULES: Side effect type != expected -> FINDING. Type UNKNOWN -> CONTESTED (Rule 4). Check BOTH CPI calls AND unsolicited transfers.

Example Application

// RED FLAG: Direct balance usage - donatable
let rate = ctx.accounts.vault_token.amount / vault.total_shares;
// BETTER: Tracked balance - but verify total_deposited updated on ALL entry paths
let rate = vault.total_deposited / vault.total_shares;

Finding Template

When this skill identifies an issue:

**ID**: [TF-N]
**Severity**: [based on fund impact]
**Step Execution**: S1,2,3,4,5,6,7,8,9 | X(reasons) | ?(uncertain)
**Location**: program/src/instructions/file.rs: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]

Step Execution Checklist (MANDATORY)

CRITICAL: You MUST report completion status for ALL sections. Findings with incomplete sections will be flagged for depth review.

| Section | Required | Completed? | Notes |

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

| 1. Token Entry Points | YES | Y/X/? | |

| 2. Token State Tracking | YES | Y/X/? | |

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

| 4. Token Type Separation | IF multi-token | Y/X(N/A)/? | |

| 5. Unsolicited Transfer Analysis | YES | Y/X/? | |

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

| 6. Token Flow Checklist | YES | Y/X/? | |

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

| 8. CPI Return Type Verification | YES | Y/X/? | MANDATORY - never skip |

| 9. Transfer Side Effects (Token-2022) | YES | Y/X/? | MANDATORY - never skip |

| 9d. Side Effect Token Type | YES | Y/X/? | MANDATORY - never skip |

Cross-Reference Markers

Mandatory Forced Output

Sections 8 and 9 MUST produce tabular output even if uncertain. If UNVERIFIED: verdict cannot be REFUTED, use CONTESTED. If side effects UNKNOWN: apply adversarial default and document assumptions.

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.