Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ The config file (`~/.config/polymarket/config.json`):
{
"private_key": "0x...",
"chain_id": 137,
"signature_type": "proxy"
"signature_type": "poly-1271",
"funder": "0xYOUR_POLYMARKET_DEPOSIT_WALLET"
}
```

Expand All @@ -87,8 +88,27 @@ The config file (`~/.config/polymarket/config.json`):
- `proxy` (default) — uses Polymarket's proxy wallet system
- `eoa` — signs directly with your key
- `gnosis-safe` — for multisig wallets
- `poly-1271` — for Polymarket deposit wallets; requires an explicit funder address

Override per-command with `--signature-type eoa` or via `POLYMARKET_SIGNATURE_TYPE`.
For `poly-1271`, provide the funding wallet with `--funder 0x...` or
`POLYMARKET_FUNDER`. Supplying a funder with another signature type is rejected
to prevent authenticating against the wrong wallet.

For an email/social-login Polymarket account backed by a deposit wallet:

```bash
polymarket wallet import 0xSIGNER_KEY \
--signature-type poly-1271 \
--funder 0xPOLYMARKET_DEPOSIT_WALLET

polymarket clob balance --asset-type collateral
```

`poly-1271` is supported for CLOB V2 authentication, balances, orders, and
trades. Direct on-chain mutations (`approve set` and CTF split/merge/redeem)
are rejected for deposit wallets; perform those operations in the Polymarket
app. `approve check` remains available and checks the configured funder.

### What Needs a Wallet

Expand Down
50 changes: 36 additions & 14 deletions src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ 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 {
fn parse_signature_type(s: &str) -> Result<SignatureType> {
match s {
config::DEFAULT_SIGNATURE_TYPE => SignatureType::Proxy,
"gnosis-safe" => SignatureType::GnosisSafe,
_ => SignatureType::Eoa,
config::DEFAULT_SIGNATURE_TYPE => Ok(SignatureType::Proxy),
"gnosis-safe" => Ok(SignatureType::GnosisSafe),
config::POLY_1271_SIGNATURE_TYPE => Ok(SignatureType::Poly1271),
"eoa" => Ok(SignatureType::Eoa),
_ => Err(anyhow::anyhow!("Unsupported signature type: {s}")),
}
}

Expand All @@ -41,20 +43,32 @@ pub fn resolve_signer(
pub async fn authenticated_clob_client(
private_key: Option<&str>,
signature_type_flag: Option<&str>,
funder_flag: Option<&str>,
) -> Result<clob::Client<Authenticated<Normal>>> {
let signer = resolve_signer(private_key)?;
authenticate_with_signer(&signer, signature_type_flag).await
authenticate_with_signer(&signer, signature_type_flag, funder_flag).await
}

pub async fn authenticate_with_signer(
signer: &(impl polymarket_client_sdk_v2::auth::Signer + Sync),
signature_type_flag: Option<&str>,
funder_flag: Option<&str>,
) -> Result<clob::Client<Authenticated<Normal>>> {
let sig_type = parse_signature_type(&config::resolve_signature_type(signature_type_flag)?);

unauthenticated_clob_client()?
let signature_type = config::resolve_signature_type(signature_type_flag)?;
let sig_type = parse_signature_type(&signature_type)?;
let funder = config::validate_funder_for_signature_type(
&signature_type,
config::resolve_funder(funder_flag)?,
)?;

let mut builder = unauthenticated_clob_client()?
.authentication_builder(signer)
.signature_type(sig_type)
.signature_type(sig_type);
if let Some(funder) = funder {
builder = builder.funder(funder);
}
Comment thread
cursor[bot] marked this conversation as resolved.

builder
.authenticate()
.await
.context("Failed to authenticate with Polymarket CLOB")
Expand Down Expand Up @@ -93,24 +107,32 @@ mod tests {

#[test]
fn parse_signature_type_proxy() {
assert_eq!(parse_signature_type("proxy"), SignatureType::Proxy);
assert_eq!(parse_signature_type("proxy").unwrap(), SignatureType::Proxy);
}

#[test]
fn parse_signature_type_gnosis_safe() {
assert_eq!(
parse_signature_type("gnosis-safe"),
parse_signature_type("gnosis-safe").unwrap(),
SignatureType::GnosisSafe
);
}

#[test]
fn parse_signature_type_eoa() {
assert_eq!(parse_signature_type("eoa"), SignatureType::Eoa);
assert_eq!(parse_signature_type("eoa").unwrap(), SignatureType::Eoa);
}

#[test]
fn parse_signature_type_poly_1271() {
assert_eq!(
parse_signature_type("poly-1271").unwrap(),
SignatureType::Poly1271
);
}

#[test]
fn parse_signature_type_unknown_defaults_to_eoa() {
assert_eq!(parse_signature_type("unknown"), SignatureType::Eoa);
fn parse_signature_type_unknown_is_rejected() {
assert!(parse_signature_type("unknown").is_err());
}
}
10 changes: 9 additions & 1 deletion src/commands/approve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,11 @@ pub async fn execute(
output: OutputFormat,
private_key: Option<&str>,
signature_type: Option<&str>,
funder: Option<&str>,
) -> Result<()> {
match args.command {
ApproveCommand::Check { address } => {
check(address, private_key, signature_type, output).await
check(address, private_key, signature_type, funder, output).await
}
ApproveCommand::Set => set(private_key, signature_type, output).await,
}
Expand All @@ -114,10 +115,17 @@ async fn check(
address_arg: Option<Address>,
private_key: Option<&str>,
signature_type: Option<&str>,
funder: Option<&str>,
output: OutputFormat,
) -> Result<()> {
let owner: Address = if let Some(addr) = address_arg {
addr
} else if proxy::is_poly_1271_mode(signature_type)? {
crate::config::validate_funder_for_signature_type(
crate::config::POLY_1271_SIGNATURE_TYPE,
crate::config::resolve_funder(funder)?,
)?
.context("--funder is required when using signature type poly-1271")?
} else if proxy::is_proxy_mode(signature_type)? {
proxy::derive_proxy_address(private_key)?
} else {
Expand Down
73 changes: 48 additions & 25 deletions src/commands/clob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,7 @@ pub async fn execute(
output: OutputFormat,
private_key: Option<&str>,
signature_type: Option<&str>,
funder: Option<&str>,
) -> Result<()> {
// Unauthenticated client — cheap to construct, used by read commands and CreateApiKey.
let unauth = auth::unauthenticated_clob_client()?;
Expand Down Expand Up @@ -668,7 +669,7 @@ pub async fn execute(
post_only,
} => {
let signer = auth::resolve_signer(private_key)?;
let client = auth::authenticate_with_signer(&signer, signature_type).await?;
let client = auth::authenticate_with_signer(&signer, signature_type, funder).await?;

let price_dec =
Decimal::from_str(&price).map_err(|_| anyhow::anyhow!("Invalid price: {price}"))?;
Expand Down Expand Up @@ -701,7 +702,7 @@ pub async fn execute(
order_type,
} => {
let signer = auth::resolve_signer(private_key)?;
let client = auth::authenticate_with_signer(&signer, signature_type).await?;
let client = auth::authenticate_with_signer(&signer, signature_type, funder).await?;

let token_ids = parse_token_ids(&tokens)?;
let price_strs: Vec<&str> = prices.split(',').map(str::trim).collect();
Expand Down Expand Up @@ -748,7 +749,7 @@ pub async fn execute(
order_type,
} => {
let signer = auth::resolve_signer(private_key)?;
let client = auth::authenticate_with_signer(&signer, signature_type).await?;
let client = auth::authenticate_with_signer(&signer, signature_type, funder).await?;

let amount_dec = Decimal::from_str(&amount)
.map_err(|_| anyhow::anyhow!("Invalid amount: {amount}"))?;
Expand Down Expand Up @@ -781,7 +782,8 @@ pub async fn execute(
asset,
cursor,
} => {
let client = auth::authenticated_clob_client(private_key, signature_type).await?;
let client =
auth::authenticated_clob_client(private_key, signature_type, funder).await?;
let request = OrdersRequest::builder()
.maybe_market(market)
.maybe_asset_id(asset)
Expand All @@ -791,32 +793,37 @@ pub async fn execute(
}

ClobCommand::Order { order_id } => {
let client = auth::authenticated_clob_client(private_key, signature_type).await?;
let client =
auth::authenticated_clob_client(private_key, signature_type, funder).await?;
let result = client.order(&order_id).await?;
print_order_detail(&result, output)?;
}

ClobCommand::Cancel { order_id } => {
let client = auth::authenticated_clob_client(private_key, signature_type).await?;
let client =
auth::authenticated_clob_client(private_key, signature_type, funder).await?;
let result = client.cancel_order(&order_id).await?;
print_cancel_result(&result, output)?;
}

ClobCommand::CancelOrders { order_ids } => {
let client = auth::authenticated_clob_client(private_key, signature_type).await?;
let client =
auth::authenticated_clob_client(private_key, signature_type, funder).await?;
let ids: Vec<&str> = order_ids.split(',').map(str::trim).collect();
let result = client.cancel_orders(&ids).await?;
print_cancel_result(&result, output)?;
}

ClobCommand::CancelAll => {
let client = auth::authenticated_clob_client(private_key, signature_type).await?;
let client =
auth::authenticated_clob_client(private_key, signature_type, funder).await?;
let result = client.cancel_all_orders().await?;
print_cancel_result(&result, output)?;
}

ClobCommand::CancelMarket { market, asset } => {
let client = auth::authenticated_clob_client(private_key, signature_type).await?;
let client =
auth::authenticated_clob_client(private_key, signature_type, funder).await?;
let request = CancelMarketOrderRequest::builder()
.maybe_market(market)
.maybe_asset_id(asset)
Expand All @@ -830,7 +837,8 @@ pub async fn execute(
asset,
cursor,
} => {
let client = auth::authenticated_clob_client(private_key, signature_type).await?;
let client =
auth::authenticated_clob_client(private_key, signature_type, funder).await?;
let request = TradesRequest::builder()
.maybe_market(market)
.maybe_asset_id(asset)
Expand All @@ -840,7 +848,8 @@ pub async fn execute(
}

ClobCommand::Balance { asset_type, token } => {
let client = auth::authenticated_clob_client(private_key, signature_type).await?;
let client =
auth::authenticated_clob_client(private_key, signature_type, funder).await?;
let is_collateral = matches!(asset_type, CliAssetType::Collateral);
let request = BalanceAllowanceRequest::builder()
.asset_type(AssetType::from(asset_type))
Expand All @@ -851,7 +860,8 @@ pub async fn execute(
}

ClobCommand::UpdateBalance { asset_type, token } => {
let client = auth::authenticated_clob_client(private_key, signature_type).await?;
let client =
auth::authenticated_clob_client(private_key, signature_type, funder).await?;
let request = BalanceAllowanceRequest::builder()
.asset_type(AssetType::from(asset_type))
.maybe_token_id(token)
Expand All @@ -866,13 +876,15 @@ pub async fn execute(
}

ClobCommand::Notifications => {
let client = auth::authenticated_clob_client(private_key, signature_type).await?;
let client =
auth::authenticated_clob_client(private_key, signature_type, funder).await?;
let result = client.notifications().await?;
print_notifications(&result, output)?;
}

ClobCommand::DeleteNotifications { ids } => {
let client = auth::authenticated_clob_client(private_key, signature_type).await?;
let client =
auth::authenticated_clob_client(private_key, signature_type, funder).await?;
let notification_ids: Vec<String> =
ids.split(',').map(|s| s.trim().to_string()).collect();
let request = DeleteNotificationsRequest::builder()
Expand All @@ -889,23 +901,26 @@ pub async fn execute(

// ── Authenticated reward commands ────────────────────────────────
ClobCommand::Rewards { date, cursor } => {
let client = auth::authenticated_clob_client(private_key, signature_type).await?;
let client =
auth::authenticated_clob_client(private_key, signature_type, funder).await?;
let result = client
.earnings_for_user_for_day(parse_date(&date)?, cursor)
.await?;
print_rewards(&result, output)?;
}

ClobCommand::Earnings { date } => {
let client = auth::authenticated_clob_client(private_key, signature_type).await?;
let client =
auth::authenticated_clob_client(private_key, signature_type, funder).await?;
let result = client
.total_earnings_for_user_for_day(parse_date(&date)?)
.await?;
print_earnings(&result, output)?;
}

ClobCommand::EarningsMarkets { date, cursor } => {
let client = auth::authenticated_clob_client(private_key, signature_type).await?;
let client =
auth::authenticated_clob_client(private_key, signature_type, funder).await?;
let request = UserRewardsEarningRequest::builder()
.date(parse_date(&date)?)
.build();
Expand All @@ -916,13 +931,15 @@ pub async fn execute(
}

ClobCommand::RewardPercentages => {
let client = auth::authenticated_clob_client(private_key, signature_type).await?;
let client =
auth::authenticated_clob_client(private_key, signature_type, funder).await?;
let result = client.reward_percentages().await?;
print_reward_percentages(&result, output)?;
}

ClobCommand::CurrentRewards { cursor } => {
let client = auth::authenticated_clob_client(private_key, signature_type).await?;
let client =
auth::authenticated_clob_client(private_key, signature_type, funder).await?;
let result = client.current_rewards(cursor).await?;
print_current_rewards(&result, output)?;
}
Expand All @@ -931,19 +948,22 @@ pub async fn execute(
condition_id,
cursor,
} => {
let client = auth::authenticated_clob_client(private_key, signature_type).await?;
let client =
auth::authenticated_clob_client(private_key, signature_type, funder).await?;
let result = client.raw_rewards_for_market(&condition_id, cursor).await?;
print_market_reward(&result, output)?;
}

ClobCommand::OrderScoring { order_id } => {
let client = auth::authenticated_clob_client(private_key, signature_type).await?;
let client =
auth::authenticated_clob_client(private_key, signature_type, funder).await?;
let result = client.is_order_scoring(&order_id).await?;
print_order_scoring(&result, output)?;
}

ClobCommand::OrdersScoring { order_ids } => {
let client = auth::authenticated_clob_client(private_key, signature_type).await?;
let client =
auth::authenticated_clob_client(private_key, signature_type, funder).await?;
let ids: Vec<&str> = order_ids.split(',').map(str::trim).collect();
let result = client.are_orders_scoring(&ids).await?;
print_orders_scoring(&result, output)?;
Expand All @@ -957,19 +977,22 @@ pub async fn execute(
}

ClobCommand::ApiKeys => {
let client = auth::authenticated_clob_client(private_key, signature_type).await?;
let client =
auth::authenticated_clob_client(private_key, signature_type, funder).await?;
let result = client.api_keys().await?;
print_api_keys(&result, output)?;
}

ClobCommand::DeleteApiKey => {
let client = auth::authenticated_clob_client(private_key, signature_type).await?;
let client =
auth::authenticated_clob_client(private_key, signature_type, funder).await?;
let result = client.delete_api_key().await?;
print_delete_api_key(&result, output)?;
}

ClobCommand::AccountStatus => {
let client = auth::authenticated_clob_client(private_key, signature_type).await?;
Comment thread
sharananurag998 marked this conversation as resolved.
let client =
auth::authenticated_clob_client(private_key, signature_type, funder).await?;
let result = client.closed_only_mode().await?;
print_account_status(&result, output)?;
}
Expand Down
Loading