pmquant 0.2.0__tar.gz → 0.4.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {pmquant-0.2.0 → pmquant-0.4.0}/.github/workflows/canary.yml +2 -2
- {pmquant-0.2.0 → pmquant-0.4.0}/.github/workflows/publish.yml +13 -2
- {pmquant-0.2.0 → pmquant-0.4.0}/.github/workflows/test.yml +5 -4
- {pmquant-0.2.0 → pmquant-0.4.0}/.gitignore +1 -0
- pmquant-0.4.0/CHANGELOG.md +75 -0
- pmquant-0.4.0/CLAUDE.md +52 -0
- pmquant-0.4.0/CONTRIBUTING.md +52 -0
- {pmquant-0.2.0 → pmquant-0.4.0}/PKG-INFO +29 -1
- {pmquant-0.2.0 → pmquant-0.4.0}/README.md +20 -0
- {pmquant-0.2.0 → pmquant-0.4.0}/bot-template/dash/bot_dash.py +4 -4
- pmquant-0.4.0/docs/assets/pmq-doctor.svg +17 -0
- pmquant-0.4.0/docs/recipes.md +93 -0
- {pmquant-0.2.0 → pmquant-0.4.0}/docs/rounding-study.md +11 -4
- {pmquant-0.2.0 → pmquant-0.4.0}/pyproject.toml +19 -2
- {pmquant-0.2.0 → pmquant-0.4.0}/src/pmq/__init__.py +2 -2
- {pmquant-0.2.0 → pmquant-0.4.0}/src/pmq/data.py +70 -34
- pmquant-0.4.0/src/pmq/doctor.py +202 -0
- {pmquant-0.2.0 → pmquant-0.4.0}/src/pmq/executor.py +50 -31
- {pmquant-0.2.0 → pmquant-0.4.0}/src/pmq/mcp.py +20 -14
- pmquant-0.4.0/src/pmq/py.typed +0 -0
- pmquant-0.4.0/tests/test_data.py +179 -0
- pmquant-0.4.0/tests/test_doctor.py +160 -0
- {pmquant-0.2.0 → pmquant-0.4.0}/tests/test_executor.py +101 -0
- pmquant-0.4.0/tests/test_mcp.py +133 -0
- pmquant-0.2.0/CHANGELOG.md +0 -35
- pmquant-0.2.0/tests/test_data.py +0 -86
- pmquant-0.2.0/tests/test_mcp.py +0 -38
- {pmquant-0.2.0 → pmquant-0.4.0}/AGENTS.md +0 -0
- {pmquant-0.2.0 → pmquant-0.4.0}/LICENSE +0 -0
- {pmquant-0.2.0 → pmquant-0.4.0}/SECURITY.md +0 -0
- {pmquant-0.2.0 → pmquant-0.4.0}/bot-template/README.md +0 -0
- {pmquant-0.2.0 → pmquant-0.4.0}/bot-template/bot.py +0 -0
- {pmquant-0.2.0 → pmquant-0.4.0}/bot-template/dash/dash.html +0 -0
- {pmquant-0.2.0 → pmquant-0.4.0}/bot-template/pmq-bot.service +0 -0
- {pmquant-0.2.0 → pmquant-0.4.0}/bot-template/strategy.py +0 -0
- {pmquant-0.2.0 → pmquant-0.4.0}/docs/war-story.md +0 -0
- {pmquant-0.2.0 → pmquant-0.4.0}/examples/fak_buy_guarded.py +0 -0
- {pmquant-0.2.0 → pmquant-0.4.0}/examples/read_market.py +0 -0
- {pmquant-0.2.0 → pmquant-0.4.0}/llms.txt +0 -0
- {pmquant-0.2.0 → pmquant-0.4.0}/src/pmq/exceptions.py +0 -0
- {pmquant-0.2.0 → pmquant-0.4.0}/tests/test_canary_live.py +0 -0
- {pmquant-0.2.0 → pmquant-0.4.0}/tests/test_template_engine.py +0 -0
|
@@ -11,13 +11,24 @@ jobs:
|
|
|
11
11
|
permissions:
|
|
12
12
|
id-token: write
|
|
13
13
|
steps:
|
|
14
|
-
- uses: actions/checkout@
|
|
14
|
+
- uses: actions/checkout@v5
|
|
15
15
|
|
|
16
16
|
- name: Set up Python
|
|
17
|
-
uses: actions/setup-python@
|
|
17
|
+
uses: actions/setup-python@v6
|
|
18
18
|
with:
|
|
19
19
|
python-version: "3.12"
|
|
20
20
|
|
|
21
|
+
- name: Tag matches pyproject version
|
|
22
|
+
if: github.event_name == 'release'
|
|
23
|
+
run: |
|
|
24
|
+
pkg=$(grep -m1 '^version' pyproject.toml | cut -d'"' -f2)
|
|
25
|
+
init=$(grep -m1 '^__version__' src/pmq/__init__.py | cut -d'"' -f2)
|
|
26
|
+
tag="${GITHUB_REF_NAME#v}"
|
|
27
|
+
if [ "$pkg" != "$tag" ] || [ "$init" != "$tag" ]; then
|
|
28
|
+
echo "tag v$tag, pyproject $pkg, __init__ $init: bump all three before releasing"
|
|
29
|
+
exit 1
|
|
30
|
+
fi
|
|
31
|
+
|
|
21
32
|
- name: Install
|
|
22
33
|
run: pip install -e ".[dev]"
|
|
23
34
|
|
|
@@ -7,12 +7,13 @@ jobs:
|
|
|
7
7
|
runs-on: ubuntu-latest
|
|
8
8
|
strategy:
|
|
9
9
|
matrix:
|
|
10
|
-
python-version: ["3.10", "3.12"]
|
|
10
|
+
python-version: ["3.10", "3.11", "3.12", "3.13", "3.14"]
|
|
11
11
|
steps:
|
|
12
|
-
- uses: actions/checkout@
|
|
13
|
-
- uses: actions/setup-python@
|
|
12
|
+
- uses: actions/checkout@v5
|
|
13
|
+
- uses: actions/setup-python@v6
|
|
14
14
|
with:
|
|
15
15
|
python-version: ${{ matrix.python-version }}
|
|
16
16
|
- run: pip install -e ".[dev]"
|
|
17
17
|
- run: ruff check .
|
|
18
|
-
- run:
|
|
18
|
+
- run: mypy
|
|
19
|
+
- run: pytest -q --cov=pmq --cov-fail-under=85
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 0.4.0 (2026-07-03)
|
|
4
|
+
|
|
5
|
+
* Typing: the whole public API is annotated and ships a `py.typed` marker;
|
|
6
|
+
`mypy --strict` runs in CI. New exported types `ParsedMarket` and
|
|
7
|
+
`BookMeta` (TypedDicts) describe what `parse_market` and `book_meta`
|
|
8
|
+
return.
|
|
9
|
+
* Builder attribution: the code now rides explicitly inside EVERY order's
|
|
10
|
+
args (market and limit paths) in addition to the client-level
|
|
11
|
+
BuilderConfig, and tests pin all three layers: executor args, real client
|
|
12
|
+
construction, and the dependency's own config injection. Disclosure and
|
|
13
|
+
one-line opt-out unchanged.
|
|
14
|
+
* Tests: 97 (from 39). Executable table of the fail-closed fill contract
|
|
15
|
+
(one row per possible exchange outcome, both order paths), deep pmq-doctor
|
|
16
|
+
scenarios with mocked RPC and CLOB (sig_type advice, alternative sig_type
|
|
17
|
+
probing, market section), MCP tool coverage including the gated live
|
|
18
|
+
tools. Coverage gate at 85% in CI.
|
|
19
|
+
* pmq-doctor fixes: an RPC failure now fails the run (it used to leave the
|
|
20
|
+
verdict green), and a non-numeric POLY_SIG_TYPE is diagnosed instead of
|
|
21
|
+
crashing.
|
|
22
|
+
* CI: test matrix extended to Python 3.10 through 3.14, classifiers updated
|
|
23
|
+
accordingly.
|
|
24
|
+
* Template: the dash brands itself pmq-bot-dash, matching the shipped
|
|
25
|
+
pmq-bot.service unit name.
|
|
26
|
+
|
|
27
|
+
## 0.3.0 (2026-07-03)
|
|
28
|
+
|
|
29
|
+
* New: `pmq-doctor`, a read-only diagnosis command for Polymarket V2 setups.
|
|
30
|
+
Checks the installed py-clob-client-v2 against the verified surface,
|
|
31
|
+
derives the EOA from `POLY_PRIVATE_KEY` (never printed), reads the funder
|
|
32
|
+
on-chain (contract vs EOA, `owner()`), advises the right `POLY_SIG_TYPE`,
|
|
33
|
+
verifies the CLOB sees collateral with the configured identity (and probes
|
|
34
|
+
the other signature types when it does not), and optionally checks one
|
|
35
|
+
market (`--market <slug>`: book, min_order_size, tick, taker fee).
|
|
36
|
+
* Docs: docs/recipes.md cookbook (trade a market, paper-test a strategy,
|
|
37
|
+
read positions, verify builder attribution on-chain), demo card in the
|
|
38
|
+
README.
|
|
39
|
+
* CI: the publish workflow refuses to upload when the release tag does not
|
|
40
|
+
match the version in pyproject.toml (the failure mode that blocked the
|
|
41
|
+
first 0.3.0 release).
|
|
42
|
+
|
|
43
|
+
## 0.2.0 (2026-07-03)
|
|
44
|
+
|
|
45
|
+
* New: `positions(user)` and `event_markets(slug)` in the data layer; `event`
|
|
46
|
+
tool in the MCP server; `fee_rate(condition_id)` (authoritative per-market
|
|
47
|
+
taker rate from the exchange) and `cancel_order(order_id)` on the executor.
|
|
48
|
+
* Trust: weekly live canary workflow (real-endpoint checks, auto-opens an
|
|
49
|
+
issue on drift), SECURITY.md, production receipt in the README,
|
|
50
|
+
docs/rounding-study.md (measured V2 rounding behavior).
|
|
51
|
+
* Quality: ruff in CI, tests for the bot-template engine (34 tests total).
|
|
52
|
+
|
|
53
|
+
## 0.1.0 (2026-07-03)
|
|
54
|
+
|
|
55
|
+
First release.
|
|
56
|
+
|
|
57
|
+
* `pmq.data`: real-time books (with per-market exchange rules via
|
|
58
|
+
`book_meta`), gamma slug resolution with expired-market fallback,
|
|
59
|
+
market-agnostic `parse_market` (any binary outcomes, close time), settled
|
|
60
|
+
and book-inferred winners, offline trade tape, official per-category taker
|
|
61
|
+
fee formula.
|
|
62
|
+
* `pmq.executor.PolymarketExecutor`: fail-closed CLOB V2 execution. FAK
|
|
63
|
+
buys/sells through the market-order path, exchange-confirmed fills only,
|
|
64
|
+
`OrderUncertain` + `reconcile()` from get_trades, deposit-wallet
|
|
65
|
+
(POLY_1271) support, collateral fail-fast, builder-code default with
|
|
66
|
+
disclosure and opt-out, startup introspection of the installed
|
|
67
|
+
py-clob-client-v2.
|
|
68
|
+
* `pmq.mcp` (`pmq-mcp`): MCP server. Read tools always available
|
|
69
|
+
(find_markets, market, book, taker_fee, account tools); trading tools only
|
|
70
|
+
registered when the operator sets `PMQ_MCP_LIVE=1`, per-order cap via
|
|
71
|
+
`PMQ_MCP_MAX_USD`.
|
|
72
|
+
* `bot-template/`: market-agnostic bot engine (strategy owns `watchlist()`
|
|
73
|
+
and `decide()`), honest paper mode against real books, risk rails (budget
|
|
74
|
+
with fee headroom, poisoning, halts with exit code 42, disk-persisted
|
|
75
|
+
daily halt), systemd unit, phone dashboard.
|
pmquant-0.4.0/CLAUDE.md
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# pmq: engineering invariants for agents CONTRIBUTING to this repo
|
|
2
|
+
|
|
3
|
+
(AGENTS.md in this repo is for agents USING the library; this file is for
|
|
4
|
+
agents EDITING it. Read both before changing code.)
|
|
5
|
+
|
|
6
|
+
## Never weaken (the product IS these properties)
|
|
7
|
+
|
|
8
|
+
1. **The fail-closed fill contract**: a `Fill` books only what the exchange
|
|
9
|
+
confirmed (`orderID` + `success is not False` + matched amounts); 4xx is a
|
|
10
|
+
clean rejection; timeout/5xx raises `OrderUncertain`; unparseable = zero.
|
|
11
|
+
Any change that books more optimistically is a regression by definition,
|
|
12
|
+
whatever it fixes elsewhere. `reconcile()` must keep meaning cancel +
|
|
13
|
+
`get_trades` truth.
|
|
14
|
+
2. **Startup introspection** (`_EXPECTED_METHODS`/`_EXPECTED_MARKET_ARGS`):
|
|
15
|
+
the executor REFUSES to run on a drifted py-clob-client-v2. When bumping
|
|
16
|
+
the client dependency, re-verify signatures by introspection and update
|
|
17
|
+
the tables in the same commit.
|
|
18
|
+
3. **Builder code policy**: default = maintainer's code, DISCLOSED in README
|
|
19
|
+
and code comment, opt-out one line (`builder_code=None` / env). Never
|
|
20
|
+
hide it, never remove the disclosure, never make opt-out harder. This is
|
|
21
|
+
the trust model (JKorf pattern).
|
|
22
|
+
4. **No strategy content, ever**: the maintainer's private bot strategy
|
|
23
|
+
(bands, timing, hours, families, sizing) must never appear in code, docs,
|
|
24
|
+
tests, commits or issues. The bot-template ships deliberately naive
|
|
25
|
+
demos only.
|
|
26
|
+
5. **Claims must be falsifiable**: no superlatives in README/docs; dated
|
|
27
|
+
claims with evidence (comparison table, on-chain receipts, measured
|
|
28
|
+
studies). If you cannot prove it, do not write it.
|
|
29
|
+
6. **MCP safety gates**: trading tools are REGISTERED only when the operator
|
|
30
|
+
sets `PMQ_MCP_LIVE=1`; per-order `PMQ_MCP_MAX_USD` cap enforced before
|
|
31
|
+
any client call. Read tools must keep working with zero credentials.
|
|
32
|
+
|
|
33
|
+
## Working rules
|
|
34
|
+
|
|
35
|
+
* Tests green (`pytest -q`) and `ruff check .` clean before any push; add
|
|
36
|
+
tests with every behavior change. Network-touching tests go to
|
|
37
|
+
`tests/test_canary_live.py` behind `PMQ_CANARY=1`, never in default CI.
|
|
38
|
+
* Exchange rules (min size, tick, fee rate) are READ from the venue
|
|
39
|
+
(`book_meta`, `fee_rate`), not hardcoded. `FEE_RATES` is a documented
|
|
40
|
+
snapshot of the official schedule used for estimates.
|
|
41
|
+
* Releases: bump version in `pyproject.toml` AND `src/pmq/__init__.py`,
|
|
42
|
+
update CHANGELOG.md, push, then `gh release create vX.Y.Z`: PyPI publish
|
|
43
|
+
is automatic via trusted publishing (no tokens anywhere). PyPI name is
|
|
44
|
+
`pmquant`, import name `pmq`: keep the README line explaining it.
|
|
45
|
+
* The weekly canary workflow is the drift alarm: if it opens an issue, the
|
|
46
|
+
fix starts by re-running the introspection against the new surface, not
|
|
47
|
+
by loosening the checks.
|
|
48
|
+
* Keep the library small and auditable (five modules): resist adding
|
|
49
|
+
dependencies; stdlib first. Anything bot-shaped belongs in bot-template/,
|
|
50
|
+
not in the package.
|
|
51
|
+
* Style: no em-dashes and no " - " connectors anywhere (strong user rule);
|
|
52
|
+
keep comments sparse and constraint-focused.
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# pmq: engineering invariants for agents CONTRIBUTING to this repo
|
|
2
|
+
|
|
3
|
+
(AGENTS.md in this repo is for agents USING the library; this file is for
|
|
4
|
+
agents EDITING it. Read both before changing code.)
|
|
5
|
+
|
|
6
|
+
## Never weaken (the product IS these properties)
|
|
7
|
+
|
|
8
|
+
1. **The fail-closed fill contract**: a `Fill` books only what the exchange
|
|
9
|
+
confirmed (`orderID` + `success is not False` + matched amounts); 4xx is a
|
|
10
|
+
clean rejection; timeout/5xx raises `OrderUncertain`; unparseable = zero.
|
|
11
|
+
Any change that books more optimistically is a regression by definition,
|
|
12
|
+
whatever it fixes elsewhere. `reconcile()` must keep meaning cancel +
|
|
13
|
+
`get_trades` truth.
|
|
14
|
+
2. **Startup introspection** (`_EXPECTED_METHODS`/`_EXPECTED_MARKET_ARGS`):
|
|
15
|
+
the executor REFUSES to run on a drifted py-clob-client-v2. When bumping
|
|
16
|
+
the client dependency, re-verify signatures by introspection and update
|
|
17
|
+
the tables in the same commit.
|
|
18
|
+
3. **Builder code policy**: default = maintainer's code, DISCLOSED in README
|
|
19
|
+
and code comment, opt-out one line (`builder_code=None` / env). Never
|
|
20
|
+
hide it, never remove the disclosure, never make opt-out harder. This is
|
|
21
|
+
the trust model (JKorf pattern).
|
|
22
|
+
4. **No strategy content, ever**: the maintainer's private bot strategy
|
|
23
|
+
(bands, timing, hours, families, sizing) must never appear in code, docs,
|
|
24
|
+
tests, commits or issues. The bot-template ships deliberately naive
|
|
25
|
+
demos only.
|
|
26
|
+
5. **Claims must be falsifiable**: no superlatives in README/docs; dated
|
|
27
|
+
claims with evidence (comparison table, on-chain receipts, measured
|
|
28
|
+
studies). If you cannot prove it, do not write it.
|
|
29
|
+
6. **MCP safety gates**: trading tools are REGISTERED only when the operator
|
|
30
|
+
sets `PMQ_MCP_LIVE=1`; per-order `PMQ_MCP_MAX_USD` cap enforced before
|
|
31
|
+
any client call. Read tools must keep working with zero credentials.
|
|
32
|
+
|
|
33
|
+
## Working rules
|
|
34
|
+
|
|
35
|
+
* Tests green (`pytest -q`) and `ruff check .` clean before any push; add
|
|
36
|
+
tests with every behavior change. Network-touching tests go to
|
|
37
|
+
`tests/test_canary_live.py` behind `PMQ_CANARY=1`, never in default CI.
|
|
38
|
+
* Exchange rules (min size, tick, fee rate) are READ from the venue
|
|
39
|
+
(`book_meta`, `fee_rate`), not hardcoded. `FEE_RATES` is a documented
|
|
40
|
+
snapshot of the official schedule used for estimates.
|
|
41
|
+
* Releases: bump version in `pyproject.toml` AND `src/pmq/__init__.py`,
|
|
42
|
+
update CHANGELOG.md, push, then `gh release create vX.Y.Z`: PyPI publish
|
|
43
|
+
is automatic via trusted publishing (no tokens anywhere). PyPI name is
|
|
44
|
+
`pmquant`, import name `pmq`: keep the README line explaining it.
|
|
45
|
+
* The weekly canary workflow is the drift alarm: if it opens an issue, the
|
|
46
|
+
fix starts by re-running the introspection against the new surface, not
|
|
47
|
+
by loosening the checks.
|
|
48
|
+
* Keep the library small and auditable (five modules): resist adding
|
|
49
|
+
dependencies; stdlib first. Anything bot-shaped belongs in bot-template/,
|
|
50
|
+
not in the package.
|
|
51
|
+
* Style: no em-dashes and no " - " connectors anywhere (strong user rule);
|
|
52
|
+
keep comments sparse and constraint-focused.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: pmquant
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.4.0
|
|
4
4
|
Summary: Fail-closed execution and market-data layer for Polymarket CLOB V2: local signing, confirmed fills only, fee-correct math, working deposit-wallet (POLY_1271) support.
|
|
5
5
|
Project-URL: Homepage, https://github.com/crp4222/pmq
|
|
6
6
|
Project-URL: Issues, https://github.com/crp4222/pmq/issues
|
|
@@ -13,11 +13,19 @@ Classifier: Intended Audience :: Developers
|
|
|
13
13
|
Classifier: Intended Audience :: Financial and Insurance Industry
|
|
14
14
|
Classifier: License :: OSI Approved :: MIT License
|
|
15
15
|
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
16
21
|
Classifier: Topic :: Office/Business :: Financial :: Investment
|
|
22
|
+
Classifier: Typing :: Typed
|
|
17
23
|
Requires-Python: >=3.10
|
|
18
24
|
Requires-Dist: py-clob-client-v2<2,>=1.0.2
|
|
19
25
|
Provides-Extra: dev
|
|
20
26
|
Requires-Dist: mcp>=1.2; extra == 'dev'
|
|
27
|
+
Requires-Dist: mypy>=1.10; extra == 'dev'
|
|
28
|
+
Requires-Dist: pytest-cov>=5; extra == 'dev'
|
|
21
29
|
Requires-Dist: pytest>=8; extra == 'dev'
|
|
22
30
|
Requires-Dist: ruff>=0.6; extra == 'dev'
|
|
23
31
|
Provides-Extra: mcp
|
|
@@ -29,6 +37,8 @@ Description-Content-Type: text/markdown
|
|
|
29
37
|
[](https://pypi.org/project/pmquant/)
|
|
30
38
|
[](https://github.com/crp4222/pmq/actions/workflows/test.yml)
|
|
31
39
|
[](https://github.com/crp4222/pmq/actions/workflows/canary.yml)
|
|
40
|
+
[](.github/workflows/test.yml)
|
|
41
|
+
[](pyproject.toml)
|
|
32
42
|
[](LICENSE)
|
|
33
43
|
|
|
34
44
|
Fail-closed execution and market data for **Polymarket CLOB V2**, in Python.
|
|
@@ -80,6 +90,24 @@ settled, with the builder code visible in the calldata. Additionally, a weekly
|
|
|
80
90
|
and the installed client surface, and opens an issue by itself if Polymarket
|
|
81
91
|
drifts.
|
|
82
92
|
|
|
93
|
+
## pmq-doctor: diagnose your setup in one command
|
|
94
|
+
|
|
95
|
+
```bash
|
|
96
|
+
pip install pmquant && pmq-doctor --market <slug>
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
It checks, in order: the installed client surface (introspection), your
|
|
100
|
+
derived EOA, the funder wallet on-chain (`owner()` and bytecode: is it a
|
|
101
|
+
deposit wallet?), whether `POLY_SIG_TYPE` matches the wallet type, whether
|
|
102
|
+
the CLOB actually sees your collateral (and if not, WHICH sig_type does),
|
|
103
|
+
and the target market's minimum size and tick. Real output on a real
|
|
104
|
+
deposit-wallet account:
|
|
105
|
+
|
|
106
|
+

|
|
107
|
+
|
|
108
|
+
If you landed here from "the order signer address has to be the address of
|
|
109
|
+
the API KEY" or a CLOB balance of 0 with funds on-chain: this is the tool.
|
|
110
|
+
|
|
83
111
|
## The contract: nothing is booked without exchange confirmation
|
|
84
112
|
|
|
85
113
|
| Situation | What pmq does |
|
|
@@ -3,6 +3,8 @@
|
|
|
3
3
|
[](https://pypi.org/project/pmquant/)
|
|
4
4
|
[](https://github.com/crp4222/pmq/actions/workflows/test.yml)
|
|
5
5
|
[](https://github.com/crp4222/pmq/actions/workflows/canary.yml)
|
|
6
|
+
[](.github/workflows/test.yml)
|
|
7
|
+
[](pyproject.toml)
|
|
6
8
|
[](LICENSE)
|
|
7
9
|
|
|
8
10
|
Fail-closed execution and market data for **Polymarket CLOB V2**, in Python.
|
|
@@ -54,6 +56,24 @@ settled, with the builder code visible in the calldata. Additionally, a weekly
|
|
|
54
56
|
and the installed client surface, and opens an issue by itself if Polymarket
|
|
55
57
|
drifts.
|
|
56
58
|
|
|
59
|
+
## pmq-doctor: diagnose your setup in one command
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
pip install pmquant && pmq-doctor --market <slug>
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
It checks, in order: the installed client surface (introspection), your
|
|
66
|
+
derived EOA, the funder wallet on-chain (`owner()` and bytecode: is it a
|
|
67
|
+
deposit wallet?), whether `POLY_SIG_TYPE` matches the wallet type, whether
|
|
68
|
+
the CLOB actually sees your collateral (and if not, WHICH sig_type does),
|
|
69
|
+
and the target market's minimum size and tick. Real output on a real
|
|
70
|
+
deposit-wallet account:
|
|
71
|
+
|
|
72
|
+

|
|
73
|
+
|
|
74
|
+
If you landed here from "the order signer address has to be the address of
|
|
75
|
+
the API KEY" or a CLOB balance of 0 with funds on-chain: this is the tool.
|
|
76
|
+
|
|
57
77
|
## The contract: nothing is booked without exchange confirmation
|
|
58
78
|
|
|
59
79
|
| Situation | What pmq does |
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
|
|
4
4
|
Design constraints:
|
|
5
5
|
- stdlib only (http.server), one process, a few MB of RSS: safe on the Pi
|
|
6
|
-
- READ ONLY: parses bot_runs CSVs, systemctl show and the
|
|
6
|
+
- READ ONLY: parses bot_runs CSVs, systemctl show and the bot's journal;
|
|
7
7
|
never reads live.env, never talks to any exchange API, no secrets
|
|
8
8
|
- the phone does the rendering; this process only ships small JSON + one
|
|
9
9
|
static HTML file (cached in memory, CSVs re-parsed only on mtime change)
|
|
@@ -67,7 +67,7 @@ def _run(cmd):
|
|
|
67
67
|
|
|
68
68
|
|
|
69
69
|
def service_state():
|
|
70
|
-
"""
|
|
70
|
+
"""bot systemd state + last collateral line from its journal."""
|
|
71
71
|
with _lock:
|
|
72
72
|
cached = _cache.get("svc")
|
|
73
73
|
if cached and time.time() - cached[0] < SVC_CACHE_S:
|
|
@@ -141,7 +141,7 @@ def api_payload():
|
|
|
141
141
|
|
|
142
142
|
|
|
143
143
|
class Handler(BaseHTTPRequestHandler):
|
|
144
|
-
server_version = "
|
|
144
|
+
server_version = "pmq-bot-dash"
|
|
145
145
|
|
|
146
146
|
def _send(self, code, ctype, body):
|
|
147
147
|
self.send_response(code)
|
|
@@ -172,5 +172,5 @@ class Handler(BaseHTTPRequestHandler):
|
|
|
172
172
|
|
|
173
173
|
if __name__ == "__main__":
|
|
174
174
|
srv = ThreadingHTTPServer(("0.0.0.0", PORT), Handler)
|
|
175
|
-
print(f"
|
|
175
|
+
print(f"pmq-bot-dash listening on :{PORT}", flush=True)
|
|
176
176
|
srv.serve_forever()
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" width="880" height="292" font-family="SFMono-Regular,Consolas,Menlo,monospace" font-size="12.5">
|
|
2
|
+
<rect width="880" height="292" rx="10" fill="#16161a"/>
|
|
3
|
+
<circle cx="20" cy="20" r="5.5" fill="#ff5f57"/><circle cx="38" cy="20" r="5.5" fill="#febc2e"/><circle cx="56" cy="20" r="5.5" fill="#28c840"/>
|
|
4
|
+
<text x="440.0" y="24" fill="#71717a" text-anchor="middle" font-size="11">pmq-doctor · real account, real output</text>
|
|
5
|
+
<text x="18" y="54" fill="#ffffff" xml:space="preserve">pmq-doctor: Polymarket V2 setup diagnosis (read-only, no key ever printed)</text>
|
|
6
|
+
<text x="18" y="73" fill="#22c55e" xml:space="preserve">[ok] installed py-clob-client-v2 matches the verified surface</text>
|
|
7
|
+
<text x="18" y="92" fill="#22c55e" xml:space="preserve">[ok] POLY_PRIVATE_KEY present (never printed)</text>
|
|
8
|
+
<text x="18" y="111" fill="#c3c2b7" xml:space="preserve"> derived EOA: 0xdb581D8BE7b5C53C6D832083a8A49D0CBF73d6e1</text>
|
|
9
|
+
<text x="18" y="130" fill="#c3c2b7" xml:space="preserve"> POLY_FUNDER: 0x76cd962fc8c5f5e5a0cbe14c74339aa78268da58 POLY_SIG_TYPE: 3</text>
|
|
10
|
+
<text x="18" y="149" fill="#22c55e" xml:space="preserve">[ok] funder wallet on-chain: funder is a contract owned by your EOA: a deposit wallet, signature_type=3 (POLY_1271)</text>
|
|
11
|
+
<text x="18" y="168" fill="#c3c2b7" xml:space="preserve"> on-chain pUSD at funder: 39.42</text>
|
|
12
|
+
<text x="18" y="187" fill="#22c55e" xml:space="preserve">[ok] POLY_SIG_TYPE=3 matches the wallet type</text>
|
|
13
|
+
<text x="18" y="206" fill="#22c55e" xml:space="preserve">[ok] CLOB sees collateral with sig_type=3: 39.42 USDC</text>
|
|
14
|
+
<text x="18" y="225" fill="#22c55e" xml:space="preserve">[ok] market btc-updown-15m-1783081800: bid=0.48 ask=0.49 min_order_size=5.0 tick=0.01</text>
|
|
15
|
+
<text x="18" y="244" fill="#c3c2b7" xml:space="preserve"> taker fee at ask: 0.0175$/share (crypto table; authoritative per-market rate via executor.fee_rate)</text>
|
|
16
|
+
<text x="18" y="263" fill="#eab308" xml:space="preserve">verdict: everything green, orders should work</text>
|
|
17
|
+
</svg>
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
# Recipes
|
|
2
|
+
|
|
3
|
+
Copy-paste starting points. Everything below is Python against `pip install
|
|
4
|
+
pmquant` (`import pmq`); execution snippets read `POLY_PRIVATE_KEY`,
|
|
5
|
+
`POLY_FUNDER`, `POLY_SIG_TYPE` from the environment and never print them.
|
|
6
|
+
|
|
7
|
+
## Trade a political market in 20 lines
|
|
8
|
+
|
|
9
|
+
```python
|
|
10
|
+
import pmq
|
|
11
|
+
|
|
12
|
+
# discover: any gamma slug works (politics, sports, crypto)
|
|
13
|
+
pm = pmq.parse_market(pmq.get_market("mississippi-gubernatorial-election-presley-d-vs-reeves-r"))
|
|
14
|
+
book = pmq.get_book(pm["token_a"]) # real-time, trustable
|
|
15
|
+
bid, bid_sz, ask, ask_sz = pmq.best_bid_ask(book)
|
|
16
|
+
rules = pmq.book_meta(book) # min_order_size, tick_size
|
|
17
|
+
|
|
18
|
+
ex = pmq.PolymarketExecutor() # sig_type 3 = app wallet
|
|
19
|
+
ex.require_collateral(10)
|
|
20
|
+
if ask and ask * rules["min_order_size"] <= 10:
|
|
21
|
+
fill = ex.buy_fak(pm["token_a"], price_cap=ask, usd=10.0)
|
|
22
|
+
if fill: # book ONLY what matched
|
|
23
|
+
print(f"bought {fill.matched_shares} {pm['outcome_a']} at {fill.price:.3f}")
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Scan a multi-outcome event (election, tournament)
|
|
27
|
+
|
|
28
|
+
```python
|
|
29
|
+
import pmq
|
|
30
|
+
|
|
31
|
+
total_asks = 0.0
|
|
32
|
+
for pm in pmq.event_markets("world-cup-winner"):
|
|
33
|
+
_, _, ask, _ = pmq.best_bid_ask(pmq.get_book(pm["token_a"]))
|
|
34
|
+
if ask is None:
|
|
35
|
+
print(f"{pm['outcome_a']:24s} unquoted (basket incomplete)")
|
|
36
|
+
continue
|
|
37
|
+
total_asks += ask
|
|
38
|
+
print(f"{pm['outcome_a']:24s} ask {ask}")
|
|
39
|
+
print("sum of asks:", round(total_asks, 4), "(a full basket below 1 - fees pays 1)")
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## What do I hold?
|
|
43
|
+
|
|
44
|
+
```python
|
|
45
|
+
import os
|
|
46
|
+
import pmq
|
|
47
|
+
|
|
48
|
+
for p in pmq.positions(os.environ["POLY_FUNDER"]):
|
|
49
|
+
print(p["slug"], p["outcome"], p["size"], "avg", p["avgPrice"],
|
|
50
|
+
"now", p.get("curPrice"), "value", p.get("currentValue"))
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## Paper-test a strategy on any market
|
|
54
|
+
|
|
55
|
+
Use [bot-template/](../bot-template/): implement `watchlist()` and
|
|
56
|
+
`decide()` in `strategy.py`, run `python bot.py 24`, read
|
|
57
|
+
`bot_runs/windows.csv`. Paper fills execute against the REAL ask, capped by
|
|
58
|
+
displayed size and the per-market exchange minimum, and are scored with the
|
|
59
|
+
real fee at resolution: if it does not survive paper, it will not survive
|
|
60
|
+
live.
|
|
61
|
+
|
|
62
|
+
## Budget with the real fee
|
|
63
|
+
|
|
64
|
+
```python
|
|
65
|
+
import pmq
|
|
66
|
+
|
|
67
|
+
ex = pmq.PolymarketExecutor()
|
|
68
|
+
rate = ex.fee_rate(pm["condition_id"]) # authoritative, from the exchange
|
|
69
|
+
cost_per_share = ask + pmq.fee(ask, 1.0, rate)
|
|
70
|
+
shares_affordable = budget_usd / cost_per_share
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## Verify builder attribution on-chain
|
|
74
|
+
|
|
75
|
+
```python
|
|
76
|
+
import pmq
|
|
77
|
+
|
|
78
|
+
sh, usd, fees = ex.trades_totals(pm["condition_id"]) # your fills, exchange truth
|
|
79
|
+
# every matched order settles on the CTF Exchange V2; the bytes32 builder
|
|
80
|
+
# code rides in the calldata. Grep any of your settlement transactions:
|
|
81
|
+
# https://polygonscan.com/tx/<transactionHash from get_trades>
|
|
82
|
+
# and search the input data for your builder code (without the 0x prefix).
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
## After a timeout or 5xx
|
|
86
|
+
|
|
87
|
+
```python
|
|
88
|
+
try:
|
|
89
|
+
fill = ex.buy_fak(token, cap, usd)
|
|
90
|
+
except pmq.OrderUncertain:
|
|
91
|
+
sh, usd_spent, fees = ex.reconcile(pm["condition_id"], token)
|
|
92
|
+
# book sh/usd_spent, nothing else, and only now place new orders
|
|
93
|
+
```
|
|
@@ -36,10 +36,17 @@ amounts against requested size, cross-checked with `get_trades`.
|
|
|
36
36
|
unrounded size or price, it diverges from what the exchange signed. Book
|
|
37
37
|
from the response's matched amounts (what pmq's `Fill` does), never from
|
|
38
38
|
your request.
|
|
39
|
-
3. Limit-path fills never exceeded the signed size in these tests
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
39
|
+
3. Limit-path fills never exceeded the signed size in these tests because
|
|
40
|
+
they matched AT the limit price. The mechanism behind the overfill
|
|
41
|
+
reports (credit to gmoutsin in
|
|
42
|
+
[#89](https://github.com/Polymarket/py-clob-client-v2/issues/89)): the
|
|
43
|
+
signed V2 order is an amounts PAIR (makerAmount USDC, takerAmount
|
|
44
|
+
tokens), a ratio with a worst-case bound, not a (price, size) tuple.
|
|
45
|
+
Under price improvement the engine preserves your dollars and returns
|
|
46
|
+
proportionally MORE tokens (4.95 at a 0.98 ask = 5.051 tokens on a
|
|
47
|
+
"size 5" order at 0.99). Strictly favorable, but it breaks size-based
|
|
48
|
+
accounting: book from the matched amounts, and never rely on a
|
|
49
|
+
marketable limit to cap token count exactly.
|
|
43
50
|
4. The per-market minimum size is enforced server-side with a clean 400, and
|
|
44
51
|
is readable in advance from the book response (`min_order_size`, exposed
|
|
45
52
|
by `pmq.book_meta`).
|
|
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "pmquant"
|
|
7
|
-
version = "0.
|
|
7
|
+
version = "0.4.0"
|
|
8
8
|
description = "Fail-closed execution and market-data layer for Polymarket CLOB V2: local signing, confirmed fills only, fee-correct math, working deposit-wallet (POLY_1271) support."
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
license = { text = "MIT" }
|
|
@@ -22,7 +22,13 @@ classifiers = [
|
|
|
22
22
|
"Intended Audience :: Financial and Insurance Industry",
|
|
23
23
|
"License :: OSI Approved :: MIT License",
|
|
24
24
|
"Programming Language :: Python :: 3",
|
|
25
|
+
"Programming Language :: Python :: 3.10",
|
|
26
|
+
"Programming Language :: Python :: 3.11",
|
|
27
|
+
"Programming Language :: Python :: 3.12",
|
|
28
|
+
"Programming Language :: Python :: 3.13",
|
|
29
|
+
"Programming Language :: Python :: 3.14",
|
|
25
30
|
"Topic :: Office/Business :: Financial :: Investment",
|
|
31
|
+
"Typing :: Typed",
|
|
26
32
|
]
|
|
27
33
|
|
|
28
34
|
[project.urls]
|
|
@@ -31,14 +37,25 @@ Issues = "https://github.com/crp4222/pmq/issues"
|
|
|
31
37
|
|
|
32
38
|
[project.optional-dependencies]
|
|
33
39
|
mcp = ["mcp>=1.2"]
|
|
34
|
-
dev = ["pytest>=8", "mcp>=1.2", "ruff>=0.6"]
|
|
40
|
+
dev = ["pytest>=8", "pytest-cov>=5", "mcp>=1.2", "ruff>=0.6", "mypy>=1.10"]
|
|
35
41
|
|
|
36
42
|
[project.scripts]
|
|
37
43
|
pmq-mcp = "pmq.mcp:main"
|
|
44
|
+
pmq-doctor = "pmq.doctor:main"
|
|
38
45
|
|
|
39
46
|
[tool.hatch.build.targets.wheel]
|
|
40
47
|
packages = ["src/pmq"]
|
|
41
48
|
|
|
49
|
+
[tool.mypy]
|
|
50
|
+
python_version = "3.10"
|
|
51
|
+
strict = true
|
|
52
|
+
files = ["src/pmq"]
|
|
53
|
+
|
|
54
|
+
[[tool.mypy.overrides]]
|
|
55
|
+
module = ["py_clob_client_v2.*", "eth_account.*"]
|
|
56
|
+
ignore_missing_imports = true
|
|
57
|
+
follow_imports = "skip"
|
|
58
|
+
|
|
42
59
|
[tool.ruff]
|
|
43
60
|
line-length = 100
|
|
44
61
|
target-version = "py310"
|
|
@@ -25,7 +25,7 @@ from .data import (
|
|
|
25
25
|
)
|
|
26
26
|
from .exceptions import IntrospectionMismatch, OrderUncertain, PmqError
|
|
27
27
|
|
|
28
|
-
__version__ = "0.
|
|
28
|
+
__version__ = "0.4.0"
|
|
29
29
|
__all__ = [
|
|
30
30
|
"FEE_RATES", "band_ask_depth_usd", "best_bid_ask", "book_inferred_winner",
|
|
31
31
|
"book_meta", "event_markets", "fee", "get_book", "get_market", "get_tape",
|
|
@@ -36,7 +36,7 @@ __all__ = [
|
|
|
36
36
|
]
|
|
37
37
|
|
|
38
38
|
|
|
39
|
-
def __getattr__(name):
|
|
39
|
+
def __getattr__(name: str) -> object:
|
|
40
40
|
# Lazy import: the data layer must stay usable without py-clob-client-v2.
|
|
41
41
|
if name in ("PolymarketExecutor", "Fill", "DEFAULT_BUILDER_CODE"):
|
|
42
42
|
from . import executor
|