feat: read any wallet's order-book positions and collateral#1399
Conversation
📝 WalkthroughWalkthroughAdds SQL migration ChangesOrder-book portfolio-by-wallet getters
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Suggested labels
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
Time Submission Status
Submit or update total time with: Add time on top of previous submission with: See available commands to help comply with our Guidelines. |
|
@holdex pr submit-time 3h |
resolves: truflation/website#4171 ## What Read a wallet's order-book portfolio by **address** from Python, plus a working demonstration in the MAA lifecycle example: - `TNClient.get_positions_by_wallet(wallet_address)` — holdings + open orders - `TNClient.get_collateral_by_wallet(wallet_address, bridge)` — total locked collateral on a bridge ## Why `get_user_positions` / `get_user_collateral` read the signer (`@caller`), so they cannot read a wallet you do not sign for — e.g. an owner, or a delegated market-maker bot, watching an **agent wallet (MAA)** whose positions live under the MAA's participant id, not the signer's. These wrap the new node action and sdk-go methods. ## How - gopy bindings (`GetPositionsByWallet` / `GetCollateralByWallet`) forwarding to sdk-go, plus thin `TNClient` wrappers that JSON-parse the result and reuse the existing `UserPosition` / `UserCollateral` types. - Re-pins `sdk-go` to the merged commit that adds the methods (`go.mod` / `go.sum`); `kwil-db` is unchanged. ## Example `examples/maa_lifecycle_example` now: - loads config from a gitignored `.env` (zero-dependency loader; real env vars still win), with a committed `.env.example` - adds step (h): reads the agent wallet's positions/collateral by address - uses a fresh salt per run so the smoke test is re-runnable (`MAA_SALT` pins it) ## Testing - `tests/test_portfolio_by_wallet.py` — pure unit tests (monkeypatched binding) for argument forwarding, JSON parsing, and empty handling. All pass. - Verified live: the example's step (h) runs against testnet and both getters return cleanly (migration 051 applied), proving the path end-to-end. ## Depends on (merged) - <trufnetwork/node#1399> - <trufnetwork/sdk-go#195> ## Context - <truflation/website#4121> <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Added wallet-scoped order-book queries: `get_positions_by_wallet()` retrieves positions and holdings for any wallet address, while `get_collateral_by_wallet()` returns collateral data without requiring the wallet signer. * **Documentation** * Updated lifecycle example to use .env file configuration and added documentation for new wallet address-based portfolio queries. * **Tests** * Added unit tests covering wallet-scoped portfolio retrieval methods. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
resolves: https://github.com/truflation/website/issues/4171
What
Two address-parameterized, read-only order-book getters so a wallet's live portfolio can be read by address rather than by signer (migration
051):get_positions_by_wallet($wallet_address)— holdings + open orders across all marketsget_collateral_by_wallet($wallet_address, $bridge)— total locked collateral on a bridgeWhy
get_user_positions/get_user_collateral(038) read@caller, so they only ever return the signer's own rows. That cannot read a wallet you do not sign for — e.g. an owner, or a delegated market-maker bot, watching an agent wallet (MAA) whose positions are recorded under the MAA's participant id, not the signer's. Migration050already did this for order-event history; these two complete the set for the live portfolio an inventory-aware market maker needs.How
038getters exactly, except the participant is resolved from the$wallet_addressargument (normalized with/without a0xprefix, like050) instead of@caller. The collateral computation is byte-for-byte identical toget_user_collateralapart from that resolution.$bridgeis a required argument (per-bridge token decimals) rather than env-defaulted, so the file references no bridge namespace literal and needs no dev/prod twin.PUBLIC VIEWreads over existing tables (noALTER), so it is safe to apply against a live, settling node. Deploy withkwil-cli exec-sql --file 051… --syncrather than replaying all migrations.Testing
tests/streams/order_book/portfolio_by_wallet_test.go—TestPortfolioByWallet, 6 cases (all pass):@caller)0xprefixContext
Summary by CodeRabbit
Release Notes
0xprefix).