### Get Index Set from Question IDs with TypeScript Source: https://context7.com/polymarket/examples/llms.txt Calculates a bit mask index set from market question IDs, used for negative-risk position conversions. This function resides in the './src/utils' module and takes an array of question IDs as input. The output is a bit mask representing selected positions. ```typescript import { getIndexSet } from "./src/utils"; // Example with multiple question IDs from a negative-risk market const questionIDs = [ "0x4e4c30b82cc1cf91f8e5e1e4f8f0e8e5e2f0e8e5e2f0e8e5e2f0e8e5e2f0e801", "0x4e4c30b82cc1cf91f8e5e1e4f8f0e8e5e2f0e8e5e2f0e8e5e2f0e8e5e2f0e802", "0x4e4c30b82cc1cf91f8e5e1e4f8f0e8e5e2f0e8e5e2f0e8e5e2f0e8e5e2f0e803" ]; // Calculate index set - extracts last byte of each ID and creates bit mask const indexSet = getIndexSet(questionIDs); console.log(`Index set: ${indexSet}`); // Output: bit mask representing positions // Use with convert function import { encodeConvert } from "./src/encode"; import { ethers } from "ethers"; import { USDCE_DIGITS } from "./src/constants"; const marketId = "0xe64f063b9e2b02f8ac679ebacfb938088c1ddde2a953a1ebfd4a92b802910371"; const convertAmount = ethers.utils.parseUnits("10", USDCE_DIGITS); const convertData = encodeConvert(marketId, indexSet, convertAmount); console.log("Convert transaction data ready"); ``` -------------------------------- ### Deploy New Safe Wallet with TypeScript Source: https://context7.com/polymarket/examples/llms.txt Creates and deploys a new Gnosis Safe wallet using EIP-712 signature authorization. This example utilizes ethers.js and requires helper functions for signature creation and specific ABIs. The deployment process involves interacting with a Safe Factory contract. ```typescript import { ethers } from "ethers"; import { Zero, AddressZero } from "@ethersproject/constants"; import { splitSignature } from "@ethersproject/bytes"; import { createSafeCreateSignature } from "./src/safe-helpers"; import { safeFactoryAbi } from "./src/abis"; import { SAFE_FACTORY_ADDRESS } from "./src/constants"; const provider = new ethers.providers.JsonRpcProvider("https://polygon-rpc.com"); const wallet = new ethers.Wallet("YOUR_PRIVATE_KEY", provider); const chainId = await wallet.getChainId(); console.log(`Deploying Safe for: ${wallet.address}`); const factory = new ethers.Contract(SAFE_FACTORY_ADDRESS, safeFactoryAbi, wallet); // Create EIP-712 signature for Safe creation const sig = await createSafeCreateSignature(wallet, chainId); // Deploy Safe wallet const txn = await factory.createProxy( AddressZero, Zero, AddressZero, splitSignature(sig), { gasPrice: 100000000000 } ); console.log(`Deployment transaction: ${txn.hash}`); const receipt = await txn.wait(); // Extract Safe address from events const safeAddress = receipt.events?.find(e => e.event === "ProxyCreation")?.args?.proxy; console.log(`Safe deployed at: ${safeAddress}`); ``` -------------------------------- ### Aggregate Safe Transactions with TypeScript Source: https://context7.com/polymarket/examples/llms.txt Combines multiple Safe transactions into a single multi-send transaction for batch execution. This example uses ethers.js and specific helper functions for aggregation and signing. It requires the safe-helpers, encode, abis, and constants modules. ```typescript import { ethers } from "ethers"; import { aggregateTransaction, signAndExecuteSafeTransaction } from "./src/safe-helpers"; import { encodeErc20Approve, encodeSplit } from "./src/encode"; import { safeAbi } from "./src/abis"; import { USDC_ADDRESS, CONDITIONAL_TOKENS_FRAMEWORK_ADDRESS, USDCE_DIGITS } from "./src/constants"; import { SafeTransaction, OperationType } from "./src/types"; const provider = new ethers.providers.JsonRpcProvider("https://polygon-rpc.com"); const wallet = new ethers.Wallet("YOUR_PRIVATE_KEY", provider); const safeAddress = "0xYourSafeWalletAddress"; const safe = new ethers.Contract(safeAddress, safeAbi, wallet); // Create multiple transactions const approveTxn: SafeTransaction = { to: USDC_ADDRESS, data: encodeErc20Approve(CONDITIONAL_TOKENS_FRAMEWORK_ADDRESS, ethers.constants.MaxUint256), operation: OperationType.Call, value: "0" }; const splitTxn: SafeTransaction = { to: CONDITIONAL_TOKENS_FRAMEWORK_ADDRESS, data: encodeSplit(USDC_ADDRESS, "0xe64f063b...", ethers.utils.parseUnits("100", USDCE_DIGITS)), operation: OperationType.Call, value: "0" }; // Aggregate into single multi-send transaction const aggregatedTxn = aggregateTransaction([approveTxn, splitTxn]); // Execute as single transaction const txn = await signAndExecuteSafeTransaction(wallet, safe, aggregatedTxn, { gasPrice: 200000000000 }); console.log(`Batch transaction: ${txn.hash}`); await txn.wait(); console.log("Batch execution completed!"); ``` -------------------------------- ### Encode Convert Position Transaction Source: https://context7.com/polymarket/examples/llms.txt Encodes transaction data for converting NO positions in negative-risk markets to YES positions in the complementary event. This requires specifying market details and the amount to convert, utilizing helper functions to derive necessary parameters. ```typescript import { ethers } from "ethers"; import { encodeConvert } from "./src/encode"; import { getIndexSet } from "./src/utils"; import { proxyFactoryAbi } from "./src/abis"; import { PROXY_WALLET_FACTORY_ADDRESS, NEG_RISK_ADAPTER_ADDRESS, USDCE_DIGITS } from "./src/constants"; const provider = new ethers.providers.JsonRpcProvider("https://polygon-rpc.com"); const wallet = new ethers.Wallet("YOUR_PRIVATE_KEY", provider); const factory = new ethers.Contract(PROXY_WALLET_FACTORY_ADDRESS, proxyFactoryAbi, wallet); // Convert NO positions to YES positions in complementary market const questionIDs = [ "0x123abc...001", "0x456def...002" ]; const indexSet = getIndexSet(questionIDs); // Calculates bit mask from question IDs const convertAmount = "25"; const marketId = "0xe64f063b9e2b02f8ac679ebacfb938088c1ddde2a953a1ebfd4a92b802910371"; const data = encodeConvert(marketId, indexSet, ethers.utils.parseUnits(convertAmount, USDCE_DIGITS)); const proxyTxn = { to: NEG_RISK_ADAPTER_ADDRESS, typeCode: "1", data: data, value: "0" }; const txn = await factory.proxy([proxyTxn], { gasPrice: 100000000000 }); console.log(`Convert transaction: ${txn.hash}`); await txn.wait(); console.log("Position conversion completed!"); ``` -------------------------------- ### Sign and Execute Gnosis Safe Transaction Source: https://context7.com/polymarket/examples/llms.txt Signs and executes a transaction through a Gnosis Safe wallet, incorporating EIP-712 signature verification for enhanced security. This involves preparing the transaction details and utilizing a helper function to manage the signing and execution process. ```typescript import { ethers } from "ethers"; import { signAndExecuteSafeTransaction } from "./src/safe-helpers"; import { encodeSplit } from "./src/encode"; import { safeAbi } from "./src/abis"; import { USDC_ADDRESS, CONDITIONAL_TOKENS_FRAMEWORK_ADDRESS, USDCE_DIGITS } from "./src/constants"; import { SafeTransaction, OperationType } from "./src/types"; const provider = new ethers.providers.JsonRpcProvider("https://polygon-rpc.com"); const wallet = new ethers.Wallet("YOUR_PRIVATE_KEY", provider); // Connect to existing Safe wallet const safeAddress = "0xYourSafeWalletAddress"; const safe = new ethers.Contract(safeAddress, safeAbi, wallet); // Prepare split transaction const splitAmount = "75"; const conditionId = "0xe64f063b9e2b02f8ac679ebacfb938088c1ddde2a953a1ebfd4a92b802910371"; const data = encodeSplit(USDC_ADDRESS, conditionId, ethers.utils.parseUnits(splitAmount, USDCE_DIGITS)); const safeTxn: SafeTransaction = { to: CONDITIONAL_TOKENS_FRAMEWORK_ADDRESS, data: data, operation: OperationType.Call, value: "0" }; // Sign and execute through Safe const txn = await signAndExecuteSafeTransaction( wallet, safe, safeTxn, { gasPrice: 200000000000 } ); console.log(`Safe transaction: ${txn.hash}`); await txn.wait(); console.log("Safe transaction executed!"); ``` -------------------------------- ### Encode Merge Position Transaction Source: https://context7.com/polymarket/examples/llms.txt Encodes the transaction data for merging outcome tokens (YES/NO positions) back into collateral tokens (like USDC). This function is used when a user wants to exit a market by redeeming their positions for collateral. ```typescript import { ethers } from "ethers"; import { encodeMerge } from "./src/encode"; import { proxyFactoryAbi } from "./src/abis"; import { PROXY_WALLET_FACTORY_ADDRESS, USDC_ADDRESS, CONDITIONAL_TOKENS_FRAMEWORK_ADDRESS, NEG_RISK_ADAPTER_ADDRESS, USDCE_DIGITS } from "./src/constants"; const provider = new ethers.providers.JsonRpcProvider("https://polygon-rpc.com"); const wallet = new ethers.Wallet("YOUR_PRIVATE_KEY", provider); const factory = new ethers.Contract(PROXY_WALLET_FACTORY_ADDRESS, proxyFactoryAbi, wallet); // Merge 50 outcome tokens back to USDC const mergeAmount = "50"; const conditionId = "0xe64f063b9e2b02f8ac679ebacfb938088c1ddde2a953a1ebfd4a92b802910371"; const negRisk = false; const data = encodeMerge(USDC_ADDRESS, conditionId, ethers.utils.parseUnits(mergeAmount, USDCE_DIGITS)); const targetContract = negRisk ? NEG_RISK_ADAPTER_ADDRESS : CONDITIONAL_TOKENS_FRAMEWORK_ADDRESS; const proxyTxn = { to: targetContract, typeCode: "1", data: data, value: "0" }; const txn = await factory.proxy([proxyTxn], { gasPrice: 100000000000 }); console.log(`Merge transaction: ${txn.hash}`); await txn.wait(); console.log("Positions merged!"); ``` -------------------------------- ### Encode ERC20 Transfer with Polymarket Smart Contract Wallets Source: https://context7.com/polymarket/examples/llms.txt Encodes and executes a transaction to transfer ERC20 tokens (e.g., USDC) from a Polymarket smart contract wallet to a specified destination address. This requires the destination address, the amount to transfer, and uses the `encodeErc20Transfer` function. The transaction is then sent via the proxy wallet. ```typescript import { ethers } from "ethers"; import { encodeErc20Transfer } from "./src/encode"; import { proxyFactoryAbi } from "./src/abis"; import { PROXY_WALLET_FACTORY_ADDRESS, USDC_ADDRESS, USDCE_DIGITS } from "./src/constants"; const provider = new ethers.providers.JsonRpcProvider("https://polygon-rpc.com"); const wallet = new ethers.Wallet("YOUR_PRIVATE_KEY", provider); // Proxy factory contract const factory = new ethers.Contract(PROXY_WALLET_FACTORY_ADDRESS, proxyFactoryAbi, wallet); // Transfer 10 USDC to destination const destinationAddress = "0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb"; const transferAmount = ethers.utils.parseUnits("10", USDCE_DIGITS); // Encode the transfer function call const data = encodeErc20Transfer(destinationAddress, transferAmount); // Execute via proxy wallet const proxyTxn = { to: USDC_ADDRESS, typeCode: "1", // CallType.Call data: data, value: "0" }; const txn = await factory.proxy([proxyTxn], { gasPrice: 100000000000 }); console.log(`Transaction hash: ${txn.hash}`); await txn.wait(); console.log("Transfer completed!"); ``` -------------------------------- ### Encode ERC20 Approve with Polymarket Smart Contract Wallets Source: https://context7.com/polymarket/examples/llms.txt Generates transaction data to approve a spender (e.g., an exchange contract) to manage ERC20 tokens on behalf of the Polymarket smart contract wallet. It uses the `encodeErc20Approve` function, specifying the spender's address and the approval amount (often `MaxUint256` for unlimited approval). The transaction is then executed. ```typescript import { ethers } from "ethers"; import { encodeErc20Approve } from "./src/encode"; import { proxyFactoryAbi } from "./src/abis"; import { PROXY_WALLET_FACTORY_ADDRESS, USDC_ADDRESS } from "./src/constants"; const provider = new ethers.providers.JsonRpcProvider("https://polygon-rpc.com"); const wallet = new ethers.Wallet("YOUR_PRIVATE_KEY", provider); const factory = new ethers.Contract(PROXY_WALLET_FACTORY_ADDRESS, proxyFactoryAbi, wallet); // Approve exchange contract to spend unlimited USDC const spenderAddress = "0xC5d563A36AE78145C45a50134d48A1215220f80a"; const approvalAmount = ethers.constants.MaxUint256; const data = encodeErc20Approve(spenderAddress, approvalAmount); const proxyTxn = { to: USDC_ADDRESS, typeCode: "1", data: data, value: "0" }; const txn = await factory.proxy([proxyTxn], { gasPrice: 100000000000 }); console.log(`Approval transaction: ${txn.hash}`); await txn.wait(); console.log("Approval granted!"); ``` -------------------------------- ### Encode ERC1155 Approve Transaction Source: https://context7.com/polymarket/examples/llms.txt Encodes the transaction data required to grant an operator approval to manage all ERC1155 outcome tokens on behalf of a user's wallet. This is useful for decentralized exchanges or other protocols that need to interact with a user's tokens. ```typescript import { ethers } from "ethers"; import { encodeErc1155Approve } from "./src/encode"; import { proxyFactoryAbi } from "./src/abis"; import { PROXY_WALLET_FACTORY_ADDRESS, CONDITIONAL_TOKENS_FRAMEWORK_ADDRESS } from "./src/constants"; const provider = new ethers.providers.JsonRpcProvider("https://polygon-rpc.com"); const wallet = new ethers.Wallet("YOUR_PRIVATE_KEY", provider); const factory = new ethers.Contract(PROXY_WALLET_FACTORY_ADDRESS, proxyFactoryAbi, wallet); // Approve exchange to manage all outcome tokens const operatorAddress = "0x4bFb41d5B3570DeFd03C39a9A4D8dE6Bd8B8982E"; const approved = true; const data = encodeErc1155Approve(operatorAddress, approved); const proxyTxn = { to: CONDITIONAL_TOKENS_FRAMEWORK_ADDRESS, typeCode: "1", data: data, value: "0" }; const txn = await factory.proxy([proxyTxn], { gasPrice: 100000000000 }); console.log(`Approval transaction: ${txn.hash}`); await txn.wait(); console.log("ERC1155 approval set!"); ``` -------------------------------- ### Encode Redeem Position Transaction Source: https://context7.com/polymarket/examples/llms.txt Encodes transaction data for redeeming outcome tokens after a market resolves, converting winning tokens back to collateral. This function supports both standard and negative-risk markets, requiring different parameters based on the market type. ```typescript import { ethers } from "ethers"; import { encodeRedeem, encodeRedeemNegRisk } from "./src/encode"; import { proxyFactoryAbi } from "./src/abis"; import { PROXY_WALLET_FACTORY_ADDRESS, USDC_ADDRESS, CONDITIONAL_TOKENS_FRAMEWORK_ADDRESS, NEG_RISK_ADAPTER_ADDRESS } from "./src/constants"; const provider = new ethers.providers.JsonRpcProvider("https://polygon-rpc.com"); const wallet = new ethers.Wallet("YOUR_PRIVATE_KEY", provider); const factory = new ethers.Contract(PROXY_WALLET_FACTORY_ADDRESS, proxyFactoryAbi, wallet); const conditionId = "0xe64f063b9e2b02f8ac679ebacfb938088c1ddde2a953a1ebfd4a92b802910371"; const negRisk = true; let data: string; let targetContract: string; if (negRisk) { // For negative-risk markets, specify amounts of YES and NO tokens to redeem const redeemAmounts = ["100", "50"]; // [yesTokenAmount, noTokenAmount] data = encodeRedeemNegRisk(conditionId, redeemAmounts); targetContract = NEG_RISK_ADAPTER_ADDRESS; } else { // For standard markets data = encodeRedeem(USDC_ADDRESS, conditionId); targetContract = CONDITIONAL_TOKENS_FRAMEWORK_ADDRESS; } const proxyTxn = { to: targetContract, typeCode: "1", data: data, value: "0" }; const txn = await factory.proxy([proxyTxn], { gasPrice: 100000000000 }); console.log(`Redeem transaction: ${txn.hash}`); await txn.wait(); console.log("Redemption completed!"); ``` -------------------------------- ### Encode Split Position Transaction Source: https://context7.com/polymarket/examples/llms.txt Encodes the transaction data for splitting collateral tokens (like USDC) into outcome tokens (YES/NO positions) for a prediction market. This function is used when a user wants to enter a market by acquiring specific outcome tokens. ```typescript import { ethers } from "ethers"; import { encodeSplit } from "./src/encode"; import { proxyFactoryAbi } from "./src/abis"; import { PROXY_WALLET_FACTORY_ADDRESS, USDC_ADDRESS, CONDITIONAL_TOKENS_FRAMEWORK_ADDRESS, NEG_RISK_ADAPTER_ADDRESS, USDCE_DIGITS } from "./src/constants"; const provider = new ethers.providers.JsonRpcProvider("https://polygon-rpc.com"); const wallet = new ethers.Wallet("YOUR_PRIVATE_KEY", provider); const factory = new ethers.Contract(PROXY_WALLET_FACTORY_ADDRESS, proxyFactoryAbi, wallet); // Split 100 USDC into outcome tokens const splitAmount = "100"; const conditionId = "0xe64f063b9e2b02f8ac679ebacfb938088c1ddde2a953a1ebfd4a92b802910371"; const negRisk = true; // true for negative-risk markets, false for standard markets const data = encodeSplit(USDC_ADDRESS, conditionId, ethers.utils.parseUnits(splitAmount, USDCE_DIGITS)); const targetContract = negRisk ? NEG_RISK_ADAPTER_ADDRESS : CONDITIONAL_TOKENS_FRAMEWORK_ADDRESS; const proxyTxn = { to: targetContract, typeCode: "1", data: data, value: "0" }; const txn = await factory.proxy([proxyTxn], { gasPrice: 100000000000 }); console.log(`Split transaction: ${txn.hash}`); await txn.wait(); console.log("Position split completed!"); ``` -------------------------------- ### Encode ERC1155 Transfer with Polymarket Smart Contract Wallets Source: https://context7.com/polymarket/examples/llms.txt Encodes and executes a transaction to transfer ERC1155 tokens, such as outcome tokens from Polymarket prediction markets. This function `encodeErc1155TransferFrom` is used to specify the sender (proxy wallet), receiver, token ID, and amount. The transaction is then sent via the proxy wallet to the Conditional Tokens Framework contract. ```typescript import { ethers } from "ethers"; import { encodeErc1155TransferFrom } from "./src/encode"; import { proxyFactoryAbi } from "./src/abis"; import { PROXY_WALLET_FACTORY_ADDRESS, CONDITIONAL_TOKENS_FRAMEWORK_ADDRESS, USDCE_DIGITS } from "./src/constants"; const provider = new ethers.providers.JsonRpcProvider("https://polygon-rpc.com"); const wallet = new ethers.Wallet("YOUR_PRIVATE_KEY", provider); const factory = new ethers.Contract(PROXY_WALLET_FACTORY_ADDRESS, proxyFactoryAbi, wallet); // Transfer outcome tokens const proxyWalletAddress = "0x1234567890123456789012345678901234567890"; const destinationAddress = "0x0987654321098765432109876543210987654321"; const tokenId = "71321045984069851916732174605469525481402718446104698672703858725308889047619"; const transferAmount = ethers.utils.parseUnits("5", USDCE_DIGITS); const data = encodeErc1155TransferFrom(proxyWalletAddress, destinationAddress, tokenId, transferAmount); const proxyTxn = { to: CONDITIONAL_TOKENS_FRAMEWORK_ADDRESS, typeCode: "1", data: data, value: "0" }; const txn = await factory.proxy([proxyTxn], { gasPrice: 100000000000 }); console.log(`Transfer hash: ${txn.hash}`); await txn.wait(); console.log("Outcome tokens transferred!"); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.