|
| 1 | +import { readWallet, WALLET_FILE, API } from "./config.mjs"; |
| 2 | +import { existsSync } from "fs"; |
| 3 | + |
| 4 | +const HELP = `run402 tier — Manage your Run402 tier subscription |
| 5 | +
|
| 6 | +Usage: |
| 7 | + run402 tier <subcommand> [args...] |
| 8 | +
|
| 9 | +Subcommands: |
| 10 | + status Show current tier (tier name, status, expiry) |
| 11 | + set <tier> Subscribe, renew, or upgrade (pays via x402) |
| 12 | +
|
| 13 | +Tiers: prototype ($0.10/7d), hobby ($5/30d), team ($20/30d) |
| 14 | +
|
| 15 | +The server auto-detects the action based on your wallet state: |
| 16 | + - No tier or expired → subscribe |
| 17 | + - Same tier, active → renew (extends from expiry) |
| 18 | + - Higher tier → upgrade (prorated refund to allowance) |
| 19 | + - Lower tier, active → rejected (wait for expiry) |
| 20 | +
|
| 21 | +Examples: |
| 22 | + run402 tier status |
| 23 | + run402 tier set prototype |
| 24 | + run402 tier set hobby |
| 25 | +`; |
| 26 | + |
| 27 | +async function setupPaidFetch() { |
| 28 | + if (!existsSync(WALLET_FILE)) { |
| 29 | + console.error(JSON.stringify({ status: "error", message: "No wallet found. Run: run402 wallet create && run402 wallet fund" })); |
| 30 | + process.exit(1); |
| 31 | + } |
| 32 | + const wallet = readWallet(); |
| 33 | + const { privateKeyToAccount } = await import("viem/accounts"); |
| 34 | + const { createPublicClient, http } = await import("viem"); |
| 35 | + const { baseSepolia } = await import("viem/chains"); |
| 36 | + const { x402Client, wrapFetchWithPayment } = await import("@x402/fetch"); |
| 37 | + const { ExactEvmScheme } = await import("@x402/evm/exact/client"); |
| 38 | + const { toClientEvmSigner } = await import("@x402/evm"); |
| 39 | + const account = privateKeyToAccount(wallet.privateKey); |
| 40 | + const publicClient = createPublicClient({ chain: baseSepolia, transport: http() }); |
| 41 | + const signer = toClientEvmSigner(account, publicClient); |
| 42 | + const client = new x402Client(); |
| 43 | + client.register("eip155:84532", new ExactEvmScheme(signer)); |
| 44 | + return wrapFetchWithPayment(fetch, client); |
| 45 | +} |
| 46 | + |
| 47 | +async function status() { |
| 48 | + const w = readWallet(); |
| 49 | + if (!w) { console.log(JSON.stringify({ status: "error", message: "No wallet. Run: run402 wallet create" })); process.exit(1); } |
| 50 | + const { privateKeyToAccount } = await import("viem/accounts"); |
| 51 | + const account = privateKeyToAccount(w.privateKey); |
| 52 | + const timestamp = Math.floor(Date.now() / 1000).toString(); |
| 53 | + const signature = await account.signMessage({ message: `run402:${timestamp}` }); |
| 54 | + const res = await fetch(`${API}/tiers/v1/status`, { |
| 55 | + headers: { "X-Run402-Wallet": account.address, "X-Run402-Signature": signature, "X-Run402-Timestamp": timestamp }, |
| 56 | + }); |
| 57 | + const data = await res.json(); |
| 58 | + if (!res.ok) { console.error(JSON.stringify({ status: "error", http: res.status, ...data })); process.exit(1); } |
| 59 | + console.log(JSON.stringify(data, null, 2)); |
| 60 | +} |
| 61 | + |
| 62 | +async function set(tierName) { |
| 63 | + if (!tierName) { console.error(JSON.stringify({ status: "error", message: "Usage: run402 tier set <prototype|hobby|team>" })); process.exit(1); } |
| 64 | + const fetchPaid = await setupPaidFetch(); |
| 65 | + const res = await fetchPaid(`${API}/tiers/v1/${tierName}`, { method: "POST", headers: { "Content-Type": "application/json" } }); |
| 66 | + const data = await res.json(); |
| 67 | + if (!res.ok) { console.error(JSON.stringify({ status: "error", http: res.status, ...data })); process.exit(1); } |
| 68 | + console.log(JSON.stringify(data, null, 2)); |
| 69 | +} |
| 70 | + |
| 71 | +export async function run(sub, args) { |
| 72 | + if (!sub || sub === '--help' || sub === '-h') { |
| 73 | + console.log(HELP); |
| 74 | + process.exit(0); |
| 75 | + } |
| 76 | + switch (sub) { |
| 77 | + case "status": await status(); break; |
| 78 | + case "set": await set(args[0]); break; |
| 79 | + default: |
| 80 | + console.error(`Unknown subcommand: ${sub}\n`); |
| 81 | + console.log(HELP); |
| 82 | + process.exit(1); |
| 83 | + } |
| 84 | +} |
0 commit comments