|
| 1 | +import { formatUnits } from 'viem'; |
| 2 | +import { readContract } from 'viem/actions'; |
| 3 | +import { |
| 4 | + spendPermissionManagerAbi, |
| 5 | + spendPermissionManagerAddress, |
| 6 | +} from '../../sign/base-account/utils/constants.js'; |
| 7 | +import { createClients, FALLBACK_CHAINS, getClient } from '../../store/chain-clients/utils.js'; |
| 8 | +import { |
| 9 | + fetchPermission, |
| 10 | + getPermissionStatus, |
| 11 | +} from '../public-utilities/spend-permission/index.js'; |
| 12 | +import { |
| 13 | + calculateCurrentPeriod, |
| 14 | + timestampInSecondsToDate, |
| 15 | + toSpendPermissionArgs, |
| 16 | +} from '../public-utilities/spend-permission/utils.js'; |
| 17 | +import { CHAIN_IDS, TOKENS } from './constants.js'; |
| 18 | +import type { SubscriptionStatus, SubscriptionStatusOptions } from './types.js'; |
| 19 | + |
| 20 | +/** |
| 21 | + * Gets the current status and details of a subscription. |
| 22 | + * |
| 23 | + * This function fetches the subscription (spend permission) details using its ID (permission hash) |
| 24 | + * and returns status information about the subscription. If there's no on-chain state for the |
| 25 | + * subscription (e.g., it has never been used), the function will infer that the subscription |
| 26 | + * is unrevoked and the full recurring amount is available to spend. |
| 27 | + * |
| 28 | + * @param options - Options for checking subscription status |
| 29 | + * @param options.id - The subscription ID (permission hash) returned from subscribe() |
| 30 | + * @param options.testnet - Whether to check on testnet (Base Sepolia). Defaults to false (mainnet) |
| 31 | + * @returns Promise<SubscriptionStatus> - Subscription status information |
| 32 | + * @throws Error if the subscription cannot be found or if fetching fails |
| 33 | + * |
| 34 | + * @example |
| 35 | + * ```typescript |
| 36 | + * import { getSubscriptionStatus } from '@base-org/account/payment'; |
| 37 | + * |
| 38 | + * // Check status of a subscription using its ID |
| 39 | + * const status = await getSubscriptionStatus({ |
| 40 | + * id: '0x71319cd488f8e4f24687711ec5c95d9e0c1bacbf5c1064942937eba4c7cf2984', |
| 41 | + * testnet: false |
| 42 | + * }); |
| 43 | + * |
| 44 | + * console.log(`Subscribed: ${status.isSubscribed}`); |
| 45 | + * console.log(`Next payment: ${status.nextPeriodStart}`); |
| 46 | + * console.log(`Recurring amount: $${status.recurringAmount}`); |
| 47 | + * ``` |
| 48 | + */ |
| 49 | +export async function getSubscriptionStatus( |
| 50 | + options: SubscriptionStatusOptions |
| 51 | +): Promise<SubscriptionStatus> { |
| 52 | + const { id, testnet = false } = options; |
| 53 | + |
| 54 | + // First, try to fetch the permission details using the hash |
| 55 | + const permission = await fetchPermission({ |
| 56 | + permissionHash: id, |
| 57 | + }); |
| 58 | + |
| 59 | + // If no permission found in the indexer, return as not subscribed |
| 60 | + if (!permission) { |
| 61 | + // No permission found - the subscription doesn't exist or cannot be found |
| 62 | + return { |
| 63 | + isSubscribed: false, |
| 64 | + recurringCharge: '0', |
| 65 | + }; |
| 66 | + } |
| 67 | + |
| 68 | + // Validate this is a USDC permission on Base/Base Sepolia |
| 69 | + const expectedChainId = testnet ? CHAIN_IDS.baseSepolia : CHAIN_IDS.base; |
| 70 | + const expectedTokenAddress = testnet |
| 71 | + ? TOKENS.USDC.addresses.baseSepolia.toLowerCase() |
| 72 | + : TOKENS.USDC.addresses.base.toLowerCase(); |
| 73 | + |
| 74 | + if (permission.chainId !== expectedChainId) { |
| 75 | + // Determine if the subscription is on mainnet or testnet |
| 76 | + const isSubscriptionOnMainnet = permission.chainId === CHAIN_IDS.base; |
| 77 | + const isSubscriptionOnTestnet = permission.chainId === CHAIN_IDS.baseSepolia; |
| 78 | + |
| 79 | + let errorMessage: string; |
| 80 | + if (testnet && isSubscriptionOnMainnet) { |
| 81 | + errorMessage = |
| 82 | + 'The subscription was requested on testnet but is actually a mainnet subscription'; |
| 83 | + } else if (!testnet && isSubscriptionOnTestnet) { |
| 84 | + errorMessage = |
| 85 | + 'The subscription was requested on mainnet but is actually a testnet subscription'; |
| 86 | + } else { |
| 87 | + // Fallback for unexpected chain IDs |
| 88 | + errorMessage = `Subscription is on chain ${permission.chainId}, expected ${expectedChainId} (${testnet ? 'Base Sepolia' : 'Base'})`; |
| 89 | + } |
| 90 | + |
| 91 | + throw new Error(errorMessage); |
| 92 | + } |
| 93 | + |
| 94 | + if (permission.permission.token.toLowerCase() !== expectedTokenAddress) { |
| 95 | + throw new Error( |
| 96 | + `Subscription is not for USDC token. Got ${permission.permission.token}, expected ${expectedTokenAddress}` |
| 97 | + ); |
| 98 | + } |
| 99 | + |
| 100 | + // Ensure chain client is initialized for the permission's chain |
| 101 | + if (permission.chainId && !getClient(permission.chainId)) { |
| 102 | + const fallbackChain = FALLBACK_CHAINS.find((chain) => chain.id === permission.chainId); |
| 103 | + if (fallbackChain) { |
| 104 | + createClients([fallbackChain]); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + // Get the current permission status (includes period info and active state) |
| 109 | + const status = await getPermissionStatus(permission); |
| 110 | + |
| 111 | + // Get the current period info directly to get spend amount |
| 112 | + let currentPeriod: { start: number; end: number; spend: bigint }; |
| 113 | + |
| 114 | + const client = getClient(permission.chainId!); |
| 115 | + if (client) { |
| 116 | + try { |
| 117 | + const spendPermissionArgs = toSpendPermissionArgs(permission); |
| 118 | + currentPeriod = (await readContract(client, { |
| 119 | + address: spendPermissionManagerAddress, |
| 120 | + abi: spendPermissionManagerAbi, |
| 121 | + functionName: 'getCurrentPeriod', |
| 122 | + args: [spendPermissionArgs], |
| 123 | + })) as { start: number; end: number; spend: bigint }; |
| 124 | + } catch { |
| 125 | + // If we can't read on-chain state, calculate from permission parameters |
| 126 | + currentPeriod = calculateCurrentPeriod(permission); |
| 127 | + } |
| 128 | + } else { |
| 129 | + // No client available, calculate from permission parameters |
| 130 | + currentPeriod = calculateCurrentPeriod(permission); |
| 131 | + } |
| 132 | + |
| 133 | + // Format the allowance amount from wei to USD string (USDC has 6 decimals) |
| 134 | + const recurringCharge = formatUnits(BigInt(permission.permission.allowance), 6); |
| 135 | + |
| 136 | + // Calculate period in days from the period duration in seconds |
| 137 | + const periodInDays = Number(permission.permission.period) / 86400; |
| 138 | + |
| 139 | + // Check if the subscription period has started |
| 140 | + const currentTime = Math.floor(Date.now() / 1000); |
| 141 | + const permissionStart = Number(permission.permission.start); |
| 142 | + const permissionEnd = Number(permission.permission.end); |
| 143 | + |
| 144 | + if (currentTime < permissionStart) { |
| 145 | + throw new Error( |
| 146 | + `Subscription has not started yet. It will begin at ${new Date(permissionStart * 1000).toISOString()}` |
| 147 | + ); |
| 148 | + } |
| 149 | + |
| 150 | + // Check if the subscription has expired |
| 151 | + const hasNotExpired = currentTime <= permissionEnd; |
| 152 | + |
| 153 | + // A subscription is considered active if we're within the valid time bounds |
| 154 | + // and the permission hasn't been revoked. |
| 155 | + const hasNoOnChainState = currentPeriod.spend === BigInt(0); |
| 156 | + const isSubscribed = hasNotExpired && (status.isActive || hasNoOnChainState); |
| 157 | + |
| 158 | + // Build the result with data from getCurrentPeriod and other on-chain functions |
| 159 | + const result: SubscriptionStatus = { |
| 160 | + isSubscribed, |
| 161 | + recurringCharge, |
| 162 | + remainingChargeInPeriod: formatUnits(status.remainingSpend, 6), |
| 163 | + currentPeriodStart: timestampInSecondsToDate(currentPeriod.start), |
| 164 | + nextPeriodStart: status.nextPeriodStart, |
| 165 | + periodInDays, |
| 166 | + }; |
| 167 | + |
| 168 | + return result; |
| 169 | +} |
0 commit comments