Skip to content

Commit e1c021f

Browse files
authored
Merge pull request #13 from Polymarket/refactoring
refactor: use sdk-native types, improve error handling & modularize output
2 parents 3ba646b + eb44c20 commit e1c021f

37 files changed

Lines changed: 2145 additions & 2477 deletions

src/auth.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ use polymarket_client_sdk::{POLYGON, clob};
99

1010
use crate::config;
1111

12-
pub const RPC_URL: &str = "https://polygon.drpc.org";
12+
const DEFAULT_RPC_URL: &str = "https://polygon.drpc.org";
13+
14+
fn rpc_url() -> String {
15+
std::env::var("POLYMARKET_RPC_URL").unwrap_or_else(|_| DEFAULT_RPC_URL.to_string())
16+
}
1317

1418
fn parse_signature_type(s: &str) -> SignatureType {
1519
match s {
@@ -22,7 +26,7 @@ fn parse_signature_type(s: &str) -> SignatureType {
2226
pub fn resolve_signer(
2327
private_key: Option<&str>,
2428
) -> Result<impl polymarket_client_sdk::auth::Signer> {
25-
let (key, _) = config::resolve_key(private_key);
29+
let (key, _) = config::resolve_key(private_key)?;
2630
let key = key.ok_or_else(|| anyhow::anyhow!("{}", config::NO_WALLET_MSG))?;
2731
LocalSigner::from_str(&key)
2832
.context("Invalid private key")
@@ -41,7 +45,7 @@ pub async fn authenticate_with_signer(
4145
signer: &(impl polymarket_client_sdk::auth::Signer + Sync),
4246
signature_type_flag: Option<&str>,
4347
) -> Result<clob::Client<Authenticated<Normal>>> {
44-
let sig_type = parse_signature_type(&config::resolve_signature_type(signature_type_flag));
48+
let sig_type = parse_signature_type(&config::resolve_signature_type(signature_type_flag)?);
4549

4650
clob::Client::default()
4751
.authentication_builder(signer)
@@ -53,22 +57,22 @@ pub async fn authenticate_with_signer(
5357

5458
pub async fn create_readonly_provider() -> Result<impl alloy::providers::Provider + Clone> {
5559
ProviderBuilder::new()
56-
.connect(RPC_URL)
60+
.connect(&rpc_url())
5761
.await
5862
.context("Failed to connect to Polygon RPC")
5963
}
6064

6165
pub async fn create_provider(
6266
private_key: Option<&str>,
6367
) -> Result<impl alloy::providers::Provider + Clone> {
64-
let (key, _) = config::resolve_key(private_key);
68+
let (key, _) = config::resolve_key(private_key)?;
6569
let key = key.ok_or_else(|| anyhow::anyhow!("{}", config::NO_WALLET_MSG))?;
6670
let signer = LocalSigner::from_str(&key)
6771
.context("Invalid private key")?
6872
.with_chain_id(Some(POLYGON));
6973
ProviderBuilder::new()
7074
.wallet(signer)
71-
.connect(RPC_URL)
75+
.connect(&rpc_url())
7276
.await
7377
.context("Failed to connect to Polygon RPC with wallet")
7478
}

src/commands/approve.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use crate::auth;
1212
use crate::output::OutputFormat;
1313
use crate::output::approve::{ApprovalStatus, print_approval_status, print_tx_result};
1414

15+
/// Polygon USDC (same address as `USDC_ADDRESS_STR`; `address!` requires a literal).
1516
const USDC_ADDRESS: Address = address!("0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174");
1617

1718
sol! {
@@ -39,7 +40,7 @@ pub enum ApproveCommand {
3940
/// Check current contract approvals for a wallet
4041
Check {
4142
/// Wallet address to check (defaults to configured wallet)
42-
address: Option<String>,
43+
address: Option<Address>,
4344
},
4445
/// Approve all required contracts for trading (sends on-chain transactions)
4546
Set,
@@ -82,18 +83,18 @@ pub async fn execute(
8283
private_key: Option<&str>,
8384
) -> Result<()> {
8485
match args.command {
85-
ApproveCommand::Check { address } => check(address.as_deref(), private_key, output).await,
86+
ApproveCommand::Check { address } => check(address, private_key, output).await,
8687
ApproveCommand::Set => set(private_key, output).await,
8788
}
8889
}
8990

9091
async fn check(
91-
address_arg: Option<&str>,
92+
address_arg: Option<Address>,
9293
private_key: Option<&str>,
9394
output: OutputFormat,
9495
) -> Result<()> {
9596
let owner: Address = if let Some(addr) = address_arg {
96-
super::parse_address(addr)?
97+
addr
9798
} else {
9899
let signer = auth::resolve_signer(private_key)?;
99100
polymarket_client_sdk::auth::Signer::address(&signer)

src/commands/bridge.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
use super::parse_address;
2-
use crate::output::OutputFormat;
3-
use crate::output::bridge::{print_deposit, print_status, print_supported_assets};
41
use anyhow::Result;
52
use clap::{Args, Subcommand};
63
use polymarket_client_sdk::bridge::{
74
self,
85
types::{DepositRequest, StatusRequest},
96
};
107

8+
use crate::output::OutputFormat;
9+
use crate::output::bridge::{print_deposit, print_status, print_supported_assets};
10+
1111
#[derive(Args)]
1212
pub struct BridgeArgs {
1313
#[command(subcommand)]
@@ -19,7 +19,7 @@ pub enum BridgeCommand {
1919
/// Get deposit addresses for a wallet (EVM, Solana, Bitcoin)
2020
Deposit {
2121
/// Polymarket wallet address (0x...)
22-
address: String,
22+
address: polymarket_client_sdk::types::Address,
2323
},
2424

2525
/// List supported chains and tokens for deposits
@@ -39,9 +39,7 @@ pub async fn execute(
3939
) -> Result<()> {
4040
match args.command {
4141
BridgeCommand::Deposit { address } => {
42-
let request = DepositRequest::builder()
43-
.address(parse_address(&address)?)
44-
.build();
42+
let request = DepositRequest::builder().address(address).build();
4543

4644
let response = client.deposit(&request).await?;
4745
print_deposit(&response, &output)?;

0 commit comments

Comments
 (0)