refactor(cookbook): restructure learning-examples, add missing actions, fix quote decimals (#216)

* refactor(tests): move the regression verifiers into tests/regression

verify_*.py are not standalone examples: each imports src/, stubs the RPC
client, and asserts one previously fixed bug stays fixed. Their checks are
unchanged; PROJECT_ROOT, the usage lines and the bonk fixture lookup are
adjusted for the new depth. run_all.py runs the set and reports failures.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* chore(tools): move the dev harness and listener benchmarks into tools/

simulate_*, live_*, both compare_* scripts and the account cleanup import
src/ and several spend real funds — they exercise the bot rather than teach
anything. compare_listeners.py reaches the repo root one level higher than
before; nothing else changes.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* refactor(cookbook): reorganise learning-examples into cookbook/

The directory is a reference collection — one runnable script per action — but
nothing said so: no index, scripts grouped by the transport they happened to
use, fixtures loose at the root.

Now grouped by platform, then by what you are doing:
pumpfun/{listen,read,trade,graduation,decode}, pumpswap, letsbonk, legacy.
Fixtures sit beside the script that reads them, manual_sell.py becomes
sell_token.py, and the legacy create example moves to legacy/ and says why.
pump_v2.py and tx_status.py stay at the cookbook root as the only two shared
helpers; subdirectories add that root to sys.path before importing them.
cookbook/README.md indexes every script and marks the ones that spend funds.

Also fixes create_v2's account list: the five mayhem accounts are mandatory
whatever is_mayhem_mode says, so mint_and_buy_v2.py could never have worked
with mayhem off. 11 accounts fails AnchorError 3005 (AccountNotEnoughKeys) on
sol_vault, 16 succeeds with the argument either way — simulateTransaction
against mainnet. Seven scripts gain a module docstring they were missing.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* feat(cookbook): add buy, create, balance and status examples

  buy_token.py <MINT>        buy_v2 against one coin; --dry-run simulates
  create_token.py            create_v2 on its own
  get_balances.py            SOL and tokens across both token programs
  check_tx_status.py <SIG>   success, reverted, or not visible yet

Buying previously meant manual_buy.py, which waits for the next coin created
anywhere before it trades — there was no way to buy a coin you already know.
Creating one meant bundling a buy with it, and the transaction-status helper
was a library with no entry point.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* refactor(cookbook): move pump_v2 next to the scripts that use it

pump_v2.py holds the buy_v2/sell_v2 account layouts, and only the pump.fun
trade scripts import it — it sat at the cookbook root because tx_status.py
does, not because anything outside pumpfun/trade/ needed it. The five callers
now pick it up as a plain sibling; legacy/ and the layout verifier reach into
that directory. tx_status.py stays at the root, where all four platforms use it.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* refactor(cookbook): name every script protocol_verb_noun_variant

manual_buy.py did not just buy — it waited for the next coin created anywhere,
then bought, so the verb was wrong. Protocol was encoded three different ways:
directory, suffix, or not at all. And three scripts sat under pumpfun/ that have
nothing to do with pump.fun.

Filenames are now <protocol>_<verb>_<noun>[_<variant>].py, so a basename says
which chain, what it does and which instruction version without opening it:

  manual_buy.py            -> pumpfun_snipe_token_blocksubscribe.py
  manual_buy_geyser.py     -> pumpfun_snipe_token_geyser.py
  buy_token.py             -> pumpfun_buy_token_v2.py
  mint_and_buy_v2.py       -> pumpfun_create_and_buy_token_v2.py
  manual_buy_pumpswap.py   -> pumpswap_buy_token.py
  manual_buy_exact_in.py   -> letsbonk_buy_token_exact_in.py
  legacy/mint_and_buy.py   -> legacy/pumpfun_create_and_buy_token_v1.py

The sniper was named for logsSubscribe in an earlier draft; it sends
blockSubscribe, and the name now says so.

Balances, transaction status and the Anchor discriminator calculator are not
pump.fun-specific and move to a new cookbook/solana/. The two non-runnable
helpers take no verb — pump_v2.py becomes pumpfun_instructions_v2.py beside its
callers, tx_status.py becomes solana_transaction_status.py in solana/. Both are
imported under a short alias so the 106 call sites stay readable.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* fix(cookbook): resolve a coin's quote mint before pricing it

quote_units() defaulted to 9 decimals for an unresolved mint, and every trade
script called it before resolve_quote_token_program() warmed the decimals
cache. pump.fun's QuoteControl registry admits mints at 6, 8 and 9 decimals —
79 of the 170 admitted are tokenized equities, 8 decimals for xStocks and 6 for
Backpack Securities — so those coins were sized against the wrong power of ten.

The error compounds rather than cancels: price_per_token() divides by the same
wrong unit, so the price reads low by the factor, the token amount comes out
high by it, and the inflated cap authorises the overspend instead of catching
it. 10x on an xStock pair, 1000x on a Backpack one.

This is not hypothetical. A 75s sample of live curve updates on 2026-09-22
caught 8 stock-paired curves out of 124 — IONQ across 5 of them, plus QUBT,
PLTRx and COPX.

quote_units() now raises for an unresolved mint instead of guessing, the four
trade paths resolve before they price, and pumpfun_read_price.py reads decimals
from chain rather than from a two-entry table. The geyser sniper never resolved
at all: it also passed SPL Token as the quote token program, which fails the
account constraints for any Token-2022 quote, and created no quote ATA.

verify_quote_decimals_resolved.py checks the fail-closed contract, that
resolution costs no extra RPC call, and that no function prices before it
resolves — confirmed to fail when the ordering is put back.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EZ5HdjTadNUad8i6xis3TR

* feat(cookbook): read the quote-mint registry and Token-2022 mint state

Two reads that were unreachable without knowing an undocumented PDA seed.

pumpfun_read_quote_mints.py decodes QuoteControl (PDA ["quote-control"]), the
second quote-mint registry added in the 2026-09-15 upgrade. Reading only
Global.whitelisted_quote_mints under-reports: a coin can be paired with a mint
Global has never heard of. 170 admitted today, 79 of them tokenized equities,
each with the initial virtual quote reserves that set a new curve's opening
price. --stocks narrows to the equities, identified by the Token-2022 extension
set create_v2 requires of them rather than by a name lookup.

solana_read_token2022_mint.py reads the extensions that change what a balance
means: scaled UI amount, pausable, permanent delegate, transfer hook, default
account state. It exists mainly for one trap — the RPC returns both `multiplier`
and `newMultiplier` with an effective timestamp, and once that timestamp passes
the field named `multiplier` is the stale one. On AAPLx it passed on 2026-08-08,
so the obvious field has been wrong for six weeks. This compares against the
clock and says which is in force.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EZ5HdjTadNUad8i6xis3TR

* feat(cookbook): add the exact-quote-in buy

buy_exact_quote_in_v2 was uncovered. It is the mirror of buy_v2 — buy_v2 fixes
the tokens received and caps the spend, this fixes the spend and floors the
tokens — and it takes the same 27 accounts in the same order, so it reuses
build_v2_accounts and differs only in discriminator and arguments.

Worth having because pinning the spend is the natural direction once a coin is
priced in something other than SOL: "spend exactly one AAPLx" is something you
can mean, where a token count is a number derived from a price that moved while
you derived it.

Verified by simulateTransaction against a live COPX-paired coin: the program
executes the full buy and stops at TransferChecked with insufficient funds,
which is as far as a wallet holding none of the quote asset can go.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EZ5HdjTadNUad8i6xis3TR

* feat(cookbook): add the exact-SOL-in buy

buy_exact_sol_in was uncovered. SOL only — it pre-dates non-SOL quote assets and
carries no quote accounts, so the script checks the curve and points at the
exact-quote version for anything else rather than failing obscurely.

Its account list is the reason to have it. The IDL lists 16; the program needs
18, the extras being the bonding-curve-v2 PDA and a buyback fee recipient.
Determined by simulation rather than assumed: 16 accounts fails AnchorError 6062
(BuybackFeeRecipientMissing), 18 simulates clean at 96k CU. The v2 instructions
are complete in the IDL and this family is not, which is worth a newcomer
knowing before they trust the file.

The docstring carries the IDL's own fee arithmetic, since fees come out of the
amount you name rather than on top of it, and the printed estimate ignores that
split while min_tokens_out is what the program enforces.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EZ5HdjTadNUad8i6xis3TR

* feat(cookbook): add the creator-fee and cashback payouts

Two payout paths that had no script. Both take no arguments and, per the IDL,
no signer — the funds can only reach the wallet they already belong to, so
either can be run for anyone, which --creator and --user expose.

collect_creator_fee_v2 sweeps a creator vault. The vault is per creator and per
quote asset, so a creator whose coins are priced in several assets collects each
separately; --quote picks which.

claim_cashback_v2 pays out an accrued cashback balance. create_v2 has rejected
new cashback coins since 2026-09-15 (error 6082), but coins minted before that
keep accruing and stay claimable, which is why the cashback handling elsewhere
in this repo is not dead code.

Both verified by simulateTransaction against mainnet: 10 accounts each, 8 data
bytes, no error.

Also repairs two cookbook README rows that the earlier rename mangled —
pumpswap_buy_token.py had been rewritten to pumpswap_pumpfun_buy_token_v2.py
because its name contains the string that rename matched on.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EZ5HdjTadNUad8i6xis3TR

* refactor(cookbook): take every script's input on the command line

A reference collection only works if you can run a script against your own coin
without editing it. Several carried the coin as a module-level constant instead:

    TOKEN_MINT = Pubkey.from_string(sys.argv[1] if len(sys.argv) > 1 else "...")

which reads as optional but raises at import, so running the script the way its
own usage line described it died on ValueError: Invalid Base58 string before
printing anything. Nine scripts did this. Four more took their amount and
slippage from environment variables that no usage line mentioned.

Every script now builds an ArgumentParser in main(): required values are
positionals, tunables are --options, and the literals live in DEFAULT_* constants
that supply the defaults. --help works on all 32 that take input; the seven
listeners take none and are exempt. The decode scripts keep defaulting to the
fixture beside them, so they still run bare.

Behaviour is unchanged. create_and_buy's mayhem flag defaulted to on, so it takes
--mayhem/--no-mayhem via BooleanOptionalAction rather than a store_true that
would have silently flipped it off.

Two fixes found while threading the values through: the wallet listener read a
global `wallet` from four functions that no longer had one, and find_pool raised
IndexError on a coin that has not graduated instead of saying so.

verify_cookbook_arguments.py enforces the shape — no module-level sys.argv, no
placeholder values, argparse wherever there is input, and every default a usable
value. Confirmed to fail when the old pattern is put back.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EZ5HdjTadNUad8i6xis3TR

* fix(docs): replace three dead links and check the rest

The Anchor discriminator citation in anchor_calculate_discriminator.py pointed
at book.anchor-lang.com/anchor_bts/discriminator.html, which Anchor removed when
it restructured its docs. Two links in the README had also moved:
docs.chainstack.com/quickstart/ and docs.chainstack.com/docs/trader-nodes.

  book.anchor-lang.com/anchor_bts/discriminator.html
    -> www.anchor-lang.com/docs/basics/idl          (has a Discriminators section)
  docs.chainstack.com/quickstart/
    -> docs.chainstack.com/reference/blockchain-apis
  docs.chainstack.com/docs/trader-nodes
    -> docs.chainstack.com/docs/solana-trader-nodes

All 28 real URLs in the repo now return 200, checked by fetching each one.

verify_documentation_links.py keeps them honest: offline it asserts every URL is
https and that none of the four dead endpoints has come back, and --live fetches
each and fails on 4xx/5xx. It skips the unroutable hosts the offline verifiers
point stub clients at, and example.com, which RFC 2606 reserves for exactly the
placeholder metadata URI a test coin is created with.

The discriminator script also still asked you to edit a constant, which the
argument rule had just outlawed, so it takes the name on the command line and
prints the u64, the byte list and the hex. Cross-checks against the constants in
pumpfun_instructions_v2.py: global:buy_v2 gives [184, 23, 238, 97, 103, 197,
211, 61], which is BUY_V2_DISCRIMINATOR.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EZ5HdjTadNUad8i6xis3TR

* fix(cookbook,tools): detect coins via logs so v1 transactions are not skipped

Solana transaction v1 has been live since 2026-09-15 and solders cannot
deserialize one until 0.29. Three scripts detected coins by deserializing the
transaction envelope, so each went blind to v1:

  cookbook/pumpfun/listen/pumpfun_listen_tokens_blocksubscribe.py
      30,080 "skipping a v1 transaction" lines in 150s
  tools/compare_listeners.py
      601 "Failed to process transaction: io error: unexpected end of file"
      in 100s, which its own comment read as a transport difference
  cookbook/pumpfun/trade/pumpfun_snipe_token_blocksubscribe.py
      skipped v1 silently, so a coin created in one was never sniped

All three now route on meta.logMessages, which the RPC has already decoded and
which reads the same for every version. The blocks example keeps the envelope
decode as a genuine fallback for providers that return blocks without logs; the
sniper derives the associated bonding curve from the mint and curve instead of
reading the instruction's account list, verified against
pumpfun_derive_curve_addresses.py on a live coin.

Measured live after the change: the blocks example decoded 88 v0, 6 v1 and 4
legacy creates with zero skips and zero errors, and compare_listeners' block
lane now appears alongside logs and geyser for the same coins.

Upgrading solders was tried first and reverted. 0.29 does decode v1 — 0.26,
0.27.1 and 0.28 all raise "io error: unexpected end of file" on the committed
fixture — but it requires solana>=0.40, which moved TxOpts, MemcmpOpts and
TokenAccountOpts out of solana.rpc.types. 22 files import them and 16 of 19
verifiers failed on the import alone, so that is a migration of its own.

verify_transaction_v1.py gains a check that no cookbook or tools listener
detects by opening the envelope. It found the sniper, which this had missed.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EZ5HdjTadNUad8i6xis3TR

* fix(cookbook): label the transaction version and restore the lost fields

The blocks listener printed "Found CreateEvent in a 0 transaction", which reads
as a count rather than what it is — Solana's transaction version. It now prints
v0, v1 or legacy.

Routing detection through the CreateEvent also dropped two fields the
instruction path used to fill, so every detection showed "Token Standard: N/A"
and no Associated BC. Both come from the event's token_program: the standard is
that program, and the associated bonding curve is an ordinary ATA of the curve
under it. Verified against pumpfun_derive_curve_addresses.py on a live coin —
curve E94izXvHd7dd... derives to He94dArnuprt..., matching what the listener
now prints.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EZ5HdjTadNUad8i6xis3TR

---------

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Anton Sauchyk
2026-09-23 09:34:40 +02:00
committed by GitHub
co-authored by Claude Opus 5
parent f4dca24cdd
commit 41b971147a
84 changed files with 4831 additions and 973 deletions
+495
View File
@@ -0,0 +1,495 @@
"""Verify that a landed-but-reverted transaction is never reported as a success.
`AsyncClient.confirm_transaction` only waits for a signature to land in a block.
A landed transaction can still have reverted, so every trade path has to read
`meta.err` before it prints or returns success. Skipping that check is how issue
#175 happened: buys reverting with `BuybackFeeRecipientMissing` (6062) were
reported as confirmed, and the only way to notice was to inspect the signatures
by hand.
Two layers are checked:
* `tx_status.assert_transaction_succeeded` — the helper the learning examples use
* `SolanaClient.verify_transaction_succeeded` — the check the bot itself runs
Offline stub checks run always. Pass `--live` to additionally replay the three
reverted signatures from issue #175 against mainnet: both layers must fetch them
successfully and reject them on the strength of `meta.err`, not because the fetch
failed.
Usage:
uv run tests/regression/verify_tx_status_checks.py
uv run tests/regression/verify_tx_status_checks.py --live
"""
import argparse
import asyncio
import json
import os
import sys
from pathlib import Path
from typing import Any
from dotenv import load_dotenv
PROJECT_ROOT = Path(__file__).resolve().parents[2]
sys.path.insert(0, str(PROJECT_ROOT / "cookbook" / "solana"))
sys.path.insert(0, str(PROJECT_ROOT / "src"))
import solana_transaction_status as tx_status # noqa: E402
from core.client import SolanaClient # noqa: E402
# Signatures from issue #175: reported as confirmed buys, actually reverted with
# BuybackFeeRecipientMissing (6062). They are permanent mainnet history, so they
# make a stable regression fixture for "landed but failed".
REVERTED_SIGNATURES = (
"2bHRaovWyYNTX3K1KfMbMBSvefuT34ZExL2J2N83DDh9aB3Fynjyae5bcaCdRuQoihHAMHK4PzcRYoqc9hYypWb2",
"2Quu1uNZB7oSFstpKHv8KR1aKweizyvGaoGeXXKFLbMro4aqWykqZoeq7hSrHVZ8xmi6GX9J5nQVC5Ut3VV88S7T",
"4Bu9LrFK7QmUiCuLcjvejedJZRLfpKPZDiZgNqJRmw2EFjAAutmukGTErjzzJuA68emxpPQ8Pztu3NLkYhuvXKsd",
)
class _FakeMeta:
def __init__(self, err: object) -> None:
self.err = err
class _FakeTransaction:
def __init__(self, meta: _FakeMeta | None) -> None:
self.meta = meta
class _FakeValue:
def __init__(self, transaction: _FakeTransaction) -> None:
self.transaction = transaction
class _FakeResponse:
def __init__(self, value: _FakeValue | None) -> None:
self.value = value
class _StubClient:
"""Minimal stand-in for `AsyncClient.get_transaction`."""
def __init__(self, response: _FakeResponse) -> None:
self._response = response
async def get_transaction(self, *_args: Any, **_kwargs: Any) -> _FakeResponse:
return self._response
def _reverted_response() -> _FakeResponse:
# Shape of a real revert: {"InstructionError": [2, {"Custom": 6062}]}
err = {"InstructionError": [2, {"Custom": 6062}]}
return _FakeResponse(_FakeValue(_FakeTransaction(_FakeMeta(err))))
async def check_helper_rejects_revert() -> None:
client = _StubClient(_reverted_response())
try:
await tx_status.assert_transaction_succeeded(client, "SIG")
except RuntimeError as exc:
assert "6062" in str(exc), f"error should name the program error: {exc}"
return
raise AssertionError("assert_transaction_succeeded accepted a reverted transaction")
async def check_helper_rejects_missing() -> None:
client = _StubClient(_FakeResponse(None))
try:
await tx_status.assert_transaction_succeeded(client, "SIG")
except RuntimeError:
return
raise AssertionError("assert_transaction_succeeded accepted a missing transaction")
async def check_helper_accepts_success() -> None:
client = _StubClient(_FakeResponse(_FakeValue(_FakeTransaction(_FakeMeta(None)))))
await tx_status.assert_transaction_succeeded(client, "SIG")
async def check_confirm_wrapper_rejects_revert() -> None:
"""`confirm_and_assert` must surface a revert, not just a landing."""
confirmed: list[str] = []
class _ConfirmStub(_StubClient):
async def confirm_transaction(self, signature: str, **_kwargs: Any) -> None:
confirmed.append(signature)
client = _ConfirmStub(_reverted_response())
try:
await tx_status.confirm_and_assert(client, "SIG")
except RuntimeError:
assert confirmed == ["SIG"], "confirm_transaction should still be awaited"
return
raise AssertionError("confirm_and_assert accepted a reverted transaction")
async def check_examples_call_a_status_check() -> None:
"""Every example that confirms a trade must also verify it succeeded.
Guards against a new example (or an edit to an existing one) reintroducing a
bare `confirm_transaction` that prints success unconditionally.
"""
# Both roots hold scripts that submit transactions: cookbook/ teaches the
# trade, tools/ exercises it.
scan_dirs = (PROJECT_ROOT / "cookbook", PROJECT_ROOT / "tools")
# Files that confirm transactions without calling the helper, each for a
# reason. Adding an entry here is a deliberate act; forgetting the check in a
# new example is not.
exempt = {
# defines the helper
"solana_transaction_status.py",
# stubs confirm_transaction out; never sends a transaction
"simulate_bot_buy_path.py",
# uses the bot's SolanaClient wrapper, which folds meta.err into its
# return value; the boolean is read at the call site
"cleanup_accounts.py",
}
offenders = []
for path in sorted(p for d in scan_dirs for p in d.rglob("*.py")):
if path.name in exempt or "__pycache__" in path.parts:
continue
source = path.read_text()
if "confirm_transaction" not in source:
continue
if not (
"assert_transaction_succeeded" in source or "confirm_and_assert" in source
):
offenders.append(str(path.relative_to(PROJECT_ROOT)))
assert not offenders, "examples confirm without checking meta.err: " + ", ".join(
offenders
)
async def check_helper_rejects_missing_meta() -> None:
"""Absent execution metadata must fail closed, not read as success.
`meta=None` means the outcome is unknown. Folding that into "no error" is the
exact mistake this module exists to prevent.
"""
client = _StubClient(_FakeResponse(_FakeValue(_FakeTransaction(None))))
try:
await tx_status.assert_transaction_succeeded(client, "SIG")
except RuntimeError as exc:
assert "metadata" in str(exc), f"unclear reason: {exc}"
return
raise AssertionError("a transaction with no execution metadata was accepted")
async def check_revert_is_a_distinct_terminal_error() -> None:
"""A landed revert must be distinguishable from a transient failure.
The retry loops in the manual buy/sell examples resubmit identical signed
bytes, so a revert can never be repaired by retrying — they need to tell it
apart from a fetch error.
"""
client = _StubClient(_reverted_response())
try:
await tx_status.assert_transaction_succeeded(client, "SIG")
except tx_status.TransactionRevertedError:
pass
else:
raise AssertionError("revert did not raise TransactionRevertedError")
assert issubclass(tx_status.TransactionRevertedError, RuntimeError), (
"TransactionRevertedError must stay a RuntimeError so existing "
"except RuntimeError handlers keep working"
)
# not-found is transient, so it must NOT be the terminal type
missing = _StubClient(_FakeResponse(None))
try:
await tx_status.assert_transaction_succeeded(missing, "SIG")
except tx_status.TransactionRevertedError:
raise AssertionError("not-found was reported as a terminal revert") from None
except RuntimeError:
pass
async def check_commitment_is_propagated() -> None:
"""The status read must use the commitment the caller asked for.
Confirming at "finalized" but reading status at "confirmed" reports success
before the finalization the caller requested.
"""
seen: dict[str, Any] = {}
class _Recorder(_StubClient):
async def get_transaction(self, *_args: Any, **kwargs: Any) -> _FakeResponse:
seen["commitment"] = kwargs.get("commitment")
return self._response
async def confirm_transaction(self, *_args: Any, **_kwargs: Any) -> None:
return None
client = _Recorder(_FakeResponse(_FakeValue(_FakeTransaction(_FakeMeta(None)))))
await tx_status.confirm_and_assert(client, "SIG", commitment="finalized")
assert seen["commitment"] == "finalized", (
f"status read at {seen['commitment']!r}, not the requested 'finalized'"
)
async def check_endpoint_logging_hides_userinfo() -> None:
"""Endpoint logging must use hostname, not netloc.
netloc keeps any `user:pass@` userinfo, so redacting with it still prints the
credential for providers that put the key there.
"""
offenders = []
for path in sorted((PROJECT_ROOT / "cookbook").rglob("*.py")):
if "__pycache__" in path.parts or path.name == Path(__file__).name:
continue
for n, line in enumerate(path.read_text().splitlines(), 1):
if "urlsplit" in line and ".netloc" in line:
offenders.append(f"{path.relative_to(PROJECT_ROOT)}:{n}")
assert not offenders, "urlsplit(...).netloc leaks userinfo at: " + ", ".join(
offenders
)
async def check_bot_reads_the_confirmation_result() -> None:
"""No caller in `src/` may throw away `confirm_transaction`'s boolean.
Inside `src/` the only client is `SolanaClient`, whose return value carries
the `meta.err` verdict. Calling it as a bare statement discards that verdict
and whatever gets logged next is unconditional — which is how cleanup
reported "Closed successfully" for reverted close transactions.
"""
import ast
offenders = []
for path in sorted((PROJECT_ROOT / "src").rglob("*.py")):
if "__pycache__" in path.parts:
continue
# client.py holds the wrapper itself; its inner call is solana-py's,
# which signals failure by raising rather than by returning False.
if path.name == "client.py" and path.parent.name == "core":
continue
for node in ast.walk(ast.parse(path.read_text())):
if not isinstance(node, ast.Expr):
continue
call = node.value.value if isinstance(node.value, ast.Await) else node.value
if (
isinstance(call, ast.Call)
and getattr(call.func, "attr", None) == "confirm_transaction"
):
offenders.append(f"{path.relative_to(PROJECT_ROOT)}:{node.lineno}")
assert not offenders, "confirm_transaction result discarded at: " + ", ".join(
offenders
)
async def check_client_accepts_both_signature_types() -> None:
"""`SolanaClient` must handle a base58 string and a `Signature` alike.
The RPC client rejects a string and `json.dumps` rejects a `Signature`, so
whichever form a caller has, one of the two layers used to break. Both
failures surfaced as "not confirmed" for a transaction that in fact landed.
"""
from solders.signature import Signature
sig_str = REVERTED_SIGNATURES[0]
sig_obj = Signature.from_string(sig_str)
client = SolanaClient("http://127.0.0.1:1") # never contacted
bodies: list[dict[str, Any]] = []
async def capture_rpc(body: dict[str, Any], **_kwargs: Any) -> None:
bodies.append(body)
client.post_rpc = capture_rpc
for form in (sig_str, sig_obj):
bodies.clear()
# budget_seconds=0 keeps this to a single call: the capture stub always
# answers "not visible", which the retry would otherwise chase until
# its budget ran out. What is under test here is the serialization.
await client._get_transaction_result(form, budget_seconds=0) # noqa: SLF001
assert len(bodies) == 1, f"no RPC issued for {type(form).__name__}"
param = bodies[0]["params"][0]
assert param == sig_str, f"signature not normalized: {param!r}"
# aiohttp serializes the body with json.dumps; a Signature would raise.
json.dumps(bodies[0])
# Malformed strings must be reported, not raised through.
assert not await client.confirm_transaction("not-a-signature")
await client.close()
async def check_rpc_timeouts_are_retried_not_raised() -> None:
"""An RPC timeout must be retried and reported, never raised at the caller.
aiohttp signals a request timeout with `asyncio.TimeoutError`, which is not
an `aiohttp.ClientError`. While `post_rpc` caught only the latter, every
timeout escaped un-retried — and `str()` on it is empty, so callers logged a
blank reason. This is what broke three of the four listeners in
live_listener_matrix mid-run.
"""
client = SolanaClient("http://127.0.0.1:1")
attempts = 0
class _TimingOutSession:
def post(self, *_args: Any, **_kwargs: Any) -> Any:
nonlocal attempts
attempts += 1
class _Ctx:
async def __aenter__(_self) -> Any:
raise TimeoutError
async def __aexit__(_self, *_exc: Any) -> bool:
return False
return _Ctx()
async def _session() -> Any:
return _TimingOutSession()
client._get_session = _session # noqa: SLF001
client._rate_limiter.acquire = lambda: asyncio.sleep(0) # noqa: SLF001
result = await client.post_rpc({"method": "getTransaction"}, max_retries=2)
await client.close()
assert result is None, f"expected None on repeated timeout, got {result!r}"
assert attempts == 2, f"timeout should be retried; saw {attempts} attempt(s)"
async def check_versioned_transactions_are_requested() -> None:
"""getTransaction must opt in to versioned transactions.
Without `maxSupportedTransactionVersion` the RPC answers -32015 for every v0/v1
transaction, so `meta.err` is unreadable and a successful trade reads back as
unconfirmed. The bot currently sends legacy transactions, which is the only
reason this was survivable.
"""
client = SolanaClient("http://127.0.0.1:1") # never contacted
bodies: list[dict[str, Any]] = []
async def capture_rpc(body: dict[str, Any], **_kwargs: Any) -> None:
bodies.append(body)
client.post_rpc = capture_rpc
await client._get_transaction_result(REVERTED_SIGNATURES[0]) # noqa: SLF001
await client.close()
assert bodies, "no RPC issued"
config = bodies[0]["params"][1]
assert config.get("maxSupportedTransactionVersion") == 1, (
f"getTransaction omits maxSupportedTransactionVersion: {config}"
)
async def check_live_signatures() -> None:
load_dotenv()
rpc_endpoint = os.environ.get("SOLANA_NODE_RPC_ENDPOINT")
if not rpc_endpoint:
raise RuntimeError("SOLANA_NODE_RPC_ENDPOINT is required for --live")
from solana.rpc.async_api import AsyncClient
from solders.signature import Signature
bot_client = SolanaClient(rpc_endpoint)
raw_client = AsyncClient(rpc_endpoint)
try:
for signature in REVERTED_SIGNATURES:
sig = Signature.from_string(signature)
try:
await tx_status.assert_transaction_succeeded(raw_client, sig)
except RuntimeError as exc:
print(f" examples helper rejected {signature[:16]}...: {exc}")
else:
raise AssertionError(f"examples helper accepted reverted {signature}")
# The transaction must be readable at all — a rejection because the
# fetch failed would pass the assertion below for the wrong reason,
# which is how the missing maxSupportedTransactionVersion hid.
fetched = await bot_client._get_transaction_result(signature) # noqa: SLF001
assert fetched, f"SolanaClient could not fetch {signature}"
assert fetched["meta"]["err"], f"expected a revert on {signature}"
# verify_transaction_succeeded rather than confirm_transaction: these
# signatures are old, and the landing-wait polls signature statuses,
# which the RPC only keeps for recent history.
succeeded = await bot_client.verify_transaction_succeeded(signature)
assert not succeeded, f"SolanaClient accepted reverted {signature}"
print(
f" SolanaClient rejected {signature[:16]}...: "
f"{fetched['meta']['err']}"
)
finally:
await raw_client.close()
await bot_client.close()
CHECKS = (
("helper rejects a reverted transaction", check_helper_rejects_revert),
("helper rejects a missing transaction", check_helper_rejects_missing),
("helper accepts a successful transaction", check_helper_accepts_success),
("confirm_and_assert surfaces a revert", check_confirm_wrapper_rejects_revert),
("helper rejects missing execution metadata", check_helper_rejects_missing_meta),
("revert is a distinct terminal error", check_revert_is_a_distinct_terminal_error),
("requested commitment is propagated", check_commitment_is_propagated),
("endpoint logging hides userinfo", check_endpoint_logging_hides_userinfo),
(
"SolanaClient accepts str and Signature",
check_client_accepts_both_signature_types,
),
(
"getTransaction requests versioned transactions",
check_versioned_transactions_are_requested,
),
("RPC timeouts are retried, not raised", check_rpc_timeouts_are_retried_not_raised),
("every example checks meta.err", check_examples_call_a_status_check),
("src/ reads the confirmation result", check_bot_reads_the_confirmation_result),
)
async def main() -> int:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"--live",
action="store_true",
help="also replay issue #175's reverted signatures against mainnet",
)
args = parser.parse_args()
failures = 0
for label, check in CHECKS:
# Any exception is a failure of that check, not of the run: a check that
# raises must not stop the remaining ones from reporting. Some of these
# verify that a call does NOT raise, so the raise IS the finding.
try:
await check()
except Exception as exc: # noqa: BLE001
failures += 1
reason = f"{type(exc).__name__}: {exc}" if str(exc) else type(exc).__name__
print(f"{label} -> FAIL\n {reason}")
else:
print(f"{label} -> OK")
if args.live:
print("\nreplaying issue #175 signatures against mainnet...")
try:
await check_live_signatures()
except Exception as exc: # noqa: BLE001
failures += 1
reason = f"{type(exc).__name__}: {exc}" if str(exc) else type(exc).__name__
print(f"live signature replay -> FAIL\n {reason}")
else:
print("live signature replay -> OK")
if failures:
print(f"\n{failures} check(s) failed.")
return 1
print("\nAll transaction-status checks passed.")
return 0
if __name__ == "__main__":
sys.exit(asyncio.run(main()))