-
Notifications
You must be signed in to change notification settings - Fork 322
Expand file tree
/
Copy pathauth.rs
More file actions
130 lines (112 loc) · 4.22 KB
/
auth.rs
File metadata and controls
130 lines (112 loc) · 4.22 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
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
use std::str::FromStr;
use alloy::primitives::Address;
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, derive_proxy_wallet};
use crate::config;
const DEFAULT_RPC_URL: &str = "https://polygon.drpc.org";
fn rpc_url() -> String {
std::env::var("POLYMARKET_RPC_URL").unwrap_or_else(|_| DEFAULT_RPC_URL.to_string())
}
fn parse_signature_type(s: &str) -> SignatureType {
match s {
config::DEFAULT_SIGNATURE_TYPE => 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")
}
/// Resolve the wallet address that owns positions and allowances.
///
/// For `eoa` signature type, returns the EOA address directly.
/// For `proxy`, returns the derived proxy wallet address.
/// For `gnosis-safe`, returns the derived Gnosis Safe address.
pub fn resolve_wallet_address(
private_key: Option<&str>,
signature_type_flag: Option<&str>,
) -> Result<Address> {
let signer = resolve_signer(private_key)?;
let eoa = polymarket_client_sdk::auth::Signer::address(&signer);
let sig_type = parse_signature_type(&config::resolve_signature_type(signature_type_flag)?);
match sig_type {
SignatureType::Eoa => Ok(eoa),
SignatureType::Proxy => derive_proxy_wallet(eoa, POLYGON)
.context("Failed to derive proxy wallet address for Polygon"),
SignatureType::GnosisSafe => polymarket_client_sdk::derive_safe_wallet(eoa, POLYGON)
.context("Failed to derive Gnosis Safe wallet address for Polygon"),
_ => Ok(eoa),
}
}
#[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);
}
}