A Go-based backend server for Trixy Protocol, a prediction market platform built on Flow blockchain. This server provides REST API endpoints for managing markets, bets, protocols, and integrates with Flow blockchain for on-chain market creation.
- Language: Go 1.25+
- Web Framework: Gin
- Database: PostgreSQL (with pgx driver)
- Blockchain: Flow (testnet/mainnet)
- Dependencies: See
go.mod
backend/
├── main.go # Server entry point
├── seed.go # Database seeding
├── internal/ # Internal packages
│ ├── database/ # Database connection
│ ├── models/ # Data models
│ ├── handlers/ # HTTP handlers
│ ├── services/ # Business logic
│ └── utils/ # Utilities
├── migrations/ # SQL migrations
├── .env # Environment variables
├── Makefile # Build commands
└── go.mod # Go dependencies
- Go 1.25 or higher
- PostgreSQL database
- Flow testnet account (optional, for blockchain features)
-
Clone and navigate to the backend directory
cd backend -
Install dependencies
make install # or go mod download -
Set up environment variables
cp .env.example .env # Edit .env with your configuration -
Run migrations
psql $DATABASE_URL -f migrations/*.sql
-
Seed the database
make seed # or go run . seed
-
Start the server
make run # or go run .
See .env.example for all available configuration options:
| Variable | Description | Default |
|---|---|---|
DATABASE_URL |
PostgreSQL connection string | Required |
PORT |
Server port | 8080 |
FLOW_NETWORK |
Flow blockchain network (testnet or mainnet) |
testnet |
TRIXY_PROTOCOL_ADDR |
TrixyProtocol contract address | Required |
FLOW_SIGNER_ADDRESS |
Flow account address for transactions | Optional |
FLOW_SIGNER_PRIVATE_KEY |
Flow account private key | Optional |
RUN_SEEDS |
Run seeds on startup | false |
SEED_FLOW_MARKETS |
Create test markets on Flow | false |
make run # Run the server
make seed # Seed the database
make build # Build binary
make test # Run tests
make clean # Clean build artifacts
make install # Install dependenciesgo run . # Run server
go run . seed # Seed database
go build # Build binary
go test ./... # Run testsGET /api/- API infoGET /api/health- Health check
GET /api/markets- List all marketsGET /api/markets/:id- Get market by IDGET /api/markets/:id/bets- Get bets for a market
GET /api/bets- List all betsPOST /api/bets- Place a betGET /api/bets/:id- Get bet by IDGET /api/bets/user/:address- Get bets by userGET /api/bets/market/:market_id- Get bets by marketGET /api/bets/stats/summary- Get betting statistics
GET /api/users/:address- Get user infoGET /api/users/:address/bets- Get user's betsGET /api/users/:address/stats- Get user statistics
GET /api/protocols- List all protocolsGET /api/protocols/:id- Get protocol by IDGET /api/protocols/address/:address- Get protocol by address
GET /api/flow/markets- Get Flow blockchain marketsGET /api/flow/markets/:market_id- Get specific Flow marketGET /api/flow/bets- Get Flow blockchain betsGET /api/flow/users/:address/stats- Get Flow user statsGET /api/flow/users/:address/markets/:market_id/bets- Get user's market bets
GET /api/blockchain/info- Get blockchain informationGET /api/blockchain/status- Get blockchain statusGET /api/blockchain/latest-block- Get latest block
GET /api/charts/market/:id- Get market chart dataGET /api/charts/platform- Get platform chart data
GET /api/oracle/price/latest- Get latest priceGET /api/oracle/price/history- Get price historyGET /api/oracle/price/stats- Get price statisticsGET /api/oracle/price/chart- Get price chart data
GET /api/stats/platform- Get platform statisticsGET /api/stats/leaderboard- Get leaderboard
Migrations are located in migrations/ directory:
001_initial_schema.sql- Base schema002_flow_prediction_markets.sql- Flow market tables003_prediction_markets.sql- Prediction market tables004_add_image_url.sql- Add image URL fields005_fix_flow_event_schemas.sql- Fix Flow event schemas006_prediction_markets_tables.sql- Additional market tables007_price_oracle.sql- Oracle price tables008_protocol_apy_snapshots.sql- Protocol APY tracking
The seed command populates the database with initial data:
make seedSeeded data includes:
- Protocols: Ankr, Increment, Figment (with updated icons)
- Markets (optional): Test prediction markets on Flow blockchain
Handlers (internal/handlers/)
- HTTP request handlers
- Input validation
- Response formatting
Services (internal/services/)
- Business logic
- External API calls
- Blockchain interactions
Models (internal/models/)
- Data structures
- Type definitions
Database (internal/database/)
- Connection management
- Pool configuration
Utils (internal/utils/)
- Image utilities
- Helper functions
- Create handler in
internal/handlers/ - Create service in
internal/services/ - Add route in
main.go - Update this README
- Follow standard Go conventions
- Use
gofmtfor formatting - Run
go vetbefore committing - Write tests for new features
make test
# or
go test ./...make buildgo build -ldflags="-s -w" -o trixy-backend- Set production environment variables
- Run migrations
- Build the binary
- Start the server
export DATABASE_URL="postgresql://..."
export PORT=8080
export FLOW_NETWORK=testnet
./trixy-backend- Never commit
.envfiles - Use environment variables for secrets
- Keep
FLOW_SIGNER_PRIVATE_KEYsecure - Enable HTTPS in production
- Validate all user inputs
See LICENSE file for details.
- Fork the repository
- Create your feature branch
- Commit your changes
- Push to the branch
- Create a Pull Request
For issues and questions, please open an issue on GitHub.