@gala-chain/launchpad-sdk 4.0.1-beta.0 → 4.0.1-beta.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("axios"),require("ethers"),require("@gala-chain/connect"),require("zod"),require("bignumber.js"),require("@gala-chain/dex"),require("@gala-chain/api"),require("uuid"),require("socket.io-client"),require("path"),require("fs"),require("dotenv"),require("crypto")):"function"==typeof define&&define.amd?define(["exports","axios","ethers","@gala-chain/connect","zod","bignumber.js","@gala-chain/dex","@gala-chain/api","uuid","socket.io-client","path","fs","dotenv","crypto"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).GalaLaunchpadSDK={},e.axios,e.ethers,e.GalaChainConnect,e.z,e.BigNumber,e.GalaChainDex,e.GalaChainAPI,e.uuid,e.io,e.path,e.fs,e.dotenv,e.crypto)}(this,function(e,t,n,o,i,r,a,s,c,l,u,d,h,g){"use strict";function p(e){var t=Object.create(null);return e&&Object.keys(e).forEach(function(n){if("default"!==n){var o=Object.getOwnPropertyDescriptor(e,n);Object.defineProperty(t,n,o.get?o:{enumerable:!0,get:function(){return e[n]}})}}),t.default=e,Object.freeze(t)}var m,f,y=p(u),w=p(d),k=p(h);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"}(m||(m={}));class v extends Error{constructor(e,t,n){super(t),this.type=e,this.originalError=n,this.name="AuthError"}}class T{constructor(e){if(this.wallet=e.wallet,this.messagePrefix=e.messagePrefix||"Create a GalaChain Wallet",""===e.messagePrefix)throw new v(m.SIGNATURE_FAILED,"Message prefix cannot be empty")}hasWallet(){return void 0!==this.wallet}setWallet(e){if(void 0!==e){if("object"!=typeof e||!("address"in e))throw new v(m.WALLET_NOT_CONNECTED,"Invalid wallet: must be an ethers Wallet instance or undefined");if(!e.address||"string"!=typeof e.address)throw new v(m.INVALID_ADDRESS,"Wallet address is not available")}this.wallet=e}async generateSignature(){this.validateWallet();try{const e=Date.now(),t=`${this.messagePrefix} ${e}`,n=await this.wallet.signMessage(t);return{message:t,signature:n,address:this.formatAddress(this.wallet.address),timestamp:e}}catch(e){if(e instanceof v)throw e;throw new v(m.SIGNATURE_FAILED,"Failed to generate signature for authentication",e instanceof Error?e:new Error(String(e)))}}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 v(m.WALLET_NOT_CONNECTED,"Wallet private key not available for @gala-chain signing");return this.wallet.privateKey}formatAddress(e){const t=e.replace(/^0x/i,"");if(!/^[a-fA-F0-9]{40}$/.test(t))throw new v(m.INVALID_ADDRESS,`Invalid Ethereum address format: ${e}`);return`eth|${t}`}async signMessage(e){this.validateWallet();try{return{message:e,signature:await this.wallet.signMessage(e),address:this.wallet.address,timestamp:Date.now()}}catch(e){if(e instanceof v)throw e;const t=e instanceof Error?e.message:String(e);throw new v(m.SIGNATURE_FAILED,t,e instanceof Error?e:new Error(String(e)))}}async generateAuthHeaders(e,t){this.validateWallet();try{const n=Date.now(),o=`${this.messagePrefix} ${t.toUpperCase()} ${e} ${n}`,i=await this.wallet.signMessage(o);return{"x-signature":i,"x-address":this.formatAddress(this.wallet.address),"x-message":o,"x-timestamp":n.toString()}}catch(e){if(e instanceof v)throw e;throw new v(m.SIGNATURE_FAILED,"Failed to generate authentication headers",e instanceof Error?e:new Error(String(e)))}}async signTypedData(e,t,n){this.validateWallet();try{return await this.wallet.signTypedData(e,t,n)}catch(e){if(e instanceof v)throw e;throw new v(m.SIGNATURE_FAILED,"Failed to sign typed data",e instanceof Error?e:new Error(String(e)))}}async generateCustomSignature(e){if(!e||"string"!=typeof e||0===e.trim().length)throw new v(m.SIGNATURE_FAILED,"Custom message must be a non-empty string");this.validateWallet();try{const t=await this.wallet.signMessage(e);return{message:e,signature:t,address:this.formatAddress(this.wallet.address),timestamp:Date.now()}}catch(e){if(e instanceof v)throw e;throw new v(m.SIGNATURE_FAILED,"Failed to generate custom message signature",e instanceof Error?e:new Error(String(e)))}}validateWallet(){if(!this.wallet)throw new v(m.WALLET_NOT_CONNECTED,"Wallet is required for authentication");if(!this.wallet.address)throw new v(m.WALLET_NOT_CONNECTED,"Wallet address is not available");if(!this.wallet.privateKey&&!this.wallet.signMessage)throw new v(m.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"}(f||(f={}));class S{constructor(e){this.levelPriority={[f.DEBUG]:0,[f.INFO]:1,[f.WARN]:2,[f.ERROR]:3},this.debugEnabled=e.debug,this.context=e.context||"SDK",this.minLevel=e.minLevel||(e.debug?f.DEBUG:f.INFO)}debug(e,t){this.log(f.DEBUG,e,t)}info(e,t){this.log(f.INFO,e,t)}warn(e,t){this.log(f.WARN,e,t)}error(e,t){this.log(f.ERROR,e,t)}log(e,t,n){if(this.levelPriority[e]<this.levelPriority[this.minLevel])return;if(e===f.DEBUG&&!this.debugEnabled)return;const o=`[${(new Date).toISOString()}] [${this.context}] [${e}]`,i=this.getConsoleMethod(e);void 0!==n?n instanceof Error?i(`${o} ${t}`,n.message,n.stack):i(`${o} ${t}`,n):i(`${o} ${t}`)}getConsoleMethod(e){switch(e){case f.DEBUG:return console.debug;case f.INFO:return console.info;case f.WARN:return console.warn;case f.ERROR:return console.error;default:return console.log}}child(e){return new S({debug:this.debugEnabled,context:`${this.context}:${e}`,minLevel:this.minLevel})}isDebugEnabled(){return this.debugEnabled&&this.levelPriority[f.DEBUG]>=this.levelPriority[this.minLevel]}}function b(e){if(!e||"object"!=typeof e)return{};const t={};for(const[n,o]of Object.entries(e))null!=o&&("string"==typeof o?t[n]=o:"number"==typeof o||"boolean"==typeof o?t[n]=o.toString():Array.isArray(o)?t[n]=o.join(","):t[n]="object"==typeof o?JSON.stringify(o):String(o));return t}class A{constructor(e,n={}){this.auth=e,this.debug=n.debug??!1,this.logger=new S({debug:this.debug,context:"HttpClient"}),this.axios=t.create({baseURL:n.baseUrl||"https://lpad-backend-dev1.defi.gala.com",timeout:n.timeout||3e4,headers:{Accept:"application/json",...n.headers}}),this.setupInterceptors()}async request(e){try{const t={method:e.method,url:e.url,data:e.data,...e.params&&{params:b(e.params)},...e.headers&&{headers:e.headers},...e.timeout&&{timeout:e.timeout}};e.headers&&this.logger.debug("Custom headers provided:",e.headers),e.data instanceof FormData&&(t.headers&&t.headers["Content-Type"]&&delete t.headers["Content-Type"],this.logger.debug("FormData detected - removing Content-Type header for multipart upload"));const n=e.data instanceof FormData?"[FormData object - multipart/form-data]":e.data;this.logger.debug("Request:",{method:e.method,url:e.url,fullUrl:`${this.axios.defaults.baseURL}${e.url}`,baseURL:this.axios.defaults.baseURL,params:t.params,data:n,isFormData:e.data instanceof FormData,contentType:t.headers?.["Content-Type"]||"not set"});const o=await this.axios.request(t);return this.logger.debug("Response:",{status:o.status,data:o.data}),o.data}catch(e){throw this.logger.error("Error:",e),e}}async get(e,t,n){return this.request({method:"GET",url:e,...t&&{params:t},...n&&{headers:n}})}async post(e,t,n){return this.request({method:"POST",url:e,data:t,...n&&{headers:n}})}async put(e,t,n){return this.request({method:"PUT",url:e,data:t,...n&&{headers:n}})}async delete(e,t,n){return this.request({method:"DELETE",url:e,...t&&{params:t},...n&&{headers:n}})}async patch(e,t,n){return this.request({method:"PATCH",url:e,data:t,...n&&{headers:n}})}getAddress(){return this.auth.getAddress()}getEthereumAddress(){return this.auth.getEthereumAddress()}async signMessage(e){return(await this.auth.signMessage(e)).signature}async signTypedData(e,t,n){return await this.auth.signTypedData(e,t,n)}async signCustomMessage(e){try{const t=await this.auth.generateCustomSignature(e);return this.logger.debug("Generated custom signature:",{message:e,address:t.address,ethereumAddress:this.auth.getEthereumAddress()}),{signature:t.signature,address:t.address,ethereumAddress:this.auth.getEthereumAddress()}}catch(e){throw this.logger.error("Custom signature generation failed:",e),new Error(`Failed to generate custom signature for message: ${e instanceof Error?e.message:"Unknown error"}`)}}async signWithGalaChain(e,t,n=o.SigningType.SIGN_TYPED_DATA){const i=this.auth.getPrivateKey(),r=new o.SigningClient(i);return await r.sign(e,t,n)}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"),this.logger.debug("Final request headers being sent:",e.headers),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 E="Token name is required and must be a string",I=e=>`Could not find vault address for token: ${e}`;class P extends Error{constructor(e,t,n){super(e),this.field=t,this.code=n,this.name="ValidationError"}}class N extends Error{constructor(e,t,n){super(e),this.statusCode=t,this.originalError=n,this.name="NetworkError"}}class D extends Error{constructor(e,t){super(e),this.field=t,this.name="ConfigurationError"}}class F extends Error{constructor(e,t,n){super(e),this.transactionId=t,this.code=n,this.name="TransactionError"}}class C extends Error{constructor(e,t,n){super(e),this.originalError=t,this.code=n,this.name="GSwapQuoteError"}}class x extends Error{constructor(e,t,n,o){super(e),this.originalError=t,this.transactionHash=n,this.code=o,this.name="GSwapSwapError"}}class $ extends Error{constructor(e,t,n,o,i){super(e),this.originalError=t,this.tokenA=n,this.tokenB=o,this.code=i,this.name="GSwapPoolError"}}class L extends Error{constructor(e,t,n,o){super(e),this.originalError=t,this.walletAddress=n,this.code=o,this.name="GSwapAssetError"}}class O extends Error{constructor(e,t,n){super(e),this.originalError=t,this.code=n,this.name="GSwapPositionError"}}class _ extends P{constructor(e,t){super(e,"dexQuote","DEX_QUOTE_ERROR"),this.context=t,this.name="DexQuoteError"}}class B extends P{constructor(e){super(e,"dexPool","DEX_POOL_NOT_FOUND"),this.name="DexPoolNotFoundError"}}function U(e,t){return new P(`Token "${e}" not found. Please verify the token name is correct.`,"tokenName","TOKEN_NOT_FOUND")}function M(e,t){const n=t||e.charAt(0).toUpperCase()+e.slice(1);return new P(`${n} is required`,e,"REQUIRED_FIELD")}function q(e,t,n){const o=n||e.charAt(0).toUpperCase()+e.slice(1);return new P(`${o} must be ${t}`,e,"INVALID_FORMAT")}function R(e,t,n){return new N(e,t,n)}function K(e,t){return new D(e,t)}function G(e,t,n){return new F(e,t,n)}function z(e,t,n){const{MIN_PAGE:o,MAX_PAGE:i,MIN_LIMIT:r,MAX_LIMIT:a}=n.PAGINATION;if("number"!=typeof e||e<o||e>i)throw new P(`Page must be a number between ${o} and ${i}`,"page","INVALID_PAGE");if("number"!=typeof t||t<r||t>a)throw new P(`Limit must be a number between ${r} and ${a}`,"limit","INVALID_LIMIT")}const W={ETH_ADDRESS:/^0x[0-9a-fA-F]{40}$/,BACKEND_ADDRESS:/^eth\|[0-9a-fA-F]{40}$/};function j(e){return"string"==typeof e&&e.trim().length>0}function H(e){return!(!e||"string"!=typeof e)&&(W.ETH_ADDRESS.test(e)||W.BACKEND_ADDRESS.test(e))}function V(e){return e.startsWith("0x")?`eth|${e.slice(2)}`:e}const Q=i.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"),X=i.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"),Z=i.z.string().min(1,"Token description is required").max(500,"Token description must be at most 500 characters"),Y=i.z.string().min(1,"Token name must be at least 1 character").max(50,"Token name must be at most 50 characters"),J=i.z.string().min(1,"Search query must be at least 1 character").max(100,"Search query must be at most 100 characters"),ee=i.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"),te=i.z.string().regex(W.BACKEND_ADDRESS,"Address must be in format: eth|[40-hex-chars]"),ne=i.z.string().regex(W.ETH_ADDRESS,"Invalid Ethereum address format"),oe=i.z.string().refine(e=>W.BACKEND_ADDRESS.test(e)||W.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),ie=i.z.string().refine(e=>W.BACKEND_ADDRESS.test(e)||/^service\|Token\$Unit\$[A-Z0-9]+\$eth:[0-9a-fA-F]{40}\$launchpad$/.test(e),"Invalid vault address format"),re=i.z.string().regex(/^\d+(\.\d+)?$/,"Must be a valid decimal number").refine(e=>parseFloat(e)>0,"Amount must be greater than zero"),ae=i.z.string().regex(/^\d+(\.\d+)?$/,"Must be a valid decimal number").refine(e=>parseFloat(e)>=0,"Amount must be zero or greater"),se=i.z.string().regex(/^(?!0+(\.0+)?$)\d+(\.\d+)?$/,"Amount must be a positive, non-zero number"),ce=i.z.string().url("Must be a valid URL").regex(/^https?:\/\//,"URL must start with http:// or https://"),le=i.z.string().optional().refine(e=>!e||/^https?:\/\/.+\..+/.test(e),"Must be a valid URL if provided"),ue=i.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 de(e=100){return i.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 he=de(100),ge=de(20),pe=de(20),me=i.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"),fe=i.z.string().max(255,"Filename must be at most 255 characters"),ye=i.z.enum(["image/png","image/jpg","image/jpeg","image/gif","image/webp","image/svg+xml"]),we=i.z.string().min(1,"Comment message is required").max(500,"Comment must be at most 500 characters"),ke=i.z.string().datetime("Must be a valid ISO 8601 date string"),ve=i.z.number().int("Timestamp must be an integer").min(0,"Timestamp must be non-negative"),Te=i.z.string().regex(/^0x[a-fA-F0-9]{64}$/,"Private key must be format: 0x + 64 hex characters"),Se=i.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"),be=i.z.string().regex(/^galaconnect-operation-[a-z0-9-]+$/,"Unique key must be format: galaconnect-operation-{unique-id}"),Ae=i.z.object({websiteUrl:le,telegramUrl:le,twitterUrl:le}).refine(e=>e.websiteUrl||e.telegramUrl||e.twitterUrl,"At least one social URL (website, telegram, or twitter) is required"),Ee=i.z.string().min(1,"Token category must not be empty").default("Unit"),Ie=i.z.string().min(1,"Token collection must not be empty").default("Token"),Pe=i.z.object({minFeePortion:re,maxFeePortion:re}),Ne=i.z.object({tokenName:Q,tokenSymbol:X,tokenDescription:Z,tokenImage:i.z.union([i.z.instanceof(File),i.z.instanceof(Buffer),i.z.string().url("Token image must be a valid URL")]).optional(),preBuyQuantity:ae.default("0"),websiteUrl:le,telegramUrl:le,twitterUrl:le,tokenCategory:Ee,tokenCollection:Ie,reverseBondingCurveConfiguration:Pe.optional(),privateKey:Te.optional()}),De=i.z.object({file:i.z.union([i.z.instanceof(File),i.z.instanceof(Buffer)]),tokenName:Q}),Fe=i.z.enum(["recent","popular"]),Ce=i.z.object({tokenName:Q.optional(),symbol:X.optional()}).refine(e=>e.tokenName||e.symbol,"At least one of tokenName or symbol is required"),xe=i.z.enum(["NATIVE","MEME"]),$e=i.z.enum(["IN","OUT"]),Le=i.z.object({from:i.z.number().int("From timestamp must be an integer").min(173e6,"From timestamp must be at least 173000000"),to:i.z.number().int("To timestamp must be an integer").min(173e6,"To timestamp must be at least 173000000"),resolution:i.z.number().int("Resolution must be an integer").min(1,"Resolution must be at least 1"),tokenName:Q}),Oe=i.z.object({tokenName:Q,slippageToleranceFactor:i.z.number().min(0).max(1).optional(),maxAcceptableReverseBondingCurveFeeSlippageFactor:i.z.number().min(0).max(1).optional(),privateKey:Te.optional()}),_e=[".png",".jpg",".jpeg",".gif",".webp",".svg"],Be=i.z.object({file:i.z.union([i.z.instanceof(File),i.z.instanceof(Buffer)]),name:fe,size:me,type:ye}),Ue=i.z.instanceof(File).refine(e=>e.size>=1&&e.size<=10485760,"File size must be between 1 byte and 10MB").refine(e=>["image/png","image/jpg","image/jpeg","image/gif","image/webp","image/svg+xml"].includes(e.type),"File must be a valid image type (PNG, JPG, JPEG, GIF, WebP, or SVG)").refine(e=>e.name.length<=255,"Filename must be at most 255 characters"),Me=i.z.instanceof(Buffer).refine(e=>e.length>=1&&e.length<=10485760,"Buffer size must be between 1 byte and 10MB"),qe=i.z.union([Ue,Me]),Re=i.z.enum([".png",".jpg",".jpeg",".gif",".webp",".svg"]),Ke=fe.refine(e=>{const t=e.slice(e.lastIndexOf(".")).toLowerCase();return _e.includes(t)},`Filename must end with one of: ${_e.join(", ")}`),Ge=i.z.object({page:ue,limit:he}),ze=i.z.object({page:ue,limit:ge}),We=i.z.object({page:ue,limit:pe}),je=i.z.object({page:ue,limit:de(50)}),He=Ge.extend({type:i.z.enum(["recent","popular"]).optional(),tokenName:i.z.string().min(1).max(50).optional(),search:i.z.string().min(1).max(100).optional()}),Ve=ze.extend({tokenName:i.z.string().min(1).max(50).optional(),search:i.z.string().min(1).max(100).optional()}),Qe=We.extend({tradeType:i.z.enum(["BUY","SELL"]).optional(),tokenName:i.z.string().min(1).max(50).optional(),userAddress:i.z.string().regex(/^(0x[a-fA-F0-9]{40}|eth\|[a-fA-F0-9]{40})$/).optional(),startDate:i.z.string().datetime().optional(),endDate:i.z.string().datetime().optional(),sortOrder:i.z.enum(["ASC","DESC"]).default("DESC")}),Xe=i.z.object({page:i.z.number().int().min(1),limit:i.z.number().int().min(1),total:i.z.number().int().min(0),totalPages:i.z.number().int().min(0),hasNext:i.z.boolean(),hasPrevious:i.z.boolean()});const Ze=i.z.enum(["all","DEFI","ASSET"]),Ye=ze.extend({type:Ze.optional(),address:oe.optional(),search:J.optional(),tokenName:Y.optional()}),Je=i.z.object({walletAddress:oe,amount:se}),et=i.z.object({address:oe.optional(),refresh:i.z.boolean().optional()}),tt=i.z.object({profileImage:i.z.string(),fullName:ee,address:oe,privateKey:Te.optional()}),nt=i.z.object({file:i.z.union([i.z.instanceof(File),i.z.instanceof(Buffer)]),address:oe.optional(),privateKey:Te.optional()}),ot=i.z.object({address:oe,tokenId:i.z.union([i.z.string(),i.z.object({collection:i.z.string(),category:i.z.string(),type:i.z.string(),additionalKey:i.z.string()}),i.z.object({collection:i.z.string(),category:i.z.string(),type:i.z.string(),additionalKey:i.z.string(),instance:i.z.string()})]).optional(),tokenName:Y.optional()}).refine(e=>void 0!==e.tokenId||void 0!==e.tokenName,"At least one token identifier (tokenId or tokenName) is required"),it=i.z.enum(["buy","sell"]),rt=i.z.enum(["BUY","SELL"]),at=i.z.object({tradeType:it,tokenAmount:re,vaultAddress:ie,userAddress:oe,slippageTolerance:re.optional(),deadline:i.z.number().int().positive().optional()}),st=i.z.object({tokenSymbol:X,nativeTokenQuantity:re,expectedToken:re,maxAcceptableReverseBondingCurveFee:ae.default("0").optional()}),ct=i.z.object({tokenSymbol:X,tokenQuantity:re,expectedNativeToken:re,maxAcceptableReverseBondingCurveFee:ae.default("0").optional()}),lt=We.extend({tokenName:Y.optional()}),ut=i.z.object({page:i.z.number().int().min(1).max(1e3).default(1).optional(),limit:i.z.number().int().min(1).max(20).default(10).optional()}),dt=i.z.enum(["NATIVE","MEME"]),ht=i.z.enum(["IN","OUT"]),gt=i.z.object({type:dt,method:ht,vaultAddress:ie,amount:re}),pt=i.z.object({nativeTokenQuantity:re}),mt=i.z.object({vaultAddress:ie}),ft=i.z.object({minFeePortion:re,maxFeePortion:re});function yt(e){return t=>{const n=e.safeParse(t);return{success:n.success,data:n.success?n.data:void 0,errors:n.success?void 0:n.error.errors.map(e=>e.message)}}}const wt=yt(Q),kt=yt(X),vt=yt(Z),Tt=yt(oe),St=yt(ie),bt=yt(re),At=yt(se),Et=yt(ee),It=yt(J),Pt=yt(Y),Nt=yt(Ne),Dt=yt(Ae),Ft=yt(De),Ct=yt(Ce),xt=yt(Ye),$t=yt(Je),Lt=yt(et),Ot=yt(tt),_t=yt(nt),Bt=yt(ot),Ut=yt(at),Mt=yt(st),qt=yt(ct),Rt=yt(lt),Kt=yt(ut),Gt=yt(gt),zt=yt(pt),Wt=yt(mt);function jt(e,t){throw new P(e.join("; "),t,"VALIDATION_ERROR")}function Ht(e){const t=wt(e);!t.success&&t.errors&&jt(t.errors,"tokenName")}function Vt(e){const t=He.safeParse(e);t.success||jt(t.error.errors.map(e=>e.message),"pagination")}function Qt(e){const t=Ct(e);!t.success&&t.errors&&jt(t.errors,"options")}function Xt(e){const t=Gt(e);!t.success&&t.errors&&jt(t.errors,"options")}function Zt(e){const t=Le.safeParse(e);t.success||jt(t.error.errors.map(e=>e.message),"options")}function Yt(e){const t=oe.safeParse(e);if(!t.success)throw new P("Ethereum address must be 40 hex characters (with or without 0x prefix)","ethereumAddress","INVALID_FORMAT");return t.data}function Jt(e,t,n=!0){if(!e||""===e.trim())throw new P(`${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 P(`${t} cannot use scientific notation. Use standard decimal format (e.g., "1000" instead of "1e3").`,t,"INVALID_NUMERIC_STRING");const o=parseFloat(e);if(isNaN(o))throw new P(`${t} must be a valid numeric string. Received: "${e}"`,t,"INVALID_NUMERIC_STRING");if(!isFinite(o))throw new P(`${t} must be a finite number. Cannot be Infinity or -Infinity.`,t,"INVALID_NUMERIC_STRING");if(o<0)throw new P(`${t} must be non-negative. Received: "${e}"`,t,"INVALID_NUMERIC_STRING");if(!n&&0===o)throw new P(`${t} must be greater than zero. Received: "${e}"`,t,"INVALID_NUMERIC_STRING")}var en=Object.freeze({__proto__:null,normalizeAddressInput:function(e){if(!e)return;const t=oe.safeParse(e);if(!t.success)throw new P(`Invalid address format: ${e}. Must be either "0x..." (Ethereum) or "eth|..." (GalaChain) format`,"address","INVALID_FORMAT");return t.data},toBackendAddressFormat:Yt,validateCheckPoolOptions:Qt,validateGetAmountOptions:Xt,validateGetGraphOptions:Zt,validateNumericString:Jt,validatePagination:Vt,validateTokenName:Ht});function tn(e,t){const n=e,o=Number(n.page)||t.page,i=Number(n.limit)||t.limit,r=n.data,a=Number(n.total)||Number(r?.count)||0;return{page:o,limit:i,total:a,totalPages:Math.ceil(a/i)}}function nn(e,t){return{hasNext:e<t,hasPrevious:e>1}}function on(e,t,n=!1){const o=!0===e.error||200!==e.status,i=n&&!e.data;if(o||i)throw new Error(e.message||t)}const rn="/launchpad/upload-image",an="/launchpad/fetch-pool",sn="/launchpad/check-pool",cn="/launchpad/get-graph-data",ln="/holders",un="/launchpad/get-badge/",dn="/trade/",hn="/token/commment",gn="/token/commment",pn="/user/profile",mn="/user/profile",fn="/user/token-list",yn="/user/token-hold",wn="/user/transfer-faucets",kn={DEFAULT_PAGE:1,DEFAULT_LIMIT:10,BACKEND_MAX_PAGE_SIZE:20,SAFETY_MAX_PAGES:100};class vn{constructor(e,t=!1){this.http=e,this.logger=new S({debug:t,context:this.constructor.name})}}class Tn{constructor(e=!1){this.logger=new S({debug:e,context:this.constructor.name})}}function Sn(e,t){return"string"==typeof e[t]}function bn(e,t){return void 0===e[t]||"string"==typeof e[t]}function An(e,t){return void 0===e[t]||"number"==typeof e[t]}function En(e){return void 0===e.calculateAmountMode||"local"===e.calculateAmountMode||"external"===e.calculateAmountMode}function In(e){if(!e||"object"!=typeof e)return!1;const t=e;return Sn(t,"tokenName")&&An(t,"from")&&An(t,"to")&&An(t,"resolution")}class Pn extends vn{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 n=b(t),o=await this.http.get(an,n);if(!o)throw R("No response from pool service",500);on(o,"Failed to fetch pools",!0);const i=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 n=e.tokens;t=[{...n,createdAt:n.created_at||n.createdAt||""}]}else e.pools&&Array.isArray(e.pools)&&(t=e.pools.map(e=>({...e,createdAt:e.created_at||e.createdAt||""})));return t}(o.data),r=o.data.count??o.data.total??0;return{pools:i,total:r,totalPages:e.limit>0?Math.ceil(r/e.limit):1}}async fetchMultiplePages(e){const{startPage:t,totalPages:n,pageSize:o,...i}=e,r=[];if(n&&n>=t){const e=[];for(let o=t;o<=n;o++)e.push(o);for(let t=0;t<e.length;t+=5){const n=e.slice(t,t+5).map(e=>this.fetchSinglePage({...i,page:e,limit:o}).catch(e=>{if(400===e?.launchpadError?.statusCode)return{pools:[],total:0,totalPages:0};throw e}));(await Promise.all(n)).forEach(e=>{r.push(...e.pools)})}return r}let a=t,s=!0;for(;s;){const e=[];for(let t=0;t<5&&s;t++)e.push(a+t);const t=e.map(e=>this.fetchSinglePage({...i,page:e,limit:o}).catch(e=>{if(400===e?.launchpadError?.statusCode)return{pools:[],total:0,totalPages:0};throw e})),n=await Promise.all(t);for(const e of n){if(0===e.pools.length){s=!1;break}r.push(...e.pools)}a+=e.length,a>kn.SAFETY_MAX_PAGES&&(s=!1)}return r}async fetchPools(e={}){const t=e.page||kn.DEFAULT_PAGE,n=e.limit??kn.DEFAULT_LIMIT;let o;if(0!==n&&Vt({page:t,limit:n}),e.tokenName&&Ht(e.tokenName),"recent"===e.type?o="RECENT":"popular"===e.type&&(o="POPULAR"),n>0&&n<=20){const i=await this.fetchSinglePage({...e.search&&{search:e.search},...e.tokenName&&{tokenName:e.tokenName},...o&&{type:o},page:t,limit:n});return{pools:i.pools,page:t,limit:n,total:i.total,totalPages:i.totalPages,hasNext:t<i.totalPages,hasPrevious:t>1}}if(0===n){const t=kn.BACKEND_MAX_PAGE_SIZE,n=await this.fetchSinglePage({...e.search&&{search:e.search},...e.tokenName&&{tokenName:e.tokenName},...o&&{type:o},page:1,limit:t}),i=[...n.pools];if(n.pools.length>0){const r=await this.fetchMultiplePages({...e.search&&{search:e.search},...e.tokenName&&{tokenName:e.tokenName},...o&&{type:o},startPage:2,totalPages:n.totalPages>1?n.totalPages:null,pageSize:t});i.push(...r)}return{pools:i,page:1,limit:i.length,total:n.total||i.length,totalPages:1,hasNext:!1,hasPrevious:!1}}const i=kn.BACKEND_MAX_PAGE_SIZE,r=Math.ceil(n/i),a=await this.fetchSinglePage({...e.search&&{search:e.search},...e.tokenName&&{tokenName:e.tokenName},...o&&{type:o},page:t,limit:i}),s=[...a.pools],c=Math.min(r,a.totalPages-t+1);if(c>1){const n=t+c-1,r=await this.fetchMultiplePages({...e.search&&{search:e.search},...e.tokenName&&{tokenName:e.tokenName},...o&&{type:o},startPage:t+1,totalPages:n,pageSize:i});s.push(...r)}const l=s.slice(0,n);return{pools:l,page:t,limit:n,total:a.total,totalPages:a.totalPages,hasNext:t<a.totalPages&&l.length<a.total,hasPrevious:t>1}}async fetchAllPools(e){return this.fetchPools({...e,limit:0})}async checkPool(e){Qt(e),e.tokenName&&Ht(e.tokenName);const t=b(e),n=await this.http.get(sn,t);if(!n)throw R("No response from pool service",500);on(n,"Failed to check pool");const o=n.data;return e.symbol?o?.isSymbolExist??!1:e.tokenName?o?.isNameExist??!1:o?.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(!In(e))throw new P("Invalid options provided. Expected { tokenName: string, from?: number, to?: number, resolution?: number }","options","INVALID_OPTIONS");const{tokenName:t,from:n,to:o,resolution:i}=e;if(Ht(t),!n||!o||!i)throw new P("Graph options (from, to, resolution) are required","options","MISSING_GRAPH_OPTIONS");const r={tokenName:t,from:n,to:o,resolution:i};Zt(r);const a=b(r),s=await this.http.get(cn,a);if(!s)throw R("No response from pool service",500);return on(s,"Failed to fetch graph data",!0),{dataPoints:s.data}}async fetchTokenDistribution(e){if(!e)throw M("tokenName","Token name");Ht(e);const t=await this.resolveTokenNameToVault(e);if(!t)throw U(e);const n=encodeURIComponent(t),o=await this.http.get(`${ln}/${n}`);if(!o)throw R("No response from pool service",500);on(o,"Failed to fetch token distribution",!0);const i=o.data;if(!Array.isArray(i))throw R("Invalid API response: expected array of holders",o.status);for(const e of i){if(!e.owner||"string"!=typeof e.owner)throw R("Invalid holder data: missing or invalid owner field",o.status);if(!e.quantity||"string"!=typeof e.quantity)throw R("Invalid holder data: missing or invalid quantity field",o.status);const t=parseFloat(e.quantity);if(isNaN(t)||!isFinite(t))throw R(`Invalid holder quantity: "${e.quantity}"`,o.status)}const a=i.reduce((e,t)=>e.plus(t.quantity),new r(0));return{holders:i.map(e=>{const t=new r(e.quantity),n=a.isZero()?0:t.dividedBy(a).multipliedBy(100).toNumber();return{address:e.owner,balance:e.quantity,percentage:n}}),totalSupply:a.toFixed(),totalHolders:i.length,lastUpdated:new Date}}async fetchTokenBadges(e){if(!e)throw M("tokenName","Token name");Ht(e);const t=await this.http.get(un,{tokenName:e});if(!t)throw R("No response from pool service",500);return on(t,"Failed to fetch token badges",!0),{volumeBadges:t.data.volumeBadge||[],engagementBadges:t.data.engagementBadge||[]}}async hasTokenBadge(e){const{tokenName:t,badgeType:n,badgeName:o}=e;try{const e=await this.fetchTokenBadges(t);if(!e)return!1;const i=("volume"===n?e.volumeBadges:e.engagementBadges).find(e=>e.badgeName===o);return i?.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 Nn(e,t={}){const{stringifyFields:n=[],optionalFields:o=[],fieldMappings:i={}}=t,r={};for(const[t,a]of Object.entries(e)){const e=t;if(o.includes(e)&&void 0===a)continue;if(o.includes(e)&&"string"==typeof a&&0===a.trim().length)continue;const s=i[e],c=s?String(s):t;n.includes(e)?r[c]=String(a):r[c]=a}return b(r)}const Dn={PAGINATION:{MIN_PAGE:1,MAX_PAGE:1e3,MIN_LIMIT:1,MAX_LIMIT:20}};class Fn extends vn{constructor(e,t=!1){super(e,t)}async fetchTrades(e){if(!function(e){if(!e||"object"!=typeof e)return!1;const t=e;return Sn(t,"tokenName")&&(void 0===t.tradeType||"buy"===t.tradeType||"sell"===t.tradeType)&&bn(t,"userAddress")&&An(t,"page")&&An(t,"limit")}(e))throw new P("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:n,userAddress:o,page:i=kn.DEFAULT_PAGE,limit:r=kn.DEFAULT_LIMIT,startDate:a,endDate:s,sortOrder:c}=e;if(!j(t))throw new P("Token name is required and must be a non-empty string","tokenName","INVALID_TOKEN_NAME");z(i,r,Dn);const l=function(e,t,n){return Nn({tokenName:e,page:t,limit:n},{stringifyFields:["page","limit"]})}(t,i,r),u=await this.http.get(dn,l);if(!u)throw new P("No response from trade service","response","NO_RESPONSE");const d=(h=u.data)?Array.isArray(h)?h:h.trades:[];var h;const g=tn(u,{page:i,limit:r}),p=nn(g.page,g.totalPages);return{trades:d,...g,...p}}}const Cn=new S({debug:!1,context:"DateUtils"});function xn(e,t){if(!e)return t||new Date;if(e instanceof Date)return isNaN(e.getTime())?t||new Date:e;try{const n=new Date(e);return isNaN(n.getTime())?(Cn.warn(`Invalid date string received: "${e}". Using fallback.`),t||new Date):n}catch(n){return Cn.warn(`Date parsing error for "${e}":`,n),t||new Date}}const $n={PAGINATION:{MIN_PAGE:1,MAX_PAGE:1e3,MIN_LIMIT:1,MAX_LIMIT:50},CONTENT:{MIN_LENGTH:1,MAX_LENGTH:500}};class Ln extends vn{constructor(e,t,n=!1){super(e,n),this.poolService=t}async fetchComments(e){if(!function(e){if(!e||"object"!=typeof e)return!1;const t=e;return Sn(t,"tokenName")&&An(t,"page")&&An(t,"limit")}(e))throw new P("Invalid options provided. Expected { tokenName: string, page?: number, limit?: number }","options","INVALID_OPTIONS");const{tokenName:t,page:n=kn.DEFAULT_PAGE,limit:o=kn.DEFAULT_LIMIT}=e;if(!j(t))throw new P("Token name is required and must be a non-empty string","tokenName","INVALID_TOKEN_NAME");z(n,o,$n);const i=await this.poolService.resolveTokenNameToVault(t);if(!i)throw U(t);const r=Nn({vaultAddress:i,page:n,limit:o},{stringifyFields:["page","limit"]}),a=await this.http.get(hn,r);if(!a)throw new P("No response from comment service","response","NO_RESPONSE");if(on(a,"Failed to fetch comments"),!a.data)throw new P("Invalid response structure from comment service: no data field","response.data","INVALID_RESPONSE");if(0===Object.keys(a.data).length)return{comments:[],total:0};if(!Array.isArray(a.data.comments))throw new P("Invalid response structure from comment service: comments must be an array, got "+typeof a.data.comments,"response.data.comments","INVALID_RESPONSE");return{comments:a.data.comments.map(e=>({...e,createdAt:xn(e.createdAt)})),total:a.data.count||0}}async postComment(e){if(!function(e){if(!e||"object"!=typeof e)return!1;const t=e;return Sn(t,"tokenName")&&Sn(t,"content")}(e))throw new P("Invalid options provided. Expected { tokenName: string, content: string }","options","INVALID_OPTIONS");const{tokenName:t,content:n}=e;if(!j(t))throw new P("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>=$n.CONTENT.MIN_LENGTH&&t.length<=$n.CONTENT.MAX_LENGTH}(n))throw new P(`Comment content must be between ${$n.CONTENT.MIN_LENGTH} and ${$n.CONTENT.MAX_LENGTH} characters`,"content","INVALID_CONTENT");const o=await this.poolService.resolveTokenNameToVault(t);if(!o)throw U(t);const i=this.http.getAddress();if(!i)throw new P("User address not available - wallet not configured","userAddress","NO_WALLET");const r={userAddress:i,vaultAddress:o,content:n},a=await this.http.post(gn,r);if(!a)throw new P("No response from comment service","response","NO_RESPONSE");on(a,"Failed to create comment")}}function On(e){return e instanceof Error}function _n(e){return On(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 Bn(e){return"object"==typeof e&&null!==e&&"message"in e&&("response"in e||"request"in e||"config"in e)}function Un(e,t="image",n){const o=new FormData;if("undefined"!=typeof File&&e instanceof File)o.append(t,e);else{if(!Buffer.isBuffer(e))throw q("file","a File object (browser) or Buffer (Node.js)");{const i=new Blob([e],{type:"image/png"});o.append(t,i,n)}}return o}const Mn={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 qn(e){return!(!e||"string"!=typeof e)&&Mn.USER_ADDRESS.PATTERN.test(e)}const Rn="Update profile";class Kn extends vn{constructor(e,t=!1){super(e,t)}async fetchProfile(e){const t=e??this.http.getAddress();if(!t||!qn(t))throw new P("Address must be in format: eth|[40-hex-chars]","address","INVALID_ADDRESS");const n={userAddress:t},o=await this.http.get(pn,n);if(!o)throw new P("No response from user service","response","NO_RESPONSE");return o}async updateProfile(e){this.validateUpdateProfileData(e);let t=e.profileImage;if(!t||""===t.trim())try{const n=await this.fetchProfile(e.address);t=n.data?.profileImage||""}catch{t=""}const n={profileImage:t,fullName:e.fullName,userAddress:e.address},o=await this.http.signCustomMessage(Rn);if(!o)throw new P("Failed to generate signature - wallet not configured","signature","NO_SIGNATURE");const i={address:o.address,message:Rn,publickey:o.ethereumAddress,sign:o.signature},r=await this.http.put(mn,n,i);if(!r)throw new P("No response from user service","response","NO_RESPONSE");on(r,"Profile update failed")}async uploadProfileImage(e){this.validateUploadProfileImageOptions(e);const t=e.address??this.http.getAddress();if(!t)throw new P("Wallet address not available - wallet not configured","address","NO_WALLET");try{const n=`profile-image-${t}.png`,o=Un(e.file,"image",n),i=await this.http.request({method:"POST",url:`${rn}?tokenName=${encodeURIComponent(t)}`,data:o,headers:{}});if(!i)throw new P("No response from user service","response","NO_RESPONSE");return on(i,"Image upload failed"),"string"==typeof i.data?i.data:""}catch(e){if(e instanceof P)throw e;throw new P(`Profile image upload failed: ${_n(e)}`,"file","UPLOAD_FAILED")}}async fetchTokenList(e){return this.buildFetchRequest(fn,e,{includeType:!0,errorMessage:"Failed to fetch token list"})}async fetchTokensHeld(e){return this.buildFetchRequest(yn,e,{includeType:!1,errorMessage:"Failed to fetch tokens held"})}async fetchTokensCreated(e={}){const{page:t=kn.DEFAULT_PAGE,limit:n=kn.DEFAULT_LIMIT,search:o,tokenName:i}=e,r=this.http.getAddress();if(!r)throw new P("Wallet address not available - wallet not configured","address","NO_WALLET");const a={type:"DEFI",address:r,page:t,limit:n};return void 0!==o&&(a.search=o),void 0!==i&&(a.tokenName=i),this.fetchTokenList(a)}async buildFetchRequest(e,t,n){this.validateGetTokenListOptions(t);const o={page:t.page,limit:t.limit,address:t.address,search:t.search,tokenName:t.tokenName},i=Nn(n.includeType?{...o,type:"all"!==t.type&&t.type?t.type:"DEFI"}:o,{stringifyFields:["page","limit"],optionalFields:["address","search","tokenName"]}),r=await this.http.get(e,i);if(!r)throw new P("No response from user service","response","NO_RESPONSE");on(r,n.errorMessage,!0);const a=(s=r.data)?Array.isArray(s)?s:s.token:[];var s;const c=tn(r,{page:t.page||kn.DEFAULT_PAGE,limit:t.limit||kn.DEFAULT_LIMIT}),l=nn(c.page,c.totalPages);return{tokens:a,...c,...l}}validateGetTokenListOptions(e){if(z(e.page,e.limit,Mn),void 0!==e.address&&!qn(e.address))throw new P("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>=Mn.SEARCH.MIN_LENGTH&&t.length<=Mn.SEARCH.MAX_LENGTH))throw new P(`Search query must be between ${Mn.SEARCH.MIN_LENGTH} and ${Mn.SEARCH.MAX_LENGTH} characters`,"search","INVALID_SEARCH");var t,n;if(void 0!==e.tokenName&&e.tokenName.trim().length>0&&!((n=e.tokenName)&&"string"==typeof n&&n.length>=Mn.TOKEN_NAME.MIN_LENGTH&&n.length<=Mn.TOKEN_NAME.MAX_LENGTH))throw new P(`Token name must be between ${Mn.TOKEN_NAME.MIN_LENGTH} and ${Mn.TOKEN_NAME.MAX_LENGTH} characters`,"tokenName","INVALID_TOKEN_NAME")}validateUpdateProfileData(e){if(!qn(e.address))throw new P("Address must be in format: eth|[40-hex-chars]","address","INVALID_ADDRESS");if(!((t=e.fullName)&&"string"==typeof t&&t.length>=Mn.PROFILE.FULL_NAME.MIN_LENGTH&&t.length<=Mn.PROFILE.FULL_NAME.MAX_LENGTH&&Mn.PROFILE.FULL_NAME.ALPHABETS_ONLY_PATTERN.test(t)))throw new P(`Full name must be between ${Mn.PROFILE.FULL_NAME.MIN_LENGTH} and ${Mn.PROFILE.FULL_NAME.MAX_LENGTH} characters`,"fullName","INVALID_FULL_NAME");var t}validateUploadProfileImageOptions(e){if(e.address&&!qn(e.address))throw new P("Address must be in format: eth|[40-hex-chars]","address","INVALID_ADDRESS")}}class Gn extends Error{constructor(e,t,n){super(e),this.filename=t,this.mimeType=n,this.name="FileValidationError"}}function zn(e,t,n){if(!e)throw new Gn("File is required",t,n);if("undefined"!=typeof File&&e instanceof File){const t=Ue.safeParse(e);if(!t.success){const n=t.error.errors.map(e=>e.message).join("; ");throw new Gn(n,e.name,e.type)}return}if(Buffer.isBuffer(e)){if(!t)throw new Gn("Filename is required when uploading Buffer objects",t,n);const o=Me.safeParse(e);if(!o.success){const e=o.error.errors.map(e=>e.message).join("; ");throw new Gn(e,t,n)}if(t.length>255)throw new Gn(`Filename length ${t.length} exceeds maximum allowed length of 255 characters`,t,n);const i=["image/png","image/jpg","image/jpeg","image/gif","image/webp","image/svg+xml"];if(!i.includes(n))throw new Gn(`Invalid file type "${n}" is not allowed. Allowed types: ${i.join(", ")}`,t,n);const r=function(e){if(!e)return"";const t=e.lastIndexOf(".");if(-1===t)return"";return e.substring(t).toLowerCase()}(t),a=[".png",".jpg",".jpeg",".gif",".webp",".svg"];if(!a.includes(r))throw new Gn(`File extension "${r}" is not allowed. Allowed extensions: ${a.join(", ")}`,t,n);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!==n&&"application/octet-stream"!==s)throw new Gn(`File extension "${r}" does not match MIME type "${n}"`,t,n);return}throw new Gn("File must be a File object (browser) or Buffer (Node.js)",t,n)}class Wn extends vn{constructor(e,t=!1){super(e,t)}async uploadImageByTokenName(e){const{tokenName:t,options:n}=e;Ht(t);const o=`${t}.png`;zn(n.file,o,"image/png");try{const e=`${n.tokenName??t}.png`,o=Un(n.file,"image",e),i=await this.http.request({method:"POST",url:`${rn}?tokenName=${encodeURIComponent(n.tokenName??t)}`,data:o,headers:{}});if(!i)throw K("No response from image upload service","response");return on(i,"Image upload failed"),"string"==typeof i.data?i.data:""}catch(e){if(e instanceof Error&&e.message.includes("FormData"))throw K("File upload failed: FormData not supported in this environment. Ensure you have proper polyfills for Node.js environments.","FormData");throw e}}}class jn extends vn{constructor(e,t=!1){super(e,t)}async transferFaucets(e){this.validateTransferFaucetsData(e);const t={userAddress:e.walletAddress,amount:e.amount},n=await this.http.post(wn,t);if(!n)throw new P("No response from faucet service","response","NO_RESPONSE");on(n,"Faucet transfer failed")}validateTransferFaucetsData(e){if(!qn(e.walletAddress))throw new P("Address must be in format: eth|[40-hex-chars]","address","INVALID_ADDRESS");if(!(t=e.amount)||"string"!=typeof t||!Mn.FAUCET_AMOUNT.POSITIVE_NON_ZERO_DECIMAL.test(t))throw new P("Amount must be a positive decimal string greater than zero","amount","INVALID_AMOUNT");var t}}class Hn{constructor(e,t=!1){this.http=e,this.poolService=new Pn(e,t),this.tradeService=new Fn(e,t),this.commentService=new Ln(e,this.poolService,t),this.userService=new Kn(e,t),this.imageService=new Wn(e,t),this.faucetService=new jn(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 Ht(e)}}const Vn={MAX_UNIQUE_KEY_LENGTH:64,UNIQUE_KEY_PATTERN:/^(galaswap-operation-|galaconnect-operation-)/,TOKEN_NAME_PATTERN:/^[a-zA-Z0-9]+$/};var Qn;!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"}(Qn||(Qn={}));class Xn extends Error{constructor(e,t,n){super(e),this.type=t,this.details=n,this.name="TransferError"}}function Zn(e,t="0"){return new r(null==e||""===e?t:e)}function Yn(e){return a.tickToSqrtPrice(e)}function Jn(e){const t=Zn(e),n=Math.log(1.0001),o=t.toNumber();return Math.log(o)/n}function eo(e,t=!1){const n=Zn(e),o=new r(1).dividedBy(n);return t?o.toFixed():o}function to(...e){e.forEach((e,t)=>{if(e.isNaN())throw new Error(`Value at index ${t} must be a valid number, got: NaN`);if(!e.isFinite())throw new Error(`Value at index ${t} must be finite, got: ${e.toString()}`);if(e.isLessThan(0))throw new Error(`Value at index ${t} must be non-negative, got: ${e.toString()}`)})}function no(e,t,n){if(e.isNaN())throw new Error(`${t} must be a valid number, got: NaN`);if(!e.isFinite()){const o=n?`${t} must be finite ${n}, got: ${e.toString()}`:`${t} must be finite, got: ${e.toString()}`;throw new Error(o)}if(e.isLessThanOrEqualTo(0)){const o=n?`${t} must be positive ${n}, got: ${e.toString()}`:`${t} must be positive, got: ${e.toString()}`;throw new Error(o)}}class oo{static validateAmount(e){const t=new r(e);try{no(t,"amount","for transfer")}catch(t){throw new Xn(t.message,Qn.INVALID_AMOUNT,{amount:e})}}static validateUniqueKey(e){if(e){if(e.length>Vn.MAX_UNIQUE_KEY_LENGTH)throw new P(`Unique key too long. Maximum length: ${Vn.MAX_UNIQUE_KEY_LENGTH}`);if(!Vn.UNIQUE_KEY_PATTERN.test(e))throw new Xn('Invalid unique key format. Must start with "galaswap-operation-" or "galaconnect-operation-"',Qn.INVALID_AMOUNT,{uniqueKey:e})}}}class io{toLaunchpadFormat(e){if(!e)return"unknown|Unit|none|none";if("string"==typeof e)return e.includes("|")?e:`${e}|Unit|none|none`;return`${e.collection||e.symbol||"unknown"}|${e.category||"Unit"}|${e.type||"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}`);return{collection:t[0]||"Token",category:t[1]||"Unit",type:t[2]||"none",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 ro(e){if(!e||"string"!=typeof e)throw new Error("Invalid token format: token must be a non-empty string");return e.replace(/\|/g,"$")}function ao(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[n,o,i,...r]=t;if(!n||!o||!i)throw new Error("Collection, category, and type must be non-empty");const a=r.join("$");if(!a)throw new Error("AdditionalKey must be non-empty");return{collection:n,category:o,type:i,additionalKey:a}}catch(t){throw new P(`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 so 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,n,o,i){let r;if("string"==typeof o){r={...ao(o),instance:"0"}}else r={collection:o.collection,category:o.category,type:o.type,additionalKey:o.additionalKey,instance:"0"};return new so({from:e,to:t,quantity:n,tokenInstance:r,uniqueKey:i||`galaconnect-operation-${Date.now()}_${Math.random().toString(36).substring(2,8)}`})}static forGALA(e,t,n,o){return new so({from:e,to:t,quantity:n,tokenInstance:{collection:"GALA",category:"Unit",type:"none",additionalKey:"none",instance:"0"},uniqueKey:o||`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 co extends o.ChainCallDTO{constructor(e){super(),this.lockAuthority=e.lockAuthority,this.tokenInstances=e.tokenInstances,this.uniqueKey=e.uniqueKey,void 0!==e.expires&&(this.expires=e.expires),void 0!==e.name&&(this.name=e.name),e.signedPayload&&(this.signature=e.signedPayload.signature,this.domain=e.signedPayload.domain,this.types=e.signedPayload.types,e.signedPayload.prefix&&(this.prefix=e.signedPayload.prefix))}static fromTokenClassKey(e,t,n,o,i){let r;if("string"==typeof o){r={...ao(o),instance:"0"}}else r={collection:o.collection,category:o.category,type:o.type,additionalKey:o.additionalKey,instance:"0"};return new co({lockAuthority:t||e,tokenInstances:[{owner:e,quantity:n,tokenInstanceKey:r}],...void 0!==i?.expires&&{expires:i.expires},...void 0!==i?.name&&{name:i.name},uniqueKey:i?.uniqueKey||`galaconnect-operation-${Date.now()}_${Math.random().toString(36).substring(2,8)}`})}static forGALA(e,t,n,o){return new co({lockAuthority:t||e,tokenInstances:[{owner:e,quantity:n,tokenInstanceKey:{collection:"GALA",category:"Unit",type:"none",additionalKey:"none",instance:"0"}}],...void 0!==o?.expires&&{expires:o.expires},...void 0!==o?.name&&{name:o.name},uniqueKey:o?.uniqueKey||`galaconnect-operation-${Date.now()}_${Math.random().toString(36).substring(2,8)}`})}getTokenClassKey(){if(0===this.tokenInstances.length)return;const e=this.tokenInstances[0].tokenInstanceKey;return`${e.collection}$${e.category}$${e.type}$${e.additionalKey}`}toSigningPayload(){return{lockAuthority:this.lockAuthority,tokenInstances:this.tokenInstances,...void 0!==this.expires&&{expires:this.expires},...void 0!==this.name&&{name:this.name},uniqueKey:this.uniqueKey}}}class lo extends o.ChainCallDTO{constructor(e){super(),this.tokenInstances=e.tokenInstances,this.uniqueKey=e.uniqueKey,void 0!==e.name&&(this.name=e.name),e.signedPayload&&(this.signature=e.signedPayload.signature,this.domain=e.signedPayload.domain,this.types=e.signedPayload.types,e.signedPayload.prefix&&(this.prefix=e.signedPayload.prefix))}static fromTokenClassKey(e,t,n,o){let i;if("string"==typeof n){i={...ao(n),instance:"0"}}else i={collection:n.collection,category:n.category,type:n.type,additionalKey:n.additionalKey,instance:"0"};return new lo({tokenInstances:[{owner:e,quantity:t,tokenInstanceKey:i}],...void 0!==o?.name&&{name:o.name},uniqueKey:o?.uniqueKey||`galaconnect-operation-${Date.now()}_${Math.random().toString(36).substring(2,8)}`})}static forGALA(e,t,n){return new lo({tokenInstances:[{owner:e,quantity:t,tokenInstanceKey:{collection:"GALA",category:"Unit",type:"none",additionalKey:"none",instance:"0"}}],...void 0!==n?.name&&{name:n.name},uniqueKey:n?.uniqueKey||`galaconnect-operation-${Date.now()}_${Math.random().toString(36).substring(2,8)}`})}getTokenClassKey(){if(0===this.tokenInstances.length)return;const e=this.tokenInstances[0].tokenInstanceKey;return`${e.collection}$${e.category}$${e.type}$${e.additionalKey}`}toSigningPayload(){return{tokenInstances:this.tokenInstances,...void 0!==this.name&&{name:this.name},uniqueKey:this.uniqueKey}}}class uo{constructor(e){this.wallet=e}static generateUniqueKey(){return`${Date.now()}_${Math.random().toString(36).substring(2,8)}`}async signTransferToken(e){const t={name:"GalaChain",chainId:1},n={TransferToken:[{name:"from",type:"string"},{name:"to",type:"string"},{name:"quantity",type:"string"},{name:"tokenInstance",type:"TokenInstance"},{name:"uniqueKey",type:"string"}],TokenInstance:[{name:"collection",type:"string"},{name:"category",type:"string"},{name:"type",type:"string"},{name:"additionalKey",type:"string"},{name:"instance",type:"string"}]};return{signature:await this.wallet.signTypedData(t,n,e),domain:t,types:n,signerPublicKey:this.wallet.signingKey.publicKey}}async signLockToken(e){const t={name:"GalaChain",chainId:1},n={LockToken:[{name:"lockAuthority",type:"string"},{name:"tokenInstances",type:"TokenInstanceQuantity[]"},{name:"uniqueKey",type:"string"},...void 0!==e.expires?[{name:"expires",type:"uint256"}]:[],...void 0!==e.name?[{name:"name",type:"string"}]:[]],TokenInstanceQuantity:[{name:"owner",type:"string"},{name:"quantity",type:"string"},{name:"tokenInstanceKey",type:"TokenInstanceKey"}],TokenInstanceKey:[{name:"collection",type:"string"},{name:"category",type:"string"},{name:"type",type:"string"},{name:"additionalKey",type:"string"},{name:"instance",type:"string"}]},o={lockAuthority:e.lockAuthority,tokenInstances:e.tokenInstances,uniqueKey:e.uniqueKey};void 0!==e.expires&&(o.expires=e.expires),void 0!==e.name&&(o.name=e.name);return{signature:await this.wallet.signTypedData(t,n,o),domain:t,types:n,signerPublicKey:this.wallet.signingKey.publicKey}}async signUnlockToken(e){const t={name:"GalaChain",chainId:1},n={UnlockToken:[{name:"tokenInstances",type:"TokenInstanceQuantity[]"},{name:"uniqueKey",type:"string"},...void 0!==e.name?[{name:"name",type:"string"}]:[]],TokenInstanceQuantity:[{name:"owner",type:"string"},{name:"quantity",type:"string"},{name:"tokenInstanceKey",type:"TokenInstanceKey"}],TokenInstanceKey:[{name:"collection",type:"string"},{name:"category",type:"string"},{name:"type",type:"string"},{name:"additionalKey",type:"string"},{name:"instance",type:"string"}]},o={tokenInstances:e.tokenInstances,uniqueKey:e.uniqueKey};void 0!==e.name&&(o.name=e.name);return{signature:await this.wallet.signTypedData(t,n,o),domain:t,types:n,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{...ao(e),instance:"0"}}}function ho(e){if("string"==typeof e){const t=e.split("|");if(t.length<4)throw new P(`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 P(`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 P("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 P(`Invalid tokenId type: ${typeof e}. Expected string, TokenClassKey, or TokenInstanceKey`,"tokenId","INVALID_TOKEN_ID_TYPE")}function go(e){try{const[t,n]=e.split("|");if(!n)throw new Error("Missing token part after service");const o=n.split("$");if(o.length<4)throw new Error(`Invalid vault address format. Expected at least 4 parts separated by $, got ${o.length}`);if(!(o[0]&&o[1]&&o[2]&&o[3]))throw new Error("All vault address components (collection, category, type, additionalKey) must be non-empty");const i=o.slice(3,-1),r=i.length>0?i.join("$"):o[3];return{collection:o[0],category:o[1],type:o[2],additionalKey:r}}catch(t){throw new P(`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 po(e){return{...go(e),instance:"0"}}function mo(e){return go(e).type}var fo,yo=Object.freeze({__proto__:null,extractTokenSymbolFromVault:mo,normalizeToTokenInstanceKey:ho,parseVaultAddressToTokenClassKey:go,parseVaultAddressToTokenInstance:po});function wo(e){if(!e||"object"!=typeof e)return!1;const t=e;return!(!("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.lockAuthority&&"string"!=typeof t.lockAuthority||void 0!==t.expires&&"number"!=typeof t.expires||void 0!==t.name&&"string"!=typeof t.name||void 0!==t.uniqueKey&&"string"!=typeof t.uniqueKey)}function ko(e){if(!e||"object"!=typeof e)return!1;const t=e;return"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.name||"string"==typeof t.name)&&(void 0===t.uniqueKey||"string"==typeof t.uniqueKey)}e.LockErrorType=void 0,(fo=e.LockErrorType||(e.LockErrorType={})).TOKEN_NOT_FOUND="TOKEN_NOT_FOUND",fo.INVALID_AMOUNT="INVALID_AMOUNT",fo.INSUFFICIENT_BALANCE="INSUFFICIENT_BALANCE",fo.SIGNATURE_FAILED="SIGNATURE_FAILED",fo.NETWORK_ERROR="NETWORK_ERROR",fo.WALLET_REQUIRED="WALLET_REQUIRED",fo.LOCK_NOT_FOUND="LOCK_NOT_FOUND",fo.LOCK_EXPIRED="LOCK_EXPIRED",fo.INSUFFICIENT_LOCKED_BALANCE="INSUFFICIENT_LOCKED_BALANCE",fo.NOT_LOCK_AUTHORITY="NOT_LOCK_AUTHORITY",fo.LOCK_NAME_MISMATCH="LOCK_NAME_MISMATCH";class vo extends Error{constructor(e,t,n){super(e),this.type=t,this.details=n,this.name="LockError"}}const To="gala-transfer-successful",So="token-transfer-successful",bo="transfer-successful-no-id";class Ao extends vn{constructor(e,t,n,o=!1,i){super(e,o),this.wallet=t,this.tokenResolver=n,this.publicAxios=i,this.signatureHelper=t?new uo(t):void 0}async fetchPoolDetails(e){this.validateFetchPoolDetailsData(e);const t=await this.http.post("/api/asset/launchpad-contract/FetchSaleDetails",e);if(!t)throw R("No response from GalaChain service",500);if(1!==t.Status)throw R(`Failed to fetch pool details: Status ${t.Status}`,t.Status);const n=t.Data.reverseBondingCurveConfiguration,o=n?.minFeePortion??"0",i=n?.maxFeePortion??"0",r=(await import("bignumber.js")).default,a=!new r(o).isZero()||!new r(i).isZero(),s=t.Data;return s.reverseBondingCurveMinFeePortion=o,s.reverseBondingCurveMaxFeePortion=i,s.hasReverseBondingCurveFee=a,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 R("No response from GalaChain service",500);if(1!==e.Status)throw R(`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 P("Invalid fetch pool details data: missing required fields","data","INVALID_TYPE");var t;if(!e.vaultAddress||"string"!=typeof e.vaultAddress)throw new P("Vault address is required and must be a string","vaultAddress","INVALID_VAULT_ADDRESS");if(!e.vaultAddress.startsWith("service|Token$Unit$"))throw new P("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 n=t.Data.find(t=>t.collection===e.collection&&t.category===e.category&&t.additionalKey===e.additionalKey&&t.type===e.type);if(!n||"0"===n.quantity)return null;const o=`${n.collection}|${n.category}|${n.additionalKey}|${n.type}`;return{quantity:n.quantity,collection:n.collection,category:n.category,tokenId:o}}catch(e){throw R(`Failed to fetch token balance from GalaChain: ${_n(e)}`,void 0,On(e)?e:void 0)}}async fetchTokenClassFromChain(e){try{const t="string"==typeof e?ho(e):e,n={tokenClasses:[{collection:t.collection,category:t.category,type:t.type,additionalKey:t.additionalKey}]},o=(await this.publicAxios.post("/api/asset/token-contract/FetchTokenClasses",n)).data;if(1!==o.Status)throw R(`Failed to fetch token class from GalaChain: Status ${o.Status}${o.Message?` - ${o.Message}`:""}`,o.Status);if(!o.Data||0===o.Data.length)throw R(`Token not found on GalaChain: ${t.collection}|${t.category}|${t.type}|${t.additionalKey}`,404);return o.Data[0]}catch(e){if(Bn(e)&&404===e.response?.status)throw R("Token not found on GalaChain",404,On(e)?e:void 0);const t=_n(e);if(t.includes("Token not found")||t.includes("Status"))throw e;throw R(`Failed to fetch token class from GalaChain: ${t}`,void 0,On(e)?e:void 0)}}async fetchTokenClassesWithSupply(e){try{if(!e||0===e.length)throw new P("tokenClasses array must not be empty");const t={tokenClasses:e},n=(await this.publicAxios.post("/api/asset/token-contract/FetchTokenClassesWithSupply",t)).data;if(1!==n.Status)throw R(`Failed to fetch token classes with supply from GalaChain: Status ${n.Status}${n.Message?` - ${n.Message}`:""}`,n.Status);if(!n.Data||0===n.Data.length)throw R("No token supply data found for requested token classes",404);return n.Data}catch(e){if(Bn(e)&&404===e.response?.status)throw R("Token supply data not found on GalaChain",404,On(e)?e:void 0);const t=_n(e);if(t.includes("Token")||t.includes("Status")||t.includes("tokenClasses"))throw e;throw R(`Failed to fetch token classes with supply from GalaChain: ${t}`,void 0,On(e)?e:void 0)}}async transferGala(e){if(this.validateTransferGalaData(e),!this.wallet||!this.signatureHelper)throw new Xn("Wallet required for GALA transfer operations",Qn.WALLET_REQUIRED);try{const t=V(e.recipientAddress),n=V(this.wallet.address),o=so.forGALA(n,t,e.amount,e.uniqueKey),i=await this.signatureHelper.signTransferToken(o.toSigningPayload()),r=new so({...o.toSigningPayload(),signedPayload:i});this.logger.debug("[DEBUG] Full GALA Transfer Request Payload:",JSON.stringify(r,null,2));const a=await this.http.post("/api/asset/token-contract/TransferToken",r);if(!a)throw new Xn("No response from GalaChain transfer service",Qn.NETWORK_ERROR);return this.logger.debug("[DEBUG] Transfer response:",JSON.stringify(a,null,2)),this.extractTransactionIdFromResponse(a,"gala")}catch(t){throw this.handleTransferError(t,"GALA transfer failed",e)}}async transferToken(e){if(this.validateTransferTokenData(e),!this.wallet||!this.signatureHelper)throw new Xn("Wallet required for token transfer operations",Qn.WALLET_REQUIRED);try{const t=V(e.to),n=V(this.wallet.address);let o;if(e.tokenId)o=ho(e.tokenId),this.logger.debug("[DEBUG] Using provided tokenId:",e.tokenId),this.logger.debug("[DEBUG] Normalized Token Instance:",JSON.stringify(o,null,2));else{if(!e.tokenName)throw new Xn("Must provide either tokenId or tokenName for token identification",Qn.TOKEN_NOT_FOUND);o=await this.resolveTokenInstance(e.tokenName)}const i=new so({from:n,to:t,quantity:e.amount,tokenInstance:o,uniqueKey:e.uniqueKey||`galaconnect-operation-${Date.now()}_${Math.random().toString(36).substring(2,8)}`}),r=await this.signatureHelper.signTransferToken(i.toSigningPayload()),a=new so({...i.toSigningPayload(),signedPayload:r});this.logger.debug("[DEBUG] Full Transfer Request Payload:",JSON.stringify(a,null,2));const s=await this.http.post("/api/asset/token-contract/TransferToken",a);if(!s)throw new Xn("No response from GalaChain transfer service",Qn.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 Xn)throw t;throw new Xn(`Failed to resolve token class key for '${e}': ${_n(t)}`,Qn.TOKEN_NOT_FOUND,{tokenName:e})}}async lockToken(e){if(this.validateLockTokenData(e),!this.wallet||!this.signatureHelper)throw new Xn("Wallet required for token lock operations",Qn.WALLET_REQUIRED);try{const t=V(this.wallet.address),n=e.lockAuthority?V(e.lockAuthority):t;let o;if(e.tokenId)o=ho(e.tokenId),this.logger.debug("[DEBUG] Using provided tokenId:",e.tokenId);else{if(!e.tokenName)throw new Xn("Must provide either tokenId or tokenName for token identification",Qn.TOKEN_NOT_FOUND);o=await this.resolveTokenInstance(e.tokenName)}const i=new co({lockAuthority:n,tokenInstances:[{owner:t,quantity:e.amount,tokenInstanceKey:o}],...void 0!==e.expires&&{expires:e.expires},...void 0!==e.name&&{name:e.name},uniqueKey:e.uniqueKey||`galaconnect-operation-${Date.now()}_${Math.random().toString(36).substring(2,8)}`}),r=await this.signatureHelper.signLockToken(i.toSigningPayload()),a=new co({...i.toSigningPayload(),signedPayload:r});this.logger.debug("[DEBUG] Full Lock Request Payload:",JSON.stringify(a,null,2));const s=await this.http.post("/api/asset/token-contract/LockToken",a);if(!s)throw new Xn("No response from GalaChain lock service",Qn.NETWORK_ERROR);return this.logger.debug("[DEBUG] Token lock response:",JSON.stringify(s,null,2)),this.extractTransactionIdFromResponse(s,"token")}catch(t){throw this.handleTransferError(t,"Token lock failed",e)}}async unlockToken(e){if(this.validateUnlockTokenData(e),!this.wallet||!this.signatureHelper)throw new Xn("Wallet required for token unlock operations",Qn.WALLET_REQUIRED);try{const t=V(this.wallet.address);let n;if(e.tokenId)n=ho(e.tokenId),this.logger.debug("[DEBUG] Using provided tokenId:",e.tokenId);else{if(!e.tokenName)throw new Xn("Must provide either tokenId or tokenName for token identification",Qn.TOKEN_NOT_FOUND);n=await this.resolveTokenInstance(e.tokenName)}const o=new lo({tokenInstances:[{owner:t,quantity:e.amount,tokenInstanceKey:n}],...void 0!==e.name&&{name:e.name},uniqueKey:e.uniqueKey||`galaconnect-operation-${Date.now()}_${Math.random().toString(36).substring(2,8)}`}),i=await this.signatureHelper.signUnlockToken(o.toSigningPayload()),r=new lo({...o.toSigningPayload(),signedPayload:i});this.logger.debug("[DEBUG] Full Unlock Request Payload:",JSON.stringify(r,null,2));const a=await this.http.post("/api/asset/token-contract/UnlockToken",r);if(!a)throw new Xn("No response from GalaChain unlock service",Qn.NETWORK_ERROR);return this.logger.debug("[DEBUG] Token unlock response:",JSON.stringify(a,null,2)),this.extractTransactionIdFromResponse(a,"token")}catch(t){throw this.handleTransferError(t,"Token unlock failed",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 P("Invalid GALA transfer data: missing required fields");if(!H(e.recipientAddress))throw new Xn("Invalid recipient address format",Qn.INVALID_RECIPIENT,{recipientAddress:e.recipientAddress});oo.validateAmount(e.amount),oo.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 P("Invalid token transfer data: missing required fields");if(!H(e.to))throw new Xn("Invalid recipient address format",Qn.INVALID_RECIPIENT,{recipientAddress:e.to});if(!e.tokenId&&!e.tokenName)throw new Xn("Must provide either tokenId or tokenName for token identification",Qn.TOKEN_NOT_FOUND);if(e.tokenName&&!Vn.TOKEN_NAME_PATTERN.test(e.tokenName))throw new Xn("Invalid token name format",Qn.TOKEN_NOT_FOUND,{tokenName:e.tokenName});oo.validateAmount(e.amount),oo.validateUniqueKey(e.uniqueKey)}validateLockTokenData(e){if(!wo(e))throw new P("Invalid lock token data: missing required fields");if(e.lockAuthority&&!H(e.lockAuthority))throw new Xn("Invalid lock authority address format",Qn.INVALID_RECIPIENT,{recipientAddress:e.lockAuthority});if(!e.tokenId&&!e.tokenName)throw new Xn("Must provide either tokenId or tokenName for token identification",Qn.TOKEN_NOT_FOUND);if(e.tokenName&&!Vn.TOKEN_NAME_PATTERN.test(e.tokenName))throw new Xn("Invalid token name format",Qn.TOKEN_NOT_FOUND,{tokenName:e.tokenName});if(void 0!==e.expires&&(e.expires<=0||!Number.isInteger(e.expires)))throw new P("Expires must be a positive integer (epoch milliseconds)");oo.validateAmount(e.amount),oo.validateUniqueKey(e.uniqueKey)}validateUnlockTokenData(e){if(!ko(e))throw new P("Invalid unlock token data: missing required fields");if(!e.tokenId&&!e.tokenName)throw new Xn("Must provide either tokenId or tokenName for token identification",Qn.TOKEN_NOT_FOUND);if(e.tokenName&&!Vn.TOKEN_NAME_PATTERN.test(e.tokenName))throw new Xn("Invalid token name format",Qn.TOKEN_NOT_FOUND,{tokenName:e.tokenName});oo.validateAmount(e.amount),oo.validateUniqueKey(e.uniqueKey)}async resolveTokenInstance(e){try{const t=await this.tokenResolver.resolveTokenToVault(e);if(!t)throw new Xn(`Token '${e}' not found or not available for transfer`,Qn.TOKEN_NOT_FOUND,{tokenName:e});const n=this.resolveTokenInstanceFromVaultAddress(t);return this.logger.debug(`[DEBUG] Token resolution for '${e}':\n Vault Address: ${t}\n Token Instance: ${JSON.stringify(n,null,2)}`),n}catch(t){if(t instanceof Xn)throw t;throw new Xn(`Failed to resolve token '${e}': ${_n(t)}`,Qn.TOKEN_NOT_FOUND,{tokenName:e})}}resolveTokenInstanceFromVaultAddress(e){try{return po(e)}catch(e){if(e instanceof P)throw new Xn(`Invalid vault address format: ${e.message}`,Qn.TOKEN_NOT_FOUND);throw new Xn(`Failed to parse vault address: ${e instanceof Error?e.message:String(e)}`,Qn.TOKEN_NOT_FOUND)}}extractTransactionIdFromResponse(e,t){if(e&&"object"==typeof e){if("Status"in e&&1===e.Status&&"Data"in e){const n=e;return Array.isArray(n.Data)&&n.Data.length>0?"gala"===t?To:So:bo}if("transactionId"in e&&"string"==typeof e.transactionId&&e.transactionId)return e.transactionId}throw new Xn("Transfer succeeded but transaction ID could not be extracted",Qn.NETWORK_ERROR)}handleTransferError(e,t,n){if(e instanceof Xn)return e;if(e instanceof P)return new Xn(e.message,Qn.INVALID_AMOUNT);if(Bn(e)&&e.response){const t=e.response.status,o=e.response.data;if(400===t)return new Xn(("string"==typeof o?.message?o.message:void 0)||"Invalid transfer request",Qn.INVALID_AMOUNT);if(403===t)return new Xn("Insufficient balance for transfer",Qn.INSUFFICIENT_BALANCE);if(404===t){const e={};return"tokenName"in n&&(e.tokenName=n.tokenName),new Xn("Token not found",Qn.TOKEN_NOT_FOUND,e)}}if("object"==typeof e&&null!==e&&"code"in e&&("ECONNABORTED"===e.code||"ETIMEDOUT"===e.code))return new Xn("Transfer request timed out",Qn.NETWORK_ERROR);const o=_n(e);return new Xn(o||t,Qn.NETWORK_ERROR)}}class Eo{constructor(e,t,n,o=!1){this.dexBackendHttp=e,this.cache=t,this.galaChainService=n,this.logger=new S({debug:o,context:"DexService"})}async fetchTokenPrice(e){const{tokenName:t,tokenId:n}=e;if(!t&&!n)throw M("tokenName or tokenId","Either tokenName (for launchpad tokens) or tokenId (for DEX tokens) is required");if(t&&n)throw new P("tokenName and tokenId are mutually exclusive - provide only one","params","INVALID_PARAMS");if(n)return this.logger.debug(`Fetching spot price by tokenId: ${n}`),this._fetchDexTokenSpotPrice(n);throw new P("tokenName parameter requires LaunchpadSDK routing - call LaunchpadSDK.fetchTokenPrice({tokenName}) instead","tokenName","INVALID_PARAMS")}async _fetchDexTokenSpotPrice(e){if(!e)throw M("tokenId","Token ID");try{const t=ho(e),n=ro(`${t.collection}|${t.category}|${t.type}|${t.additionalKey}`);if(this.logger.debug(`Fetching DEX spot price for token: ${n}`),!this.dexBackendHttp)throw R("DEX Backend API client not configured");const o=await this.dexBackendHttp.request({method:"GET",url:"/v1/trade/price",params:{token:n}});if(!o.data||"string"!=typeof o.data)throw new P("Invalid price response: data must be a string, got "+typeof o.data,"data","INVALID_RESPONSE");const i=parseFloat(o.data);if(isNaN(i))throw new P(`Invalid price value: could not parse "${o.data}" as number`,"data","INVALID_CALCULATION");const r=`${t.collection}|${t.category}|${t.type}|${t.additionalKey}`;let a;try{if(this.cache){const e=this.cache.getByTokenId(r);if(e?.symbol)return a=e.symbol,this.logger.debug(`DEX spot price for ${a} (cached): $${i}`),{symbol:a,price:i}}this.logger.debug(`Symbol cache miss for ${r}, fetching from API`);a=(await this.fetchTokenDetails(e)).symbol,this.cache&&(this.cache.setByTokenId(r,{symbol:a}),this.logger.debug(`Cached symbol for ${r}: ${a}`)),this.logger.debug(`DEX spot price for ${a}: $${i}`)}catch(e){this.logger.debug(`Could not fetch token details for symbol, falling back to token format parsing: ${e instanceof Error?e.message:String(e)}`),a=("Token"===t.collection?t.type:t.collection).toUpperCase(),this.logger.debug(`DEX spot price for ${a} (fallback): $${i}`)}return{symbol:a,price:i}}catch(e){if(e instanceof P)throw e;throw R(`Failed to fetch DEX spot price: ${_n(e)}`)}}async fetchLaunchpadTokenSpotPrice(e,t,n){if(!e||"string"!=typeof e)throw new Error(E);try{if(n)try{this.logger.debug(`Checking graduation status for token: ${e}`);const t=await n(e);if(t&&t.isGraduated){this.logger.debug(`Token ${e} is graduated, using DEX spot price`);const n=`${t.sellingToken.collection}|${t.sellingToken.category}|${t.sellingToken.type}|${t.sellingToken.additionalKey}`;return this._fetchDexTokenSpotPrice(n)}}catch(t){this.logger.debug(`Could not determine graduation status for ${e}, falling back to bonding curve: ${_n(t)}`)}this.logger.debug(`Using bonding curve calculation for token: ${e}`);const o=await t({tokenName:e,amount:"1",type:"native"}),i=await this._fetchDexTokenSpotPrice({collection:"GALA",category:"Unit",type:"none",additionalKey:"none"});if(!i)throw R("GALA price not available");const r=Number(o.amount)/1e18;if(r<=0)throw new P(`Invalid token amount calculation: ${r}`,"amount","INVALID_CALCULATION");const a=i.price/r;return{symbol:e.toUpperCase(),price:a}}catch(t){if(t instanceof Error)throw R(`Failed to calculate launchpad token spot price for ${e}: ${t.message}`);throw R(`Failed to calculate launchpad token spot price for ${e}: ${_n(t)}`)}}async fetchTokenDetails(e){this.logger.debug("Fetching token details from GalaChain for tokenId:",e);try{if(!this.galaChainService)throw R("GalaChainService not available for token metadata fetch",500);const t=await this.galaChainService.fetchTokenClassFromChain(e),n={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`),n}catch(t){if((t instanceof P||t instanceof Error)&&("NetworkError"===t.name||t.message.includes("Token not found")))throw t;throw R(`Failed to fetch token details from GalaChain for ${e}: ${_n(t)}`,500)}}async fetchAllDexSeasons(){try{if(!this.dexBackendHttp)throw R("DEX Backend API client not configured");const e=(await this.dexBackendHttp.request({method:"GET",url:"/leaderboard/seasons"})).data.map(e=>({id:e.id,name:e.name,start:new Date(e.start),end:new Date(e.end),rulesId:e.rules_id}));return this.logger.debug(`Fetched ${e.length} DEX seasons`),e}catch(e){if(e instanceof Error&&e.message.includes("not configured"))throw e;throw R(`Failed to fetch DEX seasons: ${_n(e)}`)}}async fetchCurrentDexSeason(){const e=await this.fetchAllDexSeasons(),t=new Date,n=e.find(e=>t>=e.start&&t<=e.end);return n?this.logger.debug(`Current DEX season: ${n.name} (ID: ${n.id})`):this.logger.debug("No active DEX season found"),n||null}async fetchDexLeaderboardBySeasonId(e){if(!e||"number"!=typeof e||e<1)throw M("seasonId","Season ID must be a positive number");try{if(!this.dexBackendHttp)throw R("DEX Backend API client not configured");const t=(await this.dexBackendHttp.request({method:"GET",url:"/leaderboard",params:{seasonId:e.toString()}})).data.leaderboard.map(e=>({wallet:e.wallet,rank:e.rank,totalXp:e.total_xp,distributionPercent:e.distribution_percent,liquidityXp:e.liquidity_xp,tradingXp:e.trading_xp,masteryTitles:e.mastery_titles.map(e=>({name:e.name,type:e.type,order:e.order}))}));return this.logger.debug(`Fetched leaderboard for season ${e} with ${t.length} entries`),{entries:t,seasonId:e,totalEntries:t.length}}catch(t){if(t instanceof Error&&t.message.includes("must be a positive number"))throw t;throw R(`Failed to fetch DEX leaderboard for season ${e}: ${_n(t)}`)}}async fetchCurrentDexLeaderboard(){const e=await this.fetchCurrentDexSeason();return e?this.fetchDexLeaderboardBySeasonId(e.id):(this.logger.debug("Cannot fetch current leaderboard - no active season"),null)}async fetchDexAggregatedVolumeSummary(){try{if(!this.dexBackendHttp)throw R("DEX Backend API client not configured");const e=await this.dexBackendHttp.request({method:"GET",url:"/explore/volume"}),t={volume1d:e.data.volume1d,volume1dDelta:e.data.volume1dDelta,volume7d:e.data.volume7d,volume7dDelta:e.data.volume7dDelta,volume30d:e.data.volume30d,volume30dDelta:e.data.volume30dDelta};return this.logger.debug(`Fetched DEX volume summary: $${t.volume1d.toFixed(2)} (1d)`),t}catch(e){throw R(`Failed to fetch DEX volume summary: ${_n(e)}`)}}}function Io(e,t=18){const n=parseFloat(e);if(0===n)return"0";return n.toFixed(t).replace(/\.?0+$/,"")}function Po(e){return Io(e,8)}function No(e){return Io(e,18)}function Do(e){return{maxAcceptableReverseBondingCurveFee:Po(e.maxAcceptableReverseBondingCurveFee)}}new S({debug:!1,context:"NumberUtils"});class Fo extends o.ChainCallDTO{constructor(e,t,n="0",o={maxAcceptableReverseBondingCurveFee:"0"}){super(),this.vaultAddress=e,this.nativeTokenQuantity=Po(t),this.expectedToken=No(n),this.extraFees=Do(o)}}class Co extends o.ChainCallDTO{constructor(e,t,n,o={maxAcceptableReverseBondingCurveFee:"0"}){super(),this.vaultAddress=e,this.tokenQuantity=No(t),this.expectedNativeToken=Po(n),this.extraFees=Do(o)}}class xo extends o.ChainCallDTO{constructor(e,t,n="0",o={maxAcceptableReverseBondingCurveFee:"0"}){super(),this.vaultAddress=e,this.tokenQuantity=No(t),this.expectedNativeToken=Po(n),this.extraFees=Do(o)}}class $o extends o.ChainCallDTO{constructor(e,t,n,o={maxAcceptableReverseBondingCurveFee:"0"}){super(),this.vaultAddress=e,this.nativeTokenQuantity=Po(t),this.expectedToken=No(n),this.extraFees=Do(o)}}const Lo={BuyNativeDto:Fo,BuyExactDto:Co,SellExactDto:xo,SellNativeDto:$o};class Oo extends Tn{constructor(e,t=!1){super(t),this.walletProvider=e}async signDTO(e,t,n){try{this.logger.debug("🔐 Signing DTO:",{methodName:t,dtoKeys:Object.keys(e)});const n=this.generateEIP712Types(t,e),i=o.calculatePersonalSignPrefix(e),r={...e,prefix:i},{signature:a,domain:s}=await this.signWithEthersWallet(n,r),c={...e,signature:a,types:n,domain:s};return this.logger.debug("✅ DTO signed successfully:",{payloadKeys:Object.keys(c),signatureLength:a.length}),c}catch(e){this.logger.error("❌ Signature generation failed:",e);throw G(`Failed to sign DTO: ${_n(e)}`)}}async signWithEthersWallet(e,t){try{let n,o;if(this.walletProvider.signTypedData&&!this.walletProvider.getNetwork)n={name:"ethereum",chainId:1},o=await this.walletProvider.signTypedData(n,e,t);else{if(!this.walletProvider.getNetwork||!this.walletProvider.signTypedData)throw K("Wallet provider does not support typed data signing","walletProvider");{const i=await this.walletProvider.getNetwork();n={name:i.name,chainId:Number(i.chainId)},o=await this.walletProvider.signTypedData(n,e,t)}}return{signature:o,domain:n}}catch(e){throw G(`Ethers.js signing failed: ${_n(e)}`)}}generateEIP712Types(e,t){const n={};n[e]=[];const o=Object.fromEntries(Object.entries(t).filter(([e,t])=>void 0!==t)),i=(e,t,o,r=!1)=>{if(void 0!==t){if(Array.isArray(t)){if(0===t.length)return;const a=i(e,t[0],o,!0);return r||n[o].push({name:e,type:(a??e)+"[]"}),a?a+"[]":void 0}if("object"==typeof t&&null!==t){if(n[e])throw new P(`Type name collision not supported: ${e}`,"fieldValue","TYPE_COLLISION");return n[e]=[],Object.entries(t).forEach(([t,n])=>{i(t,n,e)}),r||n[o].push({name:e,type:e}),e}{let i;switch(typeof t){case"string":i="string";break;case"number":i="uint256";break;case"boolean":i="bool";break;default:throw new P(`Unsupported type for field "${e}": ${typeof t} (value: ${JSON.stringify(t)})`,"fieldValue","UNSUPPORTED_TYPE")}return r||n[o].push({name:e,type:i}),i}}};return Object.entries(o).forEach(([t,n])=>{i(t,n,e)}),this.logger.debug("📝 Generated EIP-712 types:",n),n}}class _o extends Tn{constructor(e=!1){super(e)}generateStringsInstructions(e){try{this.logger.debug("🔧 Generating stringsInstructions for:",e);const t=this.extractTokenSymbolFromVault(e),n=this.createTokenInstance(t),o=this.createGalaInstance(),i=`$service$${n.toStringKey()}$launchpad`,r=`$tokenBalance$${n.toStringKey()}$${e}`,a=`$tokenBalance$${n.toStringKey()}$${e}`,s=`$tokenBalance$${o.toStringKey()}$${e}`,c=[i,r,a,s,`$tokenBalance$${o.toStringKey()}$${e}`];return this.logger.debug("✅ Generated stringsInstructions:",c),c}catch(e){this.logger.error("❌ Failed to generate stringsInstructions:",e);const t=_n(e);throw new P(`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 M("vaultAddress","Vault address");try{const t=mo(e);return this.logger.debug("🔍 Extracted token symbol:",{vaultAddress:e,tokenSymbol:t}),t}catch(e){if(e instanceof P)throw q("vaultAddress","format: service|Token$Unit$SYMBOL$eth:address$launchpad");throw e}}validateVaultAddress(e){if(!e||"string"!=typeof e)throw M("vaultAddress","Vault address");if(!e.startsWith("service|Token$Unit$"))throw q("vaultAddress",'starting with "service|Token$Unit$"');if(!e.endsWith("$launchpad"))throw q("vaultAddress",'ending with "$launchpad"');const t=e.split("$");if(t.length<5)throw q("vaultAddress",'having at least 5 parts separated by "$"');const n=t[2];if(!n||!/^[A-Za-z]{1,10}$/.test(n))throw q("vaultAddress","containing a 1-10 letter token symbol (case insensitive)");return this.logger.debug("✅ Vault address validation passed:",e),!0}generateTokenClassKeyString(e,t,n,o){return`${e}$${t}$${n}$${o}`}parseTokenClassKeyString(e){try{return ao(e)}catch(e){if(e instanceof P)throw q("stringKey","format: collection$category$type$additionalKey (4 parts)");throw e}}}function Bo(e,t,n){if(t<0||t>1)throw new Error(`Invalid slippage tolerance factor: ${t}. Must be between 0 and 1 (e.g., 0.05 for 5%)`);const o=new r(e);if(o.isNaN())throw new Error(`Invalid expected amount: ${e}. Must be a valid number`);if(0===t)return e;const i=o.multipliedBy(t);let a;switch(n){case"buy-native":case"sell-exact":a=o.minus(i);break;case"buy-exact":case"sell-native":a=o.plus(i);break;default:throw new Error(`Unknown operation type: ${n}`)}return a.isLessThan(0)&&(a=new r(0)),a.toFixed()}class Uo extends vn{constructor(e,t,n=!1,o,i,r=.05,a=.01){super(e,n),this.tokenResolver=t,this.walletProvider=o,this.userAddress=i,this.defaultSlippageToleranceFactor=r,this.defaultMaxAcceptableReverseBondingCurveFeeSlippageFactor=a,this.bundleEndpoint="/bundle",o&&i&&(this.signatureService=new Oo(o,n),this.tokenKeyService=new _o(n))}async submitTransaction(e){try{this.logger.debug("📦 Submitting bundle transaction:",{method:e.method,stringsInstructionsCount:e.stringsInstructions.length,signedDtoKeys:Object.keys(e.signedDto)}),this.validateBundleData(e);const t=this.formatBundleRequest(e);this.logger.debug("🚀 Bundle request payload:",{...t,signedDto:"[REDACTED - Contains signature]"});const n=await this.http.post(this.bundleEndpoint,t);return n?(this.logger.debug("📥 Bundle API response:",{success:n.success,hasData:!!n.data,error:n.error}),this.handleBundleResponse(n)):{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 M("bundleData","Bundle data");if(!e.signedDto)throw M("signedDto","Signed DTO");if(!e.method||"string"!=typeof e.method)throw M("method","Method name");if(!Array.isArray(e.stringsInstructions))throw q("stringsInstructions","an array of resource tracking strings");if(0===e.stringsInstructions.length)throw new P("stringsInstructions cannot be empty","stringsInstructions","EMPTY_ARRAY");const t=["BuyWithNative","BuyExactToken","SellExactToken","SellWithNative"];if(!t.includes(e.method))throw q("method",`one of: ${t.join(", ")}`);e.stringsInstructions.forEach((e,t)=>{if("string"!=typeof e||0===e.length)throw new P(`stringsInstructions[${t}] must be a non-empty string`,`stringsInstructions[${t}]`,"INVALID_INSTRUCTION");if(!e.startsWith("$"))throw new P(`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(Bn(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 _n(e)||"Unknown bundle transaction error"}async getBundlerTransactionResult(e){try{if(!e||"string"!=typeof e)throw M("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 M("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:n,type:o,expectedAmount:i}=e,{effectiveSlippageFactor:r,effectiveMaxFee:a,vaultAddress:s}=await this.prepareTradingOperation(t,e.maxAcceptableReverseBondingCurveFee,e.maxAcceptableReverseBondingCurveFeeSlippageFactor,e.slippageToleranceFactor);if("native"===o){if(!i)throw new P("expectedAmount is required for native buy operations. Use getBuyTokenAmount() first to calculate expected tokens.","expectedAmount","EXPECTED_AMOUNT_REQUIRED");const e=Bo(i,r,"buy-native");this.logger.debug("BuyNative slippage applied:",{originalExpectedTokens:i,slippageFactor:r,adjustedMinTokens:e});const t=new Lo.BuyNativeDto(s,n,e,{maxAcceptableReverseBondingCurveFee:a});return await this.executeBundleTransaction(t,"BuyWithNative",s)}{if(!i)throw new P("expectedAmount is required for exact buy operations. Use getBuyTokenAmount() first to calculate expected GALA cost.","expectedAmount","EXPECTED_AMOUNT_REQUIRED");const e=Bo(i,r,"buy-exact");this.logger.debug("BuyExact slippage applied:",{originalExpectedGalaCost:i,slippageFactor:r,adjustedMaxGalaCost:e});const t=new Lo.BuyExactDto(s,n,e,{maxAcceptableReverseBondingCurveFee:a});return await this.executeBundleTransaction(t,"BuyExactToken",s)}}async sellToken(e){this.ensureTradingServicesAvailable();const{tokenName:t,amount:n,type:o,expectedAmount:i}=e,{effectiveSlippageFactor:r,effectiveMaxFee:a,vaultAddress:s}=await this.prepareTradingOperation(t,e.maxAcceptableReverseBondingCurveFee,e.maxAcceptableReverseBondingCurveFeeSlippageFactor,e.slippageToleranceFactor);if("exact"===o){if(!i)throw new P("expectedAmount is required for exact sell operations. Use getSellTokenAmount() first to calculate expected GALA.","expectedAmount","EXPECTED_AMOUNT_REQUIRED");const e=Bo(i,r,"sell-exact");this.logger.debug("SellExact slippage applied:",{originalExpectedGala:i,slippageFactor:r,adjustedMinGala:e});const t=new Lo.SellExactDto(s,n,e,{maxAcceptableReverseBondingCurveFee:a});return await this.executeBundleTransaction(t,"SellExactToken",s)}{if(!i)throw new P("expectedAmount is required for native sell operations. Use getSellTokenAmount() first to calculate tokens to sell.","expectedAmount","EXPECTED_AMOUNT_REQUIRED");const e=Bo(i,r,"sell-native");this.logger.debug("SellNative slippage applied:",{originalExpectedTokensToSell:i,slippageFactor:r,adjustedMaxTokensToSell:e});const t=new Lo.SellNativeDto(s,n,e,{maxAcceptableReverseBondingCurveFee:a});return await this.executeBundleTransaction(t,"SellWithNative",s)}}async prepareTradingOperation(e,t,n,o){const{effectiveSlippageFactor:i,effectiveMaxFee:r}=this.calculateEffectiveSlippage(t,n,o),a=await this.resolveTokenNameToVault(e);if(!a)throw U(e);return{effectiveSlippageFactor:i,effectiveMaxFee:r,vaultAddress:a}}calculateEffectiveSlippage(e,t,n){const o=n??this.defaultSlippageToleranceFactor,i=t??this.defaultMaxAcceptableReverseBondingCurveFeeSlippageFactor;let r=e||"0";return e&&(r=Bo(e,i,"buy-exact"),this.logger.debug("Reverse bonding curve fee slippage applied:",{baseFee:e,slippageFactor:i,adjustedMaxFee:r})),{effectiveSlippageFactor:o,effectiveFeeSlippageFactor:i,effectiveMaxFee:r}}ensureTradingServicesAvailable(){if(!this.signatureService||!this.tokenKeyService)throw K("Trading services not available. BundleService requires walletProvider and userAddress for trading operations.","walletProvider");if(!this.userAddress)throw M("userAddress","User address")}async executeBundleTransaction(e,t,n){this.ensureTradingServicesAvailable();try{e.uniqueKey=`galaswap - operation - ${c.v4()}-${Date.now()}-${this.userAddress}`;const o=await this.signatureService.signDTO(e,t,this.userAddress),i=this.tokenKeyService.generateStringsInstructions(n),r={stringsInstructions:i,method:t,signedDto:o};this.logger.debug("📦 Bundle transaction data:",{method:t,stringsInstructions:i,dtoKeys:Object.keys(o)});const a=await this.submitTransaction(r);if(a.success&&a.data)return this.logger.debug("✅ Bundle transaction submitted:",a.data),{success:!0,data:{transactionId:a.data,message:"Transaction submitted successfully. Monitor WebSocket for completion."}};throw new Error(String(a.error||"Bundle transaction failed"))}catch(e){throw this.logger.error("❌ Bundle transaction error:",e),e}}async resolveTokenNameToVault(e){return await this.tokenResolver.resolveTokenToVault(e)}}var Mo,qo;!function(e){e.PROCESSED="PROCESSED",e.COMPLETED="COMPLETED",e.SUCCESS="SUCCESS",e.FAILED="FAILED",e.ERROR="ERROR",e.PROCESSING="PROCESSING",e.PENDING="PENDING"}(Mo||(Mo={})),e.SDKTransactionStatus=void 0,(qo=e.SDKTransactionStatus||(e.SDKTransactionStatus={})).PENDING="pending",qo.PROCESSING="processing",qo.COMPLETED="completed",qo.FAILED="failed",qo.TIMEOUT="timeout";const Ro={[Mo.PROCESSED]:e.SDKTransactionStatus.COMPLETED,[Mo.COMPLETED]:e.SDKTransactionStatus.COMPLETED,[Mo.SUCCESS]:e.SDKTransactionStatus.COMPLETED,[Mo.FAILED]:e.SDKTransactionStatus.FAILED,[Mo.ERROR]:e.SDKTransactionStatus.FAILED,[Mo.PROCESSING]:e.SDKTransactionStatus.PROCESSING,[Mo.PENDING]:e.SDKTransactionStatus.PENDING};class Ko extends Tn{constructor(e,t=!1){super(t),this.socket=null,this.listeners=new Map,this.timeouts=new Map,this.reconnectCount=0,this.hasOnAnyListener=!1,this.eventBuffer=new Map,this.eventBufferTimeouts=new Map,this.MAX_BUFFER_SIZE=1e3,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.socket.onAny((e,...t)=>{if(e&&t.length>0&&"object"==typeof t[0]&&null!==t[0]){const n=t[0],o=n.status||n.Status;if(o&&"string"==typeof o){if(this.logger.debug(`📡 [Event Buffer] Buffering event for ${e}: ${o}`),this.eventBuffer.size>=this.MAX_BUFFER_SIZE){const e=this.eventBuffer.keys().next().value;if(e){const t=this.eventBufferTimeouts.get(e);t&&(clearTimeout(t),this.eventBufferTimeouts.delete(e)),this.eventBuffer.delete(e),this.logger.warn(`📡 [Event Buffer] Buffer full (${this.MAX_BUFFER_SIZE}), dropped oldest: ${e}`)}}this.eventBuffer.set(e,n);const t=this.eventBufferTimeouts.get(e);t&&clearTimeout(t);const i=setTimeout(()=>{this.eventBuffer.has(e)&&(this.logger.debug(`📡 [Event Buffer] Cleaning up orphaned event for ${e}`),this.eventBuffer.delete(e),this.eventBufferTimeouts.delete(e))},3e4);this.eventBufferTimeouts.set(e,i)}}this.debug&&this.logger.debug(`📡 [WebSocket Event] "${e}":`,JSON.stringify(t,null,2))}),this.hasOnAnyListener=!0}catch(e){t(e)}})}async monitorTransaction(t,n){this.listeners.set(t,n),this.logger.debug(`📡 Starting to monitor transaction: ${t}`),this.logger.debug(`📡 WebSocket connected: ${!!this.socket&&this.socket.connected}`);const o=this.eventBuffer.get(t);if(o){this.logger.debug(`📡 [Event Buffer] Found buffered event for ${t}, delivering immediately`),setImmediate(()=>{this.processTransactionEvent(t,o,n)});const e=this.eventBufferTimeouts.get(t);e&&(clearTimeout(e),this.eventBufferTimeouts.delete(t)),this.eventBuffer.delete(t)}const i=this.config.timeout||3e5,r=setTimeout(()=>{if(this.listeners.has(t)){const o=Math.round(i/1e3),r={transactionId:t,status:e.SDKTransactionStatus.TIMEOUT,message:`Transaction monitoring timeout - no response after ${o} seconds`,timestamp:Date.now()};this.logger.debug(`📡 Transaction timeout for ${t} (${o}s)`),n(r),this.listeners.delete(t),this.timeouts.delete(t),this.socket?.off(t)}},i);if(this.timeouts.set(t,r),this.socket&&this.socket.connected)this.socket.off(t),this.logger.debug(`📡 Listening for transaction updates: ${t}`),this.logger.debug(`📡 WebSocket connection ID: ${this.socket.id}`),this.logger.debug(`📡 WebSocket URL: ${this.config.url}`),this.socket.on(t,e=>{this.processTransactionEvent(t,e,n)});else{const o={transactionId:t,status:e.SDKTransactionStatus.FAILED,message:"WebSocket not connected - cannot monitor transaction",timestamp:Date.now()};n(o),this.listeners.delete(t),this.timeouts.delete(t)}}processTransactionEvent(t,n,o){this.logger.debug(`📡 Socket.IO transaction update for ${t}:`,JSON.stringify(n,null,2));const i=n,r=i?.data,a=i?.status||i?.Status||r?.status||r?.Status;let s=i?.message||i?.Message||r?.message||r?.Message||i?.error||r?.error;s&&"string"==typeof s||(s=a===Mo.FAILED||a===Mo.ERROR?"Transaction failed - check transaction details":a===Mo.COMPLETED||a===Mo.PROCESSED||a===Mo.SUCCESS?"Transaction completed successfully":a?`Transaction status: ${a}`:"Unknown transaction status");const c=i?.blockHash||r?.blockHash,l=i?.gasUsed||r?.gasUsed,u=i?.Data||r?.Data,d={transactionId:t,status:this.mapSocketStatus(a),message:"string"==typeof s?s:"Transaction update received",timestamp:Date.now(),...c&&{blockHash:c},...l&&{gasUsed:l},...u&&{data:u}};if(this.logger.debug(`📡 Mapped status for ${t}: ${a} -> ${d.status}`),this.logger.debug(`📡 Final message: "${s}"`),o(d),d.status===e.SDKTransactionStatus.COMPLETED||d.status===e.SDKTransactionStatus.FAILED){this.listeners.delete(t);const e=this.timeouts.get(t);e&&(clearTimeout(e),this.timeouts.delete(t)),this.socket?.off(t),this.logger.debug(`📡 Cleaned up listener for ${t} (${d.status})`)}}async waitForTransaction(t){return new Promise((n,o)=>{this.monitorTransaction(t,t=>{t.status===e.SDKTransactionStatus.COMPLETED?n(t):t.status!==e.SDKTransactionStatus.FAILED&&t.status!==e.SDKTransactionStatus.TIMEOUT||o(new Error(`Transaction ${t.status}: ${t.message}`))})})}mapSocketStatus(t){const n=t?.toUpperCase();return Ro[n]||e.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.eventBuffer.clear(),this.eventBufferTimeouts.forEach(e=>{clearTimeout(e)}),this.eventBufferTimeouts.clear(),this.logger.debug("🧹 Cleared event buffer and timeouts"),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 Go extends Tn{constructor(e,t=!1){super(t),this.poolService=e,this.cache=new Map}async resolveTokenToVault(e){if(!j(e))throw new P("Token name is required and must be a non-empty string","tokenName","INVALID_TOKEN_NAME");const t=e.trim().toLowerCase(),n=this.get(t);if(n)return n;try{const n=await this.poolService.resolveTokenNameToVault(e);return n&&this.set(t,n),n}catch{return null}}async resolveTokenClassKey(e){const t=await this.resolveTokenToVault(e);if(!t)throw U(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:n}of e)this.set(t,n)}parseVaultAddressToTokenClassKey(e){try{return go(e)}catch(e){if(e instanceof P)throw q("vaultAddress","format: service|Token$Unit$SYMBOL$eth:address$launchpad","Vault address");throw e}}}async function zo(e,t={}){const{maxPages:n=1e4,logger:o,pageSize:i=20}=t,r=[];let a=1,s=!0,c=0;for(;s&&a<=n;){o&&o.debug(`Auto-pagination: fetching page ${a} with limit ${i}`);const t=await e(a,i);if(!t||!Array.isArray(t.items)){o&&o.warn("Auto-pagination: received invalid result structure, stopping");break}if(r.push(...t.items),c=t.total,o&&o.debug(`Auto-pagination: page ${a} returned ${t.items.length} items, hasNext: ${t.hasNext}`),0===t.items.length){o&&o.debug(`Auto-pagination: no items returned on page ${a}, exiting loop`);break}s=t.hasNext,a++}return a>n&&o&&o.warn(`Auto-pagination: exceeded maxPages limit of ${n}, stopping iteration`),o&&o.debug(`Auto-pagination: completed with total items: ${r.length}, total count: ${c}`),{items:r,total:c}}function Wo(e,t=e.length,n="items"){return{...{page:1,limit:e.length||0,total:t,totalPages:Math.ceil(t/(e.length||1))||1,hasNext:!1,hasPrevious:!1},[n]:e}}class jo extends vn{constructor(e,t=!1,n){super(e,t),this.tokenResolverService=n}async fetchTokenClassKeyByTokenName(e){if(!this.tokenResolverService)throw K("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 K("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 K(`Token '${e}' not found or could not be resolved to vault address`,"tokenName");this.logger.debug(`Resolved '${e}' to vault address: ${t}`);const n=go(t),o=`${n.collection}|${n.category}|${n.type}|${n.additionalKey}`;return this.logger.debug(`Extracted token class key: ${o}`),o}catch(t){if(t instanceof Error&&t.message.includes("ConfigurationError"))throw t;throw R(`Failed to resolve token name '${e}': ${_n(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 n=await this.fetchTokenClassKeyByTokenName(e.tokenName);t=n,this.logger.debug(`Resolved to token ID: ${n}`)}if(!t)throw K("Token ID is required but was not provided or resolved","tokenId");const{normalizeToTokenInstanceKey:n}=await Promise.resolve().then(function(){return yo}),o=n(t),i=ro(`${o.collection}|${o.category}|${o.type}|${o.additionalKey}`),{from:r,to:a,sortOrder:s="DESC",page:c=1,limit:l=10}=e,u={token:i,page:String(c),limit:String(l)};r&&(u.from=r.toISOString()),a&&(u.to=a.toISOString());const d=function(e){if(e)return e.toLowerCase()}(s);d&&(u.order=d),this.logger.debug(`Querying price snapshots for token ${i}, page ${c}, limit ${l}`);const h=await this.http.get("/price-oracle/fetch-price",u);if(!h)throw R("No response from price history service",500);const 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 R(`Failed to fetch price history: ${_n(e)}`,500)}}transformApiResponseToPriceHistory(e){if(!e?.data?.data||!Array.isArray(e.data.data))throw R("Invalid API response: missing or invalid data.data array",500);if(!e?.data?.meta)throw R("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}`})),n=e.data.meta,o=n.currentPage??1,i=n.totalPages??1;return{snapshots:t,page:o,limit:n.pageSize??50,total:n.totalItems??0,totalPages:i,hasNext:o<i,hasPrevious:o>1}}async fetchAllPriceHistory(e){this.logger.debug("Fetching all price history with options:",e);const t=await zo((t,n)=>this.fetchPriceHistory({...e,page:t,limit:n}).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 Wo(t.items,t.total,"snapshots")}validateOptions(e){const{from:t,to:n,sortOrder:o,page:i=1,limit:r=10}=e;if(function(e,t,n,o={}){const{description:i="parameter",treatEmptyAsNull:r=!0}=o,a=e[t],s=e[n],c=null!=a&&(!r||""!==a),l=null!=s&&(!r||""!==s);if(!c&&!l)throw K(`Either ${t} or ${n} must be provided (${i})`,n);if(c&&l)throw K(`Cannot provide both ${t} and ${n}. Provide exactly one (${i}).`,n)}(e,"tokenName","tokenId",{description:"token identifier"}),t&&!(t instanceof Date)&&isNaN(new Date(t).getTime()))throw K("from must be a valid Date","from");if(n&&!(n instanceof Date)&&isNaN(new Date(n).getTime()))throw K("to must be a valid Date","to");if(o&&"ASC"!==o&&"DESC"!==o)throw K('sortOrder must be either "ASC" or "DESC"',"sortOrder");if(!Number.isInteger(i)||i<1)throw K("page must be a positive integer","page");if(!Number.isInteger(r)||r<1||r>50)throw K("limit must be between 1 and 50","limit")}}function Ho(e){if("object"==typeof e&&null!==e)return function(e){if(!e||"object"!=typeof e)throw new P("Token object must be a non-null object, got "+typeof e,"token","INVALID_TOKEN_OBJECT");const{collection:t,category:n,type:o,additionalKey:i}=e;if(!t||"string"!=typeof t)throw new P("Token.collection must be a non-empty string, got "+typeof t,"token.collection","MISSING_OR_INVALID_COLLECTION");if(!n||"string"!=typeof n)throw new P("Token.category must be a non-empty string, got "+typeof n,"token.category","MISSING_OR_INVALID_CATEGORY");if(!o||"string"!=typeof o)throw new P("Token.type must be a non-empty string, got "+typeof o,"token.type","MISSING_OR_INVALID_TYPE");if(!i||"string"!=typeof i)throw new P("Token.additionalKey must be a non-empty string, got "+typeof i,"token.additionalKey","MISSING_OR_INVALID_ADDITIONAL_KEY");return{collection:t,category:n,type:o,additionalKey:i}}(e);if(!e)throw new P(`Token cannot be null, undefined, or empty. Received: ${JSON.stringify(e)}`,"token","EMPTY_TOKEN");if("string"!=typeof e)throw new P("Token must be a string or TokenClassKey object, got "+typeof e,"token","INVALID_TOKEN_TYPE");if(e.includes("|"))return function(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 pipe-delimited token format. Expected 4+ parts separated by |, got ${t.length}`);const[n,o,i,...r]=t;if(!n||!o||!i)throw new Error("Collection, category, and type must be non-empty");const a=r.join("|");if(!a)throw new Error("AdditionalKey must be non-empty");return{collection:n,category:o,type:i,additionalKey:a}}catch(t){const n=e.split("|");throw new P(`Invalid pipe-delimited token: "${e}" (${n.length} parts). Expected format: "collection|category|type|additionalKey" (4 parts minimum). Received: [${n.map(e=>`"${e}"`).join(", ")}]. Error: ${t instanceof Error?t.message:String(t)}`,"token","INVALID_PIPE_DELIMITED_TOKEN")}}(e);if(e.includes("$"))return function(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 4+ parts separated by $, got ${t.length}`);const n=t[t.length-1],o=t[t.length-2],i=t[t.length-3],r=t.slice(0,t.length-3).join("$");if(!(r&&i&&o&&n))throw new Error("All components (collection, category, type, additionalKey) must be non-empty");return{collection:r,category:i,type:o,additionalKey:n}}catch(t){const n=e.split("$");throw new P(`Invalid dollar-delimited token: "${e}" (${n.length} parts). Expected format: "collection$category$type$additionalKey" (4 parts minimum). Received: [${n.map(e=>`"${e}"`).join(", ")}]. Error: ${t instanceof Error?t.message:String(t)}`,"token","INVALID_DOLLAR_DELIMITED_TOKEN")}}(e);throw new P(`Plain token string "${e}" (length: ${e.length}) is not allowed - tokens must be delimited with | or $. Expected format: "GALA|Unit|none|none" or "GALA$Unit$none$none". Input: "${e}"`,"token","PLAIN_STRING_NOT_ALLOWED")}class Vo extends Error{constructor(e,t,n){super(`API Error [${e}]: ${t}`),this.status=e,this.message=t,this.details=n,this.name="ApiError"}}function Qo(e){return"object"==typeof e&&null!==e&&"collection"in e&&"category"in e&&"type"in e&&"additionalKey"in e&&"string"==typeof e.collection&&"string"==typeof e.category&&"string"==typeof e.type&&"string"==typeof e.additionalKey}function Xo(e){return"object"==typeof e&&null!==e&&"positionId"in e&&"owner"in e&&"token0"in e&&"token1"in e&&"fee"in e&&"liquidity"in e&&"string"==typeof e.positionId&&"string"==typeof e.owner&&"number"==typeof e.fee}class Zo{constructor(e){this.client=t.create({baseURL:e.baseUrl,timeout:e.timeout??3e4})}async getPoolData(e){try{if("string"==typeof e.token0||"string"==typeof e.token1)throw new Error(`GalaChain API getPoolData requires TokenClassKey objects, not strings. Received: token0="${"string"==typeof e.token0?e.token0:"[object]"}", token1="${"string"==typeof e.token1?e.token1:"[object]"}". Convert pipe-delimited tokens using parseToken() before calling getPoolData(). Example: parseToken("GALA|Unit|none|none") → { collection: "GALA", category: "Unit", type: "none", additionalKey: "none" }`);const t=await this.client.post("/api/asset/dexv3-contract/GetPoolData",e);this.validateResponse(t.data);const n=t.data.Data;if(!function(e){if("object"!=typeof e||null===e)return!1;const t=e;return"string"==typeof t.token0&&"string"==typeof t.token1&&Qo(t.token0ClassKey)&&Qo(t.token1ClassKey)&&"number"==typeof t.fee&&"number"==typeof t.tickSpacing&&"string"==typeof t.liquidity&&"string"==typeof t.sqrtPrice&&"number"==typeof t.tick&&"string"==typeof t.feeGrowthGlobal0&&"string"==typeof t.feeGrowthGlobal1}(n))throw new Vo(t.status,"Invalid pool data response format",n);return n}catch(e){throw this.handleError(e,"getPoolData")}}async getPositions(e){try{const t=await this.client.post("/api/asset/dexv3-contract/GetPositions",e);this.validateResponse(t.data);const n=t.data.Data;if(!Array.isArray(n.positions))throw new Vo(t.status,"Invalid positions response format: positions must be an array",n);for(const e of n.positions)if(!Xo(e))throw new Vo(t.status,"Invalid position in response",e);return n}catch(e){throw this.handleError(e,"getPositions")}}async getPositionById(e,t,n,o,i,r,a){try{let s,c;if(void 0!==t&&void 0!==n&&void 0!==o&&void 0!==i&&void 0!==r){s={owner:e,token0:"string"==typeof t?{collection:t,category:"Unit",type:"none",additionalKey:"none"}:t,token1:"string"==typeof n?{collection:n,category:"Unit",type:"none",additionalKey:"none"}:n,fee:o,tickLower:i,tickUpper:r},a&&(s.positionId=a),c=`${e}/${t}/${n}/${o}`}else s={positionId:e},c=e;const l=await this.client.post("/api/asset/dexv3-contract/GetPositions",s);this.validateResponse(l.data);const u=l.data.Data;let d;if(u&&"object"==typeof u&&"positionId"in u&&!("positions"in u))d=u;else{if(!(u&&Array.isArray(u.positions)&&u.positions.length>0))throw new Vo(404,`Position not found: ${c}`);d=u.positions[0]}const h={Data:d,Status:l.status};return void 0!==l.data.Message&&(h.Message=l.data.Message),h}catch(t){throw this.handleError(t,`getPositionById(${e})`)}}async getRemoveLiquidityEstimation(e){try{const t=await this.client.post("/api/asset/dexv3-contract/GetRemoveLiquidityEstimation",e);this.validateResponse(t.data);const n=t.data.Data;if("string"!=typeof n.amount0||"string"!=typeof n.amount1||"string"!=typeof n.fee0||"string"!=typeof n.fee1)throw new Vo(t.status,"Invalid removal estimation response format",n);return n}catch(e){throw this.handleError(e,"getRemoveLiquidityEstimation")}}validateResponse(e){if(!e||"object"!=typeof e)throw new Vo(500,"Invalid response format: not an object");if(!("Data"in e)||!("Status"in e))throw new Vo(500,"Invalid response format: missing Data or Status field");if(e.Status>=400)throw new Vo(e.Status,e.Message??"Gateway error",e.Data)}handleError(e,n){if(e instanceof Vo)return e;if(t.isAxiosError(e)){const t=e.response?.status??500,o=e.response?.data?.Message??e.message,i=e.response?.data?.Data??void 0;return new Vo(t,`${n}: ${o}`,i)}return new Vo(500,`${n}: ${e instanceof Error?e.message:String(e)}`)}}class Yo{constructor(e){this.http=e}async getUserAssets(e,t=20,n=0){try{if(!e||"string"!=typeof e)throw new Vo(400,"Invalid wallet address");const o=Math.max(1,Math.floor(n/t)+1),i={};i.address=e,i.page=o,i.limit=t;const r=await this.http.get("/user/assets",i);if(!r||"object"!=typeof r)throw new Vo(500,"Invalid response format: not an object");const a=r.data;if(!a||"object"!=typeof a)throw new Vo(500,"Invalid response format: missing data wrapper");const s=a.token;if(!Array.isArray(s))throw new Vo(500,"Invalid response format: token array must be an array");const c=[];for(const e of s){if("object"!=typeof e||null===e)throw new Vo(500,"Invalid asset in response: asset must be an object");const t=e;if("string"!=typeof t.symbol||"string"!=typeof t.name)throw new Vo(500,"Invalid asset in response: missing symbol or name",t);const n="number"==typeof t.decimals?t.decimals:"string"==typeof t.decimals?parseInt(t.decimals,10):void 0;if("number"!=typeof n||isNaN(n))throw new Vo(500,"Invalid asset in response: decimals must be a number",t);const o={tokenId:t.compositeKey||`${t.symbol}$Unit$none$none`,symbol:t.symbol,name:t.name,decimals:n,balance:t.quantity||"0"};t.image&&(o.imageUrl=t.image),t.verify&&(o.verified=t.verify),c.push(o)}const l={tokens:c,count:a.count??c.length};return void 0!==a.totalValue&&(l.totalValue=a.totalValue),l}catch(t){throw this.handleError(t,`getUserAssets(${e})`)}}handleError(e,t){if(e instanceof Vo)return e;if(e instanceof Error&&e instanceof Error&&"response"in e){const n=e,o=n.response?.status??500,i=n.response?.data?.message??n.response?.data?.Message??n.message,r=n.response?.data?.Data??n.response?.data?.data??void 0;return n.response,new Vo(o,`${t}: ${i}`,r)}return new Vo(500,`${t}: ${e instanceof Error?e.message:String(e)}`)}}const Jo=10;class ei extends Tn{constructor(e,t,n){if(super(!1),this.pricingConcurrency=5,this.tokenConverter=new io,this.webSocketService=t,this.dexQuoteService=n,this.getWalletAddress=e.getWalletAddress,this.galaChainBaseUrl=e.galaChainBaseUrl,this.bundlerBaseUrl=e.bundlerBaseUrl,this.gatewayBaseUrl=e.gatewayBaseUrl,this.privateKey=e.privateKey,!(e.gatewayBaseUrl&&e.bundlerBaseUrl&&e.dexBackendBaseUrl&&e.dexBackendHttp))throw new Error("GSwapService requires explicit gatewayBaseUrl, bundlerBaseUrl, dexBackendBaseUrl, and dexBackendHttp configuration. These must be provided by LaunchpadSDK to ensure environment alignment.");try{this.gatewayClient=new Zo({baseUrl:e.gatewayBaseUrl,timeout:3e4}),this.dexBackendClient=new Yo(e.dexBackendHttp),this.logger.debug("HTTP clients initialized successfully",{gatewayUrl:e.gatewayBaseUrl,dexBackendUrl:e.dexBackendBaseUrl})}catch(e){throw this.logger.error("Failed to initialize HTTP clients",e),new Error("Failed to initialize GSwapService HTTP clients")}}setPricingConcurrency(e){if(e<1)throw new Error("Pricing concurrency must be at least 1");e>100&&this.logger.warn("Pricing concurrency > 100 may cause performance issues",{concurrency:e}),this.pricingConcurrency=e,this.logger.debug("Updated pricing concurrency",{concurrency:this.pricingConcurrency})}async getSwapQuoteExactInput(e){try{if(new r(e.amount).isLessThanOrEqualTo(0))throw new C("Amount must be greater than zero",{amount:e.amount,fromToken:e.fromToken,toToken:e.toToken});if(!this.dexQuoteService)throw new C("DexQuoteService not configured - cannot provide quotes",{fromToken:e.fromToken,toToken:e.toToken});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),n=this.tokenConverter.toLaunchpadFormat(e.toToken),o=[3e3,500,1e4];let i;for(const a of o)try{const o=await this.dexQuoteService.fetchCompositePoolData({token0:t,token1:n,fee:a,gatewayBaseUrl:this.gatewayBaseUrl}),i=await this.dexQuoteService.calculateDexPoolQuoteExactAmount({compositePoolData:o,fromToken:t,toToken:n,amount:e.amount}),s=new r(i.currentSqrtPrice),c=new r(i.newSqrtPrice),l=s.gt(c)?s.minus(c).dividedBy(s):new r(0),u=new r(i.amount0),d=new r(i.amount1),h=u.isNegative(),g=d.isNegative();this.logger.debug("=== AMOUNT SELECTION RAW DATA ===",{"quoteResult.amount0":i.amount0,"quoteResult.amount1":i.amount1,"amount0BN.isNegative()":h,"amount1BN.isNegative()":g});const p=h?u:d;this.logger.debug("=== AMOUNT SELECTION RESULT ===",{selectedFromAmount0:h,selectedAmount:p.toFixed(),selectedAmountAbs:p.absoluteValue().toFixed()});const m=p.absoluteValue().toFixed();return{fromToken:e.fromToken,toToken:e.toToken,inputAmount:e.amount,estimatedOutput:m,feeTier:a,priceImpact:l.toFixed(),executionPrice:this.calculateExecutionPrice(e.amount,m),currentSqrtPrice:i.currentSqrtPrice,newSqrtPrice:i.newSqrtPrice}}catch(e){i=e,this.logger.debug("DexQuoteService failed for fee tier, trying next",{feeTier:a,error:e instanceof Error?e.message:"Unknown error"})}throw i||new C("No available fee tiers for quote",{feeTiers:o,fromToken:e.fromToken,toToken:e.toToken})}catch(e){this.handleGSwapError("Failed to get swap quote for exact input",C,e)}}async getSwapQuoteExactOutput(e){try{if(new r(e.amount).isLessThanOrEqualTo(0))throw new C("Amount must be greater than zero",{amount:e.amount,fromToken:e.fromToken,toToken:e.toToken});if(!this.dexQuoteService)throw new C("DexQuoteService not configured - cannot provide quotes",{fromToken:e.fromToken,toToken:e.toToken});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),n=this.tokenConverter.toLaunchpadFormat(e.toToken),o=[3e3,500,1e4];let i;for(const a of o)try{const o=await this.dexQuoteService.fetchCompositePoolData({token0:t,token1:n,fee:a,gatewayBaseUrl:this.gatewayBaseUrl}),i=await this.dexQuoteService.calculateDexPoolQuoteExactAmount({compositePoolData:o,fromToken:t,toToken:n,amount:e.amount}),s=new r(i.currentSqrtPrice),c=new r(i.newSqrtPrice),l=s.gt(c)?s.minus(c).dividedBy(s):new r(0),u=o.pool.token0,d="string"==typeof u?u.split("|")[0]:"object"==typeof u&&null!==u&&"tokenName"in u?u.tokenName:String(u),h=n.split("|")[0]===d?i.amount1:i.amount0;return{fromToken:e.fromToken,toToken:e.toToken,inputAmount:h,estimatedOutput:e.amount,feeTier:a,priceImpact:l.toFixed(),executionPrice:this.calculateExecutionPrice(h,e.amount),currentSqrtPrice:i.currentSqrtPrice,newSqrtPrice:i.newSqrtPrice}}catch(e){i=e,this.logger.debug("DexQuoteService failed for fee tier, trying next",{feeTier:a,error:e instanceof Error?e.message:"Unknown error"})}throw i||new C("No available fee tiers for quote",{feeTiers:o,fromToken:e.fromToken,toToken:e.toToken})}catch(e){this.handleGSwapError("Failed to get swap quote for exact output",C,e)}}async executeSwap(e){try{if(!this.privateKey)throw new Error("GSwapService not initialized with signing capability (privateKey required)");this.logger.debug("Executing swap",{fromToken:e.fromToken,toToken:e.toToken,inputAmount:e.inputAmount});const{gswapToken0:t,gswapToken1:n}=this.convertTokenPair(e.fromToken,e.toToken),o=function(e,t=.01){const n=Zn(e),o=new r(1).minus(t);return n.multipliedBy(o)}(e.estimatedOutput,e.slippageTolerance||.01),i=this.getWalletAddress();if(!i)throw new Error("Wallet address required for swap execution");let a;try{const t=await this.getSwapQuoteExactInput({fromToken:e.fromToken,toToken:e.toToken,amount:e.inputAmount});a=t.currentSqrtPrice,this.logger.debug("Quote refetch successful - extracted sqrtPrices",{currentSqrtPrice:a,newSqrtPrice:t.newSqrtPrice,feeTier:t.feeTier})}catch(t){this.logger.debug("Could not re-fetch quote for sqrtPrice, using default",{fromToken:e.fromToken,toToken:e.toToken,error:t instanceof Error?t.message:String(t)})}const s={fromToken:t,toToken:n,inputAmount:e.inputAmount,minOutput:o.toFixed(),feeTier:e.feeTier,walletAddress:i,slippageTolerance:e.slippageTolerance||.01};void 0!==a&&(s.currentSqrtPrice=a);const c=await this.sendSwapToBundler(s);this.logger.debug("Swap submitted, monitoring transaction",{transactionId:c,fromToken:e.fromToken,toToken:e.toToken}),await this.ensureWebSocketConnected();const l=await this.webSocketService.waitForTransaction(c);return{transactionId:l.transactionId,status:l.status,fromToken:e.fromToken,toToken:e.toToken,inputAmount:e.inputAmount,outputAmount:e.estimatedOutput,feeTier:e.feeTier,slippageTolerance:e.slippageTolerance||.01,timestamp:new Date(l.timestamp),wait:async e=>{await this.webSocketService.waitForTransaction(c)}}}catch(e){const t=e;this.handleGSwapError("Failed to execute swap",x,e,{transactionHash:t?.txHash})}}async getUserAssets(e,t=1,n=20){try{this.logger.debug("Fetching user assets",{walletAddress:e,page:t,limit:n});const o=await this.dexBackendClient.getUserAssets(e,n,(t-1)*n);return(Array.isArray(o)?o:o?.tokens||[]).map(e=>{const t=e.symbol||"UNKNOWN";try{return{tokenId:Ho(`${t}|Unit|none|none`),symbol:t,balance:(n=e.quantity||e.balance||0,void 0!==o?Zn(n).toFixed(o):Zn(n).toFixed()),decimals:e.decimals||18}}catch(n){return this.logger.debug(`Skipping asset with processing error: ${t}`,{error:n instanceof Error?n.message:String(n),assetData:{symbol:t,quantity:e.quantity,balance:e.balance,decimals:e.decimals}}),null}var n,o}).filter(e=>null!==e)}catch(o){this.handleGSwapError("Failed to fetch user assets",L,o,{walletAddress:e,page:t,limit:n})}}async getAllUserAssets(e){try{this.logger.debug("Fetching all user assets (auto-paginated)",{walletAddress:e});const t=await zo((t,n)=>this.getUserAssets(e,t,n).then(e=>({items:e,page:t,limit:n,total:e.length,totalPages:1,hasNext:e.length===n,hasPrevious:t>1})),{maxPages:1e4,logger:this.logger,pageSize:20});return this.logger.debug("Fetched all user assets",{walletAddress:e,totalAssets:t.items.length}),t.items}catch(t){this.handleGSwapError("Failed to fetch all user assets",L,t,{walletAddress:e})}}async getPoolInfo(e,t){try{this.logger.debug("Fetching pool info",{tokenA:e,tokenB:t});const{gswapToken0:n,gswapToken1:o}=this.convertTokenPair(e,t),i=[500,3e3,1e4];let a=new r(0),s=0;for(const c of i)try{const e="string"==typeof n?Ho(n):n,t="string"==typeof o?Ho(o):o,i=await this.gatewayClient.getPoolData({token0:e,token1:t,fee:c});i&&(a=a.plus(new r(i.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:a.toFixed(),feeTiers:i,swapCount:s}}catch(n){this.logger.warn("Failed to fetch pool info",n);const o=n;return this.logger.debug("Pool error details",{error:new $(`Failed to fetch pool info: ${o?.message||String(n)}`,n,e,t,this.extractGSwapErrorCode(n))}),{tokenA:e,tokenB:t,liquidity:"0",feeTiers:[500,3e3,1e4],swapCount:0}}}chunkArray(e,t){const n=[];for(let o=0;o<e.length;o+=t)n.push(e.slice(o,o+t));return n}async fetchPositionPrices(e){const t=this.pricingConcurrency;if(0===e.length)return new Map;const n=new Map;for(const t of e){const e=`${t.token0}|${t.token1}|${t.feeTier}`;n.has(e)||n.set(e,{token0:t.token0,token1:t.token1,feeTier:t.feeTier})}const o=Array.from(n.values()),i=this.chunkArray(o,t);this.logger.debug("Fetching pricing for positions",{totalPositions:e.length,uniquePoolsToPrice:n.size,chunks:i.length,concurrency:t});const r=new Map;for(let e=0;e<i.length;e++){const t=i[e];(await Promise.allSettled(t.map(async e=>{const t=await this.getSwapQuoteExactInput({fromToken:e.token0,toToken:e.token1,amount:"1"});return{key:`${e.token0}|${e.token1}|${e.feeTier}`,data:{token0:e.token0,token1:e.token1,feeTier:e.feeTier,currentPrice:t.executionPrice,executionPrice:t.executionPrice,priceImpact:t.priceImpact,estimatedOutput:t.estimatedOutput,pricedAt:new Date}}}))).forEach(e=>{"fulfilled"===e.status?r.set(e.value.key,e.value.data):this.logger.warn("Failed to fetch price for pool",{error:e.reason})})}return r}normalizePositionResponse(e,t){const n=e=>{if(!e)return"";if("string"==typeof e)return e;if("object"==typeof e){if(e.type&&"none"!==e.type)return e.type;if(e.collection)return e.collection;if(e.symbol)return e.symbol;if(e.tokenSymbol)return e.tokenSymbol;if(e.name)return e.name}return""};return{positionId:e.positionId||e.id||"",ownerAddress:t||e.ownerAddress||e.owner||"",token0:e.token0Symbol||n(e.token0)||n(e.tokenA)||e.tokenSymbol0||"",token1:e.token1Symbol||n(e.token1)||n(e.tokenB)||e.tokenSymbol1||"",feeTier:e.feeTier||e.fee||e.feeAmount||0,tickLower:e.tickLower||e.lowerTick||0,tickUpper:e.tickUpper||e.upperTick||0,liquidity:String(e.liquidity||e.liquidityAmount||"0"),amount0:String(e.amount0||e.amountA||"0"),amount1:String(e.amount1||e.amountB||"0"),feeAmount0:String(e.feeAmount0||e.feesA||"0"),feeAmount1:String(e.feeAmount1||e.feesB||"0"),...e.createdAt&&{createdAt:new Date(e.createdAt)},...e.updatedAt&&{updatedAt:new Date(e.updatedAt)}}}parseTokenFlexible(e){try{return Ho(e)}catch(t){if(t instanceof Error&&t.message?.includes("Plain token string"))return this.logger.debug("Using default TokenClassKey for simple token symbol",{token:e}),{collection:"Token",category:"Unit",type:e,additionalKey:"none"};throw t}}async getUserLiquidityPositions(e,n=10,o,i){try{this.logger.debug("Fetching user liquidity positions",{ownerAddress:e,limit:n,bookmark:o});const r=`${this.galaChainBaseUrl}/api/asset/dexv3-contract/GetUserPositions`,a={user:e,limit:n,bookmark:o||""};this.logger.debug("Sending position query request",{endpoint:r,payload:a});const s=await t.post(r,a,{headers:{"Content-Type":"application/json",Accept:"application/json"}});if(200!==s.status||1!==s.data?.Status)return this.logger.warn("Unexpected API response status",{httpStatus:s.status,apiStatus:s.data?.Status}),{items:[]};const c=s.data.Data||{},l=c.positions||[],u=c.nextBookMark,d=l.filter(e=>null!=e&&"object"==typeof e&&("positionId"in e||"id"in e)).map(t=>this.normalizePositionResponse(t,e));let h;this.logger.debug("Retrieved liquidity positions",{count:d.length,hasNextBookmark:!!u,nextBookmark:u}),i?.withPrices&&d.length>0&&(h=await this.fetchPositionPrices(d));const g={items:d};return void 0!==u&&(g.nextBookmark=u),void 0!==h&&(g.prices=h),g}catch(t){if(t&&"object"==typeof t&&"response"in t){const n=t;this.logger.error("Position query failed with HTTP error",{status:n.response?.status,statusText:n.response?.statusText,data:n.response?.data,endpoint:this.galaChainBaseUrl,ownerAddress:e})}this.handleGSwapError("Failed to fetch user liquidity positions",O,t)}}async getAllSwapUserLiquidityPositions(e,t){try{this.logger.debug("Fetching all user liquidity positions (auto-paginated)",{ownerAddress:e});const n=async t=>{const n=await this.getUserLiquidityPositions(e,Jo,t,void 0);return{items:n.items,nextBookmark:n.nextBookmark}},o=await async function(e,t={}){const{maxPages:n=1e4,logger:o,pageSize:i=20}=t,r=[];let a,s=0;for(;s<n;){o&&o.debug(`Auto-pagination (bookmark): fetching page ${s+1} with pageSize ${i}`,{bookmark:a});const t=await e(a,i);let n,c,l;if(Array.isArray(t))n=t,c=void 0,l=!1;else{if(!t||"object"!=typeof t||!("items"in t)){o&&o.warn("Auto-pagination (bookmark): received invalid result structure, stopping");break}n=t.items,c=t.nextBookmark,l=!0}if(!Array.isArray(n)){o&&o.warn("Auto-pagination (bookmark): received invalid items array, stopping");break}if(0===n.length){o&&o.debug(`Auto-pagination (bookmark): no items returned on page ${s+1}, exiting loop`);break}r.push(...n),s++,o&&o.debug(`Auto-pagination (bookmark): page ${s} returned ${n.length} items`,{hasNextBookmark:!!c,format:l?"BookmarkPaginationResult":"legacy-array"});const u=n.length<i;if(l&&(""===c||void 0===c)){o&&o.debug("Auto-pagination (bookmark): no nextBookmark returned, reached end of results",{nextBookmark:""===c?"(empty string)":"(undefined)"});break}if(u){o&&o.debug("Auto-pagination (bookmark): received fewer items than limit, reached last page",{received:n.length,pageSize:i,format:l?"BookmarkPaginationResult":"legacy-array"});break}a=c}return s>=n&&o&&o.warn(`Auto-pagination (bookmark): exceeded maxPages limit of ${n}, stopping iteration`),o&&o.debug(`Auto-pagination (bookmark): completed with total items: ${r.length}`,{pageCount:s}),{items:r,total:r.length}}(n,{maxPages:1e4,logger:this.logger,pageSize:Jo}),i=o.items;if(this.logger.debug("Fetched all user liquidity positions",{ownerAddress:e,totalPositions:i.length}),t?.withPrices&&i.length>0){return{items:i,prices:await this.fetchPositionPrices(i)}}return i}catch(t){this.handleGSwapError("Failed to fetch all user liquidity positions",O,t,{ownerAddress:e})}}async getLiquidityPosition(e,t){try{this.logger.debug("Fetching liquidity position",{ownerAddress:e,position:t});const{gswapToken0:n,gswapToken1:o}=this.convertTokenPair(t.token0,t.token1),i=(await this.gatewayClient.getPositions({owner:e,tokenA:n,tokenB:o,feeTier:t.fee})).positions.find(e=>e.tickLower===t.tickLower&&e.tickUpper===t.tickUpper);if(!i||"object"!=typeof i||!("positionId"in i)&&!("id"in i))throw new Error("Invalid position data returned from API");const r=this.normalizePositionResponse(i,e);return this.logger.debug("Retrieved liquidity position",{positionId:r.positionId}),r}catch(e){this.handleGSwapError("Failed to fetch liquidity position",O,e)}}async getLiquidityPositionById(e,t,n,o,i,r,a){try{let s;this.logger.debug("Fetching liquidity position by ID",{ownerAddress:e,positionId:t,hasToken0:!!n,hasToken1:!!o,hasFee:!!i,hasTickLower:void 0!==r,hasTickUpper:void 0!==a});let c=null;const l=5,u=2e3;for(let d=1;d<=l;d++)try{if(n&&o&&void 0!==i&&void 0!==r&&void 0!==a)try{this.logger.debug("Attempting compound key lookup",{ownerAddress:e,token0:n,token1:o,feeTier:i,tickLower:r,tickUpper:a});if(s=(await this.gatewayClient.getPositionById(e,n,o,i,r,a,t)).Data,s&&"object"==typeof s&&("positionId"in s||"id"in s)){this.logger.debug("Successfully fetched position via compound key",{attempt:d,positionId:t});break}throw new Error("Invalid position data from compound key lookup")}catch(e){this.logger.debug("Compound key lookup failed, trying fallback",{attempt:d,error:e instanceof Error?e.message:e})}try{if(s=(await this.gatewayClient.getPositionById(t)).Data,s&&"object"==typeof s&&("positionId"in s||"id"in s)){this.logger.debug("Successfully fetched position on attempt",{attempt:d,positionId:t});break}throw new Error("Invalid position data from direct lookup")}catch(n){this.logger.debug("Direct position lookup failed, trying fallback via GetUserPositions",{attempt:d,positionId:t,error:n instanceof Error?n.message:n});const o=await this.getUserLiquidityPositions(e,10);if(Array.isArray(o.items)&&o.items.length>0){const e=o.items.find(e=>(e.positionId||e.id||"").toLowerCase()===t.toLowerCase());if(e){s=e,this.logger.debug("Found position via fallback (GetUserPositions)",{attempt:d,positionId:t});break}}if(c=new Error("Position not found in owner positions"),d<l){this.logger.warn("Fallback query did not find position, retrying",{attempt:d,positionId:t,ownerAddress:e,foundCount:Array.isArray(o?.items)?o.items.length:0}),await new Promise(e=>setTimeout(e,u));continue}}}catch(e){if(d<l){this.logger.warn("Error fetching position, retrying",{attempt:d,positionId:t,error:e instanceof Error?e.message:e}),await new Promise(e=>setTimeout(e,u));continue}c=e instanceof Error?e:new Error(String(e))}if(!s||"object"!=typeof s||!("positionId"in s)&&!("id"in s))throw this.logger.error("Invalid position data returned from API after retries",{positionId:t,resultType:typeof s,resultKeys:s?Object.keys(s):"null",resultValue:JSON.stringify(s),lastError:c?.message}),c||new Error("Invalid position data returned from API");const d=this.normalizePositionResponse(s,e);return this.logger.debug("Retrieved liquidity position by ID",{positionId:d.positionId}),d}catch(e){this.handleGSwapError("Failed to fetch liquidity position by ID",O,e)}}async fetchSwapPositionDirect(e){try{this.logger.debug("Fetching swap position via direct compound key",{token0:e.token0,token1:e.token1,fee:e.fee,owner:e.owner});const t="string"==typeof e.token0?this.parseTokenFlexible(e.token0):e.token0,n={token0:t,token1:"string"==typeof e.token1?this.parseTokenFlexible(e.token1):e.token1,fee:e.fee,tickLower:e.tickLower,tickUpper:e.tickUpper,owner:e.owner},o=`${this.galaChainBaseUrl}/api/asset/dexv3-contract/GetPositions`;this.logger.debug("Calling position endpoint via compound key",{url:o});const i=await fetch(o,{method:"POST",headers:{"Content-Type":"application/json",Accept:"application/json"},body:JSON.stringify(n)});if(!i.ok){if(404===i.status)throw new Error("Position not found (HTTP 404): No position exists for this compound key");if(400===i.status)throw new Error("Invalid parameters (HTTP 400): Check token format, fee (500|3000|10000), and tick ranges");throw new Error(`HTTP ${i.status}: ${i.statusText}`)}const r=await i.json();if(1!==r.Status||!r.Data)throw new Error(`Position not found: ${r.Message||"API returned no position data"}`);const a=this.normalizePositionResponse(r.Data,e.owner);return this.logger.debug("Retrieved swap position via compound key",{positionId:a.positionId,token0:a.token0,token1:a.token1}),a}catch(e){this.handleGSwapError("Failed to fetch swap position via compound key",O,e)}}async estimateRemoveLiquidity(e){try{this.logger.debug("Estimating liquidity removal",{token0:e.token0,token1:e.token1});const{gswapToken0:t,gswapToken1:n}=this.convertTokenPair(e.token0,e.token1),o=await this.gatewayClient.getRemoveLiquidityEstimation({tokenA:t,tokenB:n,fee:e.fee,liquidity:e.liquidity,tickLower:e.tickLower,tickUpper:e.tickUpper});return this.logger.debug("Estimated removal",{result:o}),o}catch(e){this.handleGSwapError("Failed to estimate liquidity removal",O,e)}}async addLiquidityByPrice(e){try{if(!this.privateKey)throw new Error("GSwapService not initialized with signing capability (privateKey required)");this.logger.debug("Adding liquidity by price",{token0:e.token0,token1:e.token1,priceRange:`${e.minPrice}-${e.maxPrice}`});const{gswapToken0:t,gswapToken1:n}=this.convertTokenPair(e.token0,e.token1);await this.ensureWebSocketConnected(),this.logger.debug("Converting price range to ticks",{token0:e.token0,token1:e.token1,minPrice:e.minPrice,maxPrice:e.maxPrice,fee:e.fee});const o=Ho(t),i=Ho(n),a=(await this.gatewayClient.getPoolData({token0:o,token1:i,fee:e.fee})).tickSpacing;this.logger.debug("Retrieved tick spacing from pool",{tickSpacing:a,fee:e.fee});const s=new r(e.minPrice),c=new r(e.maxPrice),l=Math.floor(Jn(s)),u=Math.ceil(Jn(c)),d=Math.floor(l/a)*a,h=Math.ceil(u/a)*a;this.logger.debug("Converted price range to ticks",{minPrice:e.minPrice,maxPrice:e.maxPrice,tickLower:d,tickUpper:h,tickSpacing:a});const g=this.getWalletAddress();if(!g)throw new Error("GSwapService: No wallet address available - cannot create position");const p="string"==typeof e.token0?Ho(e.token0):e.token0,m="string"==typeof e.token1?Ho(e.token1):e.token1;this.logger.debug("Sending AddLiquidity by price to bundler",{fee:e.fee,tickRange:`${d}-${h}`,walletAddress:g});const f=await this.sendAddLiquidityToBundler({token0:p,token1:m,fee:e.fee,tickLower:d,tickUpper:h,amount0Desired:e.amount0Desired,amount1Desired:e.amount1Desired,amount0Min:e.amount0Min||"0",amount1Min:e.amount1Min||"0",owner:g}),y={transactionId:f};if(y.positionId&&f){this.logger.debug("Position ID returned directly from backend",{transactionId:f,positionId:y.positionId}),await this.ensureWebSocketConnected();const e=await this.webSocketService.waitForTransaction(f);this.logger.debug("Liquidity transaction confirmed on-chain",{transactionId:f,status:e.status});const t=this.getWalletAddress();if(t&&y.positionId)try{const n=await this.getLiquidityPositionById(t,y.positionId),{createdAt:o,updatedAt:i,...r}=n,a=o instanceof Date?o.getTime():"number"==typeof o?o:void 0,s={...y,...r,positionId:y.positionId,status:e.status,transactionId:e.transactionId,timestamp:new Date(e.timestamp),wait:async e=>{await this.webSocketService.waitForTransaction(f)}};return void 0!==a&&(s.createdAt=a),s}catch(t){return{...y,positionId:y.positionId,status:e.status,transactionId:e.transactionId,timestamp:new Date(e.timestamp),wait:async e=>{await this.webSocketService.waitForTransaction(f)}}}}if(f){this.logger.debug("Monitoring liquidity transaction (discovery mode)",{transactionId:f}),await this.ensureWebSocketConnected();const t=await this.webSocketService.waitForTransaction(f);let n;this.logger.debug("Liquidity transaction confirmed on-chain",{transactionId:f,status:t.status});let o=null;await new Promise(e=>setTimeout(e,2e3));try{const t=this.getWalletAddress();if(!t)throw new Error("No wallet address available");const i=(await this.getUserLiquidityPositions(t,10)).items;if(i&&i.length>0){const t=e.token0.split("|")[0].toUpperCase(),r=e.token1.split("|")[0].toUpperCase(),a=[];for(const n of i){if(!n||!n.positionId)continue;const o=n.token0?.toUpperCase(),i=n.token1?.toUpperCase();if(!o||!i)continue;const s=o===t&&i===r||o===r&&i===t,c=n.feeTier===e.fee;s&&c&&a.push(n)}a.length>0&&(o=a[a.length-1],n=o.positionId,this.logger.debug("Found newly created position",{positionId:n,expectedTokens:`${e.token0}/${e.token1}`,expectedFee:e.fee,positionCount:i.length}))}}catch(e){this.logger.debug("Error waiting for position indexing",{error:e instanceof Error?e.message:String(e)})}let i=o;if(n)try{i=await this.getLiquidityPositionById(g,n)}catch(e){}return{...y,...i,positionId:n,status:t.status,transactionId:t.transactionId,timestamp:new Date(t.timestamp),wait:async e=>{await this.webSocketService.waitForTransaction(f)}}}return this.logger.warn("No transaction ID in liquidity result, cannot confirm position creation"),y}catch(e){Error,this.handleGSwapError("Failed to add liquidity by price",O,e)}}async addSwapLiquidityByTicks(e){try{if(!this.privateKey)throw new Error("GSwapService not initialized with signing capability (privateKey required)");const t=this.getWalletAddress();if(!t)throw new Error("GSwapService: No wallet address available - cannot create position");this.logger.debug("Adding liquidity by ticks with direct bundler",{token0:e.token0,token1:e.token1,fee:e.fee,walletAddress:t,tickRange:`${e.tickLower}-${e.tickUpper}`});const n="string"==typeof e.token0?Ho(e.token0):e.token0,o="string"==typeof e.token1?Ho(e.token1):e.token1;await this.ensureWebSocketConnected();const i=await this.sendAddLiquidityToBundler({token0:n,token1:o,fee:e.fee,tickLower:e.tickLower,tickUpper:e.tickUpper,amount0Desired:e.amount0Desired,amount1Desired:e.amount1Desired,amount0Min:e.amount0Min||"0",amount1Min:e.amount1Min||"0",owner:t});this.logger.info("Liquidity transaction submitted to bundler",{transactionId:i});const r=this.webSocketService.waitForTransaction(i),a={transactionId:i};if(a.positionId&&i){this.logger.info("Position ID returned directly from backend",{transactionId:i,positionId:a.positionId});const e=await r;this.logger.debug("Liquidity transaction confirmed on-chain",{transactionId:i,status:e.status});const t=this.getWalletAddress();if(t&&a.positionId)try{this.logger.debug("Fetching full position details",{positionId:a.positionId});const n=await this.getLiquidityPositionById(t,a.positionId);this.logger.debug("Fetched full position data",{positionId:n.positionId,liquidity:n.liquidity,amount0:n.amount0,amount1:n.amount1});const{createdAt:o,updatedAt:r,...s}=n,c=o instanceof Date?o.getTime():"number"==typeof o?o:void 0,l={...a,...s,positionId:a.positionId,status:e.status,transactionId:e.transactionId,timestamp:new Date(e.timestamp),wait:async e=>{await this.webSocketService.waitForTransaction(i)}};return void 0!==c&&(l.createdAt=c),l}catch(t){return this.logger.warn("Could not fetch full position details",{positionId:a.positionId,error:t instanceof Error?t.message:String(t)}),{...a,positionId:a.positionId,status:e.status,transactionId:e.transactionId,timestamp:new Date(e.timestamp),wait:async e=>{await this.webSocketService.waitForTransaction(i)}}}}if(i){this.logger.debug("Monitoring liquidity transaction (discovery mode)",{transactionId:i});const n=await r;let o;this.logger.debug("Liquidity transaction confirmed on-chain",{transactionId:i,status:n.status});let s=null;const c="string"==typeof e.token0?e.token0:e.token0.type,l="string"==typeof e.token1?e.token1:e.token1.type;this.logger.debug("Waiting for position indexing after WebSocket confirmation"),this.logger.debug("Looking for matching position",{token0:c,token1:l,fee:e.fee});try{const t=this.getWalletAddress();if(!t)throw new Error("No wallet address available");this.logger.debug("Fetching positions from API",{walletAddress:t,pageSize:Jo});const n=3,i=5e3,r=3e3;let a=[];for(let c=1;c<=n;c++){const l=1===c?i:r;this.logger.debug("Position discovery attempt",{attempt:c,maxAttempts:n,delayMs:l}),await new Promise(e=>setTimeout(e,l)),this.logger.debug("Querying positions from API",{attempt:c,pageSize:Jo});if(a=(await this.getUserLiquidityPositions(t,Jo)).items,this.logger.debug("Got positions from API",{count:a?.length||0}),a&&a.length>0){const t=("string"==typeof e.token0?e.token0.split("|")[0]:e.token0.collection).toUpperCase(),n=("string"==typeof e.token1?e.token1.split("|")[0]:e.token1.collection).toUpperCase(),i=[];for(const o of a){if(!o||!o.positionId)continue;const r=o.token0?.toUpperCase(),a=o.token1?.toUpperCase();if(!r||!a){this.logger.debug("Skipping position with empty tokens",{positionId:o.positionId});continue}const s=r===t&&a===n||r===n&&a===t,c=o.feeTier===e.fee;this.logger.debug("Checking position against target",{positionId:o.positionId,tokens:`${r}/${a}`,tokensMatch:s,fee:o.feeTier,feeMatches:c}),s&&c&&i.push(o)}if(i.length>0){s=i[i.length-1],o=s.positionId,this.logger.info("Found newly created position",{positionId:o,liquidity:s.liquidity,amount0:s.amount0,amount1:s.amount1,fee:s.feeTier}),this.logger.debug("Found newly created position",{positionId:o,expectedTokens:`${e.token0}/${e.token1}`,expectedFee:e.fee,positionCount:a.length});break}this.logger.debug("No matching position found in this attempt")}else this.logger.debug("No positions returned from API in this attempt")}}catch(e){this.logger.error("Error fetching positions during discovery",{error:e instanceof Error?e.message:String(e)}),this.logger.debug("Error waiting for position indexing",{error:e instanceof Error?e.message:String(e)})}this.logger.debug("Position discovery complete",{positionId:o||"not found",found:!!o}),this.logger.debug("Matched position data",{positionId:s?.positionId,liquidity:s?.liquidity,amount0:s?.amount0,amount1:s?.amount1,feeAmount0:s?.feeAmount0,feeAmount1:s?.feeAmount1,token0:s?.token0,token1:s?.token1,feeTier:s?.feeTier});let u=s;if(o)try{this.logger.debug("Fetching full position details",{positionId:o}),u=await this.getLiquidityPositionById(t,o),this.logger.debug("Fetched full position data",{positionId:u.positionId,liquidity:u.liquidity,amount0:u.amount0,amount1:u.amount1,feeAmount0:u.feeAmount0,feeAmount1:u.feeAmount1})}catch(e){this.logger.warn("Could not fetch full position details, using discovered data",{positionId:o,error:e instanceof Error?e.message:String(e)})}return{...a,...u,positionId:o,status:n.status,transactionId:n.transactionId,timestamp:new Date(n.timestamp),wait:async e=>{await this.webSocketService.waitForTransaction(i)}}}return this.logger.warn("No transaction ID in liquidity result, cannot confirm position creation"),a}catch(e){this.handleGSwapError("Failed to add liquidity by ticks",O,e)}}async monitorBundlerTransaction(e,t,n="bundler"){let o;try{const i=await t;o={status:i.status,transactionId:i.transactionId||e,timestamp:i.timestamp||Date.now(),data:i.data},this.logger.debug(`${n} transaction confirmed on-chain`,{transactionId:e,status:o.status})}catch(t){return this.logger.warn(`WebSocket monitoring timeout for ${n} transaction, returning result with transaction ID`,{transactionId:e,error:t instanceof Error?t.message:String(t)}),{transactionId:e,status:"SUBMITTED",timestamp:new Date,wait:async t=>{try{await this.webSocketService.waitForTransaction(e)}catch{this.logger.debug("Explicit wait also timed out",{transactionId:e})}}}}return{transactionId:o.transactionId,status:o.status,timestamp:new Date(o.timestamp),wait:async t=>{await this.webSocketService.waitForTransaction(e)}}}async removeLiquidity(e){try{if(!this.privateKey)throw new Error("Private key not available for bundler-direct operations");this.logger.debug("Removing liquidity via bundler",{token0:e.token0,token1:e.token1,liquidity:e.liquidity});try{const t=parseFloat(e.liquidity);if(isNaN(t))throw new Error(`Invalid liquidity value: "${e.liquidity}". Must be a valid number. Position ID: ${e.positionId||"unknown"}`);if(0===t)throw new Error(`Cannot remove zero liquidity from position. This would waste gas fees without any effect. Position ID: ${e.positionId||"unknown"}`)}catch(e){if(e instanceof Error&&e.message.includes("Cannot remove zero liquidity"))throw e;if(e instanceof Error&&e.message.includes("Invalid liquidity value"))throw e;throw e}const t="string"==typeof e.token0?Ho(e.token0):e.token0,n="string"==typeof e.token1?Ho(e.token1):e.token1;await this.ensureWebSocketConnected();const o=await this.sendRemoveLiquidityToBundler(e.tickLower,e.tickUpper,e.liquidity,t,n,e.fee,e.amount0Min||"0",e.amount1Min||"0",e.positionId||"");this.logger.debug("Liquidity removal submitted to bundler",{transactionId:o});const i=this.webSocketService.waitForTransaction(o);return this.monitorBundlerTransaction(o,i,"liquidity removal")}catch(e){this.handleGSwapError("Failed to remove liquidity",O,e)}}async collectPositionFees(e){try{if(!this.privateKey)throw new Error("Private key not available for bundler-direct operations");this.logger.debug("Collecting position fees via bundler",{token0:"string"==typeof e.token0?e.token0:e.token0.type,token1:"string"==typeof e.token1?e.token1:e.token1.type,tickLower:e.tickLower,tickUpper:e.tickUpper});const t="string"==typeof e.token0?Ho(e.token0):e.token0,n="string"==typeof e.token1?Ho(e.token1):e.token1;await this.ensureWebSocketConnected();const o=await this.sendCollectPositionFeesToBundler(t,n,e.fee,e.amount0Requested||"0",e.amount1Requested||"0",e.tickLower,e.tickUpper,e.positionId||"");this.logger.debug("Fee collection submitted to bundler",{transactionId:o});const i=this.webSocketService.waitForTransaction(o);return this.monitorBundlerTransaction(o,i,"fee collection")}catch(e){this.handleGSwapError("Failed to collect position fees",O,e)}}async getPoolData(e,t,n){try{this.logger.debug("Getting pool data",{tokenA:e,tokenB:t,feeTier:n});const{gswapToken0:o,gswapToken1:i}=this.convertTokenPair(e,t),a=Ho(o),s=Ho(i),c=await this.gatewayClient.getPoolData({token0:a,token1:s,fee:n}),l=this.calculatePriceFromSqrtPriceX96(new r(c.sqrtPrice));return{tokenA:e,tokenB:t,feeTier:n,liquidity:c.liquidity.toString(),sqrtPriceX96:c.sqrtPrice.toString(),tick:c.tick,feeGrowthGlobal0X128:c.feeGrowthGlobal0.toString(),feeGrowthGlobal1X128:c.feeGrowthGlobal1.toString(),currentPrice:l.toFixed()}}catch(e){this.handleGSwapError("Failed to get pool data",$,e)}}async calculateDexPoolSpotPrice(e,t,n){try{this.logger.debug("Calculating spot price",{tokenA:e,tokenB:t,feeTier:n});const o=await this.getPoolData(e,t,n),i=Zn(o.currentPrice);return{tokenA:e,tokenB:t,feeTier:n,price:i.toFixed(),invertedPrice:eo(i,!0),tick:o.tick,liquidity:o.liquidity}}catch(e){this.handleGSwapError("Failed to calculate spot price",$,e)}}async calculateOptimalPositionSize(e,t,n,o,i,s,c){try{this.logger.debug("Calculating optimal position size",{tokenA:e,tokenB:t,desiredAmount0:o,desiredAmount1:i});const l=(await this.getPoolData(e,t,n)).tick,u=a.tickToSqrtPrice(s),d=a.tickToSqrtPrice(l),h=a.tickToSqrtPrice(c),g=a.getLiquidityForAmounts(new r(o),new r(i),u,d,h),p=a.getAmountsForLiquidity(g,d,u,h),m=p[0],f=p[1],y=new r(o),w=new r(i);return{amount0:m.toFixed(),amount1:f.toFixed(),liquidity:g.toFixed(),ratio:m.dividedBy(f).toFixed(),utilizationPercent:{amount0:m.dividedBy(y).multipliedBy(100).toFixed(2),amount1:f.dividedBy(w).multipliedBy(100).toFixed(2)}}}catch(e){this.handleGSwapError("Failed to calculate optimal position size",O,e)}}async validatePositionParameters(e,t,n,o,i,a,s){const c=[],l=[];try{this.logger.debug("Validating position parameters",{tokenA:e,tokenB:t,tickLower:o,tickUpper:i});const u=[500,3e3,1e4];u.includes(n)||c.push(`Invalid fee tier: ${n}. Must be one of: ${u.join(", ")}`);const d=this.getTickSpacing(n);let h;o%d!==0&&c.push(`tickLower must be multiple of ${d}`),i%d!==0&&c.push(`tickUpper must be multiple of ${d}`),o>=i&&c.push(`tickLower (${o}) must be less than tickUpper (${i})`);try{h=await this.getPoolData(e,t,n)}catch{return c.push(`Pool not found for ${e}/${t} at fee tier ${n}`),{valid:!1,errors:c,warnings:l,gasEstimate:0}}const g=new r(a),p=new r(s);if(g.isNaN()||p.isNaN())c.push("Amounts must be valid numbers");else try{to(g,p)}catch(e){c.push(`Liquidity amounts must be non-negative: ${e.message}`)}const m=h.tick;(m<o||m>i)&&l.push("Position is out of current price range - will not earn fees until price moves into range");new r(h.liquidity||"0").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){const t=e;return c.includes(t?.message||"")||c.push(`Validation failed: ${t?.message||String(e)}`),{valid:!1,errors:c,warnings:l,gasEstimate:0}}}async calculateTicksForPrice(e,t,n,o,i){try{this.logger.debug("Calculating ticks for price range",{tokenA:e,tokenB:t,minPrice:n,maxPrice:o});const a=this.getTickSpacing(i),s=new r(n),c=new r(o);if(s.gte(c))throw new Error("minPrice must be less than maxPrice");const l=Math.floor(Jn(s)),u=Math.ceil(Jn(c)),d=Math.floor(l/a)*a,h=Math.ceil(u/a)*a,g=Math.pow(1.0001,d),p=Math.pow(1.0001,h),m=new r(g),f=new r(p);return{tokenA:e,tokenB:t,feeTier:i,tickLower:d,tickUpper:h,tickSpacing:a,requestedMinPrice:n,requestedMaxPrice:o,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",O,e)}}async calculatePriceForTicks(e,t,n,o){try{this.logger.debug("Calculating price for ticks",{tokenA:e,tokenB:t,tickLower:n,tickUpper:o});const i=Math.pow(1.0001,n),a=Math.pow(1.0001,o);let s;try{s=(await this.getPoolData(e,t,3e3)).currentPrice}catch{}const c=new r(i),l=new r(a),u={tokenA:e,tokenB:t,tickLower:n,tickUpper:o,minPrice:c.toFixed(8),maxPrice:l.toFixed(8),priceRange:`${c.toFixed(4)} - ${l.toFixed(4)}`,tickSpread:o-n};return void 0!==s&&(u.currentPrice=s),u}catch(e){this.handleGSwapError("Failed to calculate price for ticks",O,e)}}calculateExecutionPrice(e,t){try{const n=new r(e),o=new r(t);return n.isZero()?"0":o.dividedBy(n).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 r(2).pow(96);return e.dividedBy(t).pow(2)}catch{return new r(0)}}calculatePriceFromSqrtPriceDecimal(e){try{return e.pow(2)}catch{return new r(0)}}async getPoolSlot0(e,n,o){try{this.logger.debug("Fetching pool slot0 data",{token0:e,token1:n,fee:o});const i="string"==typeof e?Ho(e):e,r="string"==typeof n?Ho(n):n,a=`${this.galaChainBaseUrl}/api/asset/dexv3-contract/GetSlot0`,s=await t.post(a,{token0:i,token1:r,fee:o},{headers:{"Content-Type":"application/json",Accept:"application/json"}});if(200!==s.status||1!==s.data?.Status)throw this.logger.warn("Unexpected GetSlot0 API response",{httpStatus:s.status,apiStatus:s.data?.Status}),new $("GetSlot0 API returned unexpected status",{httpStatus:s.status,apiStatus:s.data?.Status});const c=s.data.Data||{},l={sqrtPrice:c.sqrtPrice||"0",tick:c.tick||0,liquidity:c.liquidity||"0",grossPoolLiquidity:c.grossPoolLiquidity||"0"};return this.logger.debug("Retrieved pool slot0 data",{sqrtPrice:l.sqrtPrice,tick:l.tick,liquidity:l.liquidity}),l}catch(t){this.handleGSwapError("Failed to fetch pool slot0 data",$,t,{token0:e,token1:n,fee:o})}}async getPositionCurrentPrice(e){try{this.logger.debug("Fetching position current price",{token0:e.token0,token1:e.token1,feeTier:e.feeTier});const t=await this.getPoolSlot0(e.token0,e.token1,e.feeTier),n=new r(t.sqrtPrice),o={price:this.calculatePriceFromSqrtPriceDecimal(n).toFixed(18),sqrtPrice:t.sqrtPrice,tick:t.tick,liquidity:t.liquidity};return this.logger.debug("Calculated position current price",{price:o.price,tick:o.tick}),o}catch(t){this.handleGSwapError("Failed to fetch position current price",$,t,{token0:e.token0,token1:e.token1})}}calculateLiquidityFromAmount0(e,t,n){try{const o=Yn(t),i=Yn(n);return a.liquidity0(e,o,i)}catch{return new r(0)}}calculateLiquidityFromAmount1(e,t,n){try{const o=Yn(t),i=Yn(n);return a.liquidity1(e,o,i)}catch{return new r(0)}}calculateAmount0FromLiquidity(e,t,n){try{const o=Yn(t),i=Yn(n);return a.getAmount0Delta(o,i,e)}catch{return new r(0)}}calculateAmount1FromLiquidity(e,t,n){try{const o=Yn(t),i=Yn(n);return a.getAmount1Delta(o,i,e)}catch{return new r(0)}}convertTokenPair(e,t){return{gswapToken0:this.tokenConverter.toLaunchpadFormat(e),gswapToken1:this.tokenConverter.toLaunchpadFormat(t)}}async sendAddLiquidityToBundler(e){if(!this.privateKey)throw new Error("GSwapService: AddLiquidity requires wallet (full-access mode)");if(!this.bundlerBaseUrl)throw new Error("GSwapService: Bundler URL not configured");try{this.logger.debug("Sending AddLiquidity to bundler",{token0:e.token0.type,token1:e.token1.type,fee:e.fee,tickRange:`${e.tickLower}-${e.tickUpper}`});const o=`galaswap - operation - ${c.v4()}-${Date.now()}-${e.owner}`,i={token0:e.token0,token1:e.token1,fee:e.fee,owner:e.owner,tickLower:e.tickLower,tickUpper:e.tickUpper,amount0Desired:e.amount0Desired,amount1Desired:e.amount1Desired,amount0Min:e.amount0Min,amount1Min:e.amount1Min,positionId:"",uniqueKey:o},r=new n.ethers.Wallet(this.privateKey),a={AddLiquidity:[{name:"token0",type:"token0"},{name:"token1",type:"token1"},{name:"fee",type:"int256"},{name:"owner",type:"string"},{name:"tickLower",type:"int256"},{name:"tickUpper",type:"int256"},{name:"amount0Desired",type:"string"},{name:"amount1Desired",type:"string"},{name:"amount0Min",type:"string"},{name:"amount1Min",type:"string"},{name:"positionId",type:"string"},{name:"uniqueKey",type:"string"}],token0:[{name:"additionalKey",type:"string"},{name:"category",type:"string"},{name:"collection",type:"string"},{name:"type",type:"string"}],token1:[{name:"additionalKey",type:"string"},{name:"category",type:"string"},{name:"collection",type:"string"},{name:"type",type:"string"}]},s={name:"ethereum",chainId:1},l=this.calculatePersonalSignPrefix(i),u={...i,prefix:l},d=await r.signTypedData(s,a,u),h={...u,signature:d,types:a,domain:s};this.logger.debug("AddLiquidity DTO signed with manual types",{signature:h.signature?.substring(0,20)+"...",prefix:h.prefix,tickLower:i.tickLower,tickUpper:i.tickUpper});const g=this.buildLiquidityStringsInstructions(e.token0,e.token1,e.fee,e.owner),p=t.create({baseURL:this.bundlerBaseUrl,timeout:3e4}),m=await p.post("/bundle",{method:"AddLiquidity",signedDto:h,stringsInstructions:g}),f=m.data?.data||m.data?.transactionId||m.data?.id;if(!f)throw this.logger.error("Bundler response structure",{status:m.status,data:m.data,dataType:typeof m.data}),new Error(`Bundler response does not contain transaction ID. Response: ${JSON.stringify(m.data)}`);return this.logger.debug("AddLiquidity transaction sent to bundler",{transactionId:f}),f}catch(e){throw this.logger.error("Failed to send AddLiquidity to bundler",e),e}}async sendRemoveLiquidityToBundler(e,o,i,r,a,s,l,u,d){try{if(!this.bundlerBaseUrl)throw new Error("GSwapService: Bundler URL not configured");const h=new n.ethers.Wallet(this.privateKey),g=await h.getAddress(),p=`galaswap - operation - ${c.v4()}-${Date.now()}-${g}`,m={tickLower:e,tickUpper:o,amount:i,token0:r,token1:a,fee:s,amount0Min:l,amount1Min:u,positionId:d,uniqueKey:p},f={RemoveLiquidity:[{name:"tickLower",type:"int256"},{name:"tickUpper",type:"int256"},{name:"amount",type:"string"},{name:"token0",type:"token0"},{name:"token1",type:"token1"},{name:"fee",type:"int256"},{name:"amount0Min",type:"string"},{name:"amount1Min",type:"string"},{name:"positionId",type:"string"},{name:"uniqueKey",type:"string"}],token0:[{name:"additionalKey",type:"string"},{name:"category",type:"string"},{name:"collection",type:"string"},{name:"type",type:"string"}],token1:[{name:"additionalKey",type:"string"},{name:"category",type:"string"},{name:"collection",type:"string"},{name:"type",type:"string"}]},y={name:"ethereum",chainId:1},w=this.calculatePersonalSignPrefix(m),k={...m,prefix:w},v=await h.signTypedData(y,f,k),T={...k,signature:v,types:f,domain:y},S=this.buildLiquidityStringsInstructions(r,a,s,g);this.logger.debug("Submitting RemoveLiquidity to bundler",{tickLower:e,tickUpper:o,amount:i,fee:s,positionId:d,transactionId:p});const b=t.create({baseURL:this.bundlerBaseUrl,timeout:3e4}),A=await b.post("/bundle",{method:"RemoveLiquidity",signedDto:T,stringsInstructions:S}),E=A.data?.data||A.data?.transactionId||A.data?.id;if(!E)throw this.logger.error("Bundler response structure",{status:A.status,data:A.data,dataType:typeof A.data}),new Error(`Bundler response does not contain transaction ID. Response: ${JSON.stringify(A.data)}`);return this.logger.info("RemoveLiquidity transaction sent to bundler",{transactionId:E}),E}catch(e){throw this.logger.error("Failed to send RemoveLiquidity to bundler",e),e}}async sendCollectPositionFeesToBundler(e,o,i,r,a,s,l,u){try{if(!this.bundlerBaseUrl)throw new Error("GSwapService: Bundler URL not configured");const d=new n.ethers.Wallet(this.privateKey),h=await d.getAddress(),g=`galaswap - operation - ${c.v4()}-${Date.now()}-${h}`,p={token0:e,token1:o,fee:i,amount0Requested:r,amount1Requested:a,tickLower:s,tickUpper:l,positionId:u,uniqueKey:g},m={CollectPositionFees:[{name:"token0",type:"token0"},{name:"token1",type:"token1"},{name:"fee",type:"int256"},{name:"amount0Requested",type:"string"},{name:"amount1Requested",type:"string"},{name:"tickLower",type:"int256"},{name:"tickUpper",type:"int256"},{name:"positionId",type:"string"},{name:"uniqueKey",type:"string"}],token0:[{name:"additionalKey",type:"string"},{name:"category",type:"string"},{name:"collection",type:"string"},{name:"type",type:"string"}],token1:[{name:"additionalKey",type:"string"},{name:"category",type:"string"},{name:"collection",type:"string"},{name:"type",type:"string"}]},f={name:"ethereum",chainId:1},y=this.calculatePersonalSignPrefix(p),w={...p,prefix:y},k=await d.signTypedData(f,m,w),v={...w,signature:k,types:m,domain:f},T=this.buildLiquidityStringsInstructions(e,o,i,h);this.logger.debug("Submitting CollectPositionFees to bundler",{fee:i,amount0Requested:r,amount1Requested:a,tickLower:s,tickUpper:l,positionId:u,transactionId:g});const S=t.create({baseURL:this.bundlerBaseUrl,timeout:3e4}),b=await S.post("/bundle",{method:"CollectPositionFees",signedDto:v,stringsInstructions:T}),A=b.data?.data||b.data?.transactionId||b.data?.id;if(!A)throw this.logger.error("Bundler response structure",{status:b.status,data:b.data,dataType:typeof b.data}),new Error(`Bundler response does not contain transaction ID. Response: ${JSON.stringify(b.data)}`);return this.logger.info("CollectPositionFees transaction sent to bundler",{transactionId:A}),A}catch(e){throw this.logger.error("Failed to send CollectPositionFees to bundler",e),e}}async sendSwapToBundler(e){if(!this.privateKey)throw new Error("GSwapService: Swap requires wallet (full-access mode)");if(!this.bundlerBaseUrl)throw new Error("GSwapService: Bundler URL not configured");const o=[500,3e3,1e4];if(!o.includes(e.feeTier))throw new Error(`GSwapService: Invalid fee tier ${e.feeTier}. Must be one of: ${o.join(", ")} (basis points)`);try{this.logger.debug("Sending Swap to bundler",{fromToken:"string"==typeof e.fromToken?e.fromToken:e.fromToken.type,toToken:"string"==typeof e.toToken?e.toToken:e.toToken.type,inputAmount:e.inputAmount,minOutput:e.minOutput,feeTier:e.feeTier});let o=e.fromToken,i=e.toToken;"string"==typeof o&&(o=Ho(o)),"string"==typeof i&&(i=Ho(i));const a=`${o.collection}|${o.category}|${o.type}|${o.additionalKey}`,s=`${i.collection}|${i.category}|${i.type}|${i.additionalKey}`,l=a<s?[o,i,a,s]:[i,o,s,a],[u,d,h,g]=l,p="string"==typeof e.fromToken?Ho(e.fromToken):e.fromToken,m=`${p.collection}|${p.category}|${p.type}|${p.additionalKey}`,f=m===h,y=`galaswap - operation - ${c.v4()}-${Date.now()}-${e.walletAddress}`;let w;if(!e.currentSqrtPrice)throw new Error("GSwapService: currentSqrtPrice is required for sqrtPriceLimit calculation");const k=new r(e.currentSqrtPrice),v=e.slippageTolerance??.01;if(f){const e=new r(1).minus(v);w=k.multipliedBy(e).toString()}else{const e=new r(1).plus(v);w=k.multipliedBy(e).toString()}this.logger.debug("Calculated sqrtPriceLimit based on slippage tolerance",{currentSqrtPrice:e.currentSqrtPrice,slippageTolerance:100*v+"%",zeroForOne:f,sqrtPriceLimit:w,direction:f?"token0→token1 (downward price movement)":"token1→token0 (upward price movement)",reason:"sqrtPriceLimit sets price boundaries, amountOutMinimum provides volume protection"});const T={token0:u,token1:d,fee:e.feeTier,amount:new r(e.inputAmount).toFixed(),zeroForOne:f,sqrtPriceLimit:w,recipient:e.walletAddress,amountOutMinimum:new r(e.minOutput).multipliedBy(-1).toFixed(),uniqueKey:y};this.logger.info("🔄 SWAP DTO DETAILS (what we're sending to bundler)",{orderedToken0String:h,orderedToken1String:g,fromTokenStr:m,zeroForOne:f?`TRUE (${h} → ${g})`:`FALSE (${g} → ${h})`,inputAmount:e.inputAmount,expectedOutput:e.minOutput,slippageTolerance:100*(e.slippageTolerance||.01)+"%",currentSqrtPrice:e.currentSqrtPrice,swapDto:{amount:T.amount,zeroForOne:T.zeroForOne,sqrtPriceLimit:T.sqrtPriceLimit,amountOutMinimum:T.amountOutMinimum}});const S=new n.ethers.Wallet(this.privateKey),b={Swap:[{name:"token0",type:"token0"},{name:"token1",type:"token1"},{name:"fee",type:"int256"},{name:"amount",type:"string"},{name:"zeroForOne",type:"bool"},{name:"sqrtPriceLimit",type:"string"},{name:"recipient",type:"string"},{name:"amountOutMinimum",type:"string"},{name:"uniqueKey",type:"string"}],token0:[{name:"additionalKey",type:"string"},{name:"category",type:"string"},{name:"collection",type:"string"},{name:"type",type:"string"}],token1:[{name:"additionalKey",type:"string"},{name:"category",type:"string"},{name:"collection",type:"string"},{name:"type",type:"string"}]},A={name:"ethereum",chainId:1},E=this.calculatePersonalSignPrefix(T),I={...T,prefix:E},P=await S.signTypedData(A,b,I),N={...I,signature:P,types:b,domain:A};this.logger.debug("Swap DTO signed",{signature:N.signature?.substring(0,20)+"...",prefix:N.prefix,zeroForOne:T.zeroForOne});const D=this.buildLiquidityStringsInstructions(u,d,e.feeTier,e.walletAddress),F=t.create({baseURL:this.bundlerBaseUrl,timeout:3e4}),C=await F.post("/bundle",{method:"Swap",signedDto:N,stringsInstructions:D}),x=C.data?.data||C.data?.transactionId||C.data?.id;if(!x)throw this.logger.error("Bundler response structure",{status:C.status,data:C.data,dataType:typeof C.data}),new Error(`Bundler response does not contain transaction ID. Response: ${JSON.stringify(C.data)}`);return this.logger.debug("Swap transaction sent to bundler",{transactionId:x,inputAmount:e.inputAmount,minOutput:e.minOutput}),x}catch(e){throw this.logger.error("Failed to send Swap to bundler",e),e}}buildLiquidityStringsInstructions(e,t,n,o){const i=`$${e.collection}$${e.category}$${e.type}$${e.additionalKey}`,r=`$${t.collection}$${t.category}$${t.type}$${t.additionalKey}`,a=`$pool${i}${r}$${n}`;return[a,`$userPosition${o}`,`$tokenBalance${i}${o}`,`$tokenBalance${r}${o}`,`$tokenBalance${i}${a}`,`$tokenBalance${r}${a}`]}handleGSwapError(e,t,n,o){this.logger.error(e,n);const i=this.extractGSwapErrorCode(n),r=n,a=[`${e}: ${r?.message||String(n)}`,n];throw o&&("GSwapSwapError"===t.name&&o.transactionHash&&a.push(o.transactionHash),"GSwapPoolError"===t.name&&(o.tokenA&&a.push(o.tokenA),o.tokenB&&a.push(o.tokenB)),"GSwapAssetError"===t.name&&o.walletAddress&&a.push(o.walletAddress)),i&&a.push(i),new t(...a)}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()}calculatePersonalSignPrefix(e){return`Ethereum Signed Message:\n${JSON.stringify(e).length}${JSON.stringify(e)}`}}class ti{}ti.BASE_PRICE=1650667151e-14,ti.PRICE_SCALING_FACTOR=1166069e-12,ti.TRADING_FEE_FACTOR=.001,ti.GAS_FEE="1",ti.MIN_UNBONDING_FEE_FACTOR=0,ti.MAX_UNBONDING_FEE_FACTOR=.5,ti.NET_UNBONDING_FEE_FACTOR=.5,ti.DEFAULT_LAUNCHPAD_TOKEN_MAX_SUPPLY=1e7;class ni extends Tn{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 n=this.cache.get(e);if(this.cache.has(e)&&this.cache.delete(e),this.cache.size>=ni.MAX_CACHE_SIZE){const e=this.getLRUKey();null!==e&&this.cache.delete(e)}this.cache.set(e,{...n||{},...t,lastUpdated:Date.now()})}warmFromPoolData(e,t){const n=this.normalizeTokenName(e);this.updateCacheEntry(n,t)}set(e,t){const n=this.normalizeTokenName(e);this.updateCacheEntry(n,t)}get(e){const t=this.normalizeTokenName(e);return this.cache.get(t)||null}getMaxSupply(e){const t=this.normalizeTokenName(e),n=this.cache.get(t);return n?.maxSupply||ti.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,n)=>{e[n]=t}),e}stats(){let e=Date.now(),t=0;return this.cache.forEach((n,o)=>{n.lastUpdated<e&&(e=n.lastUpdated);let i=0;i+=2*o.length,void 0!==n.reverseBondingCurveMinFeeFactor&&(i+=8),void 0!==n.reverseBondingCurveMaxFeeFactor&&(i+=8),void 0!==n.reverseBondingCurveNetFeeFactor&&(i+=8),i+=8,n.vaultAddress&&(i+=2*n.vaultAddress.length),n.maxSupply&&(i+=2*n.maxSupply.length),n.symbol&&(i+=2*n.symbol.length),i+=32,t+=i}),{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 n=`token:${e.toLowerCase().trim()}`;this.updateCacheEntry(n,t)}hasByTokenId(e){const t=`token:${e.toLowerCase().trim()}`;return this.cache.has(t)}}ni.MAX_CACHE_SIZE=1e4;class oi extends vn{constructor(e,t,n=void 0,o=5,i=!1){super(e,i),this.pricingConcurrency=5,this.dexBackendBaseUrl=t,this.gswapService=n,this.pricingConcurrency=o}setGSwapService(e){this.gswapService=e}setPricingConcurrency(e){this.pricingConcurrency=Math.max(1,Math.min(e,20))}async enrichPoolsWithPricing(e){if(!this.gswapService)return this.logger.warn("GSwap service not available, skipping pricing enrichment"),e;if(0===e.length)return e;this.logger.debug("Starting pricing enrichment",{poolCount:e.length,concurrency:this.pricingConcurrency});const t=[];for(let n=0;n<e.length;n++){const o=e[n];t.push({poolIndex:n,token:o.token0,isToken0:!0,task:this.gswapService.getSwapQuoteExactInput({fromToken:o.token0,toToken:"GUSDC",amount:"1"}).then(e=>e.estimatedOutput).catch(e=>{this.logger.debug(`Failed to price ${o.token0}`,{error:e.message})})}),t.push({poolIndex:n,token:o.token1,isToken0:!1,task:this.gswapService.getSwapQuoteExactInput({fromToken:o.token1,toToken:"GUSDC",amount:"1"}).then(e=>e.estimatedOutput).catch(e=>{this.logger.debug(`Failed to price ${o.token1}`,{error:e.message})})})}const n=new Map;for(let t=0;t<e.length;t++)n.set(t,{});for(let e=0;e<t.length;e+=this.pricingConcurrency){const o=t.slice(e,e+this.pricingConcurrency),i=await Promise.allSettled(o.map(e=>e.task));for(let e=0;e<o.length;e++){const t=o[e],r=i[e],a=n.get(t.poolIndex)||{};"fulfilled"===r.status&&r.value&&(t.isToken0?a.token0Price=r.value:a.token1Price=r.value),n.set(t.poolIndex,a)}}const o=e.map((e,t)=>{const o=n.get(t)||{},i={...e};return void 0!==o.token0Price&&(i.token0Price=o.token0Price),void 0!==o.token1Price&&(i.token1Price=o.token1Price),i}),i=o.filter(e=>e.token0Price&&e.token1Price).length;return this.logger.debug("Pricing enrichment complete",{total:e.length,successful:i,failed:e.length-i}),o}async fetchDexPools(e={}){const{search:t,sortBy:n="tvl",sortOrder:o="desc",page:i=kn.DEFAULT_PAGE,limit:r=kn.DEFAULT_LIMIT,withPrices:a=!1}=e;this.logger.debug("Fetching DEX pools",{search:t,sortBy:n,sortOrder:o,page:i,limit:r,withPrices:a});const s=new URLSearchParams({page:i.toString(),limit:Math.min(r,20).toString(),sortBy:n,sortOrder:o});t&&s.append("search",t);const c=`${this.dexBackendBaseUrl}/explore/pools?${s}`;try{const e=await this.http.get(c);if(!e||!e.data)throw new Error("No response from DEX pool service");let t=e.data.pools;const n=e.data.count,o=Math.min(r,20),s=Math.ceil(n/o);return a&&(t=await this.enrichPoolsWithPricing(t)),this.logger.debug("DEX pools fetched successfully",{poolCount:t.length,total:n,totalPages:s,withPrices:a}),{pools:t,page:i,limit:o,total:n,totalPages:s,hasNext:i<s,hasPrevious:i>1}}catch(e){throw this.logger.error("Failed to fetch DEX pools",{error:e,url:c}),e}}async fetchAllDexPools(e={}){this.logger.debug("Fetching all DEX pools (auto-paginated)",e);const t=await zo((t,n)=>this.fetchDexPools({...e,page:t,limit:n}).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});this.logger.debug("All DEX pools fetched",{totalPoolsFetched:t.items.length,totalCount:t.total,withPrices:e.withPrices});return Wo(t.items,t.total,"pools")}}function ii(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 pipe-delimited token format. Expected at least 4 parts separated by |, got ${t.length}`);const[n,o,i,...r]=t;if(!n||!o||!i)throw new Error("Collection, category, and type must be non-empty");const a=r.join("|");if(!a)throw new Error("AdditionalKey must be non-empty");return{collection:n,category:o,type:i,additionalKey:a}}catch(t){throw new P(`Invalid pipe-delimited token: "${e}". Expected format: "collection|category|type|additionalKey". Error: ${t instanceof Error?t.message:String(t)}`,"pipeDelimitedToken","INVALID_PIPE_DELIMITED_TOKEN_FORMAT")}}class ri extends vn{constructor(e,t,n=!1,o=3e4){super(e,n),this.compositePoolFetchConcurrency=5,this.galaChainBaseUrl=t,this.networkTimeout=o}validateFetchCompositePoolDataInput(e,t,n){if(!e||"string"!=typeof e)throw new _("token0 must be a non-empty string",{token0:e});if(!t||"string"!=typeof t)throw new _("token1 must be a non-empty string",{token1:t});const o=e.split("|"),i=t.split("|");if(4!==o.length)throw new _("token0 format must be: collection|category|type|additionalKey (4 pipe-separated parts)",{token0:e});if(4!==i.length)throw new _("token1 format must be: collection|category|type|additionalKey (4 pipe-separated parts)",{token1:t});const r=[500,3e3,1e4];if(!Number.isInteger(n)||!r.includes(n))throw new _(`fee must be one of: ${r.join(", ")} (got ${n})`,{fee:n})}validateQuoteAmount(e){if(!e||"string"!=typeof e)throw new _("amount must be a non-empty string",{amount:e});const t=new r(e);try{no(t,"amount","for quote calculation")}catch(t){throw new _(t.message,{amount:e})}}convertTokenClassKey(e){const t=new s.TokenClassKey;return t.collection=e.collection,t.category=e.category,t.type=e.type,t.additionalKey=e.additionalKey,t}setCompositePoolFetchConcurrency(e){this.compositePoolFetchConcurrency=Math.max(1,Math.min(e,20)),this.logger.debug(`Composite pool fetch concurrency set to ${this.compositePoolFetchConcurrency}`)}async fetchCompositePoolData(e){const{token0:t,token1:n,fee:o,gatewayBaseUrl:i}=e;this.logger.debug("Fetching composite pool data",{token0:t,token1:n,fee:o}),this.validateFetchCompositePoolDataInput(t,n,o);try{const e=ii(t),c=ii(n),l=this.convertTokenClassKey(e),u=this.convertTokenClassKey(c),d=new a.GetCompositePoolDto(l,u,o),h=`${i||this.galaChainBaseUrl}/api/asset/dexv3-contract/GetCompositePool`,g=await this.http.post(h,d);if(!g||1!==g.Status)throw new B(`Pool not found: ${t}/${n} with fee ${o}`);const p=function(e){return{pool:e.pool,tickDataMap:e.tickDataMap,token0Balance:e.token0Balance,token1Balance:e.token1Balance,token0Decimals:e.token0Decimals,token1Decimals:e.token1Decimals,compositePoolDto:e}}(function(e){const t=new a.Pool(e.pool.token0,e.pool.token1,e.pool.token0ClassKey,e.pool.token1ClassKey,e.pool.fee,new r(e.pool.sqrtPrice),e.pool.protocolFees);t.bitmap=e.pool.bitmap,t.grossPoolLiquidity=new r(e.pool.grossPoolLiquidity),t.liquidity=new r(e.pool.liquidity),t.feeGrowthGlobal0=new r(e.pool.feeGrowthGlobal0),t.feeGrowthGlobal1=new r(e.pool.feeGrowthGlobal1),t.protocolFeesToken0=new r(e.pool.protocolFeesToken0),t.protocolFeesToken1=new r(e.pool.protocolFeesToken1),t.tickSpacing=e.pool.tickSpacing,t.maxLiquidityPerTick=new r(e.pool.maxLiquidityPerTick);const n={};Object.keys(e.tickDataMap).forEach(t=>{const o=e.tickDataMap[t],i=new a.TickData(o.poolHash,o.tick);i.initialised=o.initialised,i.liquidityNet=new r(o.liquidityNet),i.liquidityGross=new r(o.liquidityGross),i.feeGrowthOutside0=new r(o.feeGrowthOutside0),i.feeGrowthOutside1=new r(o.feeGrowthOutside1),n[t]=i});const o={...e.token0Balance},i=new s.TokenBalance(o);i.quantity=new r(e.token0Balance.quantity);const c={...e.token1Balance},l=new s.TokenBalance(c);return l.quantity=new r(e.token1Balance.quantity),new a.CompositePoolDto(t,n,i,l,e.token0Decimals,e.token1Decimals)}(g.Data),g.Data);return this.logger.debug("Composite pool data fetched successfully",{token0:t,token1:n,fee:o,liquidity:p.pool.liquidity.toString()}),p}catch(e){if(e instanceof B)throw e;const i=e instanceof Error?e.message:String(e);throw this.logger.error("Failed to fetch composite pool data",e),new _(`Failed to fetch composite pool data: ${i}`,{token0:t,token1:n,fee:o})}}async calculateDexPoolQuoteExactAmountLocal(e){const{compositePoolData:t,fromToken:n,toToken:o,amount:i}=e;if(this.logger.debug("Calculating local DEX quote",{fromToken:n,toToken:o,amount:i}),this.validateQuoteAmount(i),!t)throw new _("compositePoolData is required for local quote calculation",{compositePoolData:t});try{const e=n===t.pool.token0.replace(/\$/g,"|"),s=ii(n),c=ii(o),l=this.convertTokenClassKey(s),u=this.convertTokenClassKey(c),d=new a.QuoteExactAmountDto(l,u,t.pool.fee,new r(i),e,t.compositePoolDto),h=await a.quoteExactAmount(void 0,d);return this.logger.debug("Local quote calculated",{amount0:h.amount0,amount1:h.amount1}),{amount0:h.amount0.toString(),amount1:h.amount1.toString(),currentSqrtPrice:h.currentSqrtPrice.toString(),newSqrtPrice:h.newSqrtPrice.toString()}}catch(e){const t=e instanceof Error?e.message:String(e);throw this.logger.error("Local quote calculation failed",e),new _(`Local quote calculation failed: ${t}`,{fromToken:n,toToken:o,amount:i})}}async calculateDexPoolQuoteExactAmountExternal(e){const{compositePoolData:t,fromToken:n,toToken:o,amount:i}=e;if(this.logger.debug("Calculating external DEX quote",{fromToken:n,toToken:o,amount:i}),this.validateQuoteAmount(i),!t)throw new _("compositePoolData is required for external quote calculation (token format info)",{compositePoolData:t});try{const e=n===t.pool.token0.replace(/\$/g,"|"),s=ii(n),c=ii(o),l=this.convertTokenClassKey(s),u=this.convertTokenClassKey(c),d=new a.QuoteExactAmountDto(l,u,t.pool.fee,new r(i),e,void 0),h=`${this.galaChainBaseUrl}/api/asset/dexv3-contract/QuoteExactAmount`,g=await this.http.post(h,d);if(!g||1!==g.Status)throw new _("External quote failed: "+(g?.Message||"Unknown error"));const p=g.Data;return this.logger.debug("External quote calculated",{amount0:p.amount0,amount1:p.amount1}),{amount0:p.amount0.toString(),amount1:p.amount1.toString(),currentSqrtPrice:p.currentSqrtPrice.toString(),newSqrtPrice:p.newSqrtPrice.toString()}}catch(e){const t=e instanceof Error?e.message:String(e);throw this.logger.error("External quote calculation failed",e),new _(`External quote calculation failed: ${t}`,{fromToken:n,toToken:o,amount:i})}}async calculateDexPoolQuoteExactAmount(e,t="local"){return"external"===t?this.calculateDexPoolQuoteExactAmountExternal(e):this.calculateDexPoolQuoteExactAmountLocal(e)}}class ai{constructor(){this.eventLatencies=[],this.maxLatencySamples=1e4,this.eventsProcessed=0,this.eventsDropped=0,this.queueDepth=0,this.maxQueueDepth=0,this.startTime=Date.now(),this.perPoolMetrics=new Map,this.memorySnapshots=[],this.maxMemorySnapshots=100,this.recordMemory()}recordEventLatency(e){this.eventLatencies.push(e),this.eventLatencies.length>this.maxLatencySamples&&this.eventLatencies.shift(),this.eventsProcessed++,this.lastEventTime=new Date}recordEventDropped(){this.eventsDropped++}updateQueueDepth(e){this.queueDepth=e,this.maxQueueDepth=Math.max(this.maxQueueDepth,e)}recordPoolCacheHit(e,t){const n=this.getPoolMetrics(e);n.cacheHits++,n.eventsProcessed++,n.totalLatency+=t,n.lastEventTime=new Date}recordPoolCacheMiss(e,t){const n=this.getPoolMetrics(e);n.cacheMisses++,n.eventsProcessed++,n.totalLatency+=t,n.lastEventTime=new Date}getLatencyPercentiles(){if(0===this.eventLatencies.length)return{p50:0,p95:0,p99:0};const e=[...this.eventLatencies].sort((e,t)=>e-t),t=Math.floor(.5*e.length),n=Math.floor(.95*e.length),o=Math.floor(.99*e.length);return{p50:e[t]??0,p95:e[n]??0,p99:e[o]??0}}getCacheHitRate(){if(0===this.eventsProcessed)return 0;let e=0;for(const t of this.perPoolMetrics.values())e+=t.cacheHits;return e/this.eventsProcessed*100}getThroughputPerSecond(){const e=(Date.now()-this.startTime)/1e3;return 0===e?0:this.eventsProcessed/e}recordMemory(){if("undefined"!=typeof process&&process.memoryUsage){const e=process.memoryUsage().heapUsed/1024/1024;this.memorySnapshots.push(e),this.memorySnapshots.length>this.maxMemorySnapshots&&this.memorySnapshots.shift()}}getMemoryUsedMB(){return"undefined"!=typeof process&&process.memoryUsage?process.memoryUsage().heapUsed/1024/1024:0}getPoolAverageLatency(e){const t=this.perPoolMetrics.get(e);return t&&0!==t.eventsProcessed?t.totalLatency/t.eventsProcessed:0}getPoolCacheHitRate(e){const t=this.perPoolMetrics.get(e);if(!t)return 0;const n=t.cacheHits+t.cacheMisses;return 0===n?0:t.cacheHits/n*100}getHealthMetrics(e,t,n,o,i){const r=this.getLatencyPercentiles(),a=this.getMemoryUsedMB();return{eventProcessing:{queueDepth:this.queueDepth,eventsProcessed:this.eventsProcessed,eventsDropped:this.eventsDropped,throughputPerSecond:this.getThroughputPerSecond()},metrics:{latencyP50:r.p50,latencyP95:r.p95,latencyP99:r.p99,cacheHitRate:this.getCacheHitRate()},memory:{usedMB:Math.round(10*a)/10,maxMB:i,percentUsed:Math.round(a/i*1e3)/10},pools:{totalMonitored:e,hotCacheSize:t,warmCacheSize:n,coldCacheSize:o}}}reset(){this.eventLatencies=[],this.eventsProcessed=0,this.eventsDropped=0,this.queueDepth=0,this.maxQueueDepth=0,this.startTime=Date.now(),this.lastEventTime=void 0,this.perPoolMetrics.clear(),this.memorySnapshots=[]}getSummary(){const e=this.eventLatencies.length>0?this.eventLatencies.reduce((e,t)=>e+t,0)/this.eventLatencies.length:0;return{eventsProcessed:this.eventsProcessed,eventsDropped:this.eventsDropped,cacheHitRate:this.getCacheHitRate(),averageLatency:Math.round(e),memoryUsedMB:Math.round(10*this.getMemoryUsedMB())/10,throughputPerSecond:Math.round(100*this.getThroughputPerSecond())/100}}getPoolMetrics(e){let t=this.perPoolMetrics.get(e);return t||(t={eventsProcessed:0,totalLatency:0,cacheHits:0,cacheMisses:0},this.perPoolMetrics.set(e,t)),t}}class si{static createPoolKey(e,t,n){return`${e}/${t}/${n}`}static parsePoolKey(e){if(!e||"string"!=typeof e)return null;const t=e.split("/");if(3!==t.length)return null;const n=t[0]?.trim(),o=t[1]?.trim(),i=t[2]?.trim();if(!n||!o||!i)return null;const r=Number.parseInt(i,10);return Number.isNaN(r)?null:{token0:n,token1:o,feeTier:r}}static isValidPoolKey(e){if("string"!=typeof e)return!1;return null!==this.parsePoolKey(e)}static getToken0(e){const t=this.parsePoolKey(e);return t?.token0??null}static getToken1(e){const t=this.parsePoolKey(e);return t?.token1??null}static getFeeTier(e){const t=this.parsePoolKey(e);return t?.feeTier??null}static containsToken(e,t){const n=this.parsePoolKey(e);return!!n&&(n.token0===t||n.token1===t)}static containsTokenPair(e,t,n){const o=this.parsePoolKey(e);if(!o)return!1;const i=o.token0===t||o.token1===t,r=o.token0===n||o.token1===n;return i&&r&&t!==n}static normalizeFee(e){if(null==e)return null;const t="number"==typeof e?e:Number.parseFloat(String(e).replace("%","").trim());return Number.isNaN(t)?null:1===t||1e4===t?1e4:.3===t||3e3===t?3e3:.05===t||500===t?500:Number.isInteger(t)&&t>0?t:null}static formatFeeAsPercentage(e){return`${(e/1e4).toFixed(2)}%`}static isValidTokenPair(e,t){return Boolean(e)&&Boolean(t)&&e!==t}}class ci{constructor(e){this.logger=e||new S({debug:!1,context:"SwapEventExtractor"})}walkPayloadForSwaps(e,t){const n=[],o=new WeakSet,i=(e,r=0)=>{if(r>50)this.logger.debug("Payload nesting exceeded maximum depth of 50");else if(e&&"string"!=typeof e&&"object"==typeof e){if(o.has(e))return;o.add(e);const a=this.extractSwapFromObject(e);a&&!t.has(a.transactionId)&&(n.push(a),t.add(a.transactionId));for(const t of Object.values(e))i(t,r+1)}};return i(e,0),n}extractSwapFromObject(e){const t=this.extractTransactionId(e);if(!t)return null;const n=e.Data,o=n&&"object"==typeof n&&!Array.isArray(n)?n:e,i=this.extractToken(o,"token0","fromToken","source"),r=this.extractToken(o,"token1","toToken","destination");if(!i||!r)return null;const a=this.extractAmount(o,"amount0","amountIn","inputAmount"),s=this.extractAmount(o,"amount1","amountOut","outputAmount");if(!a||!s)return null;const c=this.extractFeeTier(o);if(null===c)return null;const l=this.extractTimestamp(o),u=this.buildPoolKey(i,r,c),d=this.determineDirection(o,i,r),h={transactionId:t,poolKey:u,token0:i,token1:r,amount0:a,amount1:s,feeTier:c,direction:d,timestamp:l,exactInput:this.determineExactInput(o,d)},g=this.extractUser(o);return void 0!==g&&(h.user=g),h}extractTransactionId(e){const t=["transactionId","txId","tx_id","hash","txHash","id"];for(const n of t){const t=e[n];if("string"==typeof t&&t.trim())return t}return null}extractToken(e,...t){for(const n of t){const t=e[n];if("string"==typeof t&&t.trim())return t}return null}extractAmount(e,...t){for(const n of t){const t=e[n];if(null!=t){const e=String(t).trim();if(/^-?\d+(\.\d+)?([eE]-?\d+)?$/.test(e))return e}}return null}extractFeeTier(e){const t=["poolFee","feeTier","fee","feeTierBps","liquidityFeeBps","feeAmount"];for(const n of t){const t=e[n],o=this.normalizeFee(t);if(null!==o)return o}return null}normalizeFee(e){if(null==e)return null;const t="number"==typeof e?e:Number.parseFloat(String(e).replace("%","").trim());return Number.isNaN(t)?null:1===t||1e4===t?1e4:.3===t||3e3===t?3e3:.05===t||500===t?500:Number.isInteger(t)?t:null}extractTimestamp(e){const t=["timeStamp","timestamp","time","createdAt","date"];for(const n of t){const t=e[n];if("number"==typeof t)return t;if("string"==typeof t){const e=new Date(t).getTime();if(!Number.isNaN(e))return e}}return Date.now()}extractUser(e){const t=["userAddress","user","from","sender","wallet","address"];for(const n of t){const t=e[n];if("string"==typeof t&&t.trim())return t}}determineDirection(e,t,n){const o=e.zeroForOne||e.direction;if("boolean"==typeof o)return o?"zeroForOne":"oneForZero";if("string"==typeof o){if("zerotoone"===o.toLowerCase()||"0to1"===o)return"zeroForOne";if("onetozero"===o.toLowerCase()||"1to0"===o)return"oneForZero"}if(e.fromToken===t||e.inputToken===t)return"zeroForOne";if(e.fromToken===n||e.inputToken===n)return"oneForZero";const i=this.extractAmount(e,"amount0","amountIn");return i&&Number(i),"zeroForOne"}determineExactInput(e,t){if("boolean"==typeof e.exactInput)return e.exactInput;if("boolean"==typeof e.exactOutput)return!e.exactOutput;const n=void 0!==e.amountIn&&null!==e.amountIn,o=void 0!==e.amountOut&&null!==e.amountOut,i=void 0!==e.inputAmount&&null!==e.inputAmount,r=void 0!==e.outputAmount&&null!==e.outputAmount;return!(!n||o)||!(o&&!n)&&(!(!i||r)||!(r&&!i))}buildPoolKey(e,t,n){return`${e}/${t}/${n}`}}class li{static getCached(e){const t=e.toString();return this.CACHE.has(t)||this.CACHE.set(t,new r(e)),this.CACHE.get(t)}static clearCache(){this.CACHE.clear()}static getCacheStats(){return{size:this.CACHE.size,entries:Array.from(this.CACHE.keys())}}static trimCache(e=1e3){if(this.CACHE.size>e){const t=this.CACHE.size-e,n=Array.from(this.CACHE.keys());for(let e=0;e<t;e++)this.CACHE.delete(n[e])}}}li.CACHE=new Map,li.ZERO=new r(0),li.ONE=new r(1),li.FEE_PIPS=new r(1e6),li.MIN_SQRT_RATIO=new r("4295128739"),li.MAX_SQRT_RATIO=new r("1461446703485210103287273052203988822378723970342");const ui={maxIterations:100,enableBigNumberCache:!0,roundingMode:r.ROUND_DOWN,debugLogging:!1};class di{static calculateSwapDelta(e,t,n={}){const o=Date.now(),i={...ui,...n};try{const n=this.initializeSwapState(e,t,i);i.debugLogging&&this.logger.debug("Initialized swap state",{sqrtPrice:n.sqrtPrice.toString(),liquidity:n.liquidity.toString(),tick:n.tick,zeroForOne:t.zeroForOne});const a=this.computeSwapLoop(n,e,t,i);i.debugLogging&&this.logger.debug("Swap loop completed",{stepCount:a.stepCount,ticksCrossed:a.ticksCrossed.length,priceHitLimit:a.priceHitLimit});const s=this.createUpdatedPool(e.pool,a.state,t,i),c=this.calculateFinalAmounts(n,a.state,t);let l;if(t.actualSqrtPrice){const e=new r(s.sqrtPrice),n=new r(t.actualSqrtPrice);l=e.minus(n).abs().div(n).times(100).toNumber()}const u=Date.now()-o;i.debugLogging&&this.logger.debug("Swap delta calculated",{calculationTimeMs:u,amount0:c.amount0.toString(),amount1:c.amount1.toString(),driftPercentage:l}),u>100&&this.logger.warn("Swap calculation exceeded 100ms",{calculationTimeMs:u,stepCount:a.stepCount,ticksCrossed:a.ticksCrossed.length}),a.priceHitLimit&&this.logger.warn("Swap price hit limit - partially fulfilled",{zeroForOne:t.zeroForOne,stepCount:a.stepCount}),a.stepCount>50&&this.logger.warn("Unusually complex swap detected",{stepCount:a.stepCount,ticksCrossed:a.ticksCrossed.length});return{updatedPool:s,updatedTicks:a.updatedTicks,amount0:c.amount0,amount1:c.amount1,feeAmount0:c.feeAmount0,feeAmount1:c.feeAmount1,ticksCrossed:a.ticksCrossed,metadata:{calculationTimeMs:u,swapSteps:a.stepCount,priceHitLimit:a.priceHitLimit,...void 0!==l&&{driftPercentage:l}}}}catch(e){this.logger.error("Swap delta calculation failed",e);const t=e instanceof Error?e.message:String(e);throw new Error(`Swap delta calculation failed: ${t}`)}}static initializeSwapState(e,t,n){const{pool:o}=e;if(!o.sqrtPrice||!o.liquidity)throw new Error("Invalid pool data: missing sqrtPrice or liquidity");const i=n.enableBigNumberCache?li.getCached.bind(li):e=>new r(e),s="string"==typeof o.sqrtPrice?o.sqrtPrice:new r(o.sqrtPrice).toFixed(0),c="string"==typeof o.liquidity?o.liquidity:new r(o.liquidity).toFixed(0),l=i(s),u=i(c),d=o.tick??0,h=a.sqrtPriceToTick(new r(s)),g=Math.abs(h-d);g>100&&this.logger.warn("Significant tick/price mismatch detected in pool state",{poolTick:d,calculatedTick:h,drift:g,threshold:100});const p=i(t.amountSpecified);no(p,"amountSpecified","for swap operation");const m="string"==typeof o.feeGrowthGlobal1?o.feeGrowthGlobal1:new r(o.feeGrowthGlobal1).toFixed(0),f="string"==typeof o.feeGrowthGlobal0?o.feeGrowthGlobal0:new r(o.feeGrowthGlobal0).toFixed(0),y=t.zeroForOne?i(m):i(f);return{sqrtPrice:l,liquidity:u,tick:d,amountSpecifiedRemaining:p,amountCalculated:li.ZERO,feeGrowthGlobalX:y,protocolFee:li.ZERO}}static computeSwapLoop(e,t,n,o){const{pool:i,tickDataMap:s}=t,c=[],l={};let u=0;const d=n.zeroForOne?li.MIN_SQRT_RATIO:li.MAX_SQRT_RATIO,h=new r("0.000001");for(;e.amountSpecifiedRemaining.gt(h)&&!e.sqrtPrice.eq(d)&&u<o.maxIterations;){u++;const[t,h]=this.findNextInitializedTick(s,e.tick,i.tickSpacing,n.zeroForOne);let g;if(o.debugLogging&&this.logger.debug(`Swap step ${u}`,{currentTick:e.tick,tickNext:t,initialized:h,sqrtPrice:e.sqrtPrice.toString(),liquidity:e.liquidity.toString(),amountRemaining:e.amountSpecifiedRemaining.toString()}),h&&t>=-887272&&t<=887272){const e=a.tickToSqrtPrice(t);g=e instanceof r?e:new r(String(e))}else g=d;const p=n.zeroForOne?r.max(g,d):r.min(g,d),m=this.executeSwapStep(e.sqrtPrice,p,e.liquidity,e.amountSpecifiedRemaining,i.fee,n.exactInput);if(e.sqrtPrice=m.sqrtPriceNext,n.exactInput){const t=m.amountIn.plus(m.feeAmount);t.lte(0)?e.amountSpecifiedRemaining=li.ZERO:(e.amountSpecifiedRemaining=e.amountSpecifiedRemaining.minus(t),e.amountSpecifiedRemaining.lt(0)&&(e.amountSpecifiedRemaining=li.ZERO)),e.amountCalculated=e.amountCalculated.minus(m.amountOut)}else{m.amountOut.lte(0)?e.amountSpecifiedRemaining=li.ZERO:e.amountSpecifiedRemaining=e.amountSpecifiedRemaining.plus(m.amountOut),e.amountCalculated=e.amountCalculated.plus(m.amountIn.plus(m.feeAmount))}if(e.liquidity.gt(0)){const t=m.feeAmount.div(e.liquidity);e.feeGrowthGlobalX=e.feeGrowthGlobalX.plus(t)}if(e.sqrtPrice.eq(g)&&h){const i=s[t.toString()];if(!i)throw new Error(`Missing tick data for initialized tick ${t}`);const a=n.zeroForOne?new r(i.liquidityNet).negated():new r(i.liquidityNet);if(e.liquidity=e.liquidity.plus(a),e.liquidity.lt(0))throw new Error(`Negative liquidity after crossing tick ${t}: ${e.liquidity.toString()}`);c.push(t),l[t.toString()]=i,o.debugLogging&&this.logger.debug(`Crossed tick ${t}`,{liquidityNet:a.toString(),newLiquidity:e.liquidity.toString()})}if(e.sqrtPrice.eq(g))e.tick=n.zeroForOne?t-1:t;else{const t=a.sqrtPriceToTick(new r(e.sqrtPrice.toString()));e.tick=t}}if(u>=o.maxIterations)throw new Error(`Swap calculation exceeded maximum iterations (${o.maxIterations}). Possible infinite loop or very complex swap.`);const g=e.sqrtPrice.eq(d);return{state:e,ticksCrossed:c,priceHitLimit:g,stepCount:u,updatedTicks:l}}static createUpdatedPool(e,t,n,o){const i=Object.assign(Object.create(Object.getPrototypeOf(e)),e);if(i.sqrtPrice=t.sqrtPrice.toFixed(0),i.liquidity=t.liquidity.toFixed(0),i.tick=t.tick,n.zeroForOne?i.feeGrowthGlobal1=t.feeGrowthGlobalX.toFixed(0):i.feeGrowthGlobal0=t.feeGrowthGlobalX.toFixed(0),n.zeroForOne){const n=new r(e.protocolFeesToken0);i.protocolFeesToken0=n.plus(t.protocolFee).toFixed(0)}else{const n=new r(e.protocolFeesToken1);i.protocolFeesToken1=n.plus(t.protocolFee).toFixed(0)}return i}static calculateFinalAmounts(e,t,n){let o,i,a,s;if(n.exactInput){const e=new r(n.amountSpecified),c=t.amountCalculated.abs();n.zeroForOne?(o=e.negated(),i=c,a=li.ZERO,s=li.ZERO):(o=c,i=e.negated(),a=li.ZERO,s=li.ZERO)}else{const e=new r(n.amountSpecified),c=t.amountCalculated.abs();n.zeroForOne?(o=c.negated(),i=e,a=li.ZERO,s=li.ZERO):(o=e,i=c.negated(),a=li.ZERO,s=li.ZERO)}const c=t.feeGrowthGlobalX.minus(e.feeGrowthGlobalX).times(e.liquidity);return n.zeroForOne?s=c:a=c,{amount0:o,amount1:i,feeAmount0:a,feeAmount1:s}}static findNextInitializedTick(e,t,n,o){const i=Object.keys(e).map(e=>parseInt(e,10)).sort((e,t)=>e-t);if(0===i.length){return[o?-887272:887272,!1]}if(o){const e=i.reverse().find(e=>e<t);return void 0!==e?[e,!0]:[-887272,!1]}{const e=i.find(e=>e>t);return void 0!==e?[e,!0]:[887272,!1]}}static executeSwapStep(e,t,n,o,i,s){to(n),no(o,"amountRemaining","for swap step");const c=[500,3e3,1e4];if(!c.includes(i))throw new Error(`Invalid fee tier: ${i}. Must be one of: ${c.join(", ")}`);const l=a.computeSwapStep(e,t,n,o,i,t.lt(e)),u=l[0],d=l[1],h=l[2],g=l[3],p=r.isBigNumber(u)?u:new r(String(u)),m=r.isBigNumber(d)?d:new r(String(d)),f=r.isBigNumber(h)?h:new r(String(h)),y=r.isBigNumber(g)?g:new r(String(g));return{sqrtPriceStart:e,tickNext:a.sqrtPriceToTick(p),sqrtPriceNext:p,initialised:!1,amountIn:m,amountOut:f,feeAmount:y}}}di.logger=new S({debug:!1,context:"SwapDeltaCalculator"});class hi{constructor(e,t,n,o){this.cache=new Map,this.tierSizes={hot:50,warm:200,cold:0},this.tierTTLs={hot:1/0,warm:18e5,cold:3e5},this.refetchThresholds={swapCount:50,driftPercent:.05},this.fetchPoolFn=e,this.config=t,this.metrics=n,this.logger=o||new S({debug:!1,context:"PoolCacheManager"}),this.tierSizes.cold=Math.max(0,this.config.maxPools-this.tierSizes.hot-this.tierSizes.warm),this.logger.debug(`Initialized with cache limits: hot=${this.tierSizes.hot}, warm=${this.tierSizes.warm}, cold=${this.tierSizes.cold}, max=${this.config.maxPools}`)}async getPool(e){const t=this.cache.get(e);if(t){if(!(Date.now()>t.expiresAt))return t.lastAccessTime=Date.now(),this.checkRefetchNeeded(e,t),this.metrics.recordPoolCacheHit(e,0),t.poolData;this.cache.delete(e),this.logger.debug(`Cache expired for pool ${e}`)}this.metrics.recordPoolCacheMiss(e,0);try{const t=await this.fetchPoolFn(e),n=this.determineTier(),o={poolData:t,tier:n,lastAccessTime:Date.now(),expiresAt:Date.now()+this.tierTTLs[n],swapsSinceRefetch:0,cumulativeDrift:0,lastDeltaAppliedTime:Date.now()};return this.cache.set(e,o),this.cache.size>this.config.maxPools&&this.evictLRU(),this.logger.debug(`Fetched pool ${e} (tier: ${n})`),t}catch(t){throw this.logger.error(`Failed to fetch pool ${e}:`,t),t}}updatePoolWithSwapDelta(e,t,n,o,i){const r=this.cache.get(e);if(!r)return this.logger.debug(`Pool ${e} not in cache for delta update`),!1;try{if(i){const a="zeroForOne"===t,s=a?n:o,c={transactionId:i.transactionId,timestamp:i.timestamp,amountSpecified:s,zeroForOne:a,exactInput:i.exactInput},l=di.calculateSwapDelta(r.poolData,c);r.poolData={...r.poolData,pool:l.updatedPool},r.swapsSinceRefetch++,r.lastDeltaAppliedTime=Date.now(),void 0!==l.metadata.driftPercentage?(r.cumulativeDrift+=l.metadata.driftPercentage,this.logger.debug(`Delta applied for ${e}: drift=${l.metadata.driftPercentage.toFixed(4)}%, cumulative=${r.cumulativeDrift.toFixed(2)}%`)):this.logger.debug(`Delta applied for ${e}: ${l.ticksCrossed.length} ticks crossed`)}else r.swapsSinceRefetch++,r.lastDeltaAppliedTime=Date.now();return this.shouldRefetch(r)&&(this.logger.debug(`Refetch needed for ${e}: swaps=${r.swapsSinceRefetch}, drift=${(100*r.cumulativeDrift).toFixed(2)}%`),r.expiresAt=Date.now()),!0}catch(t){return this.logger.error(`Failed to update pool ${e}:`,t),r.expiresAt=Date.now(),!1}}getStats(){const e={totalCached:this.cache.size,hotCacheSize:0,warmCacheSize:0,coldCacheSize:0,memoryUsedMB:this.metrics.getMemoryUsedMB()};for(const t of this.cache.values())"hot"===t.tier?e.hotCacheSize++:"warm"===t.tier?e.warmCacheSize++:e.coldCacheSize++;return e}getPoolInfo(e){const t=this.cache.get(e);return t?{poolKey:e,tier:t.tier,lastAccessTime:new Date(t.lastAccessTime),expiresAt:new Date(t.expiresAt),swapsSinceRefetch:t.swapsSinceRefetch,cumulativeDrift:t.cumulativeDrift,isExpired:Date.now()>t.expiresAt}:null}async warmCache(e){if(this.cache.has(e))return!0;try{return await this.getPool(e),this.logger.debug(`Cache warmed for ${e}`),!0}catch(t){return this.logger.error(`Failed to warm cache for ${e}:`,t),!1}}async warmCacheBatch(e,t=5){const n={succeeded:0,failed:0,total:e.length};let o=0;const i=new Set;for(;o<e.length||i.size>0;){for(;o<e.length&&i.size<t;){const t=e[o];o++;const r=this.warmCache(t).then(e=>{e?n.succeeded++:n.failed++});i.add(r),r.finally(()=>i.delete(r))}i.size>0&&await Promise.race(i)}return this.logger.debug(`Cache warming complete: ${n.succeeded}/${n.total} succeeded`),n}clear(){this.cache.clear(),this.logger.debug("Cache cleared")}clearExpired(){const e=Date.now();let t=0;for(const[n,o]of this.cache)e>o.expiresAt&&(this.cache.delete(n),t++);t>0&&this.logger.debug(`Cleared ${t} expired entries`)}determineTier(){const e=Array.from(this.cache.values()).filter(e=>"hot"===e.tier).length,t=Array.from(this.cache.values()).filter(e=>"warm"===e.tier).length;return e<this.tierSizes.hot?"hot":t<this.tierSizes.warm?"warm":"cold"}checkRefetchNeeded(e,t){this.shouldRefetch(t)&&(this.logger.debug(`Scheduling refetch for ${e}: swaps=${t.swapsSinceRefetch}, drift=${(100*t.cumulativeDrift).toFixed(2)}%`),t.expiresAt=Date.now())}shouldRefetch(e){return e.swapsSinceRefetch>=this.refetchThresholds.swapCount||e.cumulativeDrift>=this.refetchThresholds.driftPercent}evictLRU(){const e=Array.from(this.cache.entries()).filter(([e,t])=>"cold"===t.tier).sort((e,t)=>e[1].lastAccessTime-t[1].lastAccessTime);if(0===e.length){this.logger.warn("No cold cache entries to evict, trying warm cache");const e=Array.from(this.cache.entries()).filter(([e,t])=>"warm"===t.tier).sort((e,t)=>e[1].lastAccessTime-t[1].lastAccessTime);if(e.length>0){const[t]=e[0];this.cache.delete(t),this.logger.debug(`Evicted warm cache entry: ${t}`)}return}const[t]=e[0];this.cache.delete(t),this.logger.debug(`Evicted cold cache entry: ${t}`)}async refreshWarmAndHotTiers(){const e=Array.from(this.cache.entries()).filter(([e,t])=>"hot"===t.tier||"warm"===t.tier).sort((e,t)=>t[1].lastAccessTime-e[1].lastAccessTime).slice(0,10);if(0===e.length)return;const t=e.map(([e,t])=>this.fetchPoolFn(e).then(n=>{t&&(t.poolData=n,t.lastAccessTime=Date.now(),t.swapsSinceRefetch=0,t.cumulativeDrift=0),this.logger.debug(`Refreshed ${e} during background warming`)}).catch(t=>{this.logger.debug(`Failed to refresh ${e} during background warming:`,t)})),n=new Promise(e=>setTimeout(()=>e(),5e3));await Promise.race([Promise.all(t),n])}}class gi{constructor(e,t,n){this.queue=[],this.isShuttingDown=!1,this.currentConcurrency=0,this.maxConcurrencyReached=0,this.eventsDropped=0,this.totalBatchesProcessed=0,this.totalBatchSize=0,this.eventsProcessedCount=0,this.processor=null,this.processingScheduled=!1,this.scheduleProcessing=e=>{"undefined"!=typeof setImmediate?setImmediate(e):Promise.resolve().then(e)},this.config=e,this.metrics=t,this.logger=n||new S({debug:!1,context:"SwapEventQueue"}),this.logger.debug(`Initialized with maxQueueSize=${this.config.maxQueueSize}, batchSize=${this.config.batchSize}, maxConcurrent=${this.config.maxConcurrent}`)}setProcessor(e){this.processor=e}enqueue(e){return this.isShuttingDown?(this.logger.debug(`Rejecting event (queue shutting down): ${e.transactionId}`),this.metrics.recordEventDropped(),this.eventsDropped++,!1):this.queue.length>=this.config.maxQueueSize?(this.logger.warn(`Queue full (${this.queue.length}/${this.config.maxQueueSize}), dropping event: ${e.transactionId}`),this.metrics.recordEventDropped(),this.eventsDropped++,!1):(this.queue.push(e),this.metrics.updateQueueDepth(this.queue.length),this.processingScheduled||this.isShuttingDown||(this.processingScheduled=!0,this.scheduleProcessing(()=>this.processNextBatch())),!0)}getQueueSize(){return this.queue.length}getStats(){return{queueSize:this.queue.length,eventsProcessed:this.eventsProcessedCount,eventsDropped:this.eventsDropped,currentConcurrent:this.currentConcurrency,maxConcurrentReached:this.maxConcurrencyReached,averageBatchSize:this.totalBatchesProcessed>0?Math.floor(this.totalBatchSize/this.totalBatchesProcessed):0,totalBatchesProcessed:this.totalBatchesProcessed}}async waitForEmpty(e){return new Promise(t=>{const n=()=>{0!==this.queue.length||0!==this.currentConcurrency?setTimeout(n,10):t()};e&&setTimeout(()=>t(),e),n()})}async shutdown(e=3e4){this.isShuttingDown=!0,this.logger.debug("Shutting down queue...");const t=Date.now();for(;this.queue.length>0||this.currentConcurrency>0;){if(Date.now()-t>e){this.logger.warn(`Queue shutdown timeout: ${this.queue.length} events remaining, ${this.currentConcurrency} processing`);break}await new Promise(e=>setTimeout(e,50))}this.logger.debug("Queue shutdown complete")}clear(){const e=this.queue.length;this.queue.length=0,this.eventsDropped+=e,this.metrics.updateQueueDepth(0),this.logger.warn(`Cleared ${e} events from queue`)}async processNextBatch(){if(this.processingScheduled=!1,this.isShuttingDown&&0===this.queue.length)return;if(this.currentConcurrency>=this.config.maxConcurrent)return void setTimeout(()=>{this.processingScheduled||(this.processingScheduled=!0,setImmediate(()=>this.processNextBatch()))},10);const e=Math.min(this.config.batchSize,this.queue.length,this.config.maxConcurrent-this.currentConcurrency);if(0===e)return;const t=this.queue.splice(0,e);this.metrics.updateQueueDepth(this.queue.length),this.currentConcurrency+=t.length,this.currentConcurrency>this.maxConcurrencyReached&&(this.maxConcurrencyReached=this.currentConcurrency),this.totalBatchSize+=t.length,this.totalBatchesProcessed++;try{const e=await Promise.allSettled(t.map(e=>this.processEvent(e)));for(let n=0;n<e.length;n++){const o=e[n];this.eventsProcessedCount++,"rejected"===o.status&&this.logger.error(`Failed to process event ${t[n].transactionId}:`,o.reason)}}finally{this.currentConcurrency-=t.length}this.queue.length>0&&!this.processingScheduled&&(this.processingScheduled=!0,this.scheduleProcessing(()=>this.processNextBatch()))}async processEvent(e){if(!this.processor)return void this.logger.warn("No processor set, discarding event:",e.transactionId);const t=Date.now();try{await this.processor(e);const n=Date.now()-t;this.metrics.recordEventLatency(n)}catch(t){throw this.logger.error(`Event processing failed for ${e.transactionId}:`,t),t}}}class pi{constructor(e,t,n,o={},i){this.socket=null,this.maxSeenTransactions=1e4,this.listeners=[],this.onErrorCallbacks=[],this.isActive=!1,this.listenerRegistered=!1,this.handleSwapEvent=null,this.warmingIntervalHandle=null,this.reconnectAttempts=0,this.maxReconnectAttempts=3,this.reconnectDelayMs=1e3,this.logger=i||new S({debug:!1,context:"MultiPoolStateManager"}),e instanceof Promise?this.socketReady=e.then(e=>(this.socket=e,this.setupConnectionMonitoring(),e)).catch(e=>{throw this.logger.error("Failed to resolve socket promise:",e),e}):(this.socket=e,this.socketReady=Promise.resolve(e),this.setupConnectionMonitoring()),this.metrics=new ai,this.config=this.applyDefaults(o),this.eventExtractor=new ci(this.logger),this.quoteService=n,this.cacheManager=new hi(t,this.config,this.metrics,this.logger),this.eventQueue=new gi(this.config,this.metrics,this.logger),this.seenTransactions=new mi(this.maxSeenTransactions),this.eventQueue.setProcessor(e=>this.processSwapEvent(e)),this.logger.debug("Initialized MultiPoolStateManager")}subscribe(e,t){this.listeners.push(t),e.onError&&this.onErrorCallbacks.push(e.onError),this.isActive||(this.setupWebSocketListener(e),this.isActive=!0);const n=this;return()=>{n.listeners=n.listeners.filter(e=>e!==t),e.onError&&(n.onErrorCallbacks=n.onErrorCallbacks.filter(t=>t!==e.onError)),0===n.listeners.length&&0===n.onErrorCallbacks.length&&n.unsubscribe()}}getHealth(){const e=this.cacheManager.getStats(),t=this.eventQueue.getStats(),n=this.metrics.getHealthMetrics(e.totalCached,e.hotCacheSize,e.warmCacheSize,e.coldCacheSize,this.getMaxMemoryMB()),o=this.determineHealthStatus(t,e),i={connected:this.socket?.connected??!1,reconnectAttempts:this.reconnectAttempts};this.socket?.connected&&(i.lastConnectionTime=new Date);const r=t.queueSize/this.config.maxQueueSize*100,a=e.totalCached/this.config.maxPools*100,s=e.memoryUsedMB/this.getMaxMemoryMB()*100,c=this.generateHealthRecommendations(o,r,a,s,n.metrics.cacheHitRate);return{...n,status:o,websocket:i,recommendations:c,detailedMetrics:{eventQueueUtilization:r,cacheUtilization:a,memoryUtilization:s}}}generateHealthRecommendations(e,t,n,o,i){const r=[];return"failed"===e&&(r.push("🔴 System is in FAILED state - immediate action required"),this.socket?.connected||r.push("Reconnect WebSocket - connection lost"),t>90&&r.push("Reduce incoming event rate or increase maxQueueSize")),"degraded"===e&&(r.push("⚠️ System is DEGRADED - performance may be impacted"),t>75&&r.push(`Queue utilization ${t.toFixed(1)}% - consider increasing maxQueueSize`),o>80&&r.push(`Memory usage ${o.toFixed(1)}% - consider reducing cache size or memory profile`)),"healthy"===e&&(i<50&&r.push(`Cache hit rate ${i.toFixed(1)}% is low - consider warming more pools`),o>50&&r.push("Memory usage is moderate - monitor for growth trends"),t>50&&r.push("Queue utilization is elevated - monitor for bottlenecks")),r}getSummary(){return{...this.metrics.getSummary(),queueStats:this.eventQueue.getStats(),cacheStats:this.cacheManager.getStats()}}startBackgroundWarming(){if(this.warmingIntervalHandle)return;const e=this.config.refreshIntervalMs;this.warmingIntervalHandle=setInterval(()=>{this.performBackgroundWarming().catch(e=>{this.logger.error("Background warming error:",e)})},e),this.logger.debug(`Background warming started (interval: ${e}ms)`)}stopBackgroundWarming(){this.warmingIntervalHandle&&(clearInterval(this.warmingIntervalHandle),this.warmingIntervalHandle=null,this.logger.debug("Background warming stopped"))}async performBackgroundWarming(){const e=this.cacheManager.getStats();if(0!==e.totalCached)try{await this.cacheManager.refreshWarmAndHotTiers(),this.logger.debug(`Background warming completed: ${e.totalCached} pools in cache (hot: ${e.hotCacheSize}, warm: ${e.warmCacheSize})`)}catch(e){this.logger.debug("Background warming encountered an error:",e)}}async shutdown(){this.stopBackgroundWarming(),await this.unsubscribe(),await this.eventQueue.shutdown(),this.cacheManager.clear(),this.metrics.reset()}setupConnectionMonitoring(){this.socket&&(this.socket.on("disconnect",()=>{this.logger.warn("WebSocket disconnected"),this.notifyError(new Error("WebSocket disconnected")),this.config.autoRecover&&this.attemptReconnection().catch(e=>{this.logger.error("Reconnection failed:",e)})}),this.socket.on("connect_error",e=>{this.logger.error("WebSocket connection error:",e),this.notifyError(e instanceof Error?e:new Error(String(e)))}))}async attemptReconnection(){if(this.logger.debug(`Reconnection attempt ${this.reconnectAttempts+1}/${this.maxReconnectAttempts}`),this.reconnectAttempts>=this.maxReconnectAttempts)return this.logger.error(`Max reconnection attempts (${this.maxReconnectAttempts}) exceeded - performing full reset`),void await this.performFullReset();this.reconnectAttempts<2&&(0===this.reconnectAttempts?this.logger.debug("Tier 1: Quick reconnect"):(this.logger.debug(`Tier 2: Exponential backoff (${this.reconnectDelayMs}ms)`),await new Promise(e=>setTimeout(e,this.reconnectDelayMs)),this.reconnectDelayMs=Math.min(2*this.reconnectDelayMs,3e4)));try{this.socket?.disconnect&&(this.socket.disconnect(),this.logger.debug("Disconnected for reconnection")),this.socket?.connect?.(),this.logger.debug("Reconnection initiated"),this.reconnectAttempts++}catch(e){throw this.logger.error("Failed to initiate reconnection:",e),e}}async performFullReset(){this.logger.warn("Performing full system reset due to connection failures"),this.stopBackgroundWarming();const e=this.getHealth();this.logger.debug("System state before reset:",{status:e.status,queueSize:e.eventProcessing.eventsProcessed,cachedPools:e.pools.totalMonitored,memory:`${e.memory.usedMB}MB / ${e.memory.maxMB}MB`,cacheHitRate:`${e.metrics.cacheHitRate.toFixed(2)}%`}),this.cacheManager.clear(),this.metrics.reset(),this.reconnectAttempts=0,this.reconnectDelayMs=1e3,this.notifyError(new Error("System reset: connection lost and recovery failed - please restart monitoring")),this.logger.info("System reset complete - ready for restart")}setupWebSocketListener(e){if(this.listenerRegistered)return void this.logger.debug("WebSocket listener already registered");const t=this;this.handleSwapEvent=(n,...o)=>{try{const n=Date.now(),i=o[0],r=t.eventExtractor.walkPayloadForSwaps(i,t.seenTransactions);if(0===r.length)return;t.logger.debug(`Extracted ${r.length} swaps from payload`);for(const n of r){if(t.filterSwap(n,e)){t.eventQueue.enqueue(n)||t.logger.debug(`Swap dropped due to queue overflow: ${n.transactionId}`)}}const a=Date.now()-n;t.metrics.recordEventLatency(a)}catch(e){t.logger.error("Error processing WebSocket payload:",e),t.notifyError(e instanceof Error?e:new Error(String(e)))}},this.socket?(this.socket.onAny(this.handleSwapEvent),this.listenerRegistered=!0,this.setupConnectionMonitoring()):this.logger.warn("Socket not available for listener registration"),this.startBackgroundWarming(),this.logger.debug("WebSocket listener registered for all events")}async unsubscribe(){this.stopBackgroundWarming(),this.handleSwapEvent&&this.listenerRegistered&&this.socket&&(this.socket.offAny(this.handleSwapEvent),this.listenerRegistered=!1),this.isActive=!1,this.listeners=[],this.onErrorCallbacks=[],this.logger.debug("Unsubscribed from swap events")}filterSwap(e,t){if(t.tokenFilter){if(!si.containsToken(e.poolKey,t.tokenFilter))return!1}if(t.pairTokens){const[n,o]=t.pairTokens;if(!si.containsTokenPair(e.poolKey,n,o))return!1}if(t.feeTierFilter){if(si.normalizeFee(t.feeTierFilter)!==e.feeTier)return!1}return!t.userFilter||e.user===t.userFilter}async processSwapEvent(e){const t=Date.now();try{const n=this.cacheManager.updatePoolWithSwapDelta(e.poolKey,e.direction,e.amount0,e.amount1,e);e.poolStateUpdated=n;const o=Date.now()-t;this.metrics.recordEventLatency(o);for(const t of this.listeners)try{const n=t(e);n instanceof Promise&&await n}catch(t){this.logger.error(`Listener error for swap ${e.transactionId}:`,t)}}catch(t){this.logger.error(`Failed to process swap ${e.transactionId}:`,t),this.notifyError(t instanceof Error?t:new Error(String(t)))}}notifyError(e){for(const t of this.onErrorCallbacks)try{t(e)}catch(e){this.logger.error("Error in error callback:",e)}}determineHealthStatus(e,t){return!this.socket?.connected||e.queueSize>.9*this.config.maxQueueSize?"failed":e.queueSize>.75*this.config.maxQueueSize||t.memoryUsedMB>.9*this.getMaxMemoryMB()?"degraded":"healthy"}getMaxMemoryMB(){switch(this.config.memoryProfile){case"conservative":return 55;case"aggressive":return 530;default:return 250}}applyDefaults(e){return{memoryProfile:e.memoryProfile??"moderate",maxPools:e.maxPools??500,softLimit:e.softLimit??200,preloadTopN:e.preloadTopN??200,warmingTimeoutMs:e.warmingTimeoutMs??3e4,refreshIntervalMs:e.refreshIntervalMs??3e5,maxQueueSize:e.maxQueueSize??1e4,batchSize:e.batchSize??100,maxConcurrent:e.maxConcurrent??10,autoRecover:e.autoRecover??!0,maxParallelRefetch:e.maxParallelRefetch??20,enableDeltaOptimization:e.enableDeltaOptimization??!0,enableOfflineQuotes:e.enableOfflineQuotes??!0,metricsEnabled:e.metricsEnabled??!0,debug:e.debug??!1}}}class mi{constructor(e){this.map=new Map,this.maxSize=e}has(e){return this.map.has(e)}add(e){if(this.map.has(e))return this.map.delete(e),this.map.set(e,Date.now()),this;if(this.map.size>=this.maxSize){const e=this.map.keys().next().value;this.map.delete(e)}return this.map.set(e,Date.now()),this}delete(e){return this.map.delete(e)}clear(){this.map.clear()}get size(){return this.map.size}}function fi(e){const t=function(e){const t=Nt(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 yi="/api/asset/launchpad-contract/CallNativeTokenIn",wi="/api/asset/launchpad-contract/CallNativeTokenOut",ki="/api/asset/launchpad-contract/CallMemeTokenIn",vi="/api/asset/launchpad-contract/CallMemeTokenOut";class Ti 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,e.websiteUrl&&(this.websiteUrl=e.websiteUrl),e.telegramUrl&&(this.telegramUrl=e.telegramUrl),e.twitterUrl&&(this.twitterUrl=e.twitterUrl),this.tokenCategory=e.tokenCategory,this.tokenCollection=e.tokenCollection,this.uniqueKey=e.uniqueKey,e.reverseBondingCurveConfiguration&&(this.reverseBondingCurveConfiguration=e.reverseBondingCurveConfiguration)}}function Si(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 bi={NATIVE:"native",EXACT:"exact"},Ai={LOCAL:"local",EXTERNAL:"external"};class Ei{static calculateBuyWithExact(e,t){const n=parseFloat(e),o=parseFloat(t),{BASE_PRICE:i,PRICE_SCALING_FACTOR:a,TRADING_FEE_FACTOR:s,GAS_FEE:c}=ti,l=this.roundUp(i*(Math.exp((o+n)*a)-Math.exp(o*a))/a,8),u=new r(l).multipliedBy(s).toFixed();return{amount:l.toString(),reverseBondingCurveFee:"0",transactionFee:u,gasFee:c}}static calculateBuyWithNative(e,t){const n=parseFloat(e),o=parseFloat(t),{BASE_PRICE:i,PRICE_SCALING_FACTOR:a,TRADING_FEE_FACTOR:s,GAS_FEE:c}=ti,l=Math.log(n*a/i+Math.exp(o*a))/a-o,u=new r(l).multipliedBy(s).toFixed();return{amount:l.toString(),reverseBondingCurveFee:"0",transactionFee:u,gasFee:c}}static calculateSellWithExact(e,t,n,o,i){const a=parseFloat(e),s=parseFloat(t),c=parseFloat(n),{BASE_PRICE:l,PRICE_SCALING_FACTOR:u,TRADING_FEE_FACTOR:d,GAS_FEE:h}=ti,g=l*(Math.exp(s*u)-Math.exp((s-a)*u))/u,p=new r(g),m=o+s/c*(i-o),f=p.multipliedBy(m).toFixed(8,r.ROUND_UP),y=p.multipliedBy(d).toFixed();return{amount:g.toString(),reverseBondingCurveFee:f,transactionFee:y,gasFee:h}}static calculateSellWithNative(e,t,n,o,i){const a=parseFloat(e),s=parseFloat(t),c=parseFloat(n),{BASE_PRICE:l,PRICE_SCALING_FACTOR:u,TRADING_FEE_FACTOR:d,GAS_FEE:h}=ti,g=s-Math.log(Math.exp(s*u)-a*u/l)/u,p=new r(a),m=o+s/c*(i-o),f=p.multipliedBy(m).toFixed(8,r.ROUND_UP),y=p.multipliedBy(d).toFixed();return{amount:g.toString(),reverseBondingCurveFee:f,transactionFee:y,gasFee:h}}static roundUp(e,t){const n=Math.pow(10,t);return Math.ceil(e*n)/n}}class Ii{constructor(e,t,n,o,i,r,a="local"){this.http=e,this.tokenResolver=t,this.logger=n,this.bundleHttp=o,this.galaChainHttp=i,this.dexApiHttp=r,this.defaultCalculateAmountMode=a,this.metadataCache=new ni}addIfDefined(e,t,n){return void 0!==n&&(e[t]=n),e}async uploadImageByTokenName(e){const{tokenName:t,options:n}=e;Ht(t);const o=`${t}.png`;zn(n.file,o,"image/png");try{const e=new FormData;if("undefined"!=typeof File&&n.file instanceof File)e.append("image",n.file);else{if(!Buffer.isBuffer(n.file))throw q("file","a File object (browser) or Buffer (Node.js)");{const o=`${n.tokenName||t}.png`,i=new Blob([n.file],{type:"image/png"});e.append("image",i,o)}}const o=await this.http.request({method:"POST",url:`/launchpad/upload-image?tokenName=${encodeURIComponent(n.tokenName||t)}`,data:e,headers:{}});if(!0===o.error||200!==o.status||!o.data?.imageUrl)throw R(o.message||"Image upload failed - no URL returned",o.status);return o.data.imageUrl}catch(e){if(e instanceof Error&&e.message.includes("FormData"))throw K("File upload failed: FormData not supported in this environment. Ensure you have proper polyfills for Node.js environments.","FormData");throw e}}async fetchPoolsFromAPI(e){Vt(e),e.tokenName&&Ht(e.tokenName);const t={page:e.page.toString(),limit:e.limit.toString()};void 0!==e.type&&(t.type=e.type),void 0!==e.tokenName&&(t.tokenName=e.tokenName),void 0!==e.search&&(t.search=e.search);const n=b(t),o=await this.http.get("/launchpad/fetch-pool",n);if(!0===o.error||200!==o.status||!o.data)throw R(o.message||"Failed to fetch pools",o.status);let i=[];const r=(await import("bignumber.js")).default;if(o.data.tokens)if(Array.isArray(o.data.tokens))i=o.data.tokens.map(e=>{const t=e.reverseBondingCurveMinFeePortion??"0",n=e.reverseBondingCurveMaxFeePortion??"0",o=!new r(t).isZero()||!new r(n).isZero();return{...e,reverseBondingCurveMinFeePortion:t,reverseBondingCurveMaxFeePortion:n,hasReverseBondingCurveFee:o,createdAt:e.created_at||e.createdAt||""}});else{const e=o.data.tokens,t=e.reverseBondingCurveMinFeePortion??"0",n=e.reverseBondingCurveMaxFeePortion??"0",a=!new r(t).isZero()||!new r(n).isZero();i=[{...e,reverseBondingCurveMinFeePortion:t,reverseBondingCurveMaxFeePortion:n,hasReverseBondingCurveFee:a,createdAt:e.created_at||e.createdAt||""}]}else o.data.pools&&Array.isArray(o.data.pools)&&(i=o.data.pools.map(e=>{const t=e.reverseBondingCurveMinFeePortion??"0",n=e.reverseBondingCurveMaxFeePortion??"0",o=!new r(t).isZero()||!new r(n).isZero();return{...e,reverseBondingCurveMinFeePortion:t,reverseBondingCurveMaxFeePortion:n,hasReverseBondingCurveFee:o,createdAt:e.created_at||e.createdAt||""}}));const{extractMetadataFromPoolData:a,isValidPoolForCaching:s}=await Promise.resolve().then(function(){return qi});i.forEach(e=>{if(!s(e))return void this.logger.debug("Skipping pool with invalid structure for caching",e);const t=a(e,this.logger);t&&this.warmCacheFromPoolData(e.tokenName,t)});const c=o.data.count??o.data.total??0,l=o.data.page??e.page??1,u=o.data.limit??e.limit??10,d=u>0?Math.ceil(c/u):1;return{pools:i,page:l,limit:u,total:c,totalPages:d,hasNext:l<d,hasPrevious:l>1}}async _getAmount(e){if(Xt(e),!this.galaChainHttp)throw K("GalaChain client not configured. Direct GalaChain calls require galaChainHttp client.","galaChainHttp");const{endpoint:t,body:n}=((e,t,n,o)=>{if("NATIVE"===e&&"IN"===t)return{endpoint:yi,body:{vaultAddress:n,tokenQuantity:o,IsPreMint:!1}};if("NATIVE"===e&&"OUT"===t)return{endpoint:wi,body:{vaultAddress:n,tokenQuantity:o,IsPreMint:!1}};if("MEME"===e&&"IN"===t)return{endpoint:ki,body:{vaultAddress:n,nativeTokenQuantity:o,IsPreMint:!1}};if("MEME"===e&&"OUT"===t)return{endpoint:vi,body:{vaultAddress:n,nativeTokenQuantity:o,IsPreMint:!1}};throw q("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,n);if(!Si(e))throw R("Malformed response data from GalaChain gateway");if(1!==e.Status)throw R(`GalaChain calculation failed with status ${e.Status}`,e.Status);const{calculatedQuantity:o,extraFees:i}=e.Data;return{amount:o,reverseBondingCurveFee:i.reverseBondingCurve,transactionFee:i.transactionFees,gasFee:"1"}}catch(o){throw this.logger.error(`GalaChain ${e.type}-${e.method} operation failed:`,{endpoint:t,requestBody:n,error:o instanceof Error?o.message:o}),o}}async checkPool(e){Qt(e),e.tokenName&&Ht(e.tokenName);const t=b(e),n=await this.http.get("/launchpad/check-pool",t);if(!0===n.error||200!==n.status)throw R(n.message||"Failed to check pool",n.status);const o=n.data;return e.symbol?o?.isSymbolExist??!1:e.tokenName?o?.isNameExist??!1:o?.exists??!1}async fetchVolumeData(e){if(!In(e))throw new P("Invalid options provided. Expected { tokenName: string, from?: number, to?: number, resolution?: number }","options","INVALID_OPTIONS");const{tokenName:t,from:n,to:o,resolution:i}=e;if(Ht(t),!n||!o||!i)throw new P("Graph options (from, to, resolution) are required","options","MISSING_GRAPH_OPTIONS");const r={tokenName:t,from:n,to:o,resolution:i};Zt(r);const a=b(r),s=await this.http.get("/launchpad/get-graph-data",a);if(!0===s.error||200!==s.status||!s.data)throw R(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 n={page:e.page||1,limit:e.limit||10};return e.search&&(n.search=e.search),e.tokenName&&(n.tokenName=e.tokenName),t&&(n.type=t),this.fetchPoolsFromAPI(n)}async isTokenNameAvailable(e){try{return!await this.checkPool({tokenName:e})}catch{return!1}}async isTokenSymbolAvailable(e){try{return!await this.checkPool({symbol:e})}catch{return!1}}async calculateBuyAmount(e){if(!e||"object"!=typeof e)throw new P("Invalid options provided. Expected an options object.","options","INVALID_OPTIONS");const{tokenName:t,amount:n,type:o,currentSupply:i}=e,r=e.mode??this.defaultCalculateAmountMode;if("local"!==r&&"external"!==r)throw new P(`Invalid calculation mode "${r}". Must be "local" or "external".`,"mode","INVALID_CALCULATION_MODE");if(!t||"string"!=typeof t)throw new P("Token name is required and must be a string","tokenName","INVALID_TOKEN_NAME");if(!n||"string"!=typeof n)throw new P("Amount is required and must be a string","amount","INVALID_AMOUNT");if(o!==bi.NATIVE&&o!==bi.EXACT)throw new P('Type must be either "native" or "exact"',"type","INVALID_TYPE");return"external"===r?this.calculateBuyAmountExternal({tokenName:t,amount:n,type:o}):this.calculateBuyAmountLocal(this.addIfDefined({tokenName:t,amount:n,type:o},"currentSupply",i))}async calculateBuyAmountExternal(e){const{tokenName:t,amount:n,type:o}=e,i=await this.tokenResolver.resolveTokenToVault(t);if(!i)throw new P(`Token "${t}" not found. Please verify the token name is correct.`,"tokenName","TOKEN_NOT_FOUND");return o===bi.EXACT?this._getAmount({type:"NATIVE",method:"IN",vaultAddress:i,amount:n}):this._getAmount({type:"MEME",method:"OUT",vaultAddress:i,amount:n})}async calculateSellAmount(e){const{tokenName:t,amount:n,type:o,currentSupply:i,maxSupply:r,reverseBondingCurveMaxFeeFactor:a,reverseBondingCurveMinFeeFactor:s}=e,c=e.mode??this.defaultCalculateAmountMode;if("local"!==c&&"external"!==c)throw new P(`Invalid calculation mode "${c}". Must be "local" or "external".`,"mode","INVALID_CALCULATION_MODE");if(!t||"string"!=typeof t)throw new P("Token name is required and must be a string","tokenName","INVALID_TOKEN_NAME");if(!n||"string"!=typeof n)throw new P("Amount is required and must be a string","amount","INVALID_AMOUNT");if(o!==bi.EXACT&&o!==bi.NATIVE)throw new P('Type must be either "exact" or "native"',"type","INVALID_TYPE");if("external"===c)return this.calculateSellAmountExternal({tokenName:t,amount:n,type:o});{const e={tokenName:t,amount:n,type:o,...void 0!==i&&{currentSupply:i},...void 0!==r&&{maxSupply:r},...void 0!==a&&{reverseBondingCurveMaxFeeFactor:a},...void 0!==s&&{reverseBondingCurveMinFeeFactor:s}};return this.calculateSellAmountLocal(e)}}async calculateSellAmountExternal(e){const{tokenName:t,amount:n,type:o}=e,i=await this.tokenResolver.resolveTokenToVault(t);if(!i)throw new P(`Token "${t}" not found. Please verify the token name is correct.`,"tokenName","TOKEN_NOT_FOUND");return o===bi.EXACT?this._getAmount({type:"NATIVE",method:"OUT",vaultAddress:i,amount:n}):this._getAmount({type:"MEME",method:"IN",vaultAddress:i,amount:n})}async calculateBuyAmountLocal(e){const{tokenName:t,amount:n,type:o,currentSupply:i}=e;if(!n||"string"!=typeof n)throw new P("Amount is required and must be a string","amount","INVALID_AMOUNT");if(o!==bi.NATIVE&&o!==bi.EXACT)throw new P('Type must be either "native" or "exact"',"type","INVALID_TYPE");void 0!==i&&Jt(i,"currentSupply");const r=!i;if(r&&!t)throw new P("Token name is required when currentSupply is not provided","tokenName","MISSING_TOKEN_NAME");t&&Ht(t);let a=i;if(r){a=(await this.fetchPoolDetailsForCalculation(t)).currentSupply}return o===bi.EXACT?Ei.calculateBuyWithExact(n,a):Ei.calculateBuyWithNative(n,a)}async calculateSellAmountLocal(e){const{tokenName:t,amount:n,type:o,currentSupply:i,maxSupply:r,reverseBondingCurveMaxFeeFactor:a,reverseBondingCurveMinFeeFactor:s}=e;if(!n||"string"!=typeof n)throw new P("Amount is required and must be a string","amount","INVALID_AMOUNT");if(o!==bi.EXACT&&o!==bi.NATIVE)throw new P('Type must be either "exact" or "native"',"type","INVALID_TYPE");void 0!==i&&Jt(i,"currentSupply");const c=!i||!r||void 0===a||void 0===s;if(c&&!t)throw new P("Token name is required when currentSupply, maxSupply, or fee factors are not provided","tokenName","MISSING_TOKEN_NAME");t&&Ht(t);let l=i,u=r,d=a,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 o===bi.EXACT?Ei.calculateSellWithExact(n,l,u,h,d):Ei.calculateSellWithNative(n,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 Sn(t,"tokenName")&&En(t)&&bn(t,"currentSupply")}(e))throw new Error("Invalid CalculateBuyAmountForGraduationOptions provided");const{tokenName:n,calculateAmountMode:o,currentSupply:i}=t;Ht(n);const r=await this.tokenResolver.resolveTokenToVault(n);if(!r)throw new P(I(n),"tokenName","VAULT_NOT_FOUND");if(!this.galaChainHttp)throw K("GalaChain HTTP client not configured");const a=await this.galaChainHttp.post("/api/asset/launchpad-contract/FetchSaleDetails",{vaultAddress:r});if(1!==a.Status)throw R(`Failed to fetch pool details: Status ${a.Status}`,a.Status);const s=a.Data,c=(await import("bignumber.js")).default,l=i??new c(s.maxSupply).minus(s.sellingTokenQuantity).toFixed(),u=s.sellingTokenQuantity;if("0"===u)throw new P(`Token ${n} is already graduated (no tokens remaining in pool)`,"tokenName","ALREADY_GRADUATED");const d={tokenName:n,amount:u,type:"exact",currentSupply:l,...void 0!==o&&{mode:o}};return await this.calculateBuyAmount(d)}async launchToken(e){if(!this.bundleHttp)throw K("Bundle backend client not configured. LaunchToken requires bundleHttp client.","bundleHttp");fi(e);const t=e.preBuyQuantity||"0";if(isNaN(Number(t))||Number(t)<0)throw new P("Pre-buy quantity must be a valid non-negative number string","preBuyQuantity","INVALID_PRE_BUY_QUANTITY");if(e.reverseBondingCurveConfiguration){const{minFeePortion:t,maxFeePortion:n}=e.reverseBondingCurveConfiguration,o=Number(t),i=Number(n);if(isNaN(o)||isNaN(i)||o<=0||i<=0||o>=i)throw new P("Reverse bonding curve configuration must have valid min/max fee portions with min < max","reverseBondingCurveConfiguration","INVALID_BONDING_CURVE_CONFIG")}let n="";if(e.tokenImage)if(e.tokenImage instanceof File||Buffer.isBuffer(e.tokenImage)){const t=await this.uploadImageByTokenName({tokenName:e.tokenName,options:{file:e.tokenImage,tokenName:e.tokenName}});if(!t)throw R("Image upload failed: No URL returned");n=t}else"string"==typeof e.tokenImage&&(n=e.tokenImage);const i=`galaswap - operation - ${c.v4()}-${Date.now()}-${this.http.getAddress()}`,r={tokenName:e.tokenName.trim(),tokenSymbol:e.tokenSymbol.trim().toUpperCase(),tokenDescription:e.tokenDescription.trim(),tokenImage:n.trim(),preBuyQuantity:t.toString(),tokenCategory:e.tokenCategory||"Unit",tokenCollection:e.tokenCollection||"Token",uniqueKey:i};e.websiteUrl?.trim()&&(r.websiteUrl=e.websiteUrl.trim()),e.telegramUrl?.trim()&&(r.telegramUrl=e.telegramUrl.trim()),e.twitterUrl?.trim()&&(r.twitterUrl=e.twitterUrl.trim()),e.reverseBondingCurveConfiguration&&(r.reverseBondingCurveConfiguration={minFeePortion:e.reverseBondingCurveConfiguration.minFeePortion.toString(),maxFeePortion:e.reverseBondingCurveConfiguration.maxFeePortion.toString()});const a=new Ti(r),s=await this.http.signWithGalaChain("CreateSale",a,o.SigningType.SIGN_TYPED_DATA),{signature:l,types:u,domain:d,prefix:h}=s,g={tokenName:a.tokenName,tokenSymbol:a.tokenSymbol,tokenDescription:a.tokenDescription,tokenImage:a.tokenImage,preBuyQuantity:a.preBuyQuantity,...a.websiteUrl&&{websiteUrl:a.websiteUrl},...a.telegramUrl&&{telegramUrl:a.telegramUrl},...a.twitterUrl&&{twitterUrl:a.twitterUrl},tokenCategory:a.tokenCategory,tokenCollection:a.tokenCollection,uniqueKey:a.uniqueKey,signature:l,types:u,domain:d,...h&&{prefix:h},...a.reverseBondingCurveConfiguration&&{reverseBondingCurveConfiguration:a.reverseBondingCurveConfiguration}},p=`${e.tokenName.trim()}$Unit$none$none`,m="GALA$Unit$none$none";let f;if(parseFloat(t)>0){const e=`$service$${p}$launchpad`;f=[e,`$token$${p}$${e}`,`$tokenBalance$${p}$${e}`,`$tokenBalance$${p}$${e}`,`$tokenBalance$${m}$${e}`,`$tokenBalance$${m}$${e}`]}else{const e=`$service$${p}$launchpad`;f=[e,`$token$${p}$${e}`,`$tokenBalance$${p}$${e}`]}const y={signedDto:g,stringsInstructions:f,method:"CreateSale"},w=await this.bundleHttp.post("/bundle",y);if(w.error||!w.data)throw R(w.message||"Token launch failed");return w.data}async fetchTokenDistribution(e){if(!e)throw M("tokenName","Token name");Ht(e);const t=await this.tokenResolver.resolveTokenToVault(e);if(!t)throw new P(I(e),"tokenName","VAULT_NOT_FOUND");this.metadataCache.set(e,{vaultAddress:t});const n=encodeURIComponent(t),o=await this.http.get(`/holders/${n}`);if(!0===o.error||200!==o.status||!o.data)throw R(o.message||"Failed to fetch token distribution",o.status);const i=o.data;if(!Array.isArray(i))throw R("Invalid API response: expected array of holders",o.status);for(const e of i){if(!e.owner||"string"!=typeof e.owner)throw R("Invalid holder data: missing or invalid owner field",o.status);if(!e.quantity||"string"!=typeof e.quantity)throw R("Invalid holder data: missing or invalid quantity field",o.status);const t=parseFloat(e.quantity);if(isNaN(t)||!isFinite(t))throw R(`Invalid holder quantity: "${e.quantity}"`,o.status)}const a=i.reduce((e,t)=>e.plus(t.quantity),new r(0));return{holders:i.map(e=>{const t=new r(e.quantity),n=a.isZero()?0:t.dividedBy(a).multipliedBy(100).toNumber();return{address:e.owner,balance:e.quantity,percentage:n}}),totalSupply:a.toFixed(),totalHolders:i.length,lastUpdated:new Date}}async fetchTokenBadges(e){if(!e)throw M("tokenName","Token name");Ht(e);const t=await this.http.get("/launchpad/get-badge/",{tokenName:e});if(t.error||!t.data)throw R(t.message||"Failed to fetch token badges");return{volumeBadges:t.data.volumeBadge||[],engagementBadges:t.data.engagementBadge||[]}}async hasTokenBadgeByTokenName(e){const{tokenName:t,badgeType:n,badgeName:o}=e;try{const e=await this.fetchTokenBadges(t);if(!e)return!1;const i=("volume"===n?e.volumeBadges:e.engagementBadges).find(e=>e.badgeName===o);return i?.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 P("Invalid pre-mint calculation data","data","INVALID_PRE_MINT_DATA");if(!this.galaChainHttp)throw K("GalaChain HTTP client not available. Please initialize SDK with galaChainBaseUrl.","galaChainHttp");try{const t={vaultAddress:"service|testToken",nativeTokenQuantity:e.nativeTokenQuantity,IsPreMint:!0},n=await this.galaChainHttp.post("/api/asset/launchpad-contract/CallMemeTokenOut",t);if(!Si(n))throw R("Malformed response data from GalaChain gateway");if(1!==n.Status)throw R(`GalaChain calculation failed with status ${n.Status}`,n.Status);const{calculatedQuantity:o,extraFees:i}=n.Data;return{amount:o,reverseBondingCurveFee:i.reverseBondingCurve,transactionFee:i.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 P(I(e),"tokenName","VAULT_NOT_FOUND");if(!this.galaChainHttp)throw K("GalaChain HTTP client not configured");const n=await this.galaChainHttp.post("/api/asset/launchpad-contract/FetchSaleDetails",{vaultAddress:t});if(1!==n.Status)throw R(`Failed to fetch pool details: Status ${n.Status}`,n.Status);const o=n.Data,i=new(0,(await import("bignumber.js")).default)(o.maxSupply).minus(o.sellingTokenQuantity).toFixed(),r=o.sellingTokenQuantity,a=o.maxSupply;let s=.5,c=0;o.reverseBondingCurveConfiguration?(s=parseFloat(o.reverseBondingCurveConfiguration.maxFeePortion),c=parseFloat(o.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:a,reverseBondingCurveMaxFeeFactor:s,reverseBondingCurveMinFeeFactor:c,reverseBondingCurveNetFeeFactor:l}),{currentSupply:i,remainingTokens:r,maxSupply:a,reverseBondingCurveMaxFeeFactor:s,reverseBondingCurveMinFeeFactor:c,reverseBondingCurveNetFeeFactor:l}}async fetchCurrentSupply(e){Ht(e);const t=await this.tokenResolver.resolveTokenToVault(e);if(!t)throw new P(I(e),"tokenName","VAULT_NOT_FOUND");if(!this.galaChainHttp)throw K("GalaChain HTTP client not configured");const n=await this.galaChainHttp.post("/api/asset/launchpad-contract/FetchSaleDetails",{vaultAddress:t});if(1!==n.Status)throw R(`Failed to fetch pool details: Status ${n.Status}`,n.Status);const o=n.Data,i=new(0,(await import("bignumber.js")).default)(o.maxSupply).minus(o.sellingTokenQuantity).toFixed(),r=o.maxSupply;return this.metadataCache.set(e,{maxSupply:r}),i}getAddress(){return this.http.getAddress()}formatAddressForBackend(e){return Yt(e)}validateTokenName(e){return Ht(e)}validatePagination(e){return Vt(e)}async fetchTokenPrice(e){if(!this.dexApiHttp)throw K("DEX API client not configured. Token price fetching requires dexApiHttp client.","dexApiHttp");if(!e||Array.isArray(e)&&0===e.length)throw M("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}}),n=[];return e.tokens&&Array.isArray(e.tokens)&&e.tokens.forEach(e=>{e.currentPrices&&e.symbol&&n.push({symbol:e.symbol,price:e.currentPrices.usd})}),n}catch(e){throw R(`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 Sn(t,"tokenName")&&En(t)&&bn(t,"currentSupply")}(e))throw new Error("Invalid FetchLaunchpadTokenSpotPriceOptions provided");const{tokenName:n,calculateAmountMode:o,currentSupply:i}=t;if(!n||"string"!=typeof n)throw M("tokenName","Token name (string)");try{const e={tokenName:n,amount:"1",type:"native",...void 0!==o&&{mode:o},...void 0!==i&&{currentSupply:i}},t=await this.calculateBuyAmount(e),r=(await this.fetchTokenPrice("GALA"))[0];if(!r)throw R("GALA price not available");const a=Number(t.amount);if(a<=0)throw new P(`Invalid token amount calculation: ${a}`,"amount","INVALID_CALCULATION");const s=r.price/a;return{symbol:n.toUpperCase(),price:s}}catch(e){if(e instanceof Error)throw new Error(`Failed to calculate launchpad token spot price for ${n}: ${e.message}`);throw new Error(`Failed to calculate launchpad token spot price for ${n}: ${String(e)}`)}}warmCacheFromPoolData(e,t){this.metadataCache.warmFromPoolData(e,t)}getCacheStats(){return this.metadataCache.stats()}clearCache(e){this.metadataCache.clear(e)}}const Pi={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 Ni(e){return Pi[e]}class Di extends Error{constructor(e,t){super(e),this.cause=t,this.name="WebSocketError"}}class Fi extends Error{constructor(e,t,n){super(`Transaction ${e} failed with status: ${t}${n?` - ${n}`:""}`),this.transactionId=e,this.status=t,this.name="TransactionFailedError"}}function Ci(e,t){if(!e)throw new Di(`Invalid WebSocket response received for transaction ${t}: response is null or undefined`);if("object"!=typeof e)throw new Di(`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 Di(`Invalid WebSocket response received for transaction ${t}: missing status field`)}function xi(e,t,n,o){Ci(e,t);const i=e,r=i.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 Di(`Invalid trade data received for transaction ${t}`);const a={transactionId:t,type:n,method:"native"===o.type?"native":"exact",inputAmount:r.inputQuantity||o.amount,outputAmount:r.outputQuantity||o.expectedAmount||"0",totalFees:r.totalFees||"0",tokenName:o.tokenName,vaultAddress:r.vaultAddress||"",timestamp:Date.now()};return void 0!==i.blockHash&&(a.blockHash=i.blockHash),void 0!==i.gasUsed&&(a.gasUsed=i.gasUsed),void 0!==o.slippageToleranceFactor&&(a.slippageTolerance=o.slippageToleranceFactor),a}const $i="4.0.1-beta.0";class Li{constructor(e){this.logger=e||new S({debug:!1,context:"LiquidityEventExtractor"})}walkPayloadForLiquidityEvents(e,t){const n=[],o=new WeakSet,i=(e,r=0)=>{if(r>50)this.logger.debug("Payload nesting exceeded maximum depth of 50");else if(e&&"string"!=typeof e&&"object"==typeof e){if(o.has(e))return;o.add(e);const a=this.extractLiquidityFromObject(e);a&&!t.has(a.transactionId)&&(n.push(a),t.add(a.transactionId));for(const t of Object.values(e))i(t,r+1)}};return i(e,0),n}extractLiquidityFromObject(e){const t=this.extractTransactionId(e);if(!t)return null;const n=e.Data,o=n&&"object"==typeof n&&!Array.isArray(n)?n:e,i=this.extractPositionId(o),r=this.extractPoolHash(o),a=this.extractAmounts(o),s=this.extractUserAddress(o),c=this.extractPoolFee(o);if(!(i&&r&&a&&s&&null!==c))return null;const l=this.extractPoolAlias(o),u=this.extractUserBalanceDelta(o),d=this.extractTimestamp(o),h=u?.token0Balance?.collection,g=u?.token1Balance?.collection,p={transactionId:t,positionId:i,poolHash:r,poolFee:c,amounts:a,userAddress:s};return void 0!==h&&(p.token0=h),void 0!==g&&(p.token1=g),void 0!==d&&(p.timestamp=d),void 0!==l&&(p.poolAlias=l),void 0!==u&&(p.userBalanceDelta=u),p}extractTransactionId(e){const t=["transactionId","txId","tx_id","hash","txHash","id"];for(const n of t){const t=e[n];if("string"==typeof t&&t.trim())return t}return null}extractPositionId(e){const t=["positionId","position_id","tokenId","nftId"];for(const n of t){const t=e[n];if("string"==typeof t&&t.trim())return t}return null}extractPoolHash(e){const t=["poolHash","pool_hash","poolId","pool"];for(const n of t){const t=e[n];if("string"==typeof t&&t.trim())return t}return null}extractPoolAlias(e){const t=["poolAlias","pool_alias"];for(const n of t){const t=e[n];if("string"==typeof t&&t.trim())return t}}extractAmounts(e){const t=e.amounts;if(Array.isArray(t)&&t.length>=2){const e=String(t[0]).trim(),n=String(t[1]).trim();if(e&&n)return[e,n]}const n=e.amount0||e.amount0Desired,o=e.amount1||e.amount1Desired;return void 0!==n&&void 0!==o?[String(n),String(o)]:null}extractUserAddress(e){const t=["userAddress","user","owner","from","sender","wallet","address"];for(const n of t){const t=e[n];if("string"==typeof t&&t.trim())return t}return null}extractPoolFee(e){const t=["poolFee","fee","feeTier","feeTierBps"];for(const n of t){const t=e[n];if("number"==typeof t)return this.normalizeFee(t);if("string"==typeof t){const e=Number(t);if(!Number.isNaN(e))return this.normalizeFee(e)}}return null}normalizeFee(e){return 1===e||1e4===e?1e4:.3===e||3e3===e?3e3:.05===e||500===e?500:Number.isInteger(e)?e:e<1?Math.round(1e4*e):e}extractTimestamp(e){const t=["timeStamp","timestamp","time","createdAt","date"];for(const n of t){const t=e[n];if("number"==typeof t)return t;if("string"==typeof t){const e=new Date(t).getTime();if(!Number.isNaN(e))return e}}}extractUserBalanceDelta(e){const t=e.userBalanceDelta;if(!t||"object"!=typeof t)return;const n=t,o=this.extractBalanceObject(n.token0Balance),i=this.extractBalanceObject(n.token1Balance);if(!o&&!i)return;const r={};return void 0!==o&&(r.token0Balance=o),void 0!==i&&(r.token1Balance=i),r}extractBalanceObject(e){if(!e||"object"!=typeof e)return;const t=e,n=t.collection,o=t.category,i=t.type,r=t.additionalKey,a=t.quantity,s=t.owner;return"string"==typeof n&&"string"==typeof o&&"string"==typeof i&&"string"==typeof r&&"string"==typeof a&&"string"==typeof s?{collection:n,category:o,type:i,additionalKey:r,quantity:a,owner:s}:void 0}}class Oi{constructor(e){this.wallet=e.wallet;let n=null,o="STAGE";e.env?(o=e.env,n=Ni(e.env)):e.baseUrl?.includes("prod")?(o="PROD",n=Ni("PROD")):(o="STAGE",n=Ni("STAGE")),this.environment=o,this.config={baseUrl:n.launchpadBaseUrl,galaChainBaseUrl:n.galaChainBaseUrl,bundleBaseUrl:n.bundleBaseUrl,webSocketUrl:n.webSocketUrl,dexApiBaseUrl:n.dexApiBaseUrl,dexBackendBaseUrl:n.dexBackendBaseUrl,launchpadFrontendUrl:n.launchpadFrontendUrl,timeout:3e4,debug:!1,...e},this.logger=new S({debug:this.config.debug??!1,context:"LaunchpadSDK"}),this.validateConfiguration(),this.slippageToleranceFactor=void 0===e.slippageToleranceFactor?Oi.DEFAULT_SLIPPAGE_TOLERANCE_FACTOR:this.parseSlippageToleranceFactor(e.slippageToleranceFactor),this.maxAcceptableReverseBondingCurveFeeSlippageFactor=void 0===e.maxAcceptableReverseBondingCurveFeeSlippageFactor?Oi.DEFAULT_MAX_ACCEPTABLE_REVERSE_BONDING_CURVE_FEE_SLIPPAGE_FACTOR:this.parseFeeSlippageFactor(e.maxAcceptableReverseBondingCurveFeeSlippageFactor),this.calculateAmountMode=e.calculateAmountMode||Oi.DEFAULT_CALCULATE_AMOUNT_MODE,this.pricingConcurrency=e.pricingConcurrency||5,this.auth=new T({wallet:e.wallet,messagePrefix:"Create a GalaChain Wallet"}),this.http=new A(this.auth,this.config),this.galaChainHttp=new A(this.auth,{...this.config,baseUrl:this.config.galaChainBaseUrl}),this.bundleHttp=new A(this.auth,{...this.config,baseUrl:this.config.bundleBaseUrl}),this.dexApiHttp=new A(this.auth,{...this.config,baseUrl:this.config.dexApiBaseUrl}),this.dexBackendHttp=new A(this.auth,{...this.config,baseUrl:this.config.dexBackendBaseUrl}),this.galaChainPublicAxios=t.create({baseURL:this.config.galaChainBaseUrl,timeout:this.config.timeout||3e4,headers:{"Content-Type":"application/json",Accept:"application/json"}}),this.cache=new ni(e.debug||!1),this.launchpadService=new Hn(this.http),this.tokenResolverService=new Go(this.launchpadService.poolService),this.launchpadAPI=new Ii(this.http,this.tokenResolverService,this.logger,this.bundleHttp,this.galaChainHttp,this.dexApiHttp,this.calculateAmountMode),this.galaChainService=new Ao(this.galaChainHttp,e.wallet,this.tokenResolverService,e.debug||!1,this.galaChainPublicAxios),this.dexService=new Eo(this.dexBackendHttp,this.cache,this.galaChainService,e.debug||!1),this.bundleService=new Uo(this.bundleHttp,this.tokenResolverService,this.config.debug||!1,e.wallet,e.wallet?this.getAddress():void 0,this.slippageToleranceFactor,this.maxAcceptableReverseBondingCurveFeeSlippageFactor),this.websocketService=new Ko({url:this.config.webSocketUrl},this.config.debug),this.priceHistoryService=new jo(this.dexBackendHttp,this.config.debug||!1,this.tokenResolverService),this.dexQuoteService=new ri(this.galaChainHttp,this.config.galaChainBaseUrl,e.debug||!1,e.dexQuoteNetworkTimeout||3e4),this.gswapService=new ei({privateKey:e.wallet?.privateKey,getWalletAddress:()=>this.wallet?this.getAddress():void 0,gatewayBaseUrl:this.config.galaChainBaseUrl,bundlerBaseUrl:this.config.bundleBaseUrl,galaChainBaseUrl:this.config.galaChainBaseUrl,dexBackendBaseUrl:this.config.dexBackendBaseUrl,dexBackendHttp:this.dexBackendHttp},this.websocketService,this.dexQuoteService),this.dexPoolService=new oi(this.dexBackendHttp,this.config.dexBackendBaseUrl,this.gswapService,this.pricingConcurrency,e.debug||!1)}createOverrideSdk(e){if(!e||"string"!=typeof e)throw K("Invalid privateKey: must be a non-empty string","privateKey");if(!e.match(/^0x[a-fA-F0-9]{64}$/))throw K('Invalid privateKey format: must be "0x" followed by 64 hexadecimal characters',"privateKey");const t=new n.Wallet(e),o={...this.config,wallet:t};return new Oi(o)}getAddress(){return this.validateWallet(),this.auth.getAddress()}getEthereumAddress(){return this.validateWallet(),this.wallet.address}validateWallet(){if(!this.wallet)throw new P("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 P("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,environment:this.environment,slippageToleranceFactor:this.slippageToleranceFactor,maxAcceptableReverseBondingCurveFeeSlippageFactor:this.maxAcceptableReverseBondingCurveFeeSlippageFactor,calculateAmountMode:this.calculateAmountMode}}getVersion(){return $i}getUrlByTokenName(e){const t=this.config.launchpadFrontendUrl;if(!t)throw K("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 fetchCompositePoolData(e){return this.dexQuoteService.fetchCompositePoolData(e)}async calculateDexPoolQuoteExactAmountLocal(e){return this.dexQuoteService.calculateDexPoolQuoteExactAmountLocal(e)}async calculateDexPoolQuoteExactAmountExternal(e){return this.dexQuoteService.calculateDexPoolQuoteExactAmountExternal(e)}async calculateDexPoolQuoteExactAmount(e,t="local"){return this.dexQuoteService.calculateDexPoolQuoteExactAmount(e,t)}async fetchTokenDistribution(e){return this.launchpadService.fetchTokenDistribution(e)}async fetchTokenBadges(e){return this.launchpadService.fetchTokenBadges(e)}async fetchTokenPrice(e){const{tokenName:t,tokenId:n,currentSupply:o,calculateAmountMode:i}=e;if(t&&!n){const e={tokenName:t};return o&&(e.currentSupply=o),i&&(e.calculateAmountMode=i),this.fetchLaunchpadTokenSpotPrice(e)}if(n&&!t)try{return await this.dexService.fetchTokenPrice({tokenId:n})}catch(e){const t=function(e){if(Bn(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(n)).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 o&&(r.currentSupply=o),i&&(r.calculateAmountMode=i),this.fetchLaunchpadTokenSpotPrice(r)}catch(t){throw this.logger.error(`Launchpad fallback failed: ${_n(t)}`),e}}throw e}if(!t&&!n)throw M("tokenName or tokenId","Either tokenName (for launchpad tokens) or tokenId (for DEX tokens) is required");throw new P("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 fetchAllDexSeasons(){return this.dexService.fetchAllDexSeasons()}async fetchCurrentDexSeason(){return this.dexService.fetchCurrentDexSeason()}async fetchDexLeaderboardBySeasonId(e){return this.dexService.fetchDexLeaderboardBySeasonId(e)}async fetchCurrentDexLeaderboard(){return this.dexService.fetchCurrentDexLeaderboard()}async fetchDexAggregatedVolumeSummary(){return this.dexService.fetchDexAggregatedVolumeSummary()}async fetchLaunchTokenFee(){return this.galaChainService.fetchLaunchTokenFee()}async fetchTokenClassesWithSupply(e){return this.galaChainService.fetchTokenClassesWithSupply(e)}async fetchPoolDetails(e){const t=await this.resolveVaultAddress(e);if(!t)throw new Error(I(e));const n=(await this.galaChainService.fetchPoolDetails({vaultAddress:t})).Data,o=await this.launchpadAPI.fetchPoolDetailsForCalculation(e);return n.currentSupply=o.currentSupply,n.reverseBondingCurveMaxFeeFactor=o.reverseBondingCurveMaxFeeFactor,n.reverseBondingCurveMinFeeFactor=o.reverseBondingCurveMinFeeFactor,n.reverseBondingCurveNetFeeFactor=o.reverseBondingCurveNetFeeFactor,n.tokenName=e,n}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 en}),n=t(e)||this.getAddress();return this.galaChainService.fetchGalaBalance({owner:n,collection:"GALA",category:"Unit",additionalKey:"none",type:"none",instance:"0"})}async fetchTokenBalance(e){const{normalizeAddressInput:t}=await Promise.resolve().then(function(){return en}),n=t(e.address);if(e.tokenId){const{normalizeToTokenInstanceKey:t}=await Promise.resolve().then(function(){return yo}),o=t(e.tokenId),{collection:i,category:r,type:a,additionalKey:s}=o;return this.galaChainService.fetchTokenBalance({owner:n,collection:i,category:r,additionalKey:s,type:a,instance:"0"})}if(e.tokenName){const t=(await this.fetchTokensHeld({tokenName:e.tokenName,page:1,limit:1,...n&&{address:n}})).tokens[0];return t?{quantity:t.quantity,collection:t.collection||"Token",category:"Unit",tokenId:`${t.collection||"Token"}|Unit|${t.symbol}|none`,symbol:t.symbol,name:t.name}:null}throw M("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:n,maxAcceptableReverseBondingCurveFeeSlippageFactor:o,privateKey:i,calculateAmountMode:r,currentSupply:a}=e;let s=t;void 0===r&&void 0===a||(s={tokenName:t,...void 0!==r&&{calculateAmountMode:r},...void 0!==a&&{currentSupply:a}});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!==n&&(l.slippageToleranceFactor=n),void 0!==o&&(l.maxAcceptableReverseBondingCurveFeeSlippageFactor=o),void 0!==i&&(l.privateKey=i),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:n,...o}=e;return t.buy(o)}this.validateWallet(),await this.ensureWebSocketConnection();const t=(await this.bundleService.buyToken(e)).data,n=t?.transactionId;if(!n)throw G("No transaction ID returned from buy operation");return this.waitForConfirmation(n,t=>xi(t,n,"buy",e))}async sell(e){if(e.privateKey){const t=this.createOverrideSdk(e.privateKey),{privateKey:n,...o}=e;return t.sell(o)}this.validateWallet(),await this.ensureWebSocketConnection();const t=(await this.bundleService.sellToken(e)).data,n=t?.transactionId;if(!n)throw G("No transaction ID returned from sell operation");return this.waitForConfirmation(n,t=>xi(t,n,"sell",e))}async getBundlerTransactionResult(e){return this.bundleService.getBundlerTransactionResult(e)}async postComment(e){if(e.privateKey){const t=this.createOverrideSdk(e.privateKey),{privateKey:n,...o}=e;return t.postComment(o)}return this.validateWallet(),this.launchpadService.postComment(e)}async launchToken(e){if(e.privateKey){const t=this.createOverrideSdk(e.privateKey),{privateKey:n,...o}=e;return t.launchToken(o)}this.validateWallet(),await this.ensureWebSocketConnection();const t=await this.launchpadAPI.launchToken(e);return this.waitForConfirmation(t,n=>{Ci(n,t);const o=n?.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)}(o))throw new Di(`Invalid launch data received for transaction ${t}`);const i={transactionId:t,vaultAddress:o.vaultAddress||"",tokenStringKey:o.tokenStringKey||"",tokenName:e.tokenName,tokenSymbol:e.tokenSymbol,creatorAddress:o.creatorAddress||this.getAddress(),timestamp:Date.now(),...n.blockHash&&{blockHash:n.blockHash},...n.gasUsed&&{gasUsed:n.gasUsed}};return"string"==typeof e.tokenImage&&(i.tokenImage=e.tokenImage),void 0!==e.preBuyQuantity&&(i.preBuyQuantity=e.preBuyQuantity),i.vaultAddress&&this.tokenResolverService.set(e.tokenName,i.vaultAddress),i})}async uploadTokenImage(e){if(e.privateKey){const t=this.createOverrideSdk(e.privateKey),{privateKey:n,...o}=e;return t.uploadTokenImage(o)}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 en}),n=t(e)||this.getAddress();return this.launchpadService.fetchProfile(n)}async updateProfile(e){const{normalizeAddressInput:t}=await Promise.resolve().then(function(){return en}),n={...e,address:t(e.address)};if(n.privateKey){const e=this.createOverrideSdk(n.privateKey),{privateKey:t,...o}=n;return e.updateProfile(o)}return this.validateWallet(),this.launchpadService.updateProfile(n)}async uploadProfileImage(e){const{normalizeAddressInput:t}=await Promise.resolve().then(function(){return en}),n={...e,address:t(e.address)||this.getAddress()};if(n.privateKey){const e=this.createOverrideSdk(n.privateKey),{privateKey:t,...o}=n;return e.uploadProfileImage(o)}return this.validateWallet(),this.launchpadService.uploadProfileImage(n)}async retrieveGalaFromFaucet(e){const{normalizeAddressInput:t}=await Promise.resolve().then(function(){return en}),n={walletAddress:t(e)||this.getAddress(),amount:"5"};return this.launchpadService.transferFaucets(n)}async fetchTokensHeld(e){const{normalizeAddressInput:t}=await Promise.resolve().then(function(){return en}),n=t(e?.address)||this.getAddress(),o={page:e?.page||1,limit:e?.limit||10,address:n};return e?.tokenName&&(o.tokenName=e.tokenName),e?.search&&(o.search=e.search),this.launchpadService.fetchTokensHeld(o)}async fetchTokensCreated(e){const{normalizeAddressInput:t}=await Promise.resolve().then(function(){return en}),n={type:"DEFI",address:t(e?.address)||this.getAddress(),page:e?.page||1,limit:e?.limit||10};return e?.tokenName&&(n.tokenName=e.tokenName),e?.search&&(n.search=e.search),this.launchpadService.fetchTokenList(n)}async 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 en}),n={...e,recipientAddress:t(e.recipientAddress)};if(n.privateKey){const e=this.createOverrideSdk(n.privateKey),{privateKey:t,...o}=n;return e.transferGala(o)}return this.validateWallet(),this.galaChainService.transferGala(n)}async transferToken(e){const{normalizeAddressInput:t}=await Promise.resolve().then(function(){return en}),n={...e,to:t(e.to)};if(n.privateKey){const e=this.createOverrideSdk(n.privateKey),{privateKey:t,...o}=n;return e.transferToken(o)}return this.validateWallet(),this.galaChainService.transferToken(n)}async resolveTokenClassKey(e){return this.galaChainService.resolveTokenClassKey(e)}async lockToken(e){if(e.lockAuthority){const{normalizeAddressInput:t}=await Promise.resolve().then(function(){return en});e={...e,lockAuthority:t(e.lockAuthority)}}if(e.privateKey){const t=this.createOverrideSdk(e.privateKey),{privateKey:n,...o}=e;return t.lockToken(o)}return this.validateWallet(),this.galaChainService.lockToken(e)}async unlockToken(e){if(e.privateKey){const t=this.createOverrideSdk(e.privateKey),{privateKey:n,...o}=e;return t.unlockToken(o)}return this.validateWallet(),this.galaChainService.unlockToken(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 K("baseUrl is required in configuration","baseUrl");if(!this.config.webSocketUrl)throw K("webSocketUrl is required in configuration","webSocketUrl");try{new URL(this.config.baseUrl)}catch{throw K(`Invalid baseUrl format: ${this.config.baseUrl}`,"baseUrl")}try{new URL(this.config.webSocketUrl)}catch{throw K(`Invalid webSocketUrl format: ${this.config.webSocketUrl}`,"webSocketUrl")}if(this.config.galaChainBaseUrl)try{new URL(this.config.galaChainBaseUrl)}catch{throw K(`Invalid galaChainBaseUrl format: ${this.config.galaChainBaseUrl}`,"galaChainBaseUrl")}if(this.config.bundleBaseUrl)try{new URL(this.config.bundleBaseUrl)}catch{throw K(`Invalid bundleBaseUrl format: ${this.config.bundleBaseUrl}`,"bundleBaseUrl")}if(this.config.launchpadFrontendUrl)try{new URL(this.config.launchpadFrontendUrl)}catch{throw K(`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: ${Oi.DEFAULT_SLIPPAGE_TOLERANCE_FACTOR}`),Oi.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: ${Oi.DEFAULT_MAX_ACCEPTABLE_REVERSE_BONDING_CURVE_FEE_SLIPPAGE_FACTOR}`),Oi.DEFAULT_MAX_ACCEPTABLE_REVERSE_BONDING_CURVE_FEE_SLIPPAGE_FACTOR):t}async ensureWebSocketConnection(){this.websocketService.isConnected()||(await this.websocketService.connect(),this.logger.debug("WebSocket connection established"))}async waitForConfirmation(e,t){this.logger.debug(`Waiting for confirmation of transaction: ${e}`);try{const n=await this.websocketService.waitForTransaction(e);if("completed"!==n.status)throw new Fi(e,n.status,n.message);let o;try{o=t(n)}catch(t){if(t instanceof Di)throw t;throw new Di(`Failed to transform WebSocket response for transaction ${e}`,t instanceof Error?t:new Error(String(t)))}return this.logger.debug(`Transaction confirmed: ${e}`,o),o}catch(t){if(this.logger.error(`Transaction confirmation failed: ${e}`,t),t instanceof Fi||t instanceof Di)throw t;throw new Di(`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:n}=await Promise.resolve().then(function(){return qi});e.forEach(e=>{if(!n(e))return;const o=t(e,this.logger);o&&this.launchpadAPI.warmCacheFromPoolData(e.tokenName,o)})}async getSwapQuoteExactInput(e,t,n){return this.gswapService.getSwapQuoteExactInput({fromToken:e,toToken:t,amount:n})}async getSwapQuoteExactOutput(e,t,n){return this.gswapService.getSwapQuoteExactOutput({fromToken:e,toToken:t,amount:n})}async executeSwap(e,t,n,o,i,r=.01){return this.validateWallet(),this.gswapService.executeSwap({fromToken:e,toToken:t,inputAmount:n,estimatedOutput:o,feeTier:i,slippageTolerance:r})}async getSwapUserAssets(e){return this.gswapService.getUserAssets(e)}async getAllSwapUserAssets(e){return this.gswapService.getAllUserAssets(e)}async getSwapPoolInfo(e,t){return this.gswapService.getPoolInfo(e,t)}async getSwapPoolPrice(e,t,n){return this.gswapService.getPositionCurrentPrice({token0:e,token1:t,feeTier:n})}async getSwapUserLiquidityPositions(e,t,n,o){let i,r;"string"==typeof n?(i=n,r=o):"object"==typeof n?r=n:o&&(r=o);return await this.gswapService.getUserLiquidityPositions(e,t,i,r)}async getAllSwapUserLiquidityPositions(e,t){const n=await this.gswapService.getAllSwapUserLiquidityPositions(e,t);if(!t?.withPrices){if(Array.isArray(n))return n;if(n&&"items"in n)return n.items}return n}async getSwapLiquidityPosition(e,t){return this.gswapService.getLiquidityPosition(e,t)}async getSwapLiquidityPositionById(e,t,n,o,i,r,a){return this.gswapService.getLiquidityPositionById(e,t,n,o,i,r,a)}async fetchSwapPositionDirect(e){return this.gswapService.fetchSwapPositionDirect(e)}async getSwapEstimateRemoveLiquidity(e){return this.gswapService.estimateRemoveLiquidity(e)}async addSwapLiquidityByPrice(e){return this.gswapService.addLiquidityByPrice(e)}async addSwapLiquidityByTicks(e){this.validateWallet();const t={...e,fee:e.feeTier??e.fee};return this.gswapService.addSwapLiquidityByTicks(t)}async removeSwapLiquidity(e){return this.validateWallet(),this.gswapService.removeLiquidity(e)}async collectSwapPositionFees(e){return this.validateWallet(),this.gswapService.collectPositionFees(e)}connectWebSocket(){this.websocketService.connect()}disconnectWebSocket(){this.websocketService.disconnect()}isWebSocketConnected(){return this.websocketService.isConnected()}subscribeToEvent(e,t){const n=this.websocketService.getSocket();return n?(n.on(e,t),()=>{n.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 n=1e3,o=Math.max(t?.intervalMs??3e4,n);t?.intervalMs&&t.intervalMs<n&&this.logger.warn(`Poll interval ${t.intervalMs}ms is below minimum 1000ms. Using minimum interval instead.`);const i=t?.minTVL,r=t?.tokens,a=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 n=(e=>`${e.token0}-${e.token1}-${e.fee}`)(t);if(!a.has(n)){if((e=>{if(a.set(e,!0),a.size>1e3){const e=a.keys().next().value;void 0!==e&&a.delete(e)}})(n),i){if((t.token0Tvl+t.token1Tvl)/2<i)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=o*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 n=1e3,o=Math.max(t?.intervalMs??3e4,n);t?.intervalMs&&t.intervalMs<n&&this.logger.warn(`Poll interval ${t.intervalMs}ms is below minimum 1000ms. Using minimum interval instead.`);const i=t?.creatorAddress,r=new Map;let a=!0,s=null;let c=0;const l=async()=>{if(a){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),i&&t.creatorAddress!==i||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(a){const e=Math.min(Math.max(c-1,0),2),t=o*Math.pow(2,e);s=setTimeout(l,t)}}};return l(),()=>{a=!1,s&&clearTimeout(s),this.logger.debug("Stopped watching for launchpad token creation")}}normalizeFee(e){if(null==e)return null;const t="number"==typeof e?e:Number.parseFloat(String(e).replace("%","").trim());return Number.isNaN(t)?null:1===t||1e4===t?1e4:.3===t||3e3===t?3e3:.05===t||500===t?500:!Number.isInteger(t)||500!==t&&3e3!==t&&1e4!==t?null:t}extractField(e,...t){if("object"!=typeof e||null===e)return"";const n=e;for(const e of t)if(n[e])return String(n[e]);return""}looksLikePoolPair(e){if("string"!=typeof e)return null;const t=e.trim();if(!t.includes("/"))return null;const n=t.split("/");return 3!==n.length?null:n[0]&&n[1]&&n[2]?t:null}buildPoolPairFromObject(e){if("object"!=typeof e||null===e)return null;const t=e,n=this.extractField(t,"token0ClassKey","token0Class","token0","token0Symbol")||"",o=this.extractField(t,"token1ClassKey","token1Class","token1","token1Symbol")||"",i=this.normalizeFee(t.feeTier??t.fee??t.feeTierBps??t.liquidityFeeBps??t.feeBps);return n&&o&&null!==i?`${n}/${o}/${i}`:null}parsePoolPairString(e){const t=e.split("/");if(3!==t.length)return null;const n=t[0].split("|")[0],o=t[1].split("|")[0],i=t[2];return n&&o&&i?{token0:n,token1:o,fee:i,poolPair:e}:null}serializeBalanceToken(e){if(!e||"object"!=typeof e)return"";const t=e;return[(t.collection??t.token??"")||"",(t.category??"")||"none",(t.type??"")||"none",(t.additionalKey??"none")||"none"].join("|")}buildPoolPairFromBalances(e){if("object"!=typeof e||null===e)return null;const t=e,n=t.userBalanceDelta??t.balanceDelta??t.delta;if(!n||"object"!=typeof n||null===n)return null;const o=n,i=o.token0Balance??o.token0??o.baseBalance??o.primaryBalance,r=o.token1Balance??o.token1??o.quoteBalance??o.secondaryBalance,a=this.serializeBalanceToken(i),s=this.serializeBalanceToken(r),c=this.normalizeFee(t.poolFee??t.feeTier??t.fee??t.feeTierBps??t.liquidityFeeBps);return a&&s&&null!==c?`${a}/${s}/${c}`:null}extractPoolDataFromPayload(e){if("string"==typeof e){const t=this.looksLikePoolPair(e);return t?this.parsePoolPairString(t):null}if("object"!=typeof e||null===e)return null;const t=e,n=this.looksLikePoolPair(t.poolPair);if(n)return this.parsePoolPairString(n);const o=this.buildPoolPairFromBalances(t);if(o)return this.parsePoolPairString(o);const i=this.buildPoolPairFromObject(t);if(i)return this.parsePoolPairString(i);if(t.pool&&"object"==typeof t.pool&&null!==t.pool){const e=this.extractPoolDataFromPayload(t.pool);if(e)return e}return null}matchesPoolFilter(e,t){if(t?.tokenFilter){if(!(e.token0===t.tokenFilter||e.token1===t.tokenFilter))return!1}if(t?.pairTokens){const[n,o]=t.pairTokens,i=e.token0===n||e.token1===n,r=e.token0===o||e.token1===o;if(!i||!r||n===o)return!1}if(void 0!==t?.feeTierFilter){if(this.normalizeFee(t.feeTierFilter)!==this.normalizeFee(e.fee))return!1}return!0}matchesCreatorFilter(e,t){return!t||e.creatorAddress===t}subscribeToTokenCreations(e,t){if(this.logger.debug("Subscribing to token creation broadcasts"+(t?.creatorFilter?` (filter: ${t.creatorFilter})`:"")),!this.websocketService)throw new Error("WebSocket service not initialized");let n=!1,o=null;const i=(n,...o)=>{try{if(o.length>0&&"object"==typeof o[0]&&null!==o[0]){const n=o[0].data;if(n&&n.Data&&"object"==typeof n.Data){const o=n.Data;if("CreateSale"===o.functionName){const n={tokenName:o.tokenName||"",symbol:o.symbol||"",creatorAddress:o.creatorAddress||"",description:o.description||"",image:o.image||"",vaultAddress:o.vaultAddress||"",tokenStringKey:o.tokenStringKey||"",preBuyQuantity:o.initialBuyQuantity||"0",websiteUrl:o.websiteUrl||"",telegramUrl:o.telegramUrl||"",twitterUrl:o.twitterUrl||"",isFinalized:o.isFinalized||!1};this.matchesCreatorFilter(n,t?.creatorFilter)&&e(n)}}}}catch(e){this.logger.warn(`Error processing token creation broadcast: ${e instanceof Error?e.message:String(e)}`)}};let r=this.websocketService.getSocket();if(r)r.onAny(i),n=!0,this.logger.debug("Token creation broadcast listener registered");else{this.logger.debug("WebSocket not yet connected, initiating connection..."),this.websocketService.connect().catch(e=>{const n=new Error(`WebSocket connection failed: ${e instanceof Error?e.message:String(e)}`);this.logger.warn("Failed to establish WebSocket connection:",e),t?.onError&&t.onError(n)});let e=0;const a=()=>{if(r=this.websocketService.getSocket(),!r&&e<Oi.TOKEN_CREATION_SOCKET_WAIT_ATTEMPTS)return e++,void(o=setTimeout(()=>a(),Oi.TOKEN_CREATION_SOCKET_POLL_INTERVAL_MS));if(!r&&e>=Oi.TOKEN_CREATION_SOCKET_WAIT_ATTEMPTS){const e=new Error(`WebSocket not available after ${Oi.TOKEN_CREATION_SOCKET_WAIT_ATTEMPTS*Oi.TOKEN_CREATION_SOCKET_POLL_INTERVAL_MS}ms`);return this.logger.warn("Token creation broadcast subscription timeout:",e.message),void(t?.onError&&t.onError(e))}r&&(r.onAny(i),n=!0,this.logger.debug("Token creation broadcast listener registered"))};a()}return()=>{try{if(null!==o&&(clearTimeout(o),o=null,this.logger.debug("Cleared token creation broadcast polling timeout")),!n)return void this.logger.debug("Cleanup called before listener registration - no action needed");const e=this.websocketService.getSocket();e&&(e.offAny(i),n=!1,this.logger.debug("Stopped listening to token creation broadcasts"))}catch(e){this.logger.warn("Error removing token creation listener:",e)}}}walkPayloadForPools(e,t,n=new WeakSet){const o=[];if("string"==typeof e){const n=this.looksLikePoolPair(e);if(n){const e=this.parsePoolPairString(n);e&&!t.has(e.poolPair)&&(t.add(e.poolPair),o.push(e))}return o}if("object"!=typeof e||null===e)return o;if(n.has(e))return o;n.add(e);const i=this.extractPoolDataFromPayload(e);i&&!t.has(i.poolPair)&&(t.add(i.poolPair),o.push(i));for(const i of Object.values(e)){const e=this.walkPayloadForPools(i,t,n);o.push(...e)}return o}subscribeToDexPoolAdded(e,t){if(this.logger.debug("Subscribing to DEX pool creation broadcasts"+(t?.tokenFilter?` (filter: ${t.tokenFilter})`:t?.pairTokens?` (pair: ${t.pairTokens.join("/")})`:"")),!this.websocketService)throw new Error("WebSocket service not initialized");let n=!1,o=null;const i=new Set,r=(n,...o)=>{try{for(const n of o){const o=this.walkPayloadForPools(n,i);for(const n of o)this.matchesPoolFilter(n,t)&&e(n)}}catch(e){this.logger.warn(`Error processing DEX pool broadcast: ${e instanceof Error?e.message:String(e)}`)}};let a=this.websocketService.getSocket();if(a)a.onAny(r),n=!0,this.logger.debug("DEX pool broadcast listener registered");else{this.logger.debug("WebSocket not yet connected, initiating connection..."),this.websocketService.connect().catch(e=>{const n=new Error(`WebSocket connection failed: ${e instanceof Error?e.message:String(e)}`);this.logger.warn("Failed to establish WebSocket connection:",e),t?.onError&&t.onError(n)});let e=0;const i=()=>{if(a=this.websocketService.getSocket(),!a&&e<Oi.DEX_POOL_SOCKET_WAIT_ATTEMPTS)return e++,void(o=setTimeout(()=>i(),Oi.DEX_POOL_SOCKET_POLL_INTERVAL_MS));if(!a&&e>=Oi.DEX_POOL_SOCKET_WAIT_ATTEMPTS){const e=new Error(`WebSocket not available after ${Oi.DEX_POOL_SOCKET_WAIT_ATTEMPTS*Oi.DEX_POOL_SOCKET_POLL_INTERVAL_MS}ms`);return this.logger.warn("DEX pool subscription timeout:",e.message),void(t?.onError&&t.onError(e))}a&&(a.onAny(r),n=!0,this.logger.debug("DEX pool broadcast listener registered"))};i()}return()=>{try{if(null!==o&&(clearTimeout(o),o=null,this.logger.debug("Cleared DEX pool polling timeout")),!n)return void this.logger.debug("Cleanup called before listener registration - no action needed");const e=this.websocketService.getSocket();e&&(e.offAny(r),n=!1,this.logger.debug("Stopped listening to DEX pool broadcasts"))}catch(e){this.logger.warn("Error removing DEX pool listener:",e)}}}subscribeToDexSwapExecuted(e,t){if(this.logger.debug("Subscribing to DEX swap execution broadcasts"+(t?.tokenFilter?` (filter: ${t.tokenFilter})`:t?.pairTokens?` (pair: ${t.pairTokens.join("/")})`:"")),!this.websocketService)throw new Error("WebSocket service not initialized");let n=null,o=null,i=null,r=!1;const a=async e=>{const t=si.parsePoolKey(e);if(!t)throw new Error(`Invalid pool key format: ${e}`);return await this.dexQuoteService.fetchCompositePoolData({token0:t.token0,token1:t.token1,fee:t.feeTier})},s=()=>{const c=this.websocketService.getSocket();if(!c)return this.logger.debug("WebSocket not yet ready for swap monitoring, polling..."),void(n=setTimeout(()=>s(),100));r=!0,o=new pi(c,a,this.dexQuoteService,t||{},this.logger),i=o.subscribe(t||{},e),this.logger.debug("DEX swap monitoring subscription established")};return this.websocketService.getSocket()||(this.logger.debug("WebSocket not yet connected, initiating connection for swap monitoring..."),this.websocketService.connect().catch(e=>{const n=new Error(`WebSocket connection failed for swap monitoring: ${e instanceof Error?e.message:String(e)}`);this.logger.warn("Failed to establish WebSocket connection:",e),t?.onError&&t.onError(n)})),s(),()=>{try{n&&clearTimeout(n),i&&r&&i(),o&&o.shutdown().catch(e=>{this.logger.warn("Error shutting down swap monitor:",e)})}catch(e){this.logger.warn("Error cleaning up swap monitor:",e)}}}subscribeToDexLiquidityAdded(e,t){return this.subscribeToDexLiquidityEvents(e,t)}subscribeToDexLiquidityRemoved(e,t){return this.subscribeToDexLiquidityEvents(e,t)}subscribeToDexLiquidityChanged(e,t){return this.subscribeToDexLiquidityEvents(e,t)}subscribeToDexLiquidityEvents(e,t){if(this.logger.debug("Subscribing to DEX liquidity broadcasts"+(t?.tokenFilter?` (filter: ${t.tokenFilter})`:t?.pairTokens?` (pair: ${t.pairTokens.join("/")})`:"")),!this.websocketService)throw new Error("WebSocket service not initialized");let n=!1,o=null;const i=new Set,r=new Li(this.logger),a=(n,...o)=>{try{for(const n of o){const o=r.walkPayloadForLiquidityEvents(n,i);for(const n of o)if(this.matchesLiquidityFilter(n,t))try{const t=e(n);t instanceof Promise&&t.catch(e=>{this.logger.warn(`Error in liquidity event callback: ${e instanceof Error?e.message:String(e)}`)})}catch(e){this.logger.warn(`Error in liquidity event callback: ${e instanceof Error?e.message:String(e)}`)}}}catch(e){this.logger.warn(`Error processing DEX liquidity broadcast: ${e instanceof Error?e.message:String(e)}`)}};let s=this.websocketService.getSocket();if(s)s.onAny(a),n=!0,this.logger.debug("DEX liquidity broadcast listener registered");else{this.logger.debug("WebSocket not yet connected, initiating connection for liquidity monitoring..."),this.websocketService.connect().catch(e=>{const n=new Error(`WebSocket connection failed: ${e instanceof Error?e.message:String(e)}`);this.logger.warn("Failed to establish WebSocket connection:",e),t?.onError&&t.onError(n)});let e=0;const i=()=>{if(s=this.websocketService.getSocket(),!s&&e<Oi.DEX_POOL_SOCKET_WAIT_ATTEMPTS)return e++,void(o=setTimeout(()=>i(),Oi.DEX_POOL_SOCKET_POLL_INTERVAL_MS));if(!s&&e>=Oi.DEX_POOL_SOCKET_WAIT_ATTEMPTS){const e=new Error(`WebSocket not available after ${Oi.DEX_POOL_SOCKET_WAIT_ATTEMPTS*Oi.DEX_POOL_SOCKET_POLL_INTERVAL_MS}ms`);return this.logger.warn("DEX liquidity subscription timeout:",e.message),void(t?.onError&&t.onError(e))}s&&(s.onAny(a),n=!0,this.logger.debug("DEX liquidity broadcast listener registered"))};i()}return async()=>{try{if(null!==o&&(clearTimeout(o),o=null,this.logger.debug("Cleared DEX liquidity polling timeout")),!n)return void this.logger.debug("Cleanup called before listener registration - no action needed");const e=this.websocketService.getSocket();e&&(e.offAny(a),n=!1,this.logger.debug("Stopped listening to DEX liquidity broadcasts"))}catch(e){this.logger.warn("Error removing DEX liquidity listener:",e)}}}matchesLiquidityFilter(e,t){if(!t)return!0;if(t.positionId&&e.positionId!==t.positionId)return!1;if(t.poolHash&&e.poolHash!==t.poolHash)return!1;if(void 0!==t.feeTierFilter){const n=this.normalizeFeeTier(t.feeTierFilter);if(e.poolFee!==n)return!1}if(t.userFilter&&e.userAddress!==t.userFilter)return!1;if(t.tokenFilter){const n=e.token0?.toLowerCase().includes(t.tokenFilter.toLowerCase()),o=e.token1?.toLowerCase().includes(t.tokenFilter.toLowerCase());if(!n&&!o)return!1}if(t.pairTokens){const[n,o]=t.pairTokens.map(e=>e.toLowerCase()),i=e.token0?.toLowerCase()||"",r=e.token1?.toLowerCase()||"",a=i.includes(n)&&r.includes(o),s=i.includes(o)&&r.includes(n);if(!a&&!s)return!1}if(t.minAmount){const n=parseFloat(t.minAmount),o=parseFloat(e.amounts[0])||0,i=parseFloat(e.amounts[1])||0;if(Math.abs(o)<n&&Math.abs(i)<n)return!1}return!0}normalizeFeeTier(e){if("number"==typeof e)return e>=100?e:Math.round(1e4*e);const t=e.replace("%","").trim(),n=parseFloat(t);return n>=100?n:Math.round(1e4*n)}async cleanup(){try{this.logger.debug("Starting cleanup..."),this.http.cleanup(),this.websocketService&&this.websocketService.disconnect(),this.logger.debug("Cleanup completed")}catch(e){this.logger.error("Error during cleanup:",e)}}static cleanupAll(e=!1){const t=new S({debug:e,context:"LaunchpadSDK"});t.debug("Starting global cleanup...");const{WebSocketService:n}=require("./services/WebSocketService");n.cleanupAll(e),t.debug("Global cleanup completed")}}Oi.DEFAULT_SLIPPAGE_TOLERANCE_FACTOR=.15,Oi.DEFAULT_MAX_ACCEPTABLE_REVERSE_BONDING_CURVE_FEE_SLIPPAGE_FACTOR=.01,Oi.DEFAULT_LAUNCHPAD_TOKEN_MAX_SUPPLY=ti.DEFAULT_LAUNCHPAD_TOKEN_MAX_SUPPLY,Oi.DEFAULT_CALCULATE_AMOUNT_MODE=Ai.LOCAL,Oi.TOKEN_CREATION_SOCKET_WAIT_ATTEMPTS=30,Oi.TOKEN_CREATION_SOCKET_POLL_INTERVAL_MS=100,Oi.DEX_POOL_SOCKET_WAIT_ATTEMPTS=30,Oi.DEX_POOL_SOCKET_POLL_INTERVAL_MS=100;class _i{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),o=new n.Wallet(t),i=this.toGalaAddress(o.address);return{privateKey:o.privateKey,address:o.address,galaAddress:i,mnemonic:"test test test test test test test test test test test junk",wallet:o}}throw e}}static fromPrivateKey(e){const t=new n.Wallet(e),o=this.toGalaAddress(t.address);return{privateKey:t.privateKey,address:t.address,galaAddress:o,mnemonic:"",wallet:t}}static fromMnemonic(e,t=0){try{const o=n.Mnemonic.fromPhrase(e),i=n.HDNodeWallet.fromMnemonic(o,`m/44'/60'/0'/0/${t}`),r=new n.Wallet(i.privateKey),a=this.toGalaAddress(r.address);return{privateKey:r.privateKey,address:r.address,galaAddress:a,mnemonic:e,wallet:r}}catch(o){if("undefined"!=typeof process&&"test"===process.env.NODE_ENV){const o=`test-mnemonic-index-${t}-${e}`,i="0x"+Buffer.from(o).toString("hex").padStart(64,"1").slice(0,64),r=new n.Wallet(i),a=this.toGalaAddress(r.address);return{privateKey:r.privateKey,address:r.address,galaAddress:a,mnemonic:e,wallet:r}}throw o}}static toGalaAddress(e){const t=e.replace(/^0x/i,"");if(!/^[a-fA-F0-9]{40}$/.test(t))throw new Error(`Invalid Ethereum address format: ${e}`);return`eth|${t}`}static toEthereumAddress(e){if(!e.startsWith("eth|"))throw new Error(`Invalid Gala address format: ${e}. Must start with 'eth|'`);const t=e.slice(4);if(!/^[a-fA-F0-9]{40}$/.test(t))throw new Error(`Invalid address in Gala format: ${e}`);return`0x${t}`}static isValidEthereumAddress(e){try{const t=e.replace(/^0x/i,"");return/^[a-fA-F0-9]{40}$/.test(t)}catch{return!1}}static isValidGalaAddress(e){try{if(!e.startsWith("eth|"))return!1;const t=e.slice(4);return/^[a-fA-F0-9]{40}$/.test(t)}catch{return!1}}static generateMultipleWallets(e=1){if(e<1||e>100)throw new Error("Count must be between 1 and 100");const t=[];if("undefined"!=typeof process&&"test"===process.env.NODE_ENV)for(let n=0;n<e;n++){const e=`test-multi-${n}-${Date.now()}-${++this.testCounter}`,o="0x"+Buffer.from(e).toString("hex").padStart(64,"1").slice(0,64);t.push(this.fromPrivateKey(o))}else for(let n=0;n<e;n++)t.push(this.generateWallet());return t}static getWalletSummary(e,t=!1){const n=["🔐 Wallet Information","═".repeat(50),`📍 Address: ${e.address}`,`🎮 Gala Address: ${e.galaAddress}`,`🌱 Mnemonic: ${e.mnemonic||"Not available"}`];return t?n.splice(3,0,`🔑 Private Key: ${e.privateKey}`):n.splice(3,0,"🔑 Private Key: [HIDDEN - use includeSensitive=true to show]"),n.push("═".repeat(50)),n.push("💾 IMPORTANT: Save your mnemonic phrase securely!"),n.push("This is your backup to recover the wallet."),n.join("\n")}}function Bi(e){if(void 0===e)return _i.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 _i.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 _i.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?"...":""}"`)}_i.testCounter=0;class Ui{static fastValidation(e,t,n,o=Ui.DEFAULT_CONFIG){const i=Date.now();let a=!0;try{const s=new r(e.sqrtPrice),c=new r(t.sqrtPrice);n.zeroForOne?c.gte(s)&&(this.logger.error("Fast validation failed: price did not decrease for zeroForOne swap",{originalSqrtPrice:s.toString(),updatedSqrtPrice:c.toString(),zeroForOne:n.zeroForOne}),a=!1):c.lte(s)&&(this.logger.error("Fast validation failed: price did not increase for oneForZero swap",{originalSqrtPrice:s.toString(),updatedSqrtPrice:c.toString(),zeroForOne:n.zeroForOne}),a=!1);const l=new r(e.liquidity),u=new r(t.liquidity);if(!l.isZero()){const e=u.minus(l).abs().div(l);e.gt(o.maxLiquidityChangePct)&&this.logger.warn("Fast validation warning: large liquidity change detected (could be legitimate)",{originalLiquidity:l.toString(),updatedLiquidity:u.toString(),changePct:e.times(100).toFixed(2)})}const d=new r(e.feeGrowthGlobal0),h=new r(t.feeGrowthGlobal0),g=new r(e.feeGrowthGlobal1),p=new r(t.feeGrowthGlobal1);n.zeroForOne?p.lt(g)&&(this.logger.error("Fast validation failed: feeGrowthGlobal1 decreased for zeroForOne",{originalFeeGrowth1:g.toString(),updatedFeeGrowth1:p.toString()}),a=!1):h.lt(d)&&(this.logger.error("Fast validation failed: feeGrowthGlobal0 decreased for oneForZero",{originalFeeGrowth0:d.toString(),updatedFeeGrowth0:h.toString()}),a=!1);const m=new r(e.protocolFeesToken0),f=new r(t.protocolFeesToken0),y=new r(e.protocolFeesToken1),w=new r(t.protocolFeesToken1);f.lt(m)&&(this.logger.error("Fast validation failed: protocolFeesToken0 decreased",{originalProtocolFees0:m.toString(),updatedProtocolFees0:f.toString()}),a=!1),w.lt(y)&&(this.logger.error("Fast validation failed: protocolFeesToken1 decreased",{originalProtocolFees1:y.toString(),updatedProtocolFees1:w.toString()}),a=!1);const k=Date.now()-i;return this.logger.debug("Fast validation completed",{passed:a,elapsedMs:k}),a}catch(e){return this.logger.error("Fast validation exception",e),!1}}static moderateValidation(e,t,n=Ui.DEFAULT_CONFIG){const o=Date.now(),i=[];let s=0;try{if(t.actualSqrtPrice){const o=new r(e.sqrtPrice),a=new r(t.actualSqrtPrice),c=this.calculateDriftPercentage(o,a);s=c,c>100*n.maxDriftThreshold&&i.push(`Price drift detected: ${c.toFixed(4)}% (threshold: ${(100*n.maxDriftThreshold).toFixed(4)}%)`),this.logger.debug("Price drift comparison",{calculatedSqrtPrice:o.toString(),actualSqrtPrice:a.toString(),driftPct:c.toFixed(4)})}const c=new r(e.sqrtPrice),l=new r(2).pow(96),u=c.dividedBy(l),d=a.sqrtPriceToTick(u),h=e.tick??0,g=Math.abs(d-h);g>n.maxTickDrift&&i.push(`Tick/price mismatch: tick=${h}, calculated=${d}, drift=${g}`);const p=new r(e.feeGrowthGlobal0),m=new r(e.feeGrowthGlobal1),f=new r(e.liquidity);try{to(p,m,f)}catch(e){i.push(e.message)}const y=0===i.length,w=!y||s>100*n.maxDriftThreshold,k=Date.now()-o;return this.logger.debug("Moderate validation completed",{isValid:y,shouldRefetch:w,driftPercentage:s,errorCount:i.length,elapsedMs:k}),this.buildValidationResult(y,s,w,i)}catch(e){this.logger.error("Moderate validation exception",e);const t=e instanceof Error?e.message:String(e);return this.buildValidationResult(!1,0,!0,[`Exception during validation: ${t}`])}}static async fullValidation(e,t,n){const o=Date.now(),i=[];let a=0;try{this.logger.debug("Starting full validation with fresh pool data fetch",{poolKey:e});const s=await n(),c=new r(t.pool.sqrtPrice),l=new r(s.pool.sqrtPrice),u=this.calculateDriftPercentage(c,l);a=Math.max(a,u),u>100*this.DEFAULT_CONFIG.maxPriceDriftPct&&i.push(`Price drift: ${u.toFixed(4)}% (cached: ${c.toString()}, fresh: ${l.toString()})`);const d=new r(t.pool.liquidity),h=new r(s.pool.liquidity),g=this.calculateDriftPercentage(d,h);a=Math.max(a,g),g>100*this.DEFAULT_CONFIG.maxLiquidityDriftPct&&i.push(`Liquidity drift: ${g.toFixed(4)}% (cached: ${d.toString()}, fresh: ${h.toString()})`);const p=Object.keys(t.tickDataMap).length,m=Object.keys(s.tickDataMap).length;if(m>0){const e=Math.abs(m-p)/m;e>this.DEFAULT_CONFIG.maxTickCountDriftPct&&i.push(`Tick data incomplete: cached has ${p} ticks, fresh has ${m} ticks (${(100*e).toFixed(2)}% difference)`)}const f=0===i.length,y=!f,w=Date.now()-o;return this.logger.debug("Full validation completed",{poolKey:e,isValid:f,shouldRefetch:y,maxDriftPercentage:a,priceDrift:u,liquidityDrift:g,cachedTickCount:p,freshTickCount:m,errorCount:i.length,elapsedMs:w}),this.buildValidationResult(f,a,y,i)}catch(t){this.logger.error("Full validation exception",{poolKey:e,error:t});const n=t instanceof Error?t.message:String(t);return this.buildValidationResult(!1,0,!0,[`Exception during full validation: ${n}`])}}static calculateDriftPercentage(e,t){if(t.isZero())return this.logger.warn("Cannot calculate drift: actual value is zero"),1/0;return t.minus(e).abs().div(t).times(100).toNumber()}static buildValidationResult(e,t,n,o=[]){let i;if(n&&o.length>0){const e=o.join(" ").toLowerCase();i=e.includes("drift")?"drift":e.includes("tick")&&e.includes("mismatch")?"tick-mismatch":e.includes("tick")?"missing-tick-data":"manual"}const r={isValid:e,driftPercentage:t,shouldRefetch:n,validationErrors:o};return void 0!==i&&(r.refetchReason=i),r}}Ui.logger=new S({debug:!1,context:"PoolStateValidator"}),Ui.DEFAULT_CONFIG={maxDriftThreshold:.001,maxLiquidityChangePct:.5,maxTickDrift:1,maxTickCountDriftPct:.1,maxPriceDriftPct:.001,maxLiquidityDriftPct:.01};class Mi{static calculatePoolStateHash(e){const t=`${e.sqrtPrice.toString()}|${e.liquidity.toString()}|${e.tick||0}`;return g.createHash("sha256").update(t).digest("hex").substring(0,16)}constructor(e,t){this.logger=new S({debug:t?.debug??!1,context:"PoolStateManager"});const n={maxIterations:t?.maxIterations??100,enableBigNumberCache:t?.enableBigNumberCache??!0,roundingMode:t?.roundingMode??r.ROUND_DOWN,debug:t?.debug??!1,maxSwapsSinceRefetch:t?.maxSwapsSinceRefetch??50,maxCumulativeDrift:t?.maxCumulativeDrift??5,strictValidation:t?.strictValidation??!1,enablePerformanceWarnings:t?.enablePerformanceWarnings??!0,performanceWarningThreshold:t?.performanceWarningThreshold??100};this.config={...n,...t?.onRefetchNeeded?{onRefetchNeeded:t.onRefetchNeeded}:{}},this.validationConfig=Ui.DEFAULT_CONFIG,this.pool=e.pool,this.tickDataMap=e.tickDataMap,this.metadata={lastFullRefetch:Date.now(),swapsSinceRefetch:0,cumulativeDrift:0,processedTransactions:[],stateHash:Mi.calculatePoolStateHash(this.pool)};if(!Ui.fastValidation(this.pool,this.pool,{transactionId:"init",timestamp:Date.now(),amountSpecified:"0",zeroForOne:!1,exactInput:!0},this.validationConfig)&&this.config.strictValidation)throw new Error("Initial pool state validation failed");this.logger.info("PoolStateManager initialized",{pool:{sqrtPrice:this.pool.sqrtPrice.toString(),liquidity:this.pool.liquidity.toString(),tick:this.pool.tick},config:this.config})}async applySwapDelta(e){const t=Date.now();if(this.metadata.processedTransactions.includes(e.transactionId))throw this.logger.warn("Duplicate swap transaction",{transactionId:e.transactionId}),new Error(`Duplicate transaction ID: ${e.transactionId}`);try{const t={pool:this.pool,tickDataMap:this.tickDataMap},n={maxIterations:this.config.maxIterations,enableBigNumberCache:this.config.enableBigNumberCache,roundingMode:this.config.roundingMode,debugLogging:this.config.debug},o=di.calculateSwapDelta(t,e,n);this.lastSwapMetrics={calculationTimeMs:o.metadata.calculationTimeMs,swapSteps:o.metadata.swapSteps,timestamp:Date.now()},this.config.enablePerformanceWarnings&&o.metadata.calculationTimeMs>this.config.performanceWarningThreshold&&this.logger.warn("Slow swap calculation",{calculationTimeMs:o.metadata.calculationTimeMs,swapSteps:o.metadata.swapSteps,threshold:this.config.performanceWarningThreshold});if(!Ui.fastValidation(t.pool,o.updatedPool,e,this.validationConfig)&&this.config.strictValidation)throw new Error("Swap validation failed");if(e.actualAmount0&&e.actualAmount1&&e.actualSqrtPrice){const t=new r(e.actualAmount0),n=new r(e.actualAmount1),i=o.amount0.minus(t).abs(),a=o.amount1.minus(n).abs(),s=t.isZero()?0:i.div(t.abs()).times(100).toNumber(),c=n.isZero()?0:a.div(n.abs()).times(100).toNumber(),l=Math.max(s,c);l>1&&(this.logger.warn("Drift detected in swap delta",{driftPercentage:l.toFixed(2),swapId:e.transactionId}),this.metadata.cumulativeDrift+=l)}if((this.metadata.swapsSinceRefetch>this.config.maxSwapsSinceRefetch||this.metadata.cumulativeDrift>this.config.maxCumulativeDrift)&&(this.logger.info("Triggering full refetch due to drift accumulation",{swapsSinceRefetch:this.metadata.swapsSinceRefetch,cumulativeDrift:this.metadata.cumulativeDrift.toFixed(2)}),this.config.onRefetchNeeded)){const e=await this.config.onRefetchNeeded(this.pool,this.tickDataMap);this.pool=e.pool,this.tickDataMap=e.tickDataMap,this.metadata.lastFullRefetch=Date.now(),this.metadata.swapsSinceRefetch=0,this.metadata.cumulativeDrift=0,this.metadata.stateHash=Mi.calculatePoolStateHash(this.pool),this.logger.info("Full refetch completed",{sqrtPrice:this.pool.sqrtPrice.toString()})}return this.pool=o.updatedPool,Object.assign(this.tickDataMap,o.updatedTicks),this.metadata.swapsSinceRefetch++,this.metadata.stateHash=Mi.calculatePoolStateHash(this.pool),this.metadata.processedTransactions.push(e.transactionId),this.metadata.processedTransactions.length>1e3&&(this.metadata.processedTransactions=this.metadata.processedTransactions.slice(-1e3)),this.logger.debug("Swap delta applied",{transactionId:e.transactionId,amount0:o.amount0.toString(),amount1:o.amount1.toString(),sqrtPriceNew:this.pool.sqrtPrice.toString()}),o}catch(n){const o=n instanceof Error?n.message:String(n);if(this.logger.error("Failed to apply swap delta",{transactionId:e.transactionId,error:o}),this.config.strictValidation)throw n;return{updatedPool:this.pool,updatedTicks:{},amount0:new r(0),amount1:new r(0),feeAmount0:new r(0),feeAmount1:new r(0),ticksCrossed:[],metadata:{calculationTimeMs:Date.now()-t,swapSteps:0,priceHitLimit:!1}}}}forceFullRefetch(e){this.pool=e.pool,this.tickDataMap=e.tickDataMap,this.metadata.lastFullRefetch=Date.now(),this.metadata.swapsSinceRefetch=0,this.metadata.cumulativeDrift=0,this.logger.info("Full refetch forced",{sqrtPrice:this.pool.sqrtPrice.toString()})}getPoolState(){return this.pool}getTickDataMap(){return{...this.tickDataMap}}getMetadata(){return{...this.metadata}}getLastSwapMetrics(){if(this.lastSwapMetrics)return{...this.lastSwapMetrics}}isRefetchRecommended(){return this.metadata.swapsSinceRefetch>this.config.maxSwapsSinceRefetch||this.metadata.cumulativeDrift>this.config.maxCumulativeDrift}reset(e){this.pool=e.pool,this.tickDataMap=e.tickDataMap,this.metadata={lastFullRefetch:Date.now(),swapsSinceRefetch:0,cumulativeDrift:0,processedTransactions:[],stateHash:Mi.calculatePoolStateHash(this.pool)},this.logger.info("PoolStateManager reset",{sqrtPrice:this.pool.sqrtPrice.toString()})}}var qi=Object.freeze({__proto__:null,extractMetadataFromPoolData:function(e,t){const n={};if(e.vaultAddress&&(n.vaultAddress=e.vaultAddress),void 0!==e.reverseBondingCurveMinFeePortion){const o=parseFloat(e.reverseBondingCurveMinFeePortion);isNaN(o)?t&&t.debug(`Skipping invalid reverseBondingCurveMinFeePortion for ${e.tokenName}: "${e.reverseBondingCurveMinFeePortion}"`):n.reverseBondingCurveMinFeeFactor=o}if(void 0!==e.reverseBondingCurveMaxFeePortion){const o=parseFloat(e.reverseBondingCurveMaxFeePortion);isNaN(o)?t&&t.debug(`Skipping invalid reverseBondingCurveMaxFeePortion for ${e.tokenName}: "${e.reverseBondingCurveMaxFeePortion}"`):n.reverseBondingCurveMaxFeeFactor=o}return void 0!==n.reverseBondingCurveMaxFeeFactor&&void 0!==n.reverseBondingCurveMinFeeFactor&&(n.reverseBondingCurveNetFeeFactor=n.reverseBondingCurveMaxFeeFactor-n.reverseBondingCurveMinFeeFactor),Object.keys(n).length>0?n: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}});e.AgentConfig=class{static async quickSetup(e={}){const t=e.environment||this.detectEnvironment(),n=this.setupWallet(e.privateKey),o={wallet:n.wallet,baseUrl:e.baseUrl||this.getDefaultBaseUrl(t),timeout:e.timeout||this.getDefaultTimeout(t),debug:e.debug??"production"!==t,...this.getEnvironmentDefaults(t),...e.config||{}},i=new Oi(o),r={sdk:i,wallet:n,config:o};if(!1!==e.autoValidate){const e=await this.validateSetup(i,n);return{...r,validation:e}}return r}static async readOnlySetup(e={}){const t=e.environment||this.detectEnvironment(),n={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 Oi(n),config:n}}static async validateSetup(e,t){const n=[],o=[],i={canTrade:!1,canCreateTokens:!1,hasBalance:!1,connectionHealthy:!1};try{const t=await e.fetchGalaBalance(e.getAddress());if(i.connectionHealthy=!0,t&&t.quantity){const e=parseFloat(t.quantity);i.hasBalance=e>0,i.canTrade=e>=.1,i.canCreateTokens=e>=100,0===e?o.push("Wallet has zero GALA balance - cannot perform transactions"):e<.1?o.push("GALA balance too low for trading (minimum 0.1 GALA)"):e<100&&o.push("GALA balance too low for token creation (minimum 100 GALA)")}else n.push("Failed to fetch GALA balance: No balance returned")}catch(e){n.push(`Balance check error: ${e instanceof Error?e.message:String(e)}`)}try{const t=await e.fetchPools({type:"recent",page:1,limit:1});t.pools&&0!==t.pools.length||o.push("Pool listing not accessible - some features may be limited")}catch(e){o.push(`Pool access test failed: ${e instanceof Error?e.message:String(e)}`)}return{ready:0===n.length&&i.connectionHealthy,sdk:e,wallet:t||_i.generateWallet(),issues:n,warnings:o,capabilities:i}}static getRecommendedConfig(e,t="general"){const n={environment:e,autoValidate:!0};switch(e){case"production":Object.assign(n,{debug:!1,timeout:3e4});break;case"development":Object.assign(n,{debug:!0,timeout:45e3});break;case"testing":Object.assign(n,{debug:!0,timeout:6e4})}switch(t){case"trading":n.timeout=1.5*(n.timeout||3e4);break;case"creation":n.timeout=2*(n.timeout||3e4);break;case"monitoring":n.timeout=.5*(n.timeout||3e4)}return n}static async multiWalletSetup(e,t="development"){const n={};for(const[o,i]of Object.entries(e)){const{sdk:e}=await this.quickSetup({environment:t,privateKey:i,agentId:`multi-wallet-${o}`,autoValidate:!1});n[o]=e}return n}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?_i.fromPrivateKey(e):_i.generateWallet()}return"generate"===e?_i.generateWallet():_i.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}},e.CALCULATION_MODES=Ai,e.COMPOSITE_POOL_FETCH_CONCURRENCY=5,e.ConfigurationError=D,e.DexPoolNotFoundError=B,e.DexQuoteError=_,e.FileValidationError=Gn,e.GALA_DECIMALS=8,e.GALA_TOKEN_CLASS_KEY={collection:"GALA",category:"Unit",type:"none",additionalKey:"none"},e.GSwapAssetError=L,e.GSwapPoolError=$,e.GSwapQuoteError=C,e.GSwapSwapError=x,e.IMAGE_EXTENSIONS=_e,e.LAUNCHPAD_TOKEN_DECIMALS=18,e.LaunchpadSDK=Oi,e.LockError=vo,e.MAX_CONCURRENT_POOL_FETCHES=5,e.NetworkError=N,e.PAGINATION_DEFAULTS=kn,e.POOL_FETCH_CONFIG={MAX_CONCURRENT_FETCHES:5,BACKEND_PAGE_SIZE:20},e.POOL_TYPES={RECENT:"recent",POPULAR:"popular"},e.PoolStateManager=Mi,e.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"},e.SDK_VERSION=$i,e.TRADING_TYPES=bi,e.TokenMetadataService=class extends Tn{constructor(e=!1){super(e),this.cache={},this.cacheExpiry=36e5}async resolveTokenMetadata(e){const t=this.getCacheKey(e),n=this.cache[t];if(n&&!this.isCacheExpired(n.timestamp))return this.logger.debug(`Using cached metadata for token: ${t}`),n.data;const o=this.extractMetadata(e);return this.cache[t]={data:o,timestamp:Date.now()},o}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,n,o="Token",i="Unit",r="none";if("string"==typeof e)if(e.includes("|")){const a=e.split("|");"Token"===a[0]&&a[2]?(o=a[0],i=a[1]||"Unit",t=a[2],r=a[3]||"none",n=t):(t=a[0],n=t,i=a[1]||"Unit",r=a[3]||"none")}else t=e,n=e;else t=e.type||"unknown",o=e.collection||"Token",i=e.category||"Unit",r=e.additionalKey||"none",n=e.symbol||("Token"===o?t:o)||"unknown";const a=this.getDecimalsForToken(n);return{symbol:n.toUpperCase(),decimals:a,collection:o,category:i,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`)}},e.TransactionError=F,e.TransactionFailedError=Fi,e.ValidationError=P,e.WebSocketError=Di,e.WebSocketTimeoutError=class extends Di{constructor(e,t){super(`WebSocket confirmation timeout for transaction ${e} after ${t}ms`),this.name="WebSocketTimeoutError"}},e.addressFormatSchema=te,e.amountMethodSchema=$e,e.amountTypeSchema=xe,e.browserFileSchema=Ue,e.bufferFileSchema=Me,e.buyTokensDataSchema=st,e.calculatePreMintDataSchema=pt,e.checkPoolOptionsSchema=Ce,e.commentMessageSchema=we,e.commentPaginationSchema=je,e.createLaunchpadSDK=function(e){e||(e={});const{wallet:t,env:o,config:i={},...r}=e,a={...r,...i},{wallet:s,env:c,config:l,...u}=a;let d;if(t)if("string"==typeof t){d=Bi(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=Bi().wallet}const h={wallet:d,...o&&{env:o},debug:!1,timeout:3e4,...u};return new Oi(h)},e.createLimitSchema=de,e.createPaginatedResultSchema=function(e){return i.z.object({data:i.z.array(e),page:i.z.number().int().min(1),limit:i.z.number().int().min(1),total:i.z.number().int().min(0),totalPages:i.z.number().int().min(0),hasNext:i.z.boolean(),hasPrevious:i.z.boolean()})},e.createPoolStateManager=function(e,t){return new Mi(e,t)},e.createTradeDataSchema=at,e.createWallet=Bi,e.ethereumAddressSchema=ne,e.faucetAmountSchema=se,e.fetchGalaBalanceOptionsSchema=et,e.fetchPoolDetailsDataSchema=mt,e.fetchTokenBalanceOptionsSchema=ot,e.fileSizeSchema=me,e.fileUploadSchema=Be,e.filenameSchema=fe,e.filterByFeeTier=function(e,t){return e.filter(e=>e.feeTier===t)},e.filterByLiquidity=function(e){return e.filter(e=>new r(e.liquidity).isGreaterThan(0))},e.filterByMinLiquidity=function(e,t){const n=new r(t);return e.filter(e=>new r(e.liquidity).isGreaterThanOrEqualTo(n))},e.filterByPoolKey=function(e,t,n,o){const i=t.toUpperCase(),r=n.toUpperCase();return e.filter(e=>{const t=e.token0.toUpperCase(),n=e.token1.toUpperCase();return(t===i&&n===r||t===r&&n===i)&&e.feeTier===o})},e.filterByToken=function(e,t){const n=t.toUpperCase();return e.filter(e=>e.token0.toUpperCase()===n||e.token1.toUpperCase()===n)},e.filterByTokenPair=function(e,t,n){const o=t.toUpperCase(),i=n.toUpperCase();return e.filter(e=>{const t=e.token0.toUpperCase(),n=e.token1.toUpperCase();return t===o&&n===i||t===i&&n===o})},e.flexibleAddressSchema=oe,e.flexibleFileSchema=qe,e.formatGalaForDTO=Po,e.formatLaunchpadTokenForDTO=No,e.fullNameSchema=ee,e.getAmountOptionsSchema=gt,e.getEnv=function(e,t){return process.env[e]??t},e.getEnvOrThrow=function(e,t){const n=process.env[e];if(!n){throw new Error(t?`${e} not set in root or local .env (${t})`:`${e} not set in root or local .env`)}return n},e.getTradeOptionsSchema=lt,e.graduateTokenOptionsSchema=Oe,e.graphDataOptionsSchema=Le,e.groupByFeeTier=function(e){const t=new Map;return e.forEach(e=>{t.has(e.feeTier)||t.set(e.feeTier,[]),t.get(e.feeTier).push(e)}),t},e.groupByPoolKey=function(e){const t=new Map;return e.forEach(e=>{const n=`${e.token0.toUpperCase()}|${e.token1.toUpperCase()}|${e.feeTier}`;t.has(n)||t.set(n,[]),t.get(n).push(e)}),t},e.groupByTokenPair=function(e){const t=new Map;return e.forEach(e=>{const n=`${e.token0.toUpperCase()}/${e.token1.toUpperCase()}`;t.has(n)||t.set(n,[]),t.get(n).push(e)}),t},e.imageExtensionSchema=Re,e.imageFilenameSchema=Ke,e.imageMimeTypeSchema=ye,e.imageUploadOptionsSchema=De,e.isLockTokenData=wo,e.isUnlockTokenData=ko,e.isoDateStringSchema=ke,e.launchTokenDataSchema=Ne,e.loadEnvWithFallback=function(){const e=y.join(process.cwd(),"..","..",".env");w.existsSync(e)&&k.config({path:e});const t=y.join(process.cwd(),".env");w.existsSync(t)&&k.config({path:t})},e.nonNegativeDecimalStringSchema=ae,e.optionalUrlSchema=le,e.pageNumberSchema=ue,e.paginationResultMetaSchema=Xe,e.poolFetchTypeSchema=Fe,e.poolPaginationSchema=He,e.positiveDecimalStringSchema=re,e.privateKeySchema=Te,e.requireNonNegative=to,e.requirePositive=function(...e){e.forEach((e,t)=>{if(e.isNaN())throw new Error(`Value at index ${t} must be a valid number, got: NaN`);if(!e.isFinite())throw new Error(`Value at index ${t} must be finite, got: ${e.toString()}`);if(e.isLessThanOrEqualTo(0))throw new Error(`Value at index ${t} must be positive, got: ${e.toString()}`)})},e.requirePositiveWithContext=no,e.reverseBondingCurveConfigSchema=Pe,e.reverseBondingCurveConfigurationSchema=ft,e.searchQuerySchema=J,e.sellTokensDataSchema=ct,e.sortByLiquidity=function(e,t="desc"){return[...e].sort((e,n)=>{const o=new r(e.liquidity),i=new r(n.liquidity);return"desc"===t?i.minus(o).toNumber():o.minus(i).toNumber()})},e.standardLimitSchema=he,e.standardPaginationSchema=Ge,e.timestampSchema=ve,e.tokenCategorySchema=Ee,e.tokenCollectionSchema=Ie,e.tokenDescriptionSchema=Z,e.tokenListOptionsSchema=Ye,e.tokenNameSchema=Q,e.tokenSymbolSchema=X,e.tokenUrlsSchema=Ae,e.tradeCalculationMethodSchema=ht,e.tradeCalculationTypeSchema=dt,e.tradeLimitSchema=pe,e.tradeListParamsSchema=ut,e.tradePaginationSchema=We,e.tradePaginationWithFiltersSchema=Qe,e.tradeTypeBackendSchema=rt,e.tradeTypeSchema=it,e.transactionIdSchema=Se,e.transferFaucetsDataSchema=Je,e.uniqueKeySchema=be,e.updateProfileDataSchema=tt,e.uploadProfileImageOptionsSchema=nt,e.urlSchema=ce,e.userLimitSchema=ge,e.userPaginationSchema=ze,e.userTokenNameSchema=Y,e.userTokenTypeSchema=Ze,e.userTokensPaginationSchema=Ve,e.validateAddress=Tt,e.validateAmountString=bt,e.validateBuyTokensData=Mt,e.validateCalculatePreMintData=zt,e.validateCheckPoolOptions=Ct,e.validateCreateTradeData=Ut,e.validateFaucetAmount=At,e.validateFetchGalaBalanceOptions=Lt,e.validateFetchPoolDetailsData=Wt,e.validateFetchTokenBalanceOptions=Bt,e.validateFullName=Et,e.validateGetAmountOptions=Gt,e.validateGetTradeOptions=Rt,e.validateImageUploadOptions=Ft,e.validateLaunchTokenData=Nt,e.validateSearchQuery=It,e.validateSellTokensData=qt,e.validateTokenDescription=vt,e.validateTokenListOptions=xt,e.validateTokenName=wt,e.validateTokenSymbol=kt,e.validateTokenUrls=Dt,e.validateTradeListParams=Kt,e.validateTransferFaucetsData=$t,e.validateUpdateProfileData=Ot,e.validateUploadProfileImageOptions=_t,e.validateUserTokenName=Pt,e.validateVaultAddress=St,e.vaultAddressSchema=ie});
|
|
1
|
+
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("axios"),require("ethers"),require("@gala-chain/connect"),require("zod"),require("bignumber.js"),require("@gala-chain/dex"),require("@gala-chain/api"),require("uuid"),require("socket.io-client"),require("path"),require("fs"),require("dotenv"),require("crypto")):"function"==typeof define&&define.amd?define(["exports","axios","ethers","@gala-chain/connect","zod","bignumber.js","@gala-chain/dex","@gala-chain/api","uuid","socket.io-client","path","fs","dotenv","crypto"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).GalaLaunchpadSDK={},e.axios,e.ethers,e.GalaChainConnect,e.z,e.BigNumber,e.GalaChainDex,e.GalaChainAPI,e.uuid,e.io,e.path,e.fs,e.dotenv,e.crypto)}(this,function(e,t,n,o,i,r,a,s,c,l,u,d,h,g){"use strict";function p(e){var t=Object.create(null);return e&&Object.keys(e).forEach(function(n){if("default"!==n){var o=Object.getOwnPropertyDescriptor(e,n);Object.defineProperty(t,n,o.get?o:{enumerable:!0,get:function(){return e[n]}})}}),t.default=e,Object.freeze(t)}var m,f,y=p(u),w=p(d),k=p(h);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"}(m||(m={}));class v extends Error{constructor(e,t,n){super(t),this.type=e,this.originalError=n,this.name="AuthError"}}class T{constructor(e){if(this.wallet=e.wallet,this.messagePrefix=e.messagePrefix||"Create a GalaChain Wallet",""===e.messagePrefix)throw new v(m.SIGNATURE_FAILED,"Message prefix cannot be empty")}hasWallet(){return void 0!==this.wallet}setWallet(e){if(void 0!==e){if("object"!=typeof e||!("address"in e))throw new v(m.WALLET_NOT_CONNECTED,"Invalid wallet: must be an ethers Wallet instance or undefined");if(!e.address||"string"!=typeof e.address)throw new v(m.INVALID_ADDRESS,"Wallet address is not available")}this.wallet=e}async generateSignature(){this.validateWallet();try{const e=Date.now(),t=`${this.messagePrefix} ${e}`,n=await this.wallet.signMessage(t);return{message:t,signature:n,address:this.formatAddress(this.wallet.address),timestamp:e}}catch(e){if(e instanceof v)throw e;throw new v(m.SIGNATURE_FAILED,"Failed to generate signature for authentication",e instanceof Error?e:new Error(String(e)))}}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 v(m.WALLET_NOT_CONNECTED,"Wallet private key not available for @gala-chain signing");return this.wallet.privateKey}formatAddress(e){const t=e.replace(/^0x/i,"");if(!/^[a-fA-F0-9]{40}$/.test(t))throw new v(m.INVALID_ADDRESS,`Invalid Ethereum address format: ${e}`);return`eth|${t}`}async signMessage(e){this.validateWallet();try{return{message:e,signature:await this.wallet.signMessage(e),address:this.wallet.address,timestamp:Date.now()}}catch(e){if(e instanceof v)throw e;const t=e instanceof Error?e.message:String(e);throw new v(m.SIGNATURE_FAILED,t,e instanceof Error?e:new Error(String(e)))}}async generateAuthHeaders(e,t){this.validateWallet();try{const n=Date.now(),o=`${this.messagePrefix} ${t.toUpperCase()} ${e} ${n}`,i=await this.wallet.signMessage(o);return{"x-signature":i,"x-address":this.formatAddress(this.wallet.address),"x-message":o,"x-timestamp":n.toString()}}catch(e){if(e instanceof v)throw e;throw new v(m.SIGNATURE_FAILED,"Failed to generate authentication headers",e instanceof Error?e:new Error(String(e)))}}async signTypedData(e,t,n){this.validateWallet();try{return await this.wallet.signTypedData(e,t,n)}catch(e){if(e instanceof v)throw e;throw new v(m.SIGNATURE_FAILED,"Failed to sign typed data",e instanceof Error?e:new Error(String(e)))}}async generateCustomSignature(e){if(!e||"string"!=typeof e||0===e.trim().length)throw new v(m.SIGNATURE_FAILED,"Custom message must be a non-empty string");this.validateWallet();try{const t=await this.wallet.signMessage(e);return{message:e,signature:t,address:this.formatAddress(this.wallet.address),timestamp:Date.now()}}catch(e){if(e instanceof v)throw e;throw new v(m.SIGNATURE_FAILED,"Failed to generate custom message signature",e instanceof Error?e:new Error(String(e)))}}validateWallet(){if(!this.wallet)throw new v(m.WALLET_NOT_CONNECTED,"Wallet is required for authentication");if(!this.wallet.address)throw new v(m.WALLET_NOT_CONNECTED,"Wallet address is not available");if(!this.wallet.privateKey&&!this.wallet.signMessage)throw new v(m.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"}(f||(f={}));class S{constructor(e){this.levelPriority={[f.DEBUG]:0,[f.INFO]:1,[f.WARN]:2,[f.ERROR]:3},this.debugEnabled=e.debug,this.context=e.context||"SDK",this.minLevel=e.minLevel||(e.debug?f.DEBUG:f.INFO)}debug(e,t){this.log(f.DEBUG,e,t)}info(e,t){this.log(f.INFO,e,t)}warn(e,t){this.log(f.WARN,e,t)}error(e,t){this.log(f.ERROR,e,t)}log(e,t,n){if(this.levelPriority[e]<this.levelPriority[this.minLevel])return;if(e===f.DEBUG&&!this.debugEnabled)return;const o=`[${(new Date).toISOString()}] [${this.context}] [${e}]`,i=this.getConsoleMethod(e);void 0!==n?n instanceof Error?i(`${o} ${t}`,n.message,n.stack):i(`${o} ${t}`,n):i(`${o} ${t}`)}getConsoleMethod(e){switch(e){case f.DEBUG:return console.debug;case f.INFO:return console.info;case f.WARN:return console.warn;case f.ERROR:return console.error;default:return console.log}}child(e){return new S({debug:this.debugEnabled,context:`${this.context}:${e}`,minLevel:this.minLevel})}isDebugEnabled(){return this.debugEnabled&&this.levelPriority[f.DEBUG]>=this.levelPriority[this.minLevel]}}function b(e){if(!e||"object"!=typeof e)return{};const t={};for(const[n,o]of Object.entries(e))null!=o&&("string"==typeof o?t[n]=o:"number"==typeof o||"boolean"==typeof o?t[n]=o.toString():Array.isArray(o)?t[n]=o.join(","):t[n]="object"==typeof o?JSON.stringify(o):String(o));return t}class A{constructor(e,n={}){this.auth=e,this.debug=n.debug??!1,this.logger=new S({debug:this.debug,context:"HttpClient"}),this.axios=t.create({baseURL:n.baseUrl||"https://lpad-backend-dev1.defi.gala.com",timeout:n.timeout||3e4,headers:{Accept:"application/json",...n.headers}}),this.setupInterceptors()}async request(e){try{const t={method:e.method,url:e.url,data:e.data,...e.params&&{params:b(e.params)},...e.headers&&{headers:e.headers},...e.timeout&&{timeout:e.timeout}};e.headers&&this.logger.debug("Custom headers provided:",e.headers),e.data instanceof FormData&&(t.headers&&t.headers["Content-Type"]&&delete t.headers["Content-Type"],this.logger.debug("FormData detected - removing Content-Type header for multipart upload"));const n=e.data instanceof FormData?"[FormData object - multipart/form-data]":e.data;this.logger.debug("Request:",{method:e.method,url:e.url,fullUrl:`${this.axios.defaults.baseURL}${e.url}`,baseURL:this.axios.defaults.baseURL,params:t.params,data:n,isFormData:e.data instanceof FormData,contentType:t.headers?.["Content-Type"]||"not set"});const o=await this.axios.request(t);return this.logger.debug("Response:",{status:o.status,data:o.data}),o.data}catch(e){throw this.logger.error("Error:",e),e}}async get(e,t,n){return this.request({method:"GET",url:e,...t&&{params:t},...n&&{headers:n}})}async post(e,t,n){return this.request({method:"POST",url:e,data:t,...n&&{headers:n}})}async put(e,t,n){return this.request({method:"PUT",url:e,data:t,...n&&{headers:n}})}async delete(e,t,n){return this.request({method:"DELETE",url:e,...t&&{params:t},...n&&{headers:n}})}async patch(e,t,n){return this.request({method:"PATCH",url:e,data:t,...n&&{headers:n}})}getAddress(){return this.auth.getAddress()}getEthereumAddress(){return this.auth.getEthereumAddress()}async signMessage(e){return(await this.auth.signMessage(e)).signature}async signTypedData(e,t,n){return await this.auth.signTypedData(e,t,n)}async signCustomMessage(e){try{const t=await this.auth.generateCustomSignature(e);return this.logger.debug("Generated custom signature:",{message:e,address:t.address,ethereumAddress:this.auth.getEthereumAddress()}),{signature:t.signature,address:t.address,ethereumAddress:this.auth.getEthereumAddress()}}catch(e){throw this.logger.error("Custom signature generation failed:",e),new Error(`Failed to generate custom signature for message: ${e instanceof Error?e.message:"Unknown error"}`)}}async signWithGalaChain(e,t,n=o.SigningType.SIGN_TYPED_DATA){const i=this.auth.getPrivateKey(),r=new o.SigningClient(i);return await r.sign(e,t,n)}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"),this.logger.debug("Final request headers being sent:",e.headers),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 E="Token name is required and must be a string",I=e=>`Could not find vault address for token: ${e}`;class P extends Error{constructor(e,t,n){super(e),this.field=t,this.code=n,this.name="ValidationError"}}class N extends Error{constructor(e,t,n){super(e),this.statusCode=t,this.originalError=n,this.name="NetworkError"}}class D extends Error{constructor(e,t){super(e),this.field=t,this.name="ConfigurationError"}}class C extends Error{constructor(e,t,n){super(e),this.transactionId=t,this.code=n,this.name="TransactionError"}}class F extends Error{constructor(e,t,n){super(e),this.originalError=t,this.code=n,this.name="GSwapQuoteError"}}class x extends Error{constructor(e,t,n,o){super(e),this.originalError=t,this.transactionHash=n,this.code=o,this.name="GSwapSwapError"}}class $ extends Error{constructor(e,t,n,o,i){super(e),this.originalError=t,this.tokenA=n,this.tokenB=o,this.code=i,this.name="GSwapPoolError"}}class L extends Error{constructor(e,t,n,o){super(e),this.originalError=t,this.walletAddress=n,this.code=o,this.name="GSwapAssetError"}}class O extends Error{constructor(e,t,n){super(e),this.originalError=t,this.code=n,this.name="GSwapPositionError"}}class _ extends P{constructor(e,t){super(e,"dexQuote","DEX_QUOTE_ERROR"),this.context=t,this.name="DexQuoteError"}}class B extends P{constructor(e){super(e,"dexPool","DEX_POOL_NOT_FOUND"),this.name="DexPoolNotFoundError"}}function U(e,t){return new P(`Token "${e}" not found. Please verify the token name is correct.`,"tokenName","TOKEN_NOT_FOUND")}function M(e,t){const n=t||e.charAt(0).toUpperCase()+e.slice(1);return new P(`${n} is required`,e,"REQUIRED_FIELD")}function R(e,t,n){const o=n||e.charAt(0).toUpperCase()+e.slice(1);return new P(`${o} must be ${t}`,e,"INVALID_FORMAT")}function q(e,t,n){return new N(e,t,n)}function K(e,t){return new D(e,t)}function G(e,t,n){return new C(e,t,n)}function z(e,t,n){const{MIN_PAGE:o,MAX_PAGE:i,MIN_LIMIT:r,MAX_LIMIT:a}=n.PAGINATION;if("number"!=typeof e||e<o||e>i)throw new P(`Page must be a number between ${o} and ${i}`,"page","INVALID_PAGE");if("number"!=typeof t||t<r||t>a)throw new P(`Limit must be a number between ${r} and ${a}`,"limit","INVALID_LIMIT")}const W={ETH_ADDRESS:/^0x[0-9a-fA-F]{40}$/,BACKEND_ADDRESS:/^eth\|[0-9a-fA-F]{40}$/};function j(e){return"string"==typeof e&&e.trim().length>0}function H(e){return!(!e||"string"!=typeof e)&&(W.ETH_ADDRESS.test(e)||W.BACKEND_ADDRESS.test(e))}function V(e){return e.startsWith("0x")?`eth|${e.slice(2)}`:e}const Q=i.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"),X=i.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"),Z=i.z.string().min(1,"Token description is required").max(500,"Token description must be at most 500 characters"),Y=i.z.string().min(1,"Token name must be at least 1 character").max(50,"Token name must be at most 50 characters"),J=i.z.string().min(1,"Search query must be at least 1 character").max(100,"Search query must be at most 100 characters"),ee=i.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"),te=i.z.string().regex(W.BACKEND_ADDRESS,"Address must be in format: eth|[40-hex-chars]"),ne=i.z.string().regex(W.ETH_ADDRESS,"Invalid Ethereum address format"),oe=i.z.string().refine(e=>W.BACKEND_ADDRESS.test(e)||W.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),ie=i.z.string().refine(e=>W.BACKEND_ADDRESS.test(e)||/^service\|Token\$Unit\$[A-Z0-9]+\$eth:[0-9a-fA-F]{40}\$launchpad$/.test(e),"Invalid vault address format"),re=i.z.string().regex(/^\d+(\.\d+)?$/,"Must be a valid decimal number").refine(e=>parseFloat(e)>0,"Amount must be greater than zero"),ae=i.z.string().regex(/^\d+(\.\d+)?$/,"Must be a valid decimal number").refine(e=>parseFloat(e)>=0,"Amount must be zero or greater"),se=i.z.string().regex(/^(?!0+(\.0+)?$)\d+(\.\d+)?$/,"Amount must be a positive, non-zero number"),ce=i.z.string().url("Must be a valid URL").regex(/^https?:\/\//,"URL must start with http:// or https://"),le=i.z.string().optional().refine(e=>!e||/^https?:\/\/.+\..+/.test(e),"Must be a valid URL if provided"),ue=i.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 de(e=100){return i.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 he=de(100),ge=de(20),pe=de(20),me=i.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"),fe=i.z.string().max(255,"Filename must be at most 255 characters"),ye=i.z.enum(["image/png","image/jpg","image/jpeg","image/gif","image/webp","image/svg+xml"]),we=i.z.string().min(1,"Comment message is required").max(500,"Comment must be at most 500 characters"),ke=i.z.string().datetime("Must be a valid ISO 8601 date string"),ve=i.z.number().int("Timestamp must be an integer").min(0,"Timestamp must be non-negative"),Te=i.z.string().regex(/^0x[a-fA-F0-9]{64}$/,"Private key must be format: 0x + 64 hex characters"),Se=i.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"),be=i.z.string().regex(/^galaconnect-operation-[a-z0-9-]+$/,"Unique key must be format: galaconnect-operation-{unique-id}"),Ae=i.z.object({websiteUrl:le,telegramUrl:le,twitterUrl:le}).refine(e=>e.websiteUrl||e.telegramUrl||e.twitterUrl,"At least one social URL (website, telegram, or twitter) is required"),Ee=i.z.string().min(1,"Token category must not be empty").default("Unit"),Ie=i.z.string().min(1,"Token collection must not be empty").default("Token"),Pe=i.z.object({minFeePortion:re,maxFeePortion:re}),Ne=i.z.object({tokenName:Q,tokenSymbol:X,tokenDescription:Z,tokenImage:i.z.union([i.z.instanceof(File),i.z.instanceof(Buffer),i.z.string().url("Token image must be a valid URL")]).optional(),preBuyQuantity:ae.default("0"),websiteUrl:le,telegramUrl:le,twitterUrl:le,tokenCategory:Ee,tokenCollection:Ie,reverseBondingCurveConfiguration:Pe.optional(),privateKey:Te.optional()}),De=i.z.object({file:i.z.union([i.z.instanceof(File),i.z.instanceof(Buffer)]),tokenName:Q}),Ce=i.z.enum(["recent","popular"]),Fe=i.z.object({tokenName:Q.optional(),symbol:X.optional()}).refine(e=>e.tokenName||e.symbol,"At least one of tokenName or symbol is required"),xe=i.z.enum(["NATIVE","MEME"]),$e=i.z.enum(["IN","OUT"]),Le=i.z.object({from:i.z.number().int("From timestamp must be an integer").min(173e6,"From timestamp must be at least 173000000"),to:i.z.number().int("To timestamp must be an integer").min(173e6,"To timestamp must be at least 173000000"),resolution:i.z.number().int("Resolution must be an integer").min(1,"Resolution must be at least 1"),tokenName:Q}),Oe=i.z.object({tokenName:Q,slippageToleranceFactor:i.z.number().min(0).max(1).optional(),maxAcceptableReverseBondingCurveFeeSlippageFactor:i.z.number().min(0).max(1).optional(),privateKey:Te.optional()}),_e=[".png",".jpg",".jpeg",".gif",".webp",".svg"],Be=i.z.object({file:i.z.union([i.z.instanceof(File),i.z.instanceof(Buffer)]),name:fe,size:me,type:ye}),Ue=i.z.instanceof(File).refine(e=>e.size>=1&&e.size<=10485760,"File size must be between 1 byte and 10MB").refine(e=>["image/png","image/jpg","image/jpeg","image/gif","image/webp","image/svg+xml"].includes(e.type),"File must be a valid image type (PNG, JPG, JPEG, GIF, WebP, or SVG)").refine(e=>e.name.length<=255,"Filename must be at most 255 characters"),Me=i.z.instanceof(Buffer).refine(e=>e.length>=1&&e.length<=10485760,"Buffer size must be between 1 byte and 10MB"),Re=i.z.union([Ue,Me]),qe=i.z.enum([".png",".jpg",".jpeg",".gif",".webp",".svg"]),Ke=fe.refine(e=>{const t=e.slice(e.lastIndexOf(".")).toLowerCase();return _e.includes(t)},`Filename must end with one of: ${_e.join(", ")}`),Ge=i.z.object({page:ue,limit:he}),ze=i.z.object({page:ue,limit:ge}),We=i.z.object({page:ue,limit:pe}),je=i.z.object({page:ue,limit:de(50)}),He=Ge.extend({type:i.z.enum(["recent","popular"]).optional(),tokenName:i.z.string().min(1).max(50).optional(),search:i.z.string().min(1).max(100).optional()}),Ve=ze.extend({tokenName:i.z.string().min(1).max(50).optional(),search:i.z.string().min(1).max(100).optional()}),Qe=We.extend({tradeType:i.z.enum(["BUY","SELL"]).optional(),tokenName:i.z.string().min(1).max(50).optional(),userAddress:i.z.string().regex(/^(0x[a-fA-F0-9]{40}|eth\|[a-fA-F0-9]{40})$/).optional(),startDate:i.z.string().datetime().optional(),endDate:i.z.string().datetime().optional(),sortOrder:i.z.enum(["ASC","DESC"]).default("DESC")}),Xe=i.z.object({page:i.z.number().int().min(1),limit:i.z.number().int().min(1),total:i.z.number().int().min(0),totalPages:i.z.number().int().min(0),hasNext:i.z.boolean(),hasPrevious:i.z.boolean()});const Ze=i.z.enum(["all","DEFI","ASSET"]),Ye=ze.extend({type:Ze.optional(),address:oe.optional(),search:J.optional(),tokenName:Y.optional()}),Je=i.z.object({walletAddress:oe,amount:se}),et=i.z.object({address:oe.optional(),refresh:i.z.boolean().optional()}),tt=i.z.object({profileImage:i.z.string(),fullName:ee,address:oe,privateKey:Te.optional()}),nt=i.z.object({file:i.z.union([i.z.instanceof(File),i.z.instanceof(Buffer)]),address:oe.optional(),privateKey:Te.optional()}),ot=i.z.object({address:oe,tokenId:i.z.union([i.z.string(),i.z.object({collection:i.z.string(),category:i.z.string(),type:i.z.string(),additionalKey:i.z.string()}),i.z.object({collection:i.z.string(),category:i.z.string(),type:i.z.string(),additionalKey:i.z.string(),instance:i.z.string()})]).optional(),tokenName:Y.optional()}).refine(e=>void 0!==e.tokenId||void 0!==e.tokenName,"At least one token identifier (tokenId or tokenName) is required"),it=i.z.enum(["buy","sell"]),rt=i.z.enum(["BUY","SELL"]),at=i.z.object({tradeType:it,tokenAmount:re,vaultAddress:ie,userAddress:oe,slippageTolerance:re.optional(),deadline:i.z.number().int().positive().optional()}),st=i.z.object({tokenSymbol:X,nativeTokenQuantity:re,expectedToken:re,maxAcceptableReverseBondingCurveFee:ae.default("0").optional()}),ct=i.z.object({tokenSymbol:X,tokenQuantity:re,expectedNativeToken:re,maxAcceptableReverseBondingCurveFee:ae.default("0").optional()}),lt=We.extend({tokenName:Y.optional()}),ut=i.z.object({page:i.z.number().int().min(1).max(1e3).default(1).optional(),limit:i.z.number().int().min(1).max(20).default(10).optional()}),dt=i.z.enum(["NATIVE","MEME"]),ht=i.z.enum(["IN","OUT"]),gt=i.z.object({type:dt,method:ht,vaultAddress:ie,amount:re}),pt=i.z.object({nativeTokenQuantity:re}),mt=i.z.object({vaultAddress:ie}),ft=i.z.object({minFeePortion:re,maxFeePortion:re});function yt(e){return t=>{const n=e.safeParse(t);return{success:n.success,data:n.success?n.data:void 0,errors:n.success?void 0:n.error.errors.map(e=>e.message)}}}const wt=yt(Q),kt=yt(X),vt=yt(Z),Tt=yt(oe),St=yt(ie),bt=yt(re),At=yt(se),Et=yt(ee),It=yt(J),Pt=yt(Y),Nt=yt(Ne),Dt=yt(Ae),Ct=yt(De),Ft=yt(Fe),xt=yt(Ye),$t=yt(Je),Lt=yt(et),Ot=yt(tt),_t=yt(nt),Bt=yt(ot),Ut=yt(at),Mt=yt(st),Rt=yt(ct),qt=yt(lt),Kt=yt(ut),Gt=yt(gt),zt=yt(pt),Wt=yt(mt);function jt(e,t){throw new P(e.join("; "),t,"VALIDATION_ERROR")}function Ht(e){const t=wt(e);!t.success&&t.errors&&jt(t.errors,"tokenName")}function Vt(e){const t=He.safeParse(e);t.success||jt(t.error.errors.map(e=>e.message),"pagination")}function Qt(e){const t=Ft(e);!t.success&&t.errors&&jt(t.errors,"options")}function Xt(e){const t=Gt(e);!t.success&&t.errors&&jt(t.errors,"options")}function Zt(e){const t=Le.safeParse(e);t.success||jt(t.error.errors.map(e=>e.message),"options")}function Yt(e){const t=oe.safeParse(e);if(!t.success)throw new P("Ethereum address must be 40 hex characters (with or without 0x prefix)","ethereumAddress","INVALID_FORMAT");return t.data}function Jt(e,t,n=!0){if(!e||""===e.trim())throw new P(`${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 P(`${t} cannot use scientific notation. Use standard decimal format (e.g., "1000" instead of "1e3").`,t,"INVALID_NUMERIC_STRING");const o=parseFloat(e);if(isNaN(o))throw new P(`${t} must be a valid numeric string. Received: "${e}"`,t,"INVALID_NUMERIC_STRING");if(!isFinite(o))throw new P(`${t} must be a finite number. Cannot be Infinity or -Infinity.`,t,"INVALID_NUMERIC_STRING");if(o<0)throw new P(`${t} must be non-negative. Received: "${e}"`,t,"INVALID_NUMERIC_STRING");if(!n&&0===o)throw new P(`${t} must be greater than zero. Received: "${e}"`,t,"INVALID_NUMERIC_STRING")}var en=Object.freeze({__proto__:null,normalizeAddressInput:function(e){if(!e)return;const t=oe.safeParse(e);if(!t.success)throw new P(`Invalid address format: ${e}. Must be either "0x..." (Ethereum) or "eth|..." (GalaChain) format`,"address","INVALID_FORMAT");return t.data},toBackendAddressFormat:Yt,validateCheckPoolOptions:Qt,validateGetAmountOptions:Xt,validateGetGraphOptions:Zt,validateNumericString:Jt,validatePagination:Vt,validateTokenName:Ht});function tn(e,t){const n=e,o=Number(n.page)||t.page,i=Number(n.limit)||t.limit,r=n.data,a=Number(n.total)||Number(r?.count)||0;return{page:o,limit:i,total:a,totalPages:Math.ceil(a/i)}}function nn(e,t){return{hasNext:e<t,hasPrevious:e>1}}function on(e,t,n=!1){const o=!0===e.error||200!==e.status,i=n&&!e.data;if(o||i)throw new Error(e.message||t)}const rn="/launchpad/upload-image",an="/launchpad/fetch-pool",sn="/launchpad/check-pool",cn="/launchpad/get-graph-data",ln="/holders",un="/launchpad/get-badge/",dn="/trade/",hn="/token/commment",gn="/token/commment",pn="/user/profile",mn="/user/profile",fn="/user/token-list",yn="/user/token-hold",wn="/user/transfer-faucets",kn={DEFAULT_PAGE:1,DEFAULT_LIMIT:10,BACKEND_MAX_PAGE_SIZE:20,SAFETY_MAX_PAGES:100};class vn{constructor(e,t=!1){this.http=e,this.logger=new S({debug:t,context:this.constructor.name})}}class Tn{constructor(e=!1){this.logger=new S({debug:e,context:this.constructor.name})}}function Sn(e,t){return"string"==typeof e[t]}function bn(e,t){return void 0===e[t]||"string"==typeof e[t]}function An(e,t){return void 0===e[t]||"number"==typeof e[t]}function En(e){return void 0===e.calculateAmountMode||"local"===e.calculateAmountMode||"external"===e.calculateAmountMode}function In(e){if(!e||"object"!=typeof e)return!1;const t=e;return Sn(t,"tokenName")&&An(t,"from")&&An(t,"to")&&An(t,"resolution")}class Pn extends vn{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 n=b(t),o=await this.http.get(an,n);if(!o)throw q("No response from pool service",500);on(o,"Failed to fetch pools",!0);const i=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 n=e.tokens;t=[{...n,createdAt:n.created_at||n.createdAt||""}]}else e.pools&&Array.isArray(e.pools)&&(t=e.pools.map(e=>({...e,createdAt:e.created_at||e.createdAt||""})));return t}(o.data),r=o.data.count??o.data.total??0;return{pools:i,total:r,totalPages:e.limit>0?Math.ceil(r/e.limit):1}}async fetchMultiplePages(e){const{startPage:t,totalPages:n,pageSize:o,...i}=e,r=[];if(n&&n>=t){const e=[];for(let o=t;o<=n;o++)e.push(o);for(let t=0;t<e.length;t+=5){const n=e.slice(t,t+5).map(e=>this.fetchSinglePage({...i,page:e,limit:o}).catch(e=>{if(400===e?.launchpadError?.statusCode)return{pools:[],total:0,totalPages:0};throw e}));(await Promise.all(n)).forEach(e=>{r.push(...e.pools)})}return r}let a=t,s=!0;for(;s;){const e=[];for(let t=0;t<5&&s;t++)e.push(a+t);const t=e.map(e=>this.fetchSinglePage({...i,page:e,limit:o}).catch(e=>{if(400===e?.launchpadError?.statusCode)return{pools:[],total:0,totalPages:0};throw e})),n=await Promise.all(t);for(const e of n){if(0===e.pools.length){s=!1;break}r.push(...e.pools)}a+=e.length,a>kn.SAFETY_MAX_PAGES&&(s=!1)}return r}async fetchPools(e={}){const t=e.page||kn.DEFAULT_PAGE,n=e.limit??kn.DEFAULT_LIMIT;let o;if(0!==n&&Vt({page:t,limit:n}),e.tokenName&&Ht(e.tokenName),"recent"===e.type?o="RECENT":"popular"===e.type&&(o="POPULAR"),n>0&&n<=20){const i=await this.fetchSinglePage({...e.search&&{search:e.search},...e.tokenName&&{tokenName:e.tokenName},...o&&{type:o},page:t,limit:n});return{pools:i.pools,page:t,limit:n,total:i.total,totalPages:i.totalPages,hasNext:t<i.totalPages,hasPrevious:t>1}}if(0===n){const t=kn.BACKEND_MAX_PAGE_SIZE,n=await this.fetchSinglePage({...e.search&&{search:e.search},...e.tokenName&&{tokenName:e.tokenName},...o&&{type:o},page:1,limit:t}),i=[...n.pools];if(n.pools.length>0){const r=await this.fetchMultiplePages({...e.search&&{search:e.search},...e.tokenName&&{tokenName:e.tokenName},...o&&{type:o},startPage:2,totalPages:n.totalPages>1?n.totalPages:null,pageSize:t});i.push(...r)}return{pools:i,page:1,limit:i.length,total:n.total||i.length,totalPages:1,hasNext:!1,hasPrevious:!1}}const i=kn.BACKEND_MAX_PAGE_SIZE,r=Math.ceil(n/i),a=await this.fetchSinglePage({...e.search&&{search:e.search},...e.tokenName&&{tokenName:e.tokenName},...o&&{type:o},page:t,limit:i}),s=[...a.pools],c=Math.min(r,a.totalPages-t+1);if(c>1){const n=t+c-1,r=await this.fetchMultiplePages({...e.search&&{search:e.search},...e.tokenName&&{tokenName:e.tokenName},...o&&{type:o},startPage:t+1,totalPages:n,pageSize:i});s.push(...r)}const l=s.slice(0,n);return{pools:l,page:t,limit:n,total:a.total,totalPages:a.totalPages,hasNext:t<a.totalPages&&l.length<a.total,hasPrevious:t>1}}async fetchAllPools(e){return this.fetchPools({...e,limit:0})}async checkPool(e){Qt(e),e.tokenName&&Ht(e.tokenName);const t=b(e),n=await this.http.get(sn,t);if(!n)throw q("No response from pool service",500);on(n,"Failed to check pool");const o=n.data;return e.symbol?o?.isSymbolExist??!1:e.tokenName?o?.isNameExist??!1:o?.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(!In(e))throw new P("Invalid options provided. Expected { tokenName: string, from?: number, to?: number, resolution?: number }","options","INVALID_OPTIONS");const{tokenName:t,from:n,to:o,resolution:i}=e;if(Ht(t),!n||!o||!i)throw new P("Graph options (from, to, resolution) are required","options","MISSING_GRAPH_OPTIONS");const r={tokenName:t,from:n,to:o,resolution:i};Zt(r);const a=b(r),s=await this.http.get(cn,a);if(!s)throw q("No response from pool service",500);return on(s,"Failed to fetch graph data",!0),{dataPoints:s.data}}async fetchTokenDistribution(e){if(!e)throw M("tokenName","Token name");Ht(e);const t=await this.resolveTokenNameToVault(e);if(!t)throw U(e);const n=encodeURIComponent(t),o=await this.http.get(`${ln}/${n}`);if(!o)throw q("No response from pool service",500);on(o,"Failed to fetch token distribution",!0);const i=o.data;if(!Array.isArray(i))throw q("Invalid API response: expected array of holders",o.status);for(const e of i){if(!e.owner||"string"!=typeof e.owner)throw q("Invalid holder data: missing or invalid owner field",o.status);if(!e.quantity||"string"!=typeof e.quantity)throw q("Invalid holder data: missing or invalid quantity field",o.status);const t=parseFloat(e.quantity);if(isNaN(t)||!isFinite(t))throw q(`Invalid holder quantity: "${e.quantity}"`,o.status)}const a=i.reduce((e,t)=>e.plus(t.quantity),new r(0));return{holders:i.map(e=>{const t=new r(e.quantity),n=a.isZero()?0:t.dividedBy(a).multipliedBy(100).toNumber();return{address:e.owner,balance:e.quantity,percentage:n}}),totalSupply:a.toFixed(),totalHolders:i.length,lastUpdated:new Date}}async fetchTokenBadges(e){if(!e)throw M("tokenName","Token name");Ht(e);const t=await this.http.get(un,{tokenName:e});if(!t)throw q("No response from pool service",500);return on(t,"Failed to fetch token badges",!0),{volumeBadges:t.data.volumeBadge||[],engagementBadges:t.data.engagementBadge||[]}}async hasTokenBadge(e){const{tokenName:t,badgeType:n,badgeName:o}=e;try{const e=await this.fetchTokenBadges(t);if(!e)return!1;const i=("volume"===n?e.volumeBadges:e.engagementBadges).find(e=>e.badgeName===o);return i?.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 Nn(e,t={}){const{stringifyFields:n=[],optionalFields:o=[],fieldMappings:i={}}=t,r={};for(const[t,a]of Object.entries(e)){const e=t;if(o.includes(e)&&void 0===a)continue;if(o.includes(e)&&"string"==typeof a&&0===a.trim().length)continue;const s=i[e],c=s?String(s):t;n.includes(e)?r[c]=String(a):r[c]=a}return b(r)}const Dn={PAGINATION:{MIN_PAGE:1,MAX_PAGE:1e3,MIN_LIMIT:1,MAX_LIMIT:20}};class Cn extends vn{constructor(e,t=!1){super(e,t)}async fetchTrades(e){if(!function(e){if(!e||"object"!=typeof e)return!1;const t=e;return Sn(t,"tokenName")&&(void 0===t.tradeType||"buy"===t.tradeType||"sell"===t.tradeType)&&bn(t,"userAddress")&&An(t,"page")&&An(t,"limit")}(e))throw new P("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:n,userAddress:o,page:i=kn.DEFAULT_PAGE,limit:r=kn.DEFAULT_LIMIT,startDate:a,endDate:s,sortOrder:c}=e;if(!j(t))throw new P("Token name is required and must be a non-empty string","tokenName","INVALID_TOKEN_NAME");z(i,r,Dn);const l=function(e,t,n){return Nn({tokenName:e,page:t,limit:n},{stringifyFields:["page","limit"]})}(t,i,r),u=await this.http.get(dn,l);if(!u)throw new P("No response from trade service","response","NO_RESPONSE");const d=(h=u.data)?Array.isArray(h)?h:h.trades:[];var h;const g=tn(u,{page:i,limit:r}),p=nn(g.page,g.totalPages);return{trades:d,...g,...p}}}const Fn=new S({debug:!1,context:"DateUtils"});function xn(e,t){if(!e)return t||new Date;if(e instanceof Date)return isNaN(e.getTime())?t||new Date:e;try{const n=new Date(e);return isNaN(n.getTime())?(Fn.warn(`Invalid date string received: "${e}". Using fallback.`),t||new Date):n}catch(n){return Fn.warn(`Date parsing error for "${e}":`,n),t||new Date}}const $n={PAGINATION:{MIN_PAGE:1,MAX_PAGE:1e3,MIN_LIMIT:1,MAX_LIMIT:50},CONTENT:{MIN_LENGTH:1,MAX_LENGTH:500}};class Ln extends vn{constructor(e,t,n=!1){super(e,n),this.poolService=t}async fetchComments(e){if(!function(e){if(!e||"object"!=typeof e)return!1;const t=e;return Sn(t,"tokenName")&&An(t,"page")&&An(t,"limit")}(e))throw new P("Invalid options provided. Expected { tokenName: string, page?: number, limit?: number }","options","INVALID_OPTIONS");const{tokenName:t,page:n=kn.DEFAULT_PAGE,limit:o=kn.DEFAULT_LIMIT}=e;if(!j(t))throw new P("Token name is required and must be a non-empty string","tokenName","INVALID_TOKEN_NAME");z(n,o,$n);const i=await this.poolService.resolveTokenNameToVault(t);if(!i)throw U(t);const r=Nn({vaultAddress:i,page:n,limit:o},{stringifyFields:["page","limit"]}),a=await this.http.get(hn,r);if(!a)throw new P("No response from comment service","response","NO_RESPONSE");if(on(a,"Failed to fetch comments"),!a.data)throw new P("Invalid response structure from comment service: no data field","response.data","INVALID_RESPONSE");if(0===Object.keys(a.data).length)return{comments:[],total:0};if(!Array.isArray(a.data.comments))throw new P("Invalid response structure from comment service: comments must be an array, got "+typeof a.data.comments,"response.data.comments","INVALID_RESPONSE");return{comments:a.data.comments.map(e=>({...e,createdAt:xn(e.createdAt)})),total:a.data.count||0}}async postComment(e){if(!function(e){if(!e||"object"!=typeof e)return!1;const t=e;return Sn(t,"tokenName")&&Sn(t,"content")}(e))throw new P("Invalid options provided. Expected { tokenName: string, content: string }","options","INVALID_OPTIONS");const{tokenName:t,content:n}=e;if(!j(t))throw new P("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>=$n.CONTENT.MIN_LENGTH&&t.length<=$n.CONTENT.MAX_LENGTH}(n))throw new P(`Comment content must be between ${$n.CONTENT.MIN_LENGTH} and ${$n.CONTENT.MAX_LENGTH} characters`,"content","INVALID_CONTENT");const o=await this.poolService.resolveTokenNameToVault(t);if(!o)throw U(t);const i=this.http.getAddress();if(!i)throw new P("User address not available - wallet not configured","userAddress","NO_WALLET");const r={userAddress:i,vaultAddress:o,content:n},a=await this.http.post(gn,r);if(!a)throw new P("No response from comment service","response","NO_RESPONSE");on(a,"Failed to create comment")}}function On(e){return e instanceof Error}function _n(e){return On(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 Bn(e){return"object"==typeof e&&null!==e&&"message"in e&&("response"in e||"request"in e||"config"in e)}function Un(e,t="image",n){const o=new FormData;if("undefined"!=typeof File&&e instanceof File)o.append(t,e);else{if(!Buffer.isBuffer(e))throw R("file","a File object (browser) or Buffer (Node.js)");{const i=new Blob([e],{type:"image/png"});o.append(t,i,n)}}return o}const Mn={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 Rn(e){return!(!e||"string"!=typeof e)&&Mn.USER_ADDRESS.PATTERN.test(e)}const qn="Update profile";class Kn extends vn{constructor(e,t=!1){super(e,t)}async fetchProfile(e){const t=e??this.http.getAddress();if(!t||!Rn(t))throw new P("Address must be in format: eth|[40-hex-chars]","address","INVALID_ADDRESS");const n={userAddress:t},o=await this.http.get(pn,n);if(!o)throw new P("No response from user service","response","NO_RESPONSE");return o}async updateProfile(e){this.validateUpdateProfileData(e);let t=e.profileImage;if(!t||""===t.trim())try{const n=await this.fetchProfile(e.address);t=n.data?.profileImage||""}catch{t=""}const n={profileImage:t,fullName:e.fullName,userAddress:e.address},o=await this.http.signCustomMessage(qn);if(!o)throw new P("Failed to generate signature - wallet not configured","signature","NO_SIGNATURE");const i={address:o.address,message:qn,publickey:o.ethereumAddress,sign:o.signature},r=await this.http.put(mn,n,i);if(!r)throw new P("No response from user service","response","NO_RESPONSE");on(r,"Profile update failed")}async uploadProfileImage(e){this.validateUploadProfileImageOptions(e);const t=e.address??this.http.getAddress();if(!t)throw new P("Wallet address not available - wallet not configured","address","NO_WALLET");try{const n=`profile-image-${t}.png`,o=Un(e.file,"image",n),i=await this.http.request({method:"POST",url:`${rn}?tokenName=${encodeURIComponent(t)}`,data:o,headers:{}});if(!i)throw new P("No response from user service","response","NO_RESPONSE");return on(i,"Image upload failed"),"string"==typeof i.data?i.data:""}catch(e){if(e instanceof P)throw e;throw new P(`Profile image upload failed: ${_n(e)}`,"file","UPLOAD_FAILED")}}async fetchTokenList(e){return this.buildFetchRequest(fn,e,{includeType:!0,errorMessage:"Failed to fetch token list"})}async fetchTokensHeld(e){return this.buildFetchRequest(yn,e,{includeType:!1,errorMessage:"Failed to fetch tokens held"})}async fetchTokensCreated(e={}){const{page:t=kn.DEFAULT_PAGE,limit:n=kn.DEFAULT_LIMIT,search:o,tokenName:i}=e,r=this.http.getAddress();if(!r)throw new P("Wallet address not available - wallet not configured","address","NO_WALLET");const a={type:"DEFI",address:r,page:t,limit:n};return void 0!==o&&(a.search=o),void 0!==i&&(a.tokenName=i),this.fetchTokenList(a)}async buildFetchRequest(e,t,n){this.validateGetTokenListOptions(t);const o={page:t.page,limit:t.limit,address:t.address,search:t.search,tokenName:t.tokenName},i=Nn(n.includeType?{...o,type:"all"!==t.type&&t.type?t.type:"DEFI"}:o,{stringifyFields:["page","limit"],optionalFields:["address","search","tokenName"]}),r=await this.http.get(e,i);if(!r)throw new P("No response from user service","response","NO_RESPONSE");on(r,n.errorMessage,!0);const a=(s=r.data)?Array.isArray(s)?s:s.token:[];var s;const c=tn(r,{page:t.page||kn.DEFAULT_PAGE,limit:t.limit||kn.DEFAULT_LIMIT}),l=nn(c.page,c.totalPages);return{tokens:a,...c,...l}}validateGetTokenListOptions(e){if(z(e.page,e.limit,Mn),void 0!==e.address&&!Rn(e.address))throw new P("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>=Mn.SEARCH.MIN_LENGTH&&t.length<=Mn.SEARCH.MAX_LENGTH))throw new P(`Search query must be between ${Mn.SEARCH.MIN_LENGTH} and ${Mn.SEARCH.MAX_LENGTH} characters`,"search","INVALID_SEARCH");var t,n;if(void 0!==e.tokenName&&e.tokenName.trim().length>0&&!((n=e.tokenName)&&"string"==typeof n&&n.length>=Mn.TOKEN_NAME.MIN_LENGTH&&n.length<=Mn.TOKEN_NAME.MAX_LENGTH))throw new P(`Token name must be between ${Mn.TOKEN_NAME.MIN_LENGTH} and ${Mn.TOKEN_NAME.MAX_LENGTH} characters`,"tokenName","INVALID_TOKEN_NAME")}validateUpdateProfileData(e){if(!Rn(e.address))throw new P("Address must be in format: eth|[40-hex-chars]","address","INVALID_ADDRESS");if(!((t=e.fullName)&&"string"==typeof t&&t.length>=Mn.PROFILE.FULL_NAME.MIN_LENGTH&&t.length<=Mn.PROFILE.FULL_NAME.MAX_LENGTH&&Mn.PROFILE.FULL_NAME.ALPHABETS_ONLY_PATTERN.test(t)))throw new P(`Full name must be between ${Mn.PROFILE.FULL_NAME.MIN_LENGTH} and ${Mn.PROFILE.FULL_NAME.MAX_LENGTH} characters`,"fullName","INVALID_FULL_NAME");var t}validateUploadProfileImageOptions(e){if(e.address&&!Rn(e.address))throw new P("Address must be in format: eth|[40-hex-chars]","address","INVALID_ADDRESS")}}class Gn extends Error{constructor(e,t,n){super(e),this.filename=t,this.mimeType=n,this.name="FileValidationError"}}function zn(e,t,n){if(!e)throw new Gn("File is required",t,n);if("undefined"!=typeof File&&e instanceof File){const t=Ue.safeParse(e);if(!t.success){const n=t.error.errors.map(e=>e.message).join("; ");throw new Gn(n,e.name,e.type)}return}if(Buffer.isBuffer(e)){if(!t)throw new Gn("Filename is required when uploading Buffer objects",t,n);const o=Me.safeParse(e);if(!o.success){const e=o.error.errors.map(e=>e.message).join("; ");throw new Gn(e,t,n)}if(t.length>255)throw new Gn(`Filename length ${t.length} exceeds maximum allowed length of 255 characters`,t,n);const i=["image/png","image/jpg","image/jpeg","image/gif","image/webp","image/svg+xml"];if(!i.includes(n))throw new Gn(`Invalid file type "${n}" is not allowed. Allowed types: ${i.join(", ")}`,t,n);const r=function(e){if(!e)return"";const t=e.lastIndexOf(".");if(-1===t)return"";return e.substring(t).toLowerCase()}(t),a=[".png",".jpg",".jpeg",".gif",".webp",".svg"];if(!a.includes(r))throw new Gn(`File extension "${r}" is not allowed. Allowed extensions: ${a.join(", ")}`,t,n);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!==n&&"application/octet-stream"!==s)throw new Gn(`File extension "${r}" does not match MIME type "${n}"`,t,n);return}throw new Gn("File must be a File object (browser) or Buffer (Node.js)",t,n)}class Wn extends vn{constructor(e,t=!1){super(e,t)}async uploadImageByTokenName(e){const{tokenName:t,options:n}=e;Ht(t);const o=`${t}.png`;zn(n.file,o,"image/png");try{const e=`${n.tokenName??t}.png`,o=Un(n.file,"image",e),i=await this.http.request({method:"POST",url:`${rn}?tokenName=${encodeURIComponent(n.tokenName??t)}`,data:o,headers:{}});if(!i)throw K("No response from image upload service","response");return on(i,"Image upload failed"),"string"==typeof i.data?i.data:""}catch(e){if(e instanceof Error&&e.message.includes("FormData"))throw K("File upload failed: FormData not supported in this environment. Ensure you have proper polyfills for Node.js environments.","FormData");throw e}}}class jn extends vn{constructor(e,t=!1){super(e,t)}async transferFaucets(e){this.validateTransferFaucetsData(e);const t={userAddress:e.walletAddress,amount:e.amount},n=await this.http.post(wn,t);if(!n)throw new P("No response from faucet service","response","NO_RESPONSE");on(n,"Faucet transfer failed")}validateTransferFaucetsData(e){if(!Rn(e.walletAddress))throw new P("Address must be in format: eth|[40-hex-chars]","address","INVALID_ADDRESS");if(!(t=e.amount)||"string"!=typeof t||!Mn.FAUCET_AMOUNT.POSITIVE_NON_ZERO_DECIMAL.test(t))throw new P("Amount must be a positive decimal string greater than zero","amount","INVALID_AMOUNT");var t}}class Hn{constructor(e,t=!1){this.http=e,this.poolService=new Pn(e,t),this.tradeService=new Cn(e,t),this.commentService=new Ln(e,this.poolService,t),this.userService=new Kn(e,t),this.imageService=new Wn(e,t),this.faucetService=new jn(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 Ht(e)}}const Vn={MAX_UNIQUE_KEY_LENGTH:64,UNIQUE_KEY_PATTERN:/^(galaswap-operation-|galaconnect-operation-)/,TOKEN_NAME_PATTERN:/^[a-zA-Z0-9]+$/};var Qn;!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"}(Qn||(Qn={}));class Xn extends Error{constructor(e,t,n){super(e),this.type=t,this.details=n,this.name="TransferError"}}function Zn(e,t="0"){return new r(null==e||""===e?t:e)}function Yn(e){return a.tickToSqrtPrice(e)}function Jn(e){const t=Zn(e),n=Math.log(1.0001),o=t.toNumber();return Math.log(o)/n}function eo(e,t=!1){const n=Zn(e),o=new r(1).dividedBy(n);return t?o.toFixed():o}function to(...e){e.forEach((e,t)=>{if(e.isNaN())throw new Error(`Value at index ${t} must be a valid number, got: NaN`);if(!e.isFinite())throw new Error(`Value at index ${t} must be finite, got: ${e.toString()}`);if(e.isLessThan(0))throw new Error(`Value at index ${t} must be non-negative, got: ${e.toString()}`)})}function no(e,t,n){if(e.isNaN())throw new Error(`${t} must be a valid number, got: NaN`);if(!e.isFinite()){const o=n?`${t} must be finite ${n}, got: ${e.toString()}`:`${t} must be finite, got: ${e.toString()}`;throw new Error(o)}if(e.isLessThanOrEqualTo(0)){const o=n?`${t} must be positive ${n}, got: ${e.toString()}`:`${t} must be positive, got: ${e.toString()}`;throw new Error(o)}}class oo{static validateAmount(e){const t=new r(e);try{no(t,"amount","for transfer")}catch(t){throw new Xn(t.message,Qn.INVALID_AMOUNT,{amount:e})}}static validateUniqueKey(e){if(e){if(e.length>Vn.MAX_UNIQUE_KEY_LENGTH)throw new P(`Unique key too long. Maximum length: ${Vn.MAX_UNIQUE_KEY_LENGTH}`);if(!Vn.UNIQUE_KEY_PATTERN.test(e))throw new Xn('Invalid unique key format. Must start with "galaswap-operation-" or "galaconnect-operation-"',Qn.INVALID_AMOUNT,{uniqueKey:e})}}}class io{toLaunchpadFormat(e){if(!e)return"unknown|Unit|none|none";if("string"==typeof e)return e.includes("|")?e:`${e}|Unit|none|none`;return`${e.collection||e.symbol||"unknown"}|${e.category||"Unit"}|${e.type||"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}`);return{collection:t[0]||"Token",category:t[1]||"Unit",type:t[2]||"none",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 ro(e){if(!e||"string"!=typeof e)throw new Error("Invalid token format: token must be a non-empty string");return e.replace(/\|/g,"$")}function ao(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[n,o,i,...r]=t;if(!n||!o||!i)throw new Error("Collection, category, and type must be non-empty");const a=r.join("$");if(!a)throw new Error("AdditionalKey must be non-empty");return{collection:n,category:o,type:i,additionalKey:a}}catch(t){throw new P(`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 so 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,n,o,i){let r;if("string"==typeof o){r={...ao(o),instance:"0"}}else r={collection:o.collection,category:o.category,type:o.type,additionalKey:o.additionalKey,instance:"0"};return new so({from:e,to:t,quantity:n,tokenInstance:r,uniqueKey:i||`galaconnect-operation-${Date.now()}_${Math.random().toString(36).substring(2,8)}`})}static forGALA(e,t,n,o){return new so({from:e,to:t,quantity:n,tokenInstance:{collection:"GALA",category:"Unit",type:"none",additionalKey:"none",instance:"0"},uniqueKey:o||`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 co extends o.ChainCallDTO{constructor(e){super(),this.lockAuthority=e.lockAuthority,this.tokenInstances=e.tokenInstances,this.uniqueKey=e.uniqueKey,void 0!==e.expires&&(this.expires=e.expires),void 0!==e.name&&(this.name=e.name),e.signedPayload&&(this.signature=e.signedPayload.signature,this.domain=e.signedPayload.domain,this.types=e.signedPayload.types,e.signedPayload.prefix&&(this.prefix=e.signedPayload.prefix))}static fromTokenClassKey(e,t,n,o,i){let r;if("string"==typeof o){r={...ao(o),instance:"0"}}else r={collection:o.collection,category:o.category,type:o.type,additionalKey:o.additionalKey,instance:"0"};return new co({lockAuthority:t||e,tokenInstances:[{owner:e,quantity:n,tokenInstanceKey:r}],...void 0!==i?.expires&&{expires:i.expires},...void 0!==i?.name&&{name:i.name},uniqueKey:i?.uniqueKey||`galaconnect-operation-${Date.now()}_${Math.random().toString(36).substring(2,8)}`})}static forGALA(e,t,n,o){return new co({lockAuthority:t||e,tokenInstances:[{owner:e,quantity:n,tokenInstanceKey:{collection:"GALA",category:"Unit",type:"none",additionalKey:"none",instance:"0"}}],...void 0!==o?.expires&&{expires:o.expires},...void 0!==o?.name&&{name:o.name},uniqueKey:o?.uniqueKey||`galaconnect-operation-${Date.now()}_${Math.random().toString(36).substring(2,8)}`})}getTokenClassKey(){if(0===this.tokenInstances.length)return;const e=this.tokenInstances[0].tokenInstanceKey;return`${e.collection}$${e.category}$${e.type}$${e.additionalKey}`}toSigningPayload(){return{lockAuthority:this.lockAuthority,tokenInstances:this.tokenInstances,...void 0!==this.expires&&{expires:this.expires},...void 0!==this.name&&{name:this.name},uniqueKey:this.uniqueKey}}}class lo extends o.ChainCallDTO{constructor(e){super(),this.tokenInstances=e.tokenInstances,this.uniqueKey=e.uniqueKey,void 0!==e.name&&(this.name=e.name),e.signedPayload&&(this.signature=e.signedPayload.signature,this.domain=e.signedPayload.domain,this.types=e.signedPayload.types,e.signedPayload.prefix&&(this.prefix=e.signedPayload.prefix))}static fromTokenClassKey(e,t,n,o){let i;if("string"==typeof n){i={...ao(n),instance:"0"}}else i={collection:n.collection,category:n.category,type:n.type,additionalKey:n.additionalKey,instance:"0"};return new lo({tokenInstances:[{owner:e,quantity:t,tokenInstanceKey:i}],...void 0!==o?.name&&{name:o.name},uniqueKey:o?.uniqueKey||`galaconnect-operation-${Date.now()}_${Math.random().toString(36).substring(2,8)}`})}static forGALA(e,t,n){return new lo({tokenInstances:[{owner:e,quantity:t,tokenInstanceKey:{collection:"GALA",category:"Unit",type:"none",additionalKey:"none",instance:"0"}}],...void 0!==n?.name&&{name:n.name},uniqueKey:n?.uniqueKey||`galaconnect-operation-${Date.now()}_${Math.random().toString(36).substring(2,8)}`})}getTokenClassKey(){if(0===this.tokenInstances.length)return;const e=this.tokenInstances[0].tokenInstanceKey;return`${e.collection}$${e.category}$${e.type}$${e.additionalKey}`}toSigningPayload(){return{tokenInstances:this.tokenInstances,...void 0!==this.name&&{name:this.name},uniqueKey:this.uniqueKey}}}class uo{constructor(e){this.wallet=e}static generateUniqueKey(){return`${Date.now()}_${Math.random().toString(36).substring(2,8)}`}async signTransferToken(e){const t={name:"GalaChain",chainId:1},n={TransferToken:[{name:"from",type:"string"},{name:"to",type:"string"},{name:"quantity",type:"string"},{name:"tokenInstance",type:"TokenInstance"},{name:"uniqueKey",type:"string"}],TokenInstance:[{name:"collection",type:"string"},{name:"category",type:"string"},{name:"type",type:"string"},{name:"additionalKey",type:"string"},{name:"instance",type:"string"}]};return{signature:await this.wallet.signTypedData(t,n,e),domain:t,types:n,signerPublicKey:this.wallet.signingKey.publicKey}}async signLockToken(e){const t={name:"GalaChain",chainId:1},n={LockToken:[{name:"lockAuthority",type:"string"},{name:"tokenInstances",type:"TokenInstanceQuantity[]"},{name:"uniqueKey",type:"string"},...void 0!==e.expires?[{name:"expires",type:"uint256"}]:[],...void 0!==e.name?[{name:"name",type:"string"}]:[]],TokenInstanceQuantity:[{name:"owner",type:"string"},{name:"quantity",type:"string"},{name:"tokenInstanceKey",type:"TokenInstanceKey"}],TokenInstanceKey:[{name:"collection",type:"string"},{name:"category",type:"string"},{name:"type",type:"string"},{name:"additionalKey",type:"string"},{name:"instance",type:"string"}]},o={lockAuthority:e.lockAuthority,tokenInstances:e.tokenInstances,uniqueKey:e.uniqueKey};void 0!==e.expires&&(o.expires=e.expires),void 0!==e.name&&(o.name=e.name);return{signature:await this.wallet.signTypedData(t,n,o),domain:t,types:n,signerPublicKey:this.wallet.signingKey.publicKey}}async signUnlockToken(e){const t={name:"GalaChain",chainId:1},n={UnlockToken:[{name:"tokenInstances",type:"TokenInstanceQuantity[]"},{name:"uniqueKey",type:"string"},...void 0!==e.name?[{name:"name",type:"string"}]:[]],TokenInstanceQuantity:[{name:"owner",type:"string"},{name:"quantity",type:"string"},{name:"tokenInstanceKey",type:"TokenInstanceKey"}],TokenInstanceKey:[{name:"collection",type:"string"},{name:"category",type:"string"},{name:"type",type:"string"},{name:"additionalKey",type:"string"},{name:"instance",type:"string"}]},o={tokenInstances:e.tokenInstances,uniqueKey:e.uniqueKey};void 0!==e.name&&(o.name=e.name);return{signature:await this.wallet.signTypedData(t,n,o),domain:t,types:n,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{...ao(e),instance:"0"}}}function ho(e){if("string"==typeof e){const t=e.split("|");if(t.length<4)throw new P(`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 P(`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 P("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 P(`Invalid tokenId type: ${typeof e}. Expected string, TokenClassKey, or TokenInstanceKey`,"tokenId","INVALID_TOKEN_ID_TYPE")}function go(e){try{const[t,n]=e.split("|");if(!n)throw new Error("Missing token part after service");const o=n.split("$");if(o.length<4)throw new Error(`Invalid vault address format. Expected at least 4 parts separated by $, got ${o.length}`);if(!(o[0]&&o[1]&&o[2]&&o[3]))throw new Error("All vault address components (collection, category, type, additionalKey) must be non-empty");const i=o.slice(3,-1),r=i.length>0?i.join("$"):o[3];return{collection:o[0],category:o[1],type:o[2],additionalKey:r}}catch(t){throw new P(`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 po(e){return{...go(e),instance:"0"}}function mo(e){return go(e).type}var fo,yo=Object.freeze({__proto__:null,extractTokenSymbolFromVault:mo,normalizeToTokenInstanceKey:ho,parseVaultAddressToTokenClassKey:go,parseVaultAddressToTokenInstance:po});function wo(e){if(!e||"object"!=typeof e)return!1;const t=e;return!(!("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.lockAuthority&&"string"!=typeof t.lockAuthority||void 0!==t.expires&&"number"!=typeof t.expires||void 0!==t.name&&"string"!=typeof t.name||void 0!==t.uniqueKey&&"string"!=typeof t.uniqueKey)}function ko(e){if(!e||"object"!=typeof e)return!1;const t=e;return"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.name||"string"==typeof t.name)&&(void 0===t.uniqueKey||"string"==typeof t.uniqueKey)}e.LockErrorType=void 0,(fo=e.LockErrorType||(e.LockErrorType={})).TOKEN_NOT_FOUND="TOKEN_NOT_FOUND",fo.INVALID_AMOUNT="INVALID_AMOUNT",fo.INSUFFICIENT_BALANCE="INSUFFICIENT_BALANCE",fo.SIGNATURE_FAILED="SIGNATURE_FAILED",fo.NETWORK_ERROR="NETWORK_ERROR",fo.WALLET_REQUIRED="WALLET_REQUIRED",fo.LOCK_NOT_FOUND="LOCK_NOT_FOUND",fo.LOCK_EXPIRED="LOCK_EXPIRED",fo.INSUFFICIENT_LOCKED_BALANCE="INSUFFICIENT_LOCKED_BALANCE",fo.NOT_LOCK_AUTHORITY="NOT_LOCK_AUTHORITY",fo.LOCK_NAME_MISMATCH="LOCK_NAME_MISMATCH";class vo extends Error{constructor(e,t,n){super(e),this.type=t,this.details=n,this.name="LockError"}}const To="gala-transfer-successful",So="token-transfer-successful",bo="transfer-successful-no-id";class Ao extends vn{constructor(e,t,n,o=!1,i){super(e,o),this.wallet=t,this.tokenResolver=n,this.publicAxios=i,this.signatureHelper=t?new uo(t):void 0}async fetchPoolDetails(e){this.validateFetchPoolDetailsData(e);const t=await this.http.post("/api/asset/launchpad-contract/FetchSaleDetails",e);if(!t)throw q("No response from GalaChain service",500);if(1!==t.Status)throw q(`Failed to fetch pool details: Status ${t.Status}`,t.Status);const n=t.Data.reverseBondingCurveConfiguration,o=n?.minFeePortion??"0",i=n?.maxFeePortion??"0",r=(await import("bignumber.js")).default,a=!new r(o).isZero()||!new r(i).isZero(),s=t.Data;return s.reverseBondingCurveMinFeePortion=o,s.reverseBondingCurveMaxFeePortion=i,s.hasReverseBondingCurveFee=a,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 q("No response from GalaChain service",500);if(1!==e.Status)throw q(`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 P("Invalid fetch pool details data: missing required fields","data","INVALID_TYPE");var t;if(!e.vaultAddress||"string"!=typeof e.vaultAddress)throw new P("Vault address is required and must be a string","vaultAddress","INVALID_VAULT_ADDRESS");if(!e.vaultAddress.startsWith("service|Token$Unit$"))throw new P("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 n=t.Data.find(t=>t.collection===e.collection&&t.category===e.category&&t.additionalKey===e.additionalKey&&t.type===e.type);if(!n||"0"===n.quantity)return null;const o=`${n.collection}|${n.category}|${n.additionalKey}|${n.type}`;return{quantity:n.quantity,collection:n.collection,category:n.category,tokenId:o}}catch(e){throw q(`Failed to fetch token balance from GalaChain: ${_n(e)}`,void 0,On(e)?e:void 0)}}async fetchTokenClassFromChain(e){try{const t="string"==typeof e?ho(e):e,n={tokenClasses:[{collection:t.collection,category:t.category,type:t.type,additionalKey:t.additionalKey}]},o=(await this.publicAxios.post("/api/asset/token-contract/FetchTokenClasses",n)).data;if(1!==o.Status)throw q(`Failed to fetch token class from GalaChain: Status ${o.Status}${o.Message?` - ${o.Message}`:""}`,o.Status);if(!o.Data||0===o.Data.length)throw q(`Token not found on GalaChain: ${t.collection}|${t.category}|${t.type}|${t.additionalKey}`,404);return o.Data[0]}catch(e){if(Bn(e)&&404===e.response?.status)throw q("Token not found on GalaChain",404,On(e)?e:void 0);const t=_n(e);if(t.includes("Token not found")||t.includes("Status"))throw e;throw q(`Failed to fetch token class from GalaChain: ${t}`,void 0,On(e)?e:void 0)}}async fetchTokenClassesWithSupply(e){try{if(!e||0===e.length)throw new P("tokenClasses array must not be empty");const t={tokenClasses:e},n=(await this.publicAxios.post("/api/asset/token-contract/FetchTokenClassesWithSupply",t)).data;if(1!==n.Status)throw q(`Failed to fetch token classes with supply from GalaChain: Status ${n.Status}${n.Message?` - ${n.Message}`:""}`,n.Status);if(!n.Data||0===n.Data.length)throw q("No token supply data found for requested token classes",404);return n.Data}catch(e){if(Bn(e)&&404===e.response?.status)throw q("Token supply data not found on GalaChain",404,On(e)?e:void 0);const t=_n(e);if(t.includes("Token")||t.includes("Status")||t.includes("tokenClasses"))throw e;throw q(`Failed to fetch token classes with supply from GalaChain: ${t}`,void 0,On(e)?e:void 0)}}async transferGala(e){if(this.validateTransferGalaData(e),!this.wallet||!this.signatureHelper)throw new Xn("Wallet required for GALA transfer operations",Qn.WALLET_REQUIRED);try{const t=V(e.recipientAddress),n=V(this.wallet.address),o=so.forGALA(n,t,e.amount,e.uniqueKey),i=await this.signatureHelper.signTransferToken(o.toSigningPayload()),r=new so({...o.toSigningPayload(),signedPayload:i});this.logger.debug("[DEBUG] Full GALA Transfer Request Payload:",JSON.stringify(r,null,2));const a=await this.http.post("/api/asset/token-contract/TransferToken",r);if(!a)throw new Xn("No response from GalaChain transfer service",Qn.NETWORK_ERROR);return this.logger.debug("[DEBUG] Transfer response:",JSON.stringify(a,null,2)),this.extractTransactionIdFromResponse(a,"gala")}catch(t){throw this.handleTransferError(t,"GALA transfer failed",e)}}async transferToken(e){if(this.validateTransferTokenData(e),!this.wallet||!this.signatureHelper)throw new Xn("Wallet required for token transfer operations",Qn.WALLET_REQUIRED);try{const t=V(e.to),n=V(this.wallet.address);let o;if(e.tokenId)o=ho(e.tokenId),this.logger.debug("[DEBUG] Using provided tokenId:",e.tokenId),this.logger.debug("[DEBUG] Normalized Token Instance:",JSON.stringify(o,null,2));else{if(!e.tokenName)throw new Xn("Must provide either tokenId or tokenName for token identification",Qn.TOKEN_NOT_FOUND);o=await this.resolveTokenInstance(e.tokenName)}const i=new so({from:n,to:t,quantity:e.amount,tokenInstance:o,uniqueKey:e.uniqueKey||`galaconnect-operation-${Date.now()}_${Math.random().toString(36).substring(2,8)}`}),r=await this.signatureHelper.signTransferToken(i.toSigningPayload()),a=new so({...i.toSigningPayload(),signedPayload:r});this.logger.debug("[DEBUG] Full Transfer Request Payload:",JSON.stringify(a,null,2));const s=await this.http.post("/api/asset/token-contract/TransferToken",a);if(!s)throw new Xn("No response from GalaChain transfer service",Qn.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 Xn)throw t;throw new Xn(`Failed to resolve token class key for '${e}': ${_n(t)}`,Qn.TOKEN_NOT_FOUND,{tokenName:e})}}async lockToken(e){if(this.validateLockTokenData(e),!this.wallet||!this.signatureHelper)throw new Xn("Wallet required for token lock operations",Qn.WALLET_REQUIRED);try{const t=V(this.wallet.address),n=e.lockAuthority?V(e.lockAuthority):t;let o;if(e.tokenId)o=ho(e.tokenId),this.logger.debug("[DEBUG] Using provided tokenId:",e.tokenId);else{if(!e.tokenName)throw new Xn("Must provide either tokenId or tokenName for token identification",Qn.TOKEN_NOT_FOUND);o=await this.resolveTokenInstance(e.tokenName)}const i=new co({lockAuthority:n,tokenInstances:[{owner:t,quantity:e.amount,tokenInstanceKey:o}],...void 0!==e.expires&&{expires:e.expires},...void 0!==e.name&&{name:e.name},uniqueKey:e.uniqueKey||`galaconnect-operation-${Date.now()}_${Math.random().toString(36).substring(2,8)}`}),r=await this.signatureHelper.signLockToken(i.toSigningPayload()),a=new co({...i.toSigningPayload(),signedPayload:r});this.logger.debug("[DEBUG] Full Lock Request Payload:",JSON.stringify(a,null,2));const s=await this.http.post("/api/asset/token-contract/LockToken",a);if(!s)throw new Xn("No response from GalaChain lock service",Qn.NETWORK_ERROR);return this.logger.debug("[DEBUG] Token lock response:",JSON.stringify(s,null,2)),this.extractTransactionIdFromResponse(s,"token")}catch(t){throw this.handleTransferError(t,"Token lock failed",e)}}async unlockToken(e){if(this.validateUnlockTokenData(e),!this.wallet||!this.signatureHelper)throw new Xn("Wallet required for token unlock operations",Qn.WALLET_REQUIRED);try{const t=V(this.wallet.address);let n;if(e.tokenId)n=ho(e.tokenId),this.logger.debug("[DEBUG] Using provided tokenId:",e.tokenId);else{if(!e.tokenName)throw new Xn("Must provide either tokenId or tokenName for token identification",Qn.TOKEN_NOT_FOUND);n=await this.resolveTokenInstance(e.tokenName)}const o=new lo({tokenInstances:[{owner:t,quantity:e.amount,tokenInstanceKey:n}],...void 0!==e.name&&{name:e.name},uniqueKey:e.uniqueKey||`galaconnect-operation-${Date.now()}_${Math.random().toString(36).substring(2,8)}`}),i=await this.signatureHelper.signUnlockToken(o.toSigningPayload()),r=new lo({...o.toSigningPayload(),signedPayload:i});this.logger.debug("[DEBUG] Full Unlock Request Payload:",JSON.stringify(r,null,2));const a=await this.http.post("/api/asset/token-contract/UnlockToken",r);if(!a)throw new Xn("No response from GalaChain unlock service",Qn.NETWORK_ERROR);return this.logger.debug("[DEBUG] Token unlock response:",JSON.stringify(a,null,2)),this.extractTransactionIdFromResponse(a,"token")}catch(t){throw this.handleTransferError(t,"Token unlock failed",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 P("Invalid GALA transfer data: missing required fields");if(!H(e.recipientAddress))throw new Xn("Invalid recipient address format",Qn.INVALID_RECIPIENT,{recipientAddress:e.recipientAddress});oo.validateAmount(e.amount),oo.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 P("Invalid token transfer data: missing required fields");if(!H(e.to))throw new Xn("Invalid recipient address format",Qn.INVALID_RECIPIENT,{recipientAddress:e.to});if(!e.tokenId&&!e.tokenName)throw new Xn("Must provide either tokenId or tokenName for token identification",Qn.TOKEN_NOT_FOUND);if(e.tokenName&&!Vn.TOKEN_NAME_PATTERN.test(e.tokenName))throw new Xn("Invalid token name format",Qn.TOKEN_NOT_FOUND,{tokenName:e.tokenName});oo.validateAmount(e.amount),oo.validateUniqueKey(e.uniqueKey)}validateLockTokenData(e){if(!wo(e))throw new P("Invalid lock token data: missing required fields");if(e.lockAuthority&&!H(e.lockAuthority))throw new Xn("Invalid lock authority address format",Qn.INVALID_RECIPIENT,{recipientAddress:e.lockAuthority});if(!e.tokenId&&!e.tokenName)throw new Xn("Must provide either tokenId or tokenName for token identification",Qn.TOKEN_NOT_FOUND);if(e.tokenName&&!Vn.TOKEN_NAME_PATTERN.test(e.tokenName))throw new Xn("Invalid token name format",Qn.TOKEN_NOT_FOUND,{tokenName:e.tokenName});if(void 0!==e.expires&&(e.expires<=0||!Number.isInteger(e.expires)))throw new P("Expires must be a positive integer (epoch milliseconds)");oo.validateAmount(e.amount),oo.validateUniqueKey(e.uniqueKey)}validateUnlockTokenData(e){if(!ko(e))throw new P("Invalid unlock token data: missing required fields");if(!e.tokenId&&!e.tokenName)throw new Xn("Must provide either tokenId or tokenName for token identification",Qn.TOKEN_NOT_FOUND);if(e.tokenName&&!Vn.TOKEN_NAME_PATTERN.test(e.tokenName))throw new Xn("Invalid token name format",Qn.TOKEN_NOT_FOUND,{tokenName:e.tokenName});oo.validateAmount(e.amount),oo.validateUniqueKey(e.uniqueKey)}async resolveTokenInstance(e){try{const t=await this.tokenResolver.resolveTokenToVault(e);if(!t)throw new Xn(`Token '${e}' not found or not available for transfer`,Qn.TOKEN_NOT_FOUND,{tokenName:e});const n=this.resolveTokenInstanceFromVaultAddress(t);return this.logger.debug(`[DEBUG] Token resolution for '${e}':\n Vault Address: ${t}\n Token Instance: ${JSON.stringify(n,null,2)}`),n}catch(t){if(t instanceof Xn)throw t;throw new Xn(`Failed to resolve token '${e}': ${_n(t)}`,Qn.TOKEN_NOT_FOUND,{tokenName:e})}}resolveTokenInstanceFromVaultAddress(e){try{return po(e)}catch(e){if(e instanceof P)throw new Xn(`Invalid vault address format: ${e.message}`,Qn.TOKEN_NOT_FOUND);throw new Xn(`Failed to parse vault address: ${e instanceof Error?e.message:String(e)}`,Qn.TOKEN_NOT_FOUND)}}extractTransactionIdFromResponse(e,t){if(e&&"object"==typeof e){if("Status"in e&&1===e.Status&&"Data"in e){const n=e;return Array.isArray(n.Data)&&n.Data.length>0?"gala"===t?To:So:bo}if("transactionId"in e&&"string"==typeof e.transactionId&&e.transactionId)return e.transactionId}throw new Xn("Transfer succeeded but transaction ID could not be extracted",Qn.NETWORK_ERROR)}handleTransferError(e,t,n){if(e instanceof Xn)return e;if(e instanceof P)return new Xn(e.message,Qn.INVALID_AMOUNT);if(Bn(e)&&e.response){const t=e.response.status,o=e.response.data;if(400===t)return new Xn(("string"==typeof o?.message?o.message:void 0)||"Invalid transfer request",Qn.INVALID_AMOUNT);if(403===t)return new Xn("Insufficient balance for transfer",Qn.INSUFFICIENT_BALANCE);if(404===t){const e={};return"tokenName"in n&&(e.tokenName=n.tokenName),new Xn("Token not found",Qn.TOKEN_NOT_FOUND,e)}}if("object"==typeof e&&null!==e&&"code"in e&&("ECONNABORTED"===e.code||"ETIMEDOUT"===e.code))return new Xn("Transfer request timed out",Qn.NETWORK_ERROR);const o=_n(e);return new Xn(o||t,Qn.NETWORK_ERROR)}}class Eo{constructor(e,t,n,o=!1){this.dexBackendHttp=e,this.cache=t,this.galaChainService=n,this.logger=new S({debug:o,context:"DexService"})}async fetchTokenPrice(e){const{tokenName:t,tokenId:n}=e;if(!t&&!n)throw M("tokenName or tokenId","Either tokenName (for launchpad tokens) or tokenId (for DEX tokens) is required");if(t&&n)throw new P("tokenName and tokenId are mutually exclusive - provide only one","params","INVALID_PARAMS");if(n)return this.logger.debug(`Fetching spot price by tokenId: ${n}`),this._fetchDexTokenSpotPrice(n);throw new P("tokenName parameter requires LaunchpadSDK routing - call LaunchpadSDK.fetchTokenPrice({tokenName}) instead","tokenName","INVALID_PARAMS")}async _fetchDexTokenSpotPrice(e){if(!e)throw M("tokenId","Token ID");try{const t=ho(e),n=ro(`${t.collection}|${t.category}|${t.type}|${t.additionalKey}`);if(this.logger.debug(`Fetching DEX spot price for token: ${n}`),!this.dexBackendHttp)throw q("DEX Backend API client not configured");const o=await this.dexBackendHttp.request({method:"GET",url:"/v1/trade/price",params:{token:n}});if(!o.data||"string"!=typeof o.data)throw new P("Invalid price response: data must be a string, got "+typeof o.data,"data","INVALID_RESPONSE");const i=parseFloat(o.data);if(isNaN(i))throw new P(`Invalid price value: could not parse "${o.data}" as number`,"data","INVALID_CALCULATION");const r=`${t.collection}|${t.category}|${t.type}|${t.additionalKey}`;let a;try{if(this.cache){const e=this.cache.getByTokenId(r);if(e?.symbol)return a=e.symbol,this.logger.debug(`DEX spot price for ${a} (cached): $${i}`),{symbol:a,price:i}}this.logger.debug(`Symbol cache miss for ${r}, fetching from API`);a=(await this.fetchTokenDetails(e)).symbol,this.cache&&(this.cache.setByTokenId(r,{symbol:a}),this.logger.debug(`Cached symbol for ${r}: ${a}`)),this.logger.debug(`DEX spot price for ${a}: $${i}`)}catch(e){this.logger.debug(`Could not fetch token details for symbol, falling back to token format parsing: ${e instanceof Error?e.message:String(e)}`),a=("Token"===t.collection?t.type:t.collection).toUpperCase(),this.logger.debug(`DEX spot price for ${a} (fallback): $${i}`)}return{symbol:a,price:i}}catch(e){if(e instanceof P)throw e;throw q(`Failed to fetch DEX spot price: ${_n(e)}`)}}async fetchLaunchpadTokenSpotPrice(e,t,n){if(!e||"string"!=typeof e)throw new Error(E);try{if(n)try{this.logger.debug(`Checking graduation status for token: ${e}`);const t=await n(e);if(t&&t.isGraduated){this.logger.debug(`Token ${e} is graduated, using DEX spot price`);const n=`${t.sellingToken.collection}|${t.sellingToken.category}|${t.sellingToken.type}|${t.sellingToken.additionalKey}`;return this._fetchDexTokenSpotPrice(n)}}catch(t){this.logger.debug(`Could not determine graduation status for ${e}, falling back to bonding curve: ${_n(t)}`)}this.logger.debug(`Using bonding curve calculation for token: ${e}`);const o=await t({tokenName:e,amount:"1",type:"native"}),i=await this._fetchDexTokenSpotPrice({collection:"GALA",category:"Unit",type:"none",additionalKey:"none"});if(!i)throw q("GALA price not available");const r=Number(o.amount)/1e18;if(r<=0)throw new P(`Invalid token amount calculation: ${r}`,"amount","INVALID_CALCULATION");const a=i.price/r;return{symbol:e.toUpperCase(),price:a}}catch(t){if(t instanceof Error)throw q(`Failed to calculate launchpad token spot price for ${e}: ${t.message}`);throw q(`Failed to calculate launchpad token spot price for ${e}: ${_n(t)}`)}}async fetchTokenDetails(e){this.logger.debug("Fetching token details from GalaChain for tokenId:",e);try{if(!this.galaChainService)throw q("GalaChainService not available for token metadata fetch",500);const t=await this.galaChainService.fetchTokenClassFromChain(e),n={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`),n}catch(t){if((t instanceof P||t instanceof Error)&&("NetworkError"===t.name||t.message.includes("Token not found")))throw t;throw q(`Failed to fetch token details from GalaChain for ${e}: ${_n(t)}`,500)}}async fetchAllDexSeasons(){try{if(!this.dexBackendHttp)throw q("DEX Backend API client not configured");const e=(await this.dexBackendHttp.request({method:"GET",url:"/leaderboard/seasons"})).data.map(e=>({id:e.id,name:e.name,start:new Date(e.start),end:new Date(e.end),rulesId:e.rules_id}));return this.logger.debug(`Fetched ${e.length} DEX seasons`),e}catch(e){if(e instanceof Error&&e.message.includes("not configured"))throw e;throw q(`Failed to fetch DEX seasons: ${_n(e)}`)}}async fetchCurrentDexSeason(){const e=await this.fetchAllDexSeasons(),t=new Date,n=e.find(e=>t>=e.start&&t<=e.end);return n?this.logger.debug(`Current DEX season: ${n.name} (ID: ${n.id})`):this.logger.debug("No active DEX season found"),n||null}async fetchDexLeaderboardBySeasonId(e){if(!e||"number"!=typeof e||e<1)throw M("seasonId","Season ID must be a positive number");try{if(!this.dexBackendHttp)throw q("DEX Backend API client not configured");const t=(await this.dexBackendHttp.request({method:"GET",url:"/leaderboard",params:{seasonId:e.toString()}})).data.leaderboard.map(e=>({wallet:e.wallet,rank:e.rank,totalXp:e.total_xp,distributionPercent:e.distribution_percent,liquidityXp:e.liquidity_xp,tradingXp:e.trading_xp,masteryTitles:e.mastery_titles.map(e=>({name:e.name,type:e.type,order:e.order}))}));return this.logger.debug(`Fetched leaderboard for season ${e} with ${t.length} entries`),{entries:t,seasonId:e,totalEntries:t.length}}catch(t){if(t instanceof Error&&t.message.includes("must be a positive number"))throw t;throw q(`Failed to fetch DEX leaderboard for season ${e}: ${_n(t)}`)}}async fetchCurrentDexLeaderboard(){const e=await this.fetchCurrentDexSeason();return e?this.fetchDexLeaderboardBySeasonId(e.id):(this.logger.debug("Cannot fetch current leaderboard - no active season"),null)}async fetchDexAggregatedVolumeSummary(){try{if(!this.dexBackendHttp)throw q("DEX Backend API client not configured");const e=await this.dexBackendHttp.request({method:"GET",url:"/explore/volume"}),t={volume1d:e.data.volume1d,volume1dDelta:e.data.volume1dDelta,volume7d:e.data.volume7d,volume7dDelta:e.data.volume7dDelta,volume30d:e.data.volume30d,volume30dDelta:e.data.volume30dDelta};return this.logger.debug(`Fetched DEX volume summary: $${t.volume1d.toFixed(2)} (1d)`),t}catch(e){throw q(`Failed to fetch DEX volume summary: ${_n(e)}`)}}}function Io(e,t=18){const n=parseFloat(e);if(0===n)return"0";return n.toFixed(t).replace(/\.?0+$/,"")}function Po(e){return Io(e,8)}function No(e){return Io(e,18)}function Do(e){return{maxAcceptableReverseBondingCurveFee:Po(e.maxAcceptableReverseBondingCurveFee)}}new S({debug:!1,context:"NumberUtils"});class Co extends o.ChainCallDTO{constructor(e,t,n="0",o={maxAcceptableReverseBondingCurveFee:"0"}){super(),this.vaultAddress=e,this.nativeTokenQuantity=Po(t),this.expectedToken=No(n),this.extraFees=Do(o)}}class Fo extends o.ChainCallDTO{constructor(e,t,n,o={maxAcceptableReverseBondingCurveFee:"0"}){super(),this.vaultAddress=e,this.tokenQuantity=No(t),this.expectedNativeToken=Po(n),this.extraFees=Do(o)}}class xo extends o.ChainCallDTO{constructor(e,t,n="0",o={maxAcceptableReverseBondingCurveFee:"0"}){super(),this.vaultAddress=e,this.tokenQuantity=No(t),this.expectedNativeToken=Po(n),this.extraFees=Do(o)}}class $o extends o.ChainCallDTO{constructor(e,t,n,o={maxAcceptableReverseBondingCurveFee:"0"}){super(),this.vaultAddress=e,this.nativeTokenQuantity=Po(t),this.expectedToken=No(n),this.extraFees=Do(o)}}const Lo={BuyNativeDto:Co,BuyExactDto:Fo,SellExactDto:xo,SellNativeDto:$o};class Oo extends Tn{constructor(e,t=!1){super(t),this.walletProvider=e}async signDTO(e,t,n){try{this.logger.debug("🔐 Signing DTO:",{methodName:t,dtoKeys:Object.keys(e)});const n=this.generateEIP712Types(t,e),i=o.calculatePersonalSignPrefix(e),r={...e,prefix:i},{signature:a,domain:s}=await this.signWithEthersWallet(n,r),c={...e,signature:a,types:n,domain:s};return this.logger.debug("✅ DTO signed successfully:",{payloadKeys:Object.keys(c),signatureLength:a.length}),c}catch(e){this.logger.error("❌ Signature generation failed:",e);throw G(`Failed to sign DTO: ${_n(e)}`)}}async signWithEthersWallet(e,t){try{let n,o;if(this.walletProvider.signTypedData&&!this.walletProvider.getNetwork)n={name:"ethereum",chainId:1},o=await this.walletProvider.signTypedData(n,e,t);else{if(!this.walletProvider.getNetwork||!this.walletProvider.signTypedData)throw K("Wallet provider does not support typed data signing","walletProvider");{const i=await this.walletProvider.getNetwork();n={name:i.name,chainId:Number(i.chainId)},o=await this.walletProvider.signTypedData(n,e,t)}}return{signature:o,domain:n}}catch(e){throw G(`Ethers.js signing failed: ${_n(e)}`)}}generateEIP712Types(e,t){const n={};n[e]=[];const o=Object.fromEntries(Object.entries(t).filter(([e,t])=>void 0!==t)),i=(e,t,o,r=!1)=>{if(void 0!==t){if(Array.isArray(t)){if(0===t.length)return;const a=i(e,t[0],o,!0);return r||n[o].push({name:e,type:(a??e)+"[]"}),a?a+"[]":void 0}if("object"==typeof t&&null!==t){if(n[e])throw new P(`Type name collision not supported: ${e}`,"fieldValue","TYPE_COLLISION");return n[e]=[],Object.entries(t).forEach(([t,n])=>{i(t,n,e)}),r||n[o].push({name:e,type:e}),e}{let i;switch(typeof t){case"string":i="string";break;case"number":i="uint256";break;case"boolean":i="bool";break;default:throw new P(`Unsupported type for field "${e}": ${typeof t} (value: ${JSON.stringify(t)})`,"fieldValue","UNSUPPORTED_TYPE")}return r||n[o].push({name:e,type:i}),i}}};return Object.entries(o).forEach(([t,n])=>{i(t,n,e)}),this.logger.debug("📝 Generated EIP-712 types:",n),n}}class _o extends Tn{constructor(e=!1){super(e)}generateStringsInstructions(e){try{this.logger.debug("🔧 Generating stringsInstructions for:",e);const t=this.extractTokenSymbolFromVault(e),n=this.createTokenInstance(t),o=this.createGalaInstance(),i=`$service$${n.toStringKey()}$launchpad`,r=`$tokenBalance$${n.toStringKey()}$${e}`,a=`$tokenBalance$${n.toStringKey()}$${e}`,s=`$tokenBalance$${o.toStringKey()}$${e}`,c=[i,r,a,s,`$tokenBalance$${o.toStringKey()}$${e}`];return this.logger.debug("✅ Generated stringsInstructions:",c),c}catch(e){this.logger.error("❌ Failed to generate stringsInstructions:",e);const t=_n(e);throw new P(`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 M("vaultAddress","Vault address");try{const t=mo(e);return this.logger.debug("🔍 Extracted token symbol:",{vaultAddress:e,tokenSymbol:t}),t}catch(e){if(e instanceof P)throw R("vaultAddress","format: service|Token$Unit$SYMBOL$eth:address$launchpad");throw e}}validateVaultAddress(e){if(!e||"string"!=typeof e)throw M("vaultAddress","Vault address");if(!e.startsWith("service|Token$Unit$"))throw R("vaultAddress",'starting with "service|Token$Unit$"');if(!e.endsWith("$launchpad"))throw R("vaultAddress",'ending with "$launchpad"');const t=e.split("$");if(t.length<5)throw R("vaultAddress",'having at least 5 parts separated by "$"');const n=t[2];if(!n||!/^[A-Za-z]{1,10}$/.test(n))throw R("vaultAddress","containing a 1-10 letter token symbol (case insensitive)");return this.logger.debug("✅ Vault address validation passed:",e),!0}generateTokenClassKeyString(e,t,n,o){return`${e}$${t}$${n}$${o}`}parseTokenClassKeyString(e){try{return ao(e)}catch(e){if(e instanceof P)throw R("stringKey","format: collection$category$type$additionalKey (4 parts)");throw e}}}function Bo(e,t,n){if(t<0||t>1)throw new Error(`Invalid slippage tolerance factor: ${t}. Must be between 0 and 1 (e.g., 0.05 for 5%)`);const o=new r(e);if(o.isNaN())throw new Error(`Invalid expected amount: ${e}. Must be a valid number`);if(0===t)return e;const i=o.multipliedBy(t);let a;switch(n){case"buy-native":case"sell-exact":a=o.minus(i);break;case"buy-exact":case"sell-native":a=o.plus(i);break;default:throw new Error(`Unknown operation type: ${n}`)}return a.isLessThan(0)&&(a=new r(0)),a.toFixed()}class Uo extends vn{constructor(e,t,n=!1,o,i,r=.05,a=.01){super(e,n),this.tokenResolver=t,this.walletProvider=o,this.userAddress=i,this.defaultSlippageToleranceFactor=r,this.defaultMaxAcceptableReverseBondingCurveFeeSlippageFactor=a,this.bundleEndpoint="/bundle",o&&i&&(this.signatureService=new Oo(o,n),this.tokenKeyService=new _o(n))}async submitTransaction(e){try{this.logger.debug("📦 Submitting bundle transaction:",{method:e.method,stringsInstructionsCount:e.stringsInstructions.length,signedDtoKeys:Object.keys(e.signedDto)}),this.validateBundleData(e);const t=this.formatBundleRequest(e);this.logger.debug("🚀 Bundle request payload:",{...t,signedDto:"[REDACTED - Contains signature]"});const n=await this.http.post(this.bundleEndpoint,t);return n?(this.logger.debug("📥 Bundle API response:",{success:n.success,hasData:!!n.data,error:n.error}),this.handleBundleResponse(n)):{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 M("bundleData","Bundle data");if(!e.signedDto)throw M("signedDto","Signed DTO");if(!e.method||"string"!=typeof e.method)throw M("method","Method name");if(!Array.isArray(e.stringsInstructions))throw R("stringsInstructions","an array of resource tracking strings");if(0===e.stringsInstructions.length)throw new P("stringsInstructions cannot be empty","stringsInstructions","EMPTY_ARRAY");const t=["BuyWithNative","BuyExactToken","SellExactToken","SellWithNative"];if(!t.includes(e.method))throw R("method",`one of: ${t.join(", ")}`);e.stringsInstructions.forEach((e,t)=>{if("string"!=typeof e||0===e.length)throw new P(`stringsInstructions[${t}] must be a non-empty string`,`stringsInstructions[${t}]`,"INVALID_INSTRUCTION");if(!e.startsWith("$"))throw new P(`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(Bn(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 _n(e)||"Unknown bundle transaction error"}async getBundlerTransactionResult(e){try{if(!e||"string"!=typeof e)throw M("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 M("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:n,type:o,expectedAmount:i}=e,{effectiveSlippageFactor:r,effectiveMaxFee:a,vaultAddress:s}=await this.prepareTradingOperation(t,e.maxAcceptableReverseBondingCurveFee,e.maxAcceptableReverseBondingCurveFeeSlippageFactor,e.slippageToleranceFactor);if("native"===o){if(!i)throw new P("expectedAmount is required for native buy operations. Use getBuyTokenAmount() first to calculate expected tokens.","expectedAmount","EXPECTED_AMOUNT_REQUIRED");const e=Bo(i,r,"buy-native");this.logger.debug("BuyNative slippage applied:",{originalExpectedTokens:i,slippageFactor:r,adjustedMinTokens:e});const t=new Lo.BuyNativeDto(s,n,e,{maxAcceptableReverseBondingCurveFee:a});return await this.executeBundleTransaction(t,"BuyWithNative",s)}{if(!i)throw new P("expectedAmount is required for exact buy operations. Use getBuyTokenAmount() first to calculate expected GALA cost.","expectedAmount","EXPECTED_AMOUNT_REQUIRED");const e=Bo(i,r,"buy-exact");this.logger.debug("BuyExact slippage applied:",{originalExpectedGalaCost:i,slippageFactor:r,adjustedMaxGalaCost:e});const t=new Lo.BuyExactDto(s,n,e,{maxAcceptableReverseBondingCurveFee:a});return await this.executeBundleTransaction(t,"BuyExactToken",s)}}async sellToken(e){this.ensureTradingServicesAvailable();const{tokenName:t,amount:n,type:o,expectedAmount:i}=e,{effectiveSlippageFactor:r,effectiveMaxFee:a,vaultAddress:s}=await this.prepareTradingOperation(t,e.maxAcceptableReverseBondingCurveFee,e.maxAcceptableReverseBondingCurveFeeSlippageFactor,e.slippageToleranceFactor);if("exact"===o){if(!i)throw new P("expectedAmount is required for exact sell operations. Use getSellTokenAmount() first to calculate expected GALA.","expectedAmount","EXPECTED_AMOUNT_REQUIRED");const e=Bo(i,r,"sell-exact");this.logger.debug("SellExact slippage applied:",{originalExpectedGala:i,slippageFactor:r,adjustedMinGala:e});const t=new Lo.SellExactDto(s,n,e,{maxAcceptableReverseBondingCurveFee:a});return await this.executeBundleTransaction(t,"SellExactToken",s)}{if(!i)throw new P("expectedAmount is required for native sell operations. Use getSellTokenAmount() first to calculate tokens to sell.","expectedAmount","EXPECTED_AMOUNT_REQUIRED");const e=Bo(i,r,"sell-native");this.logger.debug("SellNative slippage applied:",{originalExpectedTokensToSell:i,slippageFactor:r,adjustedMaxTokensToSell:e});const t=new Lo.SellNativeDto(s,n,e,{maxAcceptableReverseBondingCurveFee:a});return await this.executeBundleTransaction(t,"SellWithNative",s)}}async prepareTradingOperation(e,t,n,o){const{effectiveSlippageFactor:i,effectiveMaxFee:r}=this.calculateEffectiveSlippage(t,n,o),a=await this.resolveTokenNameToVault(e);if(!a)throw U(e);return{effectiveSlippageFactor:i,effectiveMaxFee:r,vaultAddress:a}}calculateEffectiveSlippage(e,t,n){const o=n??this.defaultSlippageToleranceFactor,i=t??this.defaultMaxAcceptableReverseBondingCurveFeeSlippageFactor;let r=e||"0";return e&&(r=Bo(e,i,"buy-exact"),this.logger.debug("Reverse bonding curve fee slippage applied:",{baseFee:e,slippageFactor:i,adjustedMaxFee:r})),{effectiveSlippageFactor:o,effectiveFeeSlippageFactor:i,effectiveMaxFee:r}}ensureTradingServicesAvailable(){if(!this.signatureService||!this.tokenKeyService)throw K("Trading services not available. BundleService requires walletProvider and userAddress for trading operations.","walletProvider");if(!this.userAddress)throw M("userAddress","User address")}async executeBundleTransaction(e,t,n){this.ensureTradingServicesAvailable();try{e.uniqueKey=`galaswap - operation - ${c.v4()}-${Date.now()}-${this.userAddress}`;const o=await this.signatureService.signDTO(e,t,this.userAddress),i=this.tokenKeyService.generateStringsInstructions(n),r={stringsInstructions:i,method:t,signedDto:o};this.logger.debug("📦 Bundle transaction data:",{method:t,stringsInstructions:i,dtoKeys:Object.keys(o)});const a=await this.submitTransaction(r);if(a.success&&a.data)return this.logger.debug("✅ Bundle transaction submitted:",a.data),{success:!0,data:{transactionId:a.data,message:"Transaction submitted successfully. Monitor WebSocket for completion."}};throw new Error(String(a.error||"Bundle transaction failed"))}catch(e){throw this.logger.error("❌ Bundle transaction error:",e),e}}async resolveTokenNameToVault(e){return await this.tokenResolver.resolveTokenToVault(e)}}var Mo,Ro;!function(e){e.PROCESSED="PROCESSED",e.COMPLETED="COMPLETED",e.SUCCESS="SUCCESS",e.FAILED="FAILED",e.ERROR="ERROR",e.PROCESSING="PROCESSING",e.PENDING="PENDING"}(Mo||(Mo={})),e.SDKTransactionStatus=void 0,(Ro=e.SDKTransactionStatus||(e.SDKTransactionStatus={})).PENDING="pending",Ro.PROCESSING="processing",Ro.COMPLETED="completed",Ro.FAILED="failed",Ro.TIMEOUT="timeout";const qo={[Mo.PROCESSED]:e.SDKTransactionStatus.COMPLETED,[Mo.COMPLETED]:e.SDKTransactionStatus.COMPLETED,[Mo.SUCCESS]:e.SDKTransactionStatus.COMPLETED,[Mo.FAILED]:e.SDKTransactionStatus.FAILED,[Mo.ERROR]:e.SDKTransactionStatus.FAILED,[Mo.PROCESSING]:e.SDKTransactionStatus.PROCESSING,[Mo.PENDING]:e.SDKTransactionStatus.PENDING};class Ko extends Tn{constructor(e,t=!1){super(t),this.socket=null,this.listeners=new Map,this.timeouts=new Map,this.reconnectCount=0,this.hasOnAnyListener=!1,this.eventBuffer=new Map,this.eventBufferTimeouts=new Map,this.MAX_BUFFER_SIZE=1e3,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.socket.onAny((e,...t)=>{if(e&&t.length>0&&"object"==typeof t[0]&&null!==t[0]){const n=t[0],o=n.status||n.Status;if(o&&"string"==typeof o){if(this.logger.debug(`📡 [Event Buffer] Buffering event for ${e}: ${o}`),this.eventBuffer.size>=this.MAX_BUFFER_SIZE){const e=this.eventBuffer.keys().next().value;if(e){const t=this.eventBufferTimeouts.get(e);t&&(clearTimeout(t),this.eventBufferTimeouts.delete(e)),this.eventBuffer.delete(e),this.logger.warn(`📡 [Event Buffer] Buffer full (${this.MAX_BUFFER_SIZE}), dropped oldest: ${e}`)}}this.eventBuffer.set(e,n);const t=this.eventBufferTimeouts.get(e);t&&clearTimeout(t);const i=setTimeout(()=>{this.eventBuffer.has(e)&&(this.logger.debug(`📡 [Event Buffer] Cleaning up orphaned event for ${e}`),this.eventBuffer.delete(e),this.eventBufferTimeouts.delete(e))},3e4);this.eventBufferTimeouts.set(e,i)}}this.debug&&this.logger.debug(`📡 [WebSocket Event] "${e}":`,JSON.stringify(t,null,2))}),this.hasOnAnyListener=!0}catch(e){t(e)}})}async monitorTransaction(t,n){this.listeners.set(t,n),this.logger.debug(`📡 Starting to monitor transaction: ${t}`),this.logger.debug(`📡 WebSocket connected: ${!!this.socket&&this.socket.connected}`);const o=this.eventBuffer.get(t);if(o){this.logger.debug(`📡 [Event Buffer] Found buffered event for ${t}, delivering immediately`),setImmediate(()=>{this.processTransactionEvent(t,o,n)});const e=this.eventBufferTimeouts.get(t);e&&(clearTimeout(e),this.eventBufferTimeouts.delete(t)),this.eventBuffer.delete(t)}const i=this.config.timeout||3e5,r=setTimeout(()=>{if(this.listeners.has(t)){const o=Math.round(i/1e3),r={transactionId:t,status:e.SDKTransactionStatus.TIMEOUT,message:`Transaction monitoring timeout - no response after ${o} seconds`,timestamp:Date.now()};this.logger.debug(`📡 Transaction timeout for ${t} (${o}s)`),n(r),this.listeners.delete(t),this.timeouts.delete(t),this.socket?.off(t)}},i);if(this.timeouts.set(t,r),this.socket&&this.socket.connected)this.socket.off(t),this.logger.debug(`📡 Listening for transaction updates: ${t}`),this.logger.debug(`📡 WebSocket connection ID: ${this.socket.id}`),this.logger.debug(`📡 WebSocket URL: ${this.config.url}`),this.socket.on(t,e=>{this.processTransactionEvent(t,e,n)});else{const o={transactionId:t,status:e.SDKTransactionStatus.FAILED,message:"WebSocket not connected - cannot monitor transaction",timestamp:Date.now()};n(o),this.listeners.delete(t),this.timeouts.delete(t)}}processTransactionEvent(t,n,o){this.logger.debug(`📡 Socket.IO transaction update for ${t}:`,JSON.stringify(n,null,2));const i=n,r=i?.data,a=i?.status||i?.Status||r?.status||r?.Status;let s=i?.message||i?.Message||r?.message||r?.Message||i?.error||r?.error;s&&"string"==typeof s||(s=a===Mo.FAILED||a===Mo.ERROR?"Transaction failed - check transaction details":a===Mo.COMPLETED||a===Mo.PROCESSED||a===Mo.SUCCESS?"Transaction completed successfully":a?`Transaction status: ${a}`:"Unknown transaction status");const c=i?.blockHash||r?.blockHash,l=i?.gasUsed||r?.gasUsed,u=i?.Data||r?.Data,d={transactionId:t,status:this.mapSocketStatus(a),message:"string"==typeof s?s:"Transaction update received",timestamp:Date.now(),...c&&{blockHash:c},...l&&{gasUsed:l},...u&&{data:u}};if(this.logger.debug(`📡 Mapped status for ${t}: ${a} -> ${d.status}`),this.logger.debug(`📡 Final message: "${s}"`),o(d),d.status===e.SDKTransactionStatus.COMPLETED||d.status===e.SDKTransactionStatus.FAILED){this.listeners.delete(t);const e=this.timeouts.get(t);e&&(clearTimeout(e),this.timeouts.delete(t)),this.socket?.off(t),this.logger.debug(`📡 Cleaned up listener for ${t} (${d.status})`)}}async waitForTransaction(t){return new Promise((n,o)=>{this.monitorTransaction(t,t=>{t.status===e.SDKTransactionStatus.COMPLETED?n(t):t.status!==e.SDKTransactionStatus.FAILED&&t.status!==e.SDKTransactionStatus.TIMEOUT||o(new Error(`Transaction ${t.status}: ${t.message}`))})})}mapSocketStatus(t){const n=t?.toUpperCase();return qo[n]||e.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.eventBuffer.clear(),this.eventBufferTimeouts.forEach(e=>{clearTimeout(e)}),this.eventBufferTimeouts.clear(),this.logger.debug("🧹 Cleared event buffer and timeouts"),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 Go extends Tn{constructor(e,t=!1){super(t),this.poolService=e,this.cache=new Map}async resolveTokenToVault(e){if(!j(e))throw new P("Token name is required and must be a non-empty string","tokenName","INVALID_TOKEN_NAME");const t=e.trim().toLowerCase(),n=this.get(t);if(n)return n;try{const n=await this.poolService.resolveTokenNameToVault(e);return n&&this.set(t,n),n}catch{return null}}async resolveTokenClassKey(e){const t=await this.resolveTokenToVault(e);if(!t)throw U(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:n}of e)this.set(t,n)}parseVaultAddressToTokenClassKey(e){try{return go(e)}catch(e){if(e instanceof P)throw R("vaultAddress","format: service|Token$Unit$SYMBOL$eth:address$launchpad","Vault address");throw e}}}async function zo(e,t={}){const{maxPages:n=1e4,logger:o,pageSize:i=20}=t,r=[];let a=1,s=!0,c=0;for(;s&&a<=n;){o&&o.debug(`Auto-pagination: fetching page ${a} with limit ${i}`);const t=await e(a,i);if(!t||!Array.isArray(t.items)){o&&o.warn("Auto-pagination: received invalid result structure, stopping");break}if(r.push(...t.items),c=t.total,o&&o.debug(`Auto-pagination: page ${a} returned ${t.items.length} items, hasNext: ${t.hasNext}`),0===t.items.length){o&&o.debug(`Auto-pagination: no items returned on page ${a}, exiting loop`);break}s=t.hasNext,a++}return a>n&&o&&o.warn(`Auto-pagination: exceeded maxPages limit of ${n}, stopping iteration`),o&&o.debug(`Auto-pagination: completed with total items: ${r.length}, total count: ${c}`),{items:r,total:c}}function Wo(e,t=e.length,n="items"){return{...{page:1,limit:e.length||0,total:t,totalPages:Math.ceil(t/(e.length||1))||1,hasNext:!1,hasPrevious:!1},[n]:e}}class jo extends vn{constructor(e,t=!1,n){super(e,t),this.tokenResolverService=n}async fetchTokenClassKeyByTokenName(e){if(!this.tokenResolverService)throw K("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 K("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 K(`Token '${e}' not found or could not be resolved to vault address`,"tokenName");this.logger.debug(`Resolved '${e}' to vault address: ${t}`);const n=go(t),o=`${n.collection}|${n.category}|${n.type}|${n.additionalKey}`;return this.logger.debug(`Extracted token class key: ${o}`),o}catch(t){if(t instanceof Error&&t.message.includes("ConfigurationError"))throw t;throw q(`Failed to resolve token name '${e}': ${_n(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 n=await this.fetchTokenClassKeyByTokenName(e.tokenName);t=n,this.logger.debug(`Resolved to token ID: ${n}`)}if(!t)throw K("Token ID is required but was not provided or resolved","tokenId");const{normalizeToTokenInstanceKey:n}=await Promise.resolve().then(function(){return yo}),o=n(t),i=ro(`${o.collection}|${o.category}|${o.type}|${o.additionalKey}`),{from:r,to:a,sortOrder:s="DESC",page:c=1,limit:l=10}=e,u={token:i,page:String(c),limit:String(l)};r&&(u.from=r.toISOString()),a&&(u.to=a.toISOString());const d=function(e){if(e)return e.toLowerCase()}(s);d&&(u.order=d),this.logger.debug(`Querying price snapshots for token ${i}, page ${c}, limit ${l}`);const h=await this.http.get("/price-oracle/fetch-price",u);if(!h)throw q("No response from price history service",500);const 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 q(`Failed to fetch price history: ${_n(e)}`,500)}}transformApiResponseToPriceHistory(e){if(!e?.data?.data||!Array.isArray(e.data.data))throw q("Invalid API response: missing or invalid data.data array",500);if(!e?.data?.meta)throw q("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}`})),n=e.data.meta,o=n.currentPage??1,i=n.totalPages??1;return{snapshots:t,page:o,limit:n.pageSize??50,total:n.totalItems??0,totalPages:i,hasNext:o<i,hasPrevious:o>1}}async fetchAllPriceHistory(e){this.logger.debug("Fetching all price history with options:",e);const t=await zo((t,n)=>this.fetchPriceHistory({...e,page:t,limit:n}).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 Wo(t.items,t.total,"snapshots")}validateOptions(e){const{from:t,to:n,sortOrder:o,page:i=1,limit:r=10}=e;if(function(e,t,n,o={}){const{description:i="parameter",treatEmptyAsNull:r=!0}=o,a=e[t],s=e[n],c=null!=a&&(!r||""!==a),l=null!=s&&(!r||""!==s);if(!c&&!l)throw K(`Either ${t} or ${n} must be provided (${i})`,n);if(c&&l)throw K(`Cannot provide both ${t} and ${n}. Provide exactly one (${i}).`,n)}(e,"tokenName","tokenId",{description:"token identifier"}),t&&!(t instanceof Date)&&isNaN(new Date(t).getTime()))throw K("from must be a valid Date","from");if(n&&!(n instanceof Date)&&isNaN(new Date(n).getTime()))throw K("to must be a valid Date","to");if(o&&"ASC"!==o&&"DESC"!==o)throw K('sortOrder must be either "ASC" or "DESC"',"sortOrder");if(!Number.isInteger(i)||i<1)throw K("page must be a positive integer","page");if(!Number.isInteger(r)||r<1||r>50)throw K("limit must be between 1 and 50","limit")}}function Ho(e){if("object"==typeof e&&null!==e)return function(e){if(!e||"object"!=typeof e)throw new P("Token object must be a non-null object, got "+typeof e,"token","INVALID_TOKEN_OBJECT");const{collection:t,category:n,type:o,additionalKey:i}=e;if(!t||"string"!=typeof t)throw new P("Token.collection must be a non-empty string, got "+typeof t,"token.collection","MISSING_OR_INVALID_COLLECTION");if(!n||"string"!=typeof n)throw new P("Token.category must be a non-empty string, got "+typeof n,"token.category","MISSING_OR_INVALID_CATEGORY");if(!o||"string"!=typeof o)throw new P("Token.type must be a non-empty string, got "+typeof o,"token.type","MISSING_OR_INVALID_TYPE");if(!i||"string"!=typeof i)throw new P("Token.additionalKey must be a non-empty string, got "+typeof i,"token.additionalKey","MISSING_OR_INVALID_ADDITIONAL_KEY");return{collection:t,category:n,type:o,additionalKey:i}}(e);if(!e)throw new P(`Token cannot be null, undefined, or empty. Received: ${JSON.stringify(e)}`,"token","EMPTY_TOKEN");if("string"!=typeof e)throw new P("Token must be a string or TokenClassKey object, got "+typeof e,"token","INVALID_TOKEN_TYPE");if(e.includes("|"))return function(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 pipe-delimited token format. Expected 4+ parts separated by |, got ${t.length}`);const[n,o,i,...r]=t;if(!n||!o||!i)throw new Error("Collection, category, and type must be non-empty");const a=r.join("|");if(!a)throw new Error("AdditionalKey must be non-empty");return{collection:n,category:o,type:i,additionalKey:a}}catch(t){const n=e.split("|");throw new P(`Invalid pipe-delimited token: "${e}" (${n.length} parts). Expected format: "collection|category|type|additionalKey" (4 parts minimum). Received: [${n.map(e=>`"${e}"`).join(", ")}]. Error: ${t instanceof Error?t.message:String(t)}`,"token","INVALID_PIPE_DELIMITED_TOKEN")}}(e);if(e.includes("$"))return function(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 4+ parts separated by $, got ${t.length}`);const n=t[t.length-1],o=t[t.length-2],i=t[t.length-3],r=t.slice(0,t.length-3).join("$");if(!(r&&i&&o&&n))throw new Error("All components (collection, category, type, additionalKey) must be non-empty");return{collection:r,category:i,type:o,additionalKey:n}}catch(t){const n=e.split("$");throw new P(`Invalid dollar-delimited token: "${e}" (${n.length} parts). Expected format: "collection$category$type$additionalKey" (4 parts minimum). Received: [${n.map(e=>`"${e}"`).join(", ")}]. Error: ${t instanceof Error?t.message:String(t)}`,"token","INVALID_DOLLAR_DELIMITED_TOKEN")}}(e);throw new P(`Plain token string "${e}" (length: ${e.length}) is not allowed - tokens must be delimited with | or $. Expected format: "GALA|Unit|none|none" or "GALA$Unit$none$none". Input: "${e}"`,"token","PLAIN_STRING_NOT_ALLOWED")}class Vo extends Error{constructor(e,t,n){super(`API Error [${e}]: ${t}`),this.status=e,this.message=t,this.details=n,this.name="ApiError"}}function Qo(e){return"object"==typeof e&&null!==e&&"collection"in e&&"category"in e&&"type"in e&&"additionalKey"in e&&"string"==typeof e.collection&&"string"==typeof e.category&&"string"==typeof e.type&&"string"==typeof e.additionalKey}function Xo(e){return"object"==typeof e&&null!==e&&"positionId"in e&&"owner"in e&&"token0"in e&&"token1"in e&&"fee"in e&&"liquidity"in e&&"string"==typeof e.positionId&&"string"==typeof e.owner&&"number"==typeof e.fee}class Zo{constructor(e){this.client=t.create({baseURL:e.baseUrl,timeout:e.timeout??3e4})}async getPoolData(e){try{if("string"==typeof e.token0||"string"==typeof e.token1)throw new Error(`GalaChain API getPoolData requires TokenClassKey objects, not strings. Received: token0="${"string"==typeof e.token0?e.token0:"[object]"}", token1="${"string"==typeof e.token1?e.token1:"[object]"}". Convert pipe-delimited tokens using parseToken() before calling getPoolData(). Example: parseToken("GALA|Unit|none|none") → { collection: "GALA", category: "Unit", type: "none", additionalKey: "none" }`);const t=await this.client.post("/api/asset/dexv3-contract/GetPoolData",e);this.validateResponse(t.data);const n=t.data.Data;if(!function(e){if("object"!=typeof e||null===e)return!1;const t=e;return"string"==typeof t.token0&&"string"==typeof t.token1&&Qo(t.token0ClassKey)&&Qo(t.token1ClassKey)&&"number"==typeof t.fee&&"number"==typeof t.tickSpacing&&"string"==typeof t.liquidity&&"string"==typeof t.sqrtPrice&&"number"==typeof t.tick&&"string"==typeof t.feeGrowthGlobal0&&"string"==typeof t.feeGrowthGlobal1}(n))throw new Vo(t.status,"Invalid pool data response format",n);return n}catch(e){throw this.handleError(e,"getPoolData")}}async getPositions(e){try{const t=await this.client.post("/api/asset/dexv3-contract/GetPositions",e);this.validateResponse(t.data);const n=t.data.Data;if(!Array.isArray(n.positions))throw new Vo(t.status,"Invalid positions response format: positions must be an array",n);for(const e of n.positions)if(!Xo(e))throw new Vo(t.status,"Invalid position in response",e);return n}catch(e){throw this.handleError(e,"getPositions")}}async getPositionById(e,t,n,o,i,r,a){try{let s,c;if(void 0!==t&&void 0!==n&&void 0!==o&&void 0!==i&&void 0!==r){s={owner:e,token0:"string"==typeof t?{collection:t,category:"Unit",type:"none",additionalKey:"none"}:t,token1:"string"==typeof n?{collection:n,category:"Unit",type:"none",additionalKey:"none"}:n,fee:o,tickLower:i,tickUpper:r},a&&(s.positionId=a),c=`${e}/${t}/${n}/${o}`}else s={positionId:e},c=e;const l=await this.client.post("/api/asset/dexv3-contract/GetPositions",s);this.validateResponse(l.data);const u=l.data.Data;let d;if(u&&"object"==typeof u&&"positionId"in u&&!("positions"in u))d=u;else{if(!(u&&Array.isArray(u.positions)&&u.positions.length>0))throw new Vo(404,`Position not found: ${c}`);d=u.positions[0]}const h={Data:d,Status:l.status};return void 0!==l.data.Message&&(h.Message=l.data.Message),h}catch(t){throw this.handleError(t,`getPositionById(${e})`)}}async getRemoveLiquidityEstimation(e){try{const t=await this.client.post("/api/asset/dexv3-contract/GetRemoveLiquidityEstimation",e);this.validateResponse(t.data);const n=t.data.Data;if("string"!=typeof n.amount0||"string"!=typeof n.amount1||"string"!=typeof n.fee0||"string"!=typeof n.fee1)throw new Vo(t.status,"Invalid removal estimation response format",n);return n}catch(e){throw this.handleError(e,"getRemoveLiquidityEstimation")}}validateResponse(e){if(!e||"object"!=typeof e)throw new Vo(500,"Invalid response format: not an object");if(!("Data"in e)||!("Status"in e))throw new Vo(500,"Invalid response format: missing Data or Status field");if(e.Status>=400)throw new Vo(e.Status,e.Message??"Gateway error",e.Data)}handleError(e,n){if(e instanceof Vo)return e;if(t.isAxiosError(e)){const t=e.response?.status??500,o=e.response?.data?.Message??e.message,i=e.response?.data?.Data??void 0;return new Vo(t,`${n}: ${o}`,i)}return new Vo(500,`${n}: ${e instanceof Error?e.message:String(e)}`)}}class Yo{constructor(e){this.http=e}async getUserAssets(e,t=20,n=0){try{if(!e||"string"!=typeof e)throw new Vo(400,"Invalid wallet address");const o=Math.max(1,Math.floor(n/t)+1),i={};i.address=e,i.page=o,i.limit=t;const r=await this.http.get("/user/assets",i);if(!r||"object"!=typeof r)throw new Vo(500,"Invalid response format: not an object");const a=r.data;if(!a||"object"!=typeof a)throw new Vo(500,"Invalid response format: missing data wrapper");const s=a.token;if(!Array.isArray(s))throw new Vo(500,"Invalid response format: token array must be an array");const c=[];for(const e of s){if("object"!=typeof e||null===e)throw new Vo(500,"Invalid asset in response: asset must be an object");const t=e;if("string"!=typeof t.symbol||"string"!=typeof t.name)throw new Vo(500,"Invalid asset in response: missing symbol or name",t);const n="number"==typeof t.decimals?t.decimals:"string"==typeof t.decimals?parseInt(t.decimals,10):void 0;if("number"!=typeof n||isNaN(n))throw new Vo(500,"Invalid asset in response: decimals must be a number",t);const o={tokenId:t.compositeKey||`${t.symbol}$Unit$none$none`,symbol:t.symbol,name:t.name,decimals:n,balance:t.quantity||"0"};t.image&&(o.imageUrl=t.image),t.verify&&(o.verified=t.verify),c.push(o)}const l={tokens:c,count:a.count??c.length};return void 0!==a.totalValue&&(l.totalValue=a.totalValue),l}catch(t){throw this.handleError(t,`getUserAssets(${e})`)}}handleError(e,t){if(e instanceof Vo)return e;if(e instanceof Error&&e instanceof Error&&"response"in e){const n=e,o=n.response?.status??500,i=n.response?.data?.message??n.response?.data?.Message??n.message,r=n.response?.data?.Data??n.response?.data?.data??void 0;return n.response,new Vo(o,`${t}: ${i}`,r)}return new Vo(500,`${t}: ${e instanceof Error?e.message:String(e)}`)}}const Jo=10;class ei extends Tn{constructor(e,t,n){if(super(!1),this.pricingConcurrency=5,this.tokenConverter=new io,this.webSocketService=t,this.dexQuoteService=n,this.getWalletAddress=e.getWalletAddress,this.galaChainBaseUrl=e.galaChainBaseUrl,this.bundlerBaseUrl=e.bundlerBaseUrl,this.gatewayBaseUrl=e.gatewayBaseUrl,this.privateKey=e.privateKey,!(e.gatewayBaseUrl&&e.bundlerBaseUrl&&e.dexBackendBaseUrl&&e.dexBackendHttp))throw new Error("GSwapService requires explicit gatewayBaseUrl, bundlerBaseUrl, dexBackendBaseUrl, and dexBackendHttp configuration. These must be provided by LaunchpadSDK to ensure environment alignment.");try{this.gatewayClient=new Zo({baseUrl:e.gatewayBaseUrl,timeout:3e4}),this.dexBackendClient=new Yo(e.dexBackendHttp),this.logger.debug("HTTP clients initialized successfully",{gatewayUrl:e.gatewayBaseUrl,dexBackendUrl:e.dexBackendBaseUrl})}catch(e){throw this.logger.error("Failed to initialize HTTP clients",e),new Error("Failed to initialize GSwapService HTTP clients")}}setPricingConcurrency(e){if(e<1)throw new Error("Pricing concurrency must be at least 1");e>100&&this.logger.warn("Pricing concurrency > 100 may cause performance issues",{concurrency:e}),this.pricingConcurrency=e,this.logger.debug("Updated pricing concurrency",{concurrency:this.pricingConcurrency})}async getSwapQuoteExactInput(e){try{if(new r(e.amount).isLessThanOrEqualTo(0))throw new F("Amount must be greater than zero",{amount:e.amount,fromToken:e.fromToken,toToken:e.toToken});if(!this.dexQuoteService)throw new F("DexQuoteService not configured - cannot provide quotes",{fromToken:e.fromToken,toToken:e.toToken});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),n=this.tokenConverter.toLaunchpadFormat(e.toToken),o=[3e3,500,1e4];let i;for(const a of o)try{const o=await this.dexQuoteService.fetchCompositePoolData({token0:t,token1:n,fee:a,gatewayBaseUrl:this.gatewayBaseUrl}),i=await this.dexQuoteService.calculateDexPoolQuoteExactAmount({compositePoolData:o,fromToken:t,toToken:n,amount:e.amount}),s=new r(i.currentSqrtPrice),c=new r(i.newSqrtPrice),l=s.gt(c)?s.minus(c).dividedBy(s):new r(0),u=new r(i.amount0),d=new r(i.amount1),h=u.isNegative(),g=d.isNegative();this.logger.debug("=== AMOUNT SELECTION RAW DATA ===",{"quoteResult.amount0":i.amount0,"quoteResult.amount1":i.amount1,"amount0BN.isNegative()":h,"amount1BN.isNegative()":g});const p=h?u:d;this.logger.debug("=== AMOUNT SELECTION RESULT ===",{selectedFromAmount0:h,selectedAmount:p.toFixed(),selectedAmountAbs:p.absoluteValue().toFixed()});const m=p.absoluteValue().toFixed();return{fromToken:e.fromToken,toToken:e.toToken,inputAmount:e.amount,estimatedOutput:m,feeTier:a,priceImpact:l.toFixed(),executionPrice:this.calculateExecutionPrice(e.amount,m),currentSqrtPrice:i.currentSqrtPrice,newSqrtPrice:i.newSqrtPrice}}catch(e){i=e,this.logger.debug("DexQuoteService failed for fee tier, trying next",{feeTier:a,error:e instanceof Error?e.message:"Unknown error"})}throw i||new F("No available fee tiers for quote",{feeTiers:o,fromToken:e.fromToken,toToken:e.toToken})}catch(e){this.handleGSwapError("Failed to get swap quote for exact input",F,e)}}async getSwapQuoteExactOutput(e){try{if(new r(e.amount).isLessThanOrEqualTo(0))throw new F("Amount must be greater than zero",{amount:e.amount,fromToken:e.fromToken,toToken:e.toToken});if(!this.dexQuoteService)throw new F("DexQuoteService not configured - cannot provide quotes",{fromToken:e.fromToken,toToken:e.toToken});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),n=this.tokenConverter.toLaunchpadFormat(e.toToken),o=[3e3,500,1e4];let i;for(const a of o)try{const o=await this.dexQuoteService.fetchCompositePoolData({token0:t,token1:n,fee:a,gatewayBaseUrl:this.gatewayBaseUrl}),i=await this.dexQuoteService.calculateDexPoolQuoteExactAmount({compositePoolData:o,fromToken:t,toToken:n,amount:e.amount}),s=new r(i.currentSqrtPrice),c=new r(i.newSqrtPrice),l=s.gt(c)?s.minus(c).dividedBy(s):new r(0),u=o.pool.token0,d="string"==typeof u?u.split("|")[0]:"object"==typeof u&&null!==u&&"tokenName"in u?u.tokenName:String(u),h=n.split("|")[0]===d?i.amount1:i.amount0;return{fromToken:e.fromToken,toToken:e.toToken,inputAmount:h,estimatedOutput:e.amount,feeTier:a,priceImpact:l.toFixed(),executionPrice:this.calculateExecutionPrice(h,e.amount),currentSqrtPrice:i.currentSqrtPrice,newSqrtPrice:i.newSqrtPrice}}catch(e){i=e,this.logger.debug("DexQuoteService failed for fee tier, trying next",{feeTier:a,error:e instanceof Error?e.message:"Unknown error"})}throw i||new F("No available fee tiers for quote",{feeTiers:o,fromToken:e.fromToken,toToken:e.toToken})}catch(e){this.handleGSwapError("Failed to get swap quote for exact output",F,e)}}async executeSwap(e){try{if(!this.privateKey)throw new Error("GSwapService not initialized with signing capability (privateKey required)");this.logger.debug("Executing swap",{fromToken:e.fromToken,toToken:e.toToken,inputAmount:e.inputAmount});const{gswapToken0:t,gswapToken1:n}=this.convertTokenPair(e.fromToken,e.toToken),o=function(e,t=.01){const n=Zn(e),o=new r(1).minus(t);return n.multipliedBy(o)}(e.estimatedOutput,e.slippageTolerance||.01),i=this.getWalletAddress();if(!i)throw new Error("Wallet address required for swap execution");let a;try{const t=await this.getSwapQuoteExactInput({fromToken:e.fromToken,toToken:e.toToken,amount:e.inputAmount});a=t.currentSqrtPrice,this.logger.debug("Quote refetch successful - extracted sqrtPrices",{currentSqrtPrice:a,newSqrtPrice:t.newSqrtPrice,feeTier:t.feeTier})}catch(t){this.logger.debug("Could not re-fetch quote for sqrtPrice, using default",{fromToken:e.fromToken,toToken:e.toToken,error:t instanceof Error?t.message:String(t)})}const s={fromToken:t,toToken:n,inputAmount:e.inputAmount,minOutput:o.toFixed(),feeTier:e.feeTier,walletAddress:i,slippageTolerance:e.slippageTolerance||.01};void 0!==a&&(s.currentSqrtPrice=a);const c=await this.sendSwapToBundler(s);this.logger.debug("Swap submitted, monitoring transaction",{transactionId:c,fromToken:e.fromToken,toToken:e.toToken}),await this.ensureWebSocketConnected();const l=await this.webSocketService.waitForTransaction(c);return{transactionId:l.transactionId,status:l.status,fromToken:e.fromToken,toToken:e.toToken,inputAmount:e.inputAmount,outputAmount:e.estimatedOutput,feeTier:e.feeTier,slippageTolerance:e.slippageTolerance||.01,timestamp:new Date(l.timestamp),wait:async e=>{await this.webSocketService.waitForTransaction(c)}}}catch(e){const t=e;this.handleGSwapError("Failed to execute swap",x,e,{transactionHash:t?.txHash})}}async getUserAssets(e,t=1,n=20){try{this.logger.debug("Fetching user assets",{walletAddress:e,page:t,limit:n});const o=await this.dexBackendClient.getUserAssets(e,n,(t-1)*n);return(Array.isArray(o)?o:o?.tokens||[]).map(e=>{const t=e.symbol||"UNKNOWN";try{return{tokenId:Ho(`${t}|Unit|none|none`),symbol:t,balance:(n=e.quantity||e.balance||0,void 0!==o?Zn(n).toFixed(o):Zn(n).toFixed()),decimals:e.decimals||18}}catch(n){return this.logger.debug(`Skipping asset with processing error: ${t}`,{error:n instanceof Error?n.message:String(n),assetData:{symbol:t,quantity:e.quantity,balance:e.balance,decimals:e.decimals}}),null}var n,o}).filter(e=>null!==e)}catch(o){this.handleGSwapError("Failed to fetch user assets",L,o,{walletAddress:e,page:t,limit:n})}}async getAllUserAssets(e){try{this.logger.debug("Fetching all user assets (auto-paginated)",{walletAddress:e});const t=await zo((t,n)=>this.getUserAssets(e,t,n).then(e=>({items:e,page:t,limit:n,total:e.length,totalPages:1,hasNext:e.length===n,hasPrevious:t>1})),{maxPages:1e4,logger:this.logger,pageSize:20});return this.logger.debug("Fetched all user assets",{walletAddress:e,totalAssets:t.items.length}),t.items}catch(t){this.handleGSwapError("Failed to fetch all user assets",L,t,{walletAddress:e})}}async getPoolInfo(e,t){try{this.logger.debug("Fetching pool info",{tokenA:e,tokenB:t});const{gswapToken0:n,gswapToken1:o}=this.convertTokenPair(e,t),i=[500,3e3,1e4];let a=new r(0),s=0;for(const c of i)try{const e="string"==typeof n?Ho(n):n,t="string"==typeof o?Ho(o):o,i=await this.gatewayClient.getPoolData({token0:e,token1:t,fee:c});i&&(a=a.plus(new r(i.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:a.toFixed(),feeTiers:i,swapCount:s}}catch(n){this.logger.warn("Failed to fetch pool info",n);const o=n;return this.logger.debug("Pool error details",{error:new $(`Failed to fetch pool info: ${o?.message||String(n)}`,n,e,t,this.extractGSwapErrorCode(n))}),{tokenA:e,tokenB:t,liquidity:"0",feeTiers:[500,3e3,1e4],swapCount:0}}}chunkArray(e,t){const n=[];for(let o=0;o<e.length;o+=t)n.push(e.slice(o,o+t));return n}async fetchPositionPrices(e){const t=this.pricingConcurrency;if(0===e.length)return new Map;const n=new Map;for(const t of e){const e=`${t.token0}|${t.token1}|${t.feeTier}`;n.has(e)||n.set(e,{token0:t.token0,token1:t.token1,feeTier:t.feeTier})}const o=Array.from(n.values()),i=this.chunkArray(o,t);this.logger.debug("Fetching pricing for positions",{totalPositions:e.length,uniquePoolsToPrice:n.size,chunks:i.length,concurrency:t});const r=new Map;for(let e=0;e<i.length;e++){const t=i[e];(await Promise.allSettled(t.map(async e=>{const t=await this.getSwapQuoteExactInput({fromToken:e.token0,toToken:e.token1,amount:"1"});return{key:`${e.token0}|${e.token1}|${e.feeTier}`,data:{token0:e.token0,token1:e.token1,feeTier:e.feeTier,currentPrice:t.executionPrice,executionPrice:t.executionPrice,priceImpact:t.priceImpact,estimatedOutput:t.estimatedOutput,pricedAt:new Date}}}))).forEach(e=>{"fulfilled"===e.status?r.set(e.value.key,e.value.data):this.logger.warn("Failed to fetch price for pool",{error:e.reason})})}return r}normalizePositionResponse(e,t){const n=e=>{if(!e)return"";if("string"==typeof e)return e;if("object"==typeof e){if(e.type&&"none"!==e.type)return e.type;if(e.collection)return e.collection;if(e.symbol)return e.symbol;if(e.tokenSymbol)return e.tokenSymbol;if(e.name)return e.name}return""};return{positionId:e.positionId||e.id||"",ownerAddress:t||e.ownerAddress||e.owner||"",token0:e.token0Symbol||n(e.token0)||n(e.tokenA)||e.tokenSymbol0||"",token1:e.token1Symbol||n(e.token1)||n(e.tokenB)||e.tokenSymbol1||"",feeTier:e.feeTier||e.fee||e.feeAmount||0,tickLower:e.tickLower||e.lowerTick||0,tickUpper:e.tickUpper||e.upperTick||0,liquidity:String(e.liquidity||e.liquidityAmount||"0"),amount0:String(e.amount0||e.amountA||"0"),amount1:String(e.amount1||e.amountB||"0"),feeAmount0:String(e.feeAmount0||e.feesA||"0"),feeAmount1:String(e.feeAmount1||e.feesB||"0"),...e.createdAt&&{createdAt:new Date(e.createdAt)},...e.updatedAt&&{updatedAt:new Date(e.updatedAt)}}}parseTokenFlexible(e){try{return Ho(e)}catch(t){if(t instanceof Error&&t.message?.includes("Plain token string"))return this.logger.debug("Using default TokenClassKey for simple token symbol",{token:e}),{collection:"Token",category:"Unit",type:e,additionalKey:"none"};throw t}}async getUserLiquidityPositions(e,n=10,o,i){try{this.logger.debug("Fetching user liquidity positions",{ownerAddress:e,limit:n,bookmark:o});const r=`${this.galaChainBaseUrl}/api/asset/dexv3-contract/GetUserPositions`,a={user:e,limit:n,bookmark:o||""};this.logger.debug("Sending position query request",{endpoint:r,payload:a});const s=await t.post(r,a,{headers:{"Content-Type":"application/json",Accept:"application/json"}});if(200!==s.status||1!==s.data?.Status)return this.logger.warn("Unexpected API response status",{httpStatus:s.status,apiStatus:s.data?.Status}),{items:[]};const c=s.data.Data||{},l=c.positions||[],u=c.nextBookMark,d=l.filter(e=>null!=e&&"object"==typeof e&&("positionId"in e||"id"in e)).map(t=>this.normalizePositionResponse(t,e));let h;this.logger.debug("Retrieved liquidity positions",{count:d.length,hasNextBookmark:!!u,nextBookmark:u}),i?.withPrices&&d.length>0&&(h=await this.fetchPositionPrices(d));const g={items:d};return void 0!==u&&(g.nextBookmark=u),void 0!==h&&(g.prices=h),g}catch(t){if(t&&"object"==typeof t&&"response"in t){const n=t;this.logger.error("Position query failed with HTTP error",{status:n.response?.status,statusText:n.response?.statusText,data:n.response?.data,endpoint:this.galaChainBaseUrl,ownerAddress:e})}this.handleGSwapError("Failed to fetch user liquidity positions",O,t)}}async getAllSwapUserLiquidityPositions(e,t){try{this.logger.debug("Fetching all user liquidity positions (auto-paginated)",{ownerAddress:e});const n=async t=>{const n=await this.getUserLiquidityPositions(e,Jo,t,void 0);return{items:n.items,nextBookmark:n.nextBookmark}},o=await async function(e,t={}){const{maxPages:n=1e4,logger:o,pageSize:i=20}=t,r=[];let a,s=0;for(;s<n;){o&&o.debug(`Auto-pagination (bookmark): fetching page ${s+1} with pageSize ${i}`,{bookmark:a});const t=await e(a,i);let n,c,l;if(Array.isArray(t))n=t,c=void 0,l=!1;else{if(!t||"object"!=typeof t||!("items"in t)){o&&o.warn("Auto-pagination (bookmark): received invalid result structure, stopping");break}n=t.items,c=t.nextBookmark,l=!0}if(!Array.isArray(n)){o&&o.warn("Auto-pagination (bookmark): received invalid items array, stopping");break}if(0===n.length){o&&o.debug(`Auto-pagination (bookmark): no items returned on page ${s+1}, exiting loop`);break}r.push(...n),s++,o&&o.debug(`Auto-pagination (bookmark): page ${s} returned ${n.length} items`,{hasNextBookmark:!!c,format:l?"BookmarkPaginationResult":"legacy-array"});const u=n.length<i;if(l&&(""===c||void 0===c)){o&&o.debug("Auto-pagination (bookmark): no nextBookmark returned, reached end of results",{nextBookmark:""===c?"(empty string)":"(undefined)"});break}if(u){o&&o.debug("Auto-pagination (bookmark): received fewer items than limit, reached last page",{received:n.length,pageSize:i,format:l?"BookmarkPaginationResult":"legacy-array"});break}a=c}return s>=n&&o&&o.warn(`Auto-pagination (bookmark): exceeded maxPages limit of ${n}, stopping iteration`),o&&o.debug(`Auto-pagination (bookmark): completed with total items: ${r.length}`,{pageCount:s}),{items:r,total:r.length}}(n,{maxPages:1e4,logger:this.logger,pageSize:Jo}),i=o.items;if(this.logger.debug("Fetched all user liquidity positions",{ownerAddress:e,totalPositions:i.length}),t?.withPrices&&i.length>0){return{items:i,prices:await this.fetchPositionPrices(i)}}return i}catch(t){this.handleGSwapError("Failed to fetch all user liquidity positions",O,t,{ownerAddress:e})}}async getLiquidityPosition(e,t){try{this.logger.debug("Fetching liquidity position",{ownerAddress:e,position:t});const{gswapToken0:n,gswapToken1:o}=this.convertTokenPair(t.token0,t.token1),i=(await this.gatewayClient.getPositions({owner:e,tokenA:n,tokenB:o,feeTier:t.fee})).positions.find(e=>e.tickLower===t.tickLower&&e.tickUpper===t.tickUpper);if(!i||"object"!=typeof i||!("positionId"in i)&&!("id"in i))throw new Error("Invalid position data returned from API");const r=this.normalizePositionResponse(i,e);return this.logger.debug("Retrieved liquidity position",{positionId:r.positionId}),r}catch(e){this.handleGSwapError("Failed to fetch liquidity position",O,e)}}async getLiquidityPositionById(e,t,n,o,i,r,a){try{let s;this.logger.debug("Fetching liquidity position by ID",{ownerAddress:e,positionId:t,hasToken0:!!n,hasToken1:!!o,hasFee:!!i,hasTickLower:void 0!==r,hasTickUpper:void 0!==a});let c=null;const l=5,u=2e3;for(let d=1;d<=l;d++)try{if(n&&o&&void 0!==i&&void 0!==r&&void 0!==a)try{this.logger.debug("Attempting compound key lookup",{ownerAddress:e,token0:n,token1:o,feeTier:i,tickLower:r,tickUpper:a});if(s=(await this.gatewayClient.getPositionById(e,n,o,i,r,a,t)).Data,s&&"object"==typeof s&&("positionId"in s||"id"in s)){this.logger.debug("Successfully fetched position via compound key",{attempt:d,positionId:t});break}throw new Error("Invalid position data from compound key lookup")}catch(e){this.logger.debug("Compound key lookup failed, trying fallback",{attempt:d,error:e instanceof Error?e.message:e})}try{if(s=(await this.gatewayClient.getPositionById(t)).Data,s&&"object"==typeof s&&("positionId"in s||"id"in s)){this.logger.debug("Successfully fetched position on attempt",{attempt:d,positionId:t});break}throw new Error("Invalid position data from direct lookup")}catch(n){this.logger.debug("Direct position lookup failed, trying fallback via GetUserPositions",{attempt:d,positionId:t,error:n instanceof Error?n.message:n});const o=await this.getUserLiquidityPositions(e,10);if(Array.isArray(o.items)&&o.items.length>0){const e=o.items.find(e=>(e.positionId||e.id||"").toLowerCase()===t.toLowerCase());if(e){s=e,this.logger.debug("Found position via fallback (GetUserPositions)",{attempt:d,positionId:t});break}}if(c=new Error("Position not found in owner positions"),d<l){this.logger.warn("Fallback query did not find position, retrying",{attempt:d,positionId:t,ownerAddress:e,foundCount:Array.isArray(o?.items)?o.items.length:0}),await new Promise(e=>setTimeout(e,u));continue}}}catch(e){if(d<l){this.logger.warn("Error fetching position, retrying",{attempt:d,positionId:t,error:e instanceof Error?e.message:e}),await new Promise(e=>setTimeout(e,u));continue}c=e instanceof Error?e:new Error(String(e))}if(!s||"object"!=typeof s||!("positionId"in s)&&!("id"in s))throw this.logger.error("Invalid position data returned from API after retries",{positionId:t,resultType:typeof s,resultKeys:s?Object.keys(s):"null",resultValue:JSON.stringify(s),lastError:c?.message}),c||new Error("Invalid position data returned from API");const d=this.normalizePositionResponse(s,e);return this.logger.debug("Retrieved liquidity position by ID",{positionId:d.positionId}),d}catch(e){this.handleGSwapError("Failed to fetch liquidity position by ID",O,e)}}async fetchSwapPositionDirect(e){try{this.logger.debug("Fetching swap position via direct compound key",{token0:e.token0,token1:e.token1,fee:e.fee,owner:e.owner});const t="string"==typeof e.token0?this.parseTokenFlexible(e.token0):e.token0,n={token0:t,token1:"string"==typeof e.token1?this.parseTokenFlexible(e.token1):e.token1,fee:e.fee,tickLower:e.tickLower,tickUpper:e.tickUpper,owner:e.owner},o=`${this.galaChainBaseUrl}/api/asset/dexv3-contract/GetPositions`;this.logger.debug("Calling position endpoint via compound key",{url:o});const i=await fetch(o,{method:"POST",headers:{"Content-Type":"application/json",Accept:"application/json"},body:JSON.stringify(n)});if(!i.ok){if(404===i.status)throw new Error("Position not found (HTTP 404): No position exists for this compound key");if(400===i.status)throw new Error("Invalid parameters (HTTP 400): Check token format, fee (500|3000|10000), and tick ranges");throw new Error(`HTTP ${i.status}: ${i.statusText}`)}const r=await i.json();if(1!==r.Status||!r.Data)throw new Error(`Position not found: ${r.Message||"API returned no position data"}`);const a=this.normalizePositionResponse(r.Data,e.owner);return this.logger.debug("Retrieved swap position via compound key",{positionId:a.positionId,token0:a.token0,token1:a.token1}),a}catch(e){this.handleGSwapError("Failed to fetch swap position via compound key",O,e)}}async estimateRemoveLiquidity(e){try{this.logger.debug("Estimating liquidity removal",{token0:e.token0,token1:e.token1});const{gswapToken0:t,gswapToken1:n}=this.convertTokenPair(e.token0,e.token1),o=await this.gatewayClient.getRemoveLiquidityEstimation({tokenA:t,tokenB:n,fee:e.fee,liquidity:e.liquidity,tickLower:e.tickLower,tickUpper:e.tickUpper});return this.logger.debug("Estimated removal",{result:o}),o}catch(e){this.handleGSwapError("Failed to estimate liquidity removal",O,e)}}async addLiquidityByPrice(e){try{if(!this.privateKey)throw new Error("GSwapService not initialized with signing capability (privateKey required)");this.logger.debug("Adding liquidity by price",{token0:e.token0,token1:e.token1,priceRange:`${e.minPrice}-${e.maxPrice}`});const{gswapToken0:t,gswapToken1:n}=this.convertTokenPair(e.token0,e.token1);await this.ensureWebSocketConnected(),this.logger.debug("Converting price range to ticks",{token0:e.token0,token1:e.token1,minPrice:e.minPrice,maxPrice:e.maxPrice,fee:e.fee});const o=Ho(t),i=Ho(n),a=(await this.gatewayClient.getPoolData({token0:o,token1:i,fee:e.fee})).tickSpacing;this.logger.debug("Retrieved tick spacing from pool",{tickSpacing:a,fee:e.fee});const s=new r(e.minPrice),c=new r(e.maxPrice),l=Math.floor(Jn(s)),u=Math.ceil(Jn(c)),d=Math.floor(l/a)*a,h=Math.ceil(u/a)*a;this.logger.debug("Converted price range to ticks",{minPrice:e.minPrice,maxPrice:e.maxPrice,tickLower:d,tickUpper:h,tickSpacing:a});const g=this.getWalletAddress();if(!g)throw new Error("GSwapService: No wallet address available - cannot create position");const p="string"==typeof e.token0?Ho(e.token0):e.token0,m="string"==typeof e.token1?Ho(e.token1):e.token1;this.logger.debug("Sending AddLiquidity by price to bundler",{fee:e.fee,tickRange:`${d}-${h}`,walletAddress:g});const f=await this.sendAddLiquidityToBundler({token0:p,token1:m,fee:e.fee,tickLower:d,tickUpper:h,amount0Desired:e.amount0Desired,amount1Desired:e.amount1Desired,amount0Min:e.amount0Min||"0",amount1Min:e.amount1Min||"0",owner:g}),y={transactionId:f};if(y.positionId&&f){this.logger.debug("Position ID returned directly from backend",{transactionId:f,positionId:y.positionId}),await this.ensureWebSocketConnected();const e=await this.webSocketService.waitForTransaction(f);this.logger.debug("Liquidity transaction confirmed on-chain",{transactionId:f,status:e.status});const t=this.getWalletAddress();if(t&&y.positionId)try{const n=await this.getLiquidityPositionById(t,y.positionId),{createdAt:o,updatedAt:i,...r}=n,a=o instanceof Date?o.getTime():"number"==typeof o?o:void 0,s={...y,...r,positionId:y.positionId,status:e.status,transactionId:e.transactionId,timestamp:new Date(e.timestamp),wait:async e=>{await this.webSocketService.waitForTransaction(f)}};return void 0!==a&&(s.createdAt=a),s}catch(t){return{...y,positionId:y.positionId,status:e.status,transactionId:e.transactionId,timestamp:new Date(e.timestamp),wait:async e=>{await this.webSocketService.waitForTransaction(f)}}}}if(f){this.logger.debug("Monitoring liquidity transaction (discovery mode)",{transactionId:f}),await this.ensureWebSocketConnected();const t=await this.webSocketService.waitForTransaction(f);let n;this.logger.debug("Liquidity transaction confirmed on-chain",{transactionId:f,status:t.status});let o=null;await new Promise(e=>setTimeout(e,2e3));try{const t=this.getWalletAddress();if(!t)throw new Error("No wallet address available");const i=(await this.getUserLiquidityPositions(t,10)).items;if(i&&i.length>0){const t=e.token0.split("|")[0].toUpperCase(),r=e.token1.split("|")[0].toUpperCase(),a=[];for(const n of i){if(!n||!n.positionId)continue;const o=n.token0?.toUpperCase(),i=n.token1?.toUpperCase();if(!o||!i)continue;const s=o===t&&i===r||o===r&&i===t,c=n.feeTier===e.fee;s&&c&&a.push(n)}a.length>0&&(o=a[a.length-1],n=o.positionId,this.logger.debug("Found newly created position",{positionId:n,expectedTokens:`${e.token0}/${e.token1}`,expectedFee:e.fee,positionCount:i.length}))}}catch(e){this.logger.debug("Error waiting for position indexing",{error:e instanceof Error?e.message:String(e)})}let i=o;if(n)try{i=await this.getLiquidityPositionById(g,n)}catch(e){}return{...y,...i,positionId:n,status:t.status,transactionId:t.transactionId,timestamp:new Date(t.timestamp),wait:async e=>{await this.webSocketService.waitForTransaction(f)}}}return this.logger.warn("No transaction ID in liquidity result, cannot confirm position creation"),y}catch(e){Error,this.handleGSwapError("Failed to add liquidity by price",O,e)}}async addSwapLiquidityByTicks(e){try{if(!this.privateKey)throw new Error("GSwapService not initialized with signing capability (privateKey required)");const t=this.getWalletAddress();if(!t)throw new Error("GSwapService: No wallet address available - cannot create position");this.logger.debug("Adding liquidity by ticks with direct bundler",{token0:e.token0,token1:e.token1,fee:e.fee,walletAddress:t,tickRange:`${e.tickLower}-${e.tickUpper}`});const n="string"==typeof e.token0?Ho(e.token0):e.token0,o="string"==typeof e.token1?Ho(e.token1):e.token1;await this.ensureWebSocketConnected();const i=await this.sendAddLiquidityToBundler({token0:n,token1:o,fee:e.fee,tickLower:e.tickLower,tickUpper:e.tickUpper,amount0Desired:e.amount0Desired,amount1Desired:e.amount1Desired,amount0Min:e.amount0Min||"0",amount1Min:e.amount1Min||"0",owner:t});this.logger.info("Liquidity transaction submitted to bundler",{transactionId:i});const r=this.webSocketService.waitForTransaction(i),a={transactionId:i};if(a.positionId&&i){this.logger.info("Position ID returned directly from backend",{transactionId:i,positionId:a.positionId});const e=await r;this.logger.debug("Liquidity transaction confirmed on-chain",{transactionId:i,status:e.status});const t=this.getWalletAddress();if(t&&a.positionId)try{this.logger.debug("Fetching full position details",{positionId:a.positionId});const n=await this.getLiquidityPositionById(t,a.positionId);this.logger.debug("Fetched full position data",{positionId:n.positionId,liquidity:n.liquidity,amount0:n.amount0,amount1:n.amount1});const{createdAt:o,updatedAt:r,...s}=n,c=o instanceof Date?o.getTime():"number"==typeof o?o:void 0,l={...a,...s,positionId:a.positionId,status:e.status,transactionId:e.transactionId,timestamp:new Date(e.timestamp),wait:async e=>{await this.webSocketService.waitForTransaction(i)}};return void 0!==c&&(l.createdAt=c),l}catch(t){return this.logger.warn("Could not fetch full position details",{positionId:a.positionId,error:t instanceof Error?t.message:String(t)}),{...a,positionId:a.positionId,status:e.status,transactionId:e.transactionId,timestamp:new Date(e.timestamp),wait:async e=>{await this.webSocketService.waitForTransaction(i)}}}}if(i){this.logger.debug("Monitoring liquidity transaction (discovery mode)",{transactionId:i});const n=await r;let o;this.logger.debug("Liquidity transaction confirmed on-chain",{transactionId:i,status:n.status});let s=null;const c="string"==typeof e.token0?e.token0:e.token0.type,l="string"==typeof e.token1?e.token1:e.token1.type;this.logger.debug("Waiting for position indexing after WebSocket confirmation"),this.logger.debug("Looking for matching position",{token0:c,token1:l,fee:e.fee});try{const t=this.getWalletAddress();if(!t)throw new Error("No wallet address available");this.logger.debug("Fetching positions from API",{walletAddress:t,pageSize:Jo});const n=3,i=5e3,r=3e3;let a=[];for(let c=1;c<=n;c++){const l=1===c?i:r;this.logger.debug("Position discovery attempt",{attempt:c,maxAttempts:n,delayMs:l}),await new Promise(e=>setTimeout(e,l)),this.logger.debug("Querying positions from API",{attempt:c,pageSize:Jo});if(a=(await this.getUserLiquidityPositions(t,Jo)).items,this.logger.debug("Got positions from API",{count:a?.length||0}),a&&a.length>0){const t=("string"==typeof e.token0?e.token0.split("|")[0]:e.token0.collection).toUpperCase(),n=("string"==typeof e.token1?e.token1.split("|")[0]:e.token1.collection).toUpperCase(),i=[];for(const o of a){if(!o||!o.positionId)continue;const r=o.token0?.toUpperCase(),a=o.token1?.toUpperCase();if(!r||!a){this.logger.debug("Skipping position with empty tokens",{positionId:o.positionId});continue}const s=r===t&&a===n||r===n&&a===t,c=o.feeTier===e.fee;this.logger.debug("Checking position against target",{positionId:o.positionId,tokens:`${r}/${a}`,tokensMatch:s,fee:o.feeTier,feeMatches:c}),s&&c&&i.push(o)}if(i.length>0){s=i[i.length-1],o=s.positionId,this.logger.info("Found newly created position",{positionId:o,liquidity:s.liquidity,amount0:s.amount0,amount1:s.amount1,fee:s.feeTier}),this.logger.debug("Found newly created position",{positionId:o,expectedTokens:`${e.token0}/${e.token1}`,expectedFee:e.fee,positionCount:a.length});break}this.logger.debug("No matching position found in this attempt")}else this.logger.debug("No positions returned from API in this attempt")}}catch(e){this.logger.error("Error fetching positions during discovery",{error:e instanceof Error?e.message:String(e)}),this.logger.debug("Error waiting for position indexing",{error:e instanceof Error?e.message:String(e)})}this.logger.debug("Position discovery complete",{positionId:o||"not found",found:!!o}),this.logger.debug("Matched position data",{positionId:s?.positionId,liquidity:s?.liquidity,amount0:s?.amount0,amount1:s?.amount1,feeAmount0:s?.feeAmount0,feeAmount1:s?.feeAmount1,token0:s?.token0,token1:s?.token1,feeTier:s?.feeTier});let u=s;if(o)try{this.logger.debug("Fetching full position details",{positionId:o}),u=await this.getLiquidityPositionById(t,o),this.logger.debug("Fetched full position data",{positionId:u.positionId,liquidity:u.liquidity,amount0:u.amount0,amount1:u.amount1,feeAmount0:u.feeAmount0,feeAmount1:u.feeAmount1})}catch(e){this.logger.warn("Could not fetch full position details, using discovered data",{positionId:o,error:e instanceof Error?e.message:String(e)})}return{...a,...u,positionId:o,status:n.status,transactionId:n.transactionId,timestamp:new Date(n.timestamp),wait:async e=>{await this.webSocketService.waitForTransaction(i)}}}return this.logger.warn("No transaction ID in liquidity result, cannot confirm position creation"),a}catch(e){this.handleGSwapError("Failed to add liquidity by ticks",O,e)}}async monitorBundlerTransaction(e,t,n="bundler"){let o;try{const i=await t;o={status:i.status,transactionId:i.transactionId||e,timestamp:i.timestamp||Date.now(),data:i.data},this.logger.debug(`${n} transaction confirmed on-chain`,{transactionId:e,status:o.status})}catch(t){return this.logger.warn(`WebSocket monitoring timeout for ${n} transaction, returning result with transaction ID`,{transactionId:e,error:t instanceof Error?t.message:String(t)}),{transactionId:e,status:"SUBMITTED",timestamp:new Date,wait:async t=>{try{await this.webSocketService.waitForTransaction(e)}catch{this.logger.debug("Explicit wait also timed out",{transactionId:e})}}}}return{transactionId:o.transactionId,status:o.status,timestamp:new Date(o.timestamp),wait:async t=>{await this.webSocketService.waitForTransaction(e)}}}async removeLiquidity(e){try{if(!this.privateKey)throw new Error("Private key not available for bundler-direct operations");this.logger.debug("Removing liquidity via bundler",{token0:e.token0,token1:e.token1,liquidity:e.liquidity});try{const t=parseFloat(e.liquidity);if(isNaN(t))throw new Error(`Invalid liquidity value: "${e.liquidity}". Must be a valid number. Position ID: ${e.positionId||"unknown"}`);if(0===t)throw new Error(`Cannot remove zero liquidity from position. This would waste gas fees without any effect. Position ID: ${e.positionId||"unknown"}`)}catch(e){if(e instanceof Error&&e.message.includes("Cannot remove zero liquidity"))throw e;if(e instanceof Error&&e.message.includes("Invalid liquidity value"))throw e;throw e}const t="string"==typeof e.token0?Ho(e.token0):e.token0,n="string"==typeof e.token1?Ho(e.token1):e.token1;await this.ensureWebSocketConnected();const o=await this.sendRemoveLiquidityToBundler(e.tickLower,e.tickUpper,e.liquidity,t,n,e.fee,e.amount0Min||"0",e.amount1Min||"0",e.positionId||"");this.logger.debug("Liquidity removal submitted to bundler",{transactionId:o});const i=this.webSocketService.waitForTransaction(o);return this.monitorBundlerTransaction(o,i,"liquidity removal")}catch(e){this.handleGSwapError("Failed to remove liquidity",O,e)}}async collectPositionFees(e){try{if(!this.privateKey)throw new Error("Private key not available for bundler-direct operations");this.logger.debug("Collecting position fees via bundler",{token0:"string"==typeof e.token0?e.token0:e.token0.type,token1:"string"==typeof e.token1?e.token1:e.token1.type,tickLower:e.tickLower,tickUpper:e.tickUpper});const t="string"==typeof e.token0?Ho(e.token0):e.token0,n="string"==typeof e.token1?Ho(e.token1):e.token1;await this.ensureWebSocketConnected();const o=await this.sendCollectPositionFeesToBundler(t,n,e.fee,e.amount0Requested||"0",e.amount1Requested||"0",e.tickLower,e.tickUpper,e.positionId||"");this.logger.debug("Fee collection submitted to bundler",{transactionId:o});const i=this.webSocketService.waitForTransaction(o);return this.monitorBundlerTransaction(o,i,"fee collection")}catch(e){this.handleGSwapError("Failed to collect position fees",O,e)}}async getPoolData(e,t,n){try{this.logger.debug("Getting pool data",{tokenA:e,tokenB:t,feeTier:n});const{gswapToken0:o,gswapToken1:i}=this.convertTokenPair(e,t),a=Ho(o),s=Ho(i),c=await this.gatewayClient.getPoolData({token0:a,token1:s,fee:n}),l=this.calculatePriceFromSqrtPriceX96(new r(c.sqrtPrice));return{tokenA:e,tokenB:t,feeTier:n,liquidity:c.liquidity.toString(),sqrtPriceX96:c.sqrtPrice.toString(),tick:c.tick,feeGrowthGlobal0X128:c.feeGrowthGlobal0.toString(),feeGrowthGlobal1X128:c.feeGrowthGlobal1.toString(),currentPrice:l.toFixed()}}catch(e){this.handleGSwapError("Failed to get pool data",$,e)}}async calculateDexPoolSpotPrice(e,t,n){try{this.logger.debug("Calculating spot price",{tokenA:e,tokenB:t,feeTier:n});const o=await this.getPoolData(e,t,n),i=Zn(o.currentPrice);return{tokenA:e,tokenB:t,feeTier:n,price:i.toFixed(),invertedPrice:eo(i,!0),tick:o.tick,liquidity:o.liquidity}}catch(e){this.handleGSwapError("Failed to calculate spot price",$,e)}}async calculateOptimalPositionSize(e,t,n,o,i,s,c){try{this.logger.debug("Calculating optimal position size",{tokenA:e,tokenB:t,desiredAmount0:o,desiredAmount1:i});const l=(await this.getPoolData(e,t,n)).tick,u=a.tickToSqrtPrice(s),d=a.tickToSqrtPrice(l),h=a.tickToSqrtPrice(c),g=a.getLiquidityForAmounts(new r(o),new r(i),u,d,h),p=a.getAmountsForLiquidity(g,d,u,h),m=p[0],f=p[1],y=new r(o),w=new r(i);return{amount0:m.toFixed(),amount1:f.toFixed(),liquidity:g.toFixed(),ratio:m.dividedBy(f).toFixed(),utilizationPercent:{amount0:m.dividedBy(y).multipliedBy(100).toFixed(2),amount1:f.dividedBy(w).multipliedBy(100).toFixed(2)}}}catch(e){this.handleGSwapError("Failed to calculate optimal position size",O,e)}}async validatePositionParameters(e,t,n,o,i,a,s){const c=[],l=[];try{this.logger.debug("Validating position parameters",{tokenA:e,tokenB:t,tickLower:o,tickUpper:i});const u=[500,3e3,1e4];u.includes(n)||c.push(`Invalid fee tier: ${n}. Must be one of: ${u.join(", ")}`);const d=this.getTickSpacing(n);let h;o%d!==0&&c.push(`tickLower must be multiple of ${d}`),i%d!==0&&c.push(`tickUpper must be multiple of ${d}`),o>=i&&c.push(`tickLower (${o}) must be less than tickUpper (${i})`);try{h=await this.getPoolData(e,t,n)}catch{return c.push(`Pool not found for ${e}/${t} at fee tier ${n}`),{valid:!1,errors:c,warnings:l,gasEstimate:0}}const g=new r(a),p=new r(s);if(g.isNaN()||p.isNaN())c.push("Amounts must be valid numbers");else try{to(g,p)}catch(e){c.push(`Liquidity amounts must be non-negative: ${e.message}`)}const m=h.tick;(m<o||m>i)&&l.push("Position is out of current price range - will not earn fees until price moves into range");new r(h.liquidity||"0").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){const t=e;return c.includes(t?.message||"")||c.push(`Validation failed: ${t?.message||String(e)}`),{valid:!1,errors:c,warnings:l,gasEstimate:0}}}async calculateTicksForPrice(e,t,n,o,i){try{this.logger.debug("Calculating ticks for price range",{tokenA:e,tokenB:t,minPrice:n,maxPrice:o});const a=this.getTickSpacing(i),s=new r(n),c=new r(o);if(s.gte(c))throw new Error("minPrice must be less than maxPrice");const l=Math.floor(Jn(s)),u=Math.ceil(Jn(c)),d=Math.floor(l/a)*a,h=Math.ceil(u/a)*a,g=Math.pow(1.0001,d),p=Math.pow(1.0001,h),m=new r(g),f=new r(p);return{tokenA:e,tokenB:t,feeTier:i,tickLower:d,tickUpper:h,tickSpacing:a,requestedMinPrice:n,requestedMaxPrice:o,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",O,e)}}async calculatePriceForTicks(e,t,n,o){try{this.logger.debug("Calculating price for ticks",{tokenA:e,tokenB:t,tickLower:n,tickUpper:o});const i=Math.pow(1.0001,n),a=Math.pow(1.0001,o);let s;try{s=(await this.getPoolData(e,t,3e3)).currentPrice}catch{}const c=new r(i),l=new r(a),u={tokenA:e,tokenB:t,tickLower:n,tickUpper:o,minPrice:c.toFixed(8),maxPrice:l.toFixed(8),priceRange:`${c.toFixed(4)} - ${l.toFixed(4)}`,tickSpread:o-n};return void 0!==s&&(u.currentPrice=s),u}catch(e){this.handleGSwapError("Failed to calculate price for ticks",O,e)}}calculateExecutionPrice(e,t){try{const n=new r(e),o=new r(t);return n.isZero()?"0":o.dividedBy(n).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 r(2).pow(96);return e.dividedBy(t).pow(2)}catch{return new r(0)}}calculatePriceFromSqrtPriceDecimal(e){try{return e.pow(2)}catch{return new r(0)}}async getPoolSlot0(e,n,o){try{this.logger.debug("Fetching pool slot0 data",{token0:e,token1:n,fee:o});const i="string"==typeof e?Ho(e):e,r="string"==typeof n?Ho(n):n,a=`${this.galaChainBaseUrl}/api/asset/dexv3-contract/GetSlot0`,s=await t.post(a,{token0:i,token1:r,fee:o},{headers:{"Content-Type":"application/json",Accept:"application/json"}});if(200!==s.status||1!==s.data?.Status)throw this.logger.warn("Unexpected GetSlot0 API response",{httpStatus:s.status,apiStatus:s.data?.Status}),new $("GetSlot0 API returned unexpected status",{httpStatus:s.status,apiStatus:s.data?.Status});const c=s.data.Data||{},l={sqrtPrice:c.sqrtPrice||"0",tick:c.tick||0,liquidity:c.liquidity||"0",grossPoolLiquidity:c.grossPoolLiquidity||"0"};return this.logger.debug("Retrieved pool slot0 data",{sqrtPrice:l.sqrtPrice,tick:l.tick,liquidity:l.liquidity}),l}catch(t){this.handleGSwapError("Failed to fetch pool slot0 data",$,t,{token0:e,token1:n,fee:o})}}async getPositionCurrentPrice(e){try{this.logger.debug("Fetching position current price",{token0:e.token0,token1:e.token1,feeTier:e.feeTier});const t=await this.getPoolSlot0(e.token0,e.token1,e.feeTier),n=new r(t.sqrtPrice),o={price:this.calculatePriceFromSqrtPriceDecimal(n).toFixed(18),sqrtPrice:t.sqrtPrice,tick:t.tick,liquidity:t.liquidity};return this.logger.debug("Calculated position current price",{price:o.price,tick:o.tick}),o}catch(t){this.handleGSwapError("Failed to fetch position current price",$,t,{token0:e.token0,token1:e.token1})}}calculateLiquidityFromAmount0(e,t,n){try{const o=Yn(t),i=Yn(n);return a.liquidity0(e,o,i)}catch{return new r(0)}}calculateLiquidityFromAmount1(e,t,n){try{const o=Yn(t),i=Yn(n);return a.liquidity1(e,o,i)}catch{return new r(0)}}calculateAmount0FromLiquidity(e,t,n){try{const o=Yn(t),i=Yn(n);return a.getAmount0Delta(o,i,e)}catch{return new r(0)}}calculateAmount1FromLiquidity(e,t,n){try{const o=Yn(t),i=Yn(n);return a.getAmount1Delta(o,i,e)}catch{return new r(0)}}convertTokenPair(e,t){return{gswapToken0:this.tokenConverter.toLaunchpadFormat(e),gswapToken1:this.tokenConverter.toLaunchpadFormat(t)}}async sendAddLiquidityToBundler(e){if(!this.privateKey)throw new Error("GSwapService: AddLiquidity requires wallet (full-access mode)");if(!this.bundlerBaseUrl)throw new Error("GSwapService: Bundler URL not configured");try{this.logger.debug("Sending AddLiquidity to bundler",{token0:e.token0.type,token1:e.token1.type,fee:e.fee,tickRange:`${e.tickLower}-${e.tickUpper}`});const o=`galaswap - operation - ${c.v4()}-${Date.now()}-${e.owner}`,i={token0:e.token0,token1:e.token1,fee:e.fee,owner:e.owner,tickLower:e.tickLower,tickUpper:e.tickUpper,amount0Desired:e.amount0Desired,amount1Desired:e.amount1Desired,amount0Min:e.amount0Min,amount1Min:e.amount1Min,positionId:"",uniqueKey:o},r=new n.ethers.Wallet(this.privateKey),a={AddLiquidity:[{name:"token0",type:"token0"},{name:"token1",type:"token1"},{name:"fee",type:"int256"},{name:"owner",type:"string"},{name:"tickLower",type:"int256"},{name:"tickUpper",type:"int256"},{name:"amount0Desired",type:"string"},{name:"amount1Desired",type:"string"},{name:"amount0Min",type:"string"},{name:"amount1Min",type:"string"},{name:"positionId",type:"string"},{name:"uniqueKey",type:"string"}],token0:[{name:"additionalKey",type:"string"},{name:"category",type:"string"},{name:"collection",type:"string"},{name:"type",type:"string"}],token1:[{name:"additionalKey",type:"string"},{name:"category",type:"string"},{name:"collection",type:"string"},{name:"type",type:"string"}]},s={name:"ethereum",chainId:1},l=this.calculatePersonalSignPrefix(i),u={...i,prefix:l},d=await r.signTypedData(s,a,u),h={...u,signature:d,types:a,domain:s};this.logger.debug("AddLiquidity DTO signed with manual types",{signature:h.signature?.substring(0,20)+"...",prefix:h.prefix,tickLower:i.tickLower,tickUpper:i.tickUpper});const g=this.buildLiquidityStringsInstructions(e.token0,e.token1,e.fee,e.owner),p=t.create({baseURL:this.bundlerBaseUrl,timeout:3e4}),m=await p.post("/bundle",{method:"AddLiquidity",signedDto:h,stringsInstructions:g}),f=m.data?.data||m.data?.transactionId||m.data?.id;if(!f)throw this.logger.error("Bundler response structure",{status:m.status,data:m.data,dataType:typeof m.data}),new Error(`Bundler response does not contain transaction ID. Response: ${JSON.stringify(m.data)}`);return this.logger.debug("AddLiquidity transaction sent to bundler",{transactionId:f}),f}catch(e){throw this.logger.error("Failed to send AddLiquidity to bundler",e),e}}async sendRemoveLiquidityToBundler(e,o,i,r,a,s,l,u,d){try{if(!this.bundlerBaseUrl)throw new Error("GSwapService: Bundler URL not configured");const h=new n.ethers.Wallet(this.privateKey),g=await h.getAddress(),p=`galaswap - operation - ${c.v4()}-${Date.now()}-${g}`,m={tickLower:e,tickUpper:o,amount:i,token0:r,token1:a,fee:s,amount0Min:l,amount1Min:u,positionId:d,uniqueKey:p},f={RemoveLiquidity:[{name:"tickLower",type:"int256"},{name:"tickUpper",type:"int256"},{name:"amount",type:"string"},{name:"token0",type:"token0"},{name:"token1",type:"token1"},{name:"fee",type:"int256"},{name:"amount0Min",type:"string"},{name:"amount1Min",type:"string"},{name:"positionId",type:"string"},{name:"uniqueKey",type:"string"}],token0:[{name:"additionalKey",type:"string"},{name:"category",type:"string"},{name:"collection",type:"string"},{name:"type",type:"string"}],token1:[{name:"additionalKey",type:"string"},{name:"category",type:"string"},{name:"collection",type:"string"},{name:"type",type:"string"}]},y={name:"ethereum",chainId:1},w=this.calculatePersonalSignPrefix(m),k={...m,prefix:w},v=await h.signTypedData(y,f,k),T={...k,signature:v,types:f,domain:y},S=this.buildLiquidityStringsInstructions(r,a,s,g);this.logger.debug("Submitting RemoveLiquidity to bundler",{tickLower:e,tickUpper:o,amount:i,fee:s,positionId:d,transactionId:p});const b=t.create({baseURL:this.bundlerBaseUrl,timeout:3e4}),A=await b.post("/bundle",{method:"RemoveLiquidity",signedDto:T,stringsInstructions:S}),E=A.data?.data||A.data?.transactionId||A.data?.id;if(!E)throw this.logger.error("Bundler response structure",{status:A.status,data:A.data,dataType:typeof A.data}),new Error(`Bundler response does not contain transaction ID. Response: ${JSON.stringify(A.data)}`);return this.logger.info("RemoveLiquidity transaction sent to bundler",{transactionId:E}),E}catch(e){throw this.logger.error("Failed to send RemoveLiquidity to bundler",e),e}}async sendCollectPositionFeesToBundler(e,o,i,r,a,s,l,u){try{if(!this.bundlerBaseUrl)throw new Error("GSwapService: Bundler URL not configured");const d=new n.ethers.Wallet(this.privateKey),h=await d.getAddress(),g=`galaswap - operation - ${c.v4()}-${Date.now()}-${h}`,p={token0:e,token1:o,fee:i,amount0Requested:r,amount1Requested:a,tickLower:s,tickUpper:l,positionId:u,uniqueKey:g},m={CollectPositionFees:[{name:"token0",type:"token0"},{name:"token1",type:"token1"},{name:"fee",type:"int256"},{name:"amount0Requested",type:"string"},{name:"amount1Requested",type:"string"},{name:"tickLower",type:"int256"},{name:"tickUpper",type:"int256"},{name:"positionId",type:"string"},{name:"uniqueKey",type:"string"}],token0:[{name:"additionalKey",type:"string"},{name:"category",type:"string"},{name:"collection",type:"string"},{name:"type",type:"string"}],token1:[{name:"additionalKey",type:"string"},{name:"category",type:"string"},{name:"collection",type:"string"},{name:"type",type:"string"}]},f={name:"ethereum",chainId:1},y=this.calculatePersonalSignPrefix(p),w={...p,prefix:y},k=await d.signTypedData(f,m,w),v={...w,signature:k,types:m,domain:f},T=this.buildLiquidityStringsInstructions(e,o,i,h);this.logger.debug("Submitting CollectPositionFees to bundler",{fee:i,amount0Requested:r,amount1Requested:a,tickLower:s,tickUpper:l,positionId:u,transactionId:g});const S=t.create({baseURL:this.bundlerBaseUrl,timeout:3e4}),b=await S.post("/bundle",{method:"CollectPositionFees",signedDto:v,stringsInstructions:T}),A=b.data?.data||b.data?.transactionId||b.data?.id;if(!A)throw this.logger.error("Bundler response structure",{status:b.status,data:b.data,dataType:typeof b.data}),new Error(`Bundler response does not contain transaction ID. Response: ${JSON.stringify(b.data)}`);return this.logger.info("CollectPositionFees transaction sent to bundler",{transactionId:A}),A}catch(e){throw this.logger.error("Failed to send CollectPositionFees to bundler",e),e}}async sendSwapToBundler(e){if(!this.privateKey)throw new Error("GSwapService: Swap requires wallet (full-access mode)");if(!this.bundlerBaseUrl)throw new Error("GSwapService: Bundler URL not configured");const o=[500,3e3,1e4];if(!o.includes(e.feeTier))throw new Error(`GSwapService: Invalid fee tier ${e.feeTier}. Must be one of: ${o.join(", ")} (basis points)`);try{this.logger.debug("Sending Swap to bundler",{fromToken:"string"==typeof e.fromToken?e.fromToken:e.fromToken.type,toToken:"string"==typeof e.toToken?e.toToken:e.toToken.type,inputAmount:e.inputAmount,minOutput:e.minOutput,feeTier:e.feeTier});let o=e.fromToken,i=e.toToken;"string"==typeof o&&(o=Ho(o)),"string"==typeof i&&(i=Ho(i));const a=`${o.collection}|${o.category}|${o.type}|${o.additionalKey}`,s=`${i.collection}|${i.category}|${i.type}|${i.additionalKey}`,l=a<s?[o,i,a,s]:[i,o,s,a],[u,d,h,g]=l,p="string"==typeof e.fromToken?Ho(e.fromToken):e.fromToken,m=`${p.collection}|${p.category}|${p.type}|${p.additionalKey}`,f=m===h,y=`galaswap - operation - ${c.v4()}-${Date.now()}-${e.walletAddress}`;let w;if(!e.currentSqrtPrice)throw new Error("GSwapService: currentSqrtPrice is required for sqrtPriceLimit calculation");const k=new r(e.currentSqrtPrice),v=e.slippageTolerance??.01;if(f){const e=new r(1).minus(v);w=k.multipliedBy(e).toString()}else{const e=new r(1).plus(v);w=k.multipliedBy(e).toString()}this.logger.debug("Calculated sqrtPriceLimit based on slippage tolerance",{currentSqrtPrice:e.currentSqrtPrice,slippageTolerance:100*v+"%",zeroForOne:f,sqrtPriceLimit:w,direction:f?"token0→token1 (downward price movement)":"token1→token0 (upward price movement)",reason:"sqrtPriceLimit sets price boundaries, amountOutMinimum provides volume protection"});const T={token0:u,token1:d,fee:e.feeTier,amount:new r(e.inputAmount).toFixed(),zeroForOne:f,sqrtPriceLimit:w,recipient:e.walletAddress,amountOutMinimum:new r(e.minOutput).multipliedBy(-1).toFixed(),uniqueKey:y};this.logger.info("🔄 SWAP DTO DETAILS (what we're sending to bundler)",{orderedToken0String:h,orderedToken1String:g,fromTokenStr:m,zeroForOne:f?`TRUE (${h} → ${g})`:`FALSE (${g} → ${h})`,inputAmount:e.inputAmount,expectedOutput:e.minOutput,slippageTolerance:100*(e.slippageTolerance||.01)+"%",currentSqrtPrice:e.currentSqrtPrice,swapDto:{amount:T.amount,zeroForOne:T.zeroForOne,sqrtPriceLimit:T.sqrtPriceLimit,amountOutMinimum:T.amountOutMinimum}});const S=new n.ethers.Wallet(this.privateKey),b={Swap:[{name:"token0",type:"token0"},{name:"token1",type:"token1"},{name:"fee",type:"int256"},{name:"amount",type:"string"},{name:"zeroForOne",type:"bool"},{name:"sqrtPriceLimit",type:"string"},{name:"recipient",type:"string"},{name:"amountOutMinimum",type:"string"},{name:"uniqueKey",type:"string"}],token0:[{name:"additionalKey",type:"string"},{name:"category",type:"string"},{name:"collection",type:"string"},{name:"type",type:"string"}],token1:[{name:"additionalKey",type:"string"},{name:"category",type:"string"},{name:"collection",type:"string"},{name:"type",type:"string"}]},A={name:"ethereum",chainId:1},E=this.calculatePersonalSignPrefix(T),I={...T,prefix:E},P=await S.signTypedData(A,b,I),N={...I,signature:P,types:b,domain:A};this.logger.debug("Swap DTO signed",{signature:N.signature?.substring(0,20)+"...",prefix:N.prefix,zeroForOne:T.zeroForOne});const D=this.buildLiquidityStringsInstructions(u,d,e.feeTier,e.walletAddress),C=t.create({baseURL:this.bundlerBaseUrl,timeout:3e4}),F=await C.post("/bundle",{method:"Swap",signedDto:N,stringsInstructions:D}),x=F.data?.data||F.data?.transactionId||F.data?.id;if(!x)throw this.logger.error("Bundler response structure",{status:F.status,data:F.data,dataType:typeof F.data}),new Error(`Bundler response does not contain transaction ID. Response: ${JSON.stringify(F.data)}`);return this.logger.debug("Swap transaction sent to bundler",{transactionId:x,inputAmount:e.inputAmount,minOutput:e.minOutput}),x}catch(e){throw this.logger.error("Failed to send Swap to bundler",e),e}}buildLiquidityStringsInstructions(e,t,n,o){const i=`$${e.collection}$${e.category}$${e.type}$${e.additionalKey}`,r=`$${t.collection}$${t.category}$${t.type}$${t.additionalKey}`,a=`$pool${i}${r}$${n}`;return[a,`$userPosition${o}`,`$tokenBalance${i}${o}`,`$tokenBalance${r}${o}`,`$tokenBalance${i}${a}`,`$tokenBalance${r}${a}`]}handleGSwapError(e,t,n,o){this.logger.error(e,n);const i=this.extractGSwapErrorCode(n),r=n,a=[`${e}: ${r?.message||String(n)}`,n];throw o&&("GSwapSwapError"===t.name&&o.transactionHash&&a.push(o.transactionHash),"GSwapPoolError"===t.name&&(o.tokenA&&a.push(o.tokenA),o.tokenB&&a.push(o.tokenB)),"GSwapAssetError"===t.name&&o.walletAddress&&a.push(o.walletAddress)),i&&a.push(i),new t(...a)}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()}calculatePersonalSignPrefix(e){return`Ethereum Signed Message:\n${JSON.stringify(e).length}${JSON.stringify(e)}`}}class ti{}ti.BASE_PRICE=1650667151e-14,ti.PRICE_SCALING_FACTOR=1166069e-12,ti.TRADING_FEE_FACTOR=.001,ti.GAS_FEE="1",ti.MIN_UNBONDING_FEE_FACTOR=0,ti.MAX_UNBONDING_FEE_FACTOR=.5,ti.NET_UNBONDING_FEE_FACTOR=.5,ti.DEFAULT_LAUNCHPAD_TOKEN_MAX_SUPPLY=1e7;class ni extends Tn{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 n=this.cache.get(e);if(this.cache.has(e)&&this.cache.delete(e),this.cache.size>=ni.MAX_CACHE_SIZE){const e=this.getLRUKey();null!==e&&this.cache.delete(e)}this.cache.set(e,{...n||{},...t,lastUpdated:Date.now()})}warmFromPoolData(e,t){const n=this.normalizeTokenName(e);this.updateCacheEntry(n,t)}set(e,t){const n=this.normalizeTokenName(e);this.updateCacheEntry(n,t)}get(e){const t=this.normalizeTokenName(e);return this.cache.get(t)||null}getMaxSupply(e){const t=this.normalizeTokenName(e),n=this.cache.get(t);return n?.maxSupply||ti.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,n)=>{e[n]=t}),e}stats(){let e=Date.now(),t=0;return this.cache.forEach((n,o)=>{n.lastUpdated<e&&(e=n.lastUpdated);let i=0;i+=2*o.length,void 0!==n.reverseBondingCurveMinFeeFactor&&(i+=8),void 0!==n.reverseBondingCurveMaxFeeFactor&&(i+=8),void 0!==n.reverseBondingCurveNetFeeFactor&&(i+=8),i+=8,n.vaultAddress&&(i+=2*n.vaultAddress.length),n.maxSupply&&(i+=2*n.maxSupply.length),n.symbol&&(i+=2*n.symbol.length),i+=32,t+=i}),{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 n=`token:${e.toLowerCase().trim()}`;this.updateCacheEntry(n,t)}hasByTokenId(e){const t=`token:${e.toLowerCase().trim()}`;return this.cache.has(t)}}ni.MAX_CACHE_SIZE=1e4;class oi extends vn{constructor(e,t,n=void 0,o=5,i=!1){super(e,i),this.pricingConcurrency=5,this.dexBackendBaseUrl=t,this.gswapService=n,this.pricingConcurrency=o}setGSwapService(e){this.gswapService=e}setPricingConcurrency(e){this.pricingConcurrency=Math.max(1,Math.min(e,20))}async enrichPoolsWithPricing(e){if(!this.gswapService)return this.logger.warn("GSwap service not available, skipping pricing enrichment"),e;if(0===e.length)return e;this.logger.debug("Starting pricing enrichment",{poolCount:e.length,concurrency:this.pricingConcurrency});const t=[];for(let n=0;n<e.length;n++){const o=e[n];t.push({poolIndex:n,token:o.token0,isToken0:!0,task:this.gswapService.getSwapQuoteExactInput({fromToken:o.token0,toToken:"GUSDC",amount:"1"}).then(e=>e.estimatedOutput).catch(e=>{this.logger.debug(`Failed to price ${o.token0}`,{error:e.message})})}),t.push({poolIndex:n,token:o.token1,isToken0:!1,task:this.gswapService.getSwapQuoteExactInput({fromToken:o.token1,toToken:"GUSDC",amount:"1"}).then(e=>e.estimatedOutput).catch(e=>{this.logger.debug(`Failed to price ${o.token1}`,{error:e.message})})})}const n=new Map;for(let t=0;t<e.length;t++)n.set(t,{});for(let e=0;e<t.length;e+=this.pricingConcurrency){const o=t.slice(e,e+this.pricingConcurrency),i=await Promise.allSettled(o.map(e=>e.task));for(let e=0;e<o.length;e++){const t=o[e],r=i[e],a=n.get(t.poolIndex)||{};"fulfilled"===r.status&&r.value&&(t.isToken0?a.token0Price=r.value:a.token1Price=r.value),n.set(t.poolIndex,a)}}const o=e.map((e,t)=>{const o=n.get(t)||{},i={...e};return void 0!==o.token0Price&&(i.token0Price=o.token0Price),void 0!==o.token1Price&&(i.token1Price=o.token1Price),i}),i=o.filter(e=>e.token0Price&&e.token1Price).length;return this.logger.debug("Pricing enrichment complete",{total:e.length,successful:i,failed:e.length-i}),o}async fetchDexPools(e={}){const{search:t,sortBy:n="tvl",sortOrder:o="desc",page:i=kn.DEFAULT_PAGE,limit:r=kn.DEFAULT_LIMIT,withPrices:a=!1}=e;this.logger.debug("Fetching DEX pools",{search:t,sortBy:n,sortOrder:o,page:i,limit:r,withPrices:a});const s=new URLSearchParams({page:i.toString(),limit:Math.min(r,20).toString(),sortBy:n,sortOrder:o});t&&s.append("search",t);const c=`${this.dexBackendBaseUrl}/explore/pools?${s}`;try{const e=await this.http.get(c);if(!e||!e.data)throw new Error("No response from DEX pool service");let t=e.data.pools;const n=e.data.count,o=Math.min(r,20),s=Math.ceil(n/o);return a&&(t=await this.enrichPoolsWithPricing(t)),this.logger.debug("DEX pools fetched successfully",{poolCount:t.length,total:n,totalPages:s,withPrices:a}),{pools:t,page:i,limit:o,total:n,totalPages:s,hasNext:i<s,hasPrevious:i>1}}catch(e){throw this.logger.error("Failed to fetch DEX pools",{error:e,url:c}),e}}async fetchAllDexPools(e={}){this.logger.debug("Fetching all DEX pools (auto-paginated)",e);const t=await zo((t,n)=>this.fetchDexPools({...e,page:t,limit:n}).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});this.logger.debug("All DEX pools fetched",{totalPoolsFetched:t.items.length,totalCount:t.total,withPrices:e.withPrices});return Wo(t.items,t.total,"pools")}}function ii(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 pipe-delimited token format. Expected at least 4 parts separated by |, got ${t.length}`);const[n,o,i,...r]=t;if(!n||!o||!i)throw new Error("Collection, category, and type must be non-empty");const a=r.join("|");if(!a)throw new Error("AdditionalKey must be non-empty");return{collection:n,category:o,type:i,additionalKey:a}}catch(t){throw new P(`Invalid pipe-delimited token: "${e}". Expected format: "collection|category|type|additionalKey". Error: ${t instanceof Error?t.message:String(t)}`,"pipeDelimitedToken","INVALID_PIPE_DELIMITED_TOKEN_FORMAT")}}class ri extends vn{constructor(e,t,n=!1,o=3e4){super(e,n),this.compositePoolFetchConcurrency=5,this.galaChainBaseUrl=t,this.networkTimeout=o}validateFetchCompositePoolDataInput(e,t,n){if(!e||"string"!=typeof e)throw new _("token0 must be a non-empty string",{token0:e});if(!t||"string"!=typeof t)throw new _("token1 must be a non-empty string",{token1:t});const o=e.split("|"),i=t.split("|");if(4!==o.length)throw new _("token0 format must be: collection|category|type|additionalKey (4 pipe-separated parts)",{token0:e});if(4!==i.length)throw new _("token1 format must be: collection|category|type|additionalKey (4 pipe-separated parts)",{token1:t});const r=[500,3e3,1e4];if(!Number.isInteger(n)||!r.includes(n))throw new _(`fee must be one of: ${r.join(", ")} (got ${n})`,{fee:n})}validateQuoteAmount(e){if(!e||"string"!=typeof e)throw new _("amount must be a non-empty string",{amount:e});const t=new r(e);try{no(t,"amount","for quote calculation")}catch(t){throw new _(t.message,{amount:e})}}convertTokenClassKey(e){const t=new s.TokenClassKey;return t.collection=e.collection,t.category=e.category,t.type=e.type,t.additionalKey=e.additionalKey,t}setCompositePoolFetchConcurrency(e){this.compositePoolFetchConcurrency=Math.max(1,Math.min(e,20)),this.logger.debug(`Composite pool fetch concurrency set to ${this.compositePoolFetchConcurrency}`)}async fetchCompositePoolData(e){const{token0:t,token1:n,fee:o,gatewayBaseUrl:i}=e;this.logger.debug("Fetching composite pool data",{token0:t,token1:n,fee:o}),this.validateFetchCompositePoolDataInput(t,n,o);try{const e=ii(t),c=ii(n),l=this.convertTokenClassKey(e),u=this.convertTokenClassKey(c),d=new a.GetCompositePoolDto(l,u,o),h=`${i||this.galaChainBaseUrl}/api/asset/dexv3-contract/GetCompositePool`,g=await this.http.post(h,d);if(!g||1!==g.Status)throw new B(`Pool not found: ${t}/${n} with fee ${o}`);const p=function(e){return{pool:e.pool,tickDataMap:e.tickDataMap,token0Balance:e.token0Balance,token1Balance:e.token1Balance,token0Decimals:e.token0Decimals,token1Decimals:e.token1Decimals,compositePoolDto:e}}(function(e){const t=new a.Pool(e.pool.token0,e.pool.token1,e.pool.token0ClassKey,e.pool.token1ClassKey,e.pool.fee,new r(e.pool.sqrtPrice),e.pool.protocolFees);t.bitmap=e.pool.bitmap,t.grossPoolLiquidity=new r(e.pool.grossPoolLiquidity),t.liquidity=new r(e.pool.liquidity),t.feeGrowthGlobal0=new r(e.pool.feeGrowthGlobal0),t.feeGrowthGlobal1=new r(e.pool.feeGrowthGlobal1),t.protocolFeesToken0=new r(e.pool.protocolFeesToken0),t.protocolFeesToken1=new r(e.pool.protocolFeesToken1),t.tickSpacing=e.pool.tickSpacing,t.maxLiquidityPerTick=new r(e.pool.maxLiquidityPerTick);const n={};Object.keys(e.tickDataMap).forEach(t=>{const o=e.tickDataMap[t],i=new a.TickData(o.poolHash,o.tick);i.initialised=o.initialised,i.liquidityNet=new r(o.liquidityNet),i.liquidityGross=new r(o.liquidityGross),i.feeGrowthOutside0=new r(o.feeGrowthOutside0),i.feeGrowthOutside1=new r(o.feeGrowthOutside1),n[t]=i});const o={...e.token0Balance},i=new s.TokenBalance(o);i.quantity=new r(e.token0Balance.quantity);const c={...e.token1Balance},l=new s.TokenBalance(c);return l.quantity=new r(e.token1Balance.quantity),new a.CompositePoolDto(t,n,i,l,e.token0Decimals,e.token1Decimals)}(g.Data),g.Data);return this.logger.debug("Composite pool data fetched successfully",{token0:t,token1:n,fee:o,liquidity:p.pool.liquidity.toString()}),p}catch(e){if(e instanceof B)throw e;const i=e instanceof Error?e.message:String(e);throw this.logger.error("Failed to fetch composite pool data",e),new _(`Failed to fetch composite pool data: ${i}`,{token0:t,token1:n,fee:o})}}async calculateDexPoolQuoteExactAmountLocal(e){const{compositePoolData:t,fromToken:n,toToken:o,amount:i}=e;if(this.logger.debug("Calculating local DEX quote",{fromToken:n,toToken:o,amount:i}),this.validateQuoteAmount(i),!t)throw new _("compositePoolData is required for local quote calculation",{compositePoolData:t});try{const e=n===t.pool.token0.replace(/\$/g,"|"),s=ii(n),c=ii(o),l=this.convertTokenClassKey(s),u=this.convertTokenClassKey(c),d=new a.QuoteExactAmountDto(l,u,t.pool.fee,new r(i),e,t.compositePoolDto),h=await a.quoteExactAmount(void 0,d);return this.logger.debug("Local quote calculated",{amount0:h.amount0,amount1:h.amount1}),{amount0:h.amount0.toString(),amount1:h.amount1.toString(),currentSqrtPrice:h.currentSqrtPrice.toString(),newSqrtPrice:h.newSqrtPrice.toString()}}catch(e){const t=e instanceof Error?e.message:String(e);throw this.logger.error("Local quote calculation failed",e),new _(`Local quote calculation failed: ${t}`,{fromToken:n,toToken:o,amount:i})}}async calculateDexPoolQuoteExactAmountExternal(e){const{compositePoolData:t,fromToken:n,toToken:o,amount:i}=e;if(this.logger.debug("Calculating external DEX quote",{fromToken:n,toToken:o,amount:i}),this.validateQuoteAmount(i),!t)throw new _("compositePoolData is required for external quote calculation (token format info)",{compositePoolData:t});try{const e=n===t.pool.token0.replace(/\$/g,"|"),s=ii(n),c=ii(o),l=this.convertTokenClassKey(s),u=this.convertTokenClassKey(c),d=new a.QuoteExactAmountDto(l,u,t.pool.fee,new r(i),e,void 0),h=`${this.galaChainBaseUrl}/api/asset/dexv3-contract/QuoteExactAmount`,g=await this.http.post(h,d);if(!g||1!==g.Status)throw new _("External quote failed: "+(g?.Message||"Unknown error"));const p=g.Data;return this.logger.debug("External quote calculated",{amount0:p.amount0,amount1:p.amount1}),{amount0:p.amount0.toString(),amount1:p.amount1.toString(),currentSqrtPrice:p.currentSqrtPrice.toString(),newSqrtPrice:p.newSqrtPrice.toString()}}catch(e){const t=e instanceof Error?e.message:String(e);throw this.logger.error("External quote calculation failed",e),new _(`External quote calculation failed: ${t}`,{fromToken:n,toToken:o,amount:i})}}async calculateDexPoolQuoteExactAmount(e,t="local"){return"external"===t?this.calculateDexPoolQuoteExactAmountExternal(e):this.calculateDexPoolQuoteExactAmountLocal(e)}}class ai{constructor(){this.eventLatencies=[],this.maxLatencySamples=1e4,this.eventsProcessed=0,this.eventsDropped=0,this.queueDepth=0,this.maxQueueDepth=0,this.startTime=Date.now(),this.perPoolMetrics=new Map,this.memorySnapshots=[],this.maxMemorySnapshots=100,this.recordMemory()}recordEventLatency(e){this.eventLatencies.push(e),this.eventLatencies.length>this.maxLatencySamples&&this.eventLatencies.shift(),this.eventsProcessed++,this.lastEventTime=new Date}recordEventDropped(){this.eventsDropped++}updateQueueDepth(e){this.queueDepth=e,this.maxQueueDepth=Math.max(this.maxQueueDepth,e)}recordPoolCacheHit(e,t){const n=this.getPoolMetrics(e);n.cacheHits++,n.eventsProcessed++,n.totalLatency+=t,n.lastEventTime=new Date}recordPoolCacheMiss(e,t){const n=this.getPoolMetrics(e);n.cacheMisses++,n.eventsProcessed++,n.totalLatency+=t,n.lastEventTime=new Date}getLatencyPercentiles(){if(0===this.eventLatencies.length)return{p50:0,p95:0,p99:0};const e=[...this.eventLatencies].sort((e,t)=>e-t),t=Math.floor(.5*e.length),n=Math.floor(.95*e.length),o=Math.floor(.99*e.length);return{p50:e[t]??0,p95:e[n]??0,p99:e[o]??0}}getCacheHitRate(){if(0===this.eventsProcessed)return 0;let e=0;for(const t of this.perPoolMetrics.values())e+=t.cacheHits;return e/this.eventsProcessed*100}getThroughputPerSecond(){const e=(Date.now()-this.startTime)/1e3;return 0===e?0:this.eventsProcessed/e}recordMemory(){if("undefined"!=typeof process&&process.memoryUsage){const e=process.memoryUsage().heapUsed/1024/1024;this.memorySnapshots.push(e),this.memorySnapshots.length>this.maxMemorySnapshots&&this.memorySnapshots.shift()}}getMemoryUsedMB(){return"undefined"!=typeof process&&process.memoryUsage?process.memoryUsage().heapUsed/1024/1024:0}getPoolAverageLatency(e){const t=this.perPoolMetrics.get(e);return t&&0!==t.eventsProcessed?t.totalLatency/t.eventsProcessed:0}getPoolCacheHitRate(e){const t=this.perPoolMetrics.get(e);if(!t)return 0;const n=t.cacheHits+t.cacheMisses;return 0===n?0:t.cacheHits/n*100}getHealthMetrics(e,t,n,o,i){const r=this.getLatencyPercentiles(),a=this.getMemoryUsedMB();return{eventProcessing:{queueDepth:this.queueDepth,eventsProcessed:this.eventsProcessed,eventsDropped:this.eventsDropped,throughputPerSecond:this.getThroughputPerSecond()},metrics:{latencyP50:r.p50,latencyP95:r.p95,latencyP99:r.p99,cacheHitRate:this.getCacheHitRate()},memory:{usedMB:Math.round(10*a)/10,maxMB:i,percentUsed:Math.round(a/i*1e3)/10},pools:{totalMonitored:e,hotCacheSize:t,warmCacheSize:n,coldCacheSize:o}}}reset(){this.eventLatencies=[],this.eventsProcessed=0,this.eventsDropped=0,this.queueDepth=0,this.maxQueueDepth=0,this.startTime=Date.now(),this.lastEventTime=void 0,this.perPoolMetrics.clear(),this.memorySnapshots=[]}getSummary(){const e=this.eventLatencies.length>0?this.eventLatencies.reduce((e,t)=>e+t,0)/this.eventLatencies.length:0;return{eventsProcessed:this.eventsProcessed,eventsDropped:this.eventsDropped,cacheHitRate:this.getCacheHitRate(),averageLatency:Math.round(e),memoryUsedMB:Math.round(10*this.getMemoryUsedMB())/10,throughputPerSecond:Math.round(100*this.getThroughputPerSecond())/100}}getPoolMetrics(e){let t=this.perPoolMetrics.get(e);return t||(t={eventsProcessed:0,totalLatency:0,cacheHits:0,cacheMisses:0},this.perPoolMetrics.set(e,t)),t}}class si{static createPoolKey(e,t,n){return`${e}/${t}/${n}`}static parsePoolKey(e){if(!e||"string"!=typeof e)return null;const t=e.split("/");if(3!==t.length)return null;const n=t[0]?.trim(),o=t[1]?.trim(),i=t[2]?.trim();if(!n||!o||!i)return null;const r=Number.parseInt(i,10);return Number.isNaN(r)?null:{token0:n,token1:o,feeTier:r}}static isValidPoolKey(e){if("string"!=typeof e)return!1;return null!==this.parsePoolKey(e)}static getToken0(e){const t=this.parsePoolKey(e);return t?.token0??null}static getToken1(e){const t=this.parsePoolKey(e);return t?.token1??null}static getFeeTier(e){const t=this.parsePoolKey(e);return t?.feeTier??null}static containsToken(e,t){const n=this.parsePoolKey(e);return!!n&&(n.token0===t||n.token1===t)}static containsTokenPair(e,t,n){const o=this.parsePoolKey(e);if(!o)return!1;const i=o.token0===t||o.token1===t,r=o.token0===n||o.token1===n;return i&&r&&t!==n}static normalizeFee(e){if(null==e)return null;const t="number"==typeof e?e:Number.parseFloat(String(e).replace("%","").trim());return Number.isNaN(t)?null:1===t||1e4===t?1e4:.3===t||3e3===t?3e3:.05===t||500===t?500:Number.isInteger(t)&&t>0?t:null}static formatFeeAsPercentage(e){return`${(e/1e4).toFixed(2)}%`}static isValidTokenPair(e,t){return Boolean(e)&&Boolean(t)&&e!==t}}class ci{constructor(e){this.logger=e||new S({debug:!1,context:"SwapEventExtractor"})}walkPayloadForSwaps(e,t){const n=[],o=new WeakSet,i=(e,r=0)=>{if(r>50)this.logger.debug("Payload nesting exceeded maximum depth of 50");else if(e&&"string"!=typeof e&&"object"==typeof e){if(o.has(e))return;o.add(e);const a=this.extractSwapFromObject(e);a&&!t.has(a.transactionId)&&(n.push(a),t.add(a.transactionId));for(const t of Object.values(e))i(t,r+1)}};return i(e,0),n}extractSwapFromObject(e){const t=this.extractTransactionId(e);if(!t)return null;const n=e.Data,o=n&&"object"==typeof n&&!Array.isArray(n)?n:e,i=this.extractToken(o,"token0","fromToken","source"),r=this.extractToken(o,"token1","toToken","destination");if(!i||!r)return null;const a=this.extractAmount(o,"amount0","amountIn","inputAmount"),s=this.extractAmount(o,"amount1","amountOut","outputAmount");if(!a||!s)return null;const c=this.extractFeeTier(o);if(null===c)return null;const l=this.extractTimestamp(o),u=this.buildPoolKey(i,r,c),d=this.determineDirection(o,i,r),h={transactionId:t,poolKey:u,token0:i,token1:r,amount0:a,amount1:s,feeTier:c,direction:d,timestamp:l,exactInput:this.determineExactInput(o,d)},g=this.extractUser(o);return void 0!==g&&(h.user=g),h}extractTransactionId(e){const t=["transactionId","txId","tx_id","hash","txHash","id"];for(const n of t){const t=e[n];if("string"==typeof t&&t.trim())return t}return null}extractToken(e,...t){for(const n of t){const t=e[n];if("string"==typeof t&&t.trim())return t}return null}extractAmount(e,...t){for(const n of t){const t=e[n];if(null!=t){const e=String(t).trim();if(/^-?\d+(\.\d+)?([eE]-?\d+)?$/.test(e))return e}}return null}extractFeeTier(e){const t=["poolFee","feeTier","fee","feeTierBps","liquidityFeeBps","feeAmount"];for(const n of t){const t=e[n],o=this.normalizeFee(t);if(null!==o)return o}return null}normalizeFee(e){if(null==e)return null;const t="number"==typeof e?e:Number.parseFloat(String(e).replace("%","").trim());return Number.isNaN(t)?null:1===t||1e4===t?1e4:.3===t||3e3===t?3e3:.05===t||500===t?500:Number.isInteger(t)?t:null}extractTimestamp(e){const t=["timeStamp","timestamp","time","createdAt","date"];for(const n of t){const t=e[n];if("number"==typeof t)return t;if("string"==typeof t){const e=new Date(t).getTime();if(!Number.isNaN(e))return e}}return Date.now()}extractUser(e){const t=["userAddress","user","from","sender","wallet","address"];for(const n of t){const t=e[n];if("string"==typeof t&&t.trim())return t}}determineDirection(e,t,n){const o=e.zeroForOne||e.direction;if("boolean"==typeof o)return o?"zeroForOne":"oneForZero";if("string"==typeof o){if("zerotoone"===o.toLowerCase()||"0to1"===o)return"zeroForOne";if("onetozero"===o.toLowerCase()||"1to0"===o)return"oneForZero"}if(e.fromToken===t||e.inputToken===t)return"zeroForOne";if(e.fromToken===n||e.inputToken===n)return"oneForZero";const i=this.extractAmount(e,"amount0","amountIn");return i&&Number(i),"zeroForOne"}determineExactInput(e,t){if("boolean"==typeof e.exactInput)return e.exactInput;if("boolean"==typeof e.exactOutput)return!e.exactOutput;const n=void 0!==e.amountIn&&null!==e.amountIn,o=void 0!==e.amountOut&&null!==e.amountOut,i=void 0!==e.inputAmount&&null!==e.inputAmount,r=void 0!==e.outputAmount&&null!==e.outputAmount;return!(!n||o)||!(o&&!n)&&(!(!i||r)||!(r&&!i))}buildPoolKey(e,t,n){return`${e}/${t}/${n}`}}class li{static getCached(e){const t=e.toString();return this.CACHE.has(t)||this.CACHE.set(t,new r(e)),this.CACHE.get(t)}static clearCache(){this.CACHE.clear()}static getCacheStats(){return{size:this.CACHE.size,entries:Array.from(this.CACHE.keys())}}static trimCache(e=1e3){if(this.CACHE.size>e){const t=this.CACHE.size-e,n=Array.from(this.CACHE.keys());for(let e=0;e<t;e++)this.CACHE.delete(n[e])}}}li.CACHE=new Map,li.ZERO=new r(0),li.ONE=new r(1),li.FEE_PIPS=new r(1e6),li.MIN_SQRT_RATIO=new r("4295128739"),li.MAX_SQRT_RATIO=new r("1461446703485210103287273052203988822378723970342");const ui={maxIterations:100,enableBigNumberCache:!0,roundingMode:r.ROUND_DOWN,debugLogging:!1};class di{static calculateSwapDelta(e,t,n={}){const o=Date.now(),i={...ui,...n};try{const n=this.initializeSwapState(e,t,i);i.debugLogging&&this.logger.debug("Initialized swap state",{sqrtPrice:n.sqrtPrice.toString(),liquidity:n.liquidity.toString(),tick:n.tick,zeroForOne:t.zeroForOne});const a=this.computeSwapLoop(n,e,t,i);i.debugLogging&&this.logger.debug("Swap loop completed",{stepCount:a.stepCount,ticksCrossed:a.ticksCrossed.length,priceHitLimit:a.priceHitLimit});const s=this.createUpdatedPool(e.pool,a.state,t,i),c=this.calculateFinalAmounts(n,a.state,t);let l;if(t.actualSqrtPrice){const e=new r(s.sqrtPrice),n=new r(t.actualSqrtPrice);l=e.minus(n).abs().div(n).times(100).toNumber()}const u=Date.now()-o;i.debugLogging&&this.logger.debug("Swap delta calculated",{calculationTimeMs:u,amount0:c.amount0.toString(),amount1:c.amount1.toString(),driftPercentage:l}),u>100&&this.logger.warn("Swap calculation exceeded 100ms",{calculationTimeMs:u,stepCount:a.stepCount,ticksCrossed:a.ticksCrossed.length}),a.priceHitLimit&&this.logger.warn("Swap price hit limit - partially fulfilled",{zeroForOne:t.zeroForOne,stepCount:a.stepCount}),a.stepCount>50&&this.logger.warn("Unusually complex swap detected",{stepCount:a.stepCount,ticksCrossed:a.ticksCrossed.length});return{updatedPool:s,updatedTicks:a.updatedTicks,amount0:c.amount0,amount1:c.amount1,feeAmount0:c.feeAmount0,feeAmount1:c.feeAmount1,ticksCrossed:a.ticksCrossed,metadata:{calculationTimeMs:u,swapSteps:a.stepCount,priceHitLimit:a.priceHitLimit,...void 0!==l&&{driftPercentage:l}}}}catch(e){this.logger.error("Swap delta calculation failed",e);const t=e instanceof Error?e.message:String(e);throw new Error(`Swap delta calculation failed: ${t}`)}}static initializeSwapState(e,t,n){const{pool:o}=e;if(!o.sqrtPrice||!o.liquidity)throw new Error("Invalid pool data: missing sqrtPrice or liquidity");const i=n.enableBigNumberCache?li.getCached.bind(li):e=>new r(e),s="string"==typeof o.sqrtPrice?o.sqrtPrice:new r(o.sqrtPrice).toFixed(0),c="string"==typeof o.liquidity?o.liquidity:new r(o.liquidity).toFixed(0),l=i(s),u=i(c),d=o.tick??0,h=a.sqrtPriceToTick(new r(s)),g=Math.abs(h-d);g>100&&this.logger.warn("Significant tick/price mismatch detected in pool state",{poolTick:d,calculatedTick:h,drift:g,threshold:100});const p=i(t.amountSpecified);no(p,"amountSpecified","for swap operation");const m="string"==typeof o.feeGrowthGlobal1?o.feeGrowthGlobal1:new r(o.feeGrowthGlobal1).toFixed(0),f="string"==typeof o.feeGrowthGlobal0?o.feeGrowthGlobal0:new r(o.feeGrowthGlobal0).toFixed(0),y=t.zeroForOne?i(m):i(f);return{sqrtPrice:l,liquidity:u,tick:d,amountSpecifiedRemaining:p,amountCalculated:li.ZERO,feeGrowthGlobalX:y,protocolFee:li.ZERO}}static computeSwapLoop(e,t,n,o){const{pool:i,tickDataMap:s}=t,c=[],l={};let u=0;const d=n.zeroForOne?li.MIN_SQRT_RATIO:li.MAX_SQRT_RATIO,h=new r("0.000001");for(;e.amountSpecifiedRemaining.gt(h)&&!e.sqrtPrice.eq(d)&&u<o.maxIterations;){u++;const[t,h]=this.findNextInitializedTick(s,e.tick,i.tickSpacing,n.zeroForOne);let g;if(o.debugLogging&&this.logger.debug(`Swap step ${u}`,{currentTick:e.tick,tickNext:t,initialized:h,sqrtPrice:e.sqrtPrice.toString(),liquidity:e.liquidity.toString(),amountRemaining:e.amountSpecifiedRemaining.toString()}),h&&t>=-887272&&t<=887272){const e=a.tickToSqrtPrice(t);g=e instanceof r?e:new r(String(e))}else g=d;const p=n.zeroForOne?r.max(g,d):r.min(g,d),m=this.executeSwapStep(e.sqrtPrice,p,e.liquidity,e.amountSpecifiedRemaining,i.fee,n.exactInput);if(e.sqrtPrice=m.sqrtPriceNext,n.exactInput){const t=m.amountIn.plus(m.feeAmount);t.lte(0)?e.amountSpecifiedRemaining=li.ZERO:(e.amountSpecifiedRemaining=e.amountSpecifiedRemaining.minus(t),e.amountSpecifiedRemaining.lt(0)&&(e.amountSpecifiedRemaining=li.ZERO)),e.amountCalculated=e.amountCalculated.minus(m.amountOut)}else{m.amountOut.lte(0)?e.amountSpecifiedRemaining=li.ZERO:e.amountSpecifiedRemaining=e.amountSpecifiedRemaining.plus(m.amountOut),e.amountCalculated=e.amountCalculated.plus(m.amountIn.plus(m.feeAmount))}if(e.liquidity.gt(0)){const t=m.feeAmount.div(e.liquidity);e.feeGrowthGlobalX=e.feeGrowthGlobalX.plus(t)}if(e.sqrtPrice.eq(g)&&h){const i=s[t.toString()];if(!i)throw new Error(`Missing tick data for initialized tick ${t}`);const a=n.zeroForOne?new r(i.liquidityNet).negated():new r(i.liquidityNet);if(e.liquidity=e.liquidity.plus(a),e.liquidity.lt(0))throw new Error(`Negative liquidity after crossing tick ${t}: ${e.liquidity.toString()}`);c.push(t),l[t.toString()]=i,o.debugLogging&&this.logger.debug(`Crossed tick ${t}`,{liquidityNet:a.toString(),newLiquidity:e.liquidity.toString()})}if(e.sqrtPrice.eq(g))e.tick=n.zeroForOne?t-1:t;else{const t=a.sqrtPriceToTick(new r(e.sqrtPrice.toString()));e.tick=t}}if(u>=o.maxIterations)throw new Error(`Swap calculation exceeded maximum iterations (${o.maxIterations}). Possible infinite loop or very complex swap.`);const g=e.sqrtPrice.eq(d);return{state:e,ticksCrossed:c,priceHitLimit:g,stepCount:u,updatedTicks:l}}static createUpdatedPool(e,t,n,o){const i=Object.assign(Object.create(Object.getPrototypeOf(e)),e);if(i.sqrtPrice=t.sqrtPrice.toFixed(0),i.liquidity=t.liquidity.toFixed(0),i.tick=t.tick,n.zeroForOne?i.feeGrowthGlobal1=t.feeGrowthGlobalX.toFixed(0):i.feeGrowthGlobal0=t.feeGrowthGlobalX.toFixed(0),n.zeroForOne){const n=new r(e.protocolFeesToken0);i.protocolFeesToken0=n.plus(t.protocolFee).toFixed(0)}else{const n=new r(e.protocolFeesToken1);i.protocolFeesToken1=n.plus(t.protocolFee).toFixed(0)}return i}static calculateFinalAmounts(e,t,n){let o,i,a,s;if(n.exactInput){const e=new r(n.amountSpecified),c=t.amountCalculated.abs();n.zeroForOne?(o=e.negated(),i=c,a=li.ZERO,s=li.ZERO):(o=c,i=e.negated(),a=li.ZERO,s=li.ZERO)}else{const e=new r(n.amountSpecified),c=t.amountCalculated.abs();n.zeroForOne?(o=c.negated(),i=e,a=li.ZERO,s=li.ZERO):(o=e,i=c.negated(),a=li.ZERO,s=li.ZERO)}const c=t.feeGrowthGlobalX.minus(e.feeGrowthGlobalX).times(e.liquidity);return n.zeroForOne?s=c:a=c,{amount0:o,amount1:i,feeAmount0:a,feeAmount1:s}}static findNextInitializedTick(e,t,n,o){const i=Object.keys(e).map(e=>parseInt(e,10)).sort((e,t)=>e-t);if(0===i.length){return[o?-887272:887272,!1]}if(o){const e=i.reverse().find(e=>e<t);return void 0!==e?[e,!0]:[-887272,!1]}{const e=i.find(e=>e>t);return void 0!==e?[e,!0]:[887272,!1]}}static executeSwapStep(e,t,n,o,i,s){to(n),no(o,"amountRemaining","for swap step");const c=[500,3e3,1e4];if(!c.includes(i))throw new Error(`Invalid fee tier: ${i}. Must be one of: ${c.join(", ")}`);const l=a.computeSwapStep(e,t,n,o,i,t.lt(e)),u=l[0],d=l[1],h=l[2],g=l[3],p=r.isBigNumber(u)?u:new r(String(u)),m=r.isBigNumber(d)?d:new r(String(d)),f=r.isBigNumber(h)?h:new r(String(h)),y=r.isBigNumber(g)?g:new r(String(g));return{sqrtPriceStart:e,tickNext:a.sqrtPriceToTick(p),sqrtPriceNext:p,initialised:!1,amountIn:m,amountOut:f,feeAmount:y}}}di.logger=new S({debug:!1,context:"SwapDeltaCalculator"});class hi{constructor(e,t,n,o){this.cache=new Map,this.tierSizes={hot:50,warm:200,cold:0},this.tierTTLs={hot:1/0,warm:18e5,cold:3e5},this.refetchThresholds={swapCount:50,driftPercent:.05},this.fetchPoolFn=e,this.config=t,this.metrics=n,this.logger=o||new S({debug:!1,context:"PoolCacheManager"}),this.tierSizes.cold=Math.max(0,this.config.maxPools-this.tierSizes.hot-this.tierSizes.warm),this.logger.debug(`Initialized with cache limits: hot=${this.tierSizes.hot}, warm=${this.tierSizes.warm}, cold=${this.tierSizes.cold}, max=${this.config.maxPools}`)}async getPool(e){const t=this.cache.get(e);if(t){if(!(Date.now()>t.expiresAt))return t.lastAccessTime=Date.now(),this.checkRefetchNeeded(e,t),this.metrics.recordPoolCacheHit(e,0),t.poolData;this.cache.delete(e),this.logger.debug(`Cache expired for pool ${e}`)}this.metrics.recordPoolCacheMiss(e,0);try{const t=await this.fetchPoolFn(e),n=this.determineTier(),o={poolData:t,tier:n,lastAccessTime:Date.now(),expiresAt:Date.now()+this.tierTTLs[n],swapsSinceRefetch:0,cumulativeDrift:0,lastDeltaAppliedTime:Date.now()};return this.cache.set(e,o),this.cache.size>this.config.maxPools&&this.evictLRU(),this.logger.debug(`Fetched pool ${e} (tier: ${n})`),t}catch(t){throw this.logger.error(`Failed to fetch pool ${e}:`,t),t}}updatePoolWithSwapDelta(e,t,n,o,i){const r=this.cache.get(e);if(!r)return this.logger.debug(`Pool ${e} not in cache for delta update`),!1;try{if(i){const a="zeroForOne"===t,s=a?n:o,c={transactionId:i.transactionId,timestamp:i.timestamp,amountSpecified:s,zeroForOne:a,exactInput:i.exactInput},l=di.calculateSwapDelta(r.poolData,c);r.poolData={...r.poolData,pool:l.updatedPool},r.swapsSinceRefetch++,r.lastDeltaAppliedTime=Date.now(),void 0!==l.metadata.driftPercentage?(r.cumulativeDrift+=l.metadata.driftPercentage,this.logger.debug(`Delta applied for ${e}: drift=${l.metadata.driftPercentage.toFixed(4)}%, cumulative=${r.cumulativeDrift.toFixed(2)}%`)):this.logger.debug(`Delta applied for ${e}: ${l.ticksCrossed.length} ticks crossed`)}else r.swapsSinceRefetch++,r.lastDeltaAppliedTime=Date.now();return this.shouldRefetch(r)&&(this.logger.debug(`Refetch needed for ${e}: swaps=${r.swapsSinceRefetch}, drift=${(100*r.cumulativeDrift).toFixed(2)}%`),r.expiresAt=Date.now()),!0}catch(t){return this.logger.error(`Failed to update pool ${e}:`,t),r.expiresAt=Date.now(),!1}}getStats(){const e={totalCached:this.cache.size,hotCacheSize:0,warmCacheSize:0,coldCacheSize:0,memoryUsedMB:this.metrics.getMemoryUsedMB()};for(const t of this.cache.values())"hot"===t.tier?e.hotCacheSize++:"warm"===t.tier?e.warmCacheSize++:e.coldCacheSize++;return e}getPoolInfo(e){const t=this.cache.get(e);return t?{poolKey:e,tier:t.tier,lastAccessTime:new Date(t.lastAccessTime),expiresAt:new Date(t.expiresAt),swapsSinceRefetch:t.swapsSinceRefetch,cumulativeDrift:t.cumulativeDrift,isExpired:Date.now()>t.expiresAt}:null}async warmCache(e){if(this.cache.has(e))return!0;try{return await this.getPool(e),this.logger.debug(`Cache warmed for ${e}`),!0}catch(t){return this.logger.error(`Failed to warm cache for ${e}:`,t),!1}}async warmCacheBatch(e,t=5){const n={succeeded:0,failed:0,total:e.length};let o=0;const i=new Set;for(;o<e.length||i.size>0;){for(;o<e.length&&i.size<t;){const t=e[o];o++;const r=this.warmCache(t).then(e=>{e?n.succeeded++:n.failed++});i.add(r),r.finally(()=>i.delete(r))}i.size>0&&await Promise.race(i)}return this.logger.debug(`Cache warming complete: ${n.succeeded}/${n.total} succeeded`),n}clear(){this.cache.clear(),this.logger.debug("Cache cleared")}clearExpired(){const e=Date.now();let t=0;for(const[n,o]of this.cache)e>o.expiresAt&&(this.cache.delete(n),t++);t>0&&this.logger.debug(`Cleared ${t} expired entries`)}determineTier(){const e=Array.from(this.cache.values()).filter(e=>"hot"===e.tier).length,t=Array.from(this.cache.values()).filter(e=>"warm"===e.tier).length;return e<this.tierSizes.hot?"hot":t<this.tierSizes.warm?"warm":"cold"}checkRefetchNeeded(e,t){this.shouldRefetch(t)&&(this.logger.debug(`Scheduling refetch for ${e}: swaps=${t.swapsSinceRefetch}, drift=${(100*t.cumulativeDrift).toFixed(2)}%`),t.expiresAt=Date.now())}shouldRefetch(e){return e.swapsSinceRefetch>=this.refetchThresholds.swapCount||e.cumulativeDrift>=this.refetchThresholds.driftPercent}evictLRU(){const e=Array.from(this.cache.entries()).filter(([e,t])=>"cold"===t.tier).sort((e,t)=>e[1].lastAccessTime-t[1].lastAccessTime);if(0===e.length){this.logger.warn("No cold cache entries to evict, trying warm cache");const e=Array.from(this.cache.entries()).filter(([e,t])=>"warm"===t.tier).sort((e,t)=>e[1].lastAccessTime-t[1].lastAccessTime);if(e.length>0){const[t]=e[0];this.cache.delete(t),this.logger.debug(`Evicted warm cache entry: ${t}`)}return}const[t]=e[0];this.cache.delete(t),this.logger.debug(`Evicted cold cache entry: ${t}`)}async refreshWarmAndHotTiers(){const e=Array.from(this.cache.entries()).filter(([e,t])=>"hot"===t.tier||"warm"===t.tier).sort((e,t)=>t[1].lastAccessTime-e[1].lastAccessTime).slice(0,10);if(0===e.length)return;const t=e.map(([e,t])=>this.fetchPoolFn(e).then(n=>{t&&(t.poolData=n,t.lastAccessTime=Date.now(),t.swapsSinceRefetch=0,t.cumulativeDrift=0),this.logger.debug(`Refreshed ${e} during background warming`)}).catch(t=>{this.logger.debug(`Failed to refresh ${e} during background warming:`,t)})),n=new Promise(e=>setTimeout(()=>e(),5e3));await Promise.race([Promise.all(t),n])}}class gi{constructor(e,t,n){this.queue=[],this.isShuttingDown=!1,this.currentConcurrency=0,this.maxConcurrencyReached=0,this.eventsDropped=0,this.totalBatchesProcessed=0,this.totalBatchSize=0,this.eventsProcessedCount=0,this.processor=null,this.processingScheduled=!1,this.scheduleProcessing=e=>{"undefined"!=typeof setImmediate?setImmediate(e):Promise.resolve().then(e)},this.config=e,this.metrics=t,this.logger=n||new S({debug:!1,context:"SwapEventQueue"}),this.logger.debug(`Initialized with maxQueueSize=${this.config.maxQueueSize}, batchSize=${this.config.batchSize}, maxConcurrent=${this.config.maxConcurrent}`)}setProcessor(e){this.processor=e}enqueue(e){return this.isShuttingDown?(this.logger.debug(`Rejecting event (queue shutting down): ${e.transactionId}`),this.metrics.recordEventDropped(),this.eventsDropped++,!1):this.queue.length>=this.config.maxQueueSize?(this.logger.warn(`Queue full (${this.queue.length}/${this.config.maxQueueSize}), dropping event: ${e.transactionId}`),this.metrics.recordEventDropped(),this.eventsDropped++,!1):(this.queue.push(e),this.metrics.updateQueueDepth(this.queue.length),this.processingScheduled||this.isShuttingDown||(this.processingScheduled=!0,this.scheduleProcessing(()=>this.processNextBatch())),!0)}getQueueSize(){return this.queue.length}getStats(){return{queueSize:this.queue.length,eventsProcessed:this.eventsProcessedCount,eventsDropped:this.eventsDropped,currentConcurrent:this.currentConcurrency,maxConcurrentReached:this.maxConcurrencyReached,averageBatchSize:this.totalBatchesProcessed>0?Math.floor(this.totalBatchSize/this.totalBatchesProcessed):0,totalBatchesProcessed:this.totalBatchesProcessed}}async waitForEmpty(e){return new Promise(t=>{const n=()=>{0!==this.queue.length||0!==this.currentConcurrency?setTimeout(n,10):t()};e&&setTimeout(()=>t(),e),n()})}async shutdown(e=3e4){this.isShuttingDown=!0,this.logger.debug("Shutting down queue...");const t=Date.now();for(;this.queue.length>0||this.currentConcurrency>0;){if(Date.now()-t>e){this.logger.warn(`Queue shutdown timeout: ${this.queue.length} events remaining, ${this.currentConcurrency} processing`);break}await new Promise(e=>setTimeout(e,50))}this.logger.debug("Queue shutdown complete")}clear(){const e=this.queue.length;this.queue.length=0,this.eventsDropped+=e,this.metrics.updateQueueDepth(0),this.logger.warn(`Cleared ${e} events from queue`)}async processNextBatch(){if(this.processingScheduled=!1,this.isShuttingDown&&0===this.queue.length)return;if(this.currentConcurrency>=this.config.maxConcurrent)return void setTimeout(()=>{this.processingScheduled||(this.processingScheduled=!0,setImmediate(()=>this.processNextBatch()))},10);const e=Math.min(this.config.batchSize,this.queue.length,this.config.maxConcurrent-this.currentConcurrency);if(0===e)return;const t=this.queue.splice(0,e);this.metrics.updateQueueDepth(this.queue.length),this.currentConcurrency+=t.length,this.currentConcurrency>this.maxConcurrencyReached&&(this.maxConcurrencyReached=this.currentConcurrency),this.totalBatchSize+=t.length,this.totalBatchesProcessed++;try{const e=await Promise.allSettled(t.map(e=>this.processEvent(e)));for(let n=0;n<e.length;n++){const o=e[n];this.eventsProcessedCount++,"rejected"===o.status&&this.logger.error(`Failed to process event ${t[n].transactionId}:`,o.reason)}}finally{this.currentConcurrency-=t.length}this.queue.length>0&&!this.processingScheduled&&(this.processingScheduled=!0,this.scheduleProcessing(()=>this.processNextBatch()))}async processEvent(e){if(!this.processor)return void this.logger.warn("No processor set, discarding event:",e.transactionId);const t=Date.now();try{await this.processor(e);const n=Date.now()-t;this.metrics.recordEventLatency(n)}catch(t){throw this.logger.error(`Event processing failed for ${e.transactionId}:`,t),t}}}class pi{constructor(e,t,n,o={},i){this.socket=null,this.maxSeenTransactions=1e4,this.listeners=[],this.onErrorCallbacks=[],this.isActive=!1,this.listenerRegistered=!1,this.handleSwapEvent=null,this.warmingIntervalHandle=null,this.reconnectAttempts=0,this.maxReconnectAttempts=3,this.reconnectDelayMs=1e3,this.logger=i||new S({debug:!1,context:"MultiPoolStateManager"}),e instanceof Promise?this.socketReady=e.then(e=>(this.socket=e,this.setupConnectionMonitoring(),e)).catch(e=>{throw this.logger.error("Failed to resolve socket promise:",e),e}):(this.socket=e,this.socketReady=Promise.resolve(e),this.setupConnectionMonitoring()),this.metrics=new ai,this.config=this.applyDefaults(o),this.eventExtractor=new ci(this.logger),this.quoteService=n,this.cacheManager=new hi(t,this.config,this.metrics,this.logger),this.eventQueue=new gi(this.config,this.metrics,this.logger),this.seenTransactions=new mi(this.maxSeenTransactions),this.eventQueue.setProcessor(e=>this.processSwapEvent(e)),this.logger.debug("Initialized MultiPoolStateManager")}subscribe(e,t){this.listeners.push(t),e.onError&&this.onErrorCallbacks.push(e.onError),this.isActive||(this.setupWebSocketListener(e),this.isActive=!0);const n=this;return()=>{n.listeners=n.listeners.filter(e=>e!==t),e.onError&&(n.onErrorCallbacks=n.onErrorCallbacks.filter(t=>t!==e.onError)),0===n.listeners.length&&0===n.onErrorCallbacks.length&&n.unsubscribe()}}getHealth(){const e=this.cacheManager.getStats(),t=this.eventQueue.getStats(),n=this.metrics.getHealthMetrics(e.totalCached,e.hotCacheSize,e.warmCacheSize,e.coldCacheSize,this.getMaxMemoryMB()),o=this.determineHealthStatus(t,e),i={connected:this.socket?.connected??!1,reconnectAttempts:this.reconnectAttempts};this.socket?.connected&&(i.lastConnectionTime=new Date);const r=t.queueSize/this.config.maxQueueSize*100,a=e.totalCached/this.config.maxPools*100,s=e.memoryUsedMB/this.getMaxMemoryMB()*100,c=this.generateHealthRecommendations(o,r,a,s,n.metrics.cacheHitRate);return{...n,status:o,websocket:i,recommendations:c,detailedMetrics:{eventQueueUtilization:r,cacheUtilization:a,memoryUtilization:s}}}generateHealthRecommendations(e,t,n,o,i){const r=[];return"failed"===e&&(r.push("🔴 System is in FAILED state - immediate action required"),this.socket?.connected||r.push("Reconnect WebSocket - connection lost"),t>90&&r.push("Reduce incoming event rate or increase maxQueueSize")),"degraded"===e&&(r.push("⚠️ System is DEGRADED - performance may be impacted"),t>75&&r.push(`Queue utilization ${t.toFixed(1)}% - consider increasing maxQueueSize`),o>80&&r.push(`Memory usage ${o.toFixed(1)}% - consider reducing cache size or memory profile`)),"healthy"===e&&(i<50&&r.push(`Cache hit rate ${i.toFixed(1)}% is low - consider warming more pools`),o>50&&r.push("Memory usage is moderate - monitor for growth trends"),t>50&&r.push("Queue utilization is elevated - monitor for bottlenecks")),r}getSummary(){return{...this.metrics.getSummary(),queueStats:this.eventQueue.getStats(),cacheStats:this.cacheManager.getStats()}}startBackgroundWarming(){if(this.warmingIntervalHandle)return;const e=this.config.refreshIntervalMs;this.warmingIntervalHandle=setInterval(()=>{this.performBackgroundWarming().catch(e=>{this.logger.error("Background warming error:",e)})},e),this.logger.debug(`Background warming started (interval: ${e}ms)`)}stopBackgroundWarming(){this.warmingIntervalHandle&&(clearInterval(this.warmingIntervalHandle),this.warmingIntervalHandle=null,this.logger.debug("Background warming stopped"))}async performBackgroundWarming(){const e=this.cacheManager.getStats();if(0!==e.totalCached)try{await this.cacheManager.refreshWarmAndHotTiers(),this.logger.debug(`Background warming completed: ${e.totalCached} pools in cache (hot: ${e.hotCacheSize}, warm: ${e.warmCacheSize})`)}catch(e){this.logger.debug("Background warming encountered an error:",e)}}async shutdown(){this.stopBackgroundWarming(),await this.unsubscribe(),await this.eventQueue.shutdown(),this.cacheManager.clear(),this.metrics.reset()}setupConnectionMonitoring(){this.socket&&(this.socket.on("disconnect",()=>{this.logger.warn("WebSocket disconnected"),this.notifyError(new Error("WebSocket disconnected")),this.config.autoRecover&&this.attemptReconnection().catch(e=>{this.logger.error("Reconnection failed:",e)})}),this.socket.on("connect_error",e=>{this.logger.error("WebSocket connection error:",e),this.notifyError(e instanceof Error?e:new Error(String(e)))}))}async attemptReconnection(){if(this.logger.debug(`Reconnection attempt ${this.reconnectAttempts+1}/${this.maxReconnectAttempts}`),this.reconnectAttempts>=this.maxReconnectAttempts)return this.logger.error(`Max reconnection attempts (${this.maxReconnectAttempts}) exceeded - performing full reset`),void await this.performFullReset();this.reconnectAttempts<2&&(0===this.reconnectAttempts?this.logger.debug("Tier 1: Quick reconnect"):(this.logger.debug(`Tier 2: Exponential backoff (${this.reconnectDelayMs}ms)`),await new Promise(e=>setTimeout(e,this.reconnectDelayMs)),this.reconnectDelayMs=Math.min(2*this.reconnectDelayMs,3e4)));try{this.socket?.disconnect&&(this.socket.disconnect(),this.logger.debug("Disconnected for reconnection")),this.socket?.connect?.(),this.logger.debug("Reconnection initiated"),this.reconnectAttempts++}catch(e){throw this.logger.error("Failed to initiate reconnection:",e),e}}async performFullReset(){this.logger.warn("Performing full system reset due to connection failures"),this.stopBackgroundWarming();const e=this.getHealth();this.logger.debug("System state before reset:",{status:e.status,queueSize:e.eventProcessing.eventsProcessed,cachedPools:e.pools.totalMonitored,memory:`${e.memory.usedMB}MB / ${e.memory.maxMB}MB`,cacheHitRate:`${e.metrics.cacheHitRate.toFixed(2)}%`}),this.cacheManager.clear(),this.metrics.reset(),this.reconnectAttempts=0,this.reconnectDelayMs=1e3,this.notifyError(new Error("System reset: connection lost and recovery failed - please restart monitoring")),this.logger.info("System reset complete - ready for restart")}setupWebSocketListener(e){if(this.listenerRegistered)return void this.logger.debug("WebSocket listener already registered");const t=this;this.handleSwapEvent=(n,...o)=>{try{const n=Date.now(),i=o[0],r=t.eventExtractor.walkPayloadForSwaps(i,t.seenTransactions);if(0===r.length)return;t.logger.debug(`Extracted ${r.length} swaps from payload`);for(const n of r){if(t.filterSwap(n,e)){t.eventQueue.enqueue(n)||t.logger.debug(`Swap dropped due to queue overflow: ${n.transactionId}`)}}const a=Date.now()-n;t.metrics.recordEventLatency(a)}catch(e){t.logger.error("Error processing WebSocket payload:",e),t.notifyError(e instanceof Error?e:new Error(String(e)))}},this.socket?(this.socket.onAny(this.handleSwapEvent),this.listenerRegistered=!0,this.setupConnectionMonitoring()):this.logger.warn("Socket not available for listener registration"),this.startBackgroundWarming(),this.logger.debug("WebSocket listener registered for all events")}async unsubscribe(){this.stopBackgroundWarming(),this.handleSwapEvent&&this.listenerRegistered&&this.socket&&(this.socket.offAny(this.handleSwapEvent),this.listenerRegistered=!1),this.isActive=!1,this.listeners=[],this.onErrorCallbacks=[],this.logger.debug("Unsubscribed from swap events")}filterSwap(e,t){if(t.tokenFilter){if(!si.containsToken(e.poolKey,t.tokenFilter))return!1}if(t.pairTokens){const[n,o]=t.pairTokens;if(!si.containsTokenPair(e.poolKey,n,o))return!1}if(t.feeTierFilter){if(si.normalizeFee(t.feeTierFilter)!==e.feeTier)return!1}return!t.userFilter||e.user===t.userFilter}async processSwapEvent(e){const t=Date.now();try{const n=this.cacheManager.updatePoolWithSwapDelta(e.poolKey,e.direction,e.amount0,e.amount1,e);e.poolStateUpdated=n;const o=Date.now()-t;this.metrics.recordEventLatency(o);for(const t of this.listeners)try{const n=t(e);n instanceof Promise&&await n}catch(t){this.logger.error(`Listener error for swap ${e.transactionId}:`,t)}}catch(t){this.logger.error(`Failed to process swap ${e.transactionId}:`,t),this.notifyError(t instanceof Error?t:new Error(String(t)))}}notifyError(e){for(const t of this.onErrorCallbacks)try{t(e)}catch(e){this.logger.error("Error in error callback:",e)}}determineHealthStatus(e,t){return!this.socket?.connected||e.queueSize>.9*this.config.maxQueueSize?"failed":e.queueSize>.75*this.config.maxQueueSize||t.memoryUsedMB>.9*this.getMaxMemoryMB()?"degraded":"healthy"}getMaxMemoryMB(){switch(this.config.memoryProfile){case"conservative":return 55;case"aggressive":return 530;default:return 250}}applyDefaults(e){return{memoryProfile:e.memoryProfile??"moderate",maxPools:e.maxPools??500,softLimit:e.softLimit??200,preloadTopN:e.preloadTopN??200,warmingTimeoutMs:e.warmingTimeoutMs??3e4,refreshIntervalMs:e.refreshIntervalMs??3e5,maxQueueSize:e.maxQueueSize??1e4,batchSize:e.batchSize??100,maxConcurrent:e.maxConcurrent??10,autoRecover:e.autoRecover??!0,maxParallelRefetch:e.maxParallelRefetch??20,enableDeltaOptimization:e.enableDeltaOptimization??!0,enableOfflineQuotes:e.enableOfflineQuotes??!0,metricsEnabled:e.metricsEnabled??!0,debug:e.debug??!1}}}class mi{constructor(e){this.map=new Map,this.maxSize=e}has(e){return this.map.has(e)}add(e){if(this.map.has(e))return this.map.delete(e),this.map.set(e,Date.now()),this;if(this.map.size>=this.maxSize){const e=this.map.keys().next().value;this.map.delete(e)}return this.map.set(e,Date.now()),this}delete(e){return this.map.delete(e)}clear(){this.map.clear()}get size(){return this.map.size}}function fi(e){const t=function(e){const t=Nt(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 yi="/api/asset/launchpad-contract/CallNativeTokenIn",wi="/api/asset/launchpad-contract/CallNativeTokenOut",ki="/api/asset/launchpad-contract/CallMemeTokenIn",vi="/api/asset/launchpad-contract/CallMemeTokenOut";class Ti 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,e.websiteUrl&&(this.websiteUrl=e.websiteUrl),e.telegramUrl&&(this.telegramUrl=e.telegramUrl),e.twitterUrl&&(this.twitterUrl=e.twitterUrl),this.tokenCategory=e.tokenCategory,this.tokenCollection=e.tokenCollection,this.uniqueKey=e.uniqueKey,e.reverseBondingCurveConfiguration&&(this.reverseBondingCurveConfiguration=e.reverseBondingCurveConfiguration)}}function Si(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 bi={NATIVE:"native",EXACT:"exact"},Ai={LOCAL:"local",EXTERNAL:"external"};class Ei{static calculateBuyWithExact(e,t){const n=parseFloat(e),o=parseFloat(t),{BASE_PRICE:i,PRICE_SCALING_FACTOR:a,TRADING_FEE_FACTOR:s,GAS_FEE:c}=ti,l=this.roundUp(i*(Math.exp((o+n)*a)-Math.exp(o*a))/a,8),u=new r(l).multipliedBy(s).toFixed();return{amount:l.toString(),reverseBondingCurveFee:"0",transactionFee:u,gasFee:c}}static calculateBuyWithNative(e,t){const n=parseFloat(e),o=parseFloat(t),{BASE_PRICE:i,PRICE_SCALING_FACTOR:a,TRADING_FEE_FACTOR:s,GAS_FEE:c}=ti,l=Math.log(n*a/i+Math.exp(o*a))/a-o,u=new r(l).multipliedBy(s).toFixed();return{amount:l.toString(),reverseBondingCurveFee:"0",transactionFee:u,gasFee:c}}static calculateSellWithExact(e,t,n,o,i){const a=parseFloat(e),s=parseFloat(t),c=parseFloat(n),{BASE_PRICE:l,PRICE_SCALING_FACTOR:u,TRADING_FEE_FACTOR:d,GAS_FEE:h}=ti,g=l*(Math.exp(s*u)-Math.exp((s-a)*u))/u,p=new r(g),m=o+s/c*(i-o),f=p.multipliedBy(m).toFixed(8,r.ROUND_UP),y=p.multipliedBy(d).toFixed();return{amount:g.toString(),reverseBondingCurveFee:f,transactionFee:y,gasFee:h}}static calculateSellWithNative(e,t,n,o,i){const a=parseFloat(e),s=parseFloat(t),c=parseFloat(n),{BASE_PRICE:l,PRICE_SCALING_FACTOR:u,TRADING_FEE_FACTOR:d,GAS_FEE:h}=ti,g=s-Math.log(Math.exp(s*u)-a*u/l)/u,p=new r(a),m=o+s/c*(i-o),f=p.multipliedBy(m).toFixed(8,r.ROUND_UP),y=p.multipliedBy(d).toFixed();return{amount:g.toString(),reverseBondingCurveFee:f,transactionFee:y,gasFee:h}}static roundUp(e,t){const n=Math.pow(10,t);return Math.ceil(e*n)/n}}class Ii{constructor(e,t,n,o,i,r,a="local"){this.http=e,this.tokenResolver=t,this.logger=n,this.bundleHttp=o,this.galaChainHttp=i,this.dexApiHttp=r,this.defaultCalculateAmountMode=a,this.metadataCache=new ni}addIfDefined(e,t,n){return void 0!==n&&(e[t]=n),e}async uploadImageByTokenName(e){const{tokenName:t,options:n}=e;Ht(t);const o=`${t}.png`;zn(n.file,o,"image/png");try{const e=new FormData;if("undefined"!=typeof File&&n.file instanceof File)e.append("image",n.file);else{if(!Buffer.isBuffer(n.file))throw R("file","a File object (browser) or Buffer (Node.js)");{const o=`${n.tokenName||t}.png`,i=new Blob([n.file],{type:"image/png"});e.append("image",i,o)}}const o=await this.http.request({method:"POST",url:`/launchpad/upload-image?tokenName=${encodeURIComponent(n.tokenName||t)}`,data:e,headers:{}});if(!0===o.error||200!==o.status||!o.data?.imageUrl)throw q(o.message||"Image upload failed - no URL returned",o.status);return o.data.imageUrl}catch(e){if(e instanceof Error&&e.message.includes("FormData"))throw K("File upload failed: FormData not supported in this environment. Ensure you have proper polyfills for Node.js environments.","FormData");throw e}}async fetchPoolsFromAPI(e){Vt(e),e.tokenName&&Ht(e.tokenName);const t={page:e.page.toString(),limit:e.limit.toString()};void 0!==e.type&&(t.type=e.type),void 0!==e.tokenName&&(t.tokenName=e.tokenName),void 0!==e.search&&(t.search=e.search);const n=b(t),o=await this.http.get("/launchpad/fetch-pool",n);if(!0===o.error||200!==o.status||!o.data)throw q(o.message||"Failed to fetch pools",o.status);let i=[];const r=(await import("bignumber.js")).default;if(o.data.tokens)if(Array.isArray(o.data.tokens))i=o.data.tokens.map(e=>{const t=e.reverseBondingCurveMinFeePortion??"0",n=e.reverseBondingCurveMaxFeePortion??"0",o=!new r(t).isZero()||!new r(n).isZero();return{...e,reverseBondingCurveMinFeePortion:t,reverseBondingCurveMaxFeePortion:n,hasReverseBondingCurveFee:o,createdAt:e.created_at||e.createdAt||""}});else{const e=o.data.tokens,t=e.reverseBondingCurveMinFeePortion??"0",n=e.reverseBondingCurveMaxFeePortion??"0",a=!new r(t).isZero()||!new r(n).isZero();i=[{...e,reverseBondingCurveMinFeePortion:t,reverseBondingCurveMaxFeePortion:n,hasReverseBondingCurveFee:a,createdAt:e.created_at||e.createdAt||""}]}else o.data.pools&&Array.isArray(o.data.pools)&&(i=o.data.pools.map(e=>{const t=e.reverseBondingCurveMinFeePortion??"0",n=e.reverseBondingCurveMaxFeePortion??"0",o=!new r(t).isZero()||!new r(n).isZero();return{...e,reverseBondingCurveMinFeePortion:t,reverseBondingCurveMaxFeePortion:n,hasReverseBondingCurveFee:o,createdAt:e.created_at||e.createdAt||""}}));const{extractMetadataFromPoolData:a,isValidPoolForCaching:s}=await Promise.resolve().then(function(){return Ri});i.forEach(e=>{if(!s(e))return void this.logger.debug("Skipping pool with invalid structure for caching",e);const t=a(e,this.logger);t&&this.warmCacheFromPoolData(e.tokenName,t)});const c=o.data.count??o.data.total??0,l=o.data.page??e.page??1,u=o.data.limit??e.limit??10,d=u>0?Math.ceil(c/u):1;return{pools:i,page:l,limit:u,total:c,totalPages:d,hasNext:l<d,hasPrevious:l>1}}async _getAmount(e){if(Xt(e),!this.galaChainHttp)throw K("GalaChain client not configured. Direct GalaChain calls require galaChainHttp client.","galaChainHttp");const{endpoint:t,body:n}=((e,t,n,o)=>{if("NATIVE"===e&&"IN"===t)return{endpoint:yi,body:{vaultAddress:n,tokenQuantity:o,IsPreMint:!1}};if("NATIVE"===e&&"OUT"===t)return{endpoint:wi,body:{vaultAddress:n,tokenQuantity:o,IsPreMint:!1}};if("MEME"===e&&"IN"===t)return{endpoint:ki,body:{vaultAddress:n,nativeTokenQuantity:o,IsPreMint:!1}};if("MEME"===e&&"OUT"===t)return{endpoint:vi,body:{vaultAddress:n,nativeTokenQuantity:o,IsPreMint:!1}};throw R("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,n);if(!Si(e))throw q("Malformed response data from GalaChain gateway");if(1!==e.Status)throw q(`GalaChain calculation failed with status ${e.Status}`,e.Status);const{calculatedQuantity:o,extraFees:i}=e.Data;return{amount:o,reverseBondingCurveFee:i.reverseBondingCurve,transactionFee:i.transactionFees,gasFee:"1"}}catch(o){throw this.logger.error(`GalaChain ${e.type}-${e.method} operation failed:`,{endpoint:t,requestBody:n,error:o instanceof Error?o.message:o}),o}}async checkPool(e){Qt(e),e.tokenName&&Ht(e.tokenName);const t=b(e),n=await this.http.get("/launchpad/check-pool",t);if(!0===n.error||200!==n.status)throw q(n.message||"Failed to check pool",n.status);const o=n.data;return e.symbol?o?.isSymbolExist??!1:e.tokenName?o?.isNameExist??!1:o?.exists??!1}async fetchVolumeData(e){if(!In(e))throw new P("Invalid options provided. Expected { tokenName: string, from?: number, to?: number, resolution?: number }","options","INVALID_OPTIONS");const{tokenName:t,from:n,to:o,resolution:i}=e;if(Ht(t),!n||!o||!i)throw new P("Graph options (from, to, resolution) are required","options","MISSING_GRAPH_OPTIONS");const r={tokenName:t,from:n,to:o,resolution:i};Zt(r);const a=b(r),s=await this.http.get("/launchpad/get-graph-data",a);if(!0===s.error||200!==s.status||!s.data)throw q(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 n={page:e.page||1,limit:e.limit||10};return e.search&&(n.search=e.search),e.tokenName&&(n.tokenName=e.tokenName),t&&(n.type=t),this.fetchPoolsFromAPI(n)}async isTokenNameAvailable(e){try{return!await this.checkPool({tokenName:e})}catch{return!1}}async isTokenSymbolAvailable(e){try{return!await this.checkPool({symbol:e})}catch{return!1}}async calculateBuyAmount(e){if(!e||"object"!=typeof e)throw new P("Invalid options provided. Expected an options object.","options","INVALID_OPTIONS");const{tokenName:t,amount:n,type:o,currentSupply:i}=e,r=e.mode??this.defaultCalculateAmountMode;if("local"!==r&&"external"!==r)throw new P(`Invalid calculation mode "${r}". Must be "local" or "external".`,"mode","INVALID_CALCULATION_MODE");if(!t||"string"!=typeof t)throw new P("Token name is required and must be a string","tokenName","INVALID_TOKEN_NAME");if(!n||"string"!=typeof n)throw new P("Amount is required and must be a string","amount","INVALID_AMOUNT");if(o!==bi.NATIVE&&o!==bi.EXACT)throw new P('Type must be either "native" or "exact"',"type","INVALID_TYPE");return"external"===r?this.calculateBuyAmountExternal({tokenName:t,amount:n,type:o}):this.calculateBuyAmountLocal(this.addIfDefined({tokenName:t,amount:n,type:o},"currentSupply",i))}async calculateBuyAmountExternal(e){const{tokenName:t,amount:n,type:o}=e,i=await this.tokenResolver.resolveTokenToVault(t);if(!i)throw new P(`Token "${t}" not found. Please verify the token name is correct.`,"tokenName","TOKEN_NOT_FOUND");return o===bi.EXACT?this._getAmount({type:"NATIVE",method:"IN",vaultAddress:i,amount:n}):this._getAmount({type:"MEME",method:"OUT",vaultAddress:i,amount:n})}async calculateSellAmount(e){const{tokenName:t,amount:n,type:o,currentSupply:i,maxSupply:r,reverseBondingCurveMaxFeeFactor:a,reverseBondingCurveMinFeeFactor:s}=e,c=e.mode??this.defaultCalculateAmountMode;if("local"!==c&&"external"!==c)throw new P(`Invalid calculation mode "${c}". Must be "local" or "external".`,"mode","INVALID_CALCULATION_MODE");if(!t||"string"!=typeof t)throw new P("Token name is required and must be a string","tokenName","INVALID_TOKEN_NAME");if(!n||"string"!=typeof n)throw new P("Amount is required and must be a string","amount","INVALID_AMOUNT");if(o!==bi.EXACT&&o!==bi.NATIVE)throw new P('Type must be either "exact" or "native"',"type","INVALID_TYPE");if("external"===c)return this.calculateSellAmountExternal({tokenName:t,amount:n,type:o});{const e={tokenName:t,amount:n,type:o,...void 0!==i&&{currentSupply:i},...void 0!==r&&{maxSupply:r},...void 0!==a&&{reverseBondingCurveMaxFeeFactor:a},...void 0!==s&&{reverseBondingCurveMinFeeFactor:s}};return this.calculateSellAmountLocal(e)}}async calculateSellAmountExternal(e){const{tokenName:t,amount:n,type:o}=e,i=await this.tokenResolver.resolveTokenToVault(t);if(!i)throw new P(`Token "${t}" not found. Please verify the token name is correct.`,"tokenName","TOKEN_NOT_FOUND");return o===bi.EXACT?this._getAmount({type:"NATIVE",method:"OUT",vaultAddress:i,amount:n}):this._getAmount({type:"MEME",method:"IN",vaultAddress:i,amount:n})}async calculateBuyAmountLocal(e){const{tokenName:t,amount:n,type:o,currentSupply:i}=e;if(!n||"string"!=typeof n)throw new P("Amount is required and must be a string","amount","INVALID_AMOUNT");if(o!==bi.NATIVE&&o!==bi.EXACT)throw new P('Type must be either "native" or "exact"',"type","INVALID_TYPE");void 0!==i&&Jt(i,"currentSupply");const r=!i;if(r&&!t)throw new P("Token name is required when currentSupply is not provided","tokenName","MISSING_TOKEN_NAME");t&&Ht(t);let a=i;if(r){a=(await this.fetchPoolDetailsForCalculation(t)).currentSupply}return o===bi.EXACT?Ei.calculateBuyWithExact(n,a):Ei.calculateBuyWithNative(n,a)}async calculateSellAmountLocal(e){const{tokenName:t,amount:n,type:o,currentSupply:i,maxSupply:r,reverseBondingCurveMaxFeeFactor:a,reverseBondingCurveMinFeeFactor:s}=e;if(!n||"string"!=typeof n)throw new P("Amount is required and must be a string","amount","INVALID_AMOUNT");if(o!==bi.EXACT&&o!==bi.NATIVE)throw new P('Type must be either "exact" or "native"',"type","INVALID_TYPE");void 0!==i&&Jt(i,"currentSupply");const c=!i||!r||void 0===a||void 0===s;if(c&&!t)throw new P("Token name is required when currentSupply, maxSupply, or fee factors are not provided","tokenName","MISSING_TOKEN_NAME");t&&Ht(t);let l=i,u=r,d=a,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 o===bi.EXACT?Ei.calculateSellWithExact(n,l,u,h,d):Ei.calculateSellWithNative(n,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 Sn(t,"tokenName")&&En(t)&&bn(t,"currentSupply")}(e))throw new Error("Invalid CalculateBuyAmountForGraduationOptions provided");const{tokenName:n,calculateAmountMode:o,currentSupply:i}=t;Ht(n);const r=await this.tokenResolver.resolveTokenToVault(n);if(!r)throw new P(I(n),"tokenName","VAULT_NOT_FOUND");if(!this.galaChainHttp)throw K("GalaChain HTTP client not configured");const a=await this.galaChainHttp.post("/api/asset/launchpad-contract/FetchSaleDetails",{vaultAddress:r});if(1!==a.Status)throw q(`Failed to fetch pool details: Status ${a.Status}`,a.Status);const s=a.Data,c=(await import("bignumber.js")).default,l=i??new c(s.maxSupply).minus(s.sellingTokenQuantity).toFixed(),u=s.sellingTokenQuantity;if("0"===u)throw new P(`Token ${n} is already graduated (no tokens remaining in pool)`,"tokenName","ALREADY_GRADUATED");const d={tokenName:n,amount:u,type:"exact",currentSupply:l,...void 0!==o&&{mode:o}};return await this.calculateBuyAmount(d)}async launchToken(e){if(!this.bundleHttp)throw K("Bundle backend client not configured. LaunchToken requires bundleHttp client.","bundleHttp");fi(e);const t=e.preBuyQuantity||"0";if(isNaN(Number(t))||Number(t)<0)throw new P("Pre-buy quantity must be a valid non-negative number string","preBuyQuantity","INVALID_PRE_BUY_QUANTITY");if(e.reverseBondingCurveConfiguration){const{minFeePortion:t,maxFeePortion:n}=e.reverseBondingCurveConfiguration,o=Number(t),i=Number(n);if(isNaN(o)||isNaN(i)||o<=0||i<=0||o>=i)throw new P("Reverse bonding curve configuration must have valid min/max fee portions with min < max","reverseBondingCurveConfiguration","INVALID_BONDING_CURVE_CONFIG")}let n="";if(e.tokenImage)if(e.tokenImage instanceof File||Buffer.isBuffer(e.tokenImage)){const t=await this.uploadImageByTokenName({tokenName:e.tokenName,options:{file:e.tokenImage,tokenName:e.tokenName}});if(!t)throw q("Image upload failed: No URL returned");n=t}else"string"==typeof e.tokenImage&&(n=e.tokenImage);const i=`galaswap - operation - ${c.v4()}-${Date.now()}-${this.http.getAddress()}`,r={tokenName:e.tokenName.trim(),tokenSymbol:e.tokenSymbol.trim().toUpperCase(),tokenDescription:e.tokenDescription.trim(),tokenImage:n.trim(),preBuyQuantity:t.toString(),tokenCategory:e.tokenCategory||"Unit",tokenCollection:e.tokenCollection||"Token",uniqueKey:i};e.websiteUrl?.trim()&&(r.websiteUrl=e.websiteUrl.trim()),e.telegramUrl?.trim()&&(r.telegramUrl=e.telegramUrl.trim()),e.twitterUrl?.trim()&&(r.twitterUrl=e.twitterUrl.trim()),e.reverseBondingCurveConfiguration&&(r.reverseBondingCurveConfiguration={minFeePortion:e.reverseBondingCurveConfiguration.minFeePortion.toString(),maxFeePortion:e.reverseBondingCurveConfiguration.maxFeePortion.toString()});const a=new Ti(r),s=await this.http.signWithGalaChain("CreateSale",a,o.SigningType.SIGN_TYPED_DATA),{signature:l,types:u,domain:d,prefix:h}=s,g={tokenName:a.tokenName,tokenSymbol:a.tokenSymbol,tokenDescription:a.tokenDescription,tokenImage:a.tokenImage,preBuyQuantity:a.preBuyQuantity,...a.websiteUrl&&{websiteUrl:a.websiteUrl},...a.telegramUrl&&{telegramUrl:a.telegramUrl},...a.twitterUrl&&{twitterUrl:a.twitterUrl},tokenCategory:a.tokenCategory,tokenCollection:a.tokenCollection,uniqueKey:a.uniqueKey,signature:l,types:u,domain:d,...h&&{prefix:h},...a.reverseBondingCurveConfiguration&&{reverseBondingCurveConfiguration:a.reverseBondingCurveConfiguration}},p=`${e.tokenName.trim()}$Unit$none$none`,m="GALA$Unit$none$none";let f;if(parseFloat(t)>0){const e=`$service$${p}$launchpad`;f=[e,`$token$${p}$${e}`,`$tokenBalance$${p}$${e}`,`$tokenBalance$${p}$${e}`,`$tokenBalance$${m}$${e}`,`$tokenBalance$${m}$${e}`]}else{const e=`$service$${p}$launchpad`;f=[e,`$token$${p}$${e}`,`$tokenBalance$${p}$${e}`]}const y={signedDto:g,stringsInstructions:f,method:"CreateSale"},w=await this.bundleHttp.post("/bundle",y);if(w.error||!w.data)throw q(w.message||"Token launch failed");return w.data}async fetchTokenDistribution(e){if(!e)throw M("tokenName","Token name");Ht(e);const t=await this.tokenResolver.resolveTokenToVault(e);if(!t)throw new P(I(e),"tokenName","VAULT_NOT_FOUND");this.metadataCache.set(e,{vaultAddress:t});const n=encodeURIComponent(t),o=await this.http.get(`/holders/${n}`);if(!0===o.error||200!==o.status||!o.data)throw q(o.message||"Failed to fetch token distribution",o.status);const i=o.data;if(!Array.isArray(i))throw q("Invalid API response: expected array of holders",o.status);for(const e of i){if(!e.owner||"string"!=typeof e.owner)throw q("Invalid holder data: missing or invalid owner field",o.status);if(!e.quantity||"string"!=typeof e.quantity)throw q("Invalid holder data: missing or invalid quantity field",o.status);const t=parseFloat(e.quantity);if(isNaN(t)||!isFinite(t))throw q(`Invalid holder quantity: "${e.quantity}"`,o.status)}const a=i.reduce((e,t)=>e.plus(t.quantity),new r(0));return{holders:i.map(e=>{const t=new r(e.quantity),n=a.isZero()?0:t.dividedBy(a).multipliedBy(100).toNumber();return{address:e.owner,balance:e.quantity,percentage:n}}),totalSupply:a.toFixed(),totalHolders:i.length,lastUpdated:new Date}}async fetchTokenBadges(e){if(!e)throw M("tokenName","Token name");Ht(e);const t=await this.http.get("/launchpad/get-badge/",{tokenName:e});if(t.error||!t.data)throw q(t.message||"Failed to fetch token badges");return{volumeBadges:t.data.volumeBadge||[],engagementBadges:t.data.engagementBadge||[]}}async hasTokenBadgeByTokenName(e){const{tokenName:t,badgeType:n,badgeName:o}=e;try{const e=await this.fetchTokenBadges(t);if(!e)return!1;const i=("volume"===n?e.volumeBadges:e.engagementBadges).find(e=>e.badgeName===o);return i?.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 P("Invalid pre-mint calculation data","data","INVALID_PRE_MINT_DATA");if(!this.galaChainHttp)throw K("GalaChain HTTP client not available. Please initialize SDK with galaChainBaseUrl.","galaChainHttp");try{const t={vaultAddress:"service|testToken",nativeTokenQuantity:e.nativeTokenQuantity,IsPreMint:!0},n=await this.galaChainHttp.post("/api/asset/launchpad-contract/CallMemeTokenOut",t);if(!Si(n))throw q("Malformed response data from GalaChain gateway");if(1!==n.Status)throw q(`GalaChain calculation failed with status ${n.Status}`,n.Status);const{calculatedQuantity:o,extraFees:i}=n.Data;return{amount:o,reverseBondingCurveFee:i.reverseBondingCurve,transactionFee:i.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 P(I(e),"tokenName","VAULT_NOT_FOUND");if(!this.galaChainHttp)throw K("GalaChain HTTP client not configured");const n=await this.galaChainHttp.post("/api/asset/launchpad-contract/FetchSaleDetails",{vaultAddress:t});if(1!==n.Status)throw q(`Failed to fetch pool details: Status ${n.Status}`,n.Status);const o=n.Data,i=new(0,(await import("bignumber.js")).default)(o.maxSupply).minus(o.sellingTokenQuantity).toFixed(),r=o.sellingTokenQuantity,a=o.maxSupply;let s=.5,c=0;o.reverseBondingCurveConfiguration?(s=parseFloat(o.reverseBondingCurveConfiguration.maxFeePortion),c=parseFloat(o.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:a,reverseBondingCurveMaxFeeFactor:s,reverseBondingCurveMinFeeFactor:c,reverseBondingCurveNetFeeFactor:l}),{currentSupply:i,remainingTokens:r,maxSupply:a,reverseBondingCurveMaxFeeFactor:s,reverseBondingCurveMinFeeFactor:c,reverseBondingCurveNetFeeFactor:l}}async fetchCurrentSupply(e){Ht(e);const t=await this.tokenResolver.resolveTokenToVault(e);if(!t)throw new P(I(e),"tokenName","VAULT_NOT_FOUND");if(!this.galaChainHttp)throw K("GalaChain HTTP client not configured");const n=await this.galaChainHttp.post("/api/asset/launchpad-contract/FetchSaleDetails",{vaultAddress:t});if(1!==n.Status)throw q(`Failed to fetch pool details: Status ${n.Status}`,n.Status);const o=n.Data,i=new(0,(await import("bignumber.js")).default)(o.maxSupply).minus(o.sellingTokenQuantity).toFixed(),r=o.maxSupply;return this.metadataCache.set(e,{maxSupply:r}),i}getAddress(){return this.http.getAddress()}formatAddressForBackend(e){return Yt(e)}validateTokenName(e){return Ht(e)}validatePagination(e){return Vt(e)}async fetchTokenPrice(e){if(!this.dexApiHttp)throw K("DEX API client not configured. Token price fetching requires dexApiHttp client.","dexApiHttp");if(!e||Array.isArray(e)&&0===e.length)throw M("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}}),n=[];return e.tokens&&Array.isArray(e.tokens)&&e.tokens.forEach(e=>{e.currentPrices&&e.symbol&&n.push({symbol:e.symbol,price:e.currentPrices.usd})}),n}catch(e){throw q(`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 Sn(t,"tokenName")&&En(t)&&bn(t,"currentSupply")}(e))throw new Error("Invalid FetchLaunchpadTokenSpotPriceOptions provided");const{tokenName:n,calculateAmountMode:o,currentSupply:i}=t;if(!n||"string"!=typeof n)throw M("tokenName","Token name (string)");try{const e={tokenName:n,amount:"1",type:"native",...void 0!==o&&{mode:o},...void 0!==i&&{currentSupply:i}},t=await this.calculateBuyAmount(e),r=(await this.fetchTokenPrice("GALA"))[0];if(!r)throw q("GALA price not available");const a=Number(t.amount);if(a<=0)throw new P(`Invalid token amount calculation: ${a}`,"amount","INVALID_CALCULATION");const s=r.price/a;return{symbol:n.toUpperCase(),price:s}}catch(e){if(e instanceof Error)throw new Error(`Failed to calculate launchpad token spot price for ${n}: ${e.message}`);throw new Error(`Failed to calculate launchpad token spot price for ${n}: ${String(e)}`)}}warmCacheFromPoolData(e,t){this.metadataCache.warmFromPoolData(e,t)}getCacheStats(){return this.metadataCache.stats()}clearCache(e){this.metadataCache.clear(e)}}const Pi={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 Ni(e){return Pi[e]}class Di extends Error{constructor(e,t){super(e),this.cause=t,this.name="WebSocketError"}}class Ci extends Error{constructor(e,t,n){super(`Transaction ${e} failed with status: ${t}${n?` - ${n}`:""}`),this.transactionId=e,this.status=t,this.name="TransactionFailedError"}}function Fi(e,t){if(!e)throw new Di(`Invalid WebSocket response received for transaction ${t}: response is null or undefined`);if("object"!=typeof e)throw new Di(`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 Di(`Invalid WebSocket response received for transaction ${t}: missing status field`)}function xi(e,t,n,o){Fi(e,t);const i=e,r=i.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 Di(`Invalid trade data received for transaction ${t}`);const a={transactionId:t,type:n,method:"native"===o.type?"native":"exact",inputAmount:r.inputQuantity||o.amount,outputAmount:r.outputQuantity||o.expectedAmount||"0",totalFees:r.totalFees||"0",tokenName:o.tokenName,vaultAddress:r.vaultAddress||"",timestamp:Date.now()};return void 0!==i.blockHash&&(a.blockHash=i.blockHash),void 0!==i.gasUsed&&(a.gasUsed=i.gasUsed),void 0!==o.slippageToleranceFactor&&(a.slippageTolerance=o.slippageToleranceFactor),a}const $i="4.0.1-beta.3";class Li{constructor(e){this.logger=e||new S({debug:!1,context:"LiquidityEventExtractor"})}walkPayloadForLiquidityEvents(e,t){const n=[],o=new WeakSet,i=(e,r=0)=>{if(r>50)this.logger.debug("Payload nesting exceeded maximum depth of 50");else if(e&&"string"!=typeof e&&"object"==typeof e){if(o.has(e))return;o.add(e);const a=this.extractLiquidityFromObject(e);a&&!t.has(a.transactionId)&&(n.push(a),t.add(a.transactionId));for(const t of Object.values(e))i(t,r+1)}};return i(e,0),n}extractLiquidityFromObject(e){const t=this.extractTransactionId(e);if(!t)return null;const n=e.Data,o=n&&"object"==typeof n&&!Array.isArray(n)?n:e,i=this.extractPositionId(o),r=this.extractPoolHash(o),a=this.extractAmounts(o),s=this.extractUserAddress(o),c=this.extractPoolFee(o);if(!(i&&r&&a&&s&&null!==c))return null;const l=this.extractPoolAlias(o),u=this.extractUserBalanceDelta(o),d=this.extractTimestamp(o),h=u?.token0Balance?.collection,g=u?.token1Balance?.collection,p={transactionId:t,positionId:i,poolHash:r,poolFee:c,amounts:a,userAddress:s};return void 0!==h&&(p.token0=h),void 0!==g&&(p.token1=g),void 0!==d&&(p.timestamp=d),void 0!==l&&(p.poolAlias=l),void 0!==u&&(p.userBalanceDelta=u),p}extractTransactionId(e){const t=["transactionId","txId","tx_id","hash","txHash","id"];for(const n of t){const t=e[n];if("string"==typeof t&&t.trim())return t}return null}extractPositionId(e){const t=["positionId","position_id","tokenId","nftId"];for(const n of t){const t=e[n];if("string"==typeof t&&t.trim())return t}return null}extractPoolHash(e){const t=["poolHash","pool_hash","poolId","pool"];for(const n of t){const t=e[n];if("string"==typeof t&&t.trim())return t}return null}extractPoolAlias(e){const t=["poolAlias","pool_alias"];for(const n of t){const t=e[n];if("string"==typeof t&&t.trim())return t}}extractAmounts(e){const t=e.amounts;if(Array.isArray(t)&&t.length>=2){const e=String(t[0]).trim(),n=String(t[1]).trim();if(e&&n)return[e,n]}const n=e.amount0||e.amount0Desired,o=e.amount1||e.amount1Desired;return void 0!==n&&void 0!==o?[String(n),String(o)]:null}extractUserAddress(e){const t=["userAddress","user","owner","from","sender","wallet","address"];for(const n of t){const t=e[n];if("string"==typeof t&&t.trim())return t}return null}extractPoolFee(e){const t=["poolFee","fee","feeTier","feeTierBps"];for(const n of t){const t=e[n];if("number"==typeof t)return this.normalizeFee(t);if("string"==typeof t){const e=Number(t);if(!Number.isNaN(e))return this.normalizeFee(e)}}return null}normalizeFee(e){return 1===e||1e4===e?1e4:.3===e||3e3===e?3e3:.05===e||500===e?500:Number.isInteger(e)?e:e<1?Math.round(1e4*e):e}extractTimestamp(e){const t=["timeStamp","timestamp","time","createdAt","date"];for(const n of t){const t=e[n];if("number"==typeof t)return t;if("string"==typeof t){const e=new Date(t).getTime();if(!Number.isNaN(e))return e}}}extractUserBalanceDelta(e){const t=e.userBalanceDelta;if(!t||"object"!=typeof t)return;const n=t,o=this.extractBalanceObject(n.token0Balance),i=this.extractBalanceObject(n.token1Balance);if(!o&&!i)return;const r={};return void 0!==o&&(r.token0Balance=o),void 0!==i&&(r.token1Balance=i),r}extractBalanceObject(e){if(!e||"object"!=typeof e)return;const t=e,n=t.collection,o=t.category,i=t.type,r=t.additionalKey,a=t.quantity,s=t.owner;return"string"==typeof n&&"string"==typeof o&&"string"==typeof i&&"string"==typeof r&&"string"==typeof a&&"string"==typeof s?{collection:n,category:o,type:i,additionalKey:r,quantity:a,owner:s}:void 0}}class Oi{constructor(e){this.wallet=e.wallet;let n=null,o="STAGE";e.env?(o=e.env,n=Ni(e.env)):e.baseUrl?.includes("prod")?(o="PROD",n=Ni("PROD")):(o="STAGE",n=Ni("STAGE")),this.environment=o,this.config={baseUrl:n.launchpadBaseUrl,galaChainBaseUrl:n.galaChainBaseUrl,bundleBaseUrl:n.bundleBaseUrl,webSocketUrl:n.webSocketUrl,dexApiBaseUrl:n.dexApiBaseUrl,dexBackendBaseUrl:n.dexBackendBaseUrl,launchpadFrontendUrl:n.launchpadFrontendUrl,timeout:3e4,debug:!1,...e},this.logger=new S({debug:this.config.debug??!1,context:"LaunchpadSDK"}),this.validateConfiguration(),this.slippageToleranceFactor=void 0===e.slippageToleranceFactor?Oi.DEFAULT_SLIPPAGE_TOLERANCE_FACTOR:this.parseSlippageToleranceFactor(e.slippageToleranceFactor),this.maxAcceptableReverseBondingCurveFeeSlippageFactor=void 0===e.maxAcceptableReverseBondingCurveFeeSlippageFactor?Oi.DEFAULT_MAX_ACCEPTABLE_REVERSE_BONDING_CURVE_FEE_SLIPPAGE_FACTOR:this.parseFeeSlippageFactor(e.maxAcceptableReverseBondingCurveFeeSlippageFactor),this.calculateAmountMode=e.calculateAmountMode||Oi.DEFAULT_CALCULATE_AMOUNT_MODE,this.pricingConcurrency=e.pricingConcurrency||5,this.auth=new T({wallet:e.wallet,messagePrefix:"Create a GalaChain Wallet"}),this.http=new A(this.auth,this.config),this.galaChainHttp=new A(this.auth,{...this.config,baseUrl:this.config.galaChainBaseUrl}),this.bundleHttp=new A(this.auth,{...this.config,baseUrl:this.config.bundleBaseUrl}),this.dexApiHttp=new A(this.auth,{...this.config,baseUrl:this.config.dexApiBaseUrl}),this.dexBackendHttp=new A(this.auth,{...this.config,baseUrl:this.config.dexBackendBaseUrl}),this.galaChainPublicAxios=t.create({baseURL:this.config.galaChainBaseUrl,timeout:this.config.timeout||3e4,headers:{"Content-Type":"application/json",Accept:"application/json"}}),this.cache=new ni(e.debug||!1),this.launchpadService=new Hn(this.http),this.tokenResolverService=new Go(this.launchpadService.poolService),this.launchpadAPI=new Ii(this.http,this.tokenResolverService,this.logger,this.bundleHttp,this.galaChainHttp,this.dexApiHttp,this.calculateAmountMode),this.galaChainService=new Ao(this.galaChainHttp,e.wallet,this.tokenResolverService,e.debug||!1,this.galaChainPublicAxios),this.dexService=new Eo(this.dexBackendHttp,this.cache,this.galaChainService,e.debug||!1),this.bundleService=new Uo(this.bundleHttp,this.tokenResolverService,this.config.debug||!1,e.wallet,e.wallet?this.getAddress():void 0,this.slippageToleranceFactor,this.maxAcceptableReverseBondingCurveFeeSlippageFactor),this.websocketService=new Ko({url:this.config.webSocketUrl},this.config.debug),this.priceHistoryService=new jo(this.dexBackendHttp,this.config.debug||!1,this.tokenResolverService),this.dexQuoteService=new ri(this.galaChainHttp,this.config.galaChainBaseUrl,e.debug||!1,e.dexQuoteNetworkTimeout||3e4),this.gswapService=new ei({privateKey:e.wallet?.privateKey,getWalletAddress:()=>this.wallet?this.getAddress():void 0,gatewayBaseUrl:this.config.galaChainBaseUrl,bundlerBaseUrl:this.config.bundleBaseUrl,galaChainBaseUrl:this.config.galaChainBaseUrl,dexBackendBaseUrl:this.config.dexBackendBaseUrl,dexBackendHttp:this.dexBackendHttp},this.websocketService,this.dexQuoteService),this.dexPoolService=new oi(this.dexBackendHttp,this.config.dexBackendBaseUrl,this.gswapService,this.pricingConcurrency,e.debug||!1)}createOverrideSdk(e){if(!e||"string"!=typeof e)throw K("Invalid privateKey: must be a non-empty string","privateKey");if(!e.match(/^0x[a-fA-F0-9]{64}$/))throw K('Invalid privateKey format: must be "0x" followed by 64 hexadecimal characters',"privateKey");const t=new n.Wallet(e),o={...this.config,wallet:t};return new Oi(o)}getAddress(){return this.validateWallet(),this.auth.getAddress()}getEthereumAddress(){return this.validateWallet(),this.wallet.address}validateWallet(){if(!this.wallet)throw new P("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 P("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,environment:this.environment,slippageToleranceFactor:this.slippageToleranceFactor,maxAcceptableReverseBondingCurveFeeSlippageFactor:this.maxAcceptableReverseBondingCurveFeeSlippageFactor,calculateAmountMode:this.calculateAmountMode}}getVersion(){return $i}getUrlByTokenName(e){const t=this.config.launchpadFrontendUrl;if(!t)throw K("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 fetchCompositePoolData(e){return this.dexQuoteService.fetchCompositePoolData(e)}async calculateDexPoolQuoteExactAmountLocal(e){return this.dexQuoteService.calculateDexPoolQuoteExactAmountLocal(e)}async calculateDexPoolQuoteExactAmountExternal(e){return this.dexQuoteService.calculateDexPoolQuoteExactAmountExternal(e)}async calculateDexPoolQuoteExactAmount(e,t="local"){return this.dexQuoteService.calculateDexPoolQuoteExactAmount(e,t)}async fetchTokenDistribution(e){return this.launchpadService.fetchTokenDistribution(e)}async fetchTokenBadges(e){return this.launchpadService.fetchTokenBadges(e)}async fetchTokenPrice(e){const{tokenName:t,tokenId:n,currentSupply:o,calculateAmountMode:i}=e;if(t&&!n){const e={tokenName:t};return o&&(e.currentSupply=o),i&&(e.calculateAmountMode=i),this.fetchLaunchpadTokenSpotPrice(e)}if(n&&!t)try{return await this.dexService.fetchTokenPrice({tokenId:n})}catch(e){const t=function(e){if(Bn(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(n)).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 o&&(r.currentSupply=o),i&&(r.calculateAmountMode=i),this.fetchLaunchpadTokenSpotPrice(r)}catch(t){throw this.logger.error(`Launchpad fallback failed: ${_n(t)}`),e}}throw e}if(!t&&!n)throw M("tokenName or tokenId","Either tokenName (for launchpad tokens) or tokenId (for DEX tokens) is required");throw new P("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 fetchAllDexSeasons(){return this.dexService.fetchAllDexSeasons()}async fetchCurrentDexSeason(){return this.dexService.fetchCurrentDexSeason()}async fetchDexLeaderboardBySeasonId(e){return this.dexService.fetchDexLeaderboardBySeasonId(e)}async fetchCurrentDexLeaderboard(){return this.dexService.fetchCurrentDexLeaderboard()}async fetchDexAggregatedVolumeSummary(){return this.dexService.fetchDexAggregatedVolumeSummary()}async fetchLaunchTokenFee(){return this.galaChainService.fetchLaunchTokenFee()}async fetchTokenClassesWithSupply(e){return this.galaChainService.fetchTokenClassesWithSupply(e)}async fetchPoolDetails(e){const t=await this.resolveVaultAddress(e);if(!t)throw new Error(I(e));const n=(await this.galaChainService.fetchPoolDetails({vaultAddress:t})).Data,o=await this.launchpadAPI.fetchPoolDetailsForCalculation(e);return n.currentSupply=o.currentSupply,n.reverseBondingCurveMaxFeeFactor=o.reverseBondingCurveMaxFeeFactor,n.reverseBondingCurveMinFeeFactor=o.reverseBondingCurveMinFeeFactor,n.reverseBondingCurveNetFeeFactor=o.reverseBondingCurveNetFeeFactor,n.tokenName=e,n}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 en}),n=t(e)||this.getAddress();return this.galaChainService.fetchGalaBalance({owner:n,collection:"GALA",category:"Unit",additionalKey:"none",type:"none",instance:"0"})}async fetchTokenBalance(e){const{normalizeAddressInput:t}=await Promise.resolve().then(function(){return en}),n=t(e.address);if(e.tokenId){const{normalizeToTokenInstanceKey:t}=await Promise.resolve().then(function(){return yo}),o=t(e.tokenId),{collection:i,category:r,type:a,additionalKey:s}=o;return this.galaChainService.fetchTokenBalance({owner:n,collection:i,category:r,additionalKey:s,type:a,instance:"0"})}if(e.tokenName){const t=(await this.fetchTokensHeld({tokenName:e.tokenName,page:1,limit:1,...n&&{address:n}})).tokens[0];return t?{quantity:t.quantity,collection:t.collection||"Token",category:"Unit",tokenId:`${t.collection||"Token"}|Unit|${t.symbol}|none`,symbol:t.symbol,name:t.name}:null}throw M("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:n,maxAcceptableReverseBondingCurveFeeSlippageFactor:o,privateKey:i,calculateAmountMode:r,currentSupply:a}=e;let s=t;void 0===r&&void 0===a||(s={tokenName:t,...void 0!==r&&{calculateAmountMode:r},...void 0!==a&&{currentSupply:a}});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!==n&&(l.slippageToleranceFactor=n),void 0!==o&&(l.maxAcceptableReverseBondingCurveFeeSlippageFactor=o),void 0!==i&&(l.privateKey=i),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:n,...o}=e;return t.buy(o)}this.validateWallet(),await this.ensureWebSocketConnection();const t=(await this.bundleService.buyToken(e)).data,n=t?.transactionId;if(!n)throw G("No transaction ID returned from buy operation");return this.waitForConfirmation(n,t=>xi(t,n,"buy",e))}async sell(e){if(e.privateKey){const t=this.createOverrideSdk(e.privateKey),{privateKey:n,...o}=e;return t.sell(o)}this.validateWallet(),await this.ensureWebSocketConnection();const t=(await this.bundleService.sellToken(e)).data,n=t?.transactionId;if(!n)throw G("No transaction ID returned from sell operation");return this.waitForConfirmation(n,t=>xi(t,n,"sell",e))}async getBundlerTransactionResult(e){return this.bundleService.getBundlerTransactionResult(e)}async postComment(e){if(e.privateKey){const t=this.createOverrideSdk(e.privateKey),{privateKey:n,...o}=e;return t.postComment(o)}return this.validateWallet(),this.launchpadService.postComment(e)}async launchToken(e){if(e.privateKey){const t=this.createOverrideSdk(e.privateKey),{privateKey:n,...o}=e;return t.launchToken(o)}this.validateWallet(),await this.ensureWebSocketConnection();const t=await this.launchpadAPI.launchToken(e);return this.waitForConfirmation(t,n=>{Fi(n,t);const o=n?.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)}(o))throw new Di(`Invalid launch data received for transaction ${t}`);const i={transactionId:t,vaultAddress:o.vaultAddress||"",tokenStringKey:o.tokenStringKey||"",tokenName:e.tokenName,tokenSymbol:e.tokenSymbol,creatorAddress:o.creatorAddress||this.getAddress(),timestamp:Date.now(),...n.blockHash&&{blockHash:n.blockHash},...n.gasUsed&&{gasUsed:n.gasUsed}};return"string"==typeof e.tokenImage&&(i.tokenImage=e.tokenImage),void 0!==e.preBuyQuantity&&(i.preBuyQuantity=e.preBuyQuantity),i.vaultAddress&&this.tokenResolverService.set(e.tokenName,i.vaultAddress),i})}async uploadTokenImage(e){if(e.privateKey){const t=this.createOverrideSdk(e.privateKey),{privateKey:n,...o}=e;return t.uploadTokenImage(o)}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 en}),n=t(e)||this.getAddress();return this.launchpadService.fetchProfile(n)}async updateProfile(e){const{normalizeAddressInput:t}=await Promise.resolve().then(function(){return en}),n={...e,address:t(e.address)};if(n.privateKey){const e=this.createOverrideSdk(n.privateKey),{privateKey:t,...o}=n;return e.updateProfile(o)}return this.validateWallet(),this.launchpadService.updateProfile(n)}async uploadProfileImage(e){const{normalizeAddressInput:t}=await Promise.resolve().then(function(){return en}),n={...e,address:t(e.address)||this.getAddress()};if(n.privateKey){const e=this.createOverrideSdk(n.privateKey),{privateKey:t,...o}=n;return e.uploadProfileImage(o)}return this.validateWallet(),this.launchpadService.uploadProfileImage(n)}async retrieveGalaFromFaucet(e){const{normalizeAddressInput:t}=await Promise.resolve().then(function(){return en}),n={walletAddress:t(e)||this.getAddress(),amount:"5"};return this.launchpadService.transferFaucets(n)}async fetchTokensHeld(e){const{normalizeAddressInput:t}=await Promise.resolve().then(function(){return en}),n=t(e?.address)||this.getAddress(),o={page:e?.page||1,limit:e?.limit||10,address:n};return e?.tokenName&&(o.tokenName=e.tokenName),e?.search&&(o.search=e.search),this.launchpadService.fetchTokensHeld(o)}async fetchTokensCreated(e){const{normalizeAddressInput:t}=await Promise.resolve().then(function(){return en}),n={type:"DEFI",address:t(e?.address)||this.getAddress(),page:e?.page||1,limit:e?.limit||10};return e?.tokenName&&(n.tokenName=e.tokenName),e?.search&&(n.search=e.search),this.launchpadService.fetchTokenList(n)}async 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 en}),n={...e,recipientAddress:t(e.recipientAddress)};if(n.privateKey){const e=this.createOverrideSdk(n.privateKey),{privateKey:t,...o}=n;return e.transferGala(o)}return this.validateWallet(),this.galaChainService.transferGala(n)}async transferToken(e){const{normalizeAddressInput:t}=await Promise.resolve().then(function(){return en}),n={...e,to:t(e.to)};if(n.privateKey){const e=this.createOverrideSdk(n.privateKey),{privateKey:t,...o}=n;return e.transferToken(o)}return this.validateWallet(),this.galaChainService.transferToken(n)}async resolveTokenClassKey(e){return this.galaChainService.resolveTokenClassKey(e)}async lockToken(e){if(e.lockAuthority){const{normalizeAddressInput:t}=await Promise.resolve().then(function(){return en});e={...e,lockAuthority:t(e.lockAuthority)}}if(e.privateKey){const t=this.createOverrideSdk(e.privateKey),{privateKey:n,...o}=e;return t.lockToken(o)}return this.validateWallet(),this.galaChainService.lockToken(e)}async unlockToken(e){if(e.privateKey){const t=this.createOverrideSdk(e.privateKey),{privateKey:n,...o}=e;return t.unlockToken(o)}return this.validateWallet(),this.galaChainService.unlockToken(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 K("baseUrl is required in configuration","baseUrl");if(!this.config.webSocketUrl)throw K("webSocketUrl is required in configuration","webSocketUrl");try{new URL(this.config.baseUrl)}catch{throw K(`Invalid baseUrl format: ${this.config.baseUrl}`,"baseUrl")}try{new URL(this.config.webSocketUrl)}catch{throw K(`Invalid webSocketUrl format: ${this.config.webSocketUrl}`,"webSocketUrl")}if(this.config.galaChainBaseUrl)try{new URL(this.config.galaChainBaseUrl)}catch{throw K(`Invalid galaChainBaseUrl format: ${this.config.galaChainBaseUrl}`,"galaChainBaseUrl")}if(this.config.bundleBaseUrl)try{new URL(this.config.bundleBaseUrl)}catch{throw K(`Invalid bundleBaseUrl format: ${this.config.bundleBaseUrl}`,"bundleBaseUrl")}if(this.config.launchpadFrontendUrl)try{new URL(this.config.launchpadFrontendUrl)}catch{throw K(`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: ${Oi.DEFAULT_SLIPPAGE_TOLERANCE_FACTOR}`),Oi.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: ${Oi.DEFAULT_MAX_ACCEPTABLE_REVERSE_BONDING_CURVE_FEE_SLIPPAGE_FACTOR}`),Oi.DEFAULT_MAX_ACCEPTABLE_REVERSE_BONDING_CURVE_FEE_SLIPPAGE_FACTOR):t}async ensureWebSocketConnection(){this.websocketService.isConnected()||(await this.websocketService.connect(),this.logger.debug("WebSocket connection established"))}async waitForConfirmation(e,t){this.logger.debug(`Waiting for confirmation of transaction: ${e}`);try{const n=await this.websocketService.waitForTransaction(e);if("completed"!==n.status)throw new Ci(e,n.status,n.message);let o;try{o=t(n)}catch(t){if(t instanceof Di)throw t;throw new Di(`Failed to transform WebSocket response for transaction ${e}`,t instanceof Error?t:new Error(String(t)))}return this.logger.debug(`Transaction confirmed: ${e}`,o),o}catch(t){if(this.logger.error(`Transaction confirmation failed: ${e}`,t),t instanceof Ci||t instanceof Di)throw t;throw new Di(`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:n}=await Promise.resolve().then(function(){return Ri});e.forEach(e=>{if(!n(e))return;const o=t(e,this.logger);o&&this.launchpadAPI.warmCacheFromPoolData(e.tokenName,o)})}async getSwapQuoteExactInput(e,t,n){return this.gswapService.getSwapQuoteExactInput({fromToken:e,toToken:t,amount:n})}async getSwapQuoteExactOutput(e,t,n){return this.gswapService.getSwapQuoteExactOutput({fromToken:e,toToken:t,amount:n})}async executeSwap(e,t,n,o,i,r=.01){return this.validateWallet(),this.gswapService.executeSwap({fromToken:e,toToken:t,inputAmount:n,estimatedOutput:o,feeTier:i,slippageTolerance:r})}async getSwapUserAssets(e){return this.gswapService.getUserAssets(e)}async getAllSwapUserAssets(e){return this.gswapService.getAllUserAssets(e)}async getSwapPoolInfo(e,t){return this.gswapService.getPoolInfo(e,t)}async getSwapPoolPrice(e,t,n){return this.gswapService.getPositionCurrentPrice({token0:e,token1:t,feeTier:n})}async getSwapUserLiquidityPositions(e,t,n,o){let i,r;"string"==typeof n?(i=n,r=o):"object"==typeof n?r=n:o&&(r=o);return await this.gswapService.getUserLiquidityPositions(e,t,i,r)}async getAllSwapUserLiquidityPositions(e,t){const n=await this.gswapService.getAllSwapUserLiquidityPositions(e,t);if(!t?.withPrices){if(Array.isArray(n))return n;if(n&&"items"in n)return n.items}return n}async getSwapLiquidityPosition(e,t){return this.gswapService.getLiquidityPosition(e,t)}async getSwapLiquidityPositionById(e,t,n,o,i,r,a){return this.gswapService.getLiquidityPositionById(e,t,n,o,i,r,a)}async fetchSwapPositionDirect(e){return this.gswapService.fetchSwapPositionDirect(e)}async getSwapEstimateRemoveLiquidity(e){return this.gswapService.estimateRemoveLiquidity(e)}async addSwapLiquidityByPrice(e){return this.gswapService.addLiquidityByPrice(e)}async addSwapLiquidityByTicks(e){this.validateWallet();const t={...e,fee:e.feeTier??e.fee};return this.gswapService.addSwapLiquidityByTicks(t)}async removeSwapLiquidity(e){return this.validateWallet(),this.gswapService.removeLiquidity(e)}async collectSwapPositionFees(e){return this.validateWallet(),this.gswapService.collectPositionFees(e)}connectWebSocket(){this.websocketService.connect()}disconnectWebSocket(){this.websocketService.disconnect()}isWebSocketConnected(){return this.websocketService.isConnected()}subscribeToEvent(e,t){const n=this.websocketService.getSocket();return n?(n.on(e,t),()=>{n.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 n=1e3,o=Math.max(t?.intervalMs??3e4,n);t?.intervalMs&&t.intervalMs<n&&this.logger.warn(`Poll interval ${t.intervalMs}ms is below minimum 1000ms. Using minimum interval instead.`);const i=t?.minTVL,r=t?.tokens,a=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 n=(e=>`${e.token0}-${e.token1}-${e.fee}`)(t);if(!a.has(n)){if((e=>{if(a.set(e,!0),a.size>1e3){const e=a.keys().next().value;void 0!==e&&a.delete(e)}})(n),i){if((t.token0Tvl+t.token1Tvl)/2<i)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=o*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 n=1e3,o=Math.max(t?.intervalMs??3e4,n);t?.intervalMs&&t.intervalMs<n&&this.logger.warn(`Poll interval ${t.intervalMs}ms is below minimum 1000ms. Using minimum interval instead.`);const i=t?.creatorAddress,r=new Map;let a=!0,s=null;let c=0;const l=async()=>{if(a){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),i&&t.creatorAddress!==i||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(a){const e=Math.min(Math.max(c-1,0),2),t=o*Math.pow(2,e);s=setTimeout(l,t)}}};return l(),()=>{a=!1,s&&clearTimeout(s),this.logger.debug("Stopped watching for launchpad token creation")}}normalizeFee(e){if(null==e)return null;const t="number"==typeof e?e:Number.parseFloat(String(e).replace("%","").trim());return Number.isNaN(t)?null:1===t||1e4===t?1e4:.3===t||3e3===t?3e3:.05===t||500===t?500:!Number.isInteger(t)||500!==t&&3e3!==t&&1e4!==t?null:t}extractField(e,...t){if("object"!=typeof e||null===e)return"";const n=e;for(const e of t)if(n[e])return String(n[e]);return""}looksLikePoolPair(e){if("string"!=typeof e)return null;const t=e.trim();if(!t.includes("/"))return null;const n=t.split("/");return 3!==n.length?null:n[0]&&n[1]&&n[2]?t:null}buildPoolPairFromObject(e){if("object"!=typeof e||null===e)return null;const t=e,n=this.extractField(t,"token0ClassKey","token0Class","token0","token0Symbol")||"",o=this.extractField(t,"token1ClassKey","token1Class","token1","token1Symbol")||"",i=this.normalizeFee(t.feeTier??t.fee??t.feeTierBps??t.liquidityFeeBps??t.feeBps);return n&&o&&null!==i?`${n}/${o}/${i}`:null}parsePoolPairString(e){const t=e.split("/");if(3!==t.length)return null;const n=t[0].split("|")[0],o=t[1].split("|")[0],i=t[2];return n&&o&&i?{token0:n,token1:o,fee:i,poolPair:e}:null}serializeBalanceToken(e){if(!e||"object"!=typeof e)return"";const t=e;return[(t.collection??t.token??"")||"",(t.category??"")||"none",(t.type??"")||"none",(t.additionalKey??"none")||"none"].join("|")}buildPoolPairFromBalances(e){if("object"!=typeof e||null===e)return null;const t=e,n=t.userBalanceDelta??t.balanceDelta??t.delta;if(!n||"object"!=typeof n||null===n)return null;const o=n,i=o.token0Balance??o.token0??o.baseBalance??o.primaryBalance,r=o.token1Balance??o.token1??o.quoteBalance??o.secondaryBalance,a=this.serializeBalanceToken(i),s=this.serializeBalanceToken(r),c=this.normalizeFee(t.poolFee??t.feeTier??t.fee??t.feeTierBps??t.liquidityFeeBps);return a&&s&&null!==c?`${a}/${s}/${c}`:null}extractPoolDataFromPayload(e){if("string"==typeof e){const t=this.looksLikePoolPair(e);return t?this.parsePoolPairString(t):null}if("object"!=typeof e||null===e)return null;const t=e,n=this.looksLikePoolPair(t.poolPair);if(n)return this.parsePoolPairString(n);const o=this.buildPoolPairFromBalances(t);if(o)return this.parsePoolPairString(o);const i=this.buildPoolPairFromObject(t);if(i)return this.parsePoolPairString(i);if(t.pool&&"object"==typeof t.pool&&null!==t.pool){const e=this.extractPoolDataFromPayload(t.pool);if(e)return e}return null}matchesPoolFilter(e,t){if(t?.tokenFilter){if(!(e.token0===t.tokenFilter||e.token1===t.tokenFilter))return!1}if(t?.pairTokens){const[n,o]=t.pairTokens,i=e.token0===n||e.token1===n,r=e.token0===o||e.token1===o;if(!i||!r||n===o)return!1}if(void 0!==t?.feeTierFilter){if(this.normalizeFee(t.feeTierFilter)!==this.normalizeFee(e.fee))return!1}return!0}matchesCreatorFilter(e,t){return!t||e.creatorAddress===t}subscribeToTokenCreations(e,t){if(this.logger.debug("Subscribing to token creation broadcasts"+(t?.creatorFilter?` (filter: ${t.creatorFilter})`:"")),!this.websocketService)throw new Error("WebSocket service not initialized");let n=!1,o=null;const i=(n,...o)=>{try{if(o.length>0&&"object"==typeof o[0]&&null!==o[0]){const n=o[0].data;if(n&&n.Data&&"object"==typeof n.Data){const o=n.Data;if("CreateSale"===o.functionName){const n={tokenName:o.tokenName||"",symbol:o.symbol||"",creatorAddress:o.creatorAddress||"",description:o.description||"",image:o.image||"",vaultAddress:o.vaultAddress||"",tokenStringKey:o.tokenStringKey||"",preBuyQuantity:o.initialBuyQuantity||"0",websiteUrl:o.websiteUrl||"",telegramUrl:o.telegramUrl||"",twitterUrl:o.twitterUrl||"",isFinalized:o.isFinalized||!1};this.matchesCreatorFilter(n,t?.creatorFilter)&&e(n)}}}}catch(e){this.logger.warn(`Error processing token creation broadcast: ${e instanceof Error?e.message:String(e)}`)}};let r=this.websocketService.getSocket();if(r)r.onAny(i),n=!0,this.logger.debug("Token creation broadcast listener registered");else{this.logger.debug("WebSocket not yet connected, initiating connection..."),this.websocketService.connect().catch(e=>{const n=new Error(`WebSocket connection failed: ${e instanceof Error?e.message:String(e)}`);this.logger.warn("Failed to establish WebSocket connection:",e),t?.onError&&t.onError(n)});let e=0;const a=()=>{if(r=this.websocketService.getSocket(),!r&&e<Oi.TOKEN_CREATION_SOCKET_WAIT_ATTEMPTS)return e++,void(o=setTimeout(()=>a(),Oi.TOKEN_CREATION_SOCKET_POLL_INTERVAL_MS));if(!r&&e>=Oi.TOKEN_CREATION_SOCKET_WAIT_ATTEMPTS){const e=new Error(`WebSocket not available after ${Oi.TOKEN_CREATION_SOCKET_WAIT_ATTEMPTS*Oi.TOKEN_CREATION_SOCKET_POLL_INTERVAL_MS}ms`);return this.logger.warn("Token creation broadcast subscription timeout:",e.message),void(t?.onError&&t.onError(e))}r&&(r.onAny(i),n=!0,this.logger.debug("Token creation broadcast listener registered"))};a()}return()=>{try{if(null!==o&&(clearTimeout(o),o=null,this.logger.debug("Cleared token creation broadcast polling timeout")),!n)return void this.logger.debug("Cleanup called before listener registration - no action needed");const e=this.websocketService.getSocket();e&&(e.offAny(i),n=!1,this.logger.debug("Stopped listening to token creation broadcasts"))}catch(e){this.logger.warn("Error removing token creation listener:",e)}}}walkPayloadForPools(e,t,n=new WeakSet){const o=[];if("string"==typeof e){const n=this.looksLikePoolPair(e);if(n){const e=this.parsePoolPairString(n);e&&!t.has(e.poolPair)&&(t.add(e.poolPair),o.push(e))}return o}if("object"!=typeof e||null===e)return o;if(n.has(e))return o;n.add(e);const i=this.extractPoolDataFromPayload(e);i&&!t.has(i.poolPair)&&(t.add(i.poolPair),o.push(i));for(const i of Object.values(e)){const e=this.walkPayloadForPools(i,t,n);o.push(...e)}return o}subscribeToDexPoolAdded(e,t){if(this.logger.debug("Subscribing to DEX pool creation broadcasts"+(t?.tokenFilter?` (filter: ${t.tokenFilter})`:t?.pairTokens?` (pair: ${t.pairTokens.join("/")})`:"")),!this.websocketService)throw new Error("WebSocket service not initialized");let n=!1,o=null;const i=new Set,r=(n,...o)=>{try{for(const n of o){const o=this.walkPayloadForPools(n,i);for(const n of o)this.matchesPoolFilter(n,t)&&e(n)}}catch(e){this.logger.warn(`Error processing DEX pool broadcast: ${e instanceof Error?e.message:String(e)}`)}};let a=this.websocketService.getSocket();if(a)a.onAny(r),n=!0,this.logger.debug("DEX pool broadcast listener registered");else{this.logger.debug("WebSocket not yet connected, initiating connection..."),this.websocketService.connect().catch(e=>{const n=new Error(`WebSocket connection failed: ${e instanceof Error?e.message:String(e)}`);this.logger.warn("Failed to establish WebSocket connection:",e),t?.onError&&t.onError(n)});let e=0;const i=()=>{if(a=this.websocketService.getSocket(),!a&&e<Oi.DEX_POOL_SOCKET_WAIT_ATTEMPTS)return e++,void(o=setTimeout(()=>i(),Oi.DEX_POOL_SOCKET_POLL_INTERVAL_MS));if(!a&&e>=Oi.DEX_POOL_SOCKET_WAIT_ATTEMPTS){const e=new Error(`WebSocket not available after ${Oi.DEX_POOL_SOCKET_WAIT_ATTEMPTS*Oi.DEX_POOL_SOCKET_POLL_INTERVAL_MS}ms`);return this.logger.warn("DEX pool subscription timeout:",e.message),void(t?.onError&&t.onError(e))}a&&(a.onAny(r),n=!0,this.logger.debug("DEX pool broadcast listener registered"))};i()}return()=>{try{if(null!==o&&(clearTimeout(o),o=null,this.logger.debug("Cleared DEX pool polling timeout")),!n)return void this.logger.debug("Cleanup called before listener registration - no action needed");const e=this.websocketService.getSocket();e&&(e.offAny(r),n=!1,this.logger.debug("Stopped listening to DEX pool broadcasts"))}catch(e){this.logger.warn("Error removing DEX pool listener:",e)}}}subscribeToDexSwapExecuted(e,t){if(this.logger.debug("Subscribing to DEX swap execution broadcasts"+(t?.tokenFilter?` (filter: ${t.tokenFilter})`:t?.pairTokens?` (pair: ${t.pairTokens.join("/")})`:"")),!this.websocketService)throw new Error("WebSocket service not initialized");let n=null,o=null,i=null,r=!1;const a=async e=>{const t=si.parsePoolKey(e);if(!t)throw new Error(`Invalid pool key format: ${e}`);return await this.dexQuoteService.fetchCompositePoolData({token0:t.token0,token1:t.token1,fee:t.feeTier})},s=()=>{const c=this.websocketService.getSocket();if(!c)return this.logger.debug("WebSocket not yet ready for swap monitoring, polling..."),void(n=setTimeout(()=>s(),100));r=!0,o=new pi(c,a,this.dexQuoteService,t||{},this.logger),i=o.subscribe(t||{},e),this.logger.debug("DEX swap monitoring subscription established")};return this.websocketService.getSocket()||(this.logger.debug("WebSocket not yet connected, initiating connection for swap monitoring..."),this.websocketService.connect().catch(e=>{const n=new Error(`WebSocket connection failed for swap monitoring: ${e instanceof Error?e.message:String(e)}`);this.logger.warn("Failed to establish WebSocket connection:",e),t?.onError&&t.onError(n)})),s(),()=>{try{n&&clearTimeout(n),i&&r&&i(),o&&o.shutdown().catch(e=>{this.logger.warn("Error shutting down swap monitor:",e)})}catch(e){this.logger.warn("Error cleaning up swap monitor:",e)}}}subscribeToDexLiquidityAdded(e,t){return this.subscribeToDexLiquidityEvents(e,t)}subscribeToDexLiquidityRemoved(e,t){return this.subscribeToDexLiquidityEvents(e,t)}subscribeToDexLiquidityChanged(e,t){return this.subscribeToDexLiquidityEvents(e,t)}subscribeToDexLiquidityEvents(e,t){if(this.logger.debug("Subscribing to DEX liquidity broadcasts"+(t?.tokenFilter?` (filter: ${t.tokenFilter})`:t?.pairTokens?` (pair: ${t.pairTokens.join("/")})`:"")),!this.websocketService)throw new Error("WebSocket service not initialized");let n=!1,o=null;const i=new Set,r=new Li(this.logger),a=(n,...o)=>{try{for(const n of o){const o=r.walkPayloadForLiquidityEvents(n,i);for(const n of o)if(this.matchesLiquidityFilter(n,t))try{const t=e(n);t instanceof Promise&&t.catch(e=>{this.logger.warn(`Error in liquidity event callback: ${e instanceof Error?e.message:String(e)}`)})}catch(e){this.logger.warn(`Error in liquidity event callback: ${e instanceof Error?e.message:String(e)}`)}}}catch(e){this.logger.warn(`Error processing DEX liquidity broadcast: ${e instanceof Error?e.message:String(e)}`)}};let s=this.websocketService.getSocket();if(s)s.onAny(a),n=!0,this.logger.debug("DEX liquidity broadcast listener registered");else{this.logger.debug("WebSocket not yet connected, initiating connection for liquidity monitoring..."),this.websocketService.connect().catch(e=>{const n=new Error(`WebSocket connection failed: ${e instanceof Error?e.message:String(e)}`);this.logger.warn("Failed to establish WebSocket connection:",e),t?.onError&&t.onError(n)});let e=0;const i=()=>{if(s=this.websocketService.getSocket(),!s&&e<Oi.DEX_POOL_SOCKET_WAIT_ATTEMPTS)return e++,void(o=setTimeout(()=>i(),Oi.DEX_POOL_SOCKET_POLL_INTERVAL_MS));if(!s&&e>=Oi.DEX_POOL_SOCKET_WAIT_ATTEMPTS){const e=new Error(`WebSocket not available after ${Oi.DEX_POOL_SOCKET_WAIT_ATTEMPTS*Oi.DEX_POOL_SOCKET_POLL_INTERVAL_MS}ms`);return this.logger.warn("DEX liquidity subscription timeout:",e.message),void(t?.onError&&t.onError(e))}s&&(s.onAny(a),n=!0,this.logger.debug("DEX liquidity broadcast listener registered"))};i()}return async()=>{try{if(null!==o&&(clearTimeout(o),o=null,this.logger.debug("Cleared DEX liquidity polling timeout")),!n)return void this.logger.debug("Cleanup called before listener registration - no action needed");const e=this.websocketService.getSocket();e&&(e.offAny(a),n=!1,this.logger.debug("Stopped listening to DEX liquidity broadcasts"))}catch(e){this.logger.warn("Error removing DEX liquidity listener:",e)}}}matchesLiquidityFilter(e,t){if(!t)return!0;if(t.positionId&&e.positionId!==t.positionId)return!1;if(t.poolHash&&e.poolHash!==t.poolHash)return!1;if(void 0!==t.feeTierFilter){const n=this.normalizeFeeTier(t.feeTierFilter);if(e.poolFee!==n)return!1}if(t.userFilter&&e.userAddress!==t.userFilter)return!1;if(t.tokenFilter){const n=e.token0?.toLowerCase().includes(t.tokenFilter.toLowerCase()),o=e.token1?.toLowerCase().includes(t.tokenFilter.toLowerCase());if(!n&&!o)return!1}if(t.pairTokens){const[n,o]=t.pairTokens.map(e=>e.toLowerCase()),i=e.token0?.toLowerCase()||"",r=e.token1?.toLowerCase()||"",a=i.includes(n)&&r.includes(o),s=i.includes(o)&&r.includes(n);if(!a&&!s)return!1}if(t.minAmount){const n=parseFloat(t.minAmount),o=parseFloat(e.amounts[0])||0,i=parseFloat(e.amounts[1])||0;if(Math.abs(o)<n&&Math.abs(i)<n)return!1}return!0}normalizeFeeTier(e){if("number"==typeof e)return e>=100?e:Math.round(1e4*e);const t=e.replace("%","").trim(),n=parseFloat(t);return n>=100?n:Math.round(1e4*n)}async cleanup(){try{this.logger.debug("Starting cleanup..."),this.http.cleanup(),this.websocketService&&this.websocketService.disconnect(),this.logger.debug("Cleanup completed")}catch(e){this.logger.error("Error during cleanup:",e)}}static cleanupAll(e=!1){const t=new S({debug:e,context:"LaunchpadSDK"});t.debug("Starting global cleanup...");const{WebSocketService:n}=require("./services/WebSocketService");n.cleanupAll(e),t.debug("Global cleanup completed")}}Oi.DEFAULT_SLIPPAGE_TOLERANCE_FACTOR=.15,Oi.DEFAULT_MAX_ACCEPTABLE_REVERSE_BONDING_CURVE_FEE_SLIPPAGE_FACTOR=.01,Oi.DEFAULT_LAUNCHPAD_TOKEN_MAX_SUPPLY=ti.DEFAULT_LAUNCHPAD_TOKEN_MAX_SUPPLY,Oi.DEFAULT_CALCULATE_AMOUNT_MODE=Ai.LOCAL,Oi.TOKEN_CREATION_SOCKET_WAIT_ATTEMPTS=30,Oi.TOKEN_CREATION_SOCKET_POLL_INTERVAL_MS=100,Oi.DEX_POOL_SOCKET_WAIT_ATTEMPTS=30,Oi.DEX_POOL_SOCKET_POLL_INTERVAL_MS=100;class _i{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),o=new n.Wallet(t),i=this.toGalaAddress(o.address);return{privateKey:o.privateKey,address:o.address,galaAddress:i,mnemonic:"test test test test test test test test test test test junk",wallet:o}}throw e}}static fromPrivateKey(e){const t=new n.Wallet(e),o=this.toGalaAddress(t.address);return{privateKey:t.privateKey,address:t.address,galaAddress:o,mnemonic:"",wallet:t}}static fromMnemonic(e,t=0){try{const o=n.Mnemonic.fromPhrase(e),i=n.HDNodeWallet.fromMnemonic(o,`m/44'/60'/0'/0/${t}`),r=new n.Wallet(i.privateKey),a=this.toGalaAddress(r.address);return{privateKey:r.privateKey,address:r.address,galaAddress:a,mnemonic:e,wallet:r}}catch(o){if("undefined"!=typeof process&&"test"===process.env.NODE_ENV){const o=`test-mnemonic-index-${t}-${e}`,i="0x"+Buffer.from(o).toString("hex").padStart(64,"1").slice(0,64),r=new n.Wallet(i),a=this.toGalaAddress(r.address);return{privateKey:r.privateKey,address:r.address,galaAddress:a,mnemonic:e,wallet:r}}throw o}}static toGalaAddress(e){const t=e.replace(/^0x/i,"");if(!/^[a-fA-F0-9]{40}$/.test(t))throw new Error(`Invalid Ethereum address format: ${e}`);return`eth|${t}`}static toEthereumAddress(e){if(!e.startsWith("eth|"))throw new Error(`Invalid Gala address format: ${e}. Must start with 'eth|'`);const t=e.slice(4);if(!/^[a-fA-F0-9]{40}$/.test(t))throw new Error(`Invalid address in Gala format: ${e}`);return`0x${t}`}static isValidEthereumAddress(e){try{const t=e.replace(/^0x/i,"");return/^[a-fA-F0-9]{40}$/.test(t)}catch{return!1}}static isValidGalaAddress(e){try{if(!e.startsWith("eth|"))return!1;const t=e.slice(4);return/^[a-fA-F0-9]{40}$/.test(t)}catch{return!1}}static generateMultipleWallets(e=1){if(e<1||e>100)throw new Error("Count must be between 1 and 100");const t=[];if("undefined"!=typeof process&&"test"===process.env.NODE_ENV)for(let n=0;n<e;n++){const e=`test-multi-${n}-${Date.now()}-${++this.testCounter}`,o="0x"+Buffer.from(e).toString("hex").padStart(64,"1").slice(0,64);t.push(this.fromPrivateKey(o))}else for(let n=0;n<e;n++)t.push(this.generateWallet());return t}static getWalletSummary(e,t=!1){const n=["🔐 Wallet Information","═".repeat(50),`📍 Address: ${e.address}`,`🎮 Gala Address: ${e.galaAddress}`,`🌱 Mnemonic: ${e.mnemonic||"Not available"}`];return t?n.splice(3,0,`🔑 Private Key: ${e.privateKey}`):n.splice(3,0,"🔑 Private Key: [HIDDEN - use includeSensitive=true to show]"),n.push("═".repeat(50)),n.push("💾 IMPORTANT: Save your mnemonic phrase securely!"),n.push("This is your backup to recover the wallet."),n.join("\n")}}function Bi(e){if(void 0===e)return _i.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 _i.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 _i.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?"...":""}"`)}_i.testCounter=0;class Ui{static fastValidation(e,t,n,o=Ui.DEFAULT_CONFIG){const i=Date.now();let a=!0;try{const s=new r(e.sqrtPrice),c=new r(t.sqrtPrice);n.zeroForOne?c.gte(s)&&(this.logger.error("Fast validation failed: price did not decrease for zeroForOne swap",{originalSqrtPrice:s.toString(),updatedSqrtPrice:c.toString(),zeroForOne:n.zeroForOne}),a=!1):c.lte(s)&&(this.logger.error("Fast validation failed: price did not increase for oneForZero swap",{originalSqrtPrice:s.toString(),updatedSqrtPrice:c.toString(),zeroForOne:n.zeroForOne}),a=!1);const l=new r(e.liquidity),u=new r(t.liquidity);if(!l.isZero()){const e=u.minus(l).abs().div(l);e.gt(o.maxLiquidityChangePct)&&this.logger.warn("Fast validation warning: large liquidity change detected (could be legitimate)",{originalLiquidity:l.toString(),updatedLiquidity:u.toString(),changePct:e.times(100).toFixed(2)})}const d=new r(e.feeGrowthGlobal0),h=new r(t.feeGrowthGlobal0),g=new r(e.feeGrowthGlobal1),p=new r(t.feeGrowthGlobal1);n.zeroForOne?p.lt(g)&&(this.logger.error("Fast validation failed: feeGrowthGlobal1 decreased for zeroForOne",{originalFeeGrowth1:g.toString(),updatedFeeGrowth1:p.toString()}),a=!1):h.lt(d)&&(this.logger.error("Fast validation failed: feeGrowthGlobal0 decreased for oneForZero",{originalFeeGrowth0:d.toString(),updatedFeeGrowth0:h.toString()}),a=!1);const m=new r(e.protocolFeesToken0),f=new r(t.protocolFeesToken0),y=new r(e.protocolFeesToken1),w=new r(t.protocolFeesToken1);f.lt(m)&&(this.logger.error("Fast validation failed: protocolFeesToken0 decreased",{originalProtocolFees0:m.toString(),updatedProtocolFees0:f.toString()}),a=!1),w.lt(y)&&(this.logger.error("Fast validation failed: protocolFeesToken1 decreased",{originalProtocolFees1:y.toString(),updatedProtocolFees1:w.toString()}),a=!1);const k=Date.now()-i;return this.logger.debug("Fast validation completed",{passed:a,elapsedMs:k}),a}catch(e){return this.logger.error("Fast validation exception",e),!1}}static moderateValidation(e,t,n=Ui.DEFAULT_CONFIG){const o=Date.now(),i=[];let s=0;try{if(t.actualSqrtPrice){const o=new r(e.sqrtPrice),a=new r(t.actualSqrtPrice),c=this.calculateDriftPercentage(o,a);s=c,c>100*n.maxDriftThreshold&&i.push(`Price drift detected: ${c.toFixed(4)}% (threshold: ${(100*n.maxDriftThreshold).toFixed(4)}%)`),this.logger.debug("Price drift comparison",{calculatedSqrtPrice:o.toString(),actualSqrtPrice:a.toString(),driftPct:c.toFixed(4)})}const c=new r(e.sqrtPrice),l=new r(2).pow(96),u=c.dividedBy(l),d=a.sqrtPriceToTick(u),h=e.tick??0,g=Math.abs(d-h);g>n.maxTickDrift&&i.push(`Tick/price mismatch: tick=${h}, calculated=${d}, drift=${g}`);const p=new r(e.feeGrowthGlobal0),m=new r(e.feeGrowthGlobal1),f=new r(e.liquidity);try{to(p,m,f)}catch(e){i.push(e.message)}const y=0===i.length,w=!y||s>100*n.maxDriftThreshold,k=Date.now()-o;return this.logger.debug("Moderate validation completed",{isValid:y,shouldRefetch:w,driftPercentage:s,errorCount:i.length,elapsedMs:k}),this.buildValidationResult(y,s,w,i)}catch(e){this.logger.error("Moderate validation exception",e);const t=e instanceof Error?e.message:String(e);return this.buildValidationResult(!1,0,!0,[`Exception during validation: ${t}`])}}static async fullValidation(e,t,n){const o=Date.now(),i=[];let a=0;try{this.logger.debug("Starting full validation with fresh pool data fetch",{poolKey:e});const s=await n(),c=new r(t.pool.sqrtPrice),l=new r(s.pool.sqrtPrice),u=this.calculateDriftPercentage(c,l);a=Math.max(a,u),u>100*this.DEFAULT_CONFIG.maxPriceDriftPct&&i.push(`Price drift: ${u.toFixed(4)}% (cached: ${c.toString()}, fresh: ${l.toString()})`);const d=new r(t.pool.liquidity),h=new r(s.pool.liquidity),g=this.calculateDriftPercentage(d,h);a=Math.max(a,g),g>100*this.DEFAULT_CONFIG.maxLiquidityDriftPct&&i.push(`Liquidity drift: ${g.toFixed(4)}% (cached: ${d.toString()}, fresh: ${h.toString()})`);const p=Object.keys(t.tickDataMap).length,m=Object.keys(s.tickDataMap).length;if(m>0){const e=Math.abs(m-p)/m;e>this.DEFAULT_CONFIG.maxTickCountDriftPct&&i.push(`Tick data incomplete: cached has ${p} ticks, fresh has ${m} ticks (${(100*e).toFixed(2)}% difference)`)}const f=0===i.length,y=!f,w=Date.now()-o;return this.logger.debug("Full validation completed",{poolKey:e,isValid:f,shouldRefetch:y,maxDriftPercentage:a,priceDrift:u,liquidityDrift:g,cachedTickCount:p,freshTickCount:m,errorCount:i.length,elapsedMs:w}),this.buildValidationResult(f,a,y,i)}catch(t){this.logger.error("Full validation exception",{poolKey:e,error:t});const n=t instanceof Error?t.message:String(t);return this.buildValidationResult(!1,0,!0,[`Exception during full validation: ${n}`])}}static calculateDriftPercentage(e,t){if(t.isZero())return this.logger.warn("Cannot calculate drift: actual value is zero"),1/0;return t.minus(e).abs().div(t).times(100).toNumber()}static buildValidationResult(e,t,n,o=[]){let i;if(n&&o.length>0){const e=o.join(" ").toLowerCase();i=e.includes("drift")?"drift":e.includes("tick")&&e.includes("mismatch")?"tick-mismatch":e.includes("tick")?"missing-tick-data":"manual"}const r={isValid:e,driftPercentage:t,shouldRefetch:n,validationErrors:o};return void 0!==i&&(r.refetchReason=i),r}}Ui.logger=new S({debug:!1,context:"PoolStateValidator"}),Ui.DEFAULT_CONFIG={maxDriftThreshold:.001,maxLiquidityChangePct:.5,maxTickDrift:1,maxTickCountDriftPct:.1,maxPriceDriftPct:.001,maxLiquidityDriftPct:.01};class Mi{static calculatePoolStateHash(e){const t=`${e.sqrtPrice.toString()}|${e.liquidity.toString()}|${e.tick||0}`;return g.createHash("sha256").update(t).digest("hex").substring(0,16)}constructor(e,t){this.logger=new S({debug:t?.debug??!1,context:"PoolStateManager"});const n={maxIterations:t?.maxIterations??100,enableBigNumberCache:t?.enableBigNumberCache??!0,roundingMode:t?.roundingMode??r.ROUND_DOWN,debug:t?.debug??!1,maxSwapsSinceRefetch:t?.maxSwapsSinceRefetch??50,maxCumulativeDrift:t?.maxCumulativeDrift??5,strictValidation:t?.strictValidation??!1,enablePerformanceWarnings:t?.enablePerformanceWarnings??!0,performanceWarningThreshold:t?.performanceWarningThreshold??100};this.config={...n,...t?.onRefetchNeeded?{onRefetchNeeded:t.onRefetchNeeded}:{}},this.validationConfig=Ui.DEFAULT_CONFIG,this.pool=e.pool,this.tickDataMap=e.tickDataMap,this.metadata={lastFullRefetch:Date.now(),swapsSinceRefetch:0,cumulativeDrift:0,processedTransactions:[],stateHash:Mi.calculatePoolStateHash(this.pool)};if(!Ui.fastValidation(this.pool,this.pool,{transactionId:"init",timestamp:Date.now(),amountSpecified:"0",zeroForOne:!1,exactInput:!0},this.validationConfig)&&this.config.strictValidation)throw new Error("Initial pool state validation failed");this.logger.info("PoolStateManager initialized",{pool:{sqrtPrice:this.pool.sqrtPrice.toString(),liquidity:this.pool.liquidity.toString(),tick:this.pool.tick},config:this.config})}async applySwapDelta(e){const t=Date.now();if(this.metadata.processedTransactions.includes(e.transactionId))throw this.logger.warn("Duplicate swap transaction",{transactionId:e.transactionId}),new Error(`Duplicate transaction ID: ${e.transactionId}`);try{const t={pool:this.pool,tickDataMap:this.tickDataMap},n={maxIterations:this.config.maxIterations,enableBigNumberCache:this.config.enableBigNumberCache,roundingMode:this.config.roundingMode,debugLogging:this.config.debug},o=di.calculateSwapDelta(t,e,n);this.lastSwapMetrics={calculationTimeMs:o.metadata.calculationTimeMs,swapSteps:o.metadata.swapSteps,timestamp:Date.now()},this.config.enablePerformanceWarnings&&o.metadata.calculationTimeMs>this.config.performanceWarningThreshold&&this.logger.warn("Slow swap calculation",{calculationTimeMs:o.metadata.calculationTimeMs,swapSteps:o.metadata.swapSteps,threshold:this.config.performanceWarningThreshold});if(!Ui.fastValidation(t.pool,o.updatedPool,e,this.validationConfig)&&this.config.strictValidation)throw new Error("Swap validation failed");if(e.actualAmount0&&e.actualAmount1&&e.actualSqrtPrice){const t=new r(e.actualAmount0),n=new r(e.actualAmount1),i=o.amount0.minus(t).abs(),a=o.amount1.minus(n).abs(),s=t.isZero()?0:i.div(t.abs()).times(100).toNumber(),c=n.isZero()?0:a.div(n.abs()).times(100).toNumber(),l=Math.max(s,c);l>1&&(this.logger.warn("Drift detected in swap delta",{driftPercentage:l.toFixed(2),swapId:e.transactionId}),this.metadata.cumulativeDrift+=l)}if((this.metadata.swapsSinceRefetch>this.config.maxSwapsSinceRefetch||this.metadata.cumulativeDrift>this.config.maxCumulativeDrift)&&(this.logger.info("Triggering full refetch due to drift accumulation",{swapsSinceRefetch:this.metadata.swapsSinceRefetch,cumulativeDrift:this.metadata.cumulativeDrift.toFixed(2)}),this.config.onRefetchNeeded)){const e=await this.config.onRefetchNeeded(this.pool,this.tickDataMap);this.pool=e.pool,this.tickDataMap=e.tickDataMap,this.metadata.lastFullRefetch=Date.now(),this.metadata.swapsSinceRefetch=0,this.metadata.cumulativeDrift=0,this.metadata.stateHash=Mi.calculatePoolStateHash(this.pool),this.logger.info("Full refetch completed",{sqrtPrice:this.pool.sqrtPrice.toString()})}return this.pool=o.updatedPool,Object.assign(this.tickDataMap,o.updatedTicks),this.metadata.swapsSinceRefetch++,this.metadata.stateHash=Mi.calculatePoolStateHash(this.pool),this.metadata.processedTransactions.push(e.transactionId),this.metadata.processedTransactions.length>1e3&&(this.metadata.processedTransactions=this.metadata.processedTransactions.slice(-1e3)),this.logger.debug("Swap delta applied",{transactionId:e.transactionId,amount0:o.amount0.toString(),amount1:o.amount1.toString(),sqrtPriceNew:this.pool.sqrtPrice.toString()}),o}catch(n){const o=n instanceof Error?n.message:String(n);if(this.logger.error("Failed to apply swap delta",{transactionId:e.transactionId,error:o}),this.config.strictValidation)throw n;return{updatedPool:this.pool,updatedTicks:{},amount0:new r(0),amount1:new r(0),feeAmount0:new r(0),feeAmount1:new r(0),ticksCrossed:[],metadata:{calculationTimeMs:Date.now()-t,swapSteps:0,priceHitLimit:!1}}}}forceFullRefetch(e){this.pool=e.pool,this.tickDataMap=e.tickDataMap,this.metadata.lastFullRefetch=Date.now(),this.metadata.swapsSinceRefetch=0,this.metadata.cumulativeDrift=0,this.logger.info("Full refetch forced",{sqrtPrice:this.pool.sqrtPrice.toString()})}getPoolState(){return this.pool}getTickDataMap(){return{...this.tickDataMap}}getMetadata(){return{...this.metadata}}getLastSwapMetrics(){if(this.lastSwapMetrics)return{...this.lastSwapMetrics}}isRefetchRecommended(){return this.metadata.swapsSinceRefetch>this.config.maxSwapsSinceRefetch||this.metadata.cumulativeDrift>this.config.maxCumulativeDrift}reset(e){this.pool=e.pool,this.tickDataMap=e.tickDataMap,this.metadata={lastFullRefetch:Date.now(),swapsSinceRefetch:0,cumulativeDrift:0,processedTransactions:[],stateHash:Mi.calculatePoolStateHash(this.pool)},this.logger.info("PoolStateManager reset",{sqrtPrice:this.pool.sqrtPrice.toString()})}}"undefined"!=typeof process&&process.env&&(process.env.CORE_CHAINCODE_LOGGING_LEVEL=process.env.CORE_CHAINCODE_LOGGING_LEVEL||"ERROR");var Ri=Object.freeze({__proto__:null,extractMetadataFromPoolData:function(e,t){const n={};if(e.vaultAddress&&(n.vaultAddress=e.vaultAddress),void 0!==e.reverseBondingCurveMinFeePortion){const o=parseFloat(e.reverseBondingCurveMinFeePortion);isNaN(o)?t&&t.debug(`Skipping invalid reverseBondingCurveMinFeePortion for ${e.tokenName}: "${e.reverseBondingCurveMinFeePortion}"`):n.reverseBondingCurveMinFeeFactor=o}if(void 0!==e.reverseBondingCurveMaxFeePortion){const o=parseFloat(e.reverseBondingCurveMaxFeePortion);isNaN(o)?t&&t.debug(`Skipping invalid reverseBondingCurveMaxFeePortion for ${e.tokenName}: "${e.reverseBondingCurveMaxFeePortion}"`):n.reverseBondingCurveMaxFeeFactor=o}return void 0!==n.reverseBondingCurveMaxFeeFactor&&void 0!==n.reverseBondingCurveMinFeeFactor&&(n.reverseBondingCurveNetFeeFactor=n.reverseBondingCurveMaxFeeFactor-n.reverseBondingCurveMinFeeFactor),Object.keys(n).length>0?n: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}});e.AgentConfig=class{static async quickSetup(e={}){const t=e.environment||this.detectEnvironment(),n=this.setupWallet(e.privateKey),o={wallet:n.wallet,baseUrl:e.baseUrl||this.getDefaultBaseUrl(t),timeout:e.timeout||this.getDefaultTimeout(t),debug:e.debug??"production"!==t,...this.getEnvironmentDefaults(t),...e.config||{}},i=new Oi(o),r={sdk:i,wallet:n,config:o};if(!1!==e.autoValidate){const e=await this.validateSetup(i,n);return{...r,validation:e}}return r}static async readOnlySetup(e={}){const t=e.environment||this.detectEnvironment(),n={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 Oi(n),config:n}}static async validateSetup(e,t){const n=[],o=[],i={canTrade:!1,canCreateTokens:!1,hasBalance:!1,connectionHealthy:!1};try{const t=await e.fetchGalaBalance(e.getAddress());if(i.connectionHealthy=!0,t&&t.quantity){const e=parseFloat(t.quantity);i.hasBalance=e>0,i.canTrade=e>=.1,i.canCreateTokens=e>=100,0===e?o.push("Wallet has zero GALA balance - cannot perform transactions"):e<.1?o.push("GALA balance too low for trading (minimum 0.1 GALA)"):e<100&&o.push("GALA balance too low for token creation (minimum 100 GALA)")}else n.push("Failed to fetch GALA balance: No balance returned")}catch(e){n.push(`Balance check error: ${e instanceof Error?e.message:String(e)}`)}try{const t=await e.fetchPools({type:"recent",page:1,limit:1});t.pools&&0!==t.pools.length||o.push("Pool listing not accessible - some features may be limited")}catch(e){o.push(`Pool access test failed: ${e instanceof Error?e.message:String(e)}`)}return{ready:0===n.length&&i.connectionHealthy,sdk:e,wallet:t||_i.generateWallet(),issues:n,warnings:o,capabilities:i}}static getRecommendedConfig(e,t="general"){const n={environment:e,autoValidate:!0};switch(e){case"production":Object.assign(n,{debug:!1,timeout:3e4});break;case"development":Object.assign(n,{debug:!0,timeout:45e3});break;case"testing":Object.assign(n,{debug:!0,timeout:6e4})}switch(t){case"trading":n.timeout=1.5*(n.timeout||3e4);break;case"creation":n.timeout=2*(n.timeout||3e4);break;case"monitoring":n.timeout=.5*(n.timeout||3e4)}return n}static async multiWalletSetup(e,t="development"){const n={};for(const[o,i]of Object.entries(e)){const{sdk:e}=await this.quickSetup({environment:t,privateKey:i,agentId:`multi-wallet-${o}`,autoValidate:!1});n[o]=e}return n}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?_i.fromPrivateKey(e):_i.generateWallet()}return"generate"===e?_i.generateWallet():_i.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}},e.CALCULATION_MODES=Ai,e.COMPOSITE_POOL_FETCH_CONCURRENCY=5,e.ConfigurationError=D,e.DexPoolNotFoundError=B,e.DexQuoteError=_,e.FileValidationError=Gn,e.GALA_DECIMALS=8,e.GALA_TOKEN_CLASS_KEY={collection:"GALA",category:"Unit",type:"none",additionalKey:"none"},e.GSwapAssetError=L,e.GSwapPoolError=$,e.GSwapQuoteError=F,e.GSwapSwapError=x,e.IMAGE_EXTENSIONS=_e,e.LAUNCHPAD_TOKEN_DECIMALS=18,e.LaunchpadSDK=Oi,e.LockError=vo,e.MAX_CONCURRENT_POOL_FETCHES=5,e.NetworkError=N,e.PAGINATION_DEFAULTS=kn,e.POOL_FETCH_CONFIG={MAX_CONCURRENT_FETCHES:5,BACKEND_PAGE_SIZE:20},e.POOL_TYPES={RECENT:"recent",POPULAR:"popular"},e.PoolStateManager=Mi,e.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"},e.SDK_VERSION=$i,e.TRADING_TYPES=bi,e.TokenMetadataService=class extends Tn{constructor(e=!1){super(e),this.cache={},this.cacheExpiry=36e5}async resolveTokenMetadata(e){const t=this.getCacheKey(e),n=this.cache[t];if(n&&!this.isCacheExpired(n.timestamp))return this.logger.debug(`Using cached metadata for token: ${t}`),n.data;const o=this.extractMetadata(e);return this.cache[t]={data:o,timestamp:Date.now()},o}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,n,o="Token",i="Unit",r="none";if("string"==typeof e)if(e.includes("|")){const a=e.split("|");"Token"===a[0]&&a[2]?(o=a[0],i=a[1]||"Unit",t=a[2],r=a[3]||"none",n=t):(t=a[0],n=t,i=a[1]||"Unit",r=a[3]||"none")}else t=e,n=e;else t=e.type||"unknown",o=e.collection||"Token",i=e.category||"Unit",r=e.additionalKey||"none",n=e.symbol||("Token"===o?t:o)||"unknown";const a=this.getDecimalsForToken(n);return{symbol:n.toUpperCase(),decimals:a,collection:o,category:i,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`)}},e.TransactionError=C,e.TransactionFailedError=Ci,e.ValidationError=P,e.WebSocketError=Di,e.WebSocketTimeoutError=class extends Di{constructor(e,t){super(`WebSocket confirmation timeout for transaction ${e} after ${t}ms`),this.name="WebSocketTimeoutError"}},e.addressFormatSchema=te,e.amountMethodSchema=$e,e.amountTypeSchema=xe,e.browserFileSchema=Ue,e.bufferFileSchema=Me,e.buyTokensDataSchema=st,e.calculatePreMintDataSchema=pt,e.checkPoolOptionsSchema=Fe,e.commentMessageSchema=we,e.commentPaginationSchema=je,e.createLaunchpadSDK=function(e){e||(e={});const{wallet:t,env:o,config:i={},...r}=e,a={...r,...i},{wallet:s,env:c,config:l,...u}=a;let d;if(t)if("string"==typeof t){d=Bi(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=Bi().wallet}const h={wallet:d,...o&&{env:o},debug:!1,timeout:3e4,...u};return new Oi(h)},e.createLimitSchema=de,e.createPaginatedResultSchema=function(e){return i.z.object({data:i.z.array(e),page:i.z.number().int().min(1),limit:i.z.number().int().min(1),total:i.z.number().int().min(0),totalPages:i.z.number().int().min(0),hasNext:i.z.boolean(),hasPrevious:i.z.boolean()})},e.createPoolStateManager=function(e,t){return new Mi(e,t)},e.createTradeDataSchema=at,e.createWallet=Bi,e.ethereumAddressSchema=ne,e.faucetAmountSchema=se,e.fetchGalaBalanceOptionsSchema=et,e.fetchPoolDetailsDataSchema=mt,e.fetchTokenBalanceOptionsSchema=ot,e.fileSizeSchema=me,e.fileUploadSchema=Be,e.filenameSchema=fe,e.filterByFeeTier=function(e,t){return e.filter(e=>e.feeTier===t)},e.filterByLiquidity=function(e){return e.filter(e=>new r(e.liquidity).isGreaterThan(0))},e.filterByMinLiquidity=function(e,t){const n=new r(t);return e.filter(e=>new r(e.liquidity).isGreaterThanOrEqualTo(n))},e.filterByPoolKey=function(e,t,n,o){const i=t.toUpperCase(),r=n.toUpperCase();return e.filter(e=>{const t=e.token0.toUpperCase(),n=e.token1.toUpperCase();return(t===i&&n===r||t===r&&n===i)&&e.feeTier===o})},e.filterByToken=function(e,t){const n=t.toUpperCase();return e.filter(e=>e.token0.toUpperCase()===n||e.token1.toUpperCase()===n)},e.filterByTokenPair=function(e,t,n){const o=t.toUpperCase(),i=n.toUpperCase();return e.filter(e=>{const t=e.token0.toUpperCase(),n=e.token1.toUpperCase();return t===o&&n===i||t===i&&n===o})},e.flexibleAddressSchema=oe,e.flexibleFileSchema=Re,e.formatGalaForDTO=Po,e.formatLaunchpadTokenForDTO=No,e.fullNameSchema=ee,e.getAmountOptionsSchema=gt,e.getEnv=function(e,t){return process.env[e]??t},e.getEnvOrThrow=function(e,t){const n=process.env[e];if(!n){throw new Error(t?`${e} not set in root or local .env (${t})`:`${e} not set in root or local .env`)}return n},e.getTradeOptionsSchema=lt,e.graduateTokenOptionsSchema=Oe,e.graphDataOptionsSchema=Le,e.groupByFeeTier=function(e){const t=new Map;return e.forEach(e=>{t.has(e.feeTier)||t.set(e.feeTier,[]),t.get(e.feeTier).push(e)}),t},e.groupByPoolKey=function(e){const t=new Map;return e.forEach(e=>{const n=`${e.token0.toUpperCase()}|${e.token1.toUpperCase()}|${e.feeTier}`;t.has(n)||t.set(n,[]),t.get(n).push(e)}),t},e.groupByTokenPair=function(e){const t=new Map;return e.forEach(e=>{const n=`${e.token0.toUpperCase()}/${e.token1.toUpperCase()}`;t.has(n)||t.set(n,[]),t.get(n).push(e)}),t},e.imageExtensionSchema=qe,e.imageFilenameSchema=Ke,e.imageMimeTypeSchema=ye,e.imageUploadOptionsSchema=De,e.isLockTokenData=wo,e.isUnlockTokenData=ko,e.isoDateStringSchema=ke,e.launchTokenDataSchema=Ne,e.loadEnvWithFallback=function(){const e=y.join(process.cwd(),"..","..",".env");w.existsSync(e)&&k.config({path:e});const t=y.join(process.cwd(),".env");w.existsSync(t)&&k.config({path:t})},e.nonNegativeDecimalStringSchema=ae,e.optionalUrlSchema=le,e.pageNumberSchema=ue,e.paginationResultMetaSchema=Xe,e.poolFetchTypeSchema=Ce,e.poolPaginationSchema=He,e.positiveDecimalStringSchema=re,e.privateKeySchema=Te,e.requireNonNegative=to,e.requirePositive=function(...e){e.forEach((e,t)=>{if(e.isNaN())throw new Error(`Value at index ${t} must be a valid number, got: NaN`);if(!e.isFinite())throw new Error(`Value at index ${t} must be finite, got: ${e.toString()}`);if(e.isLessThanOrEqualTo(0))throw new Error(`Value at index ${t} must be positive, got: ${e.toString()}`)})},e.requirePositiveWithContext=no,e.reverseBondingCurveConfigSchema=Pe,e.reverseBondingCurveConfigurationSchema=ft,e.searchQuerySchema=J,e.sellTokensDataSchema=ct,e.sortByLiquidity=function(e,t="desc"){return[...e].sort((e,n)=>{const o=new r(e.liquidity),i=new r(n.liquidity);return"desc"===t?i.minus(o).toNumber():o.minus(i).toNumber()})},e.standardLimitSchema=he,e.standardPaginationSchema=Ge,e.timestampSchema=ve,e.tokenCategorySchema=Ee,e.tokenCollectionSchema=Ie,e.tokenDescriptionSchema=Z,e.tokenListOptionsSchema=Ye,e.tokenNameSchema=Q,e.tokenSymbolSchema=X,e.tokenUrlsSchema=Ae,e.tradeCalculationMethodSchema=ht,e.tradeCalculationTypeSchema=dt,e.tradeLimitSchema=pe,e.tradeListParamsSchema=ut,e.tradePaginationSchema=We,e.tradePaginationWithFiltersSchema=Qe,e.tradeTypeBackendSchema=rt,e.tradeTypeSchema=it,e.transactionIdSchema=Se,e.transferFaucetsDataSchema=Je,e.uniqueKeySchema=be,e.updateProfileDataSchema=tt,e.uploadProfileImageOptionsSchema=nt,e.urlSchema=ce,e.userLimitSchema=ge,e.userPaginationSchema=ze,e.userTokenNameSchema=Y,e.userTokenTypeSchema=Ze,e.userTokensPaginationSchema=Ve,e.validateAddress=Tt,e.validateAmountString=bt,e.validateBuyTokensData=Mt,e.validateCalculatePreMintData=zt,e.validateCheckPoolOptions=Ft,e.validateCreateTradeData=Ut,e.validateFaucetAmount=At,e.validateFetchGalaBalanceOptions=Lt,e.validateFetchPoolDetailsData=Wt,e.validateFetchTokenBalanceOptions=Bt,e.validateFullName=Et,e.validateGetAmountOptions=Gt,e.validateGetTradeOptions=qt,e.validateImageUploadOptions=Ct,e.validateLaunchTokenData=Nt,e.validateSearchQuery=It,e.validateSellTokensData=Rt,e.validateTokenDescription=vt,e.validateTokenListOptions=xt,e.validateTokenName=wt,e.validateTokenSymbol=kt,e.validateTokenUrls=Dt,e.validateTradeListParams=Kt,e.validateTransferFaucetsData=$t,e.validateUpdateProfileData=Ot,e.validateUploadProfileImageOptions=_t,e.validateUserTokenName=Pt,e.validateVaultAddress=St,e.vaultAddressSchema=ie});
|