#!/usr/bin/env python3 """Send one RFQ to the Ivy desk and print the quote. pip install requests eth-account python ivy_desk_rfq.py Fill in the three settings and the order below, then run it. Steps: log in -> send the RFQ -> wait for the quote -> (only if ACCEPT is True) accept it, which books the trade. The API is the same as Monarq's /v1: a client written for Monarq works here by changing API_URL and the key.""" import hashlib, json, os, secrets, time, uuid import requests from eth_account import Account from eth_account.messages import encode_defunct # ---- your settings ----------------------------------------------------------------------------------------------- API_URL = "https://desk.baguette.finance" # Testnet (Arbitrum Sepolia, test USDC). No mainnet yet. KEY_ID = "ivy-desk-..." # your key id, given at registration KEY_PRIVATE = "0x..." # your API private key (keep it in a secret store) # or set DESK_API_URL, DESK_KEY_ID, DESK_API_PRIVATE_KEY in the environment API_URL = os.environ.get("DESK_API_URL") or API_URL KEY_ID = os.environ.get("DESK_KEY_ID") or KEY_ID KEY_PRIVATE = os.environ.get("DESK_API_PRIVATE_KEY") or KEY_PRIVATE # ---- the order --------------------------------------------------------------------------------------------------- ORDER = { "client_rfq_id": f"rfq-{uuid.uuid4().hex[:12]}", # your own id for this request "instrument": "ETH-4DEC26-1800-C", # as on Deribit: UNDERLYING-DDMMMYY-STRIKE-C|P (expiry 08:00 UTC) "quantity": 1, # in coins "side": "buy", } ACCEPT = False # True = accept the quote and book the trade def sign(text): # EIP-191 personal_sign with your API key sig = Account.sign_message(encode_defunct(text=text), KEY_PRIVATE).signature.hex() return sig if sig.startswith("0x") else "0x" + sig def call(method, path, body=None, token=None, key=None, signed=False): data = json.dumps(body).encode() if body is not None else b"" # serialised once: signed and sent as is headers = {"Content-Type": "application/json", "User-Agent": "ivy-desk-client/1.0 (ivy_desk_rfq.py)"} if token: headers["Authorization"] = f"Bearer {token}" if key: headers["Idempotency-Key"] = key # the same key on a retry = nothing done twice if signed: # accept needs a signature on the request too ts = str(int(time.time())) headers.update({"X-Key-Id": KEY_ID, "X-Timestamp": ts, "X-Signature": sign(f"{ts}.{method}.{path}.{hashlib.sha256(data).hexdigest()}")}) r = requests.request(method, API_URL + path, data=data or None, headers=headers, timeout=15) answer = r.json() if r.status_code >= 400: raise SystemExit(f"{method} {path} refused: {r.status_code} {answer.get('code')} - {answer.get('message')}") return answer # 1) log in: a signed challenge gives a token valid 24 hours nonce, ts = secrets.token_hex(16), str(int(time.time())) token = call("POST", "/v1/auth/session", {"key_id": KEY_ID, "nonce": nonce, "timestamp": ts, "signature": sign(f"monarq-session.{KEY_ID}.{nonce}.{ts}")})["token"] # 2) send the RFQ: the desk prices at once, so the answer is the quote (or a decline with its reason) quote = call("POST", "/v1/rfq", ORDER, token, key=ORDER["client_rfq_id"]) if quote["status"] != "quoted": raise SystemExit(f"no quote: {quote['status']} {quote.get('reason_code') or quote.get('reason') or ''}") print(f"Quote {quote['quote_id']}: premium ${float(quote['premium_usd']):,.2f} for {quote['quantity']} " f"{quote['instrument']} at {100 * float(quote['iv']):.1f} vol, firm until {quote['expires_at']}") # 3) accept: books the trade at that premium (signed with your key), then pay the premium on chain if ACCEPT: order_id = f"ord-{uuid.uuid4().hex[:12]}" trade = call("POST", "/v1/accept", {"quote_id": quote["quote_id"], "client_order_id": order_id, "quantity": quote["quantity"], "premium_usd": quote["premium_usd"]}, token, key=order_id, signed=True) pay = trade["premium_payment"] print("Trade booked:", trade["trade_id"]) print(f"Pay {pay['pay_amount']} test USDC from your registered wallet to {pay['pay_to']} before {pay['due_by']}")