@gala-chain/launchpad-sdk 3.29.0 → 3.31.2

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.
Files changed (53) hide show
  1. package/CHANGELOG.md +35 -7
  2. package/README.md +105 -0
  3. package/dist/LaunchpadSDK.d.ts +8 -0
  4. package/dist/LaunchpadSDK.d.ts.map +1 -1
  5. package/dist/api/dto/TransferTokenDto.d.ts.map +1 -1
  6. package/dist/constants/version.generated.d.ts +1 -1
  7. package/dist/helpers/sdk.d.ts +1 -0
  8. package/dist/helpers/sdk.d.ts.map +1 -1
  9. package/dist/index.cjs.js +1 -1
  10. package/dist/index.esm.js +1 -1
  11. package/dist/index.js +1 -1
  12. package/dist/services/BaseService.d.ts +32 -3
  13. package/dist/services/BaseService.d.ts.map +1 -1
  14. package/dist/services/BundleService.d.ts.map +1 -1
  15. package/dist/services/CommentService.d.ts.map +1 -1
  16. package/dist/services/DexPoolService.d.ts.map +1 -1
  17. package/dist/services/DexService.d.ts.map +1 -1
  18. package/dist/services/FaucetService.d.ts.map +1 -1
  19. package/dist/services/GSwapService.d.ts +57 -4
  20. package/dist/services/GSwapService.d.ts.map +1 -1
  21. package/dist/services/GalaChainService.d.ts.map +1 -1
  22. package/dist/services/ImageService.d.ts.map +1 -1
  23. package/dist/services/PoolService.d.ts.map +1 -1
  24. package/dist/services/PriceHistoryService.d.ts +4 -5
  25. package/dist/services/PriceHistoryService.d.ts.map +1 -1
  26. package/dist/services/SignatureService.d.ts +2 -3
  27. package/dist/services/SignatureService.d.ts.map +1 -1
  28. package/dist/services/TokenClassKeyService.d.ts +3 -4
  29. package/dist/services/TokenClassKeyService.d.ts.map +1 -1
  30. package/dist/services/TokenMetadataCache.d.ts +2 -2
  31. package/dist/services/TokenMetadataCache.d.ts.map +1 -1
  32. package/dist/services/TokenMetadataService.d.ts +3 -3
  33. package/dist/services/TokenMetadataService.d.ts.map +1 -1
  34. package/dist/services/TokenResolverService.d.ts +2 -2
  35. package/dist/services/TokenResolverService.d.ts.map +1 -1
  36. package/dist/services/TradeService.d.ts.map +1 -1
  37. package/dist/services/UserService.d.ts.map +1 -1
  38. package/dist/services/WebSocketService.d.ts +2 -2
  39. package/dist/services/WebSocketService.d.ts.map +1 -1
  40. package/dist/utils/SignatureHelper.d.ts.map +1 -1
  41. package/dist/utils/auto-pagination.d.ts +132 -0
  42. package/dist/utils/auto-pagination.d.ts.map +1 -0
  43. package/dist/utils/bignumber-helpers.d.ts +264 -0
  44. package/dist/utils/bignumber-helpers.d.ts.map +1 -0
  45. package/dist/utils/token-format-converter.d.ts +75 -3
  46. package/dist/utils/token-format-converter.d.ts.map +1 -1
  47. package/dist/utils/tokenNormalizer.d.ts +66 -0
  48. package/dist/utils/tokenNormalizer.d.ts.map +1 -1
  49. package/dist/utils/validation-helpers.d.ts +75 -0
  50. package/dist/utils/validation-helpers.d.ts.map +1 -1
  51. package/package.json +2 -2
  52. package/dist/utils/tokenFormatConverter.d.ts +0 -53
  53. package/dist/utils/tokenFormatConverter.d.ts.map +0 -1
package/dist/index.cjs.js CHANGED
@@ -1 +1 @@
1
- "use strict";var e,t,a=require("axios"),n=require("ethers"),o=require("@gala-chain/connect"),r=require("zod"),i=require("bignumber.js"),s=require("@gala-chain/api"),c=require("uuid"),l=require("socket.io-client"),u=require("@gala-chain/gswap-sdk");if("undefined"==typeof File){const{File:e}=require("web-file-polyfill");global.File=e}!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 d extends Error{constructor(e,t,a){super(t),this.type=e,this.originalError=a,this.name="AuthError"}}class h{constructor(t){if(this.wallet=t.wallet,this.messagePrefix=t.messagePrefix||"Create a GalaChain Wallet",""===t.messagePrefix)throw new d(e.SIGNATURE_FAILED,"Message prefix cannot be empty")}hasWallet(){return void 0!==this.wallet}setWallet(t){if(void 0!==t){if("object"!=typeof t||!("address"in t))throw new d(e.WALLET_NOT_CONNECTED,"Invalid wallet: must be an ethers Wallet instance or undefined");if(!t.address||"string"!=typeof t.address)throw new d(e.INVALID_ADDRESS,"Wallet address is not available")}this.wallet=t}async generateSignature(){this.validateWallet();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){if(t instanceof d)throw t;throw new d(e.SIGNATURE_FAILED,"Failed to generate signature for authentication",t instanceof Error?t:new Error(String(t)))}}getAddress(){return this.validateWallet(),this.formatAddress(this.wallet.address)}getEthereumAddress(){return this.validateWallet(),this.wallet.address}getPrivateKey(){if(this.validateWallet(),!this.wallet.privateKey)throw new d(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 d(e.INVALID_ADDRESS,`Invalid Ethereum address format: ${t}`);return`eth|${a}`}async signMessage(t){this.validateWallet();try{return{message:t,signature:await this.wallet.signMessage(t),address:this.wallet.address,timestamp:Date.now()}}catch(t){if(t instanceof d)throw t;const a=t instanceof Error?t.message:String(t);throw new d(e.SIGNATURE_FAILED,a,t instanceof Error?t:new Error(String(t)))}}async generateAuthHeaders(t,a){this.validateWallet();try{const e=Date.now(),n=`${this.messagePrefix} ${a.toUpperCase()} ${t} ${e}`,o=await this.wallet.signMessage(n);return{"x-signature":o,"x-address":this.formatAddress(this.wallet.address),"x-message":n,"x-timestamp":e.toString()}}catch(t){if(t instanceof d)throw t;throw new d(e.SIGNATURE_FAILED,"Failed to generate authentication headers",t instanceof Error?t:new Error(String(t)))}}async signTypedData(t,a,n){this.validateWallet();try{return await this.wallet.signTypedData(t,a,n)}catch(t){if(t instanceof d)throw t;throw new d(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 d(e.SIGNATURE_FAILED,"Custom message must be a non-empty string");this.validateWallet();try{const e=await this.wallet.signMessage(t);return{message:t,signature:e,address:this.formatAddress(this.wallet.address),timestamp:Date.now()}}catch(t){if(t instanceof d)throw t;throw new d(e.SIGNATURE_FAILED,"Failed to generate custom message signature",t instanceof Error?t:new Error(String(t)))}}validateWallet(){if(!this.wallet)throw new d(e.WALLET_NOT_CONNECTED,"Wallet is required for authentication");if(!this.wallet.address)throw new d(e.WALLET_NOT_CONNECTED,"Wallet address is not available");if(!this.wallet.privateKey&&!this.wallet.signMessage)throw new d(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 g{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 o=`[${(new Date).toISOString()}] [${this.context}] [${e}]`,r=this.getConsoleMethod(e);void 0!==n?n instanceof Error?r(`${o} ${a}`,n.message,n.stack):r(`${o} ${a}`,n):r(`${o} ${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 g({debug:this.debugEnabled,context:`${this.context}:${e}`,minLevel:this.minLevel})}isDebugEnabled(){return this.debugEnabled&&this.levelPriority[t.DEBUG]>=this.levelPriority[this.minLevel]}}function p(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 m{constructor(e,t={}){this.auth=e,this.debug=t.debug??!1,this.logger=new g({debug:this.debug,context:"HttpClient"}),this.axios=a.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:p(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=o.SigningType.SIGN_TYPED_DATA){const n=this.auth.getPrivateKey(),r=new o.SigningClient(n);return await r.sign(e,t,a)}setupInterceptors(){this.requestInterceptorId=this.axios.interceptors.request.use(async e=>{try{if(e.headers||(e.headers={}),this.auth.hasWallet()){const t=await this.auth.generateSignature();e.headers.Sign=t.signature,this.logger.debug("Added signature header:",{address:t.address,message:t.message,timestamp:t.timestamp})}else this.logger.debug("No wallet configured - skipping signature header");return e.data instanceof FormData||(e.headers["Content-Type"]="application/json"),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 f="Token name is required and must be a string",y=e=>`Could not find vault address for token: ${e}`;class w extends Error{constructor(e,t,a){super(e),this.field=t,this.code=a,this.name="ValidationError"}}class k 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"}}class T extends Error{constructor(e,t,a){super(e),this.originalError=t,this.code=a,this.name="GSwapQuoteError"}}class S extends Error{constructor(e,t,a,n){super(e),this.originalError=t,this.transactionHash=a,this.code=n,this.name="GSwapSwapError"}}class E extends Error{constructor(e,t,a,n,o){super(e),this.originalError=t,this.tokenA=a,this.tokenB=n,this.code=o,this.name="GSwapPoolError"}}class b extends Error{constructor(e,t,a,n){super(e),this.originalError=t,this.walletAddress=a,this.code=n,this.name="GSwapAssetError"}}class N extends Error{constructor(e,t,a){super(e),this.originalError=t,this.code=a,this.name="GSwapPositionError"}}function I(e,t,a){const{MIN_PAGE:n,MAX_PAGE:o,MIN_LIMIT:r,MAX_LIMIT:i}=a.PAGINATION;if("number"!=typeof e||e<n||e>o)throw new w(`Page must be a number between ${n} and ${o}`,"page","INVALID_PAGE");if("number"!=typeof t||t<r||t>i)throw new w(`Limit must be a number between ${r} and ${i}`,"limit","INVALID_LIMIT")}const F={ETH_ADDRESS:/^0x[0-9a-fA-F]{40}$/,BACKEND_ADDRESS:/^eth\|[0-9a-fA-F]{40}$/};function x(e){return"string"==typeof e&&e.trim().length>0}function C(e){return!(!e||"string"!=typeof e)&&(F.ETH_ADDRESS.test(e)||F.BACKEND_ADDRESS.test(e))}function D(e){return e.startsWith("0x")?`eth|${e.slice(2)}`:e}const P=r.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"),L=r.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"),$=r.z.string().min(1,"Token description is required").max(500,"Token description must be at most 500 characters"),_=r.z.string().min(1,"Token name must be at least 1 character").max(50,"Token name must be at most 50 characters"),U=r.z.string().min(1,"Search query must be at least 1 character").max(100,"Search query must be at most 100 characters"),B=r.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"),M=r.z.string().regex(F.BACKEND_ADDRESS,"Address must be in format: eth|[40-hex-chars]"),O=r.z.string().regex(F.ETH_ADDRESS,"Invalid Ethereum address format"),R=r.z.string().refine(e=>F.BACKEND_ADDRESS.test(e)||F.ETH_ADDRESS.test(e),"Address must be either eth|[40-hex-chars] or 0x[40-hex-chars] format").transform(e=>e.startsWith("0x")?`eth|${e.slice(2)}`:e),G=r.z.string().refine(e=>F.BACKEND_ADDRESS.test(e)||/^service\|Token\$Unit\$[A-Z0-9]+\$eth:[0-9a-fA-F]{40}\$launchpad$/.test(e),"Invalid vault address format"),K=r.z.string().regex(/^\d+(\.\d+)?$/,"Must be a valid decimal number").refine(e=>parseFloat(e)>0,"Amount must be greater than zero"),q=r.z.string().regex(/^\d+(\.\d+)?$/,"Must be a valid decimal number").refine(e=>parseFloat(e)>=0,"Amount must be zero or greater"),z=r.z.string().regex(/^(?!0+(\.0+)?$)\d+(\.\d+)?$/,"Amount must be a positive, non-zero number"),V=r.z.string().url("Must be a valid URL").regex(/^https?:\/\//,"URL must start with http:// or https://"),W=r.z.string().optional().refine(e=>!e||/^https?:\/\/.+\..+/.test(e),"Must be a valid URL if provided"),j=r.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 H(e=100){return r.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 X=H(100),Q=H(20),Y=H(20),Z=r.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=r.z.string().max(255,"Filename must be at most 255 characters"),ee=r.z.enum(["image/png","image/jpg","image/jpeg","image/gif","image/webp","image/svg+xml"]),te=r.z.string().min(1,"Comment message is required").max(500,"Comment must be at most 500 characters"),ae=r.z.string().datetime("Must be a valid ISO 8601 date string"),ne=r.z.number().int("Timestamp must be an integer").min(0,"Timestamp must be non-negative"),oe=r.z.string().regex(/^0x[a-fA-F0-9]{64}$/,"Private key must be format: 0x + 64 hex characters"),re=r.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"),ie=r.z.string().regex(/^galaconnect-operation-[a-z0-9-]+$/,"Unique key must be format: galaconnect-operation-{unique-id}"),se=r.z.object({websiteUrl:W,telegramUrl:W,twitterUrl:W}).refine(e=>e.websiteUrl||e.telegramUrl||e.twitterUrl,"At least one social URL (website, telegram, or twitter) is required"),ce=r.z.string().min(1,"Token category must not be empty").default("Unit"),le=r.z.string().min(1,"Token collection must not be empty").default("Token"),ue=r.z.object({minFeePortion:K,maxFeePortion:K}),de=r.z.object({tokenName:P,tokenSymbol:L,tokenDescription:$,tokenImage:r.z.union([r.z.instanceof(File),r.z.instanceof(Buffer),r.z.string().url("Token image must be a valid URL")]).optional(),preBuyQuantity:q.default("0"),websiteUrl:W,telegramUrl:W,twitterUrl:W,tokenCategory:ce,tokenCollection:le,reverseBondingCurveConfiguration:ue.optional(),privateKey:oe.optional()}),he=r.z.object({file:r.z.union([r.z.instanceof(File),r.z.instanceof(Buffer)]),tokenName:P}),ge=r.z.enum(["recent","popular"]),pe=r.z.object({tokenName:P.optional(),symbol:L.optional()}).refine(e=>e.tokenName||e.symbol,"At least one of tokenName or symbol is required"),me=r.z.enum(["NATIVE","MEME"]),fe=r.z.enum(["IN","OUT"]),ye=r.z.object({from:r.z.number().int("From timestamp must be an integer").min(173e6,"From timestamp must be at least 173000000"),to:r.z.number().int("To timestamp must be an integer").min(173e6,"To timestamp must be at least 173000000"),resolution:r.z.number().int("Resolution must be an integer").min(1,"Resolution must be at least 1"),tokenName:P}),we=r.z.object({tokenName:P,slippageToleranceFactor:r.z.number().min(0).max(1).optional(),maxAcceptableReverseBondingCurveFeeSlippageFactor:r.z.number().min(0).max(1).optional(),privateKey:oe.optional()}),ke=[".png",".jpg",".jpeg",".gif",".webp",".svg"],ve=r.z.object({file:r.z.union([r.z.instanceof(File),r.z.instanceof(Buffer)]),name:J,size:Z,type:ee}),Ae=r.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"),Te=r.z.instanceof(Buffer).refine(e=>e.length>=1&&e.length<=10485760,"Buffer size must be between 1 byte and 10MB"),Se=r.z.union([Ae,Te]),Ee=r.z.enum([".png",".jpg",".jpeg",".gif",".webp",".svg"]),be=J.refine(e=>{const t=e.slice(e.lastIndexOf(".")).toLowerCase();return ke.includes(t)},`Filename must end with one of: ${ke.join(", ")}`),Ne=r.z.object({page:j,limit:X}),Ie=r.z.object({page:j,limit:Q}),Fe=r.z.object({page:j,limit:Y}),xe=r.z.object({page:j,limit:H(50)}),Ce=Ne.extend({type:r.z.enum(["recent","popular"]).optional(),tokenName:r.z.string().min(1).max(50).optional(),search:r.z.string().min(1).max(100).optional()}),De=Ie.extend({tokenName:r.z.string().min(1).max(50).optional(),search:r.z.string().min(1).max(100).optional()}),Pe=Fe.extend({tradeType:r.z.enum(["BUY","SELL"]).optional(),tokenName:r.z.string().min(1).max(50).optional(),userAddress:r.z.string().regex(/^(0x[a-fA-F0-9]{40}|eth\|[a-fA-F0-9]{40})$/).optional(),startDate:r.z.string().datetime().optional(),endDate:r.z.string().datetime().optional(),sortOrder:r.z.enum(["ASC","DESC"]).default("DESC")}),Le=r.z.object({page:r.z.number().int().min(1),limit:r.z.number().int().min(1),total:r.z.number().int().min(0),totalPages:r.z.number().int().min(0),hasNext:r.z.boolean(),hasPrevious:r.z.boolean()});const $e=r.z.enum(["all","DEFI","ASSET"]),_e=Ie.extend({type:$e.optional(),address:R.optional(),search:U.optional(),tokenName:_.optional()}),Ue=r.z.object({walletAddress:R,amount:z}),Be=r.z.object({address:R.optional(),refresh:r.z.boolean().optional()}),Me=r.z.object({profileImage:r.z.string(),fullName:B,address:R,privateKey:oe.optional()}),Oe=r.z.object({file:r.z.union([r.z.instanceof(File),r.z.instanceof(Buffer)]),address:R.optional(),privateKey:oe.optional()}),Re=r.z.object({address:R,tokenId:r.z.union([r.z.string(),r.z.object({collection:r.z.string(),category:r.z.string(),type:r.z.string(),additionalKey:r.z.string()}),r.z.object({collection:r.z.string(),category:r.z.string(),type:r.z.string(),additionalKey:r.z.string(),instance:r.z.string()})]).optional(),tokenName:_.optional()}).refine(e=>void 0!==e.tokenId||void 0!==e.tokenName,"At least one token identifier (tokenId or tokenName) is required"),Ge=r.z.enum(["buy","sell"]),Ke=r.z.enum(["BUY","SELL"]),qe=r.z.object({tradeType:Ge,tokenAmount:K,vaultAddress:G,userAddress:R,slippageTolerance:K.optional(),deadline:r.z.number().int().positive().optional()}),ze=r.z.object({tokenSymbol:L,nativeTokenQuantity:K,expectedToken:K,maxAcceptableReverseBondingCurveFee:q.default("0").optional()}),Ve=r.z.object({tokenSymbol:L,tokenQuantity:K,expectedNativeToken:K,maxAcceptableReverseBondingCurveFee:q.default("0").optional()}),We=Fe.extend({tokenName:_.optional()}),je=r.z.object({page:r.z.number().int().min(1).max(1e3).default(1).optional(),limit:r.z.number().int().min(1).max(20).default(10).optional()}),He=r.z.enum(["NATIVE","MEME"]),Xe=r.z.enum(["IN","OUT"]),Qe=r.z.object({type:He,method:Xe,vaultAddress:G,amount:K}),Ye=r.z.object({nativeTokenQuantity:K}),Ze=r.z.object({vaultAddress:G}),Je=r.z.object({minFeePortion:K,maxFeePortion:K});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(P),at=et(L),nt=et($),ot=et(R),rt=et(G),it=et(K),st=et(z),ct=et(B),lt=et(U),ut=et(_),dt=et(de),ht=et(se),gt=et(he),pt=et(pe),mt=et(_e),ft=et(Ue),yt=et(Be),wt=et(Me),kt=et(Oe),vt=et(Re),At=et(qe),Tt=et(ze),St=et(Ve),Et=et(We),bt=et(je),Nt=et(Qe),It=et(Ye),Ft=et(Ze);function xt(e,t){throw new w(e.join("; "),t,"VALIDATION_ERROR")}function Ct(e){const t=tt(e);!t.success&&t.errors&&xt(t.errors,"tokenName")}function Dt(e){const t=Ce.safeParse(e);t.success||xt(t.error.errors.map(e=>e.message),"pagination")}function Pt(e){const t=pt(e);!t.success&&t.errors&&xt(t.errors,"options")}function Lt(e){const t=Nt(e);!t.success&&t.errors&&xt(t.errors,"options")}function $t(e){const t=ye.safeParse(e);t.success||xt(t.error.errors.map(e=>e.message),"options")}function _t(e){const t=R.safeParse(e);if(!t.success)throw new w("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 w(`${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 w(`${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 w(`${t} must be a valid numeric string. Received: "${e}"`,t,"INVALID_NUMERIC_STRING");if(!isFinite(n))throw new w(`${t} must be a finite number. Cannot be Infinity or -Infinity.`,t,"INVALID_NUMERIC_STRING");if(n<0)throw new w(`${t} must be non-negative. Received: "${e}"`,t,"INVALID_NUMERIC_STRING");if(!a&&0===n)throw new w(`${t} must be greater than zero. Received: "${e}"`,t,"INVALID_NUMERIC_STRING")}var Bt=Object.freeze({__proto__:null,normalizeAddressInput:function(e){if(!e)return;const t=R.safeParse(e);if(!t.success)throw new w(`Invalid address format: ${e}. Must be either "0x..." (Ethereum) or "eth|..." (GalaChain) format`,"address","INVALID_FORMAT");return t.data},toBackendAddressFormat:_t,validateCheckPoolOptions:Pt,validateGetAmountOptions:Lt,validateGetGraphOptions:$t,validateNumericString:Ut,validatePagination:Dt,validateTokenName:Ct});function Mt(e,t){return new w(`Token "${e}" not found. Please verify the token name is correct.`,"tokenName","TOKEN_NOT_FOUND")}function Ot(e,t){const a=t||e.charAt(0).toUpperCase()+e.slice(1);return new w(`${a} is required`,e,"REQUIRED_FIELD")}function Rt(e,t,a){const n=a||e.charAt(0).toUpperCase()+e.slice(1);return new w(`${n} must be ${t}`,e,"INVALID_FORMAT")}function Gt(e,t,a){return new k(e,t,a)}function Kt(e,t){return new v(e,t)}function qt(e,t,a){return new A(e,t,a)}function zt(e,t){const a=e,n=Number(a.page)||t.page,o=Number(a.limit)||t.limit,r=a.data,i=Number(a.total)||Number(r?.count)||0;return{page:n,limit:o,total:i,totalPages:Math.ceil(i/o)}}function Vt(e,t){return{hasNext:e<t,hasPrevious:e>1}}function Wt(e,t,a=!1){const n=!0===e.error||200!==e.status,o=a&&!e.data;if(n||o)throw new Error(e.message||t)}const jt="/launchpad/upload-image",Ht="/launchpad/fetch-pool",Xt="/launchpad/check-pool",Qt="/launchpad/get-graph-data",Yt="/holders",Zt="/launchpad/get-badge/",Jt="/trade/",ea="/token/commment",ta="/token/commment",aa="/user/profile",na="/user/profile",oa="/user/token-list",ra="/user/token-hold",ia="/user/transfer-faucets",sa={DEFAULT_PAGE:1,DEFAULT_LIMIT:10,BACKEND_MAX_PAGE_SIZE:20,SAFETY_MAX_PAGES:100};class ca{constructor(e,t=!1){this.http=e,this.logger=new g({debug:t,context:this.constructor.name})}}function la(e,t){return"string"==typeof e[t]}function ua(e,t){return void 0===e[t]||"string"==typeof e[t]}function da(e,t){return void 0===e[t]||"number"==typeof e[t]}function ha(e){return void 0===e.calculateAmountMode||"local"===e.calculateAmountMode||"external"===e.calculateAmountMode}function ga(e){if(!e||"object"!=typeof e)return!1;const t=e;return la(t,"tokenName")&&da(t,"from")&&da(t,"to")&&da(t,"resolution")}class pa extends ca{constructor(e,t=!1){super(e,t)}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=p(t),n=await this.http.get(Ht,a);Wt(n,"Failed to fetch pools",!0);const o=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),r=n.data.count??n.data.total??0;return{pools:o,total:r,totalPages:e.limit>0?Math.ceil(r/e.limit):1}}async fetchMultiplePages(e){const{startPage:t,totalPages:a,pageSize:n,...o}=e,r=[];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({...o,page:e,limit:n}));(await Promise.all(a)).forEach(e=>{r.push(...e.pools)})}return r}let i=t,s=!0;for(;s;){const e=[];for(let t=0;t<5&&s;t++)e.push(i+t);const t=e.map(e=>this.fetchSinglePage({...o,page:e,limit:n})),a=await Promise.all(t);for(const e of a){if(0===e.pools.length){s=!1;break}r.push(...e.pools)}i+=e.length,i>sa.SAFETY_MAX_PAGES&&(s=!1)}return r}async fetchPools(e={}){const t=e.page||sa.DEFAULT_PAGE,a=e.limit??sa.DEFAULT_LIMIT;let n;if(0!==a&&Dt({page:t,limit:a}),e.tokenName&&Ct(e.tokenName),"recent"===e.type?n="RECENT":"popular"===e.type&&(n="POPULAR"),a>0&&a<=20){const o=await this.fetchSinglePage({...e.search&&{search:e.search},...e.tokenName&&{tokenName:e.tokenName},...n&&{type:n},page:t,limit:a});return{pools:o.pools,page:t,limit:a,total:o.total,totalPages:o.totalPages,hasNext:t<o.totalPages,hasPrevious:t>1}}if(0===a){const t=sa.BACKEND_MAX_PAGE_SIZE,a=await this.fetchSinglePage({...e.search&&{search:e.search},...e.tokenName&&{tokenName:e.tokenName},...n&&{type:n},page:1,limit:t}),o=[...a.pools];if(a.pools.length>0){const r=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});o.push(...r)}return{pools:o,page:1,limit:o.length,total:a.total||o.length,totalPages:1,hasNext:!1,hasPrevious:!1}}const o=sa.BACKEND_MAX_PAGE_SIZE,r=Math.ceil(a/o),i=await this.fetchSinglePage({...e.search&&{search:e.search},...e.tokenName&&{tokenName:e.tokenName},...n&&{type:n},page:t,limit:o}),s=[...i.pools],c=Math.min(r,i.totalPages-t+1);if(c>1){const a=t+c-1,r=await this.fetchMultiplePages({...e.search&&{search:e.search},...e.tokenName&&{tokenName:e.tokenName},...n&&{type:n},startPage:t+1,totalPages:a,pageSize:o});s.push(...r)}const l=s.slice(0,a);return{pools:l,page:t,limit:a,total:i.total,totalPages:i.totalPages,hasNext:t<i.totalPages&&l.length<i.total,hasPrevious:t>1}}async fetchAllPools(e){return this.fetchPools({...e,limit:0})}async checkPool(e){Pt(e),e.tokenName&&Ct(e.tokenName);const t=p(e),a=await this.http.get(Xt,t);Wt(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(!ga(e))throw new w("Invalid options provided. Expected { tokenName: string, from?: number, to?: number, resolution?: number }","options","INVALID_OPTIONS");const{tokenName:t,from:a,to:n,resolution:o}=e;if(Ct(t),!a||!n||!o)throw new w("Graph options (from, to, resolution) are required","options","MISSING_GRAPH_OPTIONS");const r={tokenName:t,from:a,to:n,resolution:o};$t(r);const i=p(r),s=await this.http.get(Qt,i);return Wt(s,"Failed to fetch graph data",!0),{dataPoints:s.data}}async fetchTokenDistribution(e){if(!e)throw Ot("tokenName","Token name");Ct(e);const t=await this.resolveTokenNameToVault(e);if(!t)throw Mt(e);const a=encodeURIComponent(t),n=await this.http.get(`${Yt}/${a}`);Wt(n,"Failed to fetch token distribution",!0);const o=n.data;if(!Array.isArray(o))throw Gt("Invalid API response: expected array of holders",n.status);for(const e of o){if(!e.owner||"string"!=typeof e.owner)throw Gt("Invalid holder data: missing or invalid owner field",n.status);if(!e.quantity||"string"!=typeof e.quantity)throw Gt("Invalid holder data: missing or invalid quantity field",n.status);const t=parseFloat(e.quantity);if(isNaN(t)||!isFinite(t))throw Gt(`Invalid holder quantity: "${e.quantity}"`,n.status)}const r=o.reduce((e,t)=>e.plus(t.quantity),new i(0));return{holders:o.map(e=>{const t=new i(e.quantity),a=r.isZero()?0:t.dividedBy(r).multipliedBy(100).toNumber();return{address:e.owner,balance:e.quantity,percentage:a}}),totalSupply:r.toFixed(),totalHolders:o.length,lastUpdated:new Date}}async fetchTokenBadges(e){if(!e)throw Ot("tokenName","Token name");Ct(e);const t=await this.http.get(Zt,{tokenName:e});return Wt(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 o=("volume"===a?e.volumeBadges:e.engagementBadges).find(e=>e.badgeName===n);return o?.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 ma(e,t={}){const{stringifyFields:a=[],optionalFields:n=[],fieldMappings:o={}}=t,r={};for(const[t,i]of Object.entries(e)){const e=t;if(n.includes(e)&&void 0===i)continue;if(n.includes(e)&&"string"==typeof i&&0===i.trim().length)continue;const s=o[e],c=s?String(s):t;a.includes(e)?r[c]=String(i):r[c]=i}return p(r)}const fa={PAGINATION:{MIN_PAGE:1,MAX_PAGE:1e3,MIN_LIMIT:1,MAX_LIMIT:20}};class ya extends ca{constructor(e,t=!1){super(e,t)}async fetchTrades(e){if(!function(e){if(!e||"object"!=typeof e)return!1;const t=e;return la(t,"tokenName")&&(void 0===t.tradeType||"buy"===t.tradeType||"sell"===t.tradeType)&&ua(t,"userAddress")&&da(t,"page")&&da(t,"limit")}(e))throw new w("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:o=sa.DEFAULT_PAGE,limit:r=sa.DEFAULT_LIMIT,startDate:i,endDate:s,sortOrder:c}=e;if(!x(t))throw new w("Token name is required and must be a non-empty string","tokenName","INVALID_TOKEN_NAME");I(o,r,fa);const l=function(e,t,a){return ma({tokenName:e,page:t,limit:a},{stringifyFields:["page","limit"]})}(t,o,r),u=await this.http.get(Jt,l),d=(h=u.data)?Array.isArray(h)?h:h.trades:[];var h;const g=zt(u,{page:o,limit:r}),p=Vt(g.page,g.totalPages);return{trades:d,...g,...p}}}const wa=new g({debug:!1,context:"DateUtils"});function ka(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())?(wa.warn(`Invalid date string received: "${e}". Using fallback.`),t||new Date):a}catch(a){return wa.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 extends ca{constructor(e,t,a=!1){super(e,a),this.poolService=t}async fetchComments(e){if(!function(e){if(!e||"object"!=typeof e)return!1;const t=e;return la(t,"tokenName")&&da(t,"page")&&da(t,"limit")}(e))throw new w("Invalid options provided. Expected { tokenName: string, page?: number, limit?: number }","options","INVALID_OPTIONS");const{tokenName:t,page:a=sa.DEFAULT_PAGE,limit:n=sa.DEFAULT_LIMIT}=e;if(!x(t))throw new w("Token name is required and must be a non-empty string","tokenName","INVALID_TOKEN_NAME");I(a,n,va);const o=await this.poolService.resolveTokenNameToVault(t);if(!o)throw Mt(t);const r=ma({vaultAddress:o,page:a,limit:n},{stringifyFields:["page","limit"]}),i=await this.http.get(ea,r);Wt(i,"Failed to fetch comments");return{comments:i.data.comments.map(e=>({...e,createdAt:ka(e.createdAt)})),total:i.data.count}}async postComment(e){if(!function(e){if(!e||"object"!=typeof e)return!1;const t=e;return la(t,"tokenName")&&la(t,"content")}(e))throw new w("Invalid options provided. Expected { tokenName: string, content: string }","options","INVALID_OPTIONS");const{tokenName:t,content:a}=e;if(!x(t))throw new w("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 w(`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 Mt(t);const o={userAddress:this.http.getAddress(),vaultAddress:n,content:a};Wt(await this.http.post(ta,o),"Failed to create comment")}}function Ta(e){return e instanceof Error}function Sa(e){return Ta(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 Ea(e){return"object"==typeof e&&null!==e&&"message"in e&&("response"in e||"request"in e||"config"in e)}function ba(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 o=new Blob([e],{type:"image/png"});n.append(t,o,a)}}return n}const Na={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 Ia(e){return!(!e||"string"!=typeof e)&&Na.USER_ADDRESS.PATTERN.test(e)}const Fa="Update profile";class xa extends ca{constructor(e,t=!1){super(e,t)}async fetchProfile(e){const t=e??this.http.getAddress();if(!Ia(t))throw new w("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(Fa),o={address:n.address,message:Fa,publickey:n.ethereumAddress,sign:n.signature};Wt(await this.http.put(na,a,o),"Profile update failed")}async uploadProfileImage(e){this.validateUploadProfileImageOptions(e);const t=e.address??this.http.getAddress();try{const a=`profile-image-${t}.png`,n=ba(e.file,"image",a),o=await this.http.request({method:"POST",url:`${jt}?tokenName=${encodeURIComponent(t)}`,data:n,headers:{}});return Wt(o,"Image upload failed"),"string"==typeof o.data?o.data:""}catch(e){if(e instanceof w)throw e;throw new w(`Profile image upload failed: ${Sa(e)}`,"file","UPLOAD_FAILED")}}async fetchTokenList(e){return this.buildFetchRequest(oa,e,{includeType:!0,errorMessage:"Failed to fetch token list"})}async fetchTokensHeld(e){return this.buildFetchRequest(ra,e,{includeType:!1,errorMessage:"Failed to fetch tokens held"})}async fetchTokensCreated(e={}){const{page:t=sa.DEFAULT_PAGE,limit:a=sa.DEFAULT_LIMIT,search:n,tokenName:o}=e,r={type:"DEFI",address:this.http.getAddress(),page:t,limit:a};return void 0!==n&&(r.search=n),void 0!==o&&(r.tokenName=o),this.fetchTokenList(r)}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},o=ma(a.includeType?{...n,type:"all"!==t.type&&t.type?t.type:"DEFI"}:n,{stringifyFields:["page","limit"],optionalFields:["address","search","tokenName"]}),r=await this.http.get(e,o);Wt(r,a.errorMessage,!0);const i=(s=r.data)?Array.isArray(s)?s:s.token:[];var s;const c=zt(r,{page:t.page||sa.DEFAULT_PAGE,limit:t.limit||sa.DEFAULT_LIMIT}),l=Vt(c.page,c.totalPages);return{tokens:i,...c,...l}}validateGetTokenListOptions(e){if(I(e.page,e.limit,Na),void 0!==e.address&&!Ia(e.address))throw new w("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>=Na.SEARCH.MIN_LENGTH&&t.length<=Na.SEARCH.MAX_LENGTH))throw new w(`Search query must be between ${Na.SEARCH.MIN_LENGTH} and ${Na.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>=Na.TOKEN_NAME.MIN_LENGTH&&a.length<=Na.TOKEN_NAME.MAX_LENGTH))throw new w(`Token name must be between ${Na.TOKEN_NAME.MIN_LENGTH} and ${Na.TOKEN_NAME.MAX_LENGTH} characters`,"tokenName","INVALID_TOKEN_NAME")}validateUpdateProfileData(e){if(!Ia(e.address))throw new w("Address must be in format: eth|[40-hex-chars]","address","INVALID_ADDRESS");if(!((t=e.fullName)&&"string"==typeof t&&t.length>=Na.PROFILE.FULL_NAME.MIN_LENGTH&&t.length<=Na.PROFILE.FULL_NAME.MAX_LENGTH&&Na.PROFILE.FULL_NAME.ALPHABETS_ONLY_PATTERN.test(t)))throw new w(`Full name must be between ${Na.PROFILE.FULL_NAME.MIN_LENGTH} and ${Na.PROFILE.FULL_NAME.MAX_LENGTH} characters`,"fullName","INVALID_FULL_NAME");var t}validateUploadProfileImageOptions(e){if(e.address&&!Ia(e.address))throw new w("Address must be in format: eth|[40-hex-chars]","address","INVALID_ADDRESS")}}class Ca extends Error{constructor(e,t,a){super(e),this.filename=t,this.mimeType=a,this.name="FileValidationError"}}function Da(e,t,a){if(!e)throw new Ca("File is required",t,a);if("undefined"!=typeof File&&e instanceof File){const t=Ae.safeParse(e);if(!t.success){const a=t.error.errors.map(e=>e.message).join("; ");throw new Ca(a,e.name,e.type)}return}if(Buffer.isBuffer(e)){if(!t)throw new Ca("Filename is required when uploading Buffer objects",t,a);const n=Te.safeParse(e);if(!n.success){const e=n.error.errors.map(e=>e.message).join("; ");throw new Ca(e,t,a)}if(t.length>255)throw new Ca(`Filename length ${t.length} exceeds maximum allowed length of 255 characters`,t,a);const o=["image/png","image/jpg","image/jpeg","image/gif","image/webp","image/svg+xml"];if(!o.includes(a))throw new Ca(`Invalid file type "${a}" is not allowed. Allowed types: ${o.join(", ")}`,t,a);const r=function(e){if(!e)return"";const t=e.lastIndexOf(".");if(-1===t)return"";return e.substring(t).toLowerCase()}(t),i=[".png",".jpg",".jpeg",".gif",".webp",".svg"];if(!i.includes(r))throw new Ca(`File extension "${r}" is not allowed. Allowed extensions: ${i.join(", ")}`,t,a);const s=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"}}(r);if(s!==a&&"application/octet-stream"!==s)throw new Ca(`File extension "${r}" does not match MIME type "${a}"`,t,a);return}throw new Ca("File must be a File object (browser) or Buffer (Node.js)",t,a)}class Pa extends ca{constructor(e,t=!1){super(e,t)}async uploadImageByTokenName(e){const{tokenName:t,options:a}=e;Ct(t);const n=`${t}.png`;Da(a.file,n,"image/png");try{const e=`${a.tokenName??t}.png`,n=ba(a.file,"image",e),o=await this.http.request({method:"POST",url:`${jt}?tokenName=${encodeURIComponent(a.tokenName??t)}`,data:n,headers:{}});return Wt(o,"Image upload failed"),"string"==typeof o.data?o.data:""}catch(e){if(e instanceof Error&&e.message.includes("FormData"))throw Kt("File upload failed: FormData not supported in this environment. Ensure you have proper polyfills for Node.js environments.","FormData");throw e}}}class La extends ca{constructor(e,t=!1){super(e,t)}async transferFaucets(e){this.validateTransferFaucetsData(e);const t={userAddress:e.walletAddress,amount:e.amount};Wt(await this.http.post(ia,t),"Faucet transfer failed")}validateTransferFaucetsData(e){if(!Ia(e.walletAddress))throw new w("Address must be in format: eth|[40-hex-chars]","address","INVALID_ADDRESS");if(!(t=e.amount)||"string"!=typeof t||!Na.FAUCET_AMOUNT.POSITIVE_NON_ZERO_DECIMAL.test(t))throw new w("Amount must be a positive decimal string greater than zero","amount","INVALID_AMOUNT");var t}}class $a{constructor(e,t=!1){this.http=e,this.poolService=new pa(e,t),this.tradeService=new ya(e,t),this.commentService=new Aa(e,this.poolService,t),this.userService=new xa(e,t),this.imageService=new Pa(e,t),this.faucetService=new La(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 Ct(e)}}const _a={MAX_UNIQUE_KEY_LENGTH:64,UNIQUE_KEY_PATTERN:/^(galaswap-operation-|galaconnect-operation-)/,TOKEN_NAME_PATTERN:/^[a-zA-Z0-9]+$/};var Ua;!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",e.WALLET_REQUIRED="WALLET_REQUIRED"}(Ua||(Ua={}));class Ba extends Error{constructor(e,t,a){super(e),this.type=t,this.details=a,this.name="TransferError"}}class Ma{static validateAmount(e){if(parseFloat(e)<=0)throw new Ba("Transfer amount must be positive",Ua.INVALID_AMOUNT,{amount:e})}static validateUniqueKey(e){if(e){if(e.length>_a.MAX_UNIQUE_KEY_LENGTH)throw new w(`Unique key too long. Maximum length: ${_a.MAX_UNIQUE_KEY_LENGTH}`);if(!_a.UNIQUE_KEY_PATTERN.test(e))throw new Ba('Invalid unique key format. Must start with "galaswap-operation-" or "galaconnect-operation-"',Ua.INVALID_AMOUNT,{uniqueKey:e})}}}class Oa extends o.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,o){let r;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`);r={collection:e[0],category:e[1],type:e[2],additionalKey:e[3],instance:"0"}}else r={collection:n.collection,category:n.category,type:n.type,additionalKey:n.additionalKey,instance:"0"};return new Oa({from:e,to:t,quantity:a,tokenInstance:r,uniqueKey:o||`galaconnect-operation-${Date.now()}_${Math.random().toString(36).substring(2,8)}`})}static forGALA(e,t,a,n){return new Oa({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 Ra{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|${n.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 Ga(e){if("string"==typeof e){const t=e.split("|");if(t.length<4)throw new w(`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 w(`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 w("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 w(`Invalid tokenId type: ${typeof e}. Expected string, TokenClassKey, or TokenInstanceKey`,"tokenId","INVALID_TOKEN_ID_TYPE")}var Ka=Object.freeze({__proto__:null,normalizeToTokenInstanceKey:Ga});const qa="gala-transfer-successful",za="token-transfer-successful",Va="transfer-successful-no-id";class Wa extends ca{constructor(e,t,a,n=!1,o){super(e,n),this.wallet=t,this.tokenResolver=a,this.publicAxios=o,this.signatureHelper=t?new Ra(t):void 0}async fetchPoolDetails(e){this.validateFetchPoolDetailsData(e);const t=await this.http.post("/api/asset/launchpad-contract/FetchSaleDetails",e);if(1!==t.Status)throw Gt(`Failed to fetch pool details: Status ${t.Status}`,t.Status);const a=t.Data.reverseBondingCurveConfiguration,n=a?.minFeePortion??"0",o=a?.maxFeePortion??"0",r=(await import("bignumber.js")).default,i=!new r(n).isZero()||!new r(o).isZero(),s=t.Data;return s.reverseBondingCurveMinFeePortion=n,s.reverseBondingCurveMaxFeePortion=o,s.hasReverseBondingCurveFee=i,s.isGraduated="Finished"===t.Data.saleStatus,delete s.reverseBondingCurveConfiguration,t}async fetchLaunchTokenFee(){const e=await this.http.post("/api/asset/launchpad-contract/FetchLaunchpadFeeAmount",{});if(1!==e.Status)throw Gt(`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 w("Invalid fetch pool details data: missing required fields","data","INVALID_TYPE");var t;if(!e.vaultAddress||"string"!=typeof e.vaultAddress)throw new w("Vault address is required and must be a string","vaultAddress","INVALID_VAULT_ADDRESS");if(!e.vaultAddress.startsWith("service|Token$Unit$"))throw new w("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 Gt(`Failed to fetch token balance from GalaChain: ${Sa(e)}`,void 0,Ta(e)?e:void 0)}}async fetchTokenClassFromChain(e){try{const t="string"==typeof e?Ga(e):e,a={tokenClasses:[{collection:t.collection,category:t.category,type:t.type,additionalKey:t.additionalKey}]},n=(await this.publicAxios.post("/api/asset/token-contract/FetchTokenClasses",a)).data;if(1!==n.Status)throw Gt(`Failed to fetch token class from GalaChain: Status ${n.Status}${n.Message?` - ${n.Message}`:""}`,n.Status);if(!n.Data||0===n.Data.length)throw Gt(`Token not found on GalaChain: ${t.collection}|${t.category}|${t.type}|${t.additionalKey}`,404);return n.Data[0]}catch(e){if(Ea(e)&&404===e.response?.status)throw Gt("Token not found on GalaChain",404,Ta(e)?e:void 0);const t=Sa(e);if(t.includes("Token not found")||t.includes("Status"))throw e;throw Gt(`Failed to fetch token class from GalaChain: ${t}`,void 0,Ta(e)?e:void 0)}}async transferGala(e){if(this.validateTransferGalaData(e),!this.wallet||!this.signatureHelper)throw new Ba("Wallet required for GALA transfer operations",Ua.WALLET_REQUIRED);try{const t=D(e.recipientAddress),a=D(this.wallet.address),n=Oa.forGALA(a,t,e.amount,e.uniqueKey),o=await this.signatureHelper.signTransferToken(n.toSigningPayload()),r=new Oa({...n.toSigningPayload(),signedPayload:o});this.logger.debug("[DEBUG] Full GALA Transfer Request Payload:",JSON.stringify(r,null,2));const i=await this.http.post("/api/asset/token-contract/TransferToken",r);return this.logger.debug("[DEBUG] Transfer response:",JSON.stringify(i,null,2)),this.extractTransactionIdFromResponse(i,"gala")}catch(t){throw this.handleTransferError(t,"GALA transfer failed",e)}}async transferToken(e){if(this.validateTransferTokenData(e),!this.wallet||!this.signatureHelper)throw new Ba("Wallet required for token transfer operations",Ua.WALLET_REQUIRED);try{const t=D(e.to),a=D(this.wallet.address);let n;if(e.tokenId)n=Ga(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 Ba("Must provide either tokenId or tokenName for token identification",Ua.TOKEN_NOT_FOUND);n=await this.resolveTokenInstance(e.tokenName)}const o=new Oa({from:a,to:t,quantity:e.amount,tokenInstance:n,uniqueKey:e.uniqueKey||`galaconnect-operation-${Date.now()}_${Math.random().toString(36).substring(2,8)}`}),r=await this.signatureHelper.signTransferToken(o.toSigningPayload()),i=new Oa({...o.toSigningPayload(),signedPayload:r});this.logger.debug("[DEBUG] Full Transfer Request Payload:",JSON.stringify(i,null,2));const s=await this.http.post("/api/asset/token-contract/TransferToken",i);return this.logger.debug("[DEBUG] Token transfer response:",JSON.stringify(s,null,2)),this.extractTransactionIdFromResponse(s,"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 Ba)throw t;throw new Ba(`Failed to resolve token class key for '${e}': ${Sa(t)}`,Ua.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 w("Invalid GALA transfer data: missing required fields");if(!C(e.recipientAddress))throw new Ba("Invalid recipient address format",Ua.INVALID_RECIPIENT,{recipientAddress:e.recipientAddress});Ma.validateAmount(e.amount),Ma.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 w("Invalid token transfer data: missing required fields");if(!C(e.to))throw new Ba("Invalid recipient address format",Ua.INVALID_RECIPIENT,{recipientAddress:e.to});if(!e.tokenId&&!e.tokenName)throw new Ba("Must provide either tokenId or tokenName for token identification",Ua.TOKEN_NOT_FOUND);if(e.tokenName&&!_a.TOKEN_NAME_PATTERN.test(e.tokenName))throw new Ba("Invalid token name format",Ua.TOKEN_NOT_FOUND,{tokenName:e.tokenName});Ma.validateAmount(e.amount),Ma.validateUniqueKey(e.uniqueKey)}async resolveTokenInstance(e){try{const t=await this.tokenResolver.resolveTokenToVault(e);if(!t)throw new Ba(`Token '${e}' not found or not available for transfer`,Ua.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 Ba)throw t;throw new Ba(`Failed to resolve token '${e}': ${Sa(t)}`,Ua.TOKEN_NOT_FOUND,{tokenName:e})}}resolveTokenInstanceFromVaultAddress(e){const[t,a]=e.split("|");if(!a)throw new Ba(`Invalid vault address format: missing token components. Address: ${e}`,Ua.TOKEN_NOT_FOUND);const n=a.split("$");if(n.length<4)throw new Ba(`Invalid vault address format: insufficient token components. Expected 4+, got ${n.length}. Address: ${e}`,Ua.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?qa:za:Va}if("transactionId"in e&&"string"==typeof e.transactionId&&e.transactionId)return e.transactionId}throw new Ba("Transfer succeeded but transaction ID could not be extracted",Ua.NETWORK_ERROR)}handleTransferError(e,t,a){if(e instanceof Ba)return e;if(e instanceof w)return new Ba(e.message,Ua.INVALID_AMOUNT);if(Ea(e)&&e.response){const t=e.response.status,n=e.response.data;if(400===t)return new Ba(("string"==typeof n?.message?n.message:void 0)||"Invalid transfer request",Ua.INVALID_AMOUNT);if(403===t)return new Ba("Insufficient balance for transfer",Ua.INSUFFICIENT_BALANCE);if(404===t){const e={};return"tokenName"in a&&(e.tokenName=a.tokenName),new Ba("Token not found",Ua.TOKEN_NOT_FOUND,e)}}if("object"==typeof e&&null!==e&&"code"in e&&("ECONNABORTED"===e.code||"ETIMEDOUT"===e.code))return new Ba("Transfer request timed out",Ua.NETWORK_ERROR);const n=Sa(e);return new Ba(n||t,Ua.NETWORK_ERROR)}}function ja(e){if(!e||"string"!=typeof e)throw new Error("Invalid token format: token must be a non-empty string");return e.replace(/\|/g,"$")}class Ha{constructor(e,t,a,n=!1){this.dexBackendHttp=e,this.cache=t,this.galaChainService=a,this.logger=new g({debug:n,context:"DexService"})}async fetchTokenPrice(e){const{tokenName:t,tokenId:a}=e;if(!t&&!a)throw Ot("tokenName or tokenId","Either tokenName (for launchpad tokens) or tokenId (for DEX tokens) is required");if(t&&a)throw new w("tokenName and tokenId are mutually exclusive - provide only one","params","INVALID_PARAMS");if(a)return this.logger.debug(`Fetching spot price by tokenId: ${a}`),this._fetchDexTokenSpotPrice(a);throw new w("tokenName parameter requires LaunchpadSDK routing - call LaunchpadSDK.fetchTokenPrice({tokenName}) instead","tokenName","INVALID_PARAMS")}async _fetchDexTokenSpotPrice(e){if(!e)throw Ot("tokenId","Token ID");try{const t=Ga(e),a=ja(`${t.collection}|${t.category}|${t.type}|${t.additionalKey}`);if(this.logger.debug(`Fetching DEX spot price for token: ${a}`),!this.dexBackendHttp)throw Gt("DEX Backend API client not configured");const n=await this.dexBackendHttp.request({method:"GET",url:"/v1/trade/price",params:{token:a}});if(!n.data||"string"!=typeof n.data)throw new w("Invalid price response: data must be a string, got "+typeof n.data,"data","INVALID_RESPONSE");const o=parseFloat(n.data);if(isNaN(o))throw new w(`Invalid price value: could not parse "${n.data}" as number`,"data","INVALID_CALCULATION");const r=`${t.collection}|${t.category}|${t.type}|${t.additionalKey}`;let i;try{if(this.cache){const e=this.cache.getByTokenId(r);if(e?.symbol)return i=e.symbol,this.logger.debug(`DEX spot price for ${i} (cached): $${o}`),{symbol:i,price:o}}this.logger.debug(`Symbol cache miss for ${r}, fetching from API`);i=(await this.fetchTokenDetails(e)).symbol,this.cache&&(this.cache.setByTokenId(r,{symbol:i}),this.logger.debug(`Cached symbol for ${r}: ${i}`)),this.logger.debug(`DEX spot price for ${i}: $${o}`)}catch(e){this.logger.debug(`Could not fetch token details for symbol, falling back to type field: ${e instanceof Error?e.message:String(e)}`),i=t.type.toUpperCase(),this.logger.debug(`DEX spot price for ${i} (fallback): $${o}`)}return{symbol:i,price:o}}catch(e){if(e instanceof w)throw e;throw Gt(`Failed to fetch DEX spot price: ${Sa(e)}`)}}async fetchLaunchpadTokenSpotPrice(e,t,a){if(!e||"string"!=typeof e)throw new Error(f);try{if(a)try{this.logger.debug(`Checking graduation status for token: ${e}`);const t=await a(e);if(t&&t.isGraduated){this.logger.debug(`Token ${e} is graduated, using DEX spot price`);const a=`${t.sellingToken.collection}|${t.sellingToken.category}|${t.sellingToken.type}|${t.sellingToken.additionalKey}`;return this._fetchDexTokenSpotPrice(a)}}catch(t){this.logger.debug(`Could not determine graduation status for ${e}, falling back to bonding curve: ${Sa(t)}`)}this.logger.debug(`Using bonding curve calculation for token: ${e}`);const n=await t({tokenName:e,amount:"1",type:"native"}),o=await this._fetchDexTokenSpotPrice({collection:"GALA",category:"Unit",type:"none",additionalKey:"none"});if(!o)throw Gt("GALA price not available");const r=Number(n.amount)/1e18;if(r<=0)throw new w(`Invalid token amount calculation: ${r}`,"amount","INVALID_CALCULATION");const i=o.price/r;return{symbol:e.toUpperCase(),price:i}}catch(t){if(t instanceof Error)throw Gt(`Failed to calculate launchpad token spot price for ${e}: ${t.message}`);throw Gt(`Failed to calculate launchpad token spot price for ${e}: ${Sa(t)}`)}}async fetchTokenDetails(e){this.logger.debug("Fetching token details from GalaChain for tokenId:",e);try{if(!this.galaChainService)throw Gt("GalaChainService not available for token metadata fetch",500);const t=await this.galaChainService.fetchTokenClassFromChain(e),a={collection:t.collection,category:t.category,type:t.type,additionalKey:t.additionalKey,symbol:t.symbol,decimals:t.decimals,name:t.name,image:t.image,description:t.description,network:t.network,...void 0!==t.contractAddress&&{contractAddress:t.contractAddress}};return this.logger.debug(`Fetched token details for ${t.symbol} from GalaChain`),a}catch(t){if((t instanceof w||t instanceof Error)&&("NetworkError"===t.name||t.message.includes("Token not found")))throw t;throw Gt(`Failed to fetch token details from GalaChain for ${e}: ${Sa(t)}`,500)}}}function Xa(e,t=18){const a=parseFloat(e);if(0===a)return"0";return a.toFixed(t).replace(/\.?0+$/,"")}function Qa(e){return Xa(e,8)}function Ya(e){return Xa(e,18)}function Za(e){return{maxAcceptableReverseBondingCurveFee:Qa(e.maxAcceptableReverseBondingCurveFee)}}new g({debug:!1,context:"NumberUtils"});class Ja extends o.ChainCallDTO{constructor(e,t,a="0",n={maxAcceptableReverseBondingCurveFee:"0"}){super(),this.vaultAddress=e,this.nativeTokenQuantity=Qa(t),this.expectedToken=Ya(a),this.extraFees=Za(n)}}class en extends o.ChainCallDTO{constructor(e,t,a,n={maxAcceptableReverseBondingCurveFee:"0"}){super(),this.vaultAddress=e,this.tokenQuantity=Ya(t),this.expectedNativeToken=Qa(a),this.extraFees=Za(n)}}class tn extends o.ChainCallDTO{constructor(e,t,a="0",n={maxAcceptableReverseBondingCurveFee:"0"}){super(),this.vaultAddress=e,this.tokenQuantity=Ya(t),this.expectedNativeToken=Qa(a),this.extraFees=Za(n)}}class an extends o.ChainCallDTO{constructor(e,t,a,n={maxAcceptableReverseBondingCurveFee:"0"}){super(),this.vaultAddress=e,this.nativeTokenQuantity=Qa(t),this.expectedToken=Ya(a),this.extraFees=Za(n)}}const nn={BuyNativeDto:Ja,BuyExactDto:en,SellExactDto:tn,SellNativeDto:an};var on,rn,sn;!function(e){e[e.METAMASK=0]="METAMASK",e[e.TRUST_WALLET=1]="TRUST_WALLET",e[e.GALA_WALLET=2]="GALA_WALLET"}(on||(on={}));class cn{constructor(e,t=!1){this.walletProvider=e,this.debug=t,this.logger=new g({debug:t,context:"SignatureService"})}async signDTO(e,t,a,n=on.METAMASK){try{this.logger.debug("🔐 Signing DTO:",{methodName:t,walletPreference:n,dtoKeys:Object.keys(e)});const r=this.generateEIP712Types(t,e),i=o.calculatePersonalSignPrefix(e),s={...e,prefix:i};let c,l,u;switch(n){case on.GALA_WALLET:({signature:c,domain:l}=await this.signWithGalaWallet(r,s,t,a));break;case on.TRUST_WALLET:c=await this.signWithTrustWallet(s),l={name:"ethereum",chainId:1};break;case on.METAMASK:default:({signature:c,domain:l}=await this.signWithMetaMask(r,s))}return u=n===on.TRUST_WALLET?{...s,signature:c}:{...e,signature:c,types:r,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 qt(`Failed to sign DTO: ${Sa(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 Kt("Wallet provider does not support typed data signing","walletProvider");{const o=await this.walletProvider.getNetwork();a={name:o.name,chainId:Number(o.chainId)},n=await this.walletProvider.signTypedData(a,e,t)}}return{signature:n,domain:a}}catch(e){throw qt(`MetaMask/ethers signing failed: ${Sa(e)}`)}}async signWithTrustWallet(e){try{const a=(t=e,JSON.stringify(t));let n;if(!this.walletProvider.signMessage)throw Kt("Wallet provider does not support signMessage","walletProvider");return n=await this.walletProvider.signMessage(a),n}catch(e){throw qt(`TrustWallet signing failed: ${Sa(e)}`)}var t}async signWithGalaWallet(e,t,a,n){try{const o={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 r={domain:o,types:e,message:t,Primary_type:a},i=window;if(!i.gala)throw Kt("GalaWallet not found in window object","galaWallet");await i.gala.setAddress(n);return{signature:await i.gala.request({method:"eth_signTypedData",params:[JSON.stringify(r),n]}),domain:o}}catch(e){throw qt(`GalaWallet signing failed: ${Sa(e)}`)}}generateEIP712Types(e,t){const a={};a[e]=[];const n=(e,t,o,r=!1)=>{if(void 0!==t){if(Array.isArray(t)){const i=n(e,t[0],o,!0);return r||a[o].push({name:e,type:(i??e)+"[]"}),i?i+"[]":void 0}if("object"==typeof t&&null!==t){if(a[e])throw new w(`Type name collision not supported: ${e}`,"fieldValue","TYPE_COLLISION");return a[e]=[],Object.entries(t).forEach(([t,a])=>{n(t,a,e)}),r||a[o].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 w(`Unsupported type for fieldName ${e}: ${typeof t}, value: ${t}`,"fieldValue","UNSUPPORTED_TYPE")}return r||a[o].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 on.METAMASK;const e=window;return e.gala?on.GALA_WALLET:e.trustWallet?.isTrust?on.TRUST_WALLET:(e.ethereum,on.METAMASK)}}class ln{constructor(e=!1){this.debug=e,this.logger=new g({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(),o=`$service$${a.toStringKey()}$launchpad`,r=`$tokenBalance$${a.toStringKey()}$${e}`,i=`$tokenBalance$${a.toStringKey()}$${e}`,s=`$tokenBalance$${n.toStringKey()}$${e}`,c=[o,r,i,s,`$tokenBalance$${n.toStringKey()}$${e}`];return this.logger.debug("✅ Generated stringsInstructions:",c),c}catch(e){this.logger.error("❌ Failed to generate stringsInstructions:",e);const t=Sa(e);throw new w(`Failed to generate stringsInstructions: ${t}`,"vaultAddress","INVALID_VAULT_ADDRESS")}}createTokenInstance(e){const t=new s.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 s.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 Ot("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 w(`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 Ot("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 un(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 i(e);if(n.isNaN())throw new Error(`Invalid expected amount: ${e}. Must be a valid number`);if(0===t)return e;const o=n.multipliedBy(t);let r;switch(a){case"buy-native":case"sell-exact":r=n.minus(o);break;case"buy-exact":case"sell-native":r=n.plus(o);break;default:throw new Error(`Unknown operation type: ${a}`)}return r.isLessThan(0)&&(r=new i(0)),r.toFixed()}class dn extends ca{constructor(e,t,a=!1,n,o,r=.05,i=.01){super(e,a),this.tokenResolver=t,this.walletProvider=n,this.userAddress=o,this.defaultSlippageToleranceFactor=r,this.defaultMaxAcceptableReverseBondingCurveFeeSlippageFactor=i,this.bundleEndpoint="/bundle",n&&o&&(this.signatureService=new cn(n,a),this.tokenKeyService=new ln(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.http.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 Ot("bundleData","Bundle data");if(!e.signedDto)throw Ot("signedDto","Signed DTO");if(!e.method||"string"!=typeof e.method)throw Ot("method","Method name");if(!Array.isArray(e.stringsInstructions))throw Rt("stringsInstructions","an array of resource tracking strings");if(0===e.stringsInstructions.length)throw new w("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 w(`stringsInstructions[${t}] must be a non-empty string`,`stringsInstructions[${t}]`,"INVALID_INSTRUCTION");if(!e.startsWith("$"))throw new w(`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(Ea(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 Sa(e)||"Unknown bundle transaction error"}async getBundlerTransactionResult(e){try{if(!e||"string"!=typeof e)throw Ot("transactionId","Transaction ID");this.logger.debug("🔍 Checking bundler transaction result:",e);const t=await this.http.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 Ot("transactionId","Transaction ID");this.logger.debug("🚫 Cancelling transaction:",e);const t=await this.http.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.http.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:o}=e,{effectiveSlippageFactor:r,effectiveMaxFee:i,vaultAddress:s}=await this.prepareTradingOperation(t,e.maxAcceptableReverseBondingCurveFee,e.maxAcceptableReverseBondingCurveFeeSlippageFactor,e.slippageToleranceFactor);if("native"===n){if(!o)throw new w("expectedAmount is required for native buy operations. Use getBuyTokenAmount() first to calculate expected tokens.","expectedAmount","EXPECTED_AMOUNT_REQUIRED");const e=un(o,r,"buy-native");this.logger.debug("BuyNative slippage applied:",{originalExpectedTokens:o,slippageFactor:r,adjustedMinTokens:e});const t=new nn.BuyNativeDto(s,a,e,{maxAcceptableReverseBondingCurveFee:i});return await this.executeBundleTransaction(t,"BuyWithNative",s)}{if(!o)throw new w("expectedAmount is required for exact buy operations. Use getBuyTokenAmount() first to calculate expected GALA cost.","expectedAmount","EXPECTED_AMOUNT_REQUIRED");const e=un(o,r,"buy-exact");this.logger.debug("BuyExact slippage applied:",{originalExpectedGalaCost:o,slippageFactor:r,adjustedMaxGalaCost:e});const t=new nn.BuyExactDto(s,a,e,{maxAcceptableReverseBondingCurveFee:i});return await this.executeBundleTransaction(t,"BuyExactToken",s)}}async sellToken(e){this.ensureTradingServicesAvailable();const{tokenName:t,amount:a,type:n,expectedAmount:o}=e,{effectiveSlippageFactor:r,effectiveMaxFee:i,vaultAddress:s}=await this.prepareTradingOperation(t,e.maxAcceptableReverseBondingCurveFee,e.maxAcceptableReverseBondingCurveFeeSlippageFactor,e.slippageToleranceFactor);if("exact"===n){if(!o)throw new w("expectedAmount is required for exact sell operations. Use getSellTokenAmount() first to calculate expected GALA.","expectedAmount","EXPECTED_AMOUNT_REQUIRED");const e=un(o,r,"sell-exact");this.logger.debug("SellExact slippage applied:",{originalExpectedGala:o,slippageFactor:r,adjustedMinGala:e});const t=new nn.SellExactDto(s,a,e,{maxAcceptableReverseBondingCurveFee:i});return await this.executeBundleTransaction(t,"SellExactToken",s)}{if(!o)throw new w("expectedAmount is required for native sell operations. Use getSellTokenAmount() first to calculate tokens to sell.","expectedAmount","EXPECTED_AMOUNT_REQUIRED");const e=un(o,r,"sell-native");this.logger.debug("SellNative slippage applied:",{originalExpectedTokensToSell:o,slippageFactor:r,adjustedMaxTokensToSell:e});const t=new nn.SellNativeDto(s,a,e,{maxAcceptableReverseBondingCurveFee:i});return await this.executeBundleTransaction(t,"SellWithNative",s)}}async prepareTradingOperation(e,t,a,n){const{effectiveSlippageFactor:o,effectiveMaxFee:r}=this.calculateEffectiveSlippage(t,a,n),i=await this.resolveTokenNameToVault(e);if(!i)throw Mt(e);return{effectiveSlippageFactor:o,effectiveMaxFee:r,vaultAddress:i}}calculateEffectiveSlippage(e,t,a){const n=a??this.defaultSlippageToleranceFactor,o=t??this.defaultMaxAcceptableReverseBondingCurveFeeSlippageFactor;let r=e||"0";return e&&(r=un(e,o,"buy-exact"),this.logger.debug("Reverse bonding curve fee slippage applied:",{baseFee:e,slippageFactor:o,adjustedMaxFee:r})),{effectiveSlippageFactor:n,effectiveFeeSlippageFactor:o,effectiveMaxFee:r}}ensureTradingServicesAvailable(){if(!this.signatureService||!this.tokenKeyService)throw Kt("Trading services not available. BundleService requires walletProvider and userAddress for trading operations.","walletProvider");if(!this.userAddress)throw Ot("userAddress","User address")}async executeBundleTransaction(e,t,a){this.ensureTradingServicesAvailable();try{e.uniqueKey=`galaswap - operation - ${c.v4()}-${Date.now()}-${this.userAddress}`;const n=await this.signatureService.signDTO(e,t,this.userAddress),o=this.tokenKeyService.generateStringsInstructions(a),r={stringsInstructions:o,method:t,signedDto:n};this.logger.debug("📦 Bundle transaction data:",{method:t,stringsInstructions:o,dtoKeys:Object.keys(n)});const i=await this.submitTransaction(r);if(i.success&&i.data)return this.logger.debug("✅ Bundle transaction submitted:",i.data),{success:!0,data:{transactionId:i.data,message:"Transaction submitted successfully. Monitor WebSocket for completion."}};throw new Error(String(i.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"}(rn||(rn={})),exports.SDKTransactionStatus=void 0,(sn=exports.SDKTransactionStatus||(exports.SDKTransactionStatus={})).PENDING="pending",sn.PROCESSING="processing",sn.COMPLETED="completed",sn.FAILED="failed",sn.TIMEOUT="timeout";const hn={[rn.PROCESSED]:exports.SDKTransactionStatus.COMPLETED,[rn.COMPLETED]:exports.SDKTransactionStatus.COMPLETED,[rn.SUCCESS]:exports.SDKTransactionStatus.COMPLETED,[rn.FAILED]:exports.SDKTransactionStatus.FAILED,[rn.ERROR]:exports.SDKTransactionStatus.FAILED,[rn.PROCESSING]:exports.SDKTransactionStatus.PROCESSING,[rn.PENDING]:exports.SDKTransactionStatus.PENDING};class gn{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 g({debug:t,context:"WebSocketService"}),this.isSocketIOAvailable=this.checkSocketIOAvailability()}checkSocketIOAvailability(){try{return"function"==typeof l.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=l.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,o=n?.data,r=n?.status||n?.Status||o?.status||o?.Status;let i=n?.message||n?.Message||o?.message||o?.Message||n?.error||o?.error;i&&"string"==typeof i||(i=r===rn.FAILED||r===rn.ERROR?"Transaction failed - check transaction details":r===rn.COMPLETED||r===rn.PROCESSED||r===rn.SUCCESS?"Transaction completed successfully":r?`Transaction status: ${r}`:"Unknown transaction status");const s=n?.blockHash||o?.blockHash,c=n?.gasUsed||o?.gasUsed,l={transactionId:e,status:this.mapSocketStatus(r),message:"string"==typeof i?i:"Transaction update received",timestamp:Date.now(),...s&&{blockHash:s},...c&&{gasUsed:c}};if(this.logger.debug(`📡 Mapped status for ${e}: ${r} -> ${l.status}`),this.logger.debug(`📡 Final message: "${i}"`),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 hn[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}getSocket(){return this.socket}}class pn{constructor(e,t=!1){this.poolService=e,this.cache=new Map,this.logger=new g({debug:t,context:"TokenResolverService"})}async resolveTokenToVault(e){if(!x(e))throw new w("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 Mt(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 mn{constructor(e,t=!1,a){this.httpClient=e,this.tokenResolverService=a,this.logger=new g({debug:t,context:"PriceHistoryService"})}async fetchTokenClassKeyByTokenName(e){if(!this.tokenResolverService)throw Kt("TokenResolverService is required for token name resolution. Ensure it is passed to PriceHistoryService constructor.","tokenResolverService");if(!e||"string"!=typeof e||e.length<3||e.length>20)throw Kt("Token name must be a string between 3 and 20 characters","tokenName");this.logger.debug(`Resolving token name '${e}' to token class key`);try{const t=await this.tokenResolverService.resolveTokenToVault(e);if(!t)throw Kt(`Token '${e}' not found or could not be resolved to vault address`,"tokenName");this.logger.debug(`Resolved '${e}' to vault address: ${t}`);const a=t.split("|");if(2!==a.length||"service"!==a[0])throw Kt(`Invalid vault address format: ${t}. Expected service|Token$Unit$...`,"vaultAddress");const n=a[1].split("$");if(n.length<4)throw Kt(`Invalid token parts in vault address: ${t}. Expected at least 4 parts separated by $`,"vaultAddress");const o=n[0],r=n[1],i=n[2],s=n.slice(3,-1),c=`${o}|${r}|${i}|${s.join("$")}`;return this.logger.debug(`Extracted token class key: ${c}`),c}catch(t){if(t instanceof Error&&t.message.includes("ConfigurationError"))throw t;throw Gt(`Failed to resolve token name '${e}': ${Sa(t)}`,500)}}async fetchPriceHistory(e){if(this.logger.debug("Fetching price history from DEX Backend API with options:",e),!this.httpClient)throw Kt("HttpClient is required for fetchPriceHistory","httpClient");this.validateOptions(e);try{let t=e.tokenId;if(e.tokenName){this.logger.debug(`Resolving token name '${e.tokenName}' to token ID`);const a=await this.fetchTokenClassKeyByTokenName(e.tokenName);t=a,this.logger.debug(`Resolved to token ID: ${a}`)}if(!t)throw Kt("Token ID is required but was not provided or resolved","tokenId");const{normalizeToTokenInstanceKey:a}=await Promise.resolve().then(function(){return Ka}),n=a(t),o=ja(`${n.collection}|${n.category}|${n.type}|${n.additionalKey}`),{from:r,to:i,sortOrder:s="DESC",page:c=1,limit:l=10}=e,u={token:o,page:String(c),limit:String(l)};r&&(u.from=r.toISOString()),i&&(u.to=i.toISOString());const d=function(e){if(e)return e.toLowerCase()}(s);d&&(u.order=d),this.logger.debug(`Querying price snapshots for token ${o}, page ${c}, limit ${l}`);const h=await this.httpClient.get("/price-oracle/fetch-price",u),g=this.transformApiResponseToPriceHistory(h);return this.logger.debug(`Found ${g.snapshots.length} price snapshots, total ${g.total}`),g}catch(e){if(e instanceof Error&&(e.message.includes("ConfigurationError")||e.message.includes("NetworkError")))throw e;throw Gt(`Failed to fetch price history: ${Sa(e)}`,500)}}transformApiResponseToPriceHistory(e){if(!e?.data?.data||!Array.isArray(e.data.data))throw Gt("Invalid API response: missing or invalid data.data array",500);if(!e?.data?.meta)throw Gt("Invalid API response: missing data.meta pagination info",500);const t=e.data.data.map(e=>({price:e.price,timestamp:new Date(e.createdAt),tokenId:`${e.collection}|${e.category}|${e.type}|${e.additionalKey}`})),a=e.data.meta,n=a.currentPage??1,o=a.totalPages??1;return{snapshots:t,page:n,limit:a.pageSize??50,total:a.totalItems??0,totalPages:o,hasNext:n<o,hasPrevious:n>1}}async fetchAllPriceHistory(e){this.logger.debug("Fetching all price history with options:",e);const t=[];let a=1,n=!0;const o=1e4;for(;n&&a<=o;){const o=await this.fetchPriceHistory({...e,page:a,limit:50});if(this.logger.debug(`Fetched page ${a}: ${o.snapshots.length} snapshots, hasNext: ${o.hasNext}`),t.push(...o.snapshots),0===o.snapshots.length){this.logger.debug("No snapshots returned on page ${page}, exiting pagination loop");break}n=o.hasNext,a++}return a>o&&this.logger.warn("Auto-pagination exceeded MAX_PAGES limit of 10000, stopping iteration"),{snapshots:t,page:1,limit:t.length||0,total:t.length,totalPages:1,hasNext:!1,hasPrevious:!1}}validateOptions(e){const{tokenName:t,tokenId:a}=e,n=null!=t&&""!==t,o=null!=a;if(!n&&!o)throw Kt("Either tokenName or tokenId must be provided","tokenId");if(n&&o)throw Kt("Cannot provide both tokenName and tokenId. Provide exactly one.","tokenId");const{from:r,to:i,sortOrder:s,page:c=1,limit:l=10}=e;if(r&&!(r instanceof Date)&&isNaN(new Date(r).getTime()))throw Kt("from must be a valid Date","from");if(i&&!(i instanceof Date)&&isNaN(new Date(i).getTime()))throw Kt("to must be a valid Date","to");if(s&&"ASC"!==s&&"DESC"!==s)throw Kt('sortOrder must be either "ASC" or "DESC"',"sortOrder");if(!Number.isInteger(c)||c<1)throw Kt("page must be a positive integer","page");if(!Number.isInteger(l)||l<1||l>50)throw Kt("limit must be between 1 and 50","limit")}}class fn{toLaunchpadFormat(e){if(!e)return"unknown|Unit|none|none";if("string"==typeof e)return e.includes("|")?e:`${e}|Unit|none|none`;return`${e.type||e.symbol||"unknown"}|${e.category||"Unit"}|none|${e.additionalKey||"none"}`}toTokenClass(e){if("object"==typeof e&&null!==e)return{collection:e.collection||"Token",category:e.category||"Unit",type:e.type||e.symbol||"unknown",additionalKey:e.additionalKey||"none"};if("string"!=typeof e)throw new Error("Invalid token format: expected string or object, got "+typeof e);const t=e.split("|");if(t.length<3)throw new Error(`Invalid token format: ${e}`);const a=("Token"===t[0]||"Collection"===t[0])&&t[2]?t[2]:t[0];return{collection:t[0]||"Token",category:t[1]||"Unit",type:a,additionalKey:t[3]||"none"}}isGSwapFormat(e){return"string"==typeof e&&e.includes("|")}normalize(e){return"string"==typeof e&&this.isGSwapFormat(e)?e:this.toLaunchpadFormat(e)}}class yn{constructor(e,t){if(this.tokenConverter=new fn,this.logger=new g({debug:!1,context:"GSwapService"}),this.webSocketService=t,!e.gatewayBaseUrl||!e.bundlerBaseUrl||!e.dexBackendBaseUrl)throw new Error("GSwapService requires explicit gatewayBaseUrl, bundlerBaseUrl, and dexBackendBaseUrl configuration. These must be provided by LaunchpadSDK to ensure environment alignment.");if(e.privateKey&&e.walletAddress)try{const t=new u.PrivateKeySigner(e.privateKey);this.gswap=new u.GSwap({signer:t,walletAddress:e.walletAddress,gatewayBaseUrl:e.gatewayBaseUrl,bundlerBaseUrl:e.bundlerBaseUrl,dexBackendBaseUrl:e.dexBackendBaseUrl}),this.logger.debug("GSwap initialized in full-access mode")}catch(e){throw this.logger.error("Failed to initialize GSwap with signer",e),new Error("Failed to initialize GSwap SDK with signer")}else this.gswap=new u.GSwap({gatewayBaseUrl:e.gatewayBaseUrl,bundlerBaseUrl:e.bundlerBaseUrl,dexBackendBaseUrl:e.dexBackendBaseUrl}),this.logger.debug("GSwap initialized in read-only mode")}async getSwapQuoteExactInput(e){try{this.logger.debug("Getting swap quote for exact input",{fromToken:e.fromToken,toToken:e.toToken,amount:e.amount});const t=this.tokenConverter.toLaunchpadFormat(e.fromToken),a=this.tokenConverter.toLaunchpadFormat(e.toToken),n=await this.gswap.quoting.quoteExactInput(t,a,e.amount);return{fromToken:e.fromToken,toToken:e.toToken,inputAmount:e.amount,estimatedOutput:n.outTokenAmount.toFixed(),feeTier:n.feeTier,priceImpact:n.priceImpact.toString(),executionPrice:this.calculateExecutionPrice(e.amount,n.outTokenAmount)}}catch(e){this.logger.error("Failed to get swap quote",e);const t=e;throw new T(`Failed to get swap quote for exact input: ${t?.message||String(e)}`,e,this.extractGSwapErrorCode(e))}}async getSwapQuoteExactOutput(e){try{this.logger.debug("Getting swap quote for exact output",{fromToken:e.fromToken,toToken:e.toToken,amount:e.amount});const t=this.tokenConverter.toLaunchpadFormat(e.fromToken),a=this.tokenConverter.toLaunchpadFormat(e.toToken),n=await this.gswap.quoting.quoteExactOutput(t,a,e.amount);return{fromToken:e.fromToken,toToken:e.toToken,inputAmount:n.inTokenAmount.toFixed(),estimatedOutput:e.amount,feeTier:n.feeTier,priceImpact:n.priceImpact.toString(),executionPrice:this.calculateExecutionPrice(n.inTokenAmount,e.amount)}}catch(e){this.logger.error("Failed to get swap quote for exact output",e);const t=e;throw new T(`Failed to get swap quote for exact output: ${t?.message||String(e)}`,e,this.extractGSwapErrorCode(e))}}async executeSwap(e){try{if(!this.gswap)throw new Error("GSwap SDK not initialized");this.logger.debug("Executing swap",{fromToken:e.fromToken,toToken:e.toToken,inputAmount:e.inputAmount});const t=this.tokenConverter.toLaunchpadFormat(e.fromToken),a=this.tokenConverter.toLaunchpadFormat(e.toToken),n=new i(e.estimatedOutput),o=new i(1).minus(e.slippageTolerance||.01),r=n.multipliedBy(o),s=(await this.gswap.swaps.swap(t,a,e.feeTier,{exactIn:e.inputAmount,amountOutMinimum:r.toFixed()})).transactionId;if(!s)throw new Error("Failed to get transaction ID from swap result");this.logger.debug("Swap submitted, monitoring transaction",{transactionId:s,fromToken:e.fromToken,toToken:e.toToken}),this.webSocketService.isConnected()||await this.webSocketService.connect();const c=await this.webSocketService.waitForTransaction(s);return{transactionId:c.transactionId,status:c.status,fromToken:e.fromToken,toToken:e.toToken,inputAmount:e.inputAmount,outputAmount:e.estimatedOutput,feeTier:e.feeTier,slippageTolerance:e.slippageTolerance||.01,timestamp:new Date(c.timestamp),wait:async e=>{await this.webSocketService.waitForTransaction(s)}}}catch(e){this.logger.error("Failed to execute swap",e);const t=e;throw new S(`Failed to execute swap: ${t?.message||String(e)}`,e,t?.txHash,this.extractGSwapErrorCode(e))}}async getUserAssets(e){try{this.logger.debug("Fetching user assets",{walletAddress:e});const t=await this.gswap.assets.getUserAssets(e);return(Array.isArray(t)?t:[t]).filter(e=>!!(e.tokenInstance||e.tokenClassKey||e.type||e.symbol)||(this.logger.debug("Skipping asset with no valid token identifier",e),!1)).map(e=>({tokenId:this.tokenConverter.toTokenClass(e.tokenInstance||e.tokenClassKey||e.type||""),symbol:e.symbol||e.type||"UNKNOWN",balance:new i(e.balance||0).toFixed(),decimals:e.decimals||18}))}catch(t){this.logger.error("Failed to fetch user assets",t);const a=t;throw new b(`Failed to fetch user assets: ${a?.message||String(t)}`,t,e,this.extractGSwapErrorCode(t))}}async getPoolInfo(e,t){try{this.logger.debug("Fetching pool info",{tokenA:e,tokenB:t});const a=this.tokenConverter.toLaunchpadFormat(e),n=this.tokenConverter.toLaunchpadFormat(t),o=[500,3e3,1e4];let r=new i(0),s=0;for(const c of o)try{const e=await this.gswap.pools.getPoolData(a,n,c);e&&(r=r.plus(new i(e.liquidity||0)),s++)}catch{this.logger.debug("Pool not found for fee tier",{tokenA:e,tokenB:t,feeTier:c})}return{tokenA:e,tokenB:t,liquidity:r.toFixed(),feeTiers:o,swapCount:s}}catch(a){this.logger.warn("Failed to fetch pool info",a);const n=a;return this.logger.debug("Pool error details",{error:new E(`Failed to fetch pool info: ${n?.message||String(a)}`,a,e,t,this.extractGSwapErrorCode(a))}),{tokenA:e,tokenB:t,liquidity:"0",feeTiers:[500,3e3,1e4],swapCount:0}}}async getUserLiquidityPositions(e,t=100,a){try{this.logger.debug("Fetching user liquidity positions",{ownerAddress:e,limit:t});const n=await this.gswap.positions.getUserPositions(e,t,a);let o;Array.isArray(n)?o=n:n?.positions&&Array.isArray(n.positions)?o=n.positions:n?o=[n]:(o=[],this.logger.warn("No positions returned from API for wallet",{ownerAddress:e}));const r=o.filter(e=>null!=e&&"object"==typeof e&&"positionId"in e);return r.length!==o.length&&this.logger.warn("Filtered out invalid position objects",{total:o.length,valid:r.length}),this.logger.debug("Retrieved liquidity positions",{count:r.length}),r}catch(e){throw this.logger.error("Failed to fetch user liquidity positions",e),new N(`Failed to fetch user liquidity positions: ${e?.message||String(e)}`,e)}}async getLiquidityPosition(e,t){try{this.logger.debug("Fetching liquidity position",{ownerAddress:e,position:t});const a=this.tokenConverter.toLaunchpadFormat(t.token0),n=this.tokenConverter.toLaunchpadFormat(t.token1),o=await this.gswap.positions.getPosition(e,{token0:a,token1:n,fee:t.fee,tickLower:t.tickLower,tickUpper:t.tickUpper});if(!o||"object"!=typeof o||!("positionId"in o))throw new Error("Invalid position data returned from API");return this.logger.debug("Retrieved liquidity position",{positionId:o.positionId}),o}catch(e){throw this.logger.error("Failed to fetch liquidity position",e),new N(`Failed to fetch liquidity position: ${e?.message||String(e)}`,e)}}async getLiquidityPositionById(e,t){try{this.logger.debug("Fetching liquidity position by ID",{ownerAddress:e,positionId:t});const a=await this.gswap.positions.getPositionById(e,t);if(!a||"object"!=typeof a||!("positionId"in a))throw new Error("Invalid position data returned from API");return this.logger.debug("Retrieved liquidity position by ID",{positionId:a.positionId}),a}catch(e){throw this.logger.error("Failed to fetch liquidity position by ID",e),new N(`Failed to fetch liquidity position: ${e?.message||String(e)}`,e)}}async estimateRemoveLiquidity(e){try{this.logger.debug("Estimating liquidity removal",{token0:e.token0,token1:e.token1});const t=this.tokenConverter.toLaunchpadFormat(e.token0),a=this.tokenConverter.toLaunchpadFormat(e.token1),n=await this.gswap.positions.estimateRemoveLiquidity({token0:t,token1:a,fee:e.fee,liquidity:e.liquidity,tickLower:e.tickLower,tickUpper:e.tickUpper});return this.logger.debug("Estimated removal",{result:n}),n}catch(e){throw this.logger.error("Failed to estimate liquidity removal",e),new N(`Failed to estimate liquidity removal: ${e?.message||String(e)}`,e)}}async addLiquidityByPrice(e){try{if(!this.gswap)throw new Error("GSwap SDK not initialized");this.logger.debug("Adding liquidity by price",{token0:e.token0,token1:e.token1,priceRange:`${e.minPrice}-${e.maxPrice}`});const t=this.tokenConverter.toLaunchpadFormat(e.token0),a=this.tokenConverter.toLaunchpadFormat(e.token1),n=await this.gswap.positions.addLiquidityByPrice({token0:t,token1:a,fee:e.fee,minPrice:e.minPrice,maxPrice:e.maxPrice,amount0Desired:e.amount0Desired,amount1Desired:e.amount1Desired,amount0Min:e.amount0Min||"0",amount1Min:e.amount1Min||"0"}),o=n.transactionId;return this.logger.debug("Liquidity added",{transactionId:o}),o&&!this.webSocketService.isConnected()&&await this.webSocketService.connect(),n}catch(e){throw this.logger.error("Failed to add liquidity by price",e),new N(`Failed to add liquidity: ${e?.message||String(e)}`,e)}}async addLiquidityByTicks(e){try{if(!this.gswap)throw new Error("GSwap SDK not initialized");this.logger.debug("Adding liquidity by ticks",{token0:e.token0,token1:e.token1,tickRange:`${e.tickLower}-${e.tickUpper}`});const t=this.tokenConverter.toLaunchpadFormat(e.token0),a=this.tokenConverter.toLaunchpadFormat(e.token1),n=await this.gswap.positions.addLiquidityByTicks({token0:t,token1:a,fee:e.fee,tickLower:e.tickLower,tickUpper:e.tickUpper,amount0Desired:e.amount0Desired,amount1Desired:e.amount1Desired,amount0Min:e.amount0Min||"0",amount1Min:e.amount1Min||"0"}),o=n.transactionId;return this.logger.debug("Liquidity added",{transactionId:o}),o&&!this.webSocketService.isConnected()&&await this.webSocketService.connect(),n}catch(e){throw this.logger.error("Failed to add liquidity by ticks",e),new N(`Failed to add liquidity: ${e?.message||String(e)}`,e)}}async removeLiquidity(e){try{if(!this.gswap)throw new Error("GSwap SDK not initialized");this.logger.debug("Removing liquidity",{token0:e.token0,token1:e.token1,liquidity:e.liquidity});const t=this.tokenConverter.toLaunchpadFormat(e.token0),a=this.tokenConverter.toLaunchpadFormat(e.token1),n=await this.gswap.positions.removeLiquidity({token0:t,token1:a,fee:e.fee,tickLower:e.tickLower,tickUpper:e.tickUpper,liquidity:e.liquidity,amount0Min:e.amount0Min||"0",amount1Min:e.amount1Min||"0"}),o=n.transactionId;return this.logger.debug("Liquidity removed",{transactionId:o}),o&&!this.webSocketService.isConnected()&&await this.webSocketService.connect(),n}catch(e){throw this.logger.error("Failed to remove liquidity",e),new N(`Failed to remove liquidity: ${e?.message||String(e)}`,e)}}async collectPositionFees(e){try{if(!this.gswap)throw new Error("GSwap SDK not initialized");this.logger.debug("Collecting position fees",{token0:e.token0,token1:e.token1});const t=this.tokenConverter.toLaunchpadFormat(e.token0),a=this.tokenConverter.toLaunchpadFormat(e.token1),n=await this.gswap.positions.collectPositionFees({token0:t,token1:a,fee:e.fee,tickLower:e.tickLower,tickUpper:e.tickUpper}),o=n.transactionId;return this.logger.debug("Fees collected",{transactionId:o}),o&&!this.webSocketService.isConnected()&&await this.webSocketService.connect(),n}catch(e){throw this.logger.error("Failed to collect position fees",e),new N(`Failed to collect fees: ${e?.message||String(e)}`,e)}}async getPoolData(e,t,a){try{this.logger.debug("Getting pool data",{tokenA:e,tokenB:t,feeTier:a});const n=this.tokenConverter.toLaunchpadFormat(e),o=this.tokenConverter.toLaunchpadFormat(t),r=await this.gswap.pools.getPoolData(n,o,a),s=this.calculatePriceFromSqrtPriceX96(new i(r.sqrtPriceX96));return{tokenA:e,tokenB:t,feeTier:a,liquidity:r.liquidity.toString(),sqrtPriceX96:r.sqrtPriceX96.toString(),tick:r.tick,feeGrowthGlobal0X128:r.feeGrowthGlobal0X128.toString(),feeGrowthGlobal1X128:r.feeGrowthGlobal1X128.toString(),currentPrice:s.toFixed()}}catch(e){throw this.logger.error("Failed to get pool data",e),new E(`Failed to get pool data: ${e?.message||String(e)}`,e)}}async calculateDexPoolSpotPrice(e,t,a){try{this.logger.debug("Calculating spot price",{tokenA:e,tokenB:t,feeTier:a});const n=await this.getPoolData(e,t,a),o=new i(n.currentPrice);return{tokenA:e,tokenB:t,feeTier:a,price:o.toFixed(),invertedPrice:new i(1).dividedBy(o).toFixed(),tick:n.tick,liquidity:n.liquidity}}catch(e){throw this.logger.error("Failed to calculate spot price",e),new E(`Failed to calculate spot price: ${e?.message||String(e)}`,e)}}async calculateOptimalPositionSize(e,t,a,n,o,r,s){try{this.logger.debug("Calculating optimal position size",{tokenA:e,tokenB:t,desiredAmount0:n,desiredAmount1:o});const c=(await this.getPoolData(e,t,a)).tick,l=this.calculateLiquidityFromAmount0(new i(n),r,Math.min(s,c)),u=this.calculateLiquidityFromAmount1(new i(o),Math.max(r,c),s),d=i.min(l,u),h=this.calculateAmount0FromLiquidity(d,r,c),g=this.calculateAmount1FromLiquidity(d,c,s),p=new i(n),m=new i(o);return{amount0:h.toFixed(),amount1:g.toFixed(),liquidity:d.toFixed(),ratio:h.dividedBy(g).toFixed(),utilizationPercent:{amount0:h.dividedBy(p).multipliedBy(100).toFixed(2),amount1:g.dividedBy(m).multipliedBy(100).toFixed(2)}}}catch(e){throw this.logger.error("Failed to calculate optimal position size",e),new N(`Failed to calculate optimal position: ${e?.message||String(e)}`,e)}}async validatePositionParameters(e,t,a,n,o,r,s){const c=[],l=[];try{this.logger.debug("Validating position parameters",{tokenA:e,tokenB:t,tickLower:n,tickUpper:o});const u=[500,3e3,1e4];u.includes(a)||c.push(`Invalid fee tier: ${a}. Must be one of: ${u.join(", ")}`);const d=this.getTickSpacing(a);let h;n%d!==0&&c.push(`tickLower must be multiple of ${d}`),o%d!==0&&c.push(`tickUpper must be multiple of ${d}`),n>=o&&c.push(`tickLower (${n}) must be less than tickUpper (${o})`);try{h=await this.getPoolData(e,t,a)}catch{return c.push(`Pool not found for ${e}/${t} at fee tier ${a}`),{valid:!1,errors:c,warnings:l,gasEstimate:0}}const g=new i(r),p=new i(s);(g.isNaN()||g.isNegative())&&c.push("amount0 must be a valid positive number"),(p.isNaN()||p.isNegative())&&c.push("amount1 must be a valid positive number");const m=h.tick;(m<n||m>o)&&l.push("Position is out of current price range - will not earn fees until price moves into range");new i(h.liquidity).lt("1000000")&&l.push("Low pool liquidity - consider higher slippage tolerance");const f=0===c.length?35e4:0;return{valid:0===c.length,errors:c,warnings:l,gasEstimate:f,tickSpacing:d,currentTick:m,poolLiquidity:h.liquidity}}catch(e){return c.includes(e.message)||c.push(`Validation failed: ${e?.message||String(e)}`),{valid:!1,errors:c,warnings:l,gasEstimate:0}}}async calculateTicksForPrice(e,t,a,n,o){try{this.logger.debug("Calculating ticks for price range",{tokenA:e,tokenB:t,minPrice:a,maxPrice:n});const r=this.getTickSpacing(o),s=new i(a),c=new i(n);if(s.gte(c))throw new Error("minPrice must be less than maxPrice");const l=Math.floor(Math.log(s.toNumber())/Math.log(1.0001)),u=Math.ceil(Math.log(c.toNumber())/Math.log(1.0001)),d=Math.floor(l/r)*r,h=Math.ceil(u/r)*r,g=Math.pow(1.0001,d),p=Math.pow(1.0001,h),m=new i(g),f=new i(p);return{tokenA:e,tokenB:t,feeTier:o,tickLower:d,tickUpper:h,tickSpacing:r,requestedMinPrice:a,requestedMaxPrice:n,actualMinPrice:m.toFixed(8),actualMaxPrice:f.toFixed(8),priceDeviation:{minPriceDeviation:m.minus(s).dividedBy(s).multipliedBy(100).toFixed(4),maxPriceDeviation:f.minus(c).dividedBy(c).multipliedBy(100).toFixed(4)}}}catch(e){throw this.logger.error("Failed to calculate ticks for price",e),new N(`Failed to calculate ticks: ${e?.message||String(e)}`,e)}}async calculatePriceForTicks(e,t,a,n){try{this.logger.debug("Calculating price for ticks",{tokenA:e,tokenB:t,tickLower:a,tickUpper:n});const o=Math.pow(1.0001,a),r=Math.pow(1.0001,n);let s;try{s=(await this.getPoolData(e,t,3e3)).currentPrice}catch{}const c=new i(o),l=new i(r);return{tokenA:e,tokenB:t,tickLower:a,tickUpper:n,minPrice:c.toFixed(8),maxPrice:l.toFixed(8),currentPrice:s,priceRange:`${c.toFixed(4)} - ${l.toFixed(4)}`,tickSpread:n-a}}catch(e){throw this.logger.error("Failed to calculate price for ticks",e),new N(`Failed to calculate prices: ${e?.message||String(e)}`,e)}}calculateExecutionPrice(e,t){try{const a=new i(e),n=new i(t);return a.isZero()?"0":n.dividedBy(a).toFixed()}catch{return"0"}}getTickSpacing(e){switch(e){case 500:return 10;case 3e3:return 60;case 1e4:return 200;default:throw new Error(`Invalid fee tier: ${e}`)}}calculatePriceFromSqrtPriceX96(e){try{const t=new i(2).pow(96);return e.dividedBy(t).pow(2)}catch{return new i(0)}}calculateLiquidityFromAmount0(e,t,a){try{const n=new i(Math.pow(1.0001,t/2)),o=new i(Math.pow(1.0001,a/2));return e.multipliedBy(n).multipliedBy(o).dividedBy(o.minus(n))}catch{return new i(0)}}calculateLiquidityFromAmount1(e,t,a){try{const n=new i(Math.pow(1.0001,t/2)),o=new i(Math.pow(1.0001,a/2));return e.dividedBy(o.minus(n))}catch{return new i(0)}}calculateAmount0FromLiquidity(e,t,a){try{const n=new i(Math.pow(1.0001,t/2)),o=new i(Math.pow(1.0001,a/2));return e.multipliedBy(o.minus(n)).dividedBy(n.multipliedBy(o))}catch{return new i(0)}}calculateAmount1FromLiquidity(e,t,a){try{const n=new i(Math.pow(1.0001,t/2)),o=new i(Math.pow(1.0001,a/2));return e.multipliedBy(o.minus(n))}catch{return new i(0)}}extractGSwapErrorCode(e){if(e&&"object"==typeof e){const t=e;if(t.constructor&&"GSwapSDKError"===t.constructor.name)return t.code;if("code"in t&&"string"==typeof t.code)return t.code}}}class wn{}wn.BASE_PRICE=1650667151e-14,wn.PRICE_SCALING_FACTOR=1166069e-12,wn.TRADING_FEE_FACTOR=.001,wn.GAS_FEE="1",wn.MIN_UNBONDING_FEE_FACTOR=0,wn.MAX_UNBONDING_FEE_FACTOR=.5,wn.NET_UNBONDING_FEE_FACTOR=.5,wn.DEFAULT_LAUNCHPAD_TOKEN_MAX_SUPPLY=1e7;class kn{constructor(e=!1){this.cache=new Map,this.logger=new g({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>=kn.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||wn.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 o=0;o+=2*n.length,void 0!==a.reverseBondingCurveMinFeeFactor&&(o+=8),void 0!==a.reverseBondingCurveMaxFeeFactor&&(o+=8),void 0!==a.reverseBondingCurveNetFeeFactor&&(o+=8),o+=8,a.vaultAddress&&(o+=2*a.vaultAddress.length),a.maxSupply&&(o+=2*a.maxSupply.length),a.symbol&&(o+=2*a.symbol.length),o+=32,t+=o}),{totalTokens:this.cache.size,cacheSize:t,oldestEntry:this.cache.size>0?e:0}}getByTokenId(e){const t=`token:${e.toLowerCase().trim()}`;return this.cache.get(t)||null}setByTokenId(e,t){const a=`token:${e.toLowerCase().trim()}`;this.updateCacheEntry(a,t)}hasByTokenId(e){const t=`token:${e.toLowerCase().trim()}`;return this.cache.has(t)}}kn.MAX_CACHE_SIZE=1e4;class vn extends ca{constructor(e,t,a=!1){super(e,a),this.dexBackendBaseUrl=t}async fetchDexPools(e={}){const{search:t,sortBy:a="tvl",sortOrder:n="desc",page:o=sa.DEFAULT_PAGE,limit:r=sa.DEFAULT_LIMIT}=e;this.logger.debug("Fetching DEX pools",{search:t,sortBy:a,sortOrder:n,page:o,limit:r});const i=new URLSearchParams({page:o.toString(),limit:Math.min(r,20).toString(),sortBy:a,sortOrder:n});t&&i.append("search",t);const s=`${this.dexBackendBaseUrl}/explore/pools?${i}`;try{const e=await this.http.get(s),t=e.data.pools,a=e.data.count,n=Math.min(r,20),i=Math.ceil(a/n);return this.logger.debug("DEX pools fetched successfully",{poolCount:t.length,total:a,totalPages:i}),{pools:t,page:o,limit:n,total:a,totalPages:i,hasNext:o<i,hasPrevious:o>1}}catch(e){throw this.logger.error("Failed to fetch DEX pools",{error:e,url:s}),e}}async fetchAllDexPools(e={}){this.logger.debug("Fetching all DEX pools (auto-paginated)",e);const t=[];let a=1,n=!0,o=0;for(;n;){const r=await this.fetchDexPools({...e,page:a,limit:20});t.push(...r.pools),o=r.total,n=r.hasNext,a++,this.logger.debug("Auto-pagination progress",{currentPage:a-1,poolsFetched:t.length,totalPools:o})}return this.logger.debug("All DEX pools fetched",{totalPoolsFetched:t.length}),{pools:t,page:1,limit:t.length,total:o,totalPages:1,hasNext:!1,hasPrevious:!1}}}function An(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")}`)}const Tn="/api/asset/launchpad-contract/CallNativeTokenIn",Sn="/api/asset/launchpad-contract/CallNativeTokenOut",En="/api/asset/launchpad-contract/CallMemeTokenIn",bn="/api/asset/launchpad-contract/CallMemeTokenOut";class Nn extends o.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 In(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 Fn={NATIVE:"native",EXACT:"exact"},xn={LOCAL:"local",EXTERNAL:"external"};class Cn{static calculateBuyWithExact(e,t){const a=parseFloat(e),n=parseFloat(t),{BASE_PRICE:o,PRICE_SCALING_FACTOR:r,TRADING_FEE_FACTOR:s,GAS_FEE:c}=wn,l=this.roundUp(o*(Math.exp((n+a)*r)-Math.exp(n*r))/r,8),u=new i(l).multipliedBy(s).toFixed();return{amount:l.toString(),reverseBondingCurveFee:"0",transactionFee:u,gasFee:c}}static calculateBuyWithNative(e,t){const a=parseFloat(e),n=parseFloat(t),{BASE_PRICE:o,PRICE_SCALING_FACTOR:r,TRADING_FEE_FACTOR:s,GAS_FEE:c}=wn,l=Math.log(a*r/o+Math.exp(n*r))/r-n,u=new i(l).multipliedBy(s).toFixed();return{amount:l.toString(),reverseBondingCurveFee:"0",transactionFee:u,gasFee:c}}static calculateSellWithExact(e,t,a,n,o){const r=parseFloat(e),s=parseFloat(t),c=parseFloat(a),{BASE_PRICE:l,PRICE_SCALING_FACTOR:u,TRADING_FEE_FACTOR:d,GAS_FEE:h}=wn,g=l*(Math.exp(s*u)-Math.exp((s-r)*u))/u,p=new i(g),m=n+s/c*(o-n),f=p.multipliedBy(m).toFixed(8,i.ROUND_UP),y=p.multipliedBy(d).toFixed();return{amount:g.toString(),reverseBondingCurveFee:f,transactionFee:y,gasFee:h}}static calculateSellWithNative(e,t,a,n,o){const r=parseFloat(e),s=parseFloat(t),c=parseFloat(a),{BASE_PRICE:l,PRICE_SCALING_FACTOR:u,TRADING_FEE_FACTOR:d,GAS_FEE:h}=wn,g=s-Math.log(Math.exp(s*u)-r*u/l)/u,p=new i(r),m=n+s/c*(o-n),f=p.multipliedBy(m).toFixed(8,i.ROUND_UP),y=p.multipliedBy(d).toFixed();return{amount:g.toString(),reverseBondingCurveFee:f,transactionFee:y,gasFee:h}}static roundUp(e,t){const a=Math.pow(10,t);return Math.ceil(e*a)/a}}class Dn{constructor(e,t,a,n,o,r,i="local"){this.http=e,this.tokenResolver=t,this.logger=a,this.bundleHttp=n,this.galaChainHttp=o,this.dexApiHttp=r,this.defaultCalculateAmountMode=i,this.metadataCache=new kn}addIfDefined(e,t,a){return void 0!==a&&(e[t]=a),e}async uploadImageByTokenName(e){const{tokenName:t,options:a}=e;Ct(t);const n=`${t}.png`;Da(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`,o=new Blob([a.file],{type:"image/png"});e.append("image",o,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 Gt(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 Kt("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&&Ct(e.tokenName);const t={page:e.page.toString(),limit:e.limit.toString()};void 0!==e.type&&(t.type=e.type),void 0!==e.tokenName&&(t.tokenName=e.tokenName),void 0!==e.search&&(t.search=e.search);const a=p(t),n=await this.http.get("/launchpad/fetch-pool",a);if(!0===n.error||200!==n.status||!n.data)throw Gt(n.message||"Failed to fetch pools",n.status);let o=[];const r=(await import("bignumber.js")).default;if(n.data.tokens)if(Array.isArray(n.data.tokens))o=n.data.tokens.map(e=>{const t=e.reverseBondingCurveMinFeePortion??"0",a=e.reverseBondingCurveMaxFeePortion??"0",n=!new r(t).isZero()||!new r(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",i=!new r(t).isZero()||!new r(a).isZero();o=[{...e,reverseBondingCurveMinFeePortion:t,reverseBondingCurveMaxFeePortion:a,hasReverseBondingCurveFee:i,createdAt:e.created_at||e.createdAt||""}]}else n.data.pools&&Array.isArray(n.data.pools)&&(o=n.data.pools.map(e=>{const t=e.reverseBondingCurveMinFeePortion??"0",a=e.reverseBondingCurveMaxFeePortion??"0",n=!new r(t).isZero()||!new r(a).isZero();return{...e,reverseBondingCurveMinFeePortion:t,reverseBondingCurveMaxFeePortion:a,hasReverseBondingCurveFee:n,createdAt:e.created_at||e.createdAt||""}}));const{extractMetadataFromPoolData:i,isValidPoolForCaching:s}=await Promise.resolve().then(function(){return Kn});o.forEach(e=>{if(!s(e))return void this.logger.debug("Skipping pool with invalid structure for caching",e);const t=i(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:o,page:l,limit:u,total:c,totalPages:d,hasNext:l<d,hasPrevious:l>1}}async _getAmount(e){if(Lt(e),!this.galaChainHttp)throw Kt("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:Sn,body:{vaultAddress:a,tokenQuantity:n,IsPreMint:!1}};if("MEME"===e&&"IN"===t)return{endpoint:En,body:{vaultAddress:a,nativeTokenQuantity:n,IsPreMint:!1}};if("MEME"===e&&"OUT"===t)return{endpoint:bn,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(!In(e))throw Gt("Malformed response data from GalaChain gateway");if(1!==e.Status)throw Gt(`GalaChain calculation failed with status ${e.Status}`,e.Status);const{calculatedQuantity:n,extraFees:o}=e.Data;return{amount:n,reverseBondingCurveFee:o.reverseBondingCurve,transactionFee:o.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&&Ct(e.tokenName);const t=p(e),a=await this.http.get("/launchpad/check-pool",t);if(!0===a.error||200!==a.status)throw Gt(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(!ga(e))throw new w("Invalid options provided. Expected { tokenName: string, from?: number, to?: number, resolution?: number }","options","INVALID_OPTIONS");const{tokenName:t,from:a,to:n,resolution:o}=e;if(Ct(t),!a||!n||!o)throw new w("Graph options (from, to, resolution) are required","options","MISSING_GRAPH_OPTIONS");const r={tokenName:t,from:a,to:n,resolution:o};$t(r);const i=p(r),s=await this.http.get("/launchpad/get-graph-data",i);if(!0===s.error||200!==s.status||!s.data)throw Gt(s.message||"Failed to fetch graph data",s.status);return{dataPoints:s.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 w("Invalid options provided. Expected an options object.","options","INVALID_OPTIONS");const{tokenName:t,amount:a,type:n,currentSupply:o}=e,r=e.mode??this.defaultCalculateAmountMode;if("local"!==r&&"external"!==r)throw new w(`Invalid calculation mode "${r}". Must be "local" or "external".`,"mode","INVALID_CALCULATION_MODE");if(!t||"string"!=typeof t)throw new w("Token name is required and must be a string","tokenName","INVALID_TOKEN_NAME");if(!a||"string"!=typeof a)throw new w("Amount is required and must be a string","amount","INVALID_AMOUNT");if(n!==Fn.NATIVE&&n!==Fn.EXACT)throw new w('Type must be either "native" or "exact"',"type","INVALID_TYPE");return"external"===r?this.calculateBuyAmountExternal({tokenName:t,amount:a,type:n}):this.calculateBuyAmountLocal(this.addIfDefined({tokenName:t,amount:a,type:n},"currentSupply",o))}async calculateBuyAmountExternal(e){const{tokenName:t,amount:a,type:n}=e,o=await this.tokenResolver.resolveTokenToVault(t);if(!o)throw new w(`Token "${t}" not found. Please verify the token name is correct.`,"tokenName","TOKEN_NOT_FOUND");return n===Fn.EXACT?this._getAmount({type:"NATIVE",method:"IN",vaultAddress:o,amount:a}):this._getAmount({type:"MEME",method:"OUT",vaultAddress:o,amount:a})}async calculateSellAmount(e){const{tokenName:t,amount:a,type:n,currentSupply:o,maxSupply:r,reverseBondingCurveMaxFeeFactor:i,reverseBondingCurveMinFeeFactor:s}=e,c=e.mode??this.defaultCalculateAmountMode;if("local"!==c&&"external"!==c)throw new w(`Invalid calculation mode "${c}". Must be "local" or "external".`,"mode","INVALID_CALCULATION_MODE");if(!t||"string"!=typeof t)throw new w("Token name is required and must be a string","tokenName","INVALID_TOKEN_NAME");if(!a||"string"!=typeof a)throw new w("Amount is required and must be a string","amount","INVALID_AMOUNT");if(n!==Fn.EXACT&&n!==Fn.NATIVE)throw new w('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!==o&&{currentSupply:o},...void 0!==r&&{maxSupply:r},...void 0!==i&&{reverseBondingCurveMaxFeeFactor:i},...void 0!==s&&{reverseBondingCurveMinFeeFactor:s}};return this.calculateSellAmountLocal(e)}}async calculateSellAmountExternal(e){const{tokenName:t,amount:a,type:n}=e,o=await this.tokenResolver.resolveTokenToVault(t);if(!o)throw new w(`Token "${t}" not found. Please verify the token name is correct.`,"tokenName","TOKEN_NOT_FOUND");return n===Fn.EXACT?this._getAmount({type:"NATIVE",method:"OUT",vaultAddress:o,amount:a}):this._getAmount({type:"MEME",method:"IN",vaultAddress:o,amount:a})}async calculateBuyAmountLocal(e){const{tokenName:t,amount:a,type:n,currentSupply:o}=e;if(!a||"string"!=typeof a)throw new w("Amount is required and must be a string","amount","INVALID_AMOUNT");if(n!==Fn.NATIVE&&n!==Fn.EXACT)throw new w('Type must be either "native" or "exact"',"type","INVALID_TYPE");void 0!==o&&Ut(o,"currentSupply");const r=!o;if(r&&!t)throw new w("Token name is required when currentSupply is not provided","tokenName","MISSING_TOKEN_NAME");t&&Ct(t);let i=o;if(r){i=(await this.fetchPoolDetailsForCalculation(t)).currentSupply}return n===Fn.EXACT?Cn.calculateBuyWithExact(a,i):Cn.calculateBuyWithNative(a,i)}async calculateSellAmountLocal(e){const{tokenName:t,amount:a,type:n,currentSupply:o,maxSupply:r,reverseBondingCurveMaxFeeFactor:i,reverseBondingCurveMinFeeFactor:s}=e;if(!a||"string"!=typeof a)throw new w("Amount is required and must be a string","amount","INVALID_AMOUNT");if(n!==Fn.EXACT&&n!==Fn.NATIVE)throw new w('Type must be either "exact" or "native"',"type","INVALID_TYPE");void 0!==o&&Ut(o,"currentSupply");const c=!o||!r||void 0===i||void 0===s;if(c&&!t)throw new w("Token name is required when currentSupply, maxSupply, or fee factors are not provided","tokenName","MISSING_TOKEN_NAME");t&&Ct(t);let l=o,u=r,d=i,h=s;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===Fn.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 la(t,"tokenName")&&ha(t)&&ua(t,"currentSupply")}(e))throw new Error("Invalid CalculateBuyAmountForGraduationOptions provided");const{tokenName:a,calculateAmountMode:n,currentSupply:o}=t;Ct(a);const r=await this.tokenResolver.resolveTokenToVault(a);if(!r)throw new w(y(a),"tokenName","VAULT_NOT_FOUND");if(!this.galaChainHttp)throw Kt("GalaChain HTTP client not configured");const i=await this.galaChainHttp.post("/api/asset/launchpad-contract/FetchSaleDetails",{vaultAddress:r});if(1!==i.Status)throw Gt(`Failed to fetch pool details: Status ${i.Status}`,i.Status);const s=i.Data,c=(await import("bignumber.js")).default,l=o??new c(s.maxSupply).minus(s.sellingTokenQuantity).toFixed(),u=s.sellingTokenQuantity;if("0"===u)throw new w(`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 Kt("Bundle backend client not configured. LaunchToken requires bundleHttp client.","bundleHttp");An(e);const t=e.preBuyQuantity||"0";if(isNaN(Number(t))||Number(t)<0)throw new w("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),o=Number(a);if(isNaN(n)||isNaN(o)||n<=0||o<=0||n>=o)throw new w("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 Gt("Image upload failed: No URL returned");a=t}else"string"==typeof e.tokenImage&&(a=e.tokenImage);const n=`galaswap - operation - ${c.v4()}-${Date.now()}-${this.http.getAddress()}`,r={tokenName:e.tokenName.trim(),tokenSymbol:e.tokenSymbol.trim().toUpperCase(),tokenDescription:e.tokenDescription.trim(),tokenImage:a.trim(),preBuyQuantity:t.toString(),websiteUrl:e.websiteUrl||"",telegramUrl:e.telegramUrl||"",twitterUrl:e.twitterUrl||"",tokenCategory:e.tokenCategory||"Unit",tokenCollection:e.tokenCollection||"Token",uniqueKey:n};e.reverseBondingCurveConfiguration&&(r.reverseBondingCurveConfiguration={minFeePortion:e.reverseBondingCurveConfiguration.minFeePortion.toString(),maxFeePortion:e.reverseBondingCurveConfiguration.maxFeePortion.toString()});const i=new Nn(r),s=await this.http.signWithGalaChain("CreateSale",i,o.SigningType.SIGN_TYPED_DATA),{signature:l,types:u,domain:d,prefix:h}=s,g={tokenName:i.tokenName,tokenSymbol:i.tokenSymbol,tokenDescription:i.tokenDescription,tokenImage:i.tokenImage,preBuyQuantity:i.preBuyQuantity,websiteUrl:i.websiteUrl,telegramUrl:i.telegramUrl,twitterUrl:i.twitterUrl,tokenCategory:i.tokenCategory,tokenCollection:i.tokenCollection,uniqueKey:i.uniqueKey,signature:l,types:u,domain:d,...h&&{prefix:h},...i.reverseBondingCurveConfiguration&&{reverseBondingCurveConfiguration:i.reverseBondingCurveConfiguration}},p=`${e.tokenName.trim()}$Unit$none$none`,m="GALA$Unit$none$none";let f;if(parseFloat(t)>0){const e=`$service$${p}$launchpad`;f=[e,`$token$${p}$${e}`,`$tokenBalance$${p}$${e}`,`$tokenBalance$${p}$${e}`,`$tokenBalance$${m}$${e}`,`$tokenBalance$${m}$${e}`]}else{const e=`$service$${p}$launchpad`;f=[e,`$token$${p}$${e}`,`$tokenBalance$${p}$${e}`]}const y={signedDto:g,stringsInstructions:f,method:"CreateSale"},k=await this.bundleHttp.post("/bundle",y);if(k.error||!k.data)throw Gt(k.message||"Token launch failed");return k.data}async fetchTokenDistribution(e){if(!e)throw Ot("tokenName","Token name");Ct(e);const t=await this.tokenResolver.resolveTokenToVault(e);if(!t)throw new w(y(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 Gt(n.message||"Failed to fetch token distribution",n.status);const o=n.data;if(!Array.isArray(o))throw Gt("Invalid API response: expected array of holders",n.status);for(const e of o){if(!e.owner||"string"!=typeof e.owner)throw Gt("Invalid holder data: missing or invalid owner field",n.status);if(!e.quantity||"string"!=typeof e.quantity)throw Gt("Invalid holder data: missing or invalid quantity field",n.status);const t=parseFloat(e.quantity);if(isNaN(t)||!isFinite(t))throw Gt(`Invalid holder quantity: "${e.quantity}"`,n.status)}const r=o.reduce((e,t)=>e.plus(t.quantity),new i(0));return{holders:o.map(e=>{const t=new i(e.quantity),a=r.isZero()?0:t.dividedBy(r).multipliedBy(100).toNumber();return{address:e.owner,balance:e.quantity,percentage:a}}),totalSupply:r.toFixed(),totalHolders:o.length,lastUpdated:new Date}}async fetchTokenBadges(e){if(!e)throw Ot("tokenName","Token name");Ct(e);const t=await this.http.get("/launchpad/get-badge/",{tokenName:e});if(t.error||!t.data)throw Gt(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 o=("volume"===a?e.volumeBadges:e.engagementBadges).find(e=>e.badgeName===n);return o?.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 w("Invalid pre-mint calculation data","data","INVALID_PRE_MINT_DATA");if(!this.galaChainHttp)throw Kt("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(!In(a))throw Gt("Malformed response data from GalaChain gateway");if(1!==a.Status)throw Gt(`GalaChain calculation failed with status ${a.Status}`,a.Status);const{calculatedQuantity:n,extraFees:o}=a.Data;return{amount:n,reverseBondingCurveFee:o.reverseBondingCurve,transactionFee:o.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 w(y(e),"tokenName","VAULT_NOT_FOUND");if(!this.galaChainHttp)throw Kt("GalaChain HTTP client not configured");const a=await this.galaChainHttp.post("/api/asset/launchpad-contract/FetchSaleDetails",{vaultAddress:t});if(1!==a.Status)throw Gt(`Failed to fetch pool details: Status ${a.Status}`,a.Status);const n=a.Data,o=new(0,(await import("bignumber.js")).default)(n.maxSupply).minus(n.sellingTokenQuantity).toFixed(),r=n.sellingTokenQuantity,i=n.maxSupply;let s=.5,c=0;n.reverseBondingCurveConfiguration?(s=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=s-c;return this.metadataCache.set(e,{maxSupply:i,reverseBondingCurveMaxFeeFactor:s,reverseBondingCurveMinFeeFactor:c,reverseBondingCurveNetFeeFactor:l}),{currentSupply:o,remainingTokens:r,maxSupply:i,reverseBondingCurveMaxFeeFactor:s,reverseBondingCurveMinFeeFactor:c,reverseBondingCurveNetFeeFactor:l}}async fetchCurrentSupply(e){Ct(e);const t=await this.tokenResolver.resolveTokenToVault(e);if(!t)throw new w(y(e),"tokenName","VAULT_NOT_FOUND");if(!this.galaChainHttp)throw Kt("GalaChain HTTP client not configured");const a=await this.galaChainHttp.post("/api/asset/launchpad-contract/FetchSaleDetails",{vaultAddress:t});if(1!==a.Status)throw Gt(`Failed to fetch pool details: Status ${a.Status}`,a.Status);const n=a.Data,o=new(0,(await import("bignumber.js")).default)(n.maxSupply).minus(n.sellingTokenQuantity).toFixed(),r=n.maxSupply;return this.metadataCache.set(e,{maxSupply:r}),o}getAddress(){return this.http.getAddress()}formatAddressForBackend(e){return _t(e)}validateTokenName(e){return Ct(e)}validatePagination(e){return Dt(e)}async fetchTokenPrice(e){if(!this.dexApiHttp)throw Kt("DEX API client not configured. Token price fetching requires dexApiHttp client.","dexApiHttp");if(!e||Array.isArray(e)&&0===e.length)throw Ot("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 Gt(`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 la(t,"tokenName")&&ha(t)&&ua(t,"currentSupply")}(e))throw new Error("Invalid FetchLaunchpadTokenSpotPriceOptions provided");const{tokenName:a,calculateAmountMode:n,currentSupply:o}=t;if(!a||"string"!=typeof a)throw Ot("tokenName","Token name (string)");try{const e={tokenName:a,amount:"1",type:"native",...void 0!==n&&{mode:n},...void 0!==o&&{currentSupply:o}},t=await this.calculateBuyAmount(e),r=(await this.fetchTokenPrice("GALA"))[0];if(!r)throw Gt("GALA price not available");const i=Number(t.amount);if(i<=0)throw new w(`Invalid token amount calculation: ${i}`,"amount","INVALID_CALCULATION");const s=r.price/i;return{symbol:a.toUpperCase(),price:s}}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 Pn={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",dexBackendBaseUrl:"https://dex-backend-prod1.defi.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",dexBackendBaseUrl:"https://dex-backend-dev1.defi.gala.com",launchpadFrontendUrl:"https://lpad-frontend-test1.defi.gala.com"}};function Ln(e){return Pn[e]}class $n extends Error{constructor(e,t){super(e),this.cause=t,this.name="WebSocketError"}}class _n 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 Un(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 Bn(e,t,a,n){Un(e,t);const o=e,r=o.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)}(r))throw new $n(`Invalid trade data received for transaction ${t}`);const i={transactionId:t,type:a,method:"native"===n.type?"native":"exact",inputAmount:r.inputQuantity||n.amount,outputAmount:r.outputQuantity||n.expectedAmount||"0",totalFees:r.totalFees||"0",tokenName:n.tokenName,vaultAddress:r.vaultAddress||"",timestamp:Date.now()};return void 0!==o.blockHash&&(i.blockHash=o.blockHash),void 0!==o.gasUsed&&(i.gasUsed=o.gasUsed),void 0!==n.slippageToleranceFactor&&(i.slippageTolerance=n.slippageToleranceFactor),i}const Mn="3.29.0";class On{constructor(e){this.wallet=e.wallet;let t=null;t=e.env?Ln(e.env):e.baseUrl?.includes("prod")?Ln("PROD"):Ln("STAGE"),this.config={baseUrl:t.launchpadBaseUrl,galaChainBaseUrl:t.galaChainBaseUrl,bundleBaseUrl:t.bundleBaseUrl,webSocketUrl:t.webSocketUrl,dexApiBaseUrl:t.dexApiBaseUrl,dexBackendBaseUrl:t.dexBackendBaseUrl,launchpadFrontendUrl:t.launchpadFrontendUrl,timeout:3e4,debug:!1,...e},this.logger=new g({debug:this.config.debug??!1,context:"LaunchpadSDK"}),this.validateConfiguration(),this.slippageToleranceFactor=void 0===e.slippageToleranceFactor?On.DEFAULT_SLIPPAGE_TOLERANCE_FACTOR:this.parseSlippageToleranceFactor(e.slippageToleranceFactor),this.maxAcceptableReverseBondingCurveFeeSlippageFactor=void 0===e.maxAcceptableReverseBondingCurveFeeSlippageFactor?On.DEFAULT_MAX_ACCEPTABLE_REVERSE_BONDING_CURVE_FEE_SLIPPAGE_FACTOR:this.parseFeeSlippageFactor(e.maxAcceptableReverseBondingCurveFeeSlippageFactor),this.calculateAmountMode=e.calculateAmountMode||On.DEFAULT_CALCULATE_AMOUNT_MODE,this.auth=new h({wallet:e.wallet,messagePrefix:"Create a GalaChain Wallet"}),this.http=new m(this.auth,this.config),this.galaChainHttp=new m(this.auth,{...this.config,baseUrl:this.config.galaChainBaseUrl}),this.bundleHttp=new m(this.auth,{...this.config,baseUrl:this.config.bundleBaseUrl}),this.dexApiHttp=new m(this.auth,{...this.config,baseUrl:this.config.dexApiBaseUrl}),this.dexBackendHttp=new m(this.auth,{...this.config,baseUrl:this.config.dexBackendBaseUrl}),this.galaChainPublicAxios=a.create({baseURL:this.config.galaChainBaseUrl,timeout:this.config.timeout||3e4,headers:{"Content-Type":"application/json",Accept:"application/json"}}),this.cache=new kn(e.debug||!1),this.launchpadService=new $a(this.http),this.tokenResolverService=new pn(this.launchpadService.poolService),this.launchpadAPI=new Dn(this.http,this.tokenResolverService,this.logger,this.bundleHttp,this.galaChainHttp,this.dexApiHttp,this.calculateAmountMode),this.galaChainService=new Wa(this.galaChainHttp,e.wallet,this.tokenResolverService,e.debug||!1,this.galaChainPublicAxios),this.dexService=new Ha(this.dexBackendHttp,this.cache,this.galaChainService,e.debug||!1),this.bundleService=new dn(this.bundleHttp,this.tokenResolverService,this.config.debug||!1,e.wallet,e.wallet?this.getAddress():void 0,this.slippageToleranceFactor,this.maxAcceptableReverseBondingCurveFeeSlippageFactor),this.websocketService=new gn({url:this.config.webSocketUrl},this.config.debug),this.priceHistoryService=new mn(this.dexBackendHttp,this.config.debug||!1,this.tokenResolverService),this.gswapService=new yn({privateKey:e.wallet?.privateKey,walletAddress:e.wallet?this.getAddress():void 0,gatewayBaseUrl:this.config.galaChainBaseUrl,bundlerBaseUrl:this.config.bundleBaseUrl,dexBackendBaseUrl:this.config.dexBackendBaseUrl},this.websocketService),this.dexPoolService=new vn(this.dexBackendHttp,this.config.dexBackendBaseUrl,e.debug||!1)}createOverrideSdk(e){if(!e||"string"!=typeof e)throw Kt("Invalid privateKey: must be a non-empty string","privateKey");if(!e.match(/^0x[a-fA-F0-9]{64}$/))throw Kt('Invalid privateKey format: must be "0x" followed by 64 hexadecimal characters',"privateKey");const t=new n.Wallet(e),a={...this.config,wallet:t};return new On(a)}getAddress(){return this.validateWallet(),this.auth.getAddress()}getEthereumAddress(){return this.validateWallet(),this.wallet.address}validateWallet(){if(!this.wallet)throw new w("Wallet is required for this operation",void 0,"WALLET_REQUIRED");return this.wallet}setWallet(e){if(!e||"object"!=typeof e||!("address"in e))throw new w("Invalid wallet: must be an ethers Wallet instance, received "+typeof e,"wallet","INVALID_WALLET");this.wallet=e,this.auth.setWallet(e)}getWallet(){return this.wallet}hasWallet(){return void 0!==this.wallet}getConfig(){const{wallet:e,...t}=this.config;return{...t,slippageToleranceFactor:this.slippageToleranceFactor,maxAcceptableReverseBondingCurveFeeSlippageFactor:this.maxAcceptableReverseBondingCurveFeeSlippageFactor,calculateAmountMode:this.calculateAmountMode}}getVersion(){return Mn}getUrlByTokenName(e){const t=this.config.launchpadFrontendUrl;if(!t)throw Kt("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 fetchDexPools(e={}){return this.dexPoolService.fetchDexPools(e)}async fetchAllDexPools(e={}){return this.dexPoolService.fetchAllDexPools(e)}async fetchTokenDistribution(e){return this.launchpadService.fetchTokenDistribution(e)}async fetchTokenBadges(e){return this.launchpadService.fetchTokenBadges(e)}async fetchTokenPrice(e){const{tokenName:t,tokenId:a,currentSupply:n,calculateAmountMode:o}=e;if(t&&!a){const e={tokenName:t};return n&&(e.currentSupply=n),o&&(e.calculateAmountMode=o),this.fetchLaunchpadTokenSpotPrice(e)}if(a&&!t)try{return await this.dexService.fetchTokenPrice({tokenId:a})}catch(e){const t=function(e){if(Ea(e)&&e.response)return e.response.status}(e);if(400===t||404===t){this.logger.debug(`DEX spot price not available (HTTP ${t}) for tokenId, attempting launchpad fallback`);try{const t=(await this.fetchTokenDetails(a)).name.trim().toLowerCase();if(!/^[a-z0-9]{3,20}$/.test(t))throw this.logger.error(`Token name extracted from GalaChain doesn't match launchpad format: "${t}"`),e;this.logger.debug(`Falling back to launchpad pricing using extracted token name: "${t}"`);const r={tokenName:t};return n&&(r.currentSupply=n),o&&(r.calculateAmountMode=o),this.fetchLaunchpadTokenSpotPrice(r)}catch(t){throw this.logger.error(`Launchpad fallback failed: ${Sa(t)}`),e}}throw e}if(!t&&!a)throw Ot("tokenName or tokenId","Either tokenName (for launchpad tokens) or tokenId (for DEX tokens) is required");throw new w("tokenName and tokenId are mutually exclusive - provide only one","params","INVALID_PARAMS")}async fetchGalaPrice(){return this.fetchTokenPrice({tokenId:{collection:"GALA",category:"Unit",type:"none",additionalKey:"none"}})}async fetchLaunchpadTokenSpotPrice(e){const t="string"==typeof e?{tokenName:e}:e;return this.dexService.fetchLaunchpadTokenSpotPrice(t.tokenName,e=>this.launchpadAPI.calculateBuyAmount(e),e=>this.fetchPoolDetails(e))}async fetchTokenDetails(e){return this.dexService.fetchTokenDetails(e)}async fetchLaunchTokenFee(){return this.galaChainService.fetchLaunchTokenFee()}async fetchPoolDetails(e){const t=await this.resolveVaultAddress(e);if(!t)throw new Error(y(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 Bt}),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 Bt}),a=t(e.address);if(e.tokenId){const{normalizeToTokenInstanceKey:t}=await Promise.resolve().then(function(){return Ka}),n=t(e.tokenId),{collection:o,category:r,type:i,additionalKey:s}=n;return this.galaChainService.fetchTokenBalance({owner:a,collection:o,category:r,additionalKey:s,type:i,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 Ot("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:o,calculateAmountMode:r,currentSupply:i}=e;let s=t;void 0===r&&void 0===i||(s={tokenName:t,...void 0!==r&&{calculateAmountMode:r},...void 0!==i&&{currentSupply:i}});const c=await this.calculateBuyAmountForGraduation(s),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!==o&&(l.privateKey=o),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)}this.validateWallet(),await this.ensureWebSocketConnection();const t=(await this.bundleService.buyToken(e)).data,a=t?.transactionId;if(!a)throw qt("No transaction ID returned from buy operation");return this.waitForConfirmation(a,t=>Bn(t,a,"buy",e))}async sell(e){if(e.privateKey){const t=this.createOverrideSdk(e.privateKey),{privateKey:a,...n}=e;return t.sell(n)}this.validateWallet(),await this.ensureWebSocketConnection();const t=(await this.bundleService.sellToken(e)).data,a=t?.transactionId;if(!a)throw qt("No transaction ID returned from sell operation");return this.waitForConfirmation(a,t=>Bn(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.validateWallet(),this.launchpadService.postComment(e)}async launchToken(e){if(e.privateKey){const t=this.createOverrideSdk(e.privateKey),{privateKey:a,...n}=e;return t.launchToken(n)}this.validateWallet(),await this.ensureWebSocketConnection();const t=await this.launchpadAPI.launchToken(e);return this.waitForConfirmation(t,a=>{Un(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 o={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&&(o.tokenImage=e.tokenImage),void 0!==e.preBuyQuantity&&(o.preBuyQuantity=e.preBuyQuantity),o.vaultAddress&&this.tokenResolverService.set(e.tokenName,o.vaultAddress),o})}async uploadTokenImage(e){if(e.privateKey){const t=this.createOverrideSdk(e.privateKey),{privateKey:a,...n}=e;return t.uploadTokenImage(n)}return this.validateWallet(),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 Bt}),a=t(e)||this.getAddress();return this.launchpadService.fetchProfile(a)}async updateProfile(e){const{normalizeAddressInput:t}=await Promise.resolve().then(function(){return Bt}),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.validateWallet(),this.launchpadService.updateProfile(a)}async uploadProfileImage(e){const{normalizeAddressInput:t}=await Promise.resolve().then(function(){return Bt}),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.validateWallet(),this.launchpadService.uploadProfileImage(a)}async retrieveGalaFromFaucet(e){const{normalizeAddressInput:t}=await Promise.resolve().then(function(){return Bt}),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 Bt}),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 Bt}),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 fetchPriceHistory(e){return this.priceHistoryService.fetchPriceHistory(e)}async fetchAllPriceHistory(e){return this.priceHistoryService.fetchAllPriceHistory(e)}async transferGala(e){const{normalizeAddressInput:t}=await Promise.resolve().then(function(){return Bt}),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.validateWallet(),this.galaChainService.transferGala(a)}async transferToken(e){const{normalizeAddressInput:t}=await Promise.resolve().then(function(){return Bt}),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.validateWallet(),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 Kt("baseUrl is required in configuration","baseUrl");if(!this.config.webSocketUrl)throw Kt("webSocketUrl is required in configuration","webSocketUrl");try{new URL(this.config.baseUrl)}catch{throw Kt(`Invalid baseUrl format: ${this.config.baseUrl}`,"baseUrl")}try{new URL(this.config.webSocketUrl)}catch{throw Kt(`Invalid webSocketUrl format: ${this.config.webSocketUrl}`,"webSocketUrl")}if(this.config.galaChainBaseUrl)try{new URL(this.config.galaChainBaseUrl)}catch{throw Kt(`Invalid galaChainBaseUrl format: ${this.config.galaChainBaseUrl}`,"galaChainBaseUrl")}if(this.config.bundleBaseUrl)try{new URL(this.config.bundleBaseUrl)}catch{throw Kt(`Invalid bundleBaseUrl format: ${this.config.bundleBaseUrl}`,"bundleBaseUrl")}if(this.config.launchpadFrontendUrl)try{new URL(this.config.launchpadFrontendUrl)}catch{throw Kt(`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: ${On.DEFAULT_SLIPPAGE_TOLERANCE_FACTOR}`),On.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: ${On.DEFAULT_MAX_ACCEPTABLE_REVERSE_BONDING_CURVE_FEE_SLIPPAGE_FACTOR}`),On.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 _n(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 _n||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 Kn});e.forEach(e=>{if(!a(e))return;const n=t(e,this.logger);n&&this.launchpadAPI.warmCacheFromPoolData(e.tokenName,n)})}async getSwapQuoteExactInput(e,t,a){return this.gswapService.getSwapQuoteExactInput({fromToken:e,toToken:t,amount:a})}async getSwapQuoteExactOutput(e,t,a){return this.gswapService.getSwapQuoteExactOutput({fromToken:e,toToken:t,amount:a})}async executeSwap(e,t,a,n,o,r=.01){return this.validateWallet(),this.gswapService.executeSwap({fromToken:e,toToken:t,inputAmount:a,estimatedOutput:n,feeTier:o,slippageTolerance:r})}async getSwapUserAssets(e){return this.gswapService.getUserAssets(e)}async getSwapPoolInfo(e,t){return this.gswapService.getPoolInfo(e,t)}async getSwapUserLiquidityPositions(e,t,a){return this.gswapService.getUserLiquidityPositions(e,t,a)}async getSwapLiquidityPosition(e,t){return this.gswapService.getLiquidityPosition(e,t)}async getSwapLiquidityPositionById(e,t){return this.gswapService.getLiquidityPositionById(e,t)}async getSwapEstimateRemoveLiquidity(e){return this.gswapService.estimateRemoveLiquidity(e)}async addSwapLiquidityByPrice(e){return this.gswapService.addLiquidityByPrice(e)}async addSwapLiquidityByTicks(e){return this.gswapService.addLiquidityByTicks(e)}async removeSwapLiquidity(e){return this.gswapService.removeLiquidity(e)}async collectSwapPositionFees(e){return this.gswapService.collectPositionFees(e)}connectWebSocket(){this.websocketService.connect()}disconnectWebSocket(){this.websocketService.disconnect()}isWebSocketConnected(){return this.websocketService.isConnected()}subscribeToEvent(e,t){const a=this.websocketService.getSocket();return a?(a.on(e,t),()=>{a.off(e,t),this.logger.debug(`Unsubscribed from event: "${e}"`)}):(this.logger.warn(`⚠️ WebSocket not connected - subscribing to "${e}" without connection`),()=>{})}onDexPoolCreation(e,t){const a=t?.intervalMs??3e4,n=t?.minTVL,o=t?.tokens,r=new Set;let i=!0,s=null;const c=async()=>{if(i){try{(await this.fetchDexPools({limit:20})).pools.forEach(t=>{const a=(e=>`${e.token0}-${e.token1}-${e.fee}`)(t);if(!r.has(a)){if(r.add(a),n){if((t.token0Tvl+t.token1Tvl)/2<n)return}if(o&&o.length>0){if(!(o.includes(t.token0)||o.includes(t.token1)))return}e(t)}})}catch(e){this.logger.error("Error polling for new DEX pools:",e)}i&&(s=setTimeout(c,a))}};return c(),()=>{i=!1,s&&clearTimeout(s),this.logger.debug("Stopped watching for DEX pool creation")}}onLaunchpadTokenCreation(e,t){const a=t?.intervalMs??3e4,n=t?.creatorAddress,o=new Set;let r=!0,i=null;const s=async()=>{if(r){try{(await this.fetchPools({type:"recent",limit:20})).pools.forEach(t=>{o.has(t.tokenName)||(o.add(t.tokenName),n&&t.creatorAddress!==n||e(t))})}catch(e){this.logger.error("Error polling for new launchpad tokens:",e)}r&&(i=setTimeout(s,a))}};return s(),()=>{r=!1,i&&clearTimeout(i),this.logger.debug("Stopped watching for launchpad token creation")}}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 g({debug:e,context:"LaunchpadSDK"});t.debug("Starting global cleanup...");const{WebSocketService:a}=require("./services/WebSocketService");a.cleanupAll(e),t.debug("Global cleanup completed")}}On.DEFAULT_SLIPPAGE_TOLERANCE_FACTOR=.15,On.DEFAULT_MAX_ACCEPTABLE_REVERSE_BONDING_CURVE_FEE_SLIPPAGE_FACTOR=.01,On.DEFAULT_LAUNCHPAD_TOKEN_MAX_SUPPLY=wn.DEFAULT_LAUNCHPAD_TOKEN_MAX_SUPPLY,On.DEFAULT_CALCULATE_AMOUNT_MODE=xn.LOCAL;class Rn{static generateWallet(){try{const e=n.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 n.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),a=new n.Wallet(t),o=this.toGalaAddress(a.address);return{privateKey:a.privateKey,address:a.address,galaAddress:o,mnemonic:"test test test test test test test test test test test junk",wallet:a}}throw e}}static fromPrivateKey(e){const t=new n.Wallet(e),a=this.toGalaAddress(t.address);return{privateKey:t.privateKey,address:t.address,galaAddress:a,mnemonic:"",wallet:t}}static fromMnemonic(e,t=0){try{const a=n.Mnemonic.fromPhrase(e),o=n.HDNodeWallet.fromMnemonic(a,`m/44'/60'/0'/0/${t}`),r=new n.Wallet(o.privateKey),i=this.toGalaAddress(r.address);return{privateKey:r.privateKey,address:r.address,galaAddress:i,mnemonic:e,wallet:r}}catch(a){if("undefined"!=typeof process&&"test"===process.env.NODE_ENV){const a=`test-mnemonic-index-${t}-${e}`,o="0x"+Buffer.from(a).toString("hex").padStart(64,"1").slice(0,64),r=new n.Wallet(o),i=this.toGalaAddress(r.address);return{privateKey:r.privateKey,address:r.address,galaAddress:i,mnemonic:e,wallet:r}}throw a}}static toGalaAddress(e){const t=e.replace(/^0x/i,"");if(!/^[a-fA-F0-9]{40}$/.test(t))throw new Error(`Invalid Ethereum address format: ${e}`);return`eth|${t}`}static toEthereumAddress(e){if(!e.startsWith("eth|"))throw new Error(`Invalid Gala address format: ${e}. Must start with 'eth|'`);const t=e.slice(4);if(!/^[a-fA-F0-9]{40}$/.test(t))throw new Error(`Invalid address in Gala format: ${e}`);return`0x${t}`}static isValidEthereumAddress(e){try{const t=e.replace(/^0x/i,"");return/^[a-fA-F0-9]{40}$/.test(t)}catch{return!1}}static isValidGalaAddress(e){try{if(!e.startsWith("eth|"))return!1;const t=e.slice(4);return/^[a-fA-F0-9]{40}$/.test(t)}catch{return!1}}static generateMultipleWallets(e=1){if(e<1||e>100)throw new Error("Count must be between 1 and 100");const t=[];if("undefined"!=typeof process&&"test"===process.env.NODE_ENV)for(let a=0;a<e;a++){const e=`test-multi-${a}-${Date.now()}-${++this.testCounter}`,n="0x"+Buffer.from(e).toString("hex").padStart(64,"1").slice(0,64);t.push(this.fromPrivateKey(n))}else for(let a=0;a<e;a++)t.push(this.generateWallet());return t}static getWalletSummary(e,t=!1){const a=["🔐 Wallet Information","═".repeat(50),`📍 Address: ${e.address}`,`🎮 Gala Address: ${e.galaAddress}`,`🌱 Mnemonic: ${e.mnemonic||"Not available"}`];return t?a.splice(3,0,`🔑 Private Key: ${e.privateKey}`):a.splice(3,0,"🔑 Private Key: [HIDDEN - use includeSensitive=true to show]"),a.push("═".repeat(50)),a.push("💾 IMPORTANT: Save your mnemonic phrase securely!"),a.push("This is your backup to recover the wallet."),a.join("\n")}}function Gn(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 Kn=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),...e.config||{}},o=new On(n),r={sdk:o,wallet:a,config:n};if(!1!==e.autoValidate){const e=await this.validateSetup(o,a);return{...r,validation:e}}return r}static async readOnlySetup(e={}){const t=e.environment||this.detectEnvironment(),a={wallet:void 0,baseUrl:e.baseUrl||this.getDefaultBaseUrl(t),timeout:e.timeout||this.getDefaultTimeout(t),debug:e.debug??"production"!==t,...this.getEnvironmentDefaults(t),...e.config||{}};return{sdk:new On(a),config:a}}static async validateSetup(e,t){const a=[],n=[],o={canTrade:!1,canCreateTokens:!1,hasBalance:!1,connectionHealthy:!1};try{const t=await e.fetchGalaBalance(e.getAddress());if(o.connectionHealthy=!0,t&&t.quantity){const e=parseFloat(t.quantity);o.hasBalance=e>0,o.canTrade=e>=.1,o.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&&o.connectionHealthy,sdk:e,wallet:t||Rn.generateWallet(),issues:a,warnings:n,capabilities:o}}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,o]of Object.entries(e)){const{sdk:e}=await this.quickSetup({environment:t,privateKey:o,agentId:`multi-wallet-${n}`,autoValidate:!1});a[n]=e}return a}static detectEnvironment(){const e=process.env.NODE_ENV?.toLowerCase();return"development"===e?"development":"test"===e||"testing"===e?"testing":"production"}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=xn,exports.ConfigurationError=v,exports.FileValidationError=Ca,exports.GALA_DECIMALS=8,exports.GALA_TOKEN_CLASS_KEY={collection:"GALA",category:"Unit",type:"none",additionalKey:"none"},exports.GSwapAssetError=b,exports.GSwapPoolError=E,exports.GSwapQuoteError=T,exports.GSwapSwapError=S,exports.IMAGE_EXTENSIONS=ke,exports.LAUNCHPAD_TOKEN_DECIMALS=18,exports.LaunchpadSDK=On,exports.MAX_CONCURRENT_POOL_FETCHES=5,exports.NetworkError=k,exports.PAGINATION_DEFAULTS=sa,exports.POOL_FETCH_CONFIG={MAX_CONCURRENT_FETCHES:5,BACKEND_PAGE_SIZE:20},exports.POOL_TYPES={RECENT:"recent",POPULAR:"popular"},exports.QUERY_FIELD_NAMES={PAGE:"page",LIMIT:"limit",TOKEN_NAME:"tokenName",VAULT_ADDRESS:"vaultAddress",USER_ADDRESS:"userAddress",TRADE_TYPE:"tradeType",POOL_TYPE:"type",SEARCH:"search",SORT_ORDER:"sortOrder",START_DATE:"startDate",END_DATE:"endDate"},exports.SDK_VERSION=Mn,exports.TRADING_TYPES=Fn,exports.TokenMetadataService=class{constructor(){this.cache={},this.cacheExpiry=36e5,this.logger=new g({debug:!1,context:"TokenMetadataService"})}async resolveTokenMetadata(e){const t=this.getCacheKey(e),a=this.cache[t];if(a&&!this.isCacheExpired(a.timestamp))return this.logger.debug(`Using cached metadata for token: ${t}`),a.data;const n=this.extractMetadata(e);return this.cache[t]={data:n,timestamp:Date.now()},n}async getTokenSymbol(e){return(await this.resolveTokenMetadata(e)).symbol}async getTokenDecimals(e){return(await this.resolveTokenMetadata(e)).decimals}clearCache(e){e?(delete this.cache[e],this.logger.debug(`Cleared cache for token: ${e}`)):(this.cache={},this.logger.debug("Cleared all token metadata cache"))}getCacheStats(){const e=Object.keys(this.cache);return{size:e.length,entries:e}}getCacheKey(e){if("string"==typeof e)return e.toLowerCase();return`${e.type||e.symbol||"unknown"}|${e.additionalKey||"none"}`.toLowerCase()}extractMetadata(e){let t,a,n="Token",o="Unit",r="none";if("string"==typeof e)if(e.includes("|")){const i=e.split("|");"Token"===i[0]&&i[2]?(n=i[0],o=i[1]||"Unit",t=i[2],r=i[3]||"none",a=t):(t=i[0],a=t,o=i[1]||"Unit",r=i[3]||"none")}else t=e,a=e;else t=e.type||"unknown",a=e.symbol||e.type||"unknown",n=e.collection||"Token",o=e.category||"Unit",r=e.additionalKey||"none";const i=this.getDecimalsForToken(a);return{symbol:a.toUpperCase(),decimals:i,collection:n,category:o,type:t.toUpperCase(),additionalKey:r,verified:!1}}getDecimalsForToken(e){return{GALA:8,GUSDC:6,USDC:6,USDT:6,WETH:18,DAI:18}[e.toUpperCase()]??18}isCacheExpired(e){return Date.now()-e>this.cacheExpiry}setCacheExpiry(e){this.cacheExpiry=e,this.logger.debug(`Set token metadata cache expiry to ${e}ms`)}},exports.TransactionError=A,exports.TransactionFailedError=_n,exports.ValidationError=w,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=M,exports.amountMethodSchema=fe,exports.amountTypeSchema=me,exports.browserFileSchema=Ae,exports.bufferFileSchema=Te,exports.buyTokensDataSchema=ze,exports.calculatePreMintDataSchema=Ye,exports.checkPoolOptionsSchema=pe,exports.commentMessageSchema=te,exports.commentPaginationSchema=xe,exports.createLaunchpadSDK=function(e){const{wallet:t,env:a,config:o={}}=e||{};let r;if(t)if("string"==typeof t){r=Gn(t).wallet}else{if(!(t instanceof n.Wallet))throw new Error("Invalid wallet input. Expected string (private key or mnemonic) or Wallet instance.");r=t}else{r=Gn().wallet}const i={wallet:r,...a&&{env:a},debug:!1,timeout:3e4,...o};return new On(i)},exports.createLimitSchema=H,exports.createPaginatedResultSchema=function(e){return r.z.object({data:r.z.array(e),page:r.z.number().int().min(1),limit:r.z.number().int().min(1),total:r.z.number().int().min(0),totalPages:r.z.number().int().min(0),hasNext:r.z.boolean(),hasPrevious:r.z.boolean()})},exports.createTradeDataSchema=qe,exports.createWallet=Gn,exports.ethereumAddressSchema=O,exports.faucetAmountSchema=z,exports.fetchGalaBalanceOptionsSchema=Be,exports.fetchPoolDetailsDataSchema=Ze,exports.fetchTokenBalanceOptionsSchema=Re,exports.fileSizeSchema=Z,exports.fileUploadSchema=ve,exports.filenameSchema=J,exports.flexibleAddressSchema=R,exports.flexibleFileSchema=Se,exports.formatGalaForDTO=Qa,exports.formatLaunchpadTokenForDTO=Ya,exports.fullNameSchema=B,exports.getAmountOptionsSchema=Qe,exports.getTradeOptionsSchema=We,exports.graduateTokenOptionsSchema=we,exports.graphDataOptionsSchema=ye,exports.imageExtensionSchema=Ee,exports.imageFilenameSchema=be,exports.imageMimeTypeSchema=ee,exports.imageUploadOptionsSchema=he,exports.isoDateStringSchema=ae,exports.launchTokenDataSchema=de,exports.nonNegativeDecimalStringSchema=q,exports.optionalUrlSchema=W,exports.pageNumberSchema=j,exports.paginationResultMetaSchema=Le,exports.poolFetchTypeSchema=ge,exports.poolPaginationSchema=Ce,exports.positiveDecimalStringSchema=K,exports.privateKeySchema=oe,exports.reverseBondingCurveConfigSchema=ue,exports.reverseBondingCurveConfigurationSchema=Je,exports.searchQuerySchema=U,exports.sellTokensDataSchema=Ve,exports.standardLimitSchema=X,exports.standardPaginationSchema=Ne,exports.timestampSchema=ne,exports.tokenCategorySchema=ce,exports.tokenCollectionSchema=le,exports.tokenDescriptionSchema=$,exports.tokenListOptionsSchema=_e,exports.tokenNameSchema=P,exports.tokenSymbolSchema=L,exports.tokenUrlsSchema=se,exports.tradeCalculationMethodSchema=Xe,exports.tradeCalculationTypeSchema=He,exports.tradeLimitSchema=Y,exports.tradeListParamsSchema=je,exports.tradePaginationSchema=Fe,exports.tradePaginationWithFiltersSchema=Pe,exports.tradeTypeBackendSchema=Ke,exports.tradeTypeSchema=Ge,exports.transactionIdSchema=re,exports.transferFaucetsDataSchema=Ue,exports.uniqueKeySchema=ie,exports.updateProfileDataSchema=Me,exports.uploadProfileImageOptionsSchema=Oe,exports.urlSchema=V,exports.userLimitSchema=Q,exports.userPaginationSchema=Ie,exports.userTokenNameSchema=_,exports.userTokenTypeSchema=$e,exports.userTokensPaginationSchema=De,exports.validateAddress=ot,exports.validateAmountString=it,exports.validateBuyTokensData=Tt,exports.validateCalculatePreMintData=It,exports.validateCheckPoolOptions=pt,exports.validateCreateTradeData=At,exports.validateFaucetAmount=st,exports.validateFetchGalaBalanceOptions=yt,exports.validateFetchPoolDetailsData=Ft,exports.validateFetchTokenBalanceOptions=vt,exports.validateFullName=ct,exports.validateGetAmountOptions=Nt,exports.validateGetTradeOptions=Et,exports.validateImageUploadOptions=gt,exports.validateLaunchTokenData=dt,exports.validateSearchQuery=lt,exports.validateSellTokensData=St,exports.validateTokenDescription=nt,exports.validateTokenListOptions=mt,exports.validateTokenName=tt,exports.validateTokenSymbol=at,exports.validateTokenUrls=ht,exports.validateTradeListParams=bt,exports.validateTransferFaucetsData=ft,exports.validateUpdateProfileData=wt,exports.validateUploadProfileImageOptions=kt,exports.validateUserTokenName=ut,exports.validateVaultAddress=rt,exports.vaultAddressSchema=G;
1
+ "use strict";var e,t,a=require("axios"),n=require("ethers"),o=require("@gala-chain/connect"),r=require("zod"),i=require("bignumber.js"),s=require("@gala-chain/api"),c=require("uuid"),l=require("socket.io-client"),u=require("@gala-chain/gswap-sdk");if("undefined"==typeof File){const{File:e}=require("web-file-polyfill");global.File=e}!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 d extends Error{constructor(e,t,a){super(t),this.type=e,this.originalError=a,this.name="AuthError"}}class h{constructor(t){if(this.wallet=t.wallet,this.messagePrefix=t.messagePrefix||"Create a GalaChain Wallet",""===t.messagePrefix)throw new d(e.SIGNATURE_FAILED,"Message prefix cannot be empty")}hasWallet(){return void 0!==this.wallet}setWallet(t){if(void 0!==t){if("object"!=typeof t||!("address"in t))throw new d(e.WALLET_NOT_CONNECTED,"Invalid wallet: must be an ethers Wallet instance or undefined");if(!t.address||"string"!=typeof t.address)throw new d(e.INVALID_ADDRESS,"Wallet address is not available")}this.wallet=t}async generateSignature(){this.validateWallet();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){if(t instanceof d)throw t;throw new d(e.SIGNATURE_FAILED,"Failed to generate signature for authentication",t instanceof Error?t:new Error(String(t)))}}getAddress(){return this.validateWallet(),this.formatAddress(this.wallet.address)}getEthereumAddress(){return this.validateWallet(),this.wallet.address}getPrivateKey(){if(this.validateWallet(),!this.wallet.privateKey)throw new d(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 d(e.INVALID_ADDRESS,`Invalid Ethereum address format: ${t}`);return`eth|${a}`}async signMessage(t){this.validateWallet();try{return{message:t,signature:await this.wallet.signMessage(t),address:this.wallet.address,timestamp:Date.now()}}catch(t){if(t instanceof d)throw t;const a=t instanceof Error?t.message:String(t);throw new d(e.SIGNATURE_FAILED,a,t instanceof Error?t:new Error(String(t)))}}async generateAuthHeaders(t,a){this.validateWallet();try{const e=Date.now(),n=`${this.messagePrefix} ${a.toUpperCase()} ${t} ${e}`,o=await this.wallet.signMessage(n);return{"x-signature":o,"x-address":this.formatAddress(this.wallet.address),"x-message":n,"x-timestamp":e.toString()}}catch(t){if(t instanceof d)throw t;throw new d(e.SIGNATURE_FAILED,"Failed to generate authentication headers",t instanceof Error?t:new Error(String(t)))}}async signTypedData(t,a,n){this.validateWallet();try{return await this.wallet.signTypedData(t,a,n)}catch(t){if(t instanceof d)throw t;throw new d(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 d(e.SIGNATURE_FAILED,"Custom message must be a non-empty string");this.validateWallet();try{const e=await this.wallet.signMessage(t);return{message:t,signature:e,address:this.formatAddress(this.wallet.address),timestamp:Date.now()}}catch(t){if(t instanceof d)throw t;throw new d(e.SIGNATURE_FAILED,"Failed to generate custom message signature",t instanceof Error?t:new Error(String(t)))}}validateWallet(){if(!this.wallet)throw new d(e.WALLET_NOT_CONNECTED,"Wallet is required for authentication");if(!this.wallet.address)throw new d(e.WALLET_NOT_CONNECTED,"Wallet address is not available");if(!this.wallet.privateKey&&!this.wallet.signMessage)throw new d(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 p{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 o=`[${(new Date).toISOString()}] [${this.context}] [${e}]`,r=this.getConsoleMethod(e);void 0!==n?n instanceof Error?r(`${o} ${a}`,n.message,n.stack):r(`${o} ${a}`,n):r(`${o} ${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 p({debug:this.debugEnabled,context:`${this.context}:${e}`,minLevel:this.minLevel})}isDebugEnabled(){return this.debugEnabled&&this.levelPriority[t.DEBUG]>=this.levelPriority[this.minLevel]}}function g(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 m{constructor(e,t={}){this.auth=e,this.debug=t.debug??!1,this.logger=new p({debug:this.debug,context:"HttpClient"}),this.axios=a.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:g(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=o.SigningType.SIGN_TYPED_DATA){const n=this.auth.getPrivateKey(),r=new o.SigningClient(n);return await r.sign(e,t,a)}setupInterceptors(){this.requestInterceptorId=this.axios.interceptors.request.use(async e=>{try{if(e.headers||(e.headers={}),this.auth.hasWallet()){const t=await this.auth.generateSignature();e.headers.Sign=t.signature,this.logger.debug("Added signature header:",{address:t.address,message:t.message,timestamp:t.timestamp})}else this.logger.debug("No wallet configured - skipping signature header");return e.data instanceof FormData||(e.headers["Content-Type"]="application/json"),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 f="Token name is required and must be a string",y=e=>`Could not find vault address for token: ${e}`;class w extends Error{constructor(e,t,a){super(e),this.field=t,this.code=a,this.name="ValidationError"}}class k 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"}}class T extends Error{constructor(e,t,a){super(e),this.originalError=t,this.code=a,this.name="GSwapQuoteError"}}class E extends Error{constructor(e,t,a,n){super(e),this.originalError=t,this.transactionHash=a,this.code=n,this.name="GSwapSwapError"}}class S extends Error{constructor(e,t,a,n,o){super(e),this.originalError=t,this.tokenA=a,this.tokenB=n,this.code=o,this.name="GSwapPoolError"}}class b extends Error{constructor(e,t,a,n){super(e),this.originalError=t,this.walletAddress=a,this.code=n,this.name="GSwapAssetError"}}class N extends Error{constructor(e,t,a){super(e),this.originalError=t,this.code=a,this.name="GSwapPositionError"}}function I(e,t){return new w(`Token "${e}" not found. Please verify the token name is correct.`,"tokenName","TOKEN_NOT_FOUND")}function F(e,t){const a=t||e.charAt(0).toUpperCase()+e.slice(1);return new w(`${a} is required`,e,"REQUIRED_FIELD")}function x(e,t,a){const n=a||e.charAt(0).toUpperCase()+e.slice(1);return new w(`${n} must be ${t}`,e,"INVALID_FORMAT")}function D(e,t,a){return new k(e,t,a)}function C(e,t){return new v(e,t)}function P(e,t,a){return new A(e,t,a)}function L(e,t,a){const{MIN_PAGE:n,MAX_PAGE:o,MIN_LIMIT:r,MAX_LIMIT:i}=a.PAGINATION;if("number"!=typeof e||e<n||e>o)throw new w(`Page must be a number between ${n} and ${o}`,"page","INVALID_PAGE");if("number"!=typeof t||t<r||t>i)throw new w(`Limit must be a number between ${r} and ${i}`,"limit","INVALID_LIMIT")}const $={ETH_ADDRESS:/^0x[0-9a-fA-F]{40}$/,BACKEND_ADDRESS:/^eth\|[0-9a-fA-F]{40}$/};function _(e){return"string"==typeof e&&e.trim().length>0}function U(e){return!(!e||"string"!=typeof e)&&($.ETH_ADDRESS.test(e)||$.BACKEND_ADDRESS.test(e))}function B(e){return e.startsWith("0x")?`eth|${e.slice(2)}`:e}const O=r.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"),M=r.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"),R=r.z.string().min(1,"Token description is required").max(500,"Token description must be at most 500 characters"),G=r.z.string().min(1,"Token name must be at least 1 character").max(50,"Token name must be at most 50 characters"),K=r.z.string().min(1,"Search query must be at least 1 character").max(100,"Search query must be at most 100 characters"),q=r.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"),z=r.z.string().regex($.BACKEND_ADDRESS,"Address must be in format: eth|[40-hex-chars]"),W=r.z.string().regex($.ETH_ADDRESS,"Invalid Ethereum address format"),V=r.z.string().refine(e=>$.BACKEND_ADDRESS.test(e)||$.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),j=r.z.string().refine(e=>$.BACKEND_ADDRESS.test(e)||/^service\|Token\$Unit\$[A-Z0-9]+\$eth:[0-9a-fA-F]{40}\$launchpad$/.test(e),"Invalid vault address format"),H=r.z.string().regex(/^\d+(\.\d+)?$/,"Must be a valid decimal number").refine(e=>parseFloat(e)>0,"Amount must be greater than zero"),X=r.z.string().regex(/^\d+(\.\d+)?$/,"Must be a valid decimal number").refine(e=>parseFloat(e)>=0,"Amount must be zero or greater"),Q=r.z.string().regex(/^(?!0+(\.0+)?$)\d+(\.\d+)?$/,"Amount must be a positive, non-zero number"),Y=r.z.string().url("Must be a valid URL").regex(/^https?:\/\//,"URL must start with http:// or https://"),Z=r.z.string().optional().refine(e=>!e||/^https?:\/\/.+\..+/.test(e),"Must be a valid URL if provided"),J=r.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 ee(e=100){return r.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 te=ee(100),ae=ee(20),ne=ee(20),oe=r.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"),re=r.z.string().max(255,"Filename must be at most 255 characters"),ie=r.z.enum(["image/png","image/jpg","image/jpeg","image/gif","image/webp","image/svg+xml"]),se=r.z.string().min(1,"Comment message is required").max(500,"Comment must be at most 500 characters"),ce=r.z.string().datetime("Must be a valid ISO 8601 date string"),le=r.z.number().int("Timestamp must be an integer").min(0,"Timestamp must be non-negative"),ue=r.z.string().regex(/^0x[a-fA-F0-9]{64}$/,"Private key must be format: 0x + 64 hex characters"),de=r.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"),he=r.z.string().regex(/^galaconnect-operation-[a-z0-9-]+$/,"Unique key must be format: galaconnect-operation-{unique-id}"),pe=r.z.object({websiteUrl:Z,telegramUrl:Z,twitterUrl:Z}).refine(e=>e.websiteUrl||e.telegramUrl||e.twitterUrl,"At least one social URL (website, telegram, or twitter) is required"),ge=r.z.string().min(1,"Token category must not be empty").default("Unit"),me=r.z.string().min(1,"Token collection must not be empty").default("Token"),fe=r.z.object({minFeePortion:H,maxFeePortion:H}),ye=r.z.object({tokenName:O,tokenSymbol:M,tokenDescription:R,tokenImage:r.z.union([r.z.instanceof(File),r.z.instanceof(Buffer),r.z.string().url("Token image must be a valid URL")]).optional(),preBuyQuantity:X.default("0"),websiteUrl:Z,telegramUrl:Z,twitterUrl:Z,tokenCategory:ge,tokenCollection:me,reverseBondingCurveConfiguration:fe.optional(),privateKey:ue.optional()}),we=r.z.object({file:r.z.union([r.z.instanceof(File),r.z.instanceof(Buffer)]),tokenName:O}),ke=r.z.enum(["recent","popular"]),ve=r.z.object({tokenName:O.optional(),symbol:M.optional()}).refine(e=>e.tokenName||e.symbol,"At least one of tokenName or symbol is required"),Ae=r.z.enum(["NATIVE","MEME"]),Te=r.z.enum(["IN","OUT"]),Ee=r.z.object({from:r.z.number().int("From timestamp must be an integer").min(173e6,"From timestamp must be at least 173000000"),to:r.z.number().int("To timestamp must be an integer").min(173e6,"To timestamp must be at least 173000000"),resolution:r.z.number().int("Resolution must be an integer").min(1,"Resolution must be at least 1"),tokenName:O}),Se=r.z.object({tokenName:O,slippageToleranceFactor:r.z.number().min(0).max(1).optional(),maxAcceptableReverseBondingCurveFeeSlippageFactor:r.z.number().min(0).max(1).optional(),privateKey:ue.optional()}),be=[".png",".jpg",".jpeg",".gif",".webp",".svg"],Ne=r.z.object({file:r.z.union([r.z.instanceof(File),r.z.instanceof(Buffer)]),name:re,size:oe,type:ie}),Ie=r.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"),Fe=r.z.instanceof(Buffer).refine(e=>e.length>=1&&e.length<=10485760,"Buffer size must be between 1 byte and 10MB"),xe=r.z.union([Ie,Fe]),De=r.z.enum([".png",".jpg",".jpeg",".gif",".webp",".svg"]),Ce=re.refine(e=>{const t=e.slice(e.lastIndexOf(".")).toLowerCase();return be.includes(t)},`Filename must end with one of: ${be.join(", ")}`),Pe=r.z.object({page:J,limit:te}),Le=r.z.object({page:J,limit:ae}),$e=r.z.object({page:J,limit:ne}),_e=r.z.object({page:J,limit:ee(50)}),Ue=Pe.extend({type:r.z.enum(["recent","popular"]).optional(),tokenName:r.z.string().min(1).max(50).optional(),search:r.z.string().min(1).max(100).optional()}),Be=Le.extend({tokenName:r.z.string().min(1).max(50).optional(),search:r.z.string().min(1).max(100).optional()}),Oe=$e.extend({tradeType:r.z.enum(["BUY","SELL"]).optional(),tokenName:r.z.string().min(1).max(50).optional(),userAddress:r.z.string().regex(/^(0x[a-fA-F0-9]{40}|eth\|[a-fA-F0-9]{40})$/).optional(),startDate:r.z.string().datetime().optional(),endDate:r.z.string().datetime().optional(),sortOrder:r.z.enum(["ASC","DESC"]).default("DESC")}),Me=r.z.object({page:r.z.number().int().min(1),limit:r.z.number().int().min(1),total:r.z.number().int().min(0),totalPages:r.z.number().int().min(0),hasNext:r.z.boolean(),hasPrevious:r.z.boolean()});const Re=r.z.enum(["all","DEFI","ASSET"]),Ge=Le.extend({type:Re.optional(),address:V.optional(),search:K.optional(),tokenName:G.optional()}),Ke=r.z.object({walletAddress:V,amount:Q}),qe=r.z.object({address:V.optional(),refresh:r.z.boolean().optional()}),ze=r.z.object({profileImage:r.z.string(),fullName:q,address:V,privateKey:ue.optional()}),We=r.z.object({file:r.z.union([r.z.instanceof(File),r.z.instanceof(Buffer)]),address:V.optional(),privateKey:ue.optional()}),Ve=r.z.object({address:V,tokenId:r.z.union([r.z.string(),r.z.object({collection:r.z.string(),category:r.z.string(),type:r.z.string(),additionalKey:r.z.string()}),r.z.object({collection:r.z.string(),category:r.z.string(),type:r.z.string(),additionalKey:r.z.string(),instance:r.z.string()})]).optional(),tokenName:G.optional()}).refine(e=>void 0!==e.tokenId||void 0!==e.tokenName,"At least one token identifier (tokenId or tokenName) is required"),je=r.z.enum(["buy","sell"]),He=r.z.enum(["BUY","SELL"]),Xe=r.z.object({tradeType:je,tokenAmount:H,vaultAddress:j,userAddress:V,slippageTolerance:H.optional(),deadline:r.z.number().int().positive().optional()}),Qe=r.z.object({tokenSymbol:M,nativeTokenQuantity:H,expectedToken:H,maxAcceptableReverseBondingCurveFee:X.default("0").optional()}),Ye=r.z.object({tokenSymbol:M,tokenQuantity:H,expectedNativeToken:H,maxAcceptableReverseBondingCurveFee:X.default("0").optional()}),Ze=$e.extend({tokenName:G.optional()}),Je=r.z.object({page:r.z.number().int().min(1).max(1e3).default(1).optional(),limit:r.z.number().int().min(1).max(20).default(10).optional()}),et=r.z.enum(["NATIVE","MEME"]),tt=r.z.enum(["IN","OUT"]),at=r.z.object({type:et,method:tt,vaultAddress:j,amount:H}),nt=r.z.object({nativeTokenQuantity:H}),ot=r.z.object({vaultAddress:j}),rt=r.z.object({minFeePortion:H,maxFeePortion:H});function it(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 st=it(O),ct=it(M),lt=it(R),ut=it(V),dt=it(j),ht=it(H),pt=it(Q),gt=it(q),mt=it(K),ft=it(G),yt=it(ye),wt=it(pe),kt=it(we),vt=it(ve),At=it(Ge),Tt=it(Ke),Et=it(qe),St=it(ze),bt=it(We),Nt=it(Ve),It=it(Xe),Ft=it(Qe),xt=it(Ye),Dt=it(Ze),Ct=it(Je),Pt=it(at),Lt=it(nt),$t=it(ot);function _t(e,t){throw new w(e.join("; "),t,"VALIDATION_ERROR")}function Ut(e){const t=st(e);!t.success&&t.errors&&_t(t.errors,"tokenName")}function Bt(e){const t=Ue.safeParse(e);t.success||_t(t.error.errors.map(e=>e.message),"pagination")}function Ot(e){const t=vt(e);!t.success&&t.errors&&_t(t.errors,"options")}function Mt(e){const t=Pt(e);!t.success&&t.errors&&_t(t.errors,"options")}function Rt(e){const t=Ee.safeParse(e);t.success||_t(t.error.errors.map(e=>e.message),"options")}function Gt(e){const t=V.safeParse(e);if(!t.success)throw new w("Ethereum address must be 40 hex characters (with or without 0x prefix)","ethereumAddress","INVALID_FORMAT");return t.data}function Kt(e,t,a=!0){if(!e||""===e.trim())throw new w(`${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 w(`${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 w(`${t} must be a valid numeric string. Received: "${e}"`,t,"INVALID_NUMERIC_STRING");if(!isFinite(n))throw new w(`${t} must be a finite number. Cannot be Infinity or -Infinity.`,t,"INVALID_NUMERIC_STRING");if(n<0)throw new w(`${t} must be non-negative. Received: "${e}"`,t,"INVALID_NUMERIC_STRING");if(!a&&0===n)throw new w(`${t} must be greater than zero. Received: "${e}"`,t,"INVALID_NUMERIC_STRING")}var qt=Object.freeze({__proto__:null,normalizeAddressInput:function(e){if(!e)return;const t=V.safeParse(e);if(!t.success)throw new w(`Invalid address format: ${e}. Must be either "0x..." (Ethereum) or "eth|..." (GalaChain) format`,"address","INVALID_FORMAT");return t.data},toBackendAddressFormat:Gt,validateCheckPoolOptions:Ot,validateGetAmountOptions:Mt,validateGetGraphOptions:Rt,validateNumericString:Kt,validatePagination:Bt,validateTokenName:Ut});function zt(e,t){const a=e,n=Number(a.page)||t.page,o=Number(a.limit)||t.limit,r=a.data,i=Number(a.total)||Number(r?.count)||0;return{page:n,limit:o,total:i,totalPages:Math.ceil(i/o)}}function Wt(e,t){return{hasNext:e<t,hasPrevious:e>1}}function Vt(e,t,a=!1){const n=!0===e.error||200!==e.status,o=a&&!e.data;if(n||o)throw new Error(e.message||t)}const jt="/launchpad/upload-image",Ht="/launchpad/fetch-pool",Xt="/launchpad/check-pool",Qt="/launchpad/get-graph-data",Yt="/holders",Zt="/launchpad/get-badge/",Jt="/trade/",ea="/token/commment",ta="/token/commment",aa="/user/profile",na="/user/profile",oa="/user/token-list",ra="/user/token-hold",ia="/user/transfer-faucets",sa={DEFAULT_PAGE:1,DEFAULT_LIMIT:10,BACKEND_MAX_PAGE_SIZE:20,SAFETY_MAX_PAGES:100};class ca{constructor(e,t=!1){this.http=e,this.logger=new p({debug:t,context:this.constructor.name})}}class la{constructor(e=!1){this.logger=new p({debug:e,context:this.constructor.name})}}function ua(e,t){return"string"==typeof e[t]}function da(e,t){return void 0===e[t]||"string"==typeof e[t]}function ha(e,t){return void 0===e[t]||"number"==typeof e[t]}function pa(e){return void 0===e.calculateAmountMode||"local"===e.calculateAmountMode||"external"===e.calculateAmountMode}function ga(e){if(!e||"object"!=typeof e)return!1;const t=e;return ua(t,"tokenName")&&ha(t,"from")&&ha(t,"to")&&ha(t,"resolution")}class ma extends ca{constructor(e,t=!1){super(e,t)}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=g(t),n=await this.http.get(Ht,a);if(!n)throw D("No response from pool service",500);Vt(n,"Failed to fetch pools",!0);const o=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),r=n.data.count??n.data.total??0;return{pools:o,total:r,totalPages:e.limit>0?Math.ceil(r/e.limit):1}}async fetchMultiplePages(e){const{startPage:t,totalPages:a,pageSize:n,...o}=e,r=[];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({...o,page:e,limit:n}));(await Promise.all(a)).forEach(e=>{r.push(...e.pools)})}return r}let i=t,s=!0;for(;s;){const e=[];for(let t=0;t<5&&s;t++)e.push(i+t);const t=e.map(e=>this.fetchSinglePage({...o,page:e,limit:n})),a=await Promise.all(t);for(const e of a){if(0===e.pools.length){s=!1;break}r.push(...e.pools)}i+=e.length,i>sa.SAFETY_MAX_PAGES&&(s=!1)}return r}async fetchPools(e={}){const t=e.page||sa.DEFAULT_PAGE,a=e.limit??sa.DEFAULT_LIMIT;let n;if(0!==a&&Bt({page:t,limit:a}),e.tokenName&&Ut(e.tokenName),"recent"===e.type?n="RECENT":"popular"===e.type&&(n="POPULAR"),a>0&&a<=20){const o=await this.fetchSinglePage({...e.search&&{search:e.search},...e.tokenName&&{tokenName:e.tokenName},...n&&{type:n},page:t,limit:a});return{pools:o.pools,page:t,limit:a,total:o.total,totalPages:o.totalPages,hasNext:t<o.totalPages,hasPrevious:t>1}}if(0===a){const t=sa.BACKEND_MAX_PAGE_SIZE,a=await this.fetchSinglePage({...e.search&&{search:e.search},...e.tokenName&&{tokenName:e.tokenName},...n&&{type:n},page:1,limit:t}),o=[...a.pools];if(a.pools.length>0){const r=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});o.push(...r)}return{pools:o,page:1,limit:o.length,total:a.total||o.length,totalPages:1,hasNext:!1,hasPrevious:!1}}const o=sa.BACKEND_MAX_PAGE_SIZE,r=Math.ceil(a/o),i=await this.fetchSinglePage({...e.search&&{search:e.search},...e.tokenName&&{tokenName:e.tokenName},...n&&{type:n},page:t,limit:o}),s=[...i.pools],c=Math.min(r,i.totalPages-t+1);if(c>1){const a=t+c-1,r=await this.fetchMultiplePages({...e.search&&{search:e.search},...e.tokenName&&{tokenName:e.tokenName},...n&&{type:n},startPage:t+1,totalPages:a,pageSize:o});s.push(...r)}const l=s.slice(0,a);return{pools:l,page:t,limit:a,total:i.total,totalPages:i.totalPages,hasNext:t<i.totalPages&&l.length<i.total,hasPrevious:t>1}}async fetchAllPools(e){return this.fetchPools({...e,limit:0})}async checkPool(e){Ot(e),e.tokenName&&Ut(e.tokenName);const t=g(e),a=await this.http.get(Xt,t);if(!a)throw D("No response from pool service",500);Vt(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(!ga(e))throw new w("Invalid options provided. Expected { tokenName: string, from?: number, to?: number, resolution?: number }","options","INVALID_OPTIONS");const{tokenName:t,from:a,to:n,resolution:o}=e;if(Ut(t),!a||!n||!o)throw new w("Graph options (from, to, resolution) are required","options","MISSING_GRAPH_OPTIONS");const r={tokenName:t,from:a,to:n,resolution:o};Rt(r);const i=g(r),s=await this.http.get(Qt,i);if(!s)throw D("No response from pool service",500);return Vt(s,"Failed to fetch graph data",!0),{dataPoints:s.data}}async fetchTokenDistribution(e){if(!e)throw F("tokenName","Token name");Ut(e);const t=await this.resolveTokenNameToVault(e);if(!t)throw I(e);const a=encodeURIComponent(t),n=await this.http.get(`${Yt}/${a}`);if(!n)throw D("No response from pool service",500);Vt(n,"Failed to fetch token distribution",!0);const o=n.data;if(!Array.isArray(o))throw D("Invalid API response: expected array of holders",n.status);for(const e of o){if(!e.owner||"string"!=typeof e.owner)throw D("Invalid holder data: missing or invalid owner field",n.status);if(!e.quantity||"string"!=typeof e.quantity)throw D("Invalid holder data: missing or invalid quantity field",n.status);const t=parseFloat(e.quantity);if(isNaN(t)||!isFinite(t))throw D(`Invalid holder quantity: "${e.quantity}"`,n.status)}const r=o.reduce((e,t)=>e.plus(t.quantity),new i(0));return{holders:o.map(e=>{const t=new i(e.quantity),a=r.isZero()?0:t.dividedBy(r).multipliedBy(100).toNumber();return{address:e.owner,balance:e.quantity,percentage:a}}),totalSupply:r.toFixed(),totalHolders:o.length,lastUpdated:new Date}}async fetchTokenBadges(e){if(!e)throw F("tokenName","Token name");Ut(e);const t=await this.http.get(Zt,{tokenName:e});if(!t)throw D("No response from pool service",500);return Vt(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 o=("volume"===a?e.volumeBadges:e.engagementBadges).find(e=>e.badgeName===n);return o?.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 fa(e,t={}){const{stringifyFields:a=[],optionalFields:n=[],fieldMappings:o={}}=t,r={};for(const[t,i]of Object.entries(e)){const e=t;if(n.includes(e)&&void 0===i)continue;if(n.includes(e)&&"string"==typeof i&&0===i.trim().length)continue;const s=o[e],c=s?String(s):t;a.includes(e)?r[c]=String(i):r[c]=i}return g(r)}const ya={PAGINATION:{MIN_PAGE:1,MAX_PAGE:1e3,MIN_LIMIT:1,MAX_LIMIT:20}};class wa extends ca{constructor(e,t=!1){super(e,t)}async fetchTrades(e){if(!function(e){if(!e||"object"!=typeof e)return!1;const t=e;return ua(t,"tokenName")&&(void 0===t.tradeType||"buy"===t.tradeType||"sell"===t.tradeType)&&da(t,"userAddress")&&ha(t,"page")&&ha(t,"limit")}(e))throw new w("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:o=sa.DEFAULT_PAGE,limit:r=sa.DEFAULT_LIMIT,startDate:i,endDate:s,sortOrder:c}=e;if(!_(t))throw new w("Token name is required and must be a non-empty string","tokenName","INVALID_TOKEN_NAME");L(o,r,ya);const l=function(e,t,a){return fa({tokenName:e,page:t,limit:a},{stringifyFields:["page","limit"]})}(t,o,r),u=await this.http.get(Jt,l);if(!u)throw new w("No response from trade service","response","NO_RESPONSE");const d=(h=u.data)?Array.isArray(h)?h:h.trades:[];var h;const p=zt(u,{page:o,limit:r}),g=Wt(p.page,p.totalPages);return{trades:d,...p,...g}}}const ka=new p({debug:!1,context:"DateUtils"});function va(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())?(ka.warn(`Invalid date string received: "${e}". Using fallback.`),t||new Date):a}catch(a){return ka.warn(`Date parsing error for "${e}":`,a),t||new Date}}const Aa={PAGINATION:{MIN_PAGE:1,MAX_PAGE:1e3,MIN_LIMIT:1,MAX_LIMIT:50},CONTENT:{MIN_LENGTH:1,MAX_LENGTH:500}};class Ta extends ca{constructor(e,t,a=!1){super(e,a),this.poolService=t}async fetchComments(e){if(!function(e){if(!e||"object"!=typeof e)return!1;const t=e;return ua(t,"tokenName")&&ha(t,"page")&&ha(t,"limit")}(e))throw new w("Invalid options provided. Expected { tokenName: string, page?: number, limit?: number }","options","INVALID_OPTIONS");const{tokenName:t,page:a=sa.DEFAULT_PAGE,limit:n=sa.DEFAULT_LIMIT}=e;if(!_(t))throw new w("Token name is required and must be a non-empty string","tokenName","INVALID_TOKEN_NAME");L(a,n,Aa);const o=await this.poolService.resolveTokenNameToVault(t);if(!o)throw I(t);const r=fa({vaultAddress:o,page:a,limit:n},{stringifyFields:["page","limit"]}),i=await this.http.get(ea,r);if(!i)throw new w("No response from comment service","response","NO_RESPONSE");if(Vt(i,"Failed to fetch comments"),!i.data||!i.data.comments)throw new w("Invalid response structure from comment service","response.data","INVALID_RESPONSE");return{comments:i.data.comments.map(e=>({...e,createdAt:va(e.createdAt)})),total:i.data.count}}async postComment(e){if(!function(e){if(!e||"object"!=typeof e)return!1;const t=e;return ua(t,"tokenName")&&ua(t,"content")}(e))throw new w("Invalid options provided. Expected { tokenName: string, content: string }","options","INVALID_OPTIONS");const{tokenName:t,content:a}=e;if(!_(t))throw new w("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>=Aa.CONTENT.MIN_LENGTH&&t.length<=Aa.CONTENT.MAX_LENGTH}(a))throw new w(`Comment content must be between ${Aa.CONTENT.MIN_LENGTH} and ${Aa.CONTENT.MAX_LENGTH} characters`,"content","INVALID_CONTENT");const n=await this.poolService.resolveTokenNameToVault(t);if(!n)throw I(t);const o=this.http.getAddress();if(!o)throw new w("User address not available - wallet not configured","userAddress","NO_WALLET");const r={userAddress:o,vaultAddress:n,content:a},i=await this.http.post(ta,r);if(!i)throw new w("No response from comment service","response","NO_RESPONSE");Vt(i,"Failed to create comment")}}function Ea(e){return e instanceof Error}function Sa(e){return Ea(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 ba(e){return"object"==typeof e&&null!==e&&"message"in e&&("response"in e||"request"in e||"config"in e)}function Na(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 x("file","a File object (browser) or Buffer (Node.js)");{const o=new Blob([e],{type:"image/png"});n.append(t,o,a)}}return n}const Ia={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 Fa(e){return!(!e||"string"!=typeof e)&&Ia.USER_ADDRESS.PATTERN.test(e)}const xa="Update profile";class Da extends ca{constructor(e,t=!1){super(e,t)}async fetchProfile(e){const t=e??this.http.getAddress();if(!t||!Fa(t))throw new w("Address must be in format: eth|[40-hex-chars]","address","INVALID_ADDRESS");const a={userAddress:t},n=await this.http.get(aa,a);if(!n)throw new w("No response from user service","response","NO_RESPONSE");return n}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(xa);if(!n)throw new w("Failed to generate signature - wallet not configured","signature","NO_SIGNATURE");const o={address:n.address,message:xa,publickey:n.ethereumAddress,sign:n.signature},r=await this.http.put(na,a,o);if(!r)throw new w("No response from user service","response","NO_RESPONSE");Vt(r,"Profile update failed")}async uploadProfileImage(e){this.validateUploadProfileImageOptions(e);const t=e.address??this.http.getAddress();if(!t)throw new w("Wallet address not available - wallet not configured","address","NO_WALLET");try{const a=`profile-image-${t}.png`,n=Na(e.file,"image",a),o=await this.http.request({method:"POST",url:`${jt}?tokenName=${encodeURIComponent(t)}`,data:n,headers:{}});if(!o)throw new w("No response from user service","response","NO_RESPONSE");return Vt(o,"Image upload failed"),"string"==typeof o.data?o.data:""}catch(e){if(e instanceof w)throw e;throw new w(`Profile image upload failed: ${Sa(e)}`,"file","UPLOAD_FAILED")}}async fetchTokenList(e){return this.buildFetchRequest(oa,e,{includeType:!0,errorMessage:"Failed to fetch token list"})}async fetchTokensHeld(e){return this.buildFetchRequest(ra,e,{includeType:!1,errorMessage:"Failed to fetch tokens held"})}async fetchTokensCreated(e={}){const{page:t=sa.DEFAULT_PAGE,limit:a=sa.DEFAULT_LIMIT,search:n,tokenName:o}=e,r=this.http.getAddress();if(!r)throw new w("Wallet address not available - wallet not configured","address","NO_WALLET");const i={type:"DEFI",address:r,page:t,limit:a};return void 0!==n&&(i.search=n),void 0!==o&&(i.tokenName=o),this.fetchTokenList(i)}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},o=fa(a.includeType?{...n,type:"all"!==t.type&&t.type?t.type:"DEFI"}:n,{stringifyFields:["page","limit"],optionalFields:["address","search","tokenName"]}),r=await this.http.get(e,o);if(!r)throw new w("No response from user service","response","NO_RESPONSE");Vt(r,a.errorMessage,!0);const i=(s=r.data)?Array.isArray(s)?s:s.token:[];var s;const c=zt(r,{page:t.page||sa.DEFAULT_PAGE,limit:t.limit||sa.DEFAULT_LIMIT}),l=Wt(c.page,c.totalPages);return{tokens:i,...c,...l}}validateGetTokenListOptions(e){if(L(e.page,e.limit,Ia),void 0!==e.address&&!Fa(e.address))throw new w("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>=Ia.SEARCH.MIN_LENGTH&&t.length<=Ia.SEARCH.MAX_LENGTH))throw new w(`Search query must be between ${Ia.SEARCH.MIN_LENGTH} and ${Ia.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>=Ia.TOKEN_NAME.MIN_LENGTH&&a.length<=Ia.TOKEN_NAME.MAX_LENGTH))throw new w(`Token name must be between ${Ia.TOKEN_NAME.MIN_LENGTH} and ${Ia.TOKEN_NAME.MAX_LENGTH} characters`,"tokenName","INVALID_TOKEN_NAME")}validateUpdateProfileData(e){if(!Fa(e.address))throw new w("Address must be in format: eth|[40-hex-chars]","address","INVALID_ADDRESS");if(!((t=e.fullName)&&"string"==typeof t&&t.length>=Ia.PROFILE.FULL_NAME.MIN_LENGTH&&t.length<=Ia.PROFILE.FULL_NAME.MAX_LENGTH&&Ia.PROFILE.FULL_NAME.ALPHABETS_ONLY_PATTERN.test(t)))throw new w(`Full name must be between ${Ia.PROFILE.FULL_NAME.MIN_LENGTH} and ${Ia.PROFILE.FULL_NAME.MAX_LENGTH} characters`,"fullName","INVALID_FULL_NAME");var t}validateUploadProfileImageOptions(e){if(e.address&&!Fa(e.address))throw new w("Address must be in format: eth|[40-hex-chars]","address","INVALID_ADDRESS")}}class Ca extends Error{constructor(e,t,a){super(e),this.filename=t,this.mimeType=a,this.name="FileValidationError"}}function Pa(e,t,a){if(!e)throw new Ca("File is required",t,a);if("undefined"!=typeof File&&e instanceof File){const t=Ie.safeParse(e);if(!t.success){const a=t.error.errors.map(e=>e.message).join("; ");throw new Ca(a,e.name,e.type)}return}if(Buffer.isBuffer(e)){if(!t)throw new Ca("Filename is required when uploading Buffer objects",t,a);const n=Fe.safeParse(e);if(!n.success){const e=n.error.errors.map(e=>e.message).join("; ");throw new Ca(e,t,a)}if(t.length>255)throw new Ca(`Filename length ${t.length} exceeds maximum allowed length of 255 characters`,t,a);const o=["image/png","image/jpg","image/jpeg","image/gif","image/webp","image/svg+xml"];if(!o.includes(a))throw new Ca(`Invalid file type "${a}" is not allowed. Allowed types: ${o.join(", ")}`,t,a);const r=function(e){if(!e)return"";const t=e.lastIndexOf(".");if(-1===t)return"";return e.substring(t).toLowerCase()}(t),i=[".png",".jpg",".jpeg",".gif",".webp",".svg"];if(!i.includes(r))throw new Ca(`File extension "${r}" is not allowed. Allowed extensions: ${i.join(", ")}`,t,a);const s=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"}}(r);if(s!==a&&"application/octet-stream"!==s)throw new Ca(`File extension "${r}" does not match MIME type "${a}"`,t,a);return}throw new Ca("File must be a File object (browser) or Buffer (Node.js)",t,a)}class La extends ca{constructor(e,t=!1){super(e,t)}async uploadImageByTokenName(e){const{tokenName:t,options:a}=e;Ut(t);const n=`${t}.png`;Pa(a.file,n,"image/png");try{const e=`${a.tokenName??t}.png`,n=Na(a.file,"image",e),o=await this.http.request({method:"POST",url:`${jt}?tokenName=${encodeURIComponent(a.tokenName??t)}`,data:n,headers:{}});if(!o)throw C("No response from image upload service","response");return Vt(o,"Image upload failed"),"string"==typeof o.data?o.data:""}catch(e){if(e instanceof Error&&e.message.includes("FormData"))throw C("File upload failed: FormData not supported in this environment. Ensure you have proper polyfills for Node.js environments.","FormData");throw e}}}class $a extends ca{constructor(e,t=!1){super(e,t)}async transferFaucets(e){this.validateTransferFaucetsData(e);const t={userAddress:e.walletAddress,amount:e.amount},a=await this.http.post(ia,t);if(!a)throw new w("No response from faucet service","response","NO_RESPONSE");Vt(a,"Faucet transfer failed")}validateTransferFaucetsData(e){if(!Fa(e.walletAddress))throw new w("Address must be in format: eth|[40-hex-chars]","address","INVALID_ADDRESS");if(!(t=e.amount)||"string"!=typeof t||!Ia.FAUCET_AMOUNT.POSITIVE_NON_ZERO_DECIMAL.test(t))throw new w("Amount must be a positive decimal string greater than zero","amount","INVALID_AMOUNT");var t}}class _a{constructor(e,t=!1){this.http=e,this.poolService=new ma(e,t),this.tradeService=new wa(e,t),this.commentService=new Ta(e,this.poolService,t),this.userService=new Da(e,t),this.imageService=new La(e,t),this.faucetService=new $a(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 Ut(e)}}const Ua={MAX_UNIQUE_KEY_LENGTH:64,UNIQUE_KEY_PATTERN:/^(galaswap-operation-|galaconnect-operation-)/,TOKEN_NAME_PATTERN:/^[a-zA-Z0-9]+$/};var Ba;!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",e.WALLET_REQUIRED="WALLET_REQUIRED"}(Ba||(Ba={}));class Oa extends Error{constructor(e,t,a){super(e),this.type=t,this.details=a,this.name="TransferError"}}class Ma{static validateAmount(e){if(parseFloat(e)<=0)throw new Oa("Transfer amount must be positive",Ba.INVALID_AMOUNT,{amount:e})}static validateUniqueKey(e){if(e){if(e.length>Ua.MAX_UNIQUE_KEY_LENGTH)throw new w(`Unique key too long. Maximum length: ${Ua.MAX_UNIQUE_KEY_LENGTH}`);if(!Ua.UNIQUE_KEY_PATTERN.test(e))throw new Oa('Invalid unique key format. Must start with "galaswap-operation-" or "galaconnect-operation-"',Ba.INVALID_AMOUNT,{uniqueKey:e})}}}class Ra{toLaunchpadFormat(e){if(!e)return"unknown|Unit|none|none";if("string"==typeof e)return e.includes("|")?e:`${e}|Unit|none|none`;return`${e.type||e.symbol||"unknown"}|${e.category||"Unit"}|none|${e.additionalKey||"none"}`}toTokenClass(e){if("object"==typeof e&&null!==e)return{collection:e.collection||"Token",category:e.category||"Unit",type:e.type||e.symbol||"unknown",additionalKey:e.additionalKey||"none"};if("string"!=typeof e)throw new Error("Invalid token format: expected string or object, got "+typeof e);const t=e.split("|");if(t.length<3)throw new Error(`Invalid token format: ${e}`);const a=("Token"===t[0]||"Collection"===t[0])&&t[2]?t[2]:t[0];return{collection:t[0]||"Token",category:t[1]||"Unit",type:a,additionalKey:t[3]||"none"}}isGSwapFormat(e){return"string"==typeof e&&e.includes("|")}normalize(e){return"string"==typeof e&&this.isGSwapFormat(e)?e:this.toLaunchpadFormat(e)}}function Ga(e){if(!e||"string"!=typeof e)throw new Error("Invalid token format: token must be a non-empty string");return e.replace(/\|/g,"$")}function Ka(e){try{if(!e||"string"!=typeof e)throw new Error("Token must be a non-empty string");const t=e.split("$");if(t.length<4)throw new Error(`Invalid dollar-delimited token format. Expected at least 4 parts separated by $, got ${t.length}`);const[a,n,o,...r]=t;if(!a||!n||!o)throw new Error("Collection, category, and type must be non-empty");const i=r.join("$");if(!i)throw new Error("AdditionalKey must be non-empty");return{collection:a,category:n,type:o,additionalKey:i}}catch(t){throw new w(`Invalid dollar-delimited token: "${e}". Expected format: "Token$Unit$SYMBOL$additionalKey". Error: ${t instanceof Error?t.message:String(t)}`,"dollarToken","INVALID_DOLLAR_DELIMITED_TOKEN_FORMAT")}}class qa extends o.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,o){let r;if("string"==typeof n){r={...Ka(n),instance:"0"}}else r={collection:n.collection,category:n.category,type:n.type,additionalKey:n.additionalKey,instance:"0"};return new qa({from:e,to:t,quantity:a,tokenInstance:r,uniqueKey:o||`galaconnect-operation-${Date.now()}_${Math.random().toString(36).substring(2,8)}`})}static forGALA(e,t,a,n){return new qa({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 za{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|${n.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){return{...Ka(e),instance:"0"}}}function Wa(e){if("string"==typeof e){const t=e.split("|");if(t.length<4)throw new w(`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 w(`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 w("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 w(`Invalid tokenId type: ${typeof e}. Expected string, TokenClassKey, or TokenInstanceKey`,"tokenId","INVALID_TOKEN_ID_TYPE")}function Va(e){try{const[t,a]=e.split("|");if(!a)throw new Error("Missing token part after service");const n=a.split("$");if(n.length<4)throw new Error(`Invalid vault address format. Expected at least 4 parts separated by $, got ${n.length}`);if(!(n[0]&&n[1]&&n[2]&&n[3]))throw new Error("All vault address components (collection, category, type, additionalKey) must be non-empty");const o=n.slice(3,-1),r=o.length>0?o.join("$"):n[3];return{collection:n[0],category:n[1],type:n[2],additionalKey:r}}catch(t){throw new w(`Invalid vault address: "${e}". Expected format: "service|Token$Unit$SYMBOL$additionalKey$launchpad". Error: ${t instanceof Error?t.message:String(t)}`,"vaultAddress","INVALID_VAULT_ADDRESS_FORMAT")}}function ja(e){return{...Va(e),instance:"0"}}function Ha(e){return Va(e).type}var Xa=Object.freeze({__proto__:null,extractTokenSymbolFromVault:Ha,normalizeToTokenInstanceKey:Wa,parseVaultAddressToTokenClassKey:Va,parseVaultAddressToTokenInstance:ja});const Qa="gala-transfer-successful",Ya="token-transfer-successful",Za="transfer-successful-no-id";class Ja extends ca{constructor(e,t,a,n=!1,o){super(e,n),this.wallet=t,this.tokenResolver=a,this.publicAxios=o,this.signatureHelper=t?new za(t):void 0}async fetchPoolDetails(e){this.validateFetchPoolDetailsData(e);const t=await this.http.post("/api/asset/launchpad-contract/FetchSaleDetails",e);if(!t)throw D("No response from GalaChain service",500);if(1!==t.Status)throw D(`Failed to fetch pool details: Status ${t.Status}`,t.Status);const a=t.Data.reverseBondingCurveConfiguration,n=a?.minFeePortion??"0",o=a?.maxFeePortion??"0",r=(await import("bignumber.js")).default,i=!new r(n).isZero()||!new r(o).isZero(),s=t.Data;return s.reverseBondingCurveMinFeePortion=n,s.reverseBondingCurveMaxFeePortion=o,s.hasReverseBondingCurveFee=i,s.isGraduated="Finished"===t.Data.saleStatus,delete s.reverseBondingCurveConfiguration,t}async fetchLaunchTokenFee(){const e=await this.http.post("/api/asset/launchpad-contract/FetchLaunchpadFeeAmount",{});if(!e)throw D("No response from GalaChain service",500);if(1!==e.Status)throw D(`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 w("Invalid fetch pool details data: missing required fields","data","INVALID_TYPE");var t;if(!e.vaultAddress||"string"!=typeof e.vaultAddress)throw new w("Vault address is required and must be a string","vaultAddress","INVALID_VAULT_ADDRESS");if(!e.vaultAddress.startsWith("service|Token$Unit$"))throw new w("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(!t)return null;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 D(`Failed to fetch token balance from GalaChain: ${Sa(e)}`,void 0,Ea(e)?e:void 0)}}async fetchTokenClassFromChain(e){try{const t="string"==typeof e?Wa(e):e,a={tokenClasses:[{collection:t.collection,category:t.category,type:t.type,additionalKey:t.additionalKey}]},n=(await this.publicAxios.post("/api/asset/token-contract/FetchTokenClasses",a)).data;if(1!==n.Status)throw D(`Failed to fetch token class from GalaChain: Status ${n.Status}${n.Message?` - ${n.Message}`:""}`,n.Status);if(!n.Data||0===n.Data.length)throw D(`Token not found on GalaChain: ${t.collection}|${t.category}|${t.type}|${t.additionalKey}`,404);return n.Data[0]}catch(e){if(ba(e)&&404===e.response?.status)throw D("Token not found on GalaChain",404,Ea(e)?e:void 0);const t=Sa(e);if(t.includes("Token not found")||t.includes("Status"))throw e;throw D(`Failed to fetch token class from GalaChain: ${t}`,void 0,Ea(e)?e:void 0)}}async transferGala(e){if(this.validateTransferGalaData(e),!this.wallet||!this.signatureHelper)throw new Oa("Wallet required for GALA transfer operations",Ba.WALLET_REQUIRED);try{const t=B(e.recipientAddress),a=B(this.wallet.address),n=qa.forGALA(a,t,e.amount,e.uniqueKey),o=await this.signatureHelper.signTransferToken(n.toSigningPayload()),r=new qa({...n.toSigningPayload(),signedPayload:o});this.logger.debug("[DEBUG] Full GALA Transfer Request Payload:",JSON.stringify(r,null,2));const i=await this.http.post("/api/asset/token-contract/TransferToken",r);if(!i)throw new Oa("No response from GalaChain transfer service",Ba.NETWORK_ERROR);return this.logger.debug("[DEBUG] Transfer response:",JSON.stringify(i,null,2)),this.extractTransactionIdFromResponse(i,"gala")}catch(t){throw this.handleTransferError(t,"GALA transfer failed",e)}}async transferToken(e){if(this.validateTransferTokenData(e),!this.wallet||!this.signatureHelper)throw new Oa("Wallet required for token transfer operations",Ba.WALLET_REQUIRED);try{const t=B(e.to),a=B(this.wallet.address);let n;if(e.tokenId)n=Wa(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 Oa("Must provide either tokenId or tokenName for token identification",Ba.TOKEN_NOT_FOUND);n=await this.resolveTokenInstance(e.tokenName)}const o=new qa({from:a,to:t,quantity:e.amount,tokenInstance:n,uniqueKey:e.uniqueKey||`galaconnect-operation-${Date.now()}_${Math.random().toString(36).substring(2,8)}`}),r=await this.signatureHelper.signTransferToken(o.toSigningPayload()),i=new qa({...o.toSigningPayload(),signedPayload:r});this.logger.debug("[DEBUG] Full Transfer Request Payload:",JSON.stringify(i,null,2));const s=await this.http.post("/api/asset/token-contract/TransferToken",i);if(!s)throw new Oa("No response from GalaChain transfer service",Ba.NETWORK_ERROR);return this.logger.debug("[DEBUG] Token transfer response:",JSON.stringify(s,null,2)),this.extractTransactionIdFromResponse(s,"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 Oa)throw t;throw new Oa(`Failed to resolve token class key for '${e}': ${Sa(t)}`,Ba.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 w("Invalid GALA transfer data: missing required fields");if(!U(e.recipientAddress))throw new Oa("Invalid recipient address format",Ba.INVALID_RECIPIENT,{recipientAddress:e.recipientAddress});Ma.validateAmount(e.amount),Ma.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 w("Invalid token transfer data: missing required fields");if(!U(e.to))throw new Oa("Invalid recipient address format",Ba.INVALID_RECIPIENT,{recipientAddress:e.to});if(!e.tokenId&&!e.tokenName)throw new Oa("Must provide either tokenId or tokenName for token identification",Ba.TOKEN_NOT_FOUND);if(e.tokenName&&!Ua.TOKEN_NAME_PATTERN.test(e.tokenName))throw new Oa("Invalid token name format",Ba.TOKEN_NOT_FOUND,{tokenName:e.tokenName});Ma.validateAmount(e.amount),Ma.validateUniqueKey(e.uniqueKey)}async resolveTokenInstance(e){try{const t=await this.tokenResolver.resolveTokenToVault(e);if(!t)throw new Oa(`Token '${e}' not found or not available for transfer`,Ba.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 Oa)throw t;throw new Oa(`Failed to resolve token '${e}': ${Sa(t)}`,Ba.TOKEN_NOT_FOUND,{tokenName:e})}}resolveTokenInstanceFromVaultAddress(e){try{return ja(e)}catch(e){if(e instanceof w)throw new Oa(`Invalid vault address format: ${e.message}`,Ba.TOKEN_NOT_FOUND);throw new Oa(`Failed to parse vault address: ${e instanceof Error?e.message:String(e)}`,Ba.TOKEN_NOT_FOUND)}}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?Qa:Ya:Za}if("transactionId"in e&&"string"==typeof e.transactionId&&e.transactionId)return e.transactionId}throw new Oa("Transfer succeeded but transaction ID could not be extracted",Ba.NETWORK_ERROR)}handleTransferError(e,t,a){if(e instanceof Oa)return e;if(e instanceof w)return new Oa(e.message,Ba.INVALID_AMOUNT);if(ba(e)&&e.response){const t=e.response.status,n=e.response.data;if(400===t)return new Oa(("string"==typeof n?.message?n.message:void 0)||"Invalid transfer request",Ba.INVALID_AMOUNT);if(403===t)return new Oa("Insufficient balance for transfer",Ba.INSUFFICIENT_BALANCE);if(404===t){const e={};return"tokenName"in a&&(e.tokenName=a.tokenName),new Oa("Token not found",Ba.TOKEN_NOT_FOUND,e)}}if("object"==typeof e&&null!==e&&"code"in e&&("ECONNABORTED"===e.code||"ETIMEDOUT"===e.code))return new Oa("Transfer request timed out",Ba.NETWORK_ERROR);const n=Sa(e);return new Oa(n||t,Ba.NETWORK_ERROR)}}class en{constructor(e,t,a,n=!1){this.dexBackendHttp=e,this.cache=t,this.galaChainService=a,this.logger=new p({debug:n,context:"DexService"})}async fetchTokenPrice(e){const{tokenName:t,tokenId:a}=e;if(!t&&!a)throw F("tokenName or tokenId","Either tokenName (for launchpad tokens) or tokenId (for DEX tokens) is required");if(t&&a)throw new w("tokenName and tokenId are mutually exclusive - provide only one","params","INVALID_PARAMS");if(a)return this.logger.debug(`Fetching spot price by tokenId: ${a}`),this._fetchDexTokenSpotPrice(a);throw new w("tokenName parameter requires LaunchpadSDK routing - call LaunchpadSDK.fetchTokenPrice({tokenName}) instead","tokenName","INVALID_PARAMS")}async _fetchDexTokenSpotPrice(e){if(!e)throw F("tokenId","Token ID");try{const t=Wa(e),a=Ga(`${t.collection}|${t.category}|${t.type}|${t.additionalKey}`);if(this.logger.debug(`Fetching DEX spot price for token: ${a}`),!this.dexBackendHttp)throw D("DEX Backend API client not configured");const n=await this.dexBackendHttp.request({method:"GET",url:"/v1/trade/price",params:{token:a}});if(!n.data||"string"!=typeof n.data)throw new w("Invalid price response: data must be a string, got "+typeof n.data,"data","INVALID_RESPONSE");const o=parseFloat(n.data);if(isNaN(o))throw new w(`Invalid price value: could not parse "${n.data}" as number`,"data","INVALID_CALCULATION");const r=`${t.collection}|${t.category}|${t.type}|${t.additionalKey}`;let i;try{if(this.cache){const e=this.cache.getByTokenId(r);if(e?.symbol)return i=e.symbol,this.logger.debug(`DEX spot price for ${i} (cached): $${o}`),{symbol:i,price:o}}this.logger.debug(`Symbol cache miss for ${r}, fetching from API`);i=(await this.fetchTokenDetails(e)).symbol,this.cache&&(this.cache.setByTokenId(r,{symbol:i}),this.logger.debug(`Cached symbol for ${r}: ${i}`)),this.logger.debug(`DEX spot price for ${i}: $${o}`)}catch(e){this.logger.debug(`Could not fetch token details for symbol, falling back to type field: ${e instanceof Error?e.message:String(e)}`),i=t.type.toUpperCase(),this.logger.debug(`DEX spot price for ${i} (fallback): $${o}`)}return{symbol:i,price:o}}catch(e){if(e instanceof w)throw e;throw D(`Failed to fetch DEX spot price: ${Sa(e)}`)}}async fetchLaunchpadTokenSpotPrice(e,t,a){if(!e||"string"!=typeof e)throw new Error(f);try{if(a)try{this.logger.debug(`Checking graduation status for token: ${e}`);const t=await a(e);if(t&&t.isGraduated){this.logger.debug(`Token ${e} is graduated, using DEX spot price`);const a=`${t.sellingToken.collection}|${t.sellingToken.category}|${t.sellingToken.type}|${t.sellingToken.additionalKey}`;return this._fetchDexTokenSpotPrice(a)}}catch(t){this.logger.debug(`Could not determine graduation status for ${e}, falling back to bonding curve: ${Sa(t)}`)}this.logger.debug(`Using bonding curve calculation for token: ${e}`);const n=await t({tokenName:e,amount:"1",type:"native"}),o=await this._fetchDexTokenSpotPrice({collection:"GALA",category:"Unit",type:"none",additionalKey:"none"});if(!o)throw D("GALA price not available");const r=Number(n.amount)/1e18;if(r<=0)throw new w(`Invalid token amount calculation: ${r}`,"amount","INVALID_CALCULATION");const i=o.price/r;return{symbol:e.toUpperCase(),price:i}}catch(t){if(t instanceof Error)throw D(`Failed to calculate launchpad token spot price for ${e}: ${t.message}`);throw D(`Failed to calculate launchpad token spot price for ${e}: ${Sa(t)}`)}}async fetchTokenDetails(e){this.logger.debug("Fetching token details from GalaChain for tokenId:",e);try{if(!this.galaChainService)throw D("GalaChainService not available for token metadata fetch",500);const t=await this.galaChainService.fetchTokenClassFromChain(e),a={collection:t.collection,category:t.category,type:t.type,additionalKey:t.additionalKey,symbol:t.symbol,decimals:t.decimals,name:t.name,image:t.image,description:t.description,network:t.network,...void 0!==t.contractAddress&&{contractAddress:t.contractAddress}};return this.logger.debug(`Fetched token details for ${t.symbol} from GalaChain`),a}catch(t){if((t instanceof w||t instanceof Error)&&("NetworkError"===t.name||t.message.includes("Token not found")))throw t;throw D(`Failed to fetch token details from GalaChain for ${e}: ${Sa(t)}`,500)}}}function tn(e,t=18){const a=parseFloat(e);if(0===a)return"0";return a.toFixed(t).replace(/\.?0+$/,"")}function an(e){return tn(e,8)}function nn(e){return tn(e,18)}function on(e){return{maxAcceptableReverseBondingCurveFee:an(e.maxAcceptableReverseBondingCurveFee)}}new p({debug:!1,context:"NumberUtils"});class rn extends o.ChainCallDTO{constructor(e,t,a="0",n={maxAcceptableReverseBondingCurveFee:"0"}){super(),this.vaultAddress=e,this.nativeTokenQuantity=an(t),this.expectedToken=nn(a),this.extraFees=on(n)}}class sn extends o.ChainCallDTO{constructor(e,t,a,n={maxAcceptableReverseBondingCurveFee:"0"}){super(),this.vaultAddress=e,this.tokenQuantity=nn(t),this.expectedNativeToken=an(a),this.extraFees=on(n)}}class cn extends o.ChainCallDTO{constructor(e,t,a="0",n={maxAcceptableReverseBondingCurveFee:"0"}){super(),this.vaultAddress=e,this.tokenQuantity=nn(t),this.expectedNativeToken=an(a),this.extraFees=on(n)}}class ln extends o.ChainCallDTO{constructor(e,t,a,n={maxAcceptableReverseBondingCurveFee:"0"}){super(),this.vaultAddress=e,this.nativeTokenQuantity=an(t),this.expectedToken=nn(a),this.extraFees=on(n)}}const un={BuyNativeDto:rn,BuyExactDto:sn,SellExactDto:cn,SellNativeDto:ln};var dn,hn,pn;!function(e){e[e.METAMASK=0]="METAMASK",e[e.TRUST_WALLET=1]="TRUST_WALLET",e[e.GALA_WALLET=2]="GALA_WALLET"}(dn||(dn={}));class gn extends la{constructor(e,t=!1){super(t),this.walletProvider=e}async signDTO(e,t,a,n=dn.METAMASK){try{this.logger.debug("🔐 Signing DTO:",{methodName:t,walletPreference:n,dtoKeys:Object.keys(e)});const r=this.generateEIP712Types(t,e),i=o.calculatePersonalSignPrefix(e),s={...e,prefix:i};let c,l,u;switch(n){case dn.GALA_WALLET:({signature:c,domain:l}=await this.signWithGalaWallet(r,s,t,a));break;case dn.TRUST_WALLET:c=await this.signWithTrustWallet(s),l={name:"ethereum",chainId:1};break;case dn.METAMASK:default:({signature:c,domain:l}=await this.signWithMetaMask(r,s))}return u=n===dn.TRUST_WALLET?{...s,signature:c}:{...e,signature:c,types:r,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 P(`Failed to sign DTO: ${Sa(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 C("Wallet provider does not support typed data signing","walletProvider");{const o=await this.walletProvider.getNetwork();a={name:o.name,chainId:Number(o.chainId)},n=await this.walletProvider.signTypedData(a,e,t)}}return{signature:n,domain:a}}catch(e){throw P(`MetaMask/ethers signing failed: ${Sa(e)}`)}}async signWithTrustWallet(e){try{const a=(t=e,JSON.stringify(t));let n;if(!this.walletProvider.signMessage)throw C("Wallet provider does not support signMessage","walletProvider");return n=await this.walletProvider.signMessage(a),n}catch(e){throw P(`TrustWallet signing failed: ${Sa(e)}`)}var t}async signWithGalaWallet(e,t,a,n){try{const o={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 r={domain:o,types:e,message:t,Primary_type:a},i=window;if(!i.gala)throw C("GalaWallet not found in window object","galaWallet");await i.gala.setAddress(n);return{signature:await i.gala.request({method:"eth_signTypedData",params:[JSON.stringify(r),n]}),domain:o}}catch(e){throw P(`GalaWallet signing failed: ${Sa(e)}`)}}generateEIP712Types(e,t){const a={};a[e]=[];const n=(e,t,o,r=!1)=>{if(void 0!==t){if(Array.isArray(t)){const i=n(e,t[0],o,!0);return r||a[o].push({name:e,type:(i??e)+"[]"}),i?i+"[]":void 0}if("object"==typeof t&&null!==t){if(a[e])throw new w(`Type name collision not supported: ${e}`,"fieldValue","TYPE_COLLISION");return a[e]=[],Object.entries(t).forEach(([t,a])=>{n(t,a,e)}),r||a[o].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 w(`Unsupported type for fieldName ${e}: ${typeof t}, value: ${t}`,"fieldValue","UNSUPPORTED_TYPE")}return r||a[o].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 dn.METAMASK;const e=window;return e.gala?dn.GALA_WALLET:e.trustWallet?.isTrust?dn.TRUST_WALLET:(e.ethereum,dn.METAMASK)}}class mn extends la{constructor(e=!1){super(e)}generateStringsInstructions(e){try{this.logger.debug("🔧 Generating stringsInstructions for:",e);const t=this.extractTokenSymbolFromVault(e),a=this.createTokenInstance(t),n=this.createGalaInstance(),o=`$service$${a.toStringKey()}$launchpad`,r=`$tokenBalance$${a.toStringKey()}$${e}`,i=`$tokenBalance$${a.toStringKey()}$${e}`,s=`$tokenBalance$${n.toStringKey()}$${e}`,c=[o,r,i,s,`$tokenBalance$${n.toStringKey()}$${e}`];return this.logger.debug("✅ Generated stringsInstructions:",c),c}catch(e){this.logger.error("❌ Failed to generate stringsInstructions:",e);const t=Sa(e);throw new w(`Failed to generate stringsInstructions: ${t}`,"vaultAddress","INVALID_VAULT_ADDRESS")}}createTokenInstance(e){const t=new s.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 s.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 F("vaultAddress","Vault address");try{const t=Ha(e);return this.logger.debug("🔍 Extracted token symbol:",{vaultAddress:e,tokenSymbol:t}),t}catch(e){if(e instanceof w)throw x("vaultAddress","format: service|Token$Unit$SYMBOL$eth:address$launchpad");throw e}}validateVaultAddress(e){if(!e||"string"!=typeof e)throw F("vaultAddress","Vault address");if(!e.startsWith("service|Token$Unit$"))throw x("vaultAddress",'starting with "service|Token$Unit$"');if(!e.endsWith("$launchpad"))throw x("vaultAddress",'ending with "$launchpad"');const t=e.split("$");if(t.length<5)throw x("vaultAddress",'having at least 5 parts separated by "$"');const a=t[2];if(!a||!/^[A-Za-z]{1,10}$/.test(a))throw x("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){try{return Ka(e)}catch(e){if(e instanceof w)throw x("stringKey","format: collection$category$type$additionalKey (4 parts)");throw e}}}function fn(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 i(e);if(n.isNaN())throw new Error(`Invalid expected amount: ${e}. Must be a valid number`);if(0===t)return e;const o=n.multipliedBy(t);let r;switch(a){case"buy-native":case"sell-exact":r=n.minus(o);break;case"buy-exact":case"sell-native":r=n.plus(o);break;default:throw new Error(`Unknown operation type: ${a}`)}return r.isLessThan(0)&&(r=new i(0)),r.toFixed()}class yn extends ca{constructor(e,t,a=!1,n,o,r=.05,i=.01){super(e,a),this.tokenResolver=t,this.walletProvider=n,this.userAddress=o,this.defaultSlippageToleranceFactor=r,this.defaultMaxAcceptableReverseBondingCurveFeeSlippageFactor=i,this.bundleEndpoint="/bundle",n&&o&&(this.signatureService=new gn(n,a),this.tokenKeyService=new mn(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.http.post(this.bundleEndpoint,t);return a?(this.logger.debug("📥 Bundle API response:",{success:a.success,hasData:!!a.data,error:a.error}),this.handleBundleResponse(a)):{success:!1,error:"No response from bundle API"}}catch(e){return this.logger.error("❌ Bundle transaction submission failed:",e),{success:!1,error:this.formatErrorMessage(e)}}}validateBundleData(e){if(!e)throw F("bundleData","Bundle data");if(!e.signedDto)throw F("signedDto","Signed DTO");if(!e.method||"string"!=typeof e.method)throw F("method","Method name");if(!Array.isArray(e.stringsInstructions))throw x("stringsInstructions","an array of resource tracking strings");if(0===e.stringsInstructions.length)throw new w("stringsInstructions cannot be empty","stringsInstructions","EMPTY_ARRAY");const t=["BuyWithNative","BuyExactToken","SellExactToken","SellWithNative"];if(!t.includes(e.method))throw x("method",`one of: ${t.join(", ")}`);e.stringsInstructions.forEach((e,t)=>{if("string"!=typeof e||0===e.length)throw new w(`stringsInstructions[${t}] must be a non-empty string`,`stringsInstructions[${t}]`,"INVALID_INSTRUCTION");if(!e.startsWith("$"))throw new w(`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(ba(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 Sa(e)||"Unknown bundle transaction error"}async getBundlerTransactionResult(e){try{if(!e||"string"!=typeof e)throw F("transactionId","Transaction ID");this.logger.debug("🔍 Checking bundler transaction result:",e);const t=await this.http.get(`${this.bundleEndpoint}?id=${e}`);return t?(this.logger.debug("📊 Bundler transaction result:",t),{success:!0,data:t}):{success:!1,error:"No response from bundler transaction query"}}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 F("transactionId","Transaction ID");this.logger.debug("🚫 Cancelling transaction:",e);const t=await this.http.delete(`${this.bundleEndpoint}/${e}`);return t?(this.logger.debug("🗑️ Transaction cancellation response:",t),{success:!0,data:t}):{success:!1,error:"No response from transaction cancellation"}}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.http.get(`${this.bundleEndpoint}/health`);return e?(this.logger.debug("💚 Bundle service health:",e),{success:!0,data:e}):{success:!1,error:"No response from bundle service health check"}}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:o}=e,{effectiveSlippageFactor:r,effectiveMaxFee:i,vaultAddress:s}=await this.prepareTradingOperation(t,e.maxAcceptableReverseBondingCurveFee,e.maxAcceptableReverseBondingCurveFeeSlippageFactor,e.slippageToleranceFactor);if("native"===n){if(!o)throw new w("expectedAmount is required for native buy operations. Use getBuyTokenAmount() first to calculate expected tokens.","expectedAmount","EXPECTED_AMOUNT_REQUIRED");const e=fn(o,r,"buy-native");this.logger.debug("BuyNative slippage applied:",{originalExpectedTokens:o,slippageFactor:r,adjustedMinTokens:e});const t=new un.BuyNativeDto(s,a,e,{maxAcceptableReverseBondingCurveFee:i});return await this.executeBundleTransaction(t,"BuyWithNative",s)}{if(!o)throw new w("expectedAmount is required for exact buy operations. Use getBuyTokenAmount() first to calculate expected GALA cost.","expectedAmount","EXPECTED_AMOUNT_REQUIRED");const e=fn(o,r,"buy-exact");this.logger.debug("BuyExact slippage applied:",{originalExpectedGalaCost:o,slippageFactor:r,adjustedMaxGalaCost:e});const t=new un.BuyExactDto(s,a,e,{maxAcceptableReverseBondingCurveFee:i});return await this.executeBundleTransaction(t,"BuyExactToken",s)}}async sellToken(e){this.ensureTradingServicesAvailable();const{tokenName:t,amount:a,type:n,expectedAmount:o}=e,{effectiveSlippageFactor:r,effectiveMaxFee:i,vaultAddress:s}=await this.prepareTradingOperation(t,e.maxAcceptableReverseBondingCurveFee,e.maxAcceptableReverseBondingCurveFeeSlippageFactor,e.slippageToleranceFactor);if("exact"===n){if(!o)throw new w("expectedAmount is required for exact sell operations. Use getSellTokenAmount() first to calculate expected GALA.","expectedAmount","EXPECTED_AMOUNT_REQUIRED");const e=fn(o,r,"sell-exact");this.logger.debug("SellExact slippage applied:",{originalExpectedGala:o,slippageFactor:r,adjustedMinGala:e});const t=new un.SellExactDto(s,a,e,{maxAcceptableReverseBondingCurveFee:i});return await this.executeBundleTransaction(t,"SellExactToken",s)}{if(!o)throw new w("expectedAmount is required for native sell operations. Use getSellTokenAmount() first to calculate tokens to sell.","expectedAmount","EXPECTED_AMOUNT_REQUIRED");const e=fn(o,r,"sell-native");this.logger.debug("SellNative slippage applied:",{originalExpectedTokensToSell:o,slippageFactor:r,adjustedMaxTokensToSell:e});const t=new un.SellNativeDto(s,a,e,{maxAcceptableReverseBondingCurveFee:i});return await this.executeBundleTransaction(t,"SellWithNative",s)}}async prepareTradingOperation(e,t,a,n){const{effectiveSlippageFactor:o,effectiveMaxFee:r}=this.calculateEffectiveSlippage(t,a,n),i=await this.resolveTokenNameToVault(e);if(!i)throw I(e);return{effectiveSlippageFactor:o,effectiveMaxFee:r,vaultAddress:i}}calculateEffectiveSlippage(e,t,a){const n=a??this.defaultSlippageToleranceFactor,o=t??this.defaultMaxAcceptableReverseBondingCurveFeeSlippageFactor;let r=e||"0";return e&&(r=fn(e,o,"buy-exact"),this.logger.debug("Reverse bonding curve fee slippage applied:",{baseFee:e,slippageFactor:o,adjustedMaxFee:r})),{effectiveSlippageFactor:n,effectiveFeeSlippageFactor:o,effectiveMaxFee:r}}ensureTradingServicesAvailable(){if(!this.signatureService||!this.tokenKeyService)throw C("Trading services not available. BundleService requires walletProvider and userAddress for trading operations.","walletProvider");if(!this.userAddress)throw F("userAddress","User address")}async executeBundleTransaction(e,t,a){this.ensureTradingServicesAvailable();try{e.uniqueKey=`galaswap - operation - ${c.v4()}-${Date.now()}-${this.userAddress}`;const n=await this.signatureService.signDTO(e,t,this.userAddress),o=this.tokenKeyService.generateStringsInstructions(a),r={stringsInstructions:o,method:t,signedDto:n};this.logger.debug("📦 Bundle transaction data:",{method:t,stringsInstructions:o,dtoKeys:Object.keys(n)});const i=await this.submitTransaction(r);if(i.success&&i.data)return this.logger.debug("✅ Bundle transaction submitted:",i.data),{success:!0,data:{transactionId:i.data,message:"Transaction submitted successfully. Monitor WebSocket for completion."}};throw new Error(String(i.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"}(hn||(hn={})),exports.SDKTransactionStatus=void 0,(pn=exports.SDKTransactionStatus||(exports.SDKTransactionStatus={})).PENDING="pending",pn.PROCESSING="processing",pn.COMPLETED="completed",pn.FAILED="failed",pn.TIMEOUT="timeout";const wn={[hn.PROCESSED]:exports.SDKTransactionStatus.COMPLETED,[hn.COMPLETED]:exports.SDKTransactionStatus.COMPLETED,[hn.SUCCESS]:exports.SDKTransactionStatus.COMPLETED,[hn.FAILED]:exports.SDKTransactionStatus.FAILED,[hn.ERROR]:exports.SDKTransactionStatus.FAILED,[hn.PROCESSING]:exports.SDKTransactionStatus.PROCESSING,[hn.PENDING]:exports.SDKTransactionStatus.PENDING};class kn extends la{constructor(e,t=!1){super(t),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.isSocketIOAvailable=this.checkSocketIOAvailability()}checkSocketIOAvailability(){try{return"function"==typeof l.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=l.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,o=n?.data,r=n?.status||n?.Status||o?.status||o?.Status;let i=n?.message||n?.Message||o?.message||o?.Message||n?.error||o?.error;i&&"string"==typeof i||(i=r===hn.FAILED||r===hn.ERROR?"Transaction failed - check transaction details":r===hn.COMPLETED||r===hn.PROCESSED||r===hn.SUCCESS?"Transaction completed successfully":r?`Transaction status: ${r}`:"Unknown transaction status");const s=n?.blockHash||o?.blockHash,c=n?.gasUsed||o?.gasUsed,l={transactionId:e,status:this.mapSocketStatus(r),message:"string"==typeof i?i:"Transaction update received",timestamp:Date.now(),...s&&{blockHash:s},...c&&{gasUsed:c}};if(this.logger.debug(`📡 Mapped status for ${e}: ${r} -> ${l.status}`),this.logger.debug(`📡 Final message: "${i}"`),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 wn[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}getSocket(){return this.socket}}class vn extends la{constructor(e,t=!1){super(t),this.poolService=e,this.cache=new Map}async resolveTokenToVault(e){if(!_(e))throw new w("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 I(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){try{return Va(e)}catch(e){if(e instanceof w)throw x("vaultAddress","format: service|Token$Unit$SYMBOL$eth:address$launchpad","Vault address");throw e}}}async function An(e,t={}){const{maxPages:a=1e4,logger:n,pageSize:o=20}=t,r=[];let i=1,s=!0,c=0;for(;s&&i<=a;){n&&n.debug(`Auto-pagination: fetching page ${i} with limit ${o}`);const t=await e(i,o);if(!t||!Array.isArray(t.items)){n&&n.warn("Auto-pagination: received invalid result structure, stopping");break}if(r.push(...t.items),c=t.total,n&&n.debug(`Auto-pagination: page ${i} returned ${t.items.length} items, hasNext: ${t.hasNext}`),0===t.items.length){n&&n.debug(`Auto-pagination: no items returned on page ${i}, exiting loop`);break}s=t.hasNext,i++}return i>a&&n&&n.warn(`Auto-pagination: exceeded maxPages limit of ${a}, stopping iteration`),n&&n.debug(`Auto-pagination: completed with total items: ${r.length}, total count: ${c}`),{items:r,total:c}}function Tn(e,t=e.length,a="items"){return{...{page:1,limit:e.length||0,total:t,totalPages:Math.ceil(t/(e.length||1))||1,hasNext:!1,hasPrevious:!1},[a]:e}}class En extends ca{constructor(e,t=!1,a){super(e,t),this.tokenResolverService=a}async fetchTokenClassKeyByTokenName(e){if(!this.tokenResolverService)throw C("TokenResolverService is required for token name resolution. Ensure it is passed to PriceHistoryService constructor.","tokenResolverService");if(!e||"string"!=typeof e||e.length<3||e.length>20)throw C("Token name must be a string between 3 and 20 characters","tokenName");this.logger.debug(`Resolving token name '${e}' to token class key`);try{const t=await this.tokenResolverService.resolveTokenToVault(e);if(!t)throw C(`Token '${e}' not found or could not be resolved to vault address`,"tokenName");this.logger.debug(`Resolved '${e}' to vault address: ${t}`);const a=Va(t),n=`${a.collection}|${a.category}|${a.type}|${a.additionalKey}`;return this.logger.debug(`Extracted token class key: ${n}`),n}catch(t){if(t instanceof Error&&t.message.includes("ConfigurationError"))throw t;throw D(`Failed to resolve token name '${e}': ${Sa(t)}`,500)}}async fetchPriceHistory(e){this.logger.debug("Fetching price history from DEX Backend API with options:",e),this.validateOptions(e);try{let t=e.tokenId;if(e.tokenName){this.logger.debug(`Resolving token name '${e.tokenName}' to token ID`);const a=await this.fetchTokenClassKeyByTokenName(e.tokenName);t=a,this.logger.debug(`Resolved to token ID: ${a}`)}if(!t)throw C("Token ID is required but was not provided or resolved","tokenId");const{normalizeToTokenInstanceKey:a}=await Promise.resolve().then(function(){return Xa}),n=a(t),o=Ga(`${n.collection}|${n.category}|${n.type}|${n.additionalKey}`),{from:r,to:i,sortOrder:s="DESC",page:c=1,limit:l=10}=e,u={token:o,page:String(c),limit:String(l)};r&&(u.from=r.toISOString()),i&&(u.to=i.toISOString());const d=function(e){if(e)return e.toLowerCase()}(s);d&&(u.order=d),this.logger.debug(`Querying price snapshots for token ${o}, page ${c}, limit ${l}`);const h=await this.http.get("/price-oracle/fetch-price",u);if(!h)throw D("No response from price history service",500);const p=this.transformApiResponseToPriceHistory(h);return this.logger.debug(`Found ${p.snapshots.length} price snapshots, total ${p.total}`),p}catch(e){if(e instanceof Error&&(e.message.includes("ConfigurationError")||e.message.includes("NetworkError")))throw e;throw D(`Failed to fetch price history: ${Sa(e)}`,500)}}transformApiResponseToPriceHistory(e){if(!e?.data?.data||!Array.isArray(e.data.data))throw D("Invalid API response: missing or invalid data.data array",500);if(!e?.data?.meta)throw D("Invalid API response: missing data.meta pagination info",500);const t=e.data.data.map(e=>({price:e.price,timestamp:new Date(e.createdAt),tokenId:`${e.collection}|${e.category}|${e.type}|${e.additionalKey}`})),a=e.data.meta,n=a.currentPage??1,o=a.totalPages??1;return{snapshots:t,page:n,limit:a.pageSize??50,total:a.totalItems??0,totalPages:o,hasNext:n<o,hasPrevious:n>1}}async fetchAllPriceHistory(e){this.logger.debug("Fetching all price history with options:",e);const t=await An((t,a)=>this.fetchPriceHistory({...e,page:t,limit:a}).then(e=>({items:e.snapshots,page:e.page,limit:e.limit,total:e.total,totalPages:e.totalPages,hasNext:e.hasNext,hasPrevious:e.hasPrevious})),{maxPages:1e4,logger:this.logger,pageSize:50});return Tn(t.items,t.total,"snapshots")}validateOptions(e){const{from:t,to:a,sortOrder:n,page:o=1,limit:r=10}=e;if(function(e,t,a,n={}){const{description:o="parameter",treatEmptyAsNull:r=!0}=n,i=e[t],s=e[a],c=null!=i&&(!r||""!==i),l=null!=s&&(!r||""!==s);if(!c&&!l)throw C(`Either ${t} or ${a} must be provided (${o})`,a);if(c&&l)throw C(`Cannot provide both ${t} and ${a}. Provide exactly one (${o}).`,a)}(e,"tokenName","tokenId",{description:"token identifier"}),t&&!(t instanceof Date)&&isNaN(new Date(t).getTime()))throw C("from must be a valid Date","from");if(a&&!(a instanceof Date)&&isNaN(new Date(a).getTime()))throw C("to must be a valid Date","to");if(n&&"ASC"!==n&&"DESC"!==n)throw C('sortOrder must be either "ASC" or "DESC"',"sortOrder");if(!Number.isInteger(o)||o<1)throw C("page must be a positive integer","page");if(!Number.isInteger(r)||r<1||r>50)throw C("limit must be between 1 and 50","limit")}}function Sn(e,t="0"){return new i(null==e||""===e?t:e)}function bn(e){return new i(Math.pow(1.0001,e/2))}function Nn(e,t=!1){const a=Sn(e),n=new i(1).dividedBy(a);return t?n.toFixed():n}class In extends la{constructor(e,t){if(super(!1),this.tokenConverter=new Ra,this.webSocketService=t,!e.gatewayBaseUrl||!e.bundlerBaseUrl||!e.dexBackendBaseUrl)throw new Error("GSwapService requires explicit gatewayBaseUrl, bundlerBaseUrl, and dexBackendBaseUrl configuration. These must be provided by LaunchpadSDK to ensure environment alignment.");if(e.privateKey&&e.walletAddress)try{const t=new u.PrivateKeySigner(e.privateKey);this.gswap=new u.GSwap({signer:t,walletAddress:e.walletAddress,gatewayBaseUrl:e.gatewayBaseUrl,bundlerBaseUrl:e.bundlerBaseUrl,dexBackendBaseUrl:e.dexBackendBaseUrl}),this.logger.debug("GSwap initialized in full-access mode")}catch(e){throw this.logger.error("Failed to initialize GSwap with signer",e),new Error("Failed to initialize GSwap SDK with signer")}else this.gswap=new u.GSwap({gatewayBaseUrl:e.gatewayBaseUrl,bundlerBaseUrl:e.bundlerBaseUrl,dexBackendBaseUrl:e.dexBackendBaseUrl}),this.logger.debug("GSwap initialized in read-only mode")}async getSwapQuoteExactInput(e){try{this.logger.debug("Getting swap quote for exact input",{fromToken:e.fromToken,toToken:e.toToken,amount:e.amount});const{gswapToken0:t,gswapToken1:a}=this.convertTokenPair(e.fromToken,e.toToken),n=await this.gswap.quoting.quoteExactInput(t,a,e.amount);return{fromToken:e.fromToken,toToken:e.toToken,inputAmount:e.amount,estimatedOutput:n.outTokenAmount.toFixed(),feeTier:n.feeTier,priceImpact:n.priceImpact.toString(),executionPrice:this.calculateExecutionPrice(e.amount,n.outTokenAmount)}}catch(e){this.handleGSwapError("Failed to get swap quote for exact input",T,e)}}async getSwapQuoteExactOutput(e){try{this.logger.debug("Getting swap quote for exact output",{fromToken:e.fromToken,toToken:e.toToken,amount:e.amount});const{gswapToken0:t,gswapToken1:a}=this.convertTokenPair(e.fromToken,e.toToken),n=await this.gswap.quoting.quoteExactOutput(t,a,e.amount);return{fromToken:e.fromToken,toToken:e.toToken,inputAmount:n.inTokenAmount.toFixed(),estimatedOutput:e.amount,feeTier:n.feeTier,priceImpact:n.priceImpact.toString(),executionPrice:this.calculateExecutionPrice(n.inTokenAmount,e.amount)}}catch(e){this.handleGSwapError("Failed to get swap quote for exact output",T,e)}}async executeSwap(e){try{if(!this.gswap)throw new Error("GSwap SDK not initialized");this.logger.debug("Executing swap",{fromToken:e.fromToken,toToken:e.toToken,inputAmount:e.inputAmount});const{gswapToken0:t,gswapToken1:a}=this.convertTokenPair(e.fromToken,e.toToken),n=function(e,t=.01){const a=Sn(e),n=new i(1).minus(t);return a.multipliedBy(n)}(e.estimatedOutput,e.slippageTolerance||.01),o=(await this.gswap.swaps.swap(t,a,e.feeTier,{exactIn:e.inputAmount,amountOutMinimum:n.toFixed()})).transactionId;if(!o)throw new Error("Failed to get transaction ID from swap result");this.logger.debug("Swap submitted, monitoring transaction",{transactionId:o,fromToken:e.fromToken,toToken:e.toToken}),await this.ensureWebSocketConnected();const r=await this.webSocketService.waitForTransaction(o);return{transactionId:r.transactionId,status:r.status,fromToken:e.fromToken,toToken:e.toToken,inputAmount:e.inputAmount,outputAmount:e.estimatedOutput,feeTier:e.feeTier,slippageTolerance:e.slippageTolerance||.01,timestamp:new Date(r.timestamp),wait:async e=>{await this.webSocketService.waitForTransaction(o)}}}catch(e){const t=e;this.handleGSwapError("Failed to execute swap",E,e,{transactionHash:t?.txHash})}}async getUserAssets(e){try{this.logger.debug("Fetching user assets",{walletAddress:e});const t=await this.gswap.assets.getUserAssets(e);return(Array.isArray(t)?t:t?.tokens||[]).map(e=>{const t=e.symbol||"UNKNOWN";try{return{tokenId:{collection:"Token",category:"Unit",type:t,additionalKey:"none"},symbol:t,balance:(a=e.quantity||e.balance||0,void 0!==n?Sn(a).toFixed(n):Sn(a).toFixed()),decimals:e.decimals||18}}catch(a){return this.logger.debug(`Skipping asset with processing error: ${t}`,{error:a instanceof Error?a.message:String(a),assetData:{symbol:t,quantity:e.quantity,balance:e.balance,decimals:e.decimals}}),null}var a,n}).filter(e=>null!==e)}catch(t){this.handleGSwapError("Failed to fetch user assets",b,t,{walletAddress:e})}}async getPoolInfo(e,t){try{this.logger.debug("Fetching pool info",{tokenA:e,tokenB:t});const{gswapToken0:a,gswapToken1:n}=this.convertTokenPair(e,t),o=[500,3e3,1e4];let r=new i(0),s=0;for(const c of o)try{const e=await this.gswap.pools.getPoolData(a,n,c);e&&(r=r.plus(new i(e.liquidity||0)),s++)}catch{this.logger.debug("Pool not found for fee tier",{tokenA:e,tokenB:t,feeTier:c})}return{tokenA:e,tokenB:t,liquidity:r.toFixed(),feeTiers:o,swapCount:s}}catch(a){this.logger.warn("Failed to fetch pool info",a);const n=a;return this.logger.debug("Pool error details",{error:new S(`Failed to fetch pool info: ${n?.message||String(a)}`,a,e,t,this.extractGSwapErrorCode(a))}),{tokenA:e,tokenB:t,liquidity:"0",feeTiers:[500,3e3,1e4],swapCount:0}}}async getUserLiquidityPositions(e,t=100,a){try{this.logger.debug("Fetching user liquidity positions",{ownerAddress:e,limit:t});const n=await this.gswap.positions.getUserPositions(e,t,a);let o;Array.isArray(n)?o=n:n?.positions&&Array.isArray(n.positions)?o=n.positions:n?o=[n]:(o=[],this.logger.warn("No positions returned from API for wallet",{ownerAddress:e}));const r=o.filter(e=>null!=e&&"object"==typeof e&&"positionId"in e);return r.length!==o.length&&this.logger.warn("Filtered out invalid position objects",{total:o.length,valid:r.length}),this.logger.debug("Retrieved liquidity positions",{count:r.length}),r}catch(e){this.handleGSwapError("Failed to fetch user liquidity positions",N,e)}}async getLiquidityPosition(e,t){try{this.logger.debug("Fetching liquidity position",{ownerAddress:e,position:t});const{gswapToken0:a,gswapToken1:n}=this.convertTokenPair(t.token0,t.token1),o=await this.gswap.positions.getPosition(e,{token0:a,token1:n,fee:t.fee,tickLower:t.tickLower,tickUpper:t.tickUpper});if(!o||"object"!=typeof o||!("positionId"in o))throw new Error("Invalid position data returned from API");return this.logger.debug("Retrieved liquidity position",{positionId:o.positionId}),o}catch(e){this.handleGSwapError("Failed to fetch liquidity position",N,e)}}async getLiquidityPositionById(e,t){try{this.logger.debug("Fetching liquidity position by ID",{ownerAddress:e,positionId:t});const a=await this.gswap.positions.getPositionById(e,t);if(!a||"object"!=typeof a||!("positionId"in a))throw new Error("Invalid position data returned from API");return this.logger.debug("Retrieved liquidity position by ID",{positionId:a.positionId}),a}catch(e){this.handleGSwapError("Failed to fetch liquidity position by ID",N,e)}}async estimateRemoveLiquidity(e){try{this.logger.debug("Estimating liquidity removal",{token0:e.token0,token1:e.token1});const{gswapToken0:t,gswapToken1:a}=this.convertTokenPair(e.token0,e.token1),n=await this.gswap.positions.estimateRemoveLiquidity({token0:t,token1:a,fee:e.fee,liquidity:e.liquidity,tickLower:e.tickLower,tickUpper:e.tickUpper});return this.logger.debug("Estimated removal",{result:n}),n}catch(e){this.handleGSwapError("Failed to estimate liquidity removal",N,e)}}async addLiquidityByPrice(e){try{if(!this.gswap)throw new Error("GSwap SDK not initialized");this.logger.debug("Adding liquidity by price",{token0:e.token0,token1:e.token1,priceRange:`${e.minPrice}-${e.maxPrice}`});const{gswapToken0:t,gswapToken1:a}=this.convertTokenPair(e.token0,e.token1),n=await this.gswap.positions.addLiquidityByPrice({token0:t,token1:a,fee:e.fee,minPrice:e.minPrice,maxPrice:e.maxPrice,amount0Desired:e.amount0Desired,amount1Desired:e.amount1Desired,amount0Min:e.amount0Min||"0",amount1Min:e.amount1Min||"0"}),o=n.transactionId;return this.logger.debug("Liquidity added",{transactionId:o}),o&&await this.ensureWebSocketConnected(),n}catch(e){this.handleGSwapError("Failed to add liquidity by price",N,e)}}async addLiquidityByTicks(e){try{if(!this.gswap)throw new Error("GSwap SDK not initialized");this.logger.debug("Adding liquidity by ticks",{token0:e.token0,token1:e.token1,tickRange:`${e.tickLower}-${e.tickUpper}`});const{gswapToken0:t,gswapToken1:a}=this.convertTokenPair(e.token0,e.token1),n=await this.gswap.positions.addLiquidityByTicks({token0:t,token1:a,fee:e.fee,tickLower:e.tickLower,tickUpper:e.tickUpper,amount0Desired:e.amount0Desired,amount1Desired:e.amount1Desired,amount0Min:e.amount0Min||"0",amount1Min:e.amount1Min||"0"}),o=n.transactionId;return this.logger.debug("Liquidity added",{transactionId:o}),o&&await this.ensureWebSocketConnected(),n}catch(e){this.handleGSwapError("Failed to add liquidity by ticks",N,e)}}async removeLiquidity(e){try{if(!this.gswap)throw new Error("GSwap SDK not initialized");this.logger.debug("Removing liquidity",{token0:e.token0,token1:e.token1,liquidity:e.liquidity});const{gswapToken0:t,gswapToken1:a}=this.convertTokenPair(e.token0,e.token1),n=await this.gswap.positions.removeLiquidity({token0:t,token1:a,fee:e.fee,tickLower:e.tickLower,tickUpper:e.tickUpper,liquidity:e.liquidity,amount0Min:e.amount0Min||"0",amount1Min:e.amount1Min||"0"}),o=n.transactionId;return this.logger.debug("Liquidity removed",{transactionId:o}),o&&await this.ensureWebSocketConnected(),n}catch(e){this.handleGSwapError("Failed to remove liquidity",N,e)}}async collectPositionFees(e){try{if(!this.gswap)throw new Error("GSwap SDK not initialized");this.logger.debug("Collecting position fees",{token0:e.token0,token1:e.token1});const{gswapToken0:t,gswapToken1:a}=this.convertTokenPair(e.token0,e.token1),n=await this.gswap.positions.collectPositionFees({token0:t,token1:a,fee:e.fee,tickLower:e.tickLower,tickUpper:e.tickUpper}),o=n.transactionId;return this.logger.debug("Fees collected",{transactionId:o}),o&&await this.ensureWebSocketConnected(),n}catch(e){this.handleGSwapError("Failed to collect position fees",N,e)}}async getPoolData(e,t,a){try{this.logger.debug("Getting pool data",{tokenA:e,tokenB:t,feeTier:a});const{gswapToken0:n,gswapToken1:o}=this.convertTokenPair(e,t),r=await this.gswap.pools.getPoolData(n,o,a),s=this.calculatePriceFromSqrtPriceX96(new i(r.sqrtPriceX96));return{tokenA:e,tokenB:t,feeTier:a,liquidity:r.liquidity.toString(),sqrtPriceX96:r.sqrtPriceX96.toString(),tick:r.tick,feeGrowthGlobal0X128:r.feeGrowthGlobal0X128.toString(),feeGrowthGlobal1X128:r.feeGrowthGlobal1X128.toString(),currentPrice:s.toFixed()}}catch(e){this.handleGSwapError("Failed to get pool data",S,e)}}async calculateDexPoolSpotPrice(e,t,a){try{this.logger.debug("Calculating spot price",{tokenA:e,tokenB:t,feeTier:a});const n=await this.getPoolData(e,t,a),o=Sn(n.currentPrice);return{tokenA:e,tokenB:t,feeTier:a,price:o.toFixed(),invertedPrice:Nn(o,!0),tick:n.tick,liquidity:n.liquidity}}catch(e){this.handleGSwapError("Failed to calculate spot price",S,e)}}async calculateOptimalPositionSize(e,t,a,n,o,r,s){try{this.logger.debug("Calculating optimal position size",{tokenA:e,tokenB:t,desiredAmount0:n,desiredAmount1:o});const c=(await this.getPoolData(e,t,a)).tick,l=this.calculateLiquidityFromAmount0(new i(n),r,Math.min(s,c)),u=this.calculateLiquidityFromAmount1(new i(o),Math.max(r,c),s),d=i.min(l,u),h=this.calculateAmount0FromLiquidity(d,r,c),p=this.calculateAmount1FromLiquidity(d,c,s),g=new i(n),m=new i(o);return{amount0:h.toFixed(),amount1:p.toFixed(),liquidity:d.toFixed(),ratio:h.dividedBy(p).toFixed(),utilizationPercent:{amount0:h.dividedBy(g).multipliedBy(100).toFixed(2),amount1:p.dividedBy(m).multipliedBy(100).toFixed(2)}}}catch(e){this.handleGSwapError("Failed to calculate optimal position size",N,e)}}async validatePositionParameters(e,t,a,n,o,r,s){const c=[],l=[];try{this.logger.debug("Validating position parameters",{tokenA:e,tokenB:t,tickLower:n,tickUpper:o});const u=[500,3e3,1e4];u.includes(a)||c.push(`Invalid fee tier: ${a}. Must be one of: ${u.join(", ")}`);const d=this.getTickSpacing(a);let h;n%d!==0&&c.push(`tickLower must be multiple of ${d}`),o%d!==0&&c.push(`tickUpper must be multiple of ${d}`),n>=o&&c.push(`tickLower (${n}) must be less than tickUpper (${o})`);try{h=await this.getPoolData(e,t,a)}catch{return c.push(`Pool not found for ${e}/${t} at fee tier ${a}`),{valid:!1,errors:c,warnings:l,gasEstimate:0}}const p=new i(r),g=new i(s);(p.isNaN()||p.isNegative())&&c.push("amount0 must be a valid positive number"),(g.isNaN()||g.isNegative())&&c.push("amount1 must be a valid positive number");const m=h.tick;(m<n||m>o)&&l.push("Position is out of current price range - will not earn fees until price moves into range");new i(h.liquidity).lt("1000000")&&l.push("Low pool liquidity - consider higher slippage tolerance");const f=0===c.length?35e4:0;return{valid:0===c.length,errors:c,warnings:l,gasEstimate:f,tickSpacing:d,currentTick:m,poolLiquidity:h.liquidity}}catch(e){return c.includes(e.message)||c.push(`Validation failed: ${e?.message||String(e)}`),{valid:!1,errors:c,warnings:l,gasEstimate:0}}}async calculateTicksForPrice(e,t,a,n,o){try{this.logger.debug("Calculating ticks for price range",{tokenA:e,tokenB:t,minPrice:a,maxPrice:n});const r=this.getTickSpacing(o),s=new i(a),c=new i(n);if(s.gte(c))throw new Error("minPrice must be less than maxPrice");const l=Math.floor(Math.log(s.toNumber())/Math.log(1.0001)),u=Math.ceil(Math.log(c.toNumber())/Math.log(1.0001)),d=Math.floor(l/r)*r,h=Math.ceil(u/r)*r,p=Math.pow(1.0001,d),g=Math.pow(1.0001,h),m=new i(p),f=new i(g);return{tokenA:e,tokenB:t,feeTier:o,tickLower:d,tickUpper:h,tickSpacing:r,requestedMinPrice:a,requestedMaxPrice:n,actualMinPrice:m.toFixed(8),actualMaxPrice:f.toFixed(8),priceDeviation:{minPriceDeviation:m.minus(s).dividedBy(s).multipliedBy(100).toFixed(4),maxPriceDeviation:f.minus(c).dividedBy(c).multipliedBy(100).toFixed(4)}}}catch(e){this.handleGSwapError("Failed to calculate ticks for price",N,e)}}async calculatePriceForTicks(e,t,a,n){try{this.logger.debug("Calculating price for ticks",{tokenA:e,tokenB:t,tickLower:a,tickUpper:n});const o=Math.pow(1.0001,a),r=Math.pow(1.0001,n);let s;try{s=(await this.getPoolData(e,t,3e3)).currentPrice}catch{}const c=new i(o),l=new i(r);return{tokenA:e,tokenB:t,tickLower:a,tickUpper:n,minPrice:c.toFixed(8),maxPrice:l.toFixed(8),currentPrice:s,priceRange:`${c.toFixed(4)} - ${l.toFixed(4)}`,tickSpread:n-a}}catch(e){this.handleGSwapError("Failed to calculate price for ticks",N,e)}}calculateExecutionPrice(e,t){try{const a=new i(e),n=new i(t);return a.isZero()?"0":n.dividedBy(a).toFixed()}catch{return"0"}}getTickSpacing(e){switch(e){case 500:return 10;case 3e3:return 60;case 1e4:return 200;default:throw new Error(`Invalid fee tier: ${e}`)}}calculatePriceFromSqrtPriceX96(e){try{const t=this.getQ96();return e.dividedBy(t).pow(2)}catch{return new i(0)}}calculateLiquidityFromAmount0(e,t,a){try{const n=bn(t),o=bn(a);return e.multipliedBy(n).multipliedBy(o).dividedBy(o.minus(n))}catch{return new i(0)}}calculateLiquidityFromAmount1(e,t,a){try{const n=bn(t),o=bn(a);return e.dividedBy(o.minus(n))}catch{return new i(0)}}calculateAmount0FromLiquidity(e,t,a){try{const n=bn(t),o=bn(a);return e.multipliedBy(o.minus(n)).dividedBy(n.multipliedBy(o))}catch{return new i(0)}}calculateAmount1FromLiquidity(e,t,a){try{const n=bn(t),o=bn(a);return e.multipliedBy(o.minus(n))}catch{return new i(0)}}convertTokenPair(e,t){return{gswapToken0:this.tokenConverter.toLaunchpadFormat(e),gswapToken1:this.tokenConverter.toLaunchpadFormat(t)}}handleGSwapError(e,t,a,n){this.logger.error(e,a);const o=this.extractGSwapErrorCode(a),r=a,i=[`${e}: ${r?.message||String(a)}`,a];throw n&&("GSwapSwapError"===t.name&&n.transactionHash&&i.push(n.transactionHash),"GSwapPoolError"===t.name&&(n.tokenA&&i.push(n.tokenA),n.tokenB&&i.push(n.tokenB)),"GSwapAssetError"===t.name&&n.walletAddress&&i.push(n.walletAddress)),o&&i.push(o),new t(...i)}extractGSwapErrorCode(e){if(e&&"object"==typeof e){const t=e;if(t.constructor&&"GSwapSDKError"===t.constructor.name)return t.code;if("code"in t&&"string"==typeof t.code)return t.code}}async ensureWebSocketConnected(){this.webSocketService.isConnected()||await this.webSocketService.connect()}getQ96(){return new i(2).pow(96)}}class Fn{}Fn.BASE_PRICE=1650667151e-14,Fn.PRICE_SCALING_FACTOR=1166069e-12,Fn.TRADING_FEE_FACTOR=.001,Fn.GAS_FEE="1",Fn.MIN_UNBONDING_FEE_FACTOR=0,Fn.MAX_UNBONDING_FEE_FACTOR=.5,Fn.NET_UNBONDING_FEE_FACTOR=.5,Fn.DEFAULT_LAUNCHPAD_TOKEN_MAX_SUPPLY=1e7;class xn extends la{constructor(e=!1){super(e),this.cache=new Map}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>=xn.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||Fn.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 o=0;o+=2*n.length,void 0!==a.reverseBondingCurveMinFeeFactor&&(o+=8),void 0!==a.reverseBondingCurveMaxFeeFactor&&(o+=8),void 0!==a.reverseBondingCurveNetFeeFactor&&(o+=8),o+=8,a.vaultAddress&&(o+=2*a.vaultAddress.length),a.maxSupply&&(o+=2*a.maxSupply.length),a.symbol&&(o+=2*a.symbol.length),o+=32,t+=o}),{totalTokens:this.cache.size,cacheSize:t,oldestEntry:this.cache.size>0?e:0}}getByTokenId(e){const t=`token:${e.toLowerCase().trim()}`;return this.cache.get(t)||null}setByTokenId(e,t){const a=`token:${e.toLowerCase().trim()}`;this.updateCacheEntry(a,t)}hasByTokenId(e){const t=`token:${e.toLowerCase().trim()}`;return this.cache.has(t)}}xn.MAX_CACHE_SIZE=1e4;class Dn extends ca{constructor(e,t,a=!1){super(e,a),this.dexBackendBaseUrl=t}async fetchDexPools(e={}){const{search:t,sortBy:a="tvl",sortOrder:n="desc",page:o=sa.DEFAULT_PAGE,limit:r=sa.DEFAULT_LIMIT}=e;this.logger.debug("Fetching DEX pools",{search:t,sortBy:a,sortOrder:n,page:o,limit:r});const i=new URLSearchParams({page:o.toString(),limit:Math.min(r,20).toString(),sortBy:a,sortOrder:n});t&&i.append("search",t);const s=`${this.dexBackendBaseUrl}/explore/pools?${i}`;try{const e=await this.http.get(s);if(!e||!e.data)throw new Error("No response from DEX pool service");const t=e.data.pools,a=e.data.count,n=Math.min(r,20),i=Math.ceil(a/n);return this.logger.debug("DEX pools fetched successfully",{poolCount:t.length,total:a,totalPages:i}),{pools:t,page:o,limit:n,total:a,totalPages:i,hasNext:o<i,hasPrevious:o>1}}catch(e){throw this.logger.error("Failed to fetch DEX pools",{error:e,url:s}),e}}async fetchAllDexPools(e={}){this.logger.debug("Fetching all DEX pools (auto-paginated)",e);const t=await An((t,a)=>this.fetchDexPools({...e,page:t,limit:a}).then(e=>({items:e.pools,page:e.page,limit:e.limit,total:e.total,totalPages:e.totalPages,hasNext:e.hasNext,hasPrevious:e.hasPrevious})),{maxPages:1e4,logger:this.logger,pageSize:20});return this.logger.debug("All DEX pools fetched",{totalPoolsFetched:t.items.length,totalCount:t.total}),Tn(t.items,t.total,"pools")}}function Cn(e){const t=function(e){const t=yt(e);return t.success?[]:t.errors||["Unknown validation error"]}(e);if(t.length>0)throw new Error(`LaunchTokenData validation failed:\n${t.map(e=>`- ${e}`).join("\n")}`)}const Pn="/api/asset/launchpad-contract/CallNativeTokenIn",Ln="/api/asset/launchpad-contract/CallNativeTokenOut",$n="/api/asset/launchpad-contract/CallMemeTokenIn",_n="/api/asset/launchpad-contract/CallMemeTokenOut";class Un extends o.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 On={NATIVE:"native",EXACT:"exact"},Mn={LOCAL:"local",EXTERNAL:"external"};class Rn{static calculateBuyWithExact(e,t){const a=parseFloat(e),n=parseFloat(t),{BASE_PRICE:o,PRICE_SCALING_FACTOR:r,TRADING_FEE_FACTOR:s,GAS_FEE:c}=Fn,l=this.roundUp(o*(Math.exp((n+a)*r)-Math.exp(n*r))/r,8),u=new i(l).multipliedBy(s).toFixed();return{amount:l.toString(),reverseBondingCurveFee:"0",transactionFee:u,gasFee:c}}static calculateBuyWithNative(e,t){const a=parseFloat(e),n=parseFloat(t),{BASE_PRICE:o,PRICE_SCALING_FACTOR:r,TRADING_FEE_FACTOR:s,GAS_FEE:c}=Fn,l=Math.log(a*r/o+Math.exp(n*r))/r-n,u=new i(l).multipliedBy(s).toFixed();return{amount:l.toString(),reverseBondingCurveFee:"0",transactionFee:u,gasFee:c}}static calculateSellWithExact(e,t,a,n,o){const r=parseFloat(e),s=parseFloat(t),c=parseFloat(a),{BASE_PRICE:l,PRICE_SCALING_FACTOR:u,TRADING_FEE_FACTOR:d,GAS_FEE:h}=Fn,p=l*(Math.exp(s*u)-Math.exp((s-r)*u))/u,g=new i(p),m=n+s/c*(o-n),f=g.multipliedBy(m).toFixed(8,i.ROUND_UP),y=g.multipliedBy(d).toFixed();return{amount:p.toString(),reverseBondingCurveFee:f,transactionFee:y,gasFee:h}}static calculateSellWithNative(e,t,a,n,o){const r=parseFloat(e),s=parseFloat(t),c=parseFloat(a),{BASE_PRICE:l,PRICE_SCALING_FACTOR:u,TRADING_FEE_FACTOR:d,GAS_FEE:h}=Fn,p=s-Math.log(Math.exp(s*u)-r*u/l)/u,g=new i(r),m=n+s/c*(o-n),f=g.multipliedBy(m).toFixed(8,i.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 Gn{constructor(e,t,a,n,o,r,i="local"){this.http=e,this.tokenResolver=t,this.logger=a,this.bundleHttp=n,this.galaChainHttp=o,this.dexApiHttp=r,this.defaultCalculateAmountMode=i,this.metadataCache=new xn}addIfDefined(e,t,a){return void 0!==a&&(e[t]=a),e}async uploadImageByTokenName(e){const{tokenName:t,options:a}=e;Ut(t);const n=`${t}.png`;Pa(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 x("file","a File object (browser) or Buffer (Node.js)");{const n=`${a.tokenName||t}.png`,o=new Blob([a.file],{type:"image/png"});e.append("image",o,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 D(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 C("File upload failed: FormData not supported in this environment. Ensure you have proper polyfills for Node.js environments.","FormData");throw e}}async fetchPoolsFromAPI(e){Bt(e),e.tokenName&&Ut(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=g(t),n=await this.http.get("/launchpad/fetch-pool",a);if(!0===n.error||200!==n.status||!n.data)throw D(n.message||"Failed to fetch pools",n.status);let o=[];const r=(await import("bignumber.js")).default;if(n.data.tokens)if(Array.isArray(n.data.tokens))o=n.data.tokens.map(e=>{const t=e.reverseBondingCurveMinFeePortion??"0",a=e.reverseBondingCurveMaxFeePortion??"0",n=!new r(t).isZero()||!new r(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",i=!new r(t).isZero()||!new r(a).isZero();o=[{...e,reverseBondingCurveMinFeePortion:t,reverseBondingCurveMaxFeePortion:a,hasReverseBondingCurveFee:i,createdAt:e.created_at||e.createdAt||""}]}else n.data.pools&&Array.isArray(n.data.pools)&&(o=n.data.pools.map(e=>{const t=e.reverseBondingCurveMinFeePortion??"0",a=e.reverseBondingCurveMaxFeePortion??"0",n=!new r(t).isZero()||!new r(a).isZero();return{...e,reverseBondingCurveMinFeePortion:t,reverseBondingCurveMaxFeePortion:a,hasReverseBondingCurveFee:n,createdAt:e.created_at||e.createdAt||""}}));const{extractMetadataFromPoolData:i,isValidPoolForCaching:s}=await Promise.resolve().then(function(){return Zn});o.forEach(e=>{if(!s(e))return void this.logger.debug("Skipping pool with invalid structure for caching",e);const t=i(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:o,page:l,limit:u,total:c,totalPages:d,hasNext:l<d,hasPrevious:l>1}}async _getAmount(e){if(Mt(e),!this.galaChainHttp)throw C("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:Pn,body:{vaultAddress:a,tokenQuantity:n,IsPreMint:!1}};if("NATIVE"===e&&"OUT"===t)return{endpoint:Ln,body:{vaultAddress:a,tokenQuantity:n,IsPreMint:!1}};if("MEME"===e&&"IN"===t)return{endpoint:$n,body:{vaultAddress:a,nativeTokenQuantity:n,IsPreMint:!1}};if("MEME"===e&&"OUT"===t)return{endpoint:_n,body:{vaultAddress:a,nativeTokenQuantity:n,IsPreMint:!1}};throw x("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 D("Malformed response data from GalaChain gateway");if(1!==e.Status)throw D(`GalaChain calculation failed with status ${e.Status}`,e.Status);const{calculatedQuantity:n,extraFees:o}=e.Data;return{amount:n,reverseBondingCurveFee:o.reverseBondingCurve,transactionFee:o.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){Ot(e),e.tokenName&&Ut(e.tokenName);const t=g(e),a=await this.http.get("/launchpad/check-pool",t);if(!0===a.error||200!==a.status)throw D(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(!ga(e))throw new w("Invalid options provided. Expected { tokenName: string, from?: number, to?: number, resolution?: number }","options","INVALID_OPTIONS");const{tokenName:t,from:a,to:n,resolution:o}=e;if(Ut(t),!a||!n||!o)throw new w("Graph options (from, to, resolution) are required","options","MISSING_GRAPH_OPTIONS");const r={tokenName:t,from:a,to:n,resolution:o};Rt(r);const i=g(r),s=await this.http.get("/launchpad/get-graph-data",i);if(!0===s.error||200!==s.status||!s.data)throw D(s.message||"Failed to fetch graph data",s.status);return{dataPoints:s.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 w("Invalid options provided. Expected an options object.","options","INVALID_OPTIONS");const{tokenName:t,amount:a,type:n,currentSupply:o}=e,r=e.mode??this.defaultCalculateAmountMode;if("local"!==r&&"external"!==r)throw new w(`Invalid calculation mode "${r}". Must be "local" or "external".`,"mode","INVALID_CALCULATION_MODE");if(!t||"string"!=typeof t)throw new w("Token name is required and must be a string","tokenName","INVALID_TOKEN_NAME");if(!a||"string"!=typeof a)throw new w("Amount is required and must be a string","amount","INVALID_AMOUNT");if(n!==On.NATIVE&&n!==On.EXACT)throw new w('Type must be either "native" or "exact"',"type","INVALID_TYPE");return"external"===r?this.calculateBuyAmountExternal({tokenName:t,amount:a,type:n}):this.calculateBuyAmountLocal(this.addIfDefined({tokenName:t,amount:a,type:n},"currentSupply",o))}async calculateBuyAmountExternal(e){const{tokenName:t,amount:a,type:n}=e,o=await this.tokenResolver.resolveTokenToVault(t);if(!o)throw new w(`Token "${t}" not found. Please verify the token name is correct.`,"tokenName","TOKEN_NOT_FOUND");return n===On.EXACT?this._getAmount({type:"NATIVE",method:"IN",vaultAddress:o,amount:a}):this._getAmount({type:"MEME",method:"OUT",vaultAddress:o,amount:a})}async calculateSellAmount(e){const{tokenName:t,amount:a,type:n,currentSupply:o,maxSupply:r,reverseBondingCurveMaxFeeFactor:i,reverseBondingCurveMinFeeFactor:s}=e,c=e.mode??this.defaultCalculateAmountMode;if("local"!==c&&"external"!==c)throw new w(`Invalid calculation mode "${c}". Must be "local" or "external".`,"mode","INVALID_CALCULATION_MODE");if(!t||"string"!=typeof t)throw new w("Token name is required and must be a string","tokenName","INVALID_TOKEN_NAME");if(!a||"string"!=typeof a)throw new w("Amount is required and must be a string","amount","INVALID_AMOUNT");if(n!==On.EXACT&&n!==On.NATIVE)throw new w('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!==o&&{currentSupply:o},...void 0!==r&&{maxSupply:r},...void 0!==i&&{reverseBondingCurveMaxFeeFactor:i},...void 0!==s&&{reverseBondingCurveMinFeeFactor:s}};return this.calculateSellAmountLocal(e)}}async calculateSellAmountExternal(e){const{tokenName:t,amount:a,type:n}=e,o=await this.tokenResolver.resolveTokenToVault(t);if(!o)throw new w(`Token "${t}" not found. Please verify the token name is correct.`,"tokenName","TOKEN_NOT_FOUND");return n===On.EXACT?this._getAmount({type:"NATIVE",method:"OUT",vaultAddress:o,amount:a}):this._getAmount({type:"MEME",method:"IN",vaultAddress:o,amount:a})}async calculateBuyAmountLocal(e){const{tokenName:t,amount:a,type:n,currentSupply:o}=e;if(!a||"string"!=typeof a)throw new w("Amount is required and must be a string","amount","INVALID_AMOUNT");if(n!==On.NATIVE&&n!==On.EXACT)throw new w('Type must be either "native" or "exact"',"type","INVALID_TYPE");void 0!==o&&Kt(o,"currentSupply");const r=!o;if(r&&!t)throw new w("Token name is required when currentSupply is not provided","tokenName","MISSING_TOKEN_NAME");t&&Ut(t);let i=o;if(r){i=(await this.fetchPoolDetailsForCalculation(t)).currentSupply}return n===On.EXACT?Rn.calculateBuyWithExact(a,i):Rn.calculateBuyWithNative(a,i)}async calculateSellAmountLocal(e){const{tokenName:t,amount:a,type:n,currentSupply:o,maxSupply:r,reverseBondingCurveMaxFeeFactor:i,reverseBondingCurveMinFeeFactor:s}=e;if(!a||"string"!=typeof a)throw new w("Amount is required and must be a string","amount","INVALID_AMOUNT");if(n!==On.EXACT&&n!==On.NATIVE)throw new w('Type must be either "exact" or "native"',"type","INVALID_TYPE");void 0!==o&&Kt(o,"currentSupply");const c=!o||!r||void 0===i||void 0===s;if(c&&!t)throw new w("Token name is required when currentSupply, maxSupply, or fee factors are not provided","tokenName","MISSING_TOKEN_NAME");t&&Ut(t);let l=o,u=r,d=i,h=s;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===On.EXACT?Rn.calculateSellWithExact(a,l,u,h,d):Rn.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 ua(t,"tokenName")&&pa(t)&&da(t,"currentSupply")}(e))throw new Error("Invalid CalculateBuyAmountForGraduationOptions provided");const{tokenName:a,calculateAmountMode:n,currentSupply:o}=t;Ut(a);const r=await this.tokenResolver.resolveTokenToVault(a);if(!r)throw new w(y(a),"tokenName","VAULT_NOT_FOUND");if(!this.galaChainHttp)throw C("GalaChain HTTP client not configured");const i=await this.galaChainHttp.post("/api/asset/launchpad-contract/FetchSaleDetails",{vaultAddress:r});if(1!==i.Status)throw D(`Failed to fetch pool details: Status ${i.Status}`,i.Status);const s=i.Data,c=(await import("bignumber.js")).default,l=o??new c(s.maxSupply).minus(s.sellingTokenQuantity).toFixed(),u=s.sellingTokenQuantity;if("0"===u)throw new w(`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 C("Bundle backend client not configured. LaunchToken requires bundleHttp client.","bundleHttp");Cn(e);const t=e.preBuyQuantity||"0";if(isNaN(Number(t))||Number(t)<0)throw new w("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),o=Number(a);if(isNaN(n)||isNaN(o)||n<=0||o<=0||n>=o)throw new w("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 D("Image upload failed: No URL returned");a=t}else"string"==typeof e.tokenImage&&(a=e.tokenImage);const n=`galaswap - operation - ${c.v4()}-${Date.now()}-${this.http.getAddress()}`,r={tokenName:e.tokenName.trim(),tokenSymbol:e.tokenSymbol.trim().toUpperCase(),tokenDescription:e.tokenDescription.trim(),tokenImage:a.trim(),preBuyQuantity:t.toString(),websiteUrl:e.websiteUrl||"",telegramUrl:e.telegramUrl||"",twitterUrl:e.twitterUrl||"",tokenCategory:e.tokenCategory||"Unit",tokenCollection:e.tokenCollection||"Token",uniqueKey:n};e.reverseBondingCurveConfiguration&&(r.reverseBondingCurveConfiguration={minFeePortion:e.reverseBondingCurveConfiguration.minFeePortion.toString(),maxFeePortion:e.reverseBondingCurveConfiguration.maxFeePortion.toString()});const i=new Un(r),s=await this.http.signWithGalaChain("CreateSale",i,o.SigningType.SIGN_TYPED_DATA),{signature:l,types:u,domain:d,prefix:h}=s,p={tokenName:i.tokenName,tokenSymbol:i.tokenSymbol,tokenDescription:i.tokenDescription,tokenImage:i.tokenImage,preBuyQuantity:i.preBuyQuantity,websiteUrl:i.websiteUrl,telegramUrl:i.telegramUrl,twitterUrl:i.twitterUrl,tokenCategory:i.tokenCategory,tokenCollection:i.tokenCollection,uniqueKey:i.uniqueKey,signature:l,types:u,domain:d,...h&&{prefix:h},...i.reverseBondingCurveConfiguration&&{reverseBondingCurveConfiguration:i.reverseBondingCurveConfiguration}},g=`${e.tokenName.trim()}$Unit$none$none`,m="GALA$Unit$none$none";let f;if(parseFloat(t)>0){const e=`$service$${g}$launchpad`;f=[e,`$token$${g}$${e}`,`$tokenBalance$${g}$${e}`,`$tokenBalance$${g}$${e}`,`$tokenBalance$${m}$${e}`,`$tokenBalance$${m}$${e}`]}else{const e=`$service$${g}$launchpad`;f=[e,`$token$${g}$${e}`,`$tokenBalance$${g}$${e}`]}const y={signedDto:p,stringsInstructions:f,method:"CreateSale"},k=await this.bundleHttp.post("/bundle",y);if(k.error||!k.data)throw D(k.message||"Token launch failed");return k.data}async fetchTokenDistribution(e){if(!e)throw F("tokenName","Token name");Ut(e);const t=await this.tokenResolver.resolveTokenToVault(e);if(!t)throw new w(y(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 D(n.message||"Failed to fetch token distribution",n.status);const o=n.data;if(!Array.isArray(o))throw D("Invalid API response: expected array of holders",n.status);for(const e of o){if(!e.owner||"string"!=typeof e.owner)throw D("Invalid holder data: missing or invalid owner field",n.status);if(!e.quantity||"string"!=typeof e.quantity)throw D("Invalid holder data: missing or invalid quantity field",n.status);const t=parseFloat(e.quantity);if(isNaN(t)||!isFinite(t))throw D(`Invalid holder quantity: "${e.quantity}"`,n.status)}const r=o.reduce((e,t)=>e.plus(t.quantity),new i(0));return{holders:o.map(e=>{const t=new i(e.quantity),a=r.isZero()?0:t.dividedBy(r).multipliedBy(100).toNumber();return{address:e.owner,balance:e.quantity,percentage:a}}),totalSupply:r.toFixed(),totalHolders:o.length,lastUpdated:new Date}}async fetchTokenBadges(e){if(!e)throw F("tokenName","Token name");Ut(e);const t=await this.http.get("/launchpad/get-badge/",{tokenName:e});if(t.error||!t.data)throw D(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 o=("volume"===a?e.volumeBadges:e.engagementBadges).find(e=>e.badgeName===n);return o?.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 w("Invalid pre-mint calculation data","data","INVALID_PRE_MINT_DATA");if(!this.galaChainHttp)throw C("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 D("Malformed response data from GalaChain gateway");if(1!==a.Status)throw D(`GalaChain calculation failed with status ${a.Status}`,a.Status);const{calculatedQuantity:n,extraFees:o}=a.Data;return{amount:n,reverseBondingCurveFee:o.reverseBondingCurve,transactionFee:o.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 w(y(e),"tokenName","VAULT_NOT_FOUND");if(!this.galaChainHttp)throw C("GalaChain HTTP client not configured");const a=await this.galaChainHttp.post("/api/asset/launchpad-contract/FetchSaleDetails",{vaultAddress:t});if(1!==a.Status)throw D(`Failed to fetch pool details: Status ${a.Status}`,a.Status);const n=a.Data,o=new(0,(await import("bignumber.js")).default)(n.maxSupply).minus(n.sellingTokenQuantity).toFixed(),r=n.sellingTokenQuantity,i=n.maxSupply;let s=.5,c=0;n.reverseBondingCurveConfiguration?(s=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=s-c;return this.metadataCache.set(e,{maxSupply:i,reverseBondingCurveMaxFeeFactor:s,reverseBondingCurveMinFeeFactor:c,reverseBondingCurveNetFeeFactor:l}),{currentSupply:o,remainingTokens:r,maxSupply:i,reverseBondingCurveMaxFeeFactor:s,reverseBondingCurveMinFeeFactor:c,reverseBondingCurveNetFeeFactor:l}}async fetchCurrentSupply(e){Ut(e);const t=await this.tokenResolver.resolveTokenToVault(e);if(!t)throw new w(y(e),"tokenName","VAULT_NOT_FOUND");if(!this.galaChainHttp)throw C("GalaChain HTTP client not configured");const a=await this.galaChainHttp.post("/api/asset/launchpad-contract/FetchSaleDetails",{vaultAddress:t});if(1!==a.Status)throw D(`Failed to fetch pool details: Status ${a.Status}`,a.Status);const n=a.Data,o=new(0,(await import("bignumber.js")).default)(n.maxSupply).minus(n.sellingTokenQuantity).toFixed(),r=n.maxSupply;return this.metadataCache.set(e,{maxSupply:r}),o}getAddress(){return this.http.getAddress()}formatAddressForBackend(e){return Gt(e)}validateTokenName(e){return Ut(e)}validatePagination(e){return Bt(e)}async fetchTokenPrice(e){if(!this.dexApiHttp)throw C("DEX API client not configured. Token price fetching requires dexApiHttp client.","dexApiHttp");if(!e||Array.isArray(e)&&0===e.length)throw F("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 D(`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 ua(t,"tokenName")&&pa(t)&&da(t,"currentSupply")}(e))throw new Error("Invalid FetchLaunchpadTokenSpotPriceOptions provided");const{tokenName:a,calculateAmountMode:n,currentSupply:o}=t;if(!a||"string"!=typeof a)throw F("tokenName","Token name (string)");try{const e={tokenName:a,amount:"1",type:"native",...void 0!==n&&{mode:n},...void 0!==o&&{currentSupply:o}},t=await this.calculateBuyAmount(e),r=(await this.fetchTokenPrice("GALA"))[0];if(!r)throw D("GALA price not available");const i=Number(t.amount);if(i<=0)throw new w(`Invalid token amount calculation: ${i}`,"amount","INVALID_CALCULATION");const s=r.price/i;return{symbol:a.toUpperCase(),price:s}}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 Kn={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",dexBackendBaseUrl:"https://dex-backend-prod1.defi.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",dexBackendBaseUrl:"https://dex-backend-dev1.defi.gala.com",launchpadFrontendUrl:"https://lpad-frontend-test1.defi.gala.com"}};function qn(e){return Kn[e]}class zn extends Error{constructor(e,t){super(e),this.cause=t,this.name="WebSocketError"}}class Wn 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 Vn(e,t){if(!e)throw new zn(`Invalid WebSocket response received for transaction ${t}: response is null or undefined`);if("object"!=typeof e)throw new zn(`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 zn(`Invalid WebSocket response received for transaction ${t}: missing status field`)}function jn(e,t,a,n){Vn(e,t);const o=e,r=o.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)}(r))throw new zn(`Invalid trade data received for transaction ${t}`);const i={transactionId:t,type:a,method:"native"===n.type?"native":"exact",inputAmount:r.inputQuantity||n.amount,outputAmount:r.outputQuantity||n.expectedAmount||"0",totalFees:r.totalFees||"0",tokenName:n.tokenName,vaultAddress:r.vaultAddress||"",timestamp:Date.now()};return void 0!==o.blockHash&&(i.blockHash=o.blockHash),void 0!==o.gasUsed&&(i.gasUsed=o.gasUsed),void 0!==n.slippageToleranceFactor&&(i.slippageTolerance=n.slippageToleranceFactor),i}const Hn="3.31.2";class Xn{constructor(e){this.wallet=e.wallet;let t=null;t=e.env?qn(e.env):e.baseUrl?.includes("prod")?qn("PROD"):qn("STAGE"),this.config={baseUrl:t.launchpadBaseUrl,galaChainBaseUrl:t.galaChainBaseUrl,bundleBaseUrl:t.bundleBaseUrl,webSocketUrl:t.webSocketUrl,dexApiBaseUrl:t.dexApiBaseUrl,dexBackendBaseUrl:t.dexBackendBaseUrl,launchpadFrontendUrl:t.launchpadFrontendUrl,timeout:3e4,debug:!1,...e},this.logger=new p({debug:this.config.debug??!1,context:"LaunchpadSDK"}),this.validateConfiguration(),this.slippageToleranceFactor=void 0===e.slippageToleranceFactor?Xn.DEFAULT_SLIPPAGE_TOLERANCE_FACTOR:this.parseSlippageToleranceFactor(e.slippageToleranceFactor),this.maxAcceptableReverseBondingCurveFeeSlippageFactor=void 0===e.maxAcceptableReverseBondingCurveFeeSlippageFactor?Xn.DEFAULT_MAX_ACCEPTABLE_REVERSE_BONDING_CURVE_FEE_SLIPPAGE_FACTOR:this.parseFeeSlippageFactor(e.maxAcceptableReverseBondingCurveFeeSlippageFactor),this.calculateAmountMode=e.calculateAmountMode||Xn.DEFAULT_CALCULATE_AMOUNT_MODE,this.auth=new h({wallet:e.wallet,messagePrefix:"Create a GalaChain Wallet"}),this.http=new m(this.auth,this.config),this.galaChainHttp=new m(this.auth,{...this.config,baseUrl:this.config.galaChainBaseUrl}),this.bundleHttp=new m(this.auth,{...this.config,baseUrl:this.config.bundleBaseUrl}),this.dexApiHttp=new m(this.auth,{...this.config,baseUrl:this.config.dexApiBaseUrl}),this.dexBackendHttp=new m(this.auth,{...this.config,baseUrl:this.config.dexBackendBaseUrl}),this.galaChainPublicAxios=a.create({baseURL:this.config.galaChainBaseUrl,timeout:this.config.timeout||3e4,headers:{"Content-Type":"application/json",Accept:"application/json"}}),this.cache=new xn(e.debug||!1),this.launchpadService=new _a(this.http),this.tokenResolverService=new vn(this.launchpadService.poolService),this.launchpadAPI=new Gn(this.http,this.tokenResolverService,this.logger,this.bundleHttp,this.galaChainHttp,this.dexApiHttp,this.calculateAmountMode),this.galaChainService=new Ja(this.galaChainHttp,e.wallet,this.tokenResolverService,e.debug||!1,this.galaChainPublicAxios),this.dexService=new en(this.dexBackendHttp,this.cache,this.galaChainService,e.debug||!1),this.bundleService=new yn(this.bundleHttp,this.tokenResolverService,this.config.debug||!1,e.wallet,e.wallet?this.getAddress():void 0,this.slippageToleranceFactor,this.maxAcceptableReverseBondingCurveFeeSlippageFactor),this.websocketService=new kn({url:this.config.webSocketUrl},this.config.debug),this.priceHistoryService=new En(this.dexBackendHttp,this.config.debug||!1,this.tokenResolverService),this.gswapService=new In({privateKey:e.wallet?.privateKey,walletAddress:e.wallet?this.getAddress():void 0,gatewayBaseUrl:this.config.galaChainBaseUrl,bundlerBaseUrl:this.config.bundleBaseUrl,dexBackendBaseUrl:this.config.dexBackendBaseUrl},this.websocketService),this.dexPoolService=new Dn(this.dexBackendHttp,this.config.dexBackendBaseUrl,e.debug||!1)}createOverrideSdk(e){if(!e||"string"!=typeof e)throw C("Invalid privateKey: must be a non-empty string","privateKey");if(!e.match(/^0x[a-fA-F0-9]{64}$/))throw C('Invalid privateKey format: must be "0x" followed by 64 hexadecimal characters',"privateKey");const t=new n.Wallet(e),a={...this.config,wallet:t};return new Xn(a)}getAddress(){return this.validateWallet(),this.auth.getAddress()}getEthereumAddress(){return this.validateWallet(),this.wallet.address}validateWallet(){if(!this.wallet)throw new w("Wallet is required for this operation",void 0,"WALLET_REQUIRED");return this.wallet}setWallet(e){if(!e||"object"!=typeof e||!("address"in e))throw new w("Invalid wallet: must be an ethers Wallet instance, received "+typeof e,"wallet","INVALID_WALLET");this.wallet=e,this.auth.setWallet(e)}getWallet(){return this.wallet}hasWallet(){return void 0!==this.wallet}getConfig(){const{wallet:e,...t}=this.config;return{...t,slippageToleranceFactor:this.slippageToleranceFactor,maxAcceptableReverseBondingCurveFeeSlippageFactor:this.maxAcceptableReverseBondingCurveFeeSlippageFactor,calculateAmountMode:this.calculateAmountMode}}getVersion(){return Hn}getUrlByTokenName(e){const t=this.config.launchpadFrontendUrl;if(!t)throw C("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 fetchDexPools(e={}){return this.dexPoolService.fetchDexPools(e)}async fetchAllDexPools(e={}){return this.dexPoolService.fetchAllDexPools(e)}async fetchTokenDistribution(e){return this.launchpadService.fetchTokenDistribution(e)}async fetchTokenBadges(e){return this.launchpadService.fetchTokenBadges(e)}async fetchTokenPrice(e){const{tokenName:t,tokenId:a,currentSupply:n,calculateAmountMode:o}=e;if(t&&!a){const e={tokenName:t};return n&&(e.currentSupply=n),o&&(e.calculateAmountMode=o),this.fetchLaunchpadTokenSpotPrice(e)}if(a&&!t)try{return await this.dexService.fetchTokenPrice({tokenId:a})}catch(e){const t=function(e){if(ba(e)&&e.response)return e.response.status}(e);if(400===t||404===t){this.logger.debug(`DEX spot price not available (HTTP ${t}) for tokenId, attempting launchpad fallback`);try{const t=(await this.fetchTokenDetails(a)).name.trim().toLowerCase();if(!/^[a-z0-9]{3,20}$/.test(t))throw this.logger.error(`Token name extracted from GalaChain doesn't match launchpad format: "${t}"`),e;this.logger.debug(`Falling back to launchpad pricing using extracted token name: "${t}"`);const r={tokenName:t};return n&&(r.currentSupply=n),o&&(r.calculateAmountMode=o),this.fetchLaunchpadTokenSpotPrice(r)}catch(t){throw this.logger.error(`Launchpad fallback failed: ${Sa(t)}`),e}}throw e}if(!t&&!a)throw F("tokenName or tokenId","Either tokenName (for launchpad tokens) or tokenId (for DEX tokens) is required");throw new w("tokenName and tokenId are mutually exclusive - provide only one","params","INVALID_PARAMS")}async fetchGalaPrice(){return this.fetchTokenPrice({tokenId:{collection:"GALA",category:"Unit",type:"none",additionalKey:"none"}})}async fetchLaunchpadTokenSpotPrice(e){const t="string"==typeof e?{tokenName:e}:e;return this.dexService.fetchLaunchpadTokenSpotPrice(t.tokenName,e=>this.launchpadAPI.calculateBuyAmount(e),e=>this.fetchPoolDetails(e))}async fetchTokenDetails(e){return this.dexService.fetchTokenDetails(e)}async fetchLaunchTokenFee(){return this.galaChainService.fetchLaunchTokenFee()}async fetchPoolDetails(e){const t=await this.resolveVaultAddress(e);if(!t)throw new Error(y(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 qt}),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 qt}),a=t(e.address);if(e.tokenId){const{normalizeToTokenInstanceKey:t}=await Promise.resolve().then(function(){return Xa}),n=t(e.tokenId),{collection:o,category:r,type:i,additionalKey:s}=n;return this.galaChainService.fetchTokenBalance({owner:a,collection:o,category:r,additionalKey:s,type:i,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 F("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:o,calculateAmountMode:r,currentSupply:i}=e;let s=t;void 0===r&&void 0===i||(s={tokenName:t,...void 0!==r&&{calculateAmountMode:r},...void 0!==i&&{currentSupply:i}});const c=await this.calculateBuyAmountForGraduation(s),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!==o&&(l.privateKey=o),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)}this.validateWallet(),await this.ensureWebSocketConnection();const t=(await this.bundleService.buyToken(e)).data,a=t?.transactionId;if(!a)throw P("No transaction ID returned from buy operation");return this.waitForConfirmation(a,t=>jn(t,a,"buy",e))}async sell(e){if(e.privateKey){const t=this.createOverrideSdk(e.privateKey),{privateKey:a,...n}=e;return t.sell(n)}this.validateWallet(),await this.ensureWebSocketConnection();const t=(await this.bundleService.sellToken(e)).data,a=t?.transactionId;if(!a)throw P("No transaction ID returned from sell operation");return this.waitForConfirmation(a,t=>jn(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.validateWallet(),this.launchpadService.postComment(e)}async launchToken(e){if(e.privateKey){const t=this.createOverrideSdk(e.privateKey),{privateKey:a,...n}=e;return t.launchToken(n)}this.validateWallet(),await this.ensureWebSocketConnection();const t=await this.launchpadAPI.launchToken(e);return this.waitForConfirmation(t,a=>{Vn(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 zn(`Invalid launch data received for transaction ${t}`);const o={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&&(o.tokenImage=e.tokenImage),void 0!==e.preBuyQuantity&&(o.preBuyQuantity=e.preBuyQuantity),o.vaultAddress&&this.tokenResolverService.set(e.tokenName,o.vaultAddress),o})}async uploadTokenImage(e){if(e.privateKey){const t=this.createOverrideSdk(e.privateKey),{privateKey:a,...n}=e;return t.uploadTokenImage(n)}return this.validateWallet(),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 qt}),a=t(e)||this.getAddress();return this.launchpadService.fetchProfile(a)}async updateProfile(e){const{normalizeAddressInput:t}=await Promise.resolve().then(function(){return qt}),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.validateWallet(),this.launchpadService.updateProfile(a)}async uploadProfileImage(e){const{normalizeAddressInput:t}=await Promise.resolve().then(function(){return qt}),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.validateWallet(),this.launchpadService.uploadProfileImage(a)}async retrieveGalaFromFaucet(e){const{normalizeAddressInput:t}=await Promise.resolve().then(function(){return qt}),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 qt}),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 qt}),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 fetchPriceHistory(e){return this.priceHistoryService.fetchPriceHistory(e)}async fetchAllPriceHistory(e){return this.priceHistoryService.fetchAllPriceHistory(e)}async transferGala(e){const{normalizeAddressInput:t}=await Promise.resolve().then(function(){return qt}),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.validateWallet(),this.galaChainService.transferGala(a)}async transferToken(e){const{normalizeAddressInput:t}=await Promise.resolve().then(function(){return qt}),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.validateWallet(),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 C("baseUrl is required in configuration","baseUrl");if(!this.config.webSocketUrl)throw C("webSocketUrl is required in configuration","webSocketUrl");try{new URL(this.config.baseUrl)}catch{throw C(`Invalid baseUrl format: ${this.config.baseUrl}`,"baseUrl")}try{new URL(this.config.webSocketUrl)}catch{throw C(`Invalid webSocketUrl format: ${this.config.webSocketUrl}`,"webSocketUrl")}if(this.config.galaChainBaseUrl)try{new URL(this.config.galaChainBaseUrl)}catch{throw C(`Invalid galaChainBaseUrl format: ${this.config.galaChainBaseUrl}`,"galaChainBaseUrl")}if(this.config.bundleBaseUrl)try{new URL(this.config.bundleBaseUrl)}catch{throw C(`Invalid bundleBaseUrl format: ${this.config.bundleBaseUrl}`,"bundleBaseUrl")}if(this.config.launchpadFrontendUrl)try{new URL(this.config.launchpadFrontendUrl)}catch{throw C(`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: ${Xn.DEFAULT_SLIPPAGE_TOLERANCE_FACTOR}`),Xn.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: ${Xn.DEFAULT_MAX_ACCEPTABLE_REVERSE_BONDING_CURVE_FEE_SLIPPAGE_FACTOR}`),Xn.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 Wn(e,a.status,a.message);let n;try{n=t(a)}catch(t){if(t instanceof zn)throw t;throw new zn(`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 Wn||t instanceof zn)throw t;throw new zn(`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 Zn});e.forEach(e=>{if(!a(e))return;const n=t(e,this.logger);n&&this.launchpadAPI.warmCacheFromPoolData(e.tokenName,n)})}async getSwapQuoteExactInput(e,t,a){return this.gswapService.getSwapQuoteExactInput({fromToken:e,toToken:t,amount:a})}async getSwapQuoteExactOutput(e,t,a){return this.gswapService.getSwapQuoteExactOutput({fromToken:e,toToken:t,amount:a})}async executeSwap(e,t,a,n,o,r=.01){return this.validateWallet(),this.gswapService.executeSwap({fromToken:e,toToken:t,inputAmount:a,estimatedOutput:n,feeTier:o,slippageTolerance:r})}async getSwapUserAssets(e){return this.gswapService.getUserAssets(e)}async getSwapPoolInfo(e,t){return this.gswapService.getPoolInfo(e,t)}async getSwapUserLiquidityPositions(e,t,a){return this.gswapService.getUserLiquidityPositions(e,t,a)}async getSwapLiquidityPosition(e,t){return this.gswapService.getLiquidityPosition(e,t)}async getSwapLiquidityPositionById(e,t){return this.gswapService.getLiquidityPositionById(e,t)}async getSwapEstimateRemoveLiquidity(e){return this.gswapService.estimateRemoveLiquidity(e)}async addSwapLiquidityByPrice(e){return this.gswapService.addLiquidityByPrice(e)}async addSwapLiquidityByTicks(e){return this.gswapService.addLiquidityByTicks(e)}async removeSwapLiquidity(e){return this.gswapService.removeLiquidity(e)}async collectSwapPositionFees(e){return this.gswapService.collectPositionFees(e)}connectWebSocket(){this.websocketService.connect()}disconnectWebSocket(){this.websocketService.disconnect()}isWebSocketConnected(){return this.websocketService.isConnected()}subscribeToEvent(e,t){const a=this.websocketService.getSocket();return a?(a.on(e,t),()=>{a.off(e,t),this.logger.debug(`Unsubscribed from event: "${e}"`)}):(this.logger.warn(`⚠️ WebSocket not connected - subscribing to "${e}" without connection`),()=>{})}onDexPoolCreation(e,t){const a=1e3,n=Math.max(t?.intervalMs??3e4,a);t?.intervalMs&&t.intervalMs<a&&this.logger.warn(`Poll interval ${t.intervalMs}ms is below minimum 1000ms. Using minimum interval instead.`);const o=t?.minTVL,r=t?.tokens,i=new Map;let s=!0,c=null;let l=0;const u=async()=>{if(s){try{const t=await this.fetchDexPools({limit:20});l>0&&(this.logger.debug("Successfully recovered from polling errors"),l=0),t.pools.forEach(t=>{const a=(e=>`${e.token0}-${e.token1}-${e.fee}`)(t);if(!i.has(a)){if((e=>{if(i.set(e,!0),i.size>1e3){const e=i.keys().next().value;void 0!==e&&i.delete(e)}})(a),o){if((t.token0Tvl+t.token1Tvl)/2<o)return}if(r&&r.length>0){if(!(r.includes(t.token0)||r.includes(t.token1)))return}e(t)}})}catch(e){l++;const t=e instanceof Error?e.message:String(e);l>=5?this.logger.error(`Polling for new DEX pools failed ${l} consecutive times. Last error: ${t}. Continuing to retry...`):l>1?this.logger.warn(`Error polling for new DEX pools (attempt ${l}/5): ${t}`):this.logger.debug(`Error polling for new DEX pools: ${t}`)}if(s){const e=Math.min(Math.max(l-1,0),2),t=n*Math.pow(2,e);c=setTimeout(u,t)}}};return u(),()=>{s=!1,c&&clearTimeout(c),this.logger.debug("Stopped watching for DEX pool creation")}}onLaunchpadTokenCreation(e,t){const a=1e3,n=Math.max(t?.intervalMs??3e4,a);t?.intervalMs&&t.intervalMs<a&&this.logger.warn(`Poll interval ${t.intervalMs}ms is below minimum 1000ms. Using minimum interval instead.`);const o=t?.creatorAddress,r=new Map;let i=!0,s=null;let c=0;const l=async()=>{if(i){try{const t=await this.fetchPools({type:"recent",limit:20});c>0&&(this.logger.debug("Successfully recovered from polling errors"),c=0),t.pools.forEach(t=>{r.has(t.tokenName)||((e=>{if(r.set(e,!0),r.size>1e3){const e=r.keys().next().value;void 0!==e&&r.delete(e)}})(t.tokenName),o&&t.creatorAddress!==o||e(t))})}catch(e){c++;const t=e instanceof Error?e.message:String(e);c>=5?this.logger.error(`Polling for new launchpad tokens failed ${c} consecutive times. Last error: ${t}. Continuing to retry...`):c>1?this.logger.warn(`Error polling for new launchpad tokens (attempt ${c}/5): ${t}`):this.logger.debug(`Error polling for new launchpad tokens: ${t}`)}if(i){const e=Math.min(Math.max(c-1,0),2),t=n*Math.pow(2,e);s=setTimeout(l,t)}}};return l(),()=>{i=!1,s&&clearTimeout(s),this.logger.debug("Stopped watching for launchpad token creation")}}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 p({debug:e,context:"LaunchpadSDK"});t.debug("Starting global cleanup...");const{WebSocketService:a}=require("./services/WebSocketService");a.cleanupAll(e),t.debug("Global cleanup completed")}}Xn.DEFAULT_SLIPPAGE_TOLERANCE_FACTOR=.15,Xn.DEFAULT_MAX_ACCEPTABLE_REVERSE_BONDING_CURVE_FEE_SLIPPAGE_FACTOR=.01,Xn.DEFAULT_LAUNCHPAD_TOKEN_MAX_SUPPLY=Fn.DEFAULT_LAUNCHPAD_TOKEN_MAX_SUPPLY,Xn.DEFAULT_CALCULATE_AMOUNT_MODE=Mn.LOCAL;class Qn{static generateWallet(){try{const e=n.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 n.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),a=new n.Wallet(t),o=this.toGalaAddress(a.address);return{privateKey:a.privateKey,address:a.address,galaAddress:o,mnemonic:"test test test test test test test test test test test junk",wallet:a}}throw e}}static fromPrivateKey(e){const t=new n.Wallet(e),a=this.toGalaAddress(t.address);return{privateKey:t.privateKey,address:t.address,galaAddress:a,mnemonic:"",wallet:t}}static fromMnemonic(e,t=0){try{const a=n.Mnemonic.fromPhrase(e),o=n.HDNodeWallet.fromMnemonic(a,`m/44'/60'/0'/0/${t}`),r=new n.Wallet(o.privateKey),i=this.toGalaAddress(r.address);return{privateKey:r.privateKey,address:r.address,galaAddress:i,mnemonic:e,wallet:r}}catch(a){if("undefined"!=typeof process&&"test"===process.env.NODE_ENV){const a=`test-mnemonic-index-${t}-${e}`,o="0x"+Buffer.from(a).toString("hex").padStart(64,"1").slice(0,64),r=new n.Wallet(o),i=this.toGalaAddress(r.address);return{privateKey:r.privateKey,address:r.address,galaAddress:i,mnemonic:e,wallet:r}}throw a}}static toGalaAddress(e){const t=e.replace(/^0x/i,"");if(!/^[a-fA-F0-9]{40}$/.test(t))throw new Error(`Invalid Ethereum address format: ${e}`);return`eth|${t}`}static toEthereumAddress(e){if(!e.startsWith("eth|"))throw new Error(`Invalid Gala address format: ${e}. Must start with 'eth|'`);const t=e.slice(4);if(!/^[a-fA-F0-9]{40}$/.test(t))throw new Error(`Invalid address in Gala format: ${e}`);return`0x${t}`}static isValidEthereumAddress(e){try{const t=e.replace(/^0x/i,"");return/^[a-fA-F0-9]{40}$/.test(t)}catch{return!1}}static isValidGalaAddress(e){try{if(!e.startsWith("eth|"))return!1;const t=e.slice(4);return/^[a-fA-F0-9]{40}$/.test(t)}catch{return!1}}static generateMultipleWallets(e=1){if(e<1||e>100)throw new Error("Count must be between 1 and 100");const t=[];if("undefined"!=typeof process&&"test"===process.env.NODE_ENV)for(let a=0;a<e;a++){const e=`test-multi-${a}-${Date.now()}-${++this.testCounter}`,n="0x"+Buffer.from(e).toString("hex").padStart(64,"1").slice(0,64);t.push(this.fromPrivateKey(n))}else for(let a=0;a<e;a++)t.push(this.generateWallet());return t}static getWalletSummary(e,t=!1){const a=["🔐 Wallet Information","═".repeat(50),`📍 Address: ${e.address}`,`🎮 Gala Address: ${e.galaAddress}`,`🌱 Mnemonic: ${e.mnemonic||"Not available"}`];return t?a.splice(3,0,`🔑 Private Key: ${e.privateKey}`):a.splice(3,0,"🔑 Private Key: [HIDDEN - use includeSensitive=true to show]"),a.push("═".repeat(50)),a.push("💾 IMPORTANT: Save your mnemonic phrase securely!"),a.push("This is your backup to recover the wallet."),a.join("\n")}}function Yn(e){if(void 0===e)return Qn.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 Qn.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 Qn.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?"...":""}"`)}Qn.testCounter=0;var Zn=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),...e.config||{}},o=new Xn(n),r={sdk:o,wallet:a,config:n};if(!1!==e.autoValidate){const e=await this.validateSetup(o,a);return{...r,validation:e}}return r}static async readOnlySetup(e={}){const t=e.environment||this.detectEnvironment(),a={wallet:void 0,baseUrl:e.baseUrl||this.getDefaultBaseUrl(t),timeout:e.timeout||this.getDefaultTimeout(t),debug:e.debug??"production"!==t,...this.getEnvironmentDefaults(t),...e.config||{}};return{sdk:new Xn(a),config:a}}static async validateSetup(e,t){const a=[],n=[],o={canTrade:!1,canCreateTokens:!1,hasBalance:!1,connectionHealthy:!1};try{const t=await e.fetchGalaBalance(e.getAddress());if(o.connectionHealthy=!0,t&&t.quantity){const e=parseFloat(t.quantity);o.hasBalance=e>0,o.canTrade=e>=.1,o.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&&o.connectionHealthy,sdk:e,wallet:t||Qn.generateWallet(),issues:a,warnings:n,capabilities:o}}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,o]of Object.entries(e)){const{sdk:e}=await this.quickSetup({environment:t,privateKey:o,agentId:`multi-wallet-${n}`,autoValidate:!1});a[n]=e}return a}static detectEnvironment(){const e=process.env.NODE_ENV?.toLowerCase();return"development"===e?"development":"test"===e||"testing"===e?"testing":"production"}static setupWallet(e){if(!e){const e=process.env.PRIVATE_KEY;return e?Qn.fromPrivateKey(e):Qn.generateWallet()}return"generate"===e?Qn.generateWallet():Qn.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=Mn,exports.ConfigurationError=v,exports.FileValidationError=Ca,exports.GALA_DECIMALS=8,exports.GALA_TOKEN_CLASS_KEY={collection:"GALA",category:"Unit",type:"none",additionalKey:"none"},exports.GSwapAssetError=b,exports.GSwapPoolError=S,exports.GSwapQuoteError=T,exports.GSwapSwapError=E,exports.IMAGE_EXTENSIONS=be,exports.LAUNCHPAD_TOKEN_DECIMALS=18,exports.LaunchpadSDK=Xn,exports.MAX_CONCURRENT_POOL_FETCHES=5,exports.NetworkError=k,exports.PAGINATION_DEFAULTS=sa,exports.POOL_FETCH_CONFIG={MAX_CONCURRENT_FETCHES:5,BACKEND_PAGE_SIZE:20},exports.POOL_TYPES={RECENT:"recent",POPULAR:"popular"},exports.QUERY_FIELD_NAMES={PAGE:"page",LIMIT:"limit",TOKEN_NAME:"tokenName",VAULT_ADDRESS:"vaultAddress",USER_ADDRESS:"userAddress",TRADE_TYPE:"tradeType",POOL_TYPE:"type",SEARCH:"search",SORT_ORDER:"sortOrder",START_DATE:"startDate",END_DATE:"endDate"},exports.SDK_VERSION=Hn,exports.TRADING_TYPES=On,exports.TokenMetadataService=class extends la{constructor(e=!1){super(e),this.cache={},this.cacheExpiry=36e5}async resolveTokenMetadata(e){const t=this.getCacheKey(e),a=this.cache[t];if(a&&!this.isCacheExpired(a.timestamp))return this.logger.debug(`Using cached metadata for token: ${t}`),a.data;const n=this.extractMetadata(e);return this.cache[t]={data:n,timestamp:Date.now()},n}async getTokenSymbol(e){return(await this.resolveTokenMetadata(e)).symbol}async getTokenDecimals(e){return(await this.resolveTokenMetadata(e)).decimals}clearCache(e){e?(delete this.cache[e],this.logger.debug(`Cleared cache for token: ${e}`)):(this.cache={},this.logger.debug("Cleared all token metadata cache"))}getCacheStats(){const e=Object.keys(this.cache);return{size:e.length,entries:e}}getCacheKey(e){if("string"==typeof e)return e.toLowerCase();return`${e.type||e.symbol||"unknown"}|${e.additionalKey||"none"}`.toLowerCase()}extractMetadata(e){let t,a,n="Token",o="Unit",r="none";if("string"==typeof e)if(e.includes("|")){const i=e.split("|");"Token"===i[0]&&i[2]?(n=i[0],o=i[1]||"Unit",t=i[2],r=i[3]||"none",a=t):(t=i[0],a=t,o=i[1]||"Unit",r=i[3]||"none")}else t=e,a=e;else t=e.type||"unknown",a=e.symbol||e.type||"unknown",n=e.collection||"Token",o=e.category||"Unit",r=e.additionalKey||"none";const i=this.getDecimalsForToken(a);return{symbol:a.toUpperCase(),decimals:i,collection:n,category:o,type:t.toUpperCase(),additionalKey:r,verified:!1}}getDecimalsForToken(e){return{GALA:8,GUSDC:6,USDC:6,USDT:6,WETH:18,DAI:18}[e.toUpperCase()]??18}isCacheExpired(e){return Date.now()-e>this.cacheExpiry}setCacheExpiry(e){this.cacheExpiry=e,this.logger.debug(`Set token metadata cache expiry to ${e}ms`)}},exports.TransactionError=A,exports.TransactionFailedError=Wn,exports.ValidationError=w,exports.WebSocketError=zn,exports.WebSocketTimeoutError=class extends zn{constructor(e,t){super(`WebSocket confirmation timeout for transaction ${e} after ${t}ms`),this.name="WebSocketTimeoutError"}},exports.addressFormatSchema=z,exports.amountMethodSchema=Te,exports.amountTypeSchema=Ae,exports.browserFileSchema=Ie,exports.bufferFileSchema=Fe,exports.buyTokensDataSchema=Qe,exports.calculatePreMintDataSchema=nt,exports.checkPoolOptionsSchema=ve,exports.commentMessageSchema=se,exports.commentPaginationSchema=_e,exports.createLaunchpadSDK=function(e){e||(e={});const{wallet:t,env:a,config:o={},...r}=e,i={...r,...o},{wallet:s,env:c,config:l,...u}=i;let d;if(t)if("string"==typeof t){d=Yn(t).wallet}else{if(!(t instanceof n.Wallet))throw new Error("Invalid wallet input. Expected string (private key or mnemonic) or Wallet instance.");d=t}else{d=Yn().wallet}const h={wallet:d,...a&&{env:a},debug:!1,timeout:3e4,...u};return new Xn(h)},exports.createLimitSchema=ee,exports.createPaginatedResultSchema=function(e){return r.z.object({data:r.z.array(e),page:r.z.number().int().min(1),limit:r.z.number().int().min(1),total:r.z.number().int().min(0),totalPages:r.z.number().int().min(0),hasNext:r.z.boolean(),hasPrevious:r.z.boolean()})},exports.createTradeDataSchema=Xe,exports.createWallet=Yn,exports.ethereumAddressSchema=W,exports.faucetAmountSchema=Q,exports.fetchGalaBalanceOptionsSchema=qe,exports.fetchPoolDetailsDataSchema=ot,exports.fetchTokenBalanceOptionsSchema=Ve,exports.fileSizeSchema=oe,exports.fileUploadSchema=Ne,exports.filenameSchema=re,exports.flexibleAddressSchema=V,exports.flexibleFileSchema=xe,exports.formatGalaForDTO=an,exports.formatLaunchpadTokenForDTO=nn,exports.fullNameSchema=q,exports.getAmountOptionsSchema=at,exports.getTradeOptionsSchema=Ze,exports.graduateTokenOptionsSchema=Se,exports.graphDataOptionsSchema=Ee,exports.imageExtensionSchema=De,exports.imageFilenameSchema=Ce,exports.imageMimeTypeSchema=ie,exports.imageUploadOptionsSchema=we,exports.isoDateStringSchema=ce,exports.launchTokenDataSchema=ye,exports.nonNegativeDecimalStringSchema=X,exports.optionalUrlSchema=Z,exports.pageNumberSchema=J,exports.paginationResultMetaSchema=Me,exports.poolFetchTypeSchema=ke,exports.poolPaginationSchema=Ue,exports.positiveDecimalStringSchema=H,exports.privateKeySchema=ue,exports.reverseBondingCurveConfigSchema=fe,exports.reverseBondingCurveConfigurationSchema=rt,exports.searchQuerySchema=K,exports.sellTokensDataSchema=Ye,exports.standardLimitSchema=te,exports.standardPaginationSchema=Pe,exports.timestampSchema=le,exports.tokenCategorySchema=ge,exports.tokenCollectionSchema=me,exports.tokenDescriptionSchema=R,exports.tokenListOptionsSchema=Ge,exports.tokenNameSchema=O,exports.tokenSymbolSchema=M,exports.tokenUrlsSchema=pe,exports.tradeCalculationMethodSchema=tt,exports.tradeCalculationTypeSchema=et,exports.tradeLimitSchema=ne,exports.tradeListParamsSchema=Je,exports.tradePaginationSchema=$e,exports.tradePaginationWithFiltersSchema=Oe,exports.tradeTypeBackendSchema=He,exports.tradeTypeSchema=je,exports.transactionIdSchema=de,exports.transferFaucetsDataSchema=Ke,exports.uniqueKeySchema=he,exports.updateProfileDataSchema=ze,exports.uploadProfileImageOptionsSchema=We,exports.urlSchema=Y,exports.userLimitSchema=ae,exports.userPaginationSchema=Le,exports.userTokenNameSchema=G,exports.userTokenTypeSchema=Re,exports.userTokensPaginationSchema=Be,exports.validateAddress=ut,exports.validateAmountString=ht,exports.validateBuyTokensData=Ft,exports.validateCalculatePreMintData=Lt,exports.validateCheckPoolOptions=vt,exports.validateCreateTradeData=It,exports.validateFaucetAmount=pt,exports.validateFetchGalaBalanceOptions=Et,exports.validateFetchPoolDetailsData=$t,exports.validateFetchTokenBalanceOptions=Nt,exports.validateFullName=gt,exports.validateGetAmountOptions=Pt,exports.validateGetTradeOptions=Dt,exports.validateImageUploadOptions=kt,exports.validateLaunchTokenData=yt,exports.validateSearchQuery=mt,exports.validateSellTokensData=xt,exports.validateTokenDescription=lt,exports.validateTokenListOptions=At,exports.validateTokenName=st,exports.validateTokenSymbol=ct,exports.validateTokenUrls=wt,exports.validateTradeListParams=Ct,exports.validateTransferFaucetsData=Tt,exports.validateUpdateProfileData=St,exports.validateUploadProfileImageOptions=bt,exports.validateUserTokenName=ft,exports.validateVaultAddress=dt,exports.vaultAddressSchema=j;