|
33 | 33 |
|
34 | 34 | import argparse |
35 | 35 | import asyncio |
| 36 | +import math |
36 | 37 | import os |
37 | 38 | import re |
38 | 39 | import time |
@@ -197,7 +198,7 @@ def calculate_shares_from_usdc(self, usdc_amount: float, price: int) -> int: |
197 | 198 | return 0 |
198 | 199 | # shares = usdc / (price / 1_000_000) = usdc * 1_000_000 / price |
199 | 200 | # Result in 6 decimals |
200 | | - return int((usdc_amount * 1_000_000 * 1_000_000) / price) |
| 201 | + return math.ceil((usdc_amount * 1_000_000 * 1_000_000) / price) |
201 | 202 |
|
202 | 203 | def get_position_usdc(self, state: AssetState, market_id: str) -> float: |
203 | 204 | """Get current position in USDC for a market.""" |
@@ -240,31 +241,19 @@ def ensure_settlement_approved(self, settlement_address: str) -> None: |
240 | 241 | print(f"Relayer TX: {tx_hash}") |
241 | 242 | print("Waiting for confirmation...") |
242 | 243 |
|
243 | | - # Wait for confirmation |
244 | | - from web3 import Web3 |
245 | | - rpc_urls = { |
246 | | - 137: "https://polygon-bor-rpc.publicnode.com", |
247 | | - 43114: "https://api.avax.network/ext/bc/C/rpc", |
248 | | - 84532: "https://sepolia.base.org", |
249 | | - } |
250 | | - rpc_url = rpc_urls.get(self.client.chain_id) |
251 | | - w3 = Web3(Web3.HTTPProvider(rpc_url)) |
252 | | - |
| 244 | + # Wait for confirmation by polling allowance via API |
253 | 245 | for _ in range(30): |
254 | 246 | try: |
255 | | - receipt = w3.eth.get_transaction_receipt(tx_hash) |
256 | | - if receipt: |
257 | | - if receipt["status"] == 1: |
258 | | - print(f"✓ Max USDC approval confirmed (gasless)") |
259 | | - self.approved_settlements[settlement_address] = 2**256 - 1 |
260 | | - else: |
261 | | - print(f"✗ Transaction failed!") |
| 247 | + allowance = self.client.get_usdc_allowance(spender=settlement_address) |
| 248 | + if allowance >= self.MAX_APPROVAL_THRESHOLD: |
| 249 | + print(f"✓ Max USDC approval confirmed (gasless)") |
| 250 | + self.approved_settlements[settlement_address] = allowance |
262 | 251 | break |
263 | 252 | except Exception: |
264 | 253 | pass |
265 | | - time.sleep(1) |
| 254 | + time.sleep(2) |
266 | 255 | else: |
267 | | - print(f"⚠ Transaction pending (may still confirm)") |
| 256 | + print(f"⚠ Approval pending (may still confirm)") |
268 | 257 | self.approved_settlements[settlement_address] = 2**256 - 1 |
269 | 258 |
|
270 | 259 | except Exception as e: |
@@ -773,26 +762,34 @@ async def claim_resolved_markets(self) -> None: |
773 | 762 | await asyncio.sleep(retry_delay) |
774 | 763 | continue |
775 | 764 |
|
| 765 | + # Collect all resolved markets first |
| 766 | + resolved: list[tuple[str, str, AssetState]] = [] |
776 | 767 | for market_id, contract_address, state in all_traded: |
777 | 768 | try: |
778 | 769 | resolution = self.client.get_resolution(market_id) |
779 | | - if not (resolution and resolution.resolved): |
780 | | - continue |
| 770 | + if resolution and resolution.resolved: |
| 771 | + resolved.append((market_id, contract_address, state)) |
781 | 772 | except Exception: |
782 | 773 | continue |
783 | 774 |
|
784 | | - try: |
785 | | - result = self.client.claim_winnings(contract_address) |
786 | | - tx_hash = result.get("txHash", result.get("tx_hash", "unknown")) |
787 | | - print(f"[{state.asset}] 💰 Claimed winnings from {market_id[:8]}... TX: {tx_hash}") |
| 775 | + if not resolved: |
| 776 | + await asyncio.sleep(retry_delay) |
| 777 | + continue |
| 778 | + |
| 779 | + # Batch claim in one transaction |
| 780 | + market_addresses = [addr for _, addr, _ in resolved] |
| 781 | + try: |
| 782 | + result = self.client.batch_claim_winnings(market_addresses) |
| 783 | + tx_hash = result.get("txHash", result.get("tx_hash", "unknown")) |
| 784 | + print(f"💰 Batch claimed {len(resolved)} markets TX: {tx_hash}") |
| 785 | + for market_id, _, state in resolved: |
788 | 786 | del state.traded_markets[market_id] |
789 | | - except ValueError as e: |
790 | | - if "no winning tokens" in str(e).lower(): |
| 787 | + except ValueError as e: |
| 788 | + if "no winning tokens" in str(e).lower(): |
| 789 | + for market_id, _, state in resolved: |
791 | 790 | del state.traded_markets[market_id] |
792 | | - except Exception as e: |
793 | | - print(f"[{state.asset}] Claim error: {e}") |
794 | | - |
795 | | - await asyncio.sleep(15) |
| 791 | + except Exception as e: |
| 792 | + print(f"Batch claim error: {e}") |
796 | 793 |
|
797 | 794 | except Exception as e: |
798 | 795 | print(f"Claim monitor error: {e}") |
|
0 commit comments