@provable-games/budokan-sdk 0.1.19 → 0.1.20

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/react.cjs CHANGED
@@ -3,6 +3,7 @@
3
3
  var react = require('react');
4
4
  var starknet = require('starknet');
5
5
  var jsxRuntime = require('react/jsx-runtime');
6
+ var react$1 = require('@provable-games/denshokan-sdk/react');
6
7
 
7
8
  // src/react/context.tsx
8
9
 
@@ -243,7 +244,6 @@ async function getTournament(baseUrl, tournamentId, ctx) {
243
244
  }
244
245
  async function getTournamentRegistrations(baseUrl, tournamentId, params, ctx) {
245
246
  const qs = buildQueryString({
246
- player_address: params?.playerAddress,
247
247
  game_token_ids: params?.gameTokenIds?.length ? params.gameTokenIds.join(",") : void 0,
248
248
  has_submitted: params?.hasSubmitted,
249
249
  is_banned: params?.isBanned,
@@ -316,42 +316,8 @@ function normalizeAddress(address) {
316
316
  return ("0x" + stripped.padStart(64, "0")).toLowerCase();
317
317
  }
318
318
 
319
- // src/api/players.ts
320
- function fetchOpts2(ctx) {
321
- return {
322
- retryAttempts: ctx?.retryAttempts,
323
- retryDelay: ctx?.retryDelay,
324
- timeout: ctx?.timeout
325
- };
326
- }
327
- async function getPlayerTournaments(baseUrl, address, params, ctx) {
328
- const normalized = normalizeAddress(address);
329
- const qs = buildQueryString({
330
- limit: params?.limit,
331
- offset: params?.offset,
332
- phase: params?.phase,
333
- game_token_ids: params?.gameTokenIds?.join(",")
334
- });
335
- const result = await apiFetch(`${baseUrl}/players/${normalized}/tournaments${qs}`, fetchOpts2(ctx));
336
- const { total, limit: resLimit, offset: resOffset } = extractPagination(result, { limit: params?.limit, offset: params?.offset });
337
- return {
338
- data: result.data.map((item) => snakeToCamel(item)),
339
- total,
340
- limit: resLimit,
341
- offset: resOffset
342
- };
343
- }
344
- async function getPlayerStats(baseUrl, address, ctx) {
345
- const normalized = normalizeAddress(address);
346
- const result = await apiFetch(
347
- `${baseUrl}/players/${normalized}/stats`,
348
- fetchOpts2(ctx)
349
- );
350
- return snakeToCamel(result.data);
351
- }
352
-
353
319
  // src/api/games.ts
354
- function fetchOpts3(ctx) {
320
+ function fetchOpts2(ctx) {
355
321
  return {
356
322
  retryAttempts: ctx?.retryAttempts,
357
323
  retryDelay: ctx?.retryDelay,
@@ -366,7 +332,7 @@ async function getGameTournaments(baseUrl, gameAddress, params, ctx) {
366
332
  limit: params?.limit,
367
333
  offset: params?.offset
368
334
  });
369
- const result = await apiFetch(`${baseUrl}/games/${normalized}/tournaments${qs}`, fetchOpts3(ctx));
335
+ const result = await apiFetch(`${baseUrl}/games/${normalized}/tournaments${qs}`, fetchOpts2(ctx));
370
336
  const { total, limit: resLimit, offset: resOffset } = extractPagination(result, { limit: params?.limit, offset: params?.offset });
371
337
  return {
372
338
  data: result.data.map((item) => snakeToCamel(item)),
@@ -379,13 +345,13 @@ async function getGameStats(baseUrl, gameAddress, ctx) {
379
345
  const normalized = normalizeAddress(gameAddress);
380
346
  const result = await apiFetch(
381
347
  `${baseUrl}/games/${normalized}/stats`,
382
- fetchOpts3(ctx)
348
+ fetchOpts2(ctx)
383
349
  );
384
350
  return snakeToCamel(result.data);
385
351
  }
386
352
 
387
353
  // src/api/activity.ts
388
- function fetchOpts4(ctx) {
354
+ function fetchOpts3(ctx) {
389
355
  return {
390
356
  retryAttempts: ctx?.retryAttempts,
391
357
  retryDelay: ctx?.retryDelay,
@@ -400,7 +366,7 @@ async function getActivity(baseUrl, params, ctx) {
400
366
  limit: params?.limit,
401
367
  offset: params?.offset
402
368
  });
403
- const result = await apiFetch(`${baseUrl}/activity${qs}`, fetchOpts4(ctx));
369
+ const result = await apiFetch(`${baseUrl}/activity${qs}`, fetchOpts3(ctx));
404
370
  const { total, limit: resLimit, offset: resOffset } = extractPagination(result, { limit: params?.limit, offset: params?.offset });
405
371
  return {
406
372
  data: result.data.map((item) => snakeToCamel(item)),
@@ -412,14 +378,14 @@ async function getActivity(baseUrl, params, ctx) {
412
378
  async function getActivityStats(baseUrl, ctx) {
413
379
  const result = await apiFetch(
414
380
  `${baseUrl}/activity/stats`,
415
- fetchOpts4(ctx)
381
+ fetchOpts3(ctx)
416
382
  );
417
383
  return snakeToCamel(result.data);
418
384
  }
419
385
  async function getPrizeStats(baseUrl, ctx) {
420
386
  const result = await apiFetch(
421
387
  `${baseUrl}/activity/prize-stats`,
422
- fetchOpts4(ctx)
388
+ fetchOpts3(ctx)
423
389
  );
424
390
  return snakeToCamel(result.data);
425
391
  }
@@ -981,8 +947,6 @@ function parseRegistration(raw, tournamentId) {
981
947
  gameTokenId: starknet.num.toHex(obj.game_token_id),
982
948
  gameAddress: "",
983
949
  // Not in on-chain struct
984
- playerAddress: "",
985
- // Not in on-chain struct
986
950
  entryNumber: Number(obj.entry_id ?? 0),
987
951
  hasSubmitted: Boolean(obj.has_submitted),
988
952
  isBanned: Boolean(obj.is_banned)
@@ -1141,20 +1105,6 @@ async function viewerRegistrations(contract, tournamentId, offset, limit) {
1141
1105
  };
1142
1106
  }, contract.address);
1143
1107
  }
1144
- async function viewerRegistrationsByOwner(contract, tournamentId, owner, offset, limit) {
1145
- return wrapRpcCall(async () => {
1146
- const result = await contract.call("tournament_registrations_by_owner", [tournamentId, owner, offset, limit]);
1147
- const obj = result;
1148
- const entries = obj.entries ?? [];
1149
- const total = Number(obj.total ?? 0);
1150
- return {
1151
- data: entries.map((e) => parseRegistration(e, tournamentId)),
1152
- total,
1153
- limit,
1154
- offset
1155
- };
1156
- }, contract.address);
1157
- }
1158
1108
  async function viewerRegistrationsByTokenIds(contract, tournamentId, tokenIds, offset, limit) {
1159
1109
  return wrapRpcCall(async () => {
1160
1110
  const result = await contract.call("tournament_registrations_by_token_ids", [tournamentId, tokenIds, offset, limit]);
@@ -1200,12 +1150,6 @@ async function viewerRewardClaims(contract, tournamentId, offset, limit) {
1200
1150
  };
1201
1151
  }, contract.address);
1202
1152
  }
1203
- async function viewerPlayerTournaments(contract, playerAddress, offset, limit) {
1204
- return wrapRpcCall(async () => {
1205
- const result = await contract.call("player_tournaments", [playerAddress, offset, limit]);
1206
- return parseFilterResult(result);
1207
- }, contract.address);
1208
- }
1209
1153
 
1210
1154
  // src/rpc/abis/budokanViewer.json
1211
1155
  var budokanViewer_default = [
@@ -4513,9 +4457,6 @@ var BudokanClient = class {
4513
4457
  const contract = await this.getViewerContract();
4514
4458
  const offset = params?.offset ?? 0;
4515
4459
  const limit = params?.limit ?? 20;
4516
- if (params?.playerAddress) {
4517
- return viewerRegistrationsByOwner(contract, tournamentId, params.playerAddress, offset, limit);
4518
- }
4519
4460
  if (params?.gameTokenIds?.length) {
4520
4461
  return viewerRegistrationsByTokenIds(contract, tournamentId, params.gameTokenIds, offset, limit);
4521
4462
  }
@@ -4548,43 +4489,6 @@ var BudokanClient = class {
4548
4489
  this.connectionStatus
4549
4490
  );
4550
4491
  }
4551
- // ---- Player Queries (API-only, no on-chain equivalent) ----
4552
- /**
4553
- * Fetch tournaments that a player has registered for.
4554
- * Supports RPC fallback via viewer contract.
4555
- */
4556
- async getPlayerTournaments(address, params) {
4557
- const rpcFallback = async () => {
4558
- const contract = await this.getViewerContract();
4559
- const offset = params?.offset ?? 0;
4560
- const limit = params?.limit ?? 20;
4561
- const filterResult = await viewerPlayerTournaments(contract, address, offset, limit);
4562
- let data = [];
4563
- if (filterResult.tournamentIds.length > 0) {
4564
- const tournaments = await viewerTournamentsBatch(contract, filterResult.tournamentIds);
4565
- data = tournaments.map((t) => ({
4566
- ...t,
4567
- tournamentId: t.id
4568
- }));
4569
- }
4570
- return { data, total: filterResult.total, limit, offset };
4571
- };
4572
- if (this.resolvedConfig.primarySource === "rpc") {
4573
- return rpcFallback();
4574
- }
4575
- return withFallback(
4576
- () => getPlayerTournaments(this.resolvedConfig.apiBaseUrl, address, params, this.apiCtx),
4577
- rpcFallback,
4578
- this.connectionStatus
4579
- );
4580
- }
4581
- /**
4582
- * Fetch stats for a player.
4583
- * API-only — no RPC fallback available.
4584
- */
4585
- async getPlayerStats(address) {
4586
- return getPlayerStats(this.resolvedConfig.apiBaseUrl, address, this.apiCtx);
4587
- }
4588
4492
  // ---- Game Queries ----
4589
4493
  /**
4590
4494
  * Fetch tournaments for a specific game.
@@ -4850,30 +4754,6 @@ function useTournamentCount(phase) {
4850
4754
  }, [fetch2]);
4851
4755
  return { count, loading, error, refetch: fetch2 };
4852
4756
  }
4853
- function usePlayerTournamentCount(address, phase) {
4854
- const client = useBudokanClient();
4855
- const [count, setCount] = react.useState(null);
4856
- const [loading, setLoading] = react.useState(!!address);
4857
- const [error, setError] = react.useState(null);
4858
- useResetOnClient(client, setCount, setError);
4859
- const fetch2 = react.useCallback(async () => {
4860
- if (!address) return;
4861
- setLoading(true);
4862
- setError(null);
4863
- try {
4864
- const result = await client.getPlayerTournaments(address, { phase, limit: 1 });
4865
- setCount(result.total ?? 0);
4866
- } catch (e) {
4867
- setError(e);
4868
- } finally {
4869
- setLoading(false);
4870
- }
4871
- }, [client, address, phase]);
4872
- react.useEffect(() => {
4873
- fetch2();
4874
- }, [fetch2]);
4875
- return { count, loading, error, refetch: fetch2 };
4876
- }
4877
4757
  function useLeaderboard(tournamentId) {
4878
4758
  const client = useBudokanClient();
4879
4759
  const [leaderboard, setLeaderboard] = react.useState(null);
@@ -4923,84 +4803,157 @@ function useRegistrations(tournamentId, params) {
4923
4803
  }, [fetch2]);
4924
4804
  return { registrations, loading, error, refetch: fetch2 };
4925
4805
  }
4926
- function usePlayer(address, params) {
4806
+ var MAX_OWNED_TOKENS = 1e3;
4807
+ var emptyPage = (limit, offset) => ({
4808
+ data: [],
4809
+ total: 0,
4810
+ limit,
4811
+ offset
4812
+ });
4813
+ function useOwnedTournamentIds(owner, contextId) {
4927
4814
  const client = useBudokanClient();
4928
- const [tournaments, setTournaments] = react.useState(null);
4929
- const [stats, setStats] = react.useState(null);
4930
- const [loading, setLoading] = react.useState(!!address);
4931
- const [error, setError] = react.useState(null);
4932
- useResetOnClient(client, setTournaments, setStats, setError);
4933
- const paramsKey = JSON.stringify(params);
4934
- const fetch2 = react.useCallback(async () => {
4935
- if (!address) return;
4936
- setLoading(true);
4937
- setError(null);
4938
- try {
4939
- const [tournamentsResult, statsResult] = await Promise.all([
4940
- client.getPlayerTournaments(address, params),
4941
- client.getPlayerStats(address)
4942
- ]);
4943
- setTournaments(tournamentsResult);
4944
- setStats(statsResult);
4945
- } catch (e) {
4946
- setError(e);
4947
- } finally {
4948
- setLoading(false);
4815
+ const budokanAddress = client.clientConfig.budokanAddress;
4816
+ const enabled = !!owner && !!budokanAddress;
4817
+ const { data, isLoading } = react$1.useTokens(
4818
+ enabled ? {
4819
+ owner,
4820
+ minterAddress: budokanAddress,
4821
+ hasContext: true,
4822
+ ...{},
4823
+ limit: MAX_OWNED_TOKENS
4824
+ } : void 0
4825
+ );
4826
+ const tournamentIds = react.useMemo(() => {
4827
+ if (!enabled) return null;
4828
+ if (!data?.data) return null;
4829
+ const ids = /* @__PURE__ */ new Set();
4830
+ for (const t of data.data) {
4831
+ if (t.contextId) ids.add(String(t.contextId));
4949
4832
  }
4950
- }, [client, address, paramsKey]);
4951
- react.useEffect(() => {
4952
- fetch2();
4953
- }, [fetch2]);
4954
- return { tournaments, stats, loading, error };
4833
+ return [...ids];
4834
+ }, [enabled, data]);
4835
+ return { tournamentIds, loading: isLoading };
4955
4836
  }
4956
- function usePlayerStats(address) {
4957
- const client = useBudokanClient();
4958
- const [stats, setStats] = react.useState(null);
4959
- const [loading, setLoading] = react.useState(!!address);
4960
- const [error, setError] = react.useState(null);
4961
- useResetOnClient(client, setStats, setError);
4962
- const fetch2 = react.useCallback(async () => {
4963
- if (!address) return;
4964
- setLoading(true);
4965
- setError(null);
4966
- try {
4967
- const result = await client.getPlayerStats(address);
4968
- setStats(result);
4969
- } catch (e) {
4970
- setError(e);
4971
- } finally {
4972
- setLoading(false);
4837
+ function useTournamentsByOwner(owner, params) {
4838
+ const { tournamentIds, loading: tokensLoading } = useOwnedTournamentIds(owner);
4839
+ const inner = useTournaments(
4840
+ tournamentIds && tournamentIds.length > 0 ? {
4841
+ tournamentIds,
4842
+ phase: params?.phase,
4843
+ limit: params?.limit,
4844
+ offset: params?.offset,
4845
+ sort: params?.sort,
4846
+ includePrizeSummary: params?.includePrizeSummary
4847
+ } : void 0
4848
+ );
4849
+ if (tournamentIds !== null && tournamentIds.length === 0) {
4850
+ return {
4851
+ tournaments: emptyPage(
4852
+ params?.limit ?? 50,
4853
+ params?.offset ?? 0
4854
+ ),
4855
+ loading: false,
4856
+ error: null,
4857
+ refetch: async () => {
4858
+ }
4859
+ };
4860
+ }
4861
+ return {
4862
+ tournaments: inner.tournaments,
4863
+ loading: tokensLoading || inner.loading,
4864
+ error: inner.error,
4865
+ refetch: inner.refetch
4866
+ };
4867
+ }
4868
+ function useTournamentsByOwnerCount(owner, params) {
4869
+ const { tournamentIds, loading: tokensLoading } = useOwnedTournamentIds(owner);
4870
+ const phaseFilter = params?.phase;
4871
+ const filtered = useTournaments(
4872
+ phaseFilter && tournamentIds && tournamentIds.length > 0 ? { tournamentIds, phase: phaseFilter, limit: 1 } : void 0
4873
+ );
4874
+ if (tournamentIds === null) {
4875
+ return {
4876
+ count: null,
4877
+ loading: tokensLoading,
4878
+ error: null,
4879
+ refetch: async () => {
4880
+ }
4881
+ };
4882
+ }
4883
+ if (tournamentIds.length === 0) {
4884
+ return {
4885
+ count: 0,
4886
+ loading: false,
4887
+ error: null,
4888
+ refetch: async () => {
4889
+ }
4890
+ };
4891
+ }
4892
+ if (!phaseFilter) {
4893
+ return {
4894
+ count: tournamentIds.length,
4895
+ loading: false,
4896
+ error: null,
4897
+ refetch: async () => {
4898
+ }
4899
+ };
4900
+ }
4901
+ return {
4902
+ count: filtered.tournaments?.total ?? null,
4903
+ loading: filtered.loading,
4904
+ error: filtered.error,
4905
+ refetch: async () => {
4973
4906
  }
4974
- }, [client, address]);
4975
- react.useEffect(() => {
4976
- fetch2();
4977
- }, [fetch2]);
4978
- return { stats, loading, error, refetch: fetch2 };
4907
+ };
4979
4908
  }
4980
- function usePlayerTournaments(address, params) {
4909
+ function useRegistrationsByOwner(tournamentId, owner, params) {
4981
4910
  const client = useBudokanClient();
4982
- const [tournaments, setTournaments] = react.useState(null);
4983
- const [loading, setLoading] = react.useState(!!address);
4984
- const [error, setError] = react.useState(null);
4985
- useResetOnClient(client, setTournaments, setError);
4986
- const paramsKey = JSON.stringify(params);
4987
- const fetch2 = react.useCallback(async () => {
4988
- if (!address) return;
4989
- setLoading(true);
4990
- setError(null);
4991
- try {
4992
- const result = await client.getPlayerTournaments(address, params);
4993
- setTournaments(result);
4994
- } catch (e) {
4995
- setError(e);
4996
- } finally {
4997
- setLoading(false);
4911
+ const budokanAddress = client.clientConfig.budokanAddress;
4912
+ const contextId = tournamentId ? Number(tournamentId) : void 0;
4913
+ const enabled = !!owner && !!budokanAddress && tournamentId != null && contextId !== void 0;
4914
+ const { data: tokensResult, isLoading: tokensLoading } = react$1.useTokens(
4915
+ enabled ? {
4916
+ owner,
4917
+ minterAddress: budokanAddress,
4918
+ contextId,
4919
+ limit: MAX_OWNED_TOKENS
4920
+ } : void 0
4921
+ );
4922
+ const ownedGameTokenIds = react.useMemo(() => {
4923
+ if (!enabled) return null;
4924
+ if (!tokensResult?.data) return null;
4925
+ const ids = [];
4926
+ for (const t of tokensResult.data) {
4927
+ if (t.tokenId == null) continue;
4928
+ try {
4929
+ ids.push(BigInt(String(t.tokenId)).toString());
4930
+ } catch {
4931
+ }
4998
4932
  }
4999
- }, [client, address, paramsKey]);
5000
- react.useEffect(() => {
5001
- fetch2();
5002
- }, [fetch2]);
5003
- return { tournaments, loading, error, refetch: fetch2 };
4933
+ return ids;
4934
+ }, [enabled, tokensResult]);
4935
+ const inner = useRegistrations(
4936
+ ownedGameTokenIds && ownedGameTokenIds.length > 0 ? tournamentId : void 0,
4937
+ ownedGameTokenIds && ownedGameTokenIds.length > 0 ? { ...params, gameTokenIds: ownedGameTokenIds } : void 0
4938
+ );
4939
+ if (ownedGameTokenIds !== null && ownedGameTokenIds.length === 0) {
4940
+ return {
4941
+ registrations: emptyPage(
4942
+ params?.limit ?? 50,
4943
+ params?.offset ?? 0
4944
+ ),
4945
+ loading: false,
4946
+ error: null,
4947
+ refetch: async () => {
4948
+ }
4949
+ };
4950
+ }
4951
+ return {
4952
+ registrations: inner.registrations,
4953
+ loading: tokensLoading || inner.loading,
4954
+ error: inner.error,
4955
+ refetch: inner.refetch
4956
+ };
5004
4957
  }
5005
4958
  function useRewardClaims(tournamentId, params) {
5006
4959
  const client = useBudokanClient();
@@ -5220,20 +5173,19 @@ exports.useActivityStats = useActivityStats;
5220
5173
  exports.useBudokanClient = useBudokanClient;
5221
5174
  exports.useConnectionStatus = useConnectionStatus;
5222
5175
  exports.useLeaderboard = useLeaderboard;
5223
- exports.usePlayer = usePlayer;
5224
- exports.usePlayerStats = usePlayerStats;
5225
- exports.usePlayerTournamentCount = usePlayerTournamentCount;
5226
- exports.usePlayerTournaments = usePlayerTournaments;
5227
5176
  exports.usePrizeAggregation = usePrizeAggregation;
5228
5177
  exports.usePrizeStats = usePrizeStats;
5229
5178
  exports.usePrizes = usePrizes;
5230
5179
  exports.useQualifications = useQualifications;
5231
5180
  exports.useRegistrations = useRegistrations;
5181
+ exports.useRegistrationsByOwner = useRegistrationsByOwner;
5232
5182
  exports.useRewardClaims = useRewardClaims;
5233
5183
  exports.useRewardClaimsSummary = useRewardClaimsSummary;
5234
5184
  exports.useSubscription = useSubscription;
5235
5185
  exports.useTournament = useTournament;
5236
5186
  exports.useTournamentCount = useTournamentCount;
5237
5187
  exports.useTournaments = useTournaments;
5188
+ exports.useTournamentsByOwner = useTournamentsByOwner;
5189
+ exports.useTournamentsByOwnerCount = useTournamentsByOwnerCount;
5238
5190
  //# sourceMappingURL=react.cjs.map
5239
5191
  //# sourceMappingURL=react.cjs.map