-
Notifications
You must be signed in to change notification settings - Fork 294
Expand file tree
/
Copy pathauth.rs
More file actions
102 lines (87 loc) · 3.04 KB
/
auth.rs
File metadata and controls
102 lines (87 loc) · 3.04 KB
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
use std::str::FromStr;
use alloy::providers::ProviderBuilder;
use anyhow::{Context, Result};
use polymarket_client_sdk::auth::state::Authenticated;
use polymarket_client_sdk::auth::{LocalSigner, Normal, Signer as _};
use polymarket_client_sdk::clob::types::SignatureType;
use polymarket_client_sdk::{POLYGON, clob};
use crate::config;
pub const RPC_URL: &str = "https://polygon.drpc.org";
fn parse_signature_type(s: &str) -> SignatureType {
match s {
"proxy" => SignatureType::Proxy,
"gnosis-safe" => SignatureType::GnosisSafe,
_ => SignatureType::Eoa,
}
}
pub fn resolve_signer(
private_key: Option<&str>,
) -> Result<impl polymarket_client_sdk::auth::Signer> {
let (key, _) = config::resolve_key(private_key);
let key = key.ok_or_else(|| anyhow::anyhow!("{}", config::NO_WALLET_MSG))?;
LocalSigner::from_str(&key)
.context("Invalid private key")
.map(|s| s.with_chain_id(Some(POLYGON)))
}
pub async fn authenticated_clob_client(
private_key: Option<&str>,
signature_type_flag: Option<&str>,
) -> Result<clob::Client<Authenticated<Normal>>> {
let signer = resolve_signer(private_key)?;
authenticate_with_signer(&signer, signature_type_flag).await
}
pub async fn authenticate_with_signer(
signer: &(impl polymarket_client_sdk::auth::Signer + Sync),
signature_type_flag: Option<&str>,
) -> Result<clob::Client<Authenticated<Normal>>> {
let sig_type = parse_signature_type(&config::resolve_signature_type(signature_type_flag));
clob::Client::default()
.authentication_builder(signer)
.signature_type(sig_type)
.authenticate()
.await
.context("Failed to authenticate with Polymarket CLOB")
}
pub async fn create_readonly_provider() -> Result<impl alloy::providers::Provider + Clone> {
ProviderBuilder::new()
.connect(RPC_URL)
.await
.context("Failed to connect to Polygon RPC")
}
pub async fn create_provider(
private_key: Option<&str>,
) -> Result<impl alloy::providers::Provider + Clone> {
let (key, _) = config::resolve_key(private_key);
let key = key.ok_or_else(|| anyhow::anyhow!("{}", config::NO_WALLET_MSG))?;
let signer = LocalSigner::from_str(&key)
.context("Invalid private key")?
.with_chain_id(Some(POLYGON));
ProviderBuilder::new()
.wallet(signer)
.connect(RPC_URL)
.await
.context("Failed to connect to Polygon RPC with wallet")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_signature_type_proxy() {
assert_eq!(parse_signature_type("proxy"), SignatureType::Proxy);
}
#[test]
fn parse_signature_type_gnosis_safe() {
assert_eq!(
parse_signature_type("gnosis-safe"),
SignatureType::GnosisSafe
);
}
#[test]
fn parse_signature_type_eoa() {
assert_eq!(parse_signature_type("eoa"), SignatureType::Eoa);
}
#[test]
fn parse_signature_type_unknown_defaults_to_eoa() {
assert_eq!(parse_signature_type("unknown"), SignatureType::Eoa);
}
}