Lepton Agents Hackathon (Canteen × Circle × Arc): agent-native nanopayments on Arc, built on the existing SettleKit platform. Hackathon modules (demo-ready, RFB-aligned): - @settlekit/agent: autonomous paying agent (Claude tool-use loop) — RFB 1/3 - @settlekit/citation-toll: per-access content + recursive royalty splits — RFB 6 - @settlekit/streaming: per-second continuous-authorization settlement — RFB 4 - @settlekit/x402-client: x402 pay-and-retry + pluggable settler (local/Circle) - @settlekit/agent-economy: closed-loop traction harness + CLI - apps/api /v1/lepton: public live demo surface Production settlement spine (Phase 1): - @settlekit/settlement-core: configurable Gateway/Circle/local providers, idempotency, batch accumulator, on-chain reconciliation, nonce + receipt stores - @settlekit/wallet-fleet: registry, spending caps + kill-switch, balances, Circle provisioning - x402-client: provider-settler adapter + Arc-indexer on-chain verifier - lepton DB schema (12 tables) + persistence Pg stores (settlements, nonces, sources, royalty legs, wallets, streams, payees) - apps/worker jobs: lepton-settlement-reconcile, lepton-payout-sweep, lepton-stream-refund (no-op until the settlement spine is wired) First real-world traction sidecar (Phase 2): - @settlekit/payee-registry: identity→wallet payout rule (in-memory + Pg) - services/sidecars/rsshub-citation-toll: ingest RSS items as priced citeable sources, x402-gated, settle per-citation royalties via settlement-core - shared sweepPendingRoyalties used by the worker job and the sidecar - services/sidecars/* wired as a TS workspace area (pnpm + vitest) Docs: LEPTON.md, LEPTON_PRODUCTION_PLAN.md. All green: full typecheck + 870 tests. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
133 lines
3.6 KiB
TypeScript
133 lines
3.6 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import type { StreamSettlement } from "../src/types.js";
|
|
import { openStream } from "../src/stream.js";
|
|
|
|
function clock(start = 0) {
|
|
const ref = { t: start };
|
|
return { ref, now: () => ref.t };
|
|
}
|
|
|
|
describe("PaymentStream (per-second settlement)", () => {
|
|
it("accrues value by elapsed time at the authorized rate", () => {
|
|
const { ref, now } = clock();
|
|
const s = openStream({
|
|
payer: "0xviewer",
|
|
payee: "0xstreamer",
|
|
network: "arc",
|
|
ratePerSecondUsdc: "0.001",
|
|
reserveUsdc: "0.01",
|
|
now,
|
|
});
|
|
|
|
ref.t = 1000;
|
|
expect(s.accrued().amount).toBe("0.001");
|
|
ref.t = 2500;
|
|
expect(s.accrued().amount).toBe("0.0025");
|
|
expect(s.due().amount).toBe("0.0025");
|
|
});
|
|
|
|
it("batch-settles the due amount and tracks the cumulative total", async () => {
|
|
const { ref, now } = clock();
|
|
const legs: StreamSettlement[] = [];
|
|
const s = openStream({
|
|
payer: "0xviewer",
|
|
payee: "0xstreamer",
|
|
network: "arc",
|
|
ratePerSecondUsdc: "0.001",
|
|
reserveUsdc: "0.01",
|
|
now,
|
|
});
|
|
|
|
ref.t = 3000;
|
|
const first = await s.settle((l) => {
|
|
legs.push(l);
|
|
});
|
|
expect(first.amount.amount).toBe("0.003");
|
|
expect(s.due().amount).toBe("0");
|
|
|
|
ref.t = 5000;
|
|
const second = await s.settle((l) => {
|
|
legs.push(l);
|
|
});
|
|
expect(second.amount.amount).toBe("0.002");
|
|
expect(second.settledTotal.amount).toBe("0.005");
|
|
expect(legs).toHaveLength(2);
|
|
});
|
|
|
|
it("does not bill paused intervals", () => {
|
|
const { ref, now } = clock();
|
|
const s = openStream({
|
|
payer: "0xv",
|
|
payee: "0xs",
|
|
network: "arc",
|
|
ratePerSecondUsdc: "0.001",
|
|
reserveUsdc: "0.01",
|
|
now,
|
|
});
|
|
|
|
ref.t = 2000;
|
|
expect(s.accrued().amount).toBe("0.002");
|
|
s.pause();
|
|
ref.t = 9000; // 7s paused — must not bill
|
|
expect(s.accrued().amount).toBe("0.002");
|
|
s.resume();
|
|
ref.t = 10000; // 1s more
|
|
expect(s.accrued().amount).toBe("0.003");
|
|
});
|
|
|
|
it("pauses the meter on a proof-of-flow drop and resumes on recovery", () => {
|
|
const { ref, now } = clock();
|
|
const s = openStream({
|
|
payer: "0xv",
|
|
payee: "0xs",
|
|
network: "arc",
|
|
ratePerSecondUsdc: "0.001",
|
|
reserveUsdc: "0.01",
|
|
now,
|
|
});
|
|
|
|
ref.t = 1000;
|
|
s.reportFlow(false); // delivery dropped at 1s
|
|
ref.t = 6000; // 5s outage — not billed
|
|
expect(s.snapshot().state).toBe("paused");
|
|
expect(s.snapshot().pauseReason).toBe("flow");
|
|
s.reportFlow(true);
|
|
ref.t = 7000; // 1s after recovery
|
|
expect(s.accrued().amount).toBe("0.002");
|
|
});
|
|
|
|
it("caps accrual at the reserve", () => {
|
|
const { ref, now } = clock();
|
|
const s = openStream({
|
|
payer: "0xv",
|
|
payee: "0xs",
|
|
network: "arc",
|
|
ratePerSecondUsdc: "0.001",
|
|
reserveUsdc: "0.01",
|
|
now,
|
|
});
|
|
ref.t = 1_000_000; // far beyond reserve
|
|
expect(s.accrued().amount).toBe("0.01");
|
|
expect(s.refundable().amount).toBe("0");
|
|
});
|
|
|
|
it("refunds the reserved-but-unused remainder on close", async () => {
|
|
const { ref, now } = clock();
|
|
const s = openStream({
|
|
payer: "0xv",
|
|
payee: "0xs",
|
|
network: "arc",
|
|
ratePerSecondUsdc: "0.001",
|
|
reserveUsdc: "0.01",
|
|
now,
|
|
});
|
|
ref.t = 3500; // accrue 0.0035 of a 0.01 reserve
|
|
const { finalSettlement, refund } = await s.close();
|
|
expect(finalSettlement.amount.amount).toBe("0.0035");
|
|
expect(refund.amount).toBe("0.0065");
|
|
// After close the meter is frozen.
|
|
ref.t = 50_000;
|
|
expect(s.accrued().amount).toBe("0.0035");
|
|
});
|
|
});
|