@alpha-arcade/sdk 0.1.2 → 0.2.0

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.
package/dist/index.d.cts CHANGED
@@ -10,17 +10,20 @@ type AlphaClientConfig = {
10
10
  signer: TransactionSigner;
11
11
  /** The active Algorand address that will sign transactions */
12
12
  activeAddress: string;
13
- /** Matcher contract app ID (mainnet: 741347297) */
13
+ /** Matcher contract app ID (mainnet: 3078581851) */
14
14
  matcherAppId: number;
15
15
  /** USDC ASA ID on Algorand (mainnet: 31566704) */
16
16
  usdcAssetId: number;
17
17
  /** Base URL for the Alpha REST API (default: https://partners.alphaarcade.com/api) */
18
18
  apiBaseUrl?: string;
19
- /** API key for the Alpha partners API (passed as x-api-key header) */
20
- apiKey: string;
19
+ /** API key for the Alpha partners API. Optional -- if not provided, markets are loaded on-chain. */
20
+ apiKey?: string;
21
+ /** Market creator address for on-chain market discovery. Defaults to the Alpha Arcade mainnet creator. */
22
+ marketCreatorAddress?: string;
21
23
  };
22
- /** A prediction market returned from the Alpha API */
24
+ /** A prediction market (from the Alpha API or on-chain discovery) */
23
25
  type Market = {
26
+ /** Market ID (app ID as string for on-chain, UUID for API) */
24
27
  id: string;
25
28
  title: string;
26
29
  slug?: string;
@@ -28,9 +31,13 @@ type Market = {
28
31
  marketAppId: number;
29
32
  yesAssetId: number;
30
33
  noAssetId: number;
31
- yesProb: number;
32
- noProb: number;
33
- volume: number;
34
+ /** YES probability (API only -- not available from on-chain lookup) */
35
+ yesProb?: number;
36
+ /** NO probability (API only -- not available from on-chain lookup) */
37
+ noProb?: number;
38
+ /** Trading volume (API only -- not available from on-chain lookup) */
39
+ volume?: number;
40
+ /** End/resolution timestamp in seconds */
34
41
  endTs: number;
35
42
  resolution?: number;
36
43
  isResolved?: boolean;
@@ -39,6 +46,8 @@ type Market = {
39
46
  featured?: boolean;
40
47
  options?: MarketOption[];
41
48
  feeBase?: number;
49
+ /** Data source: 'onchain' or 'api' */
50
+ source?: 'onchain' | 'api';
42
51
  [key: string]: unknown;
43
52
  };
44
53
  /** An option within a multi-choice market */
@@ -316,7 +325,6 @@ type EscrowGlobalState = {
316
325
  * activeAddress: account.addr,
317
326
  * matcherAppId: 3078581851,
318
327
  * usdcAssetId: 31566704,
319
- * apiKey: 'YOUR_API_KEY',
320
328
  * });
321
329
  *
322
330
  * // Fetch markets
@@ -436,9 +444,10 @@ declare class AlphaClient {
436
444
  */
437
445
  getOpenOrders(marketAppId: number, walletAddress?: string): Promise<OpenOrder[]>;
438
446
  /**
439
- * Fetches all live, tradeable markets from the Alpha API.
447
+ * Fetches all live, tradeable markets.
440
448
  *
441
- * Automatically paginates to retrieve all markets.
449
+ * If an API key is configured, uses the Alpha REST API (richer data: images, categories, volume).
450
+ * Otherwise, discovers markets on-chain from the market creator address (no API key needed).
442
451
  *
443
452
  * @returns Array of live markets
444
453
  */
@@ -446,12 +455,90 @@ declare class AlphaClient {
446
455
  /**
447
456
  * Fetches a single market by its ID.
448
457
  *
449
- * @param marketId - The market ID
458
+ * If an API key is configured, uses the Alpha REST API.
459
+ * Otherwise, reads the market's on-chain global state (pass the market app ID as a string).
460
+ *
461
+ * @param marketId - The market ID (UUID for API, app ID string for on-chain)
450
462
  * @returns The market data, or null if not found
451
463
  */
452
464
  getMarket(marketId: string): Promise<Market | null>;
465
+ /**
466
+ * Fetches all live markets directly from the blockchain (no API key needed).
467
+ *
468
+ * Discovers markets by looking up all apps created by the market creator address.
469
+ * Returns core data: title, asset IDs, resolution time, fees.
470
+ * Does NOT include images, categories, volume, or probabilities.
471
+ *
472
+ * @returns Array of live markets from on-chain data
473
+ */
474
+ getMarketsOnChain(): Promise<Market[]>;
475
+ /**
476
+ * Fetches a single market by app ID directly from the blockchain (no API key needed).
477
+ *
478
+ * @param marketAppId - The market app ID
479
+ * @returns The market data, or null if not found
480
+ */
481
+ getMarketOnChain(marketAppId: number): Promise<Market | null>;
482
+ /**
483
+ * Fetches all live markets from the Alpha REST API (requires API key).
484
+ *
485
+ * Returns richer data than on-chain: images, categories, volume, probabilities.
486
+ *
487
+ * @returns Array of live markets from the API
488
+ */
489
+ getMarketsFromApi(): Promise<Market[]>;
490
+ /**
491
+ * Fetches a single market by ID from the Alpha REST API (requires API key).
492
+ *
493
+ * @param marketId - The market UUID
494
+ * @returns The market data, or null if not found
495
+ */
496
+ getMarketFromApi(marketId: string): Promise<Market | null>;
453
497
  }
454
498
 
499
+ /** The default Alpha Arcade mainnet market creator address */
500
+ declare const DEFAULT_MARKET_CREATOR_ADDRESS = "5P5Y6HTWUNG2E3VXBQDZN3ENZD3JPAIR5PKT3LOYJAPAUKOLFD6KANYTRY";
501
+ /**
502
+ * Fetches all live, tradeable markets directly from the Algorand blockchain.
503
+ *
504
+ * Discovers markets by looking up all applications created by the market creator
505
+ * address, then reading their global state. No API key required.
506
+ *
507
+ * @param config - Alpha client config
508
+ * @param options - Optional filters
509
+ * @returns Array of live markets
510
+ */
511
+ declare const getMarketsOnChain: (config: AlphaClientConfig, options?: {
512
+ activeOnly?: boolean;
513
+ }) => Promise<Market[]>;
514
+ /**
515
+ * Fetches a single market by its app ID directly from the Algorand blockchain.
516
+ *
517
+ * @param config - Alpha client config
518
+ * @param marketAppId - The market app ID (number or string)
519
+ * @returns The market data, or null if not found
520
+ */
521
+ declare const getMarketOnChain: (config: AlphaClientConfig, marketAppId: number | string) => Promise<Market | null>;
522
+ /**
523
+ * Fetches all live, tradeable markets from the Alpha REST API.
524
+ *
525
+ * Paginates automatically through all results. Requires an API key.
526
+ * Returns richer data than on-chain lookup (images, categories, volume, probabilities).
527
+ *
528
+ * @param config - Alpha client config
529
+ * @returns Array of live markets
530
+ */
531
+ declare const getMarketsFromApi: (config: AlphaClientConfig) => Promise<Market[]>;
532
+ /**
533
+ * Fetches a single market by its ID from the Alpha REST API.
534
+ * Requires an API key.
535
+ *
536
+ * @param config - Alpha client config
537
+ * @param marketId - The market ID
538
+ * @returns The market data, or null if not found
539
+ */
540
+ declare const getMarketFromApi: (config: AlphaClientConfig, marketId: string) => Promise<Market | null>;
541
+
455
542
  /**
456
543
  * Calculates the required fee amount in microunits.
457
544
  *
@@ -530,4 +617,4 @@ declare const getEscrowGlobalState: (indexerClient: algosdk.Indexer, escrowAppId
530
617
  */
531
618
  declare const checkAssetOptIn: (algodClient: algosdk.Algodv2, address: string, assetId: number) => Promise<boolean>;
532
619
 
533
- export { type AggregatedOrderbook, type AggregatedOrderbookEntry, type AggregatedOrderbookSide, AlphaClient, type AlphaClientConfig, type CancelOrderParams, type CancelOrderResult, type ClaimParams, type ClaimResult, type CounterpartyMatch, type CreateLimitOrderParams, type CreateMarketOrderParams, type CreateMarketOrderResult, type CreateOrderResult, type EscrowGlobalState, type Market, type MarketGlobalState, type MarketOption, type MergeSharesParams, type OpenOrder, type OrderSide, type Orderbook, type OrderbookEntry, type OrderbookSide, type Position, type ProposeMatchParams, type ProposeMatchResult, type SplitMergeResult, type SplitSharesParams, type WalletPosition, calculateFee, calculateFeeFromTotal, calculateMatchingOrders, checkAssetOptIn, decodeGlobalState, getEscrowGlobalState, getMarketGlobalState };
620
+ export { type AggregatedOrderbook, type AggregatedOrderbookEntry, type AggregatedOrderbookSide, AlphaClient, type AlphaClientConfig, type CancelOrderParams, type CancelOrderResult, type ClaimParams, type ClaimResult, type CounterpartyMatch, type CreateLimitOrderParams, type CreateMarketOrderParams, type CreateMarketOrderResult, type CreateOrderResult, DEFAULT_MARKET_CREATOR_ADDRESS, type EscrowGlobalState, type Market, type MarketGlobalState, type MarketOption, type MergeSharesParams, type OpenOrder, type OrderSide, type Orderbook, type OrderbookEntry, type OrderbookSide, type Position, type ProposeMatchParams, type ProposeMatchResult, type SplitMergeResult, type SplitSharesParams, type WalletPosition, calculateFee, calculateFeeFromTotal, calculateMatchingOrders, checkAssetOptIn, decodeGlobalState, getEscrowGlobalState, getMarketFromApi, getMarketGlobalState, getMarketOnChain, getMarketsFromApi, getMarketsOnChain };
package/dist/index.d.ts CHANGED
@@ -10,17 +10,20 @@ type AlphaClientConfig = {
10
10
  signer: TransactionSigner;
11
11
  /** The active Algorand address that will sign transactions */
12
12
  activeAddress: string;
13
- /** Matcher contract app ID (mainnet: 741347297) */
13
+ /** Matcher contract app ID (mainnet: 3078581851) */
14
14
  matcherAppId: number;
15
15
  /** USDC ASA ID on Algorand (mainnet: 31566704) */
16
16
  usdcAssetId: number;
17
17
  /** Base URL for the Alpha REST API (default: https://partners.alphaarcade.com/api) */
18
18
  apiBaseUrl?: string;
19
- /** API key for the Alpha partners API (passed as x-api-key header) */
20
- apiKey: string;
19
+ /** API key for the Alpha partners API. Optional -- if not provided, markets are loaded on-chain. */
20
+ apiKey?: string;
21
+ /** Market creator address for on-chain market discovery. Defaults to the Alpha Arcade mainnet creator. */
22
+ marketCreatorAddress?: string;
21
23
  };
22
- /** A prediction market returned from the Alpha API */
24
+ /** A prediction market (from the Alpha API or on-chain discovery) */
23
25
  type Market = {
26
+ /** Market ID (app ID as string for on-chain, UUID for API) */
24
27
  id: string;
25
28
  title: string;
26
29
  slug?: string;
@@ -28,9 +31,13 @@ type Market = {
28
31
  marketAppId: number;
29
32
  yesAssetId: number;
30
33
  noAssetId: number;
31
- yesProb: number;
32
- noProb: number;
33
- volume: number;
34
+ /** YES probability (API only -- not available from on-chain lookup) */
35
+ yesProb?: number;
36
+ /** NO probability (API only -- not available from on-chain lookup) */
37
+ noProb?: number;
38
+ /** Trading volume (API only -- not available from on-chain lookup) */
39
+ volume?: number;
40
+ /** End/resolution timestamp in seconds */
34
41
  endTs: number;
35
42
  resolution?: number;
36
43
  isResolved?: boolean;
@@ -39,6 +46,8 @@ type Market = {
39
46
  featured?: boolean;
40
47
  options?: MarketOption[];
41
48
  feeBase?: number;
49
+ /** Data source: 'onchain' or 'api' */
50
+ source?: 'onchain' | 'api';
42
51
  [key: string]: unknown;
43
52
  };
44
53
  /** An option within a multi-choice market */
@@ -316,7 +325,6 @@ type EscrowGlobalState = {
316
325
  * activeAddress: account.addr,
317
326
  * matcherAppId: 3078581851,
318
327
  * usdcAssetId: 31566704,
319
- * apiKey: 'YOUR_API_KEY',
320
328
  * });
321
329
  *
322
330
  * // Fetch markets
@@ -436,9 +444,10 @@ declare class AlphaClient {
436
444
  */
437
445
  getOpenOrders(marketAppId: number, walletAddress?: string): Promise<OpenOrder[]>;
438
446
  /**
439
- * Fetches all live, tradeable markets from the Alpha API.
447
+ * Fetches all live, tradeable markets.
440
448
  *
441
- * Automatically paginates to retrieve all markets.
449
+ * If an API key is configured, uses the Alpha REST API (richer data: images, categories, volume).
450
+ * Otherwise, discovers markets on-chain from the market creator address (no API key needed).
442
451
  *
443
452
  * @returns Array of live markets
444
453
  */
@@ -446,12 +455,90 @@ declare class AlphaClient {
446
455
  /**
447
456
  * Fetches a single market by its ID.
448
457
  *
449
- * @param marketId - The market ID
458
+ * If an API key is configured, uses the Alpha REST API.
459
+ * Otherwise, reads the market's on-chain global state (pass the market app ID as a string).
460
+ *
461
+ * @param marketId - The market ID (UUID for API, app ID string for on-chain)
450
462
  * @returns The market data, or null if not found
451
463
  */
452
464
  getMarket(marketId: string): Promise<Market | null>;
465
+ /**
466
+ * Fetches all live markets directly from the blockchain (no API key needed).
467
+ *
468
+ * Discovers markets by looking up all apps created by the market creator address.
469
+ * Returns core data: title, asset IDs, resolution time, fees.
470
+ * Does NOT include images, categories, volume, or probabilities.
471
+ *
472
+ * @returns Array of live markets from on-chain data
473
+ */
474
+ getMarketsOnChain(): Promise<Market[]>;
475
+ /**
476
+ * Fetches a single market by app ID directly from the blockchain (no API key needed).
477
+ *
478
+ * @param marketAppId - The market app ID
479
+ * @returns The market data, or null if not found
480
+ */
481
+ getMarketOnChain(marketAppId: number): Promise<Market | null>;
482
+ /**
483
+ * Fetches all live markets from the Alpha REST API (requires API key).
484
+ *
485
+ * Returns richer data than on-chain: images, categories, volume, probabilities.
486
+ *
487
+ * @returns Array of live markets from the API
488
+ */
489
+ getMarketsFromApi(): Promise<Market[]>;
490
+ /**
491
+ * Fetches a single market by ID from the Alpha REST API (requires API key).
492
+ *
493
+ * @param marketId - The market UUID
494
+ * @returns The market data, or null if not found
495
+ */
496
+ getMarketFromApi(marketId: string): Promise<Market | null>;
453
497
  }
454
498
 
499
+ /** The default Alpha Arcade mainnet market creator address */
500
+ declare const DEFAULT_MARKET_CREATOR_ADDRESS = "5P5Y6HTWUNG2E3VXBQDZN3ENZD3JPAIR5PKT3LOYJAPAUKOLFD6KANYTRY";
501
+ /**
502
+ * Fetches all live, tradeable markets directly from the Algorand blockchain.
503
+ *
504
+ * Discovers markets by looking up all applications created by the market creator
505
+ * address, then reading their global state. No API key required.
506
+ *
507
+ * @param config - Alpha client config
508
+ * @param options - Optional filters
509
+ * @returns Array of live markets
510
+ */
511
+ declare const getMarketsOnChain: (config: AlphaClientConfig, options?: {
512
+ activeOnly?: boolean;
513
+ }) => Promise<Market[]>;
514
+ /**
515
+ * Fetches a single market by its app ID directly from the Algorand blockchain.
516
+ *
517
+ * @param config - Alpha client config
518
+ * @param marketAppId - The market app ID (number or string)
519
+ * @returns The market data, or null if not found
520
+ */
521
+ declare const getMarketOnChain: (config: AlphaClientConfig, marketAppId: number | string) => Promise<Market | null>;
522
+ /**
523
+ * Fetches all live, tradeable markets from the Alpha REST API.
524
+ *
525
+ * Paginates automatically through all results. Requires an API key.
526
+ * Returns richer data than on-chain lookup (images, categories, volume, probabilities).
527
+ *
528
+ * @param config - Alpha client config
529
+ * @returns Array of live markets
530
+ */
531
+ declare const getMarketsFromApi: (config: AlphaClientConfig) => Promise<Market[]>;
532
+ /**
533
+ * Fetches a single market by its ID from the Alpha REST API.
534
+ * Requires an API key.
535
+ *
536
+ * @param config - Alpha client config
537
+ * @param marketId - The market ID
538
+ * @returns The market data, or null if not found
539
+ */
540
+ declare const getMarketFromApi: (config: AlphaClientConfig, marketId: string) => Promise<Market | null>;
541
+
455
542
  /**
456
543
  * Calculates the required fee amount in microunits.
457
544
  *
@@ -530,4 +617,4 @@ declare const getEscrowGlobalState: (indexerClient: algosdk.Indexer, escrowAppId
530
617
  */
531
618
  declare const checkAssetOptIn: (algodClient: algosdk.Algodv2, address: string, assetId: number) => Promise<boolean>;
532
619
 
533
- export { type AggregatedOrderbook, type AggregatedOrderbookEntry, type AggregatedOrderbookSide, AlphaClient, type AlphaClientConfig, type CancelOrderParams, type CancelOrderResult, type ClaimParams, type ClaimResult, type CounterpartyMatch, type CreateLimitOrderParams, type CreateMarketOrderParams, type CreateMarketOrderResult, type CreateOrderResult, type EscrowGlobalState, type Market, type MarketGlobalState, type MarketOption, type MergeSharesParams, type OpenOrder, type OrderSide, type Orderbook, type OrderbookEntry, type OrderbookSide, type Position, type ProposeMatchParams, type ProposeMatchResult, type SplitMergeResult, type SplitSharesParams, type WalletPosition, calculateFee, calculateFeeFromTotal, calculateMatchingOrders, checkAssetOptIn, decodeGlobalState, getEscrowGlobalState, getMarketGlobalState };
620
+ export { type AggregatedOrderbook, type AggregatedOrderbookEntry, type AggregatedOrderbookSide, AlphaClient, type AlphaClientConfig, type CancelOrderParams, type CancelOrderResult, type ClaimParams, type ClaimResult, type CounterpartyMatch, type CreateLimitOrderParams, type CreateMarketOrderParams, type CreateMarketOrderResult, type CreateOrderResult, DEFAULT_MARKET_CREATOR_ADDRESS, type EscrowGlobalState, type Market, type MarketGlobalState, type MarketOption, type MergeSharesParams, type OpenOrder, type OrderSide, type Orderbook, type OrderbookEntry, type OrderbookSide, type Position, type ProposeMatchParams, type ProposeMatchResult, type SplitMergeResult, type SplitSharesParams, type WalletPosition, calculateFee, calculateFeeFromTotal, calculateMatchingOrders, checkAssetOptIn, decodeGlobalState, getEscrowGlobalState, getMarketFromApi, getMarketGlobalState, getMarketOnChain, getMarketsFromApi, getMarketsOnChain };
package/dist/index.js CHANGED
@@ -1930,7 +1930,121 @@ var getPositions = async (config, walletAddress) => {
1930
1930
 
1931
1931
  // src/modules/markets.ts
1932
1932
  var DEFAULT_API_BASE_URL = "https://partners.alphaarcade.com/api";
1933
- var getMarkets = async (config) => {
1933
+ var DEFAULT_MARKET_CREATOR_ADDRESS = "5P5Y6HTWUNG2E3VXBQDZN3ENZD3JPAIR5PKT3LOYJAPAUKOLFD6KANYTRY";
1934
+ var groupMultiChoiceMarkets = (flatMarkets) => {
1935
+ const parentMap = /* @__PURE__ */ new Map();
1936
+ const result = [];
1937
+ for (const m of flatMarkets) {
1938
+ const separatorIdx = m.title.lastIndexOf(" : ");
1939
+ if (separatorIdx === -1) {
1940
+ result.push(m);
1941
+ continue;
1942
+ }
1943
+ const parentTitle = m.title.substring(0, separatorIdx).trim();
1944
+ const optionTitle = m.title.substring(separatorIdx + 3).trim();
1945
+ let parent = parentMap.get(parentTitle);
1946
+ if (!parent) {
1947
+ parent = {
1948
+ id: `group:${parentTitle}`,
1949
+ title: parentTitle,
1950
+ marketAppId: m.marketAppId,
1951
+ // Use first option's app ID as the parent's
1952
+ yesAssetId: 0,
1953
+ noAssetId: 0,
1954
+ endTs: m.endTs,
1955
+ isResolved: m.isResolved,
1956
+ isLive: m.isLive,
1957
+ feeBase: m.feeBase,
1958
+ source: "onchain",
1959
+ options: []
1960
+ };
1961
+ parentMap.set(parentTitle, parent);
1962
+ result.push(parent);
1963
+ }
1964
+ parent.options.push({
1965
+ id: m.id,
1966
+ title: optionTitle,
1967
+ marketAppId: m.marketAppId,
1968
+ yesAssetId: m.yesAssetId,
1969
+ noAssetId: m.noAssetId,
1970
+ yesProb: 0,
1971
+ noProb: 0
1972
+ });
1973
+ }
1974
+ return result;
1975
+ };
1976
+ var getMarketsOnChain = async (config, options) => {
1977
+ const creatorAddress = config.marketCreatorAddress ?? DEFAULT_MARKET_CREATOR_ADDRESS;
1978
+ const activeOnly = options?.activeOnly ?? true;
1979
+ const allApps = [];
1980
+ let nextToken;
1981
+ let hasMore = true;
1982
+ while (hasMore) {
1983
+ let query = config.indexerClient.lookupAccountCreatedApplications(creatorAddress).limit(100);
1984
+ if (nextToken) {
1985
+ query = query.nextToken(nextToken);
1986
+ }
1987
+ const response = await query.do();
1988
+ if (response.applications?.length) {
1989
+ allApps.push(...response.applications);
1990
+ }
1991
+ if (response["next-token"]) {
1992
+ nextToken = response["next-token"];
1993
+ } else {
1994
+ hasMore = false;
1995
+ }
1996
+ }
1997
+ const flatMarkets = [];
1998
+ for (const app of allApps) {
1999
+ if (app.deleted) continue;
2000
+ const rawState = app.params?.["global-state"];
2001
+ if (!rawState) continue;
2002
+ const state = decodeGlobalState(rawState);
2003
+ if (activeOnly && !state.is_activated) continue;
2004
+ if (activeOnly && state.is_resolved) continue;
2005
+ if (activeOnly && state.resolution_time && state.resolution_time < Math.floor(Date.now() / 1e3)) continue;
2006
+ const appId = Number(app.id);
2007
+ flatMarkets.push({
2008
+ id: String(appId),
2009
+ title: state.title || "",
2010
+ marketAppId: appId,
2011
+ yesAssetId: state.yes_asset_id || 0,
2012
+ noAssetId: state.no_asset_id || 0,
2013
+ endTs: state.resolution_time || 0,
2014
+ isResolved: !!state.is_resolved,
2015
+ isLive: !!state.is_activated && !state.is_resolved,
2016
+ feeBase: state.fee_base_percent,
2017
+ source: "onchain"
2018
+ });
2019
+ }
2020
+ return groupMultiChoiceMarkets(flatMarkets);
2021
+ };
2022
+ var getMarketOnChain = async (config, marketAppId) => {
2023
+ try {
2024
+ const appId = typeof marketAppId === "string" ? Number(marketAppId) : marketAppId;
2025
+ const appInfo = await config.algodClient.getApplicationByID(appId).do();
2026
+ const rawState = appInfo.params?.["global-state"] ?? appInfo["params"]?.["global-state"] ?? [];
2027
+ const state = decodeGlobalState(rawState);
2028
+ return {
2029
+ id: String(appId),
2030
+ title: state.title || "",
2031
+ marketAppId: appId,
2032
+ yesAssetId: state.yes_asset_id || 0,
2033
+ noAssetId: state.no_asset_id || 0,
2034
+ endTs: state.resolution_time || 0,
2035
+ isResolved: !!state.is_resolved,
2036
+ isLive: !!state.is_activated && !state.is_resolved,
2037
+ feeBase: state.fee_base_percent,
2038
+ source: "onchain"
2039
+ };
2040
+ } catch {
2041
+ return null;
2042
+ }
2043
+ };
2044
+ var getMarketsFromApi = async (config) => {
2045
+ if (!config.apiKey) {
2046
+ throw new Error("apiKey is required for API-based market fetching. Use getMarketsOnChain() instead, or pass an apiKey.");
2047
+ }
1934
2048
  const baseUrl = config.apiBaseUrl ?? DEFAULT_API_BASE_URL;
1935
2049
  const allMarkets = [];
1936
2050
  let lastEvaluatedKey;
@@ -1947,10 +2061,10 @@ var getMarkets = async (config) => {
1947
2061
  }
1948
2062
  const data = await response.json();
1949
2063
  if (Array.isArray(data)) {
1950
- allMarkets.push(...data);
2064
+ allMarkets.push(...data.map((m) => ({ ...m, source: "api" })));
1951
2065
  hasMore = false;
1952
2066
  } else if (data.markets) {
1953
- allMarkets.push(...data.markets);
2067
+ allMarkets.push(...data.markets.map((m) => ({ ...m, source: "api" })));
1954
2068
  lastEvaluatedKey = data.lastEvaluatedKey;
1955
2069
  hasMore = !!lastEvaluatedKey;
1956
2070
  } else {
@@ -1959,7 +2073,10 @@ var getMarkets = async (config) => {
1959
2073
  }
1960
2074
  return allMarkets;
1961
2075
  };
1962
- var getMarket = async (config, marketId) => {
2076
+ var getMarketFromApi = async (config, marketId) => {
2077
+ if (!config.apiKey) {
2078
+ throw new Error("apiKey is required for API-based market fetching. Use getMarketOnChain() instead, or pass an apiKey.");
2079
+ }
1963
2080
  const baseUrl = config.apiBaseUrl ?? DEFAULT_API_BASE_URL;
1964
2081
  const url = `${baseUrl}/get-market?marketId=${encodeURIComponent(marketId)}`;
1965
2082
  const response = await fetch(url, { headers: { "x-api-key": config.apiKey } });
@@ -1968,7 +2085,21 @@ var getMarket = async (config, marketId) => {
1968
2085
  throw new Error(`Alpha API error: ${response.status} ${response.statusText}`);
1969
2086
  }
1970
2087
  const data = await response.json();
1971
- return data.market ?? data ?? null;
2088
+ const market = data.market ?? data ?? null;
2089
+ if (market) market.source = "api";
2090
+ return market;
2091
+ };
2092
+ var getMarkets = async (config) => {
2093
+ if (config.apiKey) {
2094
+ return getMarketsFromApi(config);
2095
+ }
2096
+ return getMarketsOnChain(config);
2097
+ };
2098
+ var getMarket = async (config, marketId) => {
2099
+ if (config.apiKey) {
2100
+ return getMarketFromApi(config, marketId);
2101
+ }
2102
+ return getMarketOnChain(config, marketId);
1972
2103
  };
1973
2104
 
1974
2105
  // src/client.ts
@@ -1981,7 +2112,6 @@ var AlphaClient = class {
1981
2112
  if (!config.activeAddress) throw new Error("activeAddress is required");
1982
2113
  if (!config.matcherAppId) throw new Error("matcherAppId is required");
1983
2114
  if (!config.usdcAssetId) throw new Error("usdcAssetId is required");
1984
- if (!config.apiKey) throw new Error("apiKey is required");
1985
2115
  this.config = {
1986
2116
  ...config,
1987
2117
  apiBaseUrl: config.apiBaseUrl ?? "https://partners.alphaarcade.com/api"
@@ -2120,9 +2250,10 @@ var AlphaClient = class {
2120
2250
  // Markets
2121
2251
  // ============================================
2122
2252
  /**
2123
- * Fetches all live, tradeable markets from the Alpha API.
2253
+ * Fetches all live, tradeable markets.
2124
2254
  *
2125
- * Automatically paginates to retrieve all markets.
2255
+ * If an API key is configured, uses the Alpha REST API (richer data: images, categories, volume).
2256
+ * Otherwise, discovers markets on-chain from the market creator address (no API key needed).
2126
2257
  *
2127
2258
  * @returns Array of live markets
2128
2259
  */
@@ -2132,14 +2263,57 @@ var AlphaClient = class {
2132
2263
  /**
2133
2264
  * Fetches a single market by its ID.
2134
2265
  *
2135
- * @param marketId - The market ID
2266
+ * If an API key is configured, uses the Alpha REST API.
2267
+ * Otherwise, reads the market's on-chain global state (pass the market app ID as a string).
2268
+ *
2269
+ * @param marketId - The market ID (UUID for API, app ID string for on-chain)
2136
2270
  * @returns The market data, or null if not found
2137
2271
  */
2138
2272
  async getMarket(marketId) {
2139
2273
  return getMarket(this.config, marketId);
2140
2274
  }
2275
+ /**
2276
+ * Fetches all live markets directly from the blockchain (no API key needed).
2277
+ *
2278
+ * Discovers markets by looking up all apps created by the market creator address.
2279
+ * Returns core data: title, asset IDs, resolution time, fees.
2280
+ * Does NOT include images, categories, volume, or probabilities.
2281
+ *
2282
+ * @returns Array of live markets from on-chain data
2283
+ */
2284
+ async getMarketsOnChain() {
2285
+ return getMarketsOnChain(this.config);
2286
+ }
2287
+ /**
2288
+ * Fetches a single market by app ID directly from the blockchain (no API key needed).
2289
+ *
2290
+ * @param marketAppId - The market app ID
2291
+ * @returns The market data, or null if not found
2292
+ */
2293
+ async getMarketOnChain(marketAppId) {
2294
+ return getMarketOnChain(this.config, marketAppId);
2295
+ }
2296
+ /**
2297
+ * Fetches all live markets from the Alpha REST API (requires API key).
2298
+ *
2299
+ * Returns richer data than on-chain: images, categories, volume, probabilities.
2300
+ *
2301
+ * @returns Array of live markets from the API
2302
+ */
2303
+ async getMarketsFromApi() {
2304
+ return getMarketsFromApi(this.config);
2305
+ }
2306
+ /**
2307
+ * Fetches a single market by ID from the Alpha REST API (requires API key).
2308
+ *
2309
+ * @param marketId - The market UUID
2310
+ * @returns The market data, or null if not found
2311
+ */
2312
+ async getMarketFromApi(marketId) {
2313
+ return getMarketFromApi(this.config, marketId);
2314
+ }
2141
2315
  };
2142
2316
 
2143
- export { AlphaClient, calculateFee, calculateFeeFromTotal, calculateMatchingOrders, checkAssetOptIn, decodeGlobalState, getEscrowGlobalState, getMarketGlobalState };
2317
+ export { AlphaClient, DEFAULT_MARKET_CREATOR_ADDRESS, calculateFee, calculateFeeFromTotal, calculateMatchingOrders, checkAssetOptIn, decodeGlobalState, getEscrowGlobalState, getMarketFromApi, getMarketGlobalState, getMarketOnChain, getMarketsFromApi, getMarketsOnChain };
2144
2318
  //# sourceMappingURL=index.js.map
2145
2319
  //# sourceMappingURL=index.js.map