Documentation Index
Fetch the complete documentation index at: https://docs.dynamic.xyz/docs/llms.txt
Use this file to discover all available pages before exploring further.
What We’re Building
A React (Next.js) app that connects Dynamic’s embedded wallets to Polymarket prediction markets, allowing users to:- Browse active prediction markets with real-time prices
- Buy Yes/No outcome shares on markets
- View and manage portfolio positions
- Sell positions and redeem winning outcomes
- Deposit funds via multiple methods (QR, credit card, cross-chain)
Key Components
- Dynamic Embedded Wallets - Non-custodial wallets with seamless auth
- Polymarket CLOB Client - Trading SDK for placing orders on Polymarket
- Polygon Network - All trades execute on Polygon mainnet
Building the Application
Project setup
Follow the React Quickstart and use the Custom setup path: Ethereum (EVM) with Wagmi and viem. This recipe’s example uses Next.js and Polygon for trading—scaffold withcreate-next-app and apply the same packages and provider wiring as in the quickstart, or mirror the layout in the GitHub repository linked above.
Dashboard: Under Chains & Networks, enable Polygon (and Ethereum if your flow uses it). Under Sign-in Methods and Wallets, enable what your flow needs (including Embedded wallets). Under Security → Allowed Origins, add the origin where the app runs (for example
http://localhost:3000).Install Dependencies
Add the Polymarket CLOB client and other required dependencies:@polymarket/clob-client provides the SDK for interacting with Polymarket’s Central Limit Order Book (CLOB) for placing and managing orders.
Configure Environment
Create a.env.local file with your Dynamic environment ID:
.env.local
Configure Wagmi for Polygon
Since Polymarket operates on Polygon, configure Wagmi to use Polygon mainnet. Createsrc/lib/wagmi.ts:
src/lib/wagmi.ts
Configure Providers
Set up the providers with Dynamic, Wagmi, and React Query. Createsrc/lib/providers.tsx:
src/lib/providers.tsx
NetworkSwitcher component automatically switches the user’s wallet to Polygon when they connect, ensuring they’re on the correct network for Polymarket.
Define Contract Constants
Createsrc/lib/constants/contracts.ts for Polymarket contract addresses:
src/lib/constants/contracts.ts
Create Markets API Route
Create an API route to fetch markets from Polymarket. Createsrc/app/api/polymarket/route.ts:
src/app/api/polymarket/route.ts
Create Markets Hook
Create a hook to fetch and cache market data. Createsrc/lib/hooks/usePolymarketMarkets.ts:
src/lib/hooks/usePolymarketMarkets.ts
Create Trading Hook
Now let’s build the trading hook that handles placing orders. Createsrc/lib/hooks/usePolymarketTrading.ts:
src/lib/hooks/usePolymarketTrading.ts
- Creating an ethers signer from Dynamic’s wallet client
- Initializing Polymarket API credentials (required for trading)
- Checking USDC balance and approving spender contracts
- Placing market orders via the CLOB client
Create User Positions Hook
Create a hook to fetch user positions. Createsrc/lib/hooks/useUserPositions.ts:
src/lib/hooks/useUserPositions.ts
Build the Market Card Component
Createsrc/components/MarketCard.tsx for displaying individual markets:
src/components/MarketCard.tsx
Build the Main Page
Create the main page that displays markets. Updatesrc/app/page.tsx:
src/app/page.tsx
Run the Application
Start the development server:http://localhost:3000.
Configure CORS
Add your local development URL to the CORS origins in your Dynamic dashboard under Developer Settings > CORS Origins.How Trading Works
When a user places a trade on Polymarket through this app:- Wallet Connection: The user connects via Dynamic’s embedded wallet
- Network Check: The app ensures the user is on Polygon network
- Credential Initialization: The app derives or creates Polymarket API credentials by signing a message
- Approvals: The app checks and requests necessary ERC20/ERC1155 approvals for USDC and outcome tokens
- Order Placement: A market order is submitted to Polymarket’s CLOB API
- Confirmation: The user sees success/error feedback