clarityxo-sdk
v0.4.0
Published
TypeScript SDK and CLI for the ClarityXO on-chain Tic-Tac-Toe game on Stacks
Maintainers
Readme
ClarityXO SDK
A TypeScript SDK and CLI for interacting with the ClarityXO decentralized Tic-Tac-Toe game on the Stacks blockchain.
Installation
npm install clarityxo-sdkQuick Start
import { createClient } from 'clarityxo-sdk';
const client = createClient({
network: 'testnet',
contractAddress: 'ST1PQHQKVVA1MGKKKSQCXDTEYXSGHCV66VR89BS1',
});
const gameState = await client.getFullGameState();
console.log(gameState.board);API Reference
ClarityXOClient
The main client class for interacting with ClarityXO.
Constructor
new ClarityXOClient(config: ClarityXOConfig)Game State Methods
getBoardState(): Promise<Board>- Get the current board stategetGameStatus(): Promise<GameStatus>- Get the game status ('active', 'finished', 'not-started')getWinner(): Promise<'player' | 'ai' | 'draw' | null>- Get the winnergetCurrentTurn(): Promise<Turn>- Get whose turn it is ('player' or 'ai')isValidMove(row: 0|1|2, col: 0|1|2): Promise<boolean>- Check if a move is validgetFullGameState(): Promise<GameState>- Get all game state at once
Transaction Methods
These require senderKey and senderAddress in config.
startNewGame(): Promise<{ txId: string }>- Start a new gamemakeMove(row: 0|1|2, col: 0|1|2): Promise<{ txId: string }>- Make a moveresignGame(): Promise<{ txId: string }>- Resign the current game
Leaderboard Methods
getLeaderboard(month: string): Promise<LeaderboardMonth>- Get leaderboard for a monthsubmitResult(result: GameResult): Promise<void>- Submit a game resultsyncLeaderboard(): Promise<void>- Sync leaderboard datahealthCheck(): Promise<boolean>- Check if leaderboard API is healthygetPlayerStats(playerAddress: string, month?: string): Promise<PlayerStats | null>- Get statistics for a specific player
Types
Network: 'mainnet' | 'testnet'Board: 3x3 array of CellValueCellValue: 'X' | 'O' | nullGameStatus: 'active' | 'finished' | 'not-started'Turn: 'player' | 'ai'GameState: { board, status, winner, currentTurn }LeaderboardEntry: { player, wins, losses, draws, points, rank }LeaderboardMonth: { month, entries }GameResult: { player, outcome, month }PlayerStats: { player, wins, losses, draws, points, rank, totalGames, winRate }
CLI
The package includes a CLI tool called clarityxo.
Global Options
--contract <address>: Contract address (required for most commands)--network <mainnet|testnet>: Network (default: testnet)--api <url>: Leaderboard API URL
Commands
board
Display the current game board.
clarityxo board --contract ST1PQHQKVVA1MGKKKSQCXDTEYXSGHCV66VR89BS1Example output:
ClarityXO — Testnet
Contract: ST1PQHQKVVA1MGKKKSQCXDTEYXSGHCV66VR89BS1
X │ · │ O
───┼───┼───
· │ X │ ·
───┼───┼───
O │ · │ X
Turn: AI Status: active Winner: —status
Display game status and current turn.
clarityxo status --contract ST1PQHQKVVA1MGKKKSQCXDTEYXSGHCV66VR89BS1winner
Display the winner.
clarityxo winner --contract ST1PQHQKVVA1MGKKKSQCXDTEYXSGHCV66VR89BS1leaderboard
Display the leaderboard for a month.
clarityxo leaderboard --month 2023-10 --contract ST1PQHQKVVA1MGKKKSQCXDTEYXSGHCV66VR89BS1move <row> <col>
Make a move on the board. Requires sender key and address.
clarityxo move 1 2 --contract ST1PQHQKVVA1MGKKKSQCXDTEYXSGHCV66VR89BS1 --key <private-key> --address <address>start
Start a new game. Requires sender key and address.
clarityxo start --contract ST1PQHQKVVA1MGKKKSQCXDTEYXSGHCV66VR89BS1 --key <private-key> --address <address>resign
Resign the current game. Requires sender key and address.
clarityxo resign --contract ST1PQHQKVVA1MGKKKSQCXDTEYXSGHCV66VR89BS1 --key <private-key> --address <address>Network Configuration
- Mainnet: Use 'mainnet' network, mainnet contract addresses
- Testnet: Use 'testnet' network, testnet contract addresses
Default leaderboard API is https://clarityxo.onrender.com, but can be overridden.
Error Handling
All methods throw descriptive errors. Handle them appropriately:
try {
const tx = await client.startNewGame();
console.log('Transaction ID:', tx.txId);
} catch (error) {
console.error('Failed to start game:', error.message);
}Utility Functions
The SDK includes utility functions for board analysis:
checkWin(board, player)- Check if a player has wonisBoardFull(board)- Check if the board is fullisDraw(board)- Check if the game is a drawgetAvailableMoves(board)- Get list of available movesgetBoardString(board)- Convert board to string representationcloneBoard(board)- Create a deep copy of the boardmakeMoveOnBoard(board, row, col, player)- Make a move and return new board
Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Run tests:
npm test - Lint:
npm run lint - Format:
npm run format - Submit a PR
If you'd like to contribute a bugfix or small improvement, open an issue first with a short description of the change. For larger features, open a discussion or draft PR so we can coordinate before significant design work.
License
MIT
