Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions typescript/onchain-actions-plugins/registry/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
"@aave/contract-helpers": "^1.32.1",
"@aave/math-utils": "^1.32.1",
"@bgd-labs/aave-address-book": "^4.10.0",
"@ethersproject/wallet": "^5.7.2",
"@polymarket/clob-client": "^4.22.8",
"ethers": "^5.7.2"
},
"peerDependencies": {
Expand Down
1 change: 1 addition & 0 deletions typescript/onchain-actions-plugins/registry/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ export function initializePublicRegistry(chainConfigs: ChainConfig[]) {

export { type ChainConfig, PublicEmberPluginRegistry };
export * from './core/index.js';
export * from './polymarket-perpetuals-plugin/index.js';
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
# Polymarket Perpetuals Plugin

A plugin for the Ember Plugin System that enables trading on Polymarket prediction markets through the CLOB (Central Limit Order Book) API.

## Overview

This plugin integrates Polymarket's prediction markets into the Ember ecosystem, allowing agents to:
- Discover active prediction markets
- Place long positions (BUY YES tokens)
- Place short positions (BUY NO tokens)
- Cancel pending orders
- Query current positions and order history

## Architecture

The plugin maps Polymarket's prediction market model to the Ember perpetuals plugin type:
- **Long positions** β†’ BUY YES tokens (betting on an outcome)
- **Short positions** β†’ BUY NO tokens (betting against an outcome)
- **Markets** β†’ Polymarket events with YES/NO token pairs
- **Positions** β†’ User's YES/NO token holdings
- **Orders** β†’ Pending CLOB orders

## Configuration

The plugin requires the following parameters:

```typescript
{
host?: string; // CLOB API host (default: https://clob.polymarket.com)
chainId: number; // Chain ID (137 for Polygon mainnet)
funderAddress: string; // Polygon address holding USDC for trading
privateKey: string; // Private key for signing orders
signatureType?: number; // 0 = EOA, 1 = Magic/email, 2 = browser wallet (default: 1)
maxOrderSize?: number; // Max shares per order (default: 100)
maxOrderNotional?: number; // Max USDC notional per order (default: 500)
gammaApiUrl?: string; // Gamma API for market data (default: https://gamma-api.polymarket.com)
dataApiUrl?: string; // Data API for user positions (default: https://data-api.polymarket.com)
}
```

## Implementation Status

βœ… **Fully Implemented Features:**

1. **Market Data Integration**: βœ… Complete
- Fetches active markets from Polymarket's Gamma API
- Retrieves market metadata (tickSize, negRisk, liquidity)
- Maps YES/NO token pairs for input/output token mapping
- Implements market caching for performance

2. **Position Tracking**: βœ… Complete
- Fetches positions from Polymarket's Data API
- Falls back to CLOB if Data API unavailable
- Maps YES/NO token holdings to PerpetualsPosition format
- Calculates position sizes and PnL structure

3. **Order Management**: βœ… Complete
- Queries pending orders from CLOB ledger API
- Maps CLOB orders to PerpetualsOrder format
- Implements order cancellation via CLOB DELETE endpoint

4. **Action Implementation**: βœ… Complete
- Long positions (BUY YES tokens) with market validation
- Short positions (BUY NO tokens) with automatic token lookup
- Order cancellation with proper error handling
- Dynamic input/output token population from active markets

## Features

- **Market Discovery**: Query active prediction markets with filtering
- **Position Management**: Track YES/NO token holdings and PnL
- **Order Execution**: Place limit orders with risk limits
- **Order Cancellation**: Cancel pending orders
- **Risk Controls**: Configurable max order size and notional limits
- **Market Caching**: Reduces API calls for better performance

## Usage

```typescript
import { registerPolymarket } from '@emberai/onchain-actions-registry';
import { initializePublicRegistry } from '@emberai/onchain-actions-registry';

const chainConfig = {
chainId: 137, // Polygon
rpcUrl: 'https://polygon-rpc.com',
wrappedNativeToken: '0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270', // WMATIC
};

const registry = initializePublicRegistry([chainConfig]);

registerPolymarket(chainConfig, registry, {
funderAddress: '0x...', // Your Polygon address with USDC
privateKey: '0x...', // Private key for signing orders
signatureType: 1, // 1 = Magic/email login
maxOrderSize: 100, // Max shares per order
maxOrderNotional: 500, // Max USDC notional per order
gammaApiUrl: 'https://gamma-api.polymarket.com', // Optional: custom Gamma API URL
dataApiUrl: 'https://data-api.polymarket.com', // Optional: custom Data API URL
});
```

## API Endpoints Used

- **Gamma API**: `https://gamma-api.polymarket.com/markets` - Market data and metadata
- **CLOB API**: `https://clob.polymarket.com` - Order placement and cancellation
- **Data API**: `https://data-api.polymarket.com` - User positions and balances

## Notes

- Polymarket operates on **Polygon (chain ID 137)**
- The CLOB is an **off-chain order matching system**
- Orders are signed and posted via REST API
- Settlement occurs on-chain after market resolution
- This plugin bridges the off-chain CLOB with Ember's on-chain transaction model
- **USDC Address**: `0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174` (Polygon mainnet)
- Market data is cached to reduce API calls
- Position fetching falls back to CLOB if Data API is unavailable

## References

- [Polymarket CLOB Client](https://github.com/Polymarket/clob-client)
- [Polymarket API Documentation](https://docs.polymarket.com/)
- [Ember Plugin System](../README.md)

Loading