Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions migrations/add_tiered_pricing.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
-- Migration: Add tiered pricing support (Issue #1)
-- Idempotent: safe to run multiple times

-- Migration: Add tiered pricing support (Issue #1)
ALTER TABLE IF EXISTS x402_calls
ADD COLUMN IF NOT EXISTS tier TEXT DEFAULT 'standard' CHECK (tier IN ('free', 'standard', 'premium', 'priority')),
ADD COLUMN IF NOT EXISTS price_per_call NUMERIC(10, 6) DEFAULT 0.01,
Expand Down
19 changes: 2 additions & 17 deletions src/pricing/tier-engine.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,11 @@
/**
* Tiered Pricing Engine — Issue #1
* Implements 4-tier pricing for x402 API calls
*/

export type Tier = 'free' | 'standard' | 'premium' | 'priority';
export type Tier = 'free' | 'standard' | 'premium' | 'priority';

export interface TierResult {
tier: Tier;
pricePerCall: number; // in USDC
callsInTier: number;
}

/**
* Returns the price per call based on total call count and priority flag.
* - Tier 1 (Free): calls 1–50 → $0.00
* - Tier 2 (Standard): calls 51–500 → $0.01
* - Tier 3 (Premium): calls 500+ → $0.03
* - Tier 4 (Priority): priority=true → $0.10
*/
export function getTierPrice(callCount: number, priorityFlag = false): TierResult {
if (priorityFlag) {
return { tier: 'priority', pricePerCall: 0.10, callsInTier: 1 };
Expand All @@ -31,13 +19,10 @@ export function getTierPrice(callCount: number, priorityFlag = false): TierResul
return { tier: 'premium', pricePerCall: 0.03, callsInTier: Infinity };
}

/**
* Calculates total cost for a batch of calls.
*/
export function calculateBatchCost(startCount: number, numCalls: number, priority = false): number {
let total = 0;
for (let i = 0; i < numCalls; i++) {
total += getTierPrice(startCount + i, priority).pricePerCall;
}
return Math.round(total * 1e6) / 1e6; // round to 6 decimals (USDC precision)
return Math.round(total * 1e6) / 1e6;
}
29 changes: 29 additions & 0 deletions tests/pricing.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { assertEquals } from 'https://deno.land/std@0.224.0/assert/mod.ts';
import { getTierPrice, calculateBatchCost } from '../src/pricing/tier-engine.ts';

Deno.test('Tier 1: free for first 50 calls', () => {
assertEquals(getTierPrice(1).tier, 'free');
assertEquals(getTierPrice(50).tier, 'free');
assertEquals(getTierPrice(1).pricePerCall, 0.00);
});

Deno.test('Tier 2: standard for calls 51-500', () => {
assertEquals(getTierPrice(51).tier, 'standard');
assertEquals(getTierPrice(500).tier, 'standard');
assertEquals(getTierPrice(51).pricePerCall, 0.01);
});

Deno.test('Tier 3: premium for calls 500+', () => {
assertEquals(getTierPrice(501).tier, 'premium');
assertEquals(getTierPrice(501).pricePerCall, 0.03);
});

Deno.test('Tier 4: priority flag overrides all', () => {
assertEquals(getTierPrice(1, true).tier, 'priority');
assertEquals(getTierPrice(1000, true).pricePerCall, 0.10);
});

Deno.test('Batch cost calculation', () => {
assertEquals(calculateBatchCost(1, 10), 0);
assertEquals(calculateBatchCost(51, 1), 0.01);
});