import { privateKeyToAccount } from 'viem/accounts' import * as Organizations from '../../db/tables/organizations.js' import * as RoutesSubsidies from '../../db/tables/routesSubsidies.js' import * as StripeCustomers from '../../db/tables/stripeCustomers.js' import * as TestApp from '../../../test/App.js' import * as TestRoutes from '../../../test/Routes.js' import * as Viem from '../Viem.js' import * as Subsidy from './Subsidy.js' const privateKey = `0x${'11'.repeat(32)}` as const describe('check', () => { test('resolves the effective policy and production billing state', async () => { const db = TestApp.database() try { await Organizations.create(db, { id: 'org_subsidy_check', name: 'Subsidy check' }) const options = { environment: 'production', orgId: 'org_subsidy_check', } as const const missing = await Subsidy.check(db, options) await RoutesSubsidies.upsertOrganization(db, { frequency: 'tx', maxAmount: '2.5', orgId: options.orgId, }) const productionWithoutBilling = await Subsidy.check(db, options) const sandbox = await Subsidy.check(db, { ...options, environment: 'sandbox' }) await StripeCustomers.create(db, { orgId: options.orgId, stripeCustomerId: 'cus_subsidy_check', }) await StripeCustomers.setStatus(db, options.orgId, 'past_due') const pastDue = await Subsidy.check(db, options) await StripeCustomers.setStatus(db, options.orgId, 'active') const active = await Subsidy.check(db, options) expect({ active, missing, pastDue, productionWithoutBilling, sandbox }) .toMatchInlineSnapshot(` { "active": { "frequency": "tx", "maxAmount": "2.5", "type": "allowed", }, "missing": { "reason": "routes_subsidy_not_enabled", "type": "denied", }, "pastDue": { "reason": "billing_past_due", "type": "denied", }, "productionWithoutBilling": { "reason": "billing_required", "type": "denied", }, "sandbox": { "frequency": "tx", "maxAmount": "2.5", "type": "allowed", }, } `) } finally { await db.close() } }) }) describe('supports', () => { test('accepts only corresponding USD routes within the deposit limit', () => { const route = TestRoutes.transferSnapshot() expect({ aboveLimit: Subsidy.supports( { maxAmount: '1' }, { amount: '1000001', destinationToken: route.destinationToken, sourceToken: route.sourceToken, }, ), invalidLimit: Subsidy.supports( { maxAmount: 'invalid' }, { amount: '1000000', destinationToken: route.destinationToken, sourceToken: route.sourceToken, }, ), withinLimit: Subsidy.supports( { maxAmount: '1' }, { amount: '1000000', destinationToken: route.destinationToken, sourceToken: route.sourceToken, }, ), wrongCurrency: Subsidy.supports( { maxAmount: '1' }, { amount: '1000000', destinationToken: { ...route.destinationToken, currency: 'EUR' }, sourceToken: route.sourceToken, }, ), }).toMatchInlineSnapshot(` { "aboveLimit": false, "invalidLimit": false, "withinLimit": true, "wrongCurrency": false, } `) }) }) describe('supportsGap', () => { test('compares the destination-token deficit with the decimal USD limit', () => { const route = TestRoutes.transferSnapshot() expect({ aboveLimit: Subsidy.supportsGap( { maxAmount: '0.05' }, { amount: 50_001n, destinationToken: route.destinationToken }, ), atLimit: Subsidy.supportsGap( { maxAmount: '0.05' }, { amount: 50_000n, destinationToken: route.destinationToken }, ), invalidLimit: Subsidy.supportsGap( { maxAmount: 'invalid' }, { amount: 1n, destinationToken: route.destinationToken }, ), wrongCurrency: Subsidy.supportsGap( { maxAmount: '0.05' }, { amount: 50_000n, destinationToken: { ...route.destinationToken, currency: 'EUR' } }, ), }).toMatchInlineSnapshot(` { "aboveLimit": false, "atLimit": true, "invalidLimit": false, "wrongCurrency": false, } `) }) }) describe('create', () => { test('exposes the dedicated account and policy without network access', () => { const account = privateKeyToAccount(privateKey) const policy = { maxAmount: '10' } const settler = Subsidy.create({ account, getClient: Viem.createGetClient(), policy, }) expect({ account: settler.account, policy: settler.policy }).toMatchInlineSnapshot(` { "account": "0x19E7E376E7C213B7E7e7e46cc70A5dD086DAff2A", "policy": { "maxAmount": "10", }, } `) }) })