2025-03-25 03:52:30 -04:00

109 lines
2.9 KiB
Markdown

# 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 contract
- `CHAIN_ID`: (Optional) The chain ID of the network (defaults to 80002 for Amoy testnet)
## API Endpoints
### Submit Hash
```http
POST /blockchain/submit-hash
Content-Type: application/json
{
"hash": "0x1234..."
}
```
### Propose Insight
```http
POST /blockchain/propose-insight
Content-Type: application/json
{
"insight": "This is a new insight"
}
```
### Vote on Insight
```http
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
```bash
go test -v ./...
```
### Adding New Features
1. Add new methods to the `BlockchainService` struct
2. Implement the corresponding HTTP handlers
3. Add test cases in `service_test.go`
4. 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
1. Implement actual contract bindings
2. Add transaction receipt monitoring
3. Implement event subscription
4. Add support for batch transactions
5. Implement gas price optimization
6. Add support for EIP-1559 transactions
7. Implement transaction queue management