Skip to main content

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)
If you want to take a quick look at the final code, check out the GitHub repository.

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 with create-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 SecurityAllowed 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

Create src/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. Create src/lib/providers.tsx:
src/lib/providers.tsx

Build the Auth Button

Create a custom DynamicButton 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. Create src/lib/hooks/usePolymarketTrading.ts:
src/lib/hooks/usePolymarketTrading.ts
The key difference from the React SDK version is using 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. Create src/app/api/polymarket/route.ts:
src/app/api/polymarket/route.ts

Build the Main Page

Update src/app/page.tsx to display markets using the hooks:
src/app/page.tsx

Run the Application

Start the development server:
The application will be available at http://localhost:3000.

How Trading Works

When a user places a trade on Polymarket through this app:
  1. Authentication: The user signs in via Google, email OTP, or external EVM wallet using the custom DynamicButton
  2. Wallet Provisioning: After auth, an EVM embedded wallet is automatically created via createWaasWalletAccounts
  3. Wallet Client: createWalletClientForWalletAccount from @dynamic-labs-sdk/evm/viem creates a viem-compatible wallet client
  4. Credential Initialization: The app derives or creates Polymarket API credentials by signing a message
  5. Approvals: The app checks and requests necessary ERC20/ERC1155 approvals for USDC and outcome tokens
  6. Order Placement: A market order is submitted to Polymarket’s CLOB API
  7. 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.

Additional Resources

Last modified on May 21, 2026