Skip to content

Instantly share code, notes, and snippets.

@poly-rodr
Last active January 26, 2026 15:19
Show Gist options
  • Select an option

  • Save poly-rodr/44313920481de58d5a3f6d1f8226bd5e to your computer and use it in GitHub Desktop.

Select an option

Save poly-rodr/44313920481de58d5a3f6d1f8226bd5e to your computer and use it in GitHub Desktop.
CLOB Allowance Setting Python + Neg Risk
from web3 import Web3
from web3.constants import MAX_INT
from web3.middleware import geth_poa_middleware
rpc_url = "" # Polygon rpc url
priv_key = "" # Polygon account private key (needs some MATIC)
pub_key = "" # Polygon account public key corresponding to private key
chain_id = 137
erc20_approve = """[{"constant": false,"inputs": [{"name": "_spender","type": "address" },{ "name": "_value", "type": "uint256" }],"name": "approve","outputs": [{ "name": "", "type": "bool" }],"payable": false,"stateMutability": "nonpayable","type": "function"}]"""
erc1155_set_approval = """[{"inputs": [{ "internalType": "address", "name": "operator", "type": "address" },{ "internalType": "bool", "name": "approved", "type": "bool" }],"name": "setApprovalForAll","outputs": [],"stateMutability": "nonpayable","type": "function"}]"""
usdc_address = "0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174"
ctf_address = "0x4D97DCd97eC945f40cF65F87097ACe5EA0476045"
web3 = Web3(Web3.HTTPProvider(rpc_url))
web3.middleware_onion.inject(geth_poa_middleware, layer=0)
nonce = web3.eth.getTransactionCount(pub_key)
usdc = web3.eth.contract(address=usdc_address, abi=erc20_approve)
ctf = web3.eth.contract(address=ctf_address, abi=erc1155_set_approval)
# CTF Exchange
raw_usdc_approve_txn = usdc.functions.approve("0x4bFb41d5B3570DeFd03C39a9A4D8dE6Bd8B8982E", int(MAX_INT, 0)
).buildTransaction({"chainId": chain_id, "from": pub_key, "nonce": nonce})
signed_usdc_approve_tx = web3.eth.account.sign_transaction(raw_usdc_approve_txn, private_key=priv_key)
send_usdc_approve_tx = web3.eth.send_raw_transaction(signed_usdc_approve_tx.rawTransaction)
usdc_approve_tx_receipt = web3.eth.wait_for_transaction_receipt(send_usdc_approve_tx, 600)
print(usdc_approve_tx_receipt)
nonce = web3.eth.getTransactionCount(pub_key)
raw_ctf_approval_txn = ctf.functions.setApprovalForAll("0x4bFb41d5B3570DeFd03C39a9A4D8dE6Bd8B8982E", True).buildTransaction({"chainId": chain_id, "from": pub_key, "nonce": nonce})
signed_ctf_approval_tx = web3.eth.account.sign_transaction(raw_ctf_approval_txn, private_key=priv_key)
send_ctf_approval_tx = web3.eth.send_raw_transaction(signed_ctf_approval_tx.rawTransaction)
ctf_approval_tx_receipt = web3.eth.wait_for_transaction_receipt(send_ctf_approval_tx, 600)
print(ctf_approval_tx_receipt)
nonce = web3.eth.getTransactionCount(pub_key)
# Neg Risk CTF Exchange
raw_usdc_approve_txn = usdc.functions.approve("0xC5d563A36AE78145C45a50134d48A1215220f80a", int(MAX_INT, 0)
).buildTransaction({"chainId": chain_id, "from": pub_key, "nonce": nonce})
signed_usdc_approve_tx = web3.eth.account.sign_transaction(raw_usdc_approve_txn, private_key=priv_key)
send_usdc_approve_tx = web3.eth.send_raw_transaction(signed_usdc_approve_tx.rawTransaction)
usdc_approve_tx_receipt = web3.eth.wait_for_transaction_receipt(send_usdc_approve_tx, 600)
print(usdc_approve_tx_receipt)
nonce = web3.eth.getTransactionCount(pub_key)
raw_ctf_approval_txn = ctf.functions.setApprovalForAll("0xC5d563A36AE78145C45a50134d48A1215220f80a", True).buildTransaction({"chainId": chain_id, "from": pub_key, "nonce": nonce})
signed_ctf_approval_tx = web3.eth.account.sign_transaction(raw_ctf_approval_txn, private_key=priv_key)
send_ctf_approval_tx = web3.eth.send_raw_transaction(signed_ctf_approval_tx.rawTransaction)
ctf_approval_tx_receipt = web3.eth.wait_for_transaction_receipt(send_ctf_approval_tx, 600)
print(ctf_approval_tx_receipt)
nonce = web3.eth.getTransactionCount(pub_key)
# Neg Risk Adapter
raw_usdc_approve_txn = usdc.functions.approve("0xd91E80cF2E7be2e162c6513ceD06f1dD0dA35296", int(MAX_INT, 0)
).buildTransaction({"chainId": chain_id, "from": pub_key, "nonce": nonce})
signed_usdc_approve_tx = web3.eth.account.sign_transaction(raw_usdc_approve_txn, private_key=priv_key)
send_usdc_approve_tx = web3.eth.send_raw_transaction(signed_usdc_approve_tx.rawTransaction)
usdc_approve_tx_receipt = web3.eth.wait_for_transaction_receipt(send_usdc_approve_tx, 600)
print(usdc_approve_tx_receipt)
nonce = web3.eth.getTransactionCount(pub_key)
raw_ctf_approval_txn = ctf.functions.setApprovalForAll("0xd91E80cF2E7be2e162c6513ceD06f1dD0dA35296", True).buildTransaction({"chainId": chain_id, "from": pub_key, "nonce": nonce})
signed_ctf_approval_tx = web3.eth.account.sign_transaction(raw_ctf_approval_txn, private_key=priv_key)
send_ctf_approval_tx = web3.eth.send_raw_transaction(signed_ctf_approval_tx.rawTransaction)
ctf_approval_tx_receipt = web3.eth.wait_for_transaction_receipt(send_ctf_approval_tx, 600)
print(ctf_approval_tx_receipt)
@ShayenK
Copy link

ShayenK commented Nov 27, 2025

Hi This Version wasn't working for me but I made this version that works for the most recent version of web3 as of 27/11/2025

Extra Tips to Help with a Successful Bot:

  • make sure you have USDC.e (contract addy: 0x2791bca1f2de4661ed88a30c99a7a9449aa84174) for the actual trading execution otherwise it will still show balance / allowance error (NOT the other USDC contract addy: 0x3c499c542cef5e3811e1192ce70d8cc03d5c3359)
  • had to also test using a higher fill price i.e. 0.55 as opposed to 0.5 (just when testing)
  • MOST IMPORTANTLY: build out a redemption system for settlement back into your wallet -> last thing you want is to not be able to claim the money that you won
import os
from web3 import Web3
from web3.constants import MAX_INT
from web3.middleware import ExtraDataToPOAMiddleware  # Updated for web3.py v6+
from dotenv import load_dotenv
load_dotenv()

rpc_url = "https://polygon-rpc.com"
priv_key = ""  # Polygon account private key (needs some MATIC)
pub_key = ""  # Polygon account public key corresponding to private key
chain_id = 137

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"}]'
erc1155_set_approval_abi = '[{"inputs": [{ "internalType": "address", "name": "operator", "type": "address" },{ "internalType": "bool", "name": "approved", "type": "bool" }],"name": "setApprovalForAll","outputs": [],"stateMutability": "nonpayable","type": "function"}]'

usdc_address = "0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174"  # USDC.e on Polygon
ctf_address = "0x4D97DCd97eC945f40cF65F87097ACe5EA0476045"   # Conditional Tokens

web3 = Web3(Web3.HTTPProvider(rpc_url))
web3.middleware_onion.inject(ExtraDataToPOAMiddleware, layer=0)

# Get initial nonce
nonce = web3.eth.get_transaction_count(pub_key)

usdc = web3.eth.contract(address=usdc_address, abi=erc20_approve_abi)
ctf = web3.eth.contract(address=ctf_address, abi=erc1155_set_approval_abi)

# Helper to send and wait for tx
def send_and_wait(raw_tx):
    signed_tx = web3.eth.account.sign_transaction(raw_tx, private_key=priv_key)
    tx_hash = web3.eth.send_raw_transaction(signed_tx.raw_transaction)  # Fixed: raw_transaction
    receipt = web3.eth.wait_for_transaction_receipt(tx_hash, timeout=600)
    print(f"Success: {receipt.transactionHash.hex()} (status: {receipt.status})")
    return receipt

# === CTF Exchange ===
# USDC approve
raw_tx = usdc.functions.approve(
    "0x4bFb41d5B3570DeFd03C39a9A4D8dE6Bd8B8982E", int(MAX_INT, 0)
).build_transaction({
    "chainId": chain_id,
    "from": pub_key,
    "nonce": nonce,
    "gasPrice": web3.eth.gas_price  # Auto gas price
})
send_and_wait(raw_tx)
nonce += 1

# CTF setApprovalForAll
raw_tx = ctf.functions.setApprovalForAll("0x4bFb41d5B3570DeFd03C39a9A4D8dE6Bd8B8982E", True).build_transaction({
    "chainId": chain_id,
    "from": pub_key,
    "nonce": nonce,
    "gasPrice": web3.eth.gas_price
})
send_and_wait(raw_tx)
nonce += 1

# === Neg Risk CTF Exchange ===
# USDC approve
raw_tx = usdc.functions.approve(
    "0xC5d563A36AE78145C45a50134d48A1215220f80a", int(MAX_INT, 0)
).build_transaction({
    "chainId": chain_id,
    "from": pub_key,
    "nonce": nonce,
    "gasPrice": web3.eth.gas_price
})
send_and_wait(raw_tx)
nonce += 1

# CTF setApprovalForAll
raw_tx = ctf.functions.setApprovalForAll("0xC5d563A36AE78145C45a50134d48A1215220f80a", True).build_transaction({
    "chainId": chain_id,
    "from": pub_key,
    "nonce": nonce,
    "gasPrice": web3.eth.gas_price
})
send_and_wait(raw_tx)
nonce += 1

# === Neg Risk Adapter ===
# USDC approve
raw_tx = usdc.functions.approve(
    "0xd91E80cF2E7be2e162c6513ceD06f1dD0dA35296", int(MAX_INT, 0)
).build_transaction({
    "chainId": chain_id,
    "from": pub_key,
    "nonce": nonce,
    "gasPrice": web3.eth.gas_price
})
send_and_wait(raw_tx)
nonce += 1

# CTF setApprovalForAll
raw_tx = ctf.functions.setApprovalForAll("0xd91E80cF2E7be2e162c6513ceD06f1dD0dA35296", True).build_transaction({
    "chainId": chain_id,
    "from": pub_key,
    "nonce": nonce,
    "gasPrice": web3.eth.gas_price
})
send_and_wait(raw_tx)

print("All 6 approvals completed! Your wallet is now permanently set up for Polymarket trading.")

@tiagobski
Copy link

@ShayenK Executed your script, no errors, but still getting the not enough balance / allowance error when creating orders

@oncngncd
Copy link

Works with Web3 >= 7.14.0

from web3 import Web3
from web3.constants import MAX_INT
from web3.middleware import ExtraDataToPOAMiddleware

rpc_url = "https://1rpc.io/matic"
priv_key = "[PRIVATE_KEY]" # Polygon account private key (needs some MATIC)
pub_key = "[WALLET_ADDRESS]" # Polygon account public key corresponding to private key
chain_id = 137

erc20_approve = """[{"constant": false,"inputs": [{"name": "_spender","type": "address" },{ "name": "_value", "type": "uint256" }],"name": "approve","outputs": [{ "name": "", "type": "bool" }],"payable": false,"stateMutability": "nonpayable","type": "function"}]"""
erc1155_set_approval = """[{"inputs": [{ "internalType": "address", "name": "operator", "type": "address" },{ "internalType": "bool", "name": "approved", "type": "bool" }],"name": "setApprovalForAll","outputs": [],"stateMutability": "nonpayable","type": "function"}]"""

usdc_address = "0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174" #USDC.e
ctf_address = "0x4D97DCd97eC945f40cF65F87097ACe5EA0476045"

web3 = Web3(Web3.HTTPProvider(rpc_url))
web3.middleware_onion.inject(ExtraDataToPOAMiddleware, layer=0)

nonce = web3.eth.get_transaction_count(web3.to_checksum_address(pub_key))

usdc = web3.eth.contract(address= web3.to_checksum_address(usdc_address), abi=erc20_approve)
ctf = web3.eth.contract(address=web3.to_checksum_address(ctf_address), abi=erc1155_set_approval)

# CTF Exchange


raw_usdc_approve_txn = usdc.functions.approve("0x4bFb41d5B3570DeFd03C39a9A4D8dE6Bd8B8982E", int(MAX_INT, 0)
).build_transaction({"chainId": chain_id, "from": pub_key, "nonce": nonce})


signed_usdc_approve_tx = web3.eth.account.sign_transaction(raw_usdc_approve_txn, private_key=priv_key)
send_usdc_approve_tx = web3.eth.send_raw_transaction(signed_usdc_approve_tx.raw_transaction)
usdc_approve_tx_receipt = web3.eth.wait_for_transaction_receipt(send_usdc_approve_tx, 600)
print(usdc_approve_tx_receipt)

nonce = web3.eth.get_transaction_count(web3.to_checksum_address(pub_key))

raw_ctf_approval_txn = ctf.functions.setApprovalForAll("0x4bFb41d5B3570DeFd03C39a9A4D8dE6Bd8B8982E", True).build_transaction({"chainId": chain_id, "from": pub_key, "nonce": nonce})
signed_ctf_approval_tx = web3.eth.account.sign_transaction(raw_ctf_approval_txn, private_key=priv_key)
send_ctf_approval_tx = web3.eth.send_raw_transaction(signed_ctf_approval_tx.raw_transaction)
ctf_approval_tx_receipt = web3.eth.wait_for_transaction_receipt(send_ctf_approval_tx, 600)
print(ctf_approval_tx_receipt)

nonce = web3.eth.get_transaction_count(web3.to_checksum_address(pub_key))

# Neg Risk CTF Exchange
raw_usdc_approve_txn = usdc.functions.approve("0xC5d563A36AE78145C45a50134d48A1215220f80a", int(MAX_INT, 0)
).build_transaction({"chainId": chain_id, "from": pub_key, "nonce": nonce})
signed_usdc_approve_tx = web3.eth.account.sign_transaction(raw_usdc_approve_txn, private_key=priv_key)
send_usdc_approve_tx = web3.eth.send_raw_transaction(signed_usdc_approve_tx.raw_transaction)
usdc_approve_tx_receipt = web3.eth.wait_for_transaction_receipt(send_usdc_approve_tx, 600)
print(usdc_approve_tx_receipt)

nonce = web3.eth.get_transaction_count(web3.to_checksum_address(pub_key))

raw_ctf_approval_txn = ctf.functions.setApprovalForAll("0xC5d563A36AE78145C45a50134d48A1215220f80a", True).build_transaction({"chainId": chain_id, "from": pub_key, "nonce": nonce})
signed_ctf_approval_tx = web3.eth.account.sign_transaction(raw_ctf_approval_txn, private_key=priv_key)
send_ctf_approval_tx = web3.eth.send_raw_transaction(signed_ctf_approval_tx.raw_transaction)
ctf_approval_tx_receipt = web3.eth.wait_for_transaction_receipt(send_ctf_approval_tx, 600)
print(ctf_approval_tx_receipt)

nonce = web3.eth.get_transaction_count(web3.to_checksum_address(pub_key))

# Neg Risk Adapter
raw_usdc_approve_txn = usdc.functions.approve("0xd91E80cF2E7be2e162c6513ceD06f1dD0dA35296", int(MAX_INT, 0)
).build_transaction({"chainId": chain_id, "from": pub_key, "nonce": nonce})
signed_usdc_approve_tx = web3.eth.account.sign_transaction(raw_usdc_approve_txn, private_key=priv_key)
send_usdc_approve_tx = web3.eth.send_raw_transaction(signed_usdc_approve_tx.raw_transaction)
usdc_approve_tx_receipt = web3.eth.wait_for_transaction_receipt(send_usdc_approve_tx, 600)
print(usdc_approve_tx_receipt)

nonce = web3.eth.get_transaction_count(web3.to_checksum_address(pub_key))

raw_ctf_approval_txn = ctf.functions.setApprovalForAll("0xd91E80cF2E7be2e162c6513ceD06f1dD0dA35296", True).build_transaction({"chainId": chain_id, "from": pub_key, "nonce": nonce})
signed_ctf_approval_tx = web3.eth.account.sign_transaction(raw_ctf_approval_txn, private_key=priv_key)
send_ctf_approval_tx = web3.eth.send_raw_transaction(signed_ctf_approval_tx.raw_transaction)
ctf_approval_tx_receipt = web3.eth.wait_for_transaction_receipt(send_ctf_approval_tx, 600)
print(ctf_approval_tx_receipt)

@Darkness6666
Copy link

Did anyone run into error=place_order failed: PolyApiException[status_code=400, error_message={'error': 'not enough balance / allowance'}] when doing limit sell? Buying works fine for me, but limit sell does not work.

@jcalderonzumba
Copy link

Did anyone run into error=place_order failed: PolyApiException[status_code=400, error_message={'error': 'not enough balance / allowance'}] when doing limit sell? Buying works fine for me, but limit sell does not work.

Happens to me too most of the times when doing sells (not just limit sell). My setup is an EOA.

@Darkness6666
Copy link

Did anyone run into error=place_order failed: PolyApiException[status_code=400, error_message={'error': 'not enough balance / allowance'}] when doing limit sell? Buying works fine for me, but limit sell does not work.

Happens to me too most of the times when doing sells (not just limit sell). My setup is an EOA.

Mine also EOA, does anyone know how to fix/workaround it? My sells only work once for so many that’ve failed.

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