Polymarket Underlying Algorithm Explained in Full
- Core Thesis: This article provides an in-depth breakdown of the technical architecture behind Polymarket's prediction market, covering order intents, a hybrid off-chain matching and on-chain settlement model, three matching mechanisms (Complementary, Mint, Merge), PnL calculation pitfalls, and how the V2 upgrade solved the "Ghost Fill" problem while introducing the Deposit Wallet.
- Key Takeaways:
- Polymarket orders are essentially off-chain signed "intents" (EIP-712), matched by a centralized Operator and settled on-chain. Users don't pay gas fees; the Relayer covers them upfront.
- Three matching paths: COMPLEMENTARY (traditional buyer-to-seller trading), MINT (buyer-to-buyer, creating liquidity through minting), and MERGE (seller-to-seller, destroying tokens to free up capital), designed to solve market cold-start and exit issues.
- Correct PnL calculation requires considering the net cash flow of all actions (Split, Merge, Redeem), not just relying on transaction records; multi-outcome markets achieve asset conversion and guarantee value conservation through the NegRiskAdapter.
- The V2 upgrade introduced a smart contract-controlled Deposit Wallet, limiting users' immediate control over funds, and reducing the Ghost Fill ratio from 30% to 0.17%.
Author: @MrRyanChi, Founder of @insidersdotbot Prediction Market Trading Platform
Prologue: The Side of Polymarket You Don't Know
Over the past six months, hundreds of millions of prediction market articles have appeared on Twitter.
90% of them are about how AI programming brings get-rich-quick stories. This is "Yuan" (fate), your first step into this nascent market.
Another 9% cover specific trading strategies, market insights, and analyses of smart money strategies. This is "Dao" (the path), your initial step in exploring your own trading strategies and understanding how to make money in prediction markets.
However, the "Fa" (the method), which encompasses the underlying trading design, PNL calculation, and rules of money flow in prediction markets, is discussed by only that remaining 1%, mostly scattered across some short, concise tweets. These reclusive experts seem unwilling or lack the energy to share their complete methodology with everyone all at once.
So, today, with insiders.bot just launched and Polymarket having completed its V2 update, I want to deconstruct the core "Fa" of this market we've been trading from the most fundamental technical level.
Last October, I wrote a simplified version, giving a rough overview of Polymarket's core components. This time, I aim to truly show you all the technical design details and explain them in plain English.
This article encapsulates the hard work of our team over the past eight months.
During these eight months, the @insidersdotbot team deconstructed all of Polymarket's underlying smart contracts and algorithmic architecture to achieve the fastest trading and most accurate PNL calculation. This is something only our proprietary API can achieve; to this day, no one else has managed it.
Therefore, I believe we might genuinely be the most qualified to deconstruct the "Fa" underlying Polymarket.
In this article, I will guide you through understanding how the underlying ctf-exchange-v2 smart contract handles every unit of capital, how the Relayer pays your Gas fees upfront, and how Negative Risk mathematically ensures value conservation.
This is not a simple introductory guide. It is a comprehensive algorithmic deconstruction of Polymarket's underlying mechanisms from a developer's perspective.
Let's start with the fundamentals. That is, when you place an order, what exactly are you sending?
P.S This article has also been adapted in style and structure by AI. Feel free to send it to your OpenClaw, Manus, Hermes, or any AI Agent as training material!!!
Chapter 1: From Click to On-Chain, What Actually Happens
1.1 An Order is Not a Transaction, It's an "Intention"
In traditional decentralized exchanges (like Uniswap), when you trade, your wallet pops up a confirmation window, you pay Gas fees, and a Transaction is sent to the blockchain's Mempool to wait for miners to include it.
But on Polymarket, when you place an order, your wallet usually pops up a "Sign" request, not a "Transaction" request. Moreover, you don't need to pay any Gas.
This is not just a User Experience optimization; it's a fundamental difference in the underlying architecture.
On Polymarket, an Order is essentially a piece of structured data conforming to the EIP-712 standard. This data contains what you want to do:
- Are you the Maker or the Taker? Which Token (tokenId) do you want to buy?
- How much are you willing to pay (makerAmount)?
- How much do you want to receive (takerAmount)?
When you sign, you are essentially "stamping" this data with your private key, proving "I indeed intend to do this." Then, this signed data is sent to Polymarket's centralized servers and stored in an off-chain Central Limit Order Book (CLOB).
At this stage, nothing happens on the blockchain. Your money is still in your wallet, no tokens have been transferred. Your order is just a single line of data in a database.
1.2 Implicit Expression of Price
Let's pause time at the moment you send this order. If you look closely at the order structure in Polymarket's underlying contracts, you'll find something very counter-intuitive: The order signature data does not contain a "Price" field.
How is this possible? How can you trade without a price?
In Polymarket's protocol bottom-level design, the price is implicit. It is calculated from the quantity you are willing to pay and the quantity you want to receive.
If you want to buy 100 YES contracts at $0.60:
- You need to pay: $60 pUSD (makerAmount = 60)
- You want to receive: 100 YES contracts (takerAmount = 100)
- Implied Price = makerAmount / takerAmount = 60 / 100 = $0.60
If you want to sell 100 YES contracts at $0.60:
- You need to pay: 100 YES contracts (makerAmount = 100)
- You want to receive: $60 pUSD (takerAmount = 60)
- Implied Price = takerAmount / makerAmount = 60 / 100 = $0.60
(Note: Although in the latest V2 SDK, developers can directly pass in price and size, the SDK still converts these into makerAmount and takerAmount during the underlying signing process. The cleverness of this design is that the smart contract doesn't need to understand what "price" means; it only needs to handle the logic of "Asset A for Asset B." This greatly simplifies on-chain computation logic and reduces Gas consumption.)
1.3 Operator: Polymarket's "Traffic Controller"
Since the orders are all off-chain, how do they become real on-chain asset transfers?
This leads to the most central black-box role in Polymarket's architecture: the Operator.
In the ctf-exchange-v2 smart contract, there is a crucial modifier: onlyOperator. This means only the specific address controlled by Polymarket's officials has permission to call execution functions like matchOrders and fillOrder.
This is completely different from traditional DeFi. On Uniswap, anyone can call the router contract. But on Polymarket, you cannot go on-chain to match trades yourself. All matching must be submitted by the Operator.
Why design it this way? To eliminate MEV (Miner Extractable Value) and Front-running.
In a traditional on-chain order book, if someone places a large order at a cheap price, all arbitrage bots will bid furiously in the Mempool (by raising Gas fees), trying to eat the order before anyone else. This causes Gas fees to skyrocket and creates a terrible experience for regular users.
On Polymarket, however, all orders reside in the off-chain CLOB. The Operator's Matching Engine computes who should trade with whom on the server side, packages the result into a single transaction, and the Operator sends it to the chain.
Because only the Operator can submit matching results, even if bots in the Mempool see this transaction, they cannot front-run it because they lack the permission to call the execution functions.
This is a typical "hybrid decentralized" architecture. Matching and ordering are centralized (decided by the Operator), but settlement and custody of funds are decentralized (executed by the smart contract).
The Operator can decide who gets matched first, but it can absolutely never steal your funds, as it must provide the EIP-712 data you signed, and the contract will strictly verify the signature.
P.S: However, I should mention this briefly. We at @insidersdotbot seem to have recently discovered a potential exploit in this mechanism that could allow copy-trading to front-run or achieve significantly reduced latency. If there are any updates, we will announce them immediately on our official account.
Chapter 2: The Economics of the Relayer
2.1 The Illusion of "Gasless" Trading
One of Polymarket's biggest selling points is "Gasless Transactions" for users. You only need pUSD to trade; you don't need to buy POL (formerly MATIC) to hold in your wallet.
But the laws of blockchain physics are absolute: as long as a state change occurs on Polygon (like an asset transfer), someone must pay the Gas fee.
Since you didn't pay, who did? The answer is: the Relayer.
2.2 The Relay Network
Polymarket doesn't make users send transactions themselves. Instead, it deploys a set of infrastructure called the Relayer Client (relayer-v2.polymarket.com).
In earlier architectures, this type of service often relied on enterprise-grade services like OpenZeppelin Defender Relay, maintaining a Signer Pool to solve nonce conflict issues under high concurrency.
When your App creates a transaction (e.g., Approve tokens, Redeem profits), you sign it with your private key and send it to the Relayer. The Relayer acts as a Transaction Sponsor, submits this transaction to the chain, and pays the Gas fee upfront from its own fund pool.

Relayer Architecture and Economic Cycle
2.3 Is There Such a Thing as a Free Lunch?
In many earlier Meta-transaction architectures, after the Relayer fronted the Gas fee, it would usually deduct a handling fee (e.g., 0.3% or a fixed few dollars) from the user's deposit to cover the Gas cost.
But Polymarket is extremely aggressive: in the current V2 architecture, they genuinely cover the cost for you entirely.
The official documentation clearly states: "Polymarket pays gas for all operations routed through the relayer." Whether it's deploying a wallet, approving tokens, splitting, merging, or redeeming, it's all Gas-free, with no hidden operational fees.
Why is Polymarket willing to take this loss?
Because the Gas cost on Polygon is extremely low (often just a few cents), and the smooth experience offered by Gasless trading can attract a massive influx of Web2 users. As long as users generate a small Taker fee during trading (explained later), it's enough to cover this very low Gas cost.
Knowing this, the next question naturally arises: what impact does this "Gasless" architecture have on our trading?
The biggest hidden cost is Latency. Your order not only has to pass through Polymarket's matching engine but, for direct on-chain operations, also through the Relayer's verification, Gas estimation, and queue allocation.
Chapter 3: Three Matching Methods, and Why Can a Buyer and a Buyer Still Trade?
Now we enter the most hardcore and counter-intuitive part of the entire Polymarket architecture.
In traditional exchanges (like Binance's order book), the matching logic is very simple: Alice wants to buy 1 token for $60, Bob wants to sell 1 token for $60. The exchange matches them, token goes from Bob to Alice, money goes from Alice to Bob. End of story.
But on Polymarket (based on the Condition Token Framework CTF), things are completely different. Because here, tokens can be "printed out of thin air" and "burned into nothingness."
When you open the source code of ctf-exchange-v2, you will find three completely different underlying asset settlement paths: COMPLEMENTARY, MINT, and MERGE.

General Structure of Complementary, Mint, and Merge
3.1 COMPLEMENTARY (Complementary Matching): Traditional Secondary Trading
This is the easiest matching method to understand and the only method traditional exchanges have.
Scenario: The market has existed for a while, and everyone holds tokens.
- Alice wants to buy 100 YES at $0.60.
- Bob holds YES and wants to sell 100 YES at $0.60.
The Operator finds these two orders (BUY vs. SELL), packages them, and sends them on-chain. The smart contract executes a direct peer-to-peer transfer:
- Transfer 100 YES from Bob's address to Alice.
- Transfer $60 pUSD from Alice's address to Bob.
This mechanism has the following mathematical and engineering characteristics:
- Zero-Sum Game: The total token supply of the system has not changed at all.
- Lowest Gas Consumption: Only involves basic transfers, no complex CTF operations.
- Standard: In a mature, liquid market, the vast majority of daily transactions are of this type.
3.2 MINT (Mint Matching): Creating Liquidity Out of Thin Air
This might be the most revolutionary innovation in Polymarket, or even in the entire history of finance.
For a better explanation, consider this scenario: A brand new market just launched, and no one holds any YES or NO tokens.
- Alice is extremely bullish and wants to buy 100 YES at $0.60.
- Bob is extremely bearish and wants to buy 100 NO at $0.40.
Note: They are both buyers! Neither has the token the other wants!
In a traditional order book, these two orders would just stare at each other, never being able to match. On Polymarket, if it encounters BUY vs. BUY (with complementary tokens), the Operator will match these two orders together!
- The smart contract deducts $60 pUSD


