The main goal of this game is to allow players to predict whether the price of Bitcoin will go up or down within a specified time frame, earning or losing points based on the accuracy of their predictions.
Before getting started, ensure you have the following prerequisites:
- Node.js installed (v18.x)
- AWS CDK installed (
npm install -g aws-cdk
) - AWS CLI configured with appropriate credentials
- run
cat ~/.aws/credentials
(unix) to see if you have profile configured
- run
- Clone this repository to your local machine.
- Navigate to the project directory.
- Install dependencies by running
npm install
.
To deploy the AWS infrastructure, run the following command:
npm run backend:deploy
To remove the deployed infrastructure, run:
npm run backend:destroy
To run the frontend SPA
npm run frontend:serve
You run the full test suite by
npm run test
Or individually
npm run backend:test
npm run frontend:test
The AWS CDK stack defined in apps/backend/stacks/bitcoin-bet-stack.ts
sets up the following resources:
- DynamoDB tables:
bitcoin-players
: Stores player information including userId, email, and score.bitcoin-bets
: Stores bet information including userId, guess, prices, and results.
- Cognito userPool: Manages user authentication and registration.
- BetPlacementStream: Kinesis stream for processing bet placements.
- AppSync API (
betsAPI
):- GraphQL schema defined in
apps/backend/graphql/schema.graphql
. - Data sources connected to DynamoDB tables and Lambda functions.
- Resolvers to query and mutate data.
- GraphQL schema defined in
- Resolvers:
currentBet.js
: Retrieves the current bet for a user.placeBet.js
: Handles the placement of a new bet.player.js
: Retrieves player information.
- Lambdas:
PostSignupLambda
: Triggered after user confirmation to initialize player data.IngestBetLambda
: Processes incoming bets and puts them into the Kinesis stream.ProcessBetsLambda
: Processes bets from the Kinesis stream and initiates the resolution workflow.CalculateResultLambda
: Calculates the result of a bet.
- Step Functions state machine:
- Orchestrates the bet resolution process:
- Fetches initial Bitcoin price
- Waits for the specified duration
- Fetches final Bitcoin price
- Retrieves user bet and player information
- Calculates bet result
- Updates bet and player data in DynamoDB
- Orchestrates the bet resolution process:
- IAM roles and policies: Ensure proper permissions for all components.
- CloudWatch logs: Capture logs from Lambda functions and AppSync API.
- ReactJS: A JavaScript library for building user interfaces
- React Query: A powerful data synchronization library for React applications
- TailwindCSS + ShadcnUI: Utility-first CSS framework with pre-built components
- Vite: A fast build tool and development server
The frontend architecture is structured as follows:
-
Main Application Entry:
- The main entry point is defined in
src/main.tsx
, which sets up the React application with necessary providers.
- The main entry point is defined in
-
Routing:
- React Router is used for client-side routing, with routes defined in the
App
component.
- React Router is used for client-side routing, with routes defined in the
-
Authentication:
- AWS Amplify is used for authentication, configured in
src/main.tsx
. - The
Auth
component (src/app/components/Auth.tsx
) handles user authentication flow.
- AWS Amplify is used for authentication, configured in
-
State Management:
- React Query is used for server state management and data fetching.
- Custom hooks in
src/api/queries.ts
define queries and mutations for interacting with the backend.
-
Components:
- The main dashboard is composed of several components:
ScoreCard
: Displays the user's current scorePricePredictorCard
: Main interface for making price predictionsBetDetailsCard
: Shows details of the user's current or last bet
- Reusable UI components are located in
src/app/components/shadcn-ui/
- The main dashboard is composed of several components:
-
API Communication:
- The
src/api/api-client.ts
file contains the logic for making authenticated requests to the backend.
- The
-
Custom Hooks:
useLiveBtcPrice
: Manages WebSocket connection for real-time Bitcoin price updatesuseCountdown
: Handles countdown logic for betting periods
-
Styling:
- TailwindCSS is used for styling, with custom configurations in
tailwind.config.js
- ShadcnUI components are integrated for consistent UI elements
- TailwindCSS is used for styling, with custom configurations in
-
Environment Configuration:
- Environment variables are managed through Vite's env system, with types defined in
src/vite-env.d.ts
- Environment variables are managed through Vite's env system, with types defined in
-
Testing:
- Unit tests are written using Vitest and React Testing Library
- Test files are co-located with their respective components
This architecture promotes modularity, reusability, and maintainability while leveraging modern React patterns and libraries for efficient development and performance.