@alpha-arcade/sdk 0.2.11 → 0.3.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
@@ -1,4 +1,5 @@
1
- import algosdk, { Algodv2, Indexer, TransactionSigner } from 'algosdk';
1
+ import * as algosdk from 'algosdk';
2
+ import { Algodv2, Indexer, TransactionSigner } from 'algosdk';
2
3
 
3
4
  /** Configuration for initializing the AlphaClient */
4
5
  type AlphaClientConfig = {
@@ -360,7 +361,7 @@ type EscrowGlobalState = {
360
361
  * algodClient,
361
362
  * indexerClient,
362
363
  * signer,
363
- * activeAddress: account.addr,
364
+ * activeAddress: account.addr.toString(),
364
365
  * matcherAppId: 3078581851,
365
366
  * usdcAssetId: 31566704,
366
367
  * });
@@ -651,6 +652,7 @@ declare const calculateMatchingOrders: (orderbook: Orderbook, isBuying: boolean,
651
652
 
652
653
  /**
653
654
  * Decodes raw Algorand global state array into a key-value object.
655
+ * Supports both v2 (kebab-case) and v3 (camelCase) response shapes.
654
656
  *
655
657
  * @param rawState - The raw global-state array from algod/indexer
656
658
  * @returns Decoded key-value object
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
- import algosdk, { Algodv2, Indexer, TransactionSigner } from 'algosdk';
1
+ import * as algosdk from 'algosdk';
2
+ import { Algodv2, Indexer, TransactionSigner } from 'algosdk';
2
3
 
3
4
  /** Configuration for initializing the AlphaClient */
4
5
  type AlphaClientConfig = {
@@ -360,7 +361,7 @@ type EscrowGlobalState = {
360
361
  * algodClient,
361
362
  * indexerClient,
362
363
  * signer,
363
- * activeAddress: account.addr,
364
+ * activeAddress: account.addr.toString(),
364
365
  * matcherAppId: 3078581851,
365
366
  * usdcAssetId: 31566704,
366
367
  * });
@@ -651,6 +652,7 @@ declare const calculateMatchingOrders: (orderbook: Orderbook, isBuying: boolean,
651
652
 
652
653
  /**
653
654
  * Decodes raw Algorand global state array into a key-value object.
655
+ * Supports both v2 (kebab-case) and v3 (camelCase) response shapes.
654
656
  *
655
657
  * @param rawState - The raw global-state array from algod/indexer
656
658
  * @returns Decoded key-value object
package/dist/index.js CHANGED
@@ -1,4 +1,5 @@
1
- import algosdk2, { AtomicTransactionComposer, getApplicationAddress, modelsv2 } from 'algosdk';
1
+ import * as algosdk2 from 'algosdk';
2
+ import { AtomicTransactionComposer, getApplicationAddress, modelsv2 } from 'algosdk';
2
3
  import * as algokit4 from '@algorandfoundation/algokit-utils';
3
4
  import { Decimal } from 'decimal.js';
4
5
 
@@ -1973,47 +1974,58 @@ var calculateFeeFromTotal = (totalAmount, price, feeBase) => {
1973
1974
  var decodeGlobalState = (rawState) => {
1974
1975
  const state = {};
1975
1976
  for (const item of rawState) {
1976
- const key = Buffer.from(item.key, "base64").toString();
1977
- if (item.value.type === 1) {
1977
+ const rawKey = item.key;
1978
+ const key = typeof rawKey === "string" ? Buffer.from(rawKey, "base64").toString() : rawKey instanceof Uint8Array ? Buffer.from(rawKey).toString() : String(rawKey);
1979
+ const val = item.value;
1980
+ const type = val.type;
1981
+ if (type === 1) {
1978
1982
  if (key === "owner" || key === "oracle_address" || key === "fee_address" || key === "market_friend_addr" || key === "escrow_cancel_address") {
1979
1983
  try {
1980
- const addressBytes = Buffer.from(item.value.bytes, "base64");
1984
+ const rawBytes = val.bytes;
1985
+ const addressBytes = typeof rawBytes === "string" ? Buffer.from(rawBytes, "base64") : rawBytes instanceof Uint8Array ? rawBytes : Buffer.from(String(rawBytes), "base64");
1981
1986
  if (addressBytes.length === 32) {
1982
- state[key] = algosdk2.encodeAddress(addressBytes);
1987
+ state[key] = algosdk2.encodeAddress(new Uint8Array(addressBytes));
1983
1988
  } else {
1984
- state[key] = item.value.bytes;
1989
+ state[key] = val.bytes;
1985
1990
  }
1986
1991
  } catch {
1987
- state[key] = item.value.bytes;
1992
+ state[key] = val.bytes;
1988
1993
  }
1989
1994
  } else {
1990
1995
  try {
1991
- state[key] = Buffer.from(item.value.bytes, "base64").toString();
1996
+ const rawBytes = val.bytes;
1997
+ if (typeof rawBytes === "string") {
1998
+ state[key] = Buffer.from(rawBytes, "base64").toString();
1999
+ } else if (rawBytes instanceof Uint8Array) {
2000
+ state[key] = Buffer.from(rawBytes).toString();
2001
+ } else {
2002
+ state[key] = rawBytes;
2003
+ }
1992
2004
  } catch {
1993
- state[key] = item.value.bytes;
2005
+ state[key] = val.bytes;
1994
2006
  }
1995
2007
  }
1996
2008
  } else {
1997
- state[key] = Number(item.value.uint);
2009
+ state[key] = Number(val.uint);
1998
2010
  }
1999
2011
  }
2000
2012
  return state;
2001
2013
  };
2002
2014
  var getMarketGlobalState = async (algodClient, marketAppId) => {
2003
2015
  const appInfo = await algodClient.getApplicationByID(marketAppId).do();
2004
- const rawState = appInfo.params?.["global-state"] ?? appInfo["params"]?.["global-state"] ?? [];
2016
+ const rawState = appInfo.params?.globalState ?? appInfo.params?.["global-state"] ?? [];
2005
2017
  return decodeGlobalState(rawState);
2006
2018
  };
2007
2019
  var getEscrowGlobalState = async (indexerClient, escrowAppId) => {
2008
2020
  const appInfo = await indexerClient.lookupApplications(escrowAppId).do();
2009
- const rawState = appInfo.application?.params?.["global-state"] ?? [];
2021
+ const rawState = appInfo.application?.params?.globalState ?? appInfo.application?.params?.["global-state"] ?? [];
2010
2022
  return decodeGlobalState(rawState);
2011
2023
  };
2012
2024
  var checkAssetOptIn = async (algodClient, address, assetId) => {
2013
2025
  try {
2014
2026
  const accountInfo = await algodClient.accountInformation(address).do();
2015
- const assets = accountInfo.assets || accountInfo["assets"] || [];
2016
- return assets.some((a) => (a["asset-id"] ?? a.assetId) === assetId);
2027
+ const assets = accountInfo.assets || [];
2028
+ return assets.some((a) => Number(a.assetId ?? a["asset-id"]) === assetId);
2017
2029
  } catch {
2018
2030
  return false;
2019
2031
  }
@@ -2107,8 +2119,9 @@ var getAllCreatedApplications = async (indexerClient, address, limit = 100) => {
2107
2119
  if (response.applications?.length) {
2108
2120
  applications = [...applications, ...response.applications];
2109
2121
  }
2110
- if (response["next-token"]) {
2111
- nextToken = response["next-token"];
2122
+ const token = response.nextToken ?? response["next-token"];
2123
+ if (token) {
2124
+ nextToken = token;
2112
2125
  } else {
2113
2126
  hasMore = false;
2114
2127
  }
@@ -2122,7 +2135,7 @@ var fetchApplicationsGlobalState = async (indexerClient, applications) => {
2122
2135
  try {
2123
2136
  const appInfo = await indexerClient.lookupApplications(appId).do();
2124
2137
  const globalState = {};
2125
- const rawGlobalState = appInfo.application?.params?.["global-state"];
2138
+ const rawGlobalState = appInfo.application?.params?.globalState ?? appInfo.application?.params?.["global-state"];
2126
2139
  if (rawGlobalState) {
2127
2140
  const decoded = decodeGlobalState(rawGlobalState);
2128
2141
  Object.assign(globalState, decoded);
@@ -2141,7 +2154,7 @@ var transformOrders = (orders) => orders.filter((o) => o.appId > 0).map((o) => (
2141
2154
  owner: o.globalState.owner ?? ""
2142
2155
  }));
2143
2156
  var getOrderbook = async (config, marketAppId) => {
2144
- const appAddress = algosdk2.getApplicationAddress(marketAppId);
2157
+ const appAddress = algosdk2.getApplicationAddress(marketAppId).toString();
2145
2158
  const applications = await getAllCreatedApplications(config.indexerClient, appAddress);
2146
2159
  const appsWithState = await fetchApplicationsGlobalState(config.indexerClient, applications);
2147
2160
  const isOpenLimitOrder = (o) => (o.globalState.quantity ?? 0) > (o.globalState.quantity_filled ?? 0) && o.globalState.slippage === 0;
@@ -2170,7 +2183,7 @@ var getOrderbook = async (config, marketAppId) => {
2170
2183
  };
2171
2184
  var getOpenOrders = async (config, marketAppId, walletAddress) => {
2172
2185
  const owner = walletAddress ?? config.activeAddress;
2173
- const appAddress = algosdk2.getApplicationAddress(marketAppId);
2186
+ const appAddress = algosdk2.getApplicationAddress(marketAppId).toString();
2174
2187
  const applications = await getAllCreatedApplications(config.indexerClient, appAddress);
2175
2188
  const appsWithState = await fetchApplicationsGlobalState(config.indexerClient, applications);
2176
2189
  return appsWithState.filter(
@@ -2235,8 +2248,10 @@ var getWalletOrdersFromApi = async (config, walletAddress) => {
2235
2248
  var extractEscrowAppId = async (algodClient, indexerClient, targetTxId) => {
2236
2249
  try {
2237
2250
  const pendingInfo = await algodClient.pendingTransactionInformation(targetTxId).do();
2238
- if (pendingInfo?.["inner-txns"]?.[0]?.["created-application-index"]) {
2239
- return pendingInfo["inner-txns"][0]["created-application-index"];
2251
+ const innerTxns = pendingInfo.innerTxns ?? pendingInfo["inner-txns"];
2252
+ const createdAppId = innerTxns?.[0]?.applicationIndex ?? innerTxns?.[0]?.["created-application-index"];
2253
+ if (createdAppId) {
2254
+ return Number(createdAppId);
2240
2255
  }
2241
2256
  } catch {
2242
2257
  }
@@ -2245,8 +2260,11 @@ var extractEscrowAppId = async (algodClient, indexerClient, targetTxId) => {
2245
2260
  try {
2246
2261
  await new Promise((resolve) => setTimeout(resolve, delayMs));
2247
2262
  const txnLookup = await indexerClient.lookupTransactionByID(targetTxId).do();
2248
- if (txnLookup?.transaction?.["inner-txns"]?.[0]?.["created-application-index"]) {
2249
- return txnLookup.transaction["inner-txns"][0]["created-application-index"];
2263
+ const txn = txnLookup.transaction;
2264
+ const innerTxns = txn?.innerTxns ?? txn?.["inner-txns"];
2265
+ const createdAppId = innerTxns?.[0]?.createdApplicationIndex ?? innerTxns?.[0]?.["created-application-index"];
2266
+ if (createdAppId) {
2267
+ return Number(createdAppId);
2250
2268
  }
2251
2269
  } catch {
2252
2270
  }
@@ -2314,7 +2332,7 @@ var createOrder = async (config, params) => {
2314
2332
  if (isBuying) {
2315
2333
  fee = calculateFee(quantity, price + slippage, feeBase);
2316
2334
  }
2317
- const marketAddress = getApplicationAddress(marketAppId);
2335
+ const marketAddress = getApplicationAddress(marketAppId).toString();
2318
2336
  const atc = new AtomicTransactionComposer();
2319
2337
  let createEscrowTxnIndex = 0;
2320
2338
  if (!isBuying) {
@@ -2363,7 +2381,7 @@ var createOrder = async (config, params) => {
2363
2381
  const payCounterPartyTxn = await algokit4.transferAlgos(
2364
2382
  {
2365
2383
  from: signerAccount,
2366
- to: getApplicationAddress(matchingOrder.escrowAppId),
2384
+ to: getApplicationAddress(matchingOrder.escrowAppId).toString(),
2367
2385
  amount: algokit4.microAlgos(1e3 * (isBuying ? 1 : 2)),
2368
2386
  skipSending: true
2369
2387
  },
@@ -2395,7 +2413,7 @@ var createOrder = async (config, params) => {
2395
2413
  return {
2396
2414
  escrowAppId,
2397
2415
  txIds: result.txIDs,
2398
- confirmedRound: result.confirmedRound
2416
+ confirmedRound: Number(result.confirmedRound)
2399
2417
  };
2400
2418
  };
2401
2419
  var cancelOrder = async (config, params) => {
@@ -2424,7 +2442,7 @@ var cancelOrder = async (config, params) => {
2424
2442
  return {
2425
2443
  success: true,
2426
2444
  txIds: result.txIDs,
2427
- confirmedRound: result.confirmedRound
2445
+ confirmedRound: Number(result.confirmedRound)
2428
2446
  };
2429
2447
  };
2430
2448
  var proposeMatch = async (config, params) => {
@@ -2443,7 +2461,7 @@ var proposeMatch = async (config, params) => {
2443
2461
  const payMakerTxn = await algokit4.transferAlgos(
2444
2462
  {
2445
2463
  from: signerAccount,
2446
- to: getApplicationAddress(makerEscrowAppId),
2464
+ to: getApplicationAddress(makerEscrowAppId).toString(),
2447
2465
  amount: algokit4.microAlgos(2e3),
2448
2466
  skipSending: true
2449
2467
  },
@@ -2471,7 +2489,7 @@ var proposeMatch = async (config, params) => {
2471
2489
  return {
2472
2490
  success: true,
2473
2491
  txIds: result.txIDs,
2474
- confirmedRound: result.confirmedRound
2492
+ confirmedRound: Number(result.confirmedRound)
2475
2493
  };
2476
2494
  };
2477
2495
  var amendOrder = async (config, params) => {
@@ -2479,7 +2497,7 @@ var amendOrder = async (config, params) => {
2479
2497
  const { marketAppId, escrowAppId, price, quantity, slippage = 0 } = params;
2480
2498
  const escrowAppInfo = await algodClient.getApplicationByID(escrowAppId).do();
2481
2499
  const escrowState = decodeGlobalState(
2482
- escrowAppInfo.params?.["global-state"] ?? escrowAppInfo["params"]?.["global-state"] ?? []
2500
+ escrowAppInfo.params?.globalState ?? escrowAppInfo.params?.["global-state"] ?? []
2483
2501
  );
2484
2502
  if ((escrowState.quantity_filled ?? 0) > 0) {
2485
2503
  throw new Error("Cannot amend an order that has been partially or fully filled.");
@@ -2533,7 +2551,7 @@ var amendOrder = async (config, params) => {
2533
2551
  return {
2534
2552
  success: true,
2535
2553
  txIds: result.txIDs,
2536
- confirmedRound: result.confirmedRound
2554
+ confirmedRound: Number(result.confirmedRound)
2537
2555
  };
2538
2556
  };
2539
2557
  var splitShares = async (config, params) => {
@@ -2547,7 +2565,7 @@ var splitShares = async (config, params) => {
2547
2565
  { resolveBy: "id", id: marketAppId, sender: signerAccount },
2548
2566
  algodClient
2549
2567
  );
2550
- const marketAddress = getApplicationAddress(marketAppId);
2568
+ const marketAddress = getApplicationAddress(marketAppId).toString();
2551
2569
  const atc = new AtomicTransactionComposer();
2552
2570
  let optInCosts = 0;
2553
2571
  const hasYesOptIn = await checkAssetOptIn(algodClient, activeAddress, yesAssetId);
@@ -2587,7 +2605,7 @@ var splitShares = async (config, params) => {
2587
2605
  return {
2588
2606
  success: true,
2589
2607
  txIds: result.txIDs,
2590
- confirmedRound: result.confirmedRound
2608
+ confirmedRound: Number(result.confirmedRound)
2591
2609
  };
2592
2610
  };
2593
2611
  var mergeShares = async (config, params) => {
@@ -2601,7 +2619,7 @@ var mergeShares = async (config, params) => {
2601
2619
  { resolveBy: "id", id: marketAppId, sender: signerAccount },
2602
2620
  algodClient
2603
2621
  );
2604
- const marketAddress = getApplicationAddress(marketAppId);
2622
+ const marketAddress = getApplicationAddress(marketAppId).toString();
2605
2623
  const atc = new AtomicTransactionComposer();
2606
2624
  let optInCosts = 0;
2607
2625
  const hasUsdcOptIn = await checkAssetOptIn(algodClient, activeAddress, usdcAssetId);
@@ -2637,7 +2655,7 @@ var mergeShares = async (config, params) => {
2637
2655
  return {
2638
2656
  success: true,
2639
2657
  txIds: result.txIDs,
2640
- confirmedRound: result.confirmedRound
2658
+ confirmedRound: Number(result.confirmedRound)
2641
2659
  };
2642
2660
  };
2643
2661
  var claim = async (config, params) => {
@@ -2648,12 +2666,12 @@ var claim = async (config, params) => {
2648
2666
  { resolveBy: "id", id: marketAppId, sender: signerAccount },
2649
2667
  algodClient
2650
2668
  );
2651
- const marketAddress = getApplicationAddress(marketAppId);
2669
+ const marketAddress = getApplicationAddress(marketAppId).toString();
2652
2670
  let tokenBalance = params.amount;
2653
2671
  if (!tokenBalance) {
2654
2672
  const accountInfo = await algodClient.accountInformation(activeAddress).do();
2655
- const assets = accountInfo.assets || accountInfo["assets"] || [];
2656
- const asset = assets.find((a) => (a["asset-id"] ?? a.assetId) === assetId);
2673
+ const assets = accountInfo.assets || [];
2674
+ const asset = assets.find((a) => Number(a.assetId ?? a["asset-id"]) === assetId);
2657
2675
  tokenBalance = asset ? Number(asset.amount) : 0;
2658
2676
  }
2659
2677
  if (tokenBalance <= 0) {
@@ -2675,8 +2693,8 @@ var claim = async (config, params) => {
2675
2693
  atc.addTransaction({ txn: claimTxn.transaction, signer });
2676
2694
  const sp = await algodClient.getTransactionParams().do();
2677
2695
  const closeOutTxn = algosdk2.makeAssetTransferTxnWithSuggestedParamsFromObject({
2678
- from: activeAddress,
2679
- to: marketAddress,
2696
+ sender: activeAddress,
2697
+ receiver: marketAddress,
2680
2698
  amount: 0,
2681
2699
  assetIndex: assetId,
2682
2700
  closeRemainderTo: marketAddress,
@@ -2687,7 +2705,7 @@ var claim = async (config, params) => {
2687
2705
  return {
2688
2706
  success: true,
2689
2707
  txIds: result.txIDs,
2690
- confirmedRound: result.confirmedRound,
2708
+ confirmedRound: Number(result.confirmedRound),
2691
2709
  amountClaimed: tokenBalance
2692
2710
  };
2693
2711
  };
@@ -2695,17 +2713,17 @@ var getPositions = async (config, walletAddress) => {
2695
2713
  const { algodClient, indexerClient } = config;
2696
2714
  const address = walletAddress ?? config.activeAddress;
2697
2715
  const accountInfo = await algodClient.accountInformation(address).do();
2698
- const assets = accountInfo.assets || accountInfo["assets"] || [];
2716
+ const assets = accountInfo.assets || [];
2699
2717
  const nonZeroAssets = assets.filter((a) => Number(a.amount) > 0);
2700
2718
  if (nonZeroAssets.length === 0) return [];
2701
2719
  const positions = /* @__PURE__ */ new Map();
2702
2720
  for (const asset of nonZeroAssets) {
2703
- const assetId = asset["asset-id"] ?? asset.assetId;
2721
+ const assetId = Number(asset.assetId ?? asset["asset-id"]);
2704
2722
  const amount = Number(asset.amount);
2705
2723
  try {
2706
2724
  const assetInfo = await indexerClient.lookupAssetByID(assetId).do();
2707
2725
  const assetName = assetInfo.asset?.params?.name ?? "";
2708
- const unitName = assetInfo.asset?.params?.["unit-name"] ?? "";
2726
+ const unitName = assetInfo.asset?.params?.unitName ?? assetInfo.asset?.params?.["unit-name"] ?? "";
2709
2727
  if (!unitName.startsWith("ALPHA-")) continue;
2710
2728
  const match = assetName.match(/^Alpha Market (\d+) (Yes|No)$/);
2711
2729
  if (!match) continue;
@@ -2721,16 +2739,24 @@ var getPositions = async (config, walletAddress) => {
2721
2739
  } else {
2722
2740
  try {
2723
2741
  const appInfo = await indexerClient.lookupApplications(marketAppId).do();
2724
- const rawState = appInfo.application?.params?.["global-state"];
2742
+ const rawState = appInfo.application?.params?.globalState ?? appInfo.application?.params?.["global-state"];
2725
2743
  if (!rawState) continue;
2726
2744
  let yesAssetIdOnChain = 0;
2727
2745
  let noAssetIdOnChain = 0;
2728
2746
  let marketTitle = "";
2729
2747
  for (const item of rawState) {
2730
- const key = Buffer.from(item.key, "base64").toString();
2748
+ const rawKey = item.key;
2749
+ const key = typeof rawKey === "string" ? Buffer.from(rawKey, "base64").toString() : rawKey instanceof Uint8Array ? Buffer.from(rawKey).toString() : String(rawKey);
2731
2750
  if (key === "yes_asset_id") yesAssetIdOnChain = Number(item.value.uint);
2732
2751
  if (key === "no_asset_id") noAssetIdOnChain = Number(item.value.uint);
2733
- if (key === "title") marketTitle = Buffer.from(item.value.bytes, "base64").toString();
2752
+ if (key === "title") {
2753
+ const rawBytes = item.value.bytes;
2754
+ if (typeof rawBytes === "string") {
2755
+ marketTitle = Buffer.from(rawBytes, "base64").toString();
2756
+ } else if (rawBytes instanceof Uint8Array) {
2757
+ marketTitle = Buffer.from(rawBytes).toString();
2758
+ }
2759
+ }
2734
2760
  }
2735
2761
  if (yesAssetIdOnChain === 0 && noAssetIdOnChain === 0) continue;
2736
2762
  positions.set(marketAppId, {
@@ -2811,8 +2837,9 @@ var getMarketsOnChain = async (config, options) => {
2811
2837
  if (response.applications?.length) {
2812
2838
  allApps.push(...response.applications);
2813
2839
  }
2814
- if (response["next-token"]) {
2815
- nextToken = response["next-token"];
2840
+ const token = response.nextToken ?? response["next-token"];
2841
+ if (token) {
2842
+ nextToken = token;
2816
2843
  } else {
2817
2844
  hasMore = false;
2818
2845
  }
@@ -2820,7 +2847,7 @@ var getMarketsOnChain = async (config, options) => {
2820
2847
  const flatMarkets = [];
2821
2848
  for (const app of allApps) {
2822
2849
  if (app.deleted) continue;
2823
- const rawState = app.params?.["global-state"];
2850
+ const rawState = app.params?.globalState ?? app.params?.["global-state"];
2824
2851
  if (!rawState) continue;
2825
2852
  const state = decodeGlobalState(rawState);
2826
2853
  if (activeOnly && !state.is_activated) continue;
@@ -2846,7 +2873,7 @@ var getMarketOnChain = async (config, marketAppId) => {
2846
2873
  try {
2847
2874
  const appId = typeof marketAppId === "string" ? Number(marketAppId) : marketAppId;
2848
2875
  const appInfo = await config.algodClient.getApplicationByID(appId).do();
2849
- const rawState = appInfo.params?.["global-state"] ?? appInfo["params"]?.["global-state"] ?? [];
2876
+ const rawState = appInfo.params?.globalState ?? appInfo.params?.["global-state"] ?? [];
2850
2877
  const state = decodeGlobalState(rawState);
2851
2878
  return {
2852
2879
  id: String(appId),