You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
BIP-39/BIP-32/BIP-44 wallet generation and derivation
EIP-712 Signing
Full order and auth message signing
Polygon Client
USDC/MATIC balance, transfers, approvals
40+ Type-safe Enums
Every string parameter with known values is a compile-time enum
Category Detection
Auto-detect categories from events, markets, and tags
Installation
Add to your pubspec.yaml:
dependencies:
polybrainz_polymarket: ^3.0.0
dart pub get
Quick Start
Public Client (No Auth Required)
import'package:polybrainz_polymarket/polybrainz_polymarket.dart';
voidmain() async {
final client =PolymarketClient.public();
// Get active eventsfinal events =await client.gamma.events.listEvents(
limit:10,
closed:false,
);
for (final event in events) {
print('${event.title} - ${event.markets.length} markets');
}
// Get order bookfinal book =await client.clob.orderbook.getOrderBook('token-id');
print('Best bid: ${book.bestBid}, Best ask: ${book.bestAsk}');
client.close();
}
Authenticated Client (For Trading)
import'package:polybrainz_polymarket/polybrainz_polymarket.dart';
voidmain() async {
final client =PolymarketClient.authenticated(
credentials:ApiCredentials(
apiKey:'your-api-key',
secret:'your-secret',
passphrase:'your-passphrase',
),
funder:'0xYourWalletAddress',
);
// Get your open ordersfinal orders =await client.clob.orders.getOpenOrders();
print('You have ${orders.length} open orders');
// Get your positionsfinal positions =await client.data.positions.getPositions(
userAddress:'0xYourWalletAddress',
);
for (final pos in positions) {
print('${pos.title}: ${pos.size} shares @ ${pos.avgPrice}');
}
client.close();
}
Real-Time WebSocket Streams
import'package:polybrainz_polymarket/polybrainz_polymarket.dart';
voidmain() async {
final client =PolymarketClient.public();
await client.clobWebSocket.connect();
// Subscribe to price changes
client.clobWebSocket.subscribeToPriceChanges(['token-id-1', 'token-id-2'])
.listen((msg) {
for (final change in msg.priceChanges) {
print('${change.assetId}: ${change.price}');
}
});
// Subscribe to order book updates
client.clobWebSocket.subscribeToOrderBook('token-id')
.listen((book) {
print('Best bid: ${book.bestBid}, Best ask: ${book.bestAsk}');
});
// Crypto prices via RTDSawait client.rtdsWebSocket.connect(topics: [RtdsTopic.cryptoPrices]);
client.rtdsWebSocket.cryptoPrices.listen((msg) {
print('BTC: ${msg.getPrice('BTC')}, ETH: ${msg.getPrice('ETH')}');
});
}
API Reference
Client Factories
Factory
Description
PolymarketClient.public()
Public client, no authentication
PolymarketClient.authenticated()
Authenticated client with API credentials
PolymarketClient.withPrivateKey()
Client with private key for deriving credentials
PolymarketClient.withTrading()
Full trading client with order signing & blockchain ops
Trading (v2.0+)
Place Orders with Full Signing
import'package:polybrainz_polymarket/polybrainz_polymarket.dart';
voidmain() async {
// Create trading-enabled clientfinal client =PolymarketClient.withTrading(
credentials:ApiCredentials(
apiKey:'your-api-key',
secret:'your-secret',
passphrase:'your-passphrase',
),
walletAddress:'0xYourWalletAddress',
privateKey:'0xYourPrivateKey',
);
// Place an order (builds, signs, submits in one call)final response =await client.placeOrder(
tokenId:'token-id',
side:OrderSide.buy,
size:10.0, // 10 shares
price:0.55, // 55 cents
);
print('Order ID: ${response.orderId}');
// Or build and sign manually for more controlfinal signedOrder = client.buildSignedOrder(
tokenId:'token-id',
side:OrderSide.sell,
size:5.0,
price:0.60,
negRisk:false, // Set true for negative risk markets
);
final result =await client.clob.orders.postOrder(
signedOrder.toJson(),
orderType:OrderType.gtc,
);
client.close();
}
Wallet Generation & Management
import'package:polybrainz_polymarket/polybrainz_polymarket.dart';
voidmain() {
// Generate a new HD walletfinal mnemonic =HdWallet.generateMnemonic(); // 12 wordsfinal mnemonic24 =HdWallet.generateMnemonic(wordCount:24);
print('Mnemonic: $mnemonic');
// Validate a mnemonicfinal isValid =HdWallet.validateMnemonic(mnemonic);
print('Valid: $isValid');
// Derive wallet from mnemonicfinal wallet =HdWallet.deriveWallet(mnemonic);
print('Address: ${wallet.address}');
print('Private Key: ${wallet.privateKey}');
// Derive multiple wallets (different indices)final wallet0 =HdWallet.deriveWallet(mnemonic, index:0);
final wallet1 =HdWallet.deriveWallet(mnemonic, index:1);
final wallet2 =HdWallet.deriveWallet(mnemonic, index:2);
}
// Order Bookfinal book =await client.clob.orderbook.getOrderBook('token-id');
final books =await client.clob.orderbook.getOrderBooks(['token-1', 'token-2']);
// Pricingfinal price =await client.clob.pricing.getPrice('token-id', OrderSide.buy);
final midpoint =await client.clob.pricing.getMidpoint('token-id');
final spread =await client.clob.pricing.getSpread('token-id');
final history =await client.clob.pricing.getPriceHistory(
'token-id',
interval:PriceHistoryInterval.hour1,
);
// Using presets (compile-time safe)final events =await client.gamma.events.getByTagSlug(TagSlug.crypto);
final markets =await client.gamma.markets.listMarkets(tagSlug:TagSlug.nfl);
// Custom tags (for user-created tags)final custom =await client.gamma.tags.getBySlug(TagSlug.custom('my-custom-tag'));
// Get TagSlug from Tag modelfinal tag =await client.gamma.tags.getById(123);
final slug = tag.slugEnum; // TagSlug (preset or custom)final preset = tag.slugPreset; // TagSlug? (only if matches a preset)
Category Detection
Automatic Category Detection
import'package:polybrainz_polymarket/polybrainz_polymarket.dart';
// Detect from a single eventfinal result =CategoryDetector.detectFromEvent(event);
print(result.category); // MarketCategory.politicsprint(result.subcategories); // [PoliticsSubcategory.usPresidential]print(result.tagSlugs); // ['politics', 'us-presidential', 'trump']// Extension methods on listsfinal markets =await client.gamma.markets.listMarkets(limit:100);
final categories = markets.detectCategories();
final unique = markets.uniqueCategories; // Set<MarketCategory>// Group by categoryfinal grouped = markets.groupByCategory();
for (final entry in grouped.entries) {
print('${entry.key.label}: ${entry.value.length} markets');
}
// Filter by category using TagSlug (130+ presets)final cryptoMarkets =await client.gamma.markets.getByTagSlug(TagSlug.crypto);
final nflEvents =await client.gamma.events.listEvents(
tagSlug:TagSlug.nfl,
);
Authentication
Using Existing API Credentials
final client =PolymarketClient.authenticated(
credentials:ApiCredentials(
apiKey:'your-api-key',
secret:'your-secret',
passphrase:'your-passphrase',
),
funder:'0xYourWalletAddress',
);
Deriving API Credentials
final client =PolymarketClient.withPrivateKey(
privateKey:'0xYourPrivateKey',
walletAddress:'0xYourWalletAddress',
);
// Create or derive API key using EIP-712 L1 authawait client.clob.auth?.createOrDeriveApiKey();
// Now the client has credentials and can tradefinal orders =await client.clob.orders.getOpenOrders();