Code Examples

Practical examples for common use cases.

Create a Binary Market

Yes/No prediction market

import { GalaxiaPredictionsSDK, MarketType } from '@galaxia-predictions-market/sdk';
import { Connection, Wallet, PublicKey } from '@solana/web3.js';

const sdk = new GalaxiaPredictionsSDK(connection, wallet);

const tx = await sdk.createMarket({
  marketId: 'bitcoin-100k-2024',
  question: 'Will Bitcoin reach $100,000 by December 31, 2024?',
  marketType: MarketType.BINARY,
  outcomes: ['Yes', 'No'],
  endTime: Math.floor(new Date('2024-12-31').getTime() / 1000),
  oracle: new PublicKey('oracle-public-key'),
});

console.log('Market created:', tx);

Create a Multiple Choice Market

Election or multi-outcome market

const tx = await sdk.createMarket({
  marketId: 'election-2024',
  question: 'Who will win the 2024 US Presidential Election?',
  marketType: MarketType.MULTIPLE_CHOICE,
  outcomes: ['Candidate A', 'Candidate B', 'Candidate C', 'Other'],
  endTime: Math.floor(new Date('2024-11-05').getTime() / 1000),
  oracle: new PublicKey('oracle-public-key'),
});

Trade on a Market

Buy outcome tokens

// Get market first
const market = await sdk.getMarket('bitcoin-100k-2024');

// Trade on "Yes" outcome (index 0)
const tx = await sdk.trade({
  marketId: 'bitcoin-100k-2024',
  outcomeIndex: 0, // "Yes"
  amount: 1000000000, // 1 GXA (with 9 decimals)
});

console.log('Trade executed:', tx);

Add Liquidity to a Market

Provide liquidity for trading

const tx = await sdk.addLiquidity({
  marketId: 'bitcoin-100k-2024',
  amount: 10000000000, // 10 GXA
});

console.log('Liquidity added:', tx);

Resolve a Market

Oracle resolves the outcome

// Only the oracle can resolve
// Outcome index 0 = "Yes", 1 = "No"
const tx = await sdk.resolveMarket('bitcoin-100k-2024', 0);

console.log('Market resolved:', tx);

Redeem Winning Tokens

After market resolution

// Check if market is resolved
const market = await sdk.getMarket('bitcoin-100k-2024');

if (market.isResolved) {
  // Redeem winning outcome tokens
  const tx = await sdk.redeemOutcomeTokens('bitcoin-100k-2024');
  console.log('Tokens redeemed:', tx);
}

Using REST API

HTTP requests example

// Get all markets
const markets = await fetch('http://localhost:3000/api/markets')
  .then(res => res.json());

// Create market
const newMarket = await fetch('http://localhost:3000/api/markets', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer YOUR_TOKEN',
  },
  body: JSON.stringify({
    marketId: 'my-market',
    question: 'Will X happen?',
    marketType: 'BINARY',
    outcomes: ['Yes', 'No'],
    endTime: '1735689600',
    oracle: 'oracle-address',
  }),
}).then(res => res.json());