Overview
Polymarket’s Builders program allows apps and order routers to generate and share revenue on the trades they helped create. Learn more about the program here.What does the Turnkey Safe Builder Example do?
This open-source demo allows users to log-in and make wallets, then use Polymarket’s CLOB and Builder Relayer clients for gasless trading with Builder-program order attribution.- Authenticate users via Turnkey email login for web2-style onboarding
- Provision an EOA wallet automatically via Turnkey’s embedded wallets
- Deploy a Gnosis Safe Proxy Wallet using the builder-relayer-client
- Obtain User API Credentials from the CLOB client
- Set token approvals for CTF Contract, CTF Exchange, Neg Risk Exchange, and Neg Risk Adapter
- Place orders via CLOB client with builder attribution using remote signing
Getting started
Before running this demo, you need:-
Builder API Credentials from Polymarket
- Visit
polymarket.com/settings?tab=builderto obtain your Builder credentials - You’ll need:
API_KEY,SECRET, andPASSPHRASE
- Visit
-
Polygon RPC URL
- Any Polygon mainnet RPC (Alchemy, Infura, or public RPC)
-
Turnkey Organization
- Sign up at turnkey.com and create an organization
- Get your Organization ID and Auth Proxy Config ID from the Turnkey Dashboard
Install dependencies
Installation.env.local:
Turnkey Authentication
Files:providers/WalletProvider.tsx, providers/WalletContext.tsx
Users authenticate via Turnkey’s React Wallet Kit, which handles email login and automatically provisions a non-custodial EOA embedded wallet. No browser extension required.
Builder Config with Remote Signing
Fileapp/api/polymarket/sign/route.ts
Builder credentials are stored server-side and accessed via a remote signing endpoint. This keeps your builder credentials secure while enabling order attribution or relay authentication.
Why remote signing?
- Builder credentials never exposed to client
- Secure HMAC signature generation
- Required for builder order attribution (with ClobClient) or authentication (RelayClient)
Safe Deployment
File:hooks/useSafeDeployment.ts
The Safe address is deterministically derived from the user’s Turnkey EOA, then deployed if it doesn’t exist.
Important:
- Safe address is deterministic - same EOA always gets same Safe address
- Safe is the “funder” address that holds USDC.e and outcome tokens
- One-time deployment per EOA on user’s first login
- Turnkey handles the signature request
Token Approvals
Files:hooks/useTokenApprovals.ts, utils/approvals.ts
Before trading, the Safe must approve multiple contracts to spend USDC.e and manage outcome tokens. This involves setting approvals for both ERC-20 (USDC.e) and ERC-1155 (outcome tokens).
Key Points:
- Uses batch execution via
relayClient.execute()for gas efficiency - Sets unlimited approvals (MaxUint256) for ERC-20 tokens
- Sets operator approvals for ERC-1155 outcome tokens
- One-time setup per Safe (persists across sessions)
- User signs once to approve all transactions (Turnkey handles signature)
- Gasless for the user
Required Approvals
USDC.e (ERC-20) Approvals:- CTF Contract:
0x4d97dcd97ec945f40cf65f87097ace5ea0476045 - CTF Exchange:
0x4bFb41d5B3570DeFd03C39a9A4D8dE6Bd8B8982E - Neg Risk CTF Exchange:
0xC5d563A36AE78145C45a50134d48A1215220f80a - Neg Risk Adapter:
0xd91E80cF2E7be2e162c6513ceD06f1dD0dA35296
- CTF Exchange:
0x4bFb41d5B3570DeFd03C39a9A4D8dE6Bd8B8982E - Neg Risk CTF Exchange:
0xC5d563A36AE78145C45a50134d48A1215220f80a - Neg Risk Adapter:
0xd91E80cF2E7be2e162c6513ceD06f1dD0dA35296
Implementation
Approval Transaction Structure
Each approval transaction is aSafeTransaction:
Why Multiple Approvals?
Polymarket’s trading system uses different contracts for different market types:- CTF Contract: Manages outcome tokens (ERC-1155)
- CTF Exchange: Standard binary markets
- Neg Risk CTF Exchange: Negative risk markets (mutually exclusive outcomes)
- Neg Risk Adapter: Converts between neg risk and standard markets
- Users can trade in any market type
- One-time setup (approvals persist across sessions)
- Gasless execution via RelayClient
- Single user signature for all approvals
Checking Approvals
Before setting approvals, the app checks onchain state:Placing Orders
File:hooks/useClobOrder.ts
With the authenticated ClobClient, you can place orders with builder attribution.
Key Points:
- Orders are signed by the user’s Turnkey EOA (via
TurnkeyEthersSigner._signTypedData) - Executed from the Safe address (funder)
- Builder attribution is automatic via builderConfig
- Gasless execution (no gas fees for users)
Conclusion
With this integration you can:- Onboard users easily, with a variety of sign-in methods.
- Scale with ease.
- Create and manage Polymarket orders with order attribution