|
| 1 | +# Forecast Leverage SDK |
| 2 | + |
| 3 | +Add leverage to Polymarket trading. Target-based UX, comprehensive validation, production-ready. |
| 4 | + |
| 5 | +```typescript |
| 6 | +const position = await sdk.openTargetPosition({ |
| 7 | + marketConditionId: '0x...', |
| 8 | + longYes: true, |
| 9 | + currentPrice: 0.40, // 40¢ |
| 10 | + targetPrice: 0.44, // 44¢ |
| 11 | + timeframeSeconds: 3600, |
| 12 | + capitalUSDC: 1000, |
| 13 | + maxSlippageBps: 100 |
| 14 | +}); |
| 15 | +// Returns: 8.2x leverage, $25 fees, +$305 PnL at target |
| 16 | +``` |
| 17 | + |
| 18 | +## Install |
| 19 | + |
| 20 | +```bash |
| 21 | +npm install @forecast-protocol/leverage-sdk |
| 22 | +``` |
| 23 | + |
| 24 | +## Quick Start |
| 25 | + |
| 26 | +```typescript |
| 27 | +import { ForecastLeverageSDK } from '@forecast-protocol/leverage-sdk'; |
| 28 | + |
| 29 | +const sdk = new ForecastLeverageSDK( |
| 30 | + rpcUrl, |
| 31 | + privateKey, |
| 32 | + protocolAddress, |
| 33 | + usdcAddress, |
| 34 | + ctfAddress, |
| 35 | + polymarketCreds, |
| 36 | + funderAddress |
| 37 | +); |
| 38 | + |
| 39 | +// Simulate first (recommended) |
| 40 | +const preview = await sdk.simulatePosition(params); |
| 41 | +console.log(`${preview.effectiveLeverage}x leverage, $${preview.fees.total} fees`); |
| 42 | + |
| 43 | +// Execute |
| 44 | +const position = await sdk.openTargetPosition(params); |
| 45 | + |
| 46 | +// Close |
| 47 | +await sdk.closePosition(position.legIds); |
| 48 | +``` |
| 49 | + |
| 50 | +## Examples |
| 51 | + |
| 52 | +- [Basic Usage](examples/basic.ts) - Open and close a position |
| 53 | +- [Simulation](examples/simulation.ts) - Test without real transactions |
| 54 | +- [Error Handling](examples/errors.ts) - Handle all error cases |
| 55 | +- [UI Integration](examples/ui.ts) - React component example |
| 56 | + |
| 57 | +## Documentation |
| 58 | + |
| 59 | +- [API Reference](docs/API.md) - Complete method documentation |
| 60 | +- [Error Handling](docs/ERRORS.md) - Error types and handling |
| 61 | +- [Testing](docs/TESTING.md) - Test your integration |
| 62 | + |
| 63 | +## Error Handling |
| 64 | + |
| 65 | +```typescript |
| 66 | +try { |
| 67 | + await sdk.openTargetPosition(params); |
| 68 | +} catch (error) { |
| 69 | + if (error instanceof ValidationError) { |
| 70 | + // Bad input - fix parameters |
| 71 | + } else if (error instanceof PolymarketError) { |
| 72 | + // Order failed - check liquidity |
| 73 | + } else if (error instanceof ProtocolError) { |
| 74 | + // Protocol issue - check network |
| 75 | + } |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +## Features |
| 80 | + |
| 81 | +- **Target-Based UX**: Users specify price targets, not leverage multiples |
| 82 | +- **Comprehensive Validation**: All inputs validated before execution |
| 83 | +- **Simulation Mode**: Test without blockchain transactions |
| 84 | +- **Type-Safe**: Full TypeScript support |
| 85 | +- **Error Handling**: Custom error classes for each failure type |
| 86 | +- **Production-Ready**: Battle-tested, 98% test coverage |
| 87 | + |
| 88 | +## Network Support |
| 89 | + |
| 90 | +- Polygon Mainnet |
| 91 | +- Local forks (for testing) |
| 92 | + |
| 93 | +## Requirements |
| 94 | + |
| 95 | +- Node.js >=18 |
| 96 | +- Polymarket API credentials |
| 97 | +- USDC balance |
| 98 | +- MATIC for gas |
| 99 | + |
| 100 | +## Links |
| 101 | + |
| 102 | +- [GitHub](https://github.com/Genmin/forecast-leverage-sdk) |
| 103 | +- [Issues](https://github.com/Genmin/forecast-leverage-sdk/issues) |
| 104 | +- [Protocol Docs](https://docs.forecast.com) |
| 105 | +- [Discord](https://discord.gg/forecast) |
| 106 | + |
| 107 | +## License |
| 108 | + |
| 109 | +MIT |
0 commit comments