React Hooks
useSubscribe

useSubscribe

The useSubscribe hook abstracts the complexity of building Programmable Transaction Blocks (PTBs) for subscriptions. It handles:

  1. Fetching the user's PUSD coin objects if a deposit is required.
  2. Structuring the correct Move calls (create_account, deposit, create_subscription, share_account).
  3. Routing the transaction through your backend sponsor if configured.

Usage

import { useSubscribe } from "@paystreamer/sdk/react";
 
function CustomSubscribeButton() {
  const { subscribe, isLoading, error, recommendedDeposit, hasAccount } = useSubscribe({
    platformId: "0xYOUR_PLATFORM",
    tierIndex: 1,
    tierAmount: 50000000000n,
    tierFrequencyMs: 2592000000n, // 30 days
  });
 
  const handleSubscribe = async () => {
    // Optionally pass an initial deposit amount (in MIST)
    const txDigest = await subscribe(recommendedDeposit);
    
    if (txDigest) {
      console.log("Success!", txDigest);
    }
  };
 
  if (error) return <p>Error: {error}</p>;
 
  return (
    <button onClick={handleSubscribe} disabled={isLoading}>
      {isLoading ? "Processing..." : "Subscribe Now"}
    </button>
  );
}

Parameters

You must pass an object with the following properties:

PropertyTypeDescription
platformIdstringThe target platform object ID.
tierIndexnumber | bigintThe tier index on the platform to subscribe to.
tierAmountbigintThe cost of the tier per cycle (in MIST).
tierFrequencyMsbigintThe duration of the billing cycle (in MS).
accountId?stringOptional. The ID of the user's existing PayStreamer Account.
accountCapId?stringOptional. The ID of the user's existing Account Capability.

Returns

An object with the following properties:

PropertyTypeDescription
subscribe(depositAmount?: bigint) => Promise<string | null>The function to initiate the transaction. Returns the digest string on success.
isLoadingbooleanTrue while the transaction is being built, signed, or executed.
errorstring | nullError message if the transaction fails.
recommendedDepositbigintHelper value suggesting a 3-cycle buffer based on tierAmount and tierFrequencyMs.
hasAccountbooleanHelper boolean based on whether both accountId and accountCapId were provided.