@dojoengine/sdk
Version:
Dojo SDK: Build onchain and provable apps faster
2 lines • 9.21 kB
JavaScript
import{shortString as C}from"starknet";import{CairoCustomEnum as g,CairoOption as u,CairoOptionVariant as y}from"starknet";import{CairoCustomEnum as M,CairoOption as k,CairoOptionVariant as b}from"starknet";import{addAddressPadding as A}from"starknet";import{err as K,ok as $}from"neverthrow";function f(e,t){if(Object.hasOwn(e,"type")&&Object.hasOwn(e,"value"))return{Primitive:{[e.type]:e.value}};if(typeof e=="number")return{Primitive:{U32:e}};if(typeof e=="boolean")return{Primitive:{Bool:e}};if(typeof e=="bigint")return{Primitive:{Felt252:t(e.toString())}};if(typeof e=="string")return{String:e};if(Array.isArray(e))return{List:e.map(r=>f(r,t))};throw new Error(`Unsupported primitive type: ${typeof e}`)}function Y(e,t,r="VariableLen"){return new c().keys(e,t,r)}function G(e){return new c().hashed_keys(e)}function J(e,t,r,s){return new c().where(e,t,r,s)}function W(e){return new c().compose().and(e)}function X(e){return new c().compose().or(e)}var c=class{clause;constructor(){this.clause={}}keys(e,t,r="VariableLen"){return this.clause={Keys:{keys:t.length===0?[void 0]:t,pattern_matching:r,models:e}},this}hashed_keys(e){let t=e.map((r,s)=>{try{return`0x${BigInt(r).toString(16)}`}catch{throw new Error(`Invalid key value at index ${s}: ${r}. Expected a valid BigNumberish value.`)}});return this.clause={HashedKeys:t},this}where(e,t,r,s){let n=Array.isArray(s)?{List:s.map(i=>f(i,C.encodeShortString))}:f(s,C.encodeShortString);return this.clause={Member:{model:e,member:t,operator:r,value:n}},this}compose(){return new S}build(){if(Object.keys(this.clause).length===0)throw new Error("You cannot build an empty Clause");return this.clause}},S=class{orClauses=[];andClauses=[];or(e){return this.orClauses=e.map(t=>t.build()),this}and(e){return this.andClauses=e.map(t=>t.build()),this}build(){if(this.orClauses.length===0&&this.andClauses.length===0)throw new Error("ComposeClause is empty. Add .or([clause]) or .and([clause])");if(this.orClauses&&this.andClauses.length===0)return{Composite:{operator:"Or",clauses:this.orClauses}};if(this.andClauses&&this.orClauses.length===0)return{Composite:{operator:"And",clauses:this.andClauses}};if(this.andClauses&&this.orClauses)return{Composite:{operator:"And",clauses:[...this.andClauses,{Composite:{operator:"Or",clauses:this.orClauses}}]}};throw new Error("CompositeClause is not properly build")}},Z="No signer configured in sdk.init()",z="No identity configured in sdk.init()",ee="Account is undefined",q="Clause has not been defined yet. Use `.withClause()` to do so";function te(e,t,r,s,n){return{types:{StarknetDomain:[{name:"name",type:"shortstring"},{name:"version",type:"shortstring"},{name:"chainId",type:"shortstring"},{name:"revision",type:"shortstring"}],...n,[e]:s!==void 0?s:Object.keys(t).map(i=>({name:i,type:typeof t[i]=="bigint"||typeof t[i]=="number"?"felt":"string"}))},primaryType:e,domain:r,message:t}}function v(e){return e instanceof u}function N(e,t){return t instanceof u&&t.isSome()?new u(y.Some,t.unwrap()):e instanceof u?e.isSome()?new u(y.Some,e.unwrap()):new u(y.None):e}function l(e){return e instanceof g}function P(e,t){if(!l(e)||!l(t))return e;let r=t.activeVariant(),s=t.unwrap();if(r&&s!==void 0){let a={};for(let o in e.variant)a[o]=void 0;return a[r]=s,new g(a)}let n=e.activeVariant(),i=e.unwrap();if(n&&i!==void 0){let a={};for(let o in e.variant)a[o]=void 0;return a[n]=i,new g(a)}return e}function x(e,t){if(v(e)&&v(t))return N(e,t);if(l(e)&&l(t))return P(e,t);let r={...e};for(let s in t)Object.hasOwn(t,s)&&(t[s]!==null&&typeof t[s]=="object"&&!Array.isArray(t[s])&&s in e&&typeof e[s]=="object"&&!Array.isArray(e[s])?r[s]=x(e[s],t[s]):r[s]=t[s]);return r}function se(e,t,r){let[s,n]=t.split("-");for(let i of r)if(i.entityId===e)return i.models?.[s]?.[n]}function ne(e,t){let[r,s]=e.split("-");for(let n of t)return n.models?.[r]?.[s]}var U={limit:1e3,cursor:void 0,direction:"Forward",order_by:[]},ie=class O{constructor(t,r,s){this.limit=t,this.cursor=r,this.direction=s,this.items=[],s||(this.direction="Forward")}items;static fromQuery(t,r){let s=t.getPagination();return new O(s.limit??1e3,r,s.direction)}withItems(t){return this.items=t,this}getItems(){return this.items}getNextQuery(t){let r=t.withLimit(this.limit);return this.cursor&&r.withCursor(this.cursor),r.getPagination().direction!==this.direction&&r.withDirection(this.direction),r}getPreviousQuery(t){let r=t.withLimit(this.limit);return this.cursor&&r.withCursor(this.cursor),r.getPagination().direction===this.direction&&r.withDirection(j(this.direction)),r}};function j(e){return e==="Forward"?"Backward":"Forward"}function D(e,t){let r=[];for(let s of e){let n=s.hashed_keys,i=s.models,a={entityId:n,models:{}};for(let o in s.models){let[d,w]=o.split("-");if(!d||!w){t?.logging&&console.warn(`Invalid modelName format: ${o}`);continue}a.models[d]||(a.models[d]={}),a.models[d][w]=I(i[o])}r.push(a),t?.logging&&console.log("Parsed entity:",a)}return t?.logging&&console.log("Parsed result:",r),Object.values(r)}function h(e){switch(e.type){case"primitive":return F(e);case"struct":return I(e.value);case"enum":return e.value.option==="Some"?new k(b.Some,h(e.value.value)):e.value.option==="None"?new k(b.None):L(e);case"tuple":case"array":return e.value.map(h);default:return e.value}}function L(e){return e.value.value.type==="tuple"?e.value.option:new M({[e.value.option]:h(e.value.value)})}function F(e){switch(e.type_name){case"u64":return Number.parseInt(e.value,16);case"i256":case"i128":case"u256":case"u128":return BigInt(e.value);default:return e.value}}function I(e){let t=e instanceof Map?Array.from(e.entries()):Object.entries(e);return Object.fromEntries(t.map(([r,s])=>[r,h(s)]))}function oe(e){return t=>{try{if(e){let r=D([t]);e({data:r,error:void 0})}}catch(r){e&&e({data:void 0,error:r instanceof Error?r:new Error(String(r))})}}}function p(e,t){return r=>{if(r!==t)try{e({data:r,error:void 0})}catch(s){e({data:void 0,error:s})}}}var T={balance:"0x0000000000000000000000000000000000000000000000000000000000000000",account_address:"0x0",contract_address:"0x0",token_id:"0x0000000000000000000000000000000000000000000000000000000000000000"};function m(e){return e.contractAddresses&&(e.contractAddresses=e.contractAddresses.map(t=>A(t))),e.accountAddresses&&(e.accountAddresses=e.accountAddresses.map(t=>A(t))),{contractAddresses:e.contractAddresses??[],accountAddresses:e.accountAddresses??[],tokenIds:e.tokenIds??[],pagination:e.pagination??U}}async function Q(e,t){let{contractAddresses:r,tokenIds:s,pagination:n}=m(t);return await e.getTokens({contract_addresses:r,token_ids:s,pagination:n})}async function V(e,t){let{contractAddresses:r,accountAddresses:s,tokenIds:n,pagination:i}=m(t);return await e.getTokenBalances({contract_addresses:r,account_addresses:s,token_ids:n,pagination:i})}async function ce(e,t){let{contractAddresses:r,accountAddresses:s,tokenIds:n}=m(t);return await e.onTokenBalanceUpdated(r??[],s??[],n??[],p(t.callback,T))}async function de(e,t){let{subscription:r,contractAddresses:s,accountAddresses:n,tokenIds:i}=t;return await e.updateTokenBalanceSubscription(r,s??[],n??[],i??[])}async function le(e,t){let{contractAddresses:r,accountAddresses:s,tokenIds:n,callback:i}=t,a=await V(e,{contractAddresses:r??[],accountAddresses:s??[],tokenIds:n??[]}),o=await e.onTokenBalanceUpdated(r??[],s??[],n??[],p(i,T));return[a,o]}var _={contract_address:"0x0",token_id:"0x0000000000000000000000000000000000000000000000000000000000000000",name:"",symbol:"",decimals:0,metadata:""};async function he(e,t){let{contractAddresses:r,tokenIds:s,callback:n}=m(t);return await e.onTokenUpdated(r??[],s??[],p(n,_))}async function pe(e,t){let{contractAddresses:r,tokenIds:s,callback:n}=t,i=await Q(e,{contractAddresses:r??[],tokenIds:s??[]}),a=await e.onTokenUpdated(r??[],s??[],p(n,_));return[i,a]}var E=()=>({pagination:{limit:100,cursor:void 0,direction:"Forward",order_by:[]},clause:void 0,no_hashed_keys:!0,models:[],historical:!1}),H=class B{query;constructor(t){this.query={...E(),...t}}withLimit(t){return this.query.pagination.limit=t,this}withOffset(){return this}withCursor(t){return this.query.pagination.cursor=t,this}withDirection(t){return this.query.pagination.direction=t,this}withClause(t){return this.query.clause=t,this}includeHashedKeys(){return this.query.no_hashed_keys=!1,this}addOrderBy(t,r){return this.query.pagination.order_by.push({field:t,direction:r}),this}withOrderBy(t){return this.query.pagination.order_by=t,this}addEntityModel(t){return this.query.models.push(t),this}withEntityModels(t){return this.query.models=t,this}build(){return this.query}static withPagination(t,r,s){return new B().withLimit(r).withCursor(t).withDirection(s)}getClause(){return this.query.clause?$(this.query.clause):K(q)}getPagination(){return this.query.pagination}isHistorical(){return this.query.historical}},ye=class extends H{constructor(e){super({...E(),...e,historical:!0})}};export{f as a,Y as b,G as c,J as d,W as e,X as f,c as g,Z as h,z as i,ee as j,q as k,te as l,v as m,N as n,l as o,P as p,x as q,se as r,ne as s,U as t,ie as u,D as v,oe as w,p as x,T as y,m as z,Q as A,V as B,ce as C,de as D,le as E,_ as F,he as G,pe as H,H as I,ye as J};
//# sourceMappingURL=chunk-OFVRBHAO.js.map