@dojoengine/sdk
Version:
Dojo SDK: Build onchain and provable apps faster
2 lines • 5.25 kB
JavaScript
import{a as k}from"./chunk-OFKFHZQF.js";import{useContext as G,useMemo as _}from"react";import{useContext as I,createContext as N}from"react";import{DojoProvider as Q}from"@dojoengine/core";import{useContext as x}from"react";import{create as M}from"zustand";function D(){return k(M)}function J(e,o){let[r,n]=o.split("-"),{useDojoStore:s}=x(p);return s(i=>i.entities[e.toString()]?.models?.[r]?.[n])}function L(e){let[o,r]=e.split("-"),{useDojoStore:n}=x(p);return n(t=>t.getEntitiesByModel(o,r).map(i=>({[i.entityId]:i.models?.[o]?.[r]})))}import{jsx as H}from"react/jsx-runtime";var p=N(void 0);function ee({dojoConfig:e,sdk:o,clientFn:r,children:n}){if(I(p))throw new Error("DojoProvider can only be used once");let t=new Q(e.manifest,e.rpcUrl);return H(p.Provider,{value:{config:e,client:r(t),sdk:o,provider:t,useDojoStore:D()},children:n})}import{getEntityIdFromKeys as $}from"@dojoengine/utils";import{useCallback as A,useEffect as R,useRef as S}from"react";function l(e,o){if(e===o)return!0;if(e===null||o===null||typeof e!="object"||typeof o!="object")return!1;if(Array.isArray(e)&&Array.isArray(o)){if(e.length!==o.length)return!1;for(let s=0;s<e.length;s++)if(!l(e[s],o[s]))return!1;return!0}if(e instanceof Date&&o instanceof Date)return e.getTime()===o.getTime();if(e instanceof RegExp&&o instanceof RegExp)return e.toString()===o.toString();let r=Object.keys(e),n=Object.keys(o);return r.length!==n.length?!1:r.every(s=>Object.prototype.hasOwnProperty.call(o,s)&&l(e[s],o[s]))}function g(e){return new Promise(o=>setTimeout(o,e))}function y(e){return function(r){let n=S(null),s=S(null),t=S(!1),i=A(async()=>{for(;t.current;)await g(50);if(t.current=!0,n.current)return await e.updateSubscriptionMethod(n.current,e.queryToHashedKeysMethod(s.current,e.historical),e.historical),null;let[a,c]=await e.subscribeMethod({query:s.current,callback:({data:f,error:h})=>{f&&e.processUpdateData(f),h&&(console.error(`${e.getErrorPrefix()} - error subscribing with query: `,r.toString()),console.error(h))},historical:e.historical});return e.processInitialData(a.getItems()),c},[r]);R(()=>(l(r,s.current)||(s.current=r,i().then(a=>{a!==null&&(n.current=a)}).catch(a=>{console.error(`${e.getErrorPrefix()} - error fetching data for query: `,JSON.stringify(r)),console.error(a)}).finally(()=>{t.current=!1})),()=>{n.current}),[r,i])}}function ue(e){let{sdk:o,useDojoStore:r}=m(),n=r(t=>t);y({subscribeMethod:t=>o.subscribeEntityQuery(t),updateSubscriptionMethod:(t,i)=>o.updateEntitySubscription(t,i),queryToHashedKeysMethod:t=>t.getClause()._unsafeUnwrap(),processInitialData:t=>{n.mergeEntities(t)},processUpdateData:t=>{if(t){let a=t.filter(c=>Number.parseInt(c.entityId,16)!==0)[0];a&&n.updateEntity(a)}},getErrorPrefix:()=>"Dojo.js - useEntityQuery",historical:!1})(e)}import{useState as w}from"react";function ye(e){let{sdk:o,useDojoStore:r}=m(),n=r(t=>t);y({subscribeMethod:t=>o.subscribeEventQuery(t),updateSubscriptionMethod:(t,i)=>o.updateEventMessageSubscription(t,i,!1),queryToHashedKeysMethod:t=>t.getClause()._unsafeUnwrap(),processInitialData:t=>{n.mergeEntities(t)},processUpdateData:t=>{if(t){let a=t.filter(c=>Number.parseInt(c.entityId,16)!==0)[0];a&&n.updateEntity(a)}},getErrorPrefix:()=>"Dojo.js - useEventQuery",historical:!1})(e)}function fe(e){let{sdk:o}=m(),[r,n]=w([]);return y({subscribeMethod:t=>o.subscribeEventQuery(t),updateSubscriptionMethod:(t,i)=>o.updateEventMessageSubscription(t,i,!0),queryToHashedKeysMethod:t=>t.getClause()._unsafeUnwrap(),processInitialData:t=>{n(t)},processUpdateData:t=>{if(t){let i=t.filter(a=>Number.parseInt(a.entityId,16)!==0);n(a=>[...i,...a])}},getErrorPrefix:()=>"Dojo.js - useHistoricalEventsQuery",historical:!0})(e),r}import{useState as j,useEffect as U,useRef as E,useCallback as C}from"react";function ke(e){let{sdk:o}=m(),[r,n]=j([]),s=E(null),[t,i]=j([]),a=E(null),c=C(async()=>{let u=await o.getTokens({contractAddresses:e.contractAddresses??[],tokenIds:e.tokenIds??[]});n(u.items)},[o,e]),f=C(async()=>{let[u,d]=await o.subscribeTokenBalance({contractAddresses:e.contractAddresses??[],accountAddresses:e.accountAddresses??[],tokenIds:e.tokenIds??[],callback:({data:P,error:b})=>{if(b){console.error(b);return}i(v=>K(v,P))}});a.current=d,i(u.items)},[o,e]);U(()=>{l(e,s.current)||(s.current=e,c(),f())},[e]);function h(u){return t.find(d=>d.contract_address===u.contract_address)}function B(u,d){return Number.parseInt(d?.balance??"0",16)*10**-u.decimals}return{tokens:r,balances:t,getBalance:h,toDecimal:B}}function K(e,o){let r=e.findIndex(n=>n.token_id===o.token_id);return r===-1?[...e,o]:e.map((n,s)=>s===r?o:n)}function m(){return G(p)}function Ee(...e){return _(()=>e.length>0?$(e.map(r=>BigInt(r))):BigInt(0),[e])}import{useAccount as O}from"@starknet-react/core";import{jsx as T}from"react/jsx-runtime";function Ne(e,o=()=>T("div",{children:"Please connect your wallet"})){return r=>{let{account:n,address:s}=O();if(!s)return o?T(o,{}):null;let t={...r,account:n,address:s};return T(e,{...t})}}export{p as DojoContext,ee as DojoSdkProvider,Ne as WithAccount,D as createDojoStore,m as useDojoSDK,Ee as useEntityId,ue as useEntityQuery,ye as useEventQuery,fe as useHistoricalEventsQuery,J as useModel,L as useModels,ke as useTokens};
//# sourceMappingURL=react.js.map