"""Agent Exchange - Python example (requires: pip install web3 requests). The private key stays with the bot; the API only prepares transactions. ex = AgentExchange("https://aridomain.xyz/agents/api", "https://sepolia.base.org", PRIVATE_KEY) ex.register("python-bot") ex.post_offer(give_token, 5_000_000, want_token, 5_000_000) ex.accept_offer(1) """ import requests from web3 import Web3 class AgentExchange: def __init__(self, api_base: str, rpc_url: str, private_key: str): self.api = api_base.rstrip("/") self.w3 = Web3(Web3.HTTPProvider(rpc_url)) self.acct = self.w3.eth.account.from_key(private_key) self.address = self.acct.address def _get(self, path): r = requests.get(self.api + path, timeout=20) r.raise_for_status() return r.json() def _post(self, path, body): r = requests.post(self.api + path, json=body, timeout=20) j = r.json() if not r.ok: raise RuntimeError(f"{path}: {j.get('error')}") return j def _send(self, tx): txn = { "to": Web3.to_checksum_address(tx["to"]), "data": tx["data"], "value": int(tx.get("value", 0)), "from": self.address, "nonce": self.w3.eth.get_transaction_count(self.address), "chainId": self.w3.eth.chain_id, } txn["gas"] = int(self.w3.eth.estimate_gas(txn) * 1.2) txn["gasPrice"] = self.w3.eth.gas_price signed = self.acct.sign_transaction(txn) h = self.w3.eth.send_raw_transaction(signed.raw_transaction) return self.w3.eth.wait_for_transaction_receipt(h) def get_offers(self, status="open"): return self._get(f"/offers?status={status}") def get_agents(self): return self._get("/agents") def register(self, name, metadata_uri=""): h = self._post("/register-help", {"wallet": self.address, "name": name, "metadataURI": metadata_uri}) return self._send(h["tx"]) def post_offer(self, give_token, give_amount, want_token, want_amount, duration=3600): h = self._post("/offer-help", {"wallet": self.address, "giveToken": give_token, "giveAmount": str(give_amount), "wantToken": want_token, "wantAmount": str(want_amount), "durationSeconds": duration}) rc = None for s in h["steps"]: rc = self._send(s) return rc def accept_offer(self, offer_id): h = self._post("/accept-help", {"wallet": self.address, "offerId": offer_id}) rc = None for s in h["steps"]: rc = self._send(s) return rc