@layr-labs/ecloud-cli 0.5.0-dev.3 → 1.0.0-dev.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -316,7 +316,8 @@ async function createBillingClient(flags) {
316
316
  // src/commands/billing/top-up.ts
317
317
  import { formatUnits } from "viem";
318
318
  import chalk2 from "chalk";
319
- import { input as input2 } from "@inquirer/prompts";
319
+ import { input as input2, select as select2, confirm } from "@inquirer/prompts";
320
+ import open from "open";
320
321
 
321
322
  // src/telemetry.ts
322
323
  import {
@@ -380,12 +381,22 @@ async function withTelemetry(command, action) {
380
381
  var POLL_INTERVAL_MS = 5e3;
381
382
  var POLL_TIMEOUT_MS = 3 * 60 * 1e3;
382
383
  var BillingTopUp = class _BillingTopUp extends Command {
383
- static description = "Purchase EigenCompute credits with USDC";
384
+ static description = "Purchase EigenCompute credits with USDC or credit card";
385
+ static examples = [
386
+ "<%= config.bin %> billing top-up",
387
+ "<%= config.bin %> billing top-up --method usdc --amount 50",
388
+ "<%= config.bin %> billing top-up --method card --amount 25"
389
+ ];
384
390
  static flags = {
385
391
  ...commonFlags,
392
+ method: Flags2.string({
393
+ required: false,
394
+ description: "Payment method: usdc (on-chain) or card (credit card)",
395
+ options: ["usdc", "card"]
396
+ }),
386
397
  amount: Flags2.string({
387
398
  required: false,
388
- description: "Amount of USDC to spend (e.g., '50')"
399
+ description: "Amount to spend (USDC for on-chain, whole dollars for card)"
389
400
  }),
390
401
  account: Flags2.string({
391
402
  required: false,
@@ -421,96 +432,151 @@ ${chalk2.bold("Purchase EigenCompute credits")}`);
421
432
  const remaining = status.remainingCredits ?? 0;
422
433
  const applied = status.creditsApplied ?? 0;
423
434
  baselineTotal = remaining + applied;
424
- if (status.remainingCredits !== void 0) {
425
- this.log(` ${chalk2.bold("Credits:")} ${chalk2.cyan(`$${status.remainingCredits.toFixed(2)}`)}`);
426
- }
435
+ this.log(` ${chalk2.bold("Credits:")} ${chalk2.cyan(`$${remaining.toFixed(2)}`)}`);
427
436
  } catch {
428
437
  this.debug("Could not fetch current credit balance");
429
438
  }
430
- const onChainState = await billing.getTopUpInfo();
431
- const { usdcBalance, minimumPurchase } = onChainState;
432
- const balanceFormatted = formatUnits(usdcBalance, 6);
433
- this.log(` ${chalk2.bold("USDC:")} ${balanceFormatted} USDC`);
434
- if (usdcBalance === BigInt(0)) {
435
- this.log(`
439
+ const method = flags.method ?? await select2({
440
+ message: "How would you like to pay?",
441
+ choices: [
442
+ { value: "card", name: "Credit card" },
443
+ { value: "usdc", name: "USDC (on-chain)" }
444
+ ]
445
+ });
446
+ if (method === "usdc") {
447
+ await this.handleUsdc(billing, flags, walletAddress, targetAccount, baselineTotal);
448
+ } else {
449
+ await this.handleCard(billing, flags, baselineTotal);
450
+ }
451
+ });
452
+ }
453
+ async handleUsdc(billing, flags, walletAddress, targetAccount, baselineTotal) {
454
+ const onChainState = await billing.getTopUpInfo();
455
+ const { usdcBalance, minimumPurchase } = onChainState;
456
+ const balanceFormatted = formatUnits(usdcBalance, 6);
457
+ this.log(` ${chalk2.bold("USDC:")} ${balanceFormatted} USDC`);
458
+ if (usdcBalance === BigInt(0)) {
459
+ this.log(`
436
460
  ${chalk2.yellow(" No USDC in wallet.")}`);
437
- this.log(` Send USDC on Sepolia to: ${chalk2.cyan(walletAddress)}`);
438
- this.log(` Then re-run: ${chalk2.cyan("ecloud billing top-up")}
461
+ this.log(` Send USDC on Sepolia to: ${chalk2.cyan(walletAddress)}`);
462
+ this.log(` Then re-run: ${chalk2.cyan("ecloud billing top-up")}
439
463
  `);
440
- return;
464
+ return;
465
+ }
466
+ const minimumFormatted = formatUnits(minimumPurchase, 6);
467
+ const amountStr = flags.amount ?? await input2({
468
+ message: `How much USDC to spend on credits? (minimum: ${minimumFormatted})`,
469
+ validate: (val) => {
470
+ const n = parseFloat(val);
471
+ if (isNaN(n) || n <= 0) return "Enter a positive number";
472
+ const raw = BigInt(Math.round(n * 1e6));
473
+ if (raw < minimumPurchase)
474
+ return `Minimum purchase is ${minimumFormatted} USDC`;
475
+ if (raw > usdcBalance)
476
+ return `Insufficient balance. You have ${balanceFormatted} USDC`;
477
+ return true;
441
478
  }
442
- const minimumFormatted = formatUnits(minimumPurchase, 6);
443
- const amountStr = flags.amount ?? await input2({
444
- message: `How much USDC to spend on credits? (minimum: ${minimumFormatted})`,
445
- validate: (val) => {
446
- const n = parseFloat(val);
447
- if (isNaN(n) || n <= 0) return "Enter a positive number";
448
- const raw = BigInt(Math.round(n * 1e6));
449
- if (raw < minimumPurchase)
450
- return `Minimum purchase is ${minimumFormatted} USDC`;
451
- if (raw > usdcBalance)
452
- return `Insufficient balance. You have ${balanceFormatted} USDC`;
453
- return true;
454
- }
455
- });
456
- const amountFloat = parseFloat(amountStr);
457
- const amountRaw = BigInt(Math.round(amountFloat * 1e6));
458
- if (amountRaw < minimumPurchase) {
459
- this.error(`Minimum purchase is ${minimumFormatted} USDC`);
479
+ });
480
+ const amountFloat = parseFloat(amountStr);
481
+ const amountRaw = BigInt(Math.round(amountFloat * 1e6));
482
+ if (amountRaw < minimumPurchase) {
483
+ this.error(`Minimum purchase is ${minimumFormatted} USDC`);
484
+ }
485
+ if (amountRaw > usdcBalance) {
486
+ this.error(
487
+ `Insufficient USDC balance. You have ${balanceFormatted} USDC but requested ${amountFloat.toFixed(2)}`
488
+ );
489
+ }
490
+ this.log(`
491
+ Purchasing ${chalk2.bold(`$${amountFloat.toFixed(2)}`)} in credits...`);
492
+ const { txHash } = await billing.topUp({
493
+ amount: amountRaw,
494
+ account: targetAccount
495
+ });
496
+ this.log(` ${chalk2.green("\u2713")} Transaction confirmed: ${txHash}`);
497
+ await this.pollForCredits(billing, flags, baselineTotal, amountFloat);
498
+ }
499
+ async handleCard(billing, flags, baselineTotal) {
500
+ const MINIMUM_DOLLARS = 5;
501
+ const amountStr = flags.amount ?? await input2({
502
+ message: `How many dollars of credits to purchase? (minimum: $${MINIMUM_DOLLARS})`,
503
+ validate: (val) => {
504
+ const n = parseInt(val, 10);
505
+ if (isNaN(n) || n <= 0) return "Enter a positive whole number";
506
+ if (n.toString() !== val.trim()) return "Enter a whole dollar amount (no cents)";
507
+ if (n < MINIMUM_DOLLARS) return `Minimum purchase is $${MINIMUM_DOLLARS}`;
508
+ return true;
460
509
  }
461
- if (amountRaw > usdcBalance) {
462
- this.error(
463
- `Insufficient USDC balance. You have ${balanceFormatted} USDC but requested ${amountFloat.toFixed(2)}`
464
- );
510
+ });
511
+ const dollars = parseInt(amountStr, 10);
512
+ if (isNaN(dollars) || dollars < MINIMUM_DOLLARS) {
513
+ this.error(`Minimum purchase is $${MINIMUM_DOLLARS}`);
514
+ }
515
+ const amountCents = dollars * 100;
516
+ const { paymentMethods } = await billing.getPaymentMethods();
517
+ let useExistingCard = false;
518
+ let paymentMethodId;
519
+ if (paymentMethods.length > 0) {
520
+ const card = paymentMethods[0];
521
+ const lastFour = card.stripePaymentMethodId.slice(-4);
522
+ useExistingCard = await confirm({
523
+ message: `Use card on file (ending in ${lastFour})?`,
524
+ default: true
525
+ });
526
+ if (useExistingCard) {
527
+ paymentMethodId = card.id;
465
528
  }
529
+ }
530
+ this.log(`
531
+ Purchasing ${chalk2.bold(`$${dollars}`)} in credits...`);
532
+ const result = await billing.purchaseCredits(amountCents, paymentMethodId);
533
+ if (result.checkoutUrl) {
466
534
  this.log(`
467
- Purchasing ${chalk2.bold(`$${amountFloat.toFixed(2)}`)} in credits...`);
468
- const { txHash } = await billing.topUp({
469
- amount: amountRaw,
470
- account: targetAccount
471
- });
472
- this.log(` ${chalk2.green("\u2713")} Transaction confirmed: ${txHash}`);
473
- this.log(chalk2.gray("\n Waiting for credits to appear..."));
474
- const startTime = Date.now();
475
- while (Date.now() - startTime < POLL_TIMEOUT_MS) {
476
- await new Promise((r) => setTimeout(r, POLL_INTERVAL_MS));
477
- try {
478
- const status = await billing.getStatus({
479
- productId: flags.product
480
- });
481
- const remaining = status.remainingCredits ?? 0;
482
- const applied = status.creditsApplied ?? 0;
483
- const currentTotal = remaining + applied;
484
- this.debug(`Poll: remaining=${remaining}, applied=${applied}, total=${currentTotal}, baseline=${baselineTotal}`);
485
- if (baselineTotal === void 0 || currentTotal > baselineTotal) {
486
- const creditsAdded = baselineTotal !== void 0 ? currentTotal - baselineTotal : void 0;
487
- const isMatched = creditsAdded !== void 0 && Math.abs(creditsAdded - amountFloat * 2) < 0.01;
488
- const appliedFromTopUp = creditsAdded !== void 0 ? creditsAdded - remaining : 0;
489
- this.log(`
490
- ${chalk2.green("\u2713")} Credits received: ${chalk2.cyan(`$${(creditsAdded ?? amountFloat).toFixed(2)}`)}`);
491
- if (isMatched) {
492
- this.log(` ${chalk2.green("\u2713")} Includes $${amountFloat.toFixed(2)} match bonus!`);
493
- }
494
- if (remaining > 0) {
495
- this.log(` Remaining balance: ${chalk2.cyan(`$${remaining.toFixed(2)}`)}`);
496
- }
497
- if (appliedFromTopUp > 0) {
498
- this.log(` ${chalk2.gray(`$${appliedFromTopUp.toFixed(2)} applied to current bill`)}`);
499
- }
500
- this.log();
501
- return;
535
+ ${chalk2.cyan(result.checkoutUrl)}`);
536
+ this.log(chalk2.gray(" Opening checkout in browser..."));
537
+ await open(result.checkoutUrl);
538
+ } else {
539
+ this.log(` ${chalk2.green("\u2713")} Payment submitted`);
540
+ }
541
+ await this.pollForCredits(billing, flags, baselineTotal, dollars);
542
+ }
543
+ async pollForCredits(billing, flags, baselineTotal, amountPurchased) {
544
+ this.log(chalk2.gray("\n Waiting for credits to appear..."));
545
+ const startTime = Date.now();
546
+ while (Date.now() - startTime < POLL_TIMEOUT_MS) {
547
+ await new Promise((r) => setTimeout(r, POLL_INTERVAL_MS));
548
+ try {
549
+ const status = await billing.getStatus({
550
+ productId: flags.product
551
+ });
552
+ const remaining = status.remainingCredits ?? 0;
553
+ const applied = status.creditsApplied ?? 0;
554
+ const currentTotal = remaining + applied;
555
+ this.debug(
556
+ `Poll: remaining=${remaining}, applied=${applied}, total=${currentTotal}, baseline=${baselineTotal}`
557
+ );
558
+ if (baselineTotal === void 0 || currentTotal > baselineTotal) {
559
+ const creditsAdded = baselineTotal !== void 0 ? currentTotal - baselineTotal : void 0;
560
+ this.log(
561
+ `
562
+ ${chalk2.green("\u2713")} Credits received: ${chalk2.cyan(`$${(creditsAdded ?? amountPurchased).toFixed(2)}`)}`
563
+ );
564
+ if (remaining > 0) {
565
+ this.log(` Remaining balance: ${chalk2.cyan(`$${remaining.toFixed(2)}`)}`);
502
566
  }
503
- } catch {
504
- this.debug("Error polling for credit balance");
567
+ this.log();
568
+ return;
505
569
  }
570
+ } catch {
571
+ this.debug("Error polling for credit balance");
506
572
  }
507
- this.log(
508
- `
573
+ }
574
+ this.log(
575
+ `
509
576
  ${chalk2.yellow("\u26A0")} Credits haven't appeared yet. This can take a few minutes.`
510
- );
511
- this.log(` ${chalk2.gray("Check your balance:")} ecloud billing status
577
+ );
578
+ this.log(` ${chalk2.gray("Check your balance:")} ecloud billing status
512
579
  `);
513
- });
514
580
  }
515
581
  };
516
582
  export {