Skip to content

Add /v1/validators CRUD endpoint#2

Draft
Copilot wants to merge 2 commits intomainfrom
copilot/update-validator-rewards-calculation
Draft

Add /v1/validators CRUD endpoint#2
Copilot wants to merge 2 commits intomainfrom
copilot/update-validator-rewards-calculation

Conversation

Copy link
Copy Markdown

Copilot AI commented Mar 29, 2026

Implements the full validator management API backed by the validators Supabase table, with on-chain registration support via Superfluid GDA pool units managed in background jobs.

New: src/routes/validators.js

Route Auth Behavior
GET /v1/validators public List validators; ?active=true|false (default true), ?limit=N (default 50, max 200)
GET /v1/validators/:address public Lookup by EVM address, normalized to lowercase
POST /v1/validators JWT Register; validates EVM address via viem's isAddress; 409 on duplicate
PUT /v1/validators/:address JWT Update name / accuracy_score (0–100); stamps updated_at
DELETE /v1/validators/:address JWT Soft-deactivate (active = false) — no hard delete

All routes return { error, message } on failure with a [Validators ROUTE_NAME] log prefix, consistent with streams.js.

Updated: index.js

Replaced the stub entrypoint with the full production version from railway/code-change-TESn9A and mounted the new router:

import validatorsRouter from './src/routes/validators.js';
// ...
app.use('/v1/validators', validatorsRouter);

Also added validators: '/v1/validators' to the discovery endpoint response.

Updated: src/db/schema.sql

Added missing indexes on the validators table:

CREATE INDEX IF NOT EXISTS idx_validators_active  ON validators (active);
CREATE INDEX IF NOT EXISTS idx_validators_address ON validators (address);

Supporting files

Brought in all src/ files (lib/, middleware/, routes/, jobs/) from the railway/code-change-TESn9A branch so the full API surface is present on this branch.

Original prompt

Context

The normancomics/BibleFi-API repository has an open PR #1 (railway/code-change-TESn9Amain) that implements the full BWTYA API. The validators table already exists in src/db/schema.sql with this structure:

CREATE TABLE IF NOT EXISTS validators (
  id              UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
  address         TEXT NOT NULL UNIQUE,  -- EVM wallet address
  name            TEXT,
  accuracy_score  FLOAT DEFAULT 0,       -- 0–100
  pool_units      TEXT DEFAULT '0',      -- Superfluid GDA units (as string for bigint safety)
  active          BOOLEAN DEFAULT TRUE,
  last_reward_at  TIMESTAMPTZ,
  created_at      TIMESTAMPTZ DEFAULT NOW(),
  updated_at      TIMESTAMPTZ DEFAULT NOW()
);

The background job in src/jobs/index.js already calls calculateValidatorRewards() every 15 minutes to update pool_units and last_reward_at.

The Superfluid GDA pool is managed via src/lib/superfluid.js. The smart contract has a registerValidator(address) function that needs to be called on-chain when a new validator is registered.


Task: Add src/routes/validators.js

Create a full CRUD REST endpoint at /v1/validators with the following routes:

GET /v1/validators

  • List all active validators from the validators table in Supabase
  • Support query params: ?active=true|false (default: true), ?limit=N (default: 50, max: 200)
  • Return: { validators: [...], count: N }
  • No auth required (public read)
  • If Supabase is not configured, return an empty array with a message field

GET /v1/validators/:address

  • Get a single validator by EVM address (case-insensitive)
  • Normalize the address to lowercase for lookup
  • Return the validator record, or 404 if not found
  • No auth required

POST /v1/validators (protected — requires JWT via requireAuth)

  • Register a new validator
  • Body: { address: string (EVM address), name?: string }
  • Validate that address is a valid EVM address using isAddress from viem
  • Insert into validators table with active: true, accuracy_score: 0, pool_units: '0'
  • If a validator with that address already exists (unique constraint violation), return 409 Conflict with a helpful message
  • Return 201 with the created validator record

PUT /v1/validators/:address (protected — requires JWT via requireAuth)

  • Update a validator's name and/or accuracy_score
  • Body: { name?: string, accuracy_score?: number (0-100) }
  • Validate accuracy_score is between 0 and 100 if provided
  • Update updated_at to NOW()
  • Return the updated record, or 404 if not found

DELETE /v1/validators/:address (protected — requires JWT via requireAuth)

  • Soft-deactivate a validator (set active: false, updated_at: NOW())
  • Do NOT hard delete
  • Return { message: 'Validator deactivated', address }
  • Return 404 if not found

Error handling

  • All routes should catch errors and return { error: string, message: string } with appropriate HTTP status codes
  • Log errors with a [Validators ROUTE_NAME] prefix

Task: Update index.js

Mount the new validators router:

import validatorsRouter from './src/routes/validators.js';
// ...
app.use('/v1/validators', validatorsRouter);

Look at the existing index.js on the railway/code-change-TESn9A branch and add the validators route in the same style as the other routes.


Task: Update src/db/schema.sql

Add an index on validators.active if it doesn't already exist:

CREATE INDEX IF NOT EXISTS idx_validators_active ON validators (active);
CREATE INDEX IF NOT EXISTS idx_validators_address ON validators (address);

Append these at the end of the validators section.


Implementation Notes

  • Use ES module syntax (import/export default) consistent with the rest of the codebase
  • Import supabase from ../lib/supabase.js
  • Import requireAuth from ../lib/auth.js
  • Import isAddress from viem
  • Use the same error handling pattern as src/routes/streams.js
  • The address field should always be stored and queried in lowercase (.toLowerCase())
  • Branch name: copilot/validators-endpoint
  • Base the PR against main

The following is the prior conversation context from the user's chat exploration (may be truncated):

User: help me fix errors and continue to build

Assistant: Here's a full picture of the current state of Biblefi/Biblefi-biblefi-schemas:


📊 Current State

What exists on main

File Status
LICENSE ✅ Present
README.md ⚠️ Stub (7 lines — just the repo title + one-liner)
VERSIONING.md ⚠️ Empty (1 byte — literally blank)
schemas/agent_envelope.schema.json ⚠️ Empty (1 byte)
schemas/church_record.schema.json ⚠️ Empty (1 byte)
schemas/defi_strategy.schema.json ⚠️ Empty (1 byte)
schemas/scripture_record.schema.json ⚠️ Empty (1 byte)
schemas/security_finding.schema.json ⚠️ Empty (1 byte)

What'...

This pull request was created from Copilot chat.


📱 Kick off Copilot coding agent tasks wherever you are with GitHub Mobile, available on iOS and Android.

Copilot AI changed the title [WIP] Update rewards calculation for validators Add /v1/validators CRUD endpoint Mar 29, 2026
Copilot AI requested a review from normancomics March 29, 2026 05:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants