@gala-chain/launchpad-sdk 3.7.2 โ†’ 3.7.5

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/index.esm.js CHANGED
@@ -1 +1 @@
1
- import{ethers as e,Wallet as t,Mnemonic as n,HDNodeWallet as a}from"ethers";import r from"axios";import{SigningType as s,SigningClient as o,ChainCallDTO as i,calculatePersonalSignPrefix as c}from"@gala-chain/connect";import{z as l}from"zod";import{TokenClassKey as d}from"@gala-chain/api";import u from"bignumber.js";import{v4 as h}from"uuid";import{io as g}from"socket.io-client";var p,m;!function(e){e.WALLET_NOT_CONNECTED="WALLET_NOT_CONNECTED",e.SIGNATURE_FAILED="SIGNATURE_FAILED",e.INVALID_ADDRESS="INVALID_ADDRESS",e.MESSAGE_GENERATION_FAILED="MESSAGE_GENERATION_FAILED"}(p||(p={}));class f extends Error{constructor(e,t,n){super(t),this.type=e,this.originalError=n,this.name="AuthError"}}class y{constructor(e){if(this.wallet=e.wallet,this.messagePrefix=e.messagePrefix||"Create a GalaChain Wallet",""===e.messagePrefix)throw new f(p.SIGNATURE_FAILED,"Message prefix cannot be empty");this.validateWallet()}async generateSignature(){try{const e=Date.now(),t=`${this.messagePrefix} ${e}`,n=await this.wallet.signMessage(t);return{message:t,signature:n,address:this.formatAddress(this.wallet.address),timestamp:e}}catch(e){throw new f(p.SIGNATURE_FAILED,"Failed to generate signature for authentication",e instanceof Error?e:new Error(String(e)))}}getAddress(){return this.formatAddress(this.wallet.address)}getEthereumAddress(){return this.wallet.address}formatAddress(e){const t=e.replace(/^0x/i,"");if(!/^[a-fA-F0-9]{40}$/.test(t))throw new f(p.INVALID_ADDRESS,`Invalid Ethereum address format: ${e}`);return`eth|${t}`}async signMessage(e){try{return{message:e,signature:await this.wallet.signMessage(e),address:this.wallet.address,timestamp:Date.now()}}catch(e){const t=e instanceof Error?e.message:String(e);throw new f(p.SIGNATURE_FAILED,t,e instanceof Error?e:new Error(String(e)))}}async generateAuthHeaders(e,t){try{const n=Date.now(),a=`${this.messagePrefix} ${t.toUpperCase()} ${e} ${n}`,r=await this.wallet.signMessage(a);return{"x-signature":r,"x-address":this.formatAddress(this.wallet.address),"x-message":a,"x-timestamp":n.toString()}}catch(e){throw new f(p.SIGNATURE_FAILED,"Failed to generate authentication headers",e instanceof Error?e:new Error(String(e)))}}async signTypedData(e,t,n){try{return await this.wallet.signTypedData(e,t,n)}catch(e){throw new f(p.SIGNATURE_FAILED,"Failed to sign typed data",e instanceof Error?e:new Error(String(e)))}}async generateCustomSignature(e){if(!e||"string"!=typeof e||0===e.trim().length)throw new f(p.SIGNATURE_FAILED,"Custom message must be a non-empty string");try{const t=await this.wallet.signMessage(e);return{message:e,signature:t,address:this.formatAddress(this.wallet.address),timestamp:Date.now()}}catch(e){throw new f(p.SIGNATURE_FAILED,"Failed to generate custom message signature",e instanceof Error?e:new Error(String(e)))}}validateWallet(){if(!this.wallet)throw new f(p.WALLET_NOT_CONNECTED,"Wallet is required for authentication");if(!this.wallet.address)throw new f(p.WALLET_NOT_CONNECTED,"Wallet address is not available");if(!this.wallet.privateKey&&!this.wallet.signMessage)throw new f(p.WALLET_NOT_CONNECTED,"Wallet must have a private key for signing messages")}}!function(e){e.DEBUG="DEBUG",e.INFO="INFO",e.WARN="WARN",e.ERROR="ERROR"}(m||(m={}));class w{constructor(e){this.levelPriority={[m.DEBUG]:0,[m.INFO]:1,[m.WARN]:2,[m.ERROR]:3},this.debugEnabled=e.debug,this.context=e.context||"SDK",this.minLevel=e.minLevel||(e.debug?m.DEBUG:m.INFO)}debug(e,t){this.log(m.DEBUG,e,t)}info(e,t){this.log(m.INFO,e,t)}warn(e,t){this.log(m.WARN,e,t)}error(e,t){this.log(m.ERROR,e,t)}log(e,t,n){if(this.levelPriority[e]<this.levelPriority[this.minLevel])return;if(e===m.DEBUG&&!this.debugEnabled)return;const a=`[${(new Date).toISOString()}] [${this.context}] [${e}]`,r=this.getConsoleMethod(e);void 0!==n?n instanceof Error?r(`${a} ${t}`,n.message,n.stack):r(`${a} ${t}`,n):r(`${a} ${t}`)}getConsoleMethod(e){switch(e){case m.DEBUG:return console.debug;case m.INFO:return console.info;case m.WARN:return console.warn;case m.ERROR:return console.error;default:return console.log}}child(e){return new w({debug:this.debugEnabled,context:`${this.context}:${e}`,minLevel:this.minLevel})}isDebugEnabled(){return this.debugEnabled&&this.levelPriority[m.DEBUG]>=this.levelPriority[this.minLevel]}}function A(e){const t={};for(const[n,a]of Object.entries(e))null!=a&&("string"==typeof a?t[n]=a:"number"==typeof a||"boolean"==typeof a?t[n]=a.toString():Array.isArray(a)?t[n]=a.join(","):t[n]="object"==typeof a?JSON.stringify(a):String(a));return t}class k{constructor(e,t={}){this.auth=e,this.debug=t.debug??!1,this.logger=new w({debug:this.debug,context:"HttpClient"}),this.axios=r.create({baseURL:t.baseUrl||"https://lpad-backend-dev1.defi.gala.com",timeout:t.timeout||3e4,headers:{Accept:"application/json",...t.headers}}),this.setupInterceptors()}async request(e){try{const t={method:e.method,url:e.url,data:e.data,...e.params&&{params:A(e.params)},...e.headers&&{headers:e.headers},...e.timeout&&{timeout:e.timeout}};e.data instanceof FormData&&(t.headers&&t.headers["Content-Type"]&&delete t.headers["Content-Type"],this.logger.debug("FormData detected - removing Content-Type header for multipart upload"));const n=e.data instanceof FormData?"[FormData object - multipart/form-data]":e.data;this.logger.debug("Request:",{method:e.method,url:e.url,fullUrl:`${this.axios.defaults.baseURL}${e.url}`,baseURL:this.axios.defaults.baseURL,params:t.params,data:n,isFormData:e.data instanceof FormData,contentType:t.headers?.["Content-Type"]||"not set"});const a=await this.axios.request(t);return this.logger.debug("Response:",{status:a.status,data:a.data}),a.data}catch(e){throw this.logger.error("Error:",e),e}}async get(e,t,n){return this.request({method:"GET",url:e,...t&&{params:t},...n&&{headers:n}})}async post(e,t,n){return this.request({method:"POST",url:e,data:t,...n&&{headers:n}})}async put(e,t,n){return this.request({method:"PUT",url:e,data:t,...n&&{headers:n}})}async delete(e,t,n){return this.request({method:"DELETE",url:e,...t&&{params:t},...n&&{headers:n}})}async patch(e,t,n){return this.request({method:"PATCH",url:e,data:t,...n&&{headers:n}})}getAddress(){return this.auth.getAddress()}getEthereumAddress(){return this.auth.getEthereumAddress()}async signMessage(e){return(await this.auth.signMessage(e)).signature}async signTypedData(e,t,n){return await this.auth.signTypedData(e,t,n)}async signCustomMessage(e){try{const t=await this.auth.generateCustomSignature(e);return this.logger.debug("Generated custom signature:",{message:e,address:t.address,ethereumAddress:this.auth.getEthereumAddress()}),{signature:t.signature,address:t.address,ethereumAddress:this.auth.getEthereumAddress()}}catch(e){throw this.logger.error("Custom signature generation failed:",e),new Error(`Failed to generate custom signature for message: ${e instanceof Error?e.message:"Unknown error"}`)}}async signWithGalaChain(e,t,n=s.SIGN_TYPED_DATA){const a=this.auth.wallet.privateKey;if(!a)throw new Error("Wallet private key not available for @gala-chain signing");const r=new o(a);return await r.sign(e,t,n)}setupInterceptors(){this.requestInterceptorId=this.axios.interceptors.request.use(async e=>{try{const t=await this.auth.generateSignature();return e.headers||(e.headers={}),e.headers.Sign=t.signature,e.data instanceof FormData||(e.headers["Content-Type"]="application/json"),this.logger.debug("Added signature header:",{address:t.address,message:t.message,timestamp:t.timestamp}),e}catch(e){throw this.logger.error("Failed to add signature:",e),e}},e=>Promise.reject(e)),this.responseInterceptorId=this.axios.interceptors.response.use(e=>e,e=>{if(e.response){const t={message:e.response.data?.message||e.message,error:e.response.data?.error,statusCode:e.response.status,details:e.response.data?.details,timestamp:e.response.data?.timestamp,path:e.response.data?.path};e.launchpadError=t,this.logger.error("Backend error:",t)}else e.request?this.logger.error("Network error:",e.message):this.logger.error("Request setup error:",e.message);return Promise.reject(e)})}cleanup(){void 0!==this.requestInterceptorId&&(this.axios.interceptors.request.eject(this.requestInterceptorId),this.requestInterceptorId=void 0),void 0!==this.responseInterceptorId&&(this.axios.interceptors.response.eject(this.responseInterceptorId),this.responseInterceptorId=void 0),this.logger.debug("Interceptors cleaned up")}}const v="Token name is required",E="Token name is required and must be a string",T=e=>`Could not find vault address for token: ${e}`;class b extends Error{constructor(e,t,n){super(e),this.field=t,this.code=n,this.name="ValidationError"}}function N(e,t,n){const{MIN_PAGE:a,MAX_PAGE:r,MIN_LIMIT:s,MAX_LIMIT:o}=n.PAGINATION;if("number"!=typeof e||e<a||e>r)throw new b(`Page must be a number between ${a} and ${r}`,"page","INVALID_PAGE");if("number"!=typeof t||t<s||t>o)throw new b(`Limit must be a number between ${s} and ${o}`,"limit","INVALID_LIMIT")}const I={ETH_ADDRESS:/^0x[0-9a-fA-F]{40}$/,BACKEND_ADDRESS:/^eth\|[0-9a-fA-F]{40}$/};function S(e){return"string"==typeof e&&e.trim().length>0}function D(e){return!(!e||"string"!=typeof e)&&(I.ETH_ADDRESS.test(e)||I.BACKEND_ADDRESS.test(e))}function $(e){return e.startsWith("0x")?`eth|${e.slice(2)}`:e}const F=l.string().min(3,"Token name must be at least 3 characters").max(20,"Token name must be at most 20 characters").regex(/^[a-zA-Z0-9]{3,20}$/,"Token name can only contain letters and numbers"),C=l.string().min(1,"Token symbol must be at least 1 character").max(8,"Token symbol must be at most 8 characters").regex(/^[A-Z]{1,8}$/,"Token symbol must be uppercase letters only"),_=l.string().min(1,"Token description is required").max(500,"Token description must be at most 500 characters"),P=l.string().min(1,"Token name must be at least 1 character").max(50,"Token name must be at most 50 characters"),U=l.string().min(1,"Search query must be at least 1 character").max(100,"Search query must be at most 100 characters"),L=l.string().min(1,"Full name is required").max(100,"Full name must be at most 100 characters").regex(/^[a-zA-Z\s]+$/,"Full name can only contain letters and spaces"),O=l.string().regex(I.BACKEND_ADDRESS,"Address must be in format: eth|[40-hex-chars]"),x=l.string().regex(I.ETH_ADDRESS,"Invalid Ethereum address format"),R=l.string().refine(e=>I.BACKEND_ADDRESS.test(e)||I.ETH_ADDRESS.test(e),"Address must be either eth|[40-hex-chars] or 0x[40-hex-chars] format").transform(e=>e.startsWith("0x")?`eth|${e.slice(2)}`:e),B=l.string().refine(e=>I.BACKEND_ADDRESS.test(e)||/^service\|Token\$Unit\$[A-Z0-9]+\$eth:[0-9a-fA-F]{40}\$launchpad$/.test(e),"Invalid vault address format"),M=l.string().regex(/^\d+(\.\d+)?$/,"Must be a valid decimal number").refine(e=>parseFloat(e)>0,"Amount must be greater than zero"),K=l.string().regex(/^\d+(\.\d+)?$/,"Must be a valid decimal number").refine(e=>parseFloat(e)>=0,"Amount must be zero or greater"),G=l.string().regex(/^(?!0+(\.0+)?$)\d+(\.\d+)?$/,"Amount must be a positive, non-zero number"),q=l.string().url("Must be a valid URL").regex(/^https?:\/\//,"URL must start with http:// or https://"),V=l.string().optional().refine(e=>!e||/^https?:\/\/.+\..+/.test(e),"Must be a valid URL if provided"),j=l.number().int("Page must be an integer").min(1,"Page must be at least 1").max(1e3,"Page must be at most 1000").default(1);function W(e=100){return l.number().int("Limit must be an integer").min(1,"Limit must be at least 1").max(e,`Limit must be at most ${e}`).default(10)}const H=W(100),Q=W(20),X=W(20),z=l.number().int("File size must be an integer").min(1,"File must be at least 1 byte").max(10485760,"File must be at most 10MB"),Y=l.string().max(255,"Filename must be at most 255 characters"),J=l.enum(["image/png","image/jpg","image/jpeg","image/gif","image/webp","image/svg+xml"]),Z=l.string().min(1,"Comment message is required").max(500,"Comment must be at most 500 characters"),ee=l.string().datetime("Must be a valid ISO 8601 date string"),te=l.number().int("Timestamp must be an integer").min(0,"Timestamp must be non-negative"),ne=l.string().regex(/^0x[a-fA-F0-9]{64}$/,"Private key must be format: 0x + 64 hex characters"),ae=l.string().regex(/^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/,"Transaction ID must be in UUID format"),re=l.string().regex(/^galaconnect-operation-[a-z0-9-]+$/,"Unique key must be format: galaconnect-operation-{unique-id}"),se=l.object({websiteUrl:V,telegramUrl:V,twitterUrl:V}).refine(e=>e.websiteUrl||e.telegramUrl||e.twitterUrl,"At least one social URL (website, telegram, or twitter) is required"),oe=l.string().min(1,"Token category must not be empty").default("Unit"),ie=l.string().min(1,"Token collection must not be empty").default("Token"),ce=l.object({minFeePortion:M,maxFeePortion:M}),le=l.object({tokenName:F,tokenSymbol:C,tokenDescription:_,tokenImage:l.union([l.instanceof(File),l.instanceof(Buffer),l.string().url("Token image must be a valid URL")]).optional(),preBuyQuantity:K.default("0"),websiteUrl:V,telegramUrl:V,twitterUrl:V,tokenCategory:oe,tokenCollection:ie,reverseBondingCurveConfiguration:ce.optional(),privateKey:ne.optional()}),de=l.object({file:l.union([l.instanceof(File),l.instanceof(Buffer)]),tokenName:F}),ue=l.enum(["recent","popular"]),he=l.object({tokenName:F.optional(),symbol:C.optional()}).refine(e=>e.tokenName||e.symbol,"At least one of tokenName or symbol is required"),ge=l.enum(["NATIVE","MEME"]),pe=l.enum(["IN","OUT"]),me=l.object({from:l.number().int("From timestamp must be an integer").min(173e6,"From timestamp must be at least 173000000"),to:l.number().int("To timestamp must be an integer").min(173e6,"To timestamp must be at least 173000000"),resolution:l.number().int("Resolution must be an integer").min(1,"Resolution must be at least 1"),tokenName:F}),fe=[".png",".jpg",".jpeg",".gif",".webp",".svg"],ye=l.object({file:l.union([l.instanceof(File),l.instanceof(Buffer)]),name:Y,size:z,type:J}),we=l.instanceof(File).refine(e=>e.size>=1&&e.size<=10485760,"File size must be between 1 byte and 10MB").refine(e=>["image/png","image/jpg","image/jpeg","image/gif","image/webp","image/svg+xml"].includes(e.type),"File must be a valid image type (PNG, JPG, JPEG, GIF, WebP, or SVG)").refine(e=>e.name.length<=255,"Filename must be at most 255 characters"),Ae=l.instanceof(Buffer).refine(e=>e.length>=1&&e.length<=10485760,"Buffer size must be between 1 byte and 10MB"),ke=l.union([we,Ae]),ve=l.enum([".png",".jpg",".jpeg",".gif",".webp",".svg"]),Ee=Y.refine(e=>{const t=e.slice(e.lastIndexOf(".")).toLowerCase();return fe.includes(t)},`Filename must end with one of: ${fe.join(", ")}`),Te=l.object({page:j,limit:H}),be=l.object({page:j,limit:Q}),Ne=l.object({page:j,limit:X}),Ie=l.object({page:j,limit:W(50)}),Se=Te.extend({type:l.enum(["recent","popular"]).optional(),tokenName:l.string().min(1).max(50).optional(),search:l.string().min(1).max(100).optional()}),De=be.extend({tokenName:l.string().min(1).max(50).optional(),search:l.string().min(1).max(100).optional()}),$e=Ne.extend({tradeType:l.enum(["BUY","SELL"]).optional(),tokenName:l.string().min(1).max(50).optional(),userAddress:l.string().regex(/^(0x[a-fA-F0-9]{40}|eth\|[a-fA-F0-9]{40})$/).optional(),startDate:l.string().datetime().optional(),endDate:l.string().datetime().optional(),sortOrder:l.enum(["ASC","DESC"]).default("DESC")}),Fe=l.object({page:l.number().int().min(1),limit:l.number().int().min(1),total:l.number().int().min(0),totalPages:l.number().int().min(0),hasNext:l.boolean(),hasPrevious:l.boolean()});function Ce(e){return l.object({data:l.array(e),page:l.number().int().min(1),limit:l.number().int().min(1),total:l.number().int().min(0),totalPages:l.number().int().min(0),hasNext:l.boolean(),hasPrevious:l.boolean()})}const _e=l.enum(["all","DEFI","ASSET"]),Pe=be.extend({type:_e.optional(),address:R.optional(),search:U.optional(),tokenName:P.optional()}),Ue=l.object({walletAddress:R,amount:G}),Le=l.object({address:R.optional(),refresh:l.boolean().optional()}),Oe=l.object({profileImage:l.string(),fullName:L,address:R,privateKey:ne.optional()}),xe=l.object({file:l.union([l.instanceof(File),l.instanceof(Buffer)]),address:R.optional(),privateKey:ne.optional()}),Re=l.object({address:R,tokenId:l.union([l.string(),l.object({collection:l.string(),category:l.string(),type:l.string(),additionalKey:l.string()}),l.object({collection:l.string(),category:l.string(),type:l.string(),additionalKey:l.string(),instance:l.string()})]).optional(),tokenClassKey:l.object({collection:l.string(),category:l.string(),type:l.string(),additionalKey:l.string()}).optional(),tokenName:P.optional()}).refine(e=>void 0!==e.tokenId||void 0!==e.tokenName,"At least one token identifier (tokenId or tokenName) is required"),Be=l.enum(["buy","sell"]),Me=l.enum(["BUY","SELL"]),Ke=l.object({tradeType:Be,tokenAmount:M,vaultAddress:B,userAddress:R,slippageTolerance:M.optional(),deadline:l.number().int().positive().optional()}),Ge=l.object({tokenSymbol:C,nativeTokenQuantity:M,expectedToken:M,maxAcceptableReverseBondingCurveFee:K.default("0").optional()}),qe=l.object({tokenSymbol:C,tokenQuantity:M,expectedNativeToken:M,maxAcceptableReverseBondingCurveFee:K.default("0").optional()}),Ve=Ne.extend({tokenName:P.optional()}),je=l.object({page:l.number().int().min(1).max(1e3).default(1).optional(),limit:l.number().int().min(1).max(20).default(10).optional()}),We=l.enum(["NATIVE","MEME"]),He=l.enum(["IN","OUT"]),Qe=l.object({type:We,method:He,vaultAddress:B,amount:M}),Xe=l.object({nativeTokenQuantity:M}),ze=l.object({vaultAddress:B}),Ye=l.object({minFeePortion:M,maxFeePortion:M});function Je(e){return t=>{const n=e.safeParse(t);return{success:n.success,data:n.success?n.data:void 0,errors:n.success?void 0:n.error.errors.map(e=>e.message)}}}const Ze=Je(F),et=Je(C),tt=Je(_),nt=Je(R),at=Je(B),rt=Je(M),st=Je(G),ot=Je(L),it=Je(U),ct=Je(P),lt=Je(le),dt=Je(se),ut=Je(de),ht=Je(he),gt=Je(Pe),pt=Je(Ue),mt=Je(Le),ft=Je(Oe),yt=Je(xe),wt=Je(Re),At=Je(Ke),kt=Je(Ge),vt=Je(qe),Et=Je(Ve),Tt=Je(je),bt=Je(Qe),Nt=Je(Xe),It=Je(ze);function St(e,t){throw new b(e.join("; "),t,"VALIDATION_ERROR")}function Dt(e){const t=Ze(e);!t.success&&t.errors&&St(t.errors,"tokenName")}function $t(e){const t=Se.safeParse(e);t.success||St(t.error.errors.map(e=>e.message),"pagination")}function Ft(e){const t=ht(e);!t.success&&t.errors&&St(t.errors,"options")}function Ct(e){const t=bt(e);!t.success&&t.errors&&St(t.errors,"options")}function _t(e){const t=me.safeParse(e);t.success||St(t.error.errors.map(e=>e.message),"options")}function Pt(e){const t=R.safeParse(e);if(!t.success)throw new b("Ethereum address must be 40 hex characters (with or without 0x prefix)","ethereumAddress","INVALID_FORMAT");return t.data}var Ut=Object.freeze({__proto__:null,normalizeAddressInput:function(e){if(!e)return;const t=R.safeParse(e);if(!t.success)throw new b(`Invalid address format: ${e}. Must be either "0x..." (Ethereum) or "eth|..." (GalaChain) format`,"address","INVALID_FORMAT");return t.data},toBackendAddressFormat:Pt,validateCheckPoolOptions:Ft,validateGetAmountOptions:Ct,validateGetGraphOptions:_t,validatePagination:$t,validateTokenName:Dt});function Lt(e){if(!e)return[];return(Array.isArray(e)?e:e.token??[]).map(e=>({...e,createdAt:new Date(e.createdAt),updatedAt:new Date(e.updatedAt)}))}function Ot(e,t){const n=Number(e.page)||t.page,a=Number(e.limit)||t.limit,r=Number(e.total)||Number(e.data?.count)||0;return{page:n,limit:a,total:r,totalPages:Math.ceil(r/a)}}function xt(e,t){return{hasNext:e<t,hasPrevious:e>1}}function Rt(e,t,n=!1){const a=!0===e.error||200!==e.status,r=n&&!e.data;if(a||r)throw new Error(e.message||t)}const Bt="/launchpad/upload-image",Mt="/launchpad/fetch-pool",Kt="/launchpad/check-pool",Gt="/launchpad/get-graph-data",qt="/holders",Vt="/launchpad/get-badge/",jt="/trade/",Wt="/token/commment",Ht="/token/commment",Qt="/user/profile",Xt="/user/profile",zt="/user/token-list",Yt="/user/token-hold",Jt="/user/transfer-faucets";function Zt(e){return e&&"object"==typeof e&&"string"==typeof e.tokenName&&(void 0===e.from||"number"==typeof e.from)&&(void 0===e.to||"number"==typeof e.to)&&(void 0===e.resolution||"number"==typeof e.resolution)}class en{constructor(e){this.http=e}async fetchPools(e={}){let t;$t({page:e.page??1,limit:e.limit??10}),e.tokenName&&Dt(e.tokenName),"recent"===e.type?t="RECENT":"popular"===e.type&&(t="POPULAR");const n={page:e.page||1,limit:e.limit||10};e.search&&(n.search=e.search),e.tokenName&&(n.tokenName=e.tokenName),t&&(n.type=t);const a={page:n.page.toString(),limit:n.limit.toString()};void 0!==n.type&&(a.type=n.type),void 0!==n.tokenName&&(a.tokenName=n.tokenName),void 0!==n.search&&(a.search=n.search);const r=A(a),s=await this.http.get(Mt,r);Rt(s,"Failed to fetch pools",!0);return{pools:function(e){if(!e)return[];let t=[];return e.tokens?t=Array.isArray(e.tokens)?e.tokens.map(e=>({...e,createdAt:new Date(e.created_at??e.createdAt)})):[{...e.tokens,createdAt:new Date(e.tokens.created_at??e.tokens.createdAt)}]:e.pools&&Array.isArray(e.pools)&&(t=e.pools.map(e=>({...e,createdAt:new Date(e.created_at??e.createdAt)}))),t}(s.data),page:s.data.page,limit:s.data.limit,total:s.data.total,totalPages:s.data.totalPages,hasNext:s.data.page<s.data.totalPages,hasPrevious:s.data.page>1}}async checkPool(e){Ft(e),e.tokenName&&Dt(e.tokenName);const t=A(e),n=await this.http.get(Kt,t);Rt(n,"Failed to check pool");const a=n.data;return e.symbol?a?.isSymbolExist??!1:e.tokenName?a?.isNameExist??!1:a?.exists??!1}async isTokenNameAvailable(e){try{return!await this.checkPool({tokenName:e})}catch{return!1}}async isTokenSymbolAvailable(e){try{return!await this.checkPool({symbol:e})}catch{return!1}}async fetchVolumeData(e){if(!Zt(e))throw new b("Invalid options provided. Expected { tokenName: string, from?: number, to?: number, resolution?: number }","options","INVALID_OPTIONS");const{tokenName:t,from:n,to:a,resolution:r}=e;if(Dt(t),!n||!a||!r)throw new b("Graph options (from, to, resolution) are required","options","MISSING_GRAPH_OPTIONS");const s={tokenName:t,from:n,to:a,resolution:r};_t(s);const o=A(s),i=await this.http.get(Gt,o);return Rt(i,"Failed to fetch graph data",!0),{dataPoints:i.data}}async fetchTokenDistribution(e){if(!e)throw new Error(v);Dt(e);const t=await this.resolveTokenNameToVault(e);if(!t)throw new Error(T(e));const n=encodeURIComponent(t),a=await this.http.get(`${qt}/${n}`);return Rt(a,"Failed to fetch token distribution",!0),{holders:a.data.holders||[],totalSupply:a.data.totalSupply||"0",totalHolders:a.data.totalHolders||0,lastUpdated:new Date}}async fetchTokenBadges(e){if(!e)throw new Error(v);Dt(e);const t=await this.http.get(Vt,{tokenName:e});return Rt(t,"Failed to fetch token badges",!0),{volumeBadges:t.data.volumeBadge||[],engagementBadges:t.data.engagementBadge||[]}}async hasTokenBadge(e){const{tokenName:t,badgeType:n,badgeName:a}=e;try{const e=await this.fetchTokenBadges(t);if(!e)return!1;const r=("volume"===n?e.volumeBadges:e.engagementBadges).find(e=>e.badgeName===a);return r?.isActive||!1}catch{return!1}}async resolveTokenNameToVault(e){try{const t=await this.fetchPools({tokenName:e});return t.pools&&Array.isArray(t.pools)&&t.pools.length>0?t.pools[0].vaultAddress||null:t.pools&&t.pools.tokens&&t.pools.tokens.vaultAddress||null}catch{return null}}}function tn(e,t={}){const{stringifyFields:n=[],optionalFields:a=[],fieldMappings:r={}}=t,s={};for(const[t,o]of Object.entries(e)){const e=t;if(a.includes(e)&&void 0===o)continue;if(a.includes(e)&&"string"==typeof o&&0===o.trim().length)continue;const i=r[e],c=i?String(i):t;n.includes(e)?s[c]=String(o):s[c]=o}return A(s)}const nn={PAGINATION:{MIN_PAGE:1,MAX_PAGE:1e3,MIN_LIMIT:1,MAX_LIMIT:20}};class an{constructor(e){this.http=e}async fetchTrades(e){if(!(t=e)||"object"!=typeof t||"string"!=typeof t.tokenName||void 0!==t.tradeType&&"buy"!==t.tradeType&&"sell"!==t.tradeType||void 0!==t.userAddress&&"string"!=typeof t.userAddress||void 0!==t.page&&"number"!=typeof t.page||void 0!==t.limit&&"number"!=typeof t.limit)throw new b("Invalid options provided. Expected { tokenName: string, tradeType?: string, userAddress?: string, page?: number, limit?: number, startDate?: Date, endDate?: Date, sortOrder?: string }","options","INVALID_OPTIONS");var t;const{tokenName:n,tradeType:a,userAddress:r,page:s=1,limit:o=10,startDate:i,endDate:c,sortOrder:l}=e;if(!S(n))throw new b("Token name is required and must be a non-empty string","tokenName","INVALID_TOKEN_NAME");N(s,o,nn);const d=function(e,t,n){return tn({tokenName:e,page:t,limit:n},{stringifyFields:["page","limit"]})}(n,s,o),u=await this.http.get(jt,d),h=(g=u.data)?(Array.isArray(g)?g:g.trades?g.trades:[]).map(e=>({...e,createdAt:new Date(e.createdAt),updatedAt:new Date(e.updatedAt)})):[];var g;const p=Ot(u,{page:s,limit:o}),m=xt(p.page,p.totalPages);return{trades:h,...p,...m}}}const rn=new w({debug:!1,context:"DateUtils"});function sn(e,t){if(!e)return t||new Date;if(e instanceof Date)return isNaN(e.getTime())?t||new Date:e;try{const n=new Date(e);return isNaN(n.getTime())?(rn.warn(`Invalid date string received: "${e}". Using fallback.`),t||new Date):n}catch(n){return rn.warn(`Date parsing error for "${e}":`,n),t||new Date}}function on(e,t){return new b(`Token "${e}" not found. Please verify the token name is correct.`,"tokenName","TOKEN_NOT_FOUND")}const cn={PAGINATION:{MIN_PAGE:1,MAX_PAGE:1e3,MIN_LIMIT:1,MAX_LIMIT:50},CONTENT:{MIN_LENGTH:1,MAX_LENGTH:500}};class ln{constructor(e,t){this.http=e,this.poolService=t}async fetchComments(e){if(!(t=e)||"object"!=typeof t||"string"!=typeof t.tokenName||void 0!==t.page&&"number"!=typeof t.page||void 0!==t.limit&&"number"!=typeof t.limit)throw new b("Invalid options provided. Expected { tokenName: string, page?: number, limit?: number }","options","INVALID_OPTIONS");var t;const{tokenName:n,page:a=1,limit:r=10}=e;if(!S(n))throw new b("Token name is required and must be a non-empty string","tokenName","INVALID_TOKEN_NAME");N(a,r,cn);const s=await this.poolService.resolveTokenNameToVault(n);if(!s)throw on(n);const o=tn({vaultAddress:s,page:a,limit:r},{stringifyFields:["page","limit"]}),i=await this.http.get(Wt,o);Rt(i,"Failed to fetch comments");return{comments:i.data.comments.map(e=>({...e,createdAt:sn(e.createdAt)})),total:i.data.count}}async postComment(e){if(!(t=e)||"object"!=typeof t||"string"!=typeof t.tokenName||"string"!=typeof t.content)throw new b("Invalid options provided. Expected { tokenName: string, content: string }","options","INVALID_OPTIONS");var t;const{tokenName:n,content:a}=e;if(!S(n))throw new b("Token name is required and must be a non-empty string","tokenName","INVALID_TOKEN_NAME");if(!function(e){if(!e||"string"!=typeof e)return!1;const t=e.trim();return t.length>=cn.CONTENT.MIN_LENGTH&&t.length<=cn.CONTENT.MAX_LENGTH}(a))throw new b(`Comment content must be between ${cn.CONTENT.MIN_LENGTH} and ${cn.CONTENT.MAX_LENGTH} characters`,"content","INVALID_CONTENT");const r=await this.poolService.resolveTokenNameToVault(n);if(!r)throw on(n);const s={userAddress:this.http.getAddress(),vaultAddress:r,content:a};Rt(await this.http.post(Ht,s),"Failed to create comment")}}const dn={PAGINATION:{MIN_PAGE:1,MAX_PAGE:1e3,MIN_LIMIT:1,MAX_LIMIT:20},USER_ADDRESS:{PATTERN:/^eth\|[0-9a-fA-F]{40}$/},TOKEN_NAME:{MIN_LENGTH:1,MAX_LENGTH:50},SEARCH:{MIN_LENGTH:1,MAX_LENGTH:100},FAUCET_AMOUNT:{POSITIVE_NON_ZERO_DECIMAL:/^(?!0+(\.0+)?$)\d+(\.\d+)?$/},PROFILE:{FULL_NAME:{MIN_LENGTH:1,MAX_LENGTH:100,ALPHABETS_ONLY_PATTERN:/^[a-zA-Z]+(?:\s[a-zA-Z]+)?$/}}};function un(e){return!(!e||"string"!=typeof e)&&dn.USER_ADDRESS.PATTERN.test(e)}const hn="Update profile";class gn{constructor(e){this.http=e}async fetchProfile(e){const t=e??this.http.getAddress();if(!un(t))throw new b("Address must be in format: eth|[40-hex-chars]","address","INVALID_ADDRESS");const n={userAddress:t};return await this.http.get(Qt,n)}async updateProfile(e){this.validateUpdateProfileData(e);let t=e.profileImage;if(!t||""===t.trim())try{const n=await this.fetchProfile(e.address);t=n.data?.profileImage||""}catch{t=""}const n={profileImage:t,fullName:e.fullName,userAddress:e.address},a=await this.http.signCustomMessage(hn),r={address:a.address,message:hn,publickey:a.ethereumAddress,sign:a.signature};Rt(await this.http.put(Xt,n,r),"Profile update failed")}async uploadProfileImage(e){this.validateUploadProfileImageOptions(e);const t=e.address??this.http.getAddress();try{const n=new FormData;if("undefined"!=typeof File&&e.file instanceof File)n.append("image",e.file);else{if(!Buffer.isBuffer(e.file))throw new b("Invalid file type","file","INVALID_FILE_TYPE");{const a=`profile-image-${t}.png`,r=new Blob([e.file],{type:"image/png"});n.append("image",r,a)}}const a=await this.http.request({method:"POST",url:`${Bt}?tokenName=${encodeURIComponent(t)}`,data:n,headers:{}});return Rt(a,"Image upload failed"),"string"==typeof a.data?a.data:""}catch(e){if(e instanceof b)throw e;throw new b(`Profile image upload failed: ${e instanceof Error?e.message:"Unknown error"}`,"file","UPLOAD_FAILED")}}async fetchTokenList(e){this.validateGetTokenListOptions(e);const t=tn({page:e.page,limit:e.limit,type:"all"!==e.type&&e.type?e.type:"DEFI",address:e.address,search:e.search,tokenName:e.tokenName},{stringifyFields:["page","limit"],optionalFields:["address","search","tokenName"]}),n=await this.http.get(zt,t);Rt(n,"Failed to fetch token list",!0);const a=Lt(n.data),r=Ot(n,{page:e.page||1,limit:e.limit||10}),s=xt(r.page,r.totalPages);return{tokens:a,...r,...s}}async fetchTokensHeld(e){this.validateGetTokenListOptions(e);const t=tn({page:e.page,limit:e.limit,address:e.address,search:e.search,tokenName:e.tokenName},{stringifyFields:["page","limit"],optionalFields:["address","search","tokenName"]}),n=await this.http.get(Yt,t);Rt(n,"Failed to fetch tokens held",!0);const a=Lt(n.data),r=Ot(n,{page:e.page||1,limit:e.limit||10}),s=xt(r.page,r.totalPages);return{tokens:a,...r,...s}}async fetchTokensCreated(e={}){const{page:t=1,limit:n=10,search:a,tokenName:r}=e,s={type:"DEFI",address:this.http.getAddress(),page:t,limit:n};return void 0!==a&&(s.search=a),void 0!==r&&(s.tokenName=r),this.fetchTokenList(s)}validateGetTokenListOptions(e){if(N(e.page,e.limit,dn),void 0!==e.address&&!un(e.address))throw new b("Address must be in format: eth|[40-hex-chars]","address","INVALID_ADDRESS");if(void 0!==e.search&&e.search.trim().length>0&&!((t=e.search)&&"string"==typeof t&&t.length>=dn.SEARCH.MIN_LENGTH&&t.length<=dn.SEARCH.MAX_LENGTH))throw new b(`Search query must be between ${dn.SEARCH.MIN_LENGTH} and ${dn.SEARCH.MAX_LENGTH} characters`,"search","INVALID_SEARCH");var t,n;if(void 0!==e.tokenName&&e.tokenName.trim().length>0&&!((n=e.tokenName)&&"string"==typeof n&&n.length>=dn.TOKEN_NAME.MIN_LENGTH&&n.length<=dn.TOKEN_NAME.MAX_LENGTH))throw new b(`Token name must be between ${dn.TOKEN_NAME.MIN_LENGTH} and ${dn.TOKEN_NAME.MAX_LENGTH} characters`,"tokenName","INVALID_TOKEN_NAME")}validateUpdateProfileData(e){if(!un(e.address))throw new b("Address must be in format: eth|[40-hex-chars]","address","INVALID_ADDRESS");if(!((t=e.fullName)&&"string"==typeof t&&t.length>=dn.PROFILE.FULL_NAME.MIN_LENGTH&&t.length<=dn.PROFILE.FULL_NAME.MAX_LENGTH&&dn.PROFILE.FULL_NAME.ALPHABETS_ONLY_PATTERN.test(t)))throw new b(`Full name must be between ${dn.PROFILE.FULL_NAME.MIN_LENGTH} and ${dn.PROFILE.FULL_NAME.MAX_LENGTH} characters`,"fullName","INVALID_FULL_NAME");var t}validateUploadProfileImageOptions(e){if(e.address&&!un(e.address))throw new b("Address must be in format: eth|[40-hex-chars]","address","INVALID_ADDRESS")}}class pn extends Error{constructor(e,t,n){super(e),this.filename=t,this.mimeType=n,this.name="FileValidationError"}}function mn(e,t,n){if(!e)throw new pn("File is required",t,n);if("undefined"!=typeof File&&e instanceof File){const t=we.safeParse(e);if(!t.success){const n=t.error.errors.map(e=>e.message).join("; ");throw new pn(n,e.name,e.type)}return}if(Buffer.isBuffer(e)){if(!t)throw new pn("Filename is required when uploading Buffer objects",t,n);const a=Ae.safeParse(e);if(!a.success){const e=a.error.errors.map(e=>e.message).join("; ");throw new pn(e,t,n)}if(t.length>255)throw new pn(`Filename length ${t.length} exceeds maximum allowed length of 255 characters`,t,n);const r=["image/png","image/jpg","image/jpeg","image/gif","image/webp","image/svg+xml"];if(!r.includes(n))throw new pn(`Invalid file type "${n}" is not allowed. Allowed types: ${r.join(", ")}`,t,n);const s=function(e){if(!e)return"";const t=e.lastIndexOf(".");if(-1===t)return"";return e.substring(t).toLowerCase()}(t),o=[".png",".jpg",".jpeg",".gif",".webp",".svg"];if(!o.includes(s))throw new pn(`File extension "${s}" is not allowed. Allowed extensions: ${o.join(", ")}`,t,n);const i=function(e){switch(e.toLowerCase()){case".png":return"image/png";case".jpg":case".jpeg":return"image/jpeg";case".gif":return"image/gif";case".webp":return"image/webp";case".svg":return"image/svg+xml";default:return"application/octet-stream"}}(s);if(i!==n&&"application/octet-stream"!==i)throw new pn(`File extension "${s}" does not match MIME type "${n}"`,t,n);return}throw new pn("File must be a File object (browser) or Buffer (Node.js)",t,n)}class fn{constructor(e){this.http=e}async uploadImageByTokenName(e){const{tokenName:t,options:n}=e;Dt(t);const a=`${t}.png`;mn(n.file,a,"image/png");try{const e=new FormData;if("undefined"!=typeof File&&n.file instanceof File)e.append("image",n.file);else{if(!Buffer.isBuffer(n.file))throw new Error("Unsupported file type");{const a=`${n.tokenName??t}.png`,r=new Blob([n.file],{type:"image/png"});e.append("image",r,a)}}const a=await this.http.request({method:"POST",url:`${Bt}?tokenName=${encodeURIComponent(n.tokenName??t)}`,data:e,headers:{}});return Rt(a,"Image upload failed"),"string"==typeof a.data?a.data:""}catch(e){if(e instanceof Error&&e.message.includes("FormData"))throw new Error("File upload failed: FormData not supported in this environment. Ensure you have proper polyfills for Node.js environments.");throw e}}}class yn{constructor(e){this.http=e}async transferFaucets(e){this.validateTransferFaucetsData(e);const t={userAddress:e.walletAddress,amount:e.amount};Rt(await this.http.post(Jt,t),"Faucet transfer failed")}validateTransferFaucetsData(e){if(!un(e.walletAddress))throw new b("Address must be in format: eth|[40-hex-chars]","address","INVALID_ADDRESS");if(!(t=e.amount)||"string"!=typeof t||!dn.FAUCET_AMOUNT.POSITIVE_NON_ZERO_DECIMAL.test(t))throw new b("Amount must be a positive decimal string greater than zero","amount","INVALID_AMOUNT");var t}}class wn{constructor(e){this.http=e,this.poolService=new en(e),this.tradeService=new an(e),this.commentService=new ln(e,this.poolService),this.userService=new gn(e),this.imageService=new fn(e),this.faucetService=new yn(e)}async uploadImageByTokenName(e){return this.imageService.uploadImageByTokenName(e)}async fetchPools(e={}){return this.poolService.fetchPools(e)}async checkPool(e){return this.poolService.checkPool(e)}async isTokenNameAvailable(e){return this.poolService.isTokenNameAvailable(e)}async isTokenSymbolAvailable(e){return this.poolService.isTokenSymbolAvailable(e)}async fetchVolumeData(e){return this.poolService.fetchVolumeData(e)}async fetchTokenDistribution(e){return this.poolService.fetchTokenDistribution(e)}async fetchTokenBadges(e){return this.poolService.fetchTokenBadges(e)}async hasTokenBadge(e){return this.poolService.hasTokenBadge(e)}async fetchTrades(e){return this.tradeService.fetchTrades(e)}async fetchComments(e){return this.commentService.fetchComments(e)}async postComment(e){return this.commentService.postComment(e)}async fetchProfile(e){return this.userService.fetchProfile(e)}async updateProfile(e){return this.userService.updateProfile(e)}async uploadProfileImage(e){return this.userService.uploadProfileImage(e)}async fetchTokenList(e){return this.userService.fetchTokenList(e)}async fetchTokensHeld(e){return this.userService.fetchTokensHeld(e)}async fetchTokensCreated(e={}){return this.userService.fetchTokensCreated(e)}async transferFaucets(e){return this.faucetService.transferFaucets(e)}getAddress(){return this.http.getAddress()}validateTokenName(e){return Dt(e)}}class An extends i{constructor(e){super(),this.from=e.from,this.to=e.to,this.quantity=e.quantity,this.tokenInstance=e.tokenInstance,this.uniqueKey=e.uniqueKey,e.signedPayload&&(this.signature=e.signedPayload.signature,this.domain=e.signedPayload.domain,this.types=e.signedPayload.types,e.signedPayload.prefix&&(this.prefix=e.signedPayload.prefix))}static fromTokenClassKey(e,t,n,a,r){let s;if("string"==typeof a){const e=a.split("$");if(4!==e.length)throw new Error(`Invalid token class key format: ${a}. Expected format: collection$category$type$additionalKey`);s={collection:e[0],category:e[1],type:e[2],additionalKey:e[3],instance:"0"}}else s={collection:a.collection,category:a.category,type:a.type,additionalKey:a.additionalKey,instance:"0"};return new An({from:e,to:t,quantity:n,tokenInstance:s,uniqueKey:r||`galaconnect-operation-${Date.now()}_${Math.random().toString(36).substring(2,8)}`})}static forGALA(e,t,n,a){return new An({from:e,to:t,quantity:n,tokenInstance:{collection:"GALA",category:"Unit",type:"none",additionalKey:"none",instance:"0"},uniqueKey:a||`galaconnect-operation-${Date.now()}_${Math.random().toString(36).substring(2,8)}`})}getTokenClassKey(){return`${this.tokenInstance.collection}$${this.tokenInstance.category}$${this.tokenInstance.type}$${this.tokenInstance.additionalKey}`}toSigningPayload(){return{from:this.from,to:this.to,quantity:this.quantity,tokenInstance:this.tokenInstance,uniqueKey:this.uniqueKey}}}class kn{constructor(e){this.wallet=e}static generateUniqueKey(){return`${Date.now()}_${Math.random().toString(36).substring(2,8)}`}async signTransferToken(e){const t={name:"GalaChain",chainId:1},n={TransferToken:[{name:"from",type:"string"},{name:"to",type:"string"},{name:"quantity",type:"string"},{name:"tokenInstance",type:"TokenInstance"},{name:"uniqueKey",type:"string"}],TokenInstance:[{name:"collection",type:"string"},{name:"category",type:"string"},{name:"type",type:"string"},{name:"additionalKey",type:"string"},{name:"instance",type:"string"}]};return{signature:await this.wallet.signTypedData(t,n,e),domain:t,types:n,signerPublicKey:this.wallet.signingKey.publicKey}}static toGalaChainAddress(t){const n=t.replace("0x","");return`eth|${e.getAddress(`0x${n}`).replace("0x","")}`}static fromGalaChainAddress(e){return e.startsWith("eth|")?e.substring(4):e}static createGALATokenInstance(){return{collection:"GALA",category:"Unit",type:"none",additionalKey:"none",instance:"0"}}static createTokenInstanceFromClassKey(e){const t=e.split("$");if(4!==t.length)throw new Error(`Invalid token class key format: ${e}. Expected format: collection$category$type$additionalKey`);return{collection:t[0],category:t[1],type:t[2],additionalKey:t[3],instance:"0"}}}function vn(e){if("string"==typeof e){const t=e.split("|");if(t.length<4)throw new b(`Invalid tokenId string format: "${e}". Expected format: "collection|category|type|additionalKey" or "collection|category|type|additionalKey|instance"`,"tokenId","INVALID_TOKEN_ID_FORMAT");if(!(t[0]&&t[1]&&t[2]&&t[3]))throw new b(`Invalid tokenId string format: "${e}". All components (collection, category, type, additionalKey) must be non-empty`,"tokenId","INVALID_TOKEN_ID_FORMAT");return{collection:t[0],category:t[1],type:t[2],additionalKey:t[3],instance:t[4]||"0"}}if("object"==typeof e&&null!==e){if(!(e.collection&&e.category&&e.type&&e.additionalKey))throw new b("Invalid tokenId object format. All fields (collection, category, type, additionalKey) are required","tokenId","INVALID_TOKEN_ID_FORMAT");return"instance"in e&&void 0!==e.instance?e:{...e,instance:"0"}}throw new b(`Invalid tokenId type: ${typeof e}. Expected string, TokenClassKey, or TokenInstanceKey`,"tokenId","INVALID_TOKEN_ID_TYPE")}var En=Object.freeze({__proto__:null,normalizeToTokenInstanceKey:vn});const Tn={MAX_UNIQUE_KEY_LENGTH:64,UNIQUE_KEY_PATTERN:/^(galaswap-operation-|galaconnect-operation-)/,TOKEN_NAME_PATTERN:/^[a-zA-Z0-9]+$/};var bn;!function(e){e.INVALID_RECIPIENT="INVALID_RECIPIENT",e.INVALID_AMOUNT="INVALID_AMOUNT",e.INSUFFICIENT_BALANCE="INSUFFICIENT_BALANCE",e.TOKEN_NOT_FOUND="TOKEN_NOT_FOUND",e.SIGNATURE_FAILED="SIGNATURE_FAILED",e.NETWORK_ERROR="NETWORK_ERROR",e.DUPLICATE_TRANSFER="DUPLICATE_TRANSFER",e.TRANSFER_LIMIT_EXCEEDED="TRANSFER_LIMIT_EXCEEDED"}(bn||(bn={}));class Nn extends Error{constructor(e,t,n){super(e),this.type=t,this.details=n,this.name="TransferError"}}class In{constructor(e,t,n,a=!1){this.http=e,this.wallet=t,this.tokenResolver=n,this.signatureHelper=new kn(t),this.logger=new w({debug:a,context:"GalaChainService"})}async fetchPoolDetails(e){this.validateFetchPoolDetailsData(e);const t=await this.http.post("/api/asset/launchpad-contract/FetchSaleDetails",e);if(1!==t.Status)throw new Error(`Failed to fetch pool details: Status ${t.Status}`);return t}validateFetchPoolDetailsData(e){if(!(t=e)||"object"!=typeof t||"string"!=typeof t.vaultAddress)throw new b("Invalid fetch pool details data: missing required fields","data","INVALID_TYPE");var t;if(!e.vaultAddress||"string"!=typeof e.vaultAddress)throw new b("Vault address is required and must be a string","vaultAddress","INVALID_VAULT_ADDRESS");if(!e.vaultAddress.startsWith("service|Token$Unit$"))throw new b("Vault address must be in service format: service|Token$Unit$...","vaultAddress","INVALID_VAULT_ADDRESS")}async fetchGalaBalance(e){return this.fetchTokenBalance(e)}async fetchTokenBalance(e){try{const t=await this.http.post("/api/asset/token-contract/FetchBalances",e);if(1!==t.Status||!t.Data||0===t.Data.length)return null;const n=t.Data.find(t=>t.collection===e.collection&&t.category===e.category&&t.additionalKey===e.additionalKey&&t.type===e.type);if(!n||"0"===n.quantity)return null;const a=`${n.collection}|${n.category}|${n.additionalKey}|${n.type}`;return{quantity:n.quantity,collection:n.collection,category:n.category,tokenId:a}}catch(e){throw new Error(`Failed to fetch token balance from GalaChain: ${e.message||"Unknown error"}`)}}async transferGala(e){this.validateTransferGalaData(e);try{const t=$(e.recipientAddress),n=$(this.wallet.address),a=An.forGALA(n,t,e.amount,e.uniqueKey),r=await this.signatureHelper.signTransferToken(a.toSigningPayload()),s=new An({...a.toSigningPayload(),signedPayload:r});this.logger.debug("[DEBUG] Full GALA Transfer Request Payload:",JSON.stringify(s,null,2));const o=await this.http.post("/api/asset/token-contract/TransferToken",s);if(this.logger.debug("[DEBUG] Transfer response:",JSON.stringify(o,null,2)),o&&"object"==typeof o){if("Status"in o&&1===o.Status&&"Data"in o){const e=o;return Array.isArray(e.Data)&&e.Data.length>0?"gala-transfer-successful":"transfer-successful-no-id"}if("transactionId"in o&&o.transactionId)return o.transactionId}throw new Nn("Transfer succeeded but transaction ID could not be extracted",bn.NETWORK_ERROR)}catch(t){throw this.handleTransferError(t,"GALA transfer failed",e)}}async transferToken(e){this.validateTransferTokenData(e);try{const t=$(e.to),n=$(this.wallet.address);let a;if(e.tokenId)a=vn(e.tokenId),this.logger.debug("[DEBUG] Using provided tokenId:",e.tokenId),this.logger.debug("[DEBUG] Normalized Token Instance:",JSON.stringify(a,null,2));else{if(!e.tokenName)throw new Nn("Must provide either tokenId or tokenName for token identification",bn.TOKEN_NOT_FOUND);a=await this.resolveTokenInstance(e.tokenName)}const r=new An({from:n,to:t,quantity:e.amount,tokenInstance:a,uniqueKey:e.uniqueKey||`galaconnect-operation-${Date.now()}_${Math.random().toString(36).substring(2,8)}`}),s=await this.signatureHelper.signTransferToken(r.toSigningPayload()),o=new An({...r.toSigningPayload(),signedPayload:s});this.logger.debug("[DEBUG] Full Transfer Request Payload:",JSON.stringify(o,null,2));const i=await this.http.post("/api/asset/token-contract/TransferToken",o);if(this.logger.debug("[DEBUG] Token transfer response:",JSON.stringify(i,null,2)),i&&"object"==typeof i){if("Status"in i&&1===i.Status&&"Data"in i){const e=i;return Array.isArray(e.Data)&&e.Data.length>0?"token-transfer-successful":"transfer-successful-no-id"}if("transactionId"in i&&i.transactionId)return i.transactionId}throw new Nn("Transfer succeeded but transaction ID could not be extracted",bn.NETWORK_ERROR)}catch(t){throw this.handleTransferError(t,"Token transfer failed",e)}}async resolveTokenClassKey(e){try{const t=await this.tokenResolver.resolveTokenClassKey(e);return this.logger.debug(`[DEBUG] Token class key resolution for '${e}':`,JSON.stringify(t,null,2)),t}catch(t){if(t instanceof Nn)throw t;throw new Nn(`Failed to resolve token class key for '${e}': ${t instanceof Error?t.message:String(t)}`,bn.TOKEN_NOT_FOUND,{tokenName:e})}}validateTransferGalaData(e){if(!((t=e)&&"object"==typeof t&&"string"==typeof t.recipientAddress&&t.recipientAddress.trim().length>0&&"string"==typeof t.amount&&t.amount.trim().length>0)||void 0!==t.uniqueKey&&"string"!=typeof t.uniqueKey)throw new b("Invalid GALA transfer data: missing required fields");var t;if(!D(e.recipientAddress))throw new Nn("Invalid recipient address format",bn.INVALID_RECIPIENT,{recipientAddress:e.recipientAddress});if(parseFloat(e.amount)<=0)throw new Nn("Transfer amount must be positive",bn.INVALID_AMOUNT,{amount:e.amount});if(e.uniqueKey){if(e.uniqueKey.length>Tn.MAX_UNIQUE_KEY_LENGTH)throw new b(`Unique key too long. Maximum length: ${Tn.MAX_UNIQUE_KEY_LENGTH}`);if(!Tn.UNIQUE_KEY_PATTERN.test(e.uniqueKey))throw new Nn('Invalid unique key format. Must start with "galaswap-operation-" or "galaconnect-operation-"',bn.INVALID_AMOUNT,{uniqueKey:e.uniqueKey})}}validateTransferTokenData(e){if(!((t=e)&&"object"==typeof t&&"string"==typeof t.to&&t.to.trim().length>0&&"string"==typeof t.amount&&t.amount.trim().length>0&&(void 0!==t.tokenId||"string"==typeof t.tokenName&&t.tokenName.trim().length>0))||void 0!==t.uniqueKey&&"string"!=typeof t.uniqueKey)throw new b("Invalid token transfer data: missing required fields");var t;if(!D(e.to))throw new Nn("Invalid recipient address format",bn.INVALID_RECIPIENT,{recipientAddress:e.to});if(!e.tokenId&&!e.tokenName)throw new Nn("Must provide either tokenId or tokenName for token identification",bn.TOKEN_NOT_FOUND);if(e.tokenName&&!Tn.TOKEN_NAME_PATTERN.test(e.tokenName))throw new Nn("Invalid token name format",bn.TOKEN_NOT_FOUND,{tokenName:e.tokenName});if(parseFloat(e.amount)<=0)throw new Nn("Transfer amount must be positive",bn.INVALID_AMOUNT,{amount:e.amount});if(e.uniqueKey){if(e.uniqueKey.length>Tn.MAX_UNIQUE_KEY_LENGTH)throw new b(`Unique key too long. Maximum length: ${Tn.MAX_UNIQUE_KEY_LENGTH}`);if(!Tn.UNIQUE_KEY_PATTERN.test(e.uniqueKey))throw new Nn('Invalid unique key format. Must start with "galaswap-operation-" or "galaconnect-operation-"',bn.INVALID_AMOUNT,{uniqueKey:e.uniqueKey})}}async resolveTokenInstance(e){try{const t=await this.tokenResolver.resolveTokenToVault(e);if(!t)throw new Nn(`Token '${e}' not found or not available for transfer`,bn.TOKEN_NOT_FOUND,{tokenName:e});const n=this.resolveTokenInstanceFromVaultAddress(t);return this.logger.debug(`[DEBUG] Token resolution for '${e}':\n Vault Address: ${t}\n Token Instance: ${JSON.stringify(n,null,2)}`),n}catch(t){if(t instanceof Nn)throw t;throw new Nn(`Failed to resolve token '${e}': ${t instanceof Error?t.message:String(t)}`,bn.TOKEN_NOT_FOUND,{tokenName:e})}}resolveTokenInstanceFromVaultAddress(e){const[t,n]=e.split("|");if(!n)throw new Nn(`Invalid vault address format: missing token components. Address: ${e}`,bn.TOKEN_NOT_FOUND);const a=n.split("$");if(a.length<4)throw new Nn(`Invalid vault address format: insufficient token components. Expected 4+, got ${a.length}. Address: ${e}`,bn.TOKEN_NOT_FOUND);return{collection:a[0],category:a[1],type:a[2],additionalKey:a[3],instance:"0"}}handleTransferError(e,t,n){if(e instanceof Nn)return e;if(e instanceof b)return new Nn(e.message,bn.INVALID_AMOUNT);if(400===e.response?.status)return new Nn(e.response.data?.message||"Invalid transfer request",bn.INVALID_AMOUNT);if(403===e.response?.status)return new Nn("Insufficient balance for transfer",bn.INSUFFICIENT_BALANCE);if(404===e.response?.status){const e={};return"tokenName"in n&&(e.tokenName=n.tokenName),new Nn("Token not found",bn.TOKEN_NOT_FOUND,e)}return"ECONNABORTED"===e.code||"ETIMEDOUT"===e.code?new Nn("Transfer request timed out",bn.NETWORK_ERROR):new Nn(e.message||t,bn.NETWORK_ERROR)}}class Sn{constructor(e){this.http=e}async fetchTokenSpotPrice(e){if(!e||Array.isArray(e)&&0===e.length)throw new Error("At least one symbol must be provided");const t=Array.isArray(e)?e.join(","):e;try{const e=await this.http.request({method:"GET",url:"/v1/tokens",params:{symbols:t}}),n=[];return e.tokens&&Array.isArray(e.tokens)&&e.tokens.forEach(e=>{e.currentPrices&&e.symbol&&n.push({symbol:e.symbol,price:e.currentPrices.usd})}),n}catch(e){throw new Error(`Failed to fetch token prices: ${e instanceof Error?e.message:e}`)}}async fetchLaunchpadTokenSpotPrice(e,t){if(!e||"string"!=typeof e)throw new Error(E);try{const n=await t({tokenName:e,amount:"1",type:"native"}),a=(await this.fetchTokenSpotPrice("GALA"))[0];if(!a)throw new Error("GALA price not available");const r=Number(n.amount)/1e18;if(r<=0)throw new Error(`Invalid token amount calculation: ${r}`);const s=a.price/r;return{symbol:e.toUpperCase(),price:s}}catch(t){if(t instanceof Error)throw new Error(`Failed to calculate launchpad token spot price for ${e}: ${t.message}`);throw new Error(`Failed to calculate launchpad token spot price for ${e}: ${String(t)}`)}}}const Dn=8,$n=18,Fn={collection:"GALA",category:"Unit",type:"none",additionalKey:"none"};function Cn(e,t=18){const n=parseFloat(e);if(0===n)return"0";return n.toFixed(t).replace(/\.?0+$/,"")}function _n(e){return Cn(e,8)}function Pn(e){return Cn(e,18)}new w({debug:!1,context:"NumberUtils"});const Un={BuyNativeDto:class extends i{constructor(e,t,n="0",a={maxAcceptableReverseBondingCurveFee:"0"}){super(),this.vaultAddress=e,this.nativeTokenQuantity=_n(t),this.expectedToken=Pn(n),this.extraFees={maxAcceptableReverseBondingCurveFee:_n(a.maxAcceptableReverseBondingCurveFee)}}},BuyExactDto:class extends i{constructor(e,t,n,a={maxAcceptableReverseBondingCurveFee:"0"}){super(),this.vaultAddress=e,this.tokenQuantity=Pn(t),this.expectedNativeToken=_n(n),this.extraFees={maxAcceptableReverseBondingCurveFee:_n(a.maxAcceptableReverseBondingCurveFee)}}},SellExactDto:class extends i{constructor(e,t,n="0",a={maxAcceptableReverseBondingCurveFee:"0"}){super(),this.vaultAddress=e,this.tokenQuantity=Pn(t),this.expectedNativeToken=_n(n),this.extraFees={maxAcceptableReverseBondingCurveFee:_n(a.maxAcceptableReverseBondingCurveFee)}}},SellNativeDto:class extends i{constructor(e,t,n,a={maxAcceptableReverseBondingCurveFee:"0"}){super(),this.vaultAddress=e,this.nativeTokenQuantity=_n(t),this.expectedToken=Pn(n),this.extraFees={maxAcceptableReverseBondingCurveFee:_n(a.maxAcceptableReverseBondingCurveFee)}}}};var Ln,On,xn;!function(e){e[e.METAMASK=0]="METAMASK",e[e.TRUST_WALLET=1]="TRUST_WALLET",e[e.GALA_WALLET=2]="GALA_WALLET"}(Ln||(Ln={}));class Rn{constructor(e,t=!1){this.walletProvider=e,this.debug=t,this.logger=new w({debug:t,context:"SignatureService"})}async signDTO(e,t,n,a=Ln.METAMASK){try{this.logger.debug("๐Ÿ” Signing DTO:",{methodName:t,walletPreference:a,dtoKeys:Object.keys(e)});const r=this.generateEIP712Types(t,e),s=c(e),o={...e,prefix:s};let i,l,d;switch(a){case Ln.GALA_WALLET:({signature:i,domain:l}=await this.signWithGalaWallet(r,o,t,n));break;case Ln.TRUST_WALLET:i=await this.signWithTrustWallet(o),l={name:"ethereum",chainId:1};break;case Ln.METAMASK:default:({signature:i,domain:l}=await this.signWithMetaMask(r,o))}return d=a===Ln.TRUST_WALLET?{...o,signature:i}:{...e,signature:i,types:r,domain:l},this.logger.debug("โœ… DTO signed successfully:",{payloadKeys:Object.keys(d),signatureLength:i.length}),d}catch(e){throw this.logger.error("โŒ Signature generation failed:",e),new Error(`Failed to sign DTO: ${e.message}`)}}async signWithMetaMask(e,t){try{let n,a;if(this.walletProvider.signTypedData&&!this.walletProvider.getNetwork)n={name:"ethereum",chainId:1},a=await this.walletProvider.signTypedData(n,e,t);else{if(!this.walletProvider.getNetwork||!this.walletProvider.signTypedData)throw new Error("Wallet provider does not support typed data signing");{const r=await this.walletProvider.getNetwork();n={name:r.name,chainId:Number(r.chainId)},a=await this.walletProvider.signTypedData(n,e,t)}}return{signature:a,domain:n}}catch(e){throw new Error(`MetaMask/ethers signing failed: ${e.message}`)}}async signWithTrustWallet(e){try{const n=(t=e,JSON.stringify(t));let a;if(!this.walletProvider.signMessage)throw new Error("Wallet provider does not support signMessage");return a=await this.walletProvider.signMessage(n),a}catch(e){throw new Error(`TrustWallet signing failed: ${e.message}`)}var t}async signWithGalaWallet(e,t,n,a){try{const r={name:"ethereum",chainId:1};if("undefined"==typeof window)return this.logger.warn("โš ๏ธ GalaWallet not available in Node.js environment, falling back to ethers.js signing"),await this.signWithMetaMask(e,t);const s={domain:r,types:e,message:t,Primary_type:n},o=window;if(!o?.gala)throw new Error("GalaWallet not found in window object");await o.gala.setAddress(a);return{signature:await o.gala.request({method:"eth_signTypedData",params:[JSON.stringify(s),a]}),domain:r}}catch(e){throw new Error(`GalaWallet signing failed: ${e.message}`)}}generateEIP712Types(e,t){const n={};n[e]=[];const a=(e,t,r,s=!1)=>{if(void 0!==t){if(Array.isArray(t)){const o=a(e,t[0],r,!0);return s||n[r].push({name:e,type:(o??e)+"[]"}),o?o+"[]":void 0}if("object"==typeof t&&null!==t){if(n[e])throw new Error(`Type name collision not supported: ${e}`);return n[e]=[],Object.entries(t).forEach(([t,n])=>{a(t,n,e)}),s||n[r].push({name:e,type:e}),e}{let a;switch(typeof t){case"string":a="string";break;case"number":a="uint256";break;case"boolean":a="bool";break;default:throw new Error(`Unsupported type for fieldName ${e}: ${typeof t}, value: ${t}`)}return s||n[r].push({name:e,type:a}),a}}};return Object.entries(t).forEach(([t,n])=>{a(t,n,e)}),this.logger.debug("๐Ÿ“ Generated EIP-712 types:",n),n}detectWalletPreference(){if("undefined"==typeof window)return Ln.METAMASK;const e=window;return e?.gala?Ln.GALA_WALLET:e?.trustWallet?.isTrust?Ln.TRUST_WALLET:Ln.METAMASK}}class Bn{constructor(e=!1){this.debug=e,this.logger=new w({debug:e,context:"TokenClassKeyService"})}generateStringsInstructions(e){try{this.logger.debug("๐Ÿ”ง Generating stringsInstructions for:",e);const t=this.extractTokenSymbolFromVault(e),n=this.createTokenInstance(t),a=this.createGalaInstance(),r=`$service$${n.toStringKey()}$launchpad`,s=`$tokenBalance$${n.toStringKey()}$${e}`,o=`$tokenBalance$${n.toStringKey()}$${e}`,i=`$tokenBalance$${a.toStringKey()}$${e}`,c=[r,s,o,i,`$tokenBalance$${a.toStringKey()}$${e}`];return this.logger.debug("โœ… Generated stringsInstructions:",c),c}catch(e){throw this.logger.error("โŒ Failed to generate stringsInstructions:",e),new Error(`Failed to generate stringsInstructions: ${e.message}`)}}createTokenInstance(e){const t=new d;return t.collection=e.toLowerCase(),t.category="Unit",t.type="none",t.additionalKey="none",this.logger.debug("๐Ÿช™ Created token instance:",{symbol:e,lowercaseCollection:e.toLowerCase(),stringKey:t.toStringKey()}),t}createGalaInstance(){const e=new d;return e.collection="GALA",e.category="Unit",e.type="none",e.additionalKey="none",this.logger.debug("๐ŸŸก Created GALA instance:",{stringKey:e.toStringKey()}),e}extractTokenSymbolFromVault(e){if(!e||"string"!=typeof e)throw new Error("Vault address is required and must be a string");const t=e.split("$");if(t.length<3)throw new Error(`Invalid vault address format: ${e}`);const n=t[2];if(!n||0===n.trim().length)throw new Error(`Empty token symbol in vault address: ${e}`);return this.logger.debug("๐Ÿ” Extracted token symbol:",{vaultAddress:e,tokenSymbol:n,parts:t.slice(0,4)}),n}validateVaultAddress(e){if(!e||"string"!=typeof e)throw new Error("Vault address must be a non-empty string");if(!e.startsWith("service|Token$Unit$"))throw new Error('Vault address must start with "service|Token$Unit$"');if(!e.endsWith("$launchpad"))throw new Error('Vault address must end with "$launchpad"');const t=e.split("$");if(t.length<5)throw new Error('Vault address must have at least 5 parts separated by "$"');const n=t[2];if(!n||!/^[A-Za-z]{1,10}$/.test(n))throw new Error("Token symbol must be 1-10 letters (case insensitive)");return this.logger.debug("โœ… Vault address validation passed:",e),!0}generateTokenClassKeyString(e,t,n,a){return`${e}$${t}$${n}$${a}`}parseTokenClassKeyString(e){const t=e.split("$");if(4!==t.length)throw new Error(`Invalid TokenClassKey string format: ${e}`);return{collection:t[0],category:t[1],type:t[2],additionalKey:t[3]}}}function Mn(e,t,n){if(t<0||t>1)throw new Error(`Invalid slippage tolerance factor: ${t}. Must be between 0 and 1 (e.g., 0.05 for 5%)`);const a=new u(e);if(a.isNaN())throw new Error(`Invalid expected amount: ${e}. Must be a valid number`);if(0===t)return e;const r=a.multipliedBy(t);let s;switch(n){case"buy-native":case"sell-exact":s=a.minus(r);break;case"buy-exact":case"sell-native":s=a.plus(r);break;default:throw new Error(`Unknown operation type: ${n}`)}return s.isLessThan(0)&&(s=new u(0)),s.toFixed()}class Kn{constructor(e,t,n=!1,a,r,s=.05,o=.01){this.httpClient=e,this.tokenResolver=t,this.walletProvider=a,this.userAddress=r,this.defaultSlippageToleranceFactor=s,this.defaultMaxAcceptableReverseBondingCurveFeeSlippageFactor=o,this.bundleEndpoint="/bundle",this.logger=new w({debug:n,context:"BundleService"}),a&&r&&(this.signatureService=new Rn(a,n),this.tokenKeyService=new Bn(n))}async submitTransaction(e){try{this.logger.debug("๐Ÿ“ฆ Submitting bundle transaction:",{method:e.method,stringsInstructionsCount:e.stringsInstructions.length,signedDtoKeys:Object.keys(e.signedDto)}),this.validateBundleData(e);const t=this.formatBundleRequest(e);this.logger.debug("๐Ÿš€ Bundle request payload:",{...t,signedDto:"[REDACTED - Contains signature]"});const n=await this.httpClient.post(this.bundleEndpoint,t);return this.logger.debug("๐Ÿ“ฅ Bundle API response:",{success:n.success,hasData:!!n.data,error:n.error}),this.handleBundleResponse(n)}catch(e){return this.logger.error("โŒ Bundle transaction submission failed:",e),{success:!1,error:this.formatErrorMessage(e)}}}validateBundleData(e){if(!e)throw new Error("Bundle data is required");if(!e.signedDto)throw new Error("Signed DTO is required");if(!e.method||"string"!=typeof e.method)throw new Error("Method name is required");if(!Array.isArray(e.stringsInstructions))throw new Error("stringsInstructions must be an array");if(0===e.stringsInstructions.length)throw new Error("stringsInstructions cannot be empty");const t=["BuyWithNative","BuyExactToken","SellExactToken","SellWithNative"];if(!t.includes(e.method))throw new Error(`Unsupported method: ${e.method}. Supported: ${t.join(", ")}`);e.stringsInstructions.forEach((e,t)=>{if("string"!=typeof e||0===e.length)throw new Error(`stringsInstructions[${t}] must be a non-empty string`);if(!e.startsWith("$"))throw new Error(`stringsInstructions[${t}] must start with '$': ${e}`)}),this.logger.debug("โœ… Bundle data validation passed")}formatBundleRequest(e){return{signedDto:e.signedDto,stringsInstructions:e.stringsInstructions,method:e.method}}handleBundleResponse(e){if(e.data&&!1===e.error)return this.logger.debug("โœ… Bundle transaction successful:",e.data),{success:!0,data:e.data};const t=e.error||e.message||"Bundle transaction failed";return this.logger.debug("โŒ Bundle transaction failed:",t),{success:!1,error:t}}formatErrorMessage(e){return"string"==typeof e?e:e?.response?.data?.error?e.response.data.error:e?.response?.data?.message?e.response.data.message:e?.message?e.message:"Unknown bundle transaction error"}async getBundlerTransactionResult(e){try{if(!e||"string"!=typeof e)throw new Error("Transaction ID is required");this.logger.debug("๐Ÿ” Checking bundler transaction result:",e);const t=await this.httpClient.get(`${this.bundleEndpoint}?id=${e}`);return this.logger.debug("๐Ÿ“Š Bundler transaction result:",t),{success:!0,data:t}}catch(e){return this.logger.error("โŒ Failed to get bundler transaction result:",e),{success:!1,error:this.formatErrorMessage(e)}}}async cancelTransaction(e){try{if(!e||"string"!=typeof e)throw new Error("Transaction ID is required");this.logger.debug("๐Ÿšซ Cancelling transaction:",e);const t=await this.httpClient.delete(`${this.bundleEndpoint}/${e}`);return this.logger.debug("๐Ÿ—‘๏ธ Transaction cancellation response:",t),{success:!0,data:t}}catch(e){return this.logger.error("โŒ Failed to cancel transaction:",e),{success:!1,error:this.formatErrorMessage(e)}}}async getHealthStatus(){try{this.logger.debug("๐Ÿฅ Checking bundle service health");const e=await this.httpClient.get(`${this.bundleEndpoint}/health`);return this.logger.debug("๐Ÿ’š Bundle service health:",e),{success:!0,data:e}}catch(e){return this.logger.error("โŒ Bundle service health check failed:",e),{success:!1,error:this.formatErrorMessage(e)}}}async buyToken(e){this.ensureTradingServicesAvailable();const{tokenName:t,amount:n,type:a,expectedAmount:r,maxAcceptableReverseBondingCurveFee:s,maxAcceptableReverseBondingCurveFeeSlippageFactor:o,slippageToleranceFactor:i}=e,c=i??this.defaultSlippageToleranceFactor,l=o??this.defaultMaxAcceptableReverseBondingCurveFeeSlippageFactor;let d=s||"0";s&&(d=Mn(s,l,"buy-exact"),this.logger.debug("Reverse bonding curve fee slippage applied:",{baseFee:s,slippageFactor:l,adjustedMaxFee:d}));const u=await this.resolveTokenNameToVault(t);if(!u)throw on(t);if("native"===a){if(!r)throw new b("expectedAmount is required for native buy operations. Use getBuyTokenAmount() first to calculate expected tokens.","expectedAmount","EXPECTED_AMOUNT_REQUIRED");const e=Mn(r,c,"buy-native");this.logger.debug("BuyNative slippage applied:",{originalExpectedTokens:r,slippageFactor:c,adjustedMinTokens:e});const t=new Un.BuyNativeDto(u,n,e,{maxAcceptableReverseBondingCurveFee:d});return await this.executeBundleTransaction(t,"BuyWithNative",u)}{if(!r)throw new b("expectedAmount is required for exact buy operations. Use getBuyTokenAmount() first to calculate expected GALA cost.","expectedAmount","EXPECTED_AMOUNT_REQUIRED");const e=Mn(r,c,"buy-exact");this.logger.debug("BuyExact slippage applied:",{originalExpectedGalaCost:r,slippageFactor:c,adjustedMaxGalaCost:e});const t=new Un.BuyExactDto(u,n,e,{maxAcceptableReverseBondingCurveFee:d});return await this.executeBundleTransaction(t,"BuyExactToken",u)}}async sellToken(e){this.ensureTradingServicesAvailable();const{tokenName:t,amount:n,type:a,expectedAmount:r,maxAcceptableReverseBondingCurveFee:s,maxAcceptableReverseBondingCurveFeeSlippageFactor:o,slippageToleranceFactor:i}=e,c=i??this.defaultSlippageToleranceFactor,l=o??this.defaultMaxAcceptableReverseBondingCurveFeeSlippageFactor;let d=s||"0";s&&(d=Mn(s,l,"buy-exact"),this.logger.debug("Reverse bonding curve fee slippage applied:",{baseFee:s,slippageFactor:l,adjustedMaxFee:d}));const u=await this.resolveTokenNameToVault(t);if(!u)throw on(t);if("exact"===a){if(!r)throw new b("expectedAmount is required for exact sell operations. Use getSellTokenAmount() first to calculate expected GALA.","expectedAmount","EXPECTED_AMOUNT_REQUIRED");const e=Mn(r,c,"sell-exact");this.logger.debug("SellExact slippage applied:",{originalExpectedGala:r,slippageFactor:c,adjustedMinGala:e});const t=new Un.SellExactDto(u,n,e,{maxAcceptableReverseBondingCurveFee:s||"0"});return await this.executeBundleTransaction(t,"SellExactToken",u)}{if(!r)throw new b("expectedAmount is required for native sell operations. Use getSellTokenAmount() first to calculate tokens to sell.","expectedAmount","EXPECTED_AMOUNT_REQUIRED");const e=Mn(r,c,"sell-native");this.logger.debug("SellNative slippage applied:",{originalExpectedTokensToSell:r,slippageFactor:c,adjustedMaxTokensToSell:e});const t=new Un.SellNativeDto(u,n,e,{maxAcceptableReverseBondingCurveFee:s||"0"});return await this.executeBundleTransaction(t,"SellWithNative",u)}}ensureTradingServicesAvailable(){if(!this.signatureService||!this.tokenKeyService)throw new Error("Trading services not available. BundleService requires walletProvider and userAddress for trading operations.");if(!this.userAddress)throw new Error("User address is required for trading operations.")}async executeBundleTransaction(e,t,n){this.ensureTradingServicesAvailable();try{e.uniqueKey=`galaswap - operation - ${h()}-${Date.now()}-${this.userAddress}`;const a=await this.signatureService.signDTO(e,t,this.userAddress),r=this.tokenKeyService.generateStringsInstructions(n),s={stringsInstructions:r,method:t,signedDto:a};this.logger.debug("๐Ÿ“ฆ Bundle transaction data:",{method:t,stringsInstructions:r,dtoKeys:Object.keys(a)});const o=await this.submitTransaction(s);if(o.success&&o.data)return this.logger.debug("โœ… Bundle transaction submitted:",o.data),{success:!0,data:{transactionId:o.data,message:"Transaction submitted successfully. Monitor WebSocket for completion."}};throw new Error(o.error||"Bundle transaction failed")}catch(e){throw this.logger.error("โŒ Bundle transaction error:",e),e}}async resolveTokenNameToVault(e){return await this.tokenResolver.resolveTokenToVault(e)}}!function(e){e.PROCESSED="PROCESSED",e.COMPLETED="COMPLETED",e.SUCCESS="SUCCESS",e.FAILED="FAILED",e.ERROR="ERROR",e.PROCESSING="PROCESSING",e.PENDING="PENDING"}(On||(On={})),function(e){e.PENDING="pending",e.PROCESSING="processing",e.COMPLETED="completed",e.FAILED="failed",e.TIMEOUT="timeout"}(xn||(xn={}));const Gn={[On.PROCESSED]:xn.COMPLETED,[On.COMPLETED]:xn.COMPLETED,[On.SUCCESS]:xn.COMPLETED,[On.FAILED]:xn.FAILED,[On.ERROR]:xn.FAILED,[On.PROCESSING]:xn.PROCESSING,[On.PENDING]:xn.PENDING};class qn{constructor(e,t=!1){this.socket=null,this.listeners=new Map,this.timeouts=new Map,this.reconnectCount=0,this.hasOnAnyListener=!1,this.config={reconnectAttempts:5,reconnectDelay:2e3,timeout:3e5,...e},this.debug=t,this.logger=new w({debug:t,context:"WebSocketService"}),this.isSocketIOAvailable=this.checkSocketIOAvailability()}checkSocketIOAvailability(){try{return"function"==typeof g||(this.logger.warn('โš ๏ธ Socket.IO client not available. Install "socket.io-client" package.'),!1)}catch(e){return this.logger.warn("โš ๏ธ Socket.IO availability check failed:",e),!1}}async connect(){return new Promise((e,t)=>{try{if(!this.isSocketIOAvailable){const e=new Error('Socket.IO not available in current environment. Install "socket.io-client" package.');return this.logger.error("โŒ Socket.IO connection failed:",e.message),void t(e)}this.logger.debug("๐Ÿ”Œ Connecting to Socket.IO server:",this.config.url),this.socket=g(this.config.url,{transports:["websocket"],reconnection:!0,reconnectionAttempts:this.config.reconnectAttempts||5,reconnectionDelay:this.config.reconnectDelay||2e3}),this.socket.on("connect",()=>{this.logger.debug("โœ… Socket.IO connected successfully:",this.socket?.id),this.logger.debug("๐Ÿ“ก Connected to bundle backend WebSocket:",this.config.url),this.logger.debug("๐Ÿ”— Ready to monitor transaction updates"),this.reconnectCount=0,e()}),this.socket.on("connect_error",e=>{this.logger.error("โŒ Socket.IO connection error:",e),t(e)}),this.socket.on("disconnect",e=>{this.logger.debug(`๐Ÿ”Œ Socket.IO disconnected: ${e}`),this.handleReconnect()}),this.socket.on("error",e=>{this.logger.error("โŒ Socket.IO error:",e)}),this.debug&&(this.socket.onAny((e,...t)=>{this.logger.debug(`๐Ÿ“ก [WebSocket Event] "${e}":`,JSON.stringify(t,null,2))}),this.hasOnAnyListener=!0)}catch(e){t(e)}})}async monitorTransaction(e,t){this.listeners.set(e,t),this.logger.debug(`๐Ÿ“ก Starting to monitor transaction: ${e}`),this.logger.debug(`๐Ÿ“ก WebSocket connected: ${!!this.socket&&this.socket.connected}`);const n=setTimeout(()=>{if(this.listeners.has(e)){const n={transactionId:e,status:xn.TIMEOUT,message:"Transaction monitoring timeout - no response after 60 seconds",timestamp:Date.now()};this.logger.debug(`๐Ÿ“ก Transaction timeout for ${e}`),t(n),this.listeners.delete(e),this.timeouts.delete(e),this.socket?.off(e)}},6e4);if(this.timeouts.set(e,n),this.socket&&this.socket.connected)this.socket.off(e),this.logger.debug(`๐Ÿ“ก Listening for transaction updates: ${e}`),this.logger.debug(`๐Ÿ“ก WebSocket connection ID: ${this.socket.id}`),this.logger.debug(`๐Ÿ“ก WebSocket URL: ${this.config.url}`),this.socket.on(e,n=>{this.logger.debug(`๐Ÿ“ก Socket.IO transaction update for ${e}:`,JSON.stringify(n,null,2));const a=n?.status||n?.Status||n?.data?.status||n?.data?.Status;let r=n?.message||n?.Message||n?.data?.message||n?.data?.Message||n?.error||n?.data?.error;r&&"string"==typeof r||(r=a===On.FAILED||a===On.ERROR?"Transaction failed - check transaction details":a===On.COMPLETED||a===On.PROCESSED||a===On.SUCCESS?"Transaction completed successfully":a?`Transaction status: ${a}`:"Unknown transaction status");const s={transactionId:e,status:this.mapSocketStatus(a),message:r,timestamp:Date.now(),blockHash:n?.blockHash||n?.data?.blockHash,gasUsed:n?.gasUsed||n?.data?.gasUsed};if(this.logger.debug(`๐Ÿ“ก Mapped status for ${e}: ${a} -> ${s.status}`),this.logger.debug(`๐Ÿ“ก Final message: "${r}"`),t(s),s.status===xn.COMPLETED||s.status===xn.FAILED){this.listeners.delete(e);const t=this.timeouts.get(e);t&&(clearTimeout(t),this.timeouts.delete(e)),this.socket?.off(e),this.logger.debug(`๐Ÿ“ก Cleaned up listener for ${e} (${s.status})`)}});else{const n={transactionId:e,status:xn.FAILED,message:"WebSocket not connected - cannot monitor transaction",timestamp:Date.now()};t(n),this.listeners.delete(e),this.timeouts.delete(e)}}async waitForTransaction(e){return new Promise((t,n)=>{this.monitorTransaction(e,e=>{e.status===xn.COMPLETED?t(e):e.status!==xn.FAILED&&e.status!==xn.TIMEOUT||n(new Error(`Transaction ${e.status}: ${e.message}`))})})}mapSocketStatus(e){const t=e?.toUpperCase();return Gn[t]||xn.PENDING}async handleReconnect(){this.reconnectCount<this.config.reconnectAttempts?(this.reconnectCount++,this.logger.debug(`๐Ÿ”„ Attempting Socket.IO reconnect ${this.reconnectCount}/${this.config.reconnectAttempts}`),setTimeout(()=>{this.socket&&!this.socket.connected&&this.socket.connect()},this.config.reconnectDelay)):this.logger.error("โŒ Socket.IO max reconnection attempts reached")}disconnect(){this.socket&&(this.listeners.forEach((e,t)=>{this.socket?.off(t)}),this.listeners.clear(),this.timeouts.forEach(e=>{clearTimeout(e)}),this.timeouts.clear(),this.hasOnAnyListener&&(this.socket.offAny(),this.hasOnAnyListener=!1,this.logger.debug("๐Ÿงน Removed onAny debug listener")),this.socket.disconnect(),this.socket=null,this.logger.debug("๐Ÿ”Œ Socket.IO disconnected"))}isConnected(){return this.socket?.connected||!1}}class Vn{constructor(e){this.poolService=e,this.cache=new Map}async resolveTokenToVault(e){if(!S(e))throw new b("Token name is required and must be a non-empty string","tokenName","INVALID_TOKEN_NAME");const t=e.trim().toLowerCase(),n=this.get(t);if(n)return n;try{const n=await this.poolService.resolveTokenNameToVault(e);return n&&this.set(t,n),n}catch{return null}}async resolveTokenClassKey(e){const t=await this.resolveTokenToVault(e);if(!t)throw new Error(`Token '${e}' not found. Cannot resolve to TokenClassKey.`);return this.parseVaultAddressToTokenClassKey(t)}get(e){return this.cache.get(e.toLowerCase())||null}set(e,t){this.cache.set(e.toLowerCase(),t)}clear(){this.cache.clear()}getStats(){return{size:this.cache.size,keys:Array.from(this.cache.keys())}}preWarm(e){for(const{tokenName:t,vaultAddress:n}of e)this.set(t,n)}parseVaultAddressToTokenClassKey(e){const t=e.split("|");if(2!==t.length)throw new Error(`Invalid vault address format: ${e}. Expected format: service|Token$Unit$...$launchpad`);const n=t[1].split("$");if(n.length<4)throw new Error(`Invalid vault address format: ${e}. Expected at least 4 parts after service|`);return{collection:n[0],category:n[1],type:n[2],additionalKey:n[3]}}}function jn(e){const t=function(e){const t=lt(e);return t.success?[]:t.errors||["Unknown validation error"]}(e);if(t.length>0)throw new Error(`LaunchTokenData validation failed:\n${t.map(e=>`- ${e}`).join("\n")}`)}const Wn="/api/asset/launchpad-contract/CallNativeTokenIn",Hn="/api/asset/launchpad-contract/CallNativeTokenOut",Qn="/api/asset/launchpad-contract/CallMemeTokenIn",Xn="/api/asset/launchpad-contract/CallMemeTokenOut";class zn extends i{constructor(e){super(),this.tokenName=e.tokenName,this.tokenSymbol=e.tokenSymbol,this.tokenDescription=e.tokenDescription,this.tokenImage=e.tokenImage,this.preBuyQuantity=e.preBuyQuantity,this.websiteUrl=e.websiteUrl,this.telegramUrl=e.telegramUrl,this.twitterUrl=e.twitterUrl,this.tokenCategory=e.tokenCategory,this.tokenCollection=e.tokenCollection,this.uniqueKey=e.uniqueKey,e.reverseBondingCurveConfiguration&&(this.reverseBondingCurveConfiguration=e.reverseBondingCurveConfiguration)}}function Yn(e){return e&&"object"==typeof e&&"number"==typeof e.Status&&e.Data&&"object"==typeof e.Data&&"string"==typeof e.Data.calculatedQuantity&&e.Data.extraFees&&"object"==typeof e.Data.extraFees&&"string"==typeof e.Data.extraFees.reverseBondingCurve&&"string"==typeof e.Data.extraFees.transactionFees}const Jn={NATIVE:"native",EXACT:"exact"},Zn={RECENT:"recent",POPULAR:"popular"};class ea{constructor(e,t,n,a,r,s){this.http=e,this.tokenResolver=t,this.logger=n,this.bundleHttp=a,this.galaChainHttp=r,this.dexApiHttp=s}async uploadImageByTokenName(e){const{tokenName:t,options:n}=e;Dt(t);const a=`${t}.png`;mn(n.file,a,"image/png");try{const e=new FormData;if("undefined"!=typeof File&&n.file instanceof File)e.append("image",n.file);else{if(!Buffer.isBuffer(n.file))throw new Error("Unsupported file type");{const a=`${n.tokenName||t}.png`,r=new Blob([n.file],{type:"image/png"});e.append("image",r,a)}}const a=await this.http.request({method:"POST",url:`/launchpad/upload-image?tokenName=${encodeURIComponent(n.tokenName||t)}`,data:e,headers:{}});if(!0===a.error||200!==a.status||!a.data?.imageUrl)throw new Error(a.message||"Image upload failed - no URL returned");return a.data.imageUrl}catch(e){if(e instanceof Error&&e.message.includes("FormData"))throw new Error("File upload failed: FormData not supported in this environment. Ensure you have proper polyfills for Node.js environments.");throw e}}async fetchPoolsFromAPI(e){$t(e),e.tokenName&&Dt(e.tokenName);const t={page:e.page.toString(),limit:e.limit.toString()};void 0!==e.type&&(t.type=e.type),void 0!==e.tokenName&&(t.tokenName=e.tokenName),void 0!==e.search&&(t.search=e.search);const n=A(t),a=await this.http.get("/launchpad/fetch-pool",n);if(!0===a.error||200!==a.status||!a.data)throw new Error(a.message||"Failed to fetch pools");let r=[];return a.data.tokens?r=Array.isArray(a.data.tokens)?a.data.tokens.map(e=>({...e,createdAt:new Date(e.created_at||e.createdAt)})):[{...a.data.tokens,createdAt:new Date(a.data.tokens.created_at||a.data.tokens.createdAt)}]:a.data.pools&&Array.isArray(a.data.pools)&&(r=a.data.pools.map(e=>({...e,createdAt:new Date(e.created_at||e.createdAt)}))),{pools:r,page:a.data.page,limit:a.data.limit,total:a.data.total,totalPages:a.data.totalPages,hasNext:a.data.page<a.data.totalPages,hasPrevious:a.data.page>1}}async _getAmount(e){if(Ct(e),!this.galaChainHttp)throw new Error("GalaChain client not configured. Direct GalaChain calls require galaChainHttp client.");const{endpoint:t,body:n}=((e,t,n,a)=>{if("NATIVE"===e&&"IN"===t)return{endpoint:Wn,body:{vaultAddress:n,tokenQuantity:a,IsPreMint:!1}};if("NATIVE"===e&&"OUT"===t)return{endpoint:Hn,body:{vaultAddress:n,tokenQuantity:a,IsPreMint:!1}};if("MEME"===e&&"IN"===t)return{endpoint:Qn,body:{vaultAddress:n,nativeTokenQuantity:a,IsPreMint:!1}};if("MEME"===e&&"OUT"===t)return{endpoint:Xn,body:{vaultAddress:n,nativeTokenQuantity:a,IsPreMint:!1}};throw new Error(`Unsupported operation: ${e}-${t}`)})(e.type,e.method,e.vaultAddress,e.amount);try{const e=await this.galaChainHttp.post(t,n);if(!Yn(e))throw new Error("Malformed response data from GalaChain gateway");if(1!==e.Status)throw new Error(`GalaChain calculation failed with status ${e.Status}`);const{calculatedQuantity:a,extraFees:r}=e.Data;return{amount:a,reverseBondingCurveFee:r.reverseBondingCurve,transactionFee:r.transactionFees,gasFee:"1"}}catch(a){throw this.logger.error(`GalaChain ${e.type}-${e.method} operation failed:`,{endpoint:t,requestBody:n,error:a instanceof Error?a.message:a}),a}}async checkPool(e){Ft(e),e.tokenName&&Dt(e.tokenName);const t=A(e),n=await this.http.get("/launchpad/check-pool",t);if(!0===n.error||200!==n.status)throw new Error(n.message||"Failed to check pool");const a=n.data;return e.symbol?a?.isSymbolExist??!1:e.tokenName?a?.isNameExist??!1:a?.exists??!1}async fetchVolumeData(e){if(!Zt(e))throw new b("Invalid options provided. Expected { tokenName: string, from?: number, to?: number, resolution?: number }","options","INVALID_OPTIONS");const{tokenName:t,from:n,to:a,resolution:r}=e;if(Dt(t),!n||!a||!r)throw new b("Graph options (from, to, resolution) are required","options","MISSING_GRAPH_OPTIONS");const s={tokenName:t,from:n,to:a,resolution:r};_t(s);const o=A(s),i=await this.http.get("/launchpad/get-graph-data",o);if(!0===i.error||200!==i.status||!i.data)throw new Error(i.message||"Failed to fetch graph data");return{dataPoints:i.data}}async fetchPools(e={}){let t;"recent"===e.type?t="RECENT":"popular"===e.type&&(t="POPULAR");const n={page:e.page||1,limit:e.limit||10};return e.search&&(n.search=e.search),e.tokenName&&(n.tokenName=e.tokenName),t&&(n.type=t),this.fetchPoolsFromAPI(n)}async isTokenNameAvailable(e){try{return!await this.checkPool({tokenName:e})}catch{return!1}}async isTokenSymbolAvailable(e){try{return!await this.checkPool({symbol:e})}catch{return!1}}async calculateBuyAmount(e){if(!e||"object"!=typeof e)throw new b("Invalid options provided. Expected an options object.","options","INVALID_OPTIONS");const{tokenName:t,amount:n,type:a}=e;if(!t||"string"!=typeof t)throw new b("Token name is required and must be a string","tokenName","INVALID_TOKEN_NAME");if(!n||"string"!=typeof n)throw new b("Amount is required and must be a string","amount","INVALID_AMOUNT");const r=await this.tokenResolver.resolveTokenToVault(t);if(!r)throw new b(`Token "${t}" not found. Please verify the token name is correct.`,"tokenName","TOKEN_NOT_FOUND");if(a!==Jn.NATIVE&&a!==Jn.EXACT)throw new b('Type must be either "native" or "exact"',"type","INVALID_TYPE");return a===Jn.EXACT?this._getAmount({type:"NATIVE",method:"IN",vaultAddress:r,amount:n}):this._getAmount({type:"MEME",method:"OUT",vaultAddress:r,amount:n})}async calculateSellAmount(e){const{tokenName:t,amount:n,type:a}=e,r=await this.tokenResolver.resolveTokenToVault(t);if(!r)throw new b(`Token "${t}" not found. Please verify the token name is correct.`,"tokenName","TOKEN_NOT_FOUND");return a===Jn.EXACT?this._getAmount({type:"NATIVE",method:"OUT",vaultAddress:r,amount:n}):this._getAmount({type:"MEME",method:"IN",vaultAddress:r,amount:n})}async launchToken(e){if(!this.bundleHttp)throw new Error("Bundle backend client not configured. LaunchToken requires bundleHttp client.");jn(e);const t=e.preBuyQuantity||"0";if(isNaN(Number(t))||Number(t)<0)throw new Error("Pre-buy quantity must be a valid non-negative number string");if(e.reverseBondingCurveConfiguration){const{minFeePortion:t,maxFeePortion:n}=e.reverseBondingCurveConfiguration,a=Number(t),r=Number(n);if(isNaN(a)||isNaN(r)||a<=0||r<=0||a>=r)throw new Error("Reverse bonding curve configuration must have valid min/max fee portions with min < max")}let n="";if(e.tokenImage)if(e.tokenImage instanceof File||Buffer.isBuffer(e.tokenImage)){const t=await this.uploadImageByTokenName({tokenName:e.tokenName,options:{file:e.tokenImage,tokenName:e.tokenName}});if(!t)throw new Error("Image upload failed: No URL returned");n=t}else"string"==typeof e.tokenImage&&(n=e.tokenImage);const a=`galaswap - operation - ${h()}-${Date.now()}-${this.http.getAddress()}`,r={tokenName:e.tokenName.trim(),tokenSymbol:e.tokenSymbol.trim().toUpperCase(),tokenDescription:e.tokenDescription.trim(),tokenImage:n.trim(),preBuyQuantity:t.toString(),websiteUrl:e.websiteUrl||"",telegramUrl:e.telegramUrl||"",twitterUrl:e.twitterUrl||"",tokenCategory:e.tokenCategory||"Unit",tokenCollection:e.tokenCollection||"Token",uniqueKey:a};e.reverseBondingCurveConfiguration&&(r.reverseBondingCurveConfiguration={minFeePortion:e.reverseBondingCurveConfiguration.minFeePortion.toString(),maxFeePortion:e.reverseBondingCurveConfiguration.maxFeePortion.toString()});const o=new zn(r),i=await this.http.signWithGalaChain("CreateSale",o,s.SIGN_TYPED_DATA),{signature:c,types:l,domain:d,prefix:u}=i,g={tokenName:o.tokenName,tokenSymbol:o.tokenSymbol,tokenDescription:o.tokenDescription,tokenImage:o.tokenImage,preBuyQuantity:o.preBuyQuantity,websiteUrl:o.websiteUrl,telegramUrl:o.telegramUrl,twitterUrl:o.twitterUrl,tokenCategory:o.tokenCategory,tokenCollection:o.tokenCollection,uniqueKey:o.uniqueKey,signature:c,types:l,domain:d,...u&&{prefix:u},...o.reverseBondingCurveConfiguration&&{reverseBondingCurveConfiguration:o.reverseBondingCurveConfiguration}},p=`${e.tokenName.trim()}$Unit$none$none`,m="GALA$Unit$none$none";let f;if(parseFloat(t)>0){const e=`$service$${p}$launchpad`;f=[e,`$token$${p}$${e}`,`$tokenBalance$${p}$${e}`,`$tokenBalance$${p}$${e}`,`$tokenBalance$${m}$${e}`,`$tokenBalance$${m}$${e}`]}else{const e=`$service$${p}$launchpad`;f=[e,`$token$${p}$${e}`,`$tokenBalance$${p}$${e}`]}const y={signedDto:g,stringsInstructions:f,method:"CreateSale"},w=await this.bundleHttp.post("/bundle",y);if(w.error||!w.data)throw new Error(w.message||"Token launch failed");return w.data}async fetchTokenDistribution(e){if(!e)throw new Error(v);Dt(e);const t=await this.tokenResolver.resolveTokenToVault(e);if(!t)throw new Error(T(e));const n=encodeURIComponent(t),a=await this.http.get(`/holders/${n}`);if(!0===a.error||200!==a.status||!a.data)throw new Error(a.message||"Failed to fetch token distribution");return{holders:a.data.holders||[],totalSupply:a.data.totalSupply||"0",totalHolders:a.data.totalHolders||0,lastUpdated:new Date}}async fetchTokenBadges(e){if(!e)throw new Error(v);Dt(e);const t=await this.http.get("/launchpad/get-badge/",{tokenName:e});if(t.error||!t.data)throw new Error(t.message||"Failed to fetch token badges");return{volumeBadges:t.data.volumeBadge||[],engagementBadges:t.data.engagementBadge||[]}}async hasTokenBadgeByTokenName(e){const{tokenName:t,badgeType:n,badgeName:a}=e;try{const e=await this.fetchTokenBadges(t);if(!e)return!1;const r=("volume"===n?e.volumeBadges:e.engagementBadges).find(e=>e.badgeName===a);return r?.isActive||!1}catch{return!1}}async calculateInitialBuyAmount(e){if(!(t=e)||"object"!=typeof t||"string"!=typeof t.nativeTokenQuantity||void 0!==t.vaultAddress&&"string"!=typeof t.vaultAddress)throw new Error("Invalid pre-mint calculation data");var t;if(!this.galaChainHttp)throw new Error("GalaChain HTTP client not available. Please initialize SDK with galaChainBaseUrl.");try{const t={vaultAddress:"service|testToken",nativeTokenQuantity:e.nativeTokenQuantity,IsPreMint:!0},n=await this.galaChainHttp.post("/api/asset/launchpad-contract/CallMemeTokenOut",t);if(!Yn(n))throw new Error("Malformed response data from GalaChain gateway");if(1!==n.Status)throw new Error(`GalaChain calculation failed with status ${n.Status}`);const{calculatedQuantity:a,extraFees:r}=n.Data;return{amount:a,reverseBondingCurveFee:r.reverseBondingCurve,transactionFee:r.transactionFees,gasFee:"1"}}catch(e){if(e instanceof Error){const t=new Error(`Pre-mint calculation failed: ${e.message}`);throw e.stack&&(t.stack=e.stack),t}throw new Error(`Pre-mint calculation failed: ${String(e)}`)}}getAddress(){return this.http.getAddress()}formatAddressForBackend(e){return Pt(e)}validateTokenName(e){return Dt(e)}validatePagination(e){return $t(e)}async fetchTokenSpotPrice(e){if(!this.dexApiHttp)throw new Error("DEX API client not configured. Token price fetching requires dexApiHttp client.");if(!e||Array.isArray(e)&&0===e.length)throw new Error("At least one symbol must be provided");const t=Array.isArray(e)?e.join(","):e;try{const e=await this.dexApiHttp.request({method:"GET",url:"/v1/tokens",params:{symbols:t}}),n=[];return e.tokens&&Array.isArray(e.tokens)&&e.tokens.forEach(e=>{e.currentPrices&&e.symbol&&n.push({symbol:e.symbol,price:e.currentPrices.usd})}),n}catch(e){throw new Error(`Failed to fetch token prices: ${e instanceof Error?e.message:e}`)}}async fetchLaunchpadTokenSpotPrice(e){if(!e||"string"!=typeof e)throw new Error(E);try{const t=await this.calculateBuyAmount({tokenName:e,amount:"1",type:"native"}),n=(await this.fetchTokenSpotPrice("GALA"))[0];if(!n)throw new Error("GALA price not available");const a=Number(t.amount)/1e18;if(a<=0)throw new Error(`Invalid token amount calculation: ${a}`);const r=n.price/a;return{symbol:e.toUpperCase(),price:r}}catch(t){if(t instanceof Error)throw new Error(`Failed to calculate launchpad token spot price for ${e}: ${t.message}`);throw new Error(`Failed to calculate launchpad token spot price for ${e}: ${String(t)}`)}}}const ta={PROD:{launchpadBaseUrl:"https://lpad-backend-prod1.defi.gala.com",galaChainBaseUrl:"https://gateway-mainnet.galachain.com",bundleBaseUrl:"https://bundle-backend-prod1.defi.gala.com",webSocketUrl:"https://bundle-backend-prod1.defi.gala.com",dexApiBaseUrl:"https://dex-api-platform-dex-prod-gala.gala.com",launchpadFrontendUrl:"https://lpad-frontend-prod1.defi.gala.com"},STAGE:{launchpadBaseUrl:"https://lpad-backend-dev1.defi.gala.com",galaChainBaseUrl:"https://galachain-gateway-chain-platform-stage-chain-platform-eks.stage.galachain.com",bundleBaseUrl:"https://bundle-backend-dev1.defi.gala.com",webSocketUrl:"https://bundle-backend-dev1.defi.gala.com",dexApiBaseUrl:"https://dex-api-platform-dex-stage-gala.gala.com",launchpadFrontendUrl:"https://lpad-frontend-dev1.defi.gala.com"}};function na(e){return ta[e]}class aa extends Error{constructor(e,t){super(e),this.cause=t,this.name="WebSocketError"}}class ra extends aa{constructor(e,t){super(`WebSocket confirmation timeout for transaction ${e} after ${t}ms`),this.name="WebSocketTimeoutError"}}class sa extends Error{constructor(e,t,n){super(`Transaction ${e} failed with status: ${t}${n?` - ${n}`:""}`),this.transactionId=e,this.status=t,this.name="TransactionFailedError"}}function oa(e,t){if(!e)throw new aa(`Invalid WebSocket response received for transaction ${t}: response is null or undefined`);if("object"!=typeof e)throw new aa(`Invalid WebSocket response received for transaction ${t}: expected object, got ${typeof e}`);if(!Object.prototype.hasOwnProperty.call(e,"status")&&!Object.prototype.hasOwnProperty.call(e,"Status"))throw new aa(`Invalid WebSocket response received for transaction ${t}: missing status field`)}function ia(e,t,n,a){oa(e,t);const r=e?.data||{};if(!(s=r)||"object"!=typeof s||void 0!==s.inputQuantity&&"string"!=typeof s.inputQuantity||void 0!==s.outputQuantity&&"string"!=typeof s.outputQuantity||void 0!==s.totalFees&&"string"!=typeof s.totalFees||void 0!==s.vaultAddress&&"string"!=typeof s.vaultAddress)throw new aa(`Invalid trade data received for transaction ${t}`);var s;const o={transactionId:t,type:n,method:"native"===a.type?"native":"exact",inputAmount:r.inputQuantity||a.amount,outputAmount:r.outputQuantity||a.expectedAmount||"0",totalFees:r.totalFees||"0",tokenName:a.tokenName,vaultAddress:r.vaultAddress||"",blockHash:e.blockHash,gasUsed:e.gasUsed,timestamp:Date.now()};return void 0!==a.slippageToleranceFactor&&(o.slippageTolerance=a.slippageToleranceFactor),o}class ca{constructor(e){let t=null;t=e.env?na(e.env):e.baseUrl?.includes("prod")?na("PROD"):na("STAGE"),this.config={baseUrl:t.launchpadBaseUrl,galaChainBaseUrl:t.galaChainBaseUrl,bundleBaseUrl:t.bundleBaseUrl,webSocketUrl:t.webSocketUrl,dexApiBaseUrl:t.dexApiBaseUrl,launchpadFrontendUrl:t.launchpadFrontendUrl,timeout:3e4,debug:!1,...e},this.logger=new w({debug:this.config.debug??!1,context:"LaunchpadSDK"}),this.validateConfiguration(),this.slippageToleranceFactor=void 0===e.slippageToleranceFactor?ca.DEFAULT_SLIPPAGE_TOLERANCE_FACTOR:this.parseSlippageToleranceFactor(e.slippageToleranceFactor),this.maxAcceptableReverseBondingCurveFeeSlippageFactor=void 0===e.maxAcceptableReverseBondingCurveFeeSlippageFactor?ca.DEFAULT_MAX_ACCEPTABLE_REVERSE_BONDING_CURVE_FEE_SLIPPAGE_FACTOR:this.parseFeeSlippageFactor(e.maxAcceptableReverseBondingCurveFeeSlippageFactor),this.auth=new y({wallet:e.wallet,messagePrefix:"Create a GalaChain Wallet"}),this.http=new k(this.auth,this.config),this.galaChainHttp=new k(this.auth,{...this.config,baseUrl:this.config.galaChainBaseUrl}),this.bundleHttp=new k(this.auth,{...this.config,baseUrl:this.config.bundleBaseUrl}),this.dexApiHttp=new k(this.auth,{...this.config,baseUrl:this.config.dexApiBaseUrl}),this.launchpadService=new wn(this.http),this.tokenResolverService=new Vn(this.launchpadService.poolService),this.launchpadAPI=new ea(this.http,this.tokenResolverService,this.logger,this.bundleHttp,this.galaChainHttp,this.dexApiHttp),this.galaChainService=new In(this.galaChainHttp,e.wallet,this.tokenResolverService,e.debug||!1),this.dexService=new Sn(this.dexApiHttp),this.bundleService=new Kn(this.bundleHttp,this.tokenResolverService,this.config.debug||!1,e.wallet,this.getAddress(),this.slippageToleranceFactor,this.maxAcceptableReverseBondingCurveFeeSlippageFactor),this.websocketService=new qn({url:this.config.webSocketUrl},this.config.debug)}createOverrideSdk(e){if(!e||"string"!=typeof e)throw new Error("Invalid privateKey: must be a non-empty string");if(!e.match(/^0x[a-fA-F0-9]{64}$/))throw new Error('Invalid privateKey format: must be "0x" followed by 64 hexadecimal characters');const n=new t(e),a={...this.config,wallet:n};return new ca(a)}getAddress(){return this.auth.getAddress()}getEthereumAddress(){return this.config.wallet.address}getConfig(){const{wallet:e,...t}=this.config;return{...t,slippageToleranceFactor:this.slippageToleranceFactor,maxAcceptableReverseBondingCurveFeeSlippageFactor:this.maxAcceptableReverseBondingCurveFeeSlippageFactor}}getUrlByTokenName(e){const t=this.config.launchpadFrontendUrl;if(!t)throw new Error("launchpadFrontendUrl not configured in SDK");return`${t.replace(/\/$/,"")}/buy-sell/${e}`}async fetchPools(e){return this.launchpadService.fetchPools(e||{})}async fetchTokenDistribution(e){return this.launchpadService.fetchTokenDistribution(e)}async fetchTokenBadges(e){return this.launchpadService.fetchTokenBadges(e)}async fetchTokenSpotPrice(e){return this.dexService.fetchTokenSpotPrice(e)}async fetchGalaSpotPrice(){const e=await this.dexService.fetchTokenSpotPrice("GALA");if(0===e.length)throw new Error("Failed to fetch GALA price - no price data returned");const t=e.find(e=>"GALA"===e.symbol);if(!t)throw new Error("GALA price not found in response");return t}async fetchLaunchpadTokenSpotPrice(e){return this.launchpadAPI.fetchLaunchpadTokenSpotPrice(e)}async fetchPoolDetails(e){const t=await this.resolveVaultAddress(e);if(!t)throw new Error(T(e));return(await this.galaChainService.fetchPoolDetails({vaultAddress:t})).Data}async fetchVolumeData(e){return this.launchpadService.fetchVolumeData(e)}async fetchTrades(e){return this.launchpadService.fetchTrades(e)}async fetchGalaBalance(e){const{normalizeAddressInput:t}=await Promise.resolve().then(function(){return Ut}),n=t(e)||this.getAddress();return this.galaChainService.fetchGalaBalance({owner:n,collection:"GALA",category:"Unit",additionalKey:"none",type:"none",instance:"0"})}async fetchTokenBalance(e){const{normalizeAddressInput:t}=await Promise.resolve().then(function(){return Ut}),n=t(e.address);if(e.tokenId){const{normalizeToTokenInstanceKey:t}=await Promise.resolve().then(function(){return En}),a=t(e.tokenId),{collection:r,category:s,type:o,additionalKey:i}=a;return this.galaChainService.fetchTokenBalance({owner:n,collection:r,category:s,additionalKey:i,type:o,instance:"0"})}if(e.tokenName){const t=(await this.fetchTokensHeld({tokenName:e.tokenName,page:1,limit:1,...n&&{address:n}})).tokens[0];return t?{quantity:t.quantity,collection:t.collection||"Token",category:"Unit",tokenId:`${t.collection||"Token"}|Unit|${t.symbol}|none`,symbol:t.symbol,name:t.name}:null}throw new Error("Must provide tokenId or tokenName")}async fetchComments(e){return this.launchpadService.fetchComments(e)}async calculateBuyAmount(e){return this.launchpadAPI.calculateBuyAmount(e)}async calculateSellAmount(e){return this.launchpadAPI.calculateSellAmount(e)}async calculateInitialBuyAmount(e){const t={nativeTokenQuantity:e};return this.launchpadAPI.calculateInitialBuyAmount(t)}async buy(e){if(e.privateKey){const t=this.createOverrideSdk(e.privateKey),{privateKey:n,...a}=e;return t.buy(a)}await this.ensureWebSocketConnection();const t=await this.bundleService.buyToken(e),n=t.data?.transactionId;if(!n)throw new Error("No transaction ID returned from buy operation");return this.waitForConfirmation(n,t=>ia(t,n,"buy",e))}async sell(e){if(e.privateKey){const t=this.createOverrideSdk(e.privateKey),{privateKey:n,...a}=e;return t.sell(a)}await this.ensureWebSocketConnection();const t=await this.bundleService.sellToken(e),n=t.data?.transactionId;if(!n)throw new Error("No transaction ID returned from sell operation");return this.waitForConfirmation(n,t=>ia(t,n,"sell",e))}async getBundlerTransactionResult(e){return this.bundleService.getBundlerTransactionResult(e)}async postComment(e){if(e.privateKey){const t=this.createOverrideSdk(e.privateKey),{privateKey:n,...a}=e;return t.postComment(a)}return this.launchpadService.postComment(e)}async launchToken(e){if(e.privateKey){const t=this.createOverrideSdk(e.privateKey),{privateKey:n,...a}=e;return t.launchToken(a)}await this.ensureWebSocketConnection();const t=await this.launchpadAPI.launchToken(e);return this.waitForConfirmation(t,n=>{oa(n,t);const a=n?.data||{};if(!function(e){return!(!e||"object"!=typeof e||void 0!==e.vaultAddress&&"string"!=typeof e.vaultAddress||void 0!==e.tokenStringKey&&"string"!=typeof e.tokenStringKey||void 0!==e.creatorAddress&&"string"!=typeof e.creatorAddress)}(a))throw new aa(`Invalid launch data received for transaction ${t}`);const r={transactionId:t,vaultAddress:a.vaultAddress||"",tokenStringKey:a.tokenStringKey||"",tokenName:e.tokenName,tokenSymbol:e.tokenSymbol,creatorAddress:a.creatorAddress||this.getAddress(),blockHash:n.blockHash,gasUsed:n.gasUsed,timestamp:Date.now()};return"string"==typeof e.tokenImage&&(r.tokenImage=e.tokenImage),void 0!==e.preBuyQuantity&&(r.preBuyQuantity=e.preBuyQuantity),r.vaultAddress&&this.tokenResolverService.set(e.tokenName,r.vaultAddress),r})}async uploadTokenImage(e){if(e.privateKey){const t=this.createOverrideSdk(e.privateKey),{privateKey:n,...a}=e;return t.uploadTokenImage(a)}return this.launchpadService.uploadImageByTokenName(e)}async isTokenNameAvailable(e){return this.launchpadService.isTokenNameAvailable(e)}async isTokenSymbolAvailable(e){return this.launchpadService.isTokenSymbolAvailable(e)}async fetchProfile(e){const{normalizeAddressInput:t}=await Promise.resolve().then(function(){return Ut}),n=t(e)||this.getAddress();return this.launchpadService.fetchProfile(n)}async updateProfile(e){const{normalizeAddressInput:t}=await Promise.resolve().then(function(){return Ut}),n={...e,address:t(e.address)};if(n.privateKey){const e=this.createOverrideSdk(n.privateKey),{privateKey:t,...a}=n;return e.updateProfile(a)}return this.launchpadService.updateProfile(n)}async uploadProfileImage(e){const{normalizeAddressInput:t}=await Promise.resolve().then(function(){return Ut}),n={...e,address:t(e.address)||this.getAddress()};if(n.privateKey){const e=this.createOverrideSdk(n.privateKey),{privateKey:t,...a}=n;return e.uploadProfileImage(a)}return this.launchpadService.uploadProfileImage(n)}async retrieveGalaFromFaucet(e){const{normalizeAddressInput:t}=await Promise.resolve().then(function(){return Ut}),n={walletAddress:t(e)||this.getAddress(),amount:"5"};return this.launchpadService.transferFaucets(n)}async fetchTokensHeld(e){const{normalizeAddressInput:t}=await Promise.resolve().then(function(){return Ut}),n=t(e?.address)||this.getAddress(),a={page:e?.page||1,limit:e?.limit||10,address:n};return e?.tokenName&&(a.tokenName=e.tokenName),e?.search&&(a.search=e.search),this.launchpadService.fetchTokensHeld(a)}async fetchTokensCreated(e){const{normalizeAddressInput:t}=await Promise.resolve().then(function(){return Ut}),n={type:"DEFI",address:t(e?.address)||this.getAddress(),page:e?.page||1,limit:e?.limit||10};return e?.tokenName&&(n.tokenName=e.tokenName),e?.search&&(n.search=e.search),this.launchpadService.fetchTokenList(n)}async transferGala(e){const{normalizeAddressInput:t}=await Promise.resolve().then(function(){return Ut}),n={...e,recipientAddress:t(e.recipientAddress)};if(n.privateKey){const e=this.createOverrideSdk(n.privateKey),{privateKey:t,...a}=n;return e.transferGala(a)}return this.galaChainService.transferGala(n)}async transferToken(e){const{normalizeAddressInput:t}=await Promise.resolve().then(function(){return Ut}),n={...e,to:t(e.to)};if(n.privateKey){const e=this.createOverrideSdk(n.privateKey),{privateKey:t,...a}=n;return e.transferToken(a)}return this.galaChainService.transferToken(n)}async resolveTokenClassKey(e){return this.galaChainService.resolveTokenClassKey(e)}async resolveVaultAddress(e){return this.tokenResolverService.resolveTokenToVault(e)}validateConfiguration(){if(("number"!=typeof this.config.timeout||this.config.timeout<=0||this.config.timeout>3e5)&&(this.logger.warn(`Invalid timeout value: ${this.config.timeout}. Using default 30000ms.`),this.config.timeout=3e4),!this.config.baseUrl)throw new Error("baseUrl is required in configuration");if(!this.config.webSocketUrl)throw new Error("webSocketUrl is required in configuration");try{new URL(this.config.baseUrl)}catch{throw new Error(`Invalid baseUrl format: ${this.config.baseUrl}`)}try{new URL(this.config.webSocketUrl)}catch{throw new Error(`Invalid webSocketUrl format: ${this.config.webSocketUrl}`)}if(this.config.galaChainBaseUrl)try{new URL(this.config.galaChainBaseUrl)}catch{throw new Error(`Invalid galaChainBaseUrl format: ${this.config.galaChainBaseUrl}`)}if(this.config.bundleBaseUrl)try{new URL(this.config.bundleBaseUrl)}catch{throw new Error(`Invalid bundleBaseUrl format: ${this.config.bundleBaseUrl}`)}if(this.config.launchpadFrontendUrl)try{new URL(this.config.launchpadFrontendUrl)}catch{throw new Error(`Invalid launchpadFrontendUrl format: ${this.config.launchpadFrontendUrl}`)}}parseSlippageToleranceFactor(e){const t=parseFloat(e);return isNaN(t)||t<0||t>1?(this.logger.warn(`Invalid slippage tolerance factor: ${e}, using default: ${ca.DEFAULT_SLIPPAGE_TOLERANCE_FACTOR}`),ca.DEFAULT_SLIPPAGE_TOLERANCE_FACTOR):t}parseFeeSlippageFactor(e){const t=parseFloat(e);return isNaN(t)||t<0||t>1?(this.logger.warn(`Invalid fee slippage factor: ${e}, using default: ${ca.DEFAULT_MAX_ACCEPTABLE_REVERSE_BONDING_CURVE_FEE_SLIPPAGE_FACTOR}`),ca.DEFAULT_MAX_ACCEPTABLE_REVERSE_BONDING_CURVE_FEE_SLIPPAGE_FACTOR):t}async ensureWebSocketConnection(){this.websocketService.isConnected()||(await this.websocketService.connect(),this.logger.debug("WebSocket connection established"))}async waitForConfirmation(e,t){this.logger.debug(`Waiting for confirmation of transaction: ${e}`);try{const n=await this.websocketService.waitForTransaction(e);if("completed"!==n.status)throw new sa(e,n.status,n.message);let a;try{a=t(n)}catch(t){if(t instanceof aa)throw t;throw new aa(`Failed to transform WebSocket response for transaction ${e}`,t instanceof Error?t:new Error(String(t)))}return this.logger.debug(`Transaction confirmed: ${e}`,a),a}catch(t){if(this.logger.error(`Transaction confirmation failed: ${e}`,t),t instanceof sa||t instanceof aa)throw t;throw new aa(`WebSocket confirmation failed for transaction ${e}`,t instanceof Error?t:new Error(String(t)))}}async cleanup(){try{this.logger.debug("Starting cleanup..."),this.http.cleanup(),this.websocketService&&this.websocketService.disconnect(),this.logger.debug("Cleanup completed")}catch(e){this.logger.error("Error during cleanup:",e)}}static cleanupAll(e=!1){const t=new w({debug:e,context:"LaunchpadSDK"});t.debug("Starting global cleanup...");const{WebSocketService:n}=require("./services/WebSocketService");n.cleanupAll(e),t.debug("Global cleanup completed")}}ca.DEFAULT_SLIPPAGE_TOLERANCE_FACTOR=.15,ca.DEFAULT_MAX_ACCEPTABLE_REVERSE_BONDING_CURVE_FEE_SLIPPAGE_FACTOR=.01;class la{static generateWallet(){try{const e=t.createRandom();if(!e.mnemonic?.phrase)throw new Error("Failed to generate wallet with mnemonic phrase");const n=this.toGalaAddress(e.address);return{privateKey:e.privateKey,address:e.address,galaAddress:n,mnemonic:e.mnemonic.phrase,wallet:new t(e.privateKey)}}catch(e){if("undefined"!=typeof process&&"test"===process.env.NODE_ENV){const e=`test-wallet-${Date.now()}-${++this.testCounter}`,n="0x"+Buffer.from(e).toString("hex").padStart(64,"1").slice(0,64),a=new t(n),r=this.toGalaAddress(a.address);return{privateKey:a.privateKey,address:a.address,galaAddress:r,mnemonic:"test test test test test test test test test test test junk",wallet:a}}throw e}}static fromPrivateKey(e){const n=new t(e),a=this.toGalaAddress(n.address);return{privateKey:n.privateKey,address:n.address,galaAddress:a,mnemonic:"",wallet:n}}static fromMnemonic(e,r=0){try{const s=n.fromPhrase(e),o=a.fromMnemonic(s,`m/44'/60'/0'/0/${r}`),i=new t(o.privateKey),c=this.toGalaAddress(i.address);return{privateKey:i.privateKey,address:i.address,galaAddress:c,mnemonic:e,wallet:i}}catch(n){if("undefined"!=typeof process&&"test"===process.env.NODE_ENV){const n=`test-mnemonic-index-${r}-${e}`,a="0x"+Buffer.from(n).toString("hex").padStart(64,"1").slice(0,64),s=new t(a),o=this.toGalaAddress(s.address);return{privateKey:s.privateKey,address:s.address,galaAddress:o,mnemonic:e,wallet:s}}throw n}}static toGalaAddress(e){const t=e.replace(/^0x/i,"");if(!/^[a-fA-F0-9]{40}$/.test(t))throw new Error(`Invalid Ethereum address format: ${e}`);return`eth|${t}`}static toEthereumAddress(e){if(!e.startsWith("eth|"))throw new Error(`Invalid Gala address format: ${e}. Must start with 'eth|'`);const t=e.slice(4);if(!/^[a-fA-F0-9]{40}$/.test(t))throw new Error(`Invalid address in Gala format: ${e}`);return`0x${t}`}static isValidEthereumAddress(e){try{const t=e.replace(/^0x/i,"");return/^[a-fA-F0-9]{40}$/.test(t)}catch{return!1}}static isValidGalaAddress(e){try{if(!e.startsWith("eth|"))return!1;const t=e.slice(4);return/^[a-fA-F0-9]{40}$/.test(t)}catch{return!1}}static generateMultipleWallets(e=1){if(e<1||e>100)throw new Error("Count must be between 1 and 100");const t=[];if("undefined"!=typeof process&&"test"===process.env.NODE_ENV)for(let n=0;n<e;n++){const e=`test-multi-${n}-${Date.now()}-${++this.testCounter}`,a="0x"+Buffer.from(e).toString("hex").padStart(64,"1").slice(0,64);t.push(this.fromPrivateKey(a))}else for(let n=0;n<e;n++)t.push(this.generateWallet());return t}static getWalletSummary(e,t=!1){const n=["๐Ÿ” Wallet Information","โ•".repeat(50),`๐Ÿ“ Address: ${e.address}`,`๐ŸŽฎ Gala Address: ${e.galaAddress}`,`๐ŸŒฑ Mnemonic: ${e.mnemonic||"Not available"}`];return t?n.splice(3,0,`๐Ÿ”‘ Private Key: ${e.privateKey}`):n.splice(3,0,"๐Ÿ”‘ Private Key: [HIDDEN - use includeSensitive=true to show]"),n.push("โ•".repeat(50)),n.push("๐Ÿ’พ IMPORTANT: Save your mnemonic phrase securely!"),n.push("This is your backup to recover the wallet."),n.join("\n")}}function da(e){if(void 0===e)return la.generateWallet();const t=e.trim();if(!t)throw new Error("Input cannot be empty string");if(function(e){const t=e.replace(/^0x/i,"");return/^[a-fA-F0-9]{64}$/.test(t)}(t))return la.fromPrivateKey(t);if(function(e){const t=e.split(/\s+/).filter(e=>e.length>0);if(12!==t.length&&24!==t.length)return!1;return t.every(e=>/^[a-zA-Z]+$/.test(e))}(t))return la.fromMnemonic(t);throw new Error(`Unable to detect input format. Expected:\n- Private key: 64 hexadecimal characters (with or without 0x prefix)\n- Mnemonic: 12 or 24 space-separated words\nReceived: "${t.slice(0,50)}${t.length>50?"...":""}"`)}function ua(e){const{wallet:n,env:a,config:r={}}=e||{};let s;if(n)if("string"==typeof n){s=da(n).wallet}else{if(!(n instanceof t))throw new Error("Invalid wallet input. Expected string (private key or mnemonic) or Wallet instance.");s=n}else{s=da().wallet}const o={wallet:s,...a&&{env:a},debug:!1,timeout:3e4,...r};return new ca(o)}la.testCounter=0;class ha{static async quickSetup(e={}){const t=e.environment||this.detectEnvironment(),n=this.setupWallet(e.privateKey),a={wallet:n.wallet,baseUrl:e.baseUrl||this.getDefaultBaseUrl(t),timeout:e.timeout||this.getDefaultTimeout(t),debug:e.debug??"production"!==t,...this.getEnvironmentDefaults(t)},r=new ca(a),s={sdk:r,wallet:n,config:a};if(!1!==e.autoValidate){const e=await this.validateSetup(r,n);return{...s,validation:e}}return s}static async validateSetup(e,t){const n=[],a=[],r={canTrade:!1,canCreateTokens:!1,hasBalance:!1,connectionHealthy:!1};try{const t=await e.fetchGalaBalance(e.getAddress());if(r.connectionHealthy=!0,t&&t.quantity){const e=parseFloat(t.quantity);r.hasBalance=e>0,r.canTrade=e>=.1,r.canCreateTokens=e>=100,0===e?a.push("Wallet has zero GALA balance - cannot perform transactions"):e<.1?a.push("GALA balance too low for trading (minimum 0.1 GALA)"):e<100&&a.push("GALA balance too low for token creation (minimum 100 GALA)")}else n.push("Failed to fetch GALA balance: No balance returned")}catch(e){n.push(`Balance check error: ${e instanceof Error?e.message:String(e)}`)}try{const t=await e.fetchPools({type:"recent",page:1,limit:1});t.pools&&0!==t.pools.length||a.push("Pool listing not accessible - some features may be limited")}catch(e){a.push(`Pool access test failed: ${e instanceof Error?e.message:String(e)}`)}return{ready:0===n.length&&r.connectionHealthy,sdk:e,wallet:t||la.generateWallet(),issues:n,warnings:a,capabilities:r}}static getRecommendedConfig(e,t="general"){const n={environment:e,autoValidate:!0};switch(e){case"production":Object.assign(n,{debug:!1,timeout:3e4});break;case"development":Object.assign(n,{debug:!0,timeout:45e3});break;case"testing":Object.assign(n,{debug:!0,timeout:6e4})}switch(t){case"trading":n.timeout=1.5*(n.timeout||3e4);break;case"creation":n.timeout=2*(n.timeout||3e4);break;case"monitoring":n.timeout=.5*(n.timeout||3e4)}return n}static async multiWalletSetup(e,t="development"){const n={};for(const[a,r]of Object.entries(e)){const{sdk:e}=await this.quickSetup({environment:t,privateKey:r,agentId:`multi-wallet-${a}`,autoValidate:!1});n[a]=e}return n}static detectEnvironment(){const e=process.env.NODE_ENV?.toLowerCase();return"production"===e?"production":"test"===e||"testing"===e?"testing":"development"}static setupWallet(e){if(!e){const e=process.env.PRIVATE_KEY;return e?la.fromPrivateKey(e):la.generateWallet()}return"generate"===e?la.generateWallet():la.fromPrivateKey(e)}static getDefaultBaseUrl(e){return"production"===e?"https://lpad-backend-prod1.defi.gala.com":"https://lpad-backend-dev1.defi.gala.com"}static getDefaultTimeout(e){switch(e){case"production":default:return 3e4;case"development":return 45e3;case"testing":return 6e4}}static getEnvironmentDefaults(e){const t={};if("production"===e)t.bundleBaseUrl="https://bundle-backend-prod1.defi.gala.com",t.galaChainBaseUrl="https://galachain-gateway-chain-platform-prod-chain-platform-eks.prod.galachain.com";else t.bundleBaseUrl="https://bundle-backend-dev1.defi.gala.com",t.galaChainBaseUrl="https://galachain-gateway-chain-platform-stage-chain-platform-eks.stage.galachain.com";return t}}const ga="3.7.0";export{ha as AgentConfig,pn as FileValidationError,Dn as GALA_DECIMALS,Fn as GALA_TOKEN_CLASS_KEY,fe as IMAGE_EXTENSIONS,$n as LAUNCHPAD_TOKEN_DECIMALS,ca as LaunchpadSDK,Zn as POOL_TYPES,xn as SDKTransactionStatus,ga as SDK_VERSION,Jn as TRADING_TYPES,sa as TransactionFailedError,b as ValidationError,aa as WebSocketError,ra as WebSocketTimeoutError,O as addressFormatSchema,pe as amountMethodSchema,ge as amountTypeSchema,we as browserFileSchema,Ae as bufferFileSchema,Ge as buyTokensDataSchema,Xe as calculatePreMintDataSchema,he as checkPoolOptionsSchema,Z as commentMessageSchema,Ie as commentPaginationSchema,ua as createLaunchpadSDK,W as createLimitSchema,Ce as createPaginatedResultSchema,Ke as createTradeDataSchema,da as createWallet,x as ethereumAddressSchema,G as faucetAmountSchema,Le as fetchGalaBalanceOptionsSchema,ze as fetchPoolDetailsDataSchema,Re as fetchTokenBalanceOptionsSchema,z as fileSizeSchema,ye as fileUploadSchema,Y as filenameSchema,R as flexibleAddressSchema,ke as flexibleFileSchema,_n as formatGalaForDTO,Pn as formatLaunchpadTokenForDTO,L as fullNameSchema,Qe as getAmountOptionsSchema,Ve as getTradeOptionsSchema,me as graphDataOptionsSchema,ve as imageExtensionSchema,Ee as imageFilenameSchema,J as imageMimeTypeSchema,de as imageUploadOptionsSchema,ee as isoDateStringSchema,le as launchTokenDataSchema,K as nonNegativeDecimalStringSchema,V as optionalUrlSchema,j as pageNumberSchema,Fe as paginationResultMetaSchema,ue as poolFetchTypeSchema,Se as poolPaginationSchema,M as positiveDecimalStringSchema,ne as privateKeySchema,ce as reverseBondingCurveConfigSchema,Ye as reverseBondingCurveConfigurationSchema,U as searchQuerySchema,qe as sellTokensDataSchema,H as standardLimitSchema,Te as standardPaginationSchema,te as timestampSchema,oe as tokenCategorySchema,ie as tokenCollectionSchema,_ as tokenDescriptionSchema,Pe as tokenListOptionsSchema,F as tokenNameSchema,C as tokenSymbolSchema,se as tokenUrlsSchema,He as tradeCalculationMethodSchema,We as tradeCalculationTypeSchema,X as tradeLimitSchema,je as tradeListParamsSchema,Ne as tradePaginationSchema,$e as tradePaginationWithFiltersSchema,Me as tradeTypeBackendSchema,Be as tradeTypeSchema,ae as transactionIdSchema,Ue as transferFaucetsDataSchema,re as uniqueKeySchema,Oe as updateProfileDataSchema,xe as uploadProfileImageOptionsSchema,q as urlSchema,Q as userLimitSchema,be as userPaginationSchema,P as userTokenNameSchema,_e as userTokenTypeSchema,De as userTokensPaginationSchema,nt as validateAddress,rt as validateAmountString,kt as validateBuyTokensData,Nt as validateCalculatePreMintData,ht as validateCheckPoolOptions,At as validateCreateTradeData,st as validateFaucetAmount,mt as validateFetchGalaBalanceOptions,It as validateFetchPoolDetailsData,wt as validateFetchTokenBalanceOptions,ot as validateFullName,bt as validateGetAmountOptions,Et as validateGetTradeOptions,ut as validateImageUploadOptions,lt as validateLaunchTokenData,it as validateSearchQuery,vt as validateSellTokensData,tt as validateTokenDescription,gt as validateTokenListOptions,Ze as validateTokenName,et as validateTokenSymbol,dt as validateTokenUrls,Tt as validateTradeListParams,pt as validateTransferFaucetsData,ft as validateUpdateProfileData,yt as validateUploadProfileImageOptions,ct as validateUserTokenName,at as validateVaultAddress,B as vaultAddressSchema};
1
+ import{ethers as e,Wallet as t,Mnemonic as a,HDNodeWallet as n}from"ethers";import r from"axios";import{SigningType as s,SigningClient as o,ChainCallDTO as i,calculatePersonalSignPrefix as c}from"@gala-chain/connect";import{z as l}from"zod";import{TokenClassKey as d}from"@gala-chain/api";import u from"bignumber.js";import{v4 as h}from"uuid";import{io as g}from"socket.io-client";var p,m;!function(e){e.WALLET_NOT_CONNECTED="WALLET_NOT_CONNECTED",e.SIGNATURE_FAILED="SIGNATURE_FAILED",e.INVALID_ADDRESS="INVALID_ADDRESS",e.MESSAGE_GENERATION_FAILED="MESSAGE_GENERATION_FAILED"}(p||(p={}));class f extends Error{constructor(e,t,a){super(t),this.type=e,this.originalError=a,this.name="AuthError"}}class y{constructor(e){if(this.wallet=e.wallet,this.messagePrefix=e.messagePrefix||"Create a GalaChain Wallet",""===e.messagePrefix)throw new f(p.SIGNATURE_FAILED,"Message prefix cannot be empty");this.validateWallet()}async generateSignature(){try{const e=Date.now(),t=`${this.messagePrefix} ${e}`,a=await this.wallet.signMessage(t);return{message:t,signature:a,address:this.formatAddress(this.wallet.address),timestamp:e}}catch(e){throw new f(p.SIGNATURE_FAILED,"Failed to generate signature for authentication",e instanceof Error?e:new Error(String(e)))}}getAddress(){return this.formatAddress(this.wallet.address)}getEthereumAddress(){return this.wallet.address}formatAddress(e){const t=e.replace(/^0x/i,"");if(!/^[a-fA-F0-9]{40}$/.test(t))throw new f(p.INVALID_ADDRESS,`Invalid Ethereum address format: ${e}`);return`eth|${t}`}async signMessage(e){try{return{message:e,signature:await this.wallet.signMessage(e),address:this.wallet.address,timestamp:Date.now()}}catch(e){const t=e instanceof Error?e.message:String(e);throw new f(p.SIGNATURE_FAILED,t,e instanceof Error?e:new Error(String(e)))}}async generateAuthHeaders(e,t){try{const a=Date.now(),n=`${this.messagePrefix} ${t.toUpperCase()} ${e} ${a}`,r=await this.wallet.signMessage(n);return{"x-signature":r,"x-address":this.formatAddress(this.wallet.address),"x-message":n,"x-timestamp":a.toString()}}catch(e){throw new f(p.SIGNATURE_FAILED,"Failed to generate authentication headers",e instanceof Error?e:new Error(String(e)))}}async signTypedData(e,t,a){try{return await this.wallet.signTypedData(e,t,a)}catch(e){throw new f(p.SIGNATURE_FAILED,"Failed to sign typed data",e instanceof Error?e:new Error(String(e)))}}async generateCustomSignature(e){if(!e||"string"!=typeof e||0===e.trim().length)throw new f(p.SIGNATURE_FAILED,"Custom message must be a non-empty string");try{const t=await this.wallet.signMessage(e);return{message:e,signature:t,address:this.formatAddress(this.wallet.address),timestamp:Date.now()}}catch(e){throw new f(p.SIGNATURE_FAILED,"Failed to generate custom message signature",e instanceof Error?e:new Error(String(e)))}}validateWallet(){if(!this.wallet)throw new f(p.WALLET_NOT_CONNECTED,"Wallet is required for authentication");if(!this.wallet.address)throw new f(p.WALLET_NOT_CONNECTED,"Wallet address is not available");if(!this.wallet.privateKey&&!this.wallet.signMessage)throw new f(p.WALLET_NOT_CONNECTED,"Wallet must have a private key for signing messages")}}!function(e){e.DEBUG="DEBUG",e.INFO="INFO",e.WARN="WARN",e.ERROR="ERROR"}(m||(m={}));class A{constructor(e){this.levelPriority={[m.DEBUG]:0,[m.INFO]:1,[m.WARN]:2,[m.ERROR]:3},this.debugEnabled=e.debug,this.context=e.context||"SDK",this.minLevel=e.minLevel||(e.debug?m.DEBUG:m.INFO)}debug(e,t){this.log(m.DEBUG,e,t)}info(e,t){this.log(m.INFO,e,t)}warn(e,t){this.log(m.WARN,e,t)}error(e,t){this.log(m.ERROR,e,t)}log(e,t,a){if(this.levelPriority[e]<this.levelPriority[this.minLevel])return;if(e===m.DEBUG&&!this.debugEnabled)return;const n=`[${(new Date).toISOString()}] [${this.context}] [${e}]`,r=this.getConsoleMethod(e);void 0!==a?a instanceof Error?r(`${n} ${t}`,a.message,a.stack):r(`${n} ${t}`,a):r(`${n} ${t}`)}getConsoleMethod(e){switch(e){case m.DEBUG:return console.debug;case m.INFO:return console.info;case m.WARN:return console.warn;case m.ERROR:return console.error;default:return console.log}}child(e){return new A({debug:this.debugEnabled,context:`${this.context}:${e}`,minLevel:this.minLevel})}isDebugEnabled(){return this.debugEnabled&&this.levelPriority[m.DEBUG]>=this.levelPriority[this.minLevel]}}function w(e){const t={};for(const[a,n]of Object.entries(e))null!=n&&("string"==typeof n?t[a]=n:"number"==typeof n||"boolean"==typeof n?t[a]=n.toString():Array.isArray(n)?t[a]=n.join(","):t[a]="object"==typeof n?JSON.stringify(n):String(n));return t}class k{constructor(e,t={}){this.auth=e,this.debug=t.debug??!1,this.logger=new A({debug:this.debug,context:"HttpClient"}),this.axios=r.create({baseURL:t.baseUrl||"https://lpad-backend-dev1.defi.gala.com",timeout:t.timeout||3e4,headers:{Accept:"application/json",...t.headers}}),this.setupInterceptors()}async request(e){try{const t={method:e.method,url:e.url,data:e.data,...e.params&&{params:w(e.params)},...e.headers&&{headers:e.headers},...e.timeout&&{timeout:e.timeout}};e.data instanceof FormData&&(t.headers&&t.headers["Content-Type"]&&delete t.headers["Content-Type"],this.logger.debug("FormData detected - removing Content-Type header for multipart upload"));const a=e.data instanceof FormData?"[FormData object - multipart/form-data]":e.data;this.logger.debug("Request:",{method:e.method,url:e.url,fullUrl:`${this.axios.defaults.baseURL}${e.url}`,baseURL:this.axios.defaults.baseURL,params:t.params,data:a,isFormData:e.data instanceof FormData,contentType:t.headers?.["Content-Type"]||"not set"});const n=await this.axios.request(t);return this.logger.debug("Response:",{status:n.status,data:n.data}),n.data}catch(e){throw this.logger.error("Error:",e),e}}async get(e,t,a){return this.request({method:"GET",url:e,...t&&{params:t},...a&&{headers:a}})}async post(e,t,a){return this.request({method:"POST",url:e,data:t,...a&&{headers:a}})}async put(e,t,a){return this.request({method:"PUT",url:e,data:t,...a&&{headers:a}})}async delete(e,t,a){return this.request({method:"DELETE",url:e,...t&&{params:t},...a&&{headers:a}})}async patch(e,t,a){return this.request({method:"PATCH",url:e,data:t,...a&&{headers:a}})}getAddress(){return this.auth.getAddress()}getEthereumAddress(){return this.auth.getEthereumAddress()}async signMessage(e){return(await this.auth.signMessage(e)).signature}async signTypedData(e,t,a){return await this.auth.signTypedData(e,t,a)}async signCustomMessage(e){try{const t=await this.auth.generateCustomSignature(e);return this.logger.debug("Generated custom signature:",{message:e,address:t.address,ethereumAddress:this.auth.getEthereumAddress()}),{signature:t.signature,address:t.address,ethereumAddress:this.auth.getEthereumAddress()}}catch(e){throw this.logger.error("Custom signature generation failed:",e),new Error(`Failed to generate custom signature for message: ${e instanceof Error?e.message:"Unknown error"}`)}}async signWithGalaChain(e,t,a=s.SIGN_TYPED_DATA){const n=this.auth.wallet.privateKey;if(!n)throw new Error("Wallet private key not available for @gala-chain signing");const r=new o(n);return await r.sign(e,t,a)}setupInterceptors(){this.requestInterceptorId=this.axios.interceptors.request.use(async e=>{try{const t=await this.auth.generateSignature();return e.headers||(e.headers={}),e.headers.Sign=t.signature,e.data instanceof FormData||(e.headers["Content-Type"]="application/json"),this.logger.debug("Added signature header:",{address:t.address,message:t.message,timestamp:t.timestamp}),e}catch(e){throw this.logger.error("Failed to add signature:",e),e}},e=>Promise.reject(e)),this.responseInterceptorId=this.axios.interceptors.response.use(e=>e,e=>{if(e.response){const t={message:e.response.data?.message||e.message,error:e.response.data?.error,statusCode:e.response.status,details:e.response.data?.details,timestamp:e.response.data?.timestamp,path:e.response.data?.path};e.launchpadError=t,this.logger.error("Backend error:",t)}else e.request?this.logger.error("Network error:",e.message):this.logger.error("Request setup error:",e.message);return Promise.reject(e)})}cleanup(){void 0!==this.requestInterceptorId&&(this.axios.interceptors.request.eject(this.requestInterceptorId),this.requestInterceptorId=void 0),void 0!==this.responseInterceptorId&&(this.axios.interceptors.response.eject(this.responseInterceptorId),this.responseInterceptorId=void 0),this.logger.debug("Interceptors cleaned up")}}const v="Token name is required and must be a string",T=e=>`Could not find vault address for token: ${e}`;class b extends Error{constructor(e,t,a){super(e),this.field=t,this.code=a,this.name="ValidationError"}}class E extends Error{constructor(e,t,a){super(e),this.statusCode=t,this.originalError=a,this.name="NetworkError"}}class N extends Error{constructor(e,t){super(e),this.field=t,this.name="ConfigurationError"}}class I extends Error{constructor(e,t,a){super(e),this.transactionId=t,this.code=a,this.name="TransactionError"}}function S(e,t,a){const{MIN_PAGE:n,MAX_PAGE:r,MIN_LIMIT:s,MAX_LIMIT:o}=a.PAGINATION;if("number"!=typeof e||e<n||e>r)throw new b(`Page must be a number between ${n} and ${r}`,"page","INVALID_PAGE");if("number"!=typeof t||t<s||t>o)throw new b(`Limit must be a number between ${s} and ${o}`,"limit","INVALID_LIMIT")}const D={ETH_ADDRESS:/^0x[0-9a-fA-F]{40}$/,BACKEND_ADDRESS:/^eth\|[0-9a-fA-F]{40}$/};function F(e){return"string"==typeof e&&e.trim().length>0}function $(e){return!(!e||"string"!=typeof e)&&(D.ETH_ADDRESS.test(e)||D.BACKEND_ADDRESS.test(e))}function C(e){return e.startsWith("0x")?`eth|${e.slice(2)}`:e}const _=l.string().min(3,"Token name must be at least 3 characters").max(20,"Token name must be at most 20 characters").regex(/^[a-zA-Z0-9]{3,20}$/,"Token name can only contain letters and numbers"),U=l.string().min(1,"Token symbol must be at least 1 character").max(8,"Token symbol must be at most 8 characters").regex(/^[A-Z]{1,8}$/,"Token symbol must be uppercase letters only"),P=l.string().min(1,"Token description is required").max(500,"Token description must be at most 500 characters"),L=l.string().min(1,"Token name must be at least 1 character").max(50,"Token name must be at most 50 characters"),O=l.string().min(1,"Search query must be at least 1 character").max(100,"Search query must be at most 100 characters"),x=l.string().min(1,"Full name is required").max(100,"Full name must be at most 100 characters").regex(/^[a-zA-Z\s]+$/,"Full name can only contain letters and spaces"),R=l.string().regex(D.BACKEND_ADDRESS,"Address must be in format: eth|[40-hex-chars]"),B=l.string().regex(D.ETH_ADDRESS,"Invalid Ethereum address format"),M=l.string().refine(e=>D.BACKEND_ADDRESS.test(e)||D.ETH_ADDRESS.test(e),"Address must be either eth|[40-hex-chars] or 0x[40-hex-chars] format").transform(e=>e.startsWith("0x")?`eth|${e.slice(2)}`:e),K=l.string().refine(e=>D.BACKEND_ADDRESS.test(e)||/^service\|Token\$Unit\$[A-Z0-9]+\$eth:[0-9a-fA-F]{40}\$launchpad$/.test(e),"Invalid vault address format"),G=l.string().regex(/^\d+(\.\d+)?$/,"Must be a valid decimal number").refine(e=>parseFloat(e)>0,"Amount must be greater than zero"),V=l.string().regex(/^\d+(\.\d+)?$/,"Must be a valid decimal number").refine(e=>parseFloat(e)>=0,"Amount must be zero or greater"),j=l.string().regex(/^(?!0+(\.0+)?$)\d+(\.\d+)?$/,"Amount must be a positive, non-zero number"),q=l.string().url("Must be a valid URL").regex(/^https?:\/\//,"URL must start with http:// or https://"),W=l.string().optional().refine(e=>!e||/^https?:\/\/.+\..+/.test(e),"Must be a valid URL if provided"),H=l.number().int("Page must be an integer").min(1,"Page must be at least 1").max(1e3,"Page must be at most 1000").default(1);function Q(e=100){return l.number().int("Limit must be an integer").min(1,"Limit must be at least 1").max(e,`Limit must be at most ${e}`).default(10)}const X=Q(100),z=Q(20),Y=Q(20),J=l.number().int("File size must be an integer").min(1,"File must be at least 1 byte").max(10485760,"File must be at most 10MB"),Z=l.string().max(255,"Filename must be at most 255 characters"),ee=l.enum(["image/png","image/jpg","image/jpeg","image/gif","image/webp","image/svg+xml"]),te=l.string().min(1,"Comment message is required").max(500,"Comment must be at most 500 characters"),ae=l.string().datetime("Must be a valid ISO 8601 date string"),ne=l.number().int("Timestamp must be an integer").min(0,"Timestamp must be non-negative"),re=l.string().regex(/^0x[a-fA-F0-9]{64}$/,"Private key must be format: 0x + 64 hex characters"),se=l.string().regex(/^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/,"Transaction ID must be in UUID format"),oe=l.string().regex(/^galaconnect-operation-[a-z0-9-]+$/,"Unique key must be format: galaconnect-operation-{unique-id}"),ie=l.object({websiteUrl:W,telegramUrl:W,twitterUrl:W}).refine(e=>e.websiteUrl||e.telegramUrl||e.twitterUrl,"At least one social URL (website, telegram, or twitter) is required"),ce=l.string().min(1,"Token category must not be empty").default("Unit"),le=l.string().min(1,"Token collection must not be empty").default("Token"),de=l.object({minFeePortion:G,maxFeePortion:G}),ue=l.object({tokenName:_,tokenSymbol:U,tokenDescription:P,tokenImage:l.union([l.instanceof(File),l.instanceof(Buffer),l.string().url("Token image must be a valid URL")]).optional(),preBuyQuantity:V.default("0"),websiteUrl:W,telegramUrl:W,twitterUrl:W,tokenCategory:ce,tokenCollection:le,reverseBondingCurveConfiguration:de.optional(),privateKey:re.optional()}),he=l.object({file:l.union([l.instanceof(File),l.instanceof(Buffer)]),tokenName:_}),ge=l.enum(["recent","popular"]),pe=l.object({tokenName:_.optional(),symbol:U.optional()}).refine(e=>e.tokenName||e.symbol,"At least one of tokenName or symbol is required"),me=l.enum(["NATIVE","MEME"]),fe=l.enum(["IN","OUT"]),ye=l.object({from:l.number().int("From timestamp must be an integer").min(173e6,"From timestamp must be at least 173000000"),to:l.number().int("To timestamp must be an integer").min(173e6,"To timestamp must be at least 173000000"),resolution:l.number().int("Resolution must be an integer").min(1,"Resolution must be at least 1"),tokenName:_}),Ae=l.object({tokenName:_,slippageToleranceFactor:l.number().min(0).max(1).optional(),maxAcceptableReverseBondingCurveFeeSlippageFactor:l.number().min(0).max(1).optional(),privateKey:re.optional()}),we=[".png",".jpg",".jpeg",".gif",".webp",".svg"],ke=l.object({file:l.union([l.instanceof(File),l.instanceof(Buffer)]),name:Z,size:J,type:ee}),ve=l.instanceof(File).refine(e=>e.size>=1&&e.size<=10485760,"File size must be between 1 byte and 10MB").refine(e=>["image/png","image/jpg","image/jpeg","image/gif","image/webp","image/svg+xml"].includes(e.type),"File must be a valid image type (PNG, JPG, JPEG, GIF, WebP, or SVG)").refine(e=>e.name.length<=255,"Filename must be at most 255 characters"),Te=l.instanceof(Buffer).refine(e=>e.length>=1&&e.length<=10485760,"Buffer size must be between 1 byte and 10MB"),be=l.union([ve,Te]),Ee=l.enum([".png",".jpg",".jpeg",".gif",".webp",".svg"]),Ne=Z.refine(e=>{const t=e.slice(e.lastIndexOf(".")).toLowerCase();return we.includes(t)},`Filename must end with one of: ${we.join(", ")}`),Ie=l.object({page:H,limit:X}),Se=l.object({page:H,limit:z}),De=l.object({page:H,limit:Y}),Fe=l.object({page:H,limit:Q(50)}),$e=Ie.extend({type:l.enum(["recent","popular"]).optional(),tokenName:l.string().min(1).max(50).optional(),search:l.string().min(1).max(100).optional()}),Ce=Se.extend({tokenName:l.string().min(1).max(50).optional(),search:l.string().min(1).max(100).optional()}),_e=De.extend({tradeType:l.enum(["BUY","SELL"]).optional(),tokenName:l.string().min(1).max(50).optional(),userAddress:l.string().regex(/^(0x[a-fA-F0-9]{40}|eth\|[a-fA-F0-9]{40})$/).optional(),startDate:l.string().datetime().optional(),endDate:l.string().datetime().optional(),sortOrder:l.enum(["ASC","DESC"]).default("DESC")}),Ue=l.object({page:l.number().int().min(1),limit:l.number().int().min(1),total:l.number().int().min(0),totalPages:l.number().int().min(0),hasNext:l.boolean(),hasPrevious:l.boolean()});function Pe(e){return l.object({data:l.array(e),page:l.number().int().min(1),limit:l.number().int().min(1),total:l.number().int().min(0),totalPages:l.number().int().min(0),hasNext:l.boolean(),hasPrevious:l.boolean()})}const Le=l.enum(["all","DEFI","ASSET"]),Oe=Se.extend({type:Le.optional(),address:M.optional(),search:O.optional(),tokenName:L.optional()}),xe=l.object({walletAddress:M,amount:j}),Re=l.object({address:M.optional(),refresh:l.boolean().optional()}),Be=l.object({profileImage:l.string(),fullName:x,address:M,privateKey:re.optional()}),Me=l.object({file:l.union([l.instanceof(File),l.instanceof(Buffer)]),address:M.optional(),privateKey:re.optional()}),Ke=l.object({address:M,tokenId:l.union([l.string(),l.object({collection:l.string(),category:l.string(),type:l.string(),additionalKey:l.string()}),l.object({collection:l.string(),category:l.string(),type:l.string(),additionalKey:l.string(),instance:l.string()})]).optional(),tokenClassKey:l.object({collection:l.string(),category:l.string(),type:l.string(),additionalKey:l.string()}).optional(),tokenName:L.optional()}).refine(e=>void 0!==e.tokenId||void 0!==e.tokenName,"At least one token identifier (tokenId or tokenName) is required"),Ge=l.enum(["buy","sell"]),Ve=l.enum(["BUY","SELL"]),je=l.object({tradeType:Ge,tokenAmount:G,vaultAddress:K,userAddress:M,slippageTolerance:G.optional(),deadline:l.number().int().positive().optional()}),qe=l.object({tokenSymbol:U,nativeTokenQuantity:G,expectedToken:G,maxAcceptableReverseBondingCurveFee:V.default("0").optional()}),We=l.object({tokenSymbol:U,tokenQuantity:G,expectedNativeToken:G,maxAcceptableReverseBondingCurveFee:V.default("0").optional()}),He=De.extend({tokenName:L.optional()}),Qe=l.object({page:l.number().int().min(1).max(1e3).default(1).optional(),limit:l.number().int().min(1).max(20).default(10).optional()}),Xe=l.enum(["NATIVE","MEME"]),ze=l.enum(["IN","OUT"]),Ye=l.object({type:Xe,method:ze,vaultAddress:K,amount:G}),Je=l.object({nativeTokenQuantity:G}),Ze=l.object({vaultAddress:K}),et=l.object({minFeePortion:G,maxFeePortion:G});function tt(e){return t=>{const a=e.safeParse(t);return{success:a.success,data:a.success?a.data:void 0,errors:a.success?void 0:a.error.errors.map(e=>e.message)}}}const at=tt(_),nt=tt(U),rt=tt(P),st=tt(M),ot=tt(K),it=tt(G),ct=tt(j),lt=tt(x),dt=tt(O),ut=tt(L),ht=tt(ue),gt=tt(ie),pt=tt(he),mt=tt(pe),ft=tt(Oe),yt=tt(xe),At=tt(Re),wt=tt(Be),kt=tt(Me),vt=tt(Ke),Tt=tt(je),bt=tt(qe),Et=tt(We),Nt=tt(He),It=tt(Qe),St=tt(Ye),Dt=tt(Je),Ft=tt(Ze);function $t(e,t){throw new b(e.join("; "),t,"VALIDATION_ERROR")}function Ct(e){const t=at(e);!t.success&&t.errors&&$t(t.errors,"tokenName")}function _t(e){const t=$e.safeParse(e);t.success||$t(t.error.errors.map(e=>e.message),"pagination")}function Ut(e){const t=mt(e);!t.success&&t.errors&&$t(t.errors,"options")}function Pt(e){const t=St(e);!t.success&&t.errors&&$t(t.errors,"options")}function Lt(e){const t=ye.safeParse(e);t.success||$t(t.error.errors.map(e=>e.message),"options")}function Ot(e){const t=M.safeParse(e);if(!t.success)throw new b("Ethereum address must be 40 hex characters (with or without 0x prefix)","ethereumAddress","INVALID_FORMAT");return t.data}var xt=Object.freeze({__proto__:null,normalizeAddressInput:function(e){if(!e)return;const t=M.safeParse(e);if(!t.success)throw new b(`Invalid address format: ${e}. Must be either "0x..." (Ethereum) or "eth|..." (GalaChain) format`,"address","INVALID_FORMAT");return t.data},toBackendAddressFormat:Ot,validateCheckPoolOptions:Ut,validateGetAmountOptions:Pt,validateGetGraphOptions:Lt,validatePagination:_t,validateTokenName:Ct});function Rt(e){if(!e)return[];return(Array.isArray(e)?e:e.token??[]).map(e=>({...e,createdAt:new Date(e.createdAt),updatedAt:new Date(e.updatedAt)}))}function Bt(e,t){const a=Number(e.page)||t.page,n=Number(e.limit)||t.limit,r=Number(e.total)||Number(e.data?.count)||0;return{page:a,limit:n,total:r,totalPages:Math.ceil(r/n)}}function Mt(e,t){return{hasNext:e<t,hasPrevious:e>1}}function Kt(e,t,a=!1){const n=!0===e.error||200!==e.status,r=a&&!e.data;if(n||r)throw new Error(e.message||t)}const Gt="/launchpad/upload-image",Vt="/launchpad/fetch-pool",jt="/launchpad/check-pool",qt="/launchpad/get-graph-data",Wt="/holders",Ht="/launchpad/get-badge/",Qt="/trade/",Xt="/token/commment",zt="/token/commment",Yt="/user/profile",Jt="/user/profile",Zt="/user/token-list",ea="/user/token-hold",ta="/user/transfer-faucets";function aa(e,t){return new b(`Token "${e}" not found. Please verify the token name is correct.`,"tokenName","TOKEN_NOT_FOUND")}function na(e,t){const a=t||e.charAt(0).toUpperCase()+e.slice(1);return new b(`${a} is required`,e,"REQUIRED_FIELD")}function ra(e,t,a){const n=a||e.charAt(0).toUpperCase()+e.slice(1);return new b(`${n} must be ${t}`,e,"INVALID_FORMAT")}function sa(e,t,a){return new E(e,t,a)}function oa(e,t){return new N(e,t)}function ia(e,t,a){return new I(e,t,a)}function ca(e){return e&&"object"==typeof e&&"string"==typeof e.tokenName&&(void 0===e.from||"number"==typeof e.from)&&(void 0===e.to||"number"==typeof e.to)&&(void 0===e.resolution||"number"==typeof e.resolution)}class la{constructor(e){this.http=e}async fetchPools(e={}){let t;_t({page:e.page??1,limit:e.limit??10}),e.tokenName&&Ct(e.tokenName),"recent"===e.type?t="RECENT":"popular"===e.type&&(t="POPULAR");const a={page:e.page||1,limit:e.limit||10};e.search&&(a.search=e.search),e.tokenName&&(a.tokenName=e.tokenName),t&&(a.type=t);const n={page:a.page.toString(),limit:a.limit.toString()};void 0!==a.type&&(n.type=a.type),void 0!==a.tokenName&&(n.tokenName=a.tokenName),void 0!==a.search&&(n.search=a.search);const r=w(n),s=await this.http.get(Vt,r);Kt(s,"Failed to fetch pools",!0);return{pools:function(e){if(!e)return[];let t=[];return e.tokens?t=Array.isArray(e.tokens)?e.tokens.map(e=>({...e,createdAt:new Date(e.created_at??e.createdAt)})):[{...e.tokens,createdAt:new Date(e.tokens.created_at??e.tokens.createdAt)}]:e.pools&&Array.isArray(e.pools)&&(t=e.pools.map(e=>({...e,createdAt:new Date(e.created_at??e.createdAt)}))),t}(s.data),page:s.data.page,limit:s.data.limit,total:s.data.total,totalPages:s.data.totalPages,hasNext:s.data.page<s.data.totalPages,hasPrevious:s.data.page>1}}async checkPool(e){Ut(e),e.tokenName&&Ct(e.tokenName);const t=w(e),a=await this.http.get(jt,t);Kt(a,"Failed to check pool");const n=a.data;return e.symbol?n?.isSymbolExist??!1:e.tokenName?n?.isNameExist??!1:n?.exists??!1}async isTokenNameAvailable(e){try{return!await this.checkPool({tokenName:e})}catch{return!1}}async isTokenSymbolAvailable(e){try{return!await this.checkPool({symbol:e})}catch{return!1}}async fetchVolumeData(e){if(!ca(e))throw new b("Invalid options provided. Expected { tokenName: string, from?: number, to?: number, resolution?: number }","options","INVALID_OPTIONS");const{tokenName:t,from:a,to:n,resolution:r}=e;if(Ct(t),!a||!n||!r)throw new b("Graph options (from, to, resolution) are required","options","MISSING_GRAPH_OPTIONS");const s={tokenName:t,from:a,to:n,resolution:r};Lt(s);const o=w(s),i=await this.http.get(qt,o);return Kt(i,"Failed to fetch graph data",!0),{dataPoints:i.data}}async fetchTokenDistribution(e){if(!e)throw na("tokenName","Token name");Ct(e);const t=await this.resolveTokenNameToVault(e);if(!t)throw new b(T(e),"tokenName","VAULT_NOT_FOUND");const a=encodeURIComponent(t),n=await this.http.get(`${Wt}/${a}`);return Kt(n,"Failed to fetch token distribution",!0),{holders:n.data.holders||[],totalSupply:n.data.totalSupply||"0",totalHolders:n.data.totalHolders||0,lastUpdated:new Date}}async fetchTokenBadges(e){if(!e)throw na("tokenName","Token name");Ct(e);const t=await this.http.get(Ht,{tokenName:e});return Kt(t,"Failed to fetch token badges",!0),{volumeBadges:t.data.volumeBadge||[],engagementBadges:t.data.engagementBadge||[]}}async hasTokenBadge(e){const{tokenName:t,badgeType:a,badgeName:n}=e;try{const e=await this.fetchTokenBadges(t);if(!e)return!1;const r=("volume"===a?e.volumeBadges:e.engagementBadges).find(e=>e.badgeName===n);return r?.isActive||!1}catch{return!1}}async resolveTokenNameToVault(e){try{const t=await this.fetchPools({tokenName:e});return t.pools&&Array.isArray(t.pools)&&t.pools.length>0?t.pools[0].vaultAddress||null:t.pools&&t.pools.tokens&&t.pools.tokens.vaultAddress||null}catch{return null}}}function da(e,t={}){const{stringifyFields:a=[],optionalFields:n=[],fieldMappings:r={}}=t,s={};for(const[t,o]of Object.entries(e)){const e=t;if(n.includes(e)&&void 0===o)continue;if(n.includes(e)&&"string"==typeof o&&0===o.trim().length)continue;const i=r[e],c=i?String(i):t;a.includes(e)?s[c]=String(o):s[c]=o}return w(s)}const ua={PAGINATION:{MIN_PAGE:1,MAX_PAGE:1e3,MIN_LIMIT:1,MAX_LIMIT:20}};class ha{constructor(e){this.http=e}async fetchTrades(e){if(!(t=e)||"object"!=typeof t||"string"!=typeof t.tokenName||void 0!==t.tradeType&&"buy"!==t.tradeType&&"sell"!==t.tradeType||void 0!==t.userAddress&&"string"!=typeof t.userAddress||void 0!==t.page&&"number"!=typeof t.page||void 0!==t.limit&&"number"!=typeof t.limit)throw new b("Invalid options provided. Expected { tokenName: string, tradeType?: string, userAddress?: string, page?: number, limit?: number, startDate?: Date, endDate?: Date, sortOrder?: string }","options","INVALID_OPTIONS");var t;const{tokenName:a,tradeType:n,userAddress:r,page:s=1,limit:o=10,startDate:i,endDate:c,sortOrder:l}=e;if(!F(a))throw new b("Token name is required and must be a non-empty string","tokenName","INVALID_TOKEN_NAME");S(s,o,ua);const d=function(e,t,a){return da({tokenName:e,page:t,limit:a},{stringifyFields:["page","limit"]})}(a,s,o),u=await this.http.get(Qt,d),h=(g=u.data)?(Array.isArray(g)?g:g.trades?g.trades:[]).map(e=>({...e,createdAt:new Date(e.createdAt),updatedAt:new Date(e.updatedAt)})):[];var g;const p=Bt(u,{page:s,limit:o}),m=Mt(p.page,p.totalPages);return{trades:h,...p,...m}}}const ga=new A({debug:!1,context:"DateUtils"});function pa(e,t){if(!e)return t||new Date;if(e instanceof Date)return isNaN(e.getTime())?t||new Date:e;try{const a=new Date(e);return isNaN(a.getTime())?(ga.warn(`Invalid date string received: "${e}". Using fallback.`),t||new Date):a}catch(a){return ga.warn(`Date parsing error for "${e}":`,a),t||new Date}}const ma={PAGINATION:{MIN_PAGE:1,MAX_PAGE:1e3,MIN_LIMIT:1,MAX_LIMIT:50},CONTENT:{MIN_LENGTH:1,MAX_LENGTH:500}};class fa{constructor(e,t){this.http=e,this.poolService=t}async fetchComments(e){if(!(t=e)||"object"!=typeof t||"string"!=typeof t.tokenName||void 0!==t.page&&"number"!=typeof t.page||void 0!==t.limit&&"number"!=typeof t.limit)throw new b("Invalid options provided. Expected { tokenName: string, page?: number, limit?: number }","options","INVALID_OPTIONS");var t;const{tokenName:a,page:n=1,limit:r=10}=e;if(!F(a))throw new b("Token name is required and must be a non-empty string","tokenName","INVALID_TOKEN_NAME");S(n,r,ma);const s=await this.poolService.resolveTokenNameToVault(a);if(!s)throw aa(a);const o=da({vaultAddress:s,page:n,limit:r},{stringifyFields:["page","limit"]}),i=await this.http.get(Xt,o);Kt(i,"Failed to fetch comments");return{comments:i.data.comments.map(e=>({...e,createdAt:pa(e.createdAt)})),total:i.data.count}}async postComment(e){if(!(t=e)||"object"!=typeof t||"string"!=typeof t.tokenName||"string"!=typeof t.content)throw new b("Invalid options provided. Expected { tokenName: string, content: string }","options","INVALID_OPTIONS");var t;const{tokenName:a,content:n}=e;if(!F(a))throw new b("Token name is required and must be a non-empty string","tokenName","INVALID_TOKEN_NAME");if(!function(e){if(!e||"string"!=typeof e)return!1;const t=e.trim();return t.length>=ma.CONTENT.MIN_LENGTH&&t.length<=ma.CONTENT.MAX_LENGTH}(n))throw new b(`Comment content must be between ${ma.CONTENT.MIN_LENGTH} and ${ma.CONTENT.MAX_LENGTH} characters`,"content","INVALID_CONTENT");const r=await this.poolService.resolveTokenNameToVault(a);if(!r)throw aa(a);const s={userAddress:this.http.getAddress(),vaultAddress:r,content:n};Kt(await this.http.post(zt,s),"Failed to create comment")}}const ya={PAGINATION:{MIN_PAGE:1,MAX_PAGE:1e3,MIN_LIMIT:1,MAX_LIMIT:20},USER_ADDRESS:{PATTERN:/^eth\|[0-9a-fA-F]{40}$/},TOKEN_NAME:{MIN_LENGTH:1,MAX_LENGTH:50},SEARCH:{MIN_LENGTH:1,MAX_LENGTH:100},FAUCET_AMOUNT:{POSITIVE_NON_ZERO_DECIMAL:/^(?!0+(\.0+)?$)\d+(\.\d+)?$/},PROFILE:{FULL_NAME:{MIN_LENGTH:1,MAX_LENGTH:100,ALPHABETS_ONLY_PATTERN:/^[a-zA-Z]+(?:\s[a-zA-Z]+)?$/}}};function Aa(e){return!(!e||"string"!=typeof e)&&ya.USER_ADDRESS.PATTERN.test(e)}const wa="Update profile";class ka{constructor(e){this.http=e}async fetchProfile(e){const t=e??this.http.getAddress();if(!Aa(t))throw new b("Address must be in format: eth|[40-hex-chars]","address","INVALID_ADDRESS");const a={userAddress:t};return await this.http.get(Yt,a)}async updateProfile(e){this.validateUpdateProfileData(e);let t=e.profileImage;if(!t||""===t.trim())try{const a=await this.fetchProfile(e.address);t=a.data?.profileImage||""}catch{t=""}const a={profileImage:t,fullName:e.fullName,userAddress:e.address},n=await this.http.signCustomMessage(wa),r={address:n.address,message:wa,publickey:n.ethereumAddress,sign:n.signature};Kt(await this.http.put(Jt,a,r),"Profile update failed")}async uploadProfileImage(e){this.validateUploadProfileImageOptions(e);const t=e.address??this.http.getAddress();try{const a=new FormData;if("undefined"!=typeof File&&e.file instanceof File)a.append("image",e.file);else{if(!Buffer.isBuffer(e.file))throw new b("Invalid file type","file","INVALID_FILE_TYPE");{const n=`profile-image-${t}.png`,r=new Blob([e.file],{type:"image/png"});a.append("image",r,n)}}const n=await this.http.request({method:"POST",url:`${Gt}?tokenName=${encodeURIComponent(t)}`,data:a,headers:{}});return Kt(n,"Image upload failed"),"string"==typeof n.data?n.data:""}catch(e){if(e instanceof b)throw e;throw new b(`Profile image upload failed: ${e instanceof Error?e.message:"Unknown error"}`,"file","UPLOAD_FAILED")}}async fetchTokenList(e){this.validateGetTokenListOptions(e);const t=da({page:e.page,limit:e.limit,type:"all"!==e.type&&e.type?e.type:"DEFI",address:e.address,search:e.search,tokenName:e.tokenName},{stringifyFields:["page","limit"],optionalFields:["address","search","tokenName"]}),a=await this.http.get(Zt,t);Kt(a,"Failed to fetch token list",!0);const n=Rt(a.data),r=Bt(a,{page:e.page||1,limit:e.limit||10}),s=Mt(r.page,r.totalPages);return{tokens:n,...r,...s}}async fetchTokensHeld(e){this.validateGetTokenListOptions(e);const t=da({page:e.page,limit:e.limit,address:e.address,search:e.search,tokenName:e.tokenName},{stringifyFields:["page","limit"],optionalFields:["address","search","tokenName"]}),a=await this.http.get(ea,t);Kt(a,"Failed to fetch tokens held",!0);const n=Rt(a.data),r=Bt(a,{page:e.page||1,limit:e.limit||10}),s=Mt(r.page,r.totalPages);return{tokens:n,...r,...s}}async fetchTokensCreated(e={}){const{page:t=1,limit:a=10,search:n,tokenName:r}=e,s={type:"DEFI",address:this.http.getAddress(),page:t,limit:a};return void 0!==n&&(s.search=n),void 0!==r&&(s.tokenName=r),this.fetchTokenList(s)}validateGetTokenListOptions(e){if(S(e.page,e.limit,ya),void 0!==e.address&&!Aa(e.address))throw new b("Address must be in format: eth|[40-hex-chars]","address","INVALID_ADDRESS");if(void 0!==e.search&&e.search.trim().length>0&&!((t=e.search)&&"string"==typeof t&&t.length>=ya.SEARCH.MIN_LENGTH&&t.length<=ya.SEARCH.MAX_LENGTH))throw new b(`Search query must be between ${ya.SEARCH.MIN_LENGTH} and ${ya.SEARCH.MAX_LENGTH} characters`,"search","INVALID_SEARCH");var t,a;if(void 0!==e.tokenName&&e.tokenName.trim().length>0&&!((a=e.tokenName)&&"string"==typeof a&&a.length>=ya.TOKEN_NAME.MIN_LENGTH&&a.length<=ya.TOKEN_NAME.MAX_LENGTH))throw new b(`Token name must be between ${ya.TOKEN_NAME.MIN_LENGTH} and ${ya.TOKEN_NAME.MAX_LENGTH} characters`,"tokenName","INVALID_TOKEN_NAME")}validateUpdateProfileData(e){if(!Aa(e.address))throw new b("Address must be in format: eth|[40-hex-chars]","address","INVALID_ADDRESS");if(!((t=e.fullName)&&"string"==typeof t&&t.length>=ya.PROFILE.FULL_NAME.MIN_LENGTH&&t.length<=ya.PROFILE.FULL_NAME.MAX_LENGTH&&ya.PROFILE.FULL_NAME.ALPHABETS_ONLY_PATTERN.test(t)))throw new b(`Full name must be between ${ya.PROFILE.FULL_NAME.MIN_LENGTH} and ${ya.PROFILE.FULL_NAME.MAX_LENGTH} characters`,"fullName","INVALID_FULL_NAME");var t}validateUploadProfileImageOptions(e){if(e.address&&!Aa(e.address))throw new b("Address must be in format: eth|[40-hex-chars]","address","INVALID_ADDRESS")}}class va extends Error{constructor(e,t,a){super(e),this.filename=t,this.mimeType=a,this.name="FileValidationError"}}function Ta(e,t,a){if(!e)throw new va("File is required",t,a);if("undefined"!=typeof File&&e instanceof File){const t=ve.safeParse(e);if(!t.success){const a=t.error.errors.map(e=>e.message).join("; ");throw new va(a,e.name,e.type)}return}if(Buffer.isBuffer(e)){if(!t)throw new va("Filename is required when uploading Buffer objects",t,a);const n=Te.safeParse(e);if(!n.success){const e=n.error.errors.map(e=>e.message).join("; ");throw new va(e,t,a)}if(t.length>255)throw new va(`Filename length ${t.length} exceeds maximum allowed length of 255 characters`,t,a);const r=["image/png","image/jpg","image/jpeg","image/gif","image/webp","image/svg+xml"];if(!r.includes(a))throw new va(`Invalid file type "${a}" is not allowed. Allowed types: ${r.join(", ")}`,t,a);const s=function(e){if(!e)return"";const t=e.lastIndexOf(".");if(-1===t)return"";return e.substring(t).toLowerCase()}(t),o=[".png",".jpg",".jpeg",".gif",".webp",".svg"];if(!o.includes(s))throw new va(`File extension "${s}" is not allowed. Allowed extensions: ${o.join(", ")}`,t,a);const i=function(e){switch(e.toLowerCase()){case".png":return"image/png";case".jpg":case".jpeg":return"image/jpeg";case".gif":return"image/gif";case".webp":return"image/webp";case".svg":return"image/svg+xml";default:return"application/octet-stream"}}(s);if(i!==a&&"application/octet-stream"!==i)throw new va(`File extension "${s}" does not match MIME type "${a}"`,t,a);return}throw new va("File must be a File object (browser) or Buffer (Node.js)",t,a)}class ba{constructor(e){this.http=e}async uploadImageByTokenName(e){const{tokenName:t,options:a}=e;Ct(t);const n=`${t}.png`;Ta(a.file,n,"image/png");try{const e=new FormData;if("undefined"!=typeof File&&a.file instanceof File)e.append("image",a.file);else{if(!Buffer.isBuffer(a.file))throw ra("file","a File object (browser) or Buffer (Node.js)");{const n=`${a.tokenName??t}.png`,r=new Blob([a.file],{type:"image/png"});e.append("image",r,n)}}const n=await this.http.request({method:"POST",url:`${Gt}?tokenName=${encodeURIComponent(a.tokenName??t)}`,data:e,headers:{}});return Kt(n,"Image upload failed"),"string"==typeof n.data?n.data:""}catch(e){if(e instanceof Error&&e.message.includes("FormData"))throw oa("File upload failed: FormData not supported in this environment. Ensure you have proper polyfills for Node.js environments.","FormData");throw e}}}class Ea{constructor(e){this.http=e}async transferFaucets(e){this.validateTransferFaucetsData(e);const t={userAddress:e.walletAddress,amount:e.amount};Kt(await this.http.post(ta,t),"Faucet transfer failed")}validateTransferFaucetsData(e){if(!Aa(e.walletAddress))throw new b("Address must be in format: eth|[40-hex-chars]","address","INVALID_ADDRESS");if(!(t=e.amount)||"string"!=typeof t||!ya.FAUCET_AMOUNT.POSITIVE_NON_ZERO_DECIMAL.test(t))throw new b("Amount must be a positive decimal string greater than zero","amount","INVALID_AMOUNT");var t}}class Na{constructor(e){this.http=e,this.poolService=new la(e),this.tradeService=new ha(e),this.commentService=new fa(e,this.poolService),this.userService=new ka(e),this.imageService=new ba(e),this.faucetService=new Ea(e)}async uploadImageByTokenName(e){return this.imageService.uploadImageByTokenName(e)}async fetchPools(e={}){return this.poolService.fetchPools(e)}async checkPool(e){return this.poolService.checkPool(e)}async isTokenNameAvailable(e){return this.poolService.isTokenNameAvailable(e)}async isTokenSymbolAvailable(e){return this.poolService.isTokenSymbolAvailable(e)}async fetchVolumeData(e){return this.poolService.fetchVolumeData(e)}async fetchTokenDistribution(e){return this.poolService.fetchTokenDistribution(e)}async fetchTokenBadges(e){return this.poolService.fetchTokenBadges(e)}async hasTokenBadge(e){return this.poolService.hasTokenBadge(e)}async fetchTrades(e){return this.tradeService.fetchTrades(e)}async fetchComments(e){return this.commentService.fetchComments(e)}async postComment(e){return this.commentService.postComment(e)}async fetchProfile(e){return this.userService.fetchProfile(e)}async updateProfile(e){return this.userService.updateProfile(e)}async uploadProfileImage(e){return this.userService.uploadProfileImage(e)}async fetchTokenList(e){return this.userService.fetchTokenList(e)}async fetchTokensHeld(e){return this.userService.fetchTokensHeld(e)}async fetchTokensCreated(e={}){return this.userService.fetchTokensCreated(e)}async transferFaucets(e){return this.faucetService.transferFaucets(e)}getAddress(){return this.http.getAddress()}validateTokenName(e){return Ct(e)}}class Ia extends i{constructor(e){super(),this.from=e.from,this.to=e.to,this.quantity=e.quantity,this.tokenInstance=e.tokenInstance,this.uniqueKey=e.uniqueKey,e.signedPayload&&(this.signature=e.signedPayload.signature,this.domain=e.signedPayload.domain,this.types=e.signedPayload.types,e.signedPayload.prefix&&(this.prefix=e.signedPayload.prefix))}static fromTokenClassKey(e,t,a,n,r){let s;if("string"==typeof n){const e=n.split("$");if(4!==e.length)throw new Error(`Invalid token class key format: ${n}. Expected format: collection$category$type$additionalKey`);s={collection:e[0],category:e[1],type:e[2],additionalKey:e[3],instance:"0"}}else s={collection:n.collection,category:n.category,type:n.type,additionalKey:n.additionalKey,instance:"0"};return new Ia({from:e,to:t,quantity:a,tokenInstance:s,uniqueKey:r||`galaconnect-operation-${Date.now()}_${Math.random().toString(36).substring(2,8)}`})}static forGALA(e,t,a,n){return new Ia({from:e,to:t,quantity:a,tokenInstance:{collection:"GALA",category:"Unit",type:"none",additionalKey:"none",instance:"0"},uniqueKey:n||`galaconnect-operation-${Date.now()}_${Math.random().toString(36).substring(2,8)}`})}getTokenClassKey(){return`${this.tokenInstance.collection}$${this.tokenInstance.category}$${this.tokenInstance.type}$${this.tokenInstance.additionalKey}`}toSigningPayload(){return{from:this.from,to:this.to,quantity:this.quantity,tokenInstance:this.tokenInstance,uniqueKey:this.uniqueKey}}}class Sa{constructor(e){this.wallet=e}static generateUniqueKey(){return`${Date.now()}_${Math.random().toString(36).substring(2,8)}`}async signTransferToken(e){const t={name:"GalaChain",chainId:1},a={TransferToken:[{name:"from",type:"string"},{name:"to",type:"string"},{name:"quantity",type:"string"},{name:"tokenInstance",type:"TokenInstance"},{name:"uniqueKey",type:"string"}],TokenInstance:[{name:"collection",type:"string"},{name:"category",type:"string"},{name:"type",type:"string"},{name:"additionalKey",type:"string"},{name:"instance",type:"string"}]};return{signature:await this.wallet.signTypedData(t,a,e),domain:t,types:a,signerPublicKey:this.wallet.signingKey.publicKey}}static toGalaChainAddress(t){const a=t.replace("0x","");return`eth|${e.getAddress(`0x${a}`).replace("0x","")}`}static fromGalaChainAddress(e){return e.startsWith("eth|")?e.substring(4):e}static createGALATokenInstance(){return{collection:"GALA",category:"Unit",type:"none",additionalKey:"none",instance:"0"}}static createTokenInstanceFromClassKey(e){const t=e.split("$");if(4!==t.length)throw new Error(`Invalid token class key format: ${e}. Expected format: collection$category$type$additionalKey`);return{collection:t[0],category:t[1],type:t[2],additionalKey:t[3],instance:"0"}}}function Da(e){if("string"==typeof e){const t=e.split("|");if(t.length<4)throw new b(`Invalid tokenId string format: "${e}". Expected format: "collection|category|type|additionalKey" or "collection|category|type|additionalKey|instance"`,"tokenId","INVALID_TOKEN_ID_FORMAT");if(!(t[0]&&t[1]&&t[2]&&t[3]))throw new b(`Invalid tokenId string format: "${e}". All components (collection, category, type, additionalKey) must be non-empty`,"tokenId","INVALID_TOKEN_ID_FORMAT");return{collection:t[0],category:t[1],type:t[2],additionalKey:t[3],instance:t[4]||"0"}}if("object"==typeof e&&null!==e){if(!(e.collection&&e.category&&e.type&&e.additionalKey))throw new b("Invalid tokenId object format. All fields (collection, category, type, additionalKey) are required","tokenId","INVALID_TOKEN_ID_FORMAT");return"instance"in e&&void 0!==e.instance?e:{...e,instance:"0"}}throw new b(`Invalid tokenId type: ${typeof e}. Expected string, TokenClassKey, or TokenInstanceKey`,"tokenId","INVALID_TOKEN_ID_TYPE")}var Fa=Object.freeze({__proto__:null,normalizeToTokenInstanceKey:Da});const $a={MAX_UNIQUE_KEY_LENGTH:64,UNIQUE_KEY_PATTERN:/^(galaswap-operation-|galaconnect-operation-)/,TOKEN_NAME_PATTERN:/^[a-zA-Z0-9]+$/};var Ca;!function(e){e.INVALID_RECIPIENT="INVALID_RECIPIENT",e.INVALID_AMOUNT="INVALID_AMOUNT",e.INSUFFICIENT_BALANCE="INSUFFICIENT_BALANCE",e.TOKEN_NOT_FOUND="TOKEN_NOT_FOUND",e.SIGNATURE_FAILED="SIGNATURE_FAILED",e.NETWORK_ERROR="NETWORK_ERROR",e.DUPLICATE_TRANSFER="DUPLICATE_TRANSFER",e.TRANSFER_LIMIT_EXCEEDED="TRANSFER_LIMIT_EXCEEDED"}(Ca||(Ca={}));class _a extends Error{constructor(e,t,a){super(e),this.type=t,this.details=a,this.name="TransferError"}}class Ua{constructor(e,t,a,n=!1){this.http=e,this.wallet=t,this.tokenResolver=a,this.signatureHelper=new Sa(t),this.logger=new A({debug:n,context:"GalaChainService"})}async fetchPoolDetails(e){this.validateFetchPoolDetailsData(e);const t=await this.http.post("/api/asset/launchpad-contract/FetchSaleDetails",e);if(1!==t.Status)throw sa(`Failed to fetch pool details: Status ${t.Status}`,t.Status);return t}async fetchLaunchTokenFee(){const e=await this.http.post("/api/asset/launchpad-contract/FetchLaunchpadFeeAmount",{});if(1!==e.Status)throw sa(`Failed to fetch launch token fee: Status ${e.Status}`,e.Status);return e.Data.feeAmount}validateFetchPoolDetailsData(e){if(!(t=e)||"object"!=typeof t||"string"!=typeof t.vaultAddress)throw new b("Invalid fetch pool details data: missing required fields","data","INVALID_TYPE");var t;if(!e.vaultAddress||"string"!=typeof e.vaultAddress)throw new b("Vault address is required and must be a string","vaultAddress","INVALID_VAULT_ADDRESS");if(!e.vaultAddress.startsWith("service|Token$Unit$"))throw new b("Vault address must be in service format: service|Token$Unit$...","vaultAddress","INVALID_VAULT_ADDRESS")}async fetchGalaBalance(e){return this.fetchTokenBalance(e)}async fetchTokenBalance(e){try{const t=await this.http.post("/api/asset/token-contract/FetchBalances",e);if(1!==t.Status||!t.Data||0===t.Data.length)return null;const a=t.Data.find(t=>t.collection===e.collection&&t.category===e.category&&t.additionalKey===e.additionalKey&&t.type===e.type);if(!a||"0"===a.quantity)return null;const n=`${a.collection}|${a.category}|${a.additionalKey}|${a.type}`;return{quantity:a.quantity,collection:a.collection,category:a.category,tokenId:n}}catch(e){throw sa(`Failed to fetch token balance from GalaChain: ${e.message||"Unknown error"}`,void 0,e)}}async transferGala(e){this.validateTransferGalaData(e);try{const t=C(e.recipientAddress),a=C(this.wallet.address),n=Ia.forGALA(a,t,e.amount,e.uniqueKey),r=await this.signatureHelper.signTransferToken(n.toSigningPayload()),s=new Ia({...n.toSigningPayload(),signedPayload:r});this.logger.debug("[DEBUG] Full GALA Transfer Request Payload:",JSON.stringify(s,null,2));const o=await this.http.post("/api/asset/token-contract/TransferToken",s);if(this.logger.debug("[DEBUG] Transfer response:",JSON.stringify(o,null,2)),o&&"object"==typeof o){if("Status"in o&&1===o.Status&&"Data"in o){const e=o;return Array.isArray(e.Data)&&e.Data.length>0?"gala-transfer-successful":"transfer-successful-no-id"}if("transactionId"in o&&o.transactionId)return o.transactionId}throw new _a("Transfer succeeded but transaction ID could not be extracted",Ca.NETWORK_ERROR)}catch(t){throw this.handleTransferError(t,"GALA transfer failed",e)}}async transferToken(e){this.validateTransferTokenData(e);try{const t=C(e.to),a=C(this.wallet.address);let n;if(e.tokenId)n=Da(e.tokenId),this.logger.debug("[DEBUG] Using provided tokenId:",e.tokenId),this.logger.debug("[DEBUG] Normalized Token Instance:",JSON.stringify(n,null,2));else{if(!e.tokenName)throw new _a("Must provide either tokenId or tokenName for token identification",Ca.TOKEN_NOT_FOUND);n=await this.resolveTokenInstance(e.tokenName)}const r=new Ia({from:a,to:t,quantity:e.amount,tokenInstance:n,uniqueKey:e.uniqueKey||`galaconnect-operation-${Date.now()}_${Math.random().toString(36).substring(2,8)}`}),s=await this.signatureHelper.signTransferToken(r.toSigningPayload()),o=new Ia({...r.toSigningPayload(),signedPayload:s});this.logger.debug("[DEBUG] Full Transfer Request Payload:",JSON.stringify(o,null,2));const i=await this.http.post("/api/asset/token-contract/TransferToken",o);if(this.logger.debug("[DEBUG] Token transfer response:",JSON.stringify(i,null,2)),i&&"object"==typeof i){if("Status"in i&&1===i.Status&&"Data"in i){const e=i;return Array.isArray(e.Data)&&e.Data.length>0?"token-transfer-successful":"transfer-successful-no-id"}if("transactionId"in i&&i.transactionId)return i.transactionId}throw new _a("Transfer succeeded but transaction ID could not be extracted",Ca.NETWORK_ERROR)}catch(t){throw this.handleTransferError(t,"Token transfer failed",e)}}async resolveTokenClassKey(e){try{const t=await this.tokenResolver.resolveTokenClassKey(e);return this.logger.debug(`[DEBUG] Token class key resolution for '${e}':`,JSON.stringify(t,null,2)),t}catch(t){if(t instanceof _a)throw t;throw new _a(`Failed to resolve token class key for '${e}': ${t instanceof Error?t.message:String(t)}`,Ca.TOKEN_NOT_FOUND,{tokenName:e})}}validateTransferGalaData(e){if(!((t=e)&&"object"==typeof t&&"string"==typeof t.recipientAddress&&t.recipientAddress.trim().length>0&&"string"==typeof t.amount&&t.amount.trim().length>0)||void 0!==t.uniqueKey&&"string"!=typeof t.uniqueKey)throw new b("Invalid GALA transfer data: missing required fields");var t;if(!$(e.recipientAddress))throw new _a("Invalid recipient address format",Ca.INVALID_RECIPIENT,{recipientAddress:e.recipientAddress});if(parseFloat(e.amount)<=0)throw new _a("Transfer amount must be positive",Ca.INVALID_AMOUNT,{amount:e.amount});if(e.uniqueKey){if(e.uniqueKey.length>$a.MAX_UNIQUE_KEY_LENGTH)throw new b(`Unique key too long. Maximum length: ${$a.MAX_UNIQUE_KEY_LENGTH}`);if(!$a.UNIQUE_KEY_PATTERN.test(e.uniqueKey))throw new _a('Invalid unique key format. Must start with "galaswap-operation-" or "galaconnect-operation-"',Ca.INVALID_AMOUNT,{uniqueKey:e.uniqueKey})}}validateTransferTokenData(e){if(!((t=e)&&"object"==typeof t&&"string"==typeof t.to&&t.to.trim().length>0&&"string"==typeof t.amount&&t.amount.trim().length>0&&(void 0!==t.tokenId||"string"==typeof t.tokenName&&t.tokenName.trim().length>0))||void 0!==t.uniqueKey&&"string"!=typeof t.uniqueKey)throw new b("Invalid token transfer data: missing required fields");var t;if(!$(e.to))throw new _a("Invalid recipient address format",Ca.INVALID_RECIPIENT,{recipientAddress:e.to});if(!e.tokenId&&!e.tokenName)throw new _a("Must provide either tokenId or tokenName for token identification",Ca.TOKEN_NOT_FOUND);if(e.tokenName&&!$a.TOKEN_NAME_PATTERN.test(e.tokenName))throw new _a("Invalid token name format",Ca.TOKEN_NOT_FOUND,{tokenName:e.tokenName});if(parseFloat(e.amount)<=0)throw new _a("Transfer amount must be positive",Ca.INVALID_AMOUNT,{amount:e.amount});if(e.uniqueKey){if(e.uniqueKey.length>$a.MAX_UNIQUE_KEY_LENGTH)throw new b(`Unique key too long. Maximum length: ${$a.MAX_UNIQUE_KEY_LENGTH}`);if(!$a.UNIQUE_KEY_PATTERN.test(e.uniqueKey))throw new _a('Invalid unique key format. Must start with "galaswap-operation-" or "galaconnect-operation-"',Ca.INVALID_AMOUNT,{uniqueKey:e.uniqueKey})}}async resolveTokenInstance(e){try{const t=await this.tokenResolver.resolveTokenToVault(e);if(!t)throw new _a(`Token '${e}' not found or not available for transfer`,Ca.TOKEN_NOT_FOUND,{tokenName:e});const a=this.resolveTokenInstanceFromVaultAddress(t);return this.logger.debug(`[DEBUG] Token resolution for '${e}':\n Vault Address: ${t}\n Token Instance: ${JSON.stringify(a,null,2)}`),a}catch(t){if(t instanceof _a)throw t;throw new _a(`Failed to resolve token '${e}': ${t instanceof Error?t.message:String(t)}`,Ca.TOKEN_NOT_FOUND,{tokenName:e})}}resolveTokenInstanceFromVaultAddress(e){const[t,a]=e.split("|");if(!a)throw new _a(`Invalid vault address format: missing token components. Address: ${e}`,Ca.TOKEN_NOT_FOUND);const n=a.split("$");if(n.length<4)throw new _a(`Invalid vault address format: insufficient token components. Expected 4+, got ${n.length}. Address: ${e}`,Ca.TOKEN_NOT_FOUND);return{collection:n[0],category:n[1],type:n[2],additionalKey:n[3],instance:"0"}}handleTransferError(e,t,a){if(e instanceof _a)return e;if(e instanceof b)return new _a(e.message,Ca.INVALID_AMOUNT);if(400===e.response?.status)return new _a(e.response.data?.message||"Invalid transfer request",Ca.INVALID_AMOUNT);if(403===e.response?.status)return new _a("Insufficient balance for transfer",Ca.INSUFFICIENT_BALANCE);if(404===e.response?.status){const e={};return"tokenName"in a&&(e.tokenName=a.tokenName),new _a("Token not found",Ca.TOKEN_NOT_FOUND,e)}return"ECONNABORTED"===e.code||"ETIMEDOUT"===e.code?new _a("Transfer request timed out",Ca.NETWORK_ERROR):new _a(e.message||t,Ca.NETWORK_ERROR)}}class Pa{constructor(e){this.http=e}async fetchTokenSpotPrice(e){if(!e||Array.isArray(e)&&0===e.length)throw na("symbols","At least one symbol");const t=Array.isArray(e)?e.join(","):e;try{const e=await this.http.request({method:"GET",url:"/v1/tokens",params:{symbols:t}}),a=[];return e.tokens&&Array.isArray(e.tokens)&&e.tokens.forEach(e=>{e.currentPrices&&e.symbol&&a.push({symbol:e.symbol,price:e.currentPrices.usd})}),a}catch(e){throw sa(`Failed to fetch token prices: ${e instanceof Error?e.message:e}`)}}async fetchLaunchpadTokenSpotPrice(e,t){if(!e||"string"!=typeof e)throw new Error(v);try{const a=await t({tokenName:e,amount:"1",type:"native"}),n=(await this.fetchTokenSpotPrice("GALA"))[0];if(!n)throw sa("GALA price not available");const r=Number(a.amount)/1e18;if(r<=0)throw new b(`Invalid token amount calculation: ${r}`,"amount","INVALID_CALCULATION");const s=n.price/r;return{symbol:e.toUpperCase(),price:s}}catch(t){if(t instanceof Error)throw sa(`Failed to calculate launchpad token spot price for ${e}: ${t.message}`);throw sa(`Failed to calculate launchpad token spot price for ${e}: ${String(t)}`)}}}const La=8,Oa=18,xa={collection:"GALA",category:"Unit",type:"none",additionalKey:"none"};function Ra(e,t=18){const a=parseFloat(e);if(0===a)return"0";return a.toFixed(t).replace(/\.?0+$/,"")}function Ba(e){return Ra(e,8)}function Ma(e){return Ra(e,18)}new A({debug:!1,context:"NumberUtils"});const Ka={BuyNativeDto:class extends i{constructor(e,t,a="0",n={maxAcceptableReverseBondingCurveFee:"0"}){super(),this.vaultAddress=e,this.nativeTokenQuantity=Ba(t),this.expectedToken=Ma(a),this.extraFees={maxAcceptableReverseBondingCurveFee:Ba(n.maxAcceptableReverseBondingCurveFee)}}},BuyExactDto:class extends i{constructor(e,t,a,n={maxAcceptableReverseBondingCurveFee:"0"}){super(),this.vaultAddress=e,this.tokenQuantity=Ma(t),this.expectedNativeToken=Ba(a),this.extraFees={maxAcceptableReverseBondingCurveFee:Ba(n.maxAcceptableReverseBondingCurveFee)}}},SellExactDto:class extends i{constructor(e,t,a="0",n={maxAcceptableReverseBondingCurveFee:"0"}){super(),this.vaultAddress=e,this.tokenQuantity=Ma(t),this.expectedNativeToken=Ba(a),this.extraFees={maxAcceptableReverseBondingCurveFee:Ba(n.maxAcceptableReverseBondingCurveFee)}}},SellNativeDto:class extends i{constructor(e,t,a,n={maxAcceptableReverseBondingCurveFee:"0"}){super(),this.vaultAddress=e,this.nativeTokenQuantity=Ba(t),this.expectedToken=Ma(a),this.extraFees={maxAcceptableReverseBondingCurveFee:Ba(n.maxAcceptableReverseBondingCurveFee)}}}};var Ga,Va,ja;!function(e){e[e.METAMASK=0]="METAMASK",e[e.TRUST_WALLET=1]="TRUST_WALLET",e[e.GALA_WALLET=2]="GALA_WALLET"}(Ga||(Ga={}));class qa{constructor(e,t=!1){this.walletProvider=e,this.debug=t,this.logger=new A({debug:t,context:"SignatureService"})}async signDTO(e,t,a,n=Ga.METAMASK){try{this.logger.debug("๐Ÿ” Signing DTO:",{methodName:t,walletPreference:n,dtoKeys:Object.keys(e)});const r=this.generateEIP712Types(t,e),s=c(e),o={...e,prefix:s};let i,l,d;switch(n){case Ga.GALA_WALLET:({signature:i,domain:l}=await this.signWithGalaWallet(r,o,t,a));break;case Ga.TRUST_WALLET:i=await this.signWithTrustWallet(o),l={name:"ethereum",chainId:1};break;case Ga.METAMASK:default:({signature:i,domain:l}=await this.signWithMetaMask(r,o))}return d=n===Ga.TRUST_WALLET?{...o,signature:i}:{...e,signature:i,types:r,domain:l},this.logger.debug("โœ… DTO signed successfully:",{payloadKeys:Object.keys(d),signatureLength:i.length}),d}catch(e){throw this.logger.error("โŒ Signature generation failed:",e),ia(`Failed to sign DTO: ${e.message}`)}}async signWithMetaMask(e,t){try{let a,n;if(this.walletProvider.signTypedData&&!this.walletProvider.getNetwork)a={name:"ethereum",chainId:1},n=await this.walletProvider.signTypedData(a,e,t);else{if(!this.walletProvider.getNetwork||!this.walletProvider.signTypedData)throw oa("Wallet provider does not support typed data signing","walletProvider");{const r=await this.walletProvider.getNetwork();a={name:r.name,chainId:Number(r.chainId)},n=await this.walletProvider.signTypedData(a,e,t)}}return{signature:n,domain:a}}catch(e){throw ia(`MetaMask/ethers signing failed: ${e.message}`)}}async signWithTrustWallet(e){try{const a=(t=e,JSON.stringify(t));let n;if(!this.walletProvider.signMessage)throw oa("Wallet provider does not support signMessage","walletProvider");return n=await this.walletProvider.signMessage(a),n}catch(e){throw ia(`TrustWallet signing failed: ${e.message}`)}var t}async signWithGalaWallet(e,t,a,n){try{const r={name:"ethereum",chainId:1};if("undefined"==typeof window)return this.logger.warn("โš ๏ธ GalaWallet not available in Node.js environment, falling back to ethers.js signing"),await this.signWithMetaMask(e,t);const s={domain:r,types:e,message:t,Primary_type:a},o=window;if(!o?.gala)throw oa("GalaWallet not found in window object","galaWallet");await o.gala.setAddress(n);return{signature:await o.gala.request({method:"eth_signTypedData",params:[JSON.stringify(s),n]}),domain:r}}catch(e){throw ia(`GalaWallet signing failed: ${e.message}`)}}generateEIP712Types(e,t){const a={};a[e]=[];const n=(e,t,r,s=!1)=>{if(void 0!==t){if(Array.isArray(t)){const o=n(e,t[0],r,!0);return s||a[r].push({name:e,type:(o??e)+"[]"}),o?o+"[]":void 0}if("object"==typeof t&&null!==t){if(a[e])throw new b(`Type name collision not supported: ${e}`,"fieldValue","TYPE_COLLISION");return a[e]=[],Object.entries(t).forEach(([t,a])=>{n(t,a,e)}),s||a[r].push({name:e,type:e}),e}{let n;switch(typeof t){case"string":n="string";break;case"number":n="uint256";break;case"boolean":n="bool";break;default:throw new b(`Unsupported type for fieldName ${e}: ${typeof t}, value: ${t}`,"fieldValue","UNSUPPORTED_TYPE")}return s||a[r].push({name:e,type:n}),n}}};return Object.entries(t).forEach(([t,a])=>{n(t,a,e)}),this.logger.debug("๐Ÿ“ Generated EIP-712 types:",a),a}detectWalletPreference(){if("undefined"==typeof window)return Ga.METAMASK;const e=window;return e?.gala?Ga.GALA_WALLET:e?.trustWallet?.isTrust?Ga.TRUST_WALLET:Ga.METAMASK}}class Wa{constructor(e=!1){this.debug=e,this.logger=new A({debug:e,context:"TokenClassKeyService"})}generateStringsInstructions(e){try{this.logger.debug("๐Ÿ”ง Generating stringsInstructions for:",e);const t=this.extractTokenSymbolFromVault(e),a=this.createTokenInstance(t),n=this.createGalaInstance(),r=`$service$${a.toStringKey()}$launchpad`,s=`$tokenBalance$${a.toStringKey()}$${e}`,o=`$tokenBalance$${a.toStringKey()}$${e}`,i=`$tokenBalance$${n.toStringKey()}$${e}`,c=[r,s,o,i,`$tokenBalance$${n.toStringKey()}$${e}`];return this.logger.debug("โœ… Generated stringsInstructions:",c),c}catch(e){throw this.logger.error("โŒ Failed to generate stringsInstructions:",e),new b(`Failed to generate stringsInstructions: ${e.message}`,"vaultAddress","INVALID_VAULT_ADDRESS")}}createTokenInstance(e){const t=new d;return t.collection=e.toLowerCase(),t.category="Unit",t.type="none",t.additionalKey="none",this.logger.debug("๐Ÿช™ Created token instance:",{symbol:e,lowercaseCollection:e.toLowerCase(),stringKey:t.toStringKey()}),t}createGalaInstance(){const e=new d;return e.collection="GALA",e.category="Unit",e.type="none",e.additionalKey="none",this.logger.debug("๐ŸŸก Created GALA instance:",{stringKey:e.toStringKey()}),e}extractTokenSymbolFromVault(e){if(!e||"string"!=typeof e)throw na("vaultAddress","Vault address");const t=e.split("$");if(t.length<3)throw ra("vaultAddress","format: service|Token$Unit$SYMBOL$eth:address$launchpad");const a=t[2];if(!a||0===a.trim().length)throw new b(`Empty token symbol in vault address: ${e}`,"vaultAddress","EMPTY_TOKEN_SYMBOL");return this.logger.debug("๐Ÿ” Extracted token symbol:",{vaultAddress:e,tokenSymbol:a,parts:t.slice(0,4)}),a}validateVaultAddress(e){if(!e||"string"!=typeof e)throw na("vaultAddress","Vault address");if(!e.startsWith("service|Token$Unit$"))throw ra("vaultAddress",'starting with "service|Token$Unit$"');if(!e.endsWith("$launchpad"))throw ra("vaultAddress",'ending with "$launchpad"');const t=e.split("$");if(t.length<5)throw ra("vaultAddress",'having at least 5 parts separated by "$"');const a=t[2];if(!a||!/^[A-Za-z]{1,10}$/.test(a))throw ra("vaultAddress","containing a 1-10 letter token symbol (case insensitive)");return this.logger.debug("โœ… Vault address validation passed:",e),!0}generateTokenClassKeyString(e,t,a,n){return`${e}$${t}$${a}$${n}`}parseTokenClassKeyString(e){const t=e.split("$");if(4!==t.length)throw ra("stringKey","format: collection$category$type$additionalKey (4 parts)");return{collection:t[0],category:t[1],type:t[2],additionalKey:t[3]}}}function Ha(e,t,a){if(t<0||t>1)throw new Error(`Invalid slippage tolerance factor: ${t}. Must be between 0 and 1 (e.g., 0.05 for 5%)`);const n=new u(e);if(n.isNaN())throw new Error(`Invalid expected amount: ${e}. Must be a valid number`);if(0===t)return e;const r=n.multipliedBy(t);let s;switch(a){case"buy-native":case"sell-exact":s=n.minus(r);break;case"buy-exact":case"sell-native":s=n.plus(r);break;default:throw new Error(`Unknown operation type: ${a}`)}return s.isLessThan(0)&&(s=new u(0)),s.toFixed()}class Qa{constructor(e,t,a=!1,n,r,s=.05,o=.01){this.httpClient=e,this.tokenResolver=t,this.walletProvider=n,this.userAddress=r,this.defaultSlippageToleranceFactor=s,this.defaultMaxAcceptableReverseBondingCurveFeeSlippageFactor=o,this.bundleEndpoint="/bundle",this.logger=new A({debug:a,context:"BundleService"}),n&&r&&(this.signatureService=new qa(n,a),this.tokenKeyService=new Wa(a))}async submitTransaction(e){try{this.logger.debug("๐Ÿ“ฆ Submitting bundle transaction:",{method:e.method,stringsInstructionsCount:e.stringsInstructions.length,signedDtoKeys:Object.keys(e.signedDto)}),this.validateBundleData(e);const t=this.formatBundleRequest(e);this.logger.debug("๐Ÿš€ Bundle request payload:",{...t,signedDto:"[REDACTED - Contains signature]"});const a=await this.httpClient.post(this.bundleEndpoint,t);return this.logger.debug("๐Ÿ“ฅ Bundle API response:",{success:a.success,hasData:!!a.data,error:a.error}),this.handleBundleResponse(a)}catch(e){return this.logger.error("โŒ Bundle transaction submission failed:",e),{success:!1,error:this.formatErrorMessage(e)}}}validateBundleData(e){if(!e)throw na("bundleData","Bundle data");if(!e.signedDto)throw na("signedDto","Signed DTO");if(!e.method||"string"!=typeof e.method)throw na("method","Method name");if(!Array.isArray(e.stringsInstructions))throw ra("stringsInstructions","an array of resource tracking strings");if(0===e.stringsInstructions.length)throw new b("stringsInstructions cannot be empty","stringsInstructions","EMPTY_ARRAY");const t=["BuyWithNative","BuyExactToken","SellExactToken","SellWithNative"];if(!t.includes(e.method))throw ra("method",`one of: ${t.join(", ")}`);e.stringsInstructions.forEach((e,t)=>{if("string"!=typeof e||0===e.length)throw new b(`stringsInstructions[${t}] must be a non-empty string`,`stringsInstructions[${t}]`,"INVALID_INSTRUCTION");if(!e.startsWith("$"))throw new b(`stringsInstructions[${t}] must start with '$': ${e}`,`stringsInstructions[${t}]`,"INVALID_INSTRUCTION_FORMAT")}),this.logger.debug("โœ… Bundle data validation passed")}formatBundleRequest(e){return{signedDto:e.signedDto,stringsInstructions:e.stringsInstructions,method:e.method}}handleBundleResponse(e){if(e.data&&!1===e.error)return this.logger.debug("โœ… Bundle transaction successful:",e.data),{success:!0,data:e.data};const t=e.error||e.message||"Bundle transaction failed";return this.logger.debug("โŒ Bundle transaction failed:",t),{success:!1,error:t}}formatErrorMessage(e){return"string"==typeof e?e:e?.response?.data?.error?e.response.data.error:e?.response?.data?.message?e.response.data.message:e?.message?e.message:"Unknown bundle transaction error"}async getBundlerTransactionResult(e){try{if(!e||"string"!=typeof e)throw na("transactionId","Transaction ID");this.logger.debug("๐Ÿ” Checking bundler transaction result:",e);const t=await this.httpClient.get(`${this.bundleEndpoint}?id=${e}`);return this.logger.debug("๐Ÿ“Š Bundler transaction result:",t),{success:!0,data:t}}catch(e){return this.logger.error("โŒ Failed to get bundler transaction result:",e),{success:!1,error:this.formatErrorMessage(e)}}}async cancelTransaction(e){try{if(!e||"string"!=typeof e)throw na("transactionId","Transaction ID");this.logger.debug("๐Ÿšซ Cancelling transaction:",e);const t=await this.httpClient.delete(`${this.bundleEndpoint}/${e}`);return this.logger.debug("๐Ÿ—‘๏ธ Transaction cancellation response:",t),{success:!0,data:t}}catch(e){return this.logger.error("โŒ Failed to cancel transaction:",e),{success:!1,error:this.formatErrorMessage(e)}}}async getHealthStatus(){try{this.logger.debug("๐Ÿฅ Checking bundle service health");const e=await this.httpClient.get(`${this.bundleEndpoint}/health`);return this.logger.debug("๐Ÿ’š Bundle service health:",e),{success:!0,data:e}}catch(e){return this.logger.error("โŒ Bundle service health check failed:",e),{success:!1,error:this.formatErrorMessage(e)}}}async buyToken(e){this.ensureTradingServicesAvailable();const{tokenName:t,amount:a,type:n,expectedAmount:r,maxAcceptableReverseBondingCurveFee:s,maxAcceptableReverseBondingCurveFeeSlippageFactor:o,slippageToleranceFactor:i}=e,c=i??this.defaultSlippageToleranceFactor,l=o??this.defaultMaxAcceptableReverseBondingCurveFeeSlippageFactor;let d=s||"0";s&&(d=Ha(s,l,"buy-exact"),this.logger.debug("Reverse bonding curve fee slippage applied:",{baseFee:s,slippageFactor:l,adjustedMaxFee:d}));const u=await this.resolveTokenNameToVault(t);if(!u)throw aa(t);if("native"===n){if(!r)throw new b("expectedAmount is required for native buy operations. Use getBuyTokenAmount() first to calculate expected tokens.","expectedAmount","EXPECTED_AMOUNT_REQUIRED");const e=Ha(r,c,"buy-native");this.logger.debug("BuyNative slippage applied:",{originalExpectedTokens:r,slippageFactor:c,adjustedMinTokens:e});const t=new Ka.BuyNativeDto(u,a,e,{maxAcceptableReverseBondingCurveFee:d});return await this.executeBundleTransaction(t,"BuyWithNative",u)}{if(!r)throw new b("expectedAmount is required for exact buy operations. Use getBuyTokenAmount() first to calculate expected GALA cost.","expectedAmount","EXPECTED_AMOUNT_REQUIRED");const e=Ha(r,c,"buy-exact");this.logger.debug("BuyExact slippage applied:",{originalExpectedGalaCost:r,slippageFactor:c,adjustedMaxGalaCost:e});const t=new Ka.BuyExactDto(u,a,e,{maxAcceptableReverseBondingCurveFee:d});return await this.executeBundleTransaction(t,"BuyExactToken",u)}}async sellToken(e){this.ensureTradingServicesAvailable();const{tokenName:t,amount:a,type:n,expectedAmount:r,maxAcceptableReverseBondingCurveFee:s,maxAcceptableReverseBondingCurveFeeSlippageFactor:o,slippageToleranceFactor:i}=e,c=i??this.defaultSlippageToleranceFactor,l=o??this.defaultMaxAcceptableReverseBondingCurveFeeSlippageFactor;let d=s||"0";s&&(d=Ha(s,l,"buy-exact"),this.logger.debug("Reverse bonding curve fee slippage applied:",{baseFee:s,slippageFactor:l,adjustedMaxFee:d}));const u=await this.resolveTokenNameToVault(t);if(!u)throw aa(t);if("exact"===n){if(!r)throw new b("expectedAmount is required for exact sell operations. Use getSellTokenAmount() first to calculate expected GALA.","expectedAmount","EXPECTED_AMOUNT_REQUIRED");const e=Ha(r,c,"sell-exact");this.logger.debug("SellExact slippage applied:",{originalExpectedGala:r,slippageFactor:c,adjustedMinGala:e});const t=new Ka.SellExactDto(u,a,e,{maxAcceptableReverseBondingCurveFee:s||"0"});return await this.executeBundleTransaction(t,"SellExactToken",u)}{if(!r)throw new b("expectedAmount is required for native sell operations. Use getSellTokenAmount() first to calculate tokens to sell.","expectedAmount","EXPECTED_AMOUNT_REQUIRED");const e=Ha(r,c,"sell-native");this.logger.debug("SellNative slippage applied:",{originalExpectedTokensToSell:r,slippageFactor:c,adjustedMaxTokensToSell:e});const t=new Ka.SellNativeDto(u,a,e,{maxAcceptableReverseBondingCurveFee:s||"0"});return await this.executeBundleTransaction(t,"SellWithNative",u)}}ensureTradingServicesAvailable(){if(!this.signatureService||!this.tokenKeyService)throw oa("Trading services not available. BundleService requires walletProvider and userAddress for trading operations.","walletProvider");if(!this.userAddress)throw na("userAddress","User address")}async executeBundleTransaction(e,t,a){this.ensureTradingServicesAvailable();try{e.uniqueKey=`galaswap - operation - ${h()}-${Date.now()}-${this.userAddress}`;const n=await this.signatureService.signDTO(e,t,this.userAddress),r=this.tokenKeyService.generateStringsInstructions(a),s={stringsInstructions:r,method:t,signedDto:n};this.logger.debug("๐Ÿ“ฆ Bundle transaction data:",{method:t,stringsInstructions:r,dtoKeys:Object.keys(n)});const o=await this.submitTransaction(s);if(o.success&&o.data)return this.logger.debug("โœ… Bundle transaction submitted:",o.data),{success:!0,data:{transactionId:o.data,message:"Transaction submitted successfully. Monitor WebSocket for completion."}};throw new Error(o.error||"Bundle transaction failed")}catch(e){throw this.logger.error("โŒ Bundle transaction error:",e),e}}async resolveTokenNameToVault(e){return await this.tokenResolver.resolveTokenToVault(e)}}!function(e){e.PROCESSED="PROCESSED",e.COMPLETED="COMPLETED",e.SUCCESS="SUCCESS",e.FAILED="FAILED",e.ERROR="ERROR",e.PROCESSING="PROCESSING",e.PENDING="PENDING"}(Va||(Va={})),function(e){e.PENDING="pending",e.PROCESSING="processing",e.COMPLETED="completed",e.FAILED="failed",e.TIMEOUT="timeout"}(ja||(ja={}));const Xa={[Va.PROCESSED]:ja.COMPLETED,[Va.COMPLETED]:ja.COMPLETED,[Va.SUCCESS]:ja.COMPLETED,[Va.FAILED]:ja.FAILED,[Va.ERROR]:ja.FAILED,[Va.PROCESSING]:ja.PROCESSING,[Va.PENDING]:ja.PENDING};class za{constructor(e,t=!1){this.socket=null,this.listeners=new Map,this.timeouts=new Map,this.reconnectCount=0,this.hasOnAnyListener=!1,this.config={reconnectAttempts:5,reconnectDelay:2e3,timeout:3e5,...e},this.debug=t,this.logger=new A({debug:t,context:"WebSocketService"}),this.isSocketIOAvailable=this.checkSocketIOAvailability()}checkSocketIOAvailability(){try{return"function"==typeof g||(this.logger.warn('โš ๏ธ Socket.IO client not available. Install "socket.io-client" package.'),!1)}catch(e){return this.logger.warn("โš ๏ธ Socket.IO availability check failed:",e),!1}}async connect(){return new Promise((e,t)=>{try{if(!this.isSocketIOAvailable){const e=new Error('Socket.IO not available in current environment. Install "socket.io-client" package.');return this.logger.error("โŒ Socket.IO connection failed:",e.message),void t(e)}this.logger.debug("๐Ÿ”Œ Connecting to Socket.IO server:",this.config.url),this.socket=g(this.config.url,{transports:["websocket"],reconnection:!0,reconnectionAttempts:this.config.reconnectAttempts||5,reconnectionDelay:this.config.reconnectDelay||2e3}),this.socket.on("connect",()=>{this.logger.debug("โœ… Socket.IO connected successfully:",this.socket?.id),this.logger.debug("๐Ÿ“ก Connected to bundle backend WebSocket:",this.config.url),this.logger.debug("๐Ÿ”— Ready to monitor transaction updates"),this.reconnectCount=0,e()}),this.socket.on("connect_error",e=>{this.logger.error("โŒ Socket.IO connection error:",e),t(e)}),this.socket.on("disconnect",e=>{this.logger.debug(`๐Ÿ”Œ Socket.IO disconnected: ${e}`),this.handleReconnect()}),this.socket.on("error",e=>{this.logger.error("โŒ Socket.IO error:",e)}),this.debug&&(this.socket.onAny((e,...t)=>{this.logger.debug(`๐Ÿ“ก [WebSocket Event] "${e}":`,JSON.stringify(t,null,2))}),this.hasOnAnyListener=!0)}catch(e){t(e)}})}async monitorTransaction(e,t){this.listeners.set(e,t),this.logger.debug(`๐Ÿ“ก Starting to monitor transaction: ${e}`),this.logger.debug(`๐Ÿ“ก WebSocket connected: ${!!this.socket&&this.socket.connected}`);const a=setTimeout(()=>{if(this.listeners.has(e)){const a={transactionId:e,status:ja.TIMEOUT,message:"Transaction monitoring timeout - no response after 60 seconds",timestamp:Date.now()};this.logger.debug(`๐Ÿ“ก Transaction timeout for ${e}`),t(a),this.listeners.delete(e),this.timeouts.delete(e),this.socket?.off(e)}},6e4);if(this.timeouts.set(e,a),this.socket&&this.socket.connected)this.socket.off(e),this.logger.debug(`๐Ÿ“ก Listening for transaction updates: ${e}`),this.logger.debug(`๐Ÿ“ก WebSocket connection ID: ${this.socket.id}`),this.logger.debug(`๐Ÿ“ก WebSocket URL: ${this.config.url}`),this.socket.on(e,a=>{this.logger.debug(`๐Ÿ“ก Socket.IO transaction update for ${e}:`,JSON.stringify(a,null,2));const n=a?.status||a?.Status||a?.data?.status||a?.data?.Status;let r=a?.message||a?.Message||a?.data?.message||a?.data?.Message||a?.error||a?.data?.error;r&&"string"==typeof r||(r=n===Va.FAILED||n===Va.ERROR?"Transaction failed - check transaction details":n===Va.COMPLETED||n===Va.PROCESSED||n===Va.SUCCESS?"Transaction completed successfully":n?`Transaction status: ${n}`:"Unknown transaction status");const s={transactionId:e,status:this.mapSocketStatus(n),message:r,timestamp:Date.now(),blockHash:a?.blockHash||a?.data?.blockHash,gasUsed:a?.gasUsed||a?.data?.gasUsed};if(this.logger.debug(`๐Ÿ“ก Mapped status for ${e}: ${n} -> ${s.status}`),this.logger.debug(`๐Ÿ“ก Final message: "${r}"`),t(s),s.status===ja.COMPLETED||s.status===ja.FAILED){this.listeners.delete(e);const t=this.timeouts.get(e);t&&(clearTimeout(t),this.timeouts.delete(e)),this.socket?.off(e),this.logger.debug(`๐Ÿ“ก Cleaned up listener for ${e} (${s.status})`)}});else{const a={transactionId:e,status:ja.FAILED,message:"WebSocket not connected - cannot monitor transaction",timestamp:Date.now()};t(a),this.listeners.delete(e),this.timeouts.delete(e)}}async waitForTransaction(e){return new Promise((t,a)=>{this.monitorTransaction(e,e=>{e.status===ja.COMPLETED?t(e):e.status!==ja.FAILED&&e.status!==ja.TIMEOUT||a(new Error(`Transaction ${e.status}: ${e.message}`))})})}mapSocketStatus(e){const t=e?.toUpperCase();return Xa[t]||ja.PENDING}async handleReconnect(){this.reconnectCount<this.config.reconnectAttempts?(this.reconnectCount++,this.logger.debug(`๐Ÿ”„ Attempting Socket.IO reconnect ${this.reconnectCount}/${this.config.reconnectAttempts}`),setTimeout(()=>{this.socket&&!this.socket.connected&&this.socket.connect()},this.config.reconnectDelay)):this.logger.error("โŒ Socket.IO max reconnection attempts reached")}disconnect(){this.socket&&(this.listeners.forEach((e,t)=>{this.socket?.off(t)}),this.listeners.clear(),this.timeouts.forEach(e=>{clearTimeout(e)}),this.timeouts.clear(),this.hasOnAnyListener&&(this.socket.offAny(),this.hasOnAnyListener=!1,this.logger.debug("๐Ÿงน Removed onAny debug listener")),this.socket.disconnect(),this.socket=null,this.logger.debug("๐Ÿ”Œ Socket.IO disconnected"))}isConnected(){return this.socket?.connected||!1}}class Ya{constructor(e){this.poolService=e,this.cache=new Map}async resolveTokenToVault(e){if(!F(e))throw new b("Token name is required and must be a non-empty string","tokenName","INVALID_TOKEN_NAME");const t=e.trim().toLowerCase(),a=this.get(t);if(a)return a;try{const a=await this.poolService.resolveTokenNameToVault(e);return a&&this.set(t,a),a}catch{return null}}async resolveTokenClassKey(e){const t=await this.resolveTokenToVault(e);if(!t)throw aa(e);return this.parseVaultAddressToTokenClassKey(t)}get(e){return this.cache.get(e.toLowerCase())||null}set(e,t){this.cache.set(e.toLowerCase(),t)}clear(){this.cache.clear()}getStats(){return{size:this.cache.size,keys:Array.from(this.cache.keys())}}preWarm(e){for(const{tokenName:t,vaultAddress:a}of e)this.set(t,a)}parseVaultAddressToTokenClassKey(e){const t=e.split("|");if(2!==t.length)throw ra("vaultAddress","format: service|Token$Unit$...$launchpad","Vault address");const a=t[1].split("$");if(a.length<4)throw ra("vaultAddress","at least 4 parts after service|","Vault address");return{collection:a[0],category:a[1],type:a[2],additionalKey:a[3]}}}function Ja(e){const t=function(e){const t=ht(e);return t.success?[]:t.errors||["Unknown validation error"]}(e);if(t.length>0)throw new Error(`LaunchTokenData validation failed:\n${t.map(e=>`- ${e}`).join("\n")}`)}const Za="/api/asset/launchpad-contract/CallNativeTokenIn",en="/api/asset/launchpad-contract/CallNativeTokenOut",tn="/api/asset/launchpad-contract/CallMemeTokenIn",an="/api/asset/launchpad-contract/CallMemeTokenOut";class nn extends i{constructor(e){super(),this.tokenName=e.tokenName,this.tokenSymbol=e.tokenSymbol,this.tokenDescription=e.tokenDescription,this.tokenImage=e.tokenImage,this.preBuyQuantity=e.preBuyQuantity,this.websiteUrl=e.websiteUrl,this.telegramUrl=e.telegramUrl,this.twitterUrl=e.twitterUrl,this.tokenCategory=e.tokenCategory,this.tokenCollection=e.tokenCollection,this.uniqueKey=e.uniqueKey,e.reverseBondingCurveConfiguration&&(this.reverseBondingCurveConfiguration=e.reverseBondingCurveConfiguration)}}function rn(e){return e&&"object"==typeof e&&"number"==typeof e.Status&&e.Data&&"object"==typeof e.Data&&"string"==typeof e.Data.calculatedQuantity&&e.Data.extraFees&&"object"==typeof e.Data.extraFees&&"string"==typeof e.Data.extraFees.reverseBondingCurve&&"string"==typeof e.Data.extraFees.transactionFees}const sn={NATIVE:"native",EXACT:"exact"},on={RECENT:"recent",POPULAR:"popular"};class cn{constructor(e,t,a,n,r,s){this.http=e,this.tokenResolver=t,this.logger=a,this.bundleHttp=n,this.galaChainHttp=r,this.dexApiHttp=s}async uploadImageByTokenName(e){const{tokenName:t,options:a}=e;Ct(t);const n=`${t}.png`;Ta(a.file,n,"image/png");try{const e=new FormData;if("undefined"!=typeof File&&a.file instanceof File)e.append("image",a.file);else{if(!Buffer.isBuffer(a.file))throw ra("file","a File object (browser) or Buffer (Node.js)");{const n=`${a.tokenName||t}.png`,r=new Blob([a.file],{type:"image/png"});e.append("image",r,n)}}const n=await this.http.request({method:"POST",url:`/launchpad/upload-image?tokenName=${encodeURIComponent(a.tokenName||t)}`,data:e,headers:{}});if(!0===n.error||200!==n.status||!n.data?.imageUrl)throw sa(n.message||"Image upload failed - no URL returned",n.status);return n.data.imageUrl}catch(e){if(e instanceof Error&&e.message.includes("FormData"))throw oa("File upload failed: FormData not supported in this environment. Ensure you have proper polyfills for Node.js environments.","FormData");throw e}}async fetchPoolsFromAPI(e){_t(e),e.tokenName&&Ct(e.tokenName);const t={page:e.page.toString(),limit:e.limit.toString()};void 0!==e.type&&(t.type=e.type),void 0!==e.tokenName&&(t.tokenName=e.tokenName),void 0!==e.search&&(t.search=e.search);const a=w(t),n=await this.http.get("/launchpad/fetch-pool",a);if(!0===n.error||200!==n.status||!n.data)throw sa(n.message||"Failed to fetch pools",n.status);let r=[];return n.data.tokens?r=Array.isArray(n.data.tokens)?n.data.tokens.map(e=>({...e,createdAt:new Date(e.created_at||e.createdAt)})):[{...n.data.tokens,createdAt:new Date(n.data.tokens.created_at||n.data.tokens.createdAt)}]:n.data.pools&&Array.isArray(n.data.pools)&&(r=n.data.pools.map(e=>({...e,createdAt:new Date(e.created_at||e.createdAt)}))),{pools:r,page:n.data.page,limit:n.data.limit,total:n.data.total,totalPages:n.data.totalPages,hasNext:n.data.page<n.data.totalPages,hasPrevious:n.data.page>1}}async _getAmount(e){if(Pt(e),!this.galaChainHttp)throw oa("GalaChain client not configured. Direct GalaChain calls require galaChainHttp client.","galaChainHttp");const{endpoint:t,body:a}=((e,t,a,n)=>{if("NATIVE"===e&&"IN"===t)return{endpoint:Za,body:{vaultAddress:a,tokenQuantity:n,IsPreMint:!1}};if("NATIVE"===e&&"OUT"===t)return{endpoint:en,body:{vaultAddress:a,tokenQuantity:n,IsPreMint:!1}};if("MEME"===e&&"IN"===t)return{endpoint:tn,body:{vaultAddress:a,nativeTokenQuantity:n,IsPreMint:!1}};if("MEME"===e&&"OUT"===t)return{endpoint:an,body:{vaultAddress:a,nativeTokenQuantity:n,IsPreMint:!1}};throw ra("type-method","one of: NATIVE-IN, NATIVE-OUT, MEME-IN, MEME-OUT")})(e.type,e.method,e.vaultAddress,e.amount);try{const e=await this.galaChainHttp.post(t,a);if(!rn(e))throw sa("Malformed response data from GalaChain gateway");if(1!==e.Status)throw sa(`GalaChain calculation failed with status ${e.Status}`,e.Status);const{calculatedQuantity:n,extraFees:r}=e.Data;return{amount:n,reverseBondingCurveFee:r.reverseBondingCurve,transactionFee:r.transactionFees,gasFee:"1"}}catch(n){throw this.logger.error(`GalaChain ${e.type}-${e.method} operation failed:`,{endpoint:t,requestBody:a,error:n instanceof Error?n.message:n}),n}}async checkPool(e){Ut(e),e.tokenName&&Ct(e.tokenName);const t=w(e),a=await this.http.get("/launchpad/check-pool",t);if(!0===a.error||200!==a.status)throw sa(a.message||"Failed to check pool",a.status);const n=a.data;return e.symbol?n?.isSymbolExist??!1:e.tokenName?n?.isNameExist??!1:n?.exists??!1}async fetchVolumeData(e){if(!ca(e))throw new b("Invalid options provided. Expected { tokenName: string, from?: number, to?: number, resolution?: number }","options","INVALID_OPTIONS");const{tokenName:t,from:a,to:n,resolution:r}=e;if(Ct(t),!a||!n||!r)throw new b("Graph options (from, to, resolution) are required","options","MISSING_GRAPH_OPTIONS");const s={tokenName:t,from:a,to:n,resolution:r};Lt(s);const o=w(s),i=await this.http.get("/launchpad/get-graph-data",o);if(!0===i.error||200!==i.status||!i.data)throw sa(i.message||"Failed to fetch graph data",i.status);return{dataPoints:i.data}}async fetchPools(e={}){let t;"recent"===e.type?t="RECENT":"popular"===e.type&&(t="POPULAR");const a={page:e.page||1,limit:e.limit||10};return e.search&&(a.search=e.search),e.tokenName&&(a.tokenName=e.tokenName),t&&(a.type=t),this.fetchPoolsFromAPI(a)}async isTokenNameAvailable(e){try{return!await this.checkPool({tokenName:e})}catch{return!1}}async isTokenSymbolAvailable(e){try{return!await this.checkPool({symbol:e})}catch{return!1}}async calculateBuyAmount(e){if(!e||"object"!=typeof e)throw new b("Invalid options provided. Expected an options object.","options","INVALID_OPTIONS");const{tokenName:t,amount:a,type:n}=e;if(!t||"string"!=typeof t)throw new b("Token name is required and must be a string","tokenName","INVALID_TOKEN_NAME");if(!a||"string"!=typeof a)throw new b("Amount is required and must be a string","amount","INVALID_AMOUNT");const r=await this.tokenResolver.resolveTokenToVault(t);if(!r)throw new b(`Token "${t}" not found. Please verify the token name is correct.`,"tokenName","TOKEN_NOT_FOUND");if(n!==sn.NATIVE&&n!==sn.EXACT)throw new b('Type must be either "native" or "exact"',"type","INVALID_TYPE");return n===sn.EXACT?this._getAmount({type:"NATIVE",method:"IN",vaultAddress:r,amount:a}):this._getAmount({type:"MEME",method:"OUT",vaultAddress:r,amount:a})}async calculateSellAmount(e){const{tokenName:t,amount:a,type:n}=e,r=await this.tokenResolver.resolveTokenToVault(t);if(!r)throw new b(`Token "${t}" not found. Please verify the token name is correct.`,"tokenName","TOKEN_NOT_FOUND");return n===sn.EXACT?this._getAmount({type:"NATIVE",method:"OUT",vaultAddress:r,amount:a}):this._getAmount({type:"MEME",method:"IN",vaultAddress:r,amount:a})}async calculateBuyAmountForGraduation(e){Ct(e);const t=await this.tokenResolver.resolveTokenToVault(e);if(!t)throw new b(T(e),"tokenName","VAULT_NOT_FOUND");if(!this.galaChainHttp)throw oa("GalaChain HTTP client not configured");const a=await this.galaChainHttp.post("/api/asset/launchpad-contract/FetchSaleDetails",{vaultAddress:t});if(1!==a.Status)throw sa(`Failed to fetch pool details: Status ${a.Status}`,a.Status);const n=a.Data.sellingTokenQuantity;if("0"===n)throw new b(`Token ${e} is already graduated (no tokens remaining in pool)`,"tokenName","ALREADY_GRADUATED");return await this.calculateBuyAmount({tokenName:e,amount:n,type:"exact"})}async launchToken(e){if(!this.bundleHttp)throw oa("Bundle backend client not configured. LaunchToken requires bundleHttp client.","bundleHttp");Ja(e);const t=e.preBuyQuantity||"0";if(isNaN(Number(t))||Number(t)<0)throw new b("Pre-buy quantity must be a valid non-negative number string","preBuyQuantity","INVALID_PRE_BUY_QUANTITY");if(e.reverseBondingCurveConfiguration){const{minFeePortion:t,maxFeePortion:a}=e.reverseBondingCurveConfiguration,n=Number(t),r=Number(a);if(isNaN(n)||isNaN(r)||n<=0||r<=0||n>=r)throw new b("Reverse bonding curve configuration must have valid min/max fee portions with min < max","reverseBondingCurveConfiguration","INVALID_BONDING_CURVE_CONFIG")}let a="";if(e.tokenImage)if(e.tokenImage instanceof File||Buffer.isBuffer(e.tokenImage)){const t=await this.uploadImageByTokenName({tokenName:e.tokenName,options:{file:e.tokenImage,tokenName:e.tokenName}});if(!t)throw sa("Image upload failed: No URL returned");a=t}else"string"==typeof e.tokenImage&&(a=e.tokenImage);const n=`galaswap - operation - ${h()}-${Date.now()}-${this.http.getAddress()}`,r={tokenName:e.tokenName.trim(),tokenSymbol:e.tokenSymbol.trim().toUpperCase(),tokenDescription:e.tokenDescription.trim(),tokenImage:a.trim(),preBuyQuantity:t.toString(),websiteUrl:e.websiteUrl||"",telegramUrl:e.telegramUrl||"",twitterUrl:e.twitterUrl||"",tokenCategory:e.tokenCategory||"Unit",tokenCollection:e.tokenCollection||"Token",uniqueKey:n};e.reverseBondingCurveConfiguration&&(r.reverseBondingCurveConfiguration={minFeePortion:e.reverseBondingCurveConfiguration.minFeePortion.toString(),maxFeePortion:e.reverseBondingCurveConfiguration.maxFeePortion.toString()});const o=new nn(r),i=await this.http.signWithGalaChain("CreateSale",o,s.SIGN_TYPED_DATA),{signature:c,types:l,domain:d,prefix:u}=i,g={tokenName:o.tokenName,tokenSymbol:o.tokenSymbol,tokenDescription:o.tokenDescription,tokenImage:o.tokenImage,preBuyQuantity:o.preBuyQuantity,websiteUrl:o.websiteUrl,telegramUrl:o.telegramUrl,twitterUrl:o.twitterUrl,tokenCategory:o.tokenCategory,tokenCollection:o.tokenCollection,uniqueKey:o.uniqueKey,signature:c,types:l,domain:d,...u&&{prefix:u},...o.reverseBondingCurveConfiguration&&{reverseBondingCurveConfiguration:o.reverseBondingCurveConfiguration}},p=`${e.tokenName.trim()}$Unit$none$none`,m="GALA$Unit$none$none";let f;if(parseFloat(t)>0){const e=`$service$${p}$launchpad`;f=[e,`$token$${p}$${e}`,`$tokenBalance$${p}$${e}`,`$tokenBalance$${p}$${e}`,`$tokenBalance$${m}$${e}`,`$tokenBalance$${m}$${e}`]}else{const e=`$service$${p}$launchpad`;f=[e,`$token$${p}$${e}`,`$tokenBalance$${p}$${e}`]}const y={signedDto:g,stringsInstructions:f,method:"CreateSale"},A=await this.bundleHttp.post("/bundle",y);if(A.error||!A.data)throw sa(A.message||"Token launch failed");return A.data}async fetchTokenDistribution(e){if(!e)throw na("tokenName","Token name");Ct(e);const t=await this.tokenResolver.resolveTokenToVault(e);if(!t)throw new b(T(e),"tokenName","VAULT_NOT_FOUND");const a=encodeURIComponent(t),n=await this.http.get(`/holders/${a}`);if(!0===n.error||200!==n.status||!n.data)throw sa(n.message||"Failed to fetch token distribution",n.status);return{holders:n.data.holders||[],totalSupply:n.data.totalSupply||"0",totalHolders:n.data.totalHolders||0,lastUpdated:new Date}}async fetchTokenBadges(e){if(!e)throw na("tokenName","Token name");Ct(e);const t=await this.http.get("/launchpad/get-badge/",{tokenName:e});if(t.error||!t.data)throw sa(t.message||"Failed to fetch token badges");return{volumeBadges:t.data.volumeBadge||[],engagementBadges:t.data.engagementBadge||[]}}async hasTokenBadgeByTokenName(e){const{tokenName:t,badgeType:a,badgeName:n}=e;try{const e=await this.fetchTokenBadges(t);if(!e)return!1;const r=("volume"===a?e.volumeBadges:e.engagementBadges).find(e=>e.badgeName===n);return r?.isActive||!1}catch{return!1}}async calculateInitialBuyAmount(e){if(!(t=e)||"object"!=typeof t||"string"!=typeof t.nativeTokenQuantity||void 0!==t.vaultAddress&&"string"!=typeof t.vaultAddress)throw new b("Invalid pre-mint calculation data","data","INVALID_PRE_MINT_DATA");var t;if(!this.galaChainHttp)throw oa("GalaChain HTTP client not available. Please initialize SDK with galaChainBaseUrl.","galaChainHttp");try{const t={vaultAddress:"service|testToken",nativeTokenQuantity:e.nativeTokenQuantity,IsPreMint:!0},a=await this.galaChainHttp.post("/api/asset/launchpad-contract/CallMemeTokenOut",t);if(!rn(a))throw sa("Malformed response data from GalaChain gateway");if(1!==a.Status)throw sa(`GalaChain calculation failed with status ${a.Status}`,a.Status);const{calculatedQuantity:n,extraFees:r}=a.Data;return{amount:n,reverseBondingCurveFee:r.reverseBondingCurve,transactionFee:r.transactionFees,gasFee:"1"}}catch(e){if(e instanceof Error){const t=new Error(`Pre-mint calculation failed: ${e.message}`);throw e.stack&&(t.stack=e.stack),t}throw new Error(`Pre-mint calculation failed: ${String(e)}`)}}getAddress(){return this.http.getAddress()}formatAddressForBackend(e){return Ot(e)}validateTokenName(e){return Ct(e)}validatePagination(e){return _t(e)}async fetchTokenSpotPrice(e){if(!this.dexApiHttp)throw oa("DEX API client not configured. Token price fetching requires dexApiHttp client.","dexApiHttp");if(!e||Array.isArray(e)&&0===e.length)throw na("symbols","At least one symbol");const t=Array.isArray(e)?e.join(","):e;try{const e=await this.dexApiHttp.request({method:"GET",url:"/v1/tokens",params:{symbols:t}}),a=[];return e.tokens&&Array.isArray(e.tokens)&&e.tokens.forEach(e=>{e.currentPrices&&e.symbol&&a.push({symbol:e.symbol,price:e.currentPrices.usd})}),a}catch(e){throw sa(`Failed to fetch token prices: ${e instanceof Error?e.message:e}`,void 0,e instanceof Error?e:void 0)}}async fetchLaunchpadTokenSpotPrice(e){if(!e||"string"!=typeof e)throw na("tokenName","Token name (string)");try{const t=await this.calculateBuyAmount({tokenName:e,amount:"1",type:"native"}),a=(await this.fetchTokenSpotPrice("GALA"))[0];if(!a)throw sa("GALA price not available");const n=Number(t.amount)/1e18;if(n<=0)throw new b(`Invalid token amount calculation: ${n}`,"amount","INVALID_CALCULATION");const r=a.price/n;return{symbol:e.toUpperCase(),price:r}}catch(t){if(t instanceof Error)throw new Error(`Failed to calculate launchpad token spot price for ${e}: ${t.message}`);throw new Error(`Failed to calculate launchpad token spot price for ${e}: ${String(t)}`)}}}const ln={PROD:{launchpadBaseUrl:"https://lpad-backend-prod1.defi.gala.com",galaChainBaseUrl:"https://gateway-mainnet.galachain.com",bundleBaseUrl:"https://bundle-backend-prod1.defi.gala.com",webSocketUrl:"https://bundle-backend-prod1.defi.gala.com",dexApiBaseUrl:"https://dex-api-platform-dex-prod-gala.gala.com",launchpadFrontendUrl:"https://lpad-frontend-prod1.defi.gala.com"},STAGE:{launchpadBaseUrl:"https://lpad-backend-dev1.defi.gala.com",galaChainBaseUrl:"https://galachain-gateway-chain-platform-stage-chain-platform-eks.stage.galachain.com",bundleBaseUrl:"https://bundle-backend-dev1.defi.gala.com",webSocketUrl:"https://bundle-backend-dev1.defi.gala.com",dexApiBaseUrl:"https://dex-api-platform-dex-stage-gala.gala.com",launchpadFrontendUrl:"https://lpad-frontend-dev1.defi.gala.com"}};function dn(e){return ln[e]}class un extends Error{constructor(e,t){super(e),this.cause=t,this.name="WebSocketError"}}class hn extends un{constructor(e,t){super(`WebSocket confirmation timeout for transaction ${e} after ${t}ms`),this.name="WebSocketTimeoutError"}}class gn extends Error{constructor(e,t,a){super(`Transaction ${e} failed with status: ${t}${a?` - ${a}`:""}`),this.transactionId=e,this.status=t,this.name="TransactionFailedError"}}function pn(e,t){if(!e)throw new un(`Invalid WebSocket response received for transaction ${t}: response is null or undefined`);if("object"!=typeof e)throw new un(`Invalid WebSocket response received for transaction ${t}: expected object, got ${typeof e}`);if(!Object.prototype.hasOwnProperty.call(e,"status")&&!Object.prototype.hasOwnProperty.call(e,"Status"))throw new un(`Invalid WebSocket response received for transaction ${t}: missing status field`)}function mn(e,t,a,n){pn(e,t);const r=e?.data||{};if(!(s=r)||"object"!=typeof s||void 0!==s.inputQuantity&&"string"!=typeof s.inputQuantity||void 0!==s.outputQuantity&&"string"!=typeof s.outputQuantity||void 0!==s.totalFees&&"string"!=typeof s.totalFees||void 0!==s.vaultAddress&&"string"!=typeof s.vaultAddress)throw new un(`Invalid trade data received for transaction ${t}`);var s;const o={transactionId:t,type:a,method:"native"===n.type?"native":"exact",inputAmount:r.inputQuantity||n.amount,outputAmount:r.outputQuantity||n.expectedAmount||"0",totalFees:r.totalFees||"0",tokenName:n.tokenName,vaultAddress:r.vaultAddress||"",blockHash:e.blockHash,gasUsed:e.gasUsed,timestamp:Date.now()};return void 0!==n.slippageToleranceFactor&&(o.slippageTolerance=n.slippageToleranceFactor),o}class fn{constructor(e){let t=null;t=e.env?dn(e.env):e.baseUrl?.includes("prod")?dn("PROD"):dn("STAGE"),this.config={baseUrl:t.launchpadBaseUrl,galaChainBaseUrl:t.galaChainBaseUrl,bundleBaseUrl:t.bundleBaseUrl,webSocketUrl:t.webSocketUrl,dexApiBaseUrl:t.dexApiBaseUrl,launchpadFrontendUrl:t.launchpadFrontendUrl,timeout:3e4,debug:!1,...e},this.logger=new A({debug:this.config.debug??!1,context:"LaunchpadSDK"}),this.validateConfiguration(),this.slippageToleranceFactor=void 0===e.slippageToleranceFactor?fn.DEFAULT_SLIPPAGE_TOLERANCE_FACTOR:this.parseSlippageToleranceFactor(e.slippageToleranceFactor),this.maxAcceptableReverseBondingCurveFeeSlippageFactor=void 0===e.maxAcceptableReverseBondingCurveFeeSlippageFactor?fn.DEFAULT_MAX_ACCEPTABLE_REVERSE_BONDING_CURVE_FEE_SLIPPAGE_FACTOR:this.parseFeeSlippageFactor(e.maxAcceptableReverseBondingCurveFeeSlippageFactor),this.auth=new y({wallet:e.wallet,messagePrefix:"Create a GalaChain Wallet"}),this.http=new k(this.auth,this.config),this.galaChainHttp=new k(this.auth,{...this.config,baseUrl:this.config.galaChainBaseUrl}),this.bundleHttp=new k(this.auth,{...this.config,baseUrl:this.config.bundleBaseUrl}),this.dexApiHttp=new k(this.auth,{...this.config,baseUrl:this.config.dexApiBaseUrl}),this.launchpadService=new Na(this.http),this.tokenResolverService=new Ya(this.launchpadService.poolService),this.launchpadAPI=new cn(this.http,this.tokenResolverService,this.logger,this.bundleHttp,this.galaChainHttp,this.dexApiHttp),this.galaChainService=new Ua(this.galaChainHttp,e.wallet,this.tokenResolverService,e.debug||!1),this.dexService=new Pa(this.dexApiHttp),this.bundleService=new Qa(this.bundleHttp,this.tokenResolverService,this.config.debug||!1,e.wallet,this.getAddress(),this.slippageToleranceFactor,this.maxAcceptableReverseBondingCurveFeeSlippageFactor),this.websocketService=new za({url:this.config.webSocketUrl},this.config.debug)}createOverrideSdk(e){if(!e||"string"!=typeof e)throw oa("Invalid privateKey: must be a non-empty string","privateKey");if(!e.match(/^0x[a-fA-F0-9]{64}$/))throw oa('Invalid privateKey format: must be "0x" followed by 64 hexadecimal characters',"privateKey");const a=new t(e),n={...this.config,wallet:a};return new fn(n)}getAddress(){return this.auth.getAddress()}getEthereumAddress(){return this.config.wallet.address}getConfig(){const{wallet:e,...t}=this.config;return{...t,slippageToleranceFactor:this.slippageToleranceFactor,maxAcceptableReverseBondingCurveFeeSlippageFactor:this.maxAcceptableReverseBondingCurveFeeSlippageFactor}}getUrlByTokenName(e){const t=this.config.launchpadFrontendUrl;if(!t)throw oa("launchpadFrontendUrl not configured in SDK","launchpadFrontendUrl");return`${t.replace(/\/$/,"")}/buy-sell/${e}`}async fetchPools(e){return this.launchpadService.fetchPools(e||{})}async fetchTokenDistribution(e){return this.launchpadService.fetchTokenDistribution(e)}async fetchTokenBadges(e){return this.launchpadService.fetchTokenBadges(e)}async fetchTokenSpotPrice(e){return this.dexService.fetchTokenSpotPrice(e)}async fetchGalaSpotPrice(){const e=await this.dexService.fetchTokenSpotPrice("GALA");if(0===e.length)throw sa("Failed to fetch GALA price - no price data returned");const t=e.find(e=>"GALA"===e.symbol);if(!t)throw sa("GALA price not found in response");return t}async fetchLaunchpadTokenSpotPrice(e){return this.launchpadAPI.fetchLaunchpadTokenSpotPrice(e)}async fetchLaunchTokenFee(){return this.galaChainService.fetchLaunchTokenFee()}async fetchPoolDetails(e){const t=await this.resolveVaultAddress(e);if(!t)throw new Error(T(e));return(await this.galaChainService.fetchPoolDetails({vaultAddress:t})).Data}async fetchVolumeData(e){return this.launchpadService.fetchVolumeData(e)}async fetchTrades(e){return this.launchpadService.fetchTrades(e)}async fetchGalaBalance(e){const{normalizeAddressInput:t}=await Promise.resolve().then(function(){return xt}),a=t(e)||this.getAddress();return this.galaChainService.fetchGalaBalance({owner:a,collection:"GALA",category:"Unit",additionalKey:"none",type:"none",instance:"0"})}async fetchTokenBalance(e){const{normalizeAddressInput:t}=await Promise.resolve().then(function(){return xt}),a=t(e.address);if(e.tokenId){const{normalizeToTokenInstanceKey:t}=await Promise.resolve().then(function(){return Fa}),n=t(e.tokenId),{collection:r,category:s,type:o,additionalKey:i}=n;return this.galaChainService.fetchTokenBalance({owner:a,collection:r,category:s,additionalKey:i,type:o,instance:"0"})}if(e.tokenName){const t=(await this.fetchTokensHeld({tokenName:e.tokenName,page:1,limit:1,...a&&{address:a}})).tokens[0];return t?{quantity:t.quantity,collection:t.collection||"Token",category:"Unit",tokenId:`${t.collection||"Token"}|Unit|${t.symbol}|none`,symbol:t.symbol,name:t.name}:null}throw na("tokenId or tokenName","Either tokenId or tokenName")}async fetchComments(e){return this.launchpadService.fetchComments(e)}async calculateBuyAmount(e){return this.launchpadAPI.calculateBuyAmount(e)}async calculateSellAmount(e){return this.launchpadAPI.calculateSellAmount(e)}async calculateBuyAmountForGraduation(e){return this.launchpadAPI.calculateBuyAmountForGraduation(e)}async graduateToken(e){const{tokenName:t,slippageToleranceFactor:a,maxAcceptableReverseBondingCurveFeeSlippageFactor:n,privateKey:r}=e,s=await this.calculateBuyAmountForGraduation(t),o={tokenName:t,amount:s.amount,type:"exact",expectedAmount:s.amount,maxAcceptableReverseBondingCurveFee:s.reverseBondingCurveFee,slippageToleranceFactor:this.slippageToleranceFactor};return void 0!==a&&(o.slippageToleranceFactor=a),void 0!==n&&(o.maxAcceptableReverseBondingCurveFeeSlippageFactor=n),void 0!==r&&(o.privateKey=r),await this.buy(o)}async calculateInitialBuyAmount(e){const t={nativeTokenQuantity:e};return this.launchpadAPI.calculateInitialBuyAmount(t)}async buy(e){if(e.privateKey){const t=this.createOverrideSdk(e.privateKey),{privateKey:a,...n}=e;return t.buy(n)}await this.ensureWebSocketConnection();const t=await this.bundleService.buyToken(e),a=t.data?.transactionId;if(!a)throw ia("No transaction ID returned from buy operation");return this.waitForConfirmation(a,t=>mn(t,a,"buy",e))}async sell(e){if(e.privateKey){const t=this.createOverrideSdk(e.privateKey),{privateKey:a,...n}=e;return t.sell(n)}await this.ensureWebSocketConnection();const t=await this.bundleService.sellToken(e),a=t.data?.transactionId;if(!a)throw ia("No transaction ID returned from sell operation");return this.waitForConfirmation(a,t=>mn(t,a,"sell",e))}async getBundlerTransactionResult(e){return this.bundleService.getBundlerTransactionResult(e)}async postComment(e){if(e.privateKey){const t=this.createOverrideSdk(e.privateKey),{privateKey:a,...n}=e;return t.postComment(n)}return this.launchpadService.postComment(e)}async launchToken(e){if(e.privateKey){const t=this.createOverrideSdk(e.privateKey),{privateKey:a,...n}=e;return t.launchToken(n)}await this.ensureWebSocketConnection();const t=await this.launchpadAPI.launchToken(e);return this.waitForConfirmation(t,a=>{pn(a,t);const n=a?.data||{};if(!function(e){return!(!e||"object"!=typeof e||void 0!==e.vaultAddress&&"string"!=typeof e.vaultAddress||void 0!==e.tokenStringKey&&"string"!=typeof e.tokenStringKey||void 0!==e.creatorAddress&&"string"!=typeof e.creatorAddress)}(n))throw new un(`Invalid launch data received for transaction ${t}`);const r={transactionId:t,vaultAddress:n.vaultAddress||"",tokenStringKey:n.tokenStringKey||"",tokenName:e.tokenName,tokenSymbol:e.tokenSymbol,creatorAddress:n.creatorAddress||this.getAddress(),timestamp:Date.now(),...a.blockHash&&{blockHash:a.blockHash},...a.gasUsed&&{gasUsed:a.gasUsed}};return"string"==typeof e.tokenImage&&(r.tokenImage=e.tokenImage),void 0!==e.preBuyQuantity&&(r.preBuyQuantity=e.preBuyQuantity),r.vaultAddress&&this.tokenResolverService.set(e.tokenName,r.vaultAddress),r})}async uploadTokenImage(e){if(e.privateKey){const t=this.createOverrideSdk(e.privateKey),{privateKey:a,...n}=e;return t.uploadTokenImage(n)}return this.launchpadService.uploadImageByTokenName(e)}async isTokenNameAvailable(e){return this.launchpadService.isTokenNameAvailable(e)}async isTokenSymbolAvailable(e){return this.launchpadService.isTokenSymbolAvailable(e)}async fetchProfile(e){const{normalizeAddressInput:t}=await Promise.resolve().then(function(){return xt}),a=t(e)||this.getAddress();return this.launchpadService.fetchProfile(a)}async updateProfile(e){const{normalizeAddressInput:t}=await Promise.resolve().then(function(){return xt}),a={...e,address:t(e.address)};if(a.privateKey){const e=this.createOverrideSdk(a.privateKey),{privateKey:t,...n}=a;return e.updateProfile(n)}return this.launchpadService.updateProfile(a)}async uploadProfileImage(e){const{normalizeAddressInput:t}=await Promise.resolve().then(function(){return xt}),a={...e,address:t(e.address)||this.getAddress()};if(a.privateKey){const e=this.createOverrideSdk(a.privateKey),{privateKey:t,...n}=a;return e.uploadProfileImage(n)}return this.launchpadService.uploadProfileImage(a)}async retrieveGalaFromFaucet(e){const{normalizeAddressInput:t}=await Promise.resolve().then(function(){return xt}),a={walletAddress:t(e)||this.getAddress(),amount:"5"};return this.launchpadService.transferFaucets(a)}async fetchTokensHeld(e){const{normalizeAddressInput:t}=await Promise.resolve().then(function(){return xt}),a=t(e?.address)||this.getAddress(),n={page:e?.page||1,limit:e?.limit||10,address:a};return e?.tokenName&&(n.tokenName=e.tokenName),e?.search&&(n.search=e.search),this.launchpadService.fetchTokensHeld(n)}async fetchTokensCreated(e){const{normalizeAddressInput:t}=await Promise.resolve().then(function(){return xt}),a={type:"DEFI",address:t(e?.address)||this.getAddress(),page:e?.page||1,limit:e?.limit||10};return e?.tokenName&&(a.tokenName=e.tokenName),e?.search&&(a.search=e.search),this.launchpadService.fetchTokenList(a)}async transferGala(e){const{normalizeAddressInput:t}=await Promise.resolve().then(function(){return xt}),a={...e,recipientAddress:t(e.recipientAddress)};if(a.privateKey){const e=this.createOverrideSdk(a.privateKey),{privateKey:t,...n}=a;return e.transferGala(n)}return this.galaChainService.transferGala(a)}async transferToken(e){const{normalizeAddressInput:t}=await Promise.resolve().then(function(){return xt}),a={...e,to:t(e.to)};if(a.privateKey){const e=this.createOverrideSdk(a.privateKey),{privateKey:t,...n}=a;return e.transferToken(n)}return this.galaChainService.transferToken(a)}async resolveTokenClassKey(e){return this.galaChainService.resolveTokenClassKey(e)}async resolveVaultAddress(e){return this.tokenResolverService.resolveTokenToVault(e)}validateConfiguration(){if(("number"!=typeof this.config.timeout||this.config.timeout<=0||this.config.timeout>3e5)&&(this.logger.warn(`Invalid timeout value: ${this.config.timeout}. Using default 30000ms.`),this.config.timeout=3e4),!this.config.baseUrl)throw oa("baseUrl is required in configuration","baseUrl");if(!this.config.webSocketUrl)throw oa("webSocketUrl is required in configuration","webSocketUrl");try{new URL(this.config.baseUrl)}catch{throw oa(`Invalid baseUrl format: ${this.config.baseUrl}`,"baseUrl")}try{new URL(this.config.webSocketUrl)}catch{throw oa(`Invalid webSocketUrl format: ${this.config.webSocketUrl}`,"webSocketUrl")}if(this.config.galaChainBaseUrl)try{new URL(this.config.galaChainBaseUrl)}catch{throw oa(`Invalid galaChainBaseUrl format: ${this.config.galaChainBaseUrl}`,"galaChainBaseUrl")}if(this.config.bundleBaseUrl)try{new URL(this.config.bundleBaseUrl)}catch{throw oa(`Invalid bundleBaseUrl format: ${this.config.bundleBaseUrl}`,"bundleBaseUrl")}if(this.config.launchpadFrontendUrl)try{new URL(this.config.launchpadFrontendUrl)}catch{throw oa(`Invalid launchpadFrontendUrl format: ${this.config.launchpadFrontendUrl}`,"launchpadFrontendUrl")}}parseSlippageToleranceFactor(e){const t=parseFloat(String(e));return isNaN(t)||t<0||t>1?(this.logger.warn(`Invalid slippage tolerance factor: ${e}, using default: ${fn.DEFAULT_SLIPPAGE_TOLERANCE_FACTOR}`),fn.DEFAULT_SLIPPAGE_TOLERANCE_FACTOR):t}parseFeeSlippageFactor(e){const t=parseFloat(String(e));return isNaN(t)||t<0||t>1?(this.logger.warn(`Invalid fee slippage factor: ${e}, using default: ${fn.DEFAULT_MAX_ACCEPTABLE_REVERSE_BONDING_CURVE_FEE_SLIPPAGE_FACTOR}`),fn.DEFAULT_MAX_ACCEPTABLE_REVERSE_BONDING_CURVE_FEE_SLIPPAGE_FACTOR):t}async ensureWebSocketConnection(){this.websocketService.isConnected()||(await this.websocketService.connect(),this.logger.debug("WebSocket connection established"))}async waitForConfirmation(e,t){this.logger.debug(`Waiting for confirmation of transaction: ${e}`);try{const a=await this.websocketService.waitForTransaction(e);if("completed"!==a.status)throw new gn(e,a.status,a.message);let n;try{n=t(a)}catch(t){if(t instanceof un)throw t;throw new un(`Failed to transform WebSocket response for transaction ${e}`,t instanceof Error?t:new Error(String(t)))}return this.logger.debug(`Transaction confirmed: ${e}`,n),n}catch(t){if(this.logger.error(`Transaction confirmation failed: ${e}`,t),t instanceof gn||t instanceof un)throw t;throw new un(`WebSocket confirmation failed for transaction ${e}`,t instanceof Error?t:new Error(String(t)))}}async cleanup(){try{this.logger.debug("Starting cleanup..."),this.http.cleanup(),this.websocketService&&this.websocketService.disconnect(),this.logger.debug("Cleanup completed")}catch(e){this.logger.error("Error during cleanup:",e)}}static cleanupAll(e=!1){const t=new A({debug:e,context:"LaunchpadSDK"});t.debug("Starting global cleanup...");const{WebSocketService:a}=require("./services/WebSocketService");a.cleanupAll(e),t.debug("Global cleanup completed")}}fn.DEFAULT_SLIPPAGE_TOLERANCE_FACTOR=.15,fn.DEFAULT_MAX_ACCEPTABLE_REVERSE_BONDING_CURVE_FEE_SLIPPAGE_FACTOR=.01;class yn{static generateWallet(){try{const e=t.createRandom();if(!e.mnemonic?.phrase)throw new Error("Failed to generate wallet with mnemonic phrase");const a=this.toGalaAddress(e.address);return{privateKey:e.privateKey,address:e.address,galaAddress:a,mnemonic:e.mnemonic.phrase,wallet:new t(e.privateKey)}}catch(e){if("undefined"!=typeof process&&"test"===process.env.NODE_ENV){const e=`test-wallet-${Date.now()}-${++this.testCounter}`,a="0x"+Buffer.from(e).toString("hex").padStart(64,"1").slice(0,64),n=new t(a),r=this.toGalaAddress(n.address);return{privateKey:n.privateKey,address:n.address,galaAddress:r,mnemonic:"test test test test test test test test test test test junk",wallet:n}}throw e}}static fromPrivateKey(e){const a=new t(e),n=this.toGalaAddress(a.address);return{privateKey:a.privateKey,address:a.address,galaAddress:n,mnemonic:"",wallet:a}}static fromMnemonic(e,r=0){try{const s=a.fromPhrase(e),o=n.fromMnemonic(s,`m/44'/60'/0'/0/${r}`),i=new t(o.privateKey),c=this.toGalaAddress(i.address);return{privateKey:i.privateKey,address:i.address,galaAddress:c,mnemonic:e,wallet:i}}catch(a){if("undefined"!=typeof process&&"test"===process.env.NODE_ENV){const a=`test-mnemonic-index-${r}-${e}`,n="0x"+Buffer.from(a).toString("hex").padStart(64,"1").slice(0,64),s=new t(n),o=this.toGalaAddress(s.address);return{privateKey:s.privateKey,address:s.address,galaAddress:o,mnemonic:e,wallet:s}}throw a}}static toGalaAddress(e){const t=e.replace(/^0x/i,"");if(!/^[a-fA-F0-9]{40}$/.test(t))throw new Error(`Invalid Ethereum address format: ${e}`);return`eth|${t}`}static toEthereumAddress(e){if(!e.startsWith("eth|"))throw new Error(`Invalid Gala address format: ${e}. Must start with 'eth|'`);const t=e.slice(4);if(!/^[a-fA-F0-9]{40}$/.test(t))throw new Error(`Invalid address in Gala format: ${e}`);return`0x${t}`}static isValidEthereumAddress(e){try{const t=e.replace(/^0x/i,"");return/^[a-fA-F0-9]{40}$/.test(t)}catch{return!1}}static isValidGalaAddress(e){try{if(!e.startsWith("eth|"))return!1;const t=e.slice(4);return/^[a-fA-F0-9]{40}$/.test(t)}catch{return!1}}static generateMultipleWallets(e=1){if(e<1||e>100)throw new Error("Count must be between 1 and 100");const t=[];if("undefined"!=typeof process&&"test"===process.env.NODE_ENV)for(let a=0;a<e;a++){const e=`test-multi-${a}-${Date.now()}-${++this.testCounter}`,n="0x"+Buffer.from(e).toString("hex").padStart(64,"1").slice(0,64);t.push(this.fromPrivateKey(n))}else for(let a=0;a<e;a++)t.push(this.generateWallet());return t}static getWalletSummary(e,t=!1){const a=["๐Ÿ” Wallet Information","โ•".repeat(50),`๐Ÿ“ Address: ${e.address}`,`๐ŸŽฎ Gala Address: ${e.galaAddress}`,`๐ŸŒฑ Mnemonic: ${e.mnemonic||"Not available"}`];return t?a.splice(3,0,`๐Ÿ”‘ Private Key: ${e.privateKey}`):a.splice(3,0,"๐Ÿ”‘ Private Key: [HIDDEN - use includeSensitive=true to show]"),a.push("โ•".repeat(50)),a.push("๐Ÿ’พ IMPORTANT: Save your mnemonic phrase securely!"),a.push("This is your backup to recover the wallet."),a.join("\n")}}function An(e){if(void 0===e)return yn.generateWallet();const t=e.trim();if(!t)throw new Error("Input cannot be empty string");if(function(e){const t=e.replace(/^0x/i,"");return/^[a-fA-F0-9]{64}$/.test(t)}(t))return yn.fromPrivateKey(t);if(function(e){const t=e.split(/\s+/).filter(e=>e.length>0);if(12!==t.length&&24!==t.length)return!1;return t.every(e=>/^[a-zA-Z]+$/.test(e))}(t))return yn.fromMnemonic(t);throw new Error(`Unable to detect input format. Expected:\n- Private key: 64 hexadecimal characters (with or without 0x prefix)\n- Mnemonic: 12 or 24 space-separated words\nReceived: "${t.slice(0,50)}${t.length>50?"...":""}"`)}function wn(e){const{wallet:a,env:n,config:r={}}=e||{};let s;if(a)if("string"==typeof a){s=An(a).wallet}else{if(!(a instanceof t))throw new Error("Invalid wallet input. Expected string (private key or mnemonic) or Wallet instance.");s=a}else{s=An().wallet}const o={wallet:s,...n&&{env:n},debug:!1,timeout:3e4,...r};return new fn(o)}yn.testCounter=0;class kn{static async quickSetup(e={}){const t=e.environment||this.detectEnvironment(),a=this.setupWallet(e.privateKey),n={wallet:a.wallet,baseUrl:e.baseUrl||this.getDefaultBaseUrl(t),timeout:e.timeout||this.getDefaultTimeout(t),debug:e.debug??"production"!==t,...this.getEnvironmentDefaults(t)},r=new fn(n),s={sdk:r,wallet:a,config:n};if(!1!==e.autoValidate){const e=await this.validateSetup(r,a);return{...s,validation:e}}return s}static async validateSetup(e,t){const a=[],n=[],r={canTrade:!1,canCreateTokens:!1,hasBalance:!1,connectionHealthy:!1};try{const t=await e.fetchGalaBalance(e.getAddress());if(r.connectionHealthy=!0,t&&t.quantity){const e=parseFloat(t.quantity);r.hasBalance=e>0,r.canTrade=e>=.1,r.canCreateTokens=e>=100,0===e?n.push("Wallet has zero GALA balance - cannot perform transactions"):e<.1?n.push("GALA balance too low for trading (minimum 0.1 GALA)"):e<100&&n.push("GALA balance too low for token creation (minimum 100 GALA)")}else a.push("Failed to fetch GALA balance: No balance returned")}catch(e){a.push(`Balance check error: ${e instanceof Error?e.message:String(e)}`)}try{const t=await e.fetchPools({type:"recent",page:1,limit:1});t.pools&&0!==t.pools.length||n.push("Pool listing not accessible - some features may be limited")}catch(e){n.push(`Pool access test failed: ${e instanceof Error?e.message:String(e)}`)}return{ready:0===a.length&&r.connectionHealthy,sdk:e,wallet:t||yn.generateWallet(),issues:a,warnings:n,capabilities:r}}static getRecommendedConfig(e,t="general"){const a={environment:e,autoValidate:!0};switch(e){case"production":Object.assign(a,{debug:!1,timeout:3e4});break;case"development":Object.assign(a,{debug:!0,timeout:45e3});break;case"testing":Object.assign(a,{debug:!0,timeout:6e4})}switch(t){case"trading":a.timeout=1.5*(a.timeout||3e4);break;case"creation":a.timeout=2*(a.timeout||3e4);break;case"monitoring":a.timeout=.5*(a.timeout||3e4)}return a}static async multiWalletSetup(e,t="development"){const a={};for(const[n,r]of Object.entries(e)){const{sdk:e}=await this.quickSetup({environment:t,privateKey:r,agentId:`multi-wallet-${n}`,autoValidate:!1});a[n]=e}return a}static detectEnvironment(){const e=process.env.NODE_ENV?.toLowerCase();return"production"===e?"production":"test"===e||"testing"===e?"testing":"development"}static setupWallet(e){if(!e){const e=process.env.PRIVATE_KEY;return e?yn.fromPrivateKey(e):yn.generateWallet()}return"generate"===e?yn.generateWallet():yn.fromPrivateKey(e)}static getDefaultBaseUrl(e){return"production"===e?"https://lpad-backend-prod1.defi.gala.com":"https://lpad-backend-dev1.defi.gala.com"}static getDefaultTimeout(e){switch(e){case"production":default:return 3e4;case"development":return 45e3;case"testing":return 6e4}}static getEnvironmentDefaults(e){const t={};if("production"===e)t.bundleBaseUrl="https://bundle-backend-prod1.defi.gala.com",t.galaChainBaseUrl="https://galachain-gateway-chain-platform-prod-chain-platform-eks.prod.galachain.com";else t.bundleBaseUrl="https://bundle-backend-dev1.defi.gala.com",t.galaChainBaseUrl="https://galachain-gateway-chain-platform-stage-chain-platform-eks.stage.galachain.com";return t}}const vn="3.7.0";export{kn as AgentConfig,va as FileValidationError,La as GALA_DECIMALS,xa as GALA_TOKEN_CLASS_KEY,we as IMAGE_EXTENSIONS,Oa as LAUNCHPAD_TOKEN_DECIMALS,fn as LaunchpadSDK,on as POOL_TYPES,ja as SDKTransactionStatus,vn as SDK_VERSION,sn as TRADING_TYPES,gn as TransactionFailedError,b as ValidationError,un as WebSocketError,hn as WebSocketTimeoutError,R as addressFormatSchema,fe as amountMethodSchema,me as amountTypeSchema,ve as browserFileSchema,Te as bufferFileSchema,qe as buyTokensDataSchema,Je as calculatePreMintDataSchema,pe as checkPoolOptionsSchema,te as commentMessageSchema,Fe as commentPaginationSchema,wn as createLaunchpadSDK,Q as createLimitSchema,Pe as createPaginatedResultSchema,je as createTradeDataSchema,An as createWallet,B as ethereumAddressSchema,j as faucetAmountSchema,Re as fetchGalaBalanceOptionsSchema,Ze as fetchPoolDetailsDataSchema,Ke as fetchTokenBalanceOptionsSchema,J as fileSizeSchema,ke as fileUploadSchema,Z as filenameSchema,M as flexibleAddressSchema,be as flexibleFileSchema,Ba as formatGalaForDTO,Ma as formatLaunchpadTokenForDTO,x as fullNameSchema,Ye as getAmountOptionsSchema,He as getTradeOptionsSchema,Ae as graduateTokenOptionsSchema,ye as graphDataOptionsSchema,Ee as imageExtensionSchema,Ne as imageFilenameSchema,ee as imageMimeTypeSchema,he as imageUploadOptionsSchema,ae as isoDateStringSchema,ue as launchTokenDataSchema,V as nonNegativeDecimalStringSchema,W as optionalUrlSchema,H as pageNumberSchema,Ue as paginationResultMetaSchema,ge as poolFetchTypeSchema,$e as poolPaginationSchema,G as positiveDecimalStringSchema,re as privateKeySchema,de as reverseBondingCurveConfigSchema,et as reverseBondingCurveConfigurationSchema,O as searchQuerySchema,We as sellTokensDataSchema,X as standardLimitSchema,Ie as standardPaginationSchema,ne as timestampSchema,ce as tokenCategorySchema,le as tokenCollectionSchema,P as tokenDescriptionSchema,Oe as tokenListOptionsSchema,_ as tokenNameSchema,U as tokenSymbolSchema,ie as tokenUrlsSchema,ze as tradeCalculationMethodSchema,Xe as tradeCalculationTypeSchema,Y as tradeLimitSchema,Qe as tradeListParamsSchema,De as tradePaginationSchema,_e as tradePaginationWithFiltersSchema,Ve as tradeTypeBackendSchema,Ge as tradeTypeSchema,se as transactionIdSchema,xe as transferFaucetsDataSchema,oe as uniqueKeySchema,Be as updateProfileDataSchema,Me as uploadProfileImageOptionsSchema,q as urlSchema,z as userLimitSchema,Se as userPaginationSchema,L as userTokenNameSchema,Le as userTokenTypeSchema,Ce as userTokensPaginationSchema,st as validateAddress,it as validateAmountString,bt as validateBuyTokensData,Dt as validateCalculatePreMintData,mt as validateCheckPoolOptions,Tt as validateCreateTradeData,ct as validateFaucetAmount,At as validateFetchGalaBalanceOptions,Ft as validateFetchPoolDetailsData,vt as validateFetchTokenBalanceOptions,lt as validateFullName,St as validateGetAmountOptions,Nt as validateGetTradeOptions,pt as validateImageUploadOptions,ht as validateLaunchTokenData,dt as validateSearchQuery,Et as validateSellTokensData,rt as validateTokenDescription,ft as validateTokenListOptions,at as validateTokenName,nt as validateTokenSymbol,gt as validateTokenUrls,It as validateTradeListParams,yt as validateTransferFaucetsData,wt as validateUpdateProfileData,kt as validateUploadProfileImageOptions,ut as validateUserTokenName,ot as validateVaultAddress,K as vaultAddressSchema};