Overview
Token Unwrapping allows authorized users to convert SFLUV tokens back to the underlying HONEY token. The feature includes eligibility checks, monthly unwrap limits, and minimum follow-up amounts to prevent abuse. File Reference:backend/handlers/unwrap.go:26
Key Concepts
Unwrap Eligibility
Not all wallets can unwrap. Eligibility requirements:- Wallet Must Be Registered - In user’s wallet list
- Redeemer Flag - Wallet must have
is_redeemer: true - Monthly Limit - First unwrap per month unrestricted
- Follow-up Minimum - Additional unwraps same month require ≥$100
backend/handlers/unwrap.go:56-81
Minimum Follow-up Amount
After unwrapping once in a calendar month, subsequent unwraps require:backend/handlers/unwrap.go:15-18
Eligibility Check Flow
1. Check Unwrap Eligibility
Endpoint:POST /unwrap/check-eligibility
Request:
backend/handlers/unwrap.go:26-111
2. Validation Steps
The endpoint performs these checks:a. Authenticate User
backend/handlers/unwrap.go:27-31
b. Validate Request Body
backend/handlers/unwrap.go:45-54
c. Verify Wallet Ownership
backend/handlers/unwrap.go:56-65
d. Check Redeemer Status
backend/handlers/unwrap.go:66-81
e. Check Monthly Limit
backend/handlers/unwrap.go:20-24, 83-89
3. Response Format
Allowed:backend/handlers/unwrap.go:91-111
Recording Unwrap
After successful blockchain transaction:Record Unwrap Endpoint
Endpoint:POST /unwrap/record
Request:
backend/handlers/unwrap.go:113-175
Recording Flow
a. Authenticate & Validate
backend/handlers/unwrap.go:114-135
b. Verify Wallet & Redeemer Status
backend/handlers/unwrap.go:137-150
c. Update Last Unwrap Timestamp
last_unwrap_at.
Code Reference: backend/handlers/unwrap.go:156-161
d. Return Success
backend/handlers/unwrap.go:163-175
Frontend Integration
Unwrap Flow (Frontend)
Typical unwrap user flow:- Select Wallet - Choose redeemer-enabled wallet
- Enter Amount - Specify SFLUV amount to unwrap
- Check Eligibility - Call
POST /unwrap/check-eligibility - Display Status - Show allowed/denied with reason
- Execute Transaction - If allowed, unwrap on blockchain
- Record Unwrap - Call
POST /unwrap/recordafter success - Update UI - Show confirmation and new balance
Example Frontend Code
Database Schema
Wallet Table
is_redeemer- Enables unwrap permissionlast_unwrap_at- Timestamp of most recent unwrap (for monthly limit)
Request/Response Types
UnwrapEligibilityRequest
backend/structs/app.go (implied)
UnwrapEligibilityResponse
backend/handlers/unwrap.go:67-72, 91-96
UnwrapRecordRequest
backend/structs/app.go (implied)
UnwrapRecordResponse
backend/handlers/unwrap.go:163-166
Business Rules
Redeemer Wallets
Wallets become redeemer-enabled through:- Admin Grant - Admin sets
is_redeemer: true - Merchant Approval - Approved merchants get redeemer wallets
- Special Programs - Certain roles auto-granted redeemer status
Monthly Reset
The monthly limit resets:- UTC Month Boundary - First of month at 00:00:00 UTC
- Per Wallet - Each wallet has independent limit
- First Unwrap - No minimum for first unwrap of month
- Subsequent Unwraps - Require ≥$100 within same month
backend/handlers/unwrap.go:20-24, 86-89
Amount Validation
- Positive Only - Amount must be >0
- Wei Format - Sent as string (bigint compatibility)
- No Maximum - First unwrap can be any amount
- Follow-up Minimum - Subsequent unwraps ≥100 SFLUV
backend/handlers/unwrap.go:50-54, 86-89
Error Handling
Error Responses
401 Unauthorized - No valid auth token 400 Bad Request - Missing/invalid wallet or amount 403 Forbidden - Wallet not owned, not redeemer, or monthly limit 500 Internal Server Error - Database or system error Code Reference:backend/handlers/unwrap.go:29, 36-37, 46-47, 52-53, 59-64, 78-80, 103-106
Error Messages
User-facing error reasons:"Wallet is not unwrap-enabled"- Not a redeemer wallet"You already unwrapped this month. Additional unwraps this month must be at least $100."- Monthly limit
backend/handlers/unwrap.go:69, 88
Security Considerations
Authentication
- JWT Required - All endpoints require valid Privy JWT
- User Ownership - Wallet must belong to authenticated user
- No Impersonation - Cannot unwrap others’ wallets
Authorization
- Redeemer Check - Only redeemer wallets can unwrap
- Admin Control - Admins control redeemer status
- Rate Limiting - Monthly minimum prevents abuse
Transaction Safety
- Check Before Execute - Always check eligibility first
- Record After Success - Only record after blockchain confirms
- Idempotent Recording - Safe to call record multiple times
Best Practices
For Users
- Check Eligibility First - Don’t attempt unwrap without checking
- Understand Limits - Know monthly restriction exists
- Plan Unwraps - Combine small amounts to meet $100 minimum
- Verify Redeemer Status - Ensure wallet is unwrap-enabled
For Developers
- Always Check Eligibility - Before showing unwrap UI
- Display Reasons - Show user why unwrap denied
- Show Last Unwrap - Display
last_unwrap_atfor transparency - Handle Errors Gracefully - Clear error messages to user
- Record Atomically - Only record after blockchain success
For Admins
- Control Redeemer Access - Only grant to trusted/verified users
- Monitor Unwrap Volume - Track total unwraps for liquidity planning
- Review Monthly - Check for abuse patterns
- Adjust Minimum - Update
minimumFollowupUnwrapAmountWeiif needed
Troubleshooting
Common Issues
“Wallet is not unwrap-enabled”- User’s wallet missing
is_redeemer: true - Solution: Admin grants redeemer status
- User already unwrapped this calendar month
- Amount is less than minimum follow-up
- Solution: Wait until next month or unwrap ≥$100
- Wallet doesn’t belong to authenticated user
- Check wallet address matches user’s wallet list
- Database error or wallet not found
- Check wallet ID exists and is valid
- Review server logs for specific error
Debug Queries
Future Enhancements
Potential Features
- Variable Minimums - Different follow-up amounts by user tier
- Weekly Limits - Add weekly unwrap caps
- Unwrap History - Track all unwraps with amounts
- Batch Unwrap - Unwrap from multiple wallets at once
- Scheduled Unwrap - Queue future unwraps
- Dynamic Minimum - Adjust $100 minimum based on token price
- Unwrap Fees - Optional fee structure for unwrapping
- Instant vs Delayed - Different limits for instant unwrap
Integration Opportunities
- W9 Integration - Check W9 compliance before large unwraps
- KYC Verification - Higher limits for verified users
- Loyalty Rewards - Better terms for long-term holders
- Tax Reporting - Generate tax forms for unwrap activity
- Liquidity Management - Adjust limits based on HONEY reserves