@pwngh/economy-lab
    Preparing search index...

    Function openInstanceEconomies

    • The process-level front door to the fast lane: hands out the live lane for any scope key, opening on demand, and owns the rules a host must never get wrong — every lane shares one reservations registry, epochs rotate instead of ever re-settling a session id (ids come from epochMinter, src/netting.ts), and a failed settle keeps its lane for retry instead of stranding the epoch.

      Routing across processes is deliberately the host's; scopeRouter (src/router.ts) provides the consistent-hash assignment, kept sticky for the scope's life. On failover, recoverSession with the crashed epoch's session id finishes its settle, and the orphan sweep (src/worker/orphans.ts) enumerates crashed epochs from the journal.

      The whole host program, over an openPorts composition:

      Parameters

      Returns InstanceEconomies

      const ports = await openPorts(process.env, init);
      const lanes = openInstanceEconomies(ports, {
      epochMaxAgeMs: 30_000,
      lane: { perUserCapMinor: 500_000n },
      });
      const stopSettling = lanes.start(5_000); // rotate due epochs on cadence

      // Per request from a game server (an unprivileged caller):
      await lanes.laneFor(worldInstanceId).purchase({
      buyerId,
      price,
      recipients: [{ sellerId: creatorId, shareBps: 10_000 }],
      product: { sku, kind: 'permanent' },
      });

      // Shutdown or drain:
      stopSettling();
      await lanes.settleAll();