Skip to content

Conversation

@smypmsa
Copy link
Member

@smypmsa smypmsa commented Oct 26, 2025

Added example of CU consumed by a transaction using setLoadedAccountsDataSizeLimit.

https://www.anza.xyz/blog/cu-optimization-with-setloadedaccountsdatasizelimit

image

Summary by CodeRabbit

  • New Features
    • Added a new learning example demonstrating token purchase workflows on Solana with compute unit optimization and transaction simulation capabilities. The module enables monitoring token creation events, simulating buy transactions to estimate costs, and executing optimized token purchases with explicit compute unit accounting.

@smypmsa smypmsa self-assigned this Oct 26, 2025
@smypmsa smypmsa added the enhancement New feature or request label Oct 26, 2025
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 26, 2025

Caution

Review failed

The pull request is closed.

Walkthrough

A new Solana token-purchase module added with compute unit optimization and simulation. Includes price calculation via bonding curves, transaction building, WebSocket monitoring for token creation, buy simulation without on-chain execution, and optimized token acquisition with CU accounting.

Changes

Cohort / File(s) Summary
New Solana Token Purchase Workflow
learning-examples/manual_buy_cu_optimized.py
Introduces end-to-end tooling for monitoring Solana token creation events, calculating bonding curve prices, simulating token purchases to estimate compute unit consumption, executing optimized buys with optional CU preflight limits, and analyzing transaction metrics. Includes BondingCurveState class, async RPC helpers, transaction builders, WebSocket listeners, PDA derivation utilities, and main orchestration logic.

Sequence Diagram(s)

sequenceDiagram
    participant Main
    participant WebSocket
    participant RPC
    participant Program as On-Chain Program

    Main->>WebSocket: listen_for_create_transaction()
    WebSocket->>WebSocket: Subscribe to block notifications
    activate WebSocket
    Note over WebSocket: Wait for pump program transaction
    WebSocket-->>Main: Decoded token creation data
    deactivate WebSocket

    Main->>RPC: get_pump_curve_state(curve_address)
    RPC->>Program: Fetch account data
    Program-->>RPC: Curve state (reserves, decimals)
    RPC-->>Main: BondingCurveState

    Main->>Main: calculate_pump_curve_price(curve_state)
    Note over Main: Price = virtual_token_reserves /<br/>virtual_sol_reserves

    Main->>Main: simulate_buy(token_amount)
    activate Main
    Note over Main: Build tx with compute budget<br/>Simulate without sending
    Main->>RPC: simulateTransaction()
    RPC->>Program: Execute in simulation
    Program-->>RPC: Result with CU consumed
    RPC-->>Main: Estimated CUs
    deactivate Main

    Main->>Main: buy_token (with CU optimization)
    activate Main
    Note over Main: Insert preflight CU limit<br/>Build buy instruction
    Main->>RPC: sendTransaction()
    RPC->>Program: Execute buy on-chain
    Program-->>RPC: Transaction signature
    RPC-->>Main: CU consumed from details
    deactivate Main

    Main->>Main: Report CU consumption & savings
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

  • Bonding curve price calculation: Verify reserve arithmetic and token decimal handling for correctness
  • Compute unit budget instruction: Validate correct program ID and instruction encoding for CU limits
  • Transaction simulation logic: Ensure simulation message construction matches actual transaction flow
  • PDA derivation functions: Confirm seeds and program IDs match Pump program specifications
  • WebSocket event parsing: Check discriminator validation and instruction decoding against IDL
  • Slippage and token amount computation: Verify decimal scaling and max amount calculations in token purchases

Poem

🐰 A hop through Solana's curves so sweet,
Where compute units and tokens meet!
Simulate, optimize, buy with flair—
CU savings floating through the air! ✨

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feat/cu-opt-example

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 68cac87 and bfcac6c.

📒 Files selected for processing (1)
  • learning-examples/manual_buy_cu_optimized.py (1 hunks)

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@smypmsa smypmsa changed the title feat(example): add pump buy script with compute unit optimization Add pump fun buy example with optimized CUs Oct 26, 2025
@smypmsa smypmsa merged commit 8367611 into main Oct 26, 2025
1 check was pending
@smypmsa smypmsa deleted the feat/cu-opt-example branch October 26, 2025 23:21
@smypmsa
Copy link
Member Author

smypmsa commented Oct 27, 2025

Unfortunately, this method didn't show significant improvements in the CU consumed. The screenshot above is misleading since it shows a decrease due to already existing ATA ... Didn't notice that. I will update this example.

@smypmsa
Copy link
Member Author

smypmsa commented Oct 27, 2025

Except for spamming multiple transactions and checking the results, it seems there is no way of proving the actual impact. But theoretically, it should improve transaction priority. Clarifying comments to the example will be provided in the next few commits.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants