|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +require_once __DIR__.'/../vendor/autoload.php'; |
| 6 | + |
| 7 | +use Danielgnh\PolymarketPhp\Client; |
| 8 | + |
| 9 | +/** |
| 10 | + * Example: Fetching user positions and analytics from Data API. |
| 11 | + * |
| 12 | + * This example demonstrates how to use the Data API to: |
| 13 | + * - Get current (open) positions for a user |
| 14 | + * - Get closed positions (historical) |
| 15 | + * - Calculate total position value |
| 16 | + * - Get trade history |
| 17 | + * - Get on-chain activity |
| 18 | + * - Get market analytics (holders, open interest, volume) |
| 19 | + * - Get leaderboard data |
| 20 | + */ |
| 21 | + |
| 22 | +// Initialize client (no authentication needed for Data API) |
| 23 | +$client = new Client(); |
| 24 | + |
| 25 | +// Example wallet address |
| 26 | +$walletAddress = '0x0000000000000000000000000000000000000000'; |
| 27 | + |
| 28 | +echo "Data API Examples\n"; |
| 29 | +echo "=================\n\n"; |
| 30 | + |
| 31 | +// 1. Get current (open) positions |
| 32 | +try { |
| 33 | + echo "1. Fetching current positions...\n"; |
| 34 | + $positions = $client->data()->positions()->get($walletAddress); |
| 35 | + echo 'Found '.count($positions)." open positions\n\n"; |
| 36 | +} catch (Exception $e) { |
| 37 | + echo "Error: {$e->getMessage()}\n\n"; |
| 38 | +} |
| 39 | + |
| 40 | +// 2. Get closed positions |
| 41 | +try { |
| 42 | + echo "2. Fetching closed positions...\n"; |
| 43 | + $closedPositions = $client->data()->positions()->closed($walletAddress); |
| 44 | + echo 'Found '.count($closedPositions)." closed positions\n\n"; |
| 45 | +} catch (Exception $e) { |
| 46 | + echo "Error: {$e->getMessage()}\n\n"; |
| 47 | +} |
| 48 | + |
| 49 | +// 3. Get total position value |
| 50 | +try { |
| 51 | + echo "3. Calculating total position value...\n"; |
| 52 | + $value = $client->data()->positions()->value($walletAddress); |
| 53 | + echo "Total value: \${$value['total']}\n\n"; |
| 54 | +} catch (Exception $e) { |
| 55 | + echo "Error: {$e->getMessage()}\n\n"; |
| 56 | +} |
| 57 | + |
| 58 | +// 4. Get trade history |
| 59 | +try { |
| 60 | + echo "4. Fetching trade history...\n"; |
| 61 | + $trades = $client->data()->trades()->get($walletAddress, ['limit' => 10]); |
| 62 | + echo 'Found '.count($trades)." recent trades\n\n"; |
| 63 | +} catch (Exception $e) { |
| 64 | + echo "Error: {$e->getMessage()}\n\n"; |
| 65 | +} |
| 66 | + |
| 67 | +// 5. Get on-chain activity |
| 68 | +try { |
| 69 | + echo "5. Fetching on-chain activity...\n"; |
| 70 | + $activity = $client->data()->activity()->get($walletAddress, ['limit' => 10]); |
| 71 | + echo 'Found '.count($activity)." activity records\n\n"; |
| 72 | +} catch (Exception $e) { |
| 73 | + echo "Error: {$e->getMessage()}\n\n"; |
| 74 | +} |
| 75 | + |
| 76 | +// 6. Get market analytics (example condition ID) |
| 77 | +$conditionId = '0x0000000000000000000000000000000000000000000000000000000000000000'; |
| 78 | + |
| 79 | +try { |
| 80 | + echo "6. Fetching market holders...\n"; |
| 81 | + $holders = $client->data()->analytics()->holders($conditionId, ['limit' => 5]); |
| 82 | + echo 'Found '.count($holders)." top holders\n\n"; |
| 83 | +} catch (Exception $e) { |
| 84 | + echo "Error: {$e->getMessage()}\n\n"; |
| 85 | +} |
| 86 | + |
| 87 | +try { |
| 88 | + echo "7. Fetching open interest...\n"; |
| 89 | + $openInterest = $client->data()->analytics()->openInterest($conditionId); |
| 90 | + echo "Open interest data retrieved\n\n"; |
| 91 | +} catch (Exception $e) { |
| 92 | + echo "Error: {$e->getMessage()}\n\n"; |
| 93 | +} |
| 94 | + |
| 95 | +// 8. Get leaderboard |
| 96 | +try { |
| 97 | + echo "8. Fetching leaderboard...\n"; |
| 98 | + $leaderboard = $client->data()->leaderboards()->get(['limit' => 10]); |
| 99 | + echo 'Found '.count($leaderboard)." top traders\n\n"; |
| 100 | +} catch (Exception $e) { |
| 101 | + echo "Error: {$e->getMessage()}\n\n"; |
| 102 | +} |
| 103 | + |
| 104 | +// 9. Get builder leaderboard |
| 105 | +try { |
| 106 | + echo "9. Fetching builder leaderboard...\n"; |
| 107 | + $builderLeaderboard = $client->data()->leaderboards()->builder(['limit' => 10]); |
| 108 | + echo 'Found '.count($builderLeaderboard)." builders\n\n"; |
| 109 | +} catch (Exception $e) { |
| 110 | + echo "Error: {$e->getMessage()}\n\n"; |
| 111 | +} |
| 112 | + |
| 113 | +// 10. Check API health |
| 114 | +try { |
| 115 | + echo "10. Checking Data API health...\n"; |
| 116 | + $health = $client->data()->health()->check(); |
| 117 | + echo "Status: {$health['status']}\n"; |
| 118 | +} catch (Exception $e) { |
| 119 | + echo "Error: {$e->getMessage()}\n"; |
| 120 | +} |
0 commit comments