Skip to content

Latest commit

 

History

History
672 lines (479 loc) · 23.5 KB

CONTRIBUTING.md

File metadata and controls

672 lines (479 loc) · 23.5 KB

Contributing to Stellar-Spend

Scratch Files

New here? Start with the Developer Onboarding Guide — it takes you from clone to a running local environment and explains the architecture in detail.


Table of Contents

  1. Development Environment Setup
  2. Branch Naming Conventions
  3. Commit Message Format
  4. Pull Request Process
  5. Definition of Done
  6. Review Process & SLAs
  7. Code Style Guidelines
  8. Project Structure
  9. Type Coverage
  10. Dependency License Compliance
  11. Security & Audits
  12. Getting Help
  13. Code of Conduct

Development Environment Setup

📘 Comprehensive Local Setup Guide: For a consolidated guide covering Rust, Soroban CLI, database setup, and troubleshooting RPC errors, see docs/local-dev-setup.md.

Prerequisites

  • Node.js ≥ 20 and npm
  • Git
  • A Stellar wallet (Freighter or Lobstr) on Mainnet for manual testing

Getting started

# 1. Fork and clone
git clone https://github.com/your-username/stellar-spend.git
cd stellar-spend

# 2. Install dependencies
npm install

# 3. Configure environment
cp .env.example .env.local
# Fill in .env.local — see docs/environment-variables.md

# 4. Start the dev server
npm run dev
# Open http://localhost:3001

Run checks before committing

Creating Scratch Files

npm run lint             # ESLint
npm run format:check     # Prettier
npx tsc --noEmit         # TypeScript (zero errors required)
npm test                 # Unit + integration tests
npm run type:coverage    # Type coverage ≥ 95% (see § Type Coverage below)
npm run license:check    # Dependency-license compliance (see § Licenses below)

Contract development (Soroban smart contracts)

If you modify files in the contracts/ directory, you must:

  1. Fix Clippy warnings – ensure no linting issues:
cd contracts
cargo clippy --workspace -- -D warnings
  1. Audit Rust dependencies for security vulnerabilities:
# Option 1: Use the provided audit script
./scripts/audit-contracts.sh

# Option 2: Run cargo-audit directly on a specific contract
cd contracts/fee-manager
cargo audit --deny warnings

Prerequisites for contract development:

  • Rust toolchain (install from https://rustup.rs/)
  • cargo-clippy (included with rustup)
  • cargo-audit for vulnerability scanning (install with cargo install cargo-audit)

Set up local pre-commit hooks (optional but recommended):

The project supports two pre-commit hook systems. Either one will lint and format the files you stage before each commit, so violations are caught locally instead of in review.

Option A — pre-commit framework (Python):

pip install pre-commit
pre-commit install

Option B — Husky (native Git hooks):

npm install

npm install runs the prepare script, which installs Husky and registers the hooks automatically. You do not need to run npx husky install or add the hook by hand.

The Husky pre-commit hook runs:

  1. gitleaks — blocks the commit if secrets are detected in the working tree.
  2. lint-staged — runs ESLint (--fix) and Prettier (--write) on staged files only, then re-stages the fixes.

Staged-file rules are declared under "lint-staged" in package.json:

  • *.{js,jsx,ts,tsx,mjs,cjs} — eslint --fix, then prettier --write
  • *.{json,md,css,scss,yml,yaml,html} — prettier --write

Because lint-staged only touches staged files, commits stay fast even in large working trees. Unstaged changes are never modified.

What the hook does not run:

Type-checking (npm run type:check) and the full snapshot validation are not part of the pre-commit hook, since they operate on the whole project rather than on staged files; CI enforces them on every push.

Bypassing pre-commit hooks:

To skip hooks for a specific commit (e.g., emergency fix, WIP commit), use the --no-verify flag:

git commit --no-verify -m "WIP: temporary change"

Warning: --no-verify bypasses all pre-commit checks. CI will still enforce lint, type-check, and format on every push. Use this flag sparingly and never for code that will be merged without review.

Common workflows before pushing contract changes:

  • Format code: cargo fmt --workspace (auto-fix) — style is enforced by CI
  • Check formatting: cargo fmt --workspace --check (read-only, matches CI check)
  • Run clippy: cargo clippy --workspace -- -D warnings
  • Audit for vulnerabilities: ./scripts/audit-contracts.sh
  • Check for RUSTSEC advisories: cargo audit --json (for CI/CD integration)
  • Update vulnerable dependencies: cargo update

Rust code style (rustfmt)

All Soroban contract code is formatted with rustfmt using the rules in contracts/rustfmt.toml. Key settings:

Rule Value
Edition 2021
Max line width 100
Indent 4 spaces (no hard tabs)
Import grouping StdExternalCrate
Trailing commas Vertical (multi-line)
Newline style Unix

CI runs cargo fmt --workspace --check on every push that touches contracts/. A formatting diff will fail the build. Run cargo fmt --workspace locally before pushing to keep the check green.

Component development

We use Storybook for isolated component development:

npm run storybook

When creating or updating a UI component, add a .stories.tsx file covering:

  • Different variants and states (loading, disabled, error)
  • Edge cases for input data
  • Accessibility checks via the integrated axe addon

Orphaned story check

After deleting or renaming a component, run the orphan checker to confirm no story files are left pointing to a missing component:

npm run storybook:check-orphans

If orphaned stories are found, remove them automatically with:

npm run storybook:fix-orphans

Run npm run storybook:check-orphans before opening a PR whenever you remove or rename a component. The check exits 0 when no orphans are found and exits 1 otherwise, making it suitable for CI.


Branch Naming Conventions

Use descriptive branch names with these prefixes:

Prefix Purpose
feat/ New features
fix/ Bug fixes
docs/ Documentation updates
style/ Formatting / whitespace only
refactor/ Refactoring without behavior change
test/ Adding or updating tests
chore/ Maintenance, dependency updates
ci/ CI/CD changes
contract/ Soroban smart contract changes

Examples:

  • feat/add-kes-corridor
  • fix/paycrest-webhook-hmac
  • docs/update-adr-escrow
  • test/bridge-adapter-unit-tests

Include the issue number when relevant: feat/issue-42-add-kes-corridor.


Commit Message Format

We use Conventional Commits:

<type>[optional scope]: <description>

[optional body]

[optional footer(s)]

Types

Type When to use
feat New feature
fix Bug fix
docs Documentation only
style Formatting, whitespace
refactor Code restructuring, no behavior change
test Adding or updating tests
chore Maintenance, dependency bumps
ci CI/CD configuration
perf Performance improvement
contract Soroban contract change

Scope (optional)

Use a scope to identify the affected subsystem:

feat(bridge): …, fix(paycrest): …, test(wallet): …, docs(adr): …

Examples

feat(corridor): add KES fiat corridor via Paycrest
fix(webhook): validate HMAC signature before processing payload
docs(adr): add ADR-007 for feature flag approach
test(quote): add boundary tests for fee calculation
chore(deps): bump @stellar/stellar-sdk from 14.5.0 to 14.6.0

Breaking changes

Add BREAKING CHANGE: in the footer:

feat(api)!: rename /api/offramp/rate to /api/offramp/fx-rate

BREAKING CHANGE: The old endpoint path is removed. Update all clients.

Pull Request Process

Before opening a PR

  1. Make sure your branch is up to date with main:

    git fetch origin
    git rebase origin/main
  2. Run the full check suite:

    npm run lint && npm run format:check && npx tsc --noEmit && npm test
  3. If you changed UI flows, run E2E tests:

    npm run test:e2e

Opening a PR

  • Use the PR template (.github/PULL_REQUEST_TEMPLATE.md) — fill every section
  • Title must follow the commit message format: feat(scope): short description
  • Reference the issue: Closes #<n> in the summary
  • Add screenshots or recordings for any UI changes
  • Keep PRs focused — one concern per PR; split large changes

Keeping a PR up to date

Rebase (do not merge) to stay current with main. Force-push to your branch is acceptable during review.

Merging

PRs are merged by a maintainer using squash and merge once all checks pass and approval is received. Do not merge your own PRs.


Definition of Done

A contribution is done when all of the following are true:

Code

  • Implements the acceptance criteria from the linked issue
  • No regressions introduced (all existing tests still pass)
  • No TypeScript errors: npx tsc --noEmit exits 0
  • No lint errors: npm run lint exits 0
  • No Prettier violations: npm run format:check exits 0
  • No any types without a comment explaining why
  • No secrets, PII, or credentials in the diff

Tests

  • New logic has unit tests covering the happy path and primary error paths
  • Changed API routes have integration tests
  • UI changes have component tests (render states + key interactions)
  • Mutation score for touched modules is not degraded (target ≥ 70%)
  • All CI test jobs pass (lint, type-check, unit, build, E2E)

Accessibility (UI changes)

  • New interactive elements have accessible names
  • Focus management is correct (modals trap focus, focus restores on close)
  • Keyboard navigation works without a mouse
  • Color contrast ≥ 4.5:1 for text (WCAG AA)

Documentation

  • docs/ updated if architecture, API behavior, or configuration changed
  • If the architecture changed (new service, new external dependency, data-flow change): update docs/diagrams/ source files and run the diagram check (bash scripts/check-diagrams.sh)
  • New environment variables are added to .env.example with descriptions
  • New ADRs created for significant architectural decisions (see docs/adr/, e.g. ADR-013)
  • In-code comments are accurate and not stale

Security

  • No NEXT_PUBLIC_ prefix on server-side secrets
  • All user input is validated and sanitized
  • New dependencies are from well-known, actively maintained packages
  • New API endpoints enforce appropriate rate limits and authentication

Review Process & SLAs

Who reviews?

  • Maintainers (@Lex-Studios/maintainers) review all PRs
  • Domain reviewers: tag the relevant area owner for complex changes (see CODEOWNERS when present)

Review SLAs

PR size First review Follow-up response
Small (< 100 lines changed) 2 business days 1 business day
Medium (100–500 lines) 3 business days 2 business days
Large (> 500 lines) 5 business days 2 business days
Security fix / hotfix Same day (tag urgent) 4 hours

SLAs are for the first substantive review. Trivial approvals (docs typo) may be faster.

What reviewers look for

  1. Correctness — Does it solve the stated problem? Are edge cases handled?
  2. Tests — Are the tests meaningful? Do they cover failure paths?
  3. Security — Are there any injection, auth bypass, or secret-exposure risks?
  4. Performance — Any N+1 queries, unnecessary re-renders, or bundle bloat?
  5. Accessibility — Are ARIA labels and keyboard flows correct?
  6. Definition of done — All checklist items satisfied?

Response to review comments

  • Address all reviewer comments or explain why you disagree
  • Mark conversations as resolved only after the fix is pushed
  • Request a re-review once all comments are addressed
  • Maintainers resolve conversations they opened

Approval policy

  • 1 approval from a maintainer is required to merge
  • Security-sensitive changes (auth, encryption, smart contracts) require 2 approvals

Code Style Guidelines

TypeScript

  • Use TypeScript for all new code
  • Define explicit types and interfaces; avoid any
  • Prefer unknown over any when the type is genuinely unknown
  • Use branded types (UserId, TransactionId) for domain IDs (see src/lib/types/branded-types.ts)

React / Next.js

  • Functional components with hooks only
  • Follow the state management architecture defined in ADR-013 (State Management Architecture): use React Context strictly for low-frequency global UI state (src/contexts/), custom hooks for domain workflows (src/hooks/), and local state for component-level UI interactions
  • Use 'use client' directive only where client-side state/effects are needed
  • Implement error boundaries around complex component trees
  • Always provide loading and error states for async operations

Environment variables

  • Use src/lib/env.ts for all environment access
  • Never import server-side variables in 'use client' components
  • Add new variables to .env.example with a description comment

Constants and magic numbers

  • Never inline a magic number or repeated string literal. Define it as a named constant instead.
  • Application-wide constants (timeouts, polling intervals, cache TTLs, Stellar parameters) live in src/lib/constants.ts, which re-exports everything from src/lib/config/. Import from there:
    import { BRIDGE_POLLING_INTERVAL_MS, CACHE_CURRENCY_TTL_MS } from '@/lib/constants';
  • Fee percentages and basis-point values live in @stellar-spend/shared (packages/shared/src/fee-constants.ts).
  • Per-route rate-limit configs live in src/lib/rateLimiter.ts (RATE_LIMIT_REGISTRY).
  • Currency, stablecoin, and corridor configs live in their respective files (currencies.ts, stablecoins.ts, corridor-config.ts).
  • If you add a new global constant, add it to src/lib/constants.ts and add a corresponding test in src/lib/constants.test.ts.

Error handling

All error handling must follow these rules (enforced by ESLint — violations fail CI):

  • No floating promises. Every async call must be awaited or explicitly discarded with void:

    // ❌ Floating — rejection is silently lost
    fetchData();
    
    // ✅ Awaited
    await fetchData();
    
    // ✅ Intentionally fire-and-forget
    void fetchData();

    Rule: @typescript-eslint/no-floating-promises: error

  • No empty catch blocks. At minimum, log the error or rethrow it. If you genuinely want to ignore an error, prefix the parameter with _ and add a comment:

    // ❌ Swallowed — no trace of what went wrong
    try { ... } catch (e) {}
    
    // ✅ Logged
    try { ... } catch (e) {
      logger.warn('context', { error: e });
    }
    
    // ✅ Intentionally ignored (use sparingly)
    try { ... } catch (_e) {
      // Safe to ignore: optional cache warm-up — the main path continues without it
    }

    Rule: no-empty: ['error', { allowEmptyCatch: false }]

  • Use the shared ErrorHandler for all API route errors. See docs/api/ERROR_RESPONSES.md for the full reference.

Formatting

  • Prettier is enforced; run npm run format to auto-fix
  • Single quotes for strings
  • 100-character line limit
  • Trailing commas in multi-line structures

Naming

  • Components: PascalCase
  • Functions and variables: camelCase
  • Constants: SCREAMING_SNAKE_CASE
  • Files: kebab-case.ts / PascalCase.tsx for components

Project Structure

src/
├── app/                 # Next.js App Router pages and API routes
│   ├── api/            # REST API route handlers
│   └── history/        # /history page
├── components/         # Reusable React components
│   ├── design-system/  # Base design tokens (Button, Card, Badge, Alert)
│   ├── skeletons/      # Loading skeleton variants
│   └── ui/             # Primitive form components
├── hooks/              # Custom React hooks
├── lib/                # Core business logic and utilities
│   ├── i18n/           # Internationalisation
│   ├── offramp/        # Offramp-specific logic and adapters
│   ├── security/       # Encryption, sanitization, headers
│   ├── stellar/        # Stellar / Soroban wallet adapters
│   └── wallets/        # Multi-wallet manager
├── test/               # Unit and integration test files
│   ├── integration/    # Route-level integration tests
│   └── mocks/          # MSW handlers and shared test fixtures
└── types/              # Global TypeScript type definitions

e2e/                     # Playwright end-to-end tests
contracts/               # Soroban smart contracts (Rust)
  ├── escrow/
  ├── fee-manager/
  └── treasury/
docs/
  ├── adr/              # Architecture Decision Records
  └── *.md              # Reference documentation
.github/
  ├── ISSUE_TEMPLATE/   # Bug, feature, and contract issue templates
  └── PULL_REQUEST_TEMPLATE.md

Type Coverage

TypeScript type coverage is measured with type-coverage and must stay at or above 95%.

npm run type:coverage         # check (matches CI)
npm run type:coverage:detail  # show all uncovered identifiers

Configuration lives in .type-coverage at the project root.

What the threshold means

Score Meaning
≥ 95% ✅ Green — CI passes
< 95% ❌ Red — CI fails; you must improve coverage before merging

Justifying an any exception

If you genuinely cannot type something (e.g., a third-party response with no TS definitions), add a comment directly above the line:

// eslint-disable-next-line @typescript-eslint/no-explicit-any -- no types available for XYZ SDK response
const result = sdk.call() as any;

The comment must name why any is unavoidable. Unexplained any usages will be flagged in code review.


Version Pinning

Stellar-Spend uses a two-tier dependency versioning strategy to balance security with automation.

Hard-pinned (exact versions)

The following packages must use exact version strings (no ^ or ~) in package.json:

Package Reason
react Major/minor bumps require coordinated changes; silent upgrades cause hydration mismatches
react-dom Must always match react exactly
typescript Compiler version is a build contract — drift between developer and CI compiler versions causes false-positive type errors

If you need to update a hard-pinned dependency, open a dedicated PR with a changelog entry explaining the reason and test coverage for any API changes.

Range-versioned (Renovate-managed)

All other dependencies may use caret ranges (^). Renovate automatically opens pull requests for each version bump. The PR must pass CI before merging.

The Renovate configuration lives at .github/renovate.json. If you add a new dependency category that needs special handling (e.g. pinned major version), add a packageRules entry there.

Checking the current state

# List all range-versioned dependencies
node -e "
  const p = require('./package.json');
  const all = {...p.dependencies, ...p.devDependencies};
  Object.entries(all)
    .filter(([,v]) => v.startsWith('^') || v.startsWith('~'))
    .forEach(([k,v]) => console.log(k, v));
"

Dependency License Compliance

All npm dependencies must carry a license from the approved list below. Run the compliance check before opening a PR:

npm run license:check   # exits 0 if all clear, 1 on violation
npm run license:report  # same but also prints every approved package

Approved licenses

License Notes
MIT, MIT-0 Permissive
ISC Permissive
Apache-2.0 Permissive
BSD-2-Clause, BSD-3-Clause Permissive
CC0-1.0, 0BSD Effectively public domain
BlueOak-1.0.0 Permissive
MPL-2.0 Weak copyleft — changes to MPL files must be open-sourced
Unlicense, Public Domain Permissive

LGPL and GPL packages (currently accepted with rationale)

Several transitive dependencies (via @allbridge/bridge-core-sdk) carry LGPL-3.0 or GPL-3.0 licenses. These are documented with a written rationale in scripts/check-licenses.js under ACCEPTED_EXCEPTIONS. The key point: these packages are used as libraries with no source modification — the LGPL dynamic-linking exemption applies and the MIT application code is not infected.

Review these exceptions every time the affected packages are upgraded.

Adding a new dependency with an unapproved license

  1. Check whether a compatible alternative exists — prefer it.
  2. If unavoidable, add an entry to ACCEPTED_EXCEPTIONS in scripts/check-licenses.js with:
    • The exact package@version key
    • A one-sentence rationale (why it is safe, what the linking model is)
  3. Re-run npm run license:check to confirm the check still passes.
  4. Note the exception in your PR description.

Getting Help

  • Search existing issues first
  • Open a new issue using the appropriate template
  • Join community discussions
  • Review the docs/ directory for architecture and API references

Code of Conduct

This project follows a Code of Conduct. By participating you agree to its terms.

Thank you for contributing to Stellar-Spend! 🚀