package polymarket
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>
OCaml client library for the Polymarket prediction market API
Install
dune-project
Dependency
Authors
Maintainers
Sources
0.2.0.tar.gz
md5=4eb4c5d2f63ff081c9713d90be5a51b2
sha512=0e3de0c9b40683e09ab8f9f966a44784ef1b9b482c3eefef84104a7e8042c92f1d79893ee9588b24fa3d0decaed7f365509f4d1c23c66ce8328efb64e721f276
doc/src/polymarket.clob/client.ml.html
Source file client.ml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293(** Typestate-authenticated HTTP client for the Polymarket CLOB API. See {!Client_typestate} for documentation. *) module H = Polymarket_http.Client module B = Polymarket_http.Builder module J = Polymarket_http.Json module Auth = Polymarket_common.Auth module Crypto = Polymarket_common.Crypto open Types let default_base_url = "https://clob.polymarket.com" (** {1 Client Types} *) type unauthed = { http : H.t } type l1 = { http : H.t; private_key : Crypto.private_key; address : string } type l2 = { http : H.t; private_key : Crypto.private_key; address : string; credentials : Auth.credentials; } (** {1 Public Endpoints Functor} Shared implementation for public endpoints across all auth levels. *) module type HAS_HTTP = sig type t val http : t -> H.t end module Make_public (M : HAS_HTTP) = struct let get_order_book t ~token_id () = B.new_get (M.http t) "/book" |> B.query_param "token_id" token_id |> B.fetch_json ~expected_fields:Types.yojson_fields_of_order_book_summary ~context:"order_book_summary" order_book_summary_of_yojson let get_order_books t ~token_ids () = B.new_post (M.http t) "/books" |> B.with_body (J.list_single_field "token_id" token_ids) |> B.fetch_json_list ~expected_fields:Types.yojson_fields_of_order_book_summary ~context:"order_book_summary" order_book_summary_of_yojson let get_price t ~token_id ~side () = B.new_get (M.http t) "/price" |> B.query_param "token_id" token_id |> B.query_param "side" (Side.to_string side) |> B.fetch_json ~expected_fields:Types.yojson_fields_of_price_response ~context:"price_response" price_response_of_yojson let get_midpoint t ~token_id () = B.new_get (M.http t) "/midpoint" |> B.query_param "token_id" token_id |> B.fetch_json ~expected_fields:Types.yojson_fields_of_midpoint_response ~context:"midpoint_response" midpoint_response_of_yojson let get_prices t ~requests () = B.new_post (M.http t) "/prices" |> B.with_body (J.list (fun (token_id, side) -> J.obj [ ("token_id", J.string token_id); ("side", J.string (Side.to_string side)); ]) requests) |> B.fetch_json prices_response_of_yojson let get_spreads t ~token_ids () = let body = J.list_single_field "token_id" token_ids in B.new_post (M.http t) "/spreads" |> B.with_body body |> B.fetch_json spreads_response_of_yojson let get_price_history t ~market ?start_ts ?end_ts ?interval ?fidelity () = B.new_get (M.http t) "/prices-history" |> B.query_param "market" market |> B.query_option "startTs" string_of_int start_ts |> B.query_option "endTs" string_of_int end_ts |> B.query_option "interval" Interval.to_string interval |> B.query_option "fidelity" string_of_int fidelity |> B.fetch_json ~expected_fields:Types.yojson_fields_of_price_history ~context:"price_history" price_history_of_yojson end (** {1 Unauthenticated Client} *) module Unauthed = struct type t = unauthed include Make_public (struct type t = unauthed let http (t : t) = t.http end) let create ?(base_url = default_base_url) ~sw ~net ~rate_limiter () = let http = H.create ~base_url ~sw ~net ~rate_limiter () in ({ http } : t) end (** {1 L1-Authenticated Client} *) module L1 = struct type t = l1 let create ?(base_url = default_base_url) ~sw ~net ~rate_limiter ~private_key () = let http = H.create ~base_url ~sw ~net ~rate_limiter () in let address = Crypto.private_key_to_address private_key in ({ http; private_key; address } : t) let address (t : t) = t.address include Make_public (struct type t = l1 let http (t : t) = t.http end) let create_api_key (t : t) ~nonce = B.new_post t.http "/auth/api-key" |> B.with_body "{}" |> B.with_l1_auth ~private_key:t.private_key ~address:t.address ~nonce |> B.fetch_json Auth.api_key_response_of_yojson let derive_api_key (t : t) ~nonce = match B.new_get t.http "/auth/derive-api-key" |> B.with_l1_auth ~private_key:t.private_key ~address:t.address ~nonce |> B.fetch_json Auth.api_key_response_of_yojson with | Ok resp -> let credentials = Auth.credentials_of_api_key_response resp in let l2_client : l2 = { http = t.http; private_key = t.private_key; address = t.address; credentials; } in Ok (l2_client, resp) | Error e -> Error e end (** {1 L2-Authenticated Client} *) module L2 = struct type t = l2 let create ?(base_url = default_base_url) ~sw ~net ~rate_limiter ~private_key ~credentials () = let http = H.create ~base_url ~sw ~net ~rate_limiter () in let address = Crypto.private_key_to_address private_key in ({ http; private_key; address; credentials } : t) let address (t : t) = t.address let credentials (t : t) = t.credentials include Make_public (struct type t = l2 let http (t : t) = t.http end) let create_api_key (t : t) ~nonce = B.new_post t.http "/auth/api-key" |> B.with_body "{}" |> B.with_l1_auth ~private_key:t.private_key ~address:t.address ~nonce |> B.fetch_json Auth.api_key_response_of_yojson let delete_api_key (t : t) = B.new_delete t.http "/auth/api-key" |> B.with_l2_auth ~credentials:t.credentials ~address:t.address |> B.fetch_unit let get_api_keys (t : t) = B.new_get t.http "/auth/api-keys" |> B.with_l2_auth ~credentials:t.credentials ~address:t.address |> B.fetch_json_list (fun json -> match json with | `String s -> s | _ -> failwith "Expected string in API keys list") let create_order (t : t) ~order ~owner ~order_type () = B.new_post t.http "/order" |> B.with_body (J.body (J.obj [ ("order", yojson_of_signed_order order); ("owner", J.string owner); ("orderType", J.string (Order_type.to_string order_type)); ])) |> B.with_l2_auth ~credentials:t.credentials ~address:t.address |> B.fetch_json ~expected_fields:Types.yojson_fields_of_create_order_response ~context:"create_order_response" create_order_response_of_yojson let create_orders (t : t) ~orders () = B.new_post t.http "/orders" |> B.with_body (J.list (fun (order, owner, order_type) -> J.obj [ ("order", yojson_of_signed_order order); ("owner", J.string owner); ("orderType", J.string (Order_type.to_string order_type)); ]) orders) |> B.with_l2_auth ~credentials:t.credentials ~address:t.address |> B.fetch_json_list ~expected_fields:Types.yojson_fields_of_create_order_response ~context:"create_order_response" create_order_response_of_yojson let get_order (t : t) ~order_id () = B.new_get t.http ("/data/order/" ^ order_id) |> B.with_l2_auth ~credentials:t.credentials ~address:t.address |> B.fetch_json ~expected_fields:Types.yojson_fields_of_open_order ~context:"open_order" open_order_of_yojson let get_orders (t : t) ?market ?asset_id () = B.new_get t.http "/data/orders" |> B.with_l2_auth ~credentials:t.credentials ~address:t.address |> B.query_add "market" market |> B.query_add "asset_id" asset_id |> B.fetch_json_list ~expected_fields:Types.yojson_fields_of_open_order ~context:"open_order" open_order_of_yojson let cancel_order (t : t) ~order_id () = B.new_delete t.http "/order" |> B.with_l2_auth ~credentials:t.credentials ~address:t.address |> B.query_param "orderID" order_id |> B.fetch_json cancel_response_of_yojson let cancel_orders (t : t) ~order_ids () = B.new_delete t.http "/orders" |> B.with_l2_auth ~credentials:t.credentials ~address:t.address |> B.query_each "orderIDs" Fun.id (Some order_ids) |> B.fetch_json cancel_response_of_yojson let cancel_all (t : t) () = B.new_delete t.http "/cancel-all" |> B.with_l2_auth ~credentials:t.credentials ~address:t.address |> B.fetch_json cancel_response_of_yojson let cancel_market_orders (t : t) ?market ?asset_id () = B.new_delete t.http "/cancel-market-orders" |> B.with_l2_auth ~credentials:t.credentials ~address:t.address |> B.query_add "market" market |> B.query_add "asset_id" asset_id |> B.fetch_json cancel_response_of_yojson let get_trades (t : t) ?id ?taker ?maker ?market ?before ?after () = B.new_get t.http "/data/trades" |> B.with_l2_auth ~credentials:t.credentials ~address:t.address |> B.query_add "id" id |> B.query_add "taker" taker |> B.query_add "maker" maker |> B.query_add "market" market |> B.query_add "before" before |> B.query_add "after" after |> B.fetch_json_list ~expected_fields:Types.yojson_fields_of_clob_trade ~context:"clob_trade" clob_trade_of_yojson end (** {1 State Transitions} *) let upgrade_to_l1 (t : unauthed) ~private_key : l1 = let address = Crypto.private_key_to_address private_key in { http = t.http; private_key; address } let upgrade_to_l2 (t : l1) ~credentials : l2 = { http = t.http; private_key = t.private_key; address = t.address; credentials; } let l2_to_l1 (t : l2) : l1 = { http = t.http; private_key = t.private_key; address = t.address } let l2_to_unauthed (t : l2) : unauthed = { http = t.http } let l1_to_unauthed (t : l1) : unauthed = { http = t.http }
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>