diff --git a/backend/src/services/__tests__/feePlanner.test.ts b/backend/src/services/__tests__/feePlanner.test.ts new file mode 100644 index 00000000..6f961500 --- /dev/null +++ b/backend/src/services/__tests__/feePlanner.test.ts @@ -0,0 +1,103 @@ +import { describe, expect, it } from 'vitest'; +import { FeePlanner, FeePlannerError } from '../feePlanner.js'; + +const feed = (planner: FeePlanner, corridor: string, fees: number[]) => + fees.forEach((fee) => planner.observe(corridor, fee)); + +describe('FeePlanner — Issue #1326', () => { + it('uses the live quote alone when there is no history anywhere', () => { + const plan = new FeePlanner().plan('xlm->usdc', 'normal', 100); + expect(plan).toMatchObject({ fee: 100n, source: 'live-only', priorWeight: 0 }); + }); + + it('refuses to invent a fee with no history and no live quote', () => { + expect(() => new FeePlanner().plan('xlm->usdc', 'normal')).toThrow(FeePlannerError); + }); + + it('builds an EWMA prior per corridor', () => { + const planner = new FeePlanner({ alpha: 0.5 }); + planner.observe('a', 100); + const prior = planner.observe('a', 200); + expect(prior.samples).toBe(2); + expect(prior.mean).toBe(150); + expect(prior.variance).toBeCloseTo(0.5 * (0 + 100 * 50)); + }); + + it('keeps corridors independent', () => { + const planner = new FeePlanner(); + feed(planner, 'a', [100, 100, 100]); + feed(planner, 'b', [900, 900, 900]); + expect(planner.getPrior('a')!.mean).toBe(100); + expect(planner.getPrior('b')!.mean).toBe(900); + }); + + it('trusts the prior more as samples accumulate', () => { + const few = new FeePlanner(); + feed(few, 'a', [100]); + const many = new FeePlanner(); + feed(many, 'a', Array(20).fill(100)); + + const fewPlan = few.plan('a', 'low', 200); + const manyPlan = many.plan('a', 'low', 200); + expect(fewPlan.priorWeight).toBeCloseTo(0.05); + expect(manyPlan.priorWeight).toBe(1); + expect(manyPlan.fee).toBe(100n); + expect(fewPlan.fee).toBeGreaterThan(manyPlan.fee); + }); + + it('adds more headroom for higher urgency on a noisy corridor', () => { + const planner = new FeePlanner(); + feed(planner, 'a', [50, 150, 50, 150, 50, 150, 50, 150, 50, 150, 50, 150, 50, 150, 50, 150, 50, 150, 50, 150]); + const low = planner.plan('a', 'low', 100).fee; + const normal = planner.plan('a', 'normal', 100).fee; + const high = planner.plan('a', 'high', 100).fee; + expect(low < normal && normal < high).toBe(true); + }); + + it('falls back to a discounted global prior for a corridor with no history', () => { + const planner = new FeePlanner(); + feed(planner, 'a', Array(20).fill(100)); + const plan = planner.plan('new-corridor', 'low', 300); + expect(plan.source).toBe('global-prior'); + expect(plan.priorWeight).toBe(0.5); + expect(plan.fee).toBe(200n); + }); + + it('plans from the prior alone when the live quote is unavailable', () => { + const planner = new FeePlanner(); + feed(planner, 'a', Array(5).fill(120)); + expect(planner.plan('a', 'low')).toMatchObject({ fee: 120n, source: 'prior-only' }); + }); + + it('never exceeds the live quote by more than the configured multiple', () => { + const planner = new FeePlanner({ maxLiveMultiple: 2 }); + feed(planner, 'a', Array(20).fill(10_000)); + expect(planner.plan('a', 'low', 100).fee).toBe(200n); + }); + + it('rounds the plan up to a whole unit', () => { + const planner = new FeePlanner(); + feed(planner, 'a', Array(20).fill(100.2)); + expect(planner.plan('a', 'low', 100.2).fee).toBe(101n); + }); + + it('rejects invalid fees, corridors, urgencies and config', () => { + const planner = new FeePlanner(); + expect(() => planner.observe('', 1)).toThrow(FeePlannerError); + expect(() => planner.observe('a', -1)).toThrow(FeePlannerError); + expect(() => planner.observe('a', Number.NaN)).toThrow(FeePlannerError); + expect(() => planner.plan('a', 'normal', Infinity)).toThrow(FeePlannerError); + expect(() => planner.plan('a', 'urgent' as never, 1)).toThrow(FeePlannerError); + expect(() => new FeePlanner({ alpha: 0 })).toThrow(FeePlannerError); + expect(() => new FeePlanner({ fullTrustSamples: 0 })).toThrow(FeePlannerError); + }); + + it('is deterministic for the same history and quote', () => { + const run = () => { + const planner = new FeePlanner(); + feed(planner, 'a', [80, 120, 95, 110, 100]); + return planner.plan('a', 'high', 105); + }; + expect(run()).toEqual(run()); + }); +}); diff --git a/backend/src/services/feePlanner.ts b/backend/src/services/feePlanner.ts new file mode 100644 index 00000000..2fc5e960 --- /dev/null +++ b/backend/src/services/feePlanner.ts @@ -0,0 +1,174 @@ +/** + * Adaptive network-fee planner with per-corridor historical priors (#1326). + * + * Each corridor (for example "stellar:USDC->stellar:XLM") keeps a running prior + * of the network fee it has actually cost: an exponentially weighted mean and + * variance. A fee plan blends that prior with the live fee quote, trusting the + * prior more as the corridor accumulates samples. A corridor with no history + * falls back to a global prior built from every corridor, then to the live + * quote alone, so a new corridor never gets an invented number. + * + * Fees are integers in the smallest unit (stroops, wei, ...). The planner is + * pure in-memory state with no clock and no I/O, so results are deterministic. + */ + +export type FeeUrgency = 'low' | 'normal' | 'high'; + +export interface FeePlannerConfig { + /** EWMA smoothing factor in (0, 1]. Higher reacts faster. */ + alpha: number; + /** Samples after which a corridor prior is trusted as much as the live quote. */ + fullTrustSamples: number; + /** Standard-deviation multiplier added to the estimate, per urgency. */ + urgencyZ: Record; + /** Hard cap on how far above the live quote a plan may go (1.5 = +50%). */ + maxLiveMultiple: number; +} + +export const DEFAULT_FEE_PLANNER_CONFIG: FeePlannerConfig = { + alpha: 0.2, + fullTrustSamples: 20, + urgencyZ: { low: 0, normal: 1, high: 2 }, + maxLiveMultiple: 3, +}; + +export interface CorridorPrior { + corridor: string; + samples: number; + mean: number; + variance: number; +} + +export type FeePlanSource = 'blended' | 'global-prior' | 'live-only' | 'prior-only'; + +export interface FeePlan { + corridor: string; + urgency: FeeUrgency; + /** Recommended fee, rounded up to a whole unit. */ + fee: bigint; + source: FeePlanSource; + /** Weight given to the historical prior, 0 to 1. */ + priorWeight: number; + samples: number; +} + +export class FeePlannerError extends Error { + constructor(message: string) { + super(message); + this.name = 'FeePlannerError'; + } +} + +function assertFee(value: number, label: string): void { + if (!Number.isFinite(value) || value < 0) { + throw new FeePlannerError(`${label} must be a finite, non-negative number`); + } +} + +export class FeePlanner { + private readonly priors = new Map(); + private readonly config: FeePlannerConfig; + + constructor(config: Partial = {}) { + this.config = { + ...DEFAULT_FEE_PLANNER_CONFIG, + ...config, + urgencyZ: { ...DEFAULT_FEE_PLANNER_CONFIG.urgencyZ, ...config.urgencyZ }, + }; + if (!(this.config.alpha > 0 && this.config.alpha <= 1)) { + throw new FeePlannerError('alpha must be in (0, 1]'); + } + if (this.config.fullTrustSamples < 1) { + throw new FeePlannerError('fullTrustSamples must be at least 1'); + } + if (this.config.maxLiveMultiple < 1) { + throw new FeePlannerError('maxLiveMultiple must be at least 1'); + } + } + + /** Record the fee a transfer on this corridor actually paid. */ + observe(corridor: string, paidFee: number): CorridorPrior { + if (!corridor) throw new FeePlannerError('corridor is required'); + assertFee(paidFee, 'paidFee'); + + const prior = this.priors.get(corridor); + if (!prior) { + const first: CorridorPrior = { corridor, samples: 1, mean: paidFee, variance: 0 }; + this.priors.set(corridor, first); + return { ...first }; + } + + // Exponentially weighted mean and variance (West, 1979 form). + const diff = paidFee - prior.mean; + const incr = this.config.alpha * diff; + prior.mean += incr; + prior.variance = (1 - this.config.alpha) * (prior.variance + diff * incr); + prior.samples += 1; + return { ...prior }; + } + + getPrior(corridor: string): CorridorPrior | undefined { + const prior = this.priors.get(corridor); + return prior ? { ...prior } : undefined; + } + + /** Sample-weighted prior across every corridor, or undefined when empty. */ + globalPrior(): CorridorPrior | undefined { + let samples = 0; + let mean = 0; + let variance = 0; + for (const prior of this.priors.values()) { + samples += prior.samples; + mean += prior.mean * prior.samples; + variance += prior.variance * prior.samples; + } + if (samples === 0) return undefined; + return { corridor: '*', samples, mean: mean / samples, variance: variance / samples }; + } + + /** + * Plan a fee for a corridor. + * @param liveFee Current network quote, or undefined when it is unavailable. + */ + plan(corridor: string, urgency: FeeUrgency, liveFee?: number): FeePlan { + if (!corridor) throw new FeePlannerError('corridor is required'); + if (!(urgency in this.config.urgencyZ)) throw new FeePlannerError(`unknown urgency: ${urgency}`); + if (liveFee !== undefined) assertFee(liveFee, 'liveFee'); + + const own = this.priors.get(corridor); + const prior = own ?? this.globalPrior(); + const z = this.config.urgencyZ[urgency]; + + if (!prior) { + if (liveFee === undefined) { + throw new FeePlannerError('no historical prior and no live fee quote available'); + } + return this.finish(corridor, urgency, liveFee, 'live-only', 0, 0); + } + + const stddev = Math.sqrt(prior.variance); + const priorEstimate = prior.mean + z * stddev; + // A borrowed global prior is never trusted as much as the corridor's own. + const maxWeight = own ? 1 : 0.5; + const priorWeight = Math.min(prior.samples / this.config.fullTrustSamples, 1) * maxWeight; + + if (liveFee === undefined) { + return this.finish(corridor, urgency, priorEstimate, 'prior-only', 1, prior.samples); + } + + const blended = priorWeight * priorEstimate + (1 - priorWeight) * (liveFee + z * stddev); + const capped = Math.min(blended, liveFee * this.config.maxLiveMultiple); + return this.finish(corridor, urgency, capped, own ? 'blended' : 'global-prior', priorWeight, prior.samples); + } + + private finish( + corridor: string, + urgency: FeeUrgency, + fee: number, + source: FeePlanSource, + priorWeight: number, + samples: number, + ): FeePlan { + return { corridor, urgency, fee: BigInt(Math.ceil(fee)), source, priorWeight, samples }; + } +}