polyrel 0.2.1

Unofficial Polymarket relayer client
Documentation
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
//! Core types shared across the crate.

use std::borrow::Cow;

use alloy_primitives::{Address, B256};
use serde::{Deserialize, Serialize};
use url::Url;

/// Configuration for contract addresses and relayer defaults.
pub struct Config {
	base_url: Url,
	chain_id: u64,
	ctf_exchange: Address,
	neg_risk_ctf_exchange: Address,
	neg_risk_adapter: Address,
	conditional_tokens: Address,
	usdc_e: Address,
	proxy_wallet_factory: Address,
	relay_hub: Address,
	safe_factory: Address,
	safe_multisend: Address,
	safe_init_code_hash: B256,
	proxy_init_code_hash: B256,
}

#[bon::bon]
impl Config {
	/// Build a new configuration; all fields default to Polygon mainnet values.
	#[builder]
	pub fn new(
		base_url: Option<Cow<'static, str>>,
		chain_id: Option<u64>,
		ctf_exchange: Option<Address>,
		neg_risk_ctf_exchange: Option<Address>,
		neg_risk_adapter: Option<Address>,
		conditional_tokens: Option<Address>,
		usdc_e: Option<Address>,
		proxy_wallet_factory: Option<Address>,
		relay_hub: Option<Address>,
		safe_factory: Option<Address>,
		safe_multisend: Option<Address>,
		safe_init_code_hash: Option<B256>,
		proxy_init_code_hash: Option<B256>,
	) -> Result<Self, crate::PolyrelError> {
		let url_str = base_url.as_deref().unwrap_or(crate::RELAYER_BASE_URL);
		let mut parsed = Url::parse(url_str)
			.map_err(|e| crate::PolyrelError::Http(Cow::Owned(format!("invalid base URL: {e}"))))?;
		match parsed.scheme() {
			"http" | "https" => {},
			_ => {
				return Err(crate::PolyrelError::Http(Cow::Borrowed(
					"base URL must use http or https scheme",
				)));
			},
		}
		if parsed.query().is_some() {
			return Err(crate::PolyrelError::Http(Cow::Borrowed(
				"base URL must not contain a query string",
			)));
		}
		if parsed.fragment().is_some() {
			return Err(crate::PolyrelError::Http(Cow::Borrowed(
				"base URL must not contain a fragment",
			)));
		}
		let trimmed = parsed.path().trim_end_matches('/').to_owned();
		parsed.set_path(&trimmed);

		Ok(Self {
			base_url: parsed,
			chain_id: chain_id.unwrap_or(crate::CHAIN_ID),
			ctf_exchange: ctf_exchange.unwrap_or(crate::CTF_EXCHANGE),
			neg_risk_ctf_exchange: neg_risk_ctf_exchange.unwrap_or(crate::NEG_RISK_CTF_EXCHANGE),
			neg_risk_adapter: neg_risk_adapter.unwrap_or(crate::NEG_RISK_ADAPTER),
			conditional_tokens: conditional_tokens.unwrap_or(crate::CONDITIONAL_TOKENS),
			usdc_e: usdc_e.unwrap_or(crate::USDC_E),
			proxy_wallet_factory: proxy_wallet_factory.unwrap_or(crate::PROXY_WALLET_FACTORY),
			relay_hub: relay_hub.unwrap_or(crate::RELAY_HUB),
			safe_factory: safe_factory.unwrap_or(crate::SAFE_FACTORY),
			safe_multisend: safe_multisend.unwrap_or(crate::SAFE_MULTISEND),
			safe_init_code_hash: safe_init_code_hash
				.unwrap_or_else(|| crate::SAFE_INIT_CODE_HASH.into()),
			proxy_init_code_hash: proxy_init_code_hash
				.unwrap_or_else(|| crate::PROXY_INIT_CODE_HASH.into()),
		})
	}

	/// Relayer API base URL.
	pub fn base_url(&self) -> &Url {
		&self.base_url
	}

	/// Chain ID.
	pub fn chain_id(&self) -> u64 {
		self.chain_id
	}

	/// CTF Exchange address.
	pub fn ctf_exchange(&self) -> Address {
		self.ctf_exchange
	}

	/// Neg-Risk CTF Exchange address.
	pub fn neg_risk_ctf_exchange(&self) -> Address {
		self.neg_risk_ctf_exchange
	}

	/// Neg-Risk Adapter address.
	pub fn neg_risk_adapter(&self) -> Address {
		self.neg_risk_adapter
	}

	/// Conditional Tokens address.
	pub fn conditional_tokens(&self) -> Address {
		self.conditional_tokens
	}

	/// USDC.e address.
	pub fn usdc_e(&self) -> Address {
		self.usdc_e
	}

	/// Proxy Wallet Factory address.
	pub fn proxy_wallet_factory(&self) -> Address {
		self.proxy_wallet_factory
	}

	/// GSN Relay Hub address.
	pub fn relay_hub(&self) -> Address {
		self.relay_hub
	}

	/// Gnosis Safe Factory address.
	pub fn safe_factory(&self) -> Address {
		self.safe_factory
	}

	/// Safe MultiSend address.
	pub fn safe_multisend(&self) -> Address {
		self.safe_multisend
	}

	/// Safe init code hash for CREATE2 derivation.
	pub fn safe_init_code_hash(&self) -> B256 {
		self.safe_init_code_hash
	}

	/// Proxy init code hash for CREATE2 derivation.
	pub fn proxy_init_code_hash(&self) -> B256 {
		self.proxy_init_code_hash
	}
}

/// Transaction lifecycle state.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum TransactionState {
	/// Queued for processing.
	#[serde(rename = "STATE_NEW")]
	New,

	/// Submitted to blockchain.
	#[serde(rename = "STATE_EXECUTED")]
	Executed,

	/// Included in a block.
	#[serde(rename = "STATE_MINED")]
	Mined,

	/// Finalized (~30 blocks).
	#[serde(rename = "STATE_CONFIRMED")]
	Confirmed,

	/// Invalid transaction.
	#[serde(rename = "STATE_INVALID")]
	Invalid,

	/// Execution failed.
	#[serde(rename = "STATE_FAILED")]
	Failed,
}

/// Wallet type for relayer transactions.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum WalletType {
	/// Gnosis Safe wallet.
	#[serde(rename = "SAFE")]
	Safe,

	/// Polymarket proxy wallet.
	#[serde(rename = "PROXY")]
	Proxy,

	/// Safe creation (deployment).
	#[serde(rename = "SAFE-CREATE")]
	SafeCreate,
}

/// Safe transaction operation type.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum OperationType {
	/// Standard call.
	Call = 0,

	/// Delegate call (used by MultiSend).
	DelegateCall = 1,
}

impl OperationType {
	/// Raw u8 value.
	pub fn as_u8(self) -> u8 {
		self as u8
	}
}

/// Parameters for the relayer transaction signature.
///
/// Different transaction types use different subsets of fields.
/// Safe transactions use the gas fields. Safe-create uses
/// payment fields. Proxy transactions use relay fields.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SignatureParams {
	/// Gas price (Safe transactions).
	#[serde(skip_serializing_if = "Option::is_none")]
	pub gas_price: Option<String>,

	/// Operation type: 0 = Call, 1 = DelegateCall (Safe transactions).
	#[serde(skip_serializing_if = "Option::is_none")]
	pub operation: Option<String>,

	/// Gas allocated for the Safe transaction.
	#[serde(skip_serializing_if = "Option::is_none")]
	pub safe_txn_gas: Option<String>,

	/// Base gas overhead (Safe transactions).
	#[serde(skip_serializing_if = "Option::is_none")]
	pub base_gas: Option<String>,

	/// Token used for gas payment (Safe transactions).
	#[serde(skip_serializing_if = "Option::is_none")]
	pub gas_token: Option<String>,

	/// Address receiving gas refund (Safe transactions).
	#[serde(skip_serializing_if = "Option::is_none")]
	pub refund_receiver: Option<String>,

	/// Payment token (Safe-create transactions).
	#[serde(skip_serializing_if = "Option::is_none")]
	pub payment_token: Option<String>,

	/// Payment amount (Safe-create transactions).
	#[serde(skip_serializing_if = "Option::is_none")]
	pub payment: Option<String>,

	/// Payment receiver (Safe-create transactions).
	#[serde(skip_serializing_if = "Option::is_none")]
	pub payment_receiver: Option<String>,

	/// Relayer fee (Proxy transactions).
	#[serde(skip_serializing_if = "Option::is_none")]
	pub relayer_fee: Option<String>,

	/// Gas limit (Proxy transactions).
	#[serde(skip_serializing_if = "Option::is_none")]
	pub gas_limit: Option<String>,

	/// Relay hub address (Proxy transactions).
	#[serde(skip_serializing_if = "Option::is_none")]
	pub relay_hub: Option<String>,

	/// Relay address (Proxy transactions).
	#[serde(skip_serializing_if = "Option::is_none")]
	pub relay: Option<String>,
}

impl SignatureParams {
	/// Params for a Safe transaction.
	pub fn safe(operation: u8) -> Self {
		Self {
			gas_price: Some("0".to_owned()),
			operation: Some(operation.to_string()),
			safe_txn_gas: Some("0".to_owned()),
			base_gas: Some("0".to_owned()),
			gas_token: Some(Address::ZERO.to_string()),
			refund_receiver: Some(Address::ZERO.to_string()),
			..Default::default()
		}
	}

	/// Params for a Safe-create (deployment) transaction.
	pub fn safe_create() -> Self {
		Self {
			payment_token: Some(Address::ZERO.to_string()),
			payment: Some("0".to_owned()),
			payment_receiver: Some(Address::ZERO.to_string()),
			..Default::default()
		}
	}

	/// Params for a Proxy transaction.
	pub fn proxy(
		gas_price: Cow<'static, str>,
		gas_limit: Cow<'static, str>,
		relay_hub: Address,
		relay: Address,
	) -> Self {
		Self {
			gas_price: Some(gas_price.into_owned()),
			gas_limit: Some(gas_limit.into_owned()),
			relayer_fee: Some("0".to_owned()),
			relay_hub: Some(relay_hub.to_string()),
			relay: Some(relay.to_string()),
			..Default::default()
		}
	}
}

/// Request body for `POST /submit`.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SubmitRequest {
	/// Transaction type.
	#[serde(rename = "type")]
	pub wallet_type: WalletType,

	/// Signer address.
	pub from: String,

	/// Target contract address.
	pub to: String,

	/// User's proxy wallet address.
	#[serde(skip_serializing_if = "Option::is_none")]
	pub proxy_wallet: Option<String>,

	/// Hex-encoded transaction data.
	pub data: String,

	/// Transaction nonce.
	#[serde(skip_serializing_if = "Option::is_none")]
	pub nonce: Option<String>,

	/// Hex-encoded signature.
	pub signature: String,

	/// Signature parameters.
	pub signature_params: SignatureParams,

	/// Optional metadata.
	#[serde(skip_serializing_if = "Option::is_none")]
	pub metadata: Option<String>,
}

#[bon::bon]
impl SubmitRequest {
	/// Build a new submit request.
	#[builder]
	pub fn new(
		wallet_type: WalletType,
		from: Cow<'static, str>,
		to: Cow<'static, str>,
		proxy_wallet: Option<Cow<'static, str>>,
		data: Cow<'static, str>,
		nonce: Option<Cow<'static, str>>,
		signature: Cow<'static, str>,
		signature_params: SignatureParams,
		metadata: Option<Cow<'static, str>>,
	) -> Self {
		Self {
			wallet_type,
			from: from.into_owned(),
			to: to.into_owned(),
			proxy_wallet: proxy_wallet.map(Cow::into_owned),
			data: data.into_owned(),
			nonce: nonce.map(Cow::into_owned),
			signature: signature.into_owned(),
			signature_params,
			metadata: metadata.map(Cow::into_owned),
		}
	}
}

/// Response from `POST /submit`.
#[derive(Debug, Clone, Deserialize)]
pub struct SubmitResponse {
	/// Unique transaction identifier.
	#[serde(rename = "transactionID")]
	pub transaction_id: String,

	/// Current state.
	pub state: String,

	/// On-chain transaction hash.
	#[serde(default)]
	pub hash: Option<String>,

	/// On-chain transaction hash (alias).
	#[serde(rename = "transactionHash", default)]
	pub transaction_hash: Option<String>,
}

/// Full transaction record from `GET /transaction`.
#[derive(Debug, Clone, Deserialize)]
pub struct RelayerTransaction {
	/// Unique transaction identifier.
	#[serde(rename = "transactionID")]
	pub transaction_id: String,

	/// On-chain transaction hash.
	#[serde(rename = "transactionHash", default)]
	pub transaction_hash: Option<String>,

	/// Sender address.
	#[serde(default)]
	pub from: Option<String>,

	/// Target contract address.
	#[serde(default)]
	pub to: Option<String>,

	/// Proxy/Safe address.
	#[serde(rename = "proxyAddress", default)]
	pub proxy_address: Option<String>,

	/// Transaction data.
	#[serde(default)]
	pub data: Option<String>,

	/// Transaction nonce.
	#[serde(default)]
	pub nonce: Option<String>,

	/// ETH value.
	#[serde(default)]
	pub value: Option<String>,

	/// Current state.
	pub state: String,

	/// Transaction type.
	#[serde(rename = "type", default)]
	pub transaction_type: Option<String>,

	/// Metadata.
	#[serde(default)]
	pub metadata: Option<String>,

	/// Signature.
	#[serde(default)]
	pub signature: Option<String>,

	/// Owner / API key owner.
	#[serde(default)]
	pub owner: Option<String>,

	/// Creation timestamp.
	#[serde(rename = "createdAt", default)]
	pub created_at: Option<String>,

	/// Last update timestamp.
	#[serde(rename = "updatedAt", default)]
	pub updated_at: Option<String>,
}

/// Response from `GET /relay-payload`.
#[derive(Debug, Clone, Deserialize)]
pub struct RelayerInfo {
	/// Relayer's address.
	pub address: String,

	/// Current nonce.
	pub nonce: String,
}

/// Response from `GET /deployed`.
#[derive(Debug, Clone, Deserialize)]
pub struct DeployedResponse {
	/// Whether the Safe is deployed.
	pub deployed: bool,
}

#[cfg(test)]
mod tests {
	use super::*;

	#[test]
	fn config_default_succeeds() {
		// Act
		let config = Config::builder().build();

		// Assert
		assert!(config.is_ok());
	}

	#[test]
	fn config_rejects_ftp_scheme() {
		// Act
		let result = Config::builder().base_url("ftp://example.com".into()).build();

		// Assert
		assert!(result.is_err());
	}

	#[test]
	fn config_rejects_mailto_scheme() {
		// Act
		let result = Config::builder().base_url("mailto:test@example.com".into()).build();

		// Assert
		assert!(result.is_err());
	}

	#[test]
	fn config_rejects_query_string() {
		// Act
		let result = Config::builder().base_url("https://example.com?key=val".into()).build();

		// Assert
		assert!(result.is_err());
	}

	#[test]
	fn config_rejects_fragment() {
		// Act
		let result = Config::builder().base_url("https://example.com#frag".into()).build();

		// Assert
		assert!(result.is_err());
	}

	#[test]
	fn config_normalizes_single_trailing_slash() {
		// Arrange & Act
		let config = Config::builder().base_url("https://example.com/api/".into()).build().unwrap();

		// Assert
		assert!(!config.base_url().as_str().ends_with('/'));
	}

	#[test]
	fn config_normalizes_multiple_trailing_slashes() {
		// Arrange & Act
		let config =
			Config::builder().base_url("https://example.com/api///".into()).build().unwrap();

		// Assert
		assert_eq!(config.base_url().path(), "/api");
	}

	#[test]
	fn config_accepts_valid_https_url() {
		// Arrange & Act
		let config =
			Config::builder().base_url("https://relayer.example.com".into()).build().unwrap();

		// Assert
		assert_eq!(config.base_url().scheme(), "https");
	}

	#[test]
	fn signature_params_safe_sets_gas_defaults() {
		// Act
		let params = SignatureParams::safe(0);

		// Assert
		assert_eq!(params.gas_price.as_deref(), Some("0"));
		assert_eq!(params.operation.as_deref(), Some("0"));
		assert_eq!(params.safe_txn_gas.as_deref(), Some("0"));
		assert_eq!(params.base_gas.as_deref(), Some("0"));
		assert!(params.payment_token.is_none());
	}

	#[test]
	fn signature_params_safe_create_sets_payment_defaults() {
		// Act
		let params = SignatureParams::safe_create();

		// Assert
		assert!(params.payment_token.is_some());
		assert_eq!(params.payment.as_deref(), Some("0"));
		assert!(params.payment_receiver.is_some());
		assert!(params.gas_price.is_none());
	}

	#[test]
	fn submit_response_deserializes_transaction_id_field() {
		// Arrange
		let json = r#"{"transactionID":"abc-123","state":"STATE_NEW"}"#;

		// Act
		let resp: SubmitResponse = serde_json::from_str(json).unwrap();

		// Assert
		assert_eq!(resp.transaction_id, "abc-123");
		assert_eq!(resp.state, "STATE_NEW");
	}

	#[test]
	fn relayer_transaction_deserializes_full_payload() {
		// Arrange
		let json = r#"{
			"transactionID": "tx-1",
			"transactionHash": "0xabc",
			"from": "0x1234",
			"to": "0x5678",
			"proxyAddress": "0xproxy",
			"state": "STATE_MINED",
			"signature": "0xsig",
			"owner": "owner-uuid"
		}"#;

		// Act
		let txn: RelayerTransaction = serde_json::from_str(json).unwrap();

		// Assert
		assert_eq!(txn.transaction_id, "tx-1");
		assert_eq!(txn.signature.as_deref(), Some("0xsig"));
		assert_eq!(txn.owner.as_deref(), Some("owner-uuid"));
	}

	#[test]
	fn wallet_type_serializes_safe_create_with_hyphen() {
		// Act
		let json = serde_json::to_string(&WalletType::SafeCreate).unwrap();

		// Assert
		assert_eq!(json, "\"SAFE-CREATE\"");
	}

	#[test]
	fn transaction_state_round_trips() {
		// Arrange
		let json = "\"STATE_CONFIRMED\"";

		// Act
		let state: TransactionState = serde_json::from_str(json).unwrap();

		// Assert
		assert_eq!(state, TransactionState::Confirmed);
		assert_eq!(serde_json::to_string(&state).unwrap(), json);
	}
}