6.0 KiB
6.0 KiB
๐๏ธ Farcaster Postgres Replicator
โ Use Existing Farcaster Data
Query the Postgres Replicator to access all Farcaster network data without creating new accounts.
๐ฏ What is the Replicator?
A Postgres database that mirrors all Farcaster Hub data:
- โ All registered FIDs
- โ All usernames (fnames & ENS)
- โ All casts (posts)
- โ All signers (account keys)
- โ All onchain events
No registration needed - just query existing data!
๐ Database Tables
fids
All registered Farcaster IDs
SELECT fid, registered_at, custody_address
FROM fids
WHERE fid = 1
fnames
All usernames (fnames & ENS)
SELECT fid, username, type
FROM fnames
WHERE username = 'dwr'
AND deleted_at IS NULL
casts
All posts/messages
SELECT text, timestamp, mentions
FROM casts
WHERE fid = 1
AND deleted_at IS NULL
ORDER BY timestamp DESC
signers
All account keys
SELECT key, key_type, added_at
FROM signers
WHERE fid = 1
AND removed_at IS NULL
๐ Implementation
Query Functions
import { findUserByUsername, getUserProfile } from '@/lib/farcaster/replicator'
// Find user by username
const user = await findUserByUsername('@dwr')
// Returns: { fid: 3, username: 'dwr', type: 0 }
// Get full profile
const profile = await getUserProfile(3)
// Returns: { fid, username, registeredAt, custodyAddress }
// Search users
const results = await searchUsers('alice')
// Returns: [{ fid, username, type }, ...]
API Endpoints
# Find by username
GET /api/farcaster/username?username=dwr
# Search users
GET /api/farcaster/username?search=alice
๐ก Use Cases in PollCelo
1. Username Lookup
// User enters @username to challenge
const user = await findUserByUsername('@alice')
await createChallenge(myFid, user.fid)
2. Profile Display
// Show user profile
const profile = await getUserProfile(fid)
<div>
<h2>@{profile.username}</h2>
<p>Joined: {profile.registeredAt}</p>
</div>
3. User Search
// Search for users to follow
const results = await searchUsers('bob')
results.map(user => (
<UserCard fid={user.fid} username={user.username} />
))
4. Verify User Exists
// Before creating vote
const exists = await userExists(fid)
if (!exists) {
return { error: 'Invalid FID' }
}
๐ง Setup
1. Use Same Database
The replicator uses your existing Postgres database:
DATABASE_URL=postgresql://...
2. Query with Drizzle
import { getDb } from '@/lib/db'
import { sql } from 'drizzle-orm'
const db = getDb(process.env.DATABASE_URL!)
const result = await db.execute(sql`
SELECT * FROM fnames WHERE username = 'dwr'
`)
๐ Performance
Indexes
The replicator has built-in indexes:
fnames.username- Fast username lookupfnames.fid- Fast FID lookupcasts.fid- Fast user castssigners.fid- Fast signer lookup
Caching
// Cache username lookups
const cache = new Map<string, { fid: number; timestamp: number }>()
export async function findUserByUsername(username: string) {
const cached = cache.get(username)
if (cached && Date.now() - cached.timestamp < 300000) {
return cached
}
const result = await queryDatabase(username)
cache.set(username, { ...result, timestamp: Date.now() })
return result
}
๐ฏ Benefits
vs Creating New Accounts
| Feature | New Accounts | Replicator |
|---|---|---|
| User Cost | $5 | $0 |
| Setup Time | 30s | 0s |
| Maintenance | High | Low |
| Data Access | Limited | Full |
vs Fname Registry API
| Feature | API | Replicator |
|---|---|---|
| Speed | Slow (HTTP) | Fast (SQL) |
| Queries | Limited | Unlimited |
| Rate Limits | Yes | No |
| Offline | No | Yes |
๐ Advanced Queries
Get User Activity
SELECT
c.text,
c.timestamp,
fn.username
FROM casts c
JOIN fnames fn ON c.fid = fn.fid
WHERE c.fid = 1
AND c.deleted_at IS NULL
ORDER BY c.timestamp DESC
LIMIT 10
Find Active Users
SELECT
fn.fid,
fn.username,
COUNT(c.hash) as cast_count
FROM fnames fn
JOIN casts c ON fn.fid = c.fid
WHERE c.timestamp > NOW() - INTERVAL '7 days'
AND c.deleted_at IS NULL
GROUP BY fn.fid, fn.username
ORDER BY cast_count DESC
LIMIT 100
Get User's Network
-- Users mentioned by FID 1
SELECT DISTINCT
fn.fid,
fn.username
FROM casts c
CROSS JOIN LATERAL unnest(c.mentions) AS mention_fid
JOIN fnames fn ON fn.fid = mention_fid
WHERE c.fid = 1
AND c.deleted_at IS NULL
๐ Table Schema Reference
fids
fid(bigint) - Primary keyregistered_at(timestamp) - Registration datecustody_address(bytea) - Owner addressrecovery_address(bytea) - Recovery address
fnames
fid(bigint) - User's FIDusername(text) - Username (fname or ENS)type(smallint) - 0=fname, 1=ENSdeleted_at(timestamp) - Soft delete
casts
fid(bigint) - Author FIDhash(bytea) - Cast hashtext(text) - Cast contenttimestamp(timestamp) - Post timementions(json) - Mentioned FIDsembeds(json) - Embedded URLsdeleted_at(timestamp) - Soft delete
signers
fid(bigint) - Owner FIDkey(bytea) - Public keykey_type(smallint) - Key typeadded_at(timestamp) - Added dateremoved_at(timestamp) - Removed daterequester_fid(bigint) - App FID
โ Production Checklist
- Replicator queries implemented
- Username lookup working
- User search working
- Profile queries working
- Caching implemented
- Indexes verified
- Performance tested
๐ Summary
Use Replicator Instead of:
- โ Creating new accounts ($5 per user)
- โ Fname Registry API (rate limited)
- โ Hub gRPC calls (complex)
Benefits:
- โ Free for users
- โ Fast SQL queries
- โ Full network data
- โ No rate limits
- โ Simple implementation
PollCelo uses existing Farcaster accounts via replicator! ๐