-
Notifications
You must be signed in to change notification settings - Fork 6
Description
We need to create a backend API endpoint that interfaces with our Cairo smart contract's create_campaign function. This API will handle campaign creation requests from the frontend and interact with the blockchain to deploy new fundraising campaigns.
Requirements .
API Endpoint
Method: POST
Path: /api/v1/campaigns
Authentication: Required (JWT token)
Request Body
json{
"campaign_ref": "string",
"target_amount": "string",
"donation_token": "string"
}
Response Format
json{
"success": true,
"data": {
"campaign_id": "string",
"campaign_ref": "string",
"target_amount": "string",
"donation_token": "string",
"transaction_hash": "string",
"created_at": "ISO 8601 timestamp"
}
}
Error Response
json{
"success": false,
"error": {
"code": "string",
"message": "string",
"details": {}
}
}
Validation Requirements
Input Validation
campaign_ref must be exactly 5 characters long
campaign_ref must be unique (check against existing campaigns)
campaign_ref cannot be empty or contain only whitespace
target_amount must be a positive number greater than 0
target_amount must be a valid u256 value
donation_token must be a valid contract address format
Request must include valid authentication token
Business Logic Validation
User must have sufficient wallet balance for transaction fees
Contract address must be valid and accessible
Donation token contract must exist and be verified
Implementation Details
Smart Contract Integration
Interface with the Cairo contract's create_campaign function
Handle contract deployment and transaction signing
Implement proper error handling for contract failures
Store transaction hash for tracking purposes
Database Operations
Store campaign metadata in database
Create audit log entry for campaign creation
Update user's campaign count/history
Error Handling
Map contract panics to appropriate HTTP status codes:
Empty campaign_ref → 400 Bad Request
Duplicate campaign_ref → 409 Conflict
Zero target_amount → 400 Bad Request
Contract interaction failures → 500 Internal Server Error
Authentication failures → 401 Unauthorized
Acceptance Criteria
API endpoint accepts valid campaign creation requests
Successfully interfaces with Cairo smart contract
Returns proper campaign ID from blockchain transaction
Implements all required validations
Handles all error scenarios gracefully
Includes comprehensive logging
Has unit tests with >90% coverage
Integration tests with mock contract
API documentation is updated
Rate limiting is implemented (max 5 campaigns per user per hour)
Technical Considerations
Use async/await for blockchain interactions
Implement transaction retry logic with exponential backoff
Add monitoring and alerting for failed transactions
Consider gas price optimization strategies
Implement proper connection pooling for blockchain RPC calls