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 SEP-41 token transfers, TokenClient::new, transfer/transfer_from/burn, XLM native balance - Inject Into Lifecycle, External-Env agents

How to use this skill

Skill file

TOKEN_FLOW_TRACING Skill (Soroban)

Trigger Pattern: SEP-41 transfer/transfer_from/approve/burn, TokenClient::new, XLM native balance, SAC interactions

Inject Into: Lifecycle, External-Env agents

Finding prefix: [TF-N]

Rules referenced: R4, R5, R10, R11

For every token the protocol handles:

1. Token Entry Points

Where can tokens enter?

Soroban-specific note: Unlike Solana, there are no token account PDAs. The contract address IS the recipient. Unsolicited transfers arrive directly to e.current_contract_address() and are undetectable without an explicit balance snapshot before and after calls.

2. Token State Tracking

For each entry point:

Red flags:

3. Token Exit Points

Where can tokens leave?

For each exit:

3b. Self-Transfer Accounting

For each transfer instruction: can the source and destination be the same address?

If YES: does a self-transfer update accounting state (fees credited, rewards claimed, share ratios updated) without net token movement? Flag as FINDING. This targets accounting manipulation, distinct from input validation.

4. Token Type Separation (Multi-Token Protocols)

For protocols handling multiple token types:

Check: If function A handles TokenX and function B handles TokenY, can TokenX's address be passed to function B?

5. Unsolicited Transfer Analysis

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

If YES (always YES in Soroban — any SEP-41 holder can call token.transfer(self, contract_addr, amount)):

If the protocol claims NO:

5b. Unsolicited Transfer Matrix (All Token Types)

For EVERY token type the protocol holds, queries, or receives:

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

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

| XLM (native) | YES (always) | YES/NO | YES/NO | YES/NO |

| {sep41_token_a} | YES (always) | YES/NO | YES/NO | YES/NO |

| SAC-{asset} | YES (always) | 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 | balance() Used Directly? | Unsolicited Possible? |

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

| [Name/Address] | deposit, cross-contract return | withdraw, claim | total_deposited | YES/NO | YES (always) |

7. Cross-Token Interactions

For protocols with multiple tokens:

8. Cross-Contract Call Return Verification

For every invoke_contract call that returns tokens or modifies state:

8a. Contract Address Verification

Common mismatches:

Check: Every TokenClient::new(&e, &token_address) — is token_address validated against the configured/expected token, or accepted from user-supplied input?

8b. Return Value / Post-Call State Validation

Soroban reentrancy note: Soroban DOES allow reentrant invoke_contract calls unless the contract explicitly guards against them. If a cross-contract call can call back into this contract before the current function completes, check for reentrancy vectors.

9. Allowance Expiry Analysis (Soroban-Specific)

Soroban SEP-41 allowances are stored in Temporary ledger storage with a TTL (expressed as a ledger number deadline, not an amount-only approval like EVM). This creates unique staleness vectors:

9a. Allowance Storage Type

9b. Allowance Expiry Attack Scenarios

| Scenario | Description | Impact |

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

| Expired allowance | Contract holds an approved allowance; TTL expires before it is consumed; subsequent transfer_from fails | DoS: operation reverts, user funds locked pending reapproval |

| Short TTL front-run | User approves with short TTL; attacker delays their own transaction until allowance expires; then calls function that relies on the allowance | Griefing: operation fails after attacker delays it |

| Allowance amount != i128 | Approved amount stored as i64 in older code; overflow at amounts > 2^63 | Accounting mismatch: partial approval silently truncated |

9c. Token Side Effects (SAC-specific)

9d. Side Effect Token Type Analysis

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

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

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

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

Example Application

// RED FLAG: Direct balance usage — donatable
let rate = token_client.balance(&e.current_contract_address()) / vault.total_shares;

// BETTER: Tracked balance — but verify total_deposited is updated on ALL entry paths
let rate = vault.total_deposited / vault.total_shares;

// RED FLAG: Token address from user input — not validated
let token_client = TokenClient::new(&e, &token_address); // token_address from fn args
token_client.transfer_from(&e.current_contract_address(), &from, &to, &amount);

// BETTER: Validated against configured token
let configured_token: Address = e.storage().instance().get(&DataKey::Token).unwrap();
require!(token_address == configured_token, Error::InvalidToken);

Finding Template

**ID**: [TF-N]
**Severity**: [based on fund impact]
**Step Execution**: S1,2,3,4,5,6,7,8,9 | X(reasons) | ?(uncertain)
**Location**: src/{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: 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. Cross-Contract Call Return Verification | YES | Y/X/? | MANDATORY — never skip |

| 9. Allowance Expiry Analysis | YES | Y/X/? | MANDATORY — Soroban-specific, 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.