@gala-chain/launchpad-sdk 3.12.2 → 3.12.3
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/CHANGELOG.md +8 -0
- package/dist/index.cjs.js +1 -1
- package/dist/index.esm.js +1 -1
- package/dist/index.js +1 -1
- package/dist/schemas/files.d.ts +79 -16
- package/dist/schemas/files.d.ts.map +1 -1
- package/dist/utils/multipart.d.ts.map +1 -1
- package/package.json +5 -3
package/dist/index.cjs.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var e,t,a=require("ethers"),n=require("axios"),r=require("@gala-chain/connect"),o=require("zod"),s=require("bignumber.js"),i=require("@gala-chain/api"),c=require("socket.io-client");!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"}(e||(e={}));class l extends Error{constructor(e,t,a){super(t),this.type=e,this.originalError=a,this.name="AuthError"}}class u{constructor(t){if(this.wallet=t.wallet,this.messagePrefix=t.messagePrefix||"Create a GalaChain Wallet",""===t.messagePrefix)throw new l(e.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(t){throw new l(e.SIGNATURE_FAILED,"Failed to generate signature for authentication",t instanceof Error?t:new Error(String(t)))}}getAddress(){return this.formatAddress(this.wallet.address)}getEthereumAddress(){return this.wallet.address}getPrivateKey(){if(!this.wallet.privateKey)throw new l(e.WALLET_NOT_CONNECTED,"Wallet private key not available for @gala-chain signing");return this.wallet.privateKey}formatAddress(t){const a=t.replace(/^0x/i,"");if(!/^[a-fA-F0-9]{40}$/.test(a))throw new l(e.INVALID_ADDRESS,`Invalid Ethereum address format: ${t}`);return`eth|${a}`}async signMessage(t){try{return{message:t,signature:await this.wallet.signMessage(t),address:this.wallet.address,timestamp:Date.now()}}catch(t){const a=t instanceof Error?t.message:String(t);throw new l(e.SIGNATURE_FAILED,a,t instanceof Error?t:new Error(String(t)))}}async generateAuthHeaders(t,a){try{const e=Date.now(),n=`${this.messagePrefix} ${a.toUpperCase()} ${t} ${e}`,r=await this.wallet.signMessage(n);return{"x-signature":r,"x-address":this.formatAddress(this.wallet.address),"x-message":n,"x-timestamp":e.toString()}}catch(t){throw new l(e.SIGNATURE_FAILED,"Failed to generate authentication headers",t instanceof Error?t:new Error(String(t)))}}async signTypedData(t,a,n){try{return await this.wallet.signTypedData(t,a,n)}catch(t){throw new l(e.SIGNATURE_FAILED,"Failed to sign typed data",t instanceof Error?t:new Error(String(t)))}}async generateCustomSignature(t){if(!t||"string"!=typeof t||0===t.trim().length)throw new l(e.SIGNATURE_FAILED,"Custom message must be a non-empty string");try{const e=await this.wallet.signMessage(t);return{message:t,signature:e,address:this.formatAddress(this.wallet.address),timestamp:Date.now()}}catch(t){throw new l(e.SIGNATURE_FAILED,"Failed to generate custom message signature",t instanceof Error?t:new Error(String(t)))}}validateWallet(){if(!this.wallet)throw new l(e.WALLET_NOT_CONNECTED,"Wallet is required for authentication");if(!this.wallet.address)throw new l(e.WALLET_NOT_CONNECTED,"Wallet address is not available");if(!this.wallet.privateKey&&!this.wallet.signMessage)throw new l(e.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"}(t||(t={}));class d{constructor(e){this.levelPriority={[t.DEBUG]:0,[t.INFO]:1,[t.WARN]:2,[t.ERROR]:3},this.debugEnabled=e.debug,this.context=e.context||"SDK",this.minLevel=e.minLevel||(e.debug?t.DEBUG:t.INFO)}debug(e,a){this.log(t.DEBUG,e,a)}info(e,a){this.log(t.INFO,e,a)}warn(e,a){this.log(t.WARN,e,a)}error(e,a){this.log(t.ERROR,e,a)}log(e,a,n){if(this.levelPriority[e]<this.levelPriority[this.minLevel])return;if(e===t.DEBUG&&!this.debugEnabled)return;const r=`[${(new Date).toISOString()}] [${this.context}] [${e}]`,o=this.getConsoleMethod(e);void 0!==n?n instanceof Error?o(`${r} ${a}`,n.message,n.stack):o(`${r} ${a}`,n):o(`${r} ${a}`)}getConsoleMethod(e){switch(e){case t.DEBUG:return console.debug;case t.INFO:return console.info;case t.WARN:return console.warn;case t.ERROR:return console.error;default:return console.log}}child(e){return new d({debug:this.debugEnabled,context:`${this.context}:${e}`,minLevel:this.minLevel})}isDebugEnabled(){return this.debugEnabled&&this.levelPriority[t.DEBUG]>=this.levelPriority[this.minLevel]}}function h(e){if(!e||"object"!=typeof e)return{};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 p{constructor(e,t={}){this.auth=e,this.debug=t.debug??!1,this.logger=new d({debug:this.debug,context:"HttpClient"}),this.axios=n.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:h(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=r.SigningType.SIGN_TYPED_DATA){const n=this.auth.getPrivateKey(),o=new r.SigningClient(n);return await o.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 g="Token name is required and must be a string",m=e=>`Could not find vault address for token: ${e}`;class f extends Error{constructor(e,t,a){super(e),this.field=t,this.code=a,this.name="ValidationError"}}class y extends Error{constructor(e,t,a){super(e),this.statusCode=t,this.originalError=a,this.name="NetworkError"}}class v extends Error{constructor(e,t){super(e),this.field=t,this.name="ConfigurationError"}}class A extends Error{constructor(e,t,a){super(e),this.transactionId=t,this.code=a,this.name="TransactionError"}}function w(e,t,a){const{MIN_PAGE:n,MAX_PAGE:r,MIN_LIMIT:o,MAX_LIMIT:s}=a.PAGINATION;if("number"!=typeof e||e<n||e>r)throw new f(`Page must be a number between ${n} and ${r}`,"page","INVALID_PAGE");if("number"!=typeof t||t<o||t>s)throw new f(`Limit must be a number between ${o} and ${s}`,"limit","INVALID_LIMIT")}const T={ETH_ADDRESS:/^0x[0-9a-fA-F]{40}$/,BACKEND_ADDRESS:/^eth\|[0-9a-fA-F]{40}$/};function k(e){return"string"==typeof e&&e.trim().length>0}function N(e){return!(!e||"string"!=typeof e)&&(T.ETH_ADDRESS.test(e)||T.BACKEND_ADDRESS.test(e))}function E(e){return e.startsWith("0x")?`eth|${e.slice(2)}`:e}const S=o.z.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"),b=o.z.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"),I=o.z.string().min(1,"Token description is required").max(500,"Token description must be at most 500 characters"),F=o.z.string().min(1,"Token name must be at least 1 character").max(50,"Token name must be at most 50 characters"),C=o.z.string().min(1,"Search query must be at least 1 character").max(100,"Search query must be at most 100 characters"),x=o.z.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"),D=o.z.string().regex(T.BACKEND_ADDRESS,"Address must be in format: eth|[40-hex-chars]"),P=o.z.string().regex(T.ETH_ADDRESS,"Invalid Ethereum address format"),_=o.z.string().refine(e=>T.BACKEND_ADDRESS.test(e)||T.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),L=o.z.string().refine(e=>T.BACKEND_ADDRESS.test(e)||/^service\|Token\$Unit\$[A-Z0-9]+\$eth:[0-9a-fA-F]{40}\$launchpad$/.test(e),"Invalid vault address format"),O=o.z.string().regex(/^\d+(\.\d+)?$/,"Must be a valid decimal number").refine(e=>parseFloat(e)>0,"Amount must be greater than zero"),U=o.z.string().regex(/^\d+(\.\d+)?$/,"Must be a valid decimal number").refine(e=>parseFloat(e)>=0,"Amount must be zero or greater"),$=o.z.string().regex(/^(?!0+(\.0+)?$)\d+(\.\d+)?$/,"Amount must be a positive, non-zero number"),B=o.z.string().url("Must be a valid URL").regex(/^https?:\/\//,"URL must start with http:// or https://"),M=o.z.string().optional().refine(e=>!e||/^https?:\/\/.+\..+/.test(e),"Must be a valid URL if provided"),R=o.z.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 K(e=100){return o.z.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 G=K(100),z=K(20),V=K(20),q=o.z.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=o.z.string().max(255,"Filename must be at most 255 characters"),W=o.z.enum(["image/png","image/jpg","image/jpeg","image/gif","image/webp","image/svg+xml"]),H=o.z.string().min(1,"Comment message is required").max(500,"Comment must be at most 500 characters"),Q=o.z.string().datetime("Must be a valid ISO 8601 date string"),X=o.z.number().int("Timestamp must be an integer").min(0,"Timestamp must be non-negative"),Y=o.z.string().regex(/^0x[a-fA-F0-9]{64}$/,"Private key must be format: 0x + 64 hex characters"),Z=o.z.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"),J=o.z.string().regex(/^galaconnect-operation-[a-z0-9-]+$/,"Unique key must be format: galaconnect-operation-{unique-id}"),ee=o.z.object({websiteUrl:M,telegramUrl:M,twitterUrl:M}).refine(e=>e.websiteUrl||e.telegramUrl||e.twitterUrl,"At least one social URL (website, telegram, or twitter) is required"),te=o.z.string().min(1,"Token category must not be empty").default("Unit"),ae=o.z.string().min(1,"Token collection must not be empty").default("Token"),ne=o.z.object({minFeePortion:O,maxFeePortion:O}),re=o.z.object({tokenName:S,tokenSymbol:b,tokenDescription:I,tokenImage:o.z.union([o.z.instanceof(File),o.z.instanceof(Buffer),o.z.string().url("Token image must be a valid URL")]).optional(),preBuyQuantity:U.default("0"),websiteUrl:M,telegramUrl:M,twitterUrl:M,tokenCategory:te,tokenCollection:ae,reverseBondingCurveConfiguration:ne.optional(),privateKey:Y.optional()}),oe=o.z.object({file:o.z.union([o.z.instanceof(File),o.z.instanceof(Buffer)]),tokenName:S}),se=o.z.enum(["recent","popular"]),ie=o.z.object({tokenName:S.optional(),symbol:b.optional()}).refine(e=>e.tokenName||e.symbol,"At least one of tokenName or symbol is required"),ce=o.z.enum(["NATIVE","MEME"]),le=o.z.enum(["IN","OUT"]),ue=o.z.object({from:o.z.number().int("From timestamp must be an integer").min(173e6,"From timestamp must be at least 173000000"),to:o.z.number().int("To timestamp must be an integer").min(173e6,"To timestamp must be at least 173000000"),resolution:o.z.number().int("Resolution must be an integer").min(1,"Resolution must be at least 1"),tokenName:S}),de=o.z.object({tokenName:S,slippageToleranceFactor:o.z.number().min(0).max(1).optional(),maxAcceptableReverseBondingCurveFeeSlippageFactor:o.z.number().min(0).max(1).optional(),privateKey:Y.optional()}),he=[".png",".jpg",".jpeg",".gif",".webp",".svg"],pe=o.z.object({file:o.z.union([o.z.instanceof(File),o.z.instanceof(Buffer)]),name:j,size:q,type:W}),ge=o.z.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"),me=o.z.instanceof(Buffer).refine(e=>e.length>=1&&e.length<=10485760,"Buffer size must be between 1 byte and 10MB"),fe=o.z.union([ge,me]),ye=o.z.enum([".png",".jpg",".jpeg",".gif",".webp",".svg"]),ve=j.refine(e=>{const t=e.slice(e.lastIndexOf(".")).toLowerCase();return he.includes(t)},`Filename must end with one of: ${he.join(", ")}`),Ae=o.z.object({page:R,limit:G}),we=o.z.object({page:R,limit:z}),Te=o.z.object({page:R,limit:V}),ke=o.z.object({page:R,limit:K(50)}),Ne=Ae.extend({type:o.z.enum(["recent","popular"]).optional(),tokenName:o.z.string().min(1).max(50).optional(),search:o.z.string().min(1).max(100).optional()}),Ee=we.extend({tokenName:o.z.string().min(1).max(50).optional(),search:o.z.string().min(1).max(100).optional()}),Se=Te.extend({tradeType:o.z.enum(["BUY","SELL"]).optional(),tokenName:o.z.string().min(1).max(50).optional(),userAddress:o.z.string().regex(/^(0x[a-fA-F0-9]{40}|eth\|[a-fA-F0-9]{40})$/).optional(),startDate:o.z.string().datetime().optional(),endDate:o.z.string().datetime().optional(),sortOrder:o.z.enum(["ASC","DESC"]).default("DESC")}),be=o.z.object({page:o.z.number().int().min(1),limit:o.z.number().int().min(1),total:o.z.number().int().min(0),totalPages:o.z.number().int().min(0),hasNext:o.z.boolean(),hasPrevious:o.z.boolean()});const Ie=o.z.enum(["all","DEFI","ASSET"]),Fe=we.extend({type:Ie.optional(),address:_.optional(),search:C.optional(),tokenName:F.optional()}),Ce=o.z.object({walletAddress:_,amount:$}),xe=o.z.object({address:_.optional(),refresh:o.z.boolean().optional()}),De=o.z.object({profileImage:o.z.string(),fullName:x,address:_,privateKey:Y.optional()}),Pe=o.z.object({file:o.z.union([o.z.instanceof(File),o.z.instanceof(Buffer)]),address:_.optional(),privateKey:Y.optional()}),_e=o.z.object({address:_,tokenId:o.z.union([o.z.string(),o.z.object({collection:o.z.string(),category:o.z.string(),type:o.z.string(),additionalKey:o.z.string()}),o.z.object({collection:o.z.string(),category:o.z.string(),type:o.z.string(),additionalKey:o.z.string(),instance:o.z.string()})]).optional(),tokenClassKey:o.z.object({collection:o.z.string(),category:o.z.string(),type:o.z.string(),additionalKey:o.z.string()}).optional(),tokenName:F.optional()}).refine(e=>void 0!==e.tokenId||void 0!==e.tokenName,"At least one token identifier (tokenId or tokenName) is required"),Le=o.z.enum(["buy","sell"]),Oe=o.z.enum(["BUY","SELL"]),Ue=o.z.object({tradeType:Le,tokenAmount:O,vaultAddress:L,userAddress:_,slippageTolerance:O.optional(),deadline:o.z.number().int().positive().optional()}),$e=o.z.object({tokenSymbol:b,nativeTokenQuantity:O,expectedToken:O,maxAcceptableReverseBondingCurveFee:U.default("0").optional()}),Be=o.z.object({tokenSymbol:b,tokenQuantity:O,expectedNativeToken:O,maxAcceptableReverseBondingCurveFee:U.default("0").optional()}),Me=Te.extend({tokenName:F.optional()}),Re=o.z.object({page:o.z.number().int().min(1).max(1e3).default(1).optional(),limit:o.z.number().int().min(1).max(20).default(10).optional()}),Ke=o.z.enum(["NATIVE","MEME"]),Ge=o.z.enum(["IN","OUT"]),ze=o.z.object({type:Ke,method:Ge,vaultAddress:L,amount:O}),Ve=o.z.object({nativeTokenQuantity:O}),qe=o.z.object({vaultAddress:L}),je=o.z.object({minFeePortion:O,maxFeePortion:O});function We(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 He=We(S),Qe=We(b),Xe=We(I),Ye=We(_),Ze=We(L),Je=We(O),et=We($),tt=We(x),at=We(C),nt=We(F),rt=We(re),ot=We(ee),st=We(oe),it=We(ie),ct=We(Fe),lt=We(Ce),ut=We(xe),dt=We(De),ht=We(Pe),pt=We(_e),gt=We(Ue),mt=We($e),ft=We(Be),yt=We(Me),vt=We(Re),At=We(ze),wt=We(Ve),Tt=We(qe);function kt(e,t){throw new f(e.join("; "),t,"VALIDATION_ERROR")}function Nt(e){const t=He(e);!t.success&&t.errors&&kt(t.errors,"tokenName")}function Et(e){const t=Ne.safeParse(e);t.success||kt(t.error.errors.map(e=>e.message),"pagination")}function St(e){const t=it(e);!t.success&&t.errors&&kt(t.errors,"options")}function bt(e){const t=At(e);!t.success&&t.errors&&kt(t.errors,"options")}function It(e){const t=ue.safeParse(e);t.success||kt(t.error.errors.map(e=>e.message),"options")}function Ft(e){const t=_.safeParse(e);if(!t.success)throw new f("Ethereum address must be 40 hex characters (with or without 0x prefix)","ethereumAddress","INVALID_FORMAT");return t.data}function Ct(e,t,a=!0){if(!e||""===e.trim())throw new f(`${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 f(`${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 f(`${t} must be a valid numeric string. Received: "${e}"`,t,"INVALID_NUMERIC_STRING");if(!isFinite(n))throw new f(`${t} must be a finite number. Cannot be Infinity or -Infinity.`,t,"INVALID_NUMERIC_STRING");if(n<0)throw new f(`${t} must be non-negative. Received: "${e}"`,t,"INVALID_NUMERIC_STRING");if(!a&&0===n)throw new f(`${t} must be greater than zero. Received: "${e}"`,t,"INVALID_NUMERIC_STRING")}var xt=Object.freeze({__proto__:null,normalizeAddressInput:function(e){if(!e)return;const t=_.safeParse(e);if(!t.success)throw new f(`Invalid address format: ${e}. Must be either "0x..." (Ethereum) or "eth|..." (GalaChain) format`,"address","INVALID_FORMAT");return t.data},toBackendAddressFormat:Ft,validateCheckPoolOptions:St,validateGetAmountOptions:bt,validateGetGraphOptions:It,validateNumericString:Ct,validatePagination:Et,validateTokenName:Nt});function Dt(e,t){return new f(`Token "${e}" not found. Please verify the token name is correct.`,"tokenName","TOKEN_NOT_FOUND")}function Pt(e,t){const a=t||e.charAt(0).toUpperCase()+e.slice(1);return new f(`${a} is required`,e,"REQUIRED_FIELD")}function _t(e,t,a){const n=a||e.charAt(0).toUpperCase()+e.slice(1);return new f(`${n} must be ${t}`,e,"INVALID_FORMAT")}function Lt(e,t,a){return new y(e,t,a)}function Ot(e,t){return new v(e,t)}function Ut(e,t,a){return new A(e,t,a)}function $t(e,t){const a=e,n=Number(a.page)||t.page,r=Number(a.limit)||t.limit,o=a.data,s=Number(a.total)||Number(o?.count)||0;return{page:n,limit:r,total:s,totalPages:Math.ceil(s/r)}}function Bt(e,t){return{hasNext:e<t,hasPrevious:e>1}}function Mt(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 Rt="/launchpad/upload-image",Kt="/launchpad/fetch-pool",Gt="/launchpad/check-pool",zt="/launchpad/get-graph-data",Vt="/holders",qt="/launchpad/get-badge/",jt="/trade/",Wt="/token/commment",Ht="/token/commment",Qt="/user/profile",Xt="/user/profile",Yt="/user/token-list",Zt="/user/token-hold",Jt="/user/transfer-faucets";function ea(e,t){return"string"==typeof e[t]}function ta(e,t){return void 0===e[t]||"string"==typeof e[t]}function aa(e,t){return void 0===e[t]||"number"==typeof e[t]}function na(e){return void 0===e.calculateAmountMode||"local"===e.calculateAmountMode||"external"===e.calculateAmountMode}function ra(e){if(!e||"object"!=typeof e)return!1;const t=e;return ea(t,"tokenName")&&aa(t,"from")&&aa(t,"to")&&aa(t,"resolution")}class oa{constructor(e,t=!1){this.http=e,this.logger=new d({debug:t,context:"PoolService"})}async fetchSinglePage(e){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=h(t),n=await this.http.get(Kt,a);Mt(n,"Failed to fetch pools",!0);const r=function(e){if(!e)return[];let t=[];if(e.tokens)if(Array.isArray(e.tokens))t=e.tokens.map(e=>({...e,createdAt:e.created_at||e.createdAt||""}));else{const a=e.tokens;t=[{...a,createdAt:a.created_at||a.createdAt||""}]}else e.pools&&Array.isArray(e.pools)&&(t=e.pools.map(e=>({...e,createdAt:e.created_at||e.createdAt||""})));return t}(n.data),o=n.data.count??n.data.total??0;return{pools:r,total:o,totalPages:e.limit>0?Math.ceil(o/e.limit):1}}async fetchMultiplePages(e){const{startPage:t,totalPages:a,pageSize:n,...r}=e,o=[];if(a&&a>=t){const e=[];for(let n=t;n<=a;n++)e.push(n);for(let t=0;t<e.length;t+=5){const a=e.slice(t,t+5).map(e=>this.fetchSinglePage({...r,page:e,limit:n}));(await Promise.all(a)).forEach(e=>{o.push(...e.pools)})}return o}let s=t,i=!0;for(;i;){const e=[];for(let t=0;t<5&&i;t++)e.push(s+t);const t=e.map(e=>this.fetchSinglePage({...r,page:e,limit:n})),a=await Promise.all(t);for(const e of a){if(0===e.pools.length){i=!1;break}o.push(...e.pools)}s+=e.length,s>100&&(i=!1)}return o}async fetchPools(e={}){const t=e.page||1,a=e.limit??10;let n;if(0!==a&&Et({page:t,limit:a}),e.tokenName&&Nt(e.tokenName),"recent"===e.type?n="RECENT":"popular"===e.type&&(n="POPULAR"),a>0&&a<=20){const r=await this.fetchSinglePage({...e.search&&{search:e.search},...e.tokenName&&{tokenName:e.tokenName},...n&&{type:n},page:t,limit:a});return{pools:r.pools,page:t,limit:a,total:r.total,totalPages:r.totalPages,hasNext:t<r.totalPages,hasPrevious:t>1}}if(0===a){const t=20,a=await this.fetchSinglePage({...e.search&&{search:e.search},...e.tokenName&&{tokenName:e.tokenName},...n&&{type:n},page:1,limit:t}),r=[...a.pools];if(a.pools.length>0){const o=await this.fetchMultiplePages({...e.search&&{search:e.search},...e.tokenName&&{tokenName:e.tokenName},...n&&{type:n},startPage:2,totalPages:a.totalPages>1?a.totalPages:null,pageSize:t});r.push(...o)}return{pools:r,page:1,limit:r.length,total:a.total||r.length,totalPages:1,hasNext:!1,hasPrevious:!1}}const r=Math.ceil(a/20),o=await this.fetchSinglePage({...e.search&&{search:e.search},...e.tokenName&&{tokenName:e.tokenName},...n&&{type:n},page:t,limit:20}),s=[...o.pools],i=Math.min(r,o.totalPages-t+1);if(i>1){const a=t+i-1,r=await this.fetchMultiplePages({...e.search&&{search:e.search},...e.tokenName&&{tokenName:e.tokenName},...n&&{type:n},startPage:t+1,totalPages:a,pageSize:20});s.push(...r)}const c=s.slice(0,a);return{pools:c,page:t,limit:a,total:o.total,totalPages:o.totalPages,hasNext:t<o.totalPages&&c.length<o.total,hasPrevious:t>1}}async fetchAllPools(e){return this.fetchPools({...e,limit:0})}async checkPool(e){St(e),e.tokenName&&Nt(e.tokenName);const t=h(e),a=await this.http.get(Gt,t);Mt(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(!ra(e))throw new f("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(Nt(t),!a||!n||!r)throw new f("Graph options (from, to, resolution) are required","options","MISSING_GRAPH_OPTIONS");const o={tokenName:t,from:a,to:n,resolution:r};It(o);const s=h(o),i=await this.http.get(zt,s);return Mt(i,"Failed to fetch graph data",!0),{dataPoints:i.data}}async fetchTokenDistribution(e){if(!e)throw Pt("tokenName","Token name");Nt(e);const t=await this.resolveTokenNameToVault(e);if(!t)throw new f(m(e),"tokenName","VAULT_NOT_FOUND");const a=encodeURIComponent(t),n=await this.http.get(`${Vt}/${a}`);Mt(n,"Failed to fetch token distribution",!0);const r=n.data;if(!Array.isArray(r))throw Lt("Invalid API response: expected array of holders",n.status);for(const e of r){if(!e.owner||"string"!=typeof e.owner)throw Lt("Invalid holder data: missing or invalid owner field",n.status);if(!e.quantity||"string"!=typeof e.quantity)throw Lt("Invalid holder data: missing or invalid quantity field",n.status);const t=parseFloat(e.quantity);if(isNaN(t)||!isFinite(t))throw Lt(`Invalid holder quantity: "${e.quantity}"`,n.status)}const o=r.reduce((e,t)=>e.plus(t.quantity),new s(0));return{holders:r.map(e=>{const t=new s(e.quantity),a=o.isZero()?0:t.dividedBy(o).multipliedBy(100).toNumber();return{address:e.owner,balance:e.quantity,percentage:a}}),totalSupply:o.toFixed(),totalHolders:r.length,lastUpdated:new Date}}async fetchTokenBadges(e){if(!e)throw Pt("tokenName","Token name");Nt(e);const t=await this.http.get(qt,{tokenName:e});return Mt(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});if(t.pools&&Array.isArray(t.pools)&&t.pools.length>0)return t.pools[0].vaultAddress||null;if(t.pools&&"object"==typeof t.pools){const e=t.pools.tokens;return e?.vaultAddress||null}return null}catch{return null}}}function sa(e,t={}){const{stringifyFields:a=[],optionalFields:n=[],fieldMappings:r={}}=t,o={};for(const[t,s]of Object.entries(e)){const e=t;if(n.includes(e)&&void 0===s)continue;if(n.includes(e)&&"string"==typeof s&&0===s.trim().length)continue;const i=r[e],c=i?String(i):t;a.includes(e)?o[c]=String(s):o[c]=s}return h(o)}const ia={PAGINATION:{MIN_PAGE:1,MAX_PAGE:1e3,MIN_LIMIT:1,MAX_LIMIT:20}};class ca{constructor(e,t=!1){this.http=e,this.logger=new d({debug:t,context:"TradeService"})}async fetchTrades(e){if(!function(e){if(!e||"object"!=typeof e)return!1;const t=e;return ea(t,"tokenName")&&(void 0===t.tradeType||"buy"===t.tradeType||"sell"===t.tradeType)&&ta(t,"userAddress")&&aa(t,"page")&&aa(t,"limit")}(e))throw new f("Invalid options provided. Expected { tokenName: string, tradeType?: string, userAddress?: string, page?: number, limit?: number, startDate?: Date, endDate?: Date, sortOrder?: string }","options","INVALID_OPTIONS");const{tokenName:t,tradeType:a,userAddress:n,page:r=1,limit:o=10,startDate:s,endDate:i,sortOrder:c}=e;if(!k(t))throw new f("Token name is required and must be a non-empty string","tokenName","INVALID_TOKEN_NAME");w(r,o,ia);const l=function(e,t,a){return sa({tokenName:e,page:t,limit:a},{stringifyFields:["page","limit"]})}(t,r,o),u=await this.http.get(jt,l),d=(h=u.data)?Array.isArray(h)?h:h.trades:[];var h;const p=$t(u,{page:r,limit:o}),g=Bt(p.page,p.totalPages);return{trades:d,...p,...g}}}const la=new d({debug:!1,context:"DateUtils"});function ua(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())?(la.warn(`Invalid date string received: "${e}". Using fallback.`),t||new Date):a}catch(a){return la.warn(`Date parsing error for "${e}":`,a),t||new Date}}const da={PAGINATION:{MIN_PAGE:1,MAX_PAGE:1e3,MIN_LIMIT:1,MAX_LIMIT:50},CONTENT:{MIN_LENGTH:1,MAX_LENGTH:500}};class ha{constructor(e,t,a=!1){this.http=e,this.poolService=t,this.logger=new d({debug:a,context:"CommentService"})}async fetchComments(e){if(!function(e){if(!e||"object"!=typeof e)return!1;const t=e;return ea(t,"tokenName")&&aa(t,"page")&&aa(t,"limit")}(e))throw new f("Invalid options provided. Expected { tokenName: string, page?: number, limit?: number }","options","INVALID_OPTIONS");const{tokenName:t,page:a=1,limit:n=10}=e;if(!k(t))throw new f("Token name is required and must be a non-empty string","tokenName","INVALID_TOKEN_NAME");w(a,n,da);const r=await this.poolService.resolveTokenNameToVault(t);if(!r)throw Dt(t);const o=sa({vaultAddress:r,page:a,limit:n},{stringifyFields:["page","limit"]}),s=await this.http.get(Wt,o);Mt(s,"Failed to fetch comments");return{comments:s.data.comments.map(e=>({...e,createdAt:ua(e.createdAt)})),total:s.data.count}}async postComment(e){if(!function(e){if(!e||"object"!=typeof e)return!1;const t=e;return ea(t,"tokenName")&&ea(t,"content")}(e))throw new f("Invalid options provided. Expected { tokenName: string, content: string }","options","INVALID_OPTIONS");const{tokenName:t,content:a}=e;if(!k(t))throw new f("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>=da.CONTENT.MIN_LENGTH&&t.length<=da.CONTENT.MAX_LENGTH}(a))throw new f(`Comment content must be between ${da.CONTENT.MIN_LENGTH} and ${da.CONTENT.MAX_LENGTH} characters`,"content","INVALID_CONTENT");const n=await this.poolService.resolveTokenNameToVault(t);if(!n)throw Dt(t);const r={userAddress:this.http.getAddress(),vaultAddress:n,content:a};Mt(await this.http.post(Ht,r),"Failed to create comment")}}function pa(e,t="image",a){const n=new FormData;if("undefined"!=typeof File&&e instanceof File)n.append(t,e);else{if(!Buffer.isBuffer(e))throw _t("file","a File object (browser) or Buffer (Node.js)");{const r=new Blob([e],{type:"image/png"});n.append(t,r,a)}}return n}const ga={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 ma(e){return!(!e||"string"!=typeof e)&&ga.USER_ADDRESS.PATTERN.test(e)}const fa="Update profile";class ya{constructor(e){this.http=e}async fetchProfile(e){const t=e??this.http.getAddress();if(!ma(t))throw new f("Address must be in format: eth|[40-hex-chars]","address","INVALID_ADDRESS");const a={userAddress:t};return await this.http.get(Qt,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(fa),r={address:n.address,message:fa,publickey:n.ethereumAddress,sign:n.signature};Mt(await this.http.put(Xt,a,r),"Profile update failed")}async uploadProfileImage(e){this.validateUploadProfileImageOptions(e);const t=e.address??this.http.getAddress();try{const a=`profile-image-${t}.png`,n=pa(e.file,"image",a),r=await this.http.request({method:"POST",url:`${Rt}?tokenName=${encodeURIComponent(t)}`,data:n,headers:{}});return Mt(r,"Image upload failed"),"string"==typeof r.data?r.data:""}catch(e){if(e instanceof f)throw e;throw new f(`Profile image upload failed: ${e instanceof Error?e.message:"Unknown error"}`,"file","UPLOAD_FAILED")}}async fetchTokenList(e){return this.buildFetchRequest(Yt,e,{includeType:!0,errorMessage:"Failed to fetch token list"})}async fetchTokensHeld(e){return this.buildFetchRequest(Zt,e,{includeType:!1,errorMessage:"Failed to fetch tokens held"})}async fetchTokensCreated(e={}){const{page:t=1,limit:a=10,search:n,tokenName:r}=e,o={type:"DEFI",address:this.http.getAddress(),page:t,limit:a};return void 0!==n&&(o.search=n),void 0!==r&&(o.tokenName=r),this.fetchTokenList(o)}async buildFetchRequest(e,t,a){this.validateGetTokenListOptions(t);const n={page:t.page,limit:t.limit,address:t.address,search:t.search,tokenName:t.tokenName},r=sa(a.includeType?{...n,type:"all"!==t.type&&t.type?t.type:"DEFI"}:n,{stringifyFields:["page","limit"],optionalFields:["address","search","tokenName"]}),o=await this.http.get(e,r);Mt(o,a.errorMessage,!0);const s=(i=o.data)?Array.isArray(i)?i:i.token:[];var i;const c=$t(o,{page:t.page||1,limit:t.limit||10}),l=Bt(c.page,c.totalPages);return{tokens:s,...c,...l}}validateGetTokenListOptions(e){if(w(e.page,e.limit,ga),void 0!==e.address&&!ma(e.address))throw new f("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>=ga.SEARCH.MIN_LENGTH&&t.length<=ga.SEARCH.MAX_LENGTH))throw new f(`Search query must be between ${ga.SEARCH.MIN_LENGTH} and ${ga.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>=ga.TOKEN_NAME.MIN_LENGTH&&a.length<=ga.TOKEN_NAME.MAX_LENGTH))throw new f(`Token name must be between ${ga.TOKEN_NAME.MIN_LENGTH} and ${ga.TOKEN_NAME.MAX_LENGTH} characters`,"tokenName","INVALID_TOKEN_NAME")}validateUpdateProfileData(e){if(!ma(e.address))throw new f("Address must be in format: eth|[40-hex-chars]","address","INVALID_ADDRESS");if(!((t=e.fullName)&&"string"==typeof t&&t.length>=ga.PROFILE.FULL_NAME.MIN_LENGTH&&t.length<=ga.PROFILE.FULL_NAME.MAX_LENGTH&&ga.PROFILE.FULL_NAME.ALPHABETS_ONLY_PATTERN.test(t)))throw new f(`Full name must be between ${ga.PROFILE.FULL_NAME.MIN_LENGTH} and ${ga.PROFILE.FULL_NAME.MAX_LENGTH} characters`,"fullName","INVALID_FULL_NAME");var t}validateUploadProfileImageOptions(e){if(e.address&&!ma(e.address))throw new f("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 Aa(e,t,a){if(!e)throw new va("File is required",t,a);if("undefined"!=typeof File&&e instanceof File){const t=ge.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=me.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 o=function(e){if(!e)return"";const t=e.lastIndexOf(".");if(-1===t)return"";return e.substring(t).toLowerCase()}(t),s=[".png",".jpg",".jpeg",".gif",".webp",".svg"];if(!s.includes(o))throw new va(`File extension "${o}" is not allowed. Allowed extensions: ${s.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"}}(o);if(i!==a&&"application/octet-stream"!==i)throw new va(`File extension "${o}" 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 wa{constructor(e,t=!1){this.http=e,this.logger=new d({debug:t,context:"ImageService"})}async uploadImageByTokenName(e){const{tokenName:t,options:a}=e;Nt(t);const n=`${t}.png`;Aa(a.file,n,"image/png");try{const e=`${a.tokenName??t}.png`,n=pa(a.file,"image",e),r=await this.http.request({method:"POST",url:`${Rt}?tokenName=${encodeURIComponent(a.tokenName??t)}`,data:n,headers:{}});return Mt(r,"Image upload failed"),"string"==typeof r.data?r.data:""}catch(e){if(e instanceof Error&&e.message.includes("FormData"))throw Ot("File upload failed: FormData not supported in this environment. Ensure you have proper polyfills for Node.js environments.","FormData");throw e}}}class Ta{constructor(e,t=!1){this.http=e,this.logger=new d({debug:t,context:"FaucetService"})}async transferFaucets(e){this.validateTransferFaucetsData(e);const t={userAddress:e.walletAddress,amount:e.amount};Mt(await this.http.post(Jt,t),"Faucet transfer failed")}validateTransferFaucetsData(e){if(!ma(e.walletAddress))throw new f("Address must be in format: eth|[40-hex-chars]","address","INVALID_ADDRESS");if(!(t=e.amount)||"string"!=typeof t||!ga.FAUCET_AMOUNT.POSITIVE_NON_ZERO_DECIMAL.test(t))throw new f("Amount must be a positive decimal string greater than zero","amount","INVALID_AMOUNT");var t}}class ka{constructor(e,t=!1){this.http=e,this.poolService=new oa(e,t),this.tradeService=new ca(e,t),this.commentService=new ha(e,this.poolService,t),this.userService=new ya(e),this.imageService=new wa(e,t),this.faucetService=new Ta(e,t)}async uploadImageByTokenName(e){return this.imageService.uploadImageByTokenName(e)}async fetchPools(e={}){return this.poolService.fetchPools(e)}async fetchAllPools(e){return this.poolService.fetchAllPools(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 Nt(e)}}const Na={MAX_UNIQUE_KEY_LENGTH:64,UNIQUE_KEY_PATTERN:/^(galaswap-operation-|galaconnect-operation-)/,TOKEN_NAME_PATTERN:/^[a-zA-Z0-9]+$/};var Ea;!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"}(Ea||(Ea={}));class Sa extends Error{constructor(e,t,a){super(e),this.type=t,this.details=a,this.name="TransferError"}}class ba{static validateAmount(e){if(parseFloat(e)<=0)throw new Sa("Transfer amount must be positive",Ea.INVALID_AMOUNT,{amount:e})}static validateUniqueKey(e){if(e){if(e.length>Na.MAX_UNIQUE_KEY_LENGTH)throw new f(`Unique key too long. Maximum length: ${Na.MAX_UNIQUE_KEY_LENGTH}`);if(!Na.UNIQUE_KEY_PATTERN.test(e))throw new Sa('Invalid unique key format. Must start with "galaswap-operation-" or "galaconnect-operation-"',Ea.INVALID_AMOUNT,{uniqueKey:e})}}}class Ia extends r.ChainCallDTO{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 o;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`);o={collection:e[0],category:e[1],type:e[2],additionalKey:e[3],instance:"0"}}else o={collection:n.collection,category:n.category,type:n.type,additionalKey:n.additionalKey,instance:"0"};return new Ia({from:e,to:t,quantity:a,tokenInstance:o,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 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(e){const t=e.replace("0x","");return`eth|${a.ethers.getAddress(`0x${t}`).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 f(`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 f(`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 f("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 f(`Invalid tokenId type: ${typeof e}. Expected string, TokenClassKey, or TokenInstanceKey`,"tokenId","INVALID_TOKEN_ID_TYPE")}var xa=Object.freeze({__proto__:null,normalizeToTokenInstanceKey:Ca});function Da(e){return e instanceof Error}function Pa(e){return Da(e)||function(e){return"object"==typeof e&&null!==e&&"message"in e&&"string"==typeof e.message}(e)?e.message:"string"==typeof e?e:String(e)}function _a(e){return"object"==typeof e&&null!==e&&"message"in e&&("response"in e||"request"in e||"config"in e)}const La="gala-transfer-successful",Oa="token-transfer-successful",Ua="transfer-successful-no-id";class $a{constructor(e,t,a,n=!1){this.http=e,this.wallet=t,this.tokenResolver=a,this.signatureHelper=new Fa(t),this.logger=new d({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 Lt(`Failed to fetch pool details: Status ${t.Status}`,t.Status);const a=t.Data.reverseBondingCurveConfiguration,n=a?.minFeePortion??"0",r=a?.maxFeePortion??"0",o=(await import("bignumber.js")).default,s=!new o(n).isZero()||!new o(r).isZero(),i=t.Data;return i.reverseBondingCurveMinFeePortion=n,i.reverseBondingCurveMaxFeePortion=r,i.hasReverseBondingCurveFee=s,i.isGraduated="Completed"===t.Data.saleStatus,delete i.reverseBondingCurveConfiguration,t}async fetchLaunchTokenFee(){const e=await this.http.post("/api/asset/launchpad-contract/FetchLaunchpadFeeAmount",{});if(1!==e.Status)throw Lt(`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 f("Invalid fetch pool details data: missing required fields","data","INVALID_TYPE");var t;if(!e.vaultAddress||"string"!=typeof e.vaultAddress)throw new f("Vault address is required and must be a string","vaultAddress","INVALID_VAULT_ADDRESS");if(!e.vaultAddress.startsWith("service|Token$Unit$"))throw new f("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 Lt(`Failed to fetch token balance from GalaChain: ${Pa(e)}`,void 0,Da(e)?e:void 0)}}async transferGala(e){this.validateTransferGalaData(e);try{const t=E(e.recipientAddress),a=E(this.wallet.address),n=Ia.forGALA(a,t,e.amount,e.uniqueKey),r=await this.signatureHelper.signTransferToken(n.toSigningPayload()),o=new Ia({...n.toSigningPayload(),signedPayload:r});this.logger.debug("[DEBUG] Full GALA Transfer Request Payload:",JSON.stringify(o,null,2));const s=await this.http.post("/api/asset/token-contract/TransferToken",o);return this.logger.debug("[DEBUG] Transfer response:",JSON.stringify(s,null,2)),this.extractTransactionIdFromResponse(s,"gala")}catch(t){throw this.handleTransferError(t,"GALA transfer failed",e)}}async transferToken(e){this.validateTransferTokenData(e);try{const t=E(e.to),a=E(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 Sa("Must provide either tokenId or tokenName for token identification",Ea.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)}`}),o=await this.signatureHelper.signTransferToken(r.toSigningPayload()),s=new Ia({...r.toSigningPayload(),signedPayload:o});this.logger.debug("[DEBUG] Full Transfer Request Payload:",JSON.stringify(s,null,2));const i=await this.http.post("/api/asset/token-contract/TransferToken",s);return this.logger.debug("[DEBUG] Token transfer response:",JSON.stringify(i,null,2)),this.extractTransactionIdFromResponse(i,"token")}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 Sa)throw t;throw new Sa(`Failed to resolve token class key for '${e}': ${t instanceof Error?t.message:String(t)}`,Ea.TOKEN_NOT_FOUND,{tokenName:e})}}validateTransferGalaData(e){if(!function(e){if(!e||"object"!=typeof e)return!1;const t=e;return"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)}(e))throw new f("Invalid GALA transfer data: missing required fields");if(!N(e.recipientAddress))throw new Sa("Invalid recipient address format",Ea.INVALID_RECIPIENT,{recipientAddress:e.recipientAddress});ba.validateAmount(e.amount),ba.validateUniqueKey(e.uniqueKey)}validateTransferTokenData(e){if(!function(e){if(!e||"object"!=typeof e)return!1;const t=e;return"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)}(e))throw new f("Invalid token transfer data: missing required fields");if(!N(e.to))throw new Sa("Invalid recipient address format",Ea.INVALID_RECIPIENT,{recipientAddress:e.to});if(!e.tokenId&&!e.tokenName)throw new Sa("Must provide either tokenId or tokenName for token identification",Ea.TOKEN_NOT_FOUND);if(e.tokenName&&!Na.TOKEN_NAME_PATTERN.test(e.tokenName))throw new Sa("Invalid token name format",Ea.TOKEN_NOT_FOUND,{tokenName:e.tokenName});ba.validateAmount(e.amount),ba.validateUniqueKey(e.uniqueKey)}async resolveTokenInstance(e){try{const t=await this.tokenResolver.resolveTokenToVault(e);if(!t)throw new Sa(`Token '${e}' not found or not available for transfer`,Ea.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 Sa)throw t;throw new Sa(`Failed to resolve token '${e}': ${t instanceof Error?t.message:String(t)}`,Ea.TOKEN_NOT_FOUND,{tokenName:e})}}resolveTokenInstanceFromVaultAddress(e){const[t,a]=e.split("|");if(!a)throw new Sa(`Invalid vault address format: missing token components. Address: ${e}`,Ea.TOKEN_NOT_FOUND);const n=a.split("$");if(n.length<4)throw new Sa(`Invalid vault address format: insufficient token components. Expected 4+, got ${n.length}. Address: ${e}`,Ea.TOKEN_NOT_FOUND);return{collection:n[0],category:n[1],type:n[2],additionalKey:n[3],instance:"0"}}extractTransactionIdFromResponse(e,t){if(e&&"object"==typeof e){if("Status"in e&&1===e.Status&&"Data"in e){const a=e;return Array.isArray(a.Data)&&a.Data.length>0?"gala"===t?La:Oa:Ua}if("transactionId"in e&&"string"==typeof e.transactionId&&e.transactionId)return e.transactionId}throw new Sa("Transfer succeeded but transaction ID could not be extracted",Ea.NETWORK_ERROR)}handleTransferError(e,t,a){if(e instanceof Sa)return e;if(e instanceof f)return new Sa(e.message,Ea.INVALID_AMOUNT);if(_a(e)&&e.response){const t=e.response.status,n=e.response.data;if(400===t)return new Sa(("string"==typeof n?.message?n.message:void 0)||"Invalid transfer request",Ea.INVALID_AMOUNT);if(403===t)return new Sa("Insufficient balance for transfer",Ea.INSUFFICIENT_BALANCE);if(404===t){const e={};return"tokenName"in a&&(e.tokenName=a.tokenName),new Sa("Token not found",Ea.TOKEN_NOT_FOUND,e)}}if("object"==typeof e&&null!==e&&"code"in e&&("ECONNABORTED"===e.code||"ETIMEDOUT"===e.code))return new Sa("Transfer request timed out",Ea.NETWORK_ERROR);const n=Pa(e);return new Sa(n||t,Ea.NETWORK_ERROR)}}class Ba{constructor(e,t=!1){this.http=e,this.logger=new d({debug:t,context:"DexService"})}async fetchTokenSpotPrice(e){if(!e||Array.isArray(e)&&0===e.length)throw Pt("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 Lt(`Failed to fetch token prices: ${e instanceof Error?e.message:e}`)}}async fetchLaunchpadTokenSpotPrice(e,t){if(!e||"string"!=typeof e)throw new Error(g);try{const a=await t({tokenName:e,amount:"1",type:"native"}),n=(await this.fetchTokenSpotPrice("GALA"))[0];if(!n)throw Lt("GALA price not available");const r=Number(a.amount)/1e18;if(r<=0)throw new f(`Invalid token amount calculation: ${r}`,"amount","INVALID_CALCULATION");const o=n.price/r;return{symbol:e.toUpperCase(),price:o}}catch(t){if(t instanceof Error)throw Lt(`Failed to calculate launchpad token spot price for ${e}: ${t.message}`);throw Lt(`Failed to calculate launchpad token spot price for ${e}: ${String(t)}`)}}}function Ma(e,t=18){const a=parseFloat(e);if(0===a)return"0";return a.toFixed(t).replace(/\.?0+$/,"")}function Ra(e){return Ma(e,8)}function Ka(e){return Ma(e,18)}function Ga(e){return{maxAcceptableReverseBondingCurveFee:Ra(e.maxAcceptableReverseBondingCurveFee)}}new d({debug:!1,context:"NumberUtils"});class za extends r.ChainCallDTO{constructor(e,t,a="0",n={maxAcceptableReverseBondingCurveFee:"0"}){super(),this.vaultAddress=e,this.nativeTokenQuantity=Ra(t),this.expectedToken=Ka(a),this.extraFees=Ga(n)}}class Va extends r.ChainCallDTO{constructor(e,t,a,n={maxAcceptableReverseBondingCurveFee:"0"}){super(),this.vaultAddress=e,this.tokenQuantity=Ka(t),this.expectedNativeToken=Ra(a),this.extraFees=Ga(n)}}class qa extends r.ChainCallDTO{constructor(e,t,a="0",n={maxAcceptableReverseBondingCurveFee:"0"}){super(),this.vaultAddress=e,this.tokenQuantity=Ka(t),this.expectedNativeToken=Ra(a),this.extraFees=Ga(n)}}class ja extends r.ChainCallDTO{constructor(e,t,a,n={maxAcceptableReverseBondingCurveFee:"0"}){super(),this.vaultAddress=e,this.nativeTokenQuantity=Ra(t),this.expectedToken=Ka(a),this.extraFees=Ga(n)}}const Wa={BuyNativeDto:za,BuyExactDto:Va,SellExactDto:qa,SellNativeDto:ja};var Ha;!function(e){e[e.METAMASK=0]="METAMASK",e[e.TRUST_WALLET=1]="TRUST_WALLET",e[e.GALA_WALLET=2]="GALA_WALLET"}(Ha||(Ha={}));class Qa{constructor(e,t=!1){this.walletProvider=e,this.debug=t,this.logger=new d({debug:t,context:"SignatureService"})}async signDTO(e,t,a,n=Ha.METAMASK){try{this.logger.debug("🔐 Signing DTO:",{methodName:t,walletPreference:n,dtoKeys:Object.keys(e)});const o=this.generateEIP712Types(t,e),s=r.calculatePersonalSignPrefix(e),i={...e,prefix:s};let c,l,u;switch(n){case Ha.GALA_WALLET:({signature:c,domain:l}=await this.signWithGalaWallet(o,i,t,a));break;case Ha.TRUST_WALLET:c=await this.signWithTrustWallet(i),l={name:"ethereum",chainId:1};break;case Ha.METAMASK:default:({signature:c,domain:l}=await this.signWithMetaMask(o,i))}return u=n===Ha.TRUST_WALLET?{...i,signature:c}:{...e,signature:c,types:o,domain:l},this.logger.debug("✅ DTO signed successfully:",{payloadKeys:Object.keys(u),signatureLength:c.length}),u}catch(e){this.logger.error("❌ Signature generation failed:",e);throw Ut(`Failed to sign DTO: ${Pa(e)}`)}}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 Ot("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 Ut(`MetaMask/ethers signing failed: ${Pa(e)}`)}}async signWithTrustWallet(e){try{const a=(t=e,JSON.stringify(t));let n;if(!this.walletProvider.signMessage)throw Ot("Wallet provider does not support signMessage","walletProvider");return n=await this.walletProvider.signMessage(a),n}catch(e){throw Ut(`TrustWallet signing failed: ${Pa(e)}`)}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 o={domain:r,types:e,message:t,Primary_type:a},s=window;if(!s.gala)throw Ot("GalaWallet not found in window object","galaWallet");await s.gala.setAddress(n);return{signature:await s.gala.request({method:"eth_signTypedData",params:[JSON.stringify(o),n]}),domain:r}}catch(e){throw Ut(`GalaWallet signing failed: ${Pa(e)}`)}}generateEIP712Types(e,t){const a={};a[e]=[];const n=(e,t,r,o=!1)=>{if(void 0!==t){if(Array.isArray(t)){const s=n(e,t[0],r,!0);return o||a[r].push({name:e,type:(s??e)+"[]"}),s?s+"[]":void 0}if("object"==typeof t&&null!==t){if(a[e])throw new f(`Type name collision not supported: ${e}`,"fieldValue","TYPE_COLLISION");return a[e]=[],Object.entries(t).forEach(([t,a])=>{n(t,a,e)}),o||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 f(`Unsupported type for fieldName ${e}: ${typeof t}, value: ${t}`,"fieldValue","UNSUPPORTED_TYPE")}return o||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 Ha.METAMASK;const e=window;return e.gala?Ha.GALA_WALLET:e.trustWallet?.isTrust?Ha.TRUST_WALLET:(e.ethereum,Ha.METAMASK)}}class Xa{constructor(e=!1){this.debug=e,this.logger=new d({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`,o=`$tokenBalance$${a.toStringKey()}$${e}`,s=`$tokenBalance$${a.toStringKey()}$${e}`,i=`$tokenBalance$${n.toStringKey()}$${e}`,c=[r,o,s,i,`$tokenBalance$${n.toStringKey()}$${e}`];return this.logger.debug("✅ Generated stringsInstructions:",c),c}catch(e){this.logger.error("❌ Failed to generate stringsInstructions:",e);const t=Pa(e);throw new f(`Failed to generate stringsInstructions: ${t}`,"vaultAddress","INVALID_VAULT_ADDRESS")}}createTokenInstance(e){const t=new i.TokenClassKey;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 i.TokenClassKey;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 Pt("vaultAddress","Vault address");const t=e.split("$");if(t.length<3)throw _t("vaultAddress","format: service|Token$Unit$SYMBOL$eth:address$launchpad");const a=t[2];if(!a||0===a.trim().length)throw new f(`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 Pt("vaultAddress","Vault address");if(!e.startsWith("service|Token$Unit$"))throw _t("vaultAddress",'starting with "service|Token$Unit$"');if(!e.endsWith("$launchpad"))throw _t("vaultAddress",'ending with "$launchpad"');const t=e.split("$");if(t.length<5)throw _t("vaultAddress",'having at least 5 parts separated by "$"');const a=t[2];if(!a||!/^[A-Za-z]{1,10}$/.test(a))throw _t("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 _t("stringKey","format: collection$category$type$additionalKey (4 parts)");return{collection:t[0],category:t[1],type:t[2],additionalKey:t[3]}}}function Ya(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 s(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 o;switch(a){case"buy-native":case"sell-exact":o=n.minus(r);break;case"buy-exact":case"sell-native":o=n.plus(r);break;default:throw new Error(`Unknown operation type: ${a}`)}return o.isLessThan(0)&&(o=new s(0)),o.toFixed()}const Za=[];for(let e=0;e<256;++e)Za.push((e+256).toString(16).slice(1));let Ja;const en=new Uint8Array(16);var tn,an,nn={randomUUID:"undefined"!=typeof crypto&&crypto.randomUUID&&crypto.randomUUID.bind(crypto)};function rn(e,t,a){const n=(e=e||{}).random??e.rng?.()??function(){if(!Ja){if("undefined"==typeof crypto||!crypto.getRandomValues)throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");Ja=crypto.getRandomValues.bind(crypto)}return Ja(en)}();if(n.length<16)throw new Error("Random bytes length must be >= 16");return n[6]=15&n[6]|64,n[8]=63&n[8]|128,function(e,t=0){return(Za[e[t+0]]+Za[e[t+1]]+Za[e[t+2]]+Za[e[t+3]]+"-"+Za[e[t+4]]+Za[e[t+5]]+"-"+Za[e[t+6]]+Za[e[t+7]]+"-"+Za[e[t+8]]+Za[e[t+9]]+"-"+Za[e[t+10]]+Za[e[t+11]]+Za[e[t+12]]+Za[e[t+13]]+Za[e[t+14]]+Za[e[t+15]]).toLowerCase()}(n)}function on(e,t,a){return nn.randomUUID&&!e?nn.randomUUID():rn(e)}class sn{constructor(e,t,a=!1,n,r,o=.05,s=.01){this.httpClient=e,this.tokenResolver=t,this.walletProvider=n,this.userAddress=r,this.defaultSlippageToleranceFactor=o,this.defaultMaxAcceptableReverseBondingCurveFeeSlippageFactor=s,this.bundleEndpoint="/bundle",this.logger=new d({debug:a,context:"BundleService"}),n&&r&&(this.signatureService=new Qa(n,a),this.tokenKeyService=new Xa(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 Pt("bundleData","Bundle data");if(!e.signedDto)throw Pt("signedDto","Signed DTO");if(!e.method||"string"!=typeof e.method)throw Pt("method","Method name");if(!Array.isArray(e.stringsInstructions))throw _t("stringsInstructions","an array of resource tracking strings");if(0===e.stringsInstructions.length)throw new f("stringsInstructions cannot be empty","stringsInstructions","EMPTY_ARRAY");const t=["BuyWithNative","BuyExactToken","SellExactToken","SellWithNative"];if(!t.includes(e.method))throw _t("method",`one of: ${t.join(", ")}`);e.stringsInstructions.forEach((e,t)=>{if("string"!=typeof e||0===e.length)throw new f(`stringsInstructions[${t}] must be a non-empty string`,`stringsInstructions[${t}]`,"INVALID_INSTRUCTION");if(!e.startsWith("$"))throw new f(`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){if("string"==typeof e)return e;if(_a(e)&&e.response){const t=e.response.data;if(t&&"object"==typeof t){const e=t;if(e.error)return String(e.error);if(e.message)return String(e.message)}}return Pa(e)||"Unknown bundle transaction error"}async getBundlerTransactionResult(e){try{if(!e||"string"!=typeof e)throw Pt("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 Pt("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}=e,{effectiveSlippageFactor:o,effectiveMaxFee:s,vaultAddress:i}=await this.prepareTradingOperation(t,e.maxAcceptableReverseBondingCurveFee,e.maxAcceptableReverseBondingCurveFeeSlippageFactor,e.slippageToleranceFactor);if("native"===n){if(!r)throw new f("expectedAmount is required for native buy operations. Use getBuyTokenAmount() first to calculate expected tokens.","expectedAmount","EXPECTED_AMOUNT_REQUIRED");const e=Ya(r,o,"buy-native");this.logger.debug("BuyNative slippage applied:",{originalExpectedTokens:r,slippageFactor:o,adjustedMinTokens:e});const t=new Wa.BuyNativeDto(i,a,e,{maxAcceptableReverseBondingCurveFee:s});return await this.executeBundleTransaction(t,"BuyWithNative",i)}{if(!r)throw new f("expectedAmount is required for exact buy operations. Use getBuyTokenAmount() first to calculate expected GALA cost.","expectedAmount","EXPECTED_AMOUNT_REQUIRED");const e=Ya(r,o,"buy-exact");this.logger.debug("BuyExact slippage applied:",{originalExpectedGalaCost:r,slippageFactor:o,adjustedMaxGalaCost:e});const t=new Wa.BuyExactDto(i,a,e,{maxAcceptableReverseBondingCurveFee:s});return await this.executeBundleTransaction(t,"BuyExactToken",i)}}async sellToken(e){this.ensureTradingServicesAvailable();const{tokenName:t,amount:a,type:n,expectedAmount:r}=e,{effectiveSlippageFactor:o,effectiveMaxFee:s,vaultAddress:i}=await this.prepareTradingOperation(t,e.maxAcceptableReverseBondingCurveFee,e.maxAcceptableReverseBondingCurveFeeSlippageFactor,e.slippageToleranceFactor);if("exact"===n){if(!r)throw new f("expectedAmount is required for exact sell operations. Use getSellTokenAmount() first to calculate expected GALA.","expectedAmount","EXPECTED_AMOUNT_REQUIRED");const e=Ya(r,o,"sell-exact");this.logger.debug("SellExact slippage applied:",{originalExpectedGala:r,slippageFactor:o,adjustedMinGala:e});const t=new Wa.SellExactDto(i,a,e,{maxAcceptableReverseBondingCurveFee:s});return await this.executeBundleTransaction(t,"SellExactToken",i)}{if(!r)throw new f("expectedAmount is required for native sell operations. Use getSellTokenAmount() first to calculate tokens to sell.","expectedAmount","EXPECTED_AMOUNT_REQUIRED");const e=Ya(r,o,"sell-native");this.logger.debug("SellNative slippage applied:",{originalExpectedTokensToSell:r,slippageFactor:o,adjustedMaxTokensToSell:e});const t=new Wa.SellNativeDto(i,a,e,{maxAcceptableReverseBondingCurveFee:s});return await this.executeBundleTransaction(t,"SellWithNative",i)}}async prepareTradingOperation(e,t,a,n){const{effectiveSlippageFactor:r,effectiveMaxFee:o}=this.calculateEffectiveSlippage(t,a,n),s=await this.resolveTokenNameToVault(e);if(!s)throw Dt(e);return{effectiveSlippageFactor:r,effectiveMaxFee:o,vaultAddress:s}}calculateEffectiveSlippage(e,t,a){const n=a??this.defaultSlippageToleranceFactor,r=t??this.defaultMaxAcceptableReverseBondingCurveFeeSlippageFactor;let o=e||"0";return e&&(o=Ya(e,r,"buy-exact"),this.logger.debug("Reverse bonding curve fee slippage applied:",{baseFee:e,slippageFactor:r,adjustedMaxFee:o})),{effectiveSlippageFactor:n,effectiveFeeSlippageFactor:r,effectiveMaxFee:o}}ensureTradingServicesAvailable(){if(!this.signatureService||!this.tokenKeyService)throw Ot("Trading services not available. BundleService requires walletProvider and userAddress for trading operations.","walletProvider");if(!this.userAddress)throw Pt("userAddress","User address")}async executeBundleTransaction(e,t,a){this.ensureTradingServicesAvailable();try{e.uniqueKey=`galaswap - operation - ${on()}-${Date.now()}-${this.userAddress}`;const n=await this.signatureService.signDTO(e,t,this.userAddress),r=this.tokenKeyService.generateStringsInstructions(a),o={stringsInstructions:r,method:t,signedDto:n};this.logger.debug("📦 Bundle transaction data:",{method:t,stringsInstructions:r,dtoKeys:Object.keys(n)});const s=await this.submitTransaction(o);if(s.success&&s.data)return this.logger.debug("✅ Bundle transaction submitted:",s.data),{success:!0,data:{transactionId:s.data,message:"Transaction submitted successfully. Monitor WebSocket for completion."}};throw new Error(String(s.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"}(tn||(tn={})),exports.SDKTransactionStatus=void 0,(an=exports.SDKTransactionStatus||(exports.SDKTransactionStatus={})).PENDING="pending",an.PROCESSING="processing",an.COMPLETED="completed",an.FAILED="failed",an.TIMEOUT="timeout";const cn={[tn.PROCESSED]:exports.SDKTransactionStatus.COMPLETED,[tn.COMPLETED]:exports.SDKTransactionStatus.COMPLETED,[tn.SUCCESS]:exports.SDKTransactionStatus.COMPLETED,[tn.FAILED]:exports.SDKTransactionStatus.FAILED,[tn.ERROR]:exports.SDKTransactionStatus.FAILED,[tn.PROCESSING]:exports.SDKTransactionStatus.PROCESSING,[tn.PENDING]:exports.SDKTransactionStatus.PENDING};class ln{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 d({debug:t,context:"WebSocketService"}),this.isSocketIOAvailable=this.checkSocketIOAvailability()}checkSocketIOAvailability(){try{return"function"==typeof c.io||(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=c.io(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:exports.SDKTransactionStatus.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,r=n?.data,o=n?.status||n?.Status||r?.status||r?.Status;let s=n?.message||n?.Message||r?.message||r?.Message||n?.error||r?.error;s&&"string"==typeof s||(s=o===tn.FAILED||o===tn.ERROR?"Transaction failed - check transaction details":o===tn.COMPLETED||o===tn.PROCESSED||o===tn.SUCCESS?"Transaction completed successfully":o?`Transaction status: ${o}`:"Unknown transaction status");const i=n?.blockHash||r?.blockHash,c=n?.gasUsed||r?.gasUsed,l={transactionId:e,status:this.mapSocketStatus(o),message:"string"==typeof s?s:"Transaction update received",timestamp:Date.now(),...i&&{blockHash:i},...c&&{gasUsed:c}};if(this.logger.debug(`📡 Mapped status for ${e}: ${o} -> ${l.status}`),this.logger.debug(`📡 Final message: "${s}"`),t(l),l.status===exports.SDKTransactionStatus.COMPLETED||l.status===exports.SDKTransactionStatus.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} (${l.status})`)}});else{const a={transactionId:e,status:exports.SDKTransactionStatus.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===exports.SDKTransactionStatus.COMPLETED?t(e):e.status!==exports.SDKTransactionStatus.FAILED&&e.status!==exports.SDKTransactionStatus.TIMEOUT||a(new Error(`Transaction ${e.status}: ${e.message}`))})})}mapSocketStatus(e){const t=e?.toUpperCase();return cn[t]||exports.SDKTransactionStatus.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 un{constructor(e,t=!1){this.poolService=e,this.cache=new Map,this.logger=new d({debug:t,context:"TokenResolverService"})}async resolveTokenToVault(e){if(!k(e))throw new f("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 Dt(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 _t("vaultAddress","format: service|Token$Unit$...$launchpad","Vault address");const a=t[1].split("$");if(a.length<4)throw _t("vaultAddress","at least 4 parts after service|","Vault address");return{collection:a[0],category:a[1],type:a[2],additionalKey:a[3]}}}class dn{}dn.BASE_PRICE=1650667151e-14,dn.PRICE_SCALING_FACTOR=1166069e-12,dn.TRADING_FEE_FACTOR=.001,dn.GAS_FEE="1",dn.MIN_UNBONDING_FEE_FACTOR=0,dn.MAX_UNBONDING_FEE_FACTOR=.5,dn.NET_UNBONDING_FEE_FACTOR=.5,dn.DEFAULT_LAUNCHPAD_TOKEN_MAX_SUPPLY=1e7;class hn{constructor(e=!1){this.cache=new Map,this.logger=new d({debug:e,context:"TokenMetadataCache"})}getLRUKey(){const e=this.cache.keys().next().value;return void 0!==e?e:null}normalizeTokenName(e){return e.trim().toLowerCase().replace(/\s+/g," ").replace(/[\u0000-\u001F\u007F-\u009F\u200B-\u200D\uFEFF]/g,"")}updateCacheEntry(e,t){const a=this.cache.get(e);if(this.cache.has(e)&&this.cache.delete(e),this.cache.size>=hn.MAX_CACHE_SIZE){const e=this.getLRUKey();null!==e&&this.cache.delete(e)}this.cache.set(e,{...a||{},...t,lastUpdated:Date.now()})}warmFromPoolData(e,t){const a=this.normalizeTokenName(e);this.updateCacheEntry(a,t)}set(e,t){const a=this.normalizeTokenName(e);this.updateCacheEntry(a,t)}get(e){const t=this.normalizeTokenName(e);return this.cache.get(t)||null}getMaxSupply(e){const t=this.normalizeTokenName(e),a=this.cache.get(t);return a?.maxSupply||dn.DEFAULT_LAUNCHPAD_TOKEN_MAX_SUPPLY.toString()}has(e){const t=this.normalizeTokenName(e);return this.cache.has(t)}clear(e){if(e){const t=this.normalizeTokenName(e);this.cache.delete(t)}else this.cache.clear()}dump(){const e={};return this.cache.forEach((t,a)=>{e[a]=t}),e}stats(){let e=Date.now(),t=0;return this.cache.forEach((a,n)=>{a.lastUpdated<e&&(e=a.lastUpdated);let r=0;r+=2*n.length,void 0!==a.reverseBondingCurveMinFeeFactor&&(r+=8),void 0!==a.reverseBondingCurveMaxFeeFactor&&(r+=8),void 0!==a.reverseBondingCurveNetFeeFactor&&(r+=8),r+=8,a.vaultAddress&&(r+=2*a.vaultAddress.length),a.maxSupply&&(r+=2*a.maxSupply.length),r+=32,t+=r}),{totalTokens:this.cache.size,cacheSize:t,oldestEntry:this.cache.size>0?e:0}}}function pn(e){const t=function(e){const t=rt(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")}`)}hn.MAX_CACHE_SIZE=1e4;const gn="/api/asset/launchpad-contract/CallNativeTokenIn",mn="/api/asset/launchpad-contract/CallNativeTokenOut",fn="/api/asset/launchpad-contract/CallMemeTokenIn",yn="/api/asset/launchpad-contract/CallMemeTokenOut";class vn extends r.ChainCallDTO{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 An(e){if(!e||"object"!=typeof e)return!1;const t=e;return"number"==typeof t.Status&&void 0!==t.Data&&"object"==typeof t.Data&&null!==t.Data&&"string"==typeof t.Data.calculatedQuantity&&void 0!==t.Data.extraFees&&"object"==typeof t.Data.extraFees&&null!==t.Data.extraFees&&"string"==typeof t.Data.extraFees.reverseBondingCurve&&"string"==typeof t.Data.extraFees.transactionFees}const wn={NATIVE:"native",EXACT:"exact"},Tn={LOCAL:"local",EXTERNAL:"external"};class kn{static calculateBuyWithExact(e,t){const a=parseFloat(e),n=parseFloat(t),{BASE_PRICE:r,PRICE_SCALING_FACTOR:o,TRADING_FEE_FACTOR:i,GAS_FEE:c}=dn,l=this.roundUp(r*(Math.exp((n+a)*o)-Math.exp(n*o))/o,8),u=new s(l).multipliedBy(i).toFixed();return{amount:l.toString(),reverseBondingCurveFee:"0",transactionFee:u,gasFee:c}}static calculateBuyWithNative(e,t){const a=parseFloat(e),n=parseFloat(t),{BASE_PRICE:r,PRICE_SCALING_FACTOR:o,TRADING_FEE_FACTOR:i,GAS_FEE:c}=dn,l=Math.log(a*o/r+Math.exp(n*o))/o-n,u=new s(l).multipliedBy(i).toFixed();return{amount:l.toString(),reverseBondingCurveFee:"0",transactionFee:u,gasFee:c}}static calculateSellWithExact(e,t,a,n,r){const o=parseFloat(e),i=parseFloat(t),c=parseFloat(a),{BASE_PRICE:l,PRICE_SCALING_FACTOR:u,TRADING_FEE_FACTOR:d,GAS_FEE:h}=dn,p=l*(Math.exp(i*u)-Math.exp((i-o)*u))/u,g=new s(p),m=n+i/c*(r-n),f=g.multipliedBy(m).toFixed(8,s.ROUND_UP),y=g.multipliedBy(d).toFixed();return{amount:p.toString(),reverseBondingCurveFee:f,transactionFee:y,gasFee:h}}static calculateSellWithNative(e,t,a,n,r){const o=parseFloat(e),i=parseFloat(t),c=parseFloat(a),{BASE_PRICE:l,PRICE_SCALING_FACTOR:u,TRADING_FEE_FACTOR:d,GAS_FEE:h}=dn,p=i-Math.log(Math.exp(i*u)-o*u/l)/u,g=new s(o),m=n+i/c*(r-n),f=g.multipliedBy(m).toFixed(8,s.ROUND_UP),y=g.multipliedBy(d).toFixed();return{amount:p.toString(),reverseBondingCurveFee:f,transactionFee:y,gasFee:h}}static roundUp(e,t){const a=Math.pow(10,t);return Math.ceil(e*a)/a}}class Nn{constructor(e,t,a,n,r,o,s="local"){this.http=e,this.tokenResolver=t,this.logger=a,this.bundleHttp=n,this.galaChainHttp=r,this.dexApiHttp=o,this.defaultCalculateAmountMode=s,this.metadataCache=new hn}addIfDefined(e,t,a){return void 0!==a&&(e[t]=a),e}async uploadImageByTokenName(e){const{tokenName:t,options:a}=e;Nt(t);const n=`${t}.png`;Aa(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 _t("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 Lt(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 Ot("File upload failed: FormData not supported in this environment. Ensure you have proper polyfills for Node.js environments.","FormData");throw e}}async fetchPoolsFromAPI(e){Et(e),e.tokenName&&Nt(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=h(t),n=await this.http.get("/launchpad/fetch-pool",a);if(!0===n.error||200!==n.status||!n.data)throw Lt(n.message||"Failed to fetch pools",n.status);let r=[];const o=(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 o(t).isZero()||!new o(a).isZero();return{...e,reverseBondingCurveMinFeePortion:t,reverseBondingCurveMaxFeePortion:a,hasReverseBondingCurveFee:n,createdAt:e.created_at||e.createdAt||""}});else{const e=n.data.tokens,t=e.reverseBondingCurveMinFeePortion??"0",a=e.reverseBondingCurveMaxFeePortion??"0",s=!new o(t).isZero()||!new o(a).isZero();r=[{...e,reverseBondingCurveMinFeePortion:t,reverseBondingCurveMaxFeePortion:a,hasReverseBondingCurveFee:s,createdAt:e.created_at||e.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 o(t).isZero()||!new o(a).isZero();return{...e,reverseBondingCurveMinFeePortion:t,reverseBondingCurveMaxFeePortion:a,hasReverseBondingCurveFee:n,createdAt:e.created_at||e.createdAt||""}}));const{extractMetadataFromPoolData:s,isValidPoolForCaching:i}=await Promise.resolve().then(function(){return On});r.forEach(e=>{if(!i(e))return void this.logger.debug("Skipping pool with invalid structure for caching",e);const t=s(e,this.logger);t&&this.warmCacheFromPoolData(e.tokenName,t)});const c=n.data.count??n.data.total??0,l=n.data.page??e.page??1,u=n.data.limit??e.limit??10,d=u>0?Math.ceil(c/u):1;return{pools:r,page:l,limit:u,total:c,totalPages:d,hasNext:l<d,hasPrevious:l>1}}async _getAmount(e){if(bt(e),!this.galaChainHttp)throw Ot("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:gn,body:{vaultAddress:a,tokenQuantity:n,IsPreMint:!1}};if("NATIVE"===e&&"OUT"===t)return{endpoint:mn,body:{vaultAddress:a,tokenQuantity:n,IsPreMint:!1}};if("MEME"===e&&"IN"===t)return{endpoint:fn,body:{vaultAddress:a,nativeTokenQuantity:n,IsPreMint:!1}};if("MEME"===e&&"OUT"===t)return{endpoint:yn,body:{vaultAddress:a,nativeTokenQuantity:n,IsPreMint:!1}};throw _t("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(!An(e))throw Lt("Malformed response data from GalaChain gateway");if(1!==e.Status)throw Lt(`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){St(e),e.tokenName&&Nt(e.tokenName);const t=h(e),a=await this.http.get("/launchpad/check-pool",t);if(!0===a.error||200!==a.status)throw Lt(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(!ra(e))throw new f("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(Nt(t),!a||!n||!r)throw new f("Graph options (from, to, resolution) are required","options","MISSING_GRAPH_OPTIONS");const o={tokenName:t,from:a,to:n,resolution:r};It(o);const s=h(o),i=await this.http.get("/launchpad/get-graph-data",s);if(!0===i.error||200!==i.status||!i.data)throw Lt(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 f("Invalid options provided. Expected an options object.","options","INVALID_OPTIONS");const{tokenName:t,amount:a,type:n,currentSupply:r}=e,o=e.mode??this.defaultCalculateAmountMode;if("local"!==o&&"external"!==o)throw new f(`Invalid calculation mode "${o}". Must be "local" or "external".`,"mode","INVALID_CALCULATION_MODE");if(!t||"string"!=typeof t)throw new f("Token name is required and must be a string","tokenName","INVALID_TOKEN_NAME");if(!a||"string"!=typeof a)throw new f("Amount is required and must be a string","amount","INVALID_AMOUNT");if(n!==wn.NATIVE&&n!==wn.EXACT)throw new f('Type must be either "native" or "exact"',"type","INVALID_TYPE");return"external"===o?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 f(`Token "${t}" not found. Please verify the token name is correct.`,"tokenName","TOKEN_NOT_FOUND");return n===wn.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:o,reverseBondingCurveMaxFeeFactor:s,reverseBondingCurveMinFeeFactor:i}=e,c=e.mode??this.defaultCalculateAmountMode;if("local"!==c&&"external"!==c)throw new f(`Invalid calculation mode "${c}". Must be "local" or "external".`,"mode","INVALID_CALCULATION_MODE");if(!t||"string"!=typeof t)throw new f("Token name is required and must be a string","tokenName","INVALID_TOKEN_NAME");if(!a||"string"!=typeof a)throw new f("Amount is required and must be a string","amount","INVALID_AMOUNT");if(n!==wn.EXACT&&n!==wn.NATIVE)throw new f('Type must be either "exact" or "native"',"type","INVALID_TYPE");if("external"===c)return this.calculateSellAmountExternal({tokenName:t,amount:a,type:n});{const e={tokenName:t,amount:a,type:n,...void 0!==r&&{currentSupply:r},...void 0!==o&&{maxSupply:o},...void 0!==s&&{reverseBondingCurveMaxFeeFactor:s},...void 0!==i&&{reverseBondingCurveMinFeeFactor:i}};return this.calculateSellAmountLocal(e)}}async calculateSellAmountExternal(e){const{tokenName:t,amount:a,type:n}=e,r=await this.tokenResolver.resolveTokenToVault(t);if(!r)throw new f(`Token "${t}" not found. Please verify the token name is correct.`,"tokenName","TOKEN_NOT_FOUND");return n===wn.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 f("Amount is required and must be a string","amount","INVALID_AMOUNT");if(n!==wn.NATIVE&&n!==wn.EXACT)throw new f('Type must be either "native" or "exact"',"type","INVALID_TYPE");void 0!==r&&Ct(r,"currentSupply");const o=!r;if(o&&!t)throw new f("Token name is required when currentSupply is not provided","tokenName","MISSING_TOKEN_NAME");t&&Nt(t);let s=r;if(o){s=(await this.fetchPoolDetailsForCalculation(t)).currentSupply}return n===wn.EXACT?kn.calculateBuyWithExact(a,s):kn.calculateBuyWithNative(a,s)}async calculateSellAmountLocal(e){const{tokenName:t,amount:a,type:n,currentSupply:r,maxSupply:o,reverseBondingCurveMaxFeeFactor:s,reverseBondingCurveMinFeeFactor:i}=e;if(!a||"string"!=typeof a)throw new f("Amount is required and must be a string","amount","INVALID_AMOUNT");if(n!==wn.EXACT&&n!==wn.NATIVE)throw new f('Type must be either "exact" or "native"',"type","INVALID_TYPE");void 0!==r&&Ct(r,"currentSupply");const c=!r||!o||void 0===s||void 0===i;if(c&&!t)throw new f("Token name is required when currentSupply, maxSupply, or fee factors are not provided","tokenName","MISSING_TOKEN_NAME");t&&Nt(t);let l=r,u=o,d=s,h=i;if(c&&t){const e=this.metadataCache.get(t);u=u??this.metadataCache.getMaxSupply(t),d=d??e?.reverseBondingCurveMaxFeeFactor,h=h??e?.reverseBondingCurveMinFeeFactor,l||(l=await this.fetchCurrentSupply(t));if(void 0===d||void 0===h){const e=await this.fetchPoolDetailsForCalculation(t);d=d??e.reverseBondingCurveMaxFeeFactor,h=h??e.reverseBondingCurveMinFeeFactor}}return n===wn.EXACT?kn.calculateSellWithExact(a,l,u,h,d):kn.calculateSellWithNative(a,l,u,h,d)}async calculateBuyAmountForGraduation(e){const t="string"==typeof e?{tokenName:e}:e;if("object"==typeof e&&!function(e){if(!e||"object"!=typeof e)return!1;const t=e;return ea(t,"tokenName")&&na(t)&&ta(t,"currentSupply")}(e))throw new Error("Invalid CalculateBuyAmountForGraduationOptions provided");const{tokenName:a,calculateAmountMode:n,currentSupply:r}=t;Nt(a);const o=await this.tokenResolver.resolveTokenToVault(a);if(!o)throw new f(m(a),"tokenName","VAULT_NOT_FOUND");if(!this.galaChainHttp)throw Ot("GalaChain HTTP client not configured");const s=await this.galaChainHttp.post("/api/asset/launchpad-contract/FetchSaleDetails",{vaultAddress:o});if(1!==s.Status)throw Lt(`Failed to fetch pool details: Status ${s.Status}`,s.Status);const i=s.Data,c=(await import("bignumber.js")).default,l=r??new c(i.maxSupply).minus(i.sellingTokenQuantity).toFixed(),u=i.sellingTokenQuantity;if("0"===u)throw new f(`Token ${a} is already graduated (no tokens remaining in pool)`,"tokenName","ALREADY_GRADUATED");const d={tokenName:a,amount:u,type:"exact",currentSupply:l,...void 0!==n&&{mode:n}};return await this.calculateBuyAmount(d)}async launchToken(e){if(!this.bundleHttp)throw Ot("Bundle backend client not configured. LaunchToken requires bundleHttp client.","bundleHttp");pn(e);const t=e.preBuyQuantity||"0";if(isNaN(Number(t))||Number(t)<0)throw new f("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 f("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 Lt("Image upload failed: No URL returned");a=t}else"string"==typeof e.tokenImage&&(a=e.tokenImage);const n=`galaswap - operation - ${on()}-${Date.now()}-${this.http.getAddress()}`,o={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&&(o.reverseBondingCurveConfiguration={minFeePortion:e.reverseBondingCurveConfiguration.minFeePortion.toString(),maxFeePortion:e.reverseBondingCurveConfiguration.maxFeePortion.toString()});const s=new vn(o),i=await this.http.signWithGalaChain("CreateSale",s,r.SigningType.SIGN_TYPED_DATA),{signature:c,types:l,domain:u,prefix:d}=i,h={tokenName:s.tokenName,tokenSymbol:s.tokenSymbol,tokenDescription:s.tokenDescription,tokenImage:s.tokenImage,preBuyQuantity:s.preBuyQuantity,websiteUrl:s.websiteUrl,telegramUrl:s.telegramUrl,twitterUrl:s.twitterUrl,tokenCategory:s.tokenCategory,tokenCollection:s.tokenCollection,uniqueKey:s.uniqueKey,signature:c,types:l,domain:u,...d&&{prefix:d},...s.reverseBondingCurveConfiguration&&{reverseBondingCurveConfiguration:s.reverseBondingCurveConfiguration}},p=`${e.tokenName.trim()}$Unit$none$none`,g="GALA$Unit$none$none";let m;if(parseFloat(t)>0){const e=`$service$${p}$launchpad`;m=[e,`$token$${p}$${e}`,`$tokenBalance$${p}$${e}`,`$tokenBalance$${p}$${e}`,`$tokenBalance$${g}$${e}`,`$tokenBalance$${g}$${e}`]}else{const e=`$service$${p}$launchpad`;m=[e,`$token$${p}$${e}`,`$tokenBalance$${p}$${e}`]}const y={signedDto:h,stringsInstructions:m,method:"CreateSale"},v=await this.bundleHttp.post("/bundle",y);if(v.error||!v.data)throw Lt(v.message||"Token launch failed");return v.data}async fetchTokenDistribution(e){if(!e)throw Pt("tokenName","Token name");Nt(e);const t=await this.tokenResolver.resolveTokenToVault(e);if(!t)throw new f(m(e),"tokenName","VAULT_NOT_FOUND");this.metadataCache.set(e,{vaultAddress:t});const a=encodeURIComponent(t),n=await this.http.get(`/holders/${a}`);if(!0===n.error||200!==n.status||!n.data)throw Lt(n.message||"Failed to fetch token distribution",n.status);const r=n.data;if(!Array.isArray(r))throw Lt("Invalid API response: expected array of holders",n.status);for(const e of r){if(!e.owner||"string"!=typeof e.owner)throw Lt("Invalid holder data: missing or invalid owner field",n.status);if(!e.quantity||"string"!=typeof e.quantity)throw Lt("Invalid holder data: missing or invalid quantity field",n.status);const t=parseFloat(e.quantity);if(isNaN(t)||!isFinite(t))throw Lt(`Invalid holder quantity: "${e.quantity}"`,n.status)}const o=r.reduce((e,t)=>e.plus(t.quantity),new s(0));return{holders:r.map(e=>{const t=new s(e.quantity),a=o.isZero()?0:t.dividedBy(o).multipliedBy(100).toNumber();return{address:e.owner,balance:e.quantity,percentage:a}}),totalSupply:o.toFixed(),totalHolders:r.length,lastUpdated:new Date}}async fetchTokenBadges(e){if(!e)throw Pt("tokenName","Token name");Nt(e);const t=await this.http.get("/launchpad/get-badge/",{tokenName:e});if(t.error||!t.data)throw Lt(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(!function(e){if(!e||"object"!=typeof e)return!1;const t=e;return"string"==typeof t.nativeTokenQuantity&&(void 0===t.vaultAddress||"string"==typeof t.vaultAddress)}(e))throw new f("Invalid pre-mint calculation data","data","INVALID_PRE_MINT_DATA");if(!this.galaChainHttp)throw Ot("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(!An(a))throw Lt("Malformed response data from GalaChain gateway");if(1!==a.Status)throw Lt(`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 f(m(e),"tokenName","VAULT_NOT_FOUND");if(!this.galaChainHttp)throw Ot("GalaChain HTTP client not configured");const a=await this.galaChainHttp.post("/api/asset/launchpad-contract/FetchSaleDetails",{vaultAddress:t});if(1!==a.Status)throw Lt(`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(),o=n.sellingTokenQuantity,s=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)`);const l=i-c;return this.metadataCache.set(e,{maxSupply:s,reverseBondingCurveMaxFeeFactor:i,reverseBondingCurveMinFeeFactor:c,reverseBondingCurveNetFeeFactor:l}),{currentSupply:r,remainingTokens:o,maxSupply:s,reverseBondingCurveMaxFeeFactor:i,reverseBondingCurveMinFeeFactor:c,reverseBondingCurveNetFeeFactor:l}}async fetchCurrentSupply(e){Nt(e);const t=await this.tokenResolver.resolveTokenToVault(e);if(!t)throw new f(m(e),"tokenName","VAULT_NOT_FOUND");if(!this.galaChainHttp)throw Ot("GalaChain HTTP client not configured");const a=await this.galaChainHttp.post("/api/asset/launchpad-contract/FetchSaleDetails",{vaultAddress:t});if(1!==a.Status)throw Lt(`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(),o=n.maxSupply;return this.metadataCache.set(e,{maxSupply:o}),r}getAddress(){return this.http.getAddress()}formatAddressForBackend(e){return Ft(e)}validateTokenName(e){return Nt(e)}validatePagination(e){return Et(e)}async fetchTokenSpotPrice(e){if(!this.dexApiHttp)throw Ot("DEX API client not configured. Token price fetching requires dexApiHttp client.","dexApiHttp");if(!e||Array.isArray(e)&&0===e.length)throw Pt("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 Lt(`Failed to fetch token prices: ${e instanceof Error?e.message:e}`,void 0,e instanceof Error?e:void 0)}}async fetchLaunchpadTokenSpotPrice(e){const t="string"==typeof e?{tokenName:e}:e;if("object"==typeof e&&!function(e){if(!e||"object"!=typeof e)return!1;const t=e;return ea(t,"tokenName")&&na(t)&&ta(t,"currentSupply")}(e))throw new Error("Invalid FetchLaunchpadTokenSpotPriceOptions provided");const{tokenName:a,calculateAmountMode:n,currentSupply:r}=t;if(!a||"string"!=typeof a)throw Pt("tokenName","Token name (string)");try{const e={tokenName:a,amount:"1",type:"native",...void 0!==n&&{mode:n},...void 0!==r&&{currentSupply:r}},t=await this.calculateBuyAmount(e),o=(await this.fetchTokenSpotPrice("GALA"))[0];if(!o)throw Lt("GALA price not available");const s=Number(t.amount);if(s<=0)throw new f(`Invalid token amount calculation: ${s}`,"amount","INVALID_CALCULATION");const i=o.price/s;return{symbol:a.toUpperCase(),price:i}}catch(e){if(e instanceof Error)throw new Error(`Failed to calculate launchpad token spot price for ${a}: ${e.message}`);throw new Error(`Failed to calculate launchpad token spot price for ${a}: ${String(e)}`)}}warmCacheFromPoolData(e,t){this.metadataCache.warmFromPoolData(e,t)}getCacheStats(){return this.metadataCache.stats()}clearCache(e){this.metadataCache.clear(e)}}const En={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-test1.defi.gala.com"}};function Sn(e){return En[e]}class bn extends Error{constructor(e,t){super(e),this.cause=t,this.name="WebSocketError"}}class In 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 Fn(e,t){if(!e)throw new bn(`Invalid WebSocket response received for transaction ${t}: response is null or undefined`);if("object"!=typeof e)throw new bn(`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 bn(`Invalid WebSocket response received for transaction ${t}: missing status field`)}function Cn(e,t,a,n){Fn(e,t);const r=e,o=r.data||{};if(!function(e){if(!e||"object"!=typeof e)return!1;const t=e;return!(void 0!==t.inputQuantity&&"string"!=typeof t.inputQuantity||void 0!==t.outputQuantity&&"string"!=typeof t.outputQuantity||void 0!==t.totalFees&&"string"!=typeof t.totalFees||void 0!==t.vaultAddress&&"string"!=typeof t.vaultAddress)}(o))throw new bn(`Invalid trade data received for transaction ${t}`);const s={transactionId:t,type:a,method:"native"===n.type?"native":"exact",inputAmount:o.inputQuantity||n.amount,outputAmount:o.outputQuantity||n.expectedAmount||"0",totalFees:o.totalFees||"0",tokenName:n.tokenName,vaultAddress:o.vaultAddress||"",timestamp:Date.now()};return void 0!==r.blockHash&&(s.blockHash=r.blockHash),void 0!==r.gasUsed&&(s.gasUsed=r.gasUsed),void 0!==n.slippageToleranceFactor&&(s.slippageTolerance=n.slippageToleranceFactor),s}let xn="";const Dn=function(){if(xn)return xn;try{const e=require("../../package.json");xn=e.version||"unknown"}catch{xn="unknown"}return xn}();class Pn{constructor(e){let t=null;t=e.env?Sn(e.env):e.baseUrl?.includes("prod")?Sn("PROD"):Sn("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 d({debug:this.config.debug??!1,context:"LaunchpadSDK"}),this.validateConfiguration(),this.slippageToleranceFactor=void 0===e.slippageToleranceFactor?Pn.DEFAULT_SLIPPAGE_TOLERANCE_FACTOR:this.parseSlippageToleranceFactor(e.slippageToleranceFactor),this.maxAcceptableReverseBondingCurveFeeSlippageFactor=void 0===e.maxAcceptableReverseBondingCurveFeeSlippageFactor?Pn.DEFAULT_MAX_ACCEPTABLE_REVERSE_BONDING_CURVE_FEE_SLIPPAGE_FACTOR:this.parseFeeSlippageFactor(e.maxAcceptableReverseBondingCurveFeeSlippageFactor),this.calculateAmountMode=e.calculateAmountMode||Pn.DEFAULT_CALCULATE_AMOUNT_MODE,this.auth=new u({wallet:e.wallet,messagePrefix:"Create a GalaChain Wallet"}),this.http=new p(this.auth,this.config),this.galaChainHttp=new p(this.auth,{...this.config,baseUrl:this.config.galaChainBaseUrl}),this.bundleHttp=new p(this.auth,{...this.config,baseUrl:this.config.bundleBaseUrl}),this.dexApiHttp=new p(this.auth,{...this.config,baseUrl:this.config.dexApiBaseUrl}),this.launchpadService=new ka(this.http),this.tokenResolverService=new un(this.launchpadService.poolService),this.launchpadAPI=new Nn(this.http,this.tokenResolverService,this.logger,this.bundleHttp,this.galaChainHttp,this.dexApiHttp,this.calculateAmountMode),this.galaChainService=new $a(this.galaChainHttp,e.wallet,this.tokenResolverService,e.debug||!1),this.dexService=new Ba(this.dexApiHttp),this.bundleService=new sn(this.bundleHttp,this.tokenResolverService,this.config.debug||!1,e.wallet,this.getAddress(),this.slippageToleranceFactor,this.maxAcceptableReverseBondingCurveFeeSlippageFactor),this.websocketService=new ln({url:this.config.webSocketUrl},this.config.debug)}createOverrideSdk(e){if(!e||"string"!=typeof e)throw Ot("Invalid privateKey: must be a non-empty string","privateKey");if(!e.match(/^0x[a-fA-F0-9]{64}$/))throw Ot('Invalid privateKey format: must be "0x" followed by 64 hexadecimal characters',"privateKey");const t=new a.Wallet(e),n={...this.config,wallet:t};return new Pn(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}}getVersion(){return Dn}getUrlByTokenName(e){const t=this.config.launchpadFrontendUrl;if(!t)throw Ot("launchpadFrontendUrl not configured in SDK","launchpadFrontendUrl");return`${t.replace(/\/$/,"")}/buy-sell/${e}`}async fetchPools(e){const t=await this.launchpadService.fetchPools(e||{});return await this.warmCacheFromPools(t.pools),t}async fetchAllPools(e){const t=await this.launchpadService.fetchAllPools(e);return await this.warmCacheFromPools(t.pools),t}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 Lt("Failed to fetch GALA price - no price data returned");const t=e.find(e=>"GALA"===e.symbol);if(!t)throw Lt("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(m(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.tokenName=e,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 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 xa}),n=t(e.tokenId),{collection:r,category:o,type:s,additionalKey:i}=n;return this.galaChainService.fetchTokenBalance({owner:a,collection:r,category:o,additionalKey:i,type:s,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 Pt("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,calculateAmountMode:o,currentSupply:s}=e;let i=t;void 0===o&&void 0===s||(i={tokenName:t,...void 0!==o&&{calculateAmountMode:o},...void 0!==s&&{currentSupply:s}});const c=await this.calculateBuyAmountForGraduation(i),l={tokenName:t,amount:c.amount,type:"exact",expectedAmount:c.amount,maxAcceptableReverseBondingCurveFee:c.reverseBondingCurveFee,slippageToleranceFactor:this.slippageToleranceFactor};return void 0!==a&&(l.slippageToleranceFactor=a),void 0!==n&&(l.maxAcceptableReverseBondingCurveFeeSlippageFactor=n),void 0!==r&&(l.privateKey=r),await this.buy(l)}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)).data,a=t?.transactionId;if(!a)throw Ut("No transaction ID returned from buy operation");return this.waitForConfirmation(a,t=>Cn(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)).data,a=t?.transactionId;if(!a)throw Ut("No transaction ID returned from sell operation");return this.waitForConfirmation(a,t=>Cn(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=>{Fn(a,t);const n=a?.data||{};if(!function(e){if(!e||"object"!=typeof e)return!1;const t=e;return!(void 0!==t.vaultAddress&&"string"!=typeof t.vaultAddress||void 0!==t.tokenStringKey&&"string"!=typeof t.tokenStringKey||void 0!==t.creatorAddress&&"string"!=typeof t.creatorAddress)}(n))throw new bn(`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)}getCacheInfo(){return this.launchpadAPI.getCacheStats()}clearCache(e){this.launchpadAPI.clearCache(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 Ot("baseUrl is required in configuration","baseUrl");if(!this.config.webSocketUrl)throw Ot("webSocketUrl is required in configuration","webSocketUrl");try{new URL(this.config.baseUrl)}catch{throw Ot(`Invalid baseUrl format: ${this.config.baseUrl}`,"baseUrl")}try{new URL(this.config.webSocketUrl)}catch{throw Ot(`Invalid webSocketUrl format: ${this.config.webSocketUrl}`,"webSocketUrl")}if(this.config.galaChainBaseUrl)try{new URL(this.config.galaChainBaseUrl)}catch{throw Ot(`Invalid galaChainBaseUrl format: ${this.config.galaChainBaseUrl}`,"galaChainBaseUrl")}if(this.config.bundleBaseUrl)try{new URL(this.config.bundleBaseUrl)}catch{throw Ot(`Invalid bundleBaseUrl format: ${this.config.bundleBaseUrl}`,"bundleBaseUrl")}if(this.config.launchpadFrontendUrl)try{new URL(this.config.launchpadFrontendUrl)}catch{throw Ot(`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: ${Pn.DEFAULT_SLIPPAGE_TOLERANCE_FACTOR}`),Pn.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: ${Pn.DEFAULT_MAX_ACCEPTABLE_REVERSE_BONDING_CURVE_FEE_SLIPPAGE_FACTOR}`),Pn.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 In(e,a.status,a.message);let n;try{n=t(a)}catch(t){if(t instanceof bn)throw t;throw new bn(`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 In||t instanceof bn)throw t;throw new bn(`WebSocket confirmation failed for transaction ${e}`,t instanceof Error?t:new Error(String(t)))}}async warmCacheFromPools(e){if(!e||!Array.isArray(e))return;const{extractMetadataFromPoolData:t,isValidPoolForCaching:a}=await Promise.resolve().then(function(){return On});e.forEach(e=>{if(!a(e))return;const n=t(e,this.logger);n&&this.launchpadAPI.warmCacheFromPoolData(e.tokenName,n)})}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 d({debug:e,context:"LaunchpadSDK"});t.debug("Starting global cleanup...");const{WebSocketService:a}=require("./services/WebSocketService");a.cleanupAll(e),t.debug("Global cleanup completed")}}Pn.DEFAULT_SLIPPAGE_TOLERANCE_FACTOR=.15,Pn.DEFAULT_MAX_ACCEPTABLE_REVERSE_BONDING_CURVE_FEE_SLIPPAGE_FACTOR=.01,Pn.DEFAULT_LAUNCHPAD_TOKEN_MAX_SUPPLY=dn.DEFAULT_LAUNCHPAD_TOKEN_MAX_SUPPLY,Pn.DEFAULT_CALCULATE_AMOUNT_MODE=Tn.LOCAL;class _n{static generateWallet(){try{const e=a.Wallet.createRandom();if(!e.mnemonic?.phrase)throw new Error("Failed to generate wallet with mnemonic phrase");const t=this.toGalaAddress(e.address);return{privateKey:e.privateKey,address:e.address,galaAddress:t,mnemonic:e.mnemonic.phrase,wallet:new a.Wallet(e.privateKey)}}catch(e){if("undefined"!=typeof process&&"test"===process.env.NODE_ENV){const e=`test-wallet-${Date.now()}-${++this.testCounter}`,t="0x"+Buffer.from(e).toString("hex").padStart(64,"1").slice(0,64),n=new a.Wallet(t),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 t=new a.Wallet(e),n=this.toGalaAddress(t.address);return{privateKey:t.privateKey,address:t.address,galaAddress:n,mnemonic:"",wallet:t}}static fromMnemonic(e,t=0){try{const n=a.Mnemonic.fromPhrase(e),r=a.HDNodeWallet.fromMnemonic(n,`m/44'/60'/0'/0/${t}`),o=new a.Wallet(r.privateKey),s=this.toGalaAddress(o.address);return{privateKey:o.privateKey,address:o.address,galaAddress:s,mnemonic:e,wallet:o}}catch(n){if("undefined"!=typeof process&&"test"===process.env.NODE_ENV){const n=`test-mnemonic-index-${t}-${e}`,r="0x"+Buffer.from(n).toString("hex").padStart(64,"1").slice(0,64),o=new a.Wallet(r),s=this.toGalaAddress(o.address);return{privateKey:o.privateKey,address:o.address,galaAddress:s,mnemonic:e,wallet:o}}throw n}}static toGalaAddress(e){const t=e.replace(/^0x/i,"");if(!/^[a-fA-F0-9]{40}$/.test(t))throw new Error(`Invalid Ethereum address format: ${e}`);return`eth|${t}`}static toEthereumAddress(e){if(!e.startsWith("eth|"))throw new Error(`Invalid Gala address format: ${e}. Must start with 'eth|'`);const t=e.slice(4);if(!/^[a-fA-F0-9]{40}$/.test(t))throw new Error(`Invalid address in Gala format: ${e}`);return`0x${t}`}static isValidEthereumAddress(e){try{const t=e.replace(/^0x/i,"");return/^[a-fA-F0-9]{40}$/.test(t)}catch{return!1}}static isValidGalaAddress(e){try{if(!e.startsWith("eth|"))return!1;const t=e.slice(4);return/^[a-fA-F0-9]{40}$/.test(t)}catch{return!1}}static generateMultipleWallets(e=1){if(e<1||e>100)throw new Error("Count must be between 1 and 100");const t=[];if("undefined"!=typeof process&&"test"===process.env.NODE_ENV)for(let 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 Ln(e){if(void 0===e)return _n.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 _n.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 _n.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?"...":""}"`)}_n.testCounter=0;var On=Object.freeze({__proto__:null,extractMetadataFromPoolData:function(e,t){const a={};if(e.vaultAddress&&(a.vaultAddress=e.vaultAddress),void 0!==e.reverseBondingCurveMinFeePortion){const n=parseFloat(e.reverseBondingCurveMinFeePortion);isNaN(n)?t&&t.debug(`Skipping invalid reverseBondingCurveMinFeePortion for ${e.tokenName}: "${e.reverseBondingCurveMinFeePortion}"`):a.reverseBondingCurveMinFeeFactor=n}if(void 0!==e.reverseBondingCurveMaxFeePortion){const n=parseFloat(e.reverseBondingCurveMaxFeePortion);isNaN(n)?t&&t.debug(`Skipping invalid reverseBondingCurveMaxFeePortion for ${e.tokenName}: "${e.reverseBondingCurveMaxFeePortion}"`):a.reverseBondingCurveMaxFeeFactor=n}return void 0!==a.reverseBondingCurveMaxFeeFactor&&void 0!==a.reverseBondingCurveMinFeeFactor&&(a.reverseBondingCurveNetFeeFactor=a.reverseBondingCurveMaxFeeFactor-a.reverseBondingCurveMinFeeFactor),Object.keys(a).length>0?a:null},isValidPoolForCaching:function(e){if(null===e||"object"!=typeof e)return!1;const t=e;return"tokenName"in t&&"string"==typeof t.tokenName&&t.tokenName.length>0}});exports.AgentConfig=class{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 Pn(n),o={sdk:r,wallet:a,config:n};if(!1!==e.autoValidate){const e=await this.validateSetup(r,a);return{...o,validation:e}}return o}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||_n.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?_n.fromPrivateKey(e):_n.generateWallet()}return"generate"===e?_n.generateWallet():_n.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}},exports.CALCULATION_MODES=Tn,exports.FileValidationError=va,exports.GALA_DECIMALS=8,exports.GALA_TOKEN_CLASS_KEY={collection:"GALA",category:"Unit",type:"none",additionalKey:"none"},exports.IMAGE_EXTENSIONS=he,exports.LAUNCHPAD_TOKEN_DECIMALS=18,exports.LaunchpadSDK=Pn,exports.MAX_CONCURRENT_POOL_FETCHES=5,exports.POOL_TYPES={RECENT:"recent",POPULAR:"popular"},exports.SDK_VERSION=Dn,exports.TRADING_TYPES=wn,exports.TransactionFailedError=In,exports.ValidationError=f,exports.WebSocketError=bn,exports.WebSocketTimeoutError=class extends bn{constructor(e,t){super(`WebSocket confirmation timeout for transaction ${e} after ${t}ms`),this.name="WebSocketTimeoutError"}},exports.addressFormatSchema=D,exports.amountMethodSchema=le,exports.amountTypeSchema=ce,exports.browserFileSchema=ge,exports.bufferFileSchema=me,exports.buyTokensDataSchema=$e,exports.calculatePreMintDataSchema=Ve,exports.checkPoolOptionsSchema=ie,exports.commentMessageSchema=H,exports.commentPaginationSchema=ke,exports.createLaunchpadSDK=function(e){const{wallet:t,env:n,config:r={}}=e||{};let o;if(t)if("string"==typeof t){o=Ln(t).wallet}else{if(!(t instanceof a.Wallet))throw new Error("Invalid wallet input. Expected string (private key or mnemonic) or Wallet instance.");o=t}else{o=Ln().wallet}const s={wallet:o,...n&&{env:n},debug:!1,timeout:3e4,...r};return new Pn(s)},exports.createLimitSchema=K,exports.createPaginatedResultSchema=function(e){return o.z.object({data:o.z.array(e),page:o.z.number().int().min(1),limit:o.z.number().int().min(1),total:o.z.number().int().min(0),totalPages:o.z.number().int().min(0),hasNext:o.z.boolean(),hasPrevious:o.z.boolean()})},exports.createTradeDataSchema=Ue,exports.createWallet=Ln,exports.ethereumAddressSchema=P,exports.faucetAmountSchema=$,exports.fetchGalaBalanceOptionsSchema=xe,exports.fetchPoolDetailsDataSchema=qe,exports.fetchTokenBalanceOptionsSchema=_e,exports.fileSizeSchema=q,exports.fileUploadSchema=pe,exports.filenameSchema=j,exports.flexibleAddressSchema=_,exports.flexibleFileSchema=fe,exports.formatGalaForDTO=Ra,exports.formatLaunchpadTokenForDTO=Ka,exports.fullNameSchema=x,exports.getAmountOptionsSchema=ze,exports.getTradeOptionsSchema=Me,exports.graduateTokenOptionsSchema=de,exports.graphDataOptionsSchema=ue,exports.imageExtensionSchema=ye,exports.imageFilenameSchema=ve,exports.imageMimeTypeSchema=W,exports.imageUploadOptionsSchema=oe,exports.isoDateStringSchema=Q,exports.launchTokenDataSchema=re,exports.nonNegativeDecimalStringSchema=U,exports.optionalUrlSchema=M,exports.pageNumberSchema=R,exports.paginationResultMetaSchema=be,exports.poolFetchTypeSchema=se,exports.poolPaginationSchema=Ne,exports.positiveDecimalStringSchema=O,exports.privateKeySchema=Y,exports.reverseBondingCurveConfigSchema=ne,exports.reverseBondingCurveConfigurationSchema=je,exports.searchQuerySchema=C,exports.sellTokensDataSchema=Be,exports.standardLimitSchema=G,exports.standardPaginationSchema=Ae,exports.timestampSchema=X,exports.tokenCategorySchema=te,exports.tokenCollectionSchema=ae,exports.tokenDescriptionSchema=I,exports.tokenListOptionsSchema=Fe,exports.tokenNameSchema=S,exports.tokenSymbolSchema=b,exports.tokenUrlsSchema=ee,exports.tradeCalculationMethodSchema=Ge,exports.tradeCalculationTypeSchema=Ke,exports.tradeLimitSchema=V,exports.tradeListParamsSchema=Re,exports.tradePaginationSchema=Te,exports.tradePaginationWithFiltersSchema=Se,exports.tradeTypeBackendSchema=Oe,exports.tradeTypeSchema=Le,exports.transactionIdSchema=Z,exports.transferFaucetsDataSchema=Ce,exports.uniqueKeySchema=J,exports.updateProfileDataSchema=De,exports.uploadProfileImageOptionsSchema=Pe,exports.urlSchema=B,exports.userLimitSchema=z,exports.userPaginationSchema=we,exports.userTokenNameSchema=F,exports.userTokenTypeSchema=Ie,exports.userTokensPaginationSchema=Ee,exports.validateAddress=Ye,exports.validateAmountString=Je,exports.validateBuyTokensData=mt,exports.validateCalculatePreMintData=wt,exports.validateCheckPoolOptions=it,exports.validateCreateTradeData=gt,exports.validateFaucetAmount=et,exports.validateFetchGalaBalanceOptions=ut,exports.validateFetchPoolDetailsData=Tt,exports.validateFetchTokenBalanceOptions=pt,exports.validateFullName=tt,exports.validateGetAmountOptions=At,exports.validateGetTradeOptions=yt,exports.validateImageUploadOptions=st,exports.validateLaunchTokenData=rt,exports.validateSearchQuery=at,exports.validateSellTokensData=ft,exports.validateTokenDescription=Xe,exports.validateTokenListOptions=ct,exports.validateTokenName=He,exports.validateTokenSymbol=Qe,exports.validateTokenUrls=ot,exports.validateTradeListParams=vt,exports.validateTransferFaucetsData=lt,exports.validateUpdateProfileData=dt,exports.validateUploadProfileImageOptions=ht,exports.validateUserTokenName=nt,exports.validateVaultAddress=Ze,exports.vaultAddressSchema=L;
|
|
1
|
+
"use strict";var e,t,a=require("ethers"),n=require("axios"),r=require("@gala-chain/connect"),o=require("zod"),s=require("bignumber.js"),i=require("@gala-chain/api"),c=require("socket.io-client");!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"}(e||(e={}));class l extends Error{constructor(e,t,a){super(t),this.type=e,this.originalError=a,this.name="AuthError"}}class u{constructor(t){if(this.wallet=t.wallet,this.messagePrefix=t.messagePrefix||"Create a GalaChain Wallet",""===t.messagePrefix)throw new l(e.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(t){throw new l(e.SIGNATURE_FAILED,"Failed to generate signature for authentication",t instanceof Error?t:new Error(String(t)))}}getAddress(){return this.formatAddress(this.wallet.address)}getEthereumAddress(){return this.wallet.address}getPrivateKey(){if(!this.wallet.privateKey)throw new l(e.WALLET_NOT_CONNECTED,"Wallet private key not available for @gala-chain signing");return this.wallet.privateKey}formatAddress(t){const a=t.replace(/^0x/i,"");if(!/^[a-fA-F0-9]{40}$/.test(a))throw new l(e.INVALID_ADDRESS,`Invalid Ethereum address format: ${t}`);return`eth|${a}`}async signMessage(t){try{return{message:t,signature:await this.wallet.signMessage(t),address:this.wallet.address,timestamp:Date.now()}}catch(t){const a=t instanceof Error?t.message:String(t);throw new l(e.SIGNATURE_FAILED,a,t instanceof Error?t:new Error(String(t)))}}async generateAuthHeaders(t,a){try{const e=Date.now(),n=`${this.messagePrefix} ${a.toUpperCase()} ${t} ${e}`,r=await this.wallet.signMessage(n);return{"x-signature":r,"x-address":this.formatAddress(this.wallet.address),"x-message":n,"x-timestamp":e.toString()}}catch(t){throw new l(e.SIGNATURE_FAILED,"Failed to generate authentication headers",t instanceof Error?t:new Error(String(t)))}}async signTypedData(t,a,n){try{return await this.wallet.signTypedData(t,a,n)}catch(t){throw new l(e.SIGNATURE_FAILED,"Failed to sign typed data",t instanceof Error?t:new Error(String(t)))}}async generateCustomSignature(t){if(!t||"string"!=typeof t||0===t.trim().length)throw new l(e.SIGNATURE_FAILED,"Custom message must be a non-empty string");try{const e=await this.wallet.signMessage(t);return{message:t,signature:e,address:this.formatAddress(this.wallet.address),timestamp:Date.now()}}catch(t){throw new l(e.SIGNATURE_FAILED,"Failed to generate custom message signature",t instanceof Error?t:new Error(String(t)))}}validateWallet(){if(!this.wallet)throw new l(e.WALLET_NOT_CONNECTED,"Wallet is required for authentication");if(!this.wallet.address)throw new l(e.WALLET_NOT_CONNECTED,"Wallet address is not available");if(!this.wallet.privateKey&&!this.wallet.signMessage)throw new l(e.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"}(t||(t={}));class d{constructor(e){this.levelPriority={[t.DEBUG]:0,[t.INFO]:1,[t.WARN]:2,[t.ERROR]:3},this.debugEnabled=e.debug,this.context=e.context||"SDK",this.minLevel=e.minLevel||(e.debug?t.DEBUG:t.INFO)}debug(e,a){this.log(t.DEBUG,e,a)}info(e,a){this.log(t.INFO,e,a)}warn(e,a){this.log(t.WARN,e,a)}error(e,a){this.log(t.ERROR,e,a)}log(e,a,n){if(this.levelPriority[e]<this.levelPriority[this.minLevel])return;if(e===t.DEBUG&&!this.debugEnabled)return;const r=`[${(new Date).toISOString()}] [${this.context}] [${e}]`,o=this.getConsoleMethod(e);void 0!==n?n instanceof Error?o(`${r} ${a}`,n.message,n.stack):o(`${r} ${a}`,n):o(`${r} ${a}`)}getConsoleMethod(e){switch(e){case t.DEBUG:return console.debug;case t.INFO:return console.info;case t.WARN:return console.warn;case t.ERROR:return console.error;default:return console.log}}child(e){return new d({debug:this.debugEnabled,context:`${this.context}:${e}`,minLevel:this.minLevel})}isDebugEnabled(){return this.debugEnabled&&this.levelPriority[t.DEBUG]>=this.levelPriority[this.minLevel]}}function h(e){if(!e||"object"!=typeof e)return{};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 p{constructor(e,t={}){this.auth=e,this.debug=t.debug??!1,this.logger=new d({debug:this.debug,context:"HttpClient"}),this.axios=n.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:h(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=r.SigningType.SIGN_TYPED_DATA){const n=this.auth.getPrivateKey(),o=new r.SigningClient(n);return await o.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 g="Token name is required and must be a string",m=e=>`Could not find vault address for token: ${e}`;class f extends Error{constructor(e,t,a){super(e),this.field=t,this.code=a,this.name="ValidationError"}}class y extends Error{constructor(e,t,a){super(e),this.statusCode=t,this.originalError=a,this.name="NetworkError"}}class v extends Error{constructor(e,t){super(e),this.field=t,this.name="ConfigurationError"}}class A extends Error{constructor(e,t,a){super(e),this.transactionId=t,this.code=a,this.name="TransactionError"}}function w(e,t,a){const{MIN_PAGE:n,MAX_PAGE:r,MIN_LIMIT:o,MAX_LIMIT:s}=a.PAGINATION;if("number"!=typeof e||e<n||e>r)throw new f(`Page must be a number between ${n} and ${r}`,"page","INVALID_PAGE");if("number"!=typeof t||t<o||t>s)throw new f(`Limit must be a number between ${o} and ${s}`,"limit","INVALID_LIMIT")}const T={ETH_ADDRESS:/^0x[0-9a-fA-F]{40}$/,BACKEND_ADDRESS:/^eth\|[0-9a-fA-F]{40}$/};function k(e){return"string"==typeof e&&e.trim().length>0}function N(e){return!(!e||"string"!=typeof e)&&(T.ETH_ADDRESS.test(e)||T.BACKEND_ADDRESS.test(e))}function E(e){return e.startsWith("0x")?`eth|${e.slice(2)}`:e}const S=o.z.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"),b=o.z.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"),I=o.z.string().min(1,"Token description is required").max(500,"Token description must be at most 500 characters"),F=o.z.string().min(1,"Token name must be at least 1 character").max(50,"Token name must be at most 50 characters"),C=o.z.string().min(1,"Search query must be at least 1 character").max(100,"Search query must be at most 100 characters"),x=o.z.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"),D=o.z.string().regex(T.BACKEND_ADDRESS,"Address must be in format: eth|[40-hex-chars]"),P=o.z.string().regex(T.ETH_ADDRESS,"Invalid Ethereum address format"),_=o.z.string().refine(e=>T.BACKEND_ADDRESS.test(e)||T.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),L=o.z.string().refine(e=>T.BACKEND_ADDRESS.test(e)||/^service\|Token\$Unit\$[A-Z0-9]+\$eth:[0-9a-fA-F]{40}\$launchpad$/.test(e),"Invalid vault address format"),O=o.z.string().regex(/^\d+(\.\d+)?$/,"Must be a valid decimal number").refine(e=>parseFloat(e)>0,"Amount must be greater than zero"),U=o.z.string().regex(/^\d+(\.\d+)?$/,"Must be a valid decimal number").refine(e=>parseFloat(e)>=0,"Amount must be zero or greater"),$=o.z.string().regex(/^(?!0+(\.0+)?$)\d+(\.\d+)?$/,"Amount must be a positive, non-zero number"),B=o.z.string().url("Must be a valid URL").regex(/^https?:\/\//,"URL must start with http:// or https://"),M=o.z.string().optional().refine(e=>!e||/^https?:\/\/.+\..+/.test(e),"Must be a valid URL if provided"),R=o.z.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 K(e=100){return o.z.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 G=K(100),z=K(20),V=K(20),q=o.z.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=o.z.string().max(255,"Filename must be at most 255 characters"),W=o.z.enum(["image/png","image/jpg","image/jpeg","image/gif","image/webp","image/svg+xml"]),H=o.z.string().min(1,"Comment message is required").max(500,"Comment must be at most 500 characters"),Q=o.z.string().datetime("Must be a valid ISO 8601 date string"),X=o.z.number().int("Timestamp must be an integer").min(0,"Timestamp must be non-negative"),Y=o.z.string().regex(/^0x[a-fA-F0-9]{64}$/,"Private key must be format: 0x + 64 hex characters"),Z=o.z.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"),J=o.z.string().regex(/^galaconnect-operation-[a-z0-9-]+$/,"Unique key must be format: galaconnect-operation-{unique-id}"),ee=o.z.object({websiteUrl:M,telegramUrl:M,twitterUrl:M}).refine(e=>e.websiteUrl||e.telegramUrl||e.twitterUrl,"At least one social URL (website, telegram, or twitter) is required"),te=o.z.string().min(1,"Token category must not be empty").default("Unit"),ae=o.z.string().min(1,"Token collection must not be empty").default("Token"),ne=o.z.object({minFeePortion:O,maxFeePortion:O}),re=o.z.object({tokenName:S,tokenSymbol:b,tokenDescription:I,tokenImage:o.z.union([o.z.instanceof(File),o.z.instanceof(Buffer),o.z.string().url("Token image must be a valid URL")]).optional(),preBuyQuantity:U.default("0"),websiteUrl:M,telegramUrl:M,twitterUrl:M,tokenCategory:te,tokenCollection:ae,reverseBondingCurveConfiguration:ne.optional(),privateKey:Y.optional()}),oe=o.z.object({file:o.z.union([o.z.instanceof(File),o.z.instanceof(Buffer)]),tokenName:S}),se=o.z.enum(["recent","popular"]),ie=o.z.object({tokenName:S.optional(),symbol:b.optional()}).refine(e=>e.tokenName||e.symbol,"At least one of tokenName or symbol is required"),ce=o.z.enum(["NATIVE","MEME"]),le=o.z.enum(["IN","OUT"]),ue=o.z.object({from:o.z.number().int("From timestamp must be an integer").min(173e6,"From timestamp must be at least 173000000"),to:o.z.number().int("To timestamp must be an integer").min(173e6,"To timestamp must be at least 173000000"),resolution:o.z.number().int("Resolution must be an integer").min(1,"Resolution must be at least 1"),tokenName:S}),de=o.z.object({tokenName:S,slippageToleranceFactor:o.z.number().min(0).max(1).optional(),maxAcceptableReverseBondingCurveFeeSlippageFactor:o.z.number().min(0).max(1).optional(),privateKey:Y.optional()}),he=[".png",".jpg",".jpeg",".gif",".webp",".svg"];function pe(){return o.z.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")}const ge=o.z.instanceof(Buffer).refine(e=>e.length>=1&&e.length<=10485760,"Buffer size must be between 1 byte and 10MB");let me=null,fe=null,ye=null;function ve(){return me||"undefined"==typeof File||(me=pe()),me}function Ae(){return fe||(fe="undefined"!=typeof File?o.z.union([pe(),ge]):ge),fe}function we(){return ye||(ye=function(){const e="undefined"!=typeof File?o.z.union([o.z.instanceof(File),o.z.instanceof(Buffer)]):o.z.instanceof(Buffer);return o.z.object({file:e,name:j,size:q,type:W})}()),ye}const Te={safeParse:e=>{const t=ve();return t?t.safeParse(e):{success:!1,error:new Error("File schema not available in Node.js")}},parse:e=>{const t=ve();if(!t)throw new Error("File schema not available in Node.js");return t.parse(e)}},ke={safeParse:e=>Ae().safeParse(e),parse:e=>Ae().parse(e)},Ne={safeParse:e=>we().safeParse(e),parse:e=>we().parse(e)},Ee=o.z.enum([".png",".jpg",".jpeg",".gif",".webp",".svg"]),Se=j.refine(e=>{const t=e.slice(e.lastIndexOf(".")).toLowerCase();return he.includes(t)},`Filename must end with one of: ${he.join(", ")}`),be=o.z.object({page:R,limit:G}),Ie=o.z.object({page:R,limit:z}),Fe=o.z.object({page:R,limit:V}),Ce=o.z.object({page:R,limit:K(50)}),xe=be.extend({type:o.z.enum(["recent","popular"]).optional(),tokenName:o.z.string().min(1).max(50).optional(),search:o.z.string().min(1).max(100).optional()}),De=Ie.extend({tokenName:o.z.string().min(1).max(50).optional(),search:o.z.string().min(1).max(100).optional()}),Pe=Fe.extend({tradeType:o.z.enum(["BUY","SELL"]).optional(),tokenName:o.z.string().min(1).max(50).optional(),userAddress:o.z.string().regex(/^(0x[a-fA-F0-9]{40}|eth\|[a-fA-F0-9]{40})$/).optional(),startDate:o.z.string().datetime().optional(),endDate:o.z.string().datetime().optional(),sortOrder:o.z.enum(["ASC","DESC"]).default("DESC")}),_e=o.z.object({page:o.z.number().int().min(1),limit:o.z.number().int().min(1),total:o.z.number().int().min(0),totalPages:o.z.number().int().min(0),hasNext:o.z.boolean(),hasPrevious:o.z.boolean()});const Le=o.z.enum(["all","DEFI","ASSET"]),Oe=Ie.extend({type:Le.optional(),address:_.optional(),search:C.optional(),tokenName:F.optional()}),Ue=o.z.object({walletAddress:_,amount:$}),$e=o.z.object({address:_.optional(),refresh:o.z.boolean().optional()}),Be=o.z.object({profileImage:o.z.string(),fullName:x,address:_,privateKey:Y.optional()}),Me=o.z.object({file:o.z.union([o.z.instanceof(File),o.z.instanceof(Buffer)]),address:_.optional(),privateKey:Y.optional()}),Re=o.z.object({address:_,tokenId:o.z.union([o.z.string(),o.z.object({collection:o.z.string(),category:o.z.string(),type:o.z.string(),additionalKey:o.z.string()}),o.z.object({collection:o.z.string(),category:o.z.string(),type:o.z.string(),additionalKey:o.z.string(),instance:o.z.string()})]).optional(),tokenClassKey:o.z.object({collection:o.z.string(),category:o.z.string(),type:o.z.string(),additionalKey:o.z.string()}).optional(),tokenName:F.optional()}).refine(e=>void 0!==e.tokenId||void 0!==e.tokenName,"At least one token identifier (tokenId or tokenName) is required"),Ke=o.z.enum(["buy","sell"]),Ge=o.z.enum(["BUY","SELL"]),ze=o.z.object({tradeType:Ke,tokenAmount:O,vaultAddress:L,userAddress:_,slippageTolerance:O.optional(),deadline:o.z.number().int().positive().optional()}),Ve=o.z.object({tokenSymbol:b,nativeTokenQuantity:O,expectedToken:O,maxAcceptableReverseBondingCurveFee:U.default("0").optional()}),qe=o.z.object({tokenSymbol:b,tokenQuantity:O,expectedNativeToken:O,maxAcceptableReverseBondingCurveFee:U.default("0").optional()}),je=Fe.extend({tokenName:F.optional()}),We=o.z.object({page:o.z.number().int().min(1).max(1e3).default(1).optional(),limit:o.z.number().int().min(1).max(20).default(10).optional()}),He=o.z.enum(["NATIVE","MEME"]),Qe=o.z.enum(["IN","OUT"]),Xe=o.z.object({type:He,method:Qe,vaultAddress:L,amount:O}),Ye=o.z.object({nativeTokenQuantity:O}),Ze=o.z.object({vaultAddress:L}),Je=o.z.object({minFeePortion:O,maxFeePortion:O});function et(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 tt=et(S),at=et(b),nt=et(I),rt=et(_),ot=et(L),st=et(O),it=et($),ct=et(x),lt=et(C),ut=et(F),dt=et(re),ht=et(ee),pt=et(oe),gt=et(ie),mt=et(Oe),ft=et(Ue),yt=et($e),vt=et(Be),At=et(Me),wt=et(Re),Tt=et(ze),kt=et(Ve),Nt=et(qe),Et=et(je),St=et(We),bt=et(Xe),It=et(Ye),Ft=et(Ze);function Ct(e,t){throw new f(e.join("; "),t,"VALIDATION_ERROR")}function xt(e){const t=tt(e);!t.success&&t.errors&&Ct(t.errors,"tokenName")}function Dt(e){const t=xe.safeParse(e);t.success||Ct(t.error.errors.map(e=>e.message),"pagination")}function Pt(e){const t=gt(e);!t.success&&t.errors&&Ct(t.errors,"options")}function _t(e){const t=bt(e);!t.success&&t.errors&&Ct(t.errors,"options")}function Lt(e){const t=ue.safeParse(e);t.success||Ct(t.error.errors.map(e=>e.message),"options")}function Ot(e){const t=_.safeParse(e);if(!t.success)throw new f("Ethereum address must be 40 hex characters (with or without 0x prefix)","ethereumAddress","INVALID_FORMAT");return t.data}function Ut(e,t,a=!0){if(!e||""===e.trim())throw new f(`${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 f(`${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 f(`${t} must be a valid numeric string. Received: "${e}"`,t,"INVALID_NUMERIC_STRING");if(!isFinite(n))throw new f(`${t} must be a finite number. Cannot be Infinity or -Infinity.`,t,"INVALID_NUMERIC_STRING");if(n<0)throw new f(`${t} must be non-negative. Received: "${e}"`,t,"INVALID_NUMERIC_STRING");if(!a&&0===n)throw new f(`${t} must be greater than zero. Received: "${e}"`,t,"INVALID_NUMERIC_STRING")}var $t=Object.freeze({__proto__:null,normalizeAddressInput:function(e){if(!e)return;const t=_.safeParse(e);if(!t.success)throw new f(`Invalid address format: ${e}. Must be either "0x..." (Ethereum) or "eth|..." (GalaChain) format`,"address","INVALID_FORMAT");return t.data},toBackendAddressFormat:Ot,validateCheckPoolOptions:Pt,validateGetAmountOptions:_t,validateGetGraphOptions:Lt,validateNumericString:Ut,validatePagination:Dt,validateTokenName:xt});function Bt(e,t){return new f(`Token "${e}" not found. Please verify the token name is correct.`,"tokenName","TOKEN_NOT_FOUND")}function Mt(e,t){const a=t||e.charAt(0).toUpperCase()+e.slice(1);return new f(`${a} is required`,e,"REQUIRED_FIELD")}function Rt(e,t,a){const n=a||e.charAt(0).toUpperCase()+e.slice(1);return new f(`${n} must be ${t}`,e,"INVALID_FORMAT")}function Kt(e,t,a){return new y(e,t,a)}function Gt(e,t){return new v(e,t)}function zt(e,t,a){return new A(e,t,a)}function Vt(e,t){const a=e,n=Number(a.page)||t.page,r=Number(a.limit)||t.limit,o=a.data,s=Number(a.total)||Number(o?.count)||0;return{page:n,limit:r,total:s,totalPages:Math.ceil(s/r)}}function qt(e,t){return{hasNext:e<t,hasPrevious:e>1}}function jt(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 Wt="/launchpad/upload-image",Ht="/launchpad/fetch-pool",Qt="/launchpad/check-pool",Xt="/launchpad/get-graph-data",Yt="/holders",Zt="/launchpad/get-badge/",Jt="/trade/",ea="/token/commment",ta="/token/commment",aa="/user/profile",na="/user/profile",ra="/user/token-list",oa="/user/token-hold",sa="/user/transfer-faucets";function ia(e,t){return"string"==typeof e[t]}function ca(e,t){return void 0===e[t]||"string"==typeof e[t]}function la(e,t){return void 0===e[t]||"number"==typeof e[t]}function ua(e){return void 0===e.calculateAmountMode||"local"===e.calculateAmountMode||"external"===e.calculateAmountMode}function da(e){if(!e||"object"!=typeof e)return!1;const t=e;return ia(t,"tokenName")&&la(t,"from")&&la(t,"to")&&la(t,"resolution")}class ha{constructor(e,t=!1){this.http=e,this.logger=new d({debug:t,context:"PoolService"})}async fetchSinglePage(e){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=h(t),n=await this.http.get(Ht,a);jt(n,"Failed to fetch pools",!0);const r=function(e){if(!e)return[];let t=[];if(e.tokens)if(Array.isArray(e.tokens))t=e.tokens.map(e=>({...e,createdAt:e.created_at||e.createdAt||""}));else{const a=e.tokens;t=[{...a,createdAt:a.created_at||a.createdAt||""}]}else e.pools&&Array.isArray(e.pools)&&(t=e.pools.map(e=>({...e,createdAt:e.created_at||e.createdAt||""})));return t}(n.data),o=n.data.count??n.data.total??0;return{pools:r,total:o,totalPages:e.limit>0?Math.ceil(o/e.limit):1}}async fetchMultiplePages(e){const{startPage:t,totalPages:a,pageSize:n,...r}=e,o=[];if(a&&a>=t){const e=[];for(let n=t;n<=a;n++)e.push(n);for(let t=0;t<e.length;t+=5){const a=e.slice(t,t+5).map(e=>this.fetchSinglePage({...r,page:e,limit:n}));(await Promise.all(a)).forEach(e=>{o.push(...e.pools)})}return o}let s=t,i=!0;for(;i;){const e=[];for(let t=0;t<5&&i;t++)e.push(s+t);const t=e.map(e=>this.fetchSinglePage({...r,page:e,limit:n})),a=await Promise.all(t);for(const e of a){if(0===e.pools.length){i=!1;break}o.push(...e.pools)}s+=e.length,s>100&&(i=!1)}return o}async fetchPools(e={}){const t=e.page||1,a=e.limit??10;let n;if(0!==a&&Dt({page:t,limit:a}),e.tokenName&&xt(e.tokenName),"recent"===e.type?n="RECENT":"popular"===e.type&&(n="POPULAR"),a>0&&a<=20){const r=await this.fetchSinglePage({...e.search&&{search:e.search},...e.tokenName&&{tokenName:e.tokenName},...n&&{type:n},page:t,limit:a});return{pools:r.pools,page:t,limit:a,total:r.total,totalPages:r.totalPages,hasNext:t<r.totalPages,hasPrevious:t>1}}if(0===a){const t=20,a=await this.fetchSinglePage({...e.search&&{search:e.search},...e.tokenName&&{tokenName:e.tokenName},...n&&{type:n},page:1,limit:t}),r=[...a.pools];if(a.pools.length>0){const o=await this.fetchMultiplePages({...e.search&&{search:e.search},...e.tokenName&&{tokenName:e.tokenName},...n&&{type:n},startPage:2,totalPages:a.totalPages>1?a.totalPages:null,pageSize:t});r.push(...o)}return{pools:r,page:1,limit:r.length,total:a.total||r.length,totalPages:1,hasNext:!1,hasPrevious:!1}}const r=Math.ceil(a/20),o=await this.fetchSinglePage({...e.search&&{search:e.search},...e.tokenName&&{tokenName:e.tokenName},...n&&{type:n},page:t,limit:20}),s=[...o.pools],i=Math.min(r,o.totalPages-t+1);if(i>1){const a=t+i-1,r=await this.fetchMultiplePages({...e.search&&{search:e.search},...e.tokenName&&{tokenName:e.tokenName},...n&&{type:n},startPage:t+1,totalPages:a,pageSize:20});s.push(...r)}const c=s.slice(0,a);return{pools:c,page:t,limit:a,total:o.total,totalPages:o.totalPages,hasNext:t<o.totalPages&&c.length<o.total,hasPrevious:t>1}}async fetchAllPools(e){return this.fetchPools({...e,limit:0})}async checkPool(e){Pt(e),e.tokenName&&xt(e.tokenName);const t=h(e),a=await this.http.get(Qt,t);jt(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(!da(e))throw new f("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(xt(t),!a||!n||!r)throw new f("Graph options (from, to, resolution) are required","options","MISSING_GRAPH_OPTIONS");const o={tokenName:t,from:a,to:n,resolution:r};Lt(o);const s=h(o),i=await this.http.get(Xt,s);return jt(i,"Failed to fetch graph data",!0),{dataPoints:i.data}}async fetchTokenDistribution(e){if(!e)throw Mt("tokenName","Token name");xt(e);const t=await this.resolveTokenNameToVault(e);if(!t)throw new f(m(e),"tokenName","VAULT_NOT_FOUND");const a=encodeURIComponent(t),n=await this.http.get(`${Yt}/${a}`);jt(n,"Failed to fetch token distribution",!0);const r=n.data;if(!Array.isArray(r))throw Kt("Invalid API response: expected array of holders",n.status);for(const e of r){if(!e.owner||"string"!=typeof e.owner)throw Kt("Invalid holder data: missing or invalid owner field",n.status);if(!e.quantity||"string"!=typeof e.quantity)throw Kt("Invalid holder data: missing or invalid quantity field",n.status);const t=parseFloat(e.quantity);if(isNaN(t)||!isFinite(t))throw Kt(`Invalid holder quantity: "${e.quantity}"`,n.status)}const o=r.reduce((e,t)=>e.plus(t.quantity),new s(0));return{holders:r.map(e=>{const t=new s(e.quantity),a=o.isZero()?0:t.dividedBy(o).multipliedBy(100).toNumber();return{address:e.owner,balance:e.quantity,percentage:a}}),totalSupply:o.toFixed(),totalHolders:r.length,lastUpdated:new Date}}async fetchTokenBadges(e){if(!e)throw Mt("tokenName","Token name");xt(e);const t=await this.http.get(Zt,{tokenName:e});return jt(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});if(t.pools&&Array.isArray(t.pools)&&t.pools.length>0)return t.pools[0].vaultAddress||null;if(t.pools&&"object"==typeof t.pools){const e=t.pools.tokens;return e?.vaultAddress||null}return null}catch{return null}}}function pa(e,t={}){const{stringifyFields:a=[],optionalFields:n=[],fieldMappings:r={}}=t,o={};for(const[t,s]of Object.entries(e)){const e=t;if(n.includes(e)&&void 0===s)continue;if(n.includes(e)&&"string"==typeof s&&0===s.trim().length)continue;const i=r[e],c=i?String(i):t;a.includes(e)?o[c]=String(s):o[c]=s}return h(o)}const ga={PAGINATION:{MIN_PAGE:1,MAX_PAGE:1e3,MIN_LIMIT:1,MAX_LIMIT:20}};class ma{constructor(e,t=!1){this.http=e,this.logger=new d({debug:t,context:"TradeService"})}async fetchTrades(e){if(!function(e){if(!e||"object"!=typeof e)return!1;const t=e;return ia(t,"tokenName")&&(void 0===t.tradeType||"buy"===t.tradeType||"sell"===t.tradeType)&&ca(t,"userAddress")&&la(t,"page")&&la(t,"limit")}(e))throw new f("Invalid options provided. Expected { tokenName: string, tradeType?: string, userAddress?: string, page?: number, limit?: number, startDate?: Date, endDate?: Date, sortOrder?: string }","options","INVALID_OPTIONS");const{tokenName:t,tradeType:a,userAddress:n,page:r=1,limit:o=10,startDate:s,endDate:i,sortOrder:c}=e;if(!k(t))throw new f("Token name is required and must be a non-empty string","tokenName","INVALID_TOKEN_NAME");w(r,o,ga);const l=function(e,t,a){return pa({tokenName:e,page:t,limit:a},{stringifyFields:["page","limit"]})}(t,r,o),u=await this.http.get(Jt,l),d=(h=u.data)?Array.isArray(h)?h:h.trades:[];var h;const p=Vt(u,{page:r,limit:o}),g=qt(p.page,p.totalPages);return{trades:d,...p,...g}}}const fa=new d({debug:!1,context:"DateUtils"});function ya(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())?(fa.warn(`Invalid date string received: "${e}". Using fallback.`),t||new Date):a}catch(a){return fa.warn(`Date parsing error for "${e}":`,a),t||new Date}}const va={PAGINATION:{MIN_PAGE:1,MAX_PAGE:1e3,MIN_LIMIT:1,MAX_LIMIT:50},CONTENT:{MIN_LENGTH:1,MAX_LENGTH:500}};class Aa{constructor(e,t,a=!1){this.http=e,this.poolService=t,this.logger=new d({debug:a,context:"CommentService"})}async fetchComments(e){if(!function(e){if(!e||"object"!=typeof e)return!1;const t=e;return ia(t,"tokenName")&&la(t,"page")&&la(t,"limit")}(e))throw new f("Invalid options provided. Expected { tokenName: string, page?: number, limit?: number }","options","INVALID_OPTIONS");const{tokenName:t,page:a=1,limit:n=10}=e;if(!k(t))throw new f("Token name is required and must be a non-empty string","tokenName","INVALID_TOKEN_NAME");w(a,n,va);const r=await this.poolService.resolveTokenNameToVault(t);if(!r)throw Bt(t);const o=pa({vaultAddress:r,page:a,limit:n},{stringifyFields:["page","limit"]}),s=await this.http.get(ea,o);jt(s,"Failed to fetch comments");return{comments:s.data.comments.map(e=>({...e,createdAt:ya(e.createdAt)})),total:s.data.count}}async postComment(e){if(!function(e){if(!e||"object"!=typeof e)return!1;const t=e;return ia(t,"tokenName")&&ia(t,"content")}(e))throw new f("Invalid options provided. Expected { tokenName: string, content: string }","options","INVALID_OPTIONS");const{tokenName:t,content:a}=e;if(!k(t))throw new f("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>=va.CONTENT.MIN_LENGTH&&t.length<=va.CONTENT.MAX_LENGTH}(a))throw new f(`Comment content must be between ${va.CONTENT.MIN_LENGTH} and ${va.CONTENT.MAX_LENGTH} characters`,"content","INVALID_CONTENT");const n=await this.poolService.resolveTokenNameToVault(t);if(!n)throw Bt(t);const r={userAddress:this.http.getAddress(),vaultAddress:n,content:a};jt(await this.http.post(ta,r),"Failed to create comment")}}function wa(e,t="image",a){const n=new FormData;if("undefined"!=typeof File&&e instanceof File)n.append(t,e);else{if(!Buffer.isBuffer(e))throw Rt("file","a File object (browser) or Buffer (Node.js)");{const r=new Blob([e],{type:"image/png"});n.append(t,r,a)}}return n}const Ta={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 ka(e){return!(!e||"string"!=typeof e)&&Ta.USER_ADDRESS.PATTERN.test(e)}const Na="Update profile";class Ea{constructor(e){this.http=e}async fetchProfile(e){const t=e??this.http.getAddress();if(!ka(t))throw new f("Address must be in format: eth|[40-hex-chars]","address","INVALID_ADDRESS");const a={userAddress:t};return await this.http.get(aa,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(Na),r={address:n.address,message:Na,publickey:n.ethereumAddress,sign:n.signature};jt(await this.http.put(na,a,r),"Profile update failed")}async uploadProfileImage(e){this.validateUploadProfileImageOptions(e);const t=e.address??this.http.getAddress();try{const a=`profile-image-${t}.png`,n=wa(e.file,"image",a),r=await this.http.request({method:"POST",url:`${Wt}?tokenName=${encodeURIComponent(t)}`,data:n,headers:{}});return jt(r,"Image upload failed"),"string"==typeof r.data?r.data:""}catch(e){if(e instanceof f)throw e;throw new f(`Profile image upload failed: ${e instanceof Error?e.message:"Unknown error"}`,"file","UPLOAD_FAILED")}}async fetchTokenList(e){return this.buildFetchRequest(ra,e,{includeType:!0,errorMessage:"Failed to fetch token list"})}async fetchTokensHeld(e){return this.buildFetchRequest(oa,e,{includeType:!1,errorMessage:"Failed to fetch tokens held"})}async fetchTokensCreated(e={}){const{page:t=1,limit:a=10,search:n,tokenName:r}=e,o={type:"DEFI",address:this.http.getAddress(),page:t,limit:a};return void 0!==n&&(o.search=n),void 0!==r&&(o.tokenName=r),this.fetchTokenList(o)}async buildFetchRequest(e,t,a){this.validateGetTokenListOptions(t);const n={page:t.page,limit:t.limit,address:t.address,search:t.search,tokenName:t.tokenName},r=pa(a.includeType?{...n,type:"all"!==t.type&&t.type?t.type:"DEFI"}:n,{stringifyFields:["page","limit"],optionalFields:["address","search","tokenName"]}),o=await this.http.get(e,r);jt(o,a.errorMessage,!0);const s=(i=o.data)?Array.isArray(i)?i:i.token:[];var i;const c=Vt(o,{page:t.page||1,limit:t.limit||10}),l=qt(c.page,c.totalPages);return{tokens:s,...c,...l}}validateGetTokenListOptions(e){if(w(e.page,e.limit,Ta),void 0!==e.address&&!ka(e.address))throw new f("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>=Ta.SEARCH.MIN_LENGTH&&t.length<=Ta.SEARCH.MAX_LENGTH))throw new f(`Search query must be between ${Ta.SEARCH.MIN_LENGTH} and ${Ta.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>=Ta.TOKEN_NAME.MIN_LENGTH&&a.length<=Ta.TOKEN_NAME.MAX_LENGTH))throw new f(`Token name must be between ${Ta.TOKEN_NAME.MIN_LENGTH} and ${Ta.TOKEN_NAME.MAX_LENGTH} characters`,"tokenName","INVALID_TOKEN_NAME")}validateUpdateProfileData(e){if(!ka(e.address))throw new f("Address must be in format: eth|[40-hex-chars]","address","INVALID_ADDRESS");if(!((t=e.fullName)&&"string"==typeof t&&t.length>=Ta.PROFILE.FULL_NAME.MIN_LENGTH&&t.length<=Ta.PROFILE.FULL_NAME.MAX_LENGTH&&Ta.PROFILE.FULL_NAME.ALPHABETS_ONLY_PATTERN.test(t)))throw new f(`Full name must be between ${Ta.PROFILE.FULL_NAME.MIN_LENGTH} and ${Ta.PROFILE.FULL_NAME.MAX_LENGTH} characters`,"fullName","INVALID_FULL_NAME");var t}validateUploadProfileImageOptions(e){if(e.address&&!ka(e.address))throw new f("Address must be in format: eth|[40-hex-chars]","address","INVALID_ADDRESS")}}class Sa extends Error{constructor(e,t,a){super(e),this.filename=t,this.mimeType=a,this.name="FileValidationError"}}function ba(e,t,a){if(!e)throw new Sa("File is required",t,a);if("undefined"!=typeof File&&e instanceof File){const t=ve();if(t){const a=t.safeParse(e);if(!a.success){const t=a.error.errors.map(e=>e.message).join("; ");throw new Sa(t,e.name,e.type)}}return}if(Buffer.isBuffer(e)){if(!t)throw new Sa("Filename is required when uploading Buffer objects",t,a);const n=ge.safeParse(e);if(!n.success){const e=n.error.errors.map(e=>e.message).join("; ");throw new Sa(e,t,a)}if(t.length>255)throw new Sa(`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 Sa(`Invalid file type "${a}" is not allowed. Allowed types: ${r.join(", ")}`,t,a);const o=function(e){if(!e)return"";const t=e.lastIndexOf(".");if(-1===t)return"";return e.substring(t).toLowerCase()}(t),s=[".png",".jpg",".jpeg",".gif",".webp",".svg"];if(!s.includes(o))throw new Sa(`File extension "${o}" is not allowed. Allowed extensions: ${s.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"}}(o);if(i!==a&&"application/octet-stream"!==i)throw new Sa(`File extension "${o}" does not match MIME type "${a}"`,t,a);return}throw new Sa("File must be a File object (browser) or Buffer (Node.js)",t,a)}class Ia{constructor(e,t=!1){this.http=e,this.logger=new d({debug:t,context:"ImageService"})}async uploadImageByTokenName(e){const{tokenName:t,options:a}=e;xt(t);const n=`${t}.png`;ba(a.file,n,"image/png");try{const e=`${a.tokenName??t}.png`,n=wa(a.file,"image",e),r=await this.http.request({method:"POST",url:`${Wt}?tokenName=${encodeURIComponent(a.tokenName??t)}`,data:n,headers:{}});return jt(r,"Image upload failed"),"string"==typeof r.data?r.data:""}catch(e){if(e instanceof Error&&e.message.includes("FormData"))throw Gt("File upload failed: FormData not supported in this environment. Ensure you have proper polyfills for Node.js environments.","FormData");throw e}}}class Fa{constructor(e,t=!1){this.http=e,this.logger=new d({debug:t,context:"FaucetService"})}async transferFaucets(e){this.validateTransferFaucetsData(e);const t={userAddress:e.walletAddress,amount:e.amount};jt(await this.http.post(sa,t),"Faucet transfer failed")}validateTransferFaucetsData(e){if(!ka(e.walletAddress))throw new f("Address must be in format: eth|[40-hex-chars]","address","INVALID_ADDRESS");if(!(t=e.amount)||"string"!=typeof t||!Ta.FAUCET_AMOUNT.POSITIVE_NON_ZERO_DECIMAL.test(t))throw new f("Amount must be a positive decimal string greater than zero","amount","INVALID_AMOUNT");var t}}class Ca{constructor(e,t=!1){this.http=e,this.poolService=new ha(e,t),this.tradeService=new ma(e,t),this.commentService=new Aa(e,this.poolService,t),this.userService=new Ea(e),this.imageService=new Ia(e,t),this.faucetService=new Fa(e,t)}async uploadImageByTokenName(e){return this.imageService.uploadImageByTokenName(e)}async fetchPools(e={}){return this.poolService.fetchPools(e)}async fetchAllPools(e){return this.poolService.fetchAllPools(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 xt(e)}}const xa={MAX_UNIQUE_KEY_LENGTH:64,UNIQUE_KEY_PATTERN:/^(galaswap-operation-|galaconnect-operation-)/,TOKEN_NAME_PATTERN:/^[a-zA-Z0-9]+$/};var Da;!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"}(Da||(Da={}));class Pa extends Error{constructor(e,t,a){super(e),this.type=t,this.details=a,this.name="TransferError"}}class _a{static validateAmount(e){if(parseFloat(e)<=0)throw new Pa("Transfer amount must be positive",Da.INVALID_AMOUNT,{amount:e})}static validateUniqueKey(e){if(e){if(e.length>xa.MAX_UNIQUE_KEY_LENGTH)throw new f(`Unique key too long. Maximum length: ${xa.MAX_UNIQUE_KEY_LENGTH}`);if(!xa.UNIQUE_KEY_PATTERN.test(e))throw new Pa('Invalid unique key format. Must start with "galaswap-operation-" or "galaconnect-operation-"',Da.INVALID_AMOUNT,{uniqueKey:e})}}}class La extends r.ChainCallDTO{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 o;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`);o={collection:e[0],category:e[1],type:e[2],additionalKey:e[3],instance:"0"}}else o={collection:n.collection,category:n.category,type:n.type,additionalKey:n.additionalKey,instance:"0"};return new La({from:e,to:t,quantity:a,tokenInstance:o,uniqueKey:r||`galaconnect-operation-${Date.now()}_${Math.random().toString(36).substring(2,8)}`})}static forGALA(e,t,a,n){return new La({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 Oa{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(e){const t=e.replace("0x","");return`eth|${a.ethers.getAddress(`0x${t}`).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 Ua(e){if("string"==typeof e){const t=e.split("|");if(t.length<4)throw new f(`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 f(`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 f("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 f(`Invalid tokenId type: ${typeof e}. Expected string, TokenClassKey, or TokenInstanceKey`,"tokenId","INVALID_TOKEN_ID_TYPE")}var $a=Object.freeze({__proto__:null,normalizeToTokenInstanceKey:Ua});function Ba(e){return e instanceof Error}function Ma(e){return Ba(e)||function(e){return"object"==typeof e&&null!==e&&"message"in e&&"string"==typeof e.message}(e)?e.message:"string"==typeof e?e:String(e)}function Ra(e){return"object"==typeof e&&null!==e&&"message"in e&&("response"in e||"request"in e||"config"in e)}const Ka="gala-transfer-successful",Ga="token-transfer-successful",za="transfer-successful-no-id";class Va{constructor(e,t,a,n=!1){this.http=e,this.wallet=t,this.tokenResolver=a,this.signatureHelper=new Oa(t),this.logger=new d({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 Kt(`Failed to fetch pool details: Status ${t.Status}`,t.Status);const a=t.Data.reverseBondingCurveConfiguration,n=a?.minFeePortion??"0",r=a?.maxFeePortion??"0",o=(await import("bignumber.js")).default,s=!new o(n).isZero()||!new o(r).isZero(),i=t.Data;return i.reverseBondingCurveMinFeePortion=n,i.reverseBondingCurveMaxFeePortion=r,i.hasReverseBondingCurveFee=s,i.isGraduated="Completed"===t.Data.saleStatus,delete i.reverseBondingCurveConfiguration,t}async fetchLaunchTokenFee(){const e=await this.http.post("/api/asset/launchpad-contract/FetchLaunchpadFeeAmount",{});if(1!==e.Status)throw Kt(`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 f("Invalid fetch pool details data: missing required fields","data","INVALID_TYPE");var t;if(!e.vaultAddress||"string"!=typeof e.vaultAddress)throw new f("Vault address is required and must be a string","vaultAddress","INVALID_VAULT_ADDRESS");if(!e.vaultAddress.startsWith("service|Token$Unit$"))throw new f("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 Kt(`Failed to fetch token balance from GalaChain: ${Ma(e)}`,void 0,Ba(e)?e:void 0)}}async transferGala(e){this.validateTransferGalaData(e);try{const t=E(e.recipientAddress),a=E(this.wallet.address),n=La.forGALA(a,t,e.amount,e.uniqueKey),r=await this.signatureHelper.signTransferToken(n.toSigningPayload()),o=new La({...n.toSigningPayload(),signedPayload:r});this.logger.debug("[DEBUG] Full GALA Transfer Request Payload:",JSON.stringify(o,null,2));const s=await this.http.post("/api/asset/token-contract/TransferToken",o);return this.logger.debug("[DEBUG] Transfer response:",JSON.stringify(s,null,2)),this.extractTransactionIdFromResponse(s,"gala")}catch(t){throw this.handleTransferError(t,"GALA transfer failed",e)}}async transferToken(e){this.validateTransferTokenData(e);try{const t=E(e.to),a=E(this.wallet.address);let n;if(e.tokenId)n=Ua(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",Da.TOKEN_NOT_FOUND);n=await this.resolveTokenInstance(e.tokenName)}const r=new La({from:a,to:t,quantity:e.amount,tokenInstance:n,uniqueKey:e.uniqueKey||`galaconnect-operation-${Date.now()}_${Math.random().toString(36).substring(2,8)}`}),o=await this.signatureHelper.signTransferToken(r.toSigningPayload()),s=new La({...r.toSigningPayload(),signedPayload:o});this.logger.debug("[DEBUG] Full Transfer Request Payload:",JSON.stringify(s,null,2));const i=await this.http.post("/api/asset/token-contract/TransferToken",s);return this.logger.debug("[DEBUG] Token transfer response:",JSON.stringify(i,null,2)),this.extractTransactionIdFromResponse(i,"token")}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)}`,Da.TOKEN_NOT_FOUND,{tokenName:e})}}validateTransferGalaData(e){if(!function(e){if(!e||"object"!=typeof e)return!1;const t=e;return"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)}(e))throw new f("Invalid GALA transfer data: missing required fields");if(!N(e.recipientAddress))throw new Pa("Invalid recipient address format",Da.INVALID_RECIPIENT,{recipientAddress:e.recipientAddress});_a.validateAmount(e.amount),_a.validateUniqueKey(e.uniqueKey)}validateTransferTokenData(e){if(!function(e){if(!e||"object"!=typeof e)return!1;const t=e;return"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)}(e))throw new f("Invalid token transfer data: missing required fields");if(!N(e.to))throw new Pa("Invalid recipient address format",Da.INVALID_RECIPIENT,{recipientAddress:e.to});if(!e.tokenId&&!e.tokenName)throw new Pa("Must provide either tokenId or tokenName for token identification",Da.TOKEN_NOT_FOUND);if(e.tokenName&&!xa.TOKEN_NAME_PATTERN.test(e.tokenName))throw new Pa("Invalid token name format",Da.TOKEN_NOT_FOUND,{tokenName:e.tokenName});_a.validateAmount(e.amount),_a.validateUniqueKey(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`,Da.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)}`,Da.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}`,Da.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}`,Da.TOKEN_NOT_FOUND);return{collection:n[0],category:n[1],type:n[2],additionalKey:n[3],instance:"0"}}extractTransactionIdFromResponse(e,t){if(e&&"object"==typeof e){if("Status"in e&&1===e.Status&&"Data"in e){const a=e;return Array.isArray(a.Data)&&a.Data.length>0?"gala"===t?Ka:Ga:za}if("transactionId"in e&&"string"==typeof e.transactionId&&e.transactionId)return e.transactionId}throw new Pa("Transfer succeeded but transaction ID could not be extracted",Da.NETWORK_ERROR)}handleTransferError(e,t,a){if(e instanceof Pa)return e;if(e instanceof f)return new Pa(e.message,Da.INVALID_AMOUNT);if(Ra(e)&&e.response){const t=e.response.status,n=e.response.data;if(400===t)return new Pa(("string"==typeof n?.message?n.message:void 0)||"Invalid transfer request",Da.INVALID_AMOUNT);if(403===t)return new Pa("Insufficient balance for transfer",Da.INSUFFICIENT_BALANCE);if(404===t){const e={};return"tokenName"in a&&(e.tokenName=a.tokenName),new Pa("Token not found",Da.TOKEN_NOT_FOUND,e)}}if("object"==typeof e&&null!==e&&"code"in e&&("ECONNABORTED"===e.code||"ETIMEDOUT"===e.code))return new Pa("Transfer request timed out",Da.NETWORK_ERROR);const n=Ma(e);return new Pa(n||t,Da.NETWORK_ERROR)}}class qa{constructor(e,t=!1){this.http=e,this.logger=new d({debug:t,context:"DexService"})}async fetchTokenSpotPrice(e){if(!e||Array.isArray(e)&&0===e.length)throw Mt("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 Kt(`Failed to fetch token prices: ${e instanceof Error?e.message:e}`)}}async fetchLaunchpadTokenSpotPrice(e,t){if(!e||"string"!=typeof e)throw new Error(g);try{const a=await t({tokenName:e,amount:"1",type:"native"}),n=(await this.fetchTokenSpotPrice("GALA"))[0];if(!n)throw Kt("GALA price not available");const r=Number(a.amount)/1e18;if(r<=0)throw new f(`Invalid token amount calculation: ${r}`,"amount","INVALID_CALCULATION");const o=n.price/r;return{symbol:e.toUpperCase(),price:o}}catch(t){if(t instanceof Error)throw Kt(`Failed to calculate launchpad token spot price for ${e}: ${t.message}`);throw Kt(`Failed to calculate launchpad token spot price for ${e}: ${String(t)}`)}}}function ja(e,t=18){const a=parseFloat(e);if(0===a)return"0";return a.toFixed(t).replace(/\.?0+$/,"")}function Wa(e){return ja(e,8)}function Ha(e){return ja(e,18)}function Qa(e){return{maxAcceptableReverseBondingCurveFee:Wa(e.maxAcceptableReverseBondingCurveFee)}}new d({debug:!1,context:"NumberUtils"});class Xa extends r.ChainCallDTO{constructor(e,t,a="0",n={maxAcceptableReverseBondingCurveFee:"0"}){super(),this.vaultAddress=e,this.nativeTokenQuantity=Wa(t),this.expectedToken=Ha(a),this.extraFees=Qa(n)}}class Ya extends r.ChainCallDTO{constructor(e,t,a,n={maxAcceptableReverseBondingCurveFee:"0"}){super(),this.vaultAddress=e,this.tokenQuantity=Ha(t),this.expectedNativeToken=Wa(a),this.extraFees=Qa(n)}}class Za extends r.ChainCallDTO{constructor(e,t,a="0",n={maxAcceptableReverseBondingCurveFee:"0"}){super(),this.vaultAddress=e,this.tokenQuantity=Ha(t),this.expectedNativeToken=Wa(a),this.extraFees=Qa(n)}}class Ja extends r.ChainCallDTO{constructor(e,t,a,n={maxAcceptableReverseBondingCurveFee:"0"}){super(),this.vaultAddress=e,this.nativeTokenQuantity=Wa(t),this.expectedToken=Ha(a),this.extraFees=Qa(n)}}const en={BuyNativeDto:Xa,BuyExactDto:Ya,SellExactDto:Za,SellNativeDto:Ja};var tn;!function(e){e[e.METAMASK=0]="METAMASK",e[e.TRUST_WALLET=1]="TRUST_WALLET",e[e.GALA_WALLET=2]="GALA_WALLET"}(tn||(tn={}));class an{constructor(e,t=!1){this.walletProvider=e,this.debug=t,this.logger=new d({debug:t,context:"SignatureService"})}async signDTO(e,t,a,n=tn.METAMASK){try{this.logger.debug("🔐 Signing DTO:",{methodName:t,walletPreference:n,dtoKeys:Object.keys(e)});const o=this.generateEIP712Types(t,e),s=r.calculatePersonalSignPrefix(e),i={...e,prefix:s};let c,l,u;switch(n){case tn.GALA_WALLET:({signature:c,domain:l}=await this.signWithGalaWallet(o,i,t,a));break;case tn.TRUST_WALLET:c=await this.signWithTrustWallet(i),l={name:"ethereum",chainId:1};break;case tn.METAMASK:default:({signature:c,domain:l}=await this.signWithMetaMask(o,i))}return u=n===tn.TRUST_WALLET?{...i,signature:c}:{...e,signature:c,types:o,domain:l},this.logger.debug("✅ DTO signed successfully:",{payloadKeys:Object.keys(u),signatureLength:c.length}),u}catch(e){this.logger.error("❌ Signature generation failed:",e);throw zt(`Failed to sign DTO: ${Ma(e)}`)}}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 Gt("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 zt(`MetaMask/ethers signing failed: ${Ma(e)}`)}}async signWithTrustWallet(e){try{const a=(t=e,JSON.stringify(t));let n;if(!this.walletProvider.signMessage)throw Gt("Wallet provider does not support signMessage","walletProvider");return n=await this.walletProvider.signMessage(a),n}catch(e){throw zt(`TrustWallet signing failed: ${Ma(e)}`)}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 o={domain:r,types:e,message:t,Primary_type:a},s=window;if(!s.gala)throw Gt("GalaWallet not found in window object","galaWallet");await s.gala.setAddress(n);return{signature:await s.gala.request({method:"eth_signTypedData",params:[JSON.stringify(o),n]}),domain:r}}catch(e){throw zt(`GalaWallet signing failed: ${Ma(e)}`)}}generateEIP712Types(e,t){const a={};a[e]=[];const n=(e,t,r,o=!1)=>{if(void 0!==t){if(Array.isArray(t)){const s=n(e,t[0],r,!0);return o||a[r].push({name:e,type:(s??e)+"[]"}),s?s+"[]":void 0}if("object"==typeof t&&null!==t){if(a[e])throw new f(`Type name collision not supported: ${e}`,"fieldValue","TYPE_COLLISION");return a[e]=[],Object.entries(t).forEach(([t,a])=>{n(t,a,e)}),o||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 f(`Unsupported type for fieldName ${e}: ${typeof t}, value: ${t}`,"fieldValue","UNSUPPORTED_TYPE")}return o||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 tn.METAMASK;const e=window;return e.gala?tn.GALA_WALLET:e.trustWallet?.isTrust?tn.TRUST_WALLET:(e.ethereum,tn.METAMASK)}}class nn{constructor(e=!1){this.debug=e,this.logger=new d({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`,o=`$tokenBalance$${a.toStringKey()}$${e}`,s=`$tokenBalance$${a.toStringKey()}$${e}`,i=`$tokenBalance$${n.toStringKey()}$${e}`,c=[r,o,s,i,`$tokenBalance$${n.toStringKey()}$${e}`];return this.logger.debug("✅ Generated stringsInstructions:",c),c}catch(e){this.logger.error("❌ Failed to generate stringsInstructions:",e);const t=Ma(e);throw new f(`Failed to generate stringsInstructions: ${t}`,"vaultAddress","INVALID_VAULT_ADDRESS")}}createTokenInstance(e){const t=new i.TokenClassKey;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 i.TokenClassKey;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 Mt("vaultAddress","Vault address");const t=e.split("$");if(t.length<3)throw Rt("vaultAddress","format: service|Token$Unit$SYMBOL$eth:address$launchpad");const a=t[2];if(!a||0===a.trim().length)throw new f(`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 Mt("vaultAddress","Vault address");if(!e.startsWith("service|Token$Unit$"))throw Rt("vaultAddress",'starting with "service|Token$Unit$"');if(!e.endsWith("$launchpad"))throw Rt("vaultAddress",'ending with "$launchpad"');const t=e.split("$");if(t.length<5)throw Rt("vaultAddress",'having at least 5 parts separated by "$"');const a=t[2];if(!a||!/^[A-Za-z]{1,10}$/.test(a))throw Rt("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 Rt("stringKey","format: collection$category$type$additionalKey (4 parts)");return{collection:t[0],category:t[1],type:t[2],additionalKey:t[3]}}}function rn(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 s(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 o;switch(a){case"buy-native":case"sell-exact":o=n.minus(r);break;case"buy-exact":case"sell-native":o=n.plus(r);break;default:throw new Error(`Unknown operation type: ${a}`)}return o.isLessThan(0)&&(o=new s(0)),o.toFixed()}const on=[];for(let e=0;e<256;++e)on.push((e+256).toString(16).slice(1));let sn;const cn=new Uint8Array(16);var ln,un,dn={randomUUID:"undefined"!=typeof crypto&&crypto.randomUUID&&crypto.randomUUID.bind(crypto)};function hn(e,t,a){const n=(e=e||{}).random??e.rng?.()??function(){if(!sn){if("undefined"==typeof crypto||!crypto.getRandomValues)throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");sn=crypto.getRandomValues.bind(crypto)}return sn(cn)}();if(n.length<16)throw new Error("Random bytes length must be >= 16");return n[6]=15&n[6]|64,n[8]=63&n[8]|128,function(e,t=0){return(on[e[t+0]]+on[e[t+1]]+on[e[t+2]]+on[e[t+3]]+"-"+on[e[t+4]]+on[e[t+5]]+"-"+on[e[t+6]]+on[e[t+7]]+"-"+on[e[t+8]]+on[e[t+9]]+"-"+on[e[t+10]]+on[e[t+11]]+on[e[t+12]]+on[e[t+13]]+on[e[t+14]]+on[e[t+15]]).toLowerCase()}(n)}function pn(e,t,a){return dn.randomUUID&&!e?dn.randomUUID():hn(e)}class gn{constructor(e,t,a=!1,n,r,o=.05,s=.01){this.httpClient=e,this.tokenResolver=t,this.walletProvider=n,this.userAddress=r,this.defaultSlippageToleranceFactor=o,this.defaultMaxAcceptableReverseBondingCurveFeeSlippageFactor=s,this.bundleEndpoint="/bundle",this.logger=new d({debug:a,context:"BundleService"}),n&&r&&(this.signatureService=new an(n,a),this.tokenKeyService=new nn(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 Mt("bundleData","Bundle data");if(!e.signedDto)throw Mt("signedDto","Signed DTO");if(!e.method||"string"!=typeof e.method)throw Mt("method","Method name");if(!Array.isArray(e.stringsInstructions))throw Rt("stringsInstructions","an array of resource tracking strings");if(0===e.stringsInstructions.length)throw new f("stringsInstructions cannot be empty","stringsInstructions","EMPTY_ARRAY");const t=["BuyWithNative","BuyExactToken","SellExactToken","SellWithNative"];if(!t.includes(e.method))throw Rt("method",`one of: ${t.join(", ")}`);e.stringsInstructions.forEach((e,t)=>{if("string"!=typeof e||0===e.length)throw new f(`stringsInstructions[${t}] must be a non-empty string`,`stringsInstructions[${t}]`,"INVALID_INSTRUCTION");if(!e.startsWith("$"))throw new f(`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){if("string"==typeof e)return e;if(Ra(e)&&e.response){const t=e.response.data;if(t&&"object"==typeof t){const e=t;if(e.error)return String(e.error);if(e.message)return String(e.message)}}return Ma(e)||"Unknown bundle transaction error"}async getBundlerTransactionResult(e){try{if(!e||"string"!=typeof e)throw Mt("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 Mt("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}=e,{effectiveSlippageFactor:o,effectiveMaxFee:s,vaultAddress:i}=await this.prepareTradingOperation(t,e.maxAcceptableReverseBondingCurveFee,e.maxAcceptableReverseBondingCurveFeeSlippageFactor,e.slippageToleranceFactor);if("native"===n){if(!r)throw new f("expectedAmount is required for native buy operations. Use getBuyTokenAmount() first to calculate expected tokens.","expectedAmount","EXPECTED_AMOUNT_REQUIRED");const e=rn(r,o,"buy-native");this.logger.debug("BuyNative slippage applied:",{originalExpectedTokens:r,slippageFactor:o,adjustedMinTokens:e});const t=new en.BuyNativeDto(i,a,e,{maxAcceptableReverseBondingCurveFee:s});return await this.executeBundleTransaction(t,"BuyWithNative",i)}{if(!r)throw new f("expectedAmount is required for exact buy operations. Use getBuyTokenAmount() first to calculate expected GALA cost.","expectedAmount","EXPECTED_AMOUNT_REQUIRED");const e=rn(r,o,"buy-exact");this.logger.debug("BuyExact slippage applied:",{originalExpectedGalaCost:r,slippageFactor:o,adjustedMaxGalaCost:e});const t=new en.BuyExactDto(i,a,e,{maxAcceptableReverseBondingCurveFee:s});return await this.executeBundleTransaction(t,"BuyExactToken",i)}}async sellToken(e){this.ensureTradingServicesAvailable();const{tokenName:t,amount:a,type:n,expectedAmount:r}=e,{effectiveSlippageFactor:o,effectiveMaxFee:s,vaultAddress:i}=await this.prepareTradingOperation(t,e.maxAcceptableReverseBondingCurveFee,e.maxAcceptableReverseBondingCurveFeeSlippageFactor,e.slippageToleranceFactor);if("exact"===n){if(!r)throw new f("expectedAmount is required for exact sell operations. Use getSellTokenAmount() first to calculate expected GALA.","expectedAmount","EXPECTED_AMOUNT_REQUIRED");const e=rn(r,o,"sell-exact");this.logger.debug("SellExact slippage applied:",{originalExpectedGala:r,slippageFactor:o,adjustedMinGala:e});const t=new en.SellExactDto(i,a,e,{maxAcceptableReverseBondingCurveFee:s});return await this.executeBundleTransaction(t,"SellExactToken",i)}{if(!r)throw new f("expectedAmount is required for native sell operations. Use getSellTokenAmount() first to calculate tokens to sell.","expectedAmount","EXPECTED_AMOUNT_REQUIRED");const e=rn(r,o,"sell-native");this.logger.debug("SellNative slippage applied:",{originalExpectedTokensToSell:r,slippageFactor:o,adjustedMaxTokensToSell:e});const t=new en.SellNativeDto(i,a,e,{maxAcceptableReverseBondingCurveFee:s});return await this.executeBundleTransaction(t,"SellWithNative",i)}}async prepareTradingOperation(e,t,a,n){const{effectiveSlippageFactor:r,effectiveMaxFee:o}=this.calculateEffectiveSlippage(t,a,n),s=await this.resolveTokenNameToVault(e);if(!s)throw Bt(e);return{effectiveSlippageFactor:r,effectiveMaxFee:o,vaultAddress:s}}calculateEffectiveSlippage(e,t,a){const n=a??this.defaultSlippageToleranceFactor,r=t??this.defaultMaxAcceptableReverseBondingCurveFeeSlippageFactor;let o=e||"0";return e&&(o=rn(e,r,"buy-exact"),this.logger.debug("Reverse bonding curve fee slippage applied:",{baseFee:e,slippageFactor:r,adjustedMaxFee:o})),{effectiveSlippageFactor:n,effectiveFeeSlippageFactor:r,effectiveMaxFee:o}}ensureTradingServicesAvailable(){if(!this.signatureService||!this.tokenKeyService)throw Gt("Trading services not available. BundleService requires walletProvider and userAddress for trading operations.","walletProvider");if(!this.userAddress)throw Mt("userAddress","User address")}async executeBundleTransaction(e,t,a){this.ensureTradingServicesAvailable();try{e.uniqueKey=`galaswap - operation - ${pn()}-${Date.now()}-${this.userAddress}`;const n=await this.signatureService.signDTO(e,t,this.userAddress),r=this.tokenKeyService.generateStringsInstructions(a),o={stringsInstructions:r,method:t,signedDto:n};this.logger.debug("📦 Bundle transaction data:",{method:t,stringsInstructions:r,dtoKeys:Object.keys(n)});const s=await this.submitTransaction(o);if(s.success&&s.data)return this.logger.debug("✅ Bundle transaction submitted:",s.data),{success:!0,data:{transactionId:s.data,message:"Transaction submitted successfully. Monitor WebSocket for completion."}};throw new Error(String(s.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"}(ln||(ln={})),exports.SDKTransactionStatus=void 0,(un=exports.SDKTransactionStatus||(exports.SDKTransactionStatus={})).PENDING="pending",un.PROCESSING="processing",un.COMPLETED="completed",un.FAILED="failed",un.TIMEOUT="timeout";const mn={[ln.PROCESSED]:exports.SDKTransactionStatus.COMPLETED,[ln.COMPLETED]:exports.SDKTransactionStatus.COMPLETED,[ln.SUCCESS]:exports.SDKTransactionStatus.COMPLETED,[ln.FAILED]:exports.SDKTransactionStatus.FAILED,[ln.ERROR]:exports.SDKTransactionStatus.FAILED,[ln.PROCESSING]:exports.SDKTransactionStatus.PROCESSING,[ln.PENDING]:exports.SDKTransactionStatus.PENDING};class fn{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 d({debug:t,context:"WebSocketService"}),this.isSocketIOAvailable=this.checkSocketIOAvailability()}checkSocketIOAvailability(){try{return"function"==typeof c.io||(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=c.io(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:exports.SDKTransactionStatus.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,r=n?.data,o=n?.status||n?.Status||r?.status||r?.Status;let s=n?.message||n?.Message||r?.message||r?.Message||n?.error||r?.error;s&&"string"==typeof s||(s=o===ln.FAILED||o===ln.ERROR?"Transaction failed - check transaction details":o===ln.COMPLETED||o===ln.PROCESSED||o===ln.SUCCESS?"Transaction completed successfully":o?`Transaction status: ${o}`:"Unknown transaction status");const i=n?.blockHash||r?.blockHash,c=n?.gasUsed||r?.gasUsed,l={transactionId:e,status:this.mapSocketStatus(o),message:"string"==typeof s?s:"Transaction update received",timestamp:Date.now(),...i&&{blockHash:i},...c&&{gasUsed:c}};if(this.logger.debug(`📡 Mapped status for ${e}: ${o} -> ${l.status}`),this.logger.debug(`📡 Final message: "${s}"`),t(l),l.status===exports.SDKTransactionStatus.COMPLETED||l.status===exports.SDKTransactionStatus.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} (${l.status})`)}});else{const a={transactionId:e,status:exports.SDKTransactionStatus.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===exports.SDKTransactionStatus.COMPLETED?t(e):e.status!==exports.SDKTransactionStatus.FAILED&&e.status!==exports.SDKTransactionStatus.TIMEOUT||a(new Error(`Transaction ${e.status}: ${e.message}`))})})}mapSocketStatus(e){const t=e?.toUpperCase();return mn[t]||exports.SDKTransactionStatus.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 yn{constructor(e,t=!1){this.poolService=e,this.cache=new Map,this.logger=new d({debug:t,context:"TokenResolverService"})}async resolveTokenToVault(e){if(!k(e))throw new f("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 Bt(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 Rt("vaultAddress","format: service|Token$Unit$...$launchpad","Vault address");const a=t[1].split("$");if(a.length<4)throw Rt("vaultAddress","at least 4 parts after service|","Vault address");return{collection:a[0],category:a[1],type:a[2],additionalKey:a[3]}}}class vn{}vn.BASE_PRICE=1650667151e-14,vn.PRICE_SCALING_FACTOR=1166069e-12,vn.TRADING_FEE_FACTOR=.001,vn.GAS_FEE="1",vn.MIN_UNBONDING_FEE_FACTOR=0,vn.MAX_UNBONDING_FEE_FACTOR=.5,vn.NET_UNBONDING_FEE_FACTOR=.5,vn.DEFAULT_LAUNCHPAD_TOKEN_MAX_SUPPLY=1e7;class An{constructor(e=!1){this.cache=new Map,this.logger=new d({debug:e,context:"TokenMetadataCache"})}getLRUKey(){const e=this.cache.keys().next().value;return void 0!==e?e:null}normalizeTokenName(e){return e.trim().toLowerCase().replace(/\s+/g," ").replace(/[\u0000-\u001F\u007F-\u009F\u200B-\u200D\uFEFF]/g,"")}updateCacheEntry(e,t){const a=this.cache.get(e);if(this.cache.has(e)&&this.cache.delete(e),this.cache.size>=An.MAX_CACHE_SIZE){const e=this.getLRUKey();null!==e&&this.cache.delete(e)}this.cache.set(e,{...a||{},...t,lastUpdated:Date.now()})}warmFromPoolData(e,t){const a=this.normalizeTokenName(e);this.updateCacheEntry(a,t)}set(e,t){const a=this.normalizeTokenName(e);this.updateCacheEntry(a,t)}get(e){const t=this.normalizeTokenName(e);return this.cache.get(t)||null}getMaxSupply(e){const t=this.normalizeTokenName(e),a=this.cache.get(t);return a?.maxSupply||vn.DEFAULT_LAUNCHPAD_TOKEN_MAX_SUPPLY.toString()}has(e){const t=this.normalizeTokenName(e);return this.cache.has(t)}clear(e){if(e){const t=this.normalizeTokenName(e);this.cache.delete(t)}else this.cache.clear()}dump(){const e={};return this.cache.forEach((t,a)=>{e[a]=t}),e}stats(){let e=Date.now(),t=0;return this.cache.forEach((a,n)=>{a.lastUpdated<e&&(e=a.lastUpdated);let r=0;r+=2*n.length,void 0!==a.reverseBondingCurveMinFeeFactor&&(r+=8),void 0!==a.reverseBondingCurveMaxFeeFactor&&(r+=8),void 0!==a.reverseBondingCurveNetFeeFactor&&(r+=8),r+=8,a.vaultAddress&&(r+=2*a.vaultAddress.length),a.maxSupply&&(r+=2*a.maxSupply.length),r+=32,t+=r}),{totalTokens:this.cache.size,cacheSize:t,oldestEntry:this.cache.size>0?e:0}}}function wn(e){const t=function(e){const t=dt(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")}`)}An.MAX_CACHE_SIZE=1e4;const Tn="/api/asset/launchpad-contract/CallNativeTokenIn",kn="/api/asset/launchpad-contract/CallNativeTokenOut",Nn="/api/asset/launchpad-contract/CallMemeTokenIn",En="/api/asset/launchpad-contract/CallMemeTokenOut";class Sn extends r.ChainCallDTO{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 bn(e){if(!e||"object"!=typeof e)return!1;const t=e;return"number"==typeof t.Status&&void 0!==t.Data&&"object"==typeof t.Data&&null!==t.Data&&"string"==typeof t.Data.calculatedQuantity&&void 0!==t.Data.extraFees&&"object"==typeof t.Data.extraFees&&null!==t.Data.extraFees&&"string"==typeof t.Data.extraFees.reverseBondingCurve&&"string"==typeof t.Data.extraFees.transactionFees}const In={NATIVE:"native",EXACT:"exact"},Fn={LOCAL:"local",EXTERNAL:"external"};class Cn{static calculateBuyWithExact(e,t){const a=parseFloat(e),n=parseFloat(t),{BASE_PRICE:r,PRICE_SCALING_FACTOR:o,TRADING_FEE_FACTOR:i,GAS_FEE:c}=vn,l=this.roundUp(r*(Math.exp((n+a)*o)-Math.exp(n*o))/o,8),u=new s(l).multipliedBy(i).toFixed();return{amount:l.toString(),reverseBondingCurveFee:"0",transactionFee:u,gasFee:c}}static calculateBuyWithNative(e,t){const a=parseFloat(e),n=parseFloat(t),{BASE_PRICE:r,PRICE_SCALING_FACTOR:o,TRADING_FEE_FACTOR:i,GAS_FEE:c}=vn,l=Math.log(a*o/r+Math.exp(n*o))/o-n,u=new s(l).multipliedBy(i).toFixed();return{amount:l.toString(),reverseBondingCurveFee:"0",transactionFee:u,gasFee:c}}static calculateSellWithExact(e,t,a,n,r){const o=parseFloat(e),i=parseFloat(t),c=parseFloat(a),{BASE_PRICE:l,PRICE_SCALING_FACTOR:u,TRADING_FEE_FACTOR:d,GAS_FEE:h}=vn,p=l*(Math.exp(i*u)-Math.exp((i-o)*u))/u,g=new s(p),m=n+i/c*(r-n),f=g.multipliedBy(m).toFixed(8,s.ROUND_UP),y=g.multipliedBy(d).toFixed();return{amount:p.toString(),reverseBondingCurveFee:f,transactionFee:y,gasFee:h}}static calculateSellWithNative(e,t,a,n,r){const o=parseFloat(e),i=parseFloat(t),c=parseFloat(a),{BASE_PRICE:l,PRICE_SCALING_FACTOR:u,TRADING_FEE_FACTOR:d,GAS_FEE:h}=vn,p=i-Math.log(Math.exp(i*u)-o*u/l)/u,g=new s(o),m=n+i/c*(r-n),f=g.multipliedBy(m).toFixed(8,s.ROUND_UP),y=g.multipliedBy(d).toFixed();return{amount:p.toString(),reverseBondingCurveFee:f,transactionFee:y,gasFee:h}}static roundUp(e,t){const a=Math.pow(10,t);return Math.ceil(e*a)/a}}class xn{constructor(e,t,a,n,r,o,s="local"){this.http=e,this.tokenResolver=t,this.logger=a,this.bundleHttp=n,this.galaChainHttp=r,this.dexApiHttp=o,this.defaultCalculateAmountMode=s,this.metadataCache=new An}addIfDefined(e,t,a){return void 0!==a&&(e[t]=a),e}async uploadImageByTokenName(e){const{tokenName:t,options:a}=e;xt(t);const n=`${t}.png`;ba(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 Rt("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 Kt(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 Gt("File upload failed: FormData not supported in this environment. Ensure you have proper polyfills for Node.js environments.","FormData");throw e}}async fetchPoolsFromAPI(e){Dt(e),e.tokenName&&xt(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=h(t),n=await this.http.get("/launchpad/fetch-pool",a);if(!0===n.error||200!==n.status||!n.data)throw Kt(n.message||"Failed to fetch pools",n.status);let r=[];const o=(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 o(t).isZero()||!new o(a).isZero();return{...e,reverseBondingCurveMinFeePortion:t,reverseBondingCurveMaxFeePortion:a,hasReverseBondingCurveFee:n,createdAt:e.created_at||e.createdAt||""}});else{const e=n.data.tokens,t=e.reverseBondingCurveMinFeePortion??"0",a=e.reverseBondingCurveMaxFeePortion??"0",s=!new o(t).isZero()||!new o(a).isZero();r=[{...e,reverseBondingCurveMinFeePortion:t,reverseBondingCurveMaxFeePortion:a,hasReverseBondingCurveFee:s,createdAt:e.created_at||e.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 o(t).isZero()||!new o(a).isZero();return{...e,reverseBondingCurveMinFeePortion:t,reverseBondingCurveMaxFeePortion:a,hasReverseBondingCurveFee:n,createdAt:e.created_at||e.createdAt||""}}));const{extractMetadataFromPoolData:s,isValidPoolForCaching:i}=await Promise.resolve().then(function(){return Gn});r.forEach(e=>{if(!i(e))return void this.logger.debug("Skipping pool with invalid structure for caching",e);const t=s(e,this.logger);t&&this.warmCacheFromPoolData(e.tokenName,t)});const c=n.data.count??n.data.total??0,l=n.data.page??e.page??1,u=n.data.limit??e.limit??10,d=u>0?Math.ceil(c/u):1;return{pools:r,page:l,limit:u,total:c,totalPages:d,hasNext:l<d,hasPrevious:l>1}}async _getAmount(e){if(_t(e),!this.galaChainHttp)throw Gt("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:Tn,body:{vaultAddress:a,tokenQuantity:n,IsPreMint:!1}};if("NATIVE"===e&&"OUT"===t)return{endpoint:kn,body:{vaultAddress:a,tokenQuantity:n,IsPreMint:!1}};if("MEME"===e&&"IN"===t)return{endpoint:Nn,body:{vaultAddress:a,nativeTokenQuantity:n,IsPreMint:!1}};if("MEME"===e&&"OUT"===t)return{endpoint:En,body:{vaultAddress:a,nativeTokenQuantity:n,IsPreMint:!1}};throw Rt("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(!bn(e))throw Kt("Malformed response data from GalaChain gateway");if(1!==e.Status)throw Kt(`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&&xt(e.tokenName);const t=h(e),a=await this.http.get("/launchpad/check-pool",t);if(!0===a.error||200!==a.status)throw Kt(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(!da(e))throw new f("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(xt(t),!a||!n||!r)throw new f("Graph options (from, to, resolution) are required","options","MISSING_GRAPH_OPTIONS");const o={tokenName:t,from:a,to:n,resolution:r};Lt(o);const s=h(o),i=await this.http.get("/launchpad/get-graph-data",s);if(!0===i.error||200!==i.status||!i.data)throw Kt(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 f("Invalid options provided. Expected an options object.","options","INVALID_OPTIONS");const{tokenName:t,amount:a,type:n,currentSupply:r}=e,o=e.mode??this.defaultCalculateAmountMode;if("local"!==o&&"external"!==o)throw new f(`Invalid calculation mode "${o}". Must be "local" or "external".`,"mode","INVALID_CALCULATION_MODE");if(!t||"string"!=typeof t)throw new f("Token name is required and must be a string","tokenName","INVALID_TOKEN_NAME");if(!a||"string"!=typeof a)throw new f("Amount is required and must be a string","amount","INVALID_AMOUNT");if(n!==In.NATIVE&&n!==In.EXACT)throw new f('Type must be either "native" or "exact"',"type","INVALID_TYPE");return"external"===o?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 f(`Token "${t}" not found. Please verify the token name is correct.`,"tokenName","TOKEN_NOT_FOUND");return n===In.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:o,reverseBondingCurveMaxFeeFactor:s,reverseBondingCurveMinFeeFactor:i}=e,c=e.mode??this.defaultCalculateAmountMode;if("local"!==c&&"external"!==c)throw new f(`Invalid calculation mode "${c}". Must be "local" or "external".`,"mode","INVALID_CALCULATION_MODE");if(!t||"string"!=typeof t)throw new f("Token name is required and must be a string","tokenName","INVALID_TOKEN_NAME");if(!a||"string"!=typeof a)throw new f("Amount is required and must be a string","amount","INVALID_AMOUNT");if(n!==In.EXACT&&n!==In.NATIVE)throw new f('Type must be either "exact" or "native"',"type","INVALID_TYPE");if("external"===c)return this.calculateSellAmountExternal({tokenName:t,amount:a,type:n});{const e={tokenName:t,amount:a,type:n,...void 0!==r&&{currentSupply:r},...void 0!==o&&{maxSupply:o},...void 0!==s&&{reverseBondingCurveMaxFeeFactor:s},...void 0!==i&&{reverseBondingCurveMinFeeFactor:i}};return this.calculateSellAmountLocal(e)}}async calculateSellAmountExternal(e){const{tokenName:t,amount:a,type:n}=e,r=await this.tokenResolver.resolveTokenToVault(t);if(!r)throw new f(`Token "${t}" not found. Please verify the token name is correct.`,"tokenName","TOKEN_NOT_FOUND");return n===In.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 f("Amount is required and must be a string","amount","INVALID_AMOUNT");if(n!==In.NATIVE&&n!==In.EXACT)throw new f('Type must be either "native" or "exact"',"type","INVALID_TYPE");void 0!==r&&Ut(r,"currentSupply");const o=!r;if(o&&!t)throw new f("Token name is required when currentSupply is not provided","tokenName","MISSING_TOKEN_NAME");t&&xt(t);let s=r;if(o){s=(await this.fetchPoolDetailsForCalculation(t)).currentSupply}return n===In.EXACT?Cn.calculateBuyWithExact(a,s):Cn.calculateBuyWithNative(a,s)}async calculateSellAmountLocal(e){const{tokenName:t,amount:a,type:n,currentSupply:r,maxSupply:o,reverseBondingCurveMaxFeeFactor:s,reverseBondingCurveMinFeeFactor:i}=e;if(!a||"string"!=typeof a)throw new f("Amount is required and must be a string","amount","INVALID_AMOUNT");if(n!==In.EXACT&&n!==In.NATIVE)throw new f('Type must be either "exact" or "native"',"type","INVALID_TYPE");void 0!==r&&Ut(r,"currentSupply");const c=!r||!o||void 0===s||void 0===i;if(c&&!t)throw new f("Token name is required when currentSupply, maxSupply, or fee factors are not provided","tokenName","MISSING_TOKEN_NAME");t&&xt(t);let l=r,u=o,d=s,h=i;if(c&&t){const e=this.metadataCache.get(t);u=u??this.metadataCache.getMaxSupply(t),d=d??e?.reverseBondingCurveMaxFeeFactor,h=h??e?.reverseBondingCurveMinFeeFactor,l||(l=await this.fetchCurrentSupply(t));if(void 0===d||void 0===h){const e=await this.fetchPoolDetailsForCalculation(t);d=d??e.reverseBondingCurveMaxFeeFactor,h=h??e.reverseBondingCurveMinFeeFactor}}return n===In.EXACT?Cn.calculateSellWithExact(a,l,u,h,d):Cn.calculateSellWithNative(a,l,u,h,d)}async calculateBuyAmountForGraduation(e){const t="string"==typeof e?{tokenName:e}:e;if("object"==typeof e&&!function(e){if(!e||"object"!=typeof e)return!1;const t=e;return ia(t,"tokenName")&&ua(t)&&ca(t,"currentSupply")}(e))throw new Error("Invalid CalculateBuyAmountForGraduationOptions provided");const{tokenName:a,calculateAmountMode:n,currentSupply:r}=t;xt(a);const o=await this.tokenResolver.resolveTokenToVault(a);if(!o)throw new f(m(a),"tokenName","VAULT_NOT_FOUND");if(!this.galaChainHttp)throw Gt("GalaChain HTTP client not configured");const s=await this.galaChainHttp.post("/api/asset/launchpad-contract/FetchSaleDetails",{vaultAddress:o});if(1!==s.Status)throw Kt(`Failed to fetch pool details: Status ${s.Status}`,s.Status);const i=s.Data,c=(await import("bignumber.js")).default,l=r??new c(i.maxSupply).minus(i.sellingTokenQuantity).toFixed(),u=i.sellingTokenQuantity;if("0"===u)throw new f(`Token ${a} is already graduated (no tokens remaining in pool)`,"tokenName","ALREADY_GRADUATED");const d={tokenName:a,amount:u,type:"exact",currentSupply:l,...void 0!==n&&{mode:n}};return await this.calculateBuyAmount(d)}async launchToken(e){if(!this.bundleHttp)throw Gt("Bundle backend client not configured. LaunchToken requires bundleHttp client.","bundleHttp");wn(e);const t=e.preBuyQuantity||"0";if(isNaN(Number(t))||Number(t)<0)throw new f("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 f("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 Kt("Image upload failed: No URL returned");a=t}else"string"==typeof e.tokenImage&&(a=e.tokenImage);const n=`galaswap - operation - ${pn()}-${Date.now()}-${this.http.getAddress()}`,o={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&&(o.reverseBondingCurveConfiguration={minFeePortion:e.reverseBondingCurveConfiguration.minFeePortion.toString(),maxFeePortion:e.reverseBondingCurveConfiguration.maxFeePortion.toString()});const s=new Sn(o),i=await this.http.signWithGalaChain("CreateSale",s,r.SigningType.SIGN_TYPED_DATA),{signature:c,types:l,domain:u,prefix:d}=i,h={tokenName:s.tokenName,tokenSymbol:s.tokenSymbol,tokenDescription:s.tokenDescription,tokenImage:s.tokenImage,preBuyQuantity:s.preBuyQuantity,websiteUrl:s.websiteUrl,telegramUrl:s.telegramUrl,twitterUrl:s.twitterUrl,tokenCategory:s.tokenCategory,tokenCollection:s.tokenCollection,uniqueKey:s.uniqueKey,signature:c,types:l,domain:u,...d&&{prefix:d},...s.reverseBondingCurveConfiguration&&{reverseBondingCurveConfiguration:s.reverseBondingCurveConfiguration}},p=`${e.tokenName.trim()}$Unit$none$none`,g="GALA$Unit$none$none";let m;if(parseFloat(t)>0){const e=`$service$${p}$launchpad`;m=[e,`$token$${p}$${e}`,`$tokenBalance$${p}$${e}`,`$tokenBalance$${p}$${e}`,`$tokenBalance$${g}$${e}`,`$tokenBalance$${g}$${e}`]}else{const e=`$service$${p}$launchpad`;m=[e,`$token$${p}$${e}`,`$tokenBalance$${p}$${e}`]}const y={signedDto:h,stringsInstructions:m,method:"CreateSale"},v=await this.bundleHttp.post("/bundle",y);if(v.error||!v.data)throw Kt(v.message||"Token launch failed");return v.data}async fetchTokenDistribution(e){if(!e)throw Mt("tokenName","Token name");xt(e);const t=await this.tokenResolver.resolveTokenToVault(e);if(!t)throw new f(m(e),"tokenName","VAULT_NOT_FOUND");this.metadataCache.set(e,{vaultAddress:t});const a=encodeURIComponent(t),n=await this.http.get(`/holders/${a}`);if(!0===n.error||200!==n.status||!n.data)throw Kt(n.message||"Failed to fetch token distribution",n.status);const r=n.data;if(!Array.isArray(r))throw Kt("Invalid API response: expected array of holders",n.status);for(const e of r){if(!e.owner||"string"!=typeof e.owner)throw Kt("Invalid holder data: missing or invalid owner field",n.status);if(!e.quantity||"string"!=typeof e.quantity)throw Kt("Invalid holder data: missing or invalid quantity field",n.status);const t=parseFloat(e.quantity);if(isNaN(t)||!isFinite(t))throw Kt(`Invalid holder quantity: "${e.quantity}"`,n.status)}const o=r.reduce((e,t)=>e.plus(t.quantity),new s(0));return{holders:r.map(e=>{const t=new s(e.quantity),a=o.isZero()?0:t.dividedBy(o).multipliedBy(100).toNumber();return{address:e.owner,balance:e.quantity,percentage:a}}),totalSupply:o.toFixed(),totalHolders:r.length,lastUpdated:new Date}}async fetchTokenBadges(e){if(!e)throw Mt("tokenName","Token name");xt(e);const t=await this.http.get("/launchpad/get-badge/",{tokenName:e});if(t.error||!t.data)throw Kt(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(!function(e){if(!e||"object"!=typeof e)return!1;const t=e;return"string"==typeof t.nativeTokenQuantity&&(void 0===t.vaultAddress||"string"==typeof t.vaultAddress)}(e))throw new f("Invalid pre-mint calculation data","data","INVALID_PRE_MINT_DATA");if(!this.galaChainHttp)throw Gt("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(!bn(a))throw Kt("Malformed response data from GalaChain gateway");if(1!==a.Status)throw Kt(`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 f(m(e),"tokenName","VAULT_NOT_FOUND");if(!this.galaChainHttp)throw Gt("GalaChain HTTP client not configured");const a=await this.galaChainHttp.post("/api/asset/launchpad-contract/FetchSaleDetails",{vaultAddress:t});if(1!==a.Status)throw Kt(`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(),o=n.sellingTokenQuantity,s=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)`);const l=i-c;return this.metadataCache.set(e,{maxSupply:s,reverseBondingCurveMaxFeeFactor:i,reverseBondingCurveMinFeeFactor:c,reverseBondingCurveNetFeeFactor:l}),{currentSupply:r,remainingTokens:o,maxSupply:s,reverseBondingCurveMaxFeeFactor:i,reverseBondingCurveMinFeeFactor:c,reverseBondingCurveNetFeeFactor:l}}async fetchCurrentSupply(e){xt(e);const t=await this.tokenResolver.resolveTokenToVault(e);if(!t)throw new f(m(e),"tokenName","VAULT_NOT_FOUND");if(!this.galaChainHttp)throw Gt("GalaChain HTTP client not configured");const a=await this.galaChainHttp.post("/api/asset/launchpad-contract/FetchSaleDetails",{vaultAddress:t});if(1!==a.Status)throw Kt(`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(),o=n.maxSupply;return this.metadataCache.set(e,{maxSupply:o}),r}getAddress(){return this.http.getAddress()}formatAddressForBackend(e){return Ot(e)}validateTokenName(e){return xt(e)}validatePagination(e){return Dt(e)}async fetchTokenSpotPrice(e){if(!this.dexApiHttp)throw Gt("DEX API client not configured. Token price fetching requires dexApiHttp client.","dexApiHttp");if(!e||Array.isArray(e)&&0===e.length)throw Mt("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 Kt(`Failed to fetch token prices: ${e instanceof Error?e.message:e}`,void 0,e instanceof Error?e:void 0)}}async fetchLaunchpadTokenSpotPrice(e){const t="string"==typeof e?{tokenName:e}:e;if("object"==typeof e&&!function(e){if(!e||"object"!=typeof e)return!1;const t=e;return ia(t,"tokenName")&&ua(t)&&ca(t,"currentSupply")}(e))throw new Error("Invalid FetchLaunchpadTokenSpotPriceOptions provided");const{tokenName:a,calculateAmountMode:n,currentSupply:r}=t;if(!a||"string"!=typeof a)throw Mt("tokenName","Token name (string)");try{const e={tokenName:a,amount:"1",type:"native",...void 0!==n&&{mode:n},...void 0!==r&&{currentSupply:r}},t=await this.calculateBuyAmount(e),o=(await this.fetchTokenSpotPrice("GALA"))[0];if(!o)throw Kt("GALA price not available");const s=Number(t.amount);if(s<=0)throw new f(`Invalid token amount calculation: ${s}`,"amount","INVALID_CALCULATION");const i=o.price/s;return{symbol:a.toUpperCase(),price:i}}catch(e){if(e instanceof Error)throw new Error(`Failed to calculate launchpad token spot price for ${a}: ${e.message}`);throw new Error(`Failed to calculate launchpad token spot price for ${a}: ${String(e)}`)}}warmCacheFromPoolData(e,t){this.metadataCache.warmFromPoolData(e,t)}getCacheStats(){return this.metadataCache.stats()}clearCache(e){this.metadataCache.clear(e)}}const Dn={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-test1.defi.gala.com"}};function Pn(e){return Dn[e]}class _n extends Error{constructor(e,t){super(e),this.cause=t,this.name="WebSocketError"}}class Ln 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 On(e,t){if(!e)throw new _n(`Invalid WebSocket response received for transaction ${t}: response is null or undefined`);if("object"!=typeof e)throw new _n(`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 _n(`Invalid WebSocket response received for transaction ${t}: missing status field`)}function Un(e,t,a,n){On(e,t);const r=e,o=r.data||{};if(!function(e){if(!e||"object"!=typeof e)return!1;const t=e;return!(void 0!==t.inputQuantity&&"string"!=typeof t.inputQuantity||void 0!==t.outputQuantity&&"string"!=typeof t.outputQuantity||void 0!==t.totalFees&&"string"!=typeof t.totalFees||void 0!==t.vaultAddress&&"string"!=typeof t.vaultAddress)}(o))throw new _n(`Invalid trade data received for transaction ${t}`);const s={transactionId:t,type:a,method:"native"===n.type?"native":"exact",inputAmount:o.inputQuantity||n.amount,outputAmount:o.outputQuantity||n.expectedAmount||"0",totalFees:o.totalFees||"0",tokenName:n.tokenName,vaultAddress:o.vaultAddress||"",timestamp:Date.now()};return void 0!==r.blockHash&&(s.blockHash=r.blockHash),void 0!==r.gasUsed&&(s.gasUsed=r.gasUsed),void 0!==n.slippageToleranceFactor&&(s.slippageTolerance=n.slippageToleranceFactor),s}let $n="";const Bn=function(){if($n)return $n;try{const e=require("../../package.json");$n=e.version||"unknown"}catch{$n="unknown"}return $n}();class Mn{constructor(e){let t=null;t=e.env?Pn(e.env):e.baseUrl?.includes("prod")?Pn("PROD"):Pn("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 d({debug:this.config.debug??!1,context:"LaunchpadSDK"}),this.validateConfiguration(),this.slippageToleranceFactor=void 0===e.slippageToleranceFactor?Mn.DEFAULT_SLIPPAGE_TOLERANCE_FACTOR:this.parseSlippageToleranceFactor(e.slippageToleranceFactor),this.maxAcceptableReverseBondingCurveFeeSlippageFactor=void 0===e.maxAcceptableReverseBondingCurveFeeSlippageFactor?Mn.DEFAULT_MAX_ACCEPTABLE_REVERSE_BONDING_CURVE_FEE_SLIPPAGE_FACTOR:this.parseFeeSlippageFactor(e.maxAcceptableReverseBondingCurveFeeSlippageFactor),this.calculateAmountMode=e.calculateAmountMode||Mn.DEFAULT_CALCULATE_AMOUNT_MODE,this.auth=new u({wallet:e.wallet,messagePrefix:"Create a GalaChain Wallet"}),this.http=new p(this.auth,this.config),this.galaChainHttp=new p(this.auth,{...this.config,baseUrl:this.config.galaChainBaseUrl}),this.bundleHttp=new p(this.auth,{...this.config,baseUrl:this.config.bundleBaseUrl}),this.dexApiHttp=new p(this.auth,{...this.config,baseUrl:this.config.dexApiBaseUrl}),this.launchpadService=new Ca(this.http),this.tokenResolverService=new yn(this.launchpadService.poolService),this.launchpadAPI=new xn(this.http,this.tokenResolverService,this.logger,this.bundleHttp,this.galaChainHttp,this.dexApiHttp,this.calculateAmountMode),this.galaChainService=new Va(this.galaChainHttp,e.wallet,this.tokenResolverService,e.debug||!1),this.dexService=new qa(this.dexApiHttp),this.bundleService=new gn(this.bundleHttp,this.tokenResolverService,this.config.debug||!1,e.wallet,this.getAddress(),this.slippageToleranceFactor,this.maxAcceptableReverseBondingCurveFeeSlippageFactor),this.websocketService=new fn({url:this.config.webSocketUrl},this.config.debug)}createOverrideSdk(e){if(!e||"string"!=typeof e)throw Gt("Invalid privateKey: must be a non-empty string","privateKey");if(!e.match(/^0x[a-fA-F0-9]{64}$/))throw Gt('Invalid privateKey format: must be "0x" followed by 64 hexadecimal characters',"privateKey");const t=new a.Wallet(e),n={...this.config,wallet:t};return new Mn(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}}getVersion(){return Bn}getUrlByTokenName(e){const t=this.config.launchpadFrontendUrl;if(!t)throw Gt("launchpadFrontendUrl not configured in SDK","launchpadFrontendUrl");return`${t.replace(/\/$/,"")}/buy-sell/${e}`}async fetchPools(e){const t=await this.launchpadService.fetchPools(e||{});return await this.warmCacheFromPools(t.pools),t}async fetchAllPools(e){const t=await this.launchpadService.fetchAllPools(e);return await this.warmCacheFromPools(t.pools),t}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 Kt("Failed to fetch GALA price - no price data returned");const t=e.find(e=>"GALA"===e.symbol);if(!t)throw Kt("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(m(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.tokenName=e,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 $t}),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 $t}),a=t(e.address);if(e.tokenId){const{normalizeToTokenInstanceKey:t}=await Promise.resolve().then(function(){return $a}),n=t(e.tokenId),{collection:r,category:o,type:s,additionalKey:i}=n;return this.galaChainService.fetchTokenBalance({owner:a,collection:r,category:o,additionalKey:i,type:s,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 Mt("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,calculateAmountMode:o,currentSupply:s}=e;let i=t;void 0===o&&void 0===s||(i={tokenName:t,...void 0!==o&&{calculateAmountMode:o},...void 0!==s&&{currentSupply:s}});const c=await this.calculateBuyAmountForGraduation(i),l={tokenName:t,amount:c.amount,type:"exact",expectedAmount:c.amount,maxAcceptableReverseBondingCurveFee:c.reverseBondingCurveFee,slippageToleranceFactor:this.slippageToleranceFactor};return void 0!==a&&(l.slippageToleranceFactor=a),void 0!==n&&(l.maxAcceptableReverseBondingCurveFeeSlippageFactor=n),void 0!==r&&(l.privateKey=r),await this.buy(l)}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)).data,a=t?.transactionId;if(!a)throw zt("No transaction ID returned from buy operation");return this.waitForConfirmation(a,t=>Un(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)).data,a=t?.transactionId;if(!a)throw zt("No transaction ID returned from sell operation");return this.waitForConfirmation(a,t=>Un(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=>{On(a,t);const n=a?.data||{};if(!function(e){if(!e||"object"!=typeof e)return!1;const t=e;return!(void 0!==t.vaultAddress&&"string"!=typeof t.vaultAddress||void 0!==t.tokenStringKey&&"string"!=typeof t.tokenStringKey||void 0!==t.creatorAddress&&"string"!=typeof t.creatorAddress)}(n))throw new _n(`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 $t}),a=t(e)||this.getAddress();return this.launchpadService.fetchProfile(a)}async updateProfile(e){const{normalizeAddressInput:t}=await Promise.resolve().then(function(){return $t}),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 $t}),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 $t}),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 $t}),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 $t}),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 $t}),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 $t}),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)}getCacheInfo(){return this.launchpadAPI.getCacheStats()}clearCache(e){this.launchpadAPI.clearCache(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 Gt("baseUrl is required in configuration","baseUrl");if(!this.config.webSocketUrl)throw Gt("webSocketUrl is required in configuration","webSocketUrl");try{new URL(this.config.baseUrl)}catch{throw Gt(`Invalid baseUrl format: ${this.config.baseUrl}`,"baseUrl")}try{new URL(this.config.webSocketUrl)}catch{throw Gt(`Invalid webSocketUrl format: ${this.config.webSocketUrl}`,"webSocketUrl")}if(this.config.galaChainBaseUrl)try{new URL(this.config.galaChainBaseUrl)}catch{throw Gt(`Invalid galaChainBaseUrl format: ${this.config.galaChainBaseUrl}`,"galaChainBaseUrl")}if(this.config.bundleBaseUrl)try{new URL(this.config.bundleBaseUrl)}catch{throw Gt(`Invalid bundleBaseUrl format: ${this.config.bundleBaseUrl}`,"bundleBaseUrl")}if(this.config.launchpadFrontendUrl)try{new URL(this.config.launchpadFrontendUrl)}catch{throw Gt(`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: ${Mn.DEFAULT_SLIPPAGE_TOLERANCE_FACTOR}`),Mn.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: ${Mn.DEFAULT_MAX_ACCEPTABLE_REVERSE_BONDING_CURVE_FEE_SLIPPAGE_FACTOR}`),Mn.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 Ln(e,a.status,a.message);let n;try{n=t(a)}catch(t){if(t instanceof _n)throw t;throw new _n(`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 Ln||t instanceof _n)throw t;throw new _n(`WebSocket confirmation failed for transaction ${e}`,t instanceof Error?t:new Error(String(t)))}}async warmCacheFromPools(e){if(!e||!Array.isArray(e))return;const{extractMetadataFromPoolData:t,isValidPoolForCaching:a}=await Promise.resolve().then(function(){return Gn});e.forEach(e=>{if(!a(e))return;const n=t(e,this.logger);n&&this.launchpadAPI.warmCacheFromPoolData(e.tokenName,n)})}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 d({debug:e,context:"LaunchpadSDK"});t.debug("Starting global cleanup...");const{WebSocketService:a}=require("./services/WebSocketService");a.cleanupAll(e),t.debug("Global cleanup completed")}}Mn.DEFAULT_SLIPPAGE_TOLERANCE_FACTOR=.15,Mn.DEFAULT_MAX_ACCEPTABLE_REVERSE_BONDING_CURVE_FEE_SLIPPAGE_FACTOR=.01,Mn.DEFAULT_LAUNCHPAD_TOKEN_MAX_SUPPLY=vn.DEFAULT_LAUNCHPAD_TOKEN_MAX_SUPPLY,Mn.DEFAULT_CALCULATE_AMOUNT_MODE=Fn.LOCAL;class Rn{static generateWallet(){try{const e=a.Wallet.createRandom();if(!e.mnemonic?.phrase)throw new Error("Failed to generate wallet with mnemonic phrase");const t=this.toGalaAddress(e.address);return{privateKey:e.privateKey,address:e.address,galaAddress:t,mnemonic:e.mnemonic.phrase,wallet:new a.Wallet(e.privateKey)}}catch(e){if("undefined"!=typeof process&&"test"===process.env.NODE_ENV){const e=`test-wallet-${Date.now()}-${++this.testCounter}`,t="0x"+Buffer.from(e).toString("hex").padStart(64,"1").slice(0,64),n=new a.Wallet(t),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 t=new a.Wallet(e),n=this.toGalaAddress(t.address);return{privateKey:t.privateKey,address:t.address,galaAddress:n,mnemonic:"",wallet:t}}static fromMnemonic(e,t=0){try{const n=a.Mnemonic.fromPhrase(e),r=a.HDNodeWallet.fromMnemonic(n,`m/44'/60'/0'/0/${t}`),o=new a.Wallet(r.privateKey),s=this.toGalaAddress(o.address);return{privateKey:o.privateKey,address:o.address,galaAddress:s,mnemonic:e,wallet:o}}catch(n){if("undefined"!=typeof process&&"test"===process.env.NODE_ENV){const n=`test-mnemonic-index-${t}-${e}`,r="0x"+Buffer.from(n).toString("hex").padStart(64,"1").slice(0,64),o=new a.Wallet(r),s=this.toGalaAddress(o.address);return{privateKey:o.privateKey,address:o.address,galaAddress:s,mnemonic:e,wallet:o}}throw n}}static toGalaAddress(e){const t=e.replace(/^0x/i,"");if(!/^[a-fA-F0-9]{40}$/.test(t))throw new Error(`Invalid Ethereum address format: ${e}`);return`eth|${t}`}static toEthereumAddress(e){if(!e.startsWith("eth|"))throw new Error(`Invalid Gala address format: ${e}. Must start with 'eth|'`);const t=e.slice(4);if(!/^[a-fA-F0-9]{40}$/.test(t))throw new Error(`Invalid address in Gala format: ${e}`);return`0x${t}`}static isValidEthereumAddress(e){try{const t=e.replace(/^0x/i,"");return/^[a-fA-F0-9]{40}$/.test(t)}catch{return!1}}static isValidGalaAddress(e){try{if(!e.startsWith("eth|"))return!1;const t=e.slice(4);return/^[a-fA-F0-9]{40}$/.test(t)}catch{return!1}}static generateMultipleWallets(e=1){if(e<1||e>100)throw new Error("Count must be between 1 and 100");const t=[];if("undefined"!=typeof process&&"test"===process.env.NODE_ENV)for(let 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 Kn(e){if(void 0===e)return Rn.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 Rn.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 Rn.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?"...":""}"`)}Rn.testCounter=0;var Gn=Object.freeze({__proto__:null,extractMetadataFromPoolData:function(e,t){const a={};if(e.vaultAddress&&(a.vaultAddress=e.vaultAddress),void 0!==e.reverseBondingCurveMinFeePortion){const n=parseFloat(e.reverseBondingCurveMinFeePortion);isNaN(n)?t&&t.debug(`Skipping invalid reverseBondingCurveMinFeePortion for ${e.tokenName}: "${e.reverseBondingCurveMinFeePortion}"`):a.reverseBondingCurveMinFeeFactor=n}if(void 0!==e.reverseBondingCurveMaxFeePortion){const n=parseFloat(e.reverseBondingCurveMaxFeePortion);isNaN(n)?t&&t.debug(`Skipping invalid reverseBondingCurveMaxFeePortion for ${e.tokenName}: "${e.reverseBondingCurveMaxFeePortion}"`):a.reverseBondingCurveMaxFeeFactor=n}return void 0!==a.reverseBondingCurveMaxFeeFactor&&void 0!==a.reverseBondingCurveMinFeeFactor&&(a.reverseBondingCurveNetFeeFactor=a.reverseBondingCurveMaxFeeFactor-a.reverseBondingCurveMinFeeFactor),Object.keys(a).length>0?a:null},isValidPoolForCaching:function(e){if(null===e||"object"!=typeof e)return!1;const t=e;return"tokenName"in t&&"string"==typeof t.tokenName&&t.tokenName.length>0}});exports.AgentConfig=class{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 Mn(n),o={sdk:r,wallet:a,config:n};if(!1!==e.autoValidate){const e=await this.validateSetup(r,a);return{...o,validation:e}}return o}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||Rn.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?Rn.fromPrivateKey(e):Rn.generateWallet()}return"generate"===e?Rn.generateWallet():Rn.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}},exports.CALCULATION_MODES=Fn,exports.FileValidationError=Sa,exports.GALA_DECIMALS=8,exports.GALA_TOKEN_CLASS_KEY={collection:"GALA",category:"Unit",type:"none",additionalKey:"none"},exports.IMAGE_EXTENSIONS=he,exports.LAUNCHPAD_TOKEN_DECIMALS=18,exports.LaunchpadSDK=Mn,exports.MAX_CONCURRENT_POOL_FETCHES=5,exports.POOL_TYPES={RECENT:"recent",POPULAR:"popular"},exports.SDK_VERSION=Bn,exports.TRADING_TYPES=In,exports.TransactionFailedError=Ln,exports.ValidationError=f,exports.WebSocketError=_n,exports.WebSocketTimeoutError=class extends _n{constructor(e,t){super(`WebSocket confirmation timeout for transaction ${e} after ${t}ms`),this.name="WebSocketTimeoutError"}},exports.addressFormatSchema=D,exports.amountMethodSchema=le,exports.amountTypeSchema=ce,exports.browserFileSchema=Te,exports.bufferFileSchema=ge,exports.buyTokensDataSchema=Ve,exports.calculatePreMintDataSchema=Ye,exports.checkPoolOptionsSchema=ie,exports.commentMessageSchema=H,exports.commentPaginationSchema=Ce,exports.createLaunchpadSDK=function(e){const{wallet:t,env:n,config:r={}}=e||{};let o;if(t)if("string"==typeof t){o=Kn(t).wallet}else{if(!(t instanceof a.Wallet))throw new Error("Invalid wallet input. Expected string (private key or mnemonic) or Wallet instance.");o=t}else{o=Kn().wallet}const s={wallet:o,...n&&{env:n},debug:!1,timeout:3e4,...r};return new Mn(s)},exports.createLimitSchema=K,exports.createPaginatedResultSchema=function(e){return o.z.object({data:o.z.array(e),page:o.z.number().int().min(1),limit:o.z.number().int().min(1),total:o.z.number().int().min(0),totalPages:o.z.number().int().min(0),hasNext:o.z.boolean(),hasPrevious:o.z.boolean()})},exports.createTradeDataSchema=ze,exports.createWallet=Kn,exports.ethereumAddressSchema=P,exports.faucetAmountSchema=$,exports.fetchGalaBalanceOptionsSchema=$e,exports.fetchPoolDetailsDataSchema=Ze,exports.fetchTokenBalanceOptionsSchema=Re,exports.fileSizeSchema=q,exports.fileUploadSchema=Ne,exports.filenameSchema=j,exports.flexibleAddressSchema=_,exports.flexibleFileSchema=ke,exports.formatGalaForDTO=Wa,exports.formatLaunchpadTokenForDTO=Ha,exports.fullNameSchema=x,exports.getAmountOptionsSchema=Xe,exports.getBrowserFileSchema=ve,exports.getFileUploadSchema=we,exports.getFlexibleFileSchema=Ae,exports.getTradeOptionsSchema=je,exports.graduateTokenOptionsSchema=de,exports.graphDataOptionsSchema=ue,exports.imageExtensionSchema=Ee,exports.imageFilenameSchema=Se,exports.imageMimeTypeSchema=W,exports.imageUploadOptionsSchema=oe,exports.isoDateStringSchema=Q,exports.launchTokenDataSchema=re,exports.nonNegativeDecimalStringSchema=U,exports.optionalUrlSchema=M,exports.pageNumberSchema=R,exports.paginationResultMetaSchema=_e,exports.poolFetchTypeSchema=se,exports.poolPaginationSchema=xe,exports.positiveDecimalStringSchema=O,exports.privateKeySchema=Y,exports.reverseBondingCurveConfigSchema=ne,exports.reverseBondingCurveConfigurationSchema=Je,exports.searchQuerySchema=C,exports.sellTokensDataSchema=qe,exports.standardLimitSchema=G,exports.standardPaginationSchema=be,exports.timestampSchema=X,exports.tokenCategorySchema=te,exports.tokenCollectionSchema=ae,exports.tokenDescriptionSchema=I,exports.tokenListOptionsSchema=Oe,exports.tokenNameSchema=S,exports.tokenSymbolSchema=b,exports.tokenUrlsSchema=ee,exports.tradeCalculationMethodSchema=Qe,exports.tradeCalculationTypeSchema=He,exports.tradeLimitSchema=V,exports.tradeListParamsSchema=We,exports.tradePaginationSchema=Fe,exports.tradePaginationWithFiltersSchema=Pe,exports.tradeTypeBackendSchema=Ge,exports.tradeTypeSchema=Ke,exports.transactionIdSchema=Z,exports.transferFaucetsDataSchema=Ue,exports.uniqueKeySchema=J,exports.updateProfileDataSchema=Be,exports.uploadProfileImageOptionsSchema=Me,exports.urlSchema=B,exports.userLimitSchema=z,exports.userPaginationSchema=Ie,exports.userTokenNameSchema=F,exports.userTokenTypeSchema=Le,exports.userTokensPaginationSchema=De,exports.validateAddress=rt,exports.validateAmountString=st,exports.validateBuyTokensData=kt,exports.validateCalculatePreMintData=It,exports.validateCheckPoolOptions=gt,exports.validateCreateTradeData=Tt,exports.validateFaucetAmount=it,exports.validateFetchGalaBalanceOptions=yt,exports.validateFetchPoolDetailsData=Ft,exports.validateFetchTokenBalanceOptions=wt,exports.validateFullName=ct,exports.validateGetAmountOptions=bt,exports.validateGetTradeOptions=Et,exports.validateImageUploadOptions=pt,exports.validateLaunchTokenData=dt,exports.validateSearchQuery=lt,exports.validateSellTokensData=Nt,exports.validateTokenDescription=nt,exports.validateTokenListOptions=mt,exports.validateTokenName=tt,exports.validateTokenSymbol=at,exports.validateTokenUrls=ht,exports.validateTradeListParams=St,exports.validateTransferFaucetsData=ft,exports.validateUpdateProfileData=vt,exports.validateUploadProfileImageOptions=At,exports.validateUserTokenName=ut,exports.validateVaultAddress=ot,exports.vaultAddressSchema=L;
|