@layr-labs/ecloud-sdk 1.0.0-dev.3 → 1.0.0-dev.4

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 { a6 as SubscriptionOpts, a4 as SubscribeResponse, a0 as ProductSubscriptionResponse, g as CancelResponse, M as PaymentMethodsResponse, m as CreditPurchaseResponse } from './index-Bac4HjC0.cjs';
2
+ import { a6 as SubscriptionOpts, a4 as SubscribeResponse, a0 as ProductSubscriptionResponse, g as CancelResponse, M as PaymentMethodsResponse, m as CreditPurchaseResponse } from './index-BoCE0hIh.cjs';
3
3
 
4
4
  /**
5
5
  * Main Billing namespace entry point
@@ -8,11 +8,14 @@ import { a6 as SubscriptionOpts, a4 as SubscribeResponse, a0 as ProductSubscript
8
8
  * (privateKeyToAccount) and external signers (MetaMask, etc.).
9
9
  */
10
10
 
11
+ type BillingChain = "ethereum" | "base";
11
12
  interface TopUpOpts {
12
13
  /** Amount in raw USDC units (6 decimals, e.g. 50_000_000n = 50 USDC) */
13
14
  amount: bigint;
14
15
  /** Target account for purchaseCreditsFor (defaults to wallet address) */
15
16
  account?: Address;
17
+ /** Which blockchain to transact on (defaults to "ethereum") */
18
+ chain?: BillingChain;
16
19
  }
17
20
  interface TopUpResult {
18
21
  txHash: Hex;
@@ -30,11 +33,15 @@ interface BillingModule {
30
33
  getStatus: (opts?: SubscriptionOpts) => Promise<ProductSubscriptionResponse>;
31
34
  cancel: (opts?: SubscriptionOpts) => Promise<CancelResponse>;
32
35
  /** Read on-chain state needed for top-up */
33
- getTopUpInfo: () => Promise<TopUpInfo>;
36
+ getTopUpInfo: (opts?: {
37
+ chain?: BillingChain;
38
+ }) => Promise<TopUpInfo>;
34
39
  /** Purchase credits with USDC on-chain */
35
40
  topUp: (opts: TopUpOpts) => Promise<TopUpResult>;
36
41
  getPaymentMethods: () => Promise<PaymentMethodsResponse>;
37
42
  purchaseCredits: (amountCents: number, paymentMethodId?: string) => Promise<CreditPurchaseResponse>;
43
+ /** Check if Base chain is configured for this environment */
44
+ hasBaseSupport: () => boolean;
38
45
  }
39
46
  interface BillingModuleConfig {
40
47
  verbose?: boolean;
@@ -42,7 +49,8 @@ interface BillingModuleConfig {
42
49
  skipTelemetry?: boolean;
43
50
  publicClient: PublicClient;
44
51
  environment: string;
52
+ privateKey?: Hex;
45
53
  }
46
54
  declare function createBillingModule(config: BillingModuleConfig): BillingModule;
47
55
 
48
- export { type BillingModule, type BillingModuleConfig, type TopUpInfo, type TopUpOpts, type TopUpResult, createBillingModule };
56
+ export { type BillingChain, type BillingModule, type BillingModuleConfig, type TopUpInfo, type TopUpOpts, type TopUpResult, createBillingModule };
package/dist/billing.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { Address, Hex, WalletClient, PublicClient } from 'viem';
2
- import { a6 as SubscriptionOpts, a4 as SubscribeResponse, a0 as ProductSubscriptionResponse, g as CancelResponse, M as PaymentMethodsResponse, m as CreditPurchaseResponse } from './index-Bac4HjC0.js';
2
+ import { a6 as SubscriptionOpts, a4 as SubscribeResponse, a0 as ProductSubscriptionResponse, g as CancelResponse, M as PaymentMethodsResponse, m as CreditPurchaseResponse } from './index-BoCE0hIh.js';
3
3
 
4
4
  /**
5
5
  * Main Billing namespace entry point
@@ -8,11 +8,14 @@ import { a6 as SubscriptionOpts, a4 as SubscribeResponse, a0 as ProductSubscript
8
8
  * (privateKeyToAccount) and external signers (MetaMask, etc.).
9
9
  */
10
10
 
11
+ type BillingChain = "ethereum" | "base";
11
12
  interface TopUpOpts {
12
13
  /** Amount in raw USDC units (6 decimals, e.g. 50_000_000n = 50 USDC) */
13
14
  amount: bigint;
14
15
  /** Target account for purchaseCreditsFor (defaults to wallet address) */
15
16
  account?: Address;
17
+ /** Which blockchain to transact on (defaults to "ethereum") */
18
+ chain?: BillingChain;
16
19
  }
17
20
  interface TopUpResult {
18
21
  txHash: Hex;
@@ -30,11 +33,15 @@ interface BillingModule {
30
33
  getStatus: (opts?: SubscriptionOpts) => Promise<ProductSubscriptionResponse>;
31
34
  cancel: (opts?: SubscriptionOpts) => Promise<CancelResponse>;
32
35
  /** Read on-chain state needed for top-up */
33
- getTopUpInfo: () => Promise<TopUpInfo>;
36
+ getTopUpInfo: (opts?: {
37
+ chain?: BillingChain;
38
+ }) => Promise<TopUpInfo>;
34
39
  /** Purchase credits with USDC on-chain */
35
40
  topUp: (opts: TopUpOpts) => Promise<TopUpResult>;
36
41
  getPaymentMethods: () => Promise<PaymentMethodsResponse>;
37
42
  purchaseCredits: (amountCents: number, paymentMethodId?: string) => Promise<CreditPurchaseResponse>;
43
+ /** Check if Base chain is configured for this environment */
44
+ hasBaseSupport: () => boolean;
38
45
  }
39
46
  interface BillingModuleConfig {
40
47
  verbose?: boolean;
@@ -42,7 +49,8 @@ interface BillingModuleConfig {
42
49
  skipTelemetry?: boolean;
43
50
  publicClient: PublicClient;
44
51
  environment: string;
52
+ privateKey?: Hex;
45
53
  }
46
54
  declare function createBillingModule(config: BillingModuleConfig): BillingModule;
47
55
 
48
- export { type BillingModule, type BillingModuleConfig, type TopUpInfo, type TopUpOpts, type TopUpResult, createBillingModule };
56
+ export { type BillingChain, type BillingModule, type BillingModuleConfig, type TopUpInfo, type TopUpOpts, type TopUpResult, createBillingModule };
package/dist/billing.js CHANGED
@@ -429,6 +429,7 @@ Please check:
429
429
  // src/client/common/config/environment.ts
430
430
  var SEPOLIA_CHAIN_ID = 11155111;
431
431
  var MAINNET_CHAIN_ID = 1;
432
+ var BASE_SEPOLIA_CHAIN_ID = 84532;
432
433
  var CommonAddresses = {
433
434
  ERC7702Delegator: "0x63c0c19a282a1b52b07dd5a65b58948a07dae32b"
434
435
  };
@@ -458,7 +459,9 @@ var ENVIRONMENTS = {
458
459
  kmsServerURL: "http://10.128.0.57:8080",
459
460
  userApiServerURL: "https://userapi-compute-sepolia-dev.eigencloud.xyz",
460
461
  defaultRPCURL: "https://ethereum-sepolia-rpc.publicnode.com",
461
- usdcCreditsAddress: "0xbdA3897c3A428763B59015C64AB766c288C97376"
462
+ usdcCreditsAddress: "0xbdA3897c3A428763B59015C64AB766c288C97376",
463
+ baseUsdcCreditsAddress: "0x7673a47463F80c6a3553Db9E54c8cDcd5313d0ac",
464
+ baseRPCURL: "https://base-sepolia-rpc.publicnode.com"
462
465
  },
463
466
  sepolia: {
464
467
  name: "sepolia",
@@ -470,7 +473,9 @@ var ENVIRONMENTS = {
470
473
  userApiServerURL: "https://userapi-compute-sepolia-prod.eigencloud.xyz",
471
474
  defaultRPCURL: "https://ethereum-sepolia-rpc.publicnode.com",
472
475
  billingRPCURL: "https://ethereum-rpc.publicnode.com",
473
- usdcCreditsAddress: "0xed9c88640ca9149Bd9f7ee6620074af10F2E145d"
476
+ usdcCreditsAddress: "0xed9c88640ca9149Bd9f7ee6620074af10F2E145d",
477
+ baseUsdcCreditsAddress: "0x7673a47463F80c6a3553Db9E54c8cDcd5313d0ac",
478
+ baseRPCURL: "https://base-sepolia-rpc.publicnode.com"
474
479
  },
475
480
  "mainnet-alpha": {
476
481
  name: "mainnet-alpha",
@@ -560,6 +565,41 @@ function isEnvironmentAvailable(environment) {
560
565
  return getAvailableEnvironments().includes(environment);
561
566
  }
562
567
 
568
+ // src/client/common/utils/helpers.ts
569
+ import { extractChain, createPublicClient, createWalletClient, http, fallback } from "viem";
570
+ import { sepolia as sepolia2 } from "viem/chains";
571
+ import { privateKeyToAccount } from "viem/accounts";
572
+
573
+ // src/client/common/constants.ts
574
+ import { sepolia, mainnet, baseSepolia } from "viem/chains";
575
+ var SUPPORTED_CHAINS = [mainnet, sepolia, baseSepolia];
576
+
577
+ // src/client/common/utils/helpers.ts
578
+ function getChainFromID(chainID, fallback2 = sepolia2) {
579
+ const id = Number(chainID);
580
+ return extractChain({ chains: SUPPORTED_CHAINS, id }) || fallback2;
581
+ }
582
+ function createClients(options) {
583
+ const { privateKey, rpcUrl, chainId } = options;
584
+ const privateKeyHex = addHexPrefix(privateKey);
585
+ const account = privateKeyToAccount(privateKeyHex);
586
+ const chain = getChainFromID(chainId);
587
+ const transport = typeof rpcUrl === "string" ? http(rpcUrl) : fallback(rpcUrl.map((url) => http(url)));
588
+ const publicClient = createPublicClient({
589
+ chain,
590
+ transport
591
+ });
592
+ const walletClient = createWalletClient({
593
+ account,
594
+ chain,
595
+ transport
596
+ });
597
+ return { walletClient, publicClient };
598
+ }
599
+ function addHexPrefix(value) {
600
+ return value.startsWith("0x") ? value : `0x${value}`;
601
+ }
602
+
563
603
  // src/client/common/utils/logger.ts
564
604
  var getLogger = (verbose) => ({
565
605
  info: (...args) => console.info(...args),
@@ -571,14 +611,6 @@ var getLogger = (verbose) => ({
571
611
  // src/client/common/utils/userapi.ts
572
612
  import axios3 from "axios";
573
613
 
574
- // src/client/common/utils/helpers.ts
575
- import { extractChain, createPublicClient, createWalletClient, http, fallback } from "viem";
576
- import { sepolia as sepolia2 } from "viem/chains";
577
- import { privateKeyToAccount } from "viem/accounts";
578
-
579
- // src/client/common/constants.ts
580
- import { sepolia, mainnet } from "viem/chains";
581
-
582
614
  // src/client/common/utils/retry.ts
583
615
  import axios2 from "axios";
584
616
 
@@ -2074,7 +2106,7 @@ var ERC20_default = [
2074
2106
 
2075
2107
  // src/client/modules/billing/index.ts
2076
2108
  function createBillingModule(config) {
2077
- const { verbose = false, skipTelemetry = false, walletClient, publicClient, environment } = config;
2109
+ const { verbose = false, skipTelemetry = false, walletClient, publicClient, environment, privateKey } = config;
2078
2110
  if (!walletClient.account) {
2079
2111
  throw new Error("WalletClient must have an account attached");
2080
2112
  }
@@ -2083,35 +2115,69 @@ function createBillingModule(config) {
2083
2115
  const billingEnvConfig = getBillingEnvironmentConfig(getBuildType());
2084
2116
  const billingApi = new BillingApiClient(billingEnvConfig, walletClient, { verbose });
2085
2117
  const environmentConfig = getEnvironmentConfig(environment);
2086
- const usdcCreditsAddress = environmentConfig.usdcCreditsAddress;
2087
- if (!usdcCreditsAddress) {
2118
+ if (!environmentConfig.usdcCreditsAddress) {
2088
2119
  throw new Error(`USDCCredits contract address not configured for environment "${environment}"`);
2089
2120
  }
2121
+ const usdcCreditsAddress = environmentConfig.usdcCreditsAddress;
2122
+ const baseUsdcCreditsAddress = environmentConfig.baseUsdcCreditsAddress;
2123
+ const baseRPCURL = environmentConfig.baseRPCURL;
2124
+ function resolveChainConfig(chain) {
2125
+ if (chain === "base") {
2126
+ if (!baseUsdcCreditsAddress || !baseRPCURL) {
2127
+ throw new Error(`Base chain not configured for environment "${environment}"`);
2128
+ }
2129
+ if (!privateKey) {
2130
+ throw new Error("Private key required for Base chain transactions");
2131
+ }
2132
+ const baseClients = createClients({
2133
+ privateKey,
2134
+ rpcUrl: baseRPCURL,
2135
+ chainId: BigInt(BASE_SEPOLIA_CHAIN_ID)
2136
+ });
2137
+ return {
2138
+ pub: baseClients.publicClient,
2139
+ wallet: baseClients.walletClient,
2140
+ creditsAddress: baseUsdcCreditsAddress,
2141
+ envConfig: {
2142
+ ...environmentConfig,
2143
+ chainID: BigInt(BASE_SEPOLIA_CHAIN_ID),
2144
+ defaultRPCURL: baseRPCURL
2145
+ }
2146
+ };
2147
+ }
2148
+ return {
2149
+ pub: publicClient,
2150
+ wallet: walletClient,
2151
+ creditsAddress: usdcCreditsAddress,
2152
+ envConfig: environmentConfig
2153
+ };
2154
+ }
2090
2155
  const module = {
2091
2156
  address,
2092
- async getTopUpInfo() {
2093
- const usdcAddress = await publicClient.readContract({
2094
- address: usdcCreditsAddress,
2157
+ async getTopUpInfo(opts) {
2158
+ const { pub, creditsAddress } = resolveChainConfig(opts?.chain);
2159
+ const usdcAddress = await pub.readContract({
2160
+ address: creditsAddress,
2095
2161
  abi: USDCCredits_default,
2096
2162
  functionName: "usdc"
2097
2163
  });
2098
2164
  const [minimumPurchase, usdcBalance, currentAllowance] = await Promise.all([
2099
- publicClient.readContract({
2100
- address: usdcCreditsAddress,
2165
+ pub.readContract({
2166
+ address: creditsAddress,
2101
2167
  abi: USDCCredits_default,
2102
2168
  functionName: "minimumPurchase"
2103
2169
  }),
2104
- publicClient.readContract({
2170
+ pub.readContract({
2105
2171
  address: usdcAddress,
2106
2172
  abi: ERC20_default,
2107
2173
  functionName: "balanceOf",
2108
2174
  args: [address]
2109
2175
  }),
2110
- publicClient.readContract({
2176
+ pub.readContract({
2111
2177
  address: usdcAddress,
2112
2178
  abi: ERC20_default,
2113
2179
  functionName: "allowance",
2114
- args: [address, usdcCreditsAddress]
2180
+ args: [address, creditsAddress]
2115
2181
  })
2116
2182
  ]);
2117
2183
  return { usdcAddress, minimumPurchase, usdcBalance, currentAllowance };
@@ -2121,11 +2187,12 @@ function createBillingModule(config) {
2121
2187
  {
2122
2188
  functionName: "topUp",
2123
2189
  skipTelemetry,
2124
- properties: { amount: opts.amount.toString() }
2190
+ properties: { amount: opts.amount.toString(), chain: opts.chain || "ethereum" }
2125
2191
  },
2126
2192
  async () => {
2127
2193
  const targetAccount = opts.account ?? address;
2128
- const { usdcAddress, currentAllowance } = await module.getTopUpInfo();
2194
+ const { pub, wallet, creditsAddress, envConfig } = resolveChainConfig(opts.chain);
2195
+ const { usdcAddress, currentAllowance } = await module.getTopUpInfo({ chain: opts.chain });
2129
2196
  const executions = [];
2130
2197
  if (currentAllowance < opts.amount) {
2131
2198
  executions.push({
@@ -2134,12 +2201,12 @@ function createBillingModule(config) {
2134
2201
  callData: encodeFunctionData3({
2135
2202
  abi: ERC20_default,
2136
2203
  functionName: "approve",
2137
- args: [usdcCreditsAddress, opts.amount]
2204
+ args: [creditsAddress, opts.amount]
2138
2205
  })
2139
2206
  });
2140
2207
  }
2141
2208
  executions.push({
2142
- target: usdcCreditsAddress,
2209
+ target: creditsAddress,
2143
2210
  value: 0n,
2144
2211
  callData: encodeFunctionData3({
2145
2212
  abi: USDCCredits_default,
@@ -2149,9 +2216,9 @@ function createBillingModule(config) {
2149
2216
  });
2150
2217
  const txHash = await executeBatch(
2151
2218
  {
2152
- walletClient,
2153
- publicClient,
2154
- environmentConfig,
2219
+ walletClient: wallet,
2220
+ publicClient: pub,
2221
+ environmentConfig: envConfig,
2155
2222
  executions,
2156
2223
  pendingMessage: "Submitting credit purchase..."
2157
2224
  },
@@ -2251,6 +2318,9 @@ function createBillingModule(config) {
2251
2318
  },
2252
2319
  async purchaseCredits(amountCents, paymentMethodId) {
2253
2320
  return billingApi.purchaseCredits(amountCents, paymentMethodId);
2321
+ },
2322
+ hasBaseSupport() {
2323
+ return !!baseUsdcCreditsAddress && !!baseRPCURL;
2254
2324
  }
2255
2325
  };
2256
2326
  return module;