crystara-sdk
Version:
Official SDK for Crystara NFT Marketplace
3 lines (2 loc) • 8.54 kB
JavaScript
function e(e,t,r,n){return new(r||(r=Promise))((function(o,s){function a(e){try{i(n.next(e))}catch(e){s(e)}}function c(e){try{i(n.throw(e))}catch(e){s(e)}}function i(e){var t;e.done?o(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(a,c)}i((n=n.apply(e,t||[])).next())}))}function t(e,t){var r,n,o,s,a={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return s={next:c(0),throw:c(1),return:c(2)},"function"==typeof Symbol&&(s[Symbol.iterator]=function(){return this}),s;function c(c){return function(i){return function(c){if(r)throw new TypeError("Generator is already executing.");for(;s&&(s=0,c[0]&&(a=0)),a;)try{if(r=1,n&&(o=2&c[0]?n.return:c[0]?n.throw||((o=n.return)&&o.call(n),0):n.next)&&!(o=o.call(n,c[1])).done)return o;switch(n=0,o&&(c=[2&c[0],o.value]),c[0]){case 0:case 1:o=c;break;case 4:return a.label++,{value:c[1],done:!1};case 5:a.label++,n=c[1],c=[0];continue;case 7:c=a.ops.pop(),a.trys.pop();continue;default:if(!(o=a.trys,(o=o.length>0&&o[o.length-1])||6!==c[0]&&2!==c[0])){a=0;continue}if(3===c[0]&&(!o||c[1]>o[0]&&c[1]<o[3])){a.label=c[1];break}if(6===c[0]&&a.label<o[1]){a.label=o[1],o=c;break}if(o&&a.label<o[2]){a.label=o[2],a.ops.push(c);break}o[2]&&a.ops.pop(),a.trys.pop();continue}c=t.call(e,a)}catch(e){c=[6,e],n=0}finally{r=o=0}if(5&c[0])throw c[1];return{value:c[0]?c[1]:void 0,done:!0}}([c,i])}}}function r(r){return e(this,void 0,void 0,(function(){var e,n,o,s,a,c,i,u,l;return t(this,(function(t){switch(t.label){case 0:if(e=function(e){return e?e.startsWith("ipfs://")?e.replace("ipfs://","https://ipfs.io/ipfs/"):(e.includes("/ipfs/")&&(e.startsWith("http://")||e.startsWith("https://")),e):e},n=[".json"],!r)throw new Error("URL is required");if(o=e(r),s=r.includes("ipfs/")||r.includes("ipfs://"),!(s||n.some((function(e){return o.toLowerCase().endsWith(e)}))))throw new Error("Invalid metadata extension");t.label=1;case 1:return t.trys.push([1,7,,8]),[4,fetch(o,{cache:"no-store",headers:{"Cache-Control":"no-cache"}})];case 2:return a=t.sent(),c=a.headers.get("content-type")||"",i=void 0,c.includes("image/")||o.toLowerCase().endsWith(".png")||o.toLowerCase().endsWith(".jpg")||o.toLowerCase().endsWith(".jpeg")||o.toLowerCase().endsWith(".gif")||o.toLowerCase().endsWith(".webp")?(i={image:o},[3,6]):[3,3];case 3:return t.trys.push([3,5,,6]),[4,a.json()];case 4:return i=t.sent(),[3,6];case 5:return u=t.sent(),console.error("Error parsing response as JSON:",u),i={image:o},[3,6];case 6:return i.image&&(i.image=e(i.image)),[2,{metadata:i}];case 7:throw l=t.sent(),console.error("Error fetching metadata:",l,"URL:",o),new Error("Failed to fetch metadata");case 8:return[2]}}))}))}function n(r){return e(this,arguments,void 0,(function(r,n){var o,s,a,c,i=this;return void 0===n&&(n=!1),t(this,(function(u){switch(u.label){case 0:o=function(e){if(!e)return e;var t=e;return e.includes("ipfs://")&&(t=process.env.NEXT_PUBLIC_IPFS_GATEWAY+"ipfs/"+e.split("ipfs://")[1]),t},s=[".json"],u.label=1;case 1:if(u.trys.push([1,3,,4]),!r||!Array.isArray(r)||0===r.length)throw new Error("URLs array is required");if(r.length>60)throw new Error("Batch size exceeds maximum of ".concat(60));return a=r.map((function(r){return e(i,void 0,void 0,(function(){var e,a,c,i,u,l,h,f;return t(this,(function(t){switch(t.label){case 0:if(!r||!function(e){try{var t=new URL(e);return("http:"===t.protocol||"https:"===t.protocol)&&!!t.host}catch(t){return e.startsWith("http://")||e.startsWith("https://")}}(r))return[2,{url:r,error:"Invalid URL",metadata:null}];if(e=o(r),a=r.includes("ipfs/")||r.includes("ipfs://"),!(a||s.some((function(t){return e.toLowerCase().endsWith(t)}))))return[2,{url:r,error:"Invalid metadata extension",metadata:null}];t.label=1;case 1:return t.trys.push([1,7,,8]),c=e,n&&(h="_cb=".concat(Date.now()),c=c.includes("?")?"".concat(c,"&").concat(h):"".concat(c,"?").concat(h)),[4,fetch(c,{cache:"no-store",headers:n?{"Cache-Control":"no-cache, no-store, must-revalidate",Pragma:"no-cache",Expires:"0"}:{"Cache-Control":"no-cache"}})];case 2:return i=t.sent(),u=i.headers.get("content-type")||"",l=void 0,u.includes("image/")||c.toLowerCase().endsWith(".png")||c.toLowerCase().endsWith(".jpg")||c.toLowerCase().endsWith(".jpeg")||c.toLowerCase().endsWith(".gif")||c.toLowerCase().endsWith(".webp")?(l={image:c},[3,6]):[3,3];case 3:return t.trys.push([3,5,,6]),[4,i.json()];case 4:return l=t.sent(),[3,6];case 5:return t.sent(),[2,{url:r,error:"Error parsing response as JSON",metadata:null}];case 6:return l.image&&(l.image=o(l.image),n&&(h="_cb=".concat(Date.now()),l.image=l.image.includes("?")?"".concat(l.image,"&").concat(h):"".concat(l.image,"?").concat(h))),[2,{url:r,error:null,metadata:l}];case 7:return f=t.sent(),[2,{url:r,error:"Failed to fetch metadata: ".concat(f instanceof Error?f.message:String(f)),metadata:null}];case 8:return[2]}}))}))})),[4,Promise.all(a)];case 2:return[2,{results:u.sent()}];case 3:throw c=u.sent(),console.error("Batch metadata fetch error:",c),c;case 4:return[2]}}))}))}function o(r,n){return e(this,void 0,void 0,(function(){var e,o,s,a,c,i,u,l;return t(this,(function(h){switch(h.label){case 0:e=process.env.CRYSTARA_PRIVATE_API_KEY,o=process.env.NEXT_PUBLIC_CRYSTARA_API_URL,h.label=1;case 1:h.trys.push([1,6,,7]),s=void 0,a=0,c=3,i=[100,400],u=function(){var u,l;return t(this,(function(t){switch(t.label){case 0:return t.trys.push([0,2,,4]),[4,fetch("".concat(o,"/lootbox-stats-by-url?url=").concat(r,"&includeLootbox=true&includeTokens=true").concat(n?"&viewer=".concat(n):""),{headers:{"x-api-key":e||""},cache:"no-store"})];case 1:if((s=t.sent()).ok)return[2,"break"];throw new Error("API error: ".concat(s.status));case 2:if((u=t.sent())instanceof Error&&u.message.includes("URL Does not exist"))throw new Error("Lootbox not found");if(a++,console.log("Attempt ".concat(a,"/").concat(c," failed for lootbox stats: ").concat(u instanceof Error?u.message:String(u))),a>=c)throw u;return l=i[a-1]||500,[4,new Promise((function(e){return setTimeout(e,l)}))];case 3:return t.sent(),[3,4];case 4:return[2]}}))},h.label=2;case 2:return a<c?[5,u()]:[3,4];case 3:return"break"===h.sent()?[3,4]:[3,2];case 4:if(!s)throw new Error("Failed to fetch lootbox stats");return[4,s.json()];case 5:return[2,h.sent()];case 6:throw l=h.sent(),console.error("Error fetching lootbox stats:",l),l;case 7:return[2]}}))}))}function s(r,n,o){return e(this,void 0,void 0,(function(){var e,s,a,c;return t(this,(function(t){switch(t.label){case 0:e=function(e){return Array.from((new TextEncoder).encode(e)).map((function(e){return e.toString(16).padStart(2,"0")})).join("")},t.label=1;case 1:return t.trys.push([1,4,,5]),s="".concat(process.env.NEXT_PUBLIC_CRYSTARA_ADR,"::").concat(process.env.NEXT_PUBLIC_COLLECTIONS_MODULE_NAME,"::get_whitelist_amount"),console.log("Function string:",s),console.log("Request params:",{lootboxCreatorAddress:r,collectionName:n,currentAddress:o}),[4,fetch("".concat(process.env.NEXT_PUBLIC_SUPRA_RPC_URL,"/view"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({function:s,type_arguments:[],arguments:[r,e(n),o]})})];case 2:return(a=t.sent()).ok||(console.log("Response:",a),console.log("Response status:",a.status)),[4,a.json()];case 3:return[2,t.sent()];case 4:throw c=t.sent(),console.error("Supra RPC view call failed:",c),new Error(c instanceof Error?c.message:"Failed to execute view function");case 5:return[2]}}))}))}function a(r,n,o){return e(this,void 0,void 0,(function(){var e,s,a;return t(this,(function(t){switch(t.label){case 0:return t.trys.push([0,3,,4]),e="".concat(process.env.NEXT_PUBLIC_CRYSTARA_ADR,"::").concat(process.env.NEXT_PUBLIC_COLLECTIONS_MODULE_NAME,"::get_lootbox_info_v5"),console.log("Function string:",e),console.log("Request params:",{lootboxCreatorAddress:r,collectionName:n,viewerAddress:o}),[4,fetch("".concat(process.env.NEXT_PUBLIC_SUPRA_RPC_URL,"/view"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({function:e,type_arguments:["0x1::supra_coin::SupraCoin"],arguments:[r,n,o||""]})})];case 1:return(s=t.sent()).ok||(console.log("Response:",s),console.log("Response status:",s.status)),[4,s.json()];case 2:return[2,t.sent()];case 3:throw a=t.sent(),console.error("Supra RPC view call failed:",a),new Error(a instanceof Error?a.message:"Failed to execute view function");case 4:return[2]}}))}))}export{n as batchFetchMetadataAction,a as fetchLootboxInfoAction,o as fetchLootboxStatsAction,r as fetchMetadataAction,s as fetchWhitelistAmountAction};
//# sourceMappingURL=server.esm.js.map