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 JS SDK 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
Scaffold a Next.js project withcreate-next-app. This recipe uses the Dynamic JS SDK (@dynamic-labs-sdk/client + @dynamic-labs-sdk/evm) for authentication and embedded wallet management, with viem for EVM interactions.
Dashboard: Under Chains & Networks, enable Polygon. Under Sign-in Methods and Wallets, enable what your flow needs (including Embedded wallets). Under Security → Allowed Origins, add your origin (for example
http://localhost:3000).Install Dependencies
Add the Dynamic JS SDK and Polymarket dependencies:Configure Environment
Create a.env.local file with your Dynamic environment ID:
.env.local
Initialize Dynamic Client
Createsrc/lib/dynamic.ts to initialize the Dynamic client with the EVM extension:
src/lib/dynamic.ts
Configure Providers
Set up a wallet context using the JS SDK. Createsrc/lib/providers.tsx:
src/lib/providers.tsx
Build the Auth Button
Create a customDynamicButton component at src/components/dynamic/DynamicButton.tsx. This handles Google OAuth, email OTP, and external EVM wallet connection:
src/components/dynamic/DynamicButton.tsx
Create Trading Hook
Build the trading hook that interfaces with the Polymarket CLOB API using a viem wallet client. Createsrc/lib/hooks/usePolymarketTrading.ts:
src/lib/hooks/usePolymarketTrading.ts
createWalletClientForWalletAccount from @dynamic-labs-sdk/evm/viem to get a viem wallet client from the EvmWalletAccount, which is then wrapped with ethers.js for compatibility with the Polymarket CLOB client.
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
Build the Main Page
Updatesrc/app/page.tsx to display markets using the hooks:
src/app/page.tsx
Run the Application
Start the development server:http://localhost:3000.
How Trading Works
When a user places a trade on Polymarket through this app:- Authentication: The user signs in via Google, email OTP, or external EVM wallet using the custom
DynamicButton - Wallet Provisioning: After auth, an EVM embedded wallet is automatically created via
createWaasWalletAccounts - Wallet Client:
createWalletClientForWalletAccountfrom@dynamic-labs-sdk/evm/viemcreates a viem-compatible wallet client - 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
Conclusion
If you want to take a look at the full source code, check out the GitHub repository. This integration demonstrates how Dynamic’s JS SDK embedded wallets can seamlessly connect to prediction markets like Polymarket. The@dynamic-labs-sdk/evm package provides the createWalletClientForWalletAccount helper to bridge embedded wallets with viem, enabling complex trading operations with minimal friction.