polycode-sdk-ts
v1.0.1
Published
TypeScript SDK for PolyCode - prediction markets resolved with code
Maintainers
Readme
PolyCode TypeScript SDK
A comprehensive TypeScript SDK for interacting with the PolyCode API. PolyCode allows you to create prediction markets that are resolved with code.
Features
- ✅ Create and manage prediction markets
- ✅ Place and manage bets
- ✅ Query market and bet information
- ✅ Resolve markets with code execution results
- ✅ Full async/await support
- ✅ Type-safe API with comprehensive error handling
- ✅ Builder pattern for flexible configuration
Installation
npm install polycode-sdk-tsQuick Start
Basic Usage
import {
PolyCodeConfig,
PolyCodeClient,
MarketsApi,
BetsApi,
CreateMarketRequest,
PlaceBetRequest,
MarketStatus,
} from 'polycode-sdk-ts';
async function main() {
// Configure the client
const config = new PolyCodeConfig()
.withBaseUrl('https://api.polycode.com')
.withApiKey('your-api-key')
.withTimeout(30);
const client = new PolyCodeClient(config);
// Work with markets
const markets = new MarketsApi(client);
// Create a market
const marketRequest: CreateMarketRequest = {
title: 'Will it rain tomorrow?',
description: 'Predicting weather for tomorrow',
resolution_code: 'check_weather_api()',
code_language: 'python',
outcomes: ['Yes', 'No'],
};
const market = await markets.createMarket(marketRequest);
console.log('Created market:', market.id);
// List open markets
const response = await markets.listMarkets(MarketStatus.Open, 10, undefined);
console.log('Found', response.markets.length, 'markets');
// Get a specific market
const marketDetails = await markets.getMarket(market.id);
console.log('Market status:', marketDetails.status);
// Work with bets
const bets = new BetsApi(client);
// Place a bet
const betRequest: PlaceBetRequest = {
market_id: market.id,
outcome: 'Yes',
amount: 100.0,
currency: 'USD',
};
const bet = await bets.placeBet(betRequest);
console.log('Placed bet:', bet.id);
// List bets for a market
const betResponse = await bets.listBets(market.id, undefined, 10, undefined);
console.log('Found', betResponse.bets.length, 'bets');
}
main().catch(console.error);Configuration
The SDK supports flexible configuration through the PolyCodeConfig builder:
import { PolyCodeConfig } from 'polycode-sdk-ts';
const config = new PolyCodeConfig()
.withBaseUrl('https://api.polycode.com')
.withApiKey('your-api-key')
.withTimeout(60)
.withHeader('X-Custom-Header', 'value');Error Handling
The SDK provides comprehensive error handling:
import { PolyCodeError } from 'polycode-sdk-ts';
async function example() {
try {
const market = await markets.createMarket(request);
console.log('Success:', market.id);
} catch (error) {
if (error instanceof PolyCodeError) {
if (error.type === 'ApiError') {
console.error(`API error ${error.status}: ${error.message}`);
} else if (error.type === 'AuthenticationError') {
console.error('Auth error:', error.message);
} else {
console.error('Error:', error.message);
}
} else {
console.error('Unknown error:', error);
}
}
}API Reference
Markets API
createMarket(request)- Create a new prediction marketgetMarket(id)- Get market by IDlistMarkets(status?, limit?, cursor?)- List markets with optional filtersresolveMarket(id, request)- Resolve a market with code execution resultgetMarketResolution(id)- Get resolution details for a market
Bets API
placeBet(request)- Place a bet on a market outcomegetBet(id)- Get bet by IDlistBets(marketId?, userId?, limit?, cursor?)- List bets with optional filterscancelBet(id)- Cancel a bet (if allowed)
Data Models
Market
Represents a prediction market with the following fields:
id: Unique identifiertitle: Market titledescription: Market descriptionstatus: Current status (Open, Resolved, Cancelled, Pending)resolution_code: Code that will be executed to resolve the marketcode_language: Language/runtime for the resolution codeoutcomes: Possible outcomescreated_at: Creation timestampresolved_at: Resolution timestamp (if resolved)resolved_outcome: The resolved outcome (if resolved)
Bet
Represents a bet placed on a market:
id: Unique identifiermarket_id: ID of the marketoutcome: The outcome being bet onamount: Bet amountcurrency: Currency (optional)user_id: User who placed the betcreated_at: Creation timestampstatus: Bet status (Active, Cancelled, Settled)
License
MIT
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
