@layr-labs/ecloud-sdk 0.5.0 → 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.
@@ -1,5 +1,5 @@
1
1
  import { Address, Hex, WalletClient, PublicClient } from 'viem';
2
- import { a3 as SubscriptionOpts, a1 as SubscribeResponse, Z as ProductSubscriptionResponse, g as CancelResponse } from './index-U2vKBrry.cjs';
2
+ import { a6 as SubscriptionOpts, a4 as SubscribeResponse, a0 as ProductSubscriptionResponse, g as CancelResponse, M as PaymentMethodsResponse, m as CreditPurchaseResponse } from './index-BbH7ZCIu.cjs';
3
3
 
4
4
  /**
5
5
  * Main Billing namespace entry point
@@ -33,6 +33,8 @@ interface BillingModule {
33
33
  getTopUpInfo: () => Promise<TopUpInfo>;
34
34
  /** Purchase credits with USDC on-chain */
35
35
  topUp: (opts: TopUpOpts) => Promise<TopUpResult>;
36
+ getPaymentMethods: () => Promise<PaymentMethodsResponse>;
37
+ purchaseCredits: (amountCents: number, paymentMethodId?: string) => Promise<CreditPurchaseResponse>;
36
38
  }
37
39
  interface BillingModuleConfig {
38
40
  verbose?: boolean;
package/dist/billing.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { Address, Hex, WalletClient, PublicClient } from 'viem';
2
- import { a3 as SubscriptionOpts, a1 as SubscribeResponse, Z as ProductSubscriptionResponse, g as CancelResponse } from './index-U2vKBrry.js';
2
+ import { a6 as SubscriptionOpts, a4 as SubscribeResponse, a0 as ProductSubscriptionResponse, g as CancelResponse, M as PaymentMethodsResponse, m as CreditPurchaseResponse } from './index-BbH7ZCIu.js';
3
3
 
4
4
  /**
5
5
  * Main Billing namespace entry point
@@ -33,6 +33,8 @@ interface BillingModule {
33
33
  getTopUpInfo: () => Promise<TopUpInfo>;
34
34
  /** Purchase credits with USDC on-chain */
35
35
  topUp: (opts: TopUpOpts) => Promise<TopUpResult>;
36
+ getPaymentMethods: () => Promise<PaymentMethodsResponse>;
37
+ purchaseCredits: (amountCents: number, paymentMethodId?: string) => Promise<CreditPurchaseResponse>;
36
38
  }
37
39
  interface BillingModuleConfig {
38
40
  verbose?: boolean;
package/dist/billing.js CHANGED
@@ -283,6 +283,20 @@ var BillingApiClient = class {
283
283
  const endpoint = `${this.config.billingApiServerURL}/products/${productId}/subscription`;
284
284
  await this.makeAuthenticatedRequest(endpoint, "DELETE", productId);
285
285
  }
286
+ async getPaymentMethods() {
287
+ const endpoint = `${this.config.billingApiServerURL}/v1/payment-methods`;
288
+ const resp = await this.makeAuthenticatedRequest(endpoint, "GET", "compute");
289
+ return resp.json();
290
+ }
291
+ async purchaseCredits(amountCents, paymentMethodId) {
292
+ const endpoint = `${this.config.billingApiServerURL}/v1/credits/purchase`;
293
+ const body = { amountCents };
294
+ if (paymentMethodId) {
295
+ body.paymentMethodId = paymentMethodId;
296
+ }
297
+ const resp = await this.makeAuthenticatedRequest(endpoint, "POST", "compute", body);
298
+ return resp.json();
299
+ }
286
300
  // ==========================================================================
287
301
  // Internal Methods
288
302
  // ==========================================================================
@@ -292,10 +306,19 @@ var BillingApiClient = class {
292
306
  * Uses session auth if useSession is true, otherwise uses EIP-712 signature auth.
293
307
  */
294
308
  async makeAuthenticatedRequest(url, method, productId, body) {
295
- if (this.useSession) {
296
- return this.makeSessionAuthenticatedRequest(url, method, body);
309
+ if (this.options.verbose) {
310
+ console.debug(`[BillingAPI] ${method} ${url}`);
311
+ }
312
+ const resp = this.useSession ? await this.makeSessionAuthenticatedRequest(url, method, body) : await this.makeSignatureAuthenticatedRequest(url, method, productId, body);
313
+ if (this.options.verbose) {
314
+ const data = await resp.json();
315
+ console.debug(`[BillingAPI] Response:`, JSON.stringify(data, null, 2));
316
+ return {
317
+ json: async () => data,
318
+ text: async () => JSON.stringify(data)
319
+ };
297
320
  }
298
- return this.makeSignatureAuthenticatedRequest(url, method, productId, body);
321
+ return resp;
299
322
  }
300
323
  /**
301
324
  * Make a request using session-based authentication (cookies)
@@ -481,7 +504,13 @@ function getEnvironmentConfig(environment, chainID) {
481
504
  const resolvedChainID = chainID || (environment === "sepolia" || environment === "sepolia-dev" ? SEPOLIA_CHAIN_ID : MAINNET_CHAIN_ID);
482
505
  return {
483
506
  ...env,
484
- chainID: BigInt(resolvedChainID)
507
+ chainID: BigInt(resolvedChainID),
508
+ ...process.env.ECLOUD_USER_API_URL && {
509
+ userApiServerURL: process.env.ECLOUD_USER_API_URL
510
+ },
511
+ ...process.env.ECLOUD_RPC_URL && {
512
+ defaultRPCURL: process.env.ECLOUD_RPC_URL
513
+ }
485
514
  };
486
515
  }
487
516
  function getBillingEnvironmentConfig(build) {
@@ -489,10 +518,15 @@ function getBillingEnvironmentConfig(build) {
489
518
  if (!config) {
490
519
  throw new Error(`Unknown billing environment: ${build}`);
491
520
  }
492
- return config;
521
+ return {
522
+ ...config,
523
+ ...process.env.ECLOUD_BILLING_API_URL && {
524
+ billingApiServerURL: process.env.ECLOUD_BILLING_API_URL
525
+ }
526
+ };
493
527
  }
494
528
  function getBuildType() {
495
- const buildTimeType = true ? "prod"?.toLowerCase() : void 0;
529
+ const buildTimeType = true ? "dev"?.toLowerCase() : void 0;
496
530
  const runtimeType = process.env.BUILD_TYPE?.toLowerCase();
497
531
  const buildType = buildTimeType || runtimeType;
498
532
  if (buildType === "dev") {
@@ -2032,7 +2066,7 @@ function createBillingModule(config) {
2032
2066
  const address = walletClient.account.address;
2033
2067
  const logger = getLogger(verbose);
2034
2068
  const billingEnvConfig = getBillingEnvironmentConfig(getBuildType());
2035
- const billingApi = new BillingApiClient(billingEnvConfig, walletClient);
2069
+ const billingApi = new BillingApiClient(billingEnvConfig, walletClient, { verbose });
2036
2070
  const environmentConfig = getEnvironmentConfig(environment);
2037
2071
  const usdcCreditsAddress = environmentConfig.usdcCreditsAddress;
2038
2072
  if (!usdcCreditsAddress) {
@@ -2196,6 +2230,12 @@ function createBillingModule(config) {
2196
2230
  };
2197
2231
  }
2198
2232
  );
2233
+ },
2234
+ async getPaymentMethods() {
2235
+ return billingApi.getPaymentMethods();
2236
+ },
2237
+ async purchaseCredits(amountCents, paymentMethodId) {
2238
+ return billingApi.purchaseCredits(amountCents, paymentMethodId);
2199
2239
  }
2200
2240
  };
2201
2241
  return module;