-
Notifications
You must be signed in to change notification settings - Fork 78
Feature: redeem positions #187
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
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
There was a problem hiding this 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.
There was a problem hiding this 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.
| // Replace these with actual values from a resolved market | ||
| const conditionId = "0xabcdef..."; // Condition ID (32-byte hash) |
Copilot
AI
Oct 13, 2025
There was a problem hiding this comment.
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.
| // 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) |
| // Replace these with actual values from a resolved market | ||
| const conditionId = "0x9ba..."; // Condition ID (32-byte hash) |
Copilot
AI
Oct 13, 2025
There was a problem hiding this comment.
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.
| // 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 |
|
Hey everybody. I would like to use this feature. Is there anything that speaks against a merge? |
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
getSafeTransactionHashandgenerateSafeSignatureFromHashas standalone utility functions for testabilityredeemMarketPositionsForEOAfor standard wallet redemptionredeemMarketPositionsForSafeWalletfor Safe/Proxy wallet redemption with proper EIP-712 transaction hash generation and signature approvalgetCurrentFeeParamswith Polygon network minimum gas requirements (80 gwei) to prevent transaction failuresapproveHashOnSafefor on-chain hash approval before executionHelper Function Updates
redeemMarketPositionswrapper function to route redemption requests based on signature type (EOA vs Safe wallet)funderWalletAddressrequirement for Safe wallet redemptionsType Definitions
RedeemMarketPositionsParamsinterfaceRedeemMarketPositionsForSafeWalletParamsinterfaceApproveHashOnSafeParamsinterfaceSafeTransactionHashParamsinterface with bigint types for numeric fieldsClobClient Integration
redeemPositionsmethod to ClobClient for easy redemption accessTesting
getSafeTransactionHashcovering:generateSafeSignatureFromHashvalidating:redeemPositionshelper function covering all wallet typesExamples
examples/redeemMarketPositions.tswith two example functions:redeemForEOA(): Standard wallet redemption exampleredeemForProxy(): Safe/Proxy wallet redemption exampleTechnical Implementation
Breaking Changes
None - all additions were backwards compatible
Note
Adds redemption of resolved market positions via
ClobClientfor EOA and Safe/Proxy wallets with fee handling and EIP‑712 Safe signing, plus tests and examples.src/blockchain/blockchain.client.tswithredeemMarketPositionsForEOAandredeemMarketPositionsForSafeWallet, Safe EIP-712 hash/signature helpers (getSafeTransactionHash,generateSafeSignatureFromHash), and on-chainapproveHashOnSafe.src/blockchain/abi/ctf.abi.ts,src/blockchain/abi/safe.abi.ts.REDEEM_POSITIONS_GAS_LIMIT,REDEEM_POSITIONS_MINIMUM_MAX_FEEinsrc/config.ts.redeemMarketPositionsrouter insrc/order-builder/helpers.tsto dispatch bySignatureType.ClobClient.redeemPositions(params)insrc/client.tsto redeem byConditionID(passes funder/safe address when needed).RedeemMarketPositionsParams,RedeemMarketPositionsForSafeWalletParams,ApproveHashOnSafeParams,SafeTransactionHashParamsinsrc/types.ts.tests/blockchain/blockchain.client.test.tsfor Safe hash/signature helpers.tests/order-builder/helpers.test.tswith redemption cases for EOA and Safe.examples/redeemMarketPositions.tsdemonstrating EOA and Proxy/Safe redemption flows.Written by Cursor Bugbot for commit b8d2392. This will update automatically on new commits. Configure here.