CTF思路
点击放大

The CTF Exchange V2 is the core smart contract system for trading Conditional Token Framework (CTF) assets on Polymarket. It implements operator-dri matching with support for multiple settlement types, signature schemes, and a wrapped collateral layer.
| Contract | Address |
|----------|---------|
| CollateralToken (impl) | 0x6bBCef9f7ef3B6C592c99e0f206a0DE94Ad0925f |
| CollateralToken (proxy) | 0xC011a7E12a19f7B1f670d46F03B03f3342E82DFB |
| CollateralOnramp | 0x93070a847efEf7F70739046A929D47a521F5B8ee |
| CollateralOfframp | 0x2957922Eb93258b93368531d39fAcCA3B4dC5854 |
| PermissionedRamp | 0xebC2459Ec962869ca4c0bd1E06368272732BCb08 |
| CtfCollateralAdapter | 0xADa100874d00e3331D00F2007a9c336a65009718 |
| NegRiskCtfCollateralAdapter | 0xAdA200001000ef00D07553cEE7006808F895c6F1 |
| CTFExchangeV2 | 0xE111180000d2663C0091e4f400237545B87B996B |
| NegRiskCtfExchangeV2 | 0xe2222d279d744050d28e000310F59 |
| Contract | Address |
|----------|---------|
| CollateralToken (impl) | 0x28A4eaD5bD4847d36F6d046A45e827bdf7781C29 |
| CollateralToken (proxy) | 0xC011a7E12a19f7B1f670d46F03B03f3342E82DFB |
| CollateralOnramp | 0x93070a847efEf7F70739046A929D47a521F5B8ee |
| CollateralOfframp | 0x2957922Eb93258b93368531d39fAcCA3B4dC5854 |
| PermissionedRamp | 0xebC2459Ec962869ca4c0bd1E06368272732BCb08 |
| CtfCollateralAdapter | 0xADa100874d00e3331D00F2007a9c336a65009718 |
| NegRiskCtfCollateralAdapter | 0xAdA200001000ef00D07553cEE7006808F895c6F1 |
| CTFExchangeV2 | 0xE111180000d2663C0091e4f400237545B87B996B |
| NegRiskCtfExchangeV2 | 0xe2222d279d744050d28e000310F59 |
| Auditor | Report |
|---------|--------|
| Quantstamp | CTF Exchange V2 - Quantstamp - March 2026 |
| Cantina | CTF Exchange V2 - Cantina - March 2026 |
Security can be reported through the Cantina bug bounty program.
The exchange uses a mixin composition pattern, where each concern is isolated into its own abstract contract:
1. Users sign EIP-712 typed orders off-chain specifying token, amounts, and side (BUY/SELL)
2. The operator calls matchOrders() with a taker order and array of maker orders
3. The exchange signatures, checks prices cross, and determines the match type
4. Settlement executes based on match type:
- COMPLEMENTARY (BUY vs SELL) — Direct peer-to-peer transfers, no CTF operations
- MINT (both BUY) — Collateral split into outcome tokens via CTF
- MERGE (both SELL) — Outcome tokens merged back into collateral via CTF
| Type | Description |
|------|-------------|
| EOA | Standard ECDSA — signer must equal maker |
| POLY_PROXY | ECDSA + Polymarket proxy wallet ownership |
| POLY_GNOSIS_SAFE | ECDSA + Gnosis Safe ownership |
| POLY_1271 | EIP-1271 smart contract wallet signature |
Orders can also be preapproved by the operator, bypassing signature for subsequent matches.
The exchange trades in PMCT (PolyMarket Collateral Token), an ERC20 wrapper around USDC/USDCe:
builder (origin indicator) and metadata (hashed metadata) fields for richer order attribution.fillOrder / fillOrders — Removed in favor of the unified matchOrders entry point.OrderStatus (filled + remaining).setProxyFactory() / setSafeFactory() removed. Factory addresses are now immutable constructor parameters with address deri computed in pure assembly.V2 was built with gas efficiency as a primary design goal. The optimizations span every layer of the protocol. All numbers below are from equi matchOrders gas snapshot tests (EOA signatures, no fees).
#### V1 vs V2 Comparison
| Operation | Makers | V1 | V2 | Savings | % |
|-----------|--------|-----|-----|---------|---|
| Complementary | 1 | 207,402 | 134,594 | 72,808 | -35% |
| Complementary | 5 | 411,423 | 308,940 | 102,483 | -25% |
| Complementary | 10 | 666,818 | 527,180 | 139,638 | -21% |
| Complementary | 20 | 1,178,855 | 964,688 | 214,167 | -18% |
| Mint | 1 | 297,631 | 278,853 | 18,778 | -6% |
| Mint | 5 | 724,982 | 458,937 | 266,045 | -37% |
| Mint | 10 | 1,259,558 | 684,656 | 574,902 | -46% |
| Mint | 20 | 2,330,028 | 1,138,156 | 1,191,872 | -51% |
| Merge | 1 | 267,846 | 248,241 | 19,605 | -7% |
| Merge | 5 | 684,301 | 434,534 | 249,767 | -37% |
| Merge | 10 | 1,205,260 | 668,014 | 537,246 | -45% |
| Merge | 20 | 2,248,481 | 1,137,026 | 1,111,455 | -49% |
| Combo (comp+mint) | 10 | 954,055 | 683,141 | 270,914 | -28% |
| Combo (comp+mint) | 20 | 1,745,359 | 1,143,608 | 601,751 | -34% |
| Combo (comp+merge) | 10 | 953,338 | 679,820 | 273,518 | -29% |
| Combo (comp+merge) | 20 | 1,724,687 | 1,140,733 | 583,954 | -34% |
The biggest wins are on multi-maker mint/merge (up to 51% savings) where batched CTF operations eliminate per-maker splitPosition/mergePositions calls. Complementary matches see 18-35% savings from the peer-to-peer fast path.
#### Batched CTF Operations
For MINT/MERGE matches with multiple makers, V1 called splitPosition / mergePositions once per maker order. V2 accumulates totals across all makers and executes a single CTF call for the entire batch.
#### Assembly-Optimized Event Emission
All events (OrderFilled, OrdersMatched, FeeCharged) are emitted log2/log3/log4 assembly instructions with manually packed memory layouts, a Solidity's ABI encoding o.
#### Storage-Packed Order Status
OrderStatus packs bool filled (1 byte) and uint248 remaining (31 bytes) into a single 32-byte storage slot. Updates use a single SLOAD + SSTORE with bit operations:
#### Cross-Multiplication Price
V1 computed unit prices for crossing checks. V2 uses cross-multiplication for complementary orders, a di entirely:
#### Branchless Enum Arithmetic
Match type deri and asset ID computation use arithmetic instead of conditionals:
#### EIP-712 Hashing with mcopy
Struct hashing uses the mcopy opcode (EIP-5656) to copy order fields in a single operation instead of field-by-field mstore calls:
#### Pure Assembly Address Deri
Proxy and Safe wallet address computes CREATE2 addresses entirely in assembly — constructing bytecode hashes from raw bytes and pre-computed immutables. No intermediate allocations or ABI encoding.
#### Other Optimizations
maxFeeRateBps storage is only read when the fee is non-zero(A xor B xor B) = A inInstall Foundry.
Foundry has daily updates, run foundryup to update forge and cast.
Run all tests:
Run test functions matching a regex pattern:
Run tests in contracts matching a regex pattern:
Set -vvv to see a stack trace for a failed test.
开发语言:Solidity
Star 数:82
该项目属于CTF思路方向的开源资源,代码结构清晰,适合安全研究人员学习参考。项目围绕Polymarket CTF Exchange v2展开,提供了可运行的实现,对理解相关安全机制、工具原理与攻防技术具有较高的研究价值。
从技术实现角度看,此类项目的核心在于对底层原理的深入理解与工程化落地。开发者在研究过程中可以结合官方文档与社区讨论,梳理项目的初始化流程、核心模块职责与数据流转,逐步掌握其中的技术细节与设计思路。
1. 下载源码压缩包,解压到本地环境。
2. 根据项目类型准备对应的运行环境与依赖。
3. 按照 README 中的步骤完成配置与部署。
4. 在隔离的测试环境中运行验证,避免影响生产系统。
本资源来源于开源社区,仅供安全研究、技术学习与合法授权范围内的攻防演练使用,请勿用于任何非法或侵害他人权益的用途。使用本项目产生的一切后果由使用者自行承担。
| 资源标题 | ctf-exchange-v2 - Polymarket CTF Exchange v2 |
| 所属分类 | CTF思路 |
| 发布作者 | admin |
| 发布时间 | 2026-10-05 23:58:18 |
| 下载权限 | 免费下载 |
| 附件数量 | 2 个下载链接 |
| 浏览次数 | 108 |
| 讨论数量 | 0 |
| 主题状态 | 正常讨论中 |