Create clients to connect to the Algorand blockchain:
import { AlphaClient } from '@alpha-arcade/sdk';import algosdk from 'algosdk';// Connect to Algorand mainnet (using public nodes)const algodClient = new algosdk.Algodv2( '', 'https://mainnet-api.algonode.cloud', 443);const indexerClient = new algosdk.Indexer( '', 'https://mainnet-idx.algonode.cloud', 443);
These are free public nodes. For production apps with high traffic, consider using a dedicated node provider like AlgoNode or Nodely.
3
Setup Your Wallet Signer
Create a transaction signer from your mnemonic:
// Get your mnemonic from environment variableconst mnemonic = process.env.ALGORAND_MNEMONIC || 'your twenty five word mnemonic here...';// Convert mnemonic to accountconst account = algosdk.mnemonicToSecretKey(mnemonic);// Create a transaction signerconst signer = algosdk.makeBasicAccountTransactionSigner(account);console.log('Wallet address:', account.addr);
For production applications, use secure wallet integrations like Pera Wallet or Defly Wallet instead of managing mnemonics directly.
4
Initialize Alpha Client
Create an instance of AlphaClient with your configuration:
const client = new AlphaClient({ algodClient, indexerClient, signer, activeAddress: account.addr.toString(), matcherAppId: 3078581851, // Alpha matcher contract on mainnet usdcAssetId: 31566704, // USDC on Algorand mainnet});console.log('Alpha client initialized!');
The matcherAppId and usdcAssetId values shown here are for Algorand mainnet. See Configuration for testnet values.
5
Fetch Live Markets
Get all active prediction markets:
// Fetch all live markets from the blockchainconst markets = await client.getLiveMarkets();console.log(`Found ${markets.length} live markets`);// Display the first marketif (markets.length > 0) { const market = markets[0]; console.log('Market:', market.title); console.log('Market App ID:', market.marketAppId); console.log('YES Asset:', market.yesAssetId); console.log('NO Asset:', market.noAssetId);}
Without an API key, getLiveMarkets() discovers markets directly from the blockchain. To get richer market data (images, categories, volume), add an API key to your client config. See Installation.
6
Place Your First Order
Create a limit order on a market:
// Select the first marketconst market = markets[0];// Place a limit buy order for YES at $0.50const result = await client.createLimitOrder({ marketAppId: market.marketAppId, position: 1, // 1 = Yes, 0 = No price: 500_000, // $0.50 in microunits quantity: 1_000_000, // 1 share in microunits isBuying: true,});console.log('Order created successfully!');console.log('Escrow App ID:', result.escrowAppId);console.log('Transaction IDs:', result.txIds);console.log('Confirmed in round:', result.confirmedRound);if (result.matchedQuantity && result.matchedQuantity > 0) { console.log(`Matched ${result.matchedQuantity / 1_000_000} shares at $${result.matchedPrice! / 1_000_000}`);}
Limit orders execute at your exact price (zero slippage). If there’s a matching order on the book, your order may fill immediately. Otherwise, it sits on the orderbook until matched.
Make sure your wallet has enough USDC to cover the order value plus fees. For a buy order, you need quantity * price / 1_000_000 USDC. Also ensure you have at least 0.5 ALGO for transaction fees.
Error: Asset not opted in
Before trading, your wallet must opt in to the YES and NO token assets for the market. The SDK handles this automatically, but you need enough ALGO for the minimum balance requirement (0.1 ALGO per asset).
Order not filling immediately
Limit orders only execute at your exact price. If there’s no matching order on the opposite side of the book, your order will sit on the orderbook until someone matches it. Use createMarketOrder() for immediate execution.
Network connection issues
If you’re getting timeout errors, the public nodes may be experiencing high traffic. Try again or consider using a dedicated node provider for more reliable performance.