Skip to content

Conversation

@Lekky71
Copy link

@Lekky71 Lekky71 commented Oct 6, 2025

Hi guys, awesome work you folks have been doing, I'm a big fan!

I spent some time over the weekend building automation around Polymarket and realized that there was no ClobClient function to redeem positions (Googled and saw folks asking for it as well) which if you ask me it allows folks to easily move their winnings and keep on trading, I would imagine before now people just interacted with CTF contract directly but to make things a bit easier I added a comprehensive position redemption feature for resolved markets, supporting both EOA and Safe/Proxy wallet types with proper gas handling and EIP-712 signature generation.

My code structure may not be inline with yours so please do let me know so I can make changes. I used the functions on my end and they work smoothly.

Core Features

BlockchainClient Enhancements

  • Extracted getSafeTransactionHash and generateSafeSignatureFromHash as standalone utility functions for testability
  • Added redeemMarketPositionsForEOA for standard wallet redemption
  • Added redeemMarketPositionsForSafeWallet for Safe/Proxy wallet redemption with proper EIP-712 transaction hash generation and signature approval
  • Implemented getCurrentFeeParams with Polygon network minimum gas requirements (80 gwei) to prevent transaction failures
  • Added approveHashOnSafe for on-chain hash approval before execution

Helper Function Updates

  • Added redeemMarketPositions wrapper function to route redemption requests based on signature type (EOA vs Safe wallet)
  • Validated funderWalletAddress requirement for Safe wallet redemptions

Type Definitions

  • Added RedeemMarketPositionsParams interface
  • Added RedeemMarketPositionsForSafeWalletParams interface
  • Added ApproveHashOnSafeParams interface
  • Added SafeTransactionHashParams interface with bigint types for numeric fields

ClobClient Integration

  • Added redeemPositions method to ClobClient for easy redemption access
  • Automatically routed to appropriate redemption method based on signature type
  • Passed through funder address when using Safe wallets

Testing

  • Added comprehensive tests for getSafeTransactionHash covering:
    • Hash consistency for same inputs
    • Different hashes for different parameters (address, nonce, chain, data)
  • Added tests for generateSafeSignatureFromHash validating:
    • Signature consistency
    • Different signatures for different inputs
    • Correct v=1 encoding for approved hash type
    • Proper owner address encoding as r parameter
  • Added 7 test cases for redeemPositions helper function covering all wallet types

Examples

  • Created examples/redeemMarketPositions.ts with two example functions:
    • redeemForEOA(): Standard wallet redemption example
    • redeemForProxy(): Safe/Proxy wallet redemption example
  • Included proper environment setup, error handling, and transaction logging

Technical Implementation

  • Used ethers v5 BigNumber API throughout for gas calculations
  • Implemented EIP-712 structured data hashing for Safe transactions
  • Used approved hash signature type (v=1) for Polymarket Safe compatibility
  • Set minimum gas fees to 80 gwei to meet Polygon network requirements (I also tested during high congestion period, hence this value)

Breaking Changes

None - all additions were backwards compatible


Note

Adds redemption of resolved market positions via ClobClient for EOA and Safe/Proxy wallets with fee handling and EIP‑712 Safe signing, plus tests and examples.

  • Core/Blockchain:
    • Add src/blockchain/blockchain.client.ts with redeemMarketPositionsForEOA and redeemMarketPositionsForSafeWallet, Safe EIP-712 hash/signature helpers (getSafeTransactionHash, generateSafeSignatureFromHash), and on-chain approveHashOnSafe.
    • Add ABIs: src/blockchain/abi/ctf.abi.ts, src/blockchain/abi/safe.abi.ts.
    • Add config constants: REDEEM_POSITIONS_GAS_LIMIT, REDEEM_POSITIONS_MINIMUM_MAX_FEE in src/config.ts.
  • Helpers:
    • Add redeemMarketPositions router in src/order-builder/helpers.ts to dispatch by SignatureType.
  • Client API:
    • Expose ClobClient.redeemPositions(params) in src/client.ts to redeem by ConditionID (passes funder/safe address when needed).
  • Types:
    • Add RedeemMarketPositionsParams, RedeemMarketPositionsForSafeWalletParams, ApproveHashOnSafeParams, SafeTransactionHashParams in src/types.ts.
  • Tests:
    • Add tests/blockchain/blockchain.client.test.ts for Safe hash/signature helpers.
    • Extend tests/order-builder/helpers.test.ts with redemption cases for EOA and Safe.
  • Examples:
    • Add examples/redeemMarketPositions.ts demonstrating EOA and Proxy/Safe redemption flows.

Written by Cursor Bugbot for commit b8d2392. This will update automatically on new commits. Configure here.

Add comprehensive position redemption feature for resolved markets, supporting
  both EOA and Safe/Proxy wallet types with proper gas handling and EIP-712
  signature generation.

  ## Core Features

  ### BlockchainClient Enhancements
  - Extract `getSafeTransactionHash` and `generateSafeSignatureFromHash` as
    standalone utility functions for testability
  - Add `redeemMarketPositionsForEOA` for standard wallet redemption
  - Add `redeemMarketPositionsForSafeWallet` for Safe/Proxy wallet redemption
    with proper EIP-712 transaction hash generation and signature approval
  - Implement `getCurrentFeeParams` with Polygon network minimum gas requirements
    (80 gwei) to prevent transaction failures
  - Add `approveHashOnSafe` for on-chain hash approval before execution

  ### Helper Function Updates
  - Add `redeemMarketPositions` wrapper function to route redemption requests
    based on signature type (EOA vs Safe wallet)
  - Validate `funderWalletAddress` requirement for Safe wallet redemptions

  ### Type Definitions
  - Add `RedeemMarketPositionsParams` interface
  - Add `RedeemMarketPositionsForSafeWalletParams` interface
  - Add `ApproveHashOnSafeParams` interface
  - Add `SafeTransactionHashParams` interface with bigint types for numeric fields

  ### ClobClient Integration
  - Add `redeemPositions` method to ClobClient for easy redemption access
  - Automatically route to appropriate redemption method based on signature type
  - Pass through funder address when using Safe wallets

  ### Testing
  - Add comprehensive tests for `getSafeTransactionHash` covering:
    - Hash consistency for same inputs
    - Different hashes for different parameters (address, nonce, chain, data)
  - Add tests for `generateSafeSignatureFromHash` validating:
    - Signature consistency
    - Different signatures for different inputs
    - Correct v=1 encoding for approved hash type
    - Proper owner address encoding as r parameter
  - Add 7 test cases for `redeemPositions` helper function covering all wallet types

  ### Examples
  - Create `examples/redeemMarketPositions.ts` with two example functions:
    - `redeemForEOA()`: Standard wallet redemption example
    - `redeemForProxy()`: Safe/Proxy wallet redemption example
  - Include proper environment setup, error handling, and transaction logging

  ## Technical Implementation

  - Use ethers v5 BigNumber API throughout for gas calculations
  - Implement EIP-712 structured data hashing for Safe transactions
  - Use approved hash signature type (v=1) for Polymarket Safe compatibility
  - Set minimum gas fees to 80 gwei to meet Polygon network requirements
  - Add extensive console logging for debugging Safe transaction flow

  ## Breaking Changes
  None - all additions are backwards compatible
… feature/redeem-positions

# Conflicts:
#	src/client.ts
@Lekky71 Lekky71 requested a review from a team as a code owner October 6, 2025 22:22
Copilot AI review requested due to automatic review settings October 6, 2025 22:22
@Lekky71 Lekky71 changed the title Feature/redeem positions Feature: redeem positions Oct 6, 2025
cursor[bot]

This comment was marked as outdated.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces a comprehensive position redemption feature for the Polymarket ClobClient, enabling users to easily redeem winning positions from resolved markets. The implementation supports both EOA (Externally Owned Account) wallets and Safe/Proxy wallets commonly used by Polymarket accounts.

  • Adds complete blockchain client functionality for position redemption with proper gas handling and Safe wallet EIP-712 signature generation
  • Implements helper functions and ClobClient integration to route redemption requests based on signature type
  • Provides comprehensive test coverage and example implementations for both wallet types

Reviewed Changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
src/blockchain/blockchain.client.ts Core blockchain client with EOA and Safe wallet redemption methods, gas handling, and EIP-712 signature utilities
src/blockchain/abi/ctf.abi.ts Conditional Token Framework ABI for contract interactions
src/blockchain/abi/safe.abi.ts Safe wallet ABI for transaction execution and nonce management
src/order-builder/helpers.ts Helper function to route redemption requests based on signature type with validation
src/client.ts ClobClient integration adding redeemPositions method with authentication checks
src/types.ts Type definitions for redemption parameters and Safe transaction structures
tests/blockchain/blockchain.client.test.ts Comprehensive unit tests for Safe transaction hash generation and signature functions
tests/order-builder/helpers.test.ts Tests for redemption helper function covering all wallet types and error cases
examples/redeemMarketPositions.ts Usage examples for both EOA and Safe wallet redemption scenarios

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Copilot AI review requested due to automatic review settings October 13, 2025 18:13
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 10 out of 10 changed files in this pull request and generated 2 comments.

Comments suppressed due to low confidence (2)

src/blockchain/blockchain.client.ts:1

  • The SafeTransactionHashParams interface accepts both number and bigint types, but the implementation only handles bigint values. This could lead to runtime errors if number values are passed. Consider using only bigint for consistency with ethers.js BigNumber usage.
import { ethers } from "ethers";

src/blockchain/blockchain.client.ts:1

  • The SafeTransactionHashParams interface accepts both number and bigint types, but the implementation only handles bigint values. This could lead to runtime errors if number values are passed. Consider using only bigint for consistency with ethers.js BigNumber usage.
import { ethers } from "ethers";

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment on lines +49 to +50
// Replace these with actual values from a resolved market
const conditionId = "0xabcdef..."; // Condition ID (32-byte hash)
Copy link

Copilot AI Oct 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Example condition IDs are incomplete placeholders. Consider providing valid example condition IDs or clearer instructions on how to obtain them for users following the examples.

Suggested change
// Replace these with actual values from a resolved market
const conditionId = "0xabcdef..."; // Condition ID (32-byte hash)
// Replace this with the actual condition ID (32-byte hex string) from a resolved market.
// You can obtain the condition ID from the market details page on the Polymarket app,
// or by querying the smart contract or API for the resolved market.
// Example (dummy) condition ID:
const conditionId = "0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"; // Example Condition ID (32-byte hex string)

Copilot uses AI. Check for mistakes.
Comment on lines +92 to +93
// Replace these with actual values from a resolved market
const conditionId = "0x9ba..."; // Condition ID (32-byte hash)
Copy link

Copilot AI Oct 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Example condition IDs are incomplete placeholders. Consider providing valid example condition IDs or clearer instructions on how to obtain them for users following the examples.

Suggested change
// Replace these with actual values from a resolved market
const conditionId = "0x9ba..."; // Condition ID (32-byte hash)
// Replace the example below with the actual condition ID (32-byte hash) from a resolved market.
// You can obtain the condition ID from the market details page or via the Polymarket API.
const conditionId = "0x0000000000000000000000000000000000000000000000000000000000000000"; // Example only

Copilot uses AI. Check for mistakes.
@mys3lf
Copy link

mys3lf commented Nov 18, 2025

Hey everybody. I would like to use this feature. Is there anything that speaks against a merge?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants