### Check Outcome Token Balance with Python Source: https://context7.com/polymarket/conditional-token-examples-py/llms.txt Queries the ERC1155 balance of a specific outcome token for a given address. This read-only operation uses the `balanceOf` function from the CTF smart contract on Polygon. It requires `web3.py` and RPC endpoint configuration. ```python import os from web3 import Web3 from dotenv import load_dotenv load_dotenv() erc1155_balance_of = "[{\"inputs\": [{\"internalType\": \"address\",\"name\": \"account\",\"type\": \"address\"},{\"internalType\": \"uint256\",\"name\": \"id\",\"type\": \"uint256\"}],\"name\": \"balanceOf\",\"outputs\": [{\"internalType\": \"uint256\",\"name\": \"\",\"type\": \"uint256\"}],\"stateMutability\": \"view\",\"type\": \"function\"}]" DECIMALS = 10 ** 6 # Number of decimals used by the Outcome Token def main(): rpc_url = os.getenv("RPC_URL") w3 = Web3(Web3.HTTPProvider(rpc_url)) ctf_address = w3.to_checksum_address("0x4d97dcd97ec945f40cf65f87097ace5ea0476045") token_id = 28601896009848292737837970442888227527788768015752651484914905629056684518536 owner = w3.to_checksum_address("0xYourAddressHere") ctf = w3.eth.contract(ctf_address, abi=erc1155_balance_of) try: raw_balance = ctf.functions.balanceOf(owner, token_id).call() balance_formatted = float(raw_balance / DECIMALS) print(f"CTF Balance of {owner} for {token_id}: {str(balance_formatted)}") except Exception as e: print(f"Error querying CTF balance : {e}") raise e main() # Expected output: # CTF Balance of 0xYourAddressHere for 28601896009848292737837970442888227527788768015752651484914905629056684518536: 100.0 ``` -------------------------------- ### Mint Outcome Tokens (Split Position) with Python Source: https://context7.com/polymarket/conditional-token-examples-py/llms.txt This Python script demonstrates how to mint outcome tokens for a binary market by splitting collateral (USDC). It requires setting up a web3 connection, approving the CTF contract to spend USDC, and then calling the `splitPosition` function. The script uses environment variables for private key and RPC URL. ```python import os from web3 import Web3 from web3.constants import MAX_INT, HASH_ZERO from web3.middleware import ( geth_poa_middleware, construct_sign_and_send_raw_middleware ) from web3.gas_strategies.time_based import fast_gas_price_strategy from dotenv import load_dotenv load_dotenv() erc20_approve_abi = """[{"constant": false,"inputs": [{"name": "_spender","type": "address" },[ "name": "_value", "type": "uint256" ]],"name": "approve","outputs": [{ "name": "", "type": "bool" }],"payable": false,"stateMutability": "nonpayable","type": "function"}]""" split_position_abi = """[{"constant":false,"inputs":[{"name":"collateralToken","type":"address"},{"name":"parentCollectionId","type":"bytes32"},{"name":"conditionId","type":"bytes32"},{"name":"partition","type":"uint256[]"},{"name":"amount","type":"uint256"}],"name":"splitPosition","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"}]""" def main(): pk = os.getenv("PK") rpc_url = os.getenv("RPC_URL") w3 = Web3(Web3.HTTPProvider(rpc_url)) w3.middleware_onion.inject(geth_poa_middleware, layer=0) w3.middleware_onion.add(construct_sign_and_send_raw_middleware(pk)) w3.eth.default_account = w3.eth.account.from_key(pk).address w3.eth.set_gas_price_strategy(fast_gas_price_strategy) print(f"Wallet: {w3.eth.default_account}") usdc_address = w3.to_checksum_address("0x2791bca1f2de4661ed88a30c99a7a9449aa84174") ctf_address = w3.to_checksum_address("0x4d97dcd97ec945f40cf65f87097ace5ea0476045") # Step 1: Approve USDC usdc = w3.eth.contract(usdc_address, abi=erc20_approve_abi) print(f"Approving USDC {usdc_address} on spender {ctf_address}...") try: txn_hash_bytes = usdc.functions.approve( ctf_address, int(MAX_INT, base=16) ).transact() txn_hash = w3.to_hex(txn_hash_bytes) print(f"Approve transaction hash: {txn_hash}") w3.eth.wait_for_transaction_receipt(txn_hash) print("Approve complete!") except Exception as e: print(f"Error approving : {e}") raise e # Step 2: Split position to mint outcome tokens ctf = w3.eth.contract(ctf_address, abi=split_position_abi) condition_id = "0x67eb23e8932765c1d7a094838c928476df8c50d1d3898f278ef1fb2a62afab63" amount = 100_000_000 # 100 USDC (6 decimals) try: split_txn_hash_bytes = ctf.functions.splitPosition( usdc_address, # The collateral token address HASH_ZERO, # The parent collectionId, always bytes32(0) for Polymarket markets condition_id, # The conditionId of the market [1, 2], # The index set used by Polymarket for binary markets amount, ).transact() split_txn_hash = w3.to_hex(split_txn_hash_bytes) print(f"Split transaction hash: {split_txn_hash}") w3.eth.wait_for_transaction_receipt(split_txn_hash) print("Split complete!") except Exception as e: print(f"Error minting Outcome Tokens : {e}") raise e main() ``` -------------------------------- ### Merge Outcome Tokens to Collateral with Python Source: https://context7.com/polymarket/conditional-token-examples-py/llms.txt Merges equal amounts of YES and NO outcome tokens back into collateral tokens (USDC). This is useful for exiting a position before a market resolves. It requires the collateral token address, condition ID, and the amount to merge. The function operates on the Conditional Token Factory (CTF) contract. ```python import os from web3 import Web3 from web3.constants import MAX_INT, HASH_ZERO from web3.middleware import ( geth_poa_middleware, construct_sign_and_send_raw_middleware ) from web3.gas_strategies.time_based import fast_gas_price_strategy from dotenv import load_dotenv load_dotenv() merge_positions_abi = """[{\"constant\":false,\"inputs\":[{\"name\":\"collateralToken\",\"type\":\"address\"},{\"name\":\"parentCollectionId\",\"type\":\"bytes32\"},{\"name\":\"conditionId\",\"type\":\"bytes32\"},{\"name\":\"partition\",\"type\":\"uint256[]\"},{\"name\":\"amount\",\"type\":\"uint256\"}],\"name\":\"mergePositions\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"}]""" def main(): pk = os.getenv("PK") rpc_url = os.getenv("RPC_URL") w3 = Web3(Web3.HTTPProvider(rpc_url)) w3.middleware_onion.inject(geth_poa_middleware, layer=0) w3.middleware_onion.add(construct_sign_and_send_raw_middleware(pk)) w3.eth.default_account = w3.eth.account.from_key(pk).address w3.eth.set_gas_price_strategy(fast_gas_price_strategy) print(f"Wallet: {w3.eth.default_account}") usdc_address = "0x2791bca1f2de4661ed88a30c99a7a9449aa84174" ctf_address = "0x4d97dcd97ec945f40cf65f87097ace5ea0476045" ctf = w3.eth.contract(ctf_address, abi=merge_positions_abi) condition_id = "0x67eb23e8932765c1d7a094838c928476df8c50d1d3898f278ef1fb2a62afab63" amount = 10_000_000 # 10 USDC worth of tokens (6 decimals) try: txn_hash_bytes = ctf.functions.mergePositions( usdc_address, # The collateral token address HASH_ZERO, # The parent collectionId, always bytes32(0) for Polymarket markets condition_id, # The conditionId of the market [1, 2], # The index set used by Polymarket for binary markets amount, ).transact() txn_hash = w3.to_hex(txn_hash_bytes) print(f"Merge transaction hash: {txn_hash}") w3.eth.wait_for_transaction_receipt(txn_hash) print("Merge complete!") except Exception as e: print(f"Error merging Outcome Tokens : {e}") raise e main() # Expected output: # Wallet: 0xYourAddressHere # Merge transaction hash: 0x789ghi... # Merge complete! ``` -------------------------------- ### Redeem Tokens After Resolution with Python Source: https://context7.com/polymarket/conditional-token-examples-py/llms.txt Redeems outcome tokens for collateral after a market has been resolved. This operation converts winning outcome tokens into USDC based on the final outcome determined by the oracle. It requires the collateral token address, condition ID, and the relevant index set. The function operates on the Conditional Token Factory (CTF) contract. ```python import os from web3 import Web3 from web3.constants import MAX_INT, HASH_ZERO from web3.middleware import ( geth_poa_middleware, construct_sign_and_send_raw_middleware ) from web3.gas_strategies.time_based import fast_gas_price_strategy from dotenv import load_dotenv load_dotenv() redeem_positions_abi = """[{\"constant\":false,\"inputs\":[{\"name\":\"collateralToken\",\"type\":\"address\"},{\"name\":\"parentCollectionId\",\"type\":\"bytes32\"},{\"name\":\"conditionId\",\"type\":\"bytes32\"},{\"name\":\"indexSets\",\"type\":\"uint256[]\"}],\"name\":\"redeemPositions\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"}]""" def main(): pk = os.getenv("PK") rpc_url = os.getenv("RPC_URL") w3 = Web3(Web3.HTTPProvider(rpc_url)) w3.middleware_onion.inject(geth_poa_middleware, layer=0) w3.middleware_onion.add(construct_sign_and_send_raw_middleware(pk)) w3.eth.default_account = w3.eth.account.from_key(pk).address w3.eth.set_gas_price_strategy(fast_gas_price_strategy) print(f"Wallet: {w3.eth.default_account}") usdc_address = w3.to_checksum_address("0x2791bca1f2de4661ed88a30c99a7a9449aa84174") ctf_address = w3.to_checksum_address("0x4d97dcd97ec945f40cf65f87097ace5ea0476045") ctf = w3.eth.contract(ctf_address, abi=redeem_positions_abi) condition_id = "0x67eb23e8932765c1d7a094838c928476df8c50d1d3898f278ef1fb2a62afab63" try: txn_hash_bytes = ctf.functions.redeemPositions( usdc_address, # The collateral token address HASH_ZERO, # The parent collectionId, always bytes32(0) for Polymarket markets condition_id, # The conditionId of the market [1, 2], # The index set used by Polymarket for binary markets ).transact() txn_hash = w3.to_hex(txn_hash_bytes) print(f"Redeem transaction hash: {txn_hash}") w3.eth.wait_for_transaction_receipt(txn_hash) print("Redeem complete!") except Exception as e: print(f"Error redeeming Outcome Tokens : {e}") raise e main() # Expected output: # Wallet: 0xYourAddressHere # Redeem transaction hash: 0xjkl012... # Redeem complete! ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.