This repository was archived by the owner on May 11, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 168
Feature: redeem positions #187
Open
Lekky71
wants to merge
7
commits into
Polymarket:main
Choose a base branch
from
Lekky71:feature/redeem-positions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8e0789a
Implemented redeemPositions
Lekky71 c331f67
Merge branch 'main' of https://github.com/Polymarket/clob-client into…
Lekky71 1943693
Copilot PR review cleanups
Lekky71 b8d2392
Merge branch 'main' into feature/redeem-positions
Lekky71 cdf4428
Merge branch 'feature/redeem-positions' of https://github.com/Lekky71…
Lekky71 fa1b23d
Merge branch 'main' of https://github.com/Polymarket/clob-client into…
Lekky71 af8a2d0
Synced with main branch
Lekky71 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| import { ethers } from "ethers"; | ||
| import { config as dotenvConfig } from "dotenv"; | ||
| import { resolve } from "path"; | ||
| import { ApiKeyCreds, Chain } from "../src"; | ||
| import { ClobClient } from "../src"; | ||
| import { SignatureType } from "@polymarket/order-utils"; | ||
|
|
||
| dotenvConfig({ path: resolve(__dirname, "../.env") }); | ||
|
|
||
| function getWallet(mainnetQ: boolean): ethers.Wallet { | ||
| const pk = process.env.PK as string; | ||
| const rpcToken: string = process.env.RPC_TOKEN as string; | ||
| let rpcUrl = ""; | ||
| if (mainnetQ) { | ||
| rpcUrl = `https://polygon-mainnet.g.alchemy.com/v2/${rpcToken}`; | ||
| } else { | ||
| rpcUrl = `https://polygon-amoy.g.alchemy.com/v2/${rpcToken}`; | ||
| } | ||
| const provider = new ethers.providers.JsonRpcProvider(rpcUrl); | ||
| let wallet = new ethers.Wallet(pk); | ||
| wallet = wallet.connect(provider); | ||
| return wallet; | ||
| } | ||
|
|
||
| /** | ||
| * Redeem market outcome tokens for EOA (Externally Owned Account) wallets | ||
| * Use this function if you're using a standard wallet (private key) | ||
| */ | ||
| export async function redeemForEOA() { | ||
| const chainId = parseInt(`${process.env.CHAIN_ID || Chain.AMOY}`) as Chain; | ||
| // -------------------------- | ||
| // SET MAINNET OR AMOY HERE | ||
| const isMainnet = chainId === Chain.POLYGON; | ||
| // -------------------------- | ||
|
|
||
| const wallet = getWallet(isMainnet); | ||
| const host = process.env.CLOB_API_URL || "http://localhost:8080"; | ||
| const creds: ApiKeyCreds = { | ||
| key: `${process.env.CLOB_API_KEY}`, | ||
| secret: `${process.env.CLOB_SECRET}`, | ||
| passphrase: `${process.env.CLOB_PASS_PHRASE}`, | ||
| }; | ||
|
|
||
| console.log(`Address: ${await wallet.getAddress()}, chainId: ${chainId}`); | ||
|
|
||
| const clobClient = new ClobClient(host, chainId, wallet, creds, SignatureType.EOA); | ||
|
|
||
| // Market and condition IDs for a resolved market | ||
| // Replace these with actual values from a resolved market | ||
| const conditionId = "0xabcdef..."; // Condition ID (32-byte hash) | ||
|
|
||
| try { | ||
| console.log("Redeeming outcome tokens for EOA wallet..."); | ||
| const receipt = await clobClient.redeemPositions({ | ||
| ConditionID: conditionId, | ||
| }); | ||
|
|
||
| console.log(`Transaction successful! Hash: ${receipt.transactionHash}`); | ||
| console.log(`Block number: ${receipt.blockNumber}`); | ||
| } catch (error) { | ||
| console.error("Error redeeming outcome:", error); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Redeem market outcome tokens for Proxy/Safe wallets | ||
| * Use this function if you're using a Polymarket account (Magic/Email or Browser wallet) | ||
| */ | ||
| export async function redeemForProxy() { | ||
| const chainId = parseInt(`${process.env.CHAIN_ID || Chain.AMOY}`) as Chain; | ||
| // -------------------------- | ||
| // SET MAINNET OR AMOY HERE | ||
| const isMainnet = chainId === Chain.POLYGON; | ||
| // -------------------------- | ||
|
|
||
| const wallet = getWallet(isMainnet); | ||
| const host = process.env.CLOB_API_URL || "http://localhost:8080"; | ||
| const creds: ApiKeyCreds = { | ||
| key: `${process.env.CLOB_API_KEY}`, | ||
| secret: `${process.env.CLOB_SECRET}`, | ||
| passphrase: `${process.env.CLOB_PASS_PHRASE}`, | ||
| }; | ||
|
|
||
| // Use this if you do not have API keys. | ||
| // const clobClientForKeys = new ClobClient(host, chainId, wallet); | ||
| // | ||
| // const creds = await clobClientForKeys.createOrDeriveApiKey(1); | ||
|
|
||
| console.log(`Address: ${await wallet.getAddress()}, chainId: ${chainId}`); | ||
|
|
||
| // Market and condition IDs for a resolved market | ||
| // Replace these with actual values from a resolved market | ||
| const conditionId = "0x9ba..."; // Condition ID (32-byte hash) | ||
|
Lekky71 marked this conversation as resolved.
|
||
| const proxyWalletAddress = "0xB1..."; // Your Safe/Proxy wallet address | ||
|
|
||
| // For proxy wallets, use POLY_GNOSIS_SAFE or POLY_PROXY signature type | ||
| const clobClient = new ClobClient( | ||
| host, | ||
| chainId, | ||
| wallet, | ||
| creds, | ||
| SignatureType.POLY_GNOSIS_SAFE, | ||
| proxyWalletAddress, | ||
| ); | ||
|
|
||
| try { | ||
| console.log("Redeeming outcome tokens for Proxy wallet..."); | ||
| const receipt = await clobClient.redeemPositions({ | ||
| ConditionID: conditionId, | ||
| }); | ||
|
|
||
| console.log(`Transaction successful! Hash: ${receipt.transactionHash}`); | ||
| console.log(`Block number: ${receipt.blockNumber}`); | ||
| } catch (error) { | ||
| console.error("Error redeeming outcome:", error); | ||
| } | ||
| } | ||
|
|
||
| // Example usage: | ||
| // Uncomment one of the following to run: | ||
|
|
||
| // For EOA wallets (standard private key wallets): | ||
| // redeemForEOA(); | ||
|
|
||
| // For Proxy/Safe wallets (Polymarket accounts): | ||
| // redeemForProxy(); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.