Skip to content

Commit 2ed3a5f

Browse files
authored
feat: use Multicall3 discovery for periodic MM claims (#27)
Replace the session-only claim loop with claim_all_winnings() which uses Multicall3 batched on-chain reads to discover ALL claimable positions — not just markets traded in the current session. This ensures the market maker recycles liquidity from: - Previous sessions that were restarted - Positions from manual trades or other tools - Any resolved markets the old tracker missed Runs every 5 minutes (vs 2 min before) since discovery scans all markets. Initial 60s delay to let markets initialize first.
1 parent 1b95f2c commit 2ed3a5f

File tree

1 file changed

+27
-32
lines changed

1 file changed

+27
-32
lines changed

examples/market_maker.py

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
- Allocation skew: more capital on the likely-winning side
2929
- Auto-approves USDC gaslessly when entering a new market
3030
- Automatic market transition when 15-minute markets rotate
31-
- Automatic claiming of winnings from resolved markets
31+
- Automatic claiming of winnings via Multicall3 on-chain discovery (every 5 min)
3232
3333
Usage:
3434
TURBINE_PRIVATE_KEY=0x... python examples/market_maker.py
@@ -1088,43 +1088,38 @@ async def monitor_market_transitions(self) -> None:
10881088
await asyncio.sleep(5)
10891089

10901090
async def claim_resolved_markets(self) -> None:
1091-
"""Background task to claim winnings from resolved markets."""
1092-
while self.running:
1093-
try:
1094-
all_traded: list[tuple[str, str, AssetState]] = []
1095-
for state in self.asset_states.values():
1096-
for market_id, contract_address in list(state.traded_markets.items()):
1097-
all_traded.append((market_id, contract_address, state))
1091+
"""Background task to periodically discover and claim all winnings.
10981092
1099-
if not all_traded:
1100-
await asyncio.sleep(120)
1101-
continue
1093+
Uses Multicall3-based on-chain discovery (claim_all_winnings) to find
1094+
and claim ALL resolved positions — not just markets traded in this session.
1095+
This ensures the MM recycles liquidity from any prior session or wallet activity.
1096+
"""
1097+
# Wait for initial market setup before first claim attempt
1098+
await asyncio.sleep(60)
11021099

1103-
for market_id, contract_address, state in all_traded:
1104-
try:
1105-
resolution = self.client.get_resolution(market_id)
1106-
if not (resolution and resolution.resolved):
1107-
continue
1108-
except Exception:
1109-
continue
1100+
while self.running:
1101+
try:
1102+
print("[CLAIM] Scanning for claimable positions (Multicall3 discovery)...")
1103+
result = self.client.claim_all_winnings()
1104+
tx_hash = result.get("txHash", result.get("tx_hash", "unknown"))
1105+
print(f"[CLAIM] 💰 Claimed winnings! TX: {tx_hash}")
11101106

1111-
try:
1112-
result = self.client.claim_winnings(contract_address)
1113-
tx_hash = result.get("txHash", result.get("tx_hash", "unknown"))
1114-
print(f"[{state.asset}] 💰 Claimed winnings from {market_id[:8]}... TX: {tx_hash}")
1115-
del state.traded_markets[market_id]
1116-
except ValueError as e:
1117-
if "no winning tokens" in str(e).lower():
1118-
del state.traded_markets[market_id]
1119-
except Exception as e:
1120-
print(f"[{state.asset}] Claim error: {e}")
1121-
1122-
await asyncio.sleep(15)
1107+
# Clear tracked markets that were just claimed
1108+
for state in self.asset_states.values():
1109+
state.traded_markets.clear()
11231110

1111+
except ValueError as e:
1112+
# "No claimable positions found" — normal, nothing to claim
1113+
if "no claimable" in str(e).lower():
1114+
print(f"[CLAIM] No claimable positions found — all clear")
1115+
else:
1116+
print(f"[CLAIM] Error: {e}")
11241117
except Exception as e:
1125-
print(f"Claim monitor error: {e}")
1118+
print(f"[CLAIM] Discovery/claim error: {e}")
11261119

1127-
await asyncio.sleep(120)
1120+
# Run every 5 minutes — frequent enough to recycle liquidity promptly
1121+
# after 15-min markets resolve, without hammering the RPC
1122+
await asyncio.sleep(300)
11281123

11291124
# ------------------------------------------------------------------
11301125
# Main trading loop (fast poll)

0 commit comments

Comments
 (0)