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

    Variable subscribeConst

    subscribe: (
        fields: FieldsOf<K>,
    ) => {
        actor: Principal;
        idempotencyKey: string;
        kind: "subscribe";
        periodMs: number;
        price: Amount;
        sellerId: string;
        sku: string;
        userId: string;
    } = ...

    Builds a subscribe operation: charges the first period of a recurring plan, grants the buyer the SKU through the period just billed, and saves the subscription record, all in one transaction; the worker's renewal sweep bills every later period from spendable only. The first charge draws promo first, then spendable, with the platform fee on the spendable part only — the same split as a spend. Rejects with ALREADY_SUBSCRIBED (an active subscription already exists for the same buyer, SKU, and seller), INSUFFICIENT_FUNDS, FUNDS_IMMATURE, RISK_DENIED, or ECONOMY_PAUSED. A user actor may subscribe only their own wallet, and the buyer must differ from the seller. The price must sit inside the configured per-period band. A retry under the same idempotencyKey returns the first-period transaction as duplicate.

    Type Declaration

      • (
            fields: FieldsOf<K>,
        ): {
            actor: Principal;
            idempotencyKey: string;
            kind: "subscribe";
            periodMs: number;
            price: Amount;
            sellerId: string;
            sku: string;
            userId: string;
        }
      • Parameters

        • fields: FieldsOf<K>

        Returns {
            actor: Principal;
            idempotencyKey: string;
            kind: "subscribe";
            periodMs: number;
            price: Amount;
            sellerId: string;
            sku: string;
            userId: string;
        }

    const outcome = await economy.submit(subscribe({
    idempotencyKey: idempotencyKey('sub', 'usr_buyer', 'club_pass'),
    actor: userActor('usr_buyer'),
    userId: 'usr_buyer',
    sellerId: 'usr_seller',
    sku: 'club_pass',
    price: toAmount('CREDIT', 50_000n), // 500 credits per period
    periodMs: 2_592_000_000, // 30 days
    }));