@gala-chain/launchpad-sdk 3.7.8 โ†’ 3.8.1

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 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};
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 u}from"@gala-chain/api";import d 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 v(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 w{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:v(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 T="Token name is required and must be a string",k=e=>`Could not find vault address for token: ${e}`;class E extends Error{constructor(e,t,a){super(e),this.field=t,this.code=a,this.name="ValidationError"}}class N extends Error{constructor(e,t,a){super(e),this.statusCode=t,this.originalError=a,this.name="NetworkError"}}class b 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 E(`Page must be a number between ${n} and ${r}`,"page","INVALID_PAGE");if("number"!=typeof t||t<s||t>o)throw new E(`Limit must be a number between ${s} and ${o}`,"limit","INVALID_LIMIT")}const F={ETH_ADDRESS:/^0x[0-9a-fA-F]{40}$/,BACKEND_ADDRESS:/^eth\|[0-9a-fA-F]{40}$/};function C(e){return"string"==typeof e&&e.trim().length>0}function D(e){return!(!e||"string"!=typeof e)&&(F.ETH_ADDRESS.test(e)||F.BACKEND_ADDRESS.test(e))}function _(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"),P=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=l.string().min(1,"Token description is required").max(500,"Token description must be at most 500 characters"),U=l.string().min(1,"Token name must be at least 1 character").max(50,"Token name must be at most 50 characters"),x=l.string().min(1,"Search query must be at least 1 character").max(100,"Search query must be at most 100 characters"),O=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(F.BACKEND_ADDRESS,"Address must be in format: eth|[40-hex-chars]"),B=l.string().regex(F.ETH_ADDRESS,"Invalid Ethereum address format"),M=l.string().refine(e=>F.BACKEND_ADDRESS.test(e)||F.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),G=l.string().refine(e=>F.BACKEND_ADDRESS.test(e)||/^service\|Token\$Unit\$[A-Z0-9]+\$eth:[0-9a-fA-F]{40}\$launchpad$/.test(e),"Invalid vault address format"),K=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"),q=l.string().regex(/^(?!0+(\.0+)?$)\d+(\.\d+)?$/,"Amount must be a positive, non-zero number"),j=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),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"),J=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"),ue=l.object({minFeePortion:K,maxFeePortion:K}),de=l.object({tokenName:$,tokenSymbol:P,tokenDescription:L,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:ue.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:P.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()}),ve=[".png",".jpg",".jpeg",".gif",".webp",".svg"],we=l.object({file:l.union([l.instanceof(File),l.instanceof(Buffer)]),name:J,size:Z,type:ee}),Te=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"),ke=l.instanceof(Buffer).refine(e=>e.length>=1&&e.length<=10485760,"Buffer size must be between 1 byte and 10MB"),Ee=l.union([Te,ke]),Ne=l.enum([".png",".jpg",".jpeg",".gif",".webp",".svg"]),be=J.refine(e=>{const t=e.slice(e.lastIndexOf(".")).toLowerCase();return ve.includes(t)},`Filename must end with one of: ${ve.join(", ")}`),Ie=l.object({page:H,limit:X}),Se=l.object({page:H,limit:z}),Fe=l.object({page:H,limit:Y}),Ce=l.object({page:H,limit:Q(50)}),De=Ie.extend({type:l.enum(["recent","popular"]).optional(),tokenName:l.string().min(1).max(50).optional(),search:l.string().min(1).max(100).optional()}),_e=Se.extend({tokenName:l.string().min(1).max(50).optional(),search:l.string().min(1).max(100).optional()}),$e=Fe.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")}),Pe=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 Le(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 Ue=l.enum(["all","DEFI","ASSET"]),xe=Se.extend({type:Ue.optional(),address:M.optional(),search:x.optional(),tokenName:U.optional()}),Oe=l.object({walletAddress:M,amount:q}),Re=l.object({address:M.optional(),refresh:l.boolean().optional()}),Be=l.object({profileImage:l.string(),fullName:O,address:M,privateKey:re.optional()}),Me=l.object({file:l.union([l.instanceof(File),l.instanceof(Buffer)]),address:M.optional(),privateKey:re.optional()}),Ge=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:U.optional()}).refine(e=>void 0!==e.tokenId||void 0!==e.tokenName,"At least one token identifier (tokenId or tokenName) is required"),Ke=l.enum(["buy","sell"]),Ve=l.enum(["BUY","SELL"]),qe=l.object({tradeType:Ke,tokenAmount:K,vaultAddress:G,userAddress:M,slippageTolerance:K.optional(),deadline:l.number().int().positive().optional()}),je=l.object({tokenSymbol:P,nativeTokenQuantity:K,expectedToken:K,maxAcceptableReverseBondingCurveFee:V.default("0").optional()}),We=l.object({tokenSymbol:P,tokenQuantity:K,expectedNativeToken:K,maxAcceptableReverseBondingCurveFee:V.default("0").optional()}),He=Fe.extend({tokenName:U.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:G,amount:K}),Ze=l.object({nativeTokenQuantity:K}),Je=l.object({vaultAddress:G}),et=l.object({minFeePortion:K,maxFeePortion:K});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(P),rt=tt(L),st=tt(M),ot=tt(G),it=tt(K),ct=tt(q),lt=tt(O),ut=tt(x),dt=tt(U),ht=tt(de),gt=tt(ie),pt=tt(he),mt=tt(pe),ft=tt(xe),yt=tt(Oe),At=tt(Re),vt=tt(Be),wt=tt(Me),Tt=tt(Ge),kt=tt(qe),Et=tt(je),Nt=tt(We),bt=tt(He),It=tt(Qe),St=tt(Ye),Ft=tt(Ze),Ct=tt(Je);function Dt(e,t){throw new E(e.join("; "),t,"VALIDATION_ERROR")}function _t(e){const t=at(e);!t.success&&t.errors&&Dt(t.errors,"tokenName")}function $t(e){const t=De.safeParse(e);t.success||Dt(t.error.errors.map(e=>e.message),"pagination")}function Pt(e){const t=mt(e);!t.success&&t.errors&&Dt(t.errors,"options")}function Lt(e){const t=St(e);!t.success&&t.errors&&Dt(t.errors,"options")}function Ut(e){const t=ye.safeParse(e);t.success||Dt(t.error.errors.map(e=>e.message),"options")}function xt(e){const t=M.safeParse(e);if(!t.success)throw new E("Ethereum address must be 40 hex characters (with or without 0x prefix)","ethereumAddress","INVALID_FORMAT");return t.data}function Ot(e,t,a=!0){if(!e||""===e.trim())throw new E(`${t} cannot be empty or whitespace-only. Provide a valid numeric string or omit the parameter to auto-fetch.`,t,"INVALID_NUMERIC_STRING");if(/[eE]/.test(e))throw new E(`${t} cannot use scientific notation. Use standard decimal format (e.g., "1000" instead of "1e3").`,t,"INVALID_NUMERIC_STRING");const n=parseFloat(e);if(isNaN(n))throw new E(`${t} must be a valid numeric string. Received: "${e}"`,t,"INVALID_NUMERIC_STRING");if(!isFinite(n))throw new E(`${t} must be a finite number. Cannot be Infinity or -Infinity.`,t,"INVALID_NUMERIC_STRING");if(n<0)throw new E(`${t} must be non-negative. Received: "${e}"`,t,"INVALID_NUMERIC_STRING");if(!a&&0===n)throw new E(`${t} must be greater than zero. Received: "${e}"`,t,"INVALID_NUMERIC_STRING")}var Rt=Object.freeze({__proto__:null,normalizeAddressInput:function(e){if(!e)return;const t=M.safeParse(e);if(!t.success)throw new E(`Invalid address format: ${e}. Must be either "0x..." (Ethereum) or "eth|..." (GalaChain) format`,"address","INVALID_FORMAT");return t.data},toBackendAddressFormat:xt,validateCheckPoolOptions:Pt,validateGetAmountOptions:Lt,validateGetGraphOptions:Ut,validateNumericString:Ot,validatePagination:$t,validateTokenName:_t});function Bt(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 Mt(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 Gt(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 Vt="/launchpad/upload-image",qt="/launchpad/fetch-pool",jt="/launchpad/check-pool",Wt="/launchpad/get-graph-data",Ht="/holders",Qt="/launchpad/get-badge/",Xt="/trade/",zt="/token/commment",Yt="/token/commment",Zt="/user/profile",Jt="/user/profile",ea="/user/token-list",ta="/user/token-hold",aa="/user/transfer-faucets";function na(e,t){return new E(`Token "${e}" not found. Please verify the token name is correct.`,"tokenName","TOKEN_NOT_FOUND")}function ra(e,t){const a=t||e.charAt(0).toUpperCase()+e.slice(1);return new E(`${a} is required`,e,"REQUIRED_FIELD")}function sa(e,t,a){const n=a||e.charAt(0).toUpperCase()+e.slice(1);return new E(`${n} must be ${t}`,e,"INVALID_FORMAT")}function oa(e,t,a){return new N(e,t,a)}function ia(e,t){return new b(e,t)}function ca(e,t,a){return new I(e,t,a)}function la(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 ua{constructor(e){this.http=e}async fetchPools(e={}){let t;$t({page:e.page??1,limit:e.limit??10}),e.tokenName&&_t(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=v(n),s=await this.http.get(qt,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){Pt(e),e.tokenName&&_t(e.tokenName);const t=v(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(!la(e))throw new E("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(_t(t),!a||!n||!r)throw new E("Graph options (from, to, resolution) are required","options","MISSING_GRAPH_OPTIONS");const s={tokenName:t,from:a,to:n,resolution:r};Ut(s);const o=v(s),i=await this.http.get(Wt,o);return Kt(i,"Failed to fetch graph data",!0),{dataPoints:i.data}}async fetchTokenDistribution(e){if(!e)throw ra("tokenName","Token name");_t(e);const t=await this.resolveTokenNameToVault(e);if(!t)throw new E(k(e),"tokenName","VAULT_NOT_FOUND");const a=encodeURIComponent(t),n=await this.http.get(`${Ht}/${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 ra("tokenName","Token name");_t(e);const t=await this.http.get(Qt,{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 v(s)}const ha={PAGINATION:{MIN_PAGE:1,MAX_PAGE:1e3,MIN_LIMIT:1,MAX_LIMIT:20}};class ga{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 E("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(!C(a))throw new E("Token name is required and must be a non-empty string","tokenName","INVALID_TOKEN_NAME");S(s,o,ha);const u=function(e,t,a){return da({tokenName:e,page:t,limit:a},{stringifyFields:["page","limit"]})}(a,s,o),d=await this.http.get(Xt,u),h=(g=d.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=Mt(d,{page:s,limit:o}),m=Gt(p.page,p.totalPages);return{trades:h,...p,...m}}}const pa=new A({debug:!1,context:"DateUtils"});function ma(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())?(pa.warn(`Invalid date string received: "${e}". Using fallback.`),t||new Date):a}catch(a){return pa.warn(`Date parsing error for "${e}":`,a),t||new Date}}const fa={PAGINATION:{MIN_PAGE:1,MAX_PAGE:1e3,MIN_LIMIT:1,MAX_LIMIT:50},CONTENT:{MIN_LENGTH:1,MAX_LENGTH:500}};class ya{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 E("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(!C(a))throw new E("Token name is required and must be a non-empty string","tokenName","INVALID_TOKEN_NAME");S(n,r,fa);const s=await this.poolService.resolveTokenNameToVault(a);if(!s)throw na(a);const o=da({vaultAddress:s,page:n,limit:r},{stringifyFields:["page","limit"]}),i=await this.http.get(zt,o);Kt(i,"Failed to fetch comments");return{comments:i.data.comments.map(e=>({...e,createdAt:ma(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 E("Invalid options provided. Expected { tokenName: string, content: string }","options","INVALID_OPTIONS");var t;const{tokenName:a,content:n}=e;if(!C(a))throw new E("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>=fa.CONTENT.MIN_LENGTH&&t.length<=fa.CONTENT.MAX_LENGTH}(n))throw new E(`Comment content must be between ${fa.CONTENT.MIN_LENGTH} and ${fa.CONTENT.MAX_LENGTH} characters`,"content","INVALID_CONTENT");const r=await this.poolService.resolveTokenNameToVault(a);if(!r)throw na(a);const s={userAddress:this.http.getAddress(),vaultAddress:r,content:n};Kt(await this.http.post(Yt,s),"Failed to create comment")}}const Aa={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 va(e){return!(!e||"string"!=typeof e)&&Aa.USER_ADDRESS.PATTERN.test(e)}const wa="Update profile";class Ta{constructor(e){this.http=e}async fetchProfile(e){const t=e??this.http.getAddress();if(!va(t))throw new E("Address must be in format: eth|[40-hex-chars]","address","INVALID_ADDRESS");const a={userAddress:t};return await this.http.get(Zt,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 E("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:`${Vt}?tokenName=${encodeURIComponent(t)}`,data:a,headers:{}});return Kt(n,"Image upload failed"),"string"==typeof n.data?n.data:""}catch(e){if(e instanceof E)throw e;throw new E(`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(ea,t);Kt(a,"Failed to fetch token list",!0);const n=Bt(a.data),r=Mt(a,{page:e.page||1,limit:e.limit||10}),s=Gt(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(ta,t);Kt(a,"Failed to fetch tokens held",!0);const n=Bt(a.data),r=Mt(a,{page:e.page||1,limit:e.limit||10}),s=Gt(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,Aa),void 0!==e.address&&!va(e.address))throw new E("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>=Aa.SEARCH.MIN_LENGTH&&t.length<=Aa.SEARCH.MAX_LENGTH))throw new E(`Search query must be between ${Aa.SEARCH.MIN_LENGTH} and ${Aa.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>=Aa.TOKEN_NAME.MIN_LENGTH&&a.length<=Aa.TOKEN_NAME.MAX_LENGTH))throw new E(`Token name must be between ${Aa.TOKEN_NAME.MIN_LENGTH} and ${Aa.TOKEN_NAME.MAX_LENGTH} characters`,"tokenName","INVALID_TOKEN_NAME")}validateUpdateProfileData(e){if(!va(e.address))throw new E("Address must be in format: eth|[40-hex-chars]","address","INVALID_ADDRESS");if(!((t=e.fullName)&&"string"==typeof t&&t.length>=Aa.PROFILE.FULL_NAME.MIN_LENGTH&&t.length<=Aa.PROFILE.FULL_NAME.MAX_LENGTH&&Aa.PROFILE.FULL_NAME.ALPHABETS_ONLY_PATTERN.test(t)))throw new E(`Full name must be between ${Aa.PROFILE.FULL_NAME.MIN_LENGTH} and ${Aa.PROFILE.FULL_NAME.MAX_LENGTH} characters`,"fullName","INVALID_FULL_NAME");var t}validateUploadProfileImageOptions(e){if(e.address&&!va(e.address))throw new E("Address must be in format: eth|[40-hex-chars]","address","INVALID_ADDRESS")}}class ka extends Error{constructor(e,t,a){super(e),this.filename=t,this.mimeType=a,this.name="FileValidationError"}}function Ea(e,t,a){if(!e)throw new ka("File is required",t,a);if("undefined"!=typeof File&&e instanceof File){const t=Te.safeParse(e);if(!t.success){const a=t.error.errors.map(e=>e.message).join("; ");throw new ka(a,e.name,e.type)}return}if(Buffer.isBuffer(e)){if(!t)throw new ka("Filename is required when uploading Buffer objects",t,a);const n=ke.safeParse(e);if(!n.success){const e=n.error.errors.map(e=>e.message).join("; ");throw new ka(e,t,a)}if(t.length>255)throw new ka(`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 ka(`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 ka(`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 ka(`File extension "${s}" does not match MIME type "${a}"`,t,a);return}throw new ka("File must be a File object (browser) or Buffer (Node.js)",t,a)}class Na{constructor(e){this.http=e}async uploadImageByTokenName(e){const{tokenName:t,options:a}=e;_t(t);const n=`${t}.png`;Ea(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 sa("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:`${Vt}?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 ia("File upload failed: FormData not supported in this environment. Ensure you have proper polyfills for Node.js environments.","FormData");throw e}}}class ba{constructor(e){this.http=e}async transferFaucets(e){this.validateTransferFaucetsData(e);const t={userAddress:e.walletAddress,amount:e.amount};Kt(await this.http.post(aa,t),"Faucet transfer failed")}validateTransferFaucetsData(e){if(!va(e.walletAddress))throw new E("Address must be in format: eth|[40-hex-chars]","address","INVALID_ADDRESS");if(!(t=e.amount)||"string"!=typeof t||!Aa.FAUCET_AMOUNT.POSITIVE_NON_ZERO_DECIMAL.test(t))throw new E("Amount must be a positive decimal string greater than zero","amount","INVALID_AMOUNT");var t}}class Ia{constructor(e){this.http=e,this.poolService=new ua(e),this.tradeService=new ga(e),this.commentService=new ya(e,this.poolService),this.userService=new Ta(e),this.imageService=new Na(e),this.faucetService=new ba(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 _t(e)}}class Sa 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 Sa({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 Sa({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 Fa{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 Ca(e){if("string"==typeof e){const t=e.split("|");if(t.length<4)throw new E(`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 E(`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 E("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 E(`Invalid tokenId type: ${typeof e}. Expected string, TokenClassKey, or TokenInstanceKey`,"tokenId","INVALID_TOKEN_ID_TYPE")}var Da=Object.freeze({__proto__:null,normalizeToTokenInstanceKey:Ca});const _a={MAX_UNIQUE_KEY_LENGTH:64,UNIQUE_KEY_PATTERN:/^(galaswap-operation-|galaconnect-operation-)/,TOKEN_NAME_PATTERN:/^[a-zA-Z0-9]+$/};var $a;!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"}($a||($a={}));class Pa extends Error{constructor(e,t,a){super(e),this.type=t,this.details=a,this.name="TransferError"}}class La{constructor(e,t,a,n=!1){this.http=e,this.wallet=t,this.tokenResolver=a,this.signatureHelper=new Fa(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 oa(`Failed to fetch pool details: Status ${t.Status}`,t.Status);const a=t.Data,n=a.reverseBondingCurveConfiguration,r=n?.minFeePortion??"0",s=n?.maxFeePortion??"0",o=(await import("bignumber.js")).default,i=!new o(r).isZero()||!new o(s).isZero();return a.reverseBondingCurveMinFeePortion=r,a.reverseBondingCurveMaxFeePortion=s,a.hasReverseBondingCurveFee=i,a.isGraduated="Completed"===a.saleStatus,delete a.reverseBondingCurveConfiguration,t}async fetchLaunchTokenFee(){const e=await this.http.post("/api/asset/launchpad-contract/FetchLaunchpadFeeAmount",{});if(1!==e.Status)throw oa(`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 E("Invalid fetch pool details data: missing required fields","data","INVALID_TYPE");var t;if(!e.vaultAddress||"string"!=typeof e.vaultAddress)throw new E("Vault address is required and must be a string","vaultAddress","INVALID_VAULT_ADDRESS");if(!e.vaultAddress.startsWith("service|Token$Unit$"))throw new E("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 oa(`Failed to fetch token balance from GalaChain: ${e.message||"Unknown error"}`,void 0,e)}}async transferGala(e){this.validateTransferGalaData(e);try{const t=_(e.recipientAddress),a=_(this.wallet.address),n=Sa.forGALA(a,t,e.amount,e.uniqueKey),r=await this.signatureHelper.signTransferToken(n.toSigningPayload()),s=new Sa({...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 Pa("Transfer succeeded but transaction ID could not be extracted",$a.NETWORK_ERROR)}catch(t){throw this.handleTransferError(t,"GALA transfer failed",e)}}async transferToken(e){this.validateTransferTokenData(e);try{const t=_(e.to),a=_(this.wallet.address);let n;if(e.tokenId)n=Ca(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 Pa("Must provide either tokenId or tokenName for token identification",$a.TOKEN_NOT_FOUND);n=await this.resolveTokenInstance(e.tokenName)}const r=new Sa({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 Sa({...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 Pa("Transfer succeeded but transaction ID could not be extracted",$a.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 Pa)throw t;throw new Pa(`Failed to resolve token class key for '${e}': ${t instanceof Error?t.message:String(t)}`,$a.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 E("Invalid GALA transfer data: missing required fields");var t;if(!D(e.recipientAddress))throw new Pa("Invalid recipient address format",$a.INVALID_RECIPIENT,{recipientAddress:e.recipientAddress});if(parseFloat(e.amount)<=0)throw new Pa("Transfer amount must be positive",$a.INVALID_AMOUNT,{amount:e.amount});if(e.uniqueKey){if(e.uniqueKey.length>_a.MAX_UNIQUE_KEY_LENGTH)throw new E(`Unique key too long. Maximum length: ${_a.MAX_UNIQUE_KEY_LENGTH}`);if(!_a.UNIQUE_KEY_PATTERN.test(e.uniqueKey))throw new Pa('Invalid unique key format. Must start with "galaswap-operation-" or "galaconnect-operation-"',$a.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 E("Invalid token transfer data: missing required fields");var t;if(!D(e.to))throw new Pa("Invalid recipient address format",$a.INVALID_RECIPIENT,{recipientAddress:e.to});if(!e.tokenId&&!e.tokenName)throw new Pa("Must provide either tokenId or tokenName for token identification",$a.TOKEN_NOT_FOUND);if(e.tokenName&&!_a.TOKEN_NAME_PATTERN.test(e.tokenName))throw new Pa("Invalid token name format",$a.TOKEN_NOT_FOUND,{tokenName:e.tokenName});if(parseFloat(e.amount)<=0)throw new Pa("Transfer amount must be positive",$a.INVALID_AMOUNT,{amount:e.amount});if(e.uniqueKey){if(e.uniqueKey.length>_a.MAX_UNIQUE_KEY_LENGTH)throw new E(`Unique key too long. Maximum length: ${_a.MAX_UNIQUE_KEY_LENGTH}`);if(!_a.UNIQUE_KEY_PATTERN.test(e.uniqueKey))throw new Pa('Invalid unique key format. Must start with "galaswap-operation-" or "galaconnect-operation-"',$a.INVALID_AMOUNT,{uniqueKey:e.uniqueKey})}}async resolveTokenInstance(e){try{const t=await this.tokenResolver.resolveTokenToVault(e);if(!t)throw new Pa(`Token '${e}' not found or not available for transfer`,$a.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 Pa)throw t;throw new Pa(`Failed to resolve token '${e}': ${t instanceof Error?t.message:String(t)}`,$a.TOKEN_NOT_FOUND,{tokenName:e})}}resolveTokenInstanceFromVaultAddress(e){const[t,a]=e.split("|");if(!a)throw new Pa(`Invalid vault address format: missing token components. Address: ${e}`,$a.TOKEN_NOT_FOUND);const n=a.split("$");if(n.length<4)throw new Pa(`Invalid vault address format: insufficient token components. Expected 4+, got ${n.length}. Address: ${e}`,$a.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 Pa)return e;if(e instanceof E)return new Pa(e.message,$a.INVALID_AMOUNT);if(400===e.response?.status)return new Pa(e.response.data?.message||"Invalid transfer request",$a.INVALID_AMOUNT);if(403===e.response?.status)return new Pa("Insufficient balance for transfer",$a.INSUFFICIENT_BALANCE);if(404===e.response?.status){const e={};return"tokenName"in a&&(e.tokenName=a.tokenName),new Pa("Token not found",$a.TOKEN_NOT_FOUND,e)}return"ECONNABORTED"===e.code||"ETIMEDOUT"===e.code?new Pa("Transfer request timed out",$a.NETWORK_ERROR):new Pa(e.message||t,$a.NETWORK_ERROR)}}class Ua{constructor(e){this.http=e}async fetchTokenSpotPrice(e){if(!e||Array.isArray(e)&&0===e.length)throw ra("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 oa(`Failed to fetch token prices: ${e instanceof Error?e.message:e}`)}}async fetchLaunchpadTokenSpotPrice(e,t){if(!e||"string"!=typeof e)throw new Error(T);try{const a=await t({tokenName:e,amount:"1",type:"native"}),n=(await this.fetchTokenSpotPrice("GALA"))[0];if(!n)throw oa("GALA price not available");const r=Number(a.amount)/1e18;if(r<=0)throw new E(`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 oa(`Failed to calculate launchpad token spot price for ${e}: ${t.message}`);throw oa(`Failed to calculate launchpad token spot price for ${e}: ${String(t)}`)}}}const xa=8,Oa=18,Ra={collection:"GALA",category:"Unit",type:"none",additionalKey:"none"};function Ba(e,t=18){const a=parseFloat(e);if(0===a)return"0";return a.toFixed(t).replace(/\.?0+$/,"")}function Ma(e){return Ba(e,8)}function Ga(e){return Ba(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=Ma(t),this.expectedToken=Ga(a),this.extraFees={maxAcceptableReverseBondingCurveFee:Ma(n.maxAcceptableReverseBondingCurveFee)}}},BuyExactDto:class extends i{constructor(e,t,a,n={maxAcceptableReverseBondingCurveFee:"0"}){super(),this.vaultAddress=e,this.tokenQuantity=Ga(t),this.expectedNativeToken=Ma(a),this.extraFees={maxAcceptableReverseBondingCurveFee:Ma(n.maxAcceptableReverseBondingCurveFee)}}},SellExactDto:class extends i{constructor(e,t,a="0",n={maxAcceptableReverseBondingCurveFee:"0"}){super(),this.vaultAddress=e,this.tokenQuantity=Ga(t),this.expectedNativeToken=Ma(a),this.extraFees={maxAcceptableReverseBondingCurveFee:Ma(n.maxAcceptableReverseBondingCurveFee)}}},SellNativeDto:class extends i{constructor(e,t,a,n={maxAcceptableReverseBondingCurveFee:"0"}){super(),this.vaultAddress=e,this.nativeTokenQuantity=Ma(t),this.expectedToken=Ga(a),this.extraFees={maxAcceptableReverseBondingCurveFee:Ma(n.maxAcceptableReverseBondingCurveFee)}}}};var Va,qa,ja;!function(e){e[e.METAMASK=0]="METAMASK",e[e.TRUST_WALLET=1]="TRUST_WALLET",e[e.GALA_WALLET=2]="GALA_WALLET"}(Va||(Va={}));class Wa{constructor(e,t=!1){this.walletProvider=e,this.debug=t,this.logger=new A({debug:t,context:"SignatureService"})}async signDTO(e,t,a,n=Va.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,u;switch(n){case Va.GALA_WALLET:({signature:i,domain:l}=await this.signWithGalaWallet(r,o,t,a));break;case Va.TRUST_WALLET:i=await this.signWithTrustWallet(o),l={name:"ethereum",chainId:1};break;case Va.METAMASK:default:({signature:i,domain:l}=await this.signWithMetaMask(r,o))}return u=n===Va.TRUST_WALLET?{...o,signature:i}:{...e,signature:i,types:r,domain:l},this.logger.debug("โœ… DTO signed successfully:",{payloadKeys:Object.keys(u),signatureLength:i.length}),u}catch(e){throw this.logger.error("โŒ Signature generation failed:",e),ca(`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 ia("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 ca(`MetaMask/ethers signing failed: ${e.message}`)}}async signWithTrustWallet(e){try{const a=(t=e,JSON.stringify(t));let n;if(!this.walletProvider.signMessage)throw ia("Wallet provider does not support signMessage","walletProvider");return n=await this.walletProvider.signMessage(a),n}catch(e){throw ca(`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 ia("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 ca(`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 E(`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 E(`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 Va.METAMASK;const e=window;return e?.gala?Va.GALA_WALLET:e?.trustWallet?.isTrust?Va.TRUST_WALLET:Va.METAMASK}}class Ha{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 E(`Failed to generate stringsInstructions: ${e.message}`,"vaultAddress","INVALID_VAULT_ADDRESS")}}createTokenInstance(e){const t=new u;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 u;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 ra("vaultAddress","Vault address");const t=e.split("$");if(t.length<3)throw sa("vaultAddress","format: service|Token$Unit$SYMBOL$eth:address$launchpad");const a=t[2];if(!a||0===a.trim().length)throw new E(`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 ra("vaultAddress","Vault address");if(!e.startsWith("service|Token$Unit$"))throw sa("vaultAddress",'starting with "service|Token$Unit$"');if(!e.endsWith("$launchpad"))throw sa("vaultAddress",'ending with "$launchpad"');const t=e.split("$");if(t.length<5)throw sa("vaultAddress",'having at least 5 parts separated by "$"');const a=t[2];if(!a||!/^[A-Za-z]{1,10}$/.test(a))throw sa("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 sa("stringKey","format: collection$category$type$additionalKey (4 parts)");return{collection:t[0],category:t[1],type:t[2],additionalKey:t[3]}}}function Qa(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 d(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 d(0)),s.toFixed()}class Xa{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 Wa(n,a),this.tokenKeyService=new Ha(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 ra("bundleData","Bundle data");if(!e.signedDto)throw ra("signedDto","Signed DTO");if(!e.method||"string"!=typeof e.method)throw ra("method","Method name");if(!Array.isArray(e.stringsInstructions))throw sa("stringsInstructions","an array of resource tracking strings");if(0===e.stringsInstructions.length)throw new E("stringsInstructions cannot be empty","stringsInstructions","EMPTY_ARRAY");const t=["BuyWithNative","BuyExactToken","SellExactToken","SellWithNative"];if(!t.includes(e.method))throw sa("method",`one of: ${t.join(", ")}`);e.stringsInstructions.forEach((e,t)=>{if("string"!=typeof e||0===e.length)throw new E(`stringsInstructions[${t}] must be a non-empty string`,`stringsInstructions[${t}]`,"INVALID_INSTRUCTION");if(!e.startsWith("$"))throw new E(`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 ra("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 ra("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 u=s||"0";s&&(u=Qa(s,l,"buy-exact"),this.logger.debug("Reverse bonding curve fee slippage applied:",{baseFee:s,slippageFactor:l,adjustedMaxFee:u}));const d=await this.resolveTokenNameToVault(t);if(!d)throw na(t);if("native"===n){if(!r)throw new E("expectedAmount is required for native buy operations. Use getBuyTokenAmount() first to calculate expected tokens.","expectedAmount","EXPECTED_AMOUNT_REQUIRED");const e=Qa(r,c,"buy-native");this.logger.debug("BuyNative slippage applied:",{originalExpectedTokens:r,slippageFactor:c,adjustedMinTokens:e});const t=new Ka.BuyNativeDto(d,a,e,{maxAcceptableReverseBondingCurveFee:u});return await this.executeBundleTransaction(t,"BuyWithNative",d)}{if(!r)throw new E("expectedAmount is required for exact buy operations. Use getBuyTokenAmount() first to calculate expected GALA cost.","expectedAmount","EXPECTED_AMOUNT_REQUIRED");const e=Qa(r,c,"buy-exact");this.logger.debug("BuyExact slippage applied:",{originalExpectedGalaCost:r,slippageFactor:c,adjustedMaxGalaCost:e});const t=new Ka.BuyExactDto(d,a,e,{maxAcceptableReverseBondingCurveFee:u});return await this.executeBundleTransaction(t,"BuyExactToken",d)}}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 u=s||"0";s&&(u=Qa(s,l,"buy-exact"),this.logger.debug("Reverse bonding curve fee slippage applied:",{baseFee:s,slippageFactor:l,adjustedMaxFee:u}));const d=await this.resolveTokenNameToVault(t);if(!d)throw na(t);if("exact"===n){if(!r)throw new E("expectedAmount is required for exact sell operations. Use getSellTokenAmount() first to calculate expected GALA.","expectedAmount","EXPECTED_AMOUNT_REQUIRED");const e=Qa(r,c,"sell-exact");this.logger.debug("SellExact slippage applied:",{originalExpectedGala:r,slippageFactor:c,adjustedMinGala:e});const t=new Ka.SellExactDto(d,a,e,{maxAcceptableReverseBondingCurveFee:s||"0"});return await this.executeBundleTransaction(t,"SellExactToken",d)}{if(!r)throw new E("expectedAmount is required for native sell operations. Use getSellTokenAmount() first to calculate tokens to sell.","expectedAmount","EXPECTED_AMOUNT_REQUIRED");const e=Qa(r,c,"sell-native");this.logger.debug("SellNative slippage applied:",{originalExpectedTokensToSell:r,slippageFactor:c,adjustedMaxTokensToSell:e});const t=new Ka.SellNativeDto(d,a,e,{maxAcceptableReverseBondingCurveFee:s||"0"});return await this.executeBundleTransaction(t,"SellWithNative",d)}}ensureTradingServicesAvailable(){if(!this.signatureService||!this.tokenKeyService)throw ia("Trading services not available. BundleService requires walletProvider and userAddress for trading operations.","walletProvider");if(!this.userAddress)throw ra("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"}(qa||(qa={})),function(e){e.PENDING="pending",e.PROCESSING="processing",e.COMPLETED="completed",e.FAILED="failed",e.TIMEOUT="timeout"}(ja||(ja={}));const za={[qa.PROCESSED]:ja.COMPLETED,[qa.COMPLETED]:ja.COMPLETED,[qa.SUCCESS]:ja.COMPLETED,[qa.FAILED]:ja.FAILED,[qa.ERROR]:ja.FAILED,[qa.PROCESSING]:ja.PROCESSING,[qa.PENDING]:ja.PENDING};class Ya{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===qa.FAILED||n===qa.ERROR?"Transaction failed - check transaction details":n===qa.COMPLETED||n===qa.PROCESSED||n===qa.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 za[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 Za{constructor(e){this.poolService=e,this.cache=new Map}async resolveTokenToVault(e){if(!C(e))throw new E("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 na(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 sa("vaultAddress","format: service|Token$Unit$...$launchpad","Vault address");const a=t[1].split("$");if(a.length<4)throw sa("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 en="/api/asset/launchpad-contract/CallNativeTokenIn",tn="/api/asset/launchpad-contract/CallNativeTokenOut",an="/api/asset/launchpad-contract/CallMemeTokenIn",nn="/api/asset/launchpad-contract/CallMemeTokenOut";class rn 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 sn(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 on={NATIVE:"native",EXACT:"exact"},cn={RECENT:"recent",POPULAR:"popular"};class ln{}ln.BASE_PRICE=1650667151e-14,ln.PRICE_SCALING_FACTOR=1166069e-12,ln.TRADING_FEE_FACTOR=.001,ln.GAS_FEE="1",ln.MIN_UNBONDING_FEE_FACTOR=0,ln.MAX_UNBONDING_FEE_FACTOR=.5,ln.NET_UNBONDING_FEE_FACTOR=.5,ln.DEFAULT_LAUNCHPAD_TOKEN_MAX_SUPPLY=1e7;class un{static calculateBuyWithExact(e,t){const a=parseFloat(e),n=parseFloat(t),{BASE_PRICE:r,PRICE_SCALING_FACTOR:s,TRADING_FEE_FACTOR:o,GAS_FEE:i}=ln,c=this.roundUp(r*(Math.exp((n+a)*s)-Math.exp(n*s))/s,8),l=new d(c).multipliedBy(o).toFixed();return{amount:c.toString(),reverseBondingCurveFee:"0",transactionFee:l,gasFee:i}}static calculateBuyWithNative(e,t){const a=parseFloat(e),n=parseFloat(t),{BASE_PRICE:r,PRICE_SCALING_FACTOR:s,TRADING_FEE_FACTOR:o,GAS_FEE:i}=ln,c=Math.log(a*s/r+Math.exp(n*s))/s-n,l=new d(c).multipliedBy(o).toFixed();return{amount:c.toString(),reverseBondingCurveFee:"0",transactionFee:l,gasFee:i}}static calculateSellWithExact(e,t,a,n,r){const s=parseFloat(e),o=parseFloat(t),i=parseFloat(a),{BASE_PRICE:c,PRICE_SCALING_FACTOR:l,TRADING_FEE_FACTOR:u,GAS_FEE:h}=ln,g=c*(Math.exp(o*l)-Math.exp((o-s)*l))/l,p=new d(g),m=n+o/i*(r-n),f=p.multipliedBy(m).toFixed(8,d.ROUND_UP),y=p.multipliedBy(u).toFixed();return{amount:g.toString(),reverseBondingCurveFee:f,transactionFee:y,gasFee:h}}static calculateSellWithNative(e,t,a,n,r){const s=parseFloat(e),o=parseFloat(t),i=parseFloat(a),{BASE_PRICE:c,PRICE_SCALING_FACTOR:l,TRADING_FEE_FACTOR:u,GAS_FEE:h}=ln,g=o-Math.log(Math.exp(o*l)-s*l/c)/l,p=new d(s),m=n+o/i*(r-n),f=p.multipliedBy(m).toFixed(8,d.ROUND_UP),y=p.multipliedBy(u).toFixed();return{amount:g.toString(),reverseBondingCurveFee:f,transactionFee:y,gasFee:h}}static roundUp(e,t){const a=Math.pow(10,t);return Math.ceil(e*a)/a}}class dn{constructor(e,t,a,n,r,s,o="local"){this.http=e,this.tokenResolver=t,this.logger=a,this.bundleHttp=n,this.galaChainHttp=r,this.dexApiHttp=s,this.defaultCalculateAmountMode=o}addIfDefined(e,t,a){return void 0!==a&&(e[t]=a),e}async uploadImageByTokenName(e){const{tokenName:t,options:a}=e;_t(t);const n=`${t}.png`;Ea(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 sa("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 oa(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 ia("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&&_t(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=v(t),n=await this.http.get("/launchpad/fetch-pool",a);if(!0===n.error||200!==n.status||!n.data)throw oa(n.message||"Failed to fetch pools",n.status);let r=[];const s=(await import("bignumber.js")).default;if(n.data.tokens)if(Array.isArray(n.data.tokens))r=n.data.tokens.map(e=>{const t=e.reverseBondingCurveMinFeePortion??"0",a=e.reverseBondingCurveMaxFeePortion??"0",n=!new s(t).isZero()||!new s(a).isZero();return{...e,reverseBondingCurveMinFeePortion:t,reverseBondingCurveMaxFeePortion:a,hasReverseBondingCurveFee:n,createdAt:new Date(e.created_at||e.createdAt)}});else{const e=n.data.tokens.reverseBondingCurveMinFeePortion??"0",t=n.data.tokens.reverseBondingCurveMaxFeePortion??"0",a=!new s(e).isZero()||!new s(t).isZero();r=[{...n.data.tokens,reverseBondingCurveMinFeePortion:e,reverseBondingCurveMaxFeePortion:t,hasReverseBondingCurveFee:a,createdAt:new Date(n.data.tokens.created_at||n.data.tokens.createdAt)}]}else n.data.pools&&Array.isArray(n.data.pools)&&(r=n.data.pools.map(e=>{const t=e.reverseBondingCurveMinFeePortion??"0",a=e.reverseBondingCurveMaxFeePortion??"0",n=!new s(t).isZero()||!new s(a).isZero();return{...e,reverseBondingCurveMinFeePortion:t,reverseBondingCurveMaxFeePortion:a,hasReverseBondingCurveFee:n,createdAt:new Date(e.created_at||e.createdAt)}}));return{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(Lt(e),!this.galaChainHttp)throw ia("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:en,body:{vaultAddress:a,tokenQuantity:n,IsPreMint:!1}};if("NATIVE"===e&&"OUT"===t)return{endpoint:tn,body:{vaultAddress:a,tokenQuantity:n,IsPreMint:!1}};if("MEME"===e&&"IN"===t)return{endpoint:an,body:{vaultAddress:a,nativeTokenQuantity:n,IsPreMint:!1}};if("MEME"===e&&"OUT"===t)return{endpoint:nn,body:{vaultAddress:a,nativeTokenQuantity:n,IsPreMint:!1}};throw sa("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(!sn(e))throw oa("Malformed response data from GalaChain gateway");if(1!==e.Status)throw oa(`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){Pt(e),e.tokenName&&_t(e.tokenName);const t=v(e),a=await this.http.get("/launchpad/check-pool",t);if(!0===a.error||200!==a.status)throw oa(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(!la(e))throw new E("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(_t(t),!a||!n||!r)throw new E("Graph options (from, to, resolution) are required","options","MISSING_GRAPH_OPTIONS");const s={tokenName:t,from:a,to:n,resolution:r};Ut(s);const o=v(s),i=await this.http.get("/launchpad/get-graph-data",o);if(!0===i.error||200!==i.status||!i.data)throw oa(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 E("Invalid options provided. Expected an options object.","options","INVALID_OPTIONS");const{tokenName:t,amount:a,type:n,currentSupply:r}=e,s=e.mode??this.defaultCalculateAmountMode;if("local"!==s&&"external"!==s)throw new E(`Invalid calculation mode "${s}". Must be "local" or "external".`,"mode","INVALID_CALCULATION_MODE");if(!t||"string"!=typeof t)throw new E("Token name is required and must be a string","tokenName","INVALID_TOKEN_NAME");if(!a||"string"!=typeof a)throw new E("Amount is required and must be a string","amount","INVALID_AMOUNT");if(n!==on.NATIVE&&n!==on.EXACT)throw new E('Type must be either "native" or "exact"',"type","INVALID_TYPE");return"external"===s?this.calculateBuyAmountExternal({tokenName:t,amount:a,type:n}):this.calculateBuyAmountLocal(this.addIfDefined({tokenName:t,amount:a,type:n},"currentSupply",r))}async calculateBuyAmountExternal(e){const{tokenName:t,amount:a,type:n}=e,r=await this.tokenResolver.resolveTokenToVault(t);if(!r)throw new E(`Token "${t}" not found. Please verify the token name is correct.`,"tokenName","TOKEN_NOT_FOUND");return n===on.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,currentSupply:r,maxSupply:s,reverseBondingCurveMaxFeeFactor:o,reverseBondingCurveMinFeeFactor:i}=e,c=e.mode??this.defaultCalculateAmountMode;if("local"!==c&&"external"!==c)throw new E(`Invalid calculation mode "${c}". Must be "local" or "external".`,"mode","INVALID_CALCULATION_MODE");if(!t||"string"!=typeof t)throw new E("Token name is required and must be a string","tokenName","INVALID_TOKEN_NAME");if(!a||"string"!=typeof a)throw new E("Amount is required and must be a string","amount","INVALID_AMOUNT");if(n!==on.EXACT&&n!==on.NATIVE)throw new E('Type must be either "exact" or "native"',"type","INVALID_TYPE");if("external"===c)return this.calculateSellAmountExternal({tokenName:t,amount:a,type:n});{let e={tokenName:t,amount:a,type:n};return e=this.addIfDefined(e,"currentSupply",r),e=this.addIfDefined(e,"maxSupply",s),e=this.addIfDefined(e,"reverseBondingCurveMaxFeeFactor",o),e=this.addIfDefined(e,"reverseBondingCurveMinFeeFactor",i),this.calculateSellAmountLocal(e)}}async calculateSellAmountExternal(e){const{tokenName:t,amount:a,type:n}=e,r=await this.tokenResolver.resolveTokenToVault(t);if(!r)throw new E(`Token "${t}" not found. Please verify the token name is correct.`,"tokenName","TOKEN_NOT_FOUND");return n===on.EXACT?this._getAmount({type:"NATIVE",method:"OUT",vaultAddress:r,amount:a}):this._getAmount({type:"MEME",method:"IN",vaultAddress:r,amount:a})}async calculateBuyAmountLocal(e){const{tokenName:t,amount:a,type:n,currentSupply:r}=e;if(!a||"string"!=typeof a)throw new E("Amount is required and must be a string","amount","INVALID_AMOUNT");if(n!==on.NATIVE&&n!==on.EXACT)throw new E('Type must be either "native" or "exact"',"type","INVALID_TYPE");void 0!==r&&Ot(r,"currentSupply");const s=!r;if(s&&!t)throw new E("Token name is required when currentSupply is not provided","tokenName","MISSING_TOKEN_NAME");t&&_t(t);let o=r;if(s){o=(await this.fetchPoolDetailsForCalculation(t)).currentSupply}return n===on.EXACT?un.calculateBuyWithExact(a,o):un.calculateBuyWithNative(a,o)}async calculateSellAmountLocal(e){const{tokenName:t,amount:a,type:n,currentSupply:r,maxSupply:s,reverseBondingCurveMaxFeeFactor:o,reverseBondingCurveMinFeeFactor:i}=e;if(!a||"string"!=typeof a)throw new E("Amount is required and must be a string","amount","INVALID_AMOUNT");if(n!==on.EXACT&&n!==on.NATIVE)throw new E('Type must be either "exact" or "native"',"type","INVALID_TYPE");void 0!==r&&Ot(r,"currentSupply");const c=!r||!s||void 0===o||void 0===i;if(c&&!t)throw new E("Token name is required when currentSupply, maxSupply, or fee factors are not provided","tokenName","MISSING_TOKEN_NAME");t&&_t(t);let l=r,u=s,d=o,h=i;if(c){const e=await this.fetchPoolDetailsForCalculation(t);l=l??e.currentSupply,u=u??e.maxSupply,d=d??e.reverseBondingCurveMaxFeeFactor,h=h??e.reverseBondingCurveMinFeeFactor}return n===on.EXACT?un.calculateSellWithExact(a,l,u,h,d):un.calculateSellWithNative(a,l,u,h,d)}async calculateBuyAmountForGraduation(e){_t(e);const t=await this.tokenResolver.resolveTokenToVault(e);if(!t)throw new E(k(e),"tokenName","VAULT_NOT_FOUND");if(!this.galaChainHttp)throw ia("GalaChain HTTP client not configured");const a=await this.galaChainHttp.post("/api/asset/launchpad-contract/FetchSaleDetails",{vaultAddress:t});if(1!==a.Status)throw oa(`Failed to fetch pool details: Status ${a.Status}`,a.Status);const n=a.Data,r=(await import("bignumber.js")).default;n.currentSupply=new r(n.maxSupply).minus(n.sellingTokenQuantity).toFixed();const s=n.sellingTokenQuantity;if("0"===s)throw new E(`Token ${e} is already graduated (no tokens remaining in pool)`,"tokenName","ALREADY_GRADUATED");return await this.calculateBuyAmount({tokenName:e,amount:s,type:"exact"})}async launchToken(e){if(!this.bundleHttp)throw ia("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 E("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 E("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 oa("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 rn(r),i=await this.http.signWithGalaChain("CreateSale",o,s.SIGN_TYPED_DATA),{signature:c,types:l,domain:u,prefix:d}=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:u,...d&&{prefix:d},...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 oa(A.message||"Token launch failed");return A.data}async fetchTokenDistribution(e){if(!e)throw ra("tokenName","Token name");_t(e);const t=await this.tokenResolver.resolveTokenToVault(e);if(!t)throw new E(k(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 oa(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 ra("tokenName","Token name");_t(e);const t=await this.http.get("/launchpad/get-badge/",{tokenName:e});if(t.error||!t.data)throw oa(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 E("Invalid pre-mint calculation data","data","INVALID_PRE_MINT_DATA");var t;if(!this.galaChainHttp)throw ia("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(!sn(a))throw oa("Malformed response data from GalaChain gateway");if(1!==a.Status)throw oa(`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)}`)}}async fetchPoolDetailsForCalculation(e){const t=await this.tokenResolver.resolveTokenToVault(e);if(!t)throw new E(k(e),"tokenName","VAULT_NOT_FOUND");if(!this.galaChainHttp)throw ia("GalaChain HTTP client not configured");const a=await this.galaChainHttp.post("/api/asset/launchpad-contract/FetchSaleDetails",{vaultAddress:t});if(1!==a.Status)throw oa(`Failed to fetch pool details: Status ${a.Status}`,a.Status);const n=a.Data,r=new(0,(await import("bignumber.js")).default)(n.maxSupply).minus(n.sellingTokenQuantity).toFixed(),s=n.sellingTokenQuantity,o=n.maxSupply;let i=.5,c=0;n.reverseBondingCurveConfiguration?(i=parseFloat(n.reverseBondingCurveConfiguration.maxFeePortion),c=parseFloat(n.reverseBondingCurveConfiguration.minFeePortion)):this.logger.debug(`Pool details missing reverseBondingCurveConfiguration for token ${e}, using defaults (min: 0.0, max: 0.5)`);return{currentSupply:r,remainingTokens:s,maxSupply:o,reverseBondingCurveMaxFeeFactor:i,reverseBondingCurveMinFeeFactor:c,reverseBondingCurveNetFeeFactor:i-c}}getAddress(){return this.http.getAddress()}formatAddressForBackend(e){return xt(e)}validateTokenName(e){return _t(e)}validatePagination(e){return $t(e)}async fetchTokenSpotPrice(e){if(!this.dexApiHttp)throw ia("DEX API client not configured. Token price fetching requires dexApiHttp client.","dexApiHttp");if(!e||Array.isArray(e)&&0===e.length)throw ra("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 oa(`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 ra("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 oa("GALA price not available");const n=Number(t.amount);if(n<=0)throw new E(`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 hn={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 gn(e){return hn[e]}class pn extends Error{constructor(e,t){super(e),this.cause=t,this.name="WebSocketError"}}class mn extends pn{constructor(e,t){super(`WebSocket confirmation timeout for transaction ${e} after ${t}ms`),this.name="WebSocketTimeoutError"}}class fn 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 yn(e,t){if(!e)throw new pn(`Invalid WebSocket response received for transaction ${t}: response is null or undefined`);if("object"!=typeof e)throw new pn(`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 pn(`Invalid WebSocket response received for transaction ${t}: missing status field`)}function An(e,t,a,n){yn(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 pn(`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 vn{constructor(e){let t=null;t=e.env?gn(e.env):e.baseUrl?.includes("prod")?gn("PROD"):gn("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?vn.DEFAULT_SLIPPAGE_TOLERANCE_FACTOR:this.parseSlippageToleranceFactor(e.slippageToleranceFactor),this.maxAcceptableReverseBondingCurveFeeSlippageFactor=void 0===e.maxAcceptableReverseBondingCurveFeeSlippageFactor?vn.DEFAULT_MAX_ACCEPTABLE_REVERSE_BONDING_CURVE_FEE_SLIPPAGE_FACTOR:this.parseFeeSlippageFactor(e.maxAcceptableReverseBondingCurveFeeSlippageFactor),this.calculateAmountMode=e.calculateAmountMode||vn.DEFAULT_CALCULATE_AMOUNT_MODE,this.auth=new y({wallet:e.wallet,messagePrefix:"Create a GalaChain Wallet"}),this.http=new w(this.auth,this.config),this.galaChainHttp=new w(this.auth,{...this.config,baseUrl:this.config.galaChainBaseUrl}),this.bundleHttp=new w(this.auth,{...this.config,baseUrl:this.config.bundleBaseUrl}),this.dexApiHttp=new w(this.auth,{...this.config,baseUrl:this.config.dexApiBaseUrl}),this.launchpadService=new Ia(this.http),this.tokenResolverService=new Za(this.launchpadService.poolService),this.launchpadAPI=new dn(this.http,this.tokenResolverService,this.logger,this.bundleHttp,this.galaChainHttp,this.dexApiHttp,this.calculateAmountMode),this.galaChainService=new La(this.galaChainHttp,e.wallet,this.tokenResolverService,e.debug||!1),this.dexService=new Ua(this.dexApiHttp),this.bundleService=new Xa(this.bundleHttp,this.tokenResolverService,this.config.debug||!1,e.wallet,this.getAddress(),this.slippageToleranceFactor,this.maxAcceptableReverseBondingCurveFeeSlippageFactor),this.websocketService=new Ya({url:this.config.webSocketUrl},this.config.debug)}createOverrideSdk(e){if(!e||"string"!=typeof e)throw ia("Invalid privateKey: must be a non-empty string","privateKey");if(!e.match(/^0x[a-fA-F0-9]{64}$/))throw ia('Invalid privateKey format: must be "0x" followed by 64 hexadecimal characters',"privateKey");const a=new t(e),n={...this.config,wallet:a};return new vn(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,calculateAmountMode:this.calculateAmountMode}}getUrlByTokenName(e){const t=this.config.launchpadFrontendUrl;if(!t)throw ia("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 oa("Failed to fetch GALA price - no price data returned");const t=e.find(e=>"GALA"===e.symbol);if(!t)throw oa("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(k(e));const a=(await this.galaChainService.fetchPoolDetails({vaultAddress:t})).Data,n=await this.launchpadAPI.fetchPoolDetailsForCalculation(e);return a.currentSupply=n.currentSupply,a.reverseBondingCurveMaxFeeFactor=n.reverseBondingCurveMaxFeeFactor,a.reverseBondingCurveMinFeeFactor=n.reverseBondingCurveMinFeeFactor,a.reverseBondingCurveNetFeeFactor=n.reverseBondingCurveNetFeeFactor,a}async fetchPoolDetailsForCalculation(e){return this.launchpadAPI.fetchPoolDetailsForCalculation(e)}async isTokenGraduated(e){return(await this.fetchPoolDetails(e)).isGraduated}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 Rt}),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 Rt}),a=t(e.address);if(e.tokenId){const{normalizeToTokenInstanceKey:t}=await Promise.resolve().then(function(){return Da}),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 ra("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 calculateBuyAmountLocal(e){return this.launchpadAPI.calculateBuyAmountLocal(e)}async calculateSellAmountLocal(e){return this.launchpadAPI.calculateSellAmountLocal(e)}async calculateBuyAmountExternal(e){return this.launchpadAPI.calculateBuyAmountExternal(e)}async calculateSellAmountExternal(e){return this.launchpadAPI.calculateSellAmountExternal(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 ca("No transaction ID returned from buy operation");return this.waitForConfirmation(a,t=>An(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 ca("No transaction ID returned from sell operation");return this.waitForConfirmation(a,t=>An(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=>{yn(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 pn(`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 Rt}),a=t(e)||this.getAddress();return this.launchpadService.fetchProfile(a)}async updateProfile(e){const{normalizeAddressInput:t}=await Promise.resolve().then(function(){return Rt}),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 Rt}),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 Rt}),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 Rt}),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 Rt}),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 Rt}),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 Rt}),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 ia("baseUrl is required in configuration","baseUrl");if(!this.config.webSocketUrl)throw ia("webSocketUrl is required in configuration","webSocketUrl");try{new URL(this.config.baseUrl)}catch{throw ia(`Invalid baseUrl format: ${this.config.baseUrl}`,"baseUrl")}try{new URL(this.config.webSocketUrl)}catch{throw ia(`Invalid webSocketUrl format: ${this.config.webSocketUrl}`,"webSocketUrl")}if(this.config.galaChainBaseUrl)try{new URL(this.config.galaChainBaseUrl)}catch{throw ia(`Invalid galaChainBaseUrl format: ${this.config.galaChainBaseUrl}`,"galaChainBaseUrl")}if(this.config.bundleBaseUrl)try{new URL(this.config.bundleBaseUrl)}catch{throw ia(`Invalid bundleBaseUrl format: ${this.config.bundleBaseUrl}`,"bundleBaseUrl")}if(this.config.launchpadFrontendUrl)try{new URL(this.config.launchpadFrontendUrl)}catch{throw ia(`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: ${vn.DEFAULT_SLIPPAGE_TOLERANCE_FACTOR}`),vn.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: ${vn.DEFAULT_MAX_ACCEPTABLE_REVERSE_BONDING_CURVE_FEE_SLIPPAGE_FACTOR}`),vn.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 fn(e,a.status,a.message);let n;try{n=t(a)}catch(t){if(t instanceof pn)throw t;throw new pn(`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 fn||t instanceof pn)throw t;throw new pn(`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")}}vn.DEFAULT_SLIPPAGE_TOLERANCE_FACTOR=.15,vn.DEFAULT_MAX_ACCEPTABLE_REVERSE_BONDING_CURVE_FEE_SLIPPAGE_FACTOR=.01,vn.DEFAULT_LAUNCHPAD_TOKEN_MAX_SUPPLY=ln.DEFAULT_LAUNCHPAD_TOKEN_MAX_SUPPLY,vn.DEFAULT_CALCULATE_AMOUNT_MODE="local";class wn{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 Tn(e){if(void 0===e)return wn.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 wn.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 wn.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 kn(e){const{wallet:a,env:n,config:r={}}=e||{};let s;if(a)if("string"==typeof a){s=Tn(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=Tn().wallet}const o={wallet:s,...n&&{env:n},debug:!1,timeout:3e4,...r};return new vn(o)}wn.testCounter=0;class En{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 vn(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||wn.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?wn.fromPrivateKey(e):wn.generateWallet()}return"generate"===e?wn.generateWallet():wn.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 Nn="3.7.0";export{En as AgentConfig,ka as FileValidationError,xa as GALA_DECIMALS,Ra as GALA_TOKEN_CLASS_KEY,ve as IMAGE_EXTENSIONS,Oa as LAUNCHPAD_TOKEN_DECIMALS,vn as LaunchpadSDK,cn as POOL_TYPES,ja as SDKTransactionStatus,Nn as SDK_VERSION,on as TRADING_TYPES,fn as TransactionFailedError,E as ValidationError,pn as WebSocketError,mn as WebSocketTimeoutError,R as addressFormatSchema,fe as amountMethodSchema,me as amountTypeSchema,Te as browserFileSchema,ke as bufferFileSchema,je as buyTokensDataSchema,Ze as calculatePreMintDataSchema,pe as checkPoolOptionsSchema,te as commentMessageSchema,Ce as commentPaginationSchema,kn as createLaunchpadSDK,Q as createLimitSchema,Le as createPaginatedResultSchema,qe as createTradeDataSchema,Tn as createWallet,B as ethereumAddressSchema,q as faucetAmountSchema,Re as fetchGalaBalanceOptionsSchema,Je as fetchPoolDetailsDataSchema,Ge as fetchTokenBalanceOptionsSchema,Z as fileSizeSchema,we as fileUploadSchema,J as filenameSchema,M as flexibleAddressSchema,Ee as flexibleFileSchema,Ma as formatGalaForDTO,Ga as formatLaunchpadTokenForDTO,O as fullNameSchema,Ye as getAmountOptionsSchema,He as getTradeOptionsSchema,Ae as graduateTokenOptionsSchema,ye as graphDataOptionsSchema,Ne as imageExtensionSchema,be as imageFilenameSchema,ee as imageMimeTypeSchema,he as imageUploadOptionsSchema,ae as isoDateStringSchema,de as launchTokenDataSchema,V as nonNegativeDecimalStringSchema,W as optionalUrlSchema,H as pageNumberSchema,Pe as paginationResultMetaSchema,ge as poolFetchTypeSchema,De as poolPaginationSchema,K as positiveDecimalStringSchema,re as privateKeySchema,ue as reverseBondingCurveConfigSchema,et as reverseBondingCurveConfigurationSchema,x as searchQuerySchema,We as sellTokensDataSchema,X as standardLimitSchema,Ie as standardPaginationSchema,ne as timestampSchema,ce as tokenCategorySchema,le as tokenCollectionSchema,L as tokenDescriptionSchema,xe as tokenListOptionsSchema,$ as tokenNameSchema,P as tokenSymbolSchema,ie as tokenUrlsSchema,ze as tradeCalculationMethodSchema,Xe as tradeCalculationTypeSchema,Y as tradeLimitSchema,Qe as tradeListParamsSchema,Fe as tradePaginationSchema,$e as tradePaginationWithFiltersSchema,Ve as tradeTypeBackendSchema,Ke as tradeTypeSchema,se as transactionIdSchema,Oe as transferFaucetsDataSchema,oe as uniqueKeySchema,Be as updateProfileDataSchema,Me as uploadProfileImageOptionsSchema,j as urlSchema,z as userLimitSchema,Se as userPaginationSchema,U as userTokenNameSchema,Ue as userTokenTypeSchema,_e as userTokensPaginationSchema,st as validateAddress,it as validateAmountString,Et as validateBuyTokensData,Ft as validateCalculatePreMintData,mt as validateCheckPoolOptions,kt as validateCreateTradeData,ct as validateFaucetAmount,At as validateFetchGalaBalanceOptions,Ct as validateFetchPoolDetailsData,Tt as validateFetchTokenBalanceOptions,lt as validateFullName,St as validateGetAmountOptions,bt as validateGetTradeOptions,pt as validateImageUploadOptions,ht as validateLaunchTokenData,ut as validateSearchQuery,Nt as validateSellTokensData,rt as validateTokenDescription,ft as validateTokenListOptions,at as validateTokenName,nt as validateTokenSymbol,gt as validateTokenUrls,It as validateTradeListParams,yt as validateTransferFaucetsData,vt as validateUpdateProfileData,wt as validateUploadProfileImageOptions,dt as validateUserTokenName,ot as validateVaultAddress,G as vaultAddressSchema};