VoxPop Blockchain Service
The Blockchain Service handles interactions with the Polygon blockchain for the VoxPop platform. It provides functionality for submitting perspective hashes, proposing insights, and voting on insights.
Features
- Connect to Polygon PoS network (Amoy testnet)
- Submit perspective hashes to the blockchain
- Propose new insights
- Vote on existing insights
- Transaction retry mechanism with gas estimation
- Mock contract interactions for development
Environment Variables
The service requires the following environment variables:
POLYGON_RPC
: The RPC URL for the Polygon network (e.g., "https://rpc-amoy.polygon.technology")PRIVATE_KEY
: The private key for signing transactions (without "0x" prefix)CONTRACT_ADDRESS
: The address of the deployed VoxPop smart contractCHAIN_ID
: (Optional) The chain ID of the network (defaults to 80002 for Amoy testnet)
API Endpoints
Submit Hash
POST /blockchain/submit-hash
Content-Type: application/json
{
"hash": "0x1234..."
}
Propose Insight
POST /blockchain/propose-insight
Content-Type: application/json
{
"insight": "This is a new insight"
}
Vote on Insight
POST /blockchain/vote
Content-Type: application/json
{
"insightId": "0x1234...",
"vote": true
}
Implementation Details
Transaction Handling
- Transactions are signed using the provided private key
- Gas is estimated before sending transactions
- Failed transactions are retried up to 3 times with a 2-second delay between attempts
- Gas price is automatically suggested by the network
Contract Integration
- Currently using mock contract interactions
- Contract bindings will be generated from the Solidity ABI
- Supports both read and write operations
Error Handling
- Validates input parameters
- Handles network connection issues
- Provides detailed error messages
- Implements graceful degradation
Development
Prerequisites
- Go 1.20 or later
- Access to Polygon network (Amoy testnet recommended for development)
- Private key with sufficient funds for gas fees
Running Tests
go test -v ./...
Adding New Features
- Add new methods to the
BlockchainService
struct - Implement the corresponding HTTP handlers
- Add test cases in
service_test.go
- Update the README with new endpoint documentation
Security Considerations
- Private keys should never be committed to version control
- Use environment variables or secure secret management
- Implement rate limiting for API endpoints
- Validate all input parameters
- Use HTTPS for all API communications
Future Improvements
- Implement actual contract bindings
- Add transaction receipt monitoring
- Implement event subscription
- Add support for batch transactions
- Implement gas price optimization
- Add support for EIP-1559 transactions
- Implement transaction queue management