-
Notifications
You must be signed in to change notification settings - Fork 293
Expand file tree
/
Copy pathauth.rs
More file actions
165 lines (145 loc) · 5.28 KB
/
auth.rs
File metadata and controls
165 lines (145 loc) · 5.28 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
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
use std::str::FromStr;
use alloy::primitives::B256;
use alloy::providers::{Provider, ProviderBuilder};
use anyhow::{Context, Result, bail};
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::types::Address;
use polymarket_client_sdk::{POLYGON, clob};
use crate::config::{self, SignerType};
use crate::cwp::{CwpSigner, PolySigner};
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<PolySigner> {
// CLI flag or env var always uses local signer
let (key, _source) = config::resolve_key(private_key)?;
if let Some(key) = key {
let signer = LocalSigner::from_str(&key)
.context("Invalid private key")?
.with_chain_id(Some(POLYGON));
return Ok(PolySigner::Local(signer));
}
// Check config for CWP
if let Some(cfg) = config::load_config()? {
if cfg.signer_type == SignerType::Cwp {
let provider = cfg
.cwp_provider
.context("CWP provider not configured")?;
let address: alloy::primitives::Address = cfg
.cwp_address
.context("CWP address not configured")?
.parse()
.context("Invalid CWP address in config")?;
let signer = CwpSigner::new(&provider, address, Some(POLYGON));
return Ok(PolySigner::Cwp(signer));
}
}
bail!("{}", config::NO_WALLET_MSG)
}
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(|| {
// Check if CWP wallet is configured — give a better error message
if let Some(cfg) = config::load_config().ok().flatten() {
if cfg.signer_type == SignerType::Cwp {
return anyhow::anyhow!(
"CTF operations require a local wallet. Use `polymarket wallet` to configure one."
);
}
}
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")
}
pub async fn send_and_confirm_cwp_tx(
cwp_signer: &CwpSigner,
provider: &(impl Provider + Sync),
to: Address,
calldata: Vec<u8>,
) -> Result<B256> {
// Send via CWP wallet (wallet handles gas estimation)
let tx_hash = cwp_signer
.send_transaction(to, calldata, alloy::primitives::U256::ZERO, None)
.await
.context("CWP send-transaction failed")?;
// Wait for on-chain confirmation (poll every 2s, timeout at 2 min)
for i in 0..60 {
if i > 0 {
tokio::time::sleep(std::time::Duration::from_secs(2)).await;
}
if let Some(receipt) = provider.get_transaction_receipt(tx_hash).await? {
if !receipt.status() {
bail!("Transaction {tx_hash} reverted on-chain");
}
return Ok(tx_hash);
}
}
bail!("Transaction {tx_hash} not confirmed after 2 minutes")
}
#[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);
}
}