UNPKG

@dojoengine/sdk

Version:

Dojo SDK: Build onchain and provable apps faster

140 lines (131 loc) 6.58 kB
import { AccountInterface, BigNumberish } from 'starknet'; import { S as SchemaType, c as SDK, T as ToriiQueryBuilder, o as ParsedEntity, h as GetTokenRequest, f as GetTokenBalanceRequest } from './index.d-BxgvEQ4x.js'; import * as react_jsx_runtime from 'react/jsx-runtime'; import * as react from 'react'; import { ReactNode } from 'react'; import { DojoConfig, DojoProvider } from '@dojoengine/core'; import { GameState } from '@dojoengine/state/zustand'; import { UseBoundStore, StoreApi } from 'zustand'; import { Token, TokenBalance } from '@dojoengine/torii-wasm'; import '@dojoengine/torii-wasm/node'; import '@dojoengine/torii-wasm/types'; import 'neverthrow'; interface WithAccountProps { account: AccountInterface; address: `0x${string}`; } declare function WithAccount<P extends object>(Component: React.ComponentType<P>, Fallback?: React.ComponentType): React.FC<Omit<P, keyof WithAccountProps>>; type DojoStoreHook<T extends SchemaType> = <U>(selector: (state: GameState<T>) => U, equals?: (a: U, b: U) => boolean) => U; /** * Interface defining the shape of the Dojo context. */ interface DojoContextType<Client extends (...args: any) => any, Schema extends SchemaType> { /** The Dojo client instance */ config: DojoConfig; /** The Dojo client instance */ client: ReturnType<Client>; /** SDK client instance **/ sdk: SDK<Schema>; /** The Dojo provider */ provider: typeof DojoProvider; /** The dojo zustand store */ useDojoStore: DojoStoreHook<Schema>; } /** * React context for sharing Dojo-related data throughout the application. */ declare const DojoContext: react.Context<any>; /** * Provider component that makes Dojo context available to child components. * * @param props.children - Child components that will have access to the Dojo context * @param props.burnerManager - Instance of BurnerManager for handling burner accounts * @throws {Error} If DojoProvider is used more than once in the component tree */ declare function DojoSdkProvider<Schema extends SchemaType>({ dojoConfig, sdk, clientFn, children, }: { dojoConfig: DojoConfig; sdk: SDK<Schema>; clientFn: Function; children: ReactNode; }): react_jsx_runtime.JSX.Element; /** * Subscribe to entity changes. This hook fetches initial data from torii and subscribes to each entity change. * Use `useModel` to access your data. * * @param query ToriiQuery */ declare function useEntityQuery<Schema extends SchemaType>(query: ToriiQueryBuilder<Schema>): void; /** * Subscribe to historical entity changes. This hook fetches initial data from torii and subscribes to each entity change, * storing all states in the historical entities store. Use `getHistoricalEntities` to access historical data. * * @param query ToriiQuery with historical: true */ declare function useHistoricalEntityQuery<Schema extends SchemaType>(query: ToriiQueryBuilder<Schema>): void; /** * Subscribe to event changes. This hook fetches initial events from torii and subscribes to new events. * * @param query ToriiQuery */ declare function useEventQuery<Schema extends SchemaType>(query: ToriiQueryBuilder<Schema>): void; /** * Subscribe to historical events changes. This hook fetches initial data from torii and subscribes to entity changes. * You need to specify to torii which events has to be taken in account as historical events. * * @param query ToriiQuery */ declare function useHistoricalEventsQuery<Schema extends SchemaType>(query: ToriiQueryBuilder<Schema>): ParsedEntity<Schema>[]; /** * Factory function to create a React Zustand store based on a given SchemaType. * * @template T - The schema type. * @returns A Zustand hook tailored to the provided schema. */ declare function createDojoStore<T extends SchemaType>(): UseBoundStore<StoreApi<GameState<T>>>; /** * Custom hook to retrieve a specific model for a given entityId within a specified namespace. * * @param entityId - The ID of the entity. * @param model - The model to retrieve, specified as a string in the format "namespace-modelName". * @returns The model structure if found, otherwise undefined. */ declare function useModel<N extends keyof SchemaType, M extends keyof SchemaType[N] & string, Client extends (...args: any) => any, Schema extends SchemaType>(entityId: BigNumberish, model: `${N}-${M}`): SchemaType[N][M] | undefined; /** * Custom hook to retrieve a specific model for a given entityId within a specified namespace. * * @param entityId - The ID of the entity. * @param model - The model to retrieve, specified as a string in the format "namespace-modelName". * @returns The model structure if found, otherwise undefined. */ declare function useHistoricalModel<N extends keyof SchemaType, M extends keyof SchemaType[N] & string, Client extends (...args: any) => any, Schema extends SchemaType>(entityId: BigNumberish, model: `${N}-${M}`): ParsedEntity<Schema>[]; /** * Custom hook to retrieve all entities that have a specific model. * * @param model - The model to retrieve, specified as a string in the format "namespace-modelName". * @returns The model structure if found, otherwise undefined. */ declare function useModels<N extends keyof SchemaType, M extends keyof SchemaType[N] & string, Client extends (...args: any) => any, Schema extends SchemaType>(model: `${N}-${M}`): { [entityId: string]: SchemaType[N][M] | undefined; }; declare function useTokens(request: GetTokenRequest & GetTokenBalanceRequest): { tokens: Token[]; balances: TokenBalance[]; getBalance: (token: Token) => TokenBalance | undefined; toDecimal: (token: Token, balance: TokenBalance | undefined) => number; }; /** * Hook that exposes sdk features. * * @template Client Client function generated with `sozo build --typescript` * @template Schema Schema function generated with `sozo build --typescript` * @returns DojoContextType<Client, Schema> */ declare function useDojoSDK<Client extends (...args: any) => any, Schema extends SchemaType>(): DojoContextType<Client, Schema>; /** * If you know all distinct keys of your model, here is a way to compose it. * * @param keys Each keys corresponding to your model keys. * @returns Composed entityId */ declare function useEntityId(...keys: BigNumberish[]): BigNumberish; export { DojoContext, type DojoContextType, DojoSdkProvider, type DojoStoreHook, WithAccount, createDojoStore, useDojoSDK, useEntityId, useEntityQuery, useEventQuery, useHistoricalEntityQuery, useHistoricalEventsQuery, useHistoricalModel, useModel, useModels, useTokens };