@gala-chain/launchpad-sdk 5.0.4-beta.62 → 5.0.4-beta.63

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. package/dist/LaunchpadSDK.d.ts +117 -0
  2. package/dist/LaunchpadSDK.d.ts.map +1 -1
  3. package/dist/ai-docs.json +129 -15
  4. package/dist/constants/endpoints.d.ts +44 -0
  5. package/dist/constants/endpoints.d.ts.map +1 -1
  6. package/dist/constants/version.generated.d.ts +1 -1
  7. package/dist/index.browser.d.ts +1 -0
  8. package/dist/index.browser.d.ts.map +1 -1
  9. package/dist/index.browser.esm.js +1 -1
  10. package/dist/index.cjs +1 -1
  11. package/dist/index.esm.js +1 -1
  12. package/dist/index.js +1 -1
  13. package/dist/native.cjs +1 -0
  14. package/dist/native.d.ts +22 -0
  15. package/dist/native.d.ts.map +1 -0
  16. package/dist/native.esm.js +1 -0
  17. package/dist/react.cjs +1 -1
  18. package/dist/react.esm.js +1 -1
  19. package/dist/services/ClientConfigService.d.ts +61 -0
  20. package/dist/services/ClientConfigService.d.ts.map +1 -0
  21. package/dist/services/IntervalRegistry.d.ts +79 -0
  22. package/dist/services/IntervalRegistry.d.ts.map +1 -0
  23. package/dist/services/MultiPoolStateManager.d.ts.map +1 -1
  24. package/dist/services/NotificationService.d.ts +113 -0
  25. package/dist/services/NotificationService.d.ts.map +1 -0
  26. package/dist/src/LaunchpadSDK.d.ts +117 -0
  27. package/dist/src/LaunchpadSDK.d.ts.map +1 -1
  28. package/dist/src/constants/endpoints.d.ts +44 -0
  29. package/dist/src/constants/endpoints.d.ts.map +1 -1
  30. package/dist/src/constants/version.generated.d.ts +1 -1
  31. package/dist/src/index.browser.d.ts +1 -0
  32. package/dist/src/index.browser.d.ts.map +1 -1
  33. package/dist/src/native.d.ts +22 -0
  34. package/dist/src/native.d.ts.map +1 -0
  35. package/dist/src/services/ClientConfigService.d.ts +61 -0
  36. package/dist/src/services/ClientConfigService.d.ts.map +1 -0
  37. package/dist/src/services/IntervalRegistry.d.ts +79 -0
  38. package/dist/src/services/IntervalRegistry.d.ts.map +1 -0
  39. package/dist/src/services/MultiPoolStateManager.d.ts.map +1 -1
  40. package/dist/src/services/NotificationService.d.ts +113 -0
  41. package/dist/src/services/NotificationService.d.ts.map +1 -0
  42. package/dist/src/types/client-config.dto.d.ts +43 -0
  43. package/dist/src/types/client-config.dto.d.ts.map +1 -0
  44. package/dist/src/types/common.d.ts +2 -0
  45. package/dist/src/types/common.d.ts.map +1 -1
  46. package/dist/src/types/notification.dto.d.ts +92 -0
  47. package/dist/src/types/notification.dto.d.ts.map +1 -0
  48. package/dist/src/utils/http.d.ts.map +1 -1
  49. package/dist/types/client-config.dto.d.ts +43 -0
  50. package/dist/types/client-config.dto.d.ts.map +1 -0
  51. package/dist/types/common.d.ts +2 -0
  52. package/dist/types/common.d.ts.map +1 -1
  53. package/dist/types/notification.dto.d.ts +92 -0
  54. package/dist/types/notification.dto.d.ts.map +1 -0
  55. package/dist/utils/http.d.ts.map +1 -1
  56. package/package.json +9 -1
@@ -0,0 +1 @@
1
+ "use strict";var e=require("zod"),t=require("ethers"),n=require("@gala-chain/connect"),i=require("uuid"),o=require("bignumber.js"),a=require("@solana/web3.js"),r=require("json-stringify-deterministic"),s=require("@solana/spl-token"),c=require("bs58"),l=require("@gala-chain/api"),d=require("@gala-chain/dex"),u=require("axios"),h=require("socket.io-client");const g={UPLOAD_IMAGE:"/v1/tokens/:tokenName/image",FETCH_POOLS:"/v1/tokens",GET_TOKEN:"/v1/tokens/:tokenName",GET_TOKEN_DISTRIBUTION:"/v1/holders",GET_TOKEN_BADGES:"/v1/tokens/:tokenName/badges",CHART:"/v1/chart",GET_TRADES:"/v1/tokens/:tokenName/trades",GET_PROFILE:"/v1/users/:address",UPDATE_PROFILE:"/v1/users/me",GET_TOKEN_LIST:"/v1/users/:address/tokens",GET_TOKENS_HELD:"/v1/users/:address/tokens",USER_BALANCES:"/v1/users/:address/balances",USER_REPORT:"/v1/users/:address/report",GET_MANAGED_TOKENS:"/v1/users/me/managed-tokens",VALIDATE_TOKEN:"/v1/utils/validate-token",UPDATE_TOKEN_CONFIG:"/v1/tokens/:tokenName",TOKEN_STATS:"/v1/tokens/:tokenName/stats",OEMBED:"/oembed",OEMBED_BUY_SELL:"/oembed/buy-sell/:tokenName",OEMBED_PROFILE:"/oembed/profile/:address"},m="/v1/users/referrals/url",p="/v1/users/referrals",f="/v1/users/referrals/summary",y="/v1/tokens/:tokenName/start-stream",k="/v1/tokens/:tokenName/stop-stream",v="/v1/tokens/:tokenName/stream/disable",w="/v1/tokens/:tokenName/stream/enable",b="/v1/tokens/:tokenName/reset-key",S="/v1/tokens/:tokenName/recordings",A="/v1/tokens/:tokenName/recordings/:assetId/download",T="/v1/tokens/:tokenName/recordings/:assetId",E="/v1/tokens/:tokenName/stream/simulcast",C="/v1/tokens/:tokenName/stream/simulcast",I="/v1/tokens/:tokenName/stream/simulcast/:targetId",N="/v1/config",D="/v1/config",P="/v1/config",x="/v1/tokens/:tokenName/role",_="/v1/roles",F="/v1/tokens/:tokenName/access",R="/v1/tokens/:tokenName/credentials",B="/auth/login",U="/auth/logout",O="/auth/refresh",L="/auth/session",M="/v1/tokens/:tokenName/bans",$="/v1/tokens/:tokenName/bans",K="/v1/tokens/:tokenName/bans/:userAddress",q="/v1/tokens/:tokenName/bans/:userAddress",G="/v1/tokens/:tokenName/active-users",W="/v1/user-bans",H="/v1/user-bans",z="/v1/user-bans/:userAddress",j="/v1/user-bans/:userAddress",V={CREATE:"/v1/api-keys",LIST:"/v1/api-keys",GET:"/v1/api-keys/:id",UPDATE:"/v1/api-keys/:id",REVOKE:"/v1/api-keys/:id"},Q="/v1/moderator-invites",X="/v1/moderator-invites/claim",J="/v1/moderated-tokens",Y="/v1/moderator-invites",Z="/v1/moderator-invites/:id",ee="/v1/moderator-invites/:id/role",te="/v1/moderator-invites/:id",ne="/v1/moderator-invites/code/:code",ie={CREATE:"/v1/content-flags",LIST:"/v1/content-flags",DISMISS:"/v1/content-flags/:id/dismiss",ACTION:"/v1/content-flags/:id/action"},oe="/v1/overseer-invites",ae="/v1/overseer-invites",re="/v1/overseer-invites/code/:code",se="/v1/overseer-invites/claim",ce="/v1/overseer-invites/:id",le="/v1/overseers",de="/v1/overseers",ue="/v1/overseers/:address",he="/v1/overseers/me",ge="/v1/overseer-summary",me="/v1/overseer-summary/bans",pe="/v1/overseer-summary/token-bans",fe="/v1/overseer-summary/invites",ye="/v1/overseer-summary/flags",ke="/v1/overseer-summary/users",ve="/v1/users",we="/v1/users/:address/summary",be="/v1/cache/flush/address/:address",Se="/v1/cache/flush/token/:tokenName",Ae="/v1/cache/flush/all",Te="/v1/cache/flush/wallet-aliases/:address",Ee="/v1/cache/flush/wallet-aliases",Ce="/v1/token-bans",Ie="/v1/token-bans",Ne="/v1/token-bans/:tokenName",De="/v1/token-bans/:tokenName",Pe="/v1/reactions",xe="/v1/reactions",_e="/v1/trades",Fe={GET_MESSAGES:"/v1/messages",CREATE_MESSAGE:"/v1/messages",UPDATE_MESSAGE:"/v1/messages/:id",DELETE_MESSAGE:"/v1/messages/:id",GET_PINNED:"/v1/messages/pinned",PIN_MESSAGE:"/v1/messages/:id/pin",UNPIN_MESSAGE:"/v1/messages/:id/unpin",GET_MESSAGE_STATS:"/v1/messages/stats"},Re="/v1/restricted-token-names",Be="/v1/restricted-token-names",Ue="/live/:tokenName/chat",Oe="/live/:tokenName/chat",Le="/live/:tokenName/chat",Me="/live/chat",$e="/live/chat",Ke="/live/chat",qe="/live/:tokenName/chat/slow-mode",Ge="/live/:tokenName/engagement-stats",We={GET_SETTINGS:"/v1/ai-moderation/settings",UPDATE_SETTINGS:"/v1/ai-moderation/settings",GET_MODERATION:"/v1/ai-moderation/flags/:flagId",TRIGGER_MODERATION:"/v1/ai-moderation/flags/:flagId/analyze",GET_STATUS:"/v1/ai-moderation/status"},He="/v1/stats",ze="/v1/trading/fee",je="/v1/trading/sale-details",Ve="/v1/trading/quote",Qe="/v1/trading/premint-quote",Xe={GET_LEADERBOARD:"/weekly-challenge",GET_TOKEN_HISTORY:"/weekly-challenge/history/:vaultAddress"},Je="/v1/notifications/register",Ye="/v1/notifications/unregister",Ze="/v1/notifications/preferences",et="/v1/notifications/preferences",tt="/v1/config/client-flags",nt="native",it="exact";function ot(e){return e instanceof Error}function at(e){return ot(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 rt(e){return ot(e)||"object"==typeof e&&null!==e&&"stack"in e&&"string"==typeof e.stack?e.stack:void 0}function st(e){if(function(e){return"object"==typeof e&&null!==e&&"code"in e&&("string"==typeof e.code||"number"==typeof e.code)}(e))return e.code}function ct(e){return"object"==typeof e&&null!==e&&"message"in e&&("response"in e||"request"in e||"config"in e)}function lt(e,t){if(null==e||"object"!=typeof e)throw new Error(`${t}: No response - expected object, got ${typeof e}`);const n=e;if(!("Status"in n))throw new Error(`${t}: Response missing 'Status' property`);const i=n.Status;if("number"!=typeof i)throw new Error(`${t}: Response Status must be a number, got ${typeof i}`);if(1!==i)throw new Error(`${t}: Response status indicates failure (Status: ${i})`);return n}function dt(e){if(null==e||"object"!=typeof e)return"";const t=e.Message;return"string"==typeof t&&t.trim().length>0?` - ${t}`:""}function ut(e,t,n){try{t()}catch(t){if(!function(e){return e instanceof Error&&"ValidationError"===e.name}(t))throw t;{const n=t.message;e.push(n)}}}const ht="Token name is required and must be a string",gt=e=>`Could not find vault address for token: ${e}`,mt="REQUIRED",pt="INVALID_TYPE",ft="INVALID_VALUE",yt="INVALID_FORMAT",kt="TOO_LARGE",vt="INVALID_RANGE";class wt extends Error{constructor(e,t,n){super(e),this.field=t,this.code=n,this.name="ValidationError"}}class bt extends Error{constructor(e,t,n){super(e),this.statusCode=t,this.originalError=n,this.name="NetworkError"}}class St extends Error{constructor(e,t){super(e),this.field=t,this.name="ConfigurationError"}}class At extends Error{constructor(e,t,n){super(e),this.transactionId=t,this.code=n,this.name="TransactionError"}}class Tt extends Error{constructor(e,t,n){super(e),this.originalError=t,this.code=n,this.name="GSwapQuoteError"}}class Et extends Error{constructor(e,t,n,i){super(e),this.originalError=t,this.transactionHash=n,this.code=i,this.name="GSwapSwapError"}}class Ct extends Error{constructor(e,t,n,i,o){super(e),this.originalError=t,this.tokenA=n,this.tokenB=i,this.code=o,this.name="GSwapPoolError"}}class It extends Error{constructor(e,t,n,i){super(e),this.originalError=t,this.walletAddress=n,this.code=i,this.name="GSwapAssetError"}}class Nt extends Error{constructor(e,t,n){super(e),this.originalError=t,this.code=n,this.name="GSwapPositionError"}}class Dt extends wt{constructor(e,t){super(e,"dexQuote","DEX_QUOTE_ERROR"),this.context=t,this.name="DexQuoteError"}}class Pt extends wt{constructor(e){super(e,"dexPool","DEX_POOL_NOT_FOUND"),this.name="DexPoolNotFoundError"}}function xt(e,t){return void 0!==t?t:e.charAt(0).toUpperCase()+e.slice(1)}function _t(e,t){return new wt(`Token "${e}" not found. Please verify the token name is correct.`,"tokenName","TOKEN_NOT_FOUND")}function Ft(e,t){const n=xt(e,t);return new wt(`${n} is required`,e,"REQUIRED")}function Rt(e,t,n){const i=xt(e,n);return new wt(`${i} must be ${t}`,e,"INVALID_FORMAT")}function Bt(e,t,n){return new bt(e,t,n)}function Ut(e,t){return new St(e,t)}function Ot(e,t,n){return new At(e,t,n)}function Lt(e,t,n,i){n&&n.error(`${t}:`,e);const o=ct(e)?e.response?.status:void 0;return Bt(`${t}: ${at(e)}`,o,ot(e)?e:void 0)}function Mt(e,t,n,i,o){const a=xt(e,o);return new wt(`${a} must be between ${t} and ${n}${void 0!==i?`, received: ${i}`:""}`,e,"OUT_OF_RANGE")}function $t(e,t,n,i){const o=xt(e,i);return new wt(`${o} must be at least ${t}${void 0!==n?`, received: ${n}`:""}`,e,"TOO_SMALL")}function Kt(e,t,n,i){const o=xt(e,i);return new wt(`${o} must be at most ${t}${void 0!==n?`, received: ${n}`:""}`,e,"TOO_LARGE")}function qt(e,t,n,i){const o=xt(e,i),a=void 0!==n?`, received: ${"object"==typeof n?"object":String(n)}`:"";return new wt(`${o} must be ${t}${a}`,e,"INVALID_TYPE")}function Gt(e,t,n){const i=xt(e,n);return new wt(`${i} must be a valid number${void 0!==t?`: "${t}"`:""}`,e,"INVALID_NUMBER")}function Wt(e){return new wt(`Liquidity position not found: ${e}`,"positionId","POSITION_NOT_FOUND")}function Ht(e,t,n,i){const o=xt(e,i);return new wt(`Invalid ${o}: ${t}. Must be one of: ${n.join(", ")}`,e,"INVALID_ENUM")}function zt(e){return null!=e&&"object"==typeof e&&"data"in e}function jt(e,t,n=!1){const i=!0===e.error,o=n&&(null===e.data||void 0===e.data);if(i||o){const n=e?.statusCode??e.status??void 0;throw Bt(e?.message??t,n)}}function Vt(e){if(zt(e))return e.data}function Qt(e,t="No data found in response"){if(!zt(e))throw Bt(t);const n=e.data;if(null==n)throw Bt(t);return n}async function Xt(e,t={}){const{errorContext:n="Operation failed",logger:i,debugLogEnabled:o=!1}=t;o&&i&&i.debug(`${n}: starting operation`);try{const t=await e();if(null==t)throw new wt(`${n}: No response from server`,"response","NO_RESPONSE");if(jt(t,n,!0),o&&i&&i.debug(`${n}: completed successfully`),void 0===t.data)throw new wt(`${n}: No data returned from API`,"data","NO_DATA");return t.data}catch(e){throw i&&i.error(n,{error:e}),e}}async function Jt(e,t={}){const{errorContext:n="Operation failed",logger:i,debugLogEnabled:o=!1}=t;o&&i&&i.debug(`${n}: starting operation`);try{const t=await e();return o&&i&&i.debug(`${n}: completed successfully`),t}catch(e){throw i&&i.error(n,{error:e}),e}}async function Yt(e,t={}){const{errorContext:n="Operation failed",logger:i,debugLogEnabled:o=!1}=t;o&&i&&i.debug(`${n}: starting operation`);try{const t=await e();if(null==t)throw new wt(`${n}: No response from server`,"response","NO_RESPONSE");return jt(t,n,!0),o&&i&&i.debug(`${n}: completed successfully`),t}catch(e){throw i&&i.error(n,{error:e}),e}}class Zt{}function en(e){return e.trim().toLowerCase()}function tn(e){return e.trim().toUpperCase()}function nn(e){return e.trim().toUpperCase()}var on;Zt.BASE_PRICE=1650667151e-14,Zt.PRICE_SCALING_FACTOR=1166069e-12,Zt.TRADING_FEE_FACTOR=.001,Zt.GAS_FEE="1",Zt.MIN_UNBONDING_FEE_FACTOR=0,Zt.MAX_UNBONDING_FEE_FACTOR=.5,Zt.NET_UNBONDING_FEE_FACTOR=.5,Zt.DEFAULT_LAUNCHPAD_TOKEN_MAX_SUPPLY=1e7,function(e){e.DEBUG="DEBUG",e.INFO="INFO",e.WARN="WARN",e.ERROR="ERROR"}(on||(on={}));class an{constructor(e){this.levelPriority={[on.DEBUG]:0,[on.INFO]:1,[on.WARN]:2,[on.ERROR]:3},this.debugEnabled=e.debug,this.context=e.context??"SDK",this.minLevel=e.minLevel??(e.debug?on.DEBUG:on.INFO)}debug(e,t){this.log(on.DEBUG,e,t)}info(e,t){this.log(on.INFO,e,t)}warn(e,t){this.log(on.WARN,e,t)}error(e,t){this.log(on.ERROR,e,t)}log(e,t,n){if(this.levelPriority[e]<this.levelPriority[this.minLevel])return;if(e===on.DEBUG&&!this.debugEnabled)return;const i=`[${(new Date).toISOString()}] [${this.context}] [${e}]`,o=this.getConsoleMethod(e);void 0!==n?ot(n)?o(`${i} ${t}`,n.message,rt(n)??""):o(`${i} ${t}`,n):o(`${i} ${t}`)}getConsoleMethod(e){switch(e){case on.DEBUG:return console.debug;case on.INFO:return console.info;case on.WARN:return console.warn;case on.ERROR:return console.error;default:return console.log}}child(e){return new an({debug:this.debugEnabled,context:`${this.context}:${e}`,minLevel:this.minLevel})}isDebugEnabled(){return this.debugEnabled&&this.levelPriority[on.DEBUG]>=this.levelPriority[this.minLevel]}}function rn(e){return null==e||""===e}function sn(e){return null==e}const cn=new an({debug:!1,context:"DateUtils"});function ln(e,t){if(null==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}}function dn(e){if(null==e)return!1;if(e instanceof Date)return!isNaN(e.getTime());if("string"!=typeof e)return!1;const t=new Date(e);return!isNaN(t.getTime())}function un(e){return Date.now()-e}const hn={MIN_LENGTH:3,MAX_LENGTH:20,PATTERN:/^[a-zA-Z0-9]{3,20}$/},gn={MIN_LENGTH:2,MAX_LENGTH:20,PATTERN:/^[a-zA-Z0-9]+$/},mn={MIN_LENGTH:1,MAX_LENGTH:50},pn=100,fn=20,yn={MAX_LIMIT:50},kn={PATTERN:/^(eth\|[0-9a-fA-F]{40}|client\|[a-zA-Z0-9_-]+)$/},vn={COMMENTS_V1:{MAX_LENGTH:2e3},CHAT_MESSAGES_V1:{MAX_LENGTH:500},BAN_REASON:{MAX_LENGTH:500},DESCRIPTION:{MAX_LENGTH:255},FLAG_DETAILS:{MAX_LENGTH:1e3},CONTENT_ID:{MAX_LENGTH:100}},wn={CONTENT_REACTION:{MAX_LENGTH:64,PATTERN:/^(msg-\d{13}-[a-f0-9]{32}|chat-\d{13}-[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12})$/}},bn=60,Sn=31536e3,An={MAX_LENGTH:64,PATTERN:/^(galaswap-operation-|galaconnect-operation-)/},Tn={PATTERN:/^[a-fA-F0-9]{64}$/},En={STREAM_URL_PATTERN:/^(rtmps?|srt):\/\/.+/},Cn=50,In=1,Nn=100,Dn={FULL_NAME:{MIN_LENGTH:1,MAX_LENGTH:100,ALPHABETS_ONLY_PATTERN:/^[a-zA-Z]+(?:\s[a-zA-Z]+)?$/}};function Pn(e,t){if(rn(e))return t;try{return JSON.parse(e)}catch{return t}}function xn(e,t=0){if(rn(e))return t;if("number"==typeof e)return isNaN(e)||!isFinite(e)?t:e;const n=parseFloat(e);return isNaN(n)||!isFinite(n)?t:n}function _n(e,t,n){if(sn(e))return t;if("number"==typeof e)return isNaN(e)?t:e;const i=Number(e);return isNaN(i)?t:i}function Fn(e,t=0){if(rn(e))return t;if("bigint"==typeof e)try{return Number(e)}catch{return t}if("number"==typeof e)return isNaN(e)||!isFinite(e)?t:Math.floor(e);const n=parseInt(String(e),10);return isNaN(n)?t:n}function Rn(e,t,n){if(sn(e))return t;if("bigint"==typeof e)try{return Number(e)}catch{return t}if("number"==typeof e)return isNaN(e)?t:Math.floor(e);const i=parseInt(String(e),10);return isNaN(i)?t:i}function Bn(e,t="0"){if(rn(e))return new o(t);if(o.isBigNumber(e))return e.isNaN()?new o(t):e;try{const n=new o(e);return n.isNaN()||!n.isFinite()?new o(t):n}catch{return new o(t)}}function Un(e,t){if(sn(e)||""===e)throw Ft(t);if(o.isBigNumber(e)){if(e.isNaN())throw Gt(t,"NaN");return e}try{const n=new o(e);if(n.isNaN())throw Gt(t,e);if(!n.isFinite())throw Gt(t,e);return n}catch(n){if(n instanceof wt)throw n;throw Gt(t,e)}}function On(e,t=0){if(sn(e))return t;if("number"==typeof e)return isNaN(e)?t:e;return xn(String(e).replace("%","").trim(),t)}function Ln(e,t=18){const n=xn(e,0);if(0===n)return"0";return n.toFixed(t).replace(/\.?0+$/,"")}function Mn(e){return Ln(e,8)}function $n(e){return Ln(e,18)}function Kn(e,t,n,i){if("number"!=typeof e)throw qt(i,"a number",e,i);if(e<t||e>n)throw Mt(i,t,n,e)}function qn(e,t,n){if("string"!=typeof e)throw qt(n,"a string",e,n);if(e.length>t)throw Kt(n,t,e.length)}function Gn(e,t){if("number"!=typeof e)throw qt(t,"a number",e,t);if(!Number.isInteger(e))throw qt(t,"an integer",e,t);if(e<=0)throw $t(t,1,e,t)}function Wn(e,t,n="range"){const i="number"==typeof e?e:Number(e),o="number"==typeof t?t:Number(t);if(isNaN(i)||isNaN(o))throw qt(n,"numeric values",void 0,n);if(i>o)throw new wt(`Minimum value (${i}) must be less than or equal to maximum value (${o}) for ${n}`,n,vt)}function Hn(e,t,n=!1){if("string"!=typeof e)throw qt(t,"a string",e,t);if(!n&&0===e.trim().length)throw Ft(t,`${t} (non-empty string)`)}const zn={ETH_ADDRESS:/^0x[0-9a-fA-F]{40}$/,BACKEND_ADDRESS:/^eth\|(0x)?[0-9a-fA-F]{40}$/,CLIENT_ADDRESS:/^client\|[a-zA-Z0-9_-]+$/},jn=function(e){return"string"==typeof e&&e.trim().length>0},Vn=sn;function Qn(e,t,n=100,i,o){if(void 0!==o&&"string"!=typeof o)throw new wt("cursor must be a string","cursor",pt);const a=i??t,r=void 0!==i?"pageSize":"limit";if(void 0!==a&&(Gn(a,r),a>n))throw new wt(`${r} must be at most ${n}`,r,kt)}function Xn(e){const t=Object.values(e);return e=>"string"==typeof e&&t.includes(e)}function Jn(e){return Object.values(e)}function Yn(e,t,n,i={}){const{description:o="parameter",treatEmptyAsNull:a=!0}=i,r=e[t],s=e[n],c=null!=r&&(!a||""!==r),l=null!=s&&(!a||""!==s);if(!c&&!l)throw Ut(`Either ${t} or ${n} must be provided (${o})`,n);if(c&&l)throw Ut(`Cannot provide both ${t} and ${n}. Provide exactly one (${o}).`,n);return{chosen:c?t:n,hasA:c,hasB:l}}function Zn(e,t="tokenName",n=hn){if(!jn(e))throw Ft(t);const i=e.trim();if(0===i.length)throw Ft(t);if(i.length<n.MIN_LENGTH)throw Mt(t,n.MIN_LENGTH,n.MAX_LENGTH,i.length,`${t} length`);if(i.length>n.MAX_LENGTH)throw Mt(t,n.MIN_LENGTH,n.MAX_LENGTH,i.length,`${t} length`);if(!n.PATTERN.test(i))throw new wt(`${t} must contain only alphanumeric characters`,t,yt)}function ei(e,t="tokenName",n=hn){if(null==e)throw Ft(t,t);if("string"!=typeof e)throw qt(t,"a string",e);const i=e.trim();if(0===i.length)throw Ft(t,t);if(!n.PATTERN.test(i))throw new wt(`${t} must be ${n.MIN_LENGTH}-${n.MAX_LENGTH} alphanumeric characters`,t,yt)}function ti(e){return""===e||0===e.trim().length}function ni(e,t,n){if(!Vn(e)){if("string"!=typeof e)throw qt(t,"a string",e,t);if(e.length>n)throw new wt(`${t} must be at most ${n} characters`,t,kt)}}function ii(e,t){if(Vn(e))return;let n;if(e instanceof Date)n=e;else if("string"==typeof e)n=new Date(e);else{if("number"!=typeof e)throw qt(t,"a date",e,t);n=new Date(e)}if(isNaN(n.getTime()))throw new wt(`${t} must be a valid date`,t,yt)}function oi(e,t,n){try{Gn(e,t)}catch{throw function(e,t,n,i="OPERATION_FAILED"){return new wt(`${e} failed: ${t}`,n,i)}(n,"must be a positive integer",t)}}function ai(e,t,n="status"){void 0!==e&&function(e,t,n){const i=Object.values(t);if(!i.includes(e))throw new wt(`${n} must be one of: ${i.join(", ")}`,n,yt)}(e,t,n)}class ri{constructor(e,t=!1,n){this.http=e,this.logger=new an({debug:t,context:this.constructor.name}),this.jwtAuth=n}setJwtAuth(e){this.jwtAuth=e}getJwtHeaders(){if(!this.jwtAuth){if("test"===process.env.NODE_ENV&&!this.isCalledFromUserCode())return{};throw new St("JWT authentication required. Call sdk.login() first.")}return this.jwtAuth.getJwtHeaders()}isCalledFromUserCode(){const e=rt(new Error)??"";return!(!e.includes("expect")&&!e.includes("jest"))||!(!e.includes("tests/")&&!e.includes(".test.ts"))}validatePositiveInteger(e,t,n){oi(e,t,n)}}class si{constructor(e=!1,t){this.logger=t||new an({debug:e,context:this.constructor.name})}}class ci extends si{constructor(e=!1){super(e),this.cache=new Map}get(e){const t=this.normalizeKey(e);return this.cache.get(t)}set(e,t){const n=this.normalizeKey(e);this.cache.set(n,t)}has(e){const t=this.normalizeKey(e);return this.cache.has(t)}clear(){this.cache.clear(),this.logger.debug("Cleared cache")}dump(){return new Map(this.cache)}size(){return this.cache.size}isEmpty(){return 0===this.cache.size}buildBaseStats(){return{totalItems:this.cache.size}}}class li extends ci{constructor(e=!1){super(e)}normalizeKey(e){return en(e).replace(/\s+/g," ").replace(/[\u0000-\u001F\u007F-\u009F\u200B-\u200D\uFEFF]/g,"")}getLRUKey(){return this.cache.keys().next().value??null}updateCacheEntry(e,t){const n=this.cache.get(e);if(this.cache.has(e)&&this.cache.delete(e),this.cache.size>=li.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.normalizeKey(e);this.updateCacheEntry(n,t)}set(e,t){const n=this.normalizeKey(e);this.updateCacheEntry(n,t)}getByName(e){const t=this.normalizeKey(e);return this.cache.get(t)}getMaxSupply(e){const t=this.normalizeKey(e),n=this.cache.get(t);return n?.maxSupply??Zt.DEFAULT_LAUNCHPAD_TOKEN_MAX_SUPPLY.toString()}has(e){const t=this.normalizeKey(e);return this.cache.has(t)}clear(e){if(void 0!==e&&""!==e){const t=this.normalizeKey(e);this.cache.delete(t)}else super.clear()}dumpAsObject(){const e={};return this.cache.forEach((t,n)=>{e[n]=t}),e}getStats(){const e=this.buildBaseStats(),{cacheSize:t,oldestEntry:n}=this.calculateCacheSizeAndAge();return{...e,totalTokens:e.totalItems,cacheSize:t,oldestEntry:n}}calculateCacheSizeAndAge(){let e=Date.now(),t=0;return this.cache.forEach((n,i)=>{n.lastUpdated<e&&(e=n.lastUpdated);let o=0;o+=2*i.length,void 0!==n.reverseBondingCurveMinFeeFactor&&(o+=8),void 0!==n.reverseBondingCurveMaxFeeFactor&&(o+=8),void 0!==n.reverseBondingCurveNetFeeFactor&&(o+=8),o+=8,void 0!==n.vaultAddress&&""!==n.vaultAddress&&(o+=2*n.vaultAddress.length),void 0!==n.maxSupply&&""!==n.maxSupply&&(o+=2*n.maxSupply.length),void 0!==n.symbol&&""!==n.symbol&&(o+=2*n.symbol.length),o+=32,t+=o}),{cacheSize:t,oldestEntry:this.cache.size>0?e:0}}getByTokenId(e){const t=`token:${en(e)}`;return this.cache.get(t)??null}setByTokenId(e,t){const n=`token:${en(e)}`;this.updateCacheEntry(n,t)}hasByTokenId(e){const t=`token:${en(e)}`;return this.cache.has(t)}}li.MAX_CACHE_SIZE=1e4;class di extends n.ChainCallDTO{constructor(e){super(),this.tokenName=e.tokenName,this.tokenSymbol=e.tokenSymbol,this.tokenDescription=e.tokenDescription,this.tokenImage=e.tokenImage,this.preBuyQuantity=e.preBuyQuantity,void 0!==e.websiteUrl&&(this.websiteUrl=e.websiteUrl),void 0!==e.telegramUrl&&(this.telegramUrl=e.telegramUrl),void 0!==e.twitterUrl&&(this.twitterUrl=e.twitterUrl),void 0!==e.instagramUrl&&(this.instagramUrl=e.instagramUrl),void 0!==e.facebookUrl&&(this.facebookUrl=e.facebookUrl),void 0!==e.redditUrl&&(this.redditUrl=e.redditUrl),void 0!==e.tiktokUrl&&(this.tiktokUrl=e.tiktokUrl),this.tokenCategory=e.tokenCategory,this.tokenCollection=e.tokenCollection,this.uniqueKey=e.uniqueKey,e.reverseBondingCurveConfiguration&&(this.reverseBondingCurveConfiguration=e.reverseBondingCurveConfiguration),void 0!==e.saleStartTime&&(this.saleStartTime=e.saleStartTime)}}function ui(e){return""===e?"":e.startsWith("0x")?e.slice(2):e}const hi={ETHEREUM:zn.ETH_ADDRESS,ETHEREUM_NO_PREFIX:/^[a-fA-F0-9]{40}$/,BACKEND:zn.BACKEND_ADDRESS,CLIENT:zn.CLIENT_ADDRESS};class gi{toBackendFormat(e){if(!jn(e))throw new wt("Address is required and must be a string","address","REQUIRED");let n=e;if(e.startsWith("eth|")){if(n=e.slice(4),!/^[a-fA-F0-9]{40}$/.test(n))throw new wt(`Invalid Ethereum address format in backend address. Expected 40 hex characters after 'eth|'. Got: "${e}"`,"address","INVALID_FORMAT");return e}if(n=ui(e),!/^[a-fA-F0-9]{40}$/.test(n))throw new wt(`Invalid Ethereum address format. Expected 40 hex characters (with or without 0x prefix). Got: "${e}"`,"address","INVALID_FORMAT");return`eth|${t.getAddress(`0x${n}`).slice(2)}`}toEthereumFormat(e){if(!jn(e))throw new wt("Backend address is required and must be a string","address","REQUIRED");const t=e.match(/^eth\|(0x)?([0-9a-fA-F]{40})$/i);if(!t)throw new wt(`Invalid backend address format. Expected "eth|{40-hex-characters}" or "eth|0x{40-hex-characters}". Got: "${e}"`,"address","INVALID_FORMAT");const n=t[1],i=void 0!==n&&""!==n?6:4;return`0x${e.substring(i)}`}normalizeInput(e){if(void 0!==e&&""!==e){if(e.startsWith("client|")){if(!hi.CLIENT.test(e))throw new wt(`Invalid client address format: "${e}"`,"address","INVALID_FORMAT");return e}if(e.startsWith("eth|")){if(!hi.BACKEND.test(e))throw new wt(`Invalid backend address format: "${e}"`,"address","INVALID_FORMAT");return e}return this.toBackendFormat(e)}}isValid(e){return!!jn(e)&&(!!hi.ETHEREUM.test(e)||(!!hi.ETHEREUM_NO_PREFIX.test(e)||(!!hi.BACKEND.test(e)||!!hi.CLIENT.test(e))))}assertValid(e,t="address"){if(!this.isValid(e))throw new wt(`${t} must be a valid wallet address (Ethereum or backend format)`,t,"INVALID_FORMAT")}detectFormat(e){return jn(e)?hi.ETHEREUM.test(e)||hi.ETHEREUM_NO_PREFIX.test(e)?"ethereum":hi.BACKEND.test(e)?"backend":hi.CLIENT.test(e)?"client":null:null}normalize(e){if(void 0!==e&&""!==e)return e.toLowerCase()}extractHex(e){if(e.startsWith("eth|"))return e.substring(4).toLowerCase();if(e.startsWith("0x"))return ui(e).toLowerCase();if(/^[a-fA-F0-9]{40}$/.test(e))return e.toLowerCase();throw new wt(`Cannot extract hex from address: "${e}". Expected Ethereum or backend format.`,"address","INVALID_FORMAT")}}const mi=new gi,pi=ui;function fi(e){return mi.toBackendFormat(e)}function yi(e){return mi.toEthereumFormat(e)}function ki(e){return mi.isValid(e)}function vi(e,t="address"){mi.assertValid(e,t)}function wi(e){return mi.detectFormat(e)}var bi=Object.freeze({__proto__:null,AddressFormatter:gi,assertValidWalletAddress:vi,detectFormat:wi,fromBackendAddressFormat:yi,isValidAddress:ki,normalizeAddressInput:function(e){return mi.normalizeInput(e)},stripHexPrefix:pi,toBackendAddressFormat:fi});const Si=e.z.string({message:"Token name is required"}).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"),Ai=e.z.string({message:"Token symbol is required"}).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"),Ti=e.z.string({message:"Token description is required"}).min(1,"Token description is required").max(500,"Token description must be at most 500 characters"),Ei=e.z.string().min(1,"Token name must be at least 1 character").max(50,"Token name must be at most 50 characters"),Ci=e.z.string().min(1,"Search query must be at least 1 character").max(100,"Search query must be at most 100 characters"),Ii=e.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");e.z.string().regex(zn.BACKEND_ADDRESS,"Address must be in format: eth|[40-hex-chars]"),e.z.string().regex(zn.ETH_ADDRESS,"Invalid Ethereum address format");const Ni=e.z.string().refine(e=>zn.BACKEND_ADDRESS.test(e)||zn.ETH_ADDRESS.test(e)||zn.CLIENT_ADDRESS.test(e),"Address must be eth|[40-hex], 0x[40-hex], or client|[identifier] format").transform(e=>(new gi).normalizeInput(e)??e),Di=e.z.string().refine(e=>zn.BACKEND_ADDRESS.test(e)||/^service\|Token\$Unit\$[A-Z0-9]+\$eth:[0-9a-fA-F]{40}\$launchpad$/.test(e),"Invalid vault address format"),Pi=e.z.string().regex(/^\d+(\.\d+)?$/,"Must be a valid decimal number").refine(e=>xn(e,0)>0,"Amount must be greater than zero"),xi=e.z.string().regex(/^\d+(\.\d+)?$/,"Must be a valid decimal number").refine(e=>xn(e,0)>=0,"Amount must be zero or greater");e.z.string().url("Must be a valid URL").regex(/^https?:\/\//,"URL must start with http:// or https://");const _i=e.z.string().optional().refine(e=>void 0===e||""===e||/^https?:\/\/.+\..+/.test(e),"Must be a valid URL if provided");function Fi(t=100){return e.z.number().int("Limit must be an integer").min(1,"Limit must be at least 1").max(t,`Limit must be at most ${t}`).default(10)}e.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),Fi(100),Fi(20),Fi(20);const Ri=e.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"),Bi=e.z.string().max(255,"Filename must be at most 255 characters"),Ui=e.z.enum(["image/png","image/jpg","image/jpeg","image/gif","image/webp","image/svg+xml"]);e.z.string().datetime("Must be a valid ISO 8601 date string"),e.z.number().int("Timestamp must be an integer").min(0,"Timestamp must be non-negative");const Oi=e.z.string().regex(/^0x[a-fA-F0-9]{64}$/,"Private key must be format: 0x + 64 hex characters");e.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"),e.z.string().regex(/^galaconnect-operation-[a-z0-9-]+$/,"Unique key must be format: galaconnect-operation-{unique-id}");const Li=[".png",".jpg",".jpeg",".gif",".webp",".svg"];e.z.object({file:e.z.union([e.z.instanceof(File),e.z.instanceof(Buffer)]),name:Bi,size:Ri,type:Ui});const Mi=e.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"),$i=e.z.instanceof(Buffer).refine(e=>e.length>=1&&e.length<=10485760,"Buffer size must be between 1 byte and 10MB");e.z.union([Mi,$i]),e.z.enum([".png",".jpg",".jpeg",".gif",".webp",".svg"]),Bi.refine(e=>{const t=e.slice(e.lastIndexOf(".")).toLowerCase();return Li.includes(t)},`Filename must end with one of: ${Li.join(", ")}`),e.z.object({websiteUrl:_i,telegramUrl:_i,twitterUrl:_i,instagramUrl:_i,facebookUrl:_i,redditUrl:_i,tiktokUrl:_i}).refine(e=>void 0!==e.websiteUrl||void 0!==e.telegramUrl||void 0!==e.twitterUrl||void 0!==e.instagramUrl||void 0!==e.facebookUrl||void 0!==e.redditUrl||void 0!==e.tiktokUrl,"At least one social URL (website, telegram, twitter, instagram, facebook, reddit, or tiktok) is required");const Ki=e.z.string().min(1,"Token category must not be empty").default("Unit"),qi=e.z.string().min(1,"Token collection must not be empty").default("Token"),Gi=e.z.object({minFeePortion:e.z.string().regex(/^\d+(\.\d+)?$/,"Must be a valid decimal string").refine(e=>xn(e,0)>=.1,"Minimum fee must be >= 0.1").refine(e=>xn(e,0)<=.5,"Minimum fee must be <= 0.5"),maxFeePortion:e.z.string().regex(/^\d+(\.\d+)?$/,"Must be a valid decimal string").refine(e=>xn(e,0)>=.1,"Maximum fee must be >= 0.1").refine(e=>xn(e,0)<=.5,"Maximum fee must be <= 0.5")}).refine(e=>xn(e.maxFeePortion,0)>=xn(e.minFeePortion,0),{message:"Maximum fee must be >= minimum fee",path:["maxFeePortion"]}),Wi=e.z.object({tokenName:Si,tokenSymbol:Ai,tokenDescription:Ti,tokenImage:e.z.union([e.z.instanceof(File),e.z.instanceof(Buffer),e.z.string().url("Token image must be a valid URL")]).optional(),preBuyQuantity:xi.default("0"),websiteUrl:_i,telegramUrl:_i,twitterUrl:_i,instagramUrl:_i,facebookUrl:_i,redditUrl:_i,tiktokUrl:_i,tokenCategory:Ki,tokenCollection:qi,reverseBondingCurveConfiguration:Gi.optional(),saleStartTime:e.z.number().int().positive().optional(),privateKey:Oi.optional()});e.z.object({file:e.z.union([e.z.instanceof(File),e.z.instanceof(Buffer)]),tokenName:Si}),e.z.enum(["RECENT","POPULAR"]);const Hi=e.z.object({tokenName:Si.optional(),symbol:Ai.optional()}).refine(e=>void 0!==e.tokenName||void 0!==e.symbol,"At least one of tokenName or symbol is required");e.z.enum(["NATIVE","MEME"]),e.z.enum(["IN","OUT"]);const zi=e.z.object({from:e.z.number().int("From timestamp must be an integer").min(173e6,"From timestamp must be at least 173000000"),to:e.z.number().int("To timestamp must be an integer").min(173e6,"To timestamp must be at least 173000000"),resolution:e.z.number().int("Resolution must be an integer").min(1,"Resolution must be at least 1"),tokenName:Si});e.z.object({tokenName:Si,slippageToleranceFactor:e.z.number().min(0).max(1).optional(),maxAcceptableReverseBondingCurveFeeSlippageFactor:e.z.number().min(0).max(1).optional(),privateKey:Oi.optional()});const ji=e.z.object({offset:e.z.number().int().min(0).default(0),pageSize:e.z.number().int().min(1).max(100).default(10)}),Vi=e.z.object({offset:e.z.number().int().min(0).default(0),pageSize:e.z.number().int().min(1).max(20).default(10)}),Qi=e.z.object({offset:e.z.number().int().min(0).default(0),pageSize:e.z.number().int().min(1).max(20).default(10)});ji.extend({type:e.z.enum(["RECENT","POPULAR"]).optional(),tokenName:e.z.string().min(1).max(50).optional(),search:e.z.string().min(1).max(100).optional()}),Vi.extend({tokenName:e.z.string().min(1).max(50).optional(),search:e.z.string().min(1).max(100).optional()}),Qi.extend({tradeType:e.z.enum(["BUY","SELL"]).optional(),tokenName:e.z.string().min(1).max(50).optional(),userAddress:e.z.string().regex(/^(0x[a-fA-F0-9]{40}|eth\|[a-fA-F0-9]{40})$/).optional(),startDate:e.z.string().datetime().optional(),endDate:e.z.string().datetime().optional(),sortOrder:e.z.enum(["ASC","DESC"]).default("DESC")}),e.z.object({page:e.z.number().int().min(1),limit:e.z.number().int().min(1),total:e.z.number().int().min(0),totalPages:e.z.number().int().min(0),hasNext:e.z.boolean(),hasPrevious:e.z.boolean()});const Xi=e.z.enum(["buy","sell"]);e.z.enum(["BUY","SELL"]),e.z.object({tradeType:Xi,tokenAmount:Pi,vaultAddress:Di,userAddress:Ni,slippageTolerance:Pi.optional(),deadline:e.z.number().int().positive().optional()}),e.z.object({tokenSymbol:Ai,nativeTokenQuantity:Pi,expectedToken:Pi,maxAcceptableReverseBondingCurveFee:xi.default("0").optional()}),e.z.object({tokenSymbol:Ai,tokenQuantity:Pi,expectedNativeToken:Pi,maxAcceptableReverseBondingCurveFee:xi.default("0").optional()}),Qi.extend({tokenName:Ei.optional()}),e.z.object({page:e.z.number().int().min(1).max(1e3).default(1).optional(),limit:e.z.number().int().min(1).max(20).default(10).optional()});const Ji=e.z.enum(["NATIVE","MEME"]),Yi=e.z.enum(["IN","OUT"]);e.z.object({type:Ji,method:Yi,vaultAddress:Di,amount:Pi}),e.z.object({nativeTokenQuantity:Pi}),e.z.object({vaultAddress:Di}),e.z.object({minFeePortion:Pi,maxFeePortion:Pi});const Zi=e.z.enum(["all","DEFI","ASSET"]);function eo(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.issues.map(e=>e.message)}}}Vi.extend({type:Zi.optional(),address:Ni.optional(),search:Ci.optional(),tokenName:Ei.optional()}),e.z.object({address:Ni.optional(),refresh:e.z.boolean().optional()}),e.z.object({profileImage:e.z.string(),fullName:Ii,address:Ni,privateKey:Oi.optional()}),e.z.object({file:e.z.union([e.z.instanceof(File),e.z.instanceof(Buffer)]),address:Ni.optional(),privateKey:Oi.optional()}),e.z.object({created:e.z.number(),createdBy:e.z.string(),expires:e.z.number(),instanceId:e.z.string(),lockAuthority:e.z.string(),name:e.z.string(),quantity:e.z.string(),vestingPeriodStart:e.z.number()}),e.z.object({address:Ni,tokenId:e.z.union([e.z.string(),e.z.object({collection:e.z.string(),category:e.z.string(),type:e.z.string(),additionalKey:e.z.string()}),e.z.object({collection:e.z.string(),category:e.z.string(),type:e.z.string(),additionalKey:e.z.string(),instance:e.z.string()})]).optional(),tokenName:Ei.optional(),withExpired:e.z.boolean().optional()}).refine(e=>void 0!==e.tokenId||void 0!==e.tokenName,"At least one token identifier (tokenId or tokenName) is required");const to=eo(Si),no=eo(Wi),io=eo(Hi);function oo(e){const t=function(e){const t=no(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")}`)}function ao(e,t){return"string"==typeof e[t]}function ro(e,t){return void 0===e[t]||"string"==typeof e[t]}function so(e,t){return void 0===e[t]||"number"==typeof e[t]}function co(e){if(null==e||"object"!=typeof e)return!1;const t=e;return ao(t,"tokenName")&&so(t,"from")&&so(t,"to")&&so(t,"resolution")}function lo(e,t){switch(t){case"string":return"string"==typeof e;case"number":return"number"==typeof e;case"boolean":return"boolean"==typeof e;case"object":return null!==e&&"object"==typeof e&&!Array.isArray(e);case"array":return Array.isArray(e);case"optional-string":return void 0===e||"string"==typeof e;case"optional-number":return void 0===e||"number"==typeof e;case"optional-boolean":return void 0===e||"boolean"==typeof e;case"optional-object":return void 0===e||null!==e&&"object"==typeof e&&!Array.isArray(e);case"optional-array":return void 0===e||Array.isArray(e);default:return!1}}function uo(e){return t=>{if(null==t||"object"!=typeof t||Array.isArray(t))return!1;const n=t;for(const[t,i]of Object.entries(e)){if(!lo(n[t],i))return!1}return!0}}Xn({BUY:"buy",SELL:"sell"});const ho=uo({vaultAddress:"string"}),go=uo({nativeTokenQuantity:"string"});function mo(e){if(Vn(e)||"object"!=typeof e)return{};const t={};for(const[n,i]of Object.entries(e))Vn(i)||(jn(i)?t[n]=i:"number"==typeof i||"boolean"==typeof i?t[n]=i.toString():Array.isArray(i)?t[n]=i.join(","):t[n]="object"==typeof i?JSON.stringify(i):String(i));return t}function po(e,t="0"){return Bn(e,t)}function fo(e,t){return void 0!==t?po(e).toFixed(t):po(e).toFixed()}function yo(e){const t=po(e),n=Math.log(1.0001),i=t.toNumber();return Math.log(i)/n}function ko(e,t=!1){const n=po(e),i=new o(1).dividedBy(n);return t?i.toFixed():i}function vo(e,t){return o.min(po(e),po(t))}function wo(e,t){return o.max(po(e),po(t))}function bo(e){return po(e).isZero()}function So(e){return po(e).isLessThan(0)}function Ao(e,t){return po(e).isLessThan(po(t))}function To(e,t){return po(e).multipliedBy(t).dividedBy(100)}function Eo(...e){e.forEach(e=>{if(e.isNaN())throw Gt("value","NaN");if(!e.isFinite())throw Kt("value","finite");if(e.isLessThanOrEqualTo(0))throw $t("value","0",e.toString())})}function Co(...e){e.forEach(e=>{if(e.isNaN())throw Gt("value","NaN");if(!e.isFinite())throw Kt("value","finite");if(e.isLessThan(0))throw $t("value","0",e.toString())})}function Io(e,t,n){if(e.isNaN())throw Gt(t,"NaN");if(!e.isFinite())throw Kt(t,"finite");if(e.isLessThanOrEqualTo(0))throw $t(t,"0",e.toString())}function No(e,t,n="0"){const i=po(t);return bo(i)?po(n):po(e).dividedBy(i)}function Do(e,t){return Math.floor(e/t)*t}class Po{static calculateBuyWithExact(e,t){const n=xn(e),i=xn(t),{BASE_PRICE:o,PRICE_SCALING_FACTOR:a,TRADING_FEE_FACTOR:r,GAS_FEE:s}=Zt,c=this.roundUp(o*(Math.exp((i+n)*a)-Math.exp(i*a))/a,8),l=fo(po(c).multipliedBy(r));return{amount:c.toString(),reverseBondingCurveFee:"0",transactionFee:l,gasFee:s}}static calculateBuyWithNative(e,t){const n=xn(e),i=xn(t),{BASE_PRICE:o,PRICE_SCALING_FACTOR:a,TRADING_FEE_FACTOR:r,GAS_FEE:s}=Zt,c=Math.log(n*a/o+Math.exp(i*a))/a-i,l=fo(po(c).multipliedBy(r));return{amount:c.toString(),reverseBondingCurveFee:"0",transactionFee:l,gasFee:s}}static calculateSellWithExact(e,t,n,i,o){const a=xn(e),r=xn(t),s=xn(n),{BASE_PRICE:c,PRICE_SCALING_FACTOR:l,TRADING_FEE_FACTOR:d,GAS_FEE:u}=Zt,h=c*(Math.exp(r*l)-Math.exp((r-a)*l))/l,g=po(h),m=i+r/s*(o-i),p=fo(g.multipliedBy(m),8),f=fo(g.multipliedBy(d));return{amount:h.toString(),reverseBondingCurveFee:p,transactionFee:f,gasFee:u}}static calculateSellWithNative(e,t,n,i,o){const a=xn(e),r=xn(t),s=xn(n),{BASE_PRICE:c,PRICE_SCALING_FACTOR:l,TRADING_FEE_FACTOR:d,GAS_FEE:u}=Zt;if(a>=c*(Math.exp(r*l)-1)/l){const e=po(a),t=i+r/s*(o-i),n=fo(e.multipliedBy(t),8),c=fo(e.multipliedBy(d));return{amount:r.toString(),reverseBondingCurveFee:n,transactionFee:c,gasFee:u}}const h=r-Math.log(Math.exp(r*l)-a*l/c)/l,g=po(a),m=i+r/s*(o-i),p=fo(g.multipliedBy(m),8),f=fo(g.multipliedBy(d));return{amount:h.toString(),reverseBondingCurveFee:p,transactionFee:f,gasFee:u}}static roundUp(e,t){const n=Math.pow(10,t);return Math.ceil(e*n)/n}}class xo extends Error{constructor(e,t,n){super(e),this.filename=t,this.mimeType=n,this.name="FileValidationError"}}function _o(e,t,n){if(null==e)throw new xo("File is required",t,n);if("undefined"!=typeof File&&e instanceof File){const t=Mi.safeParse(e);if(!t.success){const n=t.error.issues.map(e=>e.message).join("; ");throw new xo(n,e.name,e.type)}return}if(Buffer.isBuffer(e)){if(void 0===t||""===t)throw new xo("Filename is required when uploading Buffer objects",t,n);const i=$i.safeParse(e);if(!i.success){const e=i.error.issues.map(e=>e.message).join("; ");throw new xo(e,t,n)}try{qn(t,255,"filename")}catch(e){throw new xo(e.message,t,n)}const o=["image/png","image/jpg","image/jpeg","image/gif","image/webp","image/svg+xml"];if(!o.includes(n))throw new xo(`Invalid file type "${n}" is not allowed. Allowed types: ${o.join(", ")}`,t,n);const a=function(e){if(null==e||""===e)return"";const t=e.lastIndexOf(".");if(-1===t)return"";return e.substring(t).toLowerCase()}(t),r=[".png",".jpg",".jpeg",".gif",".webp",".svg"];if(!r.includes(a))throw new xo(`File extension "${a}" is not allowed. Allowed extensions: ${r.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"}}(a);if(s!==n&&"application/octet-stream"!==s)throw new xo(`File extension "${a}" does not match MIME type "${n}"`,t,n);return}throw new xo("File must be a File object (browser) or Buffer (Node.js)",t,n)}function Fo(e,t,n=1){if(null==e||""===e)return null;if("string"!=typeof e)return null;const i=e.trim();return i.length<n?null:i}function Ro(e,t){return e.toLowerCase()===t.toLowerCase()}function Bo(e,t){return e.toLowerCase().includes(t.toLowerCase())}function Uo(e,t,n,i){return e===n&&t===i||e===i&&t===n}class Oo{validate(e,t={}){const{allowZero:n=!0,minimum:i,maximum:o,maxDecimals:a=Oo.MAX_DECIMAL_PLACES,fieldName:r="amount"}=t;if(!jn(e))throw new wt(`${r} cannot be empty or whitespace-only. Provide a valid numeric string.`,r,"INVALID_NUMERIC_STRING");if(/[eE]/.test(e))throw new wt(`${r} cannot use scientific notation. Use standard decimal format (e.g., "1000" instead of "1e3").`,r,"INVALID_NUMERIC_STRING");const s=xn(e,NaN);if(isNaN(s))throw new wt(`${r} must be a valid numeric string. Received: "${e}"`,r,"INVALID_NUMERIC_STRING");if(!isFinite(s))throw new wt(`${r} must be a finite number. Cannot be Infinity or -Infinity.`,r,"INVALID_NUMERIC_STRING");if(s<0)throw new wt(`${r} must be non-negative. Received: "${e}"`,r,"INVALID_NUMERIC_STRING");if(!n&&0===s)throw new wt(`${r} must be greater than zero. Received: "${e}"`,r,"INVALID_NUMERIC_STRING");const c=po(e),l=c.decimalPlaces()??0;if(l>a)throw new wt(`${r} cannot exceed ${a} decimal places. Received: ${l} decimal places`,r,"PRECISION_EXCEEDED");if(void 0!==i&&Ao(c,i))throw new wt(`${r} must be at least ${i}. Received: "${e}"`,r,"BELOW_MINIMUM");if(void 0!==o&&(d=o,po(c).isGreaterThan(po(d))))throw new wt(`${r} cannot exceed ${o}. Received: "${e}"`,r,"EXCEEDS_MAXIMUM");var d}parseAmount(e,t="0"){return po(e,t)}isValid(e,t={}){try{return null==e||""===e?!1!==t.allowZero:(this.validate(e,t),!0)}catch{return!1}}formatAmount(e,t){return fo(e,t)}clampAmount(e,t,n){const i=po(e),o=po(t),a=po(n);return function(e,t,n){const i=po(e),o=po(t),a=po(n);return i.isGreaterThanOrEqualTo(o)&&i.isLessThanOrEqualTo(a)}(i,o,a)?fo(i):Ao(i,o)?fo(o):fo(a)}}function Lo(e,t){throw new wt(e.join("; "),t,"VALIDATION_ERROR")}function Mo(e){const t=to(e);!t.success&&t.errors&&Lo(t.errors,"tokenName")}function $o(e){const t=io(e);!t.success&&t.errors&&Lo(t.errors,"options")}function Ko(e){const t=zi.safeParse(e);var n;t.success||Lo((n=t.error.issues,Array.isArray(n)?n.map(e=>null!=e&&"object"==typeof e&&"message"in e&&"string"==typeof e.message?e.message:String(e)).filter(e=>""!==e):[]),"options")}function qo(e,t,n=!0){Go.validate(e,{fieldName:t,allowZero:n})}Oo.MAX_DECIMAL_PLACES=18,Oo.MIN_POSITIVE="0.00000000000000001";const Go=new Oo;class Wo{constructor(e,t,n,i,o,a,r="local"){this.http=e,this.tokenResolver=t,this.logger=n,this.bundleHttp=i,this.galaChainHttp=o,this.dexApiHttp=a,this.defaultCalculateAmountMode=r,this.metadataCache=new li}addIfDefined(e,t,n){return void 0!==n&&(e[t]=n),e}async uploadImageByTokenName(e){const{tokenName:t,options:n}=e;Mo(t);const i=`${t}.png`;_o(n.file,i,"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 Rt("file","a File object (browser) or Buffer (Node.js)");{const i=`${void 0!==n.tokenName&&""!==n.tokenName?n.tokenName:t}.png`,o=new Blob([n.file],{type:"image/png"});e.append("image",o,i)}}const i=await this.http.request({method:"POST",url:`/launchpad/upload-image?tokenName=${encodeURIComponent(void 0!==n.tokenName&&""!==n.tokenName?n.tokenName:t)}`,data:e,headers:{}});if(!0===i.error||200!==i.status)throw Bt(void 0!==i.message&&""!==i.message?i.message:"Image upload failed - no URL returned",i.status);const o=Vt(i);if(!o?.imageUrl)throw Bt("Image upload failed - no URL returned",i.status);return o.imageUrl}catch(e){if(ot(e)&&e.message.includes("FormData"))throw Ut("File upload failed: FormData not supported in this environment. Ensure you have proper polyfills for Node.js environments.","FormData");throw e}}async fetchPoolsFromAPI(e){void 0!==e.pageSize&&Qn(0,void 0,100,e.pageSize),void 0!==e.tokenName&&""!==e.tokenName&&Mo(e.tokenName);const t={pageSize:String(e.pageSize??10)};void 0!==e.cursor&&(t.cursor=e.cursor),void 0!==e.type&&(t.type=e.type),void 0!==e.tokenName&&(t.tokenName=e.tokenName),void 0!==e.search&&(t.search=e.search),void 0!==e.hasUpcomingShows&&(t.hasUpcomingShows=e.hasUpcomingShows.toString()),void 0!==e.language&&(t.language=e.language),void 0!==e.recentlyStreamed&&(t.recentlyStreamed=e.recentlyStreamed.toString());const n=mo(t),i=await this.http.get("/launchpad/fetch-pool",n);if(!0===i.error||200!==i.status)throw Bt(void 0!==i.message&&""!==i.message?i.message:"Failed to fetch pools",i.status);const o=Vt(i);if(null==o)throw Bt("Failed to fetch pools - no data returned",i.status);let a=[];if(o.tokens)if(Array.isArray(o.tokens))a=o.tokens.map(e=>{const t=e.reverseBondingCurveMinFeePortion??"0",n=e.reverseBondingCurveMaxFeePortion??"0",i=!bo(t)||!bo(n);return{...e,reverseBondingCurveMinFeePortion:t,reverseBondingCurveMaxFeePortion:n,hasReverseBondingCurveFee:i,createdAt:void 0!==e.created_at&&""!==e.created_at?e.created_at:void 0!==e.createdAt&&""!==e.createdAt?e.createdAt:""}});else{const e=o.tokens,t=e.reverseBondingCurveMinFeePortion??"0",n=e.reverseBondingCurveMaxFeePortion??"0",i=!bo(t)||!bo(n);a=[{...e,reverseBondingCurveMinFeePortion:t,reverseBondingCurveMaxFeePortion:n,hasReverseBondingCurveFee:i,createdAt:void 0!==e.created_at&&""!==e.created_at?e.created_at:void 0!==e.createdAt&&""!==e.createdAt?e.createdAt:""}]}else o.pools&&Array.isArray(o.pools)&&(a=o.pools.map(e=>{const t=e.reverseBondingCurveMinFeePortion??"0",n=e.reverseBondingCurveMaxFeePortion??"0",i=!bo(t)||!bo(n);return{...e,reverseBondingCurveMinFeePortion:t,reverseBondingCurveMaxFeePortion:n,hasReverseBondingCurveFee:i,createdAt:void 0!==e.created_at&&""!==e.created_at?e.created_at:void 0!==e.createdAt&&""!==e.createdAt?e.createdAt:""}}));const{extractMetadataFromPoolData:r,isValidPoolForCaching:s}=await Promise.resolve().then(function(){return Eu});a.forEach(e=>{if(!s(e))return void this.logger.debug("Skipping pool with invalid structure for caching",e);const t=r(e,this.logger);t&&this.warmCacheFromPoolData(e.tokenName,t)});const c=o.count??o.total??0,l={hasNextPage:o.hasNextPage??!1};return void 0!==o.hasPrevPage&&(l.hasPrevPage=o.hasPrevPage),void 0!==o.nextCursor&&(l.nextCursor=o.nextCursor),void 0!==o.prevCursor&&(l.prevCursor=o.prevCursor),c>0&&(l.totalCount=c),{items:a,pageInfo:l}}async checkPool(e){$o(e),void 0!==e.tokenName&&""!==e.tokenName&&Mo(e.tokenName);const t=mo(e),n=await this.http.get("/launchpad/check-pool",t);if(!0===n.error||200!==n.status)throw Bt(void 0!==n.message&&""!==n.message?n.message:"Failed to check pool",n.status);const i=n.data;return void 0!==e.symbol&&""!==e.symbol?i?.isSymbolExist??!1:void 0!==e.tokenName&&""!==e.tokenName?i?.isNameExist??!1:i?.exists??!1}async fetchVolumeData(e){if(!co(e))throw Rt("options","{ tokenName: string, from?: number, to?: number, resolution?: number }");const{tokenName:t,from:n,to:i,resolution:o}=e;if(Mo(t),null==n||null==i||null==o)throw Ft("graphOptions","Graph options (from, to, resolution)");const a={tokenName:t,from:n,to:i,resolution:o};Ko(a);const r=mo(a),s=await this.http.get("/launchpad/get-graph-data",r);if(!0===s.error||200!==s.status)throw Bt(void 0!==s.message&&""!==s.message?s.message:"Failed to fetch graph data",s.status);const c=Vt(s);if(!c)throw Bt("Failed to fetch graph data - no data returned",s.status);return{dataPoints:c}}async fetchPools(e={}){let t;"recent"===e.type?t="RECENT":"popular"===e.type&&(t="POPULAR");const n={pageSize:e.pageSize??10};return void 0!==e.cursor&&(n.cursor=e.cursor),void 0!==e.search&&""!==e.search&&(n.search=e.search),void 0!==e.tokenName&&""!==e.tokenName&&(n.tokenName=e.tokenName),null!=t&&(n.type=t),void 0!==e.hasUpcomingShows&&(n.hasUpcomingShows=e.hasUpcomingShows),void 0!==e.language&&""!==e.language&&(n.language=e.language),void 0!==e.recentlyStreamed&&(n.recentlyStreamed=e.recentlyStreamed),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(null==e||"object"!=typeof e)throw Rt("options","an options object");const{tokenName:t,amount:n,type:i,currentSupply:o}=e,a=e.mode??this.defaultCalculateAmountMode;if("local"!==a&&"external"!==a)throw qt("mode",'"local" or "external"',a);if(!jn(t))throw Ft("tokenName","Token name");if(!jn(n))throw Ft("amount","Amount");if(i!==nt&&i!==it)throw qt("type",'"native" or "exact"',i);return"external"===a?this.calculateBuyAmountExternal({tokenName:t,amount:n,type:i}):this.calculateBuyAmountLocal(this.addIfDefined({tokenName:t,amount:n,type:i},"currentSupply",o))}async calculateBuyAmountExternal(e){const{tokenName:t,amount:n,type:i}=e;if("string"!=typeof t||""===t)throw new wt("Token name must be a non-empty string","tokenName","INVALID_TOKEN_NAME");if("string"!=typeof n||""===n)throw new wt("Amount must be a non-empty string","amount","INVALID_AMOUNT");return"exact"===i?Xt(()=>this.http.get(Ve,{tokenName:t,amount:n,type:"MEME",method:"OUT"}),{errorContext:`Failed to calculate buy amount for ${t} (exact tokens)`}):Xt(()=>this.http.get(Ve,{tokenName:t,amount:n,type:"NATIVE",method:"IN"}),{errorContext:`Failed to calculate buy amount for ${t} (native/GALA budget)`})}async calculateSellAmount(e){const{tokenName:t,amount:n,type:i,currentSupply:o,maxSupply:a,reverseBondingCurveMaxFeeFactor:r,reverseBondingCurveMinFeeFactor:s}=e,c=e.mode??this.defaultCalculateAmountMode;if("local"!==c&&"external"!==c)throw qt("mode",'"local" or "external"',c);if(!jn(t))throw Ft("tokenName","Token name");if(!jn(n))throw Ft("amount","Amount");if(i!==it&&i!==nt)throw qt("type",'"exact" or "native"',i);if("external"===c)return this.calculateSellAmountExternal({tokenName:t,amount:n,type:i});{const e={tokenName:t,amount:n,type:i,...void 0!==o&&{currentSupply:o},...void 0!==a&&{maxSupply:a},...void 0!==r&&{reverseBondingCurveMaxFeeFactor:r},...void 0!==s&&{reverseBondingCurveMinFeeFactor:s}};return this.calculateSellAmountLocal(e)}}async calculateSellAmountExternal(e){const{tokenName:t,amount:n,type:i}=e;if("string"!=typeof t||""===t)throw new wt("Token name must be a non-empty string","tokenName","INVALID_TOKEN_NAME");if("string"!=typeof n||""===n)throw new wt("Amount must be a non-empty string","amount","INVALID_AMOUNT");return"exact"===i?Xt(()=>this.http.get(Ve,{tokenName:t,amount:n,type:"MEME",method:"IN"}),{errorContext:`Failed to calculate sell amount for ${t} (exact tokens)`}):Xt(()=>this.http.get(Ve,{tokenName:t,amount:n,type:"NATIVE",method:"OUT"}),{errorContext:`Failed to calculate sell amount for ${t} (native/GALA target)`})}async calculateBuyAmountLocal(e){const{tokenName:t,amount:n,type:i,currentSupply:o}=e;if(!jn(n))throw Ft("amount","Amount");if(i!==nt&&i!==it)throw qt("type",'"native" or "exact"',i);void 0!==o&&qo(o,"currentSupply");const a=null==o||""===o;if(a&&(null==t||""===t))throw Ft("tokenName","Token name (required when currentSupply is not provided)");null!=t&&""!==t&&Mo(t);let r=o;if(a){r=(await this.fetchPoolDetailsForCalculation(t)).currentSupply}return i===it?Po.calculateBuyWithExact(n,r):Po.calculateBuyWithNative(n,r)}async calculateSellAmountLocal(e){const{tokenName:t,amount:n,type:i,currentSupply:o,maxSupply:a,reverseBondingCurveMaxFeeFactor:r,reverseBondingCurveMinFeeFactor:s}=e;if(!jn(n))throw Ft("amount","Amount");if(i!==it&&i!==nt)throw qt("type",'"exact" or "native"',i);void 0!==o&&qo(o,"currentSupply");const c=null==o||""===o||null==a||""===a||void 0===r||void 0===s;if(c&&(null==t||""===t))throw Ft("tokenName","Token name (required when currentSupply, maxSupply, or fee factors are not provided)");null!=t&&""!==t&&Mo(t);let l=o,d=a,u=r,h=s;if(c&&null!=t&&""!==t){const e=this.metadataCache.getByName(t);d=d??this.metadataCache.getMaxSupply(t),u=u??e?.reverseBondingCurveMaxFeeFactor,h=h??e?.reverseBondingCurveMinFeeFactor,null!=l&&""!==l||(l=await this.fetchCurrentSupply(t));if(void 0===u||void 0===h){const e=await this.fetchPoolDetailsForCalculation(t);u=u??e.reverseBondingCurveMaxFeeFactor,h=h??e.reverseBondingCurveMinFeeFactor}}return i===it?Po.calculateSellWithExact(n,l,d,h,u):Po.calculateSellWithNative(n,l,d,h,u)}async calculateBuyAmountForGraduation(e){const t="string"==typeof e?{tokenName:e}:e;if("object"==typeof e&&!function(e){if(null==e||"object"!=typeof e)return!1;const t=e;return ao(t,"tokenName")&&function(e){return void 0===e.calculateAmountMode||"local"===e.calculateAmountMode||"external"===e.calculateAmountMode}(t)&&ro(t,"currentSupply")}(e))throw qt("options","CalculateBuyAmountForGraduationOptions or string (token name)",typeof e);const{tokenName:n,calculateAmountMode:i,currentSupply:o}=t;Mo(n);const a=await this.tokenResolver.resolveTokenToVault(n);if(null==a||""===a)throw new wt(gt(n),"tokenName","VAULT_NOT_FOUND");if(void 0===this.galaChainHttp||null===this.galaChainHttp)throw Ut("GalaChain HTTP client not configured");const r=await this.galaChainHttp.post("/api/asset/launchpad-contract/FetchSaleDetails",{vaultAddress:a});if(1!==r.Status)throw Bt(`Failed to fetch pool details: Status ${r.Status}`,r.Status);const s=r.Data,c=o??fo(po(s.maxSupply).minus(s.sellingTokenQuantity)),l=s.sellingTokenQuantity;if("0"===l)throw Ft("tokenName",`Token ${n} is already graduated (no tokens remaining in pool)`);const d={tokenName:n,amount:l,type:"exact",currentSupply:c,...void 0!==i&&{mode:i}};return{...await this.calculateBuyAmount(d),remainingTokens:l}}async launchToken(e){if(!this.bundleHttp)throw Ut("Bundle backend client not configured. LaunchToken requires bundleHttp client.","bundleHttp");oo(e);const t=void 0!==e.preBuyQuantity&&""!==e.preBuyQuantity?e.preBuyQuantity:"0",o=xn(t);if(0===o&&"0"!==t)throw Gt("preBuyQuantity",t,"Pre-buy quantity");if(o<0)throw Gt("preBuyQuantity",t,"Pre-buy quantity");if(e.reverseBondingCurveConfiguration){const{minFeePortion:t,maxFeePortion:n}=e.reverseBondingCurveConfiguration;!function(e,t,n){const i="string"==typeof e?parseFloat(e):e,o="string"==typeof t?parseFloat(t):t;if(!isFinite(i))throw new wt(`${n} minFee must be a valid finite number`,n,"INVALID_FEE_MINIMUM");if(!isFinite(o))throw new wt(`${n} maxFee must be a valid finite number`,n,"INVALID_FEE_MAXIMUM");if(i<.1)throw new wt(`${n} minFee must be >= 0.1, received ${i}`,n,"INVALID_FEE_MINIMUM");if(o>.5)throw new wt(`${n} maxFee must be <= 0.5, received ${o}`,n,"INVALID_FEE_MAXIMUM");if(i>o)throw new wt(`${n} minFee (${i}) must be <= maxFee (${o})`,n,"INVALID_FEE_RANGE")}(t,n,"reverseBondingCurve")}let a="https://via.placeholder.com/200?text=Token";if(void 0!==e.tokenImage&&null!==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(null==t||""===t)throw Bt("Image upload failed: No URL returned");a=t}else"string"==typeof e.tokenImage&&(a=e.tokenImage);const r=`galaswap - operation - ${i.v4()}-${Date.now()}-${this.http.getAddress()}`,s=(void 0!==t&&""!==t?t:"0").toString(),c=void 0!==e.tokenCategory&&""!==e.tokenCategory?e.tokenCategory:"Unit",l=void 0!==e.tokenCollection&&""!==e.tokenCollection?e.tokenCollection:"Token",d={tokenName:e.tokenName.trim(),tokenSymbol:nn(e.tokenSymbol),tokenDescription:e.tokenDescription.trim(),tokenImage:a.trim(),preBuyQuantity:s,tokenCategory:c,tokenCollection:l,uniqueKey:r},u=Fo(e.websiteUrl);null!==u&&(d.websiteUrl=u);const h=Fo(e.telegramUrl);null!==h&&(d.telegramUrl=h);const g=Fo(e.twitterUrl);null!==g&&(d.twitterUrl=g);const m=Fo(e.instagramUrl);null!==m&&(d.instagramUrl=m);const p=Fo(e.facebookUrl);null!==p&&(d.facebookUrl=p);const f=Fo(e.redditUrl);null!==f&&(d.redditUrl=f);const y=Fo(e.tiktokUrl);null!==y&&(d.tiktokUrl=y);const k=e.reverseBondingCurveConfiguration?.minFeePortion?.toString()??"0.1",v=e.reverseBondingCurveConfiguration?.maxFeePortion?.toString()??"0.5";d.reverseBondingCurveConfiguration={minFeePortion:k,maxFeePortion:v},void 0!==e.saleStartTime&&null!==e.saleStartTime&&(d.saleStartTime=e.saleStartTime);const w=Object.fromEntries(Object.entries(d).filter(([e,t])=>void 0!==t)),b=new di(w),S=await this.http.signWithGalaChain("CreateSale",b,n.SigningType.SIGN_TYPED_DATA),{signature:A,types:T,domain:E,prefix:C}=S,I=null!=C?{prefix:C}:{},N=void 0!==b.reverseBondingCurveConfiguration&&null!==b.reverseBondingCurveConfiguration?{reverseBondingCurveConfiguration:b.reverseBondingCurveConfiguration}:{},D=void 0!==b.saleStartTime&&null!==b.saleStartTime?{saleStartTime:b.saleStartTime}:{},P={tokenName:b.tokenName,tokenSymbol:b.tokenSymbol,tokenDescription:b.tokenDescription,tokenImage:b.tokenImage,preBuyQuantity:b.preBuyQuantity,...void 0!==b.websiteUrl&&""!==b.websiteUrl?{websiteUrl:b.websiteUrl}:{},...void 0!==b.telegramUrl&&""!==b.telegramUrl?{telegramUrl:b.telegramUrl}:{},...void 0!==b.twitterUrl&&""!==b.twitterUrl?{twitterUrl:b.twitterUrl}:{},...void 0!==b.instagramUrl&&""!==b.instagramUrl?{instagramUrl:b.instagramUrl}:{},...void 0!==b.facebookUrl&&""!==b.facebookUrl?{facebookUrl:b.facebookUrl}:{},...void 0!==b.redditUrl&&""!==b.redditUrl?{redditUrl:b.redditUrl}:{},...void 0!==b.tiktokUrl&&""!==b.tiktokUrl?{tiktokUrl:b.tiktokUrl}:{},tokenCategory:b.tokenCategory,tokenCollection:b.tokenCollection,uniqueKey:b.uniqueKey,signature:A,types:T,domain:E,...I,...N,...D},x=`${e.tokenName.trim()}$Unit$none$none`,_="GALA$Unit$none$none";let F;if(xn(t,0)>0){const e=`$service$${x}$launchpad`;F=[e,`$token$${x}$${e}`,`$tokenBalance$${x}$${e}`,`$tokenBalance$${x}$${e}`,`$tokenBalance$${_}$${e}`,`$tokenBalance$${_}$${e}`]}else{const e=`$service$${x}$launchpad`;F=[e,`$token$${x}$${e}`,`$tokenBalance$${x}$${e}`]}const R={signedDto:P,stringsInstructions:F,method:"CreateSale"},B=await this.bundleHttp.post("/bundle",R);if(B.error)throw Bt(void 0!==B.message&&""!==B.message?B.message:"Token launch failed");const U=Vt(B);if(null==U||""===U)throw Bt("Token launch failed - no transaction ID returned");return U}async fetchTokenDistribution(e){if(null==e||""===e)throw Ft("tokenName","Token name");Mo(e);const t=await this.http.get(`/holders/${e}`);if(!0===t.error||200!==t.status)throw Bt(void 0!==t.message&&""!==t.message?t.message:"Failed to fetch token distribution",t.status);const n=Vt(t);if(null==n)throw Bt("Failed to fetch token distribution - no data returned",t.status);if(!Array.isArray(n))throw Bt("Invalid API response: expected array of holders",t.status);for(const e of n){if(void 0===e.owner||null===e.owner||""===e.owner||"string"!=typeof e.owner)throw Bt("Invalid holder data: missing or invalid owner field",t.status);if(void 0===e.quantity||null===e.quantity||""===e.quantity||"string"!=typeof e.quantity)throw Bt("Invalid holder data: missing or invalid quantity field",t.status);const n=xn(e.quantity,NaN);if(isNaN(n)||!isFinite(n))throw Bt(`Invalid holder quantity: "${e.quantity}"`,t.status)}const i=n.reduce((e,t)=>e.plus(t.quantity),po(0));return{holders:n.map(e=>{const t=No(po(e.quantity),i,po(0)).multipliedBy(100).toNumber();return{address:e.owner,balance:e.quantity,percentage:t}}),totalSupply:fo(i),totalHolders:n.length,lastUpdated:new Date}}async fetchTokenBadges(e){if(null==e||""===e)throw Ft("tokenName","Token name");Mo(e);const t=await this.http.get("/launchpad/get-badge/",{tokenName:e});if(t.error)throw Bt(void 0!==t.message&&""!==t.message?t.message:"Failed to fetch token badges");const n=Vt(t);if(null==n)throw Bt("Failed to fetch token badges - no data returned");return{volumeBadges:Array.isArray(n.volumeBadge)?n.volumeBadge:[],engagementBadges:Array.isArray(n.engagementBadge)?n.engagementBadge:[]}}async hasTokenBadgeByTokenName(e){const{tokenName:t,badgeType:n,badgeName:i}=e;try{const e=await this.fetchTokenBadges(t);if(null==e)return!1;const o=("volume"===n?e.volumeBadges:e.engagementBadges).find(e=>e.badgeName===i);return o?.isActive??!1}catch{return!1}}async calculateInitialBuyAmount(e){if(!go(e))throw Rt("data","valid pre-mint calculation data");if(!this.galaChainHttp)throw Ut("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(!function(e){if(null==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}(n))throw Bt("Malformed response data from GalaChain gateway");try{lt(n,"Pre-mint calculation")}catch(e){throw Bt(at(e),500)}const{calculatedQuantity:i,extraFees:o}=n.Data;return{amount:i,reverseBondingCurveFee:o.reverseBondingCurve,transactionFee:o.transactionFees,gasFee:"1"}}catch(e){if(ot(e)&&e instanceof bt)throw e;throw Bt(at(e),500)}}async fetchPoolDetailsForCalculation(e){const t=await this.tokenResolver.resolveTokenToVault(e);if(null==t||""===t)throw new wt(gt(e),"tokenName","VAULT_NOT_FOUND");if(void 0===this.galaChainHttp||null===this.galaChainHttp)throw Ut("GalaChain HTTP client not configured");const n=await this.galaChainHttp.post("/api/asset/launchpad-contract/FetchSaleDetails",{vaultAddress:t});if(1!==n.Status)throw Bt(`Failed to fetch pool details: Status ${n.Status}`,n.Status);const i=n.Data,o=fo(po(i.maxSupply).minus(i.sellingTokenQuantity)),a=i.sellingTokenQuantity,r=i.maxSupply;let s=.5,c=.1;const l=i.reverseBondingCurveConfiguration;null!=l?.maxFeePortion&&(s=On(l.maxFeePortion,.5)),null!=l?.minFeePortion&&(c=On(l.minFeePortion,.1)),void 0===l&&this.logger.debug(`Pool details missing reverse bonding curve configuration for token ${e}, using defaults (min: 0.1, max: 0.5)`);const d=s-c;return this.metadataCache.set(e,{maxSupply:r,reverseBondingCurveMaxFeeFactor:s,reverseBondingCurveMinFeeFactor:c,reverseBondingCurveNetFeeFactor:d}),{currentSupply:o,remainingTokens:a,maxSupply:r,reverseBondingCurveMaxFeeFactor:s,reverseBondingCurveMinFeeFactor:c,reverseBondingCurveNetFeeFactor:d}}async fetchCurrentSupply(e){Mo(e);const t=await this.tokenResolver.resolveTokenToVault(e);if(null==t||""===t)throw new wt(gt(e),"tokenName","VAULT_NOT_FOUND");if(void 0===this.galaChainHttp||null===this.galaChainHttp)throw Ut("GalaChain HTTP client not configured");const n=await this.galaChainHttp.post("/api/asset/launchpad-contract/FetchSaleDetails",{vaultAddress:t});if(1!==n.Status)throw Bt(`Failed to fetch pool details: Status ${n.Status}`,n.Status);const i=n.Data,o=fo(po(i.maxSupply).minus(i.sellingTokenQuantity)),a=i.maxSupply;return this.metadataCache.set(e,{maxSupply:a}),o}getAddress(){return this.http.getAddress()}formatAddressForBackend(e){return fi(e)}validateTokenName(e){return Mo(e)}validatePagination(e){void 0!==e.pageSize&&Qn(0,void 0,100,e.pageSize)}async fetchTokenPrice(e){if(void 0===this.dexApiHttp||null===this.dexApiHttp)throw Ut("DEX API client not configured. Token price fetching requires dexApiHttp client.","dexApiHttp");if(null==e||""===e||Array.isArray(e)&&0===e.length)throw Ft("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 void 0!==e.tokens&&null!==e.tokens&&Array.isArray(e.tokens)&&e.tokens.forEach(e=>{void 0!==e.currentPrices&&null!==e.currentPrices&&void 0!==e.symbol&&null!==e.symbol&&n.push({symbol:e.symbol,price:e.currentPrices.usd})}),n}catch(e){throw Bt(`Failed to fetch token prices: ${at(e)}`,void 0,ot(e)?e:void 0)}}warmCacheFromPoolData(e,t){this.metadataCache.warmFromPoolData(e,t)}getCacheStats(){return this.metadataCache.getStats()}clearCache(e){this.metadataCache.clear(e)}}const Ho=3e5,zo="Sign in to Launchpad t:",jo="Authorization",Vo="Bearer ";class Qo{constructor(){this.data=new Map}async get(e){return this.data.get(e)??null}async set(e,t){this.data.set(e,t)}async remove(e){this.data.delete(e)}}var Xo,Jo;!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"}(Xo||(Xo={}));class Yo extends Error{constructor(e,t,n){super(t),this.type=e,this.originalError=n,this.name="AuthError"}}class Zo{constructor(e,t=Ho){this.tokenState=null,this.STORAGE_KEYS={ACCESS_TOKEN:"lpad_access_token",ACCESS_EXPIRES:"lpad_access_expires",REFRESH_TOKEN:"lpad_refresh_token",REFRESH_EXPIRES:"lpad_refresh_expires"},this.storage=e??new Qo,this.refreshThresholdMs=t}setToken(e,t){if(!jn(e))throw new Yo(Xo.SIGNATURE_FAILED,"JWT token must be a non-empty string");if("number"!=typeof t)throw new Yo(Xo.SIGNATURE_FAILED,"Token expiration must be a positive finite number (seconds)");if(!Number.isFinite(t))throw new Yo(Xo.SIGNATURE_FAILED,"Token expiration must be a positive finite number (seconds)");if(t<=0)throw new Yo(Xo.SIGNATURE_FAILED,"Token expiration must be a positive finite number (seconds)");const n=Date.now();this.tokenState={token:e.trim(),issuedAt:n,expiresAt:n+1e3*t},this.storage.set(this.STORAGE_KEYS.ACCESS_TOKEN,e.trim()),this.storage.set(this.STORAGE_KEYS.ACCESS_EXPIRES,String(this.tokenState.expiresAt))}async setTokens(e,t,n,i){if(this.setToken(e,t),n){const e=Date.now()+1e3*(i??7776e3);if(!this.tokenState)throw new Yo(Xo.SIGNATURE_FAILED,"Access token must be set before refresh token");this.tokenState.refreshToken=n.trim(),this.tokenState.refreshExpiresAt=e,await this.storage.set(this.STORAGE_KEYS.REFRESH_TOKEN,n.trim()),await this.storage.set(this.STORAGE_KEYS.REFRESH_EXPIRES,String(e))}}getToken(){return this.tokenState?.token??null}hasToken(){return null!==this.tokenState}isExpired(){return!this.tokenState||Date.now()>=this.tokenState.expiresAt}shouldRefresh(e){if(!this.tokenState)return!1;const t=e??this.refreshThresholdMs,n=this.tokenState.expiresAt-Date.now();return n>0&&n<=t}getTimeUntilExpiry(){if(!this.tokenState)return 0;const e=this.tokenState.expiresAt-Date.now();return Math.max(0,e)}getExpiresAt(){return this.tokenState?.expiresAt??null}getAuthorizationHeader(){const e=this.getToken();return null===e||""===e?null:`${Vo}${e}`}getJwtHeaders(){const e=this.getAuthorizationHeader();if(null===e||""===e)throw new Yo(Xo.WALLET_NOT_CONNECTED,"No JWT token available. Call login() first.");return{[jo]:e}}async getRefreshToken(){if(!this.tokenState?.refreshToken){const e=await this.storage.get(this.STORAGE_KEYS.REFRESH_TOKEN),t=await this.storage.get(this.STORAGE_KEYS.REFRESH_EXPIRES);if(e&&t){const n=Number(t);if(Number.isFinite(n)&&Date.now()<n)return e}return null}return null!=this.tokenState.refreshExpiresAt&&(!Number.isFinite(this.tokenState.refreshExpiresAt)||Date.now()>=this.tokenState.refreshExpiresAt)?null:this.tokenState.refreshToken??null}async restoreFromStorage(){const e=await this.storage.get(this.STORAGE_KEYS.ACCESS_TOKEN),t=await this.storage.get(this.STORAGE_KEYS.ACCESS_EXPIRES);if(!e||!t)return!1;const n=Number(t);if(!Number.isFinite(n)||Date.now()>=n)return!1;const i=Date.now();this.tokenState={token:e,issuedAt:i,expiresAt:n};const o=await this.storage.get(this.STORAGE_KEYS.REFRESH_TOKEN),a=await this.storage.get(this.STORAGE_KEYS.REFRESH_EXPIRES);if(o&&a){const e=Number(a);Number.isFinite(e)&&Date.now()<e&&(this.tokenState.refreshToken=o,this.tokenState.refreshExpiresAt=e)}return!0}async clear(){this.tokenState=null,await this.storage.remove(this.STORAGE_KEYS.ACCESS_TOKEN),await this.storage.remove(this.STORAGE_KEYS.ACCESS_EXPIRES),await this.storage.remove(this.STORAGE_KEYS.REFRESH_TOKEN),await this.storage.remove(this.STORAGE_KEYS.REFRESH_EXPIRES)}isValid(){return this.hasToken()&&!this.isExpired()}getDebugInfo(){return{hasToken:this.hasToken(),isExpired:this.isExpired(),shouldRefresh:this.shouldRefresh(),timeUntilExpiryMs:this.getTimeUntilExpiry(),expiresAt:this.tokenState?new Date(this.tokenState.expiresAt):null}}}async function ea(e,t,n,i){try{return await e()}catch(e){throw i?(i(e,t,n),new Error("Unreachable after error handler")):(n&&n.error(`${t}:`,e),Lt(e,t,n))}}function ta(e,t,n,i){try{return e()}catch(e){throw i?(i(e,t,n),new Error("Unreachable after error handler")):(n&&n.error(`${t}:`,e),Lt(e,t,n))}}class na{constructor(e,t,n,i=!1){this.http=e,this.signatureAuth=t,this.jwtAuth=n,this.refreshPromise=null,this.loginPromise=null,this.logger=new an({debug:i,context:"SessionAuthService"})}async login(){if(this.loginPromise)return this.logger.debug("Login already in progress, reusing existing promise"),this.loginPromise;if(!this.signatureAuth.hasWallet())throw new Yo(Xo.WALLET_NOT_CONNECTED,"Wallet is required for login. Configure privateKey in SDK.");this.loginPromise=this.performLogin();try{return await this.loginPromise}finally{this.loginPromise=null}}async performLogin(){return ea(async()=>{const e=Date.now(),t=`${zo}${e}`;this.logger.debug("Generating login signature",{message:t,timestamp:e});const n=await this.signatureAuth.signMessage(t),i=this.signatureAuth.getAddress();vi(i,"address");const o={address:i,message:t,signature:n.signature};let a;try{this.logger.debug("Sending login request with refresh token support",{address:i,message:t}),a=await this.http.post(B,{...o,includeRefreshToken:!0})}catch(e){if(400!==e.statusCode)throw e;this.logger.debug("Login returned 400, retrying without includeRefreshToken (backend may not support it)"),a=await this.http.post(B,o)}const r=this.extractLoginData(a);return await this.jwtAuth.setTokens(r.accessToken,r.expiresIn,r.refreshToken,r.refreshExpiresIn),this.logger.debug("Login successful",{address:r.address,expiresIn:r.expiresIn,hasRefreshToken:!!r.refreshToken}),r},"SessionAuthService.performLogin",this.logger,(e,t,n)=>{if(e instanceof Yo)throw e;throw new Yo(Xo.SIGNATURE_FAILED,`Login failed: ${at(e)}`,ot(e)?e:void 0)})}async refresh(){if(this.refreshPromise)return this.logger.debug("Refresh already in progress, reusing existing promise"),this.refreshPromise;if(!this.jwtAuth.hasToken())throw new Yo(Xo.WALLET_NOT_CONNECTED,"No token to refresh. Call login() first.");this.refreshPromise=this.performRefresh();try{return await this.refreshPromise}finally{this.refreshPromise=null}}async performRefresh(){return ea(async()=>{this.logger.debug("Refreshing JWT token");const e=await this.jwtAuth.getRefreshToken();if(e){this.logger.debug("Using refresh token flow");const t=await this.http.post(O,{refreshToken:e}),n=this.extractLoginData(t);return await this.jwtAuth.setTokens(n.accessToken,n.expiresIn,n.refreshToken,n.refreshExpiresIn),this.logger.debug("Token refreshed successfully via refresh token",{address:n.address,expiresIn:n.expiresIn,hasNewRefreshToken:!!n.refreshToken}),n}this.logger.debug("Using legacy access token refresh");const t=await this.http.post(O,{},this.jwtAuth.getJwtHeaders()),n=this.extractLoginData(t);return this.jwtAuth.setToken(n.accessToken,n.expiresIn),this.logger.debug("Token refreshed successfully via access token",{address:n.address,expiresIn:n.expiresIn}),n},"SessionAuthService.performRefresh",this.logger,(e,t,n)=>{if(e instanceof Yo)throw e;throw new Yo(Xo.SIGNATURE_FAILED,`Token refresh failed: ${at(e)}`,ot(e)?e:void 0)})}async logout(e){this.logger.debug("Logging out",{allDevices:e?.allDevices});const t=e?.allDevices??!1;if(t&&this.jwtAuth.hasToken()&&!this.jwtAuth.isExpired())try{await this.http.post(U,{allDevices:!0},this.jwtAuth.getJwtHeaders()),this.logger.debug("Logged out from all devices")}catch(e){this.logger.warn(`Failed to revoke all-device sessions (server-side revocation may be incomplete): ${at(e)}`)}else t&&this.logger.debug("Skipping server-side revocation: no valid local token available");await this.jwtAuth.clear()}async getSession(){if(!this.jwtAuth.hasToken())throw new Yo(Xo.WALLET_NOT_CONNECTED,"Not authenticated. Call login() first.");return ea(async()=>{const e=await this.http.get(L,void 0,this.jwtAuth.getJwtHeaders());return this.extractSessionData(e)},"SessionAuthService.getSession",this.logger,(e,t,n)=>{if(e instanceof Yo)throw e;throw new Yo(Xo.SIGNATURE_FAILED,`Failed to get session: ${at(e)}`,ot(e)?e:void 0)})}getAccessToken(){return this.jwtAuth.getToken()}isAuthenticated(){return this.jwtAuth.isValid()}shouldRefresh(e){return this.jwtAuth.shouldRefresh(e)}async ensureValidToken(e){if(!this.jwtAuth.hasToken())throw new Yo(Xo.WALLET_NOT_CONNECTED,"Not authenticated. Call login() first.");if(this.jwtAuth.isExpired()){if(await this.jwtAuth.getRefreshToken()){this.logger.debug("Token expired - refresh token available, attempting refresh");try{return(await this.refresh()).accessToken}catch(e){this.logger.warn(`Refresh failed after expiry: ${at(e)} - attempting re-login`)}}else this.logger.debug("Token expired - no refresh token available, attempting re-login");return(await this.login()).accessToken}if(this.jwtAuth.shouldRefresh(e)){this.logger.debug("Token near expiry - refreshing");return(await this.refresh()).accessToken}return this.jwtAuth.getToken()}extractLoginData(e){if(e.error)throw new Yo(Xo.SIGNATURE_FAILED,""!==at(e)?at(e):"Authentication failed");const t=Qt(e,"No data in authentication response"),{accessToken:n,expiresIn:i,address:o,refreshToken:a,refreshExpiresIn:r}=t;if(!jn(n))throw new Yo(Xo.SIGNATURE_FAILED,"Invalid access token in response");if("number"!=typeof i||i<=0)throw new Yo(Xo.SIGNATURE_FAILED,"Invalid expiration in response");return{accessToken:n,expiresIn:i,address:o,...void 0!==a&&{refreshToken:a},...void 0!==r&&{refreshExpiresIn:r}}}extractSessionData(e){if(e.error)throw new Yo(Xo.SIGNATURE_FAILED,""!==at(e)?at(e):"Failed to get session");return Qt(e,"No data in session response")}}class ia extends Error{constructor(e,t,n){super(e),this.code=t,this.originalError=n,this.name="WalletProviderError"}}!function(e){e.USER_REJECTED="USER_REJECTED",e.NOT_CONNECTED="NOT_CONNECTED",e.CONNECTION_FAILED="CONNECTION_FAILED",e.SIGNING_FAILED="SIGNING_FAILED",e.PROVIDER_NOT_AVAILABLE="PROVIDER_NOT_AVAILABLE",e.PRIVATE_KEY_NOT_ACCESSIBLE="PRIVATE_KEY_NOT_ACCESSIBLE",e.INVALID_CONFIG="INVALID_CONFIG",e.CHAIN_MISMATCH="CHAIN_MISMATCH",e.UNKNOWN="UNKNOWN"}(Jo||(Jo={}));class oa{constructor(e){this.providerType="privateKey",this.connected=!0;try{this.wallet=new t.Wallet(e)}catch(e){throw new ia("Invalid private key provided",Jo.INVALID_CONFIG,e instanceof Error?e:void 0)}}static fromWallet(e){return new oa(e.privateKey)}async signMessage(e){try{return await this.wallet.signMessage(e)}catch(e){throw new ia("Failed to sign message",Jo.SIGNING_FAILED,e instanceof Error?e:void 0)}}async signTypedData(e,t,n){try{return await this.wallet.signTypedData(e,t,n)}catch(e){throw new ia("Failed to sign typed data",Jo.SIGNING_FAILED,e instanceof Error?e:void 0)}}getAddress(){return Promise.resolve(t.getAddress(this.wallet.address))}async getGalaAddress(){return`eth|${(await this.getAddress()).slice(2)}`}async connect(){return this.connected=!0,this.getAddress()}disconnect(){return this.connected=!1,Promise.resolve()}isConnected(){return this.connected}getPrivateKey(){return Promise.resolve(this.wallet.privateKey)}getWallet(){return this.wallet}}class aa{constructor(e){if(this.wallet=e.wallet,e.walletProvider?this.walletProvider=e.walletProvider:e.wallet,this.messagePrefix=e.messagePrefix??"Create a GalaChain Wallet",void 0!==e.messagePrefix)try{Hn(e.messagePrefix,"messagePrefix",!1)}catch{throw new Yo(Xo.SIGNATURE_FAILED,"Message prefix cannot be empty")}}hasWallet(){return void 0!==this.walletProvider||void 0!==this.wallet}ensureWalletProvider(){if(this.wallet&&!this.walletProvider)try{this.walletProvider=oa.fromWallet(this.wallet),this.cachedAddress=this.wallet.address,this.cachedGalaAddress=fi(this.wallet.address)}catch(e){if(e instanceof Yo)throw e;if(e instanceof wt&&("INVALID_ADDRESS"===e.code||"INVALID_FORMAT"===e.code||"address"===e.field))throw new Yo(Xo.INVALID_ADDRESS,e.message);if(null!==e&&"object"==typeof e&&"message"in e)throw new Yo(Xo.WALLET_NOT_CONNECTED,e.message);throw e}}setWallet(e){if(void 0!==e){if("object"!=typeof e||!("address"in e))throw new Yo(Xo.WALLET_NOT_CONNECTED,"Invalid wallet: must be an ethers Wallet instance or undefined");if("string"!=typeof e.address||""===e.address)throw new Yo(Xo.INVALID_ADDRESS,"Wallet address is not available");this.walletProvider=oa.fromWallet(e),this.cachedAddress=e.address,this.cachedGalaAddress=fi(e.address)}else this.walletProvider=void 0,this.cachedAddress=void 0,this.cachedGalaAddress=void 0;this.wallet=e}setWalletProvider(e){this.walletProvider=e,this.wallet=void 0,this.cachedAddress=void 0,this.cachedGalaAddress=void 0}getWalletProvider(){return this.walletProvider}async cacheAddress(){this.walletProvider&&(this.cachedAddress=await this.walletProvider.getAddress(),this.cachedGalaAddress=await this.walletProvider.getGalaAddress())}async generateSignature(){await this.validateWalletAsync();try{const e=Date.now(),t=`${this.messagePrefix} ${e}`,n=await this.walletProvider.signMessage(t);return{message:t,signature:n,address:await this.walletProvider.getGalaAddress(),timestamp:e}}catch(e){if(e instanceof Yo)throw e;throw new Yo(Xo.SIGNATURE_FAILED,"Failed to generate signature for authentication",ot(e)?e:new Error(at(e)))}}getAddress(){if(this.validateWallet(),this.cachedGalaAddress)return this.cachedGalaAddress;if(this.wallet)return this.formatAddress(this.wallet.address);throw new Yo(Xo.WALLET_NOT_CONNECTED,"Address not available. For external wallets, call cacheAddress() after connecting.")}async getAddressAsync(){return await this.validateWalletAsync(),this.walletProvider.getGalaAddress()}getEthereumAddress(){if(this.validateWallet(),this.cachedAddress)return this.cachedAddress;if(this.wallet)return this.wallet.address;throw new Yo(Xo.WALLET_NOT_CONNECTED,"Address not available. For external wallets, call cacheAddress() after connecting.")}async getEthereumAddressAsync(){return await this.validateWalletAsync(),this.walletProvider.getAddress()}getPrivateKey(){if(this.validateWallet(),this.wallet){if(""===this.wallet.privateKey||null==this.wallet.privateKey)throw new Yo(Xo.WALLET_NOT_CONNECTED,"Wallet private key not available for @gala-chain signing");return this.wallet.privateKey}if("privateKey"===this.walletProvider?.providerType){return this.walletProvider.getWallet().privateKey}throw new Yo(Xo.WALLET_NOT_CONNECTED,"Private key not available. External wallet providers cannot access private keys. Use PrivateKeyProvider for @gala-chain signing operations.")}async getPrivateKeyAsync(){if(await this.validateWalletAsync(),!this.walletProvider.getPrivateKey)throw new Yo(Xo.WALLET_NOT_CONNECTED,"Private key not available with this wallet provider");try{return await this.walletProvider.getPrivateKey()}catch(e){throw new Yo(Xo.WALLET_NOT_CONNECTED,"Private key not available. External wallet providers cannot access private keys. Use PrivateKeyProvider for @gala-chain signing operations.",ot(e)?e:void 0)}}formatAddress(e){try{return fi(e)}catch{throw new Yo(Xo.INVALID_ADDRESS,`Invalid Ethereum address format: ${e}`)}}async signMessage(e){await this.validateWalletAsync();try{const t=await this.walletProvider.signMessage(e);return{message:e,signature:t,address:await this.walletProvider.getAddress(),timestamp:Date.now()}}catch(e){if(e instanceof Yo)throw e;throw new Yo(Xo.SIGNATURE_FAILED,at(e),ot(e)?e:new Error(at(e)))}}async generateAuthHeaders(e,t){await this.validateWalletAsync();try{const n=Date.now(),i=`${this.messagePrefix} ${t.toUpperCase()} ${e} ${n}`,o=await this.walletProvider.signMessage(i);return{"x-signature":o,"x-address":await this.walletProvider.getGalaAddress(),"x-message":i,"x-timestamp":n.toString()}}catch(e){if(e instanceof Yo)throw e;throw new Yo(Xo.SIGNATURE_FAILED,"Failed to generate authentication headers",ot(e)?e:new Error(at(e)))}}async signTypedData(e,t,n){await this.validateWalletAsync();try{const i={};for(const[e,n]of Object.entries(t))i[e]=n.map(e=>({name:e.name,type:e.type}));return await this.walletProvider.signTypedData(e,i,n)}catch(e){if(e instanceof Yo)throw e;throw new Yo(Xo.SIGNATURE_FAILED,"Failed to sign typed data",ot(e)?e:new Error(at(e)))}}async generateCustomSignature(e){if(!jn(e))throw new Yo(Xo.SIGNATURE_FAILED,"Custom message must be a non-empty string");await this.validateWalletAsync();try{const t=await this.walletProvider.signMessage(e);return{message:e,signature:t,address:await this.walletProvider.getGalaAddress(),timestamp:Date.now()}}catch(e){if(e instanceof Yo)throw e;throw new Yo(Xo.SIGNATURE_FAILED,"Failed to generate custom message signature",ot(e)?e:new Error(at(e)))}}validateWallet(){if(this.ensureWalletProvider(),!this.walletProvider)throw new Yo(Xo.WALLET_NOT_CONNECTED,"Wallet is required for authentication")}async validateWalletAsync(){if(this.ensureWalletProvider(),!this.walletProvider)throw new Yo(Xo.WALLET_NOT_CONNECTED,"Wallet is required for authentication");if(!this.walletProvider.isConnected())throw new Yo(Xo.WALLET_NOT_CONNECTED,"Wallet is not connected. Call connect() on the wallet provider first.")}}const ra={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"},QA1:{launchpadBaseUrl:"https://lpad-backend-qa1.defi.gala.com",galaChainBaseUrl:"https://galachain-gateway-chain-platform-stage-chain-platform-eks.stage.galachain.com",bundleBaseUrl:"https://bundle-backend-qa1.defi.gala.com",webSocketUrl:"https://bundle-backend-qa1.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-qa1.defi.gala.com"}};function sa(e){return ra[e]}function ca(e){return`${e.collection}|${e.category}|${e.type}|${e.additionalKey}`}function la(e){return`${e.collection}$${e.category}$${e.type}$${e.additionalKey}`}function da(e){return`$${e.collection}$${e.category}$${e.type}$${e.additionalKey}`}const ua=1,ha=2,ga=1002,ma=1,pa=3,fa=[{symbol:"GALA",amount:"1",contractAddress:"0xd1d2Eb1B1e90B638588728b4130137D262C87cae",bridgeUsesPermit:!0,decimals:8},{symbol:"GWETH",amount:"0.0001",contractAddress:"0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",bridgeUsesPermit:!1,decimals:18},{symbol:"GUSDC",amount:"1",contractAddress:"0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",bridgeUsesPermit:!1,decimals:6},{symbol:"GUSDT",amount:"1",contractAddress:"0xdAC17F958D2ee523a2206206994597C13D831ec7",bridgeUsesPermit:!1,decimals:6},{symbol:"GWTRX",amount:"1",contractAddress:"0x50327c6c5a14DCaDE707ABad2E27eB517df87AB5",bridgeUsesPermit:!1,decimals:6},{symbol:"GWBTC",amount:"0.00001",contractAddress:"0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599",bridgeUsesPermit:!1,decimals:8}],ya=[{symbol:"GALA",amount:"1",contractAddress:"0x9fBFf09325C1967A135AC9b4860b1cf89aca52DA",bridgeUsesPermit:!0,decimals:8},{symbol:"GWETH",amount:"0.0001",contractAddress:"0xC3F00B9CbC4221D85A66EEbe928551d0d8dD9158",bridgeUsesPermit:!1,decimals:18},{symbol:"GUSDC",amount:"1",contractAddress:"0x081e78E33bfa612b23A99ef61e7c194649AA318E",bridgeUsesPermit:!1,decimals:6},{symbol:"GUSDT",amount:"1",contractAddress:"0x461e3595f087bfb0E32B6e44BCbF4C74D99B0001",bridgeUsesPermit:!1,decimals:6},{symbol:"GWBTC",amount:"0.00001",contractAddress:"0x5f69276935EF17e5aF5289b60aFBf6d48B344770",bridgeUsesPermit:!1,decimals:8}];function ka(e){return"PROD"===e?fa:ya}function va(e){return"PROD"===e?"0x9f452b7cC24e6e6FA690fe77CF5dD2ba3DbF1ED9":"0x6a1734E09f3099a3675645D214ce547080ea67e0"}const wa=fa,ba=[{symbol:"GALA",amount:"1",mintAddress:"eEUiUs4JWYZrp72djAGF1A8PhpR6rHphGeGN7GbVLp6",isNative:!1,decimals:8},{symbol:"GSOL",amount:"0.001",mintAddress:"So11111111111111111111111111111111111111111",isNative:!0,decimals:9}],Sa={GALA:{descriptor:{collection:"GALA",category:"Unit",type:"none",additionalKey:"none"},decimals:8,channel:"asset"},GWETH:{descriptor:{collection:"GWETH",category:"Unit",type:"none",additionalKey:"none"},decimals:18,channel:"asset"},GUSDC:{descriptor:{collection:"GUSDC",category:"Unit",type:"none",additionalKey:"none"},decimals:6,channel:"asset"},GUSDT:{descriptor:{collection:"GUSDT",category:"Unit",type:"none",additionalKey:"none"},decimals:6,channel:"asset"},GWTRX:{descriptor:{collection:"GWTRX",category:"Unit",type:"none",additionalKey:"none"},decimals:6,channel:"asset"},GWBTC:{descriptor:{collection:"GWBTC",category:"Unit",type:"none",additionalKey:"none"},decimals:8,channel:"asset"},GSOL:{descriptor:{collection:"GSOL",category:"Unit",type:"none",additionalKey:"none"},decimals:9,channel:"asset"}},Aa=["function decimals() view returns (uint8)","function balanceOf(address owner) view returns (uint256)","function approve(address spender, uint256 value) returns (bool)","function allowance(address owner, address spender) view returns (uint256)","function transfer(address to, uint256 value) returns (bool)","function name() view returns (string)","function nonces(address owner) view returns (uint256)","function permit(address owner,address spender,uint256 value,uint256 deadline,uint8 v,bytes32 r,bytes32 s)"],Ta=["function bridgeOut(address token,uint256 amount,uint256 tokenId,uint16 destinationChainId,bytes recipient) external","function bridgeOutWithPermit(address token,uint256 amount,uint16 destinationChainId,bytes recipient,uint256 deadline,uint8 v,bytes32 r,bytes32 s) external"],Ea={BRIDGE_OUT:Buffer.from([27,194,57,119,215,165,247,150]),BRIDGE_OUT_NATIVE:Buffer.from([243,44,75,224,249,206,98,79])};function Ca(e){return/^0x[a-fA-F0-9]{64}$/.test(e)}function Ia(e){const t=new Map;for(const n of e)t.set(tn(n.symbol),n);return t}function Na(e,t,n){const i=function(e,t){return e.get(tn(t))}(e,t);if(!i){throw Ut(`Token ${t} not supported for ${n}. Supported: ${Array.from(e.keys()).join(", ")}`,"tokenSymbol")}return i}function Da(e){if(null==e||!Ca(e))throw Rt("privateKey","a 0x-prefixed 64-character hex string (e.g., 0x1234...abcd)","Ethereum private key")}function Pa(e,t,n){return{symbol:e,quantity:t,decimals:n,contractAddress:null,isNative:!0}}function xa(e,t){return{symbol:e.symbol,quantity:t,decimals:e.decimals??18,contractAddress:e.contractAddress,isNative:!1}}function _a(e,t){return{symbol:e.symbol,quantity:t,decimals:e.decimals??9,contractAddress:e.mintAddress,isNative:e.isNative??!1}}function Fa(e,t,n){const i=e.find(e=>e.symbol===t);if(!i){const i=e.map(e=>e.symbol).join(", ");throw Ft("tokenSymbol",`Unsupported ${n} token: ${t}. Supported: ${i}`)}return i}const Ra={Ethereum:"Ethereum bridging not configured. Provide ethereumPrivateKey in config.",Solana:"Solana bridging not configured. Provide solanaPrivateKey in config."};function Ba(e,t,n){if(void 0===e)throw Ut(n??Ra[t],`${t.toLowerCase()}Strategy`);return e}function Ua(e){return{direction:"inbound",fromChain:e.fromChain,toChain:"GalaChain",transactionHash:e.transactionHash,tokenSymbol:e.tokenSymbol,amount:e.amount,timestamp:Date.now(),statusUrl:`${e.baseUrl}/v1/bridge/transaction?hash=${e.transactionHash}`}}const Oa="Token symbol resolution failed. This is an internal error - BridgeService should resolve tokenId to symbol before calling strategy.",La="Bridge request ID missing from RequestTokenBridgeOut response",Ma="BridgeTokenOut response missing transaction hash";function $a(e){if(void 0===e||""===e)throw Ut(Oa,"tokenSymbol");return e}function Ka(e,n){if(!t.isAddress(e)){throw Rt(n??"address","a valid 0x-prefixed Ethereum address",e)}}function qa(e,t){try{return new a.PublicKey(e)}catch{throw Rt(t??"address","a valid Solana address (base58)",e)}}function Ga(e){try{return new a.PublicKey(e)}catch{throw Rt("address","a valid Solana address (base58)",e)}}function Wa(e){const t={};for(const[n,i]of Object.entries(e))void 0!==i&&(i&&"object"==typeof i&&!Array.isArray(i)?t[n]=Wa(i):t[n]=i);return t}function Ha(e,t){const n=xn(e,-1);if(n<=0)throw new Error(`Invalid bridge amount for ${t}: "${e}". Amount must be a positive number.`);return n}function za(e){const t="string"==typeof e.timestamp?Rn(e.timestamp,0):e.timestamp;return{estimatedFeeInGala:e.estimatedTotalTxFeeInGala,estimatedFeeInExternalToken:e.estimatedTotalTxFeeInExternalToken,feeToken:e.bridgeToken,pricePerUnit:e.estimatedPricePerTxFeeUnit,estimatedGasUnits:e.estimatedTxFeeUnitsTotal,exchangeRate:e.galaExchangeRate?.exchangeRate??"0",timestamp:t,raw:e}}let ja=null;if("undefined"==typeof window&&"undefined"!=typeof require)try{ja=require("crypto")}catch{}function Va(){if(void 0!==ja?.randomUUID)try{return ja.randomUUID()}catch{}if(void 0!==globalThis.crypto&&"function"==typeof globalThis.crypto.randomUUID)try{return crypto.randomUUID()}catch{}return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,e=>{const t=16*Math.random()|0;return("x"===e?t:3&t|8).toString(16)})}const Qa={name:"GalaConnect",chainId:1},Xa=[{name:"destinationChainId",type:"uint256"},{name:"destinationChainTxFee",type:"destinationChainTxFee"},{name:"quantity",type:"string"},{name:"recipient",type:"string"},{name:"tokenInstance",type:"tokenInstance"},{name:"uniqueKey",type:"string"}],Ja=[{name:"collection",type:"string"},{name:"category",type:"string"},{name:"type",type:"string"},{name:"additionalKey",type:"string"}],Ya=[{name:"collection",type:"string"},{name:"category",type:"string"},{name:"type",type:"string"},{name:"additionalKey",type:"string"},{name:"instance",type:"string"}],Za=[{name:"name",type:"string"},{name:"symbol",type:"string"}],er={GalaTransaction:Xa,destinationChainTxFee:[{name:"bridgeToken",type:"bridgeToken"},{name:"bridgeTokenIsNonFungible",type:"bool"},{name:"estimatedPricePerTxFeeUnit",type:"string"},{name:"estimatedTotalTxFeeInExternalToken",type:"string"},{name:"estimatedTotalTxFeeInGala",type:"string"},{name:"estimatedTxFeeUnitsTotal",type:"string"},{name:"galaDecimals",type:"uint256"},{name:"galaExchangeRate",type:"galaExchangeRate"},{name:"timestamp",type:"uint256"},{name:"signingIdentity",type:"string"},{name:"signature",type:"string"}],bridgeToken:Ja,galaExchangeRate:[{name:"identity",type:"string"},{name:"oracle",type:"string"},{name:"source",type:"string"},{name:"sourceUrl",type:"string"},{name:"timestamp",type:"uint256"},{name:"baseToken",type:"baseToken"},{name:"exchangeRate",type:"string"},{name:"externalQuoteToken",type:"externalQuoteToken"}],baseToken:Ya,externalQuoteToken:Za,tokenInstance:Ya},tr={GalaTransaction:Xa,destinationChainTxFee:[{name:"bridgeToken",type:"bridgeToken"},{name:"bridgeTokenIsNonFungible",type:"bool"},{name:"estimatedPricePerTxFeeUnit",type:"string"},{name:"estimatedTotalTxFeeInExternalToken",type:"string"},{name:"estimatedTotalTxFeeInGala",type:"string"},{name:"estimatedTxFeeUnitsTotal",type:"string"},{name:"galaDecimals",type:"uint256"},{name:"galaExchangeCrossRate",type:"galaExchangeCrossRate"},{name:"timestamp",type:"uint256"},{name:"signingIdentity",type:"string"},{name:"signature",type:"string"}],bridgeToken:Ja,galaExchangeCrossRate:[{name:"baseTokenCrossRate",type:"baseTokenCrossRate"},{name:"crossRate",type:"string"},{name:"externalCrossRateToken",type:"externalCrossRateToken"},{name:"identity",type:"string"},{name:"oracle",type:"string"},{name:"quoteTokenCrossRate",type:"quoteTokenCrossRate"},{name:"source",type:"string"},{name:"timestamp",type:"uint256"}],baseTokenCrossRate:[{name:"identity",type:"string"},{name:"oracle",type:"string"},{name:"source",type:"string"},{name:"sourceUrl",type:"string"},{name:"timestamp",type:"uint256"},{name:"exchangeRate",type:"string"},{name:"externalBaseToken",type:"externalBaseToken"},{name:"externalQuoteToken",type:"externalQuoteToken"},{name:"signature",type:"string"}],externalBaseToken:Za,externalQuoteToken:Za,externalCrossRateToken:Za,quoteTokenCrossRate:[{name:"identity",type:"string"},{name:"oracle",type:"string"},{name:"source",type:"string"},{name:"sourceUrl",type:"string"},{name:"timestamp",type:"uint256"},{name:"baseToken",type:"baseToken"},{name:"exchangeRate",type:"string"},{name:"externalQuoteToken",type:"externalQuoteToken"},{name:"signature",type:"string"}],baseToken:Ya,tokenInstance:Ya};const nr={GalaTransaction:[{name:"quantity",type:"string"},{name:"tokenInstance",type:"tokenInstance"},{name:"destinationChainId",type:"uint256"},{name:"recipient",type:"string"},{name:"wrap",type:"bool"},{name:"uniqueKey",type:"string"}],tokenInstance:Ya};async function ir(e,t){const{wallet:n}=t,i=e.uniqueKey??`galaconnect-operation-${Va()}`,o="string"==typeof e.destinationChainId?Rn(e.destinationChainId,1):e.destinationChainId,a=function(e){const t={...e,galaDecimals:"string"==typeof e.galaDecimals?Rn(e.galaDecimals,0):e.galaDecimals,timestamp:"string"==typeof e.timestamp?Rn(e.timestamp,0):e.timestamp};if(e.galaExchangeRate&&(t.galaExchangeRate={...e.galaExchangeRate,timestamp:"string"==typeof e.galaExchangeRate.timestamp?Rn(e.galaExchangeRate.timestamp,0):e.galaExchangeRate.timestamp}),e.galaExchangeCrossRate){const n=e.galaExchangeCrossRate;t.galaExchangeCrossRate={...n,timestamp:"string"==typeof n.timestamp?Rn(n.timestamp,0):n.timestamp},n.baseTokenCrossRate&&(t.galaExchangeCrossRate.baseTokenCrossRate={...n.baseTokenCrossRate,timestamp:"string"==typeof n.baseTokenCrossRate.timestamp?Rn(n.baseTokenCrossRate.timestamp,0):n.baseTokenCrossRate.timestamp}),n.quoteTokenCrossRate&&(t.galaExchangeCrossRate.quoteTokenCrossRate={...n.quoteTokenCrossRate,timestamp:"string"==typeof n.quoteTokenCrossRate.timestamp?Rn(n.quoteTokenCrossRate.timestamp,0):n.quoteTokenCrossRate.timestamp})}return t}(e.destinationChainTxFee),s=Boolean(a.galaExchangeCrossRate),c={destinationChainId:o,destinationChainTxFee:Wa(s?{...a,galaExchangeRate:void 0}:{...a,galaExchangeCrossRate:void 0}),quantity:e.quantity,recipient:e.recipient,tokenInstance:e.tokenInstance,uniqueKey:i},l=function(e){return e?tr:er}(s),d=await n.signTypedData(Qa,l,c),u=`Ethereum Signed Message:\n${r({domain:Qa,message:c,primaryType:"GalaTransaction",types:l}).length}`;return{...c,signature:d,prefix:u,types:l,domain:Qa}}async function or(e,t){const{amount:n,recipientAddress:i,tokenSymbol:o}=e,{galaConnectClient:a,tokenMetadataResolver:r,ethereumWallet:s,destinationChainId:c,destinationChain:l,validateRecipientAddress:d}=t,u=$a(o);Ha(n,u),d(i,"recipient");const h=await r.getTokenMetadata(u),g=await a.fetchBridgeFee({chainId:l,bridgeToken:h.descriptor}),m=function(e){return{destinationChainId:e.destinationChainId,destinationChainTxFee:e.bridgeFee,quantity:e.amount,recipient:e.recipientAddress,tokenInstance:{...e.tokenDescriptor,instance:"0"}}}({destinationChainId:c,bridgeFee:g,amount:n,recipientAddress:i,tokenDescriptor:h.descriptor}),p=await ir(m,{wallet:s}),f=function(e){if(void 0===e||""===e)throw Ut(La,"bridgeRequestId");return e}(function(e){if("string"==typeof e.Data)return e.Data;if(null!=e.data){if("string"==typeof e.data)return e.data;if("object"==typeof e.data){const t=e.data;if("string"==typeof t.Data)return t.Data}}}(await a.requestBridgeOut(p)));return function(e){return{direction:"outbound",fromChain:"GalaChain",toChain:e.toChain,transactionHash:e.transactionHash,tokenSymbol:e.tokenSymbol,amount:e.amount,feePaid:e.feePaid,timestamp:Date.now(),statusUrl:`${e.baseUrl}/v1/bridge/transaction?hash=${e.transactionHash}`}}({toChain:l,transactionHash:function(e){const t=e.Hash??e.hash??"";if(""===t)throw Ut(Ma,"transactionHash");return t}(await a.bridgeTokenOut({bridgeFromChannel:"asset",bridgeRequestId:f})),tokenSymbol:u,amount:n,feePaid:g.estimatedTotalTxFeeInGala,baseUrl:a.getBaseUrl()})}const ar=5,rr=6,sr=7;function cr(e){const t=Rn(e.status,0);return{status:t,statusDescription:e.statusDescription,fromChain:e.fromChain,toChain:e.toChain,quantity:e.quantity,transactionHash:e.emitterTransactionHash,tokenInstance:e.tokenInstance,isComplete:t===ar,isFailed:t===rr||t===sr}}class lr{constructor(e){if(this.lastTimestamp=0,this.pendingPromise=Promise.resolve(),this.chainLength=0,this.maxChainLength=1e3,e<=0)throw $t("requestsPerSecond","1",e,"Requests per second");this.minIntervalMs=1e3/e}async schedule(e){let t,n;const i=new Promise((e,i)=>{t=e,n=i});if(this.pendingPromise=this.pendingPromise.then(async()=>{const i=Date.now()-this.lastTimestamp,o=Math.max(0,this.minIntervalMs-i);o>0&&await new Promise(e=>setTimeout(e,o)),this.lastTimestamp=Date.now();try{const n=await e();t(n)}catch(e){n(ot(e)?e:new Error(at(e)))}}),this.chainLength++,this.chainLength>=this.maxChainLength){this.chainLength=0;const e=this.pendingPromise;this.pendingPromise=e.then(()=>Promise.resolve())}return i}}const dr={maxRetries:3,initialDelayMs:1e3,maxDelayMs:3e4,backoffMultiplier:2,jitterFactor:.1},ur=new Set([408,429,500,502,503,504]),hr=[/ECONNRESET/i,/ECONNREFUSED/i,/ETIMEDOUT/i,/ENOTFOUND/i,/EAI_AGAIN/i,/socket hang up/i,/network/i,/timeout/i,/aborted/i];function gr(e){if(null!=e&&"object"==typeof e){const t=e;if("number"==typeof t.status)return ur.has(t.status);if("number"==typeof t.statusCode)return ur.has(t.statusCode);const n=st(e);if("string"==typeof n&&("ECONNRESET"===n||"ECONNREFUSED"===n||"ETIMEDOUT"===n||"ENOTFOUND"===n||"EAI_AGAIN"===n))return!0}const t=at(e);return hr.some(e=>e.test(t))}function mr(e,t){const n=t.initialDelayMs*Math.pow(t.backoffMultiplier,e-1),i=Math.min(n,t.maxDelayMs),o=i*t.jitterFactor*Math.random();return Math.floor(i+o)}function pr(e){return new Promise(t=>setTimeout(t,e))}function fr(e,t){return Ba(e.get("Ethereum"),"Ethereum",t)}function yr(e,t){return Ba(e.get("Solana"),"Solana",t)}function kr(e,t){return e??t.getWalletAddress()}function vr(e,t,n){const i=void 0!==n?` (${n})`:"";try{if(!jn(e))throw new Error("Input must be a non-empty string");const n=e.split(t);if(n.length<4)throw new Error(`Invalid ${"|"===t?"pipe":"dollar"}-delimited token format. Expected 4+ parts separated by ${t}, got ${n.length}`);const[i,o,a,...r]=n;if(""===i||""===o||""===a)throw new Error("Collection, category, and type must be non-empty");const s=r.join(t);if(""===s)throw new Error("AdditionalKey must be non-empty");return{collection:i,category:o,type:a,additionalKey:s}}catch(n){const o=e?.split?.(t)??[];throw new wt(`Invalid ${"|"===t?"pipe":"dollar"}-delimited token: "${e}" (${o.length} parts)${i}. Expected format: "collection${t}category${t}type${t}additionalKey" (4 parts minimum). Received: [${o.map(e=>`"${e}"`).join(", ")}]. Error: ${at(n)}`,"token",`INVALID_${"|"===t?"PIPE":"DOLLAR"}_DELIMITED_TOKEN`)}}function wr(e){if("object"==typeof e&&null!==e)return function(e){if(null===e||"object"!=typeof e)throw new wt("Token object must be a non-null object, got "+typeof e,"token","INVALID_TOKEN_OBJECT");const{collection:t,category:n,type:i,additionalKey:o}=e;if(!jn(t))throw new wt("Token.collection must be a non-empty string, got "+typeof t,"token.collection","MISSING_OR_INVALID_COLLECTION");if(!jn(n))throw new wt("Token.category must be a non-empty string, got "+typeof n,"token.category","MISSING_OR_INVALID_CATEGORY");if(!jn(i))throw new wt("Token.type must be a non-empty string, got "+typeof i,"token.type","MISSING_OR_INVALID_TYPE");if(!jn(o))throw new wt("Token.additionalKey must be a non-empty string, got "+typeof o,"token.additionalKey","MISSING_OR_INVALID_ADDITIONAL_KEY");return{collection:t,category:n,type:i,additionalKey:o}}(e);if(null==e)throw new wt(`Token cannot be null, undefined, or empty. Received: ${JSON.stringify(e)}`,"token","EMPTY_TOKEN");if("string"!=typeof e)throw new wt("Token must be a string or TokenClassKey object, got "+typeof e,"token","INVALID_TOKEN_TYPE");if(Sr(e))return br(e);if(Ar(e))return function(e){return function(e,t,n){const i=` (${n})`;try{if(!jn(e))throw new Error("Input must be a non-empty string");const n=e.split(t);if(n.length<4)throw new Error(`Invalid dollar-delimited token format. Expected 4+ parts separated by ${t}, got ${n.length}`);const i=n[n.length-1],o=n[n.length-2],a=n[n.length-3],r=n.slice(0,n.length-3).join(t);if(""===r||""===a||""===o||""===i)throw new Error("All components (collection, category, type, additionalKey) must be non-empty");return{collection:r,category:a,type:o,additionalKey:i}}catch(n){const o=e?.split?.(t)??[];throw new wt(`Invalid dollar-delimited token: "${e}" (${o.length} parts)${i}. Expected format: "collection${t}category${t}type${t}additionalKey" (4 parts minimum). Received: [${o.map(e=>`"${e}"`).join(", ")}]. Error: ${at(n)}`,"token","INVALID_DOLLAR_DELIMITED_TOKEN")}}(e,"$","dollar-delimited token")}(e);throw new wt(`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")}function br(e){return vr(e,"|","pipe-delimited token")}function Sr(e){return jn(e)&&e.includes("|")}function Ar(e){return"string"==typeof e&&e.includes("$")}function Tr(e){if("object"!=typeof e||null===e)return!1;const t=e,n=t.collection,i=t.category,o=t.type,a=t.additionalKey;return"string"==typeof n&&"string"==typeof i&&"string"==typeof o&&"string"==typeof a&&n.length>0&&i.length>0&&o.length>0&&a.length>0}function Er(e){let t;if("string"==typeof e)t=br(e);else{if(!Tr(e))throw new Error('Invalid tokenId format. Expected pipe-delimited string (e.g., "GALA|Unit|none|none") or TokenClassKey object.');t=e}return{tokenClassKey:t,stringified:ca(t)}}function Cr(e,t){let n;try{n=Un(e,"amount")}catch(t){throw Gt("amount",`${e} (${at(t)})`)}if(!n.isFinite())throw Gt("amount",e);const i=n.multipliedBy(po(10).pow(t));if(!i.isInteger())throw Gt("amount",`${e} (cannot be represented with ${t} decimals)`);return BigInt(i.toFixed(0))}function Ir(e,t){return po(e.toString()).dividedBy(po(10).pow(t)).toFixed(t).replace(/\.?0+$/,"")}class Nr{constructor(e){this.cache=new Map,this.galaConnectClient=e.galaConnectClient}async getTokenMetadata(e){const t=nn(e),n=this.cache.get(t);if(n)return n;const i=Sa[t];if(void 0!==i)return this.cache.set(t,i),i;const o=await this.fetchFromApi(e);return this.cache.set(t,o),o}hasMetadata(e){const t=nn(e);return this.cache.has(t)||t in Sa}clearCache(){this.cache.clear()}async fetchFromApi(e){let t=e,n=await this.galaConnectClient.getBridgeConfigurations(t),i=n.find(e=>nn(e.symbol)===nn(t)&&e.verified);if(i||e.startsWith("G")||(t=`G${e}`,n=await this.galaConnectClient.getBridgeConfigurations(t),i=n.find(e=>nn(e.symbol)===nn(t)&&e.verified)),!i)throw new Error(`Unable to locate token metadata for ${e}`);return o={collection:i.collection,category:i.category,type:i.type,additionalKey:i.additionalKey},a=i.decimals,r=i.channel,{descriptor:{collection:o.collection,category:o.category,type:o.type,additionalKey:o.additionalKey},decimals:a,...void 0!==r?{channel:r}:{}};var o,a,r}}class Dr extends Error{constructor(e,t,n){super(`GalaConnect request to ${t} failed with status ${e}${null!=n?`: ${JSON.stringify(n)}`:""}`),this.status=e,this.path=t,this.responseBody=n,this.name="GalaConnectHttpError"}}const Pr="https://dex-api-platform-dex-prod-gala.gala.com",xr="https://galachain-gateway-chain-platform-galachain-mainnet.gala.com",_r=12,Fr=!0,Rr=3,Br=1e3;class Ur{constructor(e){this.baseUrl=e.baseUrl??Pr,this.galachainBaseUrl=e.galachainBaseUrl??xr,this.walletAddress=e.walletAddress,this.rateLimiter=new lr(e.requestsPerSecond??_r),this.defaultHeaders={"Content-Type":"application/json","X-Wallet-Address":this.walletAddress};const t=e.enableRetry??Fr;this.retryOptions=t?{maxRetries:e.maxRetries??Rr,initialDelayMs:e.retryInitialDelayMs??Br,...e.onRetry&&{onRetry:e.onRetry},shouldRetry:e=>gr(e instanceof Dr?{status:e.status}:e)}:null}getBaseUrl(){return this.baseUrl}async getBridgeConfigurations(e){return ea(async()=>{const t=new URL("/v1/connect/bridge-configurations",this.baseUrl);t.searchParams.set("searchprefix",e);const n=await this.request(t.toString(),{method:"GET"});if(!n.ok){const e=await n.text();let t;throw t=""!==e?Pn(e,{rawBody:e}):{message:"Failed to fetch bridge configurations"},new Dr(n.status,"/v1/connect/bridge-configurations",t)}return(await n.json()).data.tokens},"GalaConnectClient.getBridgeConfigurations",void 0,(e,t,n)=>{if(e instanceof Dr)throw e;throw Lt(e,"GalaConnectClient.getBridgeConfigurations")})}async fetchBridgeFee(e){return this.postJson("/v1/bridge/fee",e,{skipWalletHeader:!0})}async requestBridgeOut(e){return this.postJson("/v1/RequestTokenBridgeOut",e)}async bridgeTokenOut(e){return this.postJson("/v1/BridgeTokenOut",e)}async getBridgeStatus(e){return this.postJson("/v1/bridge/status",{hash:e})}async registerBridgeTransaction(e){return this.postJson("/v1/bridge/transaction",e)}async fetchBalances(e="asset"){return this.postJson("/v1/FetchBalances",{owner:this.walletAddress,channel:e},{baseUrl:this.galachainBaseUrl})}async postJson(e,t,n={}){return ea(async()=>{const i=n.baseUrl??this.baseUrl,o=new URL(e,i),a=n.skipWalletHeader?{"Content-Type":"application/json","X-Wallet-Address":""}:this.defaultHeaders,r=await this.request(o.toString(),{method:"POST",headers:a,body:JSON.stringify(t,(e,t)=>"bigint"==typeof t?t.toString():t)});if(!r.ok){const t=await r.text(),n=500,i=Pn(t,{rawBody:t.slice(0,n)});throw new Dr(r.status,e,i)}const s=await r.text();if(""!==s){const t=function(e){if(rn(e))return{success:!1,value:null,error:"Value is empty"};try{return{success:!0,value:JSON.parse(e)}}catch(e){return{success:!1,value:null,error:`JSON parse error: ${at(e)}`}}}(s);if(!t.success)throw new Error(`Failed to parse JSON response from ${e}: ${String(t.error)}`);return t.value}},`Failed to execute POST request to ${e}`,void 0,(e,t,n)=>{if(e instanceof Dr)throw e;throw Lt(e,t)})}async request(e,t){const n=async()=>this.rateLimiter.schedule(async()=>{const n={...this.defaultHeaders,...t.headers};""===n["X-Wallet-Address"]?delete n["X-Wallet-Address"]:n["X-Wallet-Address"]??(n["X-Wallet-Address"]=this.walletAddress);const i=await fetch(e,{...t,headers:n});if(this.retryOptions&&!i.ok){const t=i.status;if(429===t||t>=500){const n=i.clone(),o=await this.safeParseJson(n);throw new Dr(t,e,o)}}return i});return this.retryOptions?async function(e,t={}){const n={...dr,...t},i=t.shouldRetry??(e=>gr(e));let o;for(let a=1;a<=n.maxRetries+1;a++)try{return await e()}catch(e){if(o=e,a>n.maxRetries)break;if(!i(e,a))break;const r=mr(a,n);t.onRetry&&t.onRetry(e,a,r),await new Promise(e=>setTimeout(e,r))}throw o}(n,this.retryOptions):n()}async safeParseJson(e){const t=await e.text();try{return JSON.parse(t)}catch{return{rawBody:t}}}}var Or;!function(e){e[e.PENDING=0]="PENDING",e[e.SUBMITTED=1]="SUBMITTED",e[e.CONFIRMED=2]="CONFIRMED",e[e.PROCESSING=3]="PROCESSING",e[e.FINALIZING=4]="FINALIZING",e[e.COMPLETED=5]="COMPLETED",e[e.FAILED=6]="FAILED",e[e.DELIVERY_FAILED=7]="DELIVERY_FAILED"}(Or||(Or={}));class Lr{async waitForCompletion(e,t={}){const{pollInterval:n=15e3,timeout:i=27e5,onStatusUpdate:o}=t,a=Date.now();for(;;){const t=await this.getStatus(e);if(o&&o(t),t.status===Or.COMPLETED||t.status===Or.FAILED||t.status===Or.DELIVERY_FAILED)return t;if(un(a)>i)throw Ot(`Bridge transaction timed out after ${i}ms. Last status: ${t.status}`,e,"TIMEOUT");await new Promise(e=>setTimeout(e,n))}}}class Mr extends Lr{constructor(e){super(),this.network="Ethereum",this.galaConnectClient=e.galaConnectClient,this.galaChainWalletAddress=e.galaChainWalletAddress,Da(e.ethereumPrivateKey);const n=e.ethereumRpcUrl??"https://ethereum.publicnode.com";this.ethereumProvider=new t.JsonRpcProvider(n),this.ethereumWallet=new t.Wallet(e.ethereumPrivateKey,this.ethereumProvider),this.ethereumWalletAddress=e.ethereumWalletAddress??this.ethereumWallet.address,this.ethereumBridgeContract=e.ethereumBridgeContract??"0x3F98b5A26EF3f04E1DA3B0B41dD350E8C8F3A7c2";const i=e.tokenConfigs??wa;this.tokenConfigs=Ia(i),this.tokenMetadataResolver=new Nr({galaConnectClient:this.galaConnectClient})}async estimateFee(e,t){const n=await this.tokenMetadataResolver.getTokenMetadata(e);return za(await this.galaConnectClient.fetchBridgeFee({chainId:"Ethereum",bridgeToken:n.descriptor}))}async bridgeOut(e){return or(e,{galaConnectClient:this.galaConnectClient,tokenMetadataResolver:this.tokenMetadataResolver,ethereumWallet:this.ethereumWallet,destinationChainId:ha,destinationChain:"Ethereum",validateRecipientAddress:Ka})}async bridgeIn(e){const{amount:n,sourcePrivateKey:i,recipientAddress:o,tokenSymbol:a}=e,r=$a(a);if(Ha(n,r),void 0!==i&&""!==i&&!Ca(i))throw Rt("sourcePrivateKey","0x-prefixed 64-character hex string",i.slice(0,10)+"...");const s=void 0!==i&&""!==i?new t.Wallet(i,this.ethereumProvider):this.ethereumWallet,c=Na(this.tokenConfigs,r,"Ethereum bridge"),l=await this.tokenMetadataResolver.getTokenMetadata(r),d=new t.Contract(c.contractAddress,Aa,s),u=Rn(await d.decimals(),18),h=Cr(n,u),g=BigInt(await d.balanceOf(s.address));if(g<h){throw Ut(`Insufficient ${r} balance on Ethereum. Needed ${Ir(h,u)}, have ${Ir(g,u)}`,"amount")}const m=function(e){const n=new gi;if("client"===n.detectFormat(e))return e;const i=n.normalizeInput(e);if(null==i)throw Rt("address","a valid GalaChain address (eth|0x{40-hex}, 0x{40-hex}, or {40-hex})","GalaChain address");const o=n.extractHex(i);return`eth|${t.getAddress("0x"+o).slice(2)}`}(o??this.galaChainWalletAddress);return Ua({fromChain:"Ethereum",transactionHash:(await this.executeBridgeDeposit({wallet:s,tokenContract:d,tokenConfig:c,amountBaseUnits:h,decimals:u,recipient:m,metadata:l})).txHash,tokenSymbol:r,amount:n,baseUrl:this.galaConnectClient.getBaseUrl()})}async getStatus(e){return cr(await this.galaConnectClient.getBridgeStatus(e))}getSupportedTokens(){return Array.from(this.tokenConfigs.keys())}isTokenSupported(e){return this.tokenConfigs.has(tn(e))}isValidAddress(e){return zn.ETH_ADDRESS.test(e)}getWalletAddress(){return this.ethereumWalletAddress}async getEthereumTokenBalance(e,n){const i=Na(this.tokenConfigs,e,"Ethereum"),o=n??this.ethereumWalletAddress;Ka(o);const a=new t.Contract(i.contractAddress,Aa,this.ethereumProvider);return Ir(await a.balanceOf(o),i.decimals??18)}async getEthereumNativeBalance(e){const t=e??this.ethereumWalletAddress;Ka(t);return Ir(await this.ethereumProvider.getBalance(t),18)}async getEthereumTransactionStatus(e){!function(e){if(!function(e){return/^0x[a-fA-F0-9]{64}$/.test(e)}(e))throw Rt("hash","a 0x-prefixed 64-character hex string (66 total characters)","Ethereum transaction hash")}(e);const t=e.toLowerCase();try{const e=await this.ethereumProvider.getTransactionReceipt(t);if(e){const n=await this.ethereumProvider.getBlockNumber()-e.blockNumber+1;if(!(1===e.status))return{confirmed:!1,status:"failed",blockNumber:e.blockNumber,confirmations:n,transactionHash:t,gasUsed:e.gasUsed.toString(),effectiveGasPrice:e.gasPrice?.toString(),error:"Transaction reverted during execution"};return{confirmed:!0,status:n>=Mr.ETHEREUM_FINALITY_THRESHOLD?"finalized":"confirmed",blockNumber:e.blockNumber,confirmations:n,transactionHash:t,gasUsed:e.gasUsed.toString(),effectiveGasPrice:e.gasPrice?.toString()}}return await this.ethereumProvider.getTransaction(t)?{confirmed:!1,status:"pending",transactionHash:t}:{confirmed:!1,status:"not_found",transactionHash:t,error:"Transaction not found on Ethereum network"}}catch(e){return{confirmed:!1,status:"not_found",transactionHash:t,error:`Failed to query transaction status: ${at(e)}`}}}async executeBridgeDeposit(e){const n=new t.Contract(this.ethereumBridgeContract,Ta,e.wallet),i=(new TextEncoder).encode(e.recipient);let o;o=e.tokenConfig.bridgeUsesPermit?await this.bridgeWithPermit(e.wallet,e.tokenContract,e.tokenConfig,n,e.amountBaseUnits,i):await this.bridgeWithApproval(e.wallet,e.tokenContract,n,e.tokenConfig,e.amountBaseUnits,i);if(!await o.wait())throw Ut("Bridge transaction receipt not available","ethereumRpcUrl");await pr(3e4);const a={collection:e.metadata.descriptor.collection,category:e.metadata.descriptor.category,type:e.metadata.descriptor.type,additionalKey:e.metadata.descriptor.additionalKey,instance:"0"};return await this.galaConnectClient.registerBridgeTransaction({quantity:Ir(e.amountBaseUnits,e.decimals),tokenInstance:a,fromChain:"Ethereum",toChain:"GC",hash:o.hash}),{txHash:o.hash}}async bridgeWithPermit(e,n,i,o,a,r){const s=await this.ethereumProvider.getNetwork(),c=await n.name(),l=await n.nonces(e.address),d=BigInt(Math.floor(Date.now()/1e3)+3600),u=await e.signTypedData({name:c,version:"1",chainId:Rn(s.chainId,1),verifyingContract:i.contractAddress},{Permit:[{name:"owner",type:"address"},{name:"spender",type:"address"},{name:"value",type:"uint256"},{name:"nonce",type:"uint256"},{name:"deadline",type:"uint256"}]},{owner:e.address,spender:this.ethereumBridgeContract,value:a,nonce:l,deadline:d}),h=t.Signature.from(u);return await o.bridgeOutWithPermit(i.contractAddress,a,ua,r,d,h.v,h.r,h.s)}async bridgeWithApproval(e,t,n,i,o,a){const r=await t.allowance(e.address,this.ethereumBridgeContract);if(BigInt(r)<o){const e=await t.approve(this.ethereumBridgeContract,o);await e.wait()}return await n.bridgeOut(i.contractAddress,o,0,ua,a)}}Mr.ETHEREUM_FINALITY_THRESHOLD=12;function $r(e,t,n,i,o){const[r]=a.PublicKey.findProgramAddressSync([t.toBuffer(),i.toBuffer(),e.toBuffer()],o);return r}class Kr extends Lr{constructor(e){super(),this.network="Solana",this.solanaBridgeAccountCache=new Map,this.galaConnectClient=e.galaConnectClient,this.galaChainWalletAddress=e.galaChainWalletAddress,Da(e.ethereumPrivateKey);const n=new t.JsonRpcProvider("https://ethereum.publicnode.com");this.ethereumWallet=new t.Wallet(e.ethereumPrivateKey,n);const i=e.solanaRpcUrl??"https://api.mainnet-beta.solana.com";let o;this.solanaConnection=new a.Connection(i,"confirmed");try{o=c.decode(e.solanaPrivateKeyBase58)}catch{throw Rt("solanaPrivateKeyBase58","base58-encoded string",e.solanaPrivateKeyBase58.slice(0,20)+"...")}if(64!==o.length)throw Rt("solanaPrivateKeyBase58","64 bytes when decoded",`${o.length} bytes`);this.solanaKeypair=a.Keypair.fromSecretKey(o);const r=e.solanaBridgeProgram??"AaE4dTnL75XqgUJpdxBKg6vS9sTJgBPJwBQRVhD29WwS";this.solanaBridgeProgramId=new a.PublicKey(r),[this.solanaBridgeTokenAuthority]=a.PublicKey.findProgramAddressSync([Buffer.from("bridge_token_authority")],this.solanaBridgeProgramId),[this.solanaBridgeConfigPda]=a.PublicKey.findProgramAddressSync([Buffer.from("configv1")],this.solanaBridgeProgramId),[this.solanaNativeBridgePda]=a.PublicKey.findProgramAddressSync([Buffer.from("native_sol_bridge")],this.solanaBridgeProgramId);const s=e.tokenConfigs??ba;this.tokenConfigs=Ia(s),this.tokenMetadataResolver=new Nr({galaConnectClient:this.galaConnectClient})}async estimateFee(e,t){const n=await this.tokenMetadataResolver.getTokenMetadata(e);return za(await this.galaConnectClient.fetchBridgeFee({chainId:"Solana",bridgeToken:n.descriptor}))}async bridgeOut(e){return or(e,{galaConnectClient:this.galaConnectClient,tokenMetadataResolver:this.tokenMetadataResolver,ethereumWallet:this.ethereumWallet,destinationChainId:ga,destinationChain:"Solana",validateRecipientAddress:qa})}async bridgeIn(e){const{amount:t,sourcePrivateKey:n,recipientAddress:i,tokenSymbol:o}=e,r=$a(o);Ha(t,r);let s=this.solanaKeypair;if(void 0!==n&&""!==n){let e;try{e=c.decode(n)}catch{throw Rt("sourcePrivateKey","base58-encoded string",n.slice(0,20)+"...")}if(64!==e.length)throw Rt("sourcePrivateKey","64 bytes when decoded",`${e.length} bytes`);s=a.Keypair.fromSecretKey(e)}const l=Na(this.tokenConfigs,r,"Solana bridge"),d=await this.tokenMetadataResolver.getTokenMetadata(r),u=Cr(t,d.decimals),h=i??this.galaChainWalletAddress;return Ua({fromChain:"Solana",transactionHash:await this.executeSolanaBridgeOut({keypair:s,tokenConfig:l,metadata:d,amountBaseUnits:u,recipient:h,amount:t}),tokenSymbol:r,amount:t,baseUrl:this.galaConnectClient.getBaseUrl()})}async getStatus(e){return cr(await this.galaConnectClient.getBridgeStatus(e))}getSupportedTokens(){return Array.from(this.tokenConfigs.keys())}isTokenSupported(e){return this.tokenConfigs.has(tn(e))}isValidAddress(e){try{return new a.PublicKey(e),!0}catch{return!1}}getWalletAddress(){return this.solanaKeypair.publicKey.toBase58()}async getSolanaTokenBalance(e,t){const n=Na(this.tokenConfigs,e,"Solana");if(n.isNative)return this.getSolanaNativeBalance(t);const i=t??this.solanaKeypair.publicKey.toBase58(),o=Ga(i),r=$r(new a.PublicKey(n.mintAddress),o,0,s.TOKEN_PROGRAM_ID,s.ASSOCIATED_TOKEN_PROGRAM_ID);try{const e=await async function(e,t,n,i){const o=await e.getAccountInfo(t);if(!o)throw new Error(`could not find account ${t.toBase58()}`);if(!o.owner.equals(i))throw new Error(`account owner mismatch: expected ${i.toBase58()}, got ${o.owner.toBase58()}`);return{amount:s.AccountLayout.decode(o.data).amount,decimals:0}}(this.solanaConnection,r,0,s.TOKEN_PROGRAM_ID),t=n.decimals??8;return Ir(e.amount,t)}catch(t){if(ot(t)&&t.message.includes("could not find")){return Ir(0n,n.decimals??8)}throw Ut(`Failed to fetch ${e} balance for ${i}: ${at(t)}`,`${e}Account`)}}async getSolanaNativeBalance(e){const t=Ga(e??this.solanaKeypair.publicKey.toBase58()),n=await this.solanaConnection.getBalance(t,"confirmed");return Ir(BigInt(n),9)}async requestDevnetAirdrop(e=1,t){Kn(e,1e-5,2,"amount");const n=void 0!==t&&""!==t?Ga(t):this.solanaKeypair.publicKey,i=this.solanaConnection.rpcEndpoint;if(!i.includes("devnet"))throw Ut(`Solana devnet faucet only available on devnet. Current RPC: ${i}. Ensure SDK is configured with environment='STAGE' for devnet access.`,"solanaRpcUrl");const o=Math.floor(e*a.LAMPORTS_PER_SOL);return await this.solanaConnection.requestAirdrop(n,o)}async getSolanaTransactionStatus(e){if(!jn(e))throw Rt("signature","a non-empty string");if(!/^[1-9A-HJ-NP-Za-km-z]{80,90}$/.test(e))throw Rt("signature","a base58-encoded string (87-88 characters)",e.slice(0,20)+"...");const t=(await this.solanaConnection.getSignatureStatuses([e])).value[0];return t?null!==t.err&&void 0!==t.err?{confirmed:!1,status:"failed",slot:t.slot,error:JSON.stringify(t.err)}:{confirmed:!0,status:t.confirmationStatus??"processed",slot:t.slot}:{confirmed:!1,status:"not_found"}}async executeSolanaBridgeOut(e){const t=new a.PublicKey(e.tokenConfig.mintAddress),n=Boolean(e.tokenConfig.isNative),i=n?void 0:await this.getSolanaBridgeAccounts(t),o=n?void 0:$r(t,e.keypair.publicKey,0,s.TOKEN_PROGRAM_ID,s.ASSOCIATED_TOKEN_PROGRAM_ID),r=Buffer.from(e.recipient,"utf8"),c=Buffer.alloc(8);c.writeBigUInt64LE(e.amountBaseUnits);const l=Buffer.alloc(4);l.writeUInt32LE(r.length);const d=n?this.buildNativeBridgeInstruction(e.keypair.publicKey,c,l,r):this.buildTokenBridgeInstruction(e.keypair.publicKey,o,t,i,c,l,r),u=new a.Transaction;u.add(a.ComputeBudgetProgram.setComputeUnitPrice({microLamports:375e3}),a.ComputeBudgetProgram.setComputeUnitLimit({units:2e5}),d),u.feePayer=e.keypair.publicKey;const h=await this.sendAndConfirmWithFallback(u,e.keypair),g={collection:e.metadata.descriptor.collection,category:e.metadata.descriptor.category,type:e.metadata.descriptor.type,additionalKey:e.metadata.descriptor.additionalKey,instance:"0"};return await this.galaConnectClient.registerBridgeTransaction({quantity:e.amount,tokenInstance:g,fromChain:"Solana",toChain:"GC",hash:h}),h}async sendAndConfirmWithFallback(e,t,n=3){let i=null;for(let o=1;o<=n;o++){try{const a=await this.solanaConnection.getLatestBlockhash("confirmed");e.recentBlockhash=a.blockhash,e.feePayer=t.publicKey,e.signatures=[],e.sign(t);const r=await this.solanaConnection.sendRawTransaction(e.serialize(),{skipPreflight:!1,preflightCommitment:"confirmed"});try{return await this.solanaConnection.confirmTransaction({signature:r,blockhash:a.blockhash,lastValidBlockHeight:a.lastValidBlockHeight},"confirmed"),r}catch(e){const t=ot(e)?e.message.toLowerCase():"";if(!(t.includes("block height exceeded")||t.includes("blockhash not found")||t.includes("expired")))throw e;const a=await this.solanaConnection.getSignatureStatuses([r]);if("confirmed"===a.value[0]?.confirmationStatus||"finalized"===a.value[0]?.confirmationStatus)return r;i=new Error(`Transaction ${r} not confirmed - block height exceeded (attempt ${o}/${n})`)}}catch(e){i=ot(e)?e:new Error(at(e));const t=i.message.toLowerCase();if(!(t.includes("block height exceeded")||t.includes("blockhash not found")||t.includes("timeout")||t.includes("expired"))||o===n)throw i}const a=Math.min(1e3*Math.pow(2,o-1),5e3);await pr(a)}throw i??new Error("Transaction confirmation failed after max retries")}buildNativeBridgeInstruction(e,t,n,i){const o=Buffer.concat([Ea.BRIDGE_OUT_NATIVE,t,n,i]);return new a.TransactionInstruction({programId:this.solanaBridgeProgramId,keys:[{pubkey:e,isSigner:!0,isWritable:!0},{pubkey:this.solanaBridgeTokenAuthority,isSigner:!1,isWritable:!0},{pubkey:this.solanaNativeBridgePda,isSigner:!1,isWritable:!1},{pubkey:this.solanaBridgeConfigPda,isSigner:!1,isWritable:!0},{pubkey:a.SystemProgram.programId,isSigner:!1,isWritable:!1}],data:o})}buildTokenBridgeInstruction(e,t,n,i,o,r,c){const l=Buffer.concat([Ea.BRIDGE_OUT,o,r,c]);return new a.TransactionInstruction({programId:this.solanaBridgeProgramId,keys:[{pubkey:e,isSigner:!0,isWritable:!0},{pubkey:t,isSigner:!1,isWritable:!0},{pubkey:n,isSigner:!1,isWritable:!0},{pubkey:i.mintLookup,isSigner:!1,isWritable:!1},{pubkey:i.tokenBridge,isSigner:!1,isWritable:!1},{pubkey:i.bridgeTokenAccount,isSigner:!1,isWritable:!0},{pubkey:this.solanaBridgeTokenAuthority,isSigner:!1,isWritable:!1},{pubkey:this.solanaBridgeConfigPda,isSigner:!1,isWritable:!0},{pubkey:a.SystemProgram.programId,isSigner:!1,isWritable:!1},{pubkey:s.TOKEN_PROGRAM_ID,isSigner:!1,isWritable:!1}],data:l})}async getSolanaBridgeAccounts(e){const t=e.toBase58(),n=this.solanaBridgeAccountCache.get(t);if(n)return n;const[i]=a.PublicKey.findProgramAddressSync([Buffer.from("mint_lookup_v1"),e.toBuffer()],this.solanaBridgeProgramId),o=await this.solanaConnection.getAccountInfo(i,"confirmed");if(!o)throw Ut(`Mint lookup account not found for ${i.toBase58()}`,"solanaBridgeProgram");if(!o.owner.equals(this.solanaBridgeProgramId))throw Ut("Mint lookup account owner mismatch for Solana bridge program","solanaBridgeProgram");if(o.data.length<40)throw Ut("Mint lookup account data is unexpectedly short","solanaBridgeProgram");const r={mintLookup:i,tokenBridge:new a.PublicKey(o.data.slice(8,40)),bridgeTokenAccount:$r(e,this.solanaBridgeTokenAuthority,0,s.TOKEN_PROGRAM_ID,s.ASSOCIATED_TOKEN_PROGRAM_ID)};return this.solanaBridgeAccountCache.set(t,r),r}}const qr={PROD:{ethereum:"https://eth.gala.games",solana:"https://api.mainnet-beta.solana.com"},STAGE:{ethereum:"https://dev.eth.gala.games",solana:"https://api.devnet.solana.com"}},Gr={solanaBridgeProgram:"AaE4dTnL75XqgUJpdxBKg6vS9sTJgBPJwBQRVhD29WwS",rateLimit:12,pollInterval:15e3,pollTimeout:27e5};class Wr{static normalizeGalaChainAddress(e){const t=new gi;if("client"===t.detectFormat(e))return e;let n=e;(e.startsWith("eth|0x")||e.startsWith("eth|0X"))&&(n=e.substring(0,4)+e.substring(6));const i=t.normalizeInput(n);if(!i)throw Ut(`Invalid GalaChain address format: ${e}`,"galaChainWalletAddress");const o=t.extractHex(i);return`eth|${Wr.checksumAddress(o)}`}static checksumAddress(e){const n=e.toLowerCase(),i=ui(t.keccak256(t.toUtf8Bytes(n)));let o="";for(let e=0;e<n.length;e++){const t=n[e];parseInt(i[e],16)>=8?o+=t.toUpperCase():o+=t}return o}constructor(e){const t=Wr.normalizeGalaChainAddress(e.galaChainWalletAddress),n=e.environment??"PROD",i=ra[n],o=qr[n],a={galaConnectBaseUrl:e.galaConnectBaseUrl??i.dexApiBaseUrl,galaChainApiBaseUrl:e.galaChainApiBaseUrl??i.galaChainBaseUrl,ethereumRpcUrl:e.ethereumRpcUrl??o.ethereum,solanaRpcUrl:e.solanaRpcUrl??o.solana,ethereumBridgeContract:e.ethereumBridgeContract??va(n),solanaBridgeProgram:e.solanaBridgeProgram??Gr.solanaBridgeProgram,rateLimit:e.rateLimit??Gr.rateLimit,pollInterval:e.pollInterval??Gr.pollInterval,pollTimeout:e.pollTimeout??Gr.pollTimeout,galaChainWalletAddress:t,ethereumPrivateKey:e.ethereumPrivateKey,environment:n};this.config=e.solanaPrivateKey?{...a,solanaPrivateKey:e.solanaPrivateKey}:a,this.galaConnectClient=new Ur({baseUrl:this.config.galaConnectBaseUrl,galachainBaseUrl:this.config.galaChainApiBaseUrl,walletAddress:this.config.galaChainWalletAddress,requestsPerSecond:this.config.rateLimit}),e.bridgeableTokenService&&(this.bridgeableTokenService=e.bridgeableTokenService),this.strategies=new Map,this.initializeStrategies()}initializeStrategies(){const e=ka(this.config.environment),t={galaConnectClient:this.galaConnectClient,galaChainWalletAddress:this.config.galaChainWalletAddress,ethereumPrivateKey:this.config.ethereumPrivateKey,ethereumRpcUrl:this.config.ethereumRpcUrl,ethereumBridgeContract:this.config.ethereumBridgeContract,tokenConfigs:e};if(this.strategies.set("Ethereum",new Mr(t)),null!==this.config.solanaPrivateKey&&void 0!==this.config.solanaPrivateKey){const e={galaConnectClient:this.galaConnectClient,galaChainWalletAddress:this.config.galaChainWalletAddress,ethereumPrivateKey:this.config.ethereumPrivateKey,solanaPrivateKeyBase58:this.config.solanaPrivateKey,solanaRpcUrl:this.config.solanaRpcUrl,solanaBridgeProgram:this.config.solanaBridgeProgram,tokenConfigs:ba};this.strategies.set("Solana",new Kr(e))}}async resolveTokenSymbol(e,t){return ea(async()=>{if(!this.bridgeableTokenService)throw Ut("BridgeableTokenService is required for tokenId resolution. Pass bridgeableTokenService in BridgeServiceConfig or use the SDK's bridge methods.","bridgeableTokenService");const n=Er(e).stringified,i="Ethereum"===t?"ETHEREUM":"SOLANA",o=await this.bridgeableTokenService.getTokenByTokenId(n,i);if(!o){throw Ut([`Token "${n}" was not found in the list of tokens bridgeable to ${t}.`,"","Troubleshooting suggestions:",' 1. Verify the tokenId format is correct (e.g., "GALA|Unit|none|none")'," 2. Check if the token supports bridging to this network:",` - Use sdk.fetchAllBridgeableTokensByNetwork('${i}')`," - Or use sdk.isTokenBridgeableToNetwork({ tokenSymbol, network })"," 3. Common tokenId formats for bridge tokens:",' - GALA: "GALA|Unit|none|none"',' - GUSDC: "GUSDC|Unit|none|eth:0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"'," 4. Use sdk.getSupportedBridgeTokens() to list all available tokens"].join("\n"),"tokenId")}return o.symbol},"BridgeService.resolveTokenSymbol")}async estimateBridgeFee(e){return ea(async()=>{const t=await this.resolveTokenSymbol(e.tokenId,e.destinationChain);return this.getStrategy(e.destinationChain).estimateFee(t,e.amount??"0")},"BridgeService.estimateBridgeFee")}async bridgeOut(e){return ea(async()=>{const t=await this.resolveTokenSymbol(e.tokenId,e.destinationChain),n=this.getStrategy(e.destinationChain);if(!n.isValidAddress(e.recipientAddress))throw Ut(`Invalid recipient address for ${e.destinationChain}: ${e.recipientAddress}`,"recipientAddress");if(!n.isTokenSupported(t))throw Ut(`Token ${t} is not supported for ${e.destinationChain} bridging`,"tokenSymbol");return n.bridgeOut({...e,tokenSymbol:t})},"BridgeService.bridgeOut")}async bridgeIn(e){return ea(async()=>{const t=await this.resolveTokenSymbol(e.tokenId,e.sourceChain),n=this.getStrategy(e.sourceChain);if(!n.isTokenSupported(t))throw Ut(`Token ${t} is not supported for ${e.sourceChain} bridging`,"tokenSymbol");return n.bridgeIn({...e,tokenSymbol:t})},"BridgeService.bridgeIn")}async getBridgeStatus(e,t){return ea(async()=>{if(null!=t){const n=this.strategies.get(t);if(n)return n.getStatus(e)}const n=this.strategies.get("Ethereum");let i;if(n)try{return await n.getStatus(e)}catch(e){i=ot(e)?e:new Error(at(e))}const o=this.strategies.get("Solana");if(o)try{return await o.getStatus(e)}catch(e){i=ot(e)?e:new Error(at(e))}const a=i?` (last error: ${i.message})`:"";throw Ot(`Unable to get status for transaction ${e}${a}`,e)},"BridgeService.getBridgeStatus")}async waitForBridgeCompletion(e,t){return ea(async()=>{const n={pollInterval:t?.pollInterval??this.config.pollInterval,timeout:t?.timeout??this.config.pollTimeout,...t?.onStatusUpdate&&{onStatusUpdate:t.onStatusUpdate}},i=this.strategies.get("Ethereum");if(i)try{return await i.getStatus(e),i.waitForCompletion(e,n)}catch{}const o=this.strategies.get("Solana");if(o)return o.waitForCompletion(e,n);throw Ot(`Unable to wait for transaction ${e}: no suitable strategy found`,e)},"BridgeService.waitForBridgeCompletion")}getSupportedBridgeTokens(e){const t=[],n=ka(this.config.environment);if(null==e||"Ethereum"===e)for(const e of n)t.push({symbol:e.symbol,decimals:e.decimals??18,verified:!0,supportedChains:["Ethereum"],galaChainDescriptor:{collection:e.symbol.startsWith("G")?e.symbol:`G${e.symbol}`,category:"Unit",type:"none",additionalKey:"none"},externalAddresses:{ethereum:e.contractAddress}});if(null==e||"Solana"===e)for(const e of ba){const n=t.find(t=>t.symbol===e.symbol);n?(n.supportedChains.push("Solana"),n.externalAddresses.solana=e.mintAddress):t.push({symbol:e.symbol,decimals:e.decimals??9,verified:!0,supportedChains:["Solana"],galaChainDescriptor:{collection:e.symbol.startsWith("G")?e.symbol:`G${e.symbol}`,category:"Unit",type:"none",additionalKey:"none"},externalAddresses:{solana:e.mintAddress}})}return t}getSupportedBridgeChains(){return Array.from(this.strategies.keys())}isTokenSupported(e,t){if(null!=t){const n=this.strategies.get(t);return n?.isTokenSupported(e)??!1}for(const t of this.strategies.values())if(t.isTokenSupported(e))return!0;return!1}isValidAddress(e,t){const n=this.strategies.get(t);return n?.isValidAddress(e)??!1}async getEthereumTokenBalance(e,t){return ea(async()=>fr(this.strategies).getEthereumTokenBalance(e,t),"BridgeService.getEthereumTokenBalance")}async getEthereumNativeBalance(e){return ea(async()=>fr(this.strategies).getEthereumNativeBalance(e),"BridgeService.getEthereumNativeBalance")}async getSolanaTokenBalance(e,t){return ea(async()=>yr(this.strategies).getSolanaTokenBalance(e,t),"BridgeService.getSolanaTokenBalance")}async getSolanaNativeBalance(e){return ea(async()=>yr(this.strategies).getSolanaNativeBalance(e),"BridgeService.getSolanaNativeBalance")}async fetchEthereumWalletTokenBalance(e,t){return ea(async()=>{const n=fr(this.strategies),i=Fa(ka(this.config.environment),e,"Ethereum"),o=kr(t,n);return xa(i,await n.getEthereumTokenBalance(e,o))},"BridgeService.fetchEthereumWalletTokenBalance")}async fetchEthereumWalletNativeBalance(e){return ea(async()=>{const t=fr(this.strategies),n=kr(e,t);return Pa("ETH",await t.getEthereumNativeBalance(n),18)},"BridgeService.fetchEthereumWalletNativeBalance")}async fetchSolanaWalletTokenBalance(e,t){return ea(async()=>{const n=yr(this.strategies),i=Fa(ba,e,"Solana"),o=kr(t,n);return _a(i,await n.getSolanaTokenBalance(e,o))},"BridgeService.fetchSolanaWalletTokenBalance")}async fetchSolanaWalletNativeBalance(e){return ea(async()=>{const t=yr(this.strategies),n=kr(e,t);return Pa("SOL",await t.getSolanaNativeBalance(n),9)},"BridgeService.fetchSolanaWalletNativeBalance")}async requestSolanaDevnetAirdrop(e,t){return ea(async()=>yr(this.strategies).requestDevnetAirdrop(e,t),"BridgeService.requestSolanaDevnetAirdrop")}async getSolanaTransactionStatus(e){return ea(async()=>yr(this.strategies,"Solana bridge strategy not configured. This method requires Solana wallet configuration. Initialize SDK with solanaPrivateKey to use Solana features.").getSolanaTransactionStatus(e),"BridgeService.getSolanaTransactionStatus")}async getEthereumTransactionStatus(e){return ea(async()=>fr(this.strategies,"Ethereum bridge strategy not configured. This method requires Ethereum wallet configuration. Initialize SDK with ethereumPrivateKey to use Ethereum features.").getEthereumTransactionStatus(e),"BridgeService.getEthereumTransactionStatus")}async fetchEthereumWalletAllBalances(e){return ea(async()=>{const t=fr(this.strategies),n=kr(e,t),i=ka(this.config.environment),[o,...a]=await Promise.all([t.getEthereumNativeBalance(n),...i.map(async e=>xa(e,await t.getEthereumTokenBalance(e.symbol,n)))]);return{address:n,native:Pa("ETH",o,18),tokens:a,timestamp:Date.now()}},"BridgeService.fetchEthereumWalletAllBalances")}async fetchSolanaWalletAllBalances(e){return ea(async()=>{const t=yr(this.strategies),n=kr(e,t),[i,...o]=await Promise.all([t.getSolanaNativeBalance(n),...ba.map(async e=>_a(e,await t.getSolanaTokenBalance(e.symbol,n)))]);return{address:n,native:Pa("SOL",i,9),tokens:o,timestamp:Date.now()}},"BridgeService.fetchSolanaWalletAllBalances")}getStrategy(e){const t=this.strategies.get(e);if(!t)throw Ut(`Bridging to ${e} is not configured. `+("Solana"===e?"Please provide solanaPrivateKey in config.":"Please check your configuration."),`${e.toLowerCase()}PrivateKey`);return t}}const Hr="5.0.4-beta.63",zr={DEFAULT_LIMIT:10,BACKEND_MAX_PAGE_SIZE:20};function jr(e,t=1,n=zr.BACKEND_MAX_PAGE_SIZE){return Math.max(t,Math.min(n,Math.ceil(e??t)))}function Vr(e,t){if("number"!=typeof e||!Number.isInteger(e))throw qt("total","a non-negative integer",e);if(e<0)throw Mt("total",0,1/0,e);if("number"!=typeof t||!Number.isInteger(t))throw qt("limit","a non-negative integer",t);if(t<0)throw Mt("limit",0,1/0,t);return 0===t?1:0===e?0:Math.ceil(e/t)}function Qr(e,t){const n={};void 0!==e.cursor&&(n.cursor=e.cursor);const i=void 0!==e.pageSize?Math.min(e.pageSize,t):t;return n.pageSize=String(i),n}function Xr(e,t,n){for(const i of n){const n=t[i];null!=n&&""!==n&&(e[i]=String(n))}return e}const Jr="x-api-key";class Yr extends ri{constructor(e,t,n,i=!1,o){super(e,i),this.adminApiKey=t??void 0,this.jwtAuth=n,this.userApiKey=o??void 0}setJwtAuth(e){this.jwtAuth=e}validateTokenName(e,t){!function(e,t){if(!jn(e))throw Ft("tokenName","Token name");const n=en(e);if(!t.PATTERN.test(n))throw Rt("tokenName",`${t.MIN_LENGTH}-${t.MAX_LENGTH} alphanumeric characters`,"Token name")}(e,t)}validateRequiredString(e,t,n){!function(e,t,n){if(!jn(e))throw Ft(t,n)}(e,t,n)}validateOptionalString(e,t,n,i){!function(e,t,n,i){if(null!=e){if("string"!=typeof e)throw qt(t,"string",typeof e,n);if(e.length>i)throw Kt(t,i,e.length,n)}}(e,t,n,i)}validateOptionalNumber(e,t,n,i,o,a,r){!function(e,t,n,i,o){if(null!=e){if("number"!=typeof e)throw qt(t,"number",typeof e,n);if(e<i||e>o)throw Mt(t,i,o,e,n)}}(e,t,n,i,o)}validateOptionalDate(e,t,n){!function(e,t,n){if(null!=e&&!dn(e))throw Rt(t,"a valid ISO 8601 date string",n)}(e,t,n)}validatePositiveInteger(e,t,n){oi(e,t,n)}validateStatusFilter(e,t,n="status"){ai(e,t,n)}buildPaginationParams(e,t){return Qr(e,t)}addOptionalFilterParams(e,t,n){return Xr(e,t,n)}buildEndpoint(e,t){return function(e,t){let n=e;for(const[e,i]of Object.entries(t))n=n.replace(`:${e}`,encodeURIComponent(i.toLowerCase()));return n}(e,t)}buildEndpointWithId(e,t){return e.replace(":id",encodeURIComponent(t))}validateAndBuildTokenEndpoint(e,t,n){return this.validateTokenName(e,t),this.buildEndpoint(n,{tokenName:e})}getAdminHeaders(){if(void 0===this.adminApiKey||""===this.adminApiKey)throw new St("Admin API key required for this operation. Set streamAdminApiKey in SDK config.");return{[Jr]:this.adminApiKey}}getJwtHeaders(){if(!this.jwtAuth)throw new St("JWT authentication required. Call sdk.login() first.");return this.jwtAuth.getJwtHeaders()}hasJwtAuth(){return this.jwtAuth?.isValid()??!1}hasAdminApiKey(){return void 0!==this.adminApiKey&&""!==this.adminApiKey}getDualAuthHeaders(){return void 0!==this.adminApiKey&&""!==this.adminApiKey?this.getAdminHeaders():this.getJwtHeaders()}getMultiAuthHeaders(){return void 0!==this.userApiKey&&""!==this.userApiKey?{[Jr]:this.userApiKey}:this.getJwtHeaders()}extractData(e){return jt(e,"Backend request failed",!0),Qt(e,"No data in backend response")}extractDataOrNull(e){return jt(e,"Backend request failed",!1),e.data??null}async toggleFeature(e,t,n){this.validateTokenName(e,n);const i=this.buildEndpoint(t,{tokenName:e}),o=await this.http.post(i,{},this.getAdminHeaders()),a=this.extractData(o);return{enabled:a.enabled,tokenName:a.tokenName??en(e)}}}class Zr extends Yr{constructor(e,t,n,i=!1,o){super(e,t,n,i,o)}async getSettings(){return{settings:(await this.http.get(We.GET_SETTINGS,{},this.getDualAuthHeaders())).settings}}async updateSettings(e){const t={};void 0!==e.enabled&&(t.enabled=e.enabled),void 0!==e.mode&&(t.mode=e.mode),void 0!==e.provider&&(t.provider=e.provider);return{settings:(await this.http.post(We.UPDATE_SETTINGS,t,this.getDualAuthHeaders())).settings}}async getModeration(e){this.validatePositiveInteger(e.flagId,"flagId","Flag ID");const t=We.GET_MODERATION.replace(":flagId",String(e.flagId));return{moderation:(await this.http.get(t,{},this.getDualAuthHeaders())).analysis}}async triggerModeration(e){this.validatePositiveInteger(e.flagId,"flagId","Flag ID");const t=We.TRIGGER_MODERATION.replace(":flagId",String(e.flagId)),n=await this.http.post(t,{},this.getDualAuthHeaders());return{queued:n.queued,message:n.message.length>0?n.message:"Moderation queued successfully"}}async getStatus(){return{status:(await this.http.get(We.GET_STATUS,{},this.getDualAuthHeaders())).status}}}const es=Jn({MODERATOR:"MODERATOR",TECHNICAL_PRODUCER:"TECHNICAL_PRODUCER",MANAGER:"MANAGER",OWNER:"OWNER"});function ts(e){return es.includes(e)}class ns extends ri{constructor(e,t,n=!1){super(e,n,t)}extractData(e){let t;t="error"in e?e.error:"success"in e&&!e.success;const n={status:200,error:t,data:e.data};return void 0!==e.message&&(n.message=e.message),jt(n,"API key operation failed",!0),e.data}validateApiKeyId(e){this.validatePositiveInteger(e,"id","API key ID")}async create(e){const t=function(e){const t=[];void 0===e.role||null===e.role?t.push("Role is required"):ts(e.role)||t.push(`Invalid role. Must be one of: ${es.join(", ")}`);const n=t.length;if(ut(t,()=>{ni(e.description,"description",255)}),t.length>n&&(t[n]="Description must be 255 characters or less"),void 0!==e.tokenNames){const n=t.length;ut(t,()=>{if(!Array.isArray(e.tokenNames))throw qt("tokenNames","an array",e.tokenNames);if(!e.tokenNames.every(e=>"string"==typeof e))throw qt("tokenNames","contain only strings")});for(let e=n;e<t.length;e++){const n=t[e];n.includes("array")?t[e]="tokenNames must be an array":n.includes("string")&&(t[e]="tokenNames must contain only strings")}}if(void 0!==e.expiresAt){const n=t.length;ut(t,()=>{ii(e.expiresAt,"expiresAt")}),t.length>n&&(t[n]="expiresAt must be a valid ISO 8601 date string")}return t}(e);if(t.length>0)throw new wt(t.join("; "),"options","VALIDATION_FAILED");this.logger.debug("Creating API key",{role:e.role,delegateAllTokens:e.delegateAllTokens});const n={role:e.role};void 0!==e.description&&(n.description=e.description),void 0!==e.delegateAllTokens&&(n.delegateAllTokens=e.delegateAllTokens),void 0!==e.tokenNames&&e.tokenNames.length>0&&(n.tokenNames=e.tokenNames),void 0!==e.expiresAt&&(n.expiresAt=e.expiresAt),void 0!==e.tokenPermissions&&(n.tokenPermissions=e.tokenPermissions);const i=await this.http.post(V.CREATE,n,this.getJwtHeaders()),o=this.extractData(i);return this.logger.debug("API key created",{id:o.id,keyPrefix:o.keyPrefix}),o}async findAll(e={}){!function(e){if(void 0!==e.pageSize&&("number"!=typeof e.pageSize||e.pageSize<1||e.pageSize>pn))throw qt("pageSize",`number between 1 and ${pn}`,typeof e.pageSize);if(void 0!==e.cursor&&("string"!=typeof e.cursor||""===e.cursor.trim()))throw qt("cursor","non-empty string",typeof e.cursor)}(e),this.logger.debug("Listing API keys",e);const t=Qr(e,pn),n=await this.http.get(V.LIST,t,this.getJwtHeaders()),i=this.extractData(n);return this.logger.debug("Listed API keys",{count:i.apiKeys.length,total:i.pageInfo.totalCount}),i}async findOne(e){this.validateApiKeyId(e),this.logger.debug("Getting API key",{id:e});const t=V.GET.replace(":id",String(e)),n=await this.http.get(t,{},this.getJwtHeaders()),i=this.extractData(n);return this.logger.debug("Got API key",{id:i.id,keyPrefix:i.keyPrefix}),i}async update(e,t){this.validateApiKeyId(e);const n=function(e){const t=[];["role","description","delegateAllTokens","tokenNames","expiresAt","tokenPermissions"].some(t=>void 0!==e[t])||t.push("At least one field must be provided for update"),void 0===e.role||ts(e.role)||t.push(`Invalid role. Must be one of: ${es.join(", ")}`);const n=t.length;if(ut(t,()=>{ni(e.description,"description",255)}),t.length>n&&(t[n]="Description must be 255 characters or less"),void 0!==e.tokenNames){const n=t.length;ut(t,()=>{if(!Array.isArray(e.tokenNames))throw qt("tokenNames","an array",e.tokenNames);if(!e.tokenNames.every(e=>"string"==typeof e))throw qt("tokenNames","contain only strings")});for(let e=n;e<t.length;e++){const n=t[e];n.includes("array")?t[e]="tokenNames must be an array":n.includes("string")&&(t[e]="tokenNames must contain only strings")}}if(void 0!==e.expiresAt&&null!==e.expiresAt){const n=t.length;ut(t,()=>{ii(e.expiresAt,"expiresAt")}),t.length>n&&(t[n]="expiresAt must be a valid ISO 8601 date string")}return t}(t);if(n.length>0)throw new wt(n.join("; "),"options","VALIDATION_FAILED");this.logger.debug("Updating API key",{id:e,options:t});const i={};void 0!==t.role&&(i.role=t.role),void 0!==t.description&&(i.description=t.description),void 0!==t.delegateAllTokens&&(i.delegateAllTokens=t.delegateAllTokens),void 0!==t.tokenNames&&(i.tokenNames=t.tokenNames),void 0!==t.expiresAt&&(i.expiresAt=t.expiresAt),void 0!==t.tokenPermissions&&(i.tokenPermissions=t.tokenPermissions);const o=V.UPDATE.replace(":id",String(e)),a=await this.http.patch(o,i,this.getJwtHeaders()),r=this.extractData(a);return this.logger.debug("Updated API key",{id:r.id,keyPrefix:r.keyPrefix}),r}async revoke(e){this.validateApiKeyId(e),this.logger.debug("Revoking API key",{id:e});const t=V.REVOKE.replace(":id",String(e));await this.http.delete(t,{},this.getJwtHeaders()),this.logger.debug("API key revoked",{id:e})}getRoles(){return[...es]}}const is={VIEWERS:"viewers",CHAT_PARTICIPANTS:"chat_participants"},os=Xn(is);function as(e){return null==e||""===e||"string"==typeof e&&(!ti(e)&&e.length<=vn.BAN_REASON.MAX_LENGTH)}function rs(e){return!!Vn(e)||"number"==typeof e&&(e>=bn&&e<=Sn)}class ss extends Yr{constructor(e,t,n,i=!1,o){super(e,t,n,i,o)}async createBan(e){!function(e){if(Zn(e.tokenName,"tokenName",hn),!jn(e.userAddress))throw Ft("userAddress");if(!as(e.reason))throw new wt(`reason must be at most ${vn.BAN_REASON.MAX_LENGTH} characters`,"reason","TOO_LONG");if(!rs(e.durationSeconds))throw new wt(`durationSeconds must be between ${bn} and ${Sn} seconds`,"durationSeconds","OUT_OF_RANGE")}(e);const t=this.buildEndpoint(M,{tokenName:e.tokenName}),n={userAddress:e.userAddress};void 0!==e.reason&&(n.reason=e.reason),void 0!==e.durationSeconds&&(n.durationSeconds=e.durationSeconds);const i=await this.http.post(t,n,this.getMultiAuthHeaders());return{ban:this.extractData(i),tokenName:en(e.tokenName)}}async removeBan(e){!function(e){if(Zn(e.tokenName,"tokenName",hn),!jn(e.userAddress))throw Ft("userAddress")}(e);const t=this.buildEndpoint(q,{tokenName:e.tokenName,userAddress:e.userAddress}),n=await this.http.delete(t,{},this.getMultiAuthHeaders()),i=this.extractDataOrNull(n);return{removed:i?.removed??!0,tokenName:en(e.tokenName),userAddress:i?.userAddress??e.userAddress.toLowerCase()}}async listBans(e){!function(e){Zn(e.tokenName,"tokenName",hn),Qn(0,void 0,pn,e.pageSize)}(e);const t=this.buildEndpoint($,{tokenName:e.tokenName}),n=this.buildPaginationParams(e,pn);this.addOptionalFilterParams(n,e,["search","name","userAddress"]);const i=await this.http.get(t,n,this.getMultiAuthHeaders()),o=this.extractData(i);return{items:o.bans,pageInfo:o.pageInfo}}async getBanStatus(e){!function(e){if(Zn(e.tokenName,"tokenName",hn),!jn(e.userAddress))throw Ft("userAddress")}(e);const t=this.buildEndpoint(K,{tokenName:e.tokenName,userAddress:e.userAddress}),n=await this.http.get(t,{},this.getMultiAuthHeaders()),i=this.extractDataOrNull(n);return{banned:null!==i,...null!==i&&{ban:i},tokenName:en(e.tokenName),userAddress:e.userAddress.toLowerCase()}}async getActiveUsers(e){!function(e){if(Zn(e.tokenName,"tokenName",hn),void 0!==e.type&&!os(e.type))throw new wt(`type must be one of: ${Object.values(is).join(", ")}`,"type",ft)}(e);const t=this.buildEndpoint(G,{tokenName:e.tokenName}),n={};this.addOptionalFilterParams(n,e,["type","search","name","userAddress"]);const i=await this.http.get(t,n,this.getMultiAuthHeaders());return this.extractData(i)}async createGlobalBan(e){!function(e){if(!jn(e.userAddress))throw Ft("userAddress");if(!as(e.reason))throw new wt(`reason must be at most ${vn.BAN_REASON.MAX_LENGTH} characters`,"reason","TOO_LONG");if(!rs(e.durationSeconds))throw new wt(`durationSeconds must be between ${bn} and ${Sn} seconds`,"durationSeconds","OUT_OF_RANGE")}(e);const t={userAddress:e.userAddress};void 0!==e.reason&&(t.reason=e.reason),void 0!==e.durationSeconds&&(t.durationSeconds=e.durationSeconds);const n=await this.http.post(W,t,this.getDualAuthHeaders());return{ban:this.extractData(n)}}async removeGlobalBan(e){!function(e){if(!jn(e.userAddress))throw Ft("userAddress")}(e);const t=this.buildEndpoint(j,{userAddress:e.userAddress}),n=await this.http.delete(t,{},this.getDualAuthHeaders()),i=this.extractDataOrNull(n);return{removed:i?.removed??!0,userAddress:i?.userAddress??e.userAddress.toLowerCase()}}async listGlobalBans(e){const t=e??{pageSize:20};!function(e){Qn(0,void 0,pn,e.pageSize)}(t);const n=this.buildPaginationParams(t,pn);this.addOptionalFilterParams(n,t,["search","name","userAddress"]);const i=await this.http.get(H,n,this.getDualAuthHeaders()),o=this.extractData(i);return{items:o.bans,pageInfo:o.pageInfo}}async getGlobalBan(e){!function(e){if(!jn(e.userAddress))throw Ft("userAddress")}(e);const t=this.buildEndpoint(z,{userAddress:e.userAddress}),n=await this.http.get(t,{},this.getDualAuthHeaders()),i=this.extractDataOrNull(n);return{banned:null!==i,...null!==i&&{ban:i},userAddress:e.userAddress.toLowerCase()}}}async function cs(e,t={}){const{maxPages:n=1e4,logger:i,pageSize:o=20,concurrency:a=1,startPage:r=1}=t,s=[];let c=r;const l=r+n-1;let d=!0,u=0;if(a<=1)for(;d&&c<=l;){i&&i.debug(`Auto-pagination: fetching page ${c} with limit ${o}`);const t=await e(c,o);if(null==t||!Array.isArray(t.items)){i&&i.warn("Auto-pagination: received invalid result structure, stopping");break}if(s.push(...t.items),u=t.total,i&&i.debug(`Auto-pagination: page ${c} returned ${t.items.length} items, hasNext: ${t.hasNext}`),0===t.items.length){i&&i.debug(`Auto-pagination: no items returned on page ${c}, exiting loop`);break}d=t.hasNext,c++}else for(i&&i.debug(`Auto-pagination: using concurrent mode with concurrency=${a}`);d&&c<=l;){const t=[];for(let e=0;e<a&&c+e<=l;e++)t.push(c+e);i&&i.debug(`Auto-pagination: fetching pages ${t.join(", ")} concurrently`);const n=t.map(t=>e(t,o).catch(e=>{const n=e,a=n.statusCode??n.response?.status??n.originalError?.response?.status??n.status??n.launchpadError?.statusCode;if(400===a||404===a)return i&&i.debug(`Auto-pagination: page ${t} returned ${a} (end of pagination)`,{statusCode:a}),{items:[],total:0,totalPages:0,page:t,limit:o,hasNext:!1,hasPrevious:!1};throw e})),r=await Promise.all(n);for(const e of r){if(null==e||!Array.isArray(e.items)){i&&i.warn("Auto-pagination: received invalid result structure in batch, stopping"),d=!1;break}if(0===e.items.length){i&&i.debug("Auto-pagination: empty page in batch, reached end of results"),d=!1;break}if(s.push(...e.items),u=e.total,!e.hasNext){d=!1;break}}c+=t.length,i&&i.debug(`Auto-pagination: batch complete, total items so far: ${s.length}`)}return c>l&&i&&i.warn(`Auto-pagination: exceeded maxPages limit of ${n} (maxPage=${l}), stopping iteration`),i&&i.debug(`Auto-pagination: completed with total items: ${s.length}, total count: ${u}`),{items:s,total:u}}function ls(e){if("string"==typeof e){if(!Sr(e))throw new wt(`Invalid tokenId string format: "${e}". Expected pipe-delimited format: "collection|category|type|additionalKey" or "collection|category|type|additionalKey|instance"`,"tokenId","INVALID_TOKEN_ID_FORMAT");const t=e.split("|");if(t.length>=5){const e=br(t.slice(0,4).join("|")),n=t[4]??"0";return{...e,instance:n.length>0?n:"0"}}return{...br(e),instance:"0"}}if("object"==typeof e&&null!==e){if("instance"in e&&void 0!==e.instance&&"string"==typeof e.instance&&e.instance.length>0){if(!Tr(e))throw new wt("Invalid tokenId object format. All fields (collection, category, type, additionalKey) must be non-empty strings","tokenId","INVALID_TOKEN_ID_FORMAT");return e}if(!Tr(e))throw new wt("Invalid tokenId object format. All fields (collection, category, type, additionalKey) are required","tokenId","INVALID_TOKEN_ID_FORMAT");return{...e,instance:"0"}}throw new wt(`Invalid tokenId type: ${typeof e}. Expected string, TokenClassKey, or TokenInstanceKey`,"tokenId","INVALID_TOKEN_ID_TYPE")}function ds(e){return function(e){try{if(!jn(e))throw new Error("Vault address must be a non-empty string");const[t,n]=e.split("|");if(""===n||void 0===n)throw new Error("Missing token part after service prefix");const i=n.split("$");if(i.length<4)throw new Error(`Invalid vault address format: expected at least 4 parts separated by $, got ${i.length}`);const[o,a,r,...s]=i;if(""===o||""===a||""===r)throw new Error("Collection, category, and type must be non-empty");const c=s.slice(0,-1),l=c.length>0?c.join("$"):s[0];if(""===l||void 0===l)throw new Error("AdditionalKey must be non-empty");return{collection:o,category:a,type:r,additionalKey:l}}catch(t){throw new wt(`Invalid vault address: "${e}". Expected format: "service|Token$Unit$SYMBOL$additionalKey$launchpad". Error: ${at(t)}`,"vaultAddress","INVALID_VAULT_ADDRESS_FORMAT")}}(e)}function us(e){return{...ds(e),instance:"0"}}function hs(e){return ds(e).type}function gs(e){return ca(ls(e))}var ms=Object.freeze({__proto__:null,extractTokenSymbolFromVault:hs,isTokenClassKeyStrict:Tr,normalizeToTokenInstanceKey:ls,normalizeTokenIdToString:gs,parseVaultAddressToTokenClassKey:ds,parseVaultAddressToTokenInstance:us});class ps{constructor(e,t=!1,n){this.dexApiHttp=e,this.logger=new an({debug:t,context:n??this.constructor.name})}getApiEndpoint(){return"/v1/tokens"}getMaxLimit(){return 1e3}getDefaultLimit(){return 1e3}async executePaginatedRequest(e,t,n){const i=Math.min(t,this.getMaxLimit()),o=this.buildApiParams(n),a=await this.dexApiHttp.request({method:"GET",url:this.getApiEndpoint(),params:{...o,pageSize:i,offset:e}});if(Vn(a)||!Array.isArray(a.tokens))throw new wt("Invalid API response: expected { tokens: array }","response","INVALID_RESPONSE");return{items:this.transformApiResponse(a.tokens),rawCount:a.tokens.length}}async autoPaginateFetch(e){return async function(e,t){const{maxLimit:n,logger:i,maxPages:o=1e4}=t,a=[];let r=0,s=!0,c=0;for(;s&&c<o;){i&&i.debug(`Auto-pagination (offset): fetching at offset ${r} with limit ${n}`);const t=await e(r,n);if(null==t||!Array.isArray(t.items)){i&&i.warn("Auto-pagination (offset): received invalid result structure, stopping");break}a.push(...t.items),s=t.rawCount===n,r+=n,c++,i&&i.debug(`Auto-pagination (offset): fetched ${a.length} items so far (hasMore=${s})`)}return c>=o&&i&&i.warn(`Auto-pagination (offset): exceeded maxPages limit of ${o}, stopping`),i&&i.debug(`Auto-pagination (offset): completed with total items: ${a.length}`),a}(async(t,n)=>this.executePaginatedRequest(t,n,e),{maxLimit:this.getMaxLimit(),logger:this.logger})}handleError(e,t){throw Lt(e,t,this.logger)}}class fs extends si{constructor(e=!1){super(e),this.primaryIndex=new Map,this.secondaryIndex=new Map,this.fetchTimestamps=new Map}normalizeKey(e){return tn(e)}has(e){const t=this.primaryIndex.get(e);return void 0!==t&&t.size>0}getAll(e){const t=this.primaryIndex.get(e);return t?Array.from(t.values()):[]}getByPrimaryKey(e,t){const n=this.normalizeKey(t);return this.primaryIndex.get(e)?.get(n)}getBySecondaryKey(e,t){const n=this.normalizeKey(t);return this.secondaryIndex.get(e)?.get(n)}set(e,t){const n=new Map,i=new Map;for(const e of t){const t=this.normalizeKey(this.extractPrimaryKey(e));n.set(t,e);const o=this.normalizeKey(this.extractSecondaryKey(e));i.set(o,e)}this.primaryIndex.set(e,n),this.secondaryIndex.set(e,i),this.fetchTimestamps.set(e,Date.now()),this.logger.debug(`Cached ${t.length} items for ${e}`)}merge(e,t){let n=this.primaryIndex.get(e);n||(n=new Map,this.primaryIndex.set(e,n));let i=this.secondaryIndex.get(e);i||(i=new Map,this.secondaryIndex.set(e,i));for(const e of t){const t=this.normalizeKey(this.extractPrimaryKey(e));n.set(t,e);const o=this.normalizeKey(this.extractSecondaryKey(e));i.set(o,e)}this.fetchTimestamps.set(e,Date.now()),this.logger.debug(`Merged ${t.length} items for ${e} (total: ${n.size})`)}getFetchTimestamp(e){return this.fetchTimestamps.get(e)}buildBaseStats(e){const t=[];let n=0;const i={},o={};for(const t of e)i[t]=0;for(const[e,a]of this.primaryIndex){t.push(e),n+=a.size,i[e]=a.size;const r=this.fetchTimestamps.get(e);void 0!==r&&(o[e]=r)}return{networks:t,totalItems:n,itemsByNetwork:i,fetchTimestamps:o}}clear(e){void 0!==e&&""!==e?(this.primaryIndex.delete(e),this.secondaryIndex.delete(e),this.fetchTimestamps.delete(e),this.logger.debug(`Cleared cache for ${e}`)):(this.primaryIndex.clear(),this.secondaryIndex.clear(),this.fetchTimestamps.clear(),this.logger.debug("Cleared all caches"))}size(e){return this.primaryIndex.get(e)?.size??0}hasByPrimaryKey(e,t){return void 0!==this.getByPrimaryKey(e,t)}getCachedNetworks(){return Array.from(this.primaryIndex.keys())}dump(){const e={};for(const t of this.getCachedNetworks())e[t]=this.getAll(t);return e}}const ys=["ETHEREUM","SOLANA"];class ks extends fs{constructor(e=!1){super(e)}extractPrimaryKey(e){return e.symbol}extractSecondaryKey(e){return e.stringifiedTokenClassKey}getBySymbol(e,t){return this.getByPrimaryKey(e,t)}getByTokenId(e,t){return this.getBySecondaryKey(e,t)}getContractAddress(e,t){const n=this.getBySymbol(e,t);if(n)return"ETHEREUM"===e?n.ethereumContractAddress:n.solanaContractAddress}isTokenBridgeable(e,t){return void 0!==this.getBySymbol(e,t)}getStats(){const e=this.buildBaseStats(ys);return{...e,totalTokens:e.totalItems,tokensByNetwork:e.itemsByNetwork}}}class vs extends ps{constructor(e,t=!1){super(e,t,"BridgeableTokenService"),this.cache=new ks(t)}buildApiParams(e){return{canBridgeTo:(e?.network??"ETHEREUM").toLowerCase()}}transformApiResponse(e){return e.map(e=>{const t=e.otherNetworks?.find(e=>"Ethereum"===e.network),n=e.otherNetworks?.find(e=>"Solana"===e.network),i=e.canBridgeTo.map(e=>e.network).filter(e=>"Ethereum"===e||"Solana"===e),o={symbol:e.symbol,name:e.name,decimals:e.decimals,galaChainDescriptor:{collection:e.collection,category:e.category,type:e.type,additionalKey:e.additionalKey},stringifiedTokenClassKey:e.stringifiedTokenClassKey,verified:e.verified,supportedChains:i};return void 0!==t?.contractAddress&&(o.ethereumContractAddress=t.contractAddress),void 0!==t?.symbol&&(o.ethereumSymbol=t.symbol),void 0!==t?.allowanceStorageSlot&&(o.ethereumAllowanceSlot=t.allowanceStorageSlot),void 0!==n?.contractAddress&&(o.solanaContractAddress=n.contractAddress),void 0!==n?.symbol&&(o.solanaSymbol=n.symbol),void 0!==e.image&&(o.image=e.image),void 0!==e.description&&(o.description=e.description),o})}async fetchBridgeableTokensByNetwork(e){const{network:t,offset:n=0,limit:i=this.getDefaultLimit()}=e,o=Math.min(i,this.getMaxLimit());return this.logger.debug(`Fetching bridgeable tokens for ${t} (offset=${n}, limit=${o})`),ea(async()=>{const e=(await this.executePaginatedRequest(n,o,{network:t})).items;return 0===n?this.cache.set(t,e):this.cache.merge(t,e),{tokens:e,network:t,fetchedAt:Date.now(),tokenCount:e.length}},`Failed to fetch bridgeable tokens for ${t}`,this.logger)}async fetchAllBridgeableTokensByNetwork(e){return ea(async()=>{const t=await async function(e){const{network:t,cache:n,fetchFn:i,logger:o,itemTypeName:a="items"}=e;if(n.has(t)){const e=n.getAll(t);return o&&o.debug(`Returning ${e.length} cached ${a} for ${t}`),{items:e,fetchedAt:n.getFetchTimestamp(t)??Date.now(),itemCount:e.length}}o&&o.debug(`Fetching all ${a} for ${t} (no cache)`);const r=await i();return n.set(t,r),{items:r,fetchedAt:Date.now(),itemCount:r.length}}({network:e,cache:this.cache,fetchFn:()=>this.autoPaginateFetch({network:e}),logger:this.logger,itemTypeName:"bridgeable tokens"});return{tokens:t.items,network:e,fetchedAt:t.fetchedAt,tokenCount:t.itemCount}},`Failed to fetch bridgeable tokens for ${e}`,this.logger)}async fetchAllTokensBridgeableToEthereum(){return this.fetchAllBridgeableTokensByNetwork("ETHEREUM")}async fetchAllTokensBridgeableToSolana(){return this.fetchAllBridgeableTokensByNetwork("SOLANA")}async isTokenBridgeableToNetwork(e){const{tokenId:t,network:n}=e,i=gs(t);this.cache.has(n)||await this.fetchAllBridgeableTokensByNetwork(n);const o=this.cache.getByTokenId(n,i),a=void 0!==o,r=a?"ETHEREUM"===n?o.ethereumContractAddress:o.solanaContractAddress:void 0,s={isBridgeable:a,tokenSymbol:o?.symbol??br(i).collection,network:n};return void 0!==r&&(s.contractAddress=r),s}async isTokenBridgeableToEthereum(e){return this.isTokenBridgeableToNetwork({tokenId:e,network:"ETHEREUM"})}async isTokenBridgeableToSolana(e){return this.isTokenBridgeableToNetwork({tokenId:e,network:"SOLANA"})}async getTokenBySymbol(e,t){const n=this.cache.getBySymbol(t,e);return n||(await this.fetchAllBridgeableTokensByNetwork(t),this.cache.getBySymbol(t,e))}async getTokenByTokenId(e,t){const n=this.cache.getByTokenId(t,e);return n||(await this.fetchAllBridgeableTokensByNetwork(t),this.cache.getByTokenId(t,e))}async getContractAddress(e,t){const n=await this.getTokenBySymbol(e,t);if(n)return"ETHEREUM"===t?n.ethereumContractAddress:n.solanaContractAddress}async getSupportedTokenSymbols(e){return this.cache.has(e)||await this.fetchAllBridgeableTokensByNetwork(e),this.cache.getAll(e).map(e=>e.symbol)}async preload(){this.logger.debug("Preloading bridgeable tokens for all networks"),await Promise.all([this.fetchAllBridgeableTokensByNetwork("ETHEREUM"),this.fetchAllBridgeableTokensByNetwork("SOLANA")]),this.logger.debug("Preloading complete")}getCacheStats(){return this.cache.getStats()}clearCache(e){this.cache.clear(e)}}function ws(e){return{maxAcceptableReverseBondingCurveFee:Mn(e.maxAcceptableReverseBondingCurveFee)}}class bs extends n.ChainCallDTO{constructor(e,t,n="0",i={maxAcceptableReverseBondingCurveFee:"0"}){super(),this.vaultAddress=e,this.nativeTokenQuantity=Mn(t),this.expectedToken=$n(n),this.extraFees=ws(i)}}class Ss extends n.ChainCallDTO{constructor(e,t,n,i={maxAcceptableReverseBondingCurveFee:"0"}){super(),this.vaultAddress=e,this.tokenQuantity=$n(t),this.expectedNativeToken=Mn(n),this.extraFees=ws(i)}}class As extends n.ChainCallDTO{constructor(e,t,n="0",i={maxAcceptableReverseBondingCurveFee:"0"}){super(),this.vaultAddress=e,this.tokenQuantity=$n(t),this.expectedNativeToken=Mn(n),this.extraFees=ws(i)}}class Ts extends n.ChainCallDTO{constructor(e,t,n,i={maxAcceptableReverseBondingCurveFee:"0"}){super(),this.vaultAddress=e,this.nativeTokenQuantity=Mn(t),this.expectedToken=$n(n),this.extraFees=ws(i)}}const Es={BuyNativeDto:bs,BuyExactDto:Ss,SellExactDto:As,SellNativeDto:Ts};function Cs(e,t,n){let i;Kn(t,0,1,"slippageToleranceFactor");try{i=Un(e,"expectedAmount")}catch{throw new Error(`Invalid expected amount: ${e}. Must be a valid number`)}if(0===t)return e;const o=i.multipliedBy(t);let a;switch(n){case"buy-native":case"sell-exact":a=i.minus(o);break;case"buy-exact":case"sell-native":a=i.plus(o);break;default:throw new Error(`Unknown operation type: ${n}`)}return So(a)&&(a=po(0)),fo(a)}class Is extends si{constructor(e,t=!1){super(t),this.walletProvider=e}async signDTO(e,t,i){try{this.logger.debug("🔐 Signing DTO:",{methodName:t,dtoKeys:Object.keys(e)});const i=this.generateEIP712Types(t,e),o=n.calculatePersonalSignPrefix(e),a={...e,prefix:o},{signature:r,domain:s}=await this.signWithEthersWallet(i,a),c={...e,signature:r,types:i,domain:s};return this.logger.debug("✅ DTO signed successfully:",{payloadKeys:Object.keys(c),signatureLength:r.length}),c}catch(e){this.logger.error("❌ Signature generation failed:",e);throw Ot(`Failed to sign DTO: ${at(e)}`)}}async signWithEthersWallet(e,t){try{let n,i;if(this.walletProvider.signTypedData&&!this.walletProvider.getNetwork)n={name:"ethereum",chainId:1},i=await this.walletProvider.signTypedData(n,e,t);else{if(!this.walletProvider.getNetwork||!this.walletProvider.signTypedData)throw Ut("Wallet provider does not support typed data signing","walletProvider");{const o=await this.walletProvider.getNetwork();n={name:o.name,chainId:Rn(o.chainId,1)},i=await this.walletProvider.signTypedData(n,e,t)}}return{signature:i,domain:n}}catch(e){throw Ot(`Ethers.js signing failed: ${at(e)}`)}}generateEIP712Types(e,t){const n={};n[e]=[];const i=Object.fromEntries(Object.entries(t).filter(([e,t])=>void 0!==t)),o=(e,t,i,a=!1)=>{if(void 0!==t){if(Array.isArray(t)){if(0===t.length)return;const r=o(e,t[0],i,!0);return a||n[i].push({name:e,type:(r??e)+"[]"}),void 0!==r?r+"[]":void 0}if("object"==typeof t&&null!==t){if(void 0!==n[e])throw new wt(`Type name collision not supported: ${e}`,"fieldValue","TYPE_COLLISION");return n[e]=[],Object.entries(t).filter(([e,t])=>null!=t).forEach(([t,n])=>{o(t,n,e)}),a||n[i].push({name:e,type:e}),e}{let o;switch(typeof t){case"string":o="string";break;case"number":o="uint256";break;case"boolean":o="bool";break;default:throw new wt(`Unsupported type for field "${e}": ${typeof t} (value: ${JSON.stringify(t)})`,"fieldValue","UNSUPPORTED_TYPE")}return a||n[i].push({name:e,type:o}),o}}};return Object.entries(i).forEach(([t,n])=>{o(t,n,e)}),this.logger.debug("📝 Generated EIP-712 types:",n),n}}class Ns{toLaunchpadFormat(e){if(null==e)throw new wt('Token is required. Use full tokenId format: "GALA|Unit|none|none"',"token","MISSING_TOKEN");if("string"==typeof e){if(Sr(e))return e;throw new wt(`Invalid token format "${e}". Use full tokenId format: "${e}|Unit|none|none". For launchpad bonding curve tokens, use tokenName parameter instead (e.g., "anime").`,"token","INVALID_TOKEN_FORMAT")}const t=e;return ca({collection:t.collection??t.symbol??"unknown",category:t.category??"Unit",type:t.type??"none",additionalKey:t.additionalKey??"none"})}toTokenClass(e){if("object"==typeof e&&null!==e){const t=e;return{collection:t.collection??"Token",category:t.category??"Unit",type:t.type??t.symbol??"unknown",additionalKey:t.additionalKey??"none"}}if("string"!=typeof e)throw new Error("Invalid token format: expected string or object, got "+typeof e);const t=vr(e,"|","token format conversion");return{collection:t.collection,category:t.category,type:t.type,additionalKey:t.additionalKey}}normalizeInternalApiResponse(e){return""===e?"":Sr(e)?e:`${e}|Unit|none|none`}normalize(e){return"string"==typeof e&&Sr(e)?e:this.toLaunchpadFormat(e)}}function Ds(e){if(!jn(e))throw new Error("Invalid token format: token must be a non-empty string");return e.replace(/\|/g,"$")}function Ps(e){return vr(e,"$","dollar-delimited token")}class xs extends si{constructor(e=!1){super(e)}generateStringsInstructions(e){try{this.logger.debug("🔧 Generating stringsInstructions for:",e);const t=this.extractTokenSymbolFromVault(e),n=this.createTokenInstance(t),i=this.createGalaInstance(),o=`$service$${n.toStringKey()}$launchpad`,a=`$tokenBalance$${n.toStringKey()}$${e}`,r=`$tokenBalance$${n.toStringKey()}$${e}`,s=`$tokenBalance$${i.toStringKey()}$${e}`,c=[o,a,r,s,`$tokenBalance$${i.toStringKey()}$${e}`];return this.logger.debug("✅ Generated stringsInstructions:",c),c}catch(e){this.logger.error("❌ Failed to generate stringsInstructions:",e);const t=at(e);throw new wt(`Failed to generate stringsInstructions: ${t}`,"vaultAddress","INVALID_VAULT_ADDRESS")}}createTokenInstance(e){const t=new l.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 l.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(!jn(e))throw Ft("vaultAddress","Vault address");try{const t=hs(e);return this.logger.debug("🔍 Extracted token symbol:",{vaultAddress:e,tokenSymbol:t}),t}catch(e){if(e instanceof wt)throw Rt("vaultAddress","format: service|Token$Unit$SYMBOL$eth:address$launchpad");throw e}}validateVaultAddress(e){if(!jn(e))throw Ft("vaultAddress","Vault address");if(!e.startsWith("service|Token$Unit$"))throw Rt("vaultAddress",'starting with "service|Token$Unit$"');if(!e.endsWith("$launchpad"))throw Rt("vaultAddress",'ending with "$launchpad"');const t=function(e){if(!jn(e))return null;const t=e.match(/^service\|Token\$Unit\$([^$]+)\$eth:([a-fA-F0-9]{40})\$launchpad$/);return t?{tokenSymbol:t[1],creatorAddress:t[2].toLowerCase()}:null}(e);if(!t)throw Rt("vaultAddress","valid vault address format (service|Token$Unit$SYMBOL$eth:address$launchpad)");const n=t.tokenSymbol;if(""===n||!/^[A-Za-z]{1,10}$/.test(n))throw Rt("vaultAddress","containing a 1-10 letter token symbol (case insensitive)");return this.logger.debug("✅ Vault address validation passed:",e),!0}generateTokenClassKeyString(e,t,n,i){return`${e}$${t}$${n}$${i}`}parseTokenClassKeyString(e){try{return Ps(e)}catch(e){if(e instanceof wt)throw Rt("stringKey","format: collection$category$type$additionalKey (4 parts)");throw e}}}class _s extends ri{constructor(e,t,n=!1,i,o,a=.05,r=.01){super(e,n),this.tokenResolver=t,this._walletProvider=i,this.userAddress=o,this.defaultSlippageToleranceFactor=a,this.defaultMaxAcceptableReverseBondingCurveFeeSlippageFactor=r,this.bundleEndpoint="/bundle",null!==this._walletProvider&&void 0!==this._walletProvider&&null!=o&&(this.signatureService=new Is(this._walletProvider,n),this.tokenKeyService=new xs(n))}async submitTransaction(e){try{this.validateBundleData(e),this.logger.debug("📦 Submitting bundle transaction:",{method:e.method,stringsInstructionsCount:e.stringsInstructions.length,signedDtoKeys:Object.keys(e.signedDto)});const t=this.formatBundleRequest(e);this.logger.debug("🚀 Bundle request payload:",{...t,signedDto:"[REDACTED - Contains signature]"});let n=null;try{n=await Jt(()=>this.http.post(this.bundleEndpoint,t),{errorContext:"Bundle transaction submission failed",logger:this.logger})}catch(e){return{success:!1,error:this.formatErrorMessage(e)}}return null==n?{success:!1,error:"No response from bundle API"}:(this.logger.debug("📥 Bundle API response:",{success:n.success,hasData:zt(n),error:n.error}),this.handleBundleResponse(n))}catch(e){if(e instanceof wt)return{success:!1,error:at(e)};throw e}}validateBundleData(e){if(null==e)throw Ft("bundleData","Bundle data");if(null===e.signedDto||void 0===e.signedDto)throw Ft("signedDto","Signed DTO");if(!jn(e.method))throw Ft("method","Method name");if(!Array.isArray(e.stringsInstructions))throw Rt("stringsInstructions","an array of resource tracking strings");if(0===e.stringsInstructions.length)throw new wt("stringsInstructions cannot be empty","stringsInstructions","EMPTY_ARRAY");const t=["BuyWithNative","BuyExactToken","SellExactToken","SellWithNative"];if(!t.includes(e.method))throw Rt("method",`one of: ${t.join(", ")}`);e.stringsInstructions.forEach((e,t)=>{if(!jn(e))throw new wt(`stringsInstructions[${t}] must be a non-empty string`,`stringsInstructions[${t}]`,"INVALID_INSTRUCTION");if(!Ar(e))throw new wt(`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){const t=Vt(e);if(null!=t&&!1===e.error)return this.logger.debug("✅ Bundle transaction successful:",t),{success:!0,data:t};const n=("string"==typeof e.error?e.error:e.message)??"Bundle transaction failed";return this.logger.debug("❌ Bundle transaction failed:",n),{success:!1,error:n}}formatErrorMessage(e){if("string"==typeof e)return e;if(ct(e)&&null!==e.response&&void 0!==e.response){const t=Vt(e.response);if(null!=t&&"object"==typeof t){const e=t;if(null!==e.error&&void 0!==e.error)return"string"==typeof e.error?e.error:JSON.stringify(e.error);if(null!==e.message&&void 0!==e.message)return"string"==typeof e.message?e.message:JSON.stringify(e.message)}}return at(e)??"Unknown bundle transaction error"}async getBundlerTransactionResult(e){try{if(!jn(e))throw Ft("transactionId","Transaction ID");let t;this.logger.debug("🔍 Checking bundler transaction result:",e);try{t=await Jt(()=>this.http.get(`${this.bundleEndpoint}?id=${e}`),{errorContext:"Failed to get bundler transaction result",logger:this.logger})}catch(e){return{success:!1,error:this.formatErrorMessage(e)}}return null==t?{success:!1,error:"No response from bundler transaction query"}:(this.logger.debug("📊 Bundler transaction result:",t),{success:!0,data:t})}catch(e){if(e instanceof wt)return{success:!1,error:at(e)};throw e}}async cancelTransaction(e){try{if(!jn(e))throw Ft("transactionId","Transaction ID");let t;this.logger.debug("🚫 Cancelling transaction:",e);try{t=await Jt(()=>this.http.delete(`${this.bundleEndpoint}/${e}`),{errorContext:"Failed to cancel transaction",logger:this.logger})}catch(e){return{success:!1,error:this.formatErrorMessage(e)}}return null==t?{success:!1,error:"No response from transaction cancellation"}:(this.logger.debug("🗑️ Transaction cancellation response:",t),{success:!0,data:t})}catch(e){if(e instanceof wt)return{success:!1,error:at(e)};throw e}}async getHealthStatus(){this.logger.debug("🏥 Checking bundle service health");try{const e=await Jt(()=>this.http.get(`${this.bundleEndpoint}/health`),{errorContext:"Bundle service health check failed",logger:this.logger});return null==e?{success:!1,error:"No response from bundle service health check"}:(this.logger.debug("💚 Bundle service health:",e),{success:!0,data:e})}catch(e){return{success:!1,error:this.formatErrorMessage(e)}}}async buyToken(e){this.ensureTradingServicesAvailable();const{tokenName:t,amount:n,type:i,expectedAmount:o}=e,{effectiveSlippageFactor:a,effectiveMaxFee:r,vaultAddress:s}=await this.prepareTradingOperation(t,e.maxAcceptableReverseBondingCurveFee,e.maxAcceptableReverseBondingCurveFeeSlippageFactor,e.slippageToleranceFactor);if("native"===i){if(null==o||""===o)throw new wt("expectedAmount is required for native buy operations. Use getBuyTokenAmount() first to calculate expected tokens.","expectedAmount","EXPECTED_AMOUNT_REQUIRED");const e=Cs(o,a,"buy-native");this.logger.debug("BuyNative slippage applied:",{originalExpectedTokens:o,slippageFactor:a,adjustedMinTokens:e});const t=new Es.BuyNativeDto(s,n,e,{maxAcceptableReverseBondingCurveFee:r});return await this.executeBundleTransaction(t,"BuyWithNative",s)}{if(null==o||""===o)throw new wt("expectedAmount is required for exact buy operations. Use getBuyTokenAmount() first to calculate expected GALA cost.","expectedAmount","EXPECTED_AMOUNT_REQUIRED");const e=Cs(o,a,"buy-exact");this.logger.debug("BuyExact slippage applied:",{originalExpectedGalaCost:o,slippageFactor:a,adjustedMaxGalaCost:e});const t=new Es.BuyExactDto(s,n,e,{maxAcceptableReverseBondingCurveFee:r});return await this.executeBundleTransaction(t,"BuyExactToken",s)}}async sellToken(e){this.ensureTradingServicesAvailable();const{tokenName:t,amount:n,type:i,expectedAmount:o}=e,{effectiveSlippageFactor:a,effectiveMaxFee:r,vaultAddress:s}=await this.prepareTradingOperation(t,e.maxAcceptableReverseBondingCurveFee,e.maxAcceptableReverseBondingCurveFeeSlippageFactor,e.slippageToleranceFactor);if("exact"===i){if(null==o||""===o)throw new wt("expectedAmount is required for exact sell operations. Use getSellTokenAmount() first to calculate expected GALA.","expectedAmount","EXPECTED_AMOUNT_REQUIRED");const e=Cs(o,a,"sell-exact");this.logger.debug("SellExact slippage applied:",{originalExpectedGala:o,slippageFactor:a,adjustedMinGala:e});const t=new Es.SellExactDto(s,n,e,{maxAcceptableReverseBondingCurveFee:r});return await this.executeBundleTransaction(t,"SellExactToken",s)}{if(null==o||""===o)throw new wt("expectedAmount is required for native sell operations. Use getSellTokenAmount() first to calculate tokens to sell.","expectedAmount","EXPECTED_AMOUNT_REQUIRED");const e=Cs(o,a,"sell-native");this.logger.debug("SellNative slippage applied:",{originalExpectedTokensToSell:o,slippageFactor:a,adjustedMaxTokensToSell:e});const t=new Es.SellNativeDto(s,n,e,{maxAcceptableReverseBondingCurveFee:r});return await this.executeBundleTransaction(t,"SellWithNative",s)}}async prepareTradingOperation(e,t,n,i){const{effectiveSlippageFactor:o,effectiveMaxFee:a}=this.calculateEffectiveSlippage(t,n,i),r=await this.resolveTokenNameToVault(e);if(null==r)throw _t(e);return{effectiveSlippageFactor:o,effectiveMaxFee:a,vaultAddress:r}}calculateEffectiveSlippage(e,t,n){const i=n??this.defaultSlippageToleranceFactor,o=t??this.defaultMaxAcceptableReverseBondingCurveFeeSlippageFactor;let a=e??"0";return null!=e&&(a=Cs(e,o,"buy-exact"),this.logger.debug("Reverse bonding curve fee slippage applied:",{baseFee:e,slippageFactor:o,adjustedMaxFee:a})),{effectiveSlippageFactor:i,effectiveFeeSlippageFactor:o,effectiveMaxFee:a}}ensureTradingServicesAvailable(){if(!this.signatureService||!this.tokenKeyService)throw Ut("Trading services not available. BundleService requires walletProvider and userAddress for trading operations.","walletProvider");if(null===this.userAddress||void 0===this.userAddress)throw Ft("userAddress","User address")}async executeBundleTransaction(e,t,n){this.ensureTradingServicesAvailable();try{e.uniqueKey=`galaswap - operation - ${i.v4()}-${Date.now()}-${String(this.userAddress)}`;const o=await this.signatureService.signDTO(e,t,this.userAddress),a=this.tokenKeyService.generateStringsInstructions(n),r={stringsInstructions:a,method:t,signedDto:o};this.logger.debug("📦 Bundle transaction data:",{method:t,stringsInstructions:a,dtoKeys:Object.keys(o)});const s=await this.submitTransaction(r);if(s.success){const e=Vt(s);if(null!=e)return this.logger.debug("✅ Bundle transaction submitted:",e),{success:!0,data:{transactionId:e,message:"Transaction submitted successfully. Monitor WebSocket for completion."}}}const c="string"==typeof s.error?s.error:"Bundle transaction failed";throw new At(c,void 0,"BUNDLE_FAILED")}catch(e){throw this.logger.error("❌ Bundle transaction error:",e),e}}async resolveTokenNameToVault(e){return await this.tokenResolver.resolveTokenToVault(e)}}class Fs extends ri{constructor(e,t=!1){super(e,t)}extractData(e){if(e.error||void 0===e.data){const t=e.message??"Client config operation failed";throw new Error(t)}return e.data}async getClientFlags(){this.logger.debug("Fetching client feature flags");const e=await this.http.get(tt),t=this.extractData(e);return this.logger.debug("Fetched client feature flags",{flagCount:Object.keys(t.flags).length}),{flags:t.flags}}}var Rs,Bs,Us,Os;!function(e){e.CHAT_MESSAGE="CHAT_MESSAGE",e.COMMENT="COMMENT",e.STREAM="STREAM",e.RECORDING="RECORDING"}(Rs||(Rs={})),function(e){e.INAPPROPRIATE_CONTENT="INAPPROPRIATE_CONTENT",e.SPAM="SPAM",e.HARASSMENT="HARASSMENT",e.SCAM="SCAM",e.OTHER="OTHER"}(Bs||(Bs={})),function(e){e.PENDING="PENDING",e.DISMISSED="DISMISSED",e.ACTIONED="ACTIONED"}(Us||(Us={})),function(e){e.DELETE_CONTENT="DELETE_CONTENT",e.BAN_USER="BAN_USER",e.DELETE_AND_BAN="DELETE_AND_BAN"}(Os||(Os={})),Bs.INAPPROPRIATE_CONTENT,Bs.SPAM,Bs.HARASSMENT,Bs.SCAM,Bs.OTHER,Us.PENDING,Us.DISMISSED,Us.ACTIONED,Os.DELETE_CONTENT,Os.BAN_USER,Os.DELETE_AND_BAN,Rs.CHAT_MESSAGE,Rs.COMMENT,Rs.STREAM,Rs.RECORDING;const Ls=Xn(Rs),Ms=Xn(Bs),$s=Xn(Us),Ks=Xn(Os),qs={TOKEN_NAME:gn,CONTENT_ID:vn.CONTENT_ID,DETAILS:vn.FLAG_DETAILS,PAGINATION:{MAX_LIMIT:pn}};function Gs(e,t="tokenName"){Zn(e,t,qs.TOKEN_NAME)}class Ws extends Yr{constructor(e,t,n,i=!1,o){super(e,t,n,i,o)}async createFlag(e){!function(e){if(Gs(e.tokenName),void 0===e.contentType)throw Ft("contentType","Content type");if(!Ls(e.contentType))throw Rt("contentType",`one of: ${Object.values(Rs).join(", ")}`,"Content type");if(!jn(e.contentId))throw Ft("contentId","Content ID");if(e.contentId.length>qs.CONTENT_ID.MAX_LENGTH)throw Kt("contentId",qs.CONTENT_ID.MAX_LENGTH,e.contentId.length,"Content ID");if(!jn(e.reportedUserAddress))throw Ft("reportedUserAddress","Reported user address");if(vi(e.reportedUserAddress,"reportedUserAddress"),!(e.contentType!==Rs.STREAM&&e.contentType!==Rs.RECORDING||void 0!==e.reason&&null!==e.reason))throw Ft("reason","Reason");if(void 0!==e.reason&&!Ms(e.reason))throw Rt("reason",`one of: ${Object.values(Bs).join(", ")}`,"Reason");if(void 0!==e.details&&!jn(e.details))throw qt("details","a non-empty string",typeof e.details);if(void 0!==e.details&&e.details.length>qs.DETAILS.MAX_LENGTH)throw Kt("details",qs.DETAILS.MAX_LENGTH,e.details.length)}(e);const t={tokenName:e.tokenName,contentType:e.contentType,contentId:e.contentId,reportedUserAddress:e.reportedUserAddress};null!=e.reason&&(t.reason=e.reason),null!=e.details&&(t.details=e.details);const n=await this.http.post(ie.CREATE,t,this.getMultiAuthHeaders());return{flag:this.extractData(n).flag}}async listFlags(e){!function(e){if(Gs(e.tokenName),void 0!==e.contentType&&!Ls(e.contentType))throw Rt("contentType",`one of: ${Object.values(Rs).join(", ")}`,"Content type");if(void 0!==e.status&&!$s(e.status))throw Rt("status",`one of: ${Object.values(Us).join(", ")}`);if(void 0!==e.reason&&!Ms(e.reason))throw Rt("reason",`one of: ${Object.values(Bs).join(", ")}`);if(void 0!==e.reporterAddress&&!jn(e.reporterAddress))throw qt("reporterAddress","a non-empty string",typeof e.reporterAddress);if(void 0!==e.reporterAddress&&vi(e.reporterAddress,"reporterAddress"),void 0!==e.reportedUserAddress&&!jn(e.reportedUserAddress))throw qt("reportedUserAddress","a non-empty string",typeof e.reportedUserAddress);void 0!==e.reportedUserAddress&&vi(e.reportedUserAddress,"reportedUserAddress"),Qn(0,void 0,qs.PAGINATION.MAX_LIMIT,e.pageSize)}(e);const t=Qr(e,pn);Xr(t,e,["tokenName","contentType","status","reason","reporterAddress","reportedUserAddress"]);const n=await this.http.get(ie.LIST,t,this.getMultiAuthHeaders()),i=this.extractData(n);return{items:i.flags,pageInfo:i.pageInfo}}async listGlobalFlags(e={}){!function(e){if(void 0!==e.tokenName&&Gs(e.tokenName),void 0!==e.contentType&&!Ls(e.contentType))throw Rt("contentType",`one of: ${Object.values(Rs).join(", ")}`,"Content type");if(void 0!==e.status&&!$s(e.status))throw Rt("status",`one of: ${Object.values(Us).join(", ")}`);if(void 0!==e.reason&&!Ms(e.reason))throw Rt("reason",`one of: ${Object.values(Bs).join(", ")}`);if(void 0!==e.reporterAddress&&!jn(e.reporterAddress))throw qt("reporterAddress","a non-empty string",typeof e.reporterAddress);if(void 0!==e.reporterAddress&&vi(e.reporterAddress,"reporterAddress"),void 0!==e.reportedUserAddress&&!jn(e.reportedUserAddress))throw qt("reportedUserAddress","a non-empty string",typeof e.reportedUserAddress);void 0!==e.reportedUserAddress&&vi(e.reportedUserAddress,"reportedUserAddress"),Qn(0,void 0,qs.PAGINATION.MAX_LIMIT,e.pageSize)}(e);const t=Qr(e,pn);Xr(t,e,["tokenName","contentType","status","reason","reporterAddress","reportedUserAddress"]);const n=await this.http.get(ie.LIST,t,this.getMultiAuthHeaders()),i=this.extractData(n);return{items:i.flags,pageInfo:i.pageInfo}}async dismissFlag(e){!function(e){if(void 0===e.flagId||null===e.flagId)throw Ft("flagId","Flag ID");try{Gn(e.flagId,"flagId")}catch{throw Rt("flagId","a positive integer","Flag ID")}}(e);const t=ie.DISMISS.replace(":id",e.flagId.toString()),n=await this.http.post(t,{},this.getMultiAuthHeaders());return{flag:this.extractData(n).flag}}async actionFlag(e){!function(e){if(void 0===e.flagId||null===e.flagId)throw Ft("flagId","Flag ID");try{Gn(e.flagId,"flagId")}catch{throw Rt("flagId","a positive integer","Flag ID")}if(void 0===e.action)throw Ft("action","Action");if(!Ks(e.action))throw Rt("action",`one of: ${Object.values(Os).join(", ")}`,"Action")}(e);const t=ie.ACTION.replace(":id",e.flagId.toString()),n=await this.http.post(t,{action:e.action},this.getMultiAuthHeaders());return{flag:this.extractData(n).flag}}}const Hs=["heart","fire","laugh","wow","thumbs_up"];class zs extends Yr{constructor(e,t,n,i=!1,o){super(e,t,n,i,o)}validateReactionTokenName(e){this.validateTokenName(e,gn)}validateMessageId(e){if(!jn(e))throw Ft("messageId","Message ID");if(qn(e,wn.CONTENT_REACTION.MAX_LENGTH,"messageId"),!wn.CONTENT_REACTION.PATTERN.test(e))throw Rt("messageId","msg-{timestamp}-{uuid} or chat-{timestamp}-{uuid} format","Message ID")}validateReactionType(e){if(!jn(e))throw Ft("reactionType","Reaction type");if(!Hs.includes(e))throw Rt("reactionType",`one of: ${Hs.join(", ")}`,"Reaction type")}async addContentReaction(e){void 0!==e.tokenName&&this.validateReactionTokenName(e.tokenName),this.validateMessageId(e.messageId),this.validateReactionType(e.reactionType);const t=Pe,n={messageId:e.messageId,reactionType:e.reactionType};void 0!==e.tokenName&&(n.tokenName=en(e.tokenName));const i=await this.http.post(t,n,this.getMultiAuthHeaders()),o=this.extractData(i),{created:a,...r}=o;return{data:r,created:a}}async removeContentReaction(e){void 0!==e.tokenName&&this.validateReactionTokenName(e.tokenName),this.validateMessageId(e.messageId),this.validateReactionType(e.reactionType);const t=`${xe}?${new URLSearchParams({messageId:e.messageId,reactionType:e.reactionType}).toString()}`;return await this.http.delete(t,{},this.getMultiAuthHeaders()),{success:!0}}async addReactionToChatMessage(e){return this.addContentReaction(e)}async removeReactionFromChatMessage(e){return this.removeContentReaction(e)}async addReactionToComment(e){return this.addContentReaction(e)}async removeReactionFromComment(e){return this.removeContentReaction(e)}}class js extends ri{constructor(e,t,n=void 0,i=5,o=!1){super(e,o),this.pricingConcurrency=5,this.dexBackendBaseUrl=t,this.gswapService=n,this.pricingConcurrency=i}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 i=e[n];t.push({poolIndex:n,token:i.token0,isToken0:!0,task:this.gswapService.getSwapQuoteExactInput({fromToken:i.token0,toToken:"GUSDC",amount:"1"}).then(e=>e.estimatedOutput).catch(e=>{this.logger.debug(`Failed to price ${i.token0}`,{error:at(e)})})}),t.push({poolIndex:n,token:i.token1,isToken0:!1,task:this.gswapService.getSwapQuoteExactInput({fromToken:i.token1,toToken:"GUSDC",amount:"1"}).then(e=>e.estimatedOutput).catch(e=>{this.logger.debug(`Failed to price ${i.token1}`,{error:at(e)})})})}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 i=t.slice(e,e+this.pricingConcurrency),o=await Promise.allSettled(i.map(e=>e.task));for(let e=0;e<i.length;e++){const t=i[e],a=o[e],r=n.get(t.poolIndex)??{};"fulfilled"===a.status&&null!=a.value&&(t.isToken0?r.token0Price=a.value:r.token1Price=a.value),n.set(t.poolIndex,r)}}const i=e.map((e,t)=>{const i=n.get(t)??{},o={...e};return void 0!==i.token0Price&&(o.token0Price=i.token0Price),void 0!==i.token1Price&&(o.token1Price=i.token1Price),o}),o=i.filter(e=>null!=e.token0Price&&null!=e.token1Price).length;return this.logger.debug("Pricing enrichment complete",{total:e.length,successful:o,failed:e.length-o}),i}async fetchDexPools(e={}){const{search:t,sortBy:n="tvl",sortOrder:i="desc",page:o=1,limit:a=zr.DEFAULT_LIMIT,withPrices:r=!1}=e;this.logger.debug("Fetching DEX pools",{search:t,sortBy:n,sortOrder:i,page:o,limit:a,withPrices:r});const s=Math.min(a,20),c=Math.max(o,1),l=new URLSearchParams({page:String(c),limit:String(s),sortBy:n,sortOrder:i});null!=t&&l.append("search",t);const d=`${this.dexBackendBaseUrl}/explore/pools?${l}`,u=await Xt(()=>this.http.get(d),{errorContext:"Failed to fetch DEX pools",logger:this.logger});let h=u.pools;const g=u.count??0,m=c*s<g,p=c>1;r&&(h=await this.enrichPoolsWithPricing(h)),this.logger.debug("DEX pools fetched successfully",{poolCount:h.length,totalCount:g,hasNextPage:m,withPrices:r});return{items:h,pageInfo:{hasNextPage:m,hasPrevPage:p,totalCount:g}}}async fetchAllDexPools(e={}){this.logger.debug("Fetching all DEX pools (auto-paginated)",e);const t=[];let n=1;for(;;){const i=await this.fetchDexPools({...e,page:n,limit:20});if(t.push(...i.items),!i.pageInfo.hasNextPage)break;n+=1}const i=t.length;return this.logger.debug("All DEX pools fetched",{totalPoolsFetched:t.length,totalCount:i,withPrices:e.withPrices}),{items:t,pageInfo:{hasNextPage:!1,totalCount:i}}}}function Vs(e){try{if(!jn(e))throw new Error("Token must be a non-empty string");return br(e)}catch(t){throw new wt(`Invalid pipe-delimited token: "${e}". Expected format: "collection|category|type|additionalKey". Error: ${at(t)}`,"pipeDelimitedToken","INVALID_PIPE_DELIMITED_TOKEN_FORMAT")}}class Qs extends ri{constructor(e,t,n=!1,i=3e4){super(e,n),this.compositePoolFetchConcurrency=5,this.galaChainBaseUrl=t}validateFetchCompositePoolDataInput(e,t,n){if(!jn(e))throw new Dt("token0 must be a non-empty string",{token0:e});if(!jn(t))throw new Dt("token1 must be a non-empty string",{token1:t});try{br(e),br(t)}catch(n){throw new Dt(`Token format must be: collection|category|type|additionalKey (4 pipe-separated parts). ${at(n)}`,{token0:e,token1:t})}try{Gn(n,"fee")}catch{throw new Dt(`fee must be a positive integer (got ${n})`,{fee:n})}const i=[500,3e3,1e4];if(!i.includes(n))throw new Dt(`fee must be one of: ${i.join(", ")} (got ${n})`,{fee:n})}validateQuoteAmount(e){if(!jn(e))throw new Dt("amount must be a non-empty string",{amount:e});const t=po(e);try{Io(t,"amount")}catch(t){throw new Dt(t.message,{amount:e})}}convertTokenClassKey(e){const t=new l.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:i,gatewayBaseUrl:o}=e;this.logger.debug("Fetching composite pool data",{token0:t,token1:n,fee:i}),this.validateFetchCompositePoolDataInput(t,n,i);try{const e=Vs(t),a=Vs(n),r=this.convertTokenClassKey(e),s=this.convertTokenClassKey(a),c=new d.GetCompositePoolDto(r,s,i),u=`${o??this.galaChainBaseUrl}/api/asset/dexv3-contract/GetCompositePool`,h=await this.http.post(u,c);lt(h,`Pool not found: ${t}/${n} with fee ${i}`);const g=function(e){const t=e.token0Balance,n=e.token1Balance;return{pool:e.pool,tickDataMap:e.tickDataMap,token0Balance:t,token1Balance:n,token0Decimals:e.token0Decimals,token1Decimals:e.token1Decimals,compositePoolDto:e}}(function(e){const t=e.pool,n=new d.Pool(t.token0,t.token1,t.token0ClassKey,t.token1ClassKey,t.fee,po(t.sqrtPrice),t.protocolFees);n.bitmap=t.bitmap,n.grossPoolLiquidity=po(t.grossPoolLiquidity),n.liquidity=po(t.liquidity),n.feeGrowthGlobal0=po(t.feeGrowthGlobal0),n.feeGrowthGlobal1=po(t.feeGrowthGlobal1),n.protocolFeesToken0=po(t.protocolFeesToken0),n.protocolFeesToken1=po(t.protocolFeesToken1),n.tickSpacing=t.tickSpacing,n.maxLiquidityPerTick=po(t.maxLiquidityPerTick);const i={},o=e.tickDataMap;Object.keys(o).forEach(e=>{const t=o[e],n=t.initialised,a=t.liquidityNet,r=t.liquidityGross,s=t.feeGrowthOutside0,c=t.feeGrowthOutside1,l=new d.TickData(t.poolHash,t.tick);l.initialised=n,l.liquidityNet=po(a),l.liquidityGross=po(r),l.feeGrowthOutside0=po(s),l.feeGrowthOutside1=po(c),i[e]=l});const a=e.token0Balance,r=a.quantity,s={...a},c=new l.TokenBalance(s);c.quantity=po(r),Object.keys(s).forEach(e=>{e in c||(c[e]=s[e])});const u=e.token1Balance,h=u.quantity,g={...u},m=new l.TokenBalance(g);return m.quantity=po(h),Object.keys(g).forEach(e=>{e in m||(m[e]=g[e])}),new d.CompositePoolDto(n,i,c,m,e.token0Decimals,e.token1Decimals)}(h.Data),h.Data);return this.logger.debug("Composite pool data fetched successfully",{token0:t,token1:n,fee:i,liquidity:g.pool.liquidity.toString()}),g}catch(e){if(e instanceof Pt)throw e;const o=at(e);if(o.includes("status indicates failure")||o.includes("Pool not found"))throw new Pt(o);throw this.logger.error("Failed to fetch composite pool data",e),new Dt(`Failed to fetch composite pool data: ${o}`,{token0:t,token1:n,fee:i})}}async calculateDexPoolQuoteExactAmountLocal(e){const{compositePoolData:t,fromToken:n,toToken:i,amount:o}=e;if(this.logger.debug("Calculating local DEX quote",{fromToken:n,toToken:i,amount:o}),this.validateQuoteAmount(o),null==t)throw new Dt("compositePoolData is required for local quote calculation",{compositePoolData:t});try{const e=n===t.pool.token0.replace(/\$/g,"|"),a=Vs(n),r=Vs(i),s=this.convertTokenClassKey(a),c=this.convertTokenClassKey(r),[l,u]=n<i?[s,c]:[c,s],h=new d.QuoteExactAmountDto(l,u,t.pool.fee,po(o),e,t.compositePoolDto),g=await d.quoteExactAmount(null,h);return this.logger.debug("Local quote calculated",{amount0:g.amount0,amount1:g.amount1}),{amount0:g.amount0.toString(),amount1:g.amount1.toString(),currentSqrtPrice:g.currentSqrtPrice.toString(),newSqrtPrice:g.newSqrtPrice.toString()}}catch(e){throw this.logger.error("Local quote calculation failed",e),new Dt(`Local quote calculation failed: ${at(e)}`,{fromToken:n,toToken:i,amount:o})}}async calculateDexPoolQuoteExactAmountExternal(e){const{compositePoolData:t,fromToken:n,toToken:i,amount:o}=e;if(this.logger.debug("Calculating external DEX quote",{fromToken:n,toToken:i,amount:o}),this.validateQuoteAmount(o),null==t)throw new Dt("compositePoolData is required for external quote calculation (token format info)",{compositePoolData:t});try{const e=n===t.pool.token0.replace(/\$/g,"|"),a=Vs(n),r=Vs(i),s=this.convertTokenClassKey(a),c=this.convertTokenClassKey(r),l=new d.QuoteExactAmountDto(s,c,t.pool.fee,po(o),e,void 0),u=`${this.galaChainBaseUrl}/api/asset/dexv3-contract/QuoteExactAmount`,h=await this.http.post(u,l);lt(h,"External quote failed");const g=h.Data,m=g.amount0,p=g.amount1;return this.logger.debug("External quote calculated",{amount0:m,amount1:p}),{amount0:g.amount0.toString(),amount1:g.amount1.toString(),currentSqrtPrice:g.currentSqrtPrice.toString(),newSqrtPrice:g.newSqrtPrice.toString()}}catch(e){throw this.logger.error("External quote calculation failed",e),new Dt(`External quote calculation failed: ${at(e)}`,{fromToken:n,toToken:i,amount:o})}}async calculateDexPoolQuoteExactAmount(e,t="local"){return"external"===t?this.calculateDexPoolQuoteExactAmountExternal(e):this.calculateDexPoolQuoteExactAmountLocal(e)}}class Xs extends si{constructor(e,t,n,i=!1){super(i),this.dexBackendHttp=e,this.cache=t,this.galaChainService=n}async fetchTokenPrice(e){const{tokenId:t}=e,{hasB:n}=Yn(e,"tokenName","tokenId",{description:"token identifier"});if(!0===n&&null!=t)return this.logger.debug(`Fetching spot price by tokenId: ${String(t)}`),this._fetchDexTokenSpotPrice(t);throw new wt("tokenName parameter requires LaunchpadSDK routing - call LaunchpadSDK.fetchTokenPrice({tokenName}) instead","tokenName","INVALID_PARAMS")}async _fetchDexTokenSpotPrice(e){if(null==e)throw Ft("tokenId","Token ID");try{const t=ls(e),n=ca(t),i=Ds(n);if(this.logger.debug(`Fetching DEX spot price for token: ${i}`),!this.dexBackendHttp)throw Bt("DEX Backend API client not configured");const o=Vt(await this.dexBackendHttp.request({method:"GET",url:"/v1/trade/price",params:{token:i}}));if(null==o||"string"!=typeof o)throw new wt("Invalid price response: data must be a string, got "+typeof o,"data","INVALID_RESPONSE");const a=function(e,t){if(sn(e)||""===e)throw Ft(t);const n="number"==typeof e?e:parseFloat(String(e));if(isNaN(n))throw Gt(t,e);if(!isFinite(n))throw Gt(t,e);return n}(o,"price"),r=n;let s;try{if(null!==this.cache&&void 0!==this.cache){const e=this.cache.getByTokenId(r);if(null!=e?.symbol)return s=e.symbol,this.logger.debug(`DEX spot price for ${s} (cached): $${a}`),{symbol:s,price:a}}this.logger.debug(`Symbol cache miss for ${r}, fetching from API`);s=(await this.fetchTokenDetails(e)).symbol,this.cache&&(this.cache.setByTokenId(r,{symbol:s}),this.logger.debug(`Cached symbol for ${r}: ${s}`)),this.logger.debug(`DEX spot price for ${s}: $${a}`)}catch(e){this.logger.debug(`Could not fetch token details for symbol, falling back to token format parsing: ${ot(e)?e.message:String(e)}`),s=nn("Token"===t.collection?t.type:t.collection),this.logger.debug(`DEX spot price for ${s} (fallback): $${a}`)}return{symbol:s,price:a}}catch(e){if(e instanceof wt)throw e;throw Bt(`Failed to fetch DEX spot price: ${at(e)}`)}}async fetchLaunchpadTokenSpotPrice(e,t,n){if(!jn(e))throw new wt(ht,"tokenName",mt);try{if(null!=n)try{this.logger.debug(`Checking graduation status for token: ${e}`);const t=await n(e);if(!0===t?.isGraduated){this.logger.debug(`Token ${e} is graduated, using DEX spot price`);const n=ca(t.sellingToken);return this._fetchDexTokenSpotPrice(n)}}catch(t){this.logger.debug(`Could not determine graduation status for ${e}, falling back to bonding curve: ${at(t)}`)}this.logger.debug(`Using bonding curve calculation for token: ${e}`);const i=await t({tokenName:e,amount:"1",type:"native"}),o=await this._fetchDexTokenSpotPrice({collection:"GALA",category:"Unit",type:"none",additionalKey:"none"});if(null==o)throw Bt("GALA price not available");const a=xn(i.amount,0)/1e18;if(a<=0)throw new wt(`Invalid token amount calculation: ${a}`,"amount","INVALID_CALCULATION");const r=o.price/a;return{symbol:nn(e),price:r}}catch(t){if(ot(t))throw Bt(`Failed to calculate launchpad token spot price for ${e}: ${at(t)}`);throw Bt(`Failed to calculate launchpad token spot price for ${e}: ${at(t)}`)}}async fetchTokenDetails(e){this.logger.debug("Fetching token details from GalaChain for tokenId:",e);try{if(!this.galaChainService)throw Bt("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 wt||ot(t))&&("NetworkError"===t.name||at(t).includes("Token not found")))throw t;throw Bt(`Failed to fetch token details from GalaChain for ${String(e)}: ${at(t)}`,500)}}async fetchAllDexSeasons(){try{if(!this.dexBackendHttp)throw Bt("DEX Backend API client not configured");const e=await this.dexBackendHttp.request({method:"GET",url:"/leaderboard/seasons"});let t;if(Array.isArray(e))t=e;else{if(null==e||"object"!=typeof e)return this.logger.warn("Seasons endpoint returned invalid data:",e),[];{const n=Vt(e);if(Array.isArray(n))t=n;else if(null!=n&&"object"==typeof n&&Array.isArray(n.seasons))t=n.seasons;else{if(!Array.isArray(e.seasons))return this.logger.warn("Seasons endpoint returned unexpected structure:",e),[];t=e.seasons}}}const n=t.map(e=>{const t=e;return{id:t?.id??0,name:t?.name??"",start:ln(t?.start??null),end:ln(t?.end??null),rulesId:t?.rules_id??0}});return this.logger.debug(`Fetched ${n.length} DEX seasons`),n}catch(e){if(ot(e)&&at(e).includes("not configured"))throw e;if(ct(e)&&404===e.response?.status)return this.logger.warn("Seasons endpoint not available"),[];throw Bt(`Failed to fetch DEX seasons: ${at(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){try{Gn(e,"seasonId")}catch{throw Ft("seasonId","Season ID must be a positive number")}try{if(!this.dexBackendHttp)throw Bt("DEX Backend API client not configured");const t=await this.dexBackendHttp.request({method:"GET",url:"/leaderboard",params:{seasonId:e.toString()}});let n;if(Array.isArray(t))n=t;else{if(null==t||"object"!=typeof t)return this.logger.warn("Leaderboard endpoint returned invalid data:",t),{entries:[],seasonId:e,totalEntries:0};{const i=Vt(t);if(null!=i&&"object"==typeof i&&Array.isArray(i.leaderboard))n=i.leaderboard;else if(Array.isArray(t.leaderboard))n=t.leaderboard;else{if(!Array.isArray(i))return this.logger.warn("Leaderboard endpoint returned unexpected structure:",t),{entries:[],seasonId:e,totalEntries:0};n=i}}}const i=n.map(e=>{const t=e,n=t?.mastery_titles??[];return{wallet:t?.wallet??"",rank:t?.rank??0,totalXp:String(t?.total_xp??0),distributionPercent:String(t?.distribution_percent??0),liquidityXp:String(t?.liquidity_xp??0),tradingXp:String(t?.trading_xp??0),masteryTitles:n.map(e=>{const t=e;return{name:t?.name??"",type:t?.type??"trade",order:t?.order??0}})}});return this.logger.debug(`Fetched leaderboard for season ${e} with ${i.length} entries`),{entries:i,seasonId:e,totalEntries:i.length}}catch(t){if(ot(t)&&at(t).includes("must be a positive number"))throw t;throw Bt(`Failed to fetch DEX leaderboard for season ${e}: ${at(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 Bt("DEX Backend API client not configured");const e=Vt(await this.dexBackendHttp.request({method:"GET",url:"/explore/volume"}));if(!e)throw Bt("No data in DEX volume response",500);const t={volume1d:e.volume1d,volume1dDelta:e.volume1dDelta,volume7d:e.volume7d,volume7dDelta:e.volume7dDelta,volume30d:e.volume30d,volume30dDelta:e.volume30dDelta};return this.logger.debug(`Fetched DEX volume summary: $${t.volume1d.toFixed(2)} (1d)`),t}catch(e){throw Bt(`Failed to fetch DEX volume summary: ${at(e)}`)}}}const Js=20,Ys=3e3,Zs=1e3,ec=!0,tc=!0,nc=!1;class ic{constructor(e,t={}){this.eventBuffer=[],this.flushTimer=null,this.isShuttingDown=!1,this.totalQueued=0,this.totalFlushed=0,this.totalDropped=0,this.handleVisibilityChange=()=>{"hidden"===document.visibilityState&&this.eventBuffer.length>0&&this.flushWithBeacon()},this.handleBeforeUnload=()=>{this.eventBuffer.length>0&&this.flushWithBeacon()},this.httpClient=e,this.logger=new an({debug:t.debug??!1,context:"EventsBatcherService"}),this.config={batchSize:t.batchSize??Js,flushIntervalMs:t.flushIntervalMs??Ys,maxQueueSize:t.maxQueueSize??Zs,flushOnPageHide:t.flushOnPageHide??ec,flushOnPageUnload:t.flushOnPageUnload??tc,debug:t.debug??nc},this.setupPageLifecycleListeners()}track(e,t,n){if(this.isShuttingDown)return void this.logger.warn("EventsBatcher is shutting down, ignoring track call");const i={eventType:e,payload:t,clientTimestamp:(new Date).toISOString(),...void 0!==n?.userAddress&&""!==n.userAddress&&{userAddress:n.userAddress},...void 0!==n?.tokenName&&""!==n.tokenName&&{tokenName:n.tokenName},...void 0!==n?.sessionId&&""!==n.sessionId&&{sessionId:n.sessionId}};this.buffer(i),n?.immediate&&this.scheduleFlush(0)}async flush(){if(0===this.eventBuffer.length)return;this.cancelFlushTimer();const e=this.eventBuffer.splice(0);this.logger.debug(`Flushing ${e.length} events`);try{const t=await this.submitEvents(e);this.totalFlushed+=t.accepted,this.lastFlushTime=Date.now(),this.lastError=void 0,this.logger.debug(`Flush successful, accepted ${t.accepted} events`)}catch(t){throw this.eventBuffer.unshift(...e),this.lastError=t instanceof Error?t.message:String(t),this.logger.error(`Flush failed: ${this.lastError}`),t}}getStats(){const e={bufferedCount:this.eventBuffer.length,totalQueued:this.totalQueued,totalFlushed:this.totalFlushed,totalDropped:this.totalDropped};return void 0!==this.lastFlushTime&&(e.lastFlushTime=this.lastFlushTime),void 0!==this.lastError&&(e.lastError=this.lastError),e}configure(e){void 0!==e.batchSize&&(this.config.batchSize=e.batchSize),void 0!==e.flushIntervalMs&&(this.config.flushIntervalMs=e.flushIntervalMs),void 0!==e.maxQueueSize&&(this.config.maxQueueSize=e.maxQueueSize),void 0!==e.flushOnPageHide&&(this.config.flushOnPageHide=e.flushOnPageHide),void 0!==e.flushOnPageUnload&&(this.config.flushOnPageUnload=e.flushOnPageUnload)}async shutdown(e=5e3){if(this.isShuttingDown=!0,this.removePageLifecycleListeners(),0!==this.eventBuffer.length)try{await Promise.race([this.flush(),new Promise((t,n)=>setTimeout(()=>n(new Error("Flush timeout during shutdown")),e))])}catch(e){this.logger.error(`Shutdown flush failed: ${e instanceof Error?e.message:String(e)}`)}}buffer(e){this.eventBuffer.length>=this.config.maxQueueSize&&(this.eventBuffer.shift(),this.totalDropped++,this.logger.warn("Event buffer full, dropped oldest event")),this.eventBuffer.push(e),this.totalQueued++,this.eventBuffer.length>=this.config.batchSize?this.scheduleFlush(0):this.scheduleFlush(this.config.flushIntervalMs)}scheduleFlush(e){null===this.flushTimer&&(this.flushTimer=setTimeout(()=>{this.flushTimer=null,this.flush().catch(e=>{this.logger.error(`Scheduled flush failed: ${e instanceof Error?e.message:String(e)}`)})},e))}cancelFlushTimer(){null!==this.flushTimer&&(clearTimeout(this.flushTimer),this.flushTimer=null)}async submitEvents(e){return await this.httpClient.post("/v1/events",{events:e})}setupPageLifecycleListeners(){"undefined"!=typeof document&&(this.config.flushOnPageHide&&document.addEventListener("visibilitychange",this.handleVisibilityChange),this.config.flushOnPageUnload&&window.addEventListener("beforeunload",this.handleBeforeUnload))}removePageLifecycleListeners(){"undefined"!=typeof document&&(this.config.flushOnPageHide&&document.removeEventListener("visibilitychange",this.handleVisibilityChange),this.config.flushOnPageUnload&&window.removeEventListener("beforeunload",this.handleBeforeUnload))}flushWithBeacon(){if("undefined"==typeof navigator||!navigator.sendBeacon)return;const e=this.eventBuffer.splice(0),t=JSON.stringify({events:e});try{const n=this.getBaseUrl();navigator.sendBeacon(`${n}/v1/events`,t)?(this.totalFlushed+=e.length,this.lastFlushTime=Date.now()):(this.eventBuffer.unshift(...e),this.logger.warn("sendBeacon returned false, events re-buffered"))}catch(t){this.eventBuffer.unshift(...e),this.logger.error(`sendBeacon failed: ${t instanceof Error?t.message:String(t)}`)}}getBaseUrl(){const e=this.httpClient.config;return e?.baseUrl??""}}function oc(e){return 0===(e?.length??0)?"0":e.reduce((e,t)=>o(e).plus(t.quantity).toString(),"0")}function ac(e,t){if(0===(e?.length??0))return[];if(t)return e;const n=Date.now();return e.filter(e=>0===e.expires||e.expires>n)}class rc extends ri{constructor(e,t=!1){super(e,t)}getChannelForCollection(e){return"MUSIC"===(Ar(e)?e.slice(1).toUpperCase():e.toUpperCase())?"music":"asset"}async fetchGalaBalance(e){return this.fetchTokenBalance(e)}async fetchTokenBalance(e,t=!1){try{const n=`/api/${this.getChannelForCollection(e.collection)}/token-contract/FetchBalances`,i=await this.http.post(n,e);if(null==i)return null;try{lt(i,"Fetch balances")}catch{return null}if(0===(i.Data?.length??0))return null;const a=i.Data.find(t=>t.collection===e.collection&&t.category===e.category&&t.additionalKey===e.additionalKey&&t.type===e.type);if(!a||"0"===a.quantity)return null;const r=ca(a),s={quantity:a.quantity,collection:a.collection,category:a.category,tokenId:r};if((a.inUseHolds?.length??0)>0){const e=ac(a.inUseHolds??[],t);e.length>0&&(s.inUseHolds=e,s.inUseQuantity=oc(e))}if((a.lockedHolds?.length??0)>0){const e=ac(a.lockedHolds??[],t);e.length>0&&(s.lockedHolds=e,s.lockedQuantity=oc(e))}return void 0===s.lockedQuantity&&void 0===s.inUseQuantity||(s.availableQuantity=function(e,t="0",n="0"){return o(e).minus(t).minus(n).toString()}(s.quantity,s.lockedQuantity,s.inUseQuantity)),s}catch(e){throw Bt(`Failed to fetch token balance from GalaChain: ${at(e)}`,void 0,ot(e)?e:void 0)}}}function sc(){return`galaconnect-operation-${Date.now()}_${Math.random().toString(36).substring(2,8)}`}class cc extends n.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),void 0!==e.signedPayload&&(this.signature=e.signedPayload.signature,this.domain=e.signedPayload.domain,this.types=e.signedPayload.types,void 0!==e.signedPayload.prefix&&(this.prefix=e.signedPayload.prefix))}static fromTokenClassKey(e,t,n,i,o){let a;if("string"==typeof i){a={...Ps(i),instance:"0"}}else a={collection:i.collection,category:i.category,type:i.type,additionalKey:i.additionalKey,instance:"0"};return new cc({lockAuthority:t??e,tokenInstances:[{owner:e,quantity:n,tokenInstanceKey:a}],...void 0!==o?.expires&&{expires:o.expires},...void 0!==o?.name&&{name:o.name},uniqueKey:o?.uniqueKey??sc()})}static forGALA(e,t,n,i){return new cc({lockAuthority:t??e,tokenInstances:[{owner:e,quantity:n,tokenInstanceKey:{collection:"GALA",category:"Unit",type:"none",additionalKey:"none",instance:"0"}}],...void 0!==i?.expires&&{expires:i.expires},...void 0!==i?.name&&{name:i.name},uniqueKey:i?.uniqueKey??sc()})}getTokenClassKey(){if(0===this.tokenInstances.length)return;return la(this.tokenInstances[0].tokenInstanceKey)}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 lc extends n.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,null!=e.signedPayload.prefix&&(this.prefix=e.signedPayload.prefix))}static fromTokenClassKey(e,t,n,i){let o;if("string"==typeof n){o={...Ps(n),instance:"0"}}else o={collection:n.collection,category:n.category,type:n.type,additionalKey:n.additionalKey,instance:"0"};return new lc({tokenInstances:[{owner:e,quantity:t,tokenInstanceKey:o}],...void 0!==i?.name&&{name:i.name},uniqueKey:i?.uniqueKey??sc()})}static forGALA(e,t,n){return new lc({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??sc()})}getTokenClassKey(){if(0===this.tokenInstances.length)return;return la(this.tokenInstances[0].tokenInstanceKey)}toSigningPayload(){return{tokenInstances:this.tokenInstances,...void 0!==this.name&&{name:this.name},uniqueKey:this.uniqueKey}}}function dc(e){if(null==e||"object"!=typeof e)return!1;const t=e;return jn(t.amount)&&(void 0!==t.tokenId||jn(t.tokenName))&&(void 0===t.lockAuthority||"string"==typeof t.lockAuthority)&&(void 0===t.expires||"number"==typeof t.expires)&&(void 0===t.name||"string"==typeof t.name)}function uc(e){if(null==e||"object"!=typeof e)return!1;const t=e;return jn(t.amount)&&(void 0!==t.tokenId||jn(t.tokenName))&&(void 0===t.name||"string"==typeof t.name)}var hc,gc;!function(e){e.TOKEN_NOT_FOUND="TOKEN_NOT_FOUND",e.INVALID_AMOUNT="INVALID_AMOUNT",e.INSUFFICIENT_BALANCE="INSUFFICIENT_BALANCE",e.SIGNATURE_FAILED="SIGNATURE_FAILED",e.NETWORK_ERROR="NETWORK_ERROR",e.WALLET_REQUIRED="WALLET_REQUIRED",e.VALIDATION_ERROR="VALIDATION_ERROR",e.LOCK_NOT_FOUND="LOCK_NOT_FOUND",e.LOCK_EXPIRED="LOCK_EXPIRED",e.INSUFFICIENT_LOCKED_BALANCE="INSUFFICIENT_LOCKED_BALANCE",e.NOT_LOCK_AUTHORITY="NOT_LOCK_AUTHORITY",e.LOCK_NAME_MISMATCH="LOCK_NAME_MISMATCH"}(hc||(hc={}));class mc extends Error{constructor(e,t,n){super(e),this.type=t,this.details=n,this.name="LockError"}}!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"}(gc||(gc={}));class pc extends Error{constructor(e,t,n){super(e),this.type=t,this.details=n,this.name="TransferError"}}class fc{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"}]},i={lockAuthority:e.lockAuthority,tokenInstances:e.tokenInstances,uniqueKey:e.uniqueKey};void 0!==e.expires&&(i.expires=e.expires),void 0!==e.name&&(i.name=e.name);return{signature:await this.wallet.signTypedData(t,n,i),domain:t,types:n,signerPublicKey:this.wallet.signingKey.publicKey}}async signBurnTokens(e){const t={name:"GalaChain",chainId:1},n={BurnTokens:[{name:"tokenInstances",type:"TokenInstanceQuantity[]"},{name:"uniqueKey",type:"string"}],TokenInstanceQuantity:[{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"}]},i={tokenInstances:e.tokenInstances,uniqueKey:e.uniqueKey};return{signature:await this.wallet.signTypedData(t,n,i),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"}]},i={tokenInstances:e.tokenInstances,uniqueKey:e.uniqueKey};void 0!==e.name&&(i.name=e.name);return{signature:await this.wallet.signTypedData(t,n,i),domain:t,types:n,signerPublicKey:this.wallet.signingKey.publicKey}}static toGalaChainAddress(e){return(new gi).toBackendFormat(e)}static fromGalaChainAddress(e){try{return(new gi).toEthereumFormat(e)}catch{try{return yi(e)}catch{return e}}}static createGALATokenInstance(){return{collection:"GALA",category:"Unit",type:"none",additionalKey:"none",instance:"0"}}static createTokenInstanceFromClassKey(e){return{...Ps(e),instance:"0"}}}const yc=100,kc=100;class vc extends ri{constructor(e,t,n,i=!1){super(e,i),this.wallet=t,this.tokenResolver=n,this.signatureHelper=t?new fc(t):void 0}async lockTokens(e){if(this.validateLockTokensData(e),!this.wallet||!this.signatureHelper)throw new mc("Wallet required for token lock operations",hc.WALLET_REQUIRED);return ea(async()=>{const t=fi(this.wallet.address),n=[],i=[];let o=t;const a=e.tokens.find(e=>void 0!==e.lockAuthority);void 0!==a?.lockAuthority&&(o=fi(a.lockAuthority));const r=e.tokens.find(e=>void 0!==e.expires),s=e.tokens.find(e=>void 0!==e.name);for(const a of e.tokens){let e;if(void 0!==a.tokenId)e=ls(a.tokenId),this.logger.debug("[DEBUG] Using provided tokenId:",a.tokenId);else{if(void 0===a.tokenName)throw new mc("Must provide either tokenId or tokenName for token identification",hc.TOKEN_NOT_FOUND);e=await this.resolveTokenInstance(a.tokenName)}n.push({owner:t,quantity:a.amount,tokenInstanceKey:e}),i.push({tokenClassKey:{collection:e.collection,category:e.category,type:e.type,additionalKey:e.additionalKey},quantity:a.amount,lockAuthority:void 0!==a.lockAuthority?fi(a.lockAuthority):o})}const c=new cc({lockAuthority:o,tokenInstances:n,...void 0!==r?.expires&&{expires:r.expires},...void 0!==s?.name&&{name:s.name},uniqueKey:e.uniqueKey??sc()}),l=await this.signatureHelper.signLockToken(c.toSigningPayload()),d=new cc({...c.toSigningPayload(),signedPayload:l}),u=await this.http.post("/api/asset/token-contract/LockTokens",d);try{lt(u,"Token lock failed")}catch(e){const t=dt(u);throw new mc(`${at(e)}${t}`,hc.NETWORK_ERROR)}return this.extractLockResult(u,i)},"Token lock failed",this.logger,t=>{throw this.handleLockError(t,"Token lock failed",e)})}async unlockTokens(e){if(this.validateUnlockTokensData(e),!this.wallet||!this.signatureHelper)throw new mc("Wallet required for token unlock operations",hc.WALLET_REQUIRED);return ea(async()=>{const t=fi(this.wallet.address),n=[],i=[],o=e.tokens.find(e=>void 0!==e.name);for(const o of e.tokens){let e;if(void 0!==o.tokenId)e=ls(o.tokenId),this.logger.debug("[DEBUG] Using provided tokenId:",o.tokenId);else{if(void 0===o.tokenName)throw new mc("Must provide either tokenId or tokenName for token identification",hc.TOKEN_NOT_FOUND);e=await this.resolveTokenInstance(o.tokenName)}n.push({owner:t,quantity:o.amount,tokenInstanceKey:e}),i.push({tokenClassKey:{collection:e.collection,category:e.category,type:e.type,additionalKey:e.additionalKey},quantity:o.amount})}const a=new lc({tokenInstances:n,...void 0!==o?.name&&{name:o.name},uniqueKey:e.uniqueKey??sc()}),r=await this.signatureHelper.signUnlockToken(a.toSigningPayload()),s=new lc({...a.toSigningPayload(),signedPayload:r}),c=await this.http.post("/api/asset/token-contract/UnlockTokens",s);try{lt(c,"Token unlock failed")}catch(e){const t=dt(c);throw new mc(`${at(e)}${t}`,hc.NETWORK_ERROR)}return this.extractUnlockResult(c,i)},"Token unlock failed",this.logger,t=>{throw this.handleLockError(t,"Token unlock failed",e)})}validateLockTokensData(e){if(!function(e){if(null==e||"object"!=typeof e)return!1;const t=e;return Array.isArray(t.tokens)&&t.tokens.length>0&&t.tokens.every(dc)&&(void 0===t.uniqueKey||"string"==typeof t.uniqueKey)&&(void 0===t.privateKey||"string"==typeof t.privateKey)}(e))throw new mc("Invalid lock data: missing required fields",hc.VALIDATION_ERROR);if(e.tokens.length>yc)throw new mc(`Batch size exceeds maximum limit of ${yc} tokens per lock operation`,hc.VALIDATION_ERROR);for(const t of e.tokens){if(void 0===t.tokenId&&void 0===t.tokenName)throw new mc("Must provide either tokenId or tokenName for token identification",hc.TOKEN_NOT_FOUND);if(void 0!==t.tokenName)try{ei(t.tokenName,"tokenName")}catch{throw new mc("Invalid token name format",hc.TOKEN_NOT_FOUND,{tokenName:t.tokenName})}const e=po(t.amount);try{Eo(e)}catch{throw new mc(Rt("lockAmount","a positive number","Lock amount").message,hc.INVALID_AMOUNT,{amount:t.amount})}if(void 0!==t.lockAuthority&&!ki(t.lockAuthority))throw new mc("Invalid lock authority address format",hc.VALIDATION_ERROR,{lockAuthority:t.lockAuthority});if(void 0!==t.expires)try{Gn(t.expires,"expires")}catch{throw new mc(Rt("expires","a positive integer (epoch milliseconds)","Expires").message,hc.VALIDATION_ERROR)}}}validateUnlockTokensData(e){if(!function(e){if(null==e||"object"!=typeof e)return!1;const t=e;return Array.isArray(t.tokens)&&t.tokens.length>0&&t.tokens.every(uc)&&(void 0===t.uniqueKey||"string"==typeof t.uniqueKey)&&(void 0===t.privateKey||"string"==typeof t.privateKey)}(e))throw new mc("Invalid unlock data: missing required fields",hc.VALIDATION_ERROR);if(e.tokens.length>kc)throw new mc(`Batch size exceeds maximum limit of ${kc} tokens per unlock operation`,hc.VALIDATION_ERROR);for(const t of e.tokens){if(void 0===t.tokenId&&void 0===t.tokenName)throw new mc("Must provide either tokenId or tokenName for token identification",hc.TOKEN_NOT_FOUND);if(void 0!==t.tokenName)try{ei(t.tokenName,"tokenName")}catch{throw new mc("Invalid token name format",hc.TOKEN_NOT_FOUND,{tokenName:t.tokenName})}const e=po(t.amount);try{Eo(e)}catch{throw new mc(Rt("unlockAmount","a positive number","Unlock amount").message,hc.INVALID_AMOUNT,{amount:t.amount})}}}extractLockResult(e,t){let n;if(void 0!==e.Data&&e.Data.length>0){const t=e.Data[0];n=t.transactionId??t.txnId??t.TxnId??t.id??void 0}return{...void 0!==n&&{transactionId:n},locked:t}}extractUnlockResult(e,t){let n;if(void 0!==e.Data&&e.Data.length>0){const t=e.Data[0];n=t.transactionId??t.txnId??t.TxnId??t.id??void 0}return{...void 0!==n&&{transactionId:n},unlocked:t}}handleLockError(e,t,n){if(e instanceof mc)return e;let i=t,o=hc.NETWORK_ERROR;if(ct(e)){const n=e.response?.data;if("object"==typeof n&&null!==n){const e=n;void 0!==e.Message&&"string"==typeof e.Message&&(i=`${t}: ${e.Message}`);const a=String(e.Message??"").toLowerCase();a.includes("insufficient")||a.includes("balance")?o=hc.INSUFFICIENT_BALANCE:a.includes("lock")&&a.includes("not found")?o=hc.LOCK_NOT_FOUND:a.includes("not found")||a.includes("token")?o=hc.TOKEN_NOT_FOUND:a.includes("authority")?o=hc.NOT_LOCK_AUTHORITY:a.includes("expired")&&(o=hc.LOCK_EXPIRED)}}else ot(e)&&(i=`${t}: ${at(e)}`);const a={};return void 0!==n?.tokens?.[0]?.tokenName&&(a.tokenName=n.tokens[0].tokenName),void 0!==n?.tokens?.[0]?.amount&&(a.amount=n.tokens[0].amount),new mc(i,o,Object.keys(a).length>0?a:void 0)}async resolveTokenInstance(e){return ea(async()=>{const t=await this.tokenResolver.resolveTokenToVault(e);if(null!==t){const n=us(t);return this.logger.debug("[DEBUG] Token resolution (launchpad)",{tokenName:e,vaultAddress:t,tokenInstance:{collection:n.collection,category:n.category,type:n.type,instance:n.instance}}),n}const n={collection:tn(e),category:"Unit",type:"none",additionalKey:"none",instance:"0"};return this.logger.debug("[DEBUG] Token resolution (standard format)",{tokenName:e,tokenInstance:{collection:n.collection,category:n.category,type:n.type}}),n},`Failed to resolve token '${e}'`,this.logger,t=>{if(t instanceof pc)throw new mc(at(t),hc.TOKEN_NOT_FOUND);throw new mc(`Failed to resolve token '${e}': ${at(t)}`,hc.TOKEN_NOT_FOUND,{tokenName:e})})}}class wc extends ri{constructor(e,t=!1,n){super(e,t),this.publicAxios=n}async fetchTokenClassFromChain(e){try{const t="string"==typeof e?ls(e):e,n={tokenClasses:[{collection:t.collection,category:t.category,type:t.type,additionalKey:t.additionalKey}]};if(!this.publicAxios)throw Ft("publicAxios","Public Axios instance");const i=(await this.publicAxios.post("/api/asset/token-contract/FetchTokenClasses",n)).data;if(lt(i,"Failed to fetch token class from GalaChain"),null===i.Data||void 0===i.Data||0===i.Data.length)throw Bt(`Token not found on GalaChain: ${ca(t)}`,404);return i.Data[0]}catch(e){if(e instanceof wt)throw e;if(ct(e)&&404===e.response?.status)throw Bt("Token not found on GalaChain",404,ot(e)?e:void 0);if(ot(e)&&"Error"===e.name&&at(e).includes("status indicates failure"))throw Bt(at(e),400,ot(e)?e:void 0);const t=at(e);if(t.includes("Token not found"))throw e;throw Bt(`Failed to fetch token class from GalaChain: ${t}`,void 0,ot(e)?e:void 0)}}async fetchTokenClassesWithSupply(e){try{if(null==e||0===e.length)throw Ft("tokenClasses","Token classes array");const t={tokenClasses:e};if(!this.publicAxios)throw Ft("publicAxios","Public Axios instance");const n=(await this.publicAxios.post("/api/asset/token-contract/FetchTokenClassesWithSupply",t)).data;if(lt(n,"Failed to fetch token classes with supply from GalaChain"),!n.Data||0===n.Data.length)throw Bt("No token supply data found for requested token classes",404);return n.Data}catch(e){if(e instanceof wt)throw e;if(ct(e)&&404===e.response?.status)throw Bt("Token supply data not found on GalaChain",404,ot(e)?e:void 0);if(ot(e)&&"Error"===e.name&&at(e).includes("status indicates failure"))throw Bt(at(e),400,ot(e)?e:void 0);const t=at(e);if(t.includes("Token not found"))throw e;throw Bt(`Failed to fetch token classes with supply from GalaChain: ${t}`,void 0,ot(e)?e:void 0)}}}class bc extends n.ChainCallDTO{constructor(e){super(),this.tokenInstances=e.tokenInstances,this.uniqueKey=e.uniqueKey,e.signedPayload&&(this.signature=e.signedPayload.signature,this.domain=e.signedPayload.domain,this.types=e.signedPayload.types,void 0!==e.signedPayload.prefix&&""!==e.signedPayload.prefix&&(this.prefix=e.signedPayload.prefix))}static fromTokens(e,t){const n=e.map(e=>{let t;if("string"==typeof e.tokenClassKey){t={...Ps(e.tokenClassKey),instance:"0"}}else t={collection:e.tokenClassKey.collection,category:e.tokenClassKey.category,type:e.tokenClassKey.type,additionalKey:e.tokenClassKey.additionalKey,instance:"0"};return{quantity:e.quantity,tokenInstanceKey:t}});return new bc({tokenInstances:n,uniqueKey:t?.uniqueKey??sc()})}static fromTokenClassKey(e,t,n){return bc.fromTokens([{tokenClassKey:t,quantity:e}],n)}static forGALA(e,t){return new bc({tokenInstances:[{quantity:e,tokenInstanceKey:{collection:"GALA",category:"Unit",type:"none",additionalKey:"none",instance:"0"}}],uniqueKey:t?.uniqueKey??sc()})}getTokenClassKey(){if(0===this.tokenInstances.length)return;return la(this.tokenInstances[0].tokenInstanceKey)}toSigningPayload(){return{tokenInstances:this.tokenInstances,uniqueKey:this.uniqueKey}}}class Sc extends n.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,void 0!==e.signedPayload.prefix&&""!==e.signedPayload.prefix&&(this.prefix=e.signedPayload.prefix))}static fromTokenClassKey(e,t,n,i,o){let a;if("string"==typeof i){a={...Ps(i),instance:"0"}}else a={collection:i.collection,category:i.category,type:i.type,additionalKey:i.additionalKey,instance:"0"};return new Sc({from:e,to:t,quantity:n,tokenInstance:a,uniqueKey:o??sc()})}static forGALA(e,t,n,i){return new Sc({from:e,to:t,quantity:n,tokenInstance:{collection:"GALA",category:"Unit",type:"none",additionalKey:"none",instance:"0"},uniqueKey:i??sc()})}getTokenClassKey(){return la(this.tokenInstance)}toSigningPayload(){return{from:this.from,to:this.to,quantity:this.quantity,tokenInstance:this.tokenInstance,uniqueKey:this.uniqueKey}}}function Ac(e){if(null==e||"object"!=typeof e)return!1;const t=e;return jn(t.amount)&&(void 0!==t.tokenId||jn(t.tokenName))}var Tc;!function(e){e.TOKEN_NOT_FOUND="TOKEN_NOT_FOUND",e.INVALID_AMOUNT="INVALID_AMOUNT",e.INSUFFICIENT_BALANCE="INSUFFICIENT_BALANCE",e.SIGNATURE_FAILED="SIGNATURE_FAILED",e.NETWORK_ERROR="NETWORK_ERROR",e.WALLET_REQUIRED="WALLET_REQUIRED",e.VALIDATION_ERROR="VALIDATION_ERROR"}(Tc||(Tc={}));class Ec extends Error{constructor(e,t,n){super(e),this.type=t,this.details=n,this.name="BurnError"}}class Cc{static validateAmount(e){const t=po(e);try{Io(t,"amount")}catch(t){throw new pc(t.message,gc.INVALID_AMOUNT,{amount:e})}}static validateUniqueKey(e){if(!Vn(e)&&jn(e)){if(e.length>An.MAX_LENGTH)throw new wt(`Unique key too long. Maximum length: ${An.MAX_LENGTH}`);if(!An.PATTERN.test(e))throw new pc('Invalid unique key format. Must start with "galaswap-operation-" or "galaconnect-operation-"',gc.INVALID_AMOUNT,{uniqueKey:e})}}}const Ic="gala-transfer-successful",Nc="token-transfer-successful",Dc="token-locked-successfully",Pc="token-unlocked-successfully",xc="transfer-successful-no-id",_c=50;class Fc extends ri{constructor(e,t,n,i=!1){super(e,i),this.wallet=t,this.tokenResolver=n,this.signatureHelper=t?new fc(t):void 0}async transferGala(e){if(this.validateTransferGalaData(e),!this.wallet||!this.signatureHelper)throw new pc("Wallet required for GALA transfer operations",gc.WALLET_REQUIRED);try{const t=fi(e.recipientAddress),n=fi(this.wallet.address),i=Sc.forGALA(n,t,e.amount,e.uniqueKey),o=await this.signatureHelper.signTransferToken(i.toSigningPayload()),a=new Sc({...i.toSigningPayload(),signedPayload:o}),r=await this.http.post("/api/asset/token-contract/TransferToken",a);if(null==r)throw new pc("No response from GalaChain transfer service",gc.NETWORK_ERROR);return this.extractTransactionIdFromResponse(r,"gala")}catch(t){throw this.handleTransferError(t,"GALA transfer failed",e)}}async transferToken(e){if(this.validateTransferTokenData(e),!this.wallet||!this.signatureHelper)throw new pc("Wallet required for token transfer operations",gc.WALLET_REQUIRED);try{const t=fi(e.to),n=fi(this.wallet.address);let i;if(null!==e.tokenId&&void 0!==e.tokenId)i=ls(e.tokenId),this.logger.debug("[DEBUG] Using provided tokenId:",e.tokenId),this.logger.debug("[DEBUG] Normalized Token Instance:",{collection:i.collection,category:i.category,type:i.type,instance:i.instance});else{if(null===e.tokenName||void 0===e.tokenName)throw new pc("Must provide either tokenId or tokenName for token identification",gc.TOKEN_NOT_FOUND);i=await this.resolveTokenInstance(e.tokenName)}const o=new Sc({from:n,to:t,quantity:e.amount,tokenInstance:i,uniqueKey:e.uniqueKey??sc()}),a=await this.signatureHelper.signTransferToken(o.toSigningPayload()),r=new Sc({...o.toSigningPayload(),signedPayload:a}),s=await this.http.post("/api/asset/token-contract/TransferToken",r);if(null==s)throw new pc("No response from GalaChain transfer service",gc.NETWORK_ERROR);return 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}':`,{collection:t.collection,category:t.category,type:t.type}),t}catch(t){if(t instanceof pc)throw t;throw new pc(`Failed to resolve token class key for '${e}': ${at(t)}`,gc.TOKEN_NOT_FOUND,{tokenName:e})}}async burnTokens(e){if(this.validateBurnTokensData(e),!this.wallet||!this.signatureHelper)throw new Ec("Wallet required for token burn operations",Tc.WALLET_REQUIRED);try{const t=[];for(const n of e.tokens){let e;if(null!==n.tokenId&&void 0!==n.tokenId)e=ls(n.tokenId),this.logger.debug("[DEBUG] Using provided tokenId:",n.tokenId);else{if(null===n.tokenName||void 0===n.tokenName)throw new Ec("Must provide either tokenId or tokenName for token identification",Tc.TOKEN_NOT_FOUND);e=await this.resolveTokenInstance(n.tokenName)}t.push({quantity:n.amount,tokenInstanceKey:e})}const n=new bc({tokenInstances:t,uniqueKey:e.uniqueKey??sc()}),i=await this.signatureHelper.signBurnTokens(n.toSigningPayload()),o=new bc({...n.toSigningPayload(),signedPayload:i}),a=await this.http.post("/api/asset/token-contract/BurnTokens",o);if(null==a)throw new Ec("No response from GalaChain burn service",Tc.NETWORK_ERROR);try{lt(a,"Token burn operation")}catch(e){throw new bt(at(e),500)}return this.extractBurnResult(a)}catch(t){throw this.handleBurnError(t,"Token burn failed",e)}}validateTransferGalaData(e){if(!function(e){if(null==e||"object"!=typeof e)return!1;const t=e;return jn(t.recipientAddress)&&jn(t.amount)&&(void 0===t.uniqueKey||"string"==typeof t.uniqueKey)}(e))throw new wt("Invalid GALA transfer data: missing required fields");if(!ki(e.recipientAddress))throw new pc("Invalid recipient address format",gc.INVALID_RECIPIENT,{recipientAddress:e.recipientAddress});Cc.validateAmount(e.amount),Cc.validateUniqueKey(e.uniqueKey)}validateTransferTokenData(e){if(!function(e){if(null==e||"object"!=typeof e)return!1;const t=e;return jn(t.to)&&jn(t.amount)&&(void 0!==t.tokenId||jn(t.tokenName))&&(void 0===t.uniqueKey||"string"==typeof t.uniqueKey)}(e))throw new wt("Invalid token transfer data: missing required fields");if(!ki(e.to))throw new pc("Invalid recipient address format",gc.INVALID_RECIPIENT,{recipientAddress:e.to});if(!(null!==e.tokenId&&void 0!==e.tokenId||null!==e.tokenName&&void 0!==e.tokenName))throw new pc("Must provide either tokenId or tokenName for token identification",gc.TOKEN_NOT_FOUND);if(null!==e.tokenName&&void 0!==e.tokenName)try{ei(e.tokenName,"tokenName")}catch{throw new pc("Invalid token name format",gc.TOKEN_NOT_FOUND,{tokenName:e.tokenName})}Cc.validateAmount(e.amount),Cc.validateUniqueKey(e.uniqueKey)}validateBurnTokensData(e){if(!function(e){if(null==e||"object"!=typeof e)return!1;const t=e;return Array.isArray(t.tokens)&&t.tokens.length>0&&t.tokens.every(Ac)&&(void 0===t.uniqueKey||"string"==typeof t.uniqueKey)&&(void 0===t.privateKey||"string"==typeof t.privateKey)}(e))throw new Ec("Invalid burn data: missing required fields",Tc.VALIDATION_ERROR);if(e.tokens.length>_c)throw new Ec(`Batch size exceeds maximum limit of ${_c} tokens per burn operation`,Tc.VALIDATION_ERROR);for(const t of e.tokens){if(!(null!==t.tokenId&&void 0!==t.tokenId||null!==t.tokenName&&void 0!==t.tokenName))throw new Ec("Must provide either tokenId or tokenName for token identification",Tc.TOKEN_NOT_FOUND);const e=po(t.amount);try{Eo(e)}catch{throw new Ec("Burn amount must be a positive number",Tc.INVALID_AMOUNT,{amount:t.amount})}}}async resolveTokenInstance(e){try{const t=await this.tokenResolver.resolveTokenToVault(e);if(null!=t){const n=us(t);return this.logger.debug(`[DEBUG] Token resolution for '${e}' (launchpad):`,{collection:n.collection,category:n.category,type:n.type,instance:n.instance}),n}const n={collection:tn(e),category:"Unit",type:"none",additionalKey:"none",instance:"0"};return this.logger.debug("[DEBUG] Token resolution (standard format)",{tokenName:e,tokenInstance:{collection:n.collection,category:n.category,type:n.type}}),n}catch(t){if(t instanceof pc)throw t;throw new pc(`Failed to resolve token '${e}': ${at(t)}`,gc.TOKEN_NOT_FOUND,{tokenName:e})}}extractTransactionIdFromResponse(e,t){if(null!=e&&"object"==typeof e){if("Status"in e&&"Data"in e)try{lt(e,"Extract transaction ID");const n=e;if(Array.isArray(n.Data)&&n.Data.length>0)switch(t){case"gala":return Ic;case"token":return Nc;case"lock":return Dc;case"unlock":return Pc}return xc}catch{}if("transactionId"in e&&"string"==typeof e.transactionId&&e.transactionId.length>0)return e.transactionId}throw new pc("Operation succeeded but transaction ID could not be extracted",gc.NETWORK_ERROR)}extractBurnResult(e){const t=[];if(null!==e.Data&&void 0!==e.Data&&Array.isArray(e.Data))for(const n of e.Data)t.push({collection:n.collection??"",category:n.category??"",type:n.type??"",additionalKey:n.additionalKey??"",instance:n.instance??"0",quantity:n.quantity??"0",burnedBy:n.burnedBy??""});let n;if(null!==e.Data&&void 0!==e.Data&&e.Data.length>0){const t=e.Data[0];n=t.transactionId??t.txnId??t.TxnId??t.id??void 0}return{...void 0!==n&&{transactionId:n},burned:t}}handleTransferError(e,t,n){if(e instanceof pc)return e;if(e instanceof wt)return new pc(at(e),gc.INVALID_AMOUNT);if(ct(e)&&e.response){const t=e.response.status,i=e.response.data;if(400===t){const e="string"==typeof i?.message?i.message:void 0;return new pc(e??"Invalid transfer request",gc.INVALID_AMOUNT)}if(403===t)return new pc("Insufficient balance for transfer",gc.INSUFFICIENT_BALANCE);if(404===t){const e={};return"tokenName"in n&&(e.tokenName=n.tokenName),new pc("Token not found",gc.TOKEN_NOT_FOUND,e)}}if("object"==typeof e&&null!==e&&"code"in e&&("ECONNABORTED"===st(e)||"ETIMEDOUT"===st(e)))return new pc("Transfer request timed out",gc.NETWORK_ERROR);const i=""!==at(e)?at(e):t;return new pc(i,gc.NETWORK_ERROR)}handleBurnError(e,t,n){if(e instanceof Ec)return e;let i=t,o=Tc.NETWORK_ERROR;if(ct(e)){const n=e.response?.data;if("object"==typeof n&&"string"==typeof n.Message&&n.Message.length>0){i=`${t}: ${n.Message}`;const e=n.Message.toLowerCase();e.includes("insufficient")||e.includes("balance")?o=Tc.INSUFFICIENT_BALANCE:(e.includes("not found")||e.includes("token"))&&(o=Tc.TOKEN_NOT_FOUND)}}else ot(e)&&(i=`${t}: ${at(e)}`);const a={};return void 0!==n?.tokens?.[0]?.tokenName&&(a.tokenName=n.tokens[0].tokenName),void 0!==n?.tokens?.[0]?.amount&&(a.amount=n.tokens[0].amount),new Ec(i,o,Object.keys(a).length>0?a:void 0)}}class Rc extends ri{constructor(e,t,n,i=!1,o){super(e,i),this.balanceService=new rc(e,i),this.tokenService=new wc(e,i,o),this.lockService=new vc(e,t,n,i),this.transferService=new Fc(e,t,n,i)}async fetchPoolDetails(e){this.validateFetchPoolDetailsData(e);const t=await this.http.post("/api/asset/launchpad-contract/FetchSaleDetails",e);ta(()=>lt(t,"Failed to fetch pool details"),"Failed to fetch pool details",this.logger,e=>{throw Bt(at(e),500)});const n=t.Data.reverseBondingCurveConfiguration,i=n?.minFeePortion??"0",o=n?.maxFeePortion??"0",a=!bo(i)||!bo(o),r=t.Data;return r.reverseBondingCurveMinFeePortion=i,r.reverseBondingCurveMaxFeePortion=o,r.hasReverseBondingCurveFee=a,r.isGraduated="Finished"===t.Data.saleStatus,delete r.reverseBondingCurveConfiguration,t}async fetchLaunchTokenFee(){const e=await this.http.post("/api/asset/launchpad-contract/FetchLaunchpadFeeAmount",{});return ta(()=>lt(e,"Failed to fetch launch token fee"),"Failed to fetch launch token fee",this.logger,e=>{throw Bt(at(e),500)}),e.Data.feeAmount}validateFetchPoolDetailsData(e){if(!ho(e))throw Ft("data","Fetch pool details data");if("string"!=typeof e.vaultAddress||""===e.vaultAddress)throw Ft("vaultAddress","Vault address");if(!e.vaultAddress.startsWith("service|Token$Unit$"))throw new wt("Vault address must be in service format: service|Token$Unit$...","vaultAddress","INVALID_VAULT_ADDRESS")}async fetchGalaBalance(e){return this.balanceService.fetchGalaBalance(e)}async fetchTokenBalance(e,t=!1){return this.balanceService.fetchTokenBalance(e,t)}async fetchTokenClassFromChain(e){return this.tokenService.fetchTokenClassFromChain(e)}async fetchTokenClassesWithSupply(e){return this.tokenService.fetchTokenClassesWithSupply(e)}async transferGala(e){return this.transferService.transferGala(e)}async transferToken(e){return this.transferService.transferToken(e)}async resolveTokenClassKey(e){return this.transferService.resolveTokenClassKey(e)}async lockTokens(e){return this.lockService.lockTokens(e)}async unlockTokens(e){return this.lockService.unlockTokens(e)}async burnTokens(e){return this.transferService.burnTokens(e)}}function Bc(e,t=3e4){return u.create({baseURL:e,timeout:t,headers:{"Content-Type":"application/json"}})}class Uc{static createClient(e,t=6e4){return Bc(e,t)}}class Oc extends Error{constructor(e,t,n){super(`API Error [${e}]: ${t}`),this.status=e,this.message=t,this.details=n,this.name="ApiError"}}function Lc(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 Mc(e){if("object"!=typeof e||null===e)return!1;const t=e;return"string"==typeof t.positionId&&Lc(t.token0ClassKey)&&Lc(t.token1ClassKey)&&"number"==typeof t.fee&&"number"==typeof t.tickLower&&"number"==typeof t.tickUpper&&"string"==typeof t.liquidity&&"string"==typeof t.feeGrowthInside0Last&&"string"==typeof t.feeGrowthInside1Last&&"string"==typeof t.tokensOwed0&&"string"==typeof t.tokensOwed1}class $c{constructor(e){this.http=e}async getUserAssets(e,t=20,n=0){return ea(async()=>{if(!jn(e))throw Ft("walletAddress","Wallet address");const i=Math.max(1,Math.floor(n/t)+1),o={};o.address=e,o.page=i,o.pageSize=t;const a=await this.http.get("/user/assets",o);if("object"!=typeof a||null===a)throw new Oc(500,"Invalid response format: not an object");const r=Vt(a);if(!r||"object"!=typeof r)throw new Oc(500,"Invalid response format: missing data wrapper");const s=r.token;if(!Array.isArray(s))throw new Oc(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 Oc(500,"Invalid asset in response: asset must be an object");const t=e;if(!jn(t.symbol)||!jn(t.name))throw new Oc(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 Oc(500,"Invalid asset in response: decimals must be a number",t);const i={tokenId:"string"==typeof t.compositeKey&&t.compositeKey.length>0?t.compositeKey:`${t.symbol}$Unit$none$none`,symbol:t.symbol,name:t.name,decimals:n,balance:"string"==typeof t.quantity&&t.quantity.length>0?t.quantity:"0"};"string"==typeof t.image&&t.image.length>0&&(i.imageUrl=t.image),"boolean"==typeof t.verify&&t.verify&&(i.verified=t.verify),c.push(i)}const l={tokens:c,count:"number"==typeof r.count?r.count:c.length};return void 0!==r.totalValue&&(l.totalValue=String(r.totalValue)),l},`getUserAssets(${e})`,void 0,t=>{throw this.handleError(t,`getUserAssets(${e})`)})}async fetchTokenList(e={}){return ea(async()=>{const{address:t,search:n,page:i=1,limit:o=20}=e,a={page:i,limit:jr(o,1,20)};"string"==typeof t&&t.length>0&&(a.address=t),"string"==typeof n&&n.length>0&&(a.search=n);const r=await this.http.get("/user/token-list",a);if("object"!=typeof r||null===r)throw new Oc(500,"Invalid response format: not an object");const s=Vt(r);if(!s||"object"!=typeof s)throw new Oc(500,"Invalid response format: missing data wrapper");const c=s.token;if(!Array.isArray(c))throw new Oc(500,"Invalid response format: token array must be an array");const l=[];for(const e of c){if("object"!=typeof e||null===e)throw new Oc(500,"Invalid token in response: must be an object");const t=e;if(!jn(t.symbol))throw new Oc(500,'Invalid token in response: missing required field "symbol"',{token:t});if(!jn(t.name))throw new Oc(500,'Invalid token in response: missing required field "name"',{token:t});const n=t.decimals;if("string"!=typeof n&&"number"!=typeof n)throw new Oc(500,'Invalid token in response: missing required field "decimals"',{token:t});if(!jn(t.compositeKey))throw new Oc(500,'Invalid token in response: missing required field "compositeKey"',{token:t});l.push({image:"string"==typeof t.image?t.image:"",name:t.name,symbol:t.symbol,decimals:String(n),description:"string"==typeof t.description?t.description:"",verify:"boolean"==typeof t.verify&&t.verify,compositeKey:t.compositeKey,additionalKey:"string"==typeof t.additionalKey?t.additionalKey:"",category:"string"==typeof t.category?t.category:"",type:"string"==typeof t.type?t.type:"",collection:"string"==typeof t.collection?t.collection:"",subscribePrice:"boolean"==typeof t.subscribePrice&&t.subscribePrice,quantity:"string"==typeof t.quantity?t.quantity:"0"})}return{token:l,count:"number"==typeof s.count?s.count:l.length}},"fetchTokenList",void 0,e=>{throw this.handleError(e,"fetchTokenList")})}handleError(e,t){if(e instanceof Oc)return e;if(ct(e)){const n=e.response?.status??500,i=e.response?.data,o=i?.message??i?.Message??at(e),a=i?.Data??i?.data??void 0;return new Oc(n,`${t}: ${String(o)}`,a)}return new Oc(500,`${t}: ${at(e)}`)}}class Kc{constructor(e){this.client=Bc(e.baseUrl,e.timeout??3e4)}async getPoolData(e){return ea(async()=>{if("string"==typeof e.token0||"string"==typeof e.token1)throw new wt(`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" }`,"token","INVALID_TOKEN_FORMAT");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&&Lc(t.token0ClassKey)&&Lc(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 Oc(t.status,"Invalid pool data response format",n);return n},"GalaChainGatewayClient.getPoolData",void 0,e=>{throw this.handleError(e,"getPoolData")})}async getSlot0(e){return ea(async()=>{if("string"==typeof e.token0||"string"==typeof e.token1)throw new wt(`GalaChain API getSlot0 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 getSlot0(). Example: parseToken("GALA|Unit|none|none") → { collection: "GALA", category: "Unit", type: "none", additionalKey: "none" }`,"token","INVALID_TOKEN_FORMAT");const t=await this.client.post("/api/asset/dexv3-contract/GetSlot0",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.sqrtPrice&&"number"==typeof t.tick&&"string"==typeof t.liquidity}(n))throw new Oc(t.status,"Invalid slot0 data response format",n);return n},"GalaChainGatewayClient.getSlot0",void 0,e=>{throw this.handleError(e,"getSlot0")})}async getPositions(e){return ea(async()=>{const t=await this.client.post("/api/asset/dexv3-contract/GetPositions",e);this.validateResponse(t.data);const n=t.data.Data;let i;i=null!=n&&"object"==typeof n&&"positions"in n&&Array.isArray(n.positions)?n.positions:null!=n&&"object"==typeof n&&"positionId"in n?[n]:Array.isArray(n)?n:[];for(const e of i)if(!Mc(e))throw new Oc(t.status,"Invalid position in response",e);return{positions:i,count:i.length}},"GalaChainGatewayClient.getPositions",void 0,e=>{throw this.handleError(e,"getPositions")})}async getPositionById(e,t,n,i,o,a,r){return ea(async()=>{let s,c;if(void 0!==t&&void 0!==n&&void 0!==i&&void 0!==o&&void 0!==a){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:i,tickLower:o,tickUpper:a},null!=r&&(s.positionId=r),c=`${e}/${String(t)}/${String(n)}/${i}`}else s={positionId:e},c=e;const l=await this.client.post("/api/asset/dexv3-contract/GetPositions",s);this.validateResponse(l.data);const d=l.data.Data;let u;if(null!=d&&"object"==typeof d&&"positionId"in d&&!("positions"in d))u=d;else{if(!(null!=d&&Array.isArray(d.positions)&&d.positions.length>0))throw new Oc(404,`Position not found: ${c}`);u=d.positions[0]}const h={Data:u,Status:l.status};return void 0!==l.data.Message&&(h.Message=l.data.Message),h},"GalaChainGatewayClient.getPositionById",void 0,t=>{throw this.handleError(t,`getPositionById(${e})`)})}async getRemoveLiquidityEstimation(e){return ea(async()=>{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)throw new Oc(t.status,"Invalid removal estimation response format",n);return n},"GalaChainGatewayClient.getRemoveLiquidityEstimation",void 0,e=>{throw this.handleError(e,"getRemoveLiquidityEstimation")})}validateResponse(e){if(null==e||"object"!=typeof e)throw new Oc(500,"Invalid response format: not an object");if(!("Data"in e)||!("Status"in e))throw new Oc(500,"Invalid response format: missing Data or Status field");if(e.Status>=400)throw new Oc(e.Status,e.Message??"Gateway error",e.Data)}handleError(e,t){if(e instanceof Oc)return e;if(ct(e)){const n=e.response?.status??500,i=e.response?.data,o=i?.Message??at(e);return new Oc(n,`${t}: ${o}`,i?.Data??void 0)}return new Oc(500,`${t}: ${at(e)}`)}}const qc=10;class Gc extends si{constructor(e,t,n){if(super(!1),this.pricingConcurrency=5,this.tokenConverter=new Ns,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,void 0===e.gatewayBaseUrl||null===e.gatewayBaseUrl||""===e.gatewayBaseUrl||void 0===e.bundlerBaseUrl||null===e.bundlerBaseUrl||""===e.bundlerBaseUrl||void 0===e.dexBackendBaseUrl||null===e.dexBackendBaseUrl||""===e.dexBackendBaseUrl||void 0===e.dexBackendHttp||null===e.dexBackendHttp)throw new St("GSwapService requires explicit gatewayBaseUrl, bundlerBaseUrl, dexBackendBaseUrl, and dexBackendHttp configuration. These must be provided by LaunchpadSDK to ensure environment alignment.","gswapConfig");try{this.gatewayClient=new Kc({baseUrl:e.gatewayBaseUrl,timeout:3e4}),this.dexBackendClient=new $c(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 St("Failed to initialize GSwapService HTTP clients","httpClients")}}setPricingConcurrency(e){if(e<1)throw $t("pricingConcurrency",1,e,"Pricing concurrency");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(po(e.amount).isLessThanOrEqualTo(0))throw new Tt("Amount must be greater than zero",{amount:e.amount,fromToken:e.fromToken,toToken:e.toToken});if(!this.dexQuoteService)throw new Tt("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),[i,o]=t<n?[t,n]:[n,t],a=[3e3,500,1e4];let r;for(const s of a)try{const a=await this.dexQuoteService.fetchCompositePoolData({token0:i,token1:o,fee:s,gatewayBaseUrl:this.gatewayBaseUrl}),r=await this.dexQuoteService.calculateDexPoolQuoteExactAmount({compositePoolData:a,fromToken:t,toToken:n,amount:e.amount}),c=po(r.currentSqrtPrice),l=po(r.newSqrtPrice),d=c.gt(l)?c.minus(l).dividedBy(c):po(0),u=po(r.amount0),h=po(r.amount1),g=So(u),m=So(h);this.logger.debug("=== AMOUNT SELECTION RAW DATA ===",{"quoteResult.amount0":r.amount0,"quoteResult.amount1":r.amount1,"amount0BN.isNegative()":g,"amount1BN.isNegative()":m});const p=g?u:h;this.logger.debug("=== AMOUNT SELECTION RESULT ===",{selectedFromAmount0:g,selectedAmount:p.toFixed(),selectedAmountAbs:p.absoluteValue().toFixed()});const f=p.absoluteValue().toFixed();return{fromToken:e.fromToken,toToken:e.toToken,inputAmount:e.amount,estimatedOutput:f,feeTier:s,priceImpact:d.toFixed(),executionPrice:this.calculateExecutionPrice(e.amount,f),currentSqrtPrice:r.currentSqrtPrice,newSqrtPrice:r.newSqrtPrice}}catch(e){r=e,this.logger.debug("DexQuoteService failed for fee tier, trying next",{feeTier:s,error:ot(e)?e.message:"Unknown error"})}throw r??new Tt("No available fee tiers for quote",{feeTiers:a,fromToken:e.fromToken,toToken:e.toToken})}catch(e){this.handleGSwapError("Failed to get swap quote for exact input",Tt,e)}}async getSwapQuoteExactOutput(e){try{if(po(e.amount).isLessThanOrEqualTo(0))throw new Tt("Amount must be greater than zero",{amount:e.amount,fromToken:e.fromToken,toToken:e.toToken});if(!this.dexQuoteService)throw new Tt("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),[i,o]=t<n?[t,n]:[n,t],a=[3e3,500,1e4];let r;for(const s of a)try{const a=await this.dexQuoteService.fetchCompositePoolData({token0:i,token1:o,fee:s,gatewayBaseUrl:this.gatewayBaseUrl}),r=await this.dexQuoteService.calculateDexPoolQuoteExactAmount({compositePoolData:a,fromToken:t,toToken:n,amount:e.amount}),c=po(r.currentSqrtPrice),l=po(r.newSqrtPrice),d=c.gt(l)?c.minus(l).dividedBy(c):po(0),u=a.pool.token0,h="string"==typeof u?wr(u).collection:"object"==typeof u&&null!==u&&"tokenName"in u?u.tokenName:String(u),g=wr(n).collection===h?r.amount1:r.amount0;return{fromToken:e.fromToken,toToken:e.toToken,inputAmount:g,estimatedOutput:e.amount,feeTier:s,priceImpact:d.toFixed(),executionPrice:this.calculateExecutionPrice(g,e.amount),currentSqrtPrice:r.currentSqrtPrice,newSqrtPrice:r.newSqrtPrice}}catch(e){r=e,this.logger.debug("DexQuoteService failed for fee tier, trying next",{feeTier:s,error:ot(e)?e.message:"Unknown error"})}throw r??new Tt("No available fee tiers for quote",{feeTiers:a,fromToken:e.fromToken,toToken:e.toToken})}catch(e){this.handleGSwapError("Failed to get swap quote for exact output",Tt,e)}}async executeSwap(e){try{if(void 0===this.privateKey||null===this.privateKey||""===this.privateKey)throw new St("GSwapService not initialized with signing capability (privateKey required)","privateKey");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),i=function(e,t=.01){const n=po(e),i=new o(1).minus(t);return n.multipliedBy(i)}(e.estimatedOutput,e.slippageTolerance??.01),a=this.getWalletAddress();if(null==a||""===a)throw new wt("Wallet address required for swap execution","walletAddress",mt);let r;try{const t=await this.getSwapQuoteExactInput({fromToken:e.fromToken,toToken:e.toToken,amount:e.inputAmount});r=t.currentSqrtPrice,this.logger.debug("Quote refetch successful - extracted sqrtPrices",{currentSqrtPrice:r,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:at(t)})}const s={fromToken:t,toToken:n,inputAmount:e.inputAmount,minOutput:i.toFixed(),feeTier:e.feeTier,walletAddress:a,slippageTolerance:e.slippageTolerance??.01,...!Vn(r)&&{currentSqrtPrice:r}},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",Et,e,{transactionHash:t?.txHash})}}async getUserAssets(e,t=1,n=20){return ea(async()=>{if(!ki(e))throw new It(Rt("walletAddress","a valid address (0x..., eth|..., or client|...)").message,new Error("INVALID_ADDRESS_FORMAT"),e,"INVALID_ADDRESS");this.logger.debug("Fetching user assets",{walletAddress:e,page:t,limit:n});return(await this.dexBackendClient.fetchTokenList({address:e,page:t,limit:n})).token.filter(e=>"0"!==e.quantity).map(e=>this.transformRawTokenToUserAsset(e)).filter(e=>null!==e)},"Failed to fetch user assets",this.logger,this.createGSwapErrorHandler(It,{walletAddress:e,page:t,limit:n}))}async getAllUserAssets(e){return ea(async()=>{if(!ki(e))throw new It(Rt("walletAddress","a valid address (0x..., eth|..., or client|...)").message,new Error("INVALID_ADDRESS_FORMAT"),e,"INVALID_ADDRESS");this.logger.debug("Fetching all user assets (auto-paginated with optimization)",{walletAddress:e});let t=!1;const n=await cs(async(n,i)=>{const o=await this.dexBackendClient.fetchTokenList({address:e,page:n,limit:i}),a=[];for(const e of o.token){if("0"===e.quantity){t=!0;break}const n=this.transformRawTokenToUserAsset(e);n&&a.push(n)}return{items:a,page:n,limit:i,total:a.length,totalPages:1,hasNext:!t&&o.token.length===i,hasPrevious:n>1}},{maxPages:20,pageSize:20,logger:this.logger});return this.logger.debug("Fetched all user assets",{walletAddress:e,totalAssets:n.items.length}),n.items},"Failed to fetch all user assets",this.logger,this.createGSwapErrorHandler(It,{walletAddress:e}))}async fetchAvailableDexTokens(e={}){return ea(async()=>{const{search:t,limit:n=20,page:i=1}=e;this.logger.debug("Fetching available DEX tokens",{search:t,limit:n,page:i});const o=await this.dexBackendClient.fetchTokenList({...!Vn(t)&&{search:t},limit:n,page:i}),a=o.token.map(e=>this.transformRawTokenToDexToken(e)),r=i*n<o.count;return{tokens:a,count:o.count,page:i,limit:n,hasMore:r}},"Failed to fetch available DEX tokens",this.logger,this.createGSwapErrorHandler(It,{...e}))}async fetchAllAvailableDexTokens(e={}){return ea(async()=>{const{search:t}=e;this.logger.debug("Fetching all available DEX tokens (auto-paginated)",{search:t});const n=[];let i=1;for(;;){const e=await this.dexBackendClient.fetchTokenList({...!Vn(t)&&{search:t},limit:20,page:i}),o=e.token.map(e=>this.transformRawTokenToDexToken(e));if(n.push(...o),o.length<20||20*i>=e.count)break;i+=1}return this.logger.debug("Fetched all available DEX tokens",{search:t,totalTokens:n.length}),n},"Failed to fetch all available DEX tokens",this.logger,this.createGSwapErrorHandler(It,e))}async getPoolInfo(e,t){try{if(null==e||""===e)throw Ft("tokenA","Token A");if(null==t||""===t)throw Ft("tokenB","Token B");this.logger.debug("Fetching pool info",{tokenA:e,tokenB:t});const{gswapToken0:n,gswapToken1:i}=this.convertTokenPair(e,t),o=[500,3e3,1e4];let a=po(0),r=0;for(const s of o)try{const e="string"==typeof n?wr(n):n,t="string"==typeof i?wr(i):i,o=await this.gatewayClient.getPoolData({token0:e,token1:t,fee:s});null!=o&&(a=a.plus(po(o.liquidity)),r++)}catch{this.logger.debug("Pool not found for fee tier",{tokenA:e,tokenB:t,feeTier:s})}return{tokenA:e,tokenB:t,liquidity:a.toFixed(),feeTiers:o,swapCount:r}}catch(n){return this.logger.warn("Failed to fetch pool info",n),this.logger.debug("Pool error details",{error:new Ct(`Failed to fetch pool info: ${at(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 i=0;i<e.length;i+=t)n.push(e.slice(i,i+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 i=Array.from(n.values()),o=this.chunkArray(i,t);this.logger.debug("Fetching pricing for positions",{totalPositions:e.length,uniquePoolsToPrice:n.size,chunks:o.length,concurrency:t});const a=new Map;for(let e=0;e<o.length;e++){const t=o[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?a.set(e.value.key,e.value.data):this.logger.warn("Failed to fetch price for pool",{error:e.reason})})}return a}normalizePositionResponse(e,t){const n=e,i=e=>{if(null==e)return"";if("string"==typeof e)return e;if("object"==typeof e){const t=e;if(null!==t.type&&void 0!==t.type&&"none"!==t.type)return t.type;if(null!==t.collection&&void 0!==t.collection&&""!==t.collection)return t.collection;if(null!==t.symbol&&void 0!==t.symbol&&""!==t.symbol)return t.symbol;if(null!==t.tokenSymbol&&void 0!==t.tokenSymbol&&""!==t.tokenSymbol)return t.tokenSymbol;if(null!==t.name&&void 0!==t.name&&""!==t.name)return t.name}return""},o=i(n.token0),a=i(n.tokenA),r=i(n.token1),s=i(n.tokenB),c=n.token0Symbol??(""!==o?o:""!==a?a:n.tokenSymbol0??""),l=n.token1Symbol??(""!==r?r:""!==s?s:n.tokenSymbol1??""),d=""!==c?this.tokenConverter.normalizeInternalApiResponse(c):"",u=""!==l?this.tokenConverter.normalizeInternalApiResponse(l):"";return{positionId:n.positionId??n.id??"",ownerAddress:null!=t&&""!==t?t:n.ownerAddress??n.owner??"",token0:d,token1:u,feeTier:n.feeTier??n.fee??n.feeAmount??0,tickLower:n.tickLower??n.lowerTick??0,tickUpper:n.tickUpper??n.upperTick??0,liquidity:String(n.liquidity??n.liquidityAmount??"0"),amount0:String(n.amount0??n.amountA??"0"),amount1:String(n.amount1??n.amountB??"0"),feeAmount0:String(n.feeAmount0??n.feesA??"0"),feeAmount1:String(n.feeAmount1??n.feesB??"0"),...null!==n.createdAt&&void 0!==n.createdAt&&{createdAt:new Date(n.createdAt)},...null!==n.updatedAt&&void 0!==n.updatedAt&&{updatedAt:new Date(n.updatedAt)}}}parseTokenFlexible(e){try{return wr(e)}catch(t){if(ot(t)&&at(t).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}}transformRawTokenToDexToken(e){return{image:e.image,name:e.name,symbol:e.symbol,decimals:Rn(e.decimals,18),description:e.description,verified:e.verify,compositeKey:e.compositeKey,additionalKey:e.additionalKey,category:e.category,type:e.type,collection:e.collection,subscribePrice:e.subscribePrice}}transformRawTokenToUserAsset(e){const t=null!==e.symbol&&void 0!==e.symbol&&""!==e.symbol?e.symbol:"UNKNOWN";try{const n=null!==e.compositeKey&&void 0!==e.compositeKey&&""!==e.compositeKey?wr(e.compositeKey.replace(/\$/g,"|")):wr(`${t}|Unit|none|none`);return{...this.transformRawTokenToDexToken(e),tokenId:n,balance:fo(null!==e.quantity&&void 0!==e.quantity&&""!==e.quantity?e.quantity:"0")}}catch(e){return this.logger.debug(`Skipping asset with processing error: ${t}`,{error:at(e)}),null}}async getUserLiquidityPositions(e,t=10,n,i){try{if(null==e||""===e)throw Ft("ownerAddress","Owner address");this.logger.debug("Fetching user liquidity positions",{ownerAddress:e,limit:t,bookmark:n});const o=`${this.galaChainBaseUrl}/api/asset/dexv3-contract/GetUserPositions`,a={user:e,limit:t,bookmark:null!=n&&""!==n?n:""};this.logger.debug("Sending position query request",{endpoint:o,payload:a});const r=await u.post(o,a,{headers:{"Content-Type":"application/json",Accept:"application/json"}}),s=Vt(r);if(200!==r.status||1!==s?.Status)return this.logger.warn("Unexpected API response status",{httpStatus:r.status,apiStatus:s?.Status}),{items:[]};const c=s?.Data??{},l=c?.positions??[],d=c?.nextBookMark,h=l.filter(e=>null!=e&&"object"==typeof e&&("positionId"in e||"id"in e)).map(t=>this.normalizePositionResponse(t,e));let g;this.logger.debug("Retrieved liquidity positions",{count:h.length,hasNextBookmark:null!=d,nextBookmark:d}),i?.withPrices&&h.length>0&&(g=await this.fetchPositionPrices(h));const m={items:h};return Vn(d)||(m.nextBookmark=d),Vn(g)||(m.prices=g),m}catch(t){ct(t)&&this.logger.error("Position query failed with HTTP error",{status:t.response?.status,statusText:t.response?.statusText,data:t.response?.data,endpoint:this.galaChainBaseUrl,ownerAddress:e}),this.handleGSwapError("Failed to fetch user liquidity positions",Nt,t)}}async getAllSwapUserLiquidityPositions(e,t){try{if(null==e||""===e)throw Ft("ownerAddress","Owner address");this.logger.debug("Fetching all user liquidity positions (auto-paginated)",{ownerAddress:e});const n=async t=>{const n=await this.getUserLiquidityPositions(e,qc,t,void 0);return{items:n.items,nextBookmark:n.nextBookmark}},i=await async function(e,t={}){const{maxPages:n=1e4,logger:i,pageSize:o=20}=t,a=[];let r,s=0;for(;s<n;){i&&i.debug(`Auto-pagination (bookmark): fetching page ${s+1} with pageSize ${o}`,{bookmark:r});const t=await e(r,o);let n,c,l;if(!0===Array.isArray(t))n=t,c=void 0,l=!1;else{if(null==t||"object"!=typeof t||!("items"in t)){i&&i.warn("Auto-pagination (bookmark): received invalid result structure, stopping");break}n=t.items,c=t.nextBookmark,l=!0}if(!Array.isArray(n)){i&&i.warn("Auto-pagination (bookmark): received invalid items array, stopping");break}if(0===n.length){i&&i.debug(`Auto-pagination (bookmark): no items returned on page ${s+1}, exiting loop`);break}a.push(...n),s++,i&&i.debug(`Auto-pagination (bookmark): page ${s} returned ${n.length} items`,{hasNextBookmark:""!==(c??""),format:l?"BookmarkPaginationResult":"legacy-array"});const d=n.length<o;if(l&&(""===c||void 0===c)){i&&i.debug("Auto-pagination (bookmark): no nextBookmark returned, reached end of results",{nextBookmark:""===c?"(empty string)":"(undefined)"});break}if(d){i&&i.debug("Auto-pagination (bookmark): received fewer items than limit, reached last page",{received:n.length,pageSize:o,format:l?"BookmarkPaginationResult":"legacy-array"});break}r=c}return s>=n&&i&&i.warn(`Auto-pagination (bookmark): exceeded maxPages limit of ${n}, stopping iteration`),i&&i.debug(`Auto-pagination (bookmark): completed with total items: ${a.length}`,{pageCount:s}),{items:a,total:a.length}}(n,{maxPages:1e4,logger:this.logger,pageSize:qc}),o=i.items;if(this.logger.debug("Fetched all user liquidity positions",{ownerAddress:e,totalPositions:o.length}),t?.withPrices&&o.length>0){return{items:o,prices:await this.fetchPositionPrices(o)}}return o}catch(t){this.handleGSwapError("Failed to fetch all user liquidity positions",Nt,t,{ownerAddress:e})}}async getLiquidityPosition(e,t){try{if(null==e||""===e)throw Ft("ownerAddress","Owner address");if(void 0===t.token0||null===t.token0||""===t.token0)throw Ft("token0","Token 0");if(void 0===t.token1||null===t.token1||""===t.token1)throw Ft("token1","Token 1");this.logger.debug("Fetching liquidity position",{ownerAddress:e,position:t}),this.validateTickSpacing(t.tickLower,t.tickUpper,t.fee);const{gswapToken0:n,gswapToken1:i}=this.convertTokenPair(t.token0,t.token1),o=Vs(n),a=Vs(i),r=(await this.gatewayClient.getPositions({owner:e,token0:o,token1:a,fee:t.fee,tickLower:t.tickLower,tickUpper:t.tickUpper})).positions.find(e=>e.tickLower===t.tickLower&&e.tickUpper===t.tickUpper);if(!r||"object"!=typeof r||!("positionId"in r)&&!("id"in r))throw new Nt("Invalid position data returned from API",null,"INVALID_DATA");const s=this.normalizePositionResponse(r,e);return this.logger.debug("Retrieved liquidity position",{positionId:s.positionId}),s}catch(e){this.handleGSwapError("Failed to fetch liquidity position",Nt,e)}}async getLiquidityPositionById(e,t,n,i,o,a,r){try{if(null==e||""===e)throw Ft("ownerAddress","Owner address");if(null==t||""===t)throw Ft("positionId","Position ID");let s;this.logger.debug("Fetching liquidity position by ID",{ownerAddress:e,positionId:t,hasToken0:null!=n,hasToken1:null!=i,hasFee:null!=o,hasTickLower:!Vn(a),hasTickUpper:!Vn(r)});let c=null;const l=5,d=2e3;for(let u=1;u<=l;u++)try{if(null!=n&&""!==n&&null!=i&&""!==i&&!Vn(o)&&!Vn(a)&&!Vn(r))try{this.logger.debug("Attempting compound key lookup",{ownerAddress:e,token0:n,token1:i,feeTier:o,tickLower:a,tickUpper:r});if(s=(await this.gatewayClient.getPositionById(e,n,i,o,a,r,t)).Data,null!=s&&"object"==typeof s&&("positionId"in s||"id"in s)){this.logger.debug("Successfully fetched position via compound key",{attempt:u,positionId:t});break}throw new Nt("Invalid position data from compound key lookup",null,"INVALID_DATA")}catch(e){this.logger.debug("Compound key lookup failed, trying fallback",{attempt:u,error:ot(e)?e.message:e})}try{if(s=(await this.gatewayClient.getPositionById(t)).Data,null!=s&&"object"==typeof s&&("positionId"in s||"id"in s)){this.logger.debug("Successfully fetched position on attempt",{attempt:u,positionId:t});break}throw new Nt("Invalid position data from direct lookup",null,"INVALID_DATA")}catch(n){this.logger.debug("Direct position lookup failed, trying fallback via GetUserPositions",{attempt:u,positionId:t,error:ot(n)?n.message:n});const i=await this.getAllSwapUserLiquidityPositions(e),o=Array.isArray(i)?i:i.items;if(o.length>0){const e=o.find(e=>Ro(null!==e.positionId&&void 0!==e.positionId&&""!==e.positionId?e.positionId:"",t));if(e){s=e,this.logger.debug("Found position via fallback (GetUserPositions)",{attempt:u,positionId:t,totalPositions:o.length});break}}if(c=Wt(t),u<l){this.logger.warn("Fallback query did not find position, retrying",{attempt:u,positionId:t,ownerAddress:e,foundCount:o.length}),await new Promise(e=>setTimeout(e,d));continue}}}catch(e){if(u<l){this.logger.warn("Error fetching position, retrying",{attempt:u,positionId:t,error:at(e)}),await new Promise(e=>setTimeout(e,d));continue}c=ot(e)?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:null!=s?Object.keys(s):"null",lastError:c?.message}),c??Wt(t);const u=this.normalizePositionResponse(s,e);return this.logger.debug("Retrieved liquidity position by ID",{positionId:u.positionId}),u}catch(e){this.handleGSwapError("Failed to fetch liquidity position by ID",Nt,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="string"==typeof e.token1?this.parseTokenFlexible(e.token1):e.token1;this.logger.debug("Fetching position via compound key",{token0:e.token0,token1:e.token1,fee:e.fee,owner:e.owner});const i=await this.gatewayClient.getPositions({token0:t,token1:n,fee:e.fee,tickLower:e.tickLower,tickUpper:e.tickUpper,owner:e.owner});if(null===i.positions||void 0===i.positions||0===i.positions.length)throw new Nt("Position not found: No position exists for this compound key",null,"NOT_FOUND");const o=i.positions[0],a=this.normalizePositionResponse(o,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",Nt,e)}}async estimateRemoveLiquidity(e){try{if(void 0===e.token0||null===e.token0||""===e.token0)throw Ft("token0","Token 0");if(void 0===e.token1||null===e.token1||""===e.token1)throw Ft("token1","Token 1");if(void 0===e.liquidity||null===e.liquidity||""===e.liquidity)throw Ft("liquidity","Liquidity amount");if(void 0===e.owner||null===e.owner||""===e.owner)throw Ft("owner","Owner address");this.logger.debug("Estimating liquidity removal",{token0:e.token0,token1:e.token1,owner:e.owner}),this.validateTickSpacing(e.tickLower,e.tickUpper,e.fee);const{gswapToken0:t,gswapToken1:n}=this.convertTokenPair(e.token0,e.token1),i=Vs(t),o=Vs(n),a=await this.gatewayClient.getRemoveLiquidityEstimation({token0:i,token1:o,fee:e.fee,amount:e.liquidity,tickLower:e.tickLower,tickUpper:e.tickUpper,owner:e.owner});return this.logger.debug("Estimated removal",{result:a}),a}catch(e){this.handleGSwapError("Failed to estimate liquidity removal",Nt,e)}}async addLiquidityByPrice(e){try{if(void 0===this.privateKey||null===this.privateKey||""===this.privateKey)throw new St("GSwapService not initialized with signing capability (privateKey required)","privateKey");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 i=wr(t),o=wr(n),a=(await this.gatewayClient.getPoolData({token0:i,token1:o,fee:e.fee})).tickSpacing;this.logger.debug("Retrieved tick spacing from pool",{tickSpacing:a,fee:e.fee});const r=po(e.minPrice),s=po(e.maxPrice),c=Math.floor(yo(r)),l=Math.ceil(yo(s)),d=Do(c,a),u=Do(l,a);this.logger.debug("Converted price range to ticks",{minPrice:e.minPrice,maxPrice:e.maxPrice,tickLower:d,tickUpper:u,tickSpacing:a});const h=this.getWalletAddress();if(null==h||""===h)throw new St("GSwapService: No wallet address available - cannot create position","walletAddress");const g="string"==typeof e.token0?wr(e.token0):e.token0,m="string"==typeof e.token1?wr(e.token1):e.token1;this.logger.debug("Sending AddLiquidity by price to bundler",{fee:e.fee,tickRange:`${d}-${u}`,walletAddress:h});const p=await this.sendAddLiquidityToBundler({token0:g,token1:m,fee:e.fee,tickLower:d,tickUpper:u,amount0Desired:e.amount0Desired,amount1Desired:e.amount1Desired,amount0Min:null!==e.amount0Min&&void 0!==e.amount0Min&&""!==e.amount0Min?e.amount0Min:"0",amount1Min:null!==e.amount1Min&&void 0!==e.amount1Min&&""!==e.amount1Min?e.amount1Min:"0",owner:h}),f={transactionId:p};if(null!==f.positionId&&void 0!==f.positionId&&""!==f.positionId&&null!=p&&""!==p){this.logger.debug("Position ID returned directly from backend",{transactionId:p,positionId:f.positionId}),await this.ensureWebSocketConnected();const e=await this.webSocketService.waitForTransaction(p);this.logger.debug("Liquidity transaction confirmed on-chain",{transactionId:p,status:e.status});const t=this.getWalletAddress();if(null!=t&&""!==t&&null!==f.positionId&&void 0!==f.positionId&&""!==f.positionId)try{const n=await this.getLiquidityPositionById(t,f.positionId);this.logger.debug("Fetched full position data",{positionId:n.positionId,liquidity:n.liquidity,amount0:n.amount0,amount1:n.amount1});const{createdAt:i,updatedAt:o,...a}=n,r=i instanceof Date?i.getTime():"number"==typeof i?i:void 0,s={...f,...a,positionId:f.positionId,status:e.status,transactionId:e.transactionId,timestamp:new Date(e.timestamp),wait:async e=>{await this.webSocketService.waitForTransaction(p)}};return Vn(r)||(s.createdAt=r),s}catch(t){return this.logger.debug("Could not fetch full position details",{error:at(t)}),{...f,positionId:f.positionId,status:e.status,transactionId:e.transactionId,timestamp:new Date(e.timestamp),wait:async e=>{await this.webSocketService.waitForTransaction(p)}}}}if(null!=p&&""!==p){this.logger.debug("Monitoring liquidity transaction (discovery mode)",{transactionId:p}),await this.ensureWebSocketConnected();const t=await this.webSocketService.waitForTransaction(p);let n;this.logger.debug("Liquidity transaction confirmed on-chain",{transactionId:p,status:t.status});let i=null;this.logger.debug("Waiting for position indexing after WebSocket confirmation",{token0:e.token0,token1:e.token1,fee:e.fee}),await new Promise(e=>setTimeout(e,2e3));try{const t=this.getWalletAddress();if(null==t||""===t)throw new St("No wallet address available","walletAddress");this.logger.debug("Fetching positions",{walletAddress:t});const o=(await this.getUserLiquidityPositions(t,10)).items;if(this.logger.debug("Fetched positions",{count:null!=o?o.length:0}),null!=o&&o.length>0){const t=wr(e.token0).collection.toUpperCase(),a=wr(e.token1).collection.toUpperCase(),r=[];for(const n of o){if(""===(n?.positionId??""))continue;const i=null!==n.token0&&void 0!==n.token0?n.token0.toUpperCase():void 0,o=null!==n.token1&&void 0!==n.token1?n.token1.toUpperCase():void 0;if(null==i||""===i||null==o||""===o){this.logger.debug("Skipping position with empty tokens",{positionId:n.positionId});continue}const s=Uo(i,o,t,a),c=n.feeTier===e.fee;this.logger.debug("Checking position",{positionId:n.positionId,tokens:`${i}/${o}`,tokensMatch:s,fee:n.feeTier,feeMatches:c}),s&&c&&r.push(n)}r.length>0?(i=r[r.length-1],n=i.positionId,this.logger.debug("Found newly created position",{positionId:n,expectedTokens:`${wr(e.token0).collection}/${wr(e.token1).collection}`,expectedFee:e.fee,positionCount:o.length,liquidity:i.liquidity,amount0:i.amount0,amount1:i.amount1})):this.logger.debug("No matching position found",{availablePositions:o.map(e=>({id:e.positionId,token0:e.token0,token1:e.token1,fee:e.feeTier}))})}else this.logger.debug("No positions returned from API")}catch(e){this.logger.debug("Error fetching positions",{error:at(e)})}this.logger.debug("Discovery complete",{positionId:null!=n&&""!==n?n:"not found",matchedPositionData:{positionId:i?.positionId,liquidity:i?.liquidity,amount0:i?.amount0,amount1:i?.amount1,feeAmount0:i?.feeAmount0,feeAmount1:i?.feeAmount1,token0:i?.token0,token1:i?.token1,feeTier:i?.feeTier}});let o=i;if(null!=n&&""!==n)try{o=await this.getLiquidityPositionById(h,n),this.logger.debug("Fetched full position details",{positionId:o.positionId,liquidity:o.liquidity,amount0:o.amount0,amount1:o.amount1,feeAmount0:o.feeAmount0,feeAmount1:o.feeAmount1})}catch(e){this.logger.debug("Could not fetch full position details, using discovered data",{error:at(e)})}const a=o?{ownerAddress:o.ownerAddress,token0:o.token0,token1:o.token1,feeTier:o.feeTier,tickLower:o.tickLower,tickUpper:o.tickUpper,liquidity:o.liquidity,amount0:o.amount0,amount1:o.amount1,feeAmount0:o.feeAmount0,feeAmount1:o.feeAmount1}:{};return{...f,...a,...null!=n&&""!==n&&{positionId:n},status:t.status,transactionId:t.transactionId,timestamp:new Date(t.timestamp),wait:async e=>{await this.webSocketService.waitForTransaction(p)}}}return this.logger.warn("No transaction ID in liquidity result, cannot confirm position creation"),f}catch(e){this.logger.error("Error in addSwapLiquidityByPrice",{error:at(e),stack:ot(e)?rt(e)?.split("\n").slice(0,3).join(" | "):void 0}),this.handleGSwapError("Failed to add liquidity by price",Nt,e)}}async addSwapLiquidityByTicks(e){try{if(void 0===this.privateKey||null===this.privateKey||""===this.privateKey)throw new St("GSwapService not initialized with signing capability (privateKey required)","privateKey");const t=this.getWalletAddress();if(null==t||""===t)throw new St("GSwapService: No wallet address available - cannot create position","walletAddress");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?wr(e.token0):e.token0,i="string"==typeof e.token1?wr(e.token1):e.token1;await this.ensureWebSocketConnected();const o=await this.sendAddLiquidityToBundler({token0:n,token1:i,fee:e.fee,tickLower:e.tickLower,tickUpper:e.tickUpper,amount0Desired:e.amount0Desired,amount1Desired:e.amount1Desired,amount0Min:null!==e.amount0Min&&void 0!==e.amount0Min&&""!==e.amount0Min?e.amount0Min:"0",amount1Min:null!==e.amount1Min&&void 0!==e.amount1Min&&""!==e.amount1Min?e.amount1Min:"0",owner:t});this.logger.info("Liquidity transaction submitted to bundler",{transactionId:o});const a=this.webSocketService.waitForTransaction(o),r={transactionId:o};if(null!==r.positionId&&void 0!==r.positionId&&""!==r.positionId&&null!=o&&""!==o){this.logger.info("Position ID returned directly from backend",{transactionId:o,positionId:r.positionId});const e=await a;this.logger.debug("Liquidity transaction confirmed on-chain",{transactionId:o,status:e.status});const t=this.getWalletAddress();if(null!=t&&""!==t&&null!==r.positionId&&void 0!==r.positionId&&""!==r.positionId)try{this.logger.debug("Fetching full position details",{positionId:r.positionId});const n=await this.getLiquidityPositionById(t,r.positionId);this.logger.debug("Fetched full position data",{positionId:n.positionId,liquidity:n.liquidity,amount0:n.amount0,amount1:n.amount1});const{createdAt:i,updatedAt:a,...s}=n,c=i instanceof Date?i.getTime():"number"==typeof i?i:void 0,l={...r,...s,positionId:r.positionId,status:e.status,transactionId:e.transactionId,timestamp:new Date(e.timestamp),wait:async e=>{await this.webSocketService.waitForTransaction(o)}};return Vn(c)||(l.createdAt=c),l}catch(t){return this.logger.warn("Could not fetch full position details",{positionId:r.positionId,error:at(t)}),{...r,positionId:r.positionId,status:e.status,transactionId:e.transactionId,timestamp:new Date(e.timestamp),wait:async e=>{await this.webSocketService.waitForTransaction(o)}}}}if(null!=o&&""!==o){this.logger.debug("Monitoring liquidity transaction (discovery mode)",{transactionId:o});const n=await a;let i;this.logger.debug("Liquidity transaction confirmed on-chain",{transactionId:o,status:n.status});let s=null;const c="string"==typeof e.token0?e.token0:e.token0?.type??"unknown",l="string"==typeof e.token1?e.token1:e.token1?.type??"unknown";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(null==t||""===t)throw new St("No wallet address available","walletAddress");this.logger.debug("Fetching positions from API",{walletAddress:t,pageSize:qc});const n=3,o=5e3,a=3e3;let r=[];for(let c=1;c<=n;c++){const l=1===c?o:a;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:qc});if(r=(await this.getUserLiquidityPositions(t,qc)).items,this.logger.debug("Got positions from API",{count:null!=r?r.length:0}),null!=r&&r.length>0){const t=("string"==typeof e.token0?wr(e.token0).collection:e.token0.collection).toUpperCase(),n=("string"==typeof e.token1?wr(e.token1).collection:e.token1.collection).toUpperCase(),o=[];for(const i of r){if(""===(i?.positionId??""))continue;const a=null!==i.token0&&void 0!==i.token0?i.token0.toUpperCase():void 0,r=null!==i.token1&&void 0!==i.token1?i.token1.toUpperCase():void 0;if(null==a||""===a||null==r||""===r){this.logger.debug("Skipping position with empty tokens",{positionId:i.positionId});continue}const s=Uo(a,r,t,n),c=i.feeTier===e.fee;this.logger.debug("Checking position against target",{positionId:i.positionId,tokens:`${a}/${r}`,tokensMatch:s,fee:i.feeTier,feeMatches:c}),s&&c&&o.push(i)}if(o.length>0){s=o[o.length-1],i=s.positionId,this.logger.info("Found newly created position",{positionId:i,liquidity:s.liquidity,amount0:s.amount0,amount1:s.amount1,fee:s.feeTier}),this.logger.debug("Found newly created position",{positionId:i,expectedTokens:`${wr(e.token0).collection}/${wr(e.token1).collection}`,expectedFee:e.fee,positionCount:r.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:at(e)}),this.logger.debug("Error waiting for position indexing",{error:at(e)})}this.logger.debug("Position discovery complete",{positionId:null!=i&&""!==i?i:"not found",found:null!=i&&""!==i}),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 d=s;if(null!=i&&""!==i)try{this.logger.debug("Fetching full position details",{positionId:i}),d=await this.getLiquidityPositionById(t,i),this.logger.debug("Fetched full position data",{positionId:d.positionId,liquidity:d.liquidity,amount0:d.amount0,amount1:d.amount1,feeAmount0:d.feeAmount0,feeAmount1:d.feeAmount1})}catch(e){this.logger.warn("Could not fetch full position details, using discovered data",{positionId:i,error:at(e)})}const u=d?{ownerAddress:d.ownerAddress,token0:d.token0,token1:d.token1,feeTier:d.feeTier,tickLower:d.tickLower,tickUpper:d.tickUpper,liquidity:d.liquidity,amount0:d.amount0,amount1:d.amount1,feeAmount0:d.feeAmount0,feeAmount1:d.feeAmount1}:{};return{...r,...u,...null!=i&&""!==i&&{positionId:i},status:n.status,transactionId:n.transactionId,timestamp:new Date(n.timestamp),wait:async e=>{await this.webSocketService.waitForTransaction(o)}}}return this.logger.warn("No transaction ID in liquidity result, cannot confirm position creation"),r}catch(e){this.handleGSwapError("Failed to add liquidity by ticks",Nt,e)}}async monitorBundlerTransaction(e,t,n="bundler"){let i;try{const o=await t;i={status:o.status,transactionId:null!==o.transactionId&&void 0!==o.transactionId&&""!==o.transactionId?o.transactionId:e,timestamp:null!==o.timestamp&&void 0!==o.timestamp&&0!==o.timestamp?o.timestamp:Date.now(),data:o.data},this.logger.debug(`${n} transaction confirmed on-chain`,{transactionId:e,status:i.status})}catch(t){return this.logger.warn(`WebSocket monitoring timeout for ${n} transaction, returning result with transaction ID`,{transactionId:e,error:at(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:i.transactionId,status:i.status,timestamp:new Date(i.timestamp),wait:async t=>{await this.webSocketService.waitForTransaction(e)}}}async removeLiquidity(e){try{if(void 0===this.privateKey||null===this.privateKey||""===this.privateKey)throw new St("Private key not available for bundler-direct operations","privateKey");this.logger.debug("Removing liquidity via bundler",{token0:e.token0,token1:e.token1,liquidity:e.liquidity});try{const t=xn(e.liquidity,Number.NaN);if(isNaN(t))throw new wt(`Invalid liquidity value: "${e.liquidity}". Must be a valid number. Position ID: ${null!==e.positionId&&void 0!==e.positionId&&""!==e.positionId?e.positionId:"unknown"}`,"liquidity","INVALID_VALUE");if(0===t)throw new wt(`Cannot remove zero liquidity from position. This would waste gas fees without any effect. Position ID: ${null!==e.positionId&&void 0!==e.positionId&&""!==e.positionId?e.positionId:"unknown"}`,"liquidity","ZERO_VALUE")}catch(e){if(ot(e)&&at(e).includes("Cannot remove zero liquidity"))throw e;if(ot(e)&&at(e).includes("Invalid liquidity value"))throw e;throw e}const t="string"==typeof e.token0?wr(e.token0):e.token0,n="string"==typeof e.token1?wr(e.token1):e.token1;await this.ensureWebSocketConnected();const i=await this.sendRemoveLiquidityToBundler(e.tickLower,e.tickUpper,e.liquidity,t,n,e.fee,null!==e.amount0Min&&void 0!==e.amount0Min&&""!==e.amount0Min?e.amount0Min:"0",null!==e.amount1Min&&void 0!==e.amount1Min&&""!==e.amount1Min?e.amount1Min:"0",null!==e.positionId&&void 0!==e.positionId&&""!==e.positionId?e.positionId:"");this.logger.debug("Liquidity removal submitted to bundler",{transactionId:i});const o=this.webSocketService.waitForTransaction(i);return this.monitorBundlerTransaction(i,o,"liquidity removal")}catch(e){this.handleGSwapError("Failed to remove liquidity",Nt,e)}}async collectPositionFees(e){try{if(void 0===this.privateKey||null===this.privateKey||""===this.privateKey)throw new St("Private key not available for bundler-direct operations","privateKey");if(null!==e.ownerAddress&&void 0!==e.ownerAddress&&""!==e.ownerAddress&&null!==e.positionId&&void 0!==e.positionId&&""!==e.positionId&&(null===e.token0||void 0===e.token0)){this.logger.debug("Fetching position data before collecting fees",{ownerAddress:e.ownerAddress,positionId:e.positionId});const t=await this.getLiquidityPositionById(e.ownerAddress,e.positionId);if(null==t)throw Wt(e.positionId);if(null===t.token0||void 0===t.token0||""===t.token0||null===t.token1||void 0===t.token1||""===t.token1)throw new Nt("Position missing token information",null,"INVALID_DATA");const{gswapToken0:n,gswapToken1:i}=this.convertTokenPair(t.token0,t.token1);return this.collectPositionFees({token0:n,token1:i,fee:t.feeTier,tickLower:t.tickLower,tickUpper:t.tickUpper,amount0Requested:null!==e.amount0Max&&void 0!==e.amount0Max&&""!==e.amount0Max?e.amount0Max:null!==e.amount0Requested&&void 0!==e.amount0Requested&&""!==e.amount0Requested?e.amount0Requested:"0",amount1Requested:null!==e.amount1Max&&void 0!==e.amount1Max&&""!==e.amount1Max?e.amount1Max:null!==e.amount1Requested&&void 0!==e.amount1Requested&&""!==e.amount1Requested?e.amount1Requested:"0",positionId:e.positionId})}if(null===e.token0||void 0===e.token0||null===e.token1||void 0===e.token1||void 0===e.fee||void 0===e.tickLower||void 0===e.tickUpper)throw Ft("parameters","token0, token1, fee, tickLower, tickUpper");this.logger.debug("Collecting position fees via bundler",{token0:"string"==typeof e.token0?e.token0:e.token0?.type??"unknown",token1:"string"==typeof e.token1?e.token1:e.token1?.type??"unknown",tickLower:e.tickLower,tickUpper:e.tickUpper});const t="string"==typeof e.token0?wr(e.token0):e.token0,n="string"==typeof e.token1?wr(e.token1):e.token1;await this.ensureWebSocketConnected();const i=await this.sendCollectPositionFeesToBundler(t,n,e.fee,null!==e.amount0Requested&&void 0!==e.amount0Requested&&""!==e.amount0Requested?e.amount0Requested:"0",null!==e.amount1Requested&&void 0!==e.amount1Requested&&""!==e.amount1Requested?e.amount1Requested:"0",e.tickLower,e.tickUpper,null!==e.positionId&&void 0!==e.positionId&&""!==e.positionId?e.positionId:"");this.logger.debug("Fee collection submitted to bundler",{transactionId:i});const o=this.webSocketService.waitForTransaction(i);return this.monitorBundlerTransaction(i,o,"fee collection")}catch(e){this.handleGSwapError("Failed to collect position fees",Nt,e)}}async getPoolData(e,t,n){try{this.logger.debug("Getting pool data",{tokenA:e,tokenB:t,feeTier:n});const{gswapToken0:i,gswapToken1:o}=this.convertTokenPair(e,t),a=wr(i),r=wr(o),s=await this.gatewayClient.getPoolData({token0:a,token1:r,fee:n}),c=this.calculatePriceFromSqrtPriceX96(po(s.sqrtPrice));return{tokenA:e,tokenB:t,feeTier:n,liquidity:s.liquidity.toString(),sqrtPriceX96:s.sqrtPrice.toString(),tick:s.tick,feeGrowthGlobal0X128:s.feeGrowthGlobal0.toString(),feeGrowthGlobal1X128:s.feeGrowthGlobal1.toString(),currentPrice:c.toFixed()}}catch(e){this.handleGSwapError("Failed to get pool data",Ct,e)}}async calculateDexPoolSpotPrice(e,t,n){try{this.logger.debug("Calculating spot price",{tokenA:e,tokenB:t,feeTier:n});const i=await this.getPoolData(e,t,n),o=po(i.currentPrice);return{tokenA:e,tokenB:t,feeTier:n,price:o.toFixed(),invertedPrice:ko(o,!0),tick:i.tick,liquidity:i.liquidity}}catch(e){this.handleGSwapError("Failed to calculate spot price",Ct,e)}}async calculateOptimalPositionSize(e,t,n,i,o,a,r){try{this.logger.debug("Calculating optimal position size",{tokenA:e,tokenB:t,desiredAmount0:i,desiredAmount1:o});const s=(await this.getPoolData(e,t,n)).tick,c=d.tickToSqrtPrice(a),l=d.tickToSqrtPrice(s),u=d.tickToSqrtPrice(r),h=d.getLiquidityForAmounts(po(i),po(o),c,l,u),g=d.getAmountsForLiquidity(h,l,c,u),m=g[0],p=g[1],f=po(i),y=po(o);return{amount0:m.toFixed(),amount1:p.toFixed(),liquidity:h.toFixed(),ratio:No(m,p).toFixed(),utilizationPercent:{amount0:To(No(m,f),100).toFixed(2),amount1:To(No(p,y),100).toFixed(2)}}}catch(e){this.handleGSwapError("Failed to calculate optimal position size",Nt,e)}}async validatePositionParameters(e,t,n,i,o,a,r){const s=[],c=[];try{this.logger.debug("Validating position parameters",{tokenA:e,tokenB:t,tickLower:i,tickUpper:o});const l=[500,3e3,1e4];l.includes(n)||s.push(`Invalid fee tier: ${n}. Must be one of: ${l.join(", ")}`);const d=this.getTickSpacing(n);let u;i%d!==0&&s.push(`tickLower must be multiple of ${d}`),o%d!==0&&s.push(`tickUpper must be multiple of ${d}`),i>=o&&s.push(`tickLower (${i}) must be less than tickUpper (${o})`);try{u=await this.getPoolData(e,t,n)}catch{return s.push(`Pool not found for ${e}/${t} at fee tier ${n}`),{valid:!1,errors:s,warnings:c,gasEstimate:0}}const h=po(a),g=po(r);if(h.isNaN()||g.isNaN())s.push("Amounts must be valid numbers");else try{Co(h,g)}catch(e){s.push(`Liquidity amounts must be non-negative: ${e.message}`)}const m=u.tick;(m<i||m>o)&&c.push("Position is out of current price range - will not earn fees until price moves into range");po(u.liquidity).lt("1000000")&&c.push("Low pool liquidity - consider higher slippage tolerance");const p=0===s.length?35e4:0;return{valid:0===s.length,errors:s,warnings:c,gasEstimate:p,tickSpacing:d,currentTick:m,poolLiquidity:u.liquidity}}catch(e){const t=at(e);return s.includes(t)||s.push(`Validation failed: ${t}`),{valid:!1,errors:s,warnings:c,gasEstimate:0}}}calculateTicksForPrice(e,t,n,i,o){try{this.logger.debug("Calculating ticks for price range",{tokenA:e,tokenB:t,minPrice:n,maxPrice:i});const a=this.getTickSpacing(o),r=po(n),s=po(i);Wn(n,i,"priceRange");const c=Math.floor(yo(r)),l=Math.ceil(yo(s)),d=Do(c,a),u=Do(l,a),h=Math.pow(1.0001,d),g=Math.pow(1.0001,u),m=po(h),p=po(g);return{tokenA:e,tokenB:t,feeTier:o,tickLower:d,tickUpper:u,tickSpacing:a,requestedMinPrice:n,requestedMaxPrice:i,actualMinPrice:m.toFixed(8),actualMaxPrice:p.toFixed(8),priceDeviation:{minPriceDeviation:To(m.minus(r).dividedBy(r),100).toFixed(4),maxPriceDeviation:To(p.minus(s).dividedBy(s),100).toFixed(4)}}}catch(e){this.handleGSwapError("Failed to calculate ticks for price",Nt,e)}}async calculatePriceForTicks(e,t,n,i){try{this.logger.debug("Calculating price for ticks",{tokenA:e,tokenB:t,tickLower:n,tickUpper:i});const o=Math.pow(1.0001,n),a=Math.pow(1.0001,i);let r;try{r=(await this.getPoolData(e,t,3e3)).currentPrice}catch{}const s=po(o),c=po(a),l={tokenA:e,tokenB:t,tickLower:n,tickUpper:i,minPrice:s.toFixed(8),maxPrice:c.toFixed(8),priceRange:`${s.toFixed(4)} - ${c.toFixed(4)}`,tickSpread:i-n};return Vn(r)||(l.currentPrice=r),l}catch(e){throw this.handleGSwapError("Failed to calculate price for ticks",Nt,e),e}}calculateExecutionPrice(e,t){try{const n=po(e);return No(po(t),n,"0").toFixed()}catch{return"0"}}getTickSpacing(e){switch(e){case 500:return 10;case 3e3:return 60;case 1e4:return 200;default:throw Rt("feeTier","500, 3000, or 10000","Fee tier")}}validateTickSpacing(e,t,n){const i=this.getTickSpacing(n);if(e%i!==0)throw new wt(`Invalid tickLower: ${e} must be a multiple of ${i} for fee tier ${n}. Tip: Use getAllSwapUserLiquidityPositions() to discover valid positions with correct tick spacing.`,"tickLower","INVALID_TICK_SPACING");if(t%i!==0)throw new wt(`Invalid tickUpper: ${t} must be a multiple of ${i} for fee tier ${n}. Tip: Use getAllSwapUserLiquidityPositions() to discover valid positions with correct tick spacing.`,"tickUpper","INVALID_TICK_SPACING")}calculatePriceFromSqrtPriceX96(e){try{const t=new o(2).pow(96);return No(e,t).pow(2)}catch{return po(0)}}calculatePriceFromSqrtPriceDecimal(e){try{return e.pow(2)}catch{return po(0)}}async getPoolSlot0(e,t,n){try{this.logger.debug("Fetching pool slot0 data",{token0:e,token1:t,fee:n});const i="string"==typeof e?wr(e):e,o="string"==typeof t?wr(t):t,a=await this.gatewayClient.getSlot0({token0:i,token1:o,fee:n}),r={sqrtPrice:null!==a.sqrtPrice&&void 0!==a.sqrtPrice&&""!==a.sqrtPrice?a.sqrtPrice:"0",tick:null!==a.tick&&void 0!==a.tick&&0!==a.tick?a.tick:0,liquidity:null!==a.liquidity&&void 0!==a.liquidity&&""!==a.liquidity?a.liquidity:"0",grossPoolLiquidity:null!==a.grossPoolLiquidity&&void 0!==a.grossPoolLiquidity&&""!==a.grossPoolLiquidity?a.grossPoolLiquidity:"0"};return this.logger.debug("Retrieved pool slot0 data",{sqrtPrice:r.sqrtPrice,tick:r.tick,liquidity:r.liquidity}),r}catch(i){this.handleGSwapError("Failed to fetch pool slot0 data",Ct,i,{token0:e,token1:t,fee:n})}}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=po(t.sqrtPrice),i={price:this.calculatePriceFromSqrtPriceDecimal(n).toFixed(18),sqrtPrice:t.sqrtPrice,tick:t.tick,liquidity:t.liquidity};return this.logger.debug("Calculated position current price",{price:i.price,tick:i.tick}),i}catch(t){this.handleGSwapError("Failed to fetch position current price",Ct,t,{token0:e.token0,token1:e.token1})}}_calculateLiquidityFromAmount0(e,t,n){try{const i=d.tickToSqrtPrice(t),o=d.tickToSqrtPrice(n);return d.liquidity0(e,i,o)}catch{return po(0)}}_calculateLiquidityFromAmount1(e,t,n){try{const i=d.tickToSqrtPrice(t),o=d.tickToSqrtPrice(n);return d.liquidity1(e,i,o)}catch{return po(0)}}_calculateAmount0FromLiquidity(e,t,n){try{const i=d.tickToSqrtPrice(t),o=d.tickToSqrtPrice(n);return d.getAmount0Delta(i,o,e)}catch{return po(0)}}_calculateAmount1FromLiquidity(e,t,n){try{const i=d.tickToSqrtPrice(t),o=d.tickToSqrtPrice(n);return d.getAmount1Delta(i,o,e)}catch{return po(0)}}convertTokenPair(e,t){return{gswapToken0:this.tokenConverter.toLaunchpadFormat(e),gswapToken1:this.tokenConverter.toLaunchpadFormat(t)}}async sendAddLiquidityToBundler(e){if(void 0===this.privateKey||null===this.privateKey||""===this.privateKey)throw new St("GSwapService: AddLiquidity requires wallet (full-access mode)","privateKey");if(void 0===this.bundlerBaseUrl||null===this.bundlerBaseUrl||""===this.bundlerBaseUrl)throw new St("GSwapService: Bundler URL not configured","bundlerBaseUrl");try{this.logger.debug("Sending AddLiquidity to bundler",{token0:e.token0?.type??"unknown",token1:e.token1?.type??"unknown",fee:e.fee,tickRange:`${e.tickLower}-${e.tickUpper}`});const n=`galaswap - operation - ${i.v4()}-${Date.now()}-${e.owner}`,o={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:n},a=new t.ethers.Wallet(this.privateKey),r={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},c=this.calculatePersonalSignPrefix(o),l={...o,prefix:c},d=await a.signTypedData(s,r,l),u={...l,signature:d,types:r,domain:s};this.logger.debug("AddLiquidity DTO signed with manual types",{prefix:u.prefix,tickLower:o.tickLower,tickUpper:o.tickUpper});const h=this.buildLiquidityStringsInstructions(e.token0,e.token1,e.fee,e.owner),g=Uc.createClient(this.bundlerBaseUrl,3e4),m=await g.post("/bundle",{method:"AddLiquidity",signedDto:u,stringsInstructions:h}),p=Vt(m),f=p?.data??p?.transactionId??p?.id;if(null==f)throw this.logger.error("Bundler response structure",{status:m.status,dataType:typeof p}),new At("Bundler response does not contain transaction ID. Response type: "+typeof p,void 0,"INVALID_RESPONSE");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,n,o,a,r,s,c,l,d){try{if(void 0===this.bundlerBaseUrl||null===this.bundlerBaseUrl||""===this.bundlerBaseUrl)throw new St("GSwapService: Bundler URL not configured","bundlerBaseUrl");const u=new t.ethers.Wallet(this.privateKey),h=await u.getAddress(),g=`galaswap - operation - ${i.v4()}-${Date.now()}-${h}`,m={tickLower:e,tickUpper:n,amount:o,token0:a,token1:r,fee:s,amount0Min:c,amount1Min:l,positionId:d,uniqueKey:g},p={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"}]},f={name:"ethereum",chainId:1},y=this.calculatePersonalSignPrefix(m),k={...m,prefix:y},v=await u.signTypedData(f,p,k),w={...k,signature:v,types:p,domain:f},b=this.buildLiquidityStringsInstructions(a,r,s,h);this.logger.debug("Submitting RemoveLiquidity to bundler",{tickLower:e,tickUpper:n,amount:o,fee:s,positionId:d,transactionId:g});const S=Uc.createClient(this.bundlerBaseUrl,3e4),A=await S.post("/bundle",{method:"RemoveLiquidity",signedDto:w,stringsInstructions:b}),T=Vt(A),E=T?.data??T?.transactionId??T?.id;if(null==E)throw this.logger.error("Bundler response structure",{status:A.status,dataType:typeof T}),new At("Bundler response does not contain transaction ID. Response type: "+typeof T,void 0,"INVALID_RESPONSE");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,n,o,a,r,s,c,l){try{if(void 0===this.bundlerBaseUrl||null===this.bundlerBaseUrl||""===this.bundlerBaseUrl)throw new St("GSwapService: Bundler URL not configured","bundlerBaseUrl");const d=new t.ethers.Wallet(this.privateKey),u=await d.getAddress(),h=`galaswap - operation - ${i.v4()}-${Date.now()}-${u}`,g={token0:e,token1:n,fee:o,amount0Requested:a,amount1Requested:r,tickLower:s,tickUpper:c,positionId:l,uniqueKey:h},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"}]},p={name:"ethereum",chainId:1},f=this.calculatePersonalSignPrefix(g),y={...g,prefix:f},k=await d.signTypedData(p,m,y),v={...y,signature:k,types:m,domain:p},w=this.buildLiquidityStringsInstructions(e,n,o,u);this.logger.debug("Submitting CollectPositionFees to bundler",{fee:o,amount0Requested:a,amount1Requested:r,tickLower:s,tickUpper:c,positionId:l,transactionId:h});const b=Uc.createClient(this.bundlerBaseUrl,3e4),S=await b.post("/bundle",{method:"CollectPositionFees",signedDto:v,stringsInstructions:w}),A=Vt(S),T=A?.data??A?.transactionId??A?.id;if(null==T)throw this.logger.error("Bundler response structure",{status:S.status,dataType:typeof A}),new At("Bundler response does not contain transaction ID. Response type: "+typeof A,void 0,"INVALID_RESPONSE");return this.logger.info("CollectPositionFees transaction sent to bundler",{transactionId:T}),T}catch(e){throw this.logger.error("Failed to send CollectPositionFees to bundler",e),e}}async sendSwapToBundler(e){if(void 0===this.privateKey||null===this.privateKey||""===this.privateKey)throw new St("GSwapService: Swap requires wallet (full-access mode)","privateKey");if(void 0===this.bundlerBaseUrl||null===this.bundlerBaseUrl||""===this.bundlerBaseUrl)throw new St("GSwapService: Bundler URL not configured","bundlerBaseUrl");const n=[500,3e3,1e4];if(!n.includes(e.feeTier))throw new wt(`GSwapService: Invalid fee tier ${e.feeTier}. Must be one of: ${n.join(", ")} (basis points)`,"feeTier","INVALID_FEE_TIER");try{this.logger.debug("Sending Swap to bundler",{fromToken:"string"==typeof e.fromToken?e.fromToken:e.fromToken?.type??"unknown",toToken:"string"==typeof e.toToken?e.toToken:e.toToken?.type??"unknown",inputAmount:e.inputAmount,minOutput:e.minOutput,feeTier:e.feeTier});let n=e.fromToken,a=e.toToken;"string"==typeof n&&(n=wr(n)),"string"==typeof a&&(a=wr(a));const r=ca(n),s=ca(a),c=r<s?[n,a,r,s]:[a,n,s,r],[l,d,u,h]=c,g=ca("string"==typeof e.fromToken?wr(e.fromToken):e.fromToken),m=g===u,p=`galaswap - operation - ${i.v4()}-${Date.now()}-${e.walletAddress}`;let f;if(void 0===e.currentSqrtPrice||null===e.currentSqrtPrice||""===e.currentSqrtPrice)throw new wt("GSwapService: currentSqrtPrice is required for sqrtPriceLimit calculation","currentSqrtPrice",mt);const y=po(e.currentSqrtPrice),k=e.slippageTolerance??.01;if(m){const e=function(e=.01){return new o(1).minus(e)}(k);f=y.multipliedBy(e).toString()}else{const e=function(e=.01){return new o(1).plus(e)}(k);f=y.multipliedBy(e).toString()}this.logger.debug("Calculated sqrtPriceLimit based on slippage tolerance",{currentSqrtPrice:e.currentSqrtPrice,slippageTolerance:100*k+"%",zeroForOne:m,sqrtPriceLimit:f,direction:m?"token0→token1 (downward price movement)":"token1→token0 (upward price movement)",reason:"sqrtPriceLimit sets price boundaries, amountOutMinimum provides volume protection"});const v={token0:l,token1:d,fee:e.feeTier,amount:po(e.inputAmount).toFixed(),zeroForOne:m,sqrtPriceLimit:f,recipient:e.walletAddress,amountOutMinimum:po(e.minOutput).multipliedBy(-1).toFixed(),uniqueKey:p};this.logger.info("🔄 SWAP DTO DETAILS (what we're sending to bundler)",{orderedToken0String:u,orderedToken1String:h,fromTokenStr:g,zeroForOne:m?`TRUE (${u} → ${h})`:`FALSE (${h} → ${u})`,inputAmount:e.inputAmount,expectedOutput:e.minOutput,slippageTolerance:100*(null!==e.slippageTolerance&&void 0!==e.slippageTolerance&&0!==e.slippageTolerance?e.slippageTolerance:.01)+"%",currentSqrtPrice:e.currentSqrtPrice,swapDto:{amount:v.amount,zeroForOne:v.zeroForOne,sqrtPriceLimit:v.sqrtPriceLimit,amountOutMinimum:v.amountOutMinimum}});const w=new t.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"}]},S={name:"ethereum",chainId:1},A=this.calculatePersonalSignPrefix(v),T={...v,prefix:A},E=await w.signTypedData(S,b,T),C={...T,signature:E,types:b,domain:S};this.logger.debug("Swap DTO signed",{prefix:C.prefix,zeroForOne:v.zeroForOne});const I=this.buildLiquidityStringsInstructions(l,d,e.feeTier,e.walletAddress),N=Uc.createClient(this.bundlerBaseUrl,3e4),D=await N.post("/bundle",{method:"Swap",signedDto:C,stringsInstructions:I}),P=Vt(D),x=P?.data??P?.transactionId??P?.id;if(null==x)throw this.logger.error("Bundler response structure",{status:D.status,dataType:typeof P}),new At("Bundler response does not contain transaction ID. Response type: "+typeof P,void 0,"INVALID_RESPONSE");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,i){const o=da(e),a=da(t),r=`$pool${o}${a}$${n}`;return[r,`$userPosition${i}`,`$tokenBalance${o}${i}`,`$tokenBalance${a}${i}`,`$tokenBalance${o}${r}`,`$tokenBalance${a}${r}`]}createGSwapErrorHandler(e,t){return(n,i,o)=>{this.handleGSwapError(i,e,n,t)}}handleGSwapError(e,t,n,i){this.logger.error(e,n);const o=this.extractGSwapErrorCode(n),a=n,r=[`${e}: ${a?.message??at(n)}`,n],s=t.name;throw i&&("GSwapSwapError"===s&&null!==i.transactionHash&&void 0!==i.transactionHash&&r.push(i.transactionHash),"GSwapPoolError"===s&&(null!==i.tokenA&&void 0!==i.tokenA&&r.push(i.tokenA),null!==i.tokenB&&void 0!==i.tokenB&&r.push(i.tokenB)),"GSwapAssetError"===s&&null!==i.walletAddress&&void 0!==i.walletAddress&&r.push(i.walletAddress)),null!=o&&""!==o&&r.push(o),new t(...r)}extractGSwapErrorCode(e){const t=st(e);return void 0!==t?String(t):void 0}async ensureWebSocketConnected(){this.webSocketService.isConnected()||await this.webSocketService.connect()}calculatePersonalSignPrefix(e){return`Ethereum Signed Message:\n${JSON.stringify(e).length}${JSON.stringify(e)}`}}function Wc(e,t="image",n){const i=new FormData;if("undefined"!=typeof File&&e instanceof File)i.append(t,e);else{if(!Buffer.isBuffer(e))throw Rt("file","a File object (browser) or Buffer (Node.js)");{const o=new Blob([e],{type:"image/png"});i.append(t,o,n)}}return i}class Hc extends ri{constructor(e,t,n=!1){super(e,n,t)}async uploadImageByTokenName(e){const{tokenName:t,options:n}=e;Mo(t);const i=`${t}.png`;_o(n.file,i,"image/png");try{const e=`${n.tokenName??t}.png`,i=Wc(n.file,"image",e),o=await Xt(()=>this.http.request({method:"POST",url:`${g.UPLOAD_IMAGE}?tokenName=${encodeURIComponent(n.tokenName??t)}`,data:i,headers:this.getJwtHeaders()}),{errorContext:"Image upload failed"});return"string"==typeof o?o:""}catch(e){if(e instanceof St)throw e;if(ot(e)&&at(e).includes("FormData"))throw Ut("File upload failed: FormData not supported in this environment. Ensure you have proper polyfills for Node.js environments.","FormData");throw e}}}const zc={CHAT_MESSAGE:"CHAT_MESSAGE",COMMENT:"COMMENT"};function jc(e,t){if(null==e)return[];if(Array.isArray(e))return e;const n=e[t];return Array.isArray(n)?n:[]}class Vc extends ri{constructor(e,t,n=!1){super(e,n,t)}buildPoolFilters(e){return{...!Vn(e.search)&&{search:e.search},...!Vn(e.tokenName)&&{tokenName:e.tokenName},...!Vn(e.type)&&{type:e.type},...!Vn(e.hasUpcomingShows)&&{hasUpcomingShows:e.hasUpcomingShows},...!Vn(e.language)&&{language:e.language},...!Vn(e.recentlyStreamed)&&{recentlyStreamed:e.recentlyStreamed},...!Vn(e.hasRecordings)&&{hasRecordings:e.hasRecordings},...!Vn(e.streamStatus)&&{streamStatus:e.streamStatus}}}async fetchSinglePage(e){const t=Math.min(e.pageSize,20),n=e.cursor,i={pageSize:String(t)};void 0!==n&&(i.cursor=n),Vn(e.type)||(i.type=e.type),Vn(e.tokenName)||(i.tokenName=e.tokenName),Vn(e.search)||(i.search=e.search),Vn(e.hasUpcomingShows)||(i.hasUpcomingShows=e.hasUpcomingShows.toString()),Vn(e.language)||(i.language=e.language),Vn(e.recentlyStreamed)||(i.recentlyStreamed=e.recentlyStreamed.toString()),Vn(e.hasRecordings)||(i.hasRecordings=e.hasRecordings.toString()),Vn(e.streamStatus)||(i.streamStatus=e.streamStatus);const o=mo(i),a=Vt(await Yt(()=>this.http.get(g.FETCH_POOLS,o),{errorContext:"Failed to fetch pools"})),r=function(e){if(!e)return[];let t=[];if(e.tokens)if(Array.isArray(e.tokens))t=e.tokens.map(e=>({...e,createdAt:e.created_at??e.createdAt??""}));else{const n=e.tokens;t=[{...n,createdAt:n.created_at??n.createdAt??""}]}else null!==e.pools&&void 0!==e.pools&&Array.isArray(e.pools)&&(t=e.pools.map(e=>({...e,createdAt:e.created_at??e.createdAt??""})));return t}(a),s={hasNextPage:a.hasNextPage??!1},c=a.count??a.total;return void 0!==c&&(s.totalCount=c),void 0!==a.nextCursor&&null!==a.nextCursor&&(s.nextCursor=a.nextCursor),void 0!==a.prevCursor&&null!==a.prevCursor&&(s.prevCursor=a.prevCursor),void 0!==a.hasPrevPage&&(s.hasPrevPage=a.hasPrevPage),{items:r,pageInfo:s}}async fetchPools(e){const t=e?.cursor,n=e?.pageSize??zr.DEFAULT_LIMIT;jn(e?.tokenName)&&Mo(e.tokenName);const i=void 0!==e?.type?{recent:"RECENT",popular:"POPULAR",recordings:"RECORDINGS",seconds_streamed:"SECONDS_STREAMED"}[e.type]:void 0,o=void 0!==e?.streamStatus?{idle:"IDLE",active:"ACTIVE",disabled:"DISABLED"}[e.streamStatus]:void 0,a={...this.buildPoolFilters({search:e?.search,tokenName:e?.tokenName,type:i,hasUpcomingShows:e?.hasUpcomingShows,language:e?.language,recentlyStreamed:e?.recentlyStreamed,hasRecordings:e?.hasRecordings,streamStatus:o}),pageSize:n,...void 0!==t&&{cursor:t}},r=await this.fetchSinglePage(a);return{items:r.items,pageInfo:r.pageInfo}}async fetchAllPools(e){const t=[];let n;const i=zr.BACKEND_MAX_PAGE_SIZE;for(;;){const o={...e,pageSize:i,...void 0!==n&&{cursor:n}},a=await this.fetchPools(o);if(t.push(...a.items),!a.pageInfo.hasNextPage)break;n=a.pageInfo.nextCursor}return{items:t,pageInfo:{hasNextPage:!1,totalCount:t.length}}}async checkPool(e){$o(e),jn(e.tokenName)&&Mo(e.tokenName);try{const t=await this.http.post(g.VALIDATE_TOKEN,{tokenName:e.tokenName,symbol:e.symbol});if(null==t)throw new Error("Invalid response from check pool endpoint");const n=t.data??t;if(null==n)throw new Error("Invalid response from check pool endpoint");if(!0===jn(e.symbol)){return!n.symbolAvailable}if(!0===jn(e.tokenName)){return!n.nameAvailable}return!1}catch(e){const t=String(e),n=e,i="string"==typeof n.message?n.message:"";if(t.includes("404")||t.includes("Pool not found")||i.includes("404")||i.includes("Pool not found"))return!1;const o="number"==typeof n.statusCode?n.statusCode:null;if(404===o)return!1;if(t.includes("400")||t.includes("Missing required pool identifier")||i.includes("400")||i.includes("Missing required pool identifier"))return!1;if(400===o&&i.includes("Missing required pool identifier"))return!1;throw e}}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(!co(e))throw new wt("Invalid options provided. Expected { tokenName: string, from?: number, to?: number, resolution?: number }","options","INVALID_OPTIONS");const{tokenName:t,from:n,to:i,resolution:o}=e;if(Mo(t),null==n||null==i||null==o)throw new wt("Graph options (from, to, resolution) are required","options","MISSING_GRAPH_OPTIONS");const a={tokenName:t,from:n,to:i,resolution:o};Ko(a);const r=mo(a);return{dataPoints:await Xt(()=>this.http.get(g.CHART,r),{errorContext:"Failed to fetch graph data"})}}async fetchTokenDistribution(e){if(""===e||null==e)throw Ft("tokenName","Token name");let t;Mo(e);try{t=await Yt(()=>this.http.get(`${g.GET_TOKEN_DISTRIBUTION}?tokenName=${e}`),{errorContext:"Failed to fetch token distribution"})}catch(t){if(ct(t)&&500===t.response?.status)throw Bt(`Token distribution data temporarily unavailable for ${e}. This is a backend issue - please try again later.`,500);throw t}const n=Vt(t);if(!Array.isArray(n))throw Bt("Invalid API response: expected array of holders",t.status);for(const e of n){if(""===e.owner||null===e.owner||void 0===e.owner||"string"!=typeof e.owner)throw Bt("Invalid holder data: missing or invalid owner field",t.status);if(""===e.quantity||null===e.quantity||void 0===e.quantity||"string"!=typeof e.quantity)throw Bt("Invalid holder data: missing or invalid quantity field",t.status);const n=xn(e.quantity,NaN);if(isNaN(n)||!isFinite(n))throw Bt(`Invalid holder quantity: "${e.quantity}"`,t.status)}const i=n.reduce((e,t)=>e.plus(t.quantity),po(0));return{holders:n.map(e=>{const t=No(po(e.quantity),i,po(0)).multipliedBy(100).toNumber();return{address:e.owner,balance:e.quantity,percentage:t}}),totalSupply:i.toFixed(),totalHolders:n.length,lastUpdated:new Date}}async fetchUserHolderContext(e,t){if(""===e||null==e)throw Ft("tokenName","Token name");if(""===t||null==t)throw Ft("userAddress","User address");return Mo(e),Xt(()=>this.http.get(`${g.GET_TOKEN_DISTRIBUTION}?tokenName=${e}`,{userAddress:t}),{errorContext:"Failed to fetch user holder context"})}async fetchTokenBadges(e){if(""===e||null==e)throw Ft("tokenName","Token name");Mo(e);const t=g.GET_TOKEN_BADGES.replace(":tokenName",e.toLowerCase().trim()),n=await Xt(()=>this.http.get(t),{errorContext:"Failed to fetch token badges"});return{volumeBadges:n.volumeBadge??[],engagementBadges:n.engagementBadge??[]}}async hasTokenBadge(e){const{tokenName:t,badgeType:n,badgeName:i}=e;try{const e=await this.fetchTokenBadges(t);if(null==e)return!1;const o=("volume"===n?e.volumeBadges:e.engagementBadges).find(e=>e.badgeName===i);return o?.isActive??!1}catch{return!1}}async resolveTokenNameToVault(e){try{const t=await this.fetchPools({tokenName:e});if(null!==t.items&&void 0!==t.items&&Array.isArray(t.items)&&t.items.length>0)return t.items[0].vaultAddress??null;if(null!==t.items&&void 0!==t.items&&"object"==typeof t.items){const e=t.items.tokens;return e?.vaultAddress??null}return null}catch{return null}}async fetchToken(e){if(null==e||"string"!=typeof e||""===e.trim())throw new Error("Token name is required and must be a non-empty string");Mo(e);try{const t=g.GET_TOKEN.replace(":tokenName",e.toLowerCase().trim()),n=Vt(await this.http.get(t));if(null!=n?.tokens){const t=Array.isArray(n.tokens)?n.tokens[0]:n.tokens;if(null==t)throw new Error(`Token not found: ${e}`);return t}throw new Error(`Invalid response format when fetching token: ${e}`)}catch(t){const n=String(t);if(n.includes("404")||n.includes("not found"))throw new Error(`Token not found: ${e}`);throw t}}async validateToken(e){if(!(null!==e.tokenName&&void 0!==e.tokenName||null!==e.symbol&&void 0!==e.symbol))throw new Error("At least one of tokenName or symbol is required");if(null!==e.tokenName&&void 0!==e.tokenName&&("string"!=typeof e.tokenName||""===e.tokenName.trim()))throw new Error("tokenName must be a non-empty string");if(null!==e.symbol&&void 0!==e.symbol&&("string"!=typeof e.symbol||""===e.symbol.trim()))throw new Error("symbol must be a non-empty string");null!==e.tokenName&&void 0!==e.tokenName&&Mo(e.tokenName);try{const t=await this.http.post(g.VALIDATE_TOKEN,{tokenName:e.tokenName?.toLowerCase().trim(),symbol:e.symbol?.toUpperCase().trim()});if(null==t)throw new Error("Invalid response from validation endpoint");const n=t.data??t;if(null==n)throw new Error("Invalid response from validation endpoint");return n}catch(e){throw this.logger.warn(`Token validation failed: ${String(e)}`),e}}async fetchTokenStats(e){if(!jn(e))throw Ft("tokenName","Token name");Mo(e);const t=g.TOKEN_STATS.replace(":tokenName",e.toLowerCase().trim());try{const e=Vt(await this.http.get(t));if(!e?.stats)throw new Error("Invalid response: missing stats data");return{success:!0,data:e.stats}}catch(t){throw this.logger.warn(`Failed to fetch token stats for ${e}: ${String(t)}`),t}}async updateTokenConfig(e,t){if(""===e||null==e)throw Ft("tokenName","Token name");Mo(e),function(e){if(void 0===e.messagesEnabled&&void 0===e.commentsEnabled&&void 0===e.streamingEnabled&&void 0===e.nextLiveStreamAt&&void 0===e.streamLanguage&&void 0===e.websiteUrl&&void 0===e.telegramUrl&&void 0===e.twitterUrl&&void 0===e.instagramUrl&&void 0===e.facebookUrl&&void 0===e.redditUrl&&void 0===e.tiktokUrl)throw new wt("At least one config option must be provided (messagesEnabled, commentsEnabled, streamingEnabled, nextLiveStreamAt, streamLanguage, or social URLs)","options",mt);if(void 0!==e.messagesEnabled&&"boolean"!=typeof e.messagesEnabled)throw qt("messagesEnabled","boolean");if(void 0!==e.commentsEnabled&&"boolean"!=typeof e.commentsEnabled)throw qt("commentsEnabled","boolean");if(void 0!==e.streamingEnabled&&"boolean"!=typeof e.streamingEnabled)throw qt("streamingEnabled","boolean");if(void 0!==e.streamLanguage&&"string"!=typeof e.streamLanguage)throw qt("streamLanguage","string");const t=["websiteUrl","telegramUrl","twitterUrl","instagramUrl","facebookUrl","redditUrl","tiktokUrl"];for(const n of t){const t=e[n];if(null!=t&&"string"!=typeof t)throw qt(n,"string or null")}if(void 0!==e.nextLiveStreamAt&&null!==e.nextLiveStreamAt){if("string"!=typeof e.nextLiveStreamAt)throw qt("nextLiveStreamAt","string or null");const t=new Date(e.nextLiveStreamAt);if(isNaN(t.getTime()))throw new wt("nextLiveStreamAt must be a valid ISO date string","nextLiveStreamAt",yt)}}(t);const n=g.UPDATE_TOKEN_CONFIG.replace(":tokenName",e.toLowerCase().trim()),i={};void 0!==t.messagesEnabled&&(i.messagesEnabled=t.messagesEnabled),void 0!==t.commentsEnabled&&(i.commentsEnabled=t.commentsEnabled),void 0!==t.streamingEnabled&&(i.streamingEnabled=t.streamingEnabled),void 0!==t.nextLiveStreamAt&&(i.nextLiveStreamAt=t.nextLiveStreamAt),void 0!==t.streamLanguage&&(i.streamLanguage=t.streamLanguage),void 0!==t.websiteUrl&&(i.websiteUrl=t.websiteUrl),void 0!==t.telegramUrl&&(i.telegramUrl=t.telegramUrl),void 0!==t.twitterUrl&&(i.twitterUrl=t.twitterUrl),void 0!==t.instagramUrl&&(i.instagramUrl=t.instagramUrl),void 0!==t.facebookUrl&&(i.facebookUrl=t.facebookUrl),void 0!==t.redditUrl&&(i.redditUrl=t.redditUrl),void 0!==t.tiktokUrl&&(i.tiktokUrl=t.tiktokUrl),await this.http.patch(n,i,this.getJwtHeaders())}}const Qc=yn,Xc=mn,Jc={MAX_LENGTH:100};class Yc extends ri{constructor(e,t=!1){super(e,t)}async fetchTrades(e){if(!function(e){if(null==e||"object"!=typeof e)return!1;const t=e;return ao(t,"tokenName")&&(void 0===t.tradeType||"buy"===t.tradeType||"sell"===t.tradeType)&&ro(t,"userAddress")&&so(t,"offset")&&so(t,"pageSize")&&so(t,"page")&&so(t,"limit")}(e))throw new wt("Invalid options provided. Expected { tokenName: string, tradeType?: string, userAddress?: string, cursor?: string, pageSize?: number, startDate?: Date, endDate?: Date, sortOrder?: string }","options","INVALID_OPTIONS");const{tokenName:t,tradeType:n,userAddress:i,cursor:o,pageSize:a=zr.DEFAULT_LIMIT,startDate:r,endDate:s,sortOrder:c}=e;if(!jn(t))throw Ft("tokenName","Token name");const l={pageSize:String(Math.min(a,fn))};void 0!==o&&(l.cursor=o);const d={...l};d.tokenName=t;const u=await this.http.get(g.GET_TRADES,d);if(null==u)throw new wt("No response from trade service","response","NO_RESPONSE");const h=Vt(u),m=jc(h,"trades"),p=h?.pageInfo;return{items:m,pageInfo:p??{hasNextPage:!1}}}buildTradesQueryParams(e){const t={};null!==e.tokenName&&void 0!==e.tokenName&&e.tokenName.length>0&&(t.tokenName=en(e.tokenName)),null!==e.userAddress&&void 0!==e.userAddress&&e.userAddress.length>0&&(t.userAddress=e.userAddress),null!==e.search&&void 0!==e.search&&e.search.length>0&&(t.search=e.search);const n=void 0!==e.pageSize?jr(e.pageSize,1,Qc.MAX_LIMIT):Qc.MAX_LIMIT;return void 0!==e.cursor&&(t.cursor=e.cursor),t.pageSize=String(n),null!==e.txnType&&void 0!==e.txnType&&e.txnType.length>0&&(t.txnType=e.txnType),null!==e.startDate&&void 0!==e.startDate&&e.startDate.length>0&&(t.startDate=e.startDate),null!==e.endDate&&void 0!==e.endDate&&e.endDate.length>0&&(t.endDate=e.endDate),void 0!==e.minAmount&&(t.minAmount=String(e.minAmount)),void 0!==e.maxAmount&&(t.maxAmount=String(e.maxAmount)),t}async getTrades(e){!function(e){if(void 0!==e.tokenName){if(!jn(e.tokenName))throw qt("tokenName","a non-empty string",typeof e.tokenName);if(e.tokenName.length>Xc.MAX_LENGTH)throw Kt("tokenName",Xc.MAX_LENGTH,e.tokenName.length)}if(void 0!==e.userAddress){if(!jn(e.userAddress))throw qt("userAddress","a non-empty string",typeof e.userAddress);if(e.userAddress.length>Jc.MAX_LENGTH)throw Kt("userAddress",Jc.MAX_LENGTH,e.userAddress.length)}if(void 0!==e.search){if("string"!=typeof e.search)throw qt("search","a string",typeof e.search);if(e.search.length>256)throw Kt("search",256,e.search.length)}if(Qn(0,void 0,Qc.MAX_LIMIT,e.pageSize),void 0!==e.txnType&&"BUY"!==e.txnType&&"SELL"!==e.txnType)throw Rt("txnType",'"BUY" or "SELL"');if(void 0!==e.startDate){if(!jn(e.startDate))throw qt("startDate","a non-empty string",typeof e.startDate);if(isNaN(Date.parse(e.startDate)))throw Rt("startDate","a valid ISO 8601 date")}if(void 0!==e.endDate){if(!jn(e.endDate))throw qt("endDate","a non-empty string",typeof e.endDate);if(isNaN(Date.parse(e.endDate)))throw Rt("endDate","a valid ISO 8601 date")}if(void 0!==e.minAmount)try{Kn(e.minAmount,0,Number.MAX_SAFE_INTEGER,"minAmount")}catch{throw Rt("minAmount","a non-negative number")}if(void 0!==e.maxAmount)try{Kn(e.maxAmount,0,Number.MAX_SAFE_INTEGER,"maxAmount")}catch{throw Rt("maxAmount","a non-negative number")}void 0!==e.minAmount&&void 0!==e.maxAmount&&Wn(e.minAmount,e.maxAmount,"amountRange")}(e);const t=this.buildTradesQueryParams(e);return Xt(()=>this.http.get(_e,t),{errorContext:"Failed to fetch trades"})}}function Zc(e){return!!jn(e)&&kn.PATTERN.test(e)}Xn({ALL:"all",DEFI:"DEFI",ASSET:"ASSET",COLLECTION:"COLLECTION",CREATED:"created"});class el extends ri{constructor(e,t,n=!1){super(e,n,t)}async fetchProfile(e){const t=e??this.http.getAddress();if(""===t||null==t||!Zc(t))throw Rt("address","eth|[40-hex-chars] or client|[identifier]","Address");const n={userAddress:t},i=await this.http.get(g.GET_PROFILE,n);if(null==i)throw new wt("No response from user service","response","NO_RESPONSE");return i}async updateProfile(e){this.validateUpdateProfileData(e);let t=e.profileImage;if(!jn(t))try{const n=await this.fetchProfile(e.address);t=n.data?.profileImage??""}catch{t=""}const n={profileImage:t,fullName:e.fullName};await Xt(()=>this.http.put(g.UPDATE_PROFILE,n,this.getJwtHeaders()),{errorContext:"Profile update failed"})}async uploadProfileImage(e){this.validateUploadProfileImageOptions(e);const t=e.address??this.http.getAddress();if(null==t||""===t)throw new wt("Wallet address not available - wallet not configured","address","NO_WALLET");try{const n=`profile-image-${t}.png`,i=Wc(e.file,"image",n),o=await Xt(()=>this.http.request({method:"POST",url:`${g.UPLOAD_IMAGE}?tokenName=${encodeURIComponent(t)}`,data:i,headers:this.getJwtHeaders()}),{errorContext:"Image upload failed"});return"string"==typeof o?o:""}catch(e){if(e instanceof wt||e instanceof St)throw e;throw new wt(`Profile image upload failed: ${at(e)}`,"file","UPLOAD_FAILED")}}async fetchTokenList(e){return this.buildFetchRequest(g.GET_TOKEN_LIST,e,{includeType:!0,errorMessage:"Failed to fetch token list"})}async fetchTokensHeld(e){return this.buildFetchRequest(g.GET_TOKENS_HELD,e,{includeType:!1,errorMessage:"Failed to fetch tokens held"})}async fetchTokensCreated(e){const{cursor:t,pageSize:n,search:i,tokenName:o,status:a}=e??{},r=e?.type??(void 0!==a?"created":"DEFI"),s=this.http.getAddress();if(null==s||""===s)throw new wt("Wallet address not available - wallet not configured","address","NO_WALLET");const c={type:r,address:s,pageSize:n??zr.DEFAULT_LIMIT};return void 0!==t&&(c.cursor=t),void 0!==i&&(c.search=i),void 0!==o&&(c.tokenName=o),void 0!==a&&(c.status=a),this.fetchTokenList(c)}async getManagedTokens(e={}){const t=Qr({pageSize:e.pageSize??zr.DEFAULT_LIMIT,...void 0!==e.cursor&&{cursor:e.cursor}},fn);return Xt(()=>this.http.get(g.GET_MANAGED_TOKENS,t,this.getJwtHeaders()),{errorContext:"Failed to fetch managed tokens"})}async buildFetchRequest(e,t,n){this.validateGetTokenListOptions(t);const i=Qr({pageSize:t.pageSize??zr.DEFAULT_LIMIT,...void 0!==t.cursor&&{cursor:t.cursor}},fn);Xr(i,t,["address","search","tokenName","status"]),n.includeType&&!e.includes("?type=")&&(i.type="all"===(t.type??"DEFI")?"DEFI":t.type??"DEFI");let o=e;if(e.includes(":address")){const n=t.address??this.http.getAddress();if(0===n.length)throw Ft("address","User address");o=e.replace(":address",n),delete i.address}const a=Vt(await Yt(()=>this.http.get(o,i),{errorContext:n.errorMessage})),r=jc(a,"token"),s={hasNextPage:a.hasNextPage??!1},c=a.count??a.total;void 0!==c&&(s.totalCount=c),void 0!==a.nextCursor&&(s.nextCursor=a.nextCursor),void 0!==a.prevCursor&&(s.prevCursor=a.prevCursor),void 0!==a.hasPrevPage&&(s.hasPrevPage=a.hasPrevPage);const l={tokens:r,pageInfo:s};return void 0!==c&&(l.total=c),l}async fetchUserBalances(e){if(!Zc(e.address))throw Rt("address","eth|[40-hex-chars] or client|[identifier]","Address");const t=Qr({pageSize:e.pageSize??zr.DEFAULT_LIMIT,...void 0!==e.cursor&&{cursor:e.cursor}},fn),n=g.USER_BALANCES.replace(":address",e.address),i=Vt(await Yt(()=>this.http.get(n,t),{errorContext:"Failed to fetch user balances"}));if(!i)throw new wt("Invalid response: missing data","response","NO_DATA");const o={hasNextPage:i.hasNextPage??!1};return void 0!==i.nextCursor&&(o.nextCursor=i.nextCursor),void 0!==i.prevCursor&&(o.prevCursor=i.prevCursor),void 0!==i.hasPrevPage&&(o.hasPrevPage=i.hasPrevPage),void 0!==i.total&&(o.totalCount=i.total),{balances:Array.isArray(i.balances)?i.balances:[],pageInfo:o,total:i.total}}async fetchUserReport(e){if(!Zc(e.address))throw Rt("address","eth|[40-hex-chars] or client|[identifier]","Address");if(!this.jwtAuth)throw new St("JWT authentication is required for fetchUserReport. Call sdk.login() first.","jwtAuth");const t=g.USER_REPORT.replace(":address",e.address),n=Vt(await Yt(()=>this.http.get(t,{},this.getJwtHeaders()),{errorContext:"Failed to fetch user report"}));if(!n?.report)throw new wt("Invalid response: missing report data","response","NO_DATA");return{report:n.report}}validateGetTokenListOptions(e){const t=e.pageSize??zr.DEFAULT_LIMIT;if(t<1||t>fn)throw new wt(`pageSize must be between 1 and ${fn}`,"pageSize","OUT_OF_RANGE");if(void 0!==e.address&&!Zc(e.address))throw Rt("address","eth|[40-hex-chars] or client|[identifier]","Address");const n=Fo(e.search);if(null!==n&&!(jn(i=n)&&i.length>=In&&i.length<=Nn))throw Mt("search",In,Nn,n.length,"Search query");var i;const o=Fo(e.tokenName);if(null!==o&&!(jn(a=o)&&a.length>=mn.MIN_LENGTH&&a.length<=mn.MAX_LENGTH))throw Mt("tokenName",mn.MIN_LENGTH,mn.MAX_LENGTH,o.length,"Token name");var a}validateUpdateProfileData(e){if(!Zc(e.address))throw Rt("address","eth|[40-hex-chars] or client|[identifier]","Address");if(t=e.fullName,!(jn(t)&&t.length>=Dn.FULL_NAME.MIN_LENGTH&&t.length<=Dn.FULL_NAME.MAX_LENGTH&&Dn.FULL_NAME.ALPHABETS_ONLY_PATTERN.test(t)))throw Mt("fullName",Dn.FULL_NAME.MIN_LENGTH,Dn.FULL_NAME.MAX_LENGTH,e.fullName.length,"Full name");var t}validateUploadProfileImageOptions(e){if(void 0!==e.address&&!Zc(e.address))throw Rt("address","eth|[40-hex-chars] or client|[identifier]","Address")}}class tl{constructor(e,t,n=!1){this.http=e,this.poolService=new Vc(e,t,n),this.tradeService=new Yc(e,n),this.userService=new el(e,t,n),this.imageService=new Hc(e,t,n)}setJwtAuth(e){this.poolService.setJwtAuth(e),this.userService.setJwtAuth(e),this.imageService.setJwtAuth(e)}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 checkPoolExists(e,t){const n={};return void 0!==e&&(n.tokenName=e),void 0!==t&&(n.symbol=t),this.poolService.checkPool(n)}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 fetchUserHolderContext(e,t){return this.poolService.fetchUserHolderContext(e,t)}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 getTrades(e){return this.tradeService.getTrades(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 getManagedTokens(e={}){return this.userService.getManagedTokens(e)}getAddress(){return this.http.getAddress()}validateTokenName(e){return Mo(e)}async fetchToken(e){return this.poolService.fetchToken(e)}async validateToken(e){return this.poolService.validateToken(e)}async fetchTokenStats(e){return this.poolService.fetchTokenStats(e)}async updateTokenConfig(e,t){return this.poolService.updateTokenConfig(e,t)}async fetchUserBalances(e){return this.userService.fetchUserBalances(e)}async fetchUserReport(e){return this.userService.fetchUserReport(e)}}class nl extends Yr{constructor(e,t,n,i=!1,o){super(e,t,n,i,o)}validateContent(e,t){if(!jn(e))throw Ft("content","Message content");if(0===e.trim().length)throw Ft("content","Message content");qn(e,t===zc.CHAT_MESSAGE?vn.CHAT_MESSAGES_V1.MAX_LENGTH:vn.COMMENTS_V1.MAX_LENGTH,"content")}validateMessageId(e){if(!jn(e))throw Ft("id","Message ID");if(e.length<10||e.length>64)throw Rt("id","valid message ID format","Message ID")}validateMessageType(e){if(e!==zc.CHAT_MESSAGE&&e!==zc.COMMENT)throw new wt(`type must be one of: ${Object.values(zc).join(", ")}`,"type","INVALID_VALUE")}async fetchMessages(e){const t=void 0!==e.type,n=void 0!==e.tokenName&&""!==e.tokenName;t&&this.validateMessageType(e.type),n&&this.validateTokenName(e.tokenName,gn);const i={};void 0!==e.cursor&&(i.cursor=e.cursor),i.pageSize=e.pageSize??zr.DEFAULT_LIMIT;const o=Qr(i,yn.MAX_LIMIT),a={};void 0!==o.pageSize&&(a.pageSize=o.pageSize),void 0!==o.cursor&&(a.cursor=o.cursor),t&&(a.type=e.type.toUpperCase()),n&&(a.tokenName=en(e.tokenName)),void 0!==e.userAddress&&""!==e.userAddress&&(a.userAddress=e.userAddress),void 0!==e.search&&""!==e.search&&(a.search=e.search);const r=await this.http.get(Fe.GET_MESSAGES,a),s=this.extractData(r);return{messages:s.messages,pageInfo:s.pageInfo}}async createMessage(e){this.validateMessageType(e.type),this.validateTokenName(e.tokenName,gn),this.validateContent(e.content,e.type);const t={type:e.type,tokenName:en(e.tokenName),content:e.content.trim()},n=await this.http.post(Fe.CREATE_MESSAGE,t,this.getJwtHeaders());return{message:this.extractData(n)}}async updateMessage(e,t){this.validateMessageId(e),this.validateContent(t.content);const n=this.buildEndpointWithId(Fe.UPDATE_MESSAGE,e),i={content:t.content.trim()},o=await this.http.put(n,i,this.getJwtHeaders());return{message:this.extractData(o)}}async deleteMessage(e){this.validateMessageId(e);const t=this.buildEndpointWithId(Fe.DELETE_MESSAGE,e);return await this.http.delete(t,void 0,this.getDualAuthHeaders()),{success:!0}}async getPinnedMessage(e){this.validateTokenName(e,gn);const t=await this.http.get(Fe.GET_PINNED,{tokenName:en(e)});return{message:this.extractData(t)}}async pinMessage(e){this.validateMessageId(e);const t=Fe.PIN_MESSAGE.replace(":id",e);return await this.http.post(t,{},this.getDualAuthHeaders()),{success:!0}}async unpinMessage(e){this.validateMessageId(e);const t=Fe.UNPIN_MESSAGE.replace(":id",e);return await this.http.post(t,{},this.getDualAuthHeaders()),{success:!0}}async getMessageStats(){const e=await this.http.get(Fe.GET_MESSAGE_STATS,void 0,this.getDualAuthHeaders());return this.extractData(e)}}const il=Jn({MODERATOR:"MODERATOR",TECHNICAL_PRODUCER:"TECHNICAL_PRODUCER",MANAGER:"MANAGER"}),ol={TOKEN:"TOKEN",ALL_OWNER_TOKENS:"ALL_OWNER_TOKENS"},al=Jn(ol),rl={PENDING:"PENDING",CLAIMED:"CLAIMED",REVOKED:"REVOKED",EXPIRED:"EXPIRED"};Jn(rl);class sl extends Yr{constructor(e,t,n,i=!1,o){super(e,t,n,i,o)}validateRole(e){if(!jn(e))throw Ft("role","Role");if(!function(e){return il.includes(e)}(e))throw Rt("role",`one of: ${il.join(", ")}`,"Role")}validateInviteCodeFormat(e){if(!jn(e))throw Ft("inviteCode","Invite code");if(t=e,!Tn.PATTERN.test(t))throw Rt("inviteCode","valid invite code format","Invite code");var t}validateInviteScope(e){if(void 0!==e&&(t=e,!al.includes(t)))throw new wt(`Invalid invite scope. Must be one of: ${al.join(", ")}`,"inviteScope","INVALID_VALUE");var t}async createInvite(e){this.validateInviteScope(e.inviteScope);const t=e.inviteScope??ol.TOKEN;if(t===ol.TOKEN){if(void 0===e.tokenName||""===e.tokenName)throw new wt("Token name is required for TOKEN scope invites","tokenName","REQUIRED");this.validateTokenName(e.tokenName,hn)}else if(t===ol.ALL_OWNER_TOKENS&&e.tokenName)throw new wt("Token name should not be provided for ALL_OWNER_TOKENS scope invites","tokenName","INVALID_VALUE");this.validateRole(e.role),this.validateOptionalString(e.description,"description","Description",vn.DESCRIPTION.MAX_LENGTH),this.validateOptionalDate(e.expiresAt,"expiresAt","Expiration date");const n=Q,i={inviteScope:t,role:e.role};t===ol.TOKEN&&null!=e.tokenName&&""!==e.tokenName&&(i.tokenName=en(e.tokenName)),void 0!==e.description&&(i.description=e.description),void 0!==e.expiresAt&&(i.expiresAt=e.expiresAt);const o=await this.http.post(n,i,this.getMultiAuthHeaders());return{invite:this.extractData(o)}}async listInvites(e){void 0!==e.tokenName&&""!==e.tokenName&&this.validateTokenName(e.tokenName,hn),this.validateStatusFilter(e.status,rl);const t=Y,n={...this.buildPaginationParams(e,yn.MAX_LIMIT)};void 0!==e.tokenName&&""!==e.tokenName&&(n.tokenName=en(e.tokenName)),void 0!==e.status&&(n.status=e.status);const i=await this.http.get(t,n,this.getMultiAuthHeaders()),o=this.extractData(i);return{items:o.invites,pageInfo:o.pageInfo}}async revokeInvite(e){this.validatePositiveInteger(e,"inviteId","Invite ID");const t=this.buildEndpoint(Z,{id:String(e)});jt(await this.http.delete(t,void 0,this.getMultiAuthHeaders()),"Failed to revoke invite")}async updateInviteRole(e){this.validatePositiveInteger(e.inviteId,"inviteId","Invite ID"),this.validateRole(e.role);const t=this.buildEndpoint(ee,{id:String(e.inviteId)}),n={role:e.role},i=await this.http.patch(t,n,this.getMultiAuthHeaders());return{invite:this.extractData(i)}}async updateInvite(e){if(this.validatePositiveInteger(e.inviteId,"inviteId","Invite ID"),void 0===e.role&&(void 0===e.description||""===e.description))throw new wt("At least one of role or description must be provided","options","REQUIRED");void 0!==e.role&&this.validateRole(e.role),void 0!==e.description&&this.validateOptionalString(e.description,"description","Description",vn.DESCRIPTION.MAX_LENGTH);const t=this.buildEndpoint(te,{id:String(e.inviteId)}),n={};void 0!==e.role&&(n.role=e.role),void 0!==e.description&&(n.description=e.description);const i=await this.http.patch(t,n,this.getMultiAuthHeaders());return{invite:this.extractData(i)}}async claimInvite(e){this.validateInviteCodeFormat(e.inviteCode);const t=X,n={inviteCode:e.inviteCode},i=await this.http.post(t,n,this.getMultiAuthHeaders()),o=this.extractData(i);return"tokenName"in o&&void 0!==o.tokenName?{token:o}:{blanketAccess:o}}async getModeratedTokens(e){const t=J,n=this.buildPaginationParams(e??{},yn.MAX_LIMIT),i=await this.http.get(t,n,this.getMultiAuthHeaders()),o=this.extractData(i);return{items:o.tokens,pageInfo:o.pageInfo}}async getInviteByCode(e){this.validateInviteCodeFormat(e);const t=this.buildEndpoint(ne,{code:e}),n=await this.http.get(t,{});return this.extractData(n)}}class cl{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++}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=_n(Math.floor(.5*e.length),0),n=_n(Math.floor(.95*e.length),0),i=_n(Math.floor(.99*e.length),0);return{p50:e[t]??0,p95:e[n]??0,p99:e[i]??0}}getCacheHitRate(){if(0===this.eventsProcessed)return 0;let e=0;for(const t of this.perPoolMetrics.values())e+=t.cacheHits;return _n(e/this.eventsProcessed*100,NaN)}getThroughputPerSecond(){const e=_n(un(this.startTime)/1e3,0);return 0===e?0:_n(this.eventsProcessed/e,0)}recordMemory(){if("undefined"!=typeof process&&"function"==typeof process.memoryUsage){const e=_n(process.memoryUsage().heapUsed/1024/1024,0);this.memorySnapshots.push(e),this.memorySnapshots.length>this.maxMemorySnapshots&&this.memorySnapshots.shift()}}getMemoryUsedMB(){return"undefined"!=typeof process&&"function"==typeof process.memoryUsage?_n(process.memoryUsage().heapUsed/1024/1024,0):0}getPoolAverageLatency(e){const t=this.perPoolMetrics.get(e);return t&&0!==t.eventsProcessed?_n(t.totalLatency/t.eventsProcessed,0):0}getPoolCacheHitRate(e){const t=this.perPoolMetrics.get(e);if(!t)return 0;const n=t.cacheHits+t.cacheMisses;return 0===n?0:_n(t.cacheHits/n*100,0)}getHealthMetrics(e,t,n,i,o){const a=this.getLatencyPercentiles(),r=this.getMemoryUsedMB();return{eventProcessing:{queueDepth:this.queueDepth,eventsProcessed:this.eventsProcessed,eventsDropped:this.eventsDropped,throughputPerSecond:this.getThroughputPerSecond()},metrics:{latencyP50:a.p50,latencyP95:a.p95,latencyP99:a.p99,cacheHitRate:this.getCacheHitRate()},memory:{usedMB:_n(fo(r,1),0),maxMB:o,percentUsed:Math.min(100,_n(fo(r/o*100,1),0))},pools:{totalMonitored:e,hotCacheSize:t,warmCacheSize:n,coldCacheSize:i}}}reset(){this.eventLatencies=[],this.eventsProcessed=0,this.eventsDropped=0,this.queueDepth=0,this.maxQueueDepth=0,this.startTime=Date.now(),this.perPoolMetrics.clear(),this.memorySnapshots=[]}getSummary(){const e=this.eventLatencies.length>0?_n(this.eventLatencies.reduce((e,t)=>e+t,0)/this.eventLatencies.length,0):0;return{eventsProcessed:this.eventsProcessed,eventsDropped:this.eventsDropped,cacheHitRate:this.getCacheHitRate(),averageLatency:_n(fo(e,0),0),memoryUsedMB:_n(fo(this.getMemoryUsedMB(),1),0),throughputPerSecond:_n(fo(this.getThroughputPerSecond(),2),0)}}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 ll{static createPoolKey(e,t,n){return`${e}/${t}/${n}`}static parsePoolKey(e){if(!jn(e))return null;const t=e.split("/");if(3!==t.length)return null;const n=t[0]?.trim(),i=t[1]?.trim(),o=t[2]?.trim();if(""===n||""===i||""===o)return null;const a=Fn(o,-1);return a<0?null:{token0:n,token1:i,feeTier:a}}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 i=this.parsePoolKey(e);if(!i)return!1;const o=i.token0===t||i.token1===t,a=i.token0===n||i.token1===n;return o&&a&&t!==n}static normalizeFee(e){if(Vn(e))return null;const t="number"==typeof e?e:On(String(e),NaN);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`${fo(po(e).dividedBy(1e4),2)}%`}static isValidTokenPair(e,t){return Boolean(e)&&Boolean(t)&&e!==t}}class dl{constructor(e={}){this.attempts=0,this.config={maxAttempts:e.maxAttempts??5,baseDelayMs:e.baseDelayMs??2e3,useExponentialBackoff:e.useExponentialBackoff??!1,maxDelayMs:e.maxDelayMs??3e4,backoffMultiplier:e.backoffMultiplier??2},this.currentDelayMs=this.config.baseDelayMs}shouldRetry(){return this.attempts<this.config.maxAttempts}getNextDelay(){return this.currentDelayMs}recordAttempt(){this.attempts++,this.config.useExponentialBackoff&&(this.currentDelayMs=Math.min(this.currentDelayMs*this.config.backoffMultiplier,this.config.maxDelayMs))}reset(){this.attempts=0,this.currentDelayMs=this.config.baseDelayMs}getAttempts(){return this.attempts}getMaxAttempts(){return this.config.maxAttempts}isExhausted(){return this.attempts>=this.config.maxAttempts}getState(){return{attempts:this.attempts,maxAttempts:this.config.maxAttempts,canRetry:this.shouldRetry(),nextDelayMs:this.currentDelayMs,exhausted:this.isExhausted()}}getStatusString(){return`${this.attempts}/${this.config.maxAttempts} attempts`}}class ul{constructor(e){this.logger=e??new an({debug:!1,context:"SwapEventExtractor"})}walkPayloadForSwaps(e,t){const n=[],i=new WeakSet,o=(e,a=0)=>{if(a>50)this.logger.debug("Payload nesting exceeded maximum depth of 50");else if(null!=e&&"string"!=typeof e&&"object"==typeof e){if(i.has(e))return;i.add(e);const r=this.extractSwapFromObject(e);r&&!t.has(r.transactionId)&&(n.push(r),t.add(r.transactionId));for(const t of Object.values(e))o(t,a+1)}};return o(e,0),n}extractSwapFromObject(e){const t=this.extractTransactionId(e);if(null===t)return null;const n=e.Data,i=null==n||"object"!=typeof n||Array.isArray(n)?e:n,o=this.extractToken(i,"token0","fromToken","source"),a=this.extractToken(i,"token1","toToken","destination");if(null===o||null===a)return null;const r=this.extractAmount(i,"amount0","amountIn","inputAmount"),s=this.extractAmount(i,"amount1","amountOut","outputAmount");if(null===r||null===s)return null;const c=this.extractFeeTier(i);if(null===c)return null;const l=this.extractTimestamp(i),d=this.buildPoolKey(o,a,c),u=this.determineDirection(i,o,a),h={transactionId:t,poolKey:d,token0:o,token1:a,amount0:r,amount1:s,feeTier:c,direction:u,timestamp:l,exactInput:this.determineExactInput(i,u)},g=this.extractUser(i);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(jn(t))return t}return null}extractToken(e,...t){for(const n of t){const t=e[n];if(jn(t))return t}return null}extractAmount(e,...t){for(const n of t){const t=e[n];if(!Vn(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],i=this.normalizeFee(t);if(null!==i)return i}return null}normalizeFee(e){if(Vn(e))return null;const t="number"==typeof e?e:On(String(e),NaN);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(jn(t))return t}}determineDirection(e,t,n){const i=e.zeroForOne??e.direction;if("boolean"==typeof i)return i?"zeroForOne":"oneForZero";if("string"==typeof i){if(Ro(i,"zerotoone")||"0to1"===i)return"zeroForOne";if(Ro(i,"onetozero")||"1to0"===i)return"oneForZero"}if(e.fromToken===t||e.inputToken===t)return"zeroForOne";if(e.fromToken===n||e.inputToken===n)return"oneForZero";const o=this.extractAmount(e,"amount0","amountIn");return null!==o&&xn(o,0),"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,i=void 0!==e.amountOut&&null!==e.amountOut,o=void 0!==e.inputAmount&&null!==e.inputAmount,a=void 0!==e.outputAmount&&null!==e.outputAmount;return!(!n||i)||!(i&&!n)&&(!(!o||a)||!(a&&!o))}buildPoolKey(e,t,n){return`${e}/${t}/${n}`}}const hl=new Map;class gl{static getCached(e){const t=e.toString();return this.CACHE.has(t)||this.CACHE.set(t,po(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])}}}gl.CACHE=new Map,gl.ZERO=Bn(0),gl.ONE=Bn(1),gl.FEE_PIPS=Bn(1e6),gl.MIN_SQRT_RATIO=Bn("4295128739"),gl.MAX_SQRT_RATIO=new o("1461446703485210103287273052203988822378723970342");const ml={maxIterations:100,enableBigNumberCache:!0,roundingMode:o.ROUND_DOWN,debugLogging:!1};class pl{static calculateSwapDelta(e,t,n={}){const i=Date.now(),o={...ml,...n};try{const n=this.initializeSwapState(e,t,o);o.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,o);o.debugLogging&&this.logger.debug("Swap loop completed",{stepCount:a.stepCount,ticksCrossed:a.ticksCrossed.length,priceHitLimit:a.priceHitLimit});const r=this.createUpdatedPool(e.pool,a.state,t,o),s=this.calculateFinalAmounts(n,a.state,t);let c;if(void 0!==t.actualSqrtPrice&&""!==t.actualSqrtPrice){const e=po(r.sqrtPrice),n=po(t.actualSqrtPrice),i=e.minus(n).abs();c=_n(No(i,n).times(100).toNumber(),0)}const l=un(i);o.debugLogging&&this.logger.debug("Swap delta calculated",{calculationTimeMs:l,amount0:s.amount0.toString(),amount1:s.amount1.toString(),driftPercentage:c}),l>100&&this.logger.warn("Swap calculation exceeded 100ms",{calculationTimeMs:l,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:r,updatedTicks:a.updatedTicks,amount0:s.amount0,amount1:s.amount1,feeAmount0:s.feeAmount0,feeAmount1:s.feeAmount1,ticksCrossed:a.ticksCrossed,metadata:{calculationTimeMs:l,swapSteps:a.stepCount,priceHitLimit:a.priceHitLimit,...void 0!==c&&{driftPercentage:c}}}}catch(e){throw this.logger.error("Swap delta calculation failed",e),new Error(`Swap delta calculation failed: ${at(e)}`)}}static initializeSwapState(e,t,n){const{pool:i}=e;if(null===i.sqrtPrice||void 0===i.sqrtPrice||null===i.liquidity||void 0===i.liquidity)throw new Error("Invalid pool data: missing sqrtPrice or liquidity");const o=n.enableBigNumberCache?gl.getCached.bind(gl):e=>Bn(e),a="string"==typeof i.sqrtPrice?i.sqrtPrice:fo(i.sqrtPrice,0),r="string"==typeof i.liquidity?i.liquidity:fo(i.liquidity,0),s=o(a),c=o(r),l=i.tick??0,u=d.sqrtPriceToTick(po(a)),h=Math.abs(u-l);h>100&&this.logger.warn("Significant tick/price mismatch detected in pool state",{poolTick:l,calculatedTick:u,drift:h,threshold:100});const g=o(t.amountSpecified);Io(g,"amountSpecified");const m="string"==typeof i.feeGrowthGlobal1?i.feeGrowthGlobal1:fo(i.feeGrowthGlobal1,0),p="string"==typeof i.feeGrowthGlobal0?i.feeGrowthGlobal0:fo(i.feeGrowthGlobal0,0),f=t.zeroForOne?o(m):o(p);return{sqrtPrice:s,liquidity:c,tick:l,amountSpecifiedRemaining:g,amountCalculated:gl.ZERO,feeGrowthGlobalX:f,protocolFee:gl.ZERO}}static computeSwapLoop(e,t,n,i){const{pool:a,tickDataMap:r}=t,s=[],c={};let l=0;const u=n.zeroForOne?gl.MIN_SQRT_RATIO:gl.MAX_SQRT_RATIO,h=po("0.000001");for(;e.amountSpecifiedRemaining.gt(h)&&!e.sqrtPrice.eq(u)&&l<i.maxIterations;){l++;const[t,h]=this.findNextInitializedTick(r,e.tick,a.tickSpacing,n.zeroForOne);let g;if(i.debugLogging&&this.logger.debug(`Swap step ${l}`,{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=d.tickToSqrtPrice(t);g=e instanceof o?e:po(String(e))}else g=u;const m=n.zeroForOne?wo(g,u):vo(g,u),p=this.executeSwapStep(e.sqrtPrice,m,e.liquidity,e.amountSpecifiedRemaining,a.fee,n.exactInput);if(e.sqrtPrice=p.sqrtPriceNext,n.exactInput){const t=p.amountIn.plus(p.feeAmount);t.lte(0)?e.amountSpecifiedRemaining=gl.ZERO:(e.amountSpecifiedRemaining=e.amountSpecifiedRemaining.minus(t),e.amountSpecifiedRemaining.lt(0)&&(e.amountSpecifiedRemaining=gl.ZERO)),e.amountCalculated=e.amountCalculated.minus(p.amountOut)}else{p.amountOut.lte(0)?e.amountSpecifiedRemaining=gl.ZERO:e.amountSpecifiedRemaining=e.amountSpecifiedRemaining.plus(p.amountOut),e.amountCalculated=e.amountCalculated.plus(p.amountIn.plus(p.feeAmount))}if(e.liquidity.gt(0)){const t=No(p.feeAmount,e.liquidity);e.feeGrowthGlobalX=e.feeGrowthGlobalX.plus(t)}if(e.sqrtPrice.eq(g)&&h){const o=r[t.toString()];if(null==o)throw new Error(`Missing tick data for initialized tick ${t}`);const a=n.zeroForOne?po(o.liquidityNet).negated():po(o.liquidityNet);if(e.liquidity=e.liquidity.plus(a),e.liquidity.lt(0))throw new Error(`Negative liquidity after crossing tick ${t}: ${e.liquidity.toString()}`);s.push(t),c[t.toString()]=o,i.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=d.sqrtPriceToTick(po(e.sqrtPrice.toString()));e.tick=t}}if(l>=i.maxIterations)throw new Error(`Swap calculation exceeded maximum iterations (${i.maxIterations}). Possible infinite loop or very complex swap.`);const g=e.sqrtPrice.eq(u);return{state:e,ticksCrossed:s,priceHitLimit:g,stepCount:l,updatedTicks:c}}static createUpdatedPool(e,t,n,i){const o=Object.assign(Object.create(Object.getPrototypeOf(e)),e);if(o.sqrtPrice=po(t.sqrtPrice).toFixed(0),o.liquidity=po(t.liquidity).toFixed(0),o.tick=t.tick,n.zeroForOne?o.feeGrowthGlobal1=po(t.feeGrowthGlobalX).toFixed(0):o.feeGrowthGlobal0=po(t.feeGrowthGlobalX).toFixed(0),n.zeroForOne){const n=po(e.protocolFeesToken0);o.protocolFeesToken0=n.plus(t.protocolFee).toFixed(0)}else{const n=po(e.protocolFeesToken1);o.protocolFeesToken1=n.plus(t.protocolFee).toFixed(0)}return o}static calculateFinalAmounts(e,t,n){let i,o,a,r;if(n.exactInput){const e=po(n.amountSpecified),s=t.amountCalculated.abs();n.zeroForOne?(i=e.negated(),o=s,a=gl.ZERO,r=gl.ZERO):(i=s,o=e.negated(),a=gl.ZERO,r=gl.ZERO)}else{const e=po(n.amountSpecified),s=t.amountCalculated.abs();n.zeroForOne?(i=s.negated(),o=e,a=gl.ZERO,r=gl.ZERO):(i=e,o=s.negated(),a=gl.ZERO,r=gl.ZERO)}const s=t.feeGrowthGlobalX.minus(e.feeGrowthGlobalX).times(e.liquidity);return n.zeroForOne?r=s:a=s,{amount0:i,amount1:o,feeAmount0:a,feeAmount1:r}}static findNextInitializedTick(e,t,n,i){const o=Object.keys(e).map(e=>Rn(e,0)).sort((e,t)=>e-t);if(0===o.length){return[i?-887272:887272,!1]}if(i){const e=o.reverse().find(e=>e<t);return void 0!==e?[e,!0]:[-887272,!1]}{const e=o.find(e=>e>t);return void 0!==e?[e,!0]:[887272,!1]}}static executeSwapStep(e,t,n,i,a,r){Co(n),Io(i,"amountRemaining");const s=[500,3e3,1e4];if(!s.includes(a))throw new Error(`Invalid fee tier: ${a}. Must be one of: ${s.join(", ")}`);const c=d.computeSwapStep(e,t,n,i,a,t.lt(e)),l=c[0],u=c[1],h=c[2],g=c[3],m=o.isBigNumber(l)?l:po(String(l)),p=o.isBigNumber(u)?u:po(String(u)),f=o.isBigNumber(h)?h:po(String(h)),y=o.isBigNumber(g)?g:po(String(g));return{sqrtPriceStart:e,tickNext:d.sqrtPriceToTick(m),sqrtPriceNext:m,initialised:!1,amountIn:p,amountOut:f,feeAmount:y}}}pl.logger=new an({debug:!1,context:"SwapDeltaCalculator"});class fl extends si{constructor(e,t,n,i){super(!1,i),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.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(),i={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,i),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,i,o){const a=this.cache.get(e);if(!a)return this.logger.debug(`Pool ${e} not in cache for delta update`),!1;try{if(o){const r="zeroForOne"===t,s=r?n:i,c={transactionId:o.transactionId,timestamp:o.timestamp,amountSpecified:s,zeroForOne:r,exactInput:o.exactInput},l=pl.calculateSwapDelta(a.poolData,c);a.poolData={...a.poolData,pool:l.updatedPool},a.swapsSinceRefetch++,a.lastDeltaAppliedTime=Date.now(),void 0!==l.metadata.driftPercentage?(a.cumulativeDrift+=l.metadata.driftPercentage,this.logger.debug(`Delta applied for ${e}: drift=${l.metadata.driftPercentage.toFixed(4)}%, cumulative=${a.cumulativeDrift.toFixed(2)}%`)):this.logger.debug(`Delta applied for ${e}: ${l.ticksCrossed.length} ticks crossed`)}else a.swapsSinceRefetch++,a.lastDeltaAppliedTime=Date.now();return this.shouldRefetch(a)&&(this.logger.debug(`Refetch needed for ${e}: swaps=${a.swapsSinceRefetch}, drift=${(100*a.cumulativeDrift).toFixed(2)}%`),a.expiresAt=Date.now()),!0}catch(t){return this.logger.error(`Failed to update pool ${e}:`,t),a.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(""===e)throw Ft("poolKey","Pool key");if(this.cache.has(e))return!0;try{return await ea(async()=>{await this.getPool(e),this.logger.debug(`Cache warmed for ${e}`)},`Failed to warm cache for ${e}`,this.logger),!0}catch{return!1}}async warmCacheBatch(e,t=5){if(0===e.length)throw Ft("poolKeys","Pool keys array");return ea(async()=>{const n={succeeded:0,failed:0,total:e.length};let i=0;const o=new Set;for(;i<e.length||o.size>0;){for(;i<e.length&&o.size<t;){const t=e[i];i++;const a=this.warmCache(t).then(e=>{e?n.succeeded++:n.failed++});o.add(a),a.finally(()=>o.delete(a))}o.size>0&&await Promise.race(o)}return this.logger.debug(`Cache warming complete: ${n.succeeded}/${n.total} succeeded`),n},"Failed to warm cache batch",this.logger)}clear(){this.cache.clear(),this.logger.debug("Cache cleared")}clearExpired(){const e=Date.now();let t=0;for(const[n,i]of this.cache)e>i.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=>{null!=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 yl extends si{constructor(e,t,n){super(!1,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.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()};void 0!==e&&e>0&&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(un(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 i=e[n];this.eventsProcessedCount++,"rejected"===i.status&&this.logger.error(`Failed to process event ${t[n].transactionId}:`,i.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=un(t);this.metrics.recordEventLatency(n)}catch(t){throw this.logger.error(`Event processing failed for ${e.transactionId}:`,t),t}}}class kl extends si{constructor(e,t,n,i={},o){super(!1,o),this.socket=null,this.maxSeenTransactions=1e4,this.listeners=[],this.onErrorCallbacks=[],this.isActive=!1,this.listenerRegistered=!1,this.handleSwapEvent=null,this.warmingIntervalHandle=null,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 cl,this.config=this.applyDefaults(i),this.eventExtractor=new ul(this.logger),this.cacheManager=new fl(t,this.config,this.metrics,this.logger),this.eventQueue=new yl(this.config,this.metrics,this.logger),this.seenTransactions=new vl(this.maxSeenTransactions),this.reconnectionManager=new dl({maxAttempts:3,baseDelayMs:1e3,useExponentialBackoff:!0,maxDelayMs:3e4}),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()=>{if(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)try{n.unsubscribe()}catch(e){n.logger.error("Error during unsubscribe cleanup:",e)}}}getHealth(){const e=this.cacheManager.getStats(),t=this.eventQueue.getStats(),n=this.metrics.getHealthMetrics(e.totalCached,e.hotCacheSize,e.warmCacheSize,e.coldCacheSize,this.getMaxMemoryMB()),i=this.determineHealthStatus(t,e),o={connected:this.socket?.connected??!1,reconnectAttempts:this.reconnectionManager.getAttempts()};this.socket?.connected&&(o.lastConnectionTime=new Date);const a=t.queueSize/this.config.maxQueueSize*100,r=e.totalCached/this.config.maxPools*100,s=e.memoryUsedMB/this.getMaxMemoryMB()*100,c=this.generateHealthRecommendations(i,a,r,s,n.metrics.cacheHitRate);return{...n,status:i,websocket:o,recommendations:c,detailedMetrics:{eventQueueUtilization:a,cacheUtilization:r,memoryUtilization:s}}}generateHealthRecommendations(e,t,n,i,o){const a=[];return"failed"===e&&(a.push("🔴 System is in FAILED state - immediate action required"),this.socket?.connected||a.push("Reconnect WebSocket - connection lost"),t>90&&a.push("Reduce incoming event rate or increase maxQueueSize")),"degraded"===e&&(a.push("⚠️ System is DEGRADED - performance may be impacted"),t>75&&a.push(`Queue utilization ${t.toFixed(1)}% - consider increasing maxQueueSize`),i>80&&a.push(`Memory usage ${i.toFixed(1)}% - consider reducing cache size or memory profile`)),"healthy"===e&&(o<50&&a.push(`Cache hit rate ${o.toFixed(1)}% is low - consider warming more pools`),i>50&&a.push("Memory usage is moderate - monitor for growth trends"),t>50&&a.push("Queue utilization is elevated - monitor for bottlenecks")),a}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=function(e,t){const n=e;return hl.set(n,{createdAt:Date.now(),label:t}),n}(setInterval(()=>{this.performBackgroundWarming().catch(e=>{this.logger.error("Background warming error:",e)})},e),"background-warming"),this.logger.debug(`Background warming started (interval: ${e}ms)`)}stopBackgroundWarming(){this.warmingIntervalHandle&&(!function(e){const t=e;hl.delete(t)}(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.socketReady.catch(()=>{}),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(ot(e)?e:new Error(at(e)))}))}async attemptReconnection(){const e=this.reconnectionManager.getAttempts(),t=this.reconnectionManager.getMaxAttempts();if(this.logger.debug(`Reconnection attempt ${e+1}/${t}`),this.reconnectionManager.isExhausted())return this.logger.error(`Max reconnection attempts (${t}) exceeded - performing full reset`),void this.performFullReset();if(e<2)if(0===e)this.logger.debug("Tier 1: Quick reconnect");else{const e=this.reconnectionManager.getNextDelay();this.logger.debug(`Tier 2: Exponential backoff (${e}ms)`),await new Promise(t=>setTimeout(t,e))}try{this.socket?.disconnect&&(this.socket.disconnect(),this.logger.debug("Disconnected for reconnection")),this.socket?.connect?.(),this.logger.debug("Reconnection initiated"),this.reconnectionManager.recordAttempt()}catch(e){throw this.logger.error("Failed to initiate reconnection:",e),e}}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.reconnectionManager.reset(),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,...i)=>{try{const n=Date.now(),o=i[0],a=t.eventExtractor.walkPayloadForSwaps(o,t.seenTransactions);if(0===a.length)return;t.logger.debug(`Extracted ${a.length} swaps from payload`);for(const n of a){if(t.filterSwap(n,e)){t.eventQueue.enqueue(n)||t.logger.debug(`Swap dropped due to queue overflow: ${n.transactionId}`)}}const r=un(n);t.metrics.recordEventLatency(r)}catch(e){t.logger.error("Error processing WebSocket payload:",e),t.notifyError(ot(e)?e:new Error(at(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")}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(void 0!==t.tokenFilter&&""!==t.tokenFilter){if(!ll.containsToken(e.poolKey,t.tokenFilter))return!1}if(t.pairTokens){const[n,i]=t.pairTokens;if(!ll.containsTokenPair(e.poolKey,n,i))return!1}if(void 0!==t.feeTierFilter&&""!==t.feeTierFilter){if(ll.normalizeFee(t.feeTierFilter)!==e.feeTier)return!1}return void 0===t.userFilter||""===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 i=un(t);this.metrics.recordEventLatency(i);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(ot(t)?t:new Error(at(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 vl{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}}const wl="10000",bl="1000";function Sl(e){return`${e}-${Date.now()}-${Math.random().toString(36).substring(2,11)}`}function Al(){return Date.now()+6e4}function Tl(e){return""!==e&&"string"==typeof e&&/^[a-zA-Z0-9]{3,50}$/.test(e)}function El(e){try{return Math.floor(parseFloat(String(e))??0).toString()}catch{return"0"}}function Cl(e){return""!==e&&"string"==typeof e&&/^eth\|[0-9a-fA-F]{40}$/.test(e)}class Il extends ri{constructor(e){super(e)}getCollectionClaimFee(){return wl}getTokenClassCreateFee(){return bl}async estimateMintFee(e){try{if(this.logger.debug("Estimating mint fee",e),0===e.collection.length||0===e.type.length||0===e.category.length||0===e.quantity.length)throw new Error("Missing required parameters for mint fee estimation");if(!Cl(e.ownerAddress))throw new Error(`Invalid owner address format: ${e.ownerAddress}`);const t="0";return this.logger.debug("Estimated mint fee:",t),t}catch(e){throw this.logger.error("Failed to estimate mint fee",e),e}}estimateNftOperationFees(e){let t="0";const n={};if(e.claimCollection){const e=wl;n.collectionClaim=e,t=String(BigInt(t)+BigInt(e))}if((e.createTokenClasses??0)>0){const i=bl,o=String(BigInt(i)*BigInt(e.createTokenClasses));n.tokenClassCreate=i,n.tokenClassCount=e.createTokenClasses,t=String(BigInt(t)+BigInt(o))}return{totalFee:t,breakdown:n}}async claimCollection(e){try{if(this.logger.debug("Claiming NFT collection",e.collectionName),!Tl(e.collectionName))throw new Error(`Invalid collection name format. Must be alphanumeric, 3-50 characters: ${e.collectionName}`);if(!await this.isCollectionAvailable(e.collectionName))throw new Error(`Collection name already claimed: ${e.collectionName}`);const t={collectionName:e.collectionName,expiresAt:Al(),uniqueKey:Sl("auth")};this.logger.debug("Built claim DTO",t);return{transactionId:`claim-${Date.now()}`,collection:e.collectionName,authorizedUser:"",status:"pending"}}catch(e){throw this.logger.error("Failed to claim collection",e),e}}async fetchUserCollections(e){try{if(this.logger.debug("Fetching user collections for",e),!Cl(e))throw new Error(`Invalid wallet address format: ${e}`);const t=[];return this.logger.debug(`Found ${t.length} collections for user`),t}catch(e){throw this.logger.error("Failed to fetch user collections",e),e}}async isCollectionAvailable(e){try{if(this.logger.debug("Checking collection availability",e),!Tl(e))throw new Error(`Invalid collection name format: ${e}`);const t=!0;return this.logger.debug(`Collection ${e} available:`,t),t}catch(e){throw this.logger.error("Failed to check collection availability",e),e}}async createTokenClass(e){try{if(this.logger.debug("Creating NFT token class",e),null==e.collection||0===e.collection.length||null==e.type||0===e.type.length||null==e.category||0===e.category.length)throw new Error("Missing required parameters: collection, type, category");if(!Tl(e.collection))throw new Error(`Invalid collection name: ${e.collection}`);if(void 0!==e.symbol&&(""===(t=e.symbol)||"string"!=typeof t||!/^[a-zA-Z]{1,8}$/.test(t)))throw new Error(`Invalid token symbol: ${e.symbol}`);const n={collection:e.collection,type:e.type,category:e.category,name:e.name,description:e.description,image:e.image,symbol:e.symbol,rarity:e.rarity,maxSupply:void 0!==e.maxSupply?El(e.maxSupply):void 0,maxCapacity:void 0!==e.maxCapacity?El(e.maxCapacity):void 0,additionalKey:e.additionalKey,expiresAt:Al(),uniqueKey:Sl("create")};this.logger.debug("Built token class DTO",n);return{transactionId:`create-${Date.now()}`,tokenClass:{collection:e.collection,type:e.type,category:e.category,additionalKey:e.additionalKey??""},status:"pending"}}catch(e){throw this.logger.error("Failed to create token class",e),e}var t}async fetchTokenClasses(e){try{if(this.logger.debug("Fetching token classes",e),!Tl(e.collection))throw new Error(`Invalid collection name: ${e.collection}`);const t=[];return this.logger.debug(`Found ${t.length} token classes`),t}catch(e){throw this.logger.error("Failed to fetch token classes",e),e}}async mintNft(e){try{if(this.logger.debug("Minting NFT",e),0===e.collection.length||0===e.type.length||0===e.category.length||0===e.quantity.length)throw new Error("Missing required parameters for minting");const t=parseInt(e.quantity,10);if(t<=0)throw new Error("Quantity must be greater than 0");if(!Cl(e.ownerAddress))throw new Error(`Invalid owner address: ${e.ownerAddress}`);const n=this.estimateMintFee({collection:e.collection,type:e.type,category:e.category,quantity:e.quantity,ownerAddress:e.ownerAddress});this.logger.debug("Estimated mint fee:",n);const i={collection:e.collection,type:e.type,category:e.category,quantity:e.quantity,owner:e.ownerAddress,additionalKey:e.additionalKey??"none",expiresAt:Al(),uniqueKey:Sl("mint")};this.logger.debug("Built mint DTO",i);const o=`mint-${Date.now()}`,a=Array.from({length:t},(e,t)=>t+1);return{transactionId:o,mintedQuantity:e.quantity,owner:e.ownerAddress,tokenInstances:a,tokenClass:{collection:e.collection,type:e.type,category:e.category}}}catch(e){throw this.logger.error("Failed to mint NFT",e),e}}async fetchNftBalances(e,t){try{if(this.logger.debug("Fetching NFT balances for",{ownerAddress:e,collectionFilter:t}),!Cl(e))throw new Error(`Invalid owner address: ${e}`);const n=[];return this.logger.debug(`Found ${n.length} NFT balances`),n}catch(e){throw this.logger.error("Failed to fetch NFT balances",e),e}}}class Nl extends ri{constructor(e,t=!1,n){super(e,t,n)}extractData(e){if(e.error||void 0===e.data){const t=e.message??"Notification operation failed";throw new Error(t)}return e.data}async registerPushToken(e){this.logger.debug("Registering push token",{platform:e.platform});const t=this.getJwtHeaders(),n=await this.http.post(Je,{token:e.token,platform:e.platform,...void 0!==e.deviceId?{deviceId:e.deviceId}:{}},t),i=this.extractData(n);return this.logger.debug("Push token registered",{registered:i.registered}),i}async unregisterPushToken(e){this.logger.debug("Unregistering push token");const t=this.getJwtHeaders(),n=await this.http.request({method:"DELETE",url:Ye,data:{token:e},headers:t}),i=this.extractData(n);return this.logger.debug("Push token unregistered",{unregistered:i.unregistered}),i}async getPreferences(){this.logger.debug("Fetching notification preferences");const e=this.getJwtHeaders(),t=await this.http.get(Ze,void 0,e),n=this.extractData(t);return this.logger.debug("Fetched notification preferences",{tradesEnabled:n.tradesEnabled,streamsEnabled:n.streamsEnabled}),{preferences:n}}async updatePreferences(e){this.logger.debug("Updating notification preferences",e);const t=this.getJwtHeaders(),n=await this.http.put(et,e,t),i=this.extractData(n);return this.logger.debug("Updated notification preferences",{tradesEnabled:i.tradesEnabled,streamsEnabled:i.streamsEnabled}),{preferences:i}}}class Dl extends ri{constructor(e,t=!1){super(e,t)}async getHomeOEmbedJson(e={}){this.logger.debug("Fetching home oEmbed (JSON)");const t=await this.http.get(g.OEMBED,{format:"json"});return this.logger.debug("Fetched home oEmbed (JSON)",{title:t.title}),{oembed:t}}async getHomeOEmbedHtml(e={}){this.logger.debug("Fetching home oEmbed (HTML)");const t=await this.http.get(g.OEMBED,{},{responseType:"text"});return this.logger.debug("Fetched home oEmbed (HTML)"),{html:t}}async getPoolOEmbedJson(e){const{tokenName:t}=e;if(null==t||"string"!=typeof t||""===t.trim())throw new wt("tokenName is required","tokenName","REQUIRED_FIELD");this.logger.debug("Fetching pool oEmbed (JSON)",{tokenName:t});const n=g.OEMBED_BUY_SELL.replace(":tokenName",t.toLowerCase().trim()),i=await this.http.get(n,{format:"json"});return this.logger.debug("Fetched pool oEmbed (JSON)",{tokenName:t,title:i.title}),{oembed:i}}async getPoolOEmbedHtml(e){const{tokenName:t}=e;if(null==t||"string"!=typeof t||""===t.trim())throw new wt("tokenName is required","tokenName","REQUIRED_FIELD");this.logger.debug("Fetching pool oEmbed (HTML)",{tokenName:t});const n=g.OEMBED_BUY_SELL.replace(":tokenName",t.toLowerCase().trim()),i=await this.http.get(n,{},{responseType:"text"});return this.logger.debug("Fetched pool oEmbed (HTML)",{tokenName:t}),{html:i}}async getProfileOEmbedJson(e){const{address:t}=e;if(null==t||"string"!=typeof t||""===t.trim())throw new wt("address is required","address","REQUIRED_FIELD");this.logger.debug("Fetching profile oEmbed (JSON)",{address:t});const n=g.OEMBED_PROFILE.replace(":address",encodeURIComponent(t.toLowerCase().trim())),i=await this.http.get(n,{format:"json"});return this.logger.debug("Fetched profile oEmbed (JSON)",{address:t,title:i.title}),{oembed:i}}async getProfileOEmbedHtml(e){const{address:t}=e;if(null==t||"string"!=typeof t||""===t.trim())throw new wt("address is required","address","REQUIRED_FIELD");this.logger.debug("Fetching profile oEmbed (HTML)",{address:t});const n=g.OEMBED_PROFILE.replace(":address",encodeURIComponent(t.toLowerCase().trim())),i=await this.http.get(n,{},{responseType:"text"});return this.logger.debug("Fetched profile oEmbed (HTML)",{address:t}),{html:i}}}function Pl(e){if(!jn(e))throw Ft("address","Address")}const xl={PENDING:"PENDING",CLAIMED:"CLAIMED",REVOKED:"REVOKED",EXPIRED:"EXPIRED"},_l={ACTIVE:"ACTIVE",REVOKED:"REVOKED"},Fl=Xn(xl),Rl=Xn(_l);function Bl(e){if(!jn(e))throw new Error("Address must be a non-empty string");try{vi(e,"address")}catch{throw new Error("Invalid wallet address format. Must be eth|..., 0x..., or client|... format")}}function Ul(e){try{Gn(e,"id")}catch(e){if(e instanceof wt)throw new Error("Invite ID must be a positive integer");throw e}}class Ol extends Yr{constructor(e,t,n,i=!1,o){super(e,t,n,i,o)}validateInviteCodeFormat(e){if(!jn(e))throw Ft("inviteCode","Invite code");!function(e){if(!jn(e))throw new Error("Invite code must be a non-empty string");qn(e,100,"code")}(e)}validateWalletAddressFormat(e){if(!jn(e))throw Ft("address","Address");Bl(e)}async createInvite(e={}){!function(e){try{ni(e.description,"description",255)}catch(e){if(e instanceof wt)throw e.message.includes("255")?new Error("description must be at most 255 characters"):new Error("description must be a string");throw e}if(void 0!==e.expiresAt){if(!jn(e.expiresAt))throw new Error("expiresAt must be a non-empty string");try{ii(e.expiresAt,"expiresAt")}catch(e){if(e instanceof wt)throw new Error("expiresAt must be a valid ISO 8601 date string");throw e}if(dn(e.expiresAt)&&ln(e.expiresAt)<=new Date)throw new Error("expiresAt must be in the future")}}(e),this.validateOptionalString(e.description,"description","Description",vn.DESCRIPTION.MAX_LENGTH),this.validateOptionalDate(e.expiresAt,"expiresAt","Expiration date");const t=oe,n={};void 0!==e.description&&(n.description=e.description),void 0!==e.expiresAt&&(n.expiresAt=e.expiresAt);const i=await this.http.post(t,n,this.getDualAuthHeaders());return{invite:this.extractData(i)}}async listInvites(e={}){!function(e){if(void 0!==e.status&&!Fl(e.status))throw Ht("status",e.status,Object.values(xl),"status");Qn(0,void 0,50,e.pageSize,e.cursor)}(e),this.validateStatusFilter(e.status,xl);const t=ae,n={...this.buildPaginationParams(e,yn.MAX_LIMIT)};void 0!==e.status&&(n.status=e.status);const i=await this.http.get(t,n,this.getDualAuthHeaders()),o=this.extractData(i);return{items:o.invites,pageInfo:o.pageInfo}}async getInviteByCode(e){this.validateInviteCodeFormat(e);const t=this.buildEndpoint(re,{code:e}),n=await this.http.get(t,{});return this.extractData(n)}async revokeInvite(e){Ul(e);const t=this.buildEndpoint(ce,{id:String(e)});jt(await this.http.delete(t,this.getDualAuthHeaders()),"Failed to revoke invite")}async claimInvite(e){this.validateInviteCodeFormat(e);const t=se,n={inviteCode:e},i=await this.http.post(t,n,this.getJwtHeaders());return{invite:this.extractData(i)}}async listOverseers(e={}){!function(e){if(void 0!==e.status&&!Rl(e.status))throw Ht("status",e.status,Object.values(_l),"status");Qn(0,void 0,50,e.pageSize,e.cursor)}(e),this.validateStatusFilter(e.status,_l);const t=le,n={...this.buildPaginationParams(e,yn.MAX_LIMIT)};void 0!==e.status&&(n.status=e.status);const i=await this.http.get(t,n,this.getDualAuthHeaders()),o=this.extractData(i);return{items:o.overseers,pageInfo:o.pageInfo}}async createOverseerDirect(e){!function(e){if(null==e||"object"!=typeof e)throw new wt("Options are required");if(!jn(e.userAddress))throw new wt("userAddress is required and must be a non-empty string");try{vi(e.userAddress,"userAddress")}catch{throw new wt("Invalid wallet address format. Must be eth|..., 0x..., or client|... format")}if(void 0!==e.description){if("string"!=typeof e.description)throw new wt("description must be a string");if(e.description.length>255)throw new wt("description must be at most 255 characters")}}(e),this.validateWalletAddressFormat(e.userAddress),this.validateOptionalString(e.description,"description","Description",vn.DESCRIPTION.MAX_LENGTH);const t=de,n={userAddress:e.userAddress};void 0!==e.description&&(n.description=e.description);const i=await this.http.post(t,n,this.getDualAuthHeaders());return{overseer:this.extractData(i)}}async revokeOverseer(e){this.validateWalletAddressFormat(e);const t=this.buildEndpoint(ue,{address:e});jt(await this.http.delete(t,this.getDualAuthHeaders()),"Failed to revoke overseer")}async getMyStatus(){const e=he,t=await this.http.get(e,{},this.getJwtHeaders());return this.extractData(t)}async getSummary(){const e=ge,t=await this.http.get(e,{},this.getDualAuthHeaders());return this.extractData(t)}async getBanStats(){const e=me,t=await this.http.get(e,{},this.getDualAuthHeaders());return this.extractData(t)}async getTokenBanStats(){const e=pe,t=await this.http.get(e,{},this.getDualAuthHeaders());return this.extractData(t)}async getInviteStats(){const e=fe,t=await this.http.get(e,{},this.getDualAuthHeaders());return this.extractData(t)}async getFlagStats(){const e=ye,t=await this.http.get(e,{},this.getDualAuthHeaders());return this.extractData(t)}async getUserStats(){const e=ke,t=await this.http.get(e,{},this.getDualAuthHeaders());return this.extractData(t)}async listOverseerUsers(e){const t=e??{};Object.keys(t).length>0&&function(e){if(null==e||"object"!=typeof e)throw new wt("Options are required");if(void 0!==e.search){if("string"!=typeof e.search)throw new wt("Search must be a string");if(0===e.search.length)throw new wt("Search query cannot be empty string")}if(void 0!==e.sortBy){if("string"!=typeof e.sortBy)throw new wt("Sort field must be a string");if(0===e.sortBy.length)throw new wt("Sort field cannot be empty string")}if(void 0!==e.sortOrder&&"asc"!==e.sortOrder&&"desc"!==e.sortOrder)throw new wt('Sort order must be "asc" or "desc"')}(t);const n=ve,i=this.buildPaginationParams(t,yn.MAX_LIMIT);void 0!==t.search&&""!==t.search&&(i.search=t.search),void 0!==t.sortBy&&""!==t.sortBy&&(i.sortBy=t.sortBy),void 0!==t.sortOrder&&(i.sortOrder=t.sortOrder);const o=await this.http.get(n,i,this.getDualAuthHeaders()),a=this.extractData(o);return{items:a.users,pageInfo:a.pageInfo}}async getOverseerUserSummary(e){if(!jn(e))throw Ft("address","User address");Bl(e);const t=this.buildEndpoint(we,{address:e}),n=await this.http.get(t,{},this.getDualAuthHeaders());return this.extractData(n)}async flushCacheByAddress(e){Pl(e);const t=this.buildEndpoint(be,{address:e}),n=await this.http.delete(t,void 0,this.getDualAuthHeaders());return this.extractData(n)}async flushCacheByToken(e){!function(e){if(!jn(e))throw Ft("tokenName","Token name")}(e);const t=this.buildEndpoint(Se,{tokenName:e}),n=await this.http.delete(t,void 0,this.getDualAuthHeaders());return this.extractData(n)}async flushAllCache(e){!function(e){if(!0!==e)throw new wt("Safety confirmation required: must pass confirm=true to flush all cache","confirm","CONFIRMATION_REQUIRED")}(e);const t=Ae,n=await this.http.delete(t,{confirm:"true"},this.getDualAuthHeaders());return this.extractData(n)}async flushWalletAliasCache(e){if(void 0!==e){Pl(e);const t=this.buildEndpoint(Te,{address:e}),n=await this.http.delete(t,void 0,this.getDualAuthHeaders());return this.extractData(n)}const t=Ee,n=await this.http.delete(t,{confirm:"true"},this.getDualAuthHeaders());return this.extractData(n)}}class Ll extends ri{constructor(e,t,n=!1){super(e,n,t)}extractData(e){const t={status:200,error:"error"in e?e.error:"success"in e?!e.success:!("data"in e),data:e.data};return void 0!==e.message&&(t.message=e.message),jt(t,"Platform config operation failed",!0),e.data}async getPlatformConfig(){const e=await this.http.get(N);return this.extractData(e)}async updatePlatformConfig(e){const t=function(e){const t=[];return 0===Object.keys(e).length&&t.push("At least one field (streamingEnabled, chatEnabled, commentsEnabled, webhooksEnabled, aiModerationEnabled) must be provided"),void 0!==e.streamingEnabled&&"boolean"!=typeof e.streamingEnabled&&t.push("streamingEnabled must be a boolean"),void 0!==e.chatEnabled&&"boolean"!=typeof e.chatEnabled&&t.push("chatEnabled must be a boolean"),void 0!==e.commentsEnabled&&"boolean"!=typeof e.commentsEnabled&&t.push("commentsEnabled must be a boolean"),void 0!==e.webhooksEnabled&&"boolean"!=typeof e.webhooksEnabled&&t.push("webhooksEnabled must be a boolean"),void 0!==e.aiModerationEnabled&&"boolean"!=typeof e.aiModerationEnabled&&t.push("aiModerationEnabled must be a boolean"),t}(e);if(t.length>0)throw new wt(t.join("; "),"options","VALIDATION_FAILED");const n=await this.http.put(D,e,this.getJwtHeaders());return this.extractData(n)}async getAvailableRoles(){const e=await this.http.get(_);return this.extractData(e)}}class Ml extends ri{constructor(e,t=!1){super(e,t)}extractData(e){if(e.error||void 0===e.data){const t=e.message??"Platform stats operation failed";throw new Error(t)}return e.data}async getStats(){this.logger.debug("Fetching platform stats");const e=await this.http.get(He),t=this.extractData(e);return this.logger.debug("Fetched platform stats",{totalTokens:t.totalTokens,timestamp:t.timestamp}),{stats:t}}}class $l extends ri{constructor(e,t=!1,n){super(e,t),this.tokenResolverService=n}async fetchTokenClassKeyByTokenName(e){if(""===e)throw Ft("tokenName","Token name");if(!this.tokenResolverService)throw Ut("TokenResolverService is required for token name resolution. Ensure it is passed to PriceHistoryService constructor.","tokenResolverService");try{Zn(e)}catch(e){throw Ut(at(e),"tokenName")}this.logger.debug(`Resolving token name '${e}' to token class key`);try{const t=await this.tokenResolverService.resolveTokenToVault(e);if(null==t)throw Ut(`Token '${e}' not found or could not be resolved to vault address`,"tokenName");this.logger.debug(`Resolved '${e}' to vault address: ${t}`);const n=ca(ds(t));return this.logger.debug(`Extracted token class key: ${n}`),n}catch(t){if(ot(t)&&at(t).includes("ConfigurationError"))throw t;throw Bt(`Failed to resolve token name '${e}': ${at(t)}`,500)}}async fetchPriceHistory(e){if(null==e)throw Ft("options","Fetch options");return this.logger.debug("Fetching price history from DEX Backend API with options:",e),this.validateOptions(e),await ea(async()=>{let t=e.tokenId;if(null!=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(null==t)throw Ut("Token ID is required but was not provided or resolved","tokenId");const{normalizeToTokenInstanceKey:n}=await Promise.resolve().then(function(){return ms}),i=Ds(ca(n(t))),{from:o,to:a,sortOrder:r="DESC"}=e,s=_n(e.offset,0),c=_n(e.limit,10),l={token:i,skip:String(s),take:String(c)};o&&(l.from=o.toISOString()),a&&(l.to=a.toISOString());const d=function(e){if(null!=e)return e.toLowerCase()}(r);null!=d&&(l.order=d),this.logger.debug(`Querying price snapshots for token ${i}, offset ${s}, limit ${c}`);const u=await this.http.get("/price-oracle/fetch-price",l);if(null==u)throw Bt("No response from price history service",500);const h=this.transformApiResponseToPriceHistory(u);return this.logger.debug(`Found ${h.snapshots.length} price snapshots, total ${h.total}`),h},"Failed to fetch price history",this.logger)}transformApiResponseToPriceHistory(e){if(!zt(e))throw Bt("Invalid API response: missing data wrapper",500);const t=Vt(e);if(!t||"object"!=typeof t)throw Bt("Invalid API response: data is not an object",500);const n=t,i=n.data;if(!Array.isArray(i))throw Bt("Invalid API response: missing or invalid data.data array",500);const o=n.meta;if(null==o||"object"!=typeof o)throw Bt("Invalid API response: missing data.meta pagination info",500);const a=o,r=i.map(e=>{if("object"!=typeof e||null===e)throw Bt("Invalid API response: invalid snapshot item",500);const t=e;return{price:t.price,timestamp:ln(t.createdAt),tokenId:ca({collection:t.collection,category:t.category,type:t.type,additionalKey:t.additionalKey})}}),s=Fn(a.currentPage,1),c=Fn(a.totalPages,1);return{snapshots:r,page:s,limit:Fn(a.pageSize,50),total:Fn(a.totalItems,0),totalPages:c,hasNext:s<c,hasPrevious:s>1}}async fetchAllPriceHistory(e){if(null==e)throw Ft("options","Fetch options");return this.logger.debug("Fetching all price history with options:",e),ea(async()=>{const t=await cs((t,n)=>this.fetchPriceHistory({...e,offset: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,startPage:0});return function(e,t=e.length,n="items"){const i=e.length;return{offset:0,limit:i,total:t,totalPages:0!==i?Vr(t,i):void 0,hasNext:!1,hasPrevious:!1,[n]:e}}(t.items,t.total,"snapshots")},"Failed to fetch all price history",this.logger)}validateOptions(e){const{from:t,to:n,sortOrder:i}=e;if(Yn(e,"tokenName","tokenId",{description:"token identifier"}),t&&!dn(t))throw Ut("from must be a valid Date","from");if(n&&!dn(n))throw Ut("to must be a valid Date","to");null!=i&&function(e,t){if("ASC"!==e&&"DESC"!==e)throw new wt(`${t} must be either 'ASC' or 'DESC'`,t,"INVALID_ENUM_VALUE")}(i,"sortOrder"),Qn(0,void 0,50,e.pageSize)}}class Kl extends Yr{constructor(e,t,n,i){super(e,t,n,!1,i)}async getRestrictedNames(){try{return{success:!0,data:(await this.http.get(Re)).data}}catch(e){return{success:!1,error:e instanceof Error?e.message:String(e)}}}async updateRestrictedNames(e){try{if(!jn(e.content))throw new wt("Content is required");return{success:!0,data:(await this.http.put(Be,e)).data}}catch(e){return{success:!1,error:e instanceof Error?e.message:String(e)}}}}hn.MIN_LENGTH,hn.MAX_LENGTH,hn.PATTERN;Xn({ENABLED:"ENABLED",DISABLED:"DISABLED",ADMIN_DISABLED:"ADMIN_DISABLED"});Xn({RATE_LIMITED:"RATE_LIMITED",INVALID_EMOJI:"INVALID_EMOJI",NOT_AUTHENTICATED:"NOT_AUTHENTICATED",STREAM_NOT_LIVE:"STREAM_NOT_LIVE"});class ql extends Yr{constructor(e,t,n,i=!1,o){super(e,t,n,i,o)}async getChatStatus(e){if(null==e||""===e)throw Ft("tokenName","Token name");return this.validateTokenName(e,hn),ea(async()=>{const t=this.buildEndpoint(Ue,{tokenName:e}),n=await this.http.get(t),i=this.extractData(n),o={enabled:i.enabled,tokenName:i.tokenName??en(e)};return void 0!==i.status&&null!==i.status&&(o.status=i.status),o},`Failed to get chat status for token: ${e}`,this.logger)}async disableChat(e){if(null==e||""===e)throw Ft("tokenName","Token name");return this.validateTokenName(e,hn),ea(async()=>{const t=this.buildEndpoint(Le,{tokenName:e}),n=await this.http.post(t,{enabled:!1},this.getDualAuthHeaders()),i=this.extractData(n),o={enabled:i.enabled,tokenName:i.tokenName??en(e)};return void 0!==i.status&&null!==i.status&&(o.status=i.status),o},`Failed to disable chat for token: ${e}`,this.logger)}async enableChat(e){if(null==e||""===e)throw Ft("tokenName","Token name");return this.validateTokenName(e,hn),ea(async()=>{const t=this.buildEndpoint(Oe,{tokenName:e}),n=await this.http.post(t,{enabled:!0},this.getDualAuthHeaders()),i=this.extractData(n),o={enabled:i.enabled,tokenName:i.tokenName??en(e)};return void 0!==i.status&&null!==i.status&&(o.status=i.status),o},`Failed to enable chat for token: ${e}`,this.logger)}async setSlowMode(e,t){if(null==e||""===e)throw Ft("tokenName","Token name");if(this.validateTokenName(e,hn),"number"!=typeof t||t<0||t>600)throw new Error("slowModeSeconds must be a number between 0 and 600");return ea(async()=>{const n=this.buildEndpoint(qe,{tokenName:e}),i=await this.http.patch(n,{slowModeSeconds:t},this.getDualAuthHeaders());return this.extractData(i)},`Failed to set slow mode for token: ${e}`,this.logger)}async getGlobalChatStatus(){const e=await this.http.get(Me);return this.extractData(e)}async disableGlobalChat(){const e=await this.http.post(Ke,{enabled:!1},this.getAdminHeaders());return this.extractData(e)}async enableGlobalChat(){const e=await this.http.post($e,{enabled:!0},this.getAdminHeaders());return this.extractData(e)}async setGlobalChatEnabled(e){return e?this.enableGlobalChat():this.disableGlobalChat()}async getEngagementStats(e){if(null==e)throw Ft("options","Engagement stats options");return function(e){if(!jn(e.tokenName))throw Ft("tokenName","Token name");const t=e.tokenName.trim();if(0===t.length)throw Ft("tokenName","Token name");if(!hn.PATTERN.test(t))throw Rt("tokenName",`match pattern ${String(hn.PATTERN)}`,"Token name")}(e),ea(async()=>{const t=this.buildEndpoint(Ge,{tokenName:e.tokenName}),n=await this.http.get(t,void 0,this.getDualAuthHeaders()),i=this.extractData(n);return{tokenName:en(e.tokenName),chat:i.chat,comments:i.comments}},"Failed to get engagement stats",this.logger)}}class Gl extends si{constructor(e=!1){super(e),this.eventRegistry=this.createEmptyRegistry()}setSocket(e){this.logger.debug("📡 [Events] Socket attached to StreamingEventService")}clearSocket(){this.logger.debug("📡 [Events] Socket cleared from StreamingEventService")}createEmptyRegistry(){return{stream_status:new Set,user_banned:new Set,user_unbanned:new Set,ban_enforcement:new Set,content_flagged:new Set,flag_resolved:new Set,stream_chat_message:new Set,stream_chat_updated:new Set,stream_chat_deleted:new Set,stream_chat_pinned:new Set,stream_chat_unpinned:new Set,chat_status_changed:new Set,viewer_count:new Set,recording_status:new Set,simulcast_status:new Set,download_ready:new Set,recordings_count_updated:new Set,user_typing:new Set,stream_reaction:new Set,content_reaction_added:new Set,content_reaction_removed:new Set,stream_countdown_updated:new Set,stream_language_updated:new Set,stream_control_status_changed:new Set,connection:new Set,authenticated:new Set,token_subscribed:new Set,token_unsubscribed:new Set,room_subscribed:new Set,room_left:new Set}}registerCallback(e,t){const n=this.eventRegistry[e];return n.add(t),()=>{n.delete(t)}}async emitEvent(e,t){const n=this.eventRegistry[e];if(0!==n.size)for(const i of n)try{const e=i(t);e instanceof Promise&&await e}catch(t){this.logger.error(`Error in ${e} callback:`,t instanceof Error?t.message:String(t))}}onStreamStatusChanged(e){return this.registerCallback("stream_status",e)}onUserBanned(e){return this.registerCallback("user_banned",e)}onUserUnbanned(e){return this.registerCallback("user_unbanned",e)}onBanEnforcement(e){return this.registerCallback("ban_enforcement",e)}onContentFlagged(e){return this.registerCallback("content_flagged",e)}onFlagResolved(e){return this.registerCallback("flag_resolved",e)}onStreamChatMessage(e){return this.registerCallback("stream_chat_message",e)}onStreamChatUpdated(e){return this.registerCallback("stream_chat_updated",e)}onStreamChatDeleted(e){return this.registerCallback("stream_chat_deleted",e)}onStreamChatPinned(e){return this.registerCallback("stream_chat_pinned",e)}onStreamChatUnpinned(e){return this.registerCallback("stream_chat_unpinned",e)}onChatStatusChanged(e){return this.registerCallback("chat_status_changed",e)}onViewerCountChanged(e){return this.registerCallback("viewer_count",e)}onRecordingStatusChanged(e){return this.registerCallback("recording_status",e)}onSimulcastStatusChanged(e){return this.registerCallback("simulcast_status",e)}onDownloadReady(e){return this.registerCallback("download_ready",e)}onRecordingsCountUpdated(e){return this.registerCallback("recordings_count_updated",e)}onUserTyping(e){return this.registerCallback("user_typing",e)}onStreamReaction(e){return this.registerCallback("stream_reaction",e)}onContentReactionAdded(e){return this.registerCallback("content_reaction_added",e)}onContentReactionRemoved(e){return this.registerCallback("content_reaction_removed",e)}onStreamCountdownUpdated(e){return this.registerCallback("stream_countdown_updated",e)}onStreamLanguageUpdated(e){return this.registerCallback("stream_language_updated",e)}onStreamControlStatusChanged(e){return this.registerCallback("stream_control_status_changed",e)}onConnection(e){return this.registerCallback("connection",e)}onAuthenticated(e){return this.registerCallback("authenticated",e)}onTokenSubscribed(e){return this.registerCallback("token_subscribed",e)}onTokenUnsubscribed(e){return this.registerCallback("token_unsubscribed",e)}onRoomSubscribed(e){return this.registerCallback("room_subscribed",e)}onRoomLeft(e){return this.registerCallback("room_left",e)}async emitStreamStatusChanged(e){await this.emitEvent("stream_status",e)}async emitUserBanned(e){await this.emitEvent("user_banned",e)}async emitUserUnbanned(e){await this.emitEvent("user_unbanned",e)}async emitBanEnforcement(e){await this.emitEvent("ban_enforcement",e)}async emitContentFlagged(e){await this.emitEvent("content_flagged",e)}async emitFlagResolved(e){await this.emitEvent("flag_resolved",e)}async emitStreamChatMessage(e){await this.emitEvent("stream_chat_message",e)}async emitStreamChatUpdated(e){await this.emitEvent("stream_chat_updated",e)}async emitStreamChatDeleted(e){await this.emitEvent("stream_chat_deleted",e)}async emitStreamChatPinned(e){await this.emitEvent("stream_chat_pinned",e)}async emitStreamChatUnpinned(e){await this.emitEvent("stream_chat_unpinned",e)}async emitChatStatusChanged(e){await this.emitEvent("chat_status_changed",e)}async emitViewerCountChanged(e){await this.emitEvent("viewer_count",e)}async emitRecordingStatusChanged(e){await this.emitEvent("recording_status",e)}async emitSimulcastStatusChanged(e){await this.emitEvent("simulcast_status",e)}async emitDownloadReady(e){await this.emitEvent("download_ready",e)}async emitRecordingsCountUpdated(e){await this.emitEvent("recordings_count_updated",e)}async emitUserTyping(e){await this.emitEvent("user_typing",e)}async emitStreamReaction(e){await this.emitEvent("stream_reaction",e)}async emitContentReactionAdded(e){await this.emitEvent("content_reaction_added",e)}async emitContentReactionRemoved(e){await this.emitEvent("content_reaction_removed",e)}async emitStreamCountdownUpdated(e){await this.emitEvent("stream_countdown_updated",e)}async emitStreamLanguageUpdated(e){await this.emitEvent("stream_language_updated",e)}async emitStreamControlStatusChanged(e){await this.emitEvent("stream_control_status_changed",e)}async emitConnection(e){await this.emitEvent("connection",e)}async emitAuthenticated(e){await this.emitEvent("authenticated",e)}async emitTokenSubscribed(e){await this.emitEvent("token_subscribed",e)}async emitTokenUnsubscribed(e){await this.emitEvent("token_unsubscribed",e)}async emitRoomSubscribed(e){await this.emitEvent("room_subscribed",e)}async emitRoomLeft(e){await this.emitEvent("room_left",e)}}Xn({IDLE:"IDLE",ACTIVE:"ACTIVE",DISABLED:"DISABLED"}),Xn({READY:"READY",PROCESSING:"PROCESSING",ERRORED:"ERRORED",DELETED:"DELETED"});const Wl=Xn({YOUTUBE:"YOUTUBE",TWITCH:"TWITCH",FACEBOOK:"FACEBOOK",CUSTOM:"CUSTOM"});const Hl={OWNER:"OWNER",MANAGER:"MANAGER",TECHNICAL_PRODUCER:"TECHNICAL_PRODUCER",MODERATOR:"MODERATOR"};Xn(Hl),Xn({OWNER:"OWNER",MODERATOR:"MODERATOR"});function zl(e,t="tokenName"){Zn(e,t,hn)}function jl(e){if(zl(e.tokenName),!jn(e.language))throw Ft("language");if(t=e.language,!jn(t)||!/^[a-z]{2}(-[A-Z]{2})?$/.test(t))throw new wt('language must be a valid ISO 639-1 code (e.g., "en", "es", "zh-CN")',"language",yt);var t}Xn({...Hl,OVERSEER:"OVERSEER"}),Xn({OWNERSHIP:"ownership",MODERATOR_INVITE:"moderator_invite",OVERSEER:"overseer"});class Vl extends Yr{constructor(e,t,n,i=!1,o){super(e,t,n,i,o)}async startStream(e){this.validateTokenName(e,hn);const t=this.buildEndpoint(y,{tokenName:e}),n=await this.http.post(t,{},this.getJwtHeaders());return this.extractData(n)}async stopStream(e){this.validateTokenName(e,hn);const t=this.buildEndpoint(k,{tokenName:e});await this.http.post(t,{},this.getJwtHeaders())}async disableStream(e){return this.toggleFeature(e,v,hn)}async enableStream(e){return this.toggleFeature(e,w,hn)}async resetStreamKey(e){this.validateTokenName(e,hn);const t=this.buildEndpoint(b,{tokenName:e}),n=await this.http.post(t,{},this.getJwtHeaders());return this.extractData(n)}async getStreamCredentials(e){this.validateTokenName(e,hn);const t=this.buildEndpoint(R,{tokenName:e}),n=await this.http.get(t,{},this.getJwtHeaders());return this.extractData(n)}async getRecordings(e){!function(e){zl(e.tokenName),Qn(0,void 0,Cn,e.pageSize)}(e);const t=this.buildEndpoint(S,{tokenName:e.tokenName}),n=this.buildPaginationParams(e,Cn),i=e.offset;"number"==typeof i&&(n.offset=String(i));const o=await this.http.get(t,n),a=this.extractData(o),r=e,s=a.pageSize??a.limit??e.pageSize??("number"==typeof r.limit?r.limit:20),c="number"==typeof r.offset?r.offset:0,l=Math.floor(c/s)+1;return{recordings:a.recordings,tokenName:en(e.tokenName),page:a.page??l,limit:s,total:a.total,hasNext:a.recordings.length===s}}async getRecordingDownload(e,t){this.validateTokenName(e,hn),this.validateRequiredString(t,"assetId","Asset ID");const n=this.buildEndpoint(A,{tokenName:e,assetId:t}),i=await this.http.get(n,{},this.getJwtHeaders());return this.extractData(i)}async deleteRecording(e,t){this.validateTokenName(e,hn),this.validateRequiredString(t,"assetId","Asset ID");const n=this.buildEndpoint(T,{tokenName:e,assetId:t});await this.http.delete(n,void 0,this.getJwtHeaders())}async getSimulcastTargets(e){this.validateTokenName(e,hn);const t=this.buildEndpoint(E,{tokenName:e}),n=await this.http.get(t);return this.extractData(n)}async addSimulcastTarget(e){!function(e){if(zl(e.tokenName),!jn(e.platform))throw Ft("platform");if(!Wl(e.platform))throw new wt("platform must be one of: 'YOUTUBE', 'TWITCH', 'FACEBOOK', 'CUSTOM'","platform",ft);if(!jn(e.rtmpUrl))throw Ft("rtmpUrl");if(!En.STREAM_URL_PATTERN.test(e.rtmpUrl))throw new wt("rtmpUrl must be a valid RTMP, RTMPS, or SRT URL","rtmpUrl",yt);if(!jn(e.streamKey))throw Ft("streamKey");if(void 0!==e.name&&"string"!=typeof e.name)throw new wt("name must be a string","name",pt)}(e);const t=this.buildEndpoint(C,{tokenName:e.tokenName}),n=await this.http.post(t,{platform:e.platform,rtmpUrl:e.rtmpUrl,streamKey:e.streamKey,name:e.name},this.getJwtHeaders());return this.extractData(n)}async removeSimulcastTarget(e,t){this.validateTokenName(e,hn),this.validateRequiredString(t,"targetId","Target ID");const n=this.buildEndpoint(I,{tokenName:e,targetId:t});await this.http.delete(n,void 0,this.getJwtHeaders())}async getGlobalStreamingStatus(){const e=await this.http.get(N);return this.extractData(e)}async disableGlobalStreaming(){const e=await this.http.post(D,{},this.getAdminHeaders());return this.extractData(e)}async enableGlobalStreaming(){const e=await this.http.post(P,{},this.getAdminHeaders());return this.extractData(e)}async setNextLiveStream(e){let t;!function(e){if(zl(e.tokenName),null!==e.nextLiveStreamAt){if(!(e.nextLiveStreamAt instanceof Date||jn(e.nextLiveStreamAt)))throw new wt("nextLiveStreamAt must be a Date, ISO 8601 string, or null","nextLiveStreamAt",pt);if("string"==typeof e.nextLiveStreamAt){const t=Date.parse(e.nextLiveStreamAt);if(isNaN(t))throw new wt("nextLiveStreamAt must be a valid ISO 8601 date string","nextLiveStreamAt",yt)}}}(e),t=null===e.nextLiveStreamAt?null:e.nextLiveStreamAt instanceof Date?e.nextLiveStreamAt.toISOString():e.nextLiveStreamAt;const n=`/v1/tokens/${e.tokenName.toLowerCase().trim()}`;await this.http.patch(n,{nextLiveStreamAt:t},this.getJwtHeaders())}async clearNextLiveStream(e){this.validateTokenName(e,hn);const t=`/v1/tokens/${e.toLowerCase().trim()}`;await this.http.patch(t,{nextLiveStreamAt:null},this.getJwtHeaders())}async setStreamLanguage(e){jl(e);const t=`/v1/tokens/${e.tokenName.toLowerCase().trim()}`;await this.http.patch(t,{streamLanguage:e.language},this.getJwtHeaders())}async getStreamRole(e){!function(e){zl(e.tokenName)}(e);const t=this.buildEndpoint(x,{tokenName:e.tokenName}),n=this.jwtAuth?this.getJwtHeaders():{},i=await this.http.get(t,{},n);return this.extractData(i)}async getAvailableRoles(){const e=await this.http.get(_);return this.extractData(e)}async getTokenAccess(e){!function(e){zl(e.tokenName)}(e);const t=this.buildEndpoint(F,{tokenName:e.tokenName}),n=this.jwtAuth?this.getJwtHeaders():{};this.jwtAuth||this.logger.debug("getTokenAccess called without JWT authentication - will return hasAccess: false",{tokenName:e.tokenName});const i=await this.http.get(t,{},n);return this.extractData(i)}async setNextLiveStreamCountdown(e,t){return this.setNextLiveStream({tokenName:e,nextLiveStreamAt:t})}}const Ql="subscribe_token",Xl="unsubscribe_token",Jl="authenticate",Yl="send_stream_chat",Zl="send_stream_reaction",ed="typing_start",td="typing_stop",nd="STREAM_STATUS",id="STREAM_COUNTDOWN_UPDATED",od="STREAM_LANGUAGE_UPDATED",ad="VIEWER_COUNT",rd="token_subscribed",sd="token_unsubscribed",cd="STREAM_GLOBAL_STATUS",ld="ENGAGEMENT_STATS_UPDATED",dd="STREAM_CHAT_MESSAGE",ud="STREAM_CHAT_SENT",hd="stream_chat_error",gd="CHAT_STATUS_CHANGED",md="STREAM_CHAT_GLOBAL_STATUS",pd="authenticated",fd="stream_chat_auth_error",yd="STREAM_REACTION",kd="stream_reaction_error",vd="USER_TYPING",wd="STREAM_CHAT_PINNED",bd="STREAM_CHAT_UNPINNED",Sd="COMMENT_CREATED",Ad="TOKEN_BANNED",Td="TOKEN_UNBANNED",Ed="MODERATOR_INVITE_CREATED",Cd="MODERATOR_ADDED",Id="MODERATOR_REMOVED",Nd="MODERATOR_ROLE_UPDATED",Dd="OVERSEER_ADDED",Pd="OVERSEER_REMOVED",xd="TRADE_EXECUTED",_d="BALANCE_UPDATED",Fd="STATS_UPDATED",Rd="POOL_METADATA_UPDATED",Bd="POOL_SOCIAL_LINKS_UPDATED",Ud="POOL_BADGE_ASSIGNED",Od="HOLDER_COUNT_UPDATED",Ld="USER_PROFILE_UPDATED",Md="API_KEY_CREATED",$d="API_KEY_UPDATED",Kd="API_KEY_REVOKED",qd="TOKEN_CONFIG_UPDATED",Gd="SITE_CONFIG_CHANGED",Wd="COMMENTS_GLOBAL_STATUS",Hd="WEBHOOKS_GLOBAL_STATUS",zd="subscribe_global_feed",jd="unsubscribe_global_feed",Vd="global_feed_subscribed",Qd="global_feed_unsubscribed";class Xd extends si{constructor(e,t=!1){if(super(t),this.socket=null,this.isAuthenticated=!1,this.subscribedRooms=new Map,this.roomCallbacks=new Map,this.pendingSubscriptions=new Map,this.eventBuffer=new Map,this.eventBufferTimeouts=new Map,this.MAX_BUFFER_SIZE=100,this.BUFFER_CLEANUP_MS=3e4,this.globalCallbacks={},this.isGlobalFeedSubscribed=!1,this.globalFeedCallbacks={},this.pendingGlobalFeedSubscription=null,""===e.url)throw new St("Stream WebSocket URL is required. Set streamWebSocketUrl in SDK config.");this.config={url:e.url,authToken:e.authToken??"",reconnectAttempts:e.reconnectAttempts??5,reconnectDelay:e.reconnectDelay??2e3,subscriptionTimeout:e.subscriptionTimeout??1e4},this.reconnectionManager=new dl({maxAttempts:this.config.reconnectAttempts,baseDelayMs:this.config.reconnectDelay}),this.isSocketIOAvailable=this.checkSocketIOAvailability()}checkSocketIOAvailability(){try{return"function"==typeof h.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:",at(e)),!1}}getRoomName(e){return`token:${en(e)}`}bufferEvent(e,t,n){const i=`${e}:${t}`;let o=this.eventBuffer.get(i);o||(o=[],this.eventBuffer.set(i,o)),o.length>=this.MAX_BUFFER_SIZE&&(o.shift(),this.logger.warn(`⚠️ [Stream Buffer] Event buffer overflow for ${i} - dropping oldest event. Consider processing events faster or increasing buffer size.`)),o.push(n);const a=this.eventBufferTimeouts.get(i);a&&clearTimeout(a);const r=setTimeout(()=>{this.eventBuffer.delete(i),this.eventBufferTimeouts.delete(i),this.logger.debug(`📡 [Stream Buffer] Cleaned up buffer for ${i}`)},this.BUFFER_CLEANUP_MS);this.eventBufferTimeouts.set(i,r)}processBufferedEvents(e,t){const n=en(e),i=[{key:`${nd}:${n}`,callback:t.onStreamStatus},{key:`${ad}:${n}`,callback:t.onViewerCount},{key:`${dd}:${n}`,callback:t.onChatMessage},{key:`${gd}:${n}`,callback:t.onChatStatus},{key:`${wd}:${n}`,callback:t.onChatPinned},{key:`${bd}:${n}`,callback:t.onChatUnpinned},{key:`${id}:${n}`,callback:t.onCountdownUpdated},{key:`${od}:${n}`,callback:t.onLanguageUpdated},{key:`${Ad}:${n}`,callback:t.onTokenBanned},{key:`${Td}:${n}`,callback:t.onTokenUnbanned},{key:`${Ed}:${n}`,callback:t.onModeratorInviteCreated},{key:`${Cd}:${n}`,callback:t.onModeratorAdded},{key:`${Id}:${n}`,callback:t.onModeratorRemoved},{key:`${Nd}:${n}`,callback:t.onModeratorRoleUpdated},{key:`${xd}:${n}`,callback:t.onTradeExecuted},{key:`${_d}:${n}`,callback:t.onBalanceUpdated},{key:`${Rd}:${n}`,callback:t.onPoolMetadataUpdated},{key:`${Bd}:${n}`,callback:t.onPoolSocialLinksUpdated},{key:`${Ud}:${n}`,callback:t.onPoolBadgeAssigned},{key:`${Od}:${n}`,callback:t.onHolderCountUpdated},{key:`${Sd}:${n}`,callback:t.onCommentCreated}];for(const{key:e,callback:t}of i){const n=this.eventBuffer.get(e);if(n&&t){this.logger.debug(`📡 [Stream Buffer] Delivering ${n.length} buffered events for ${e}`);for(const i of n)try{t?.(i)}catch(t){this.logger.error(`Error delivering buffered event for ${e}:`,t)}this.eventBuffer.delete(e);const i=this.eventBufferTimeouts.get(e);i&&(clearTimeout(i),this.eventBufferTimeouts.delete(e))}}}setupGlobalListeners(){this.socket&&(this.socket.on(nd,e=>{this.logger.debug(`📡 [Stream Status] ${e.tokenName}: ${e.status}`),this.bufferEvent(nd,e.tokenName,e);const t=this.roomCallbacks.get(en(e.tokenName));t?.onStreamStatus&&t.onStreamStatus(e),this.globalCallbacks.onStreamStatus&&this.globalCallbacks.onStreamStatus(e),this.isGlobalFeedSubscribed&&!t&&this.globalFeedCallbacks.onStreamStatus&&this.globalFeedCallbacks.onStreamStatus(e)}),this.socket.on(rd,e=>{this.logger.debug(`📡 [Subscribed] ${e.tokenName}`);const t=en(e.tokenName),n=this.pendingSubscriptions.get(t);n&&(clearTimeout(n.timeoutId),this.pendingSubscriptions.delete(t),n.resolve(e));const i=this.roomCallbacks.get(t);i?.onStreamSubscribed&&i.onStreamSubscribed(e)}),this.socket.on(sd,e=>{const t=e.room.replace("token:","");this.logger.debug(`📡 [Unsubscribed] ${t}`);const n=this.roomCallbacks.get(en(t));n?.onStreamUnsubscribed&&n.onStreamUnsubscribed(e)}),this.socket.on(ad,e=>{this.logger.debug(`📡 [Viewer Count] ${e.tokenName}: ${e.viewerCount}`),this.bufferEvent(ad,e.tokenName,e);const t=this.roomCallbacks.get(en(e.tokenName));t?.onViewerCount&&t.onViewerCount(e),this.globalCallbacks.onViewerCount&&this.globalCallbacks.onViewerCount(e),this.isGlobalFeedSubscribed&&!t&&this.globalFeedCallbacks.onViewerCount&&this.globalFeedCallbacks.onViewerCount(e)}),this.socket.on(cd,e=>{this.logger.debug(`📡 [Global Stream Status] enabled: ${e.enabled}`),this.globalCallbacks.onStreamGlobalStatus&&this.globalCallbacks.onStreamGlobalStatus(e)}),this.socket.on(dd,e=>{this.logger.debug(`📡 [Chat Message] ${e.tokenName}: ${e.content.slice(0,50)}...`),this.bufferEvent(dd,e.tokenName,e);const t=this.roomCallbacks.get(en(e.tokenName));t?.onChatMessage&&t.onChatMessage(e),this.globalCallbacks.onChatMessage&&this.globalCallbacks.onChatMessage(e)}),this.socket.on(ud,e=>{this.logger.debug(`📡 [Chat Sent] ${e.tokenName}: ${e.messageId}`);const t=this.roomCallbacks.get(en(e.tokenName));t?.onChatSent&&t.onChatSent(e)}),this.socket.on(hd,e=>{this.logger.error(`📡 [Chat Error] ${e.tokenName}: ${e.message}`);const t=this.roomCallbacks.get(en(e.tokenName));t?.onChatError&&t.onChatError(e),this.globalCallbacks.onChatError&&this.globalCallbacks.onChatError(e)}),this.socket.on(gd,e=>{this.logger.debug(`📡 [Chat Status] ${e.tokenName}: ${e.enabled}`),this.bufferEvent(gd,e.tokenName,e);const t=this.roomCallbacks.get(en(e.tokenName));t?.onChatStatus&&t.onChatStatus(e),this.globalCallbacks.onChatStatus&&this.globalCallbacks.onChatStatus(e)}),this.socket.on(md,e=>{this.logger.debug(`📡 [Global Chat Status] enabled: ${e.enabled}`),this.globalCallbacks.onChatGlobalStatus&&this.globalCallbacks.onChatGlobalStatus(e)}),this.socket.on(pd,e=>{this.logger.debug(`📡 [Authenticated] ${e.address}`),this.isAuthenticated=!0,this.globalCallbacks.onChatAuthenticated&&this.globalCallbacks.onChatAuthenticated(e)}),this.socket.on(fd,e=>{this.logger.error(`📡 [Auth Error] ${e.message}`),this.isAuthenticated=!1,this.globalCallbacks.onChatAuthError&&this.globalCallbacks.onChatAuthError(e)}),this.socket.on(yd,e=>{this.logger.debug(`📡 [Reaction] ${e.tokenName}: ${e.emoji}`);const t=this.roomCallbacks.get(en(e.tokenName));t?.onReaction&&t.onReaction(e),this.globalCallbacks.onReaction&&this.globalCallbacks.onReaction(e)}),this.socket.on(kd,e=>{this.logger.error(`📡 [Reaction Error] ${e.tokenName}: ${e.message}`);const t=this.roomCallbacks.get(en(e.tokenName));t?.onReactionError&&t.onReactionError(e),this.globalCallbacks.onReactionError&&this.globalCallbacks.onReactionError(e)}),this.socket.on(vd,e=>{const t=e.typingUsers.length;this.logger.debug(`📡 [Typing] ${e.tokenName}: ${t} user(s) typing`);const n=this.roomCallbacks.get(en(e.tokenName));n?.onTypingIndicator&&n.onTypingIndicator(e),this.globalCallbacks.onTypingIndicator&&this.globalCallbacks.onTypingIndicator(e)}),this.socket.on(wd,e=>{this.logger.debug(`📡 [Chat Pinned] ${e.tokenName}: ${e.pinnedMessage.messageId}`),this.bufferEvent(wd,e.tokenName,e);const t=this.roomCallbacks.get(en(e.tokenName));t?.onChatPinned&&t.onChatPinned(e),this.globalCallbacks.onChatPinned&&this.globalCallbacks.onChatPinned(e)}),this.socket.on(bd,e=>{this.logger.debug(`📡 [Chat Unpinned] ${e.tokenName}: ${e.unpinnedMessageId}`),this.bufferEvent(bd,e.tokenName,e);const t=this.roomCallbacks.get(en(e.tokenName));t?.onChatUnpinned&&t.onChatUnpinned(e),this.globalCallbacks.onChatUnpinned&&this.globalCallbacks.onChatUnpinned(e)}),this.socket.on(id,e=>{const t=e.nextLiveStreamAt??"cleared";this.logger.debug(`📡 [Countdown Updated] ${e.tokenName}: ${t}`),this.bufferEvent(id,e.tokenName,e);const n=this.roomCallbacks.get(en(e.tokenName));n?.onCountdownUpdated&&n.onCountdownUpdated(e),this.globalCallbacks.onCountdownUpdated&&this.globalCallbacks.onCountdownUpdated(e)}),this.socket.on(od,e=>{this.logger.debug(`📡 [Language Updated] ${e.tokenName}: ${e.language}`),this.bufferEvent(od,e.tokenName,e);const t=this.roomCallbacks.get(en(e.tokenName));t?.onLanguageUpdated&&t.onLanguageUpdated(e),this.globalCallbacks.onLanguageUpdated&&this.globalCallbacks.onLanguageUpdated(e)}),this.socket.on(Ad,e=>{this.logger.debug(`📡 [Token Banned] ${e.tokenName}`),this.bufferEvent(Ad,e.tokenName,e);const t=this.roomCallbacks.get(en(e.tokenName));t?.onTokenBanned&&t.onTokenBanned(e),this.globalCallbacks.onTokenBanned&&this.globalCallbacks.onTokenBanned(e)}),this.socket.on(Td,e=>{this.logger.debug(`📡 [Token Unbanned] ${e.tokenName}`),this.bufferEvent(Td,e.tokenName,e);const t=this.roomCallbacks.get(en(e.tokenName));t?.onTokenUnbanned&&t.onTokenUnbanned(e),this.globalCallbacks.onTokenUnbanned&&this.globalCallbacks.onTokenUnbanned(e)}),this.socket.on(Ed,e=>{this.logger.debug(`📡 [Moderator Invite Created] ${e.tokenName}: by ${e.invitedBy}`),this.bufferEvent(Ed,e.tokenName,e);const t=this.roomCallbacks.get(en(e.tokenName));t?.onModeratorInviteCreated&&t.onModeratorInviteCreated(e)}),this.socket.on(Cd,e=>{this.logger.debug(`📡 [Moderator Added] ${e.tokenName}: ${e.userAddress}`),this.bufferEvent(Cd,e.tokenName,e);const t=this.roomCallbacks.get(en(e.tokenName));t?.onModeratorAdded&&t.onModeratorAdded(e)}),this.socket.on(Id,e=>{this.logger.debug(`📡 [Moderator Removed] ${e.tokenName}: ${e.userAddress}`),this.bufferEvent(Id,e.tokenName,e);const t=this.roomCallbacks.get(en(e.tokenName));t?.onModeratorRemoved&&t.onModeratorRemoved(e)}),this.socket.on(Nd,e=>{this.logger.debug(`📡 [Moderator Role Updated] ${e.tokenName}: ${e.userAddress}`),this.bufferEvent(Nd,e.tokenName,e);const t=this.roomCallbacks.get(en(e.tokenName));t?.onModeratorRoleUpdated&&t.onModeratorRoleUpdated(e)}),this.socket.on(Dd,e=>{this.logger.debug(`📡 [Overseer Added] ${e.userAddress}`),this.globalCallbacks.onOverseerAdded&&this.globalCallbacks.onOverseerAdded(e)}),this.socket.on(Pd,e=>{this.logger.debug(`📡 [Overseer Removed] ${e.userAddress}`),this.globalCallbacks.onOverseerRemoved&&this.globalCallbacks.onOverseerRemoved(e)}),this.socket.on(xd,e=>{this.logger.debug(`📡 [Trade Executed] ${e.tokenName}: ${e.tradeType}`),this.bufferEvent(xd,e.tokenName,e);const t=this.roomCallbacks.get(en(e.tokenName));t?.onTradeExecuted&&t.onTradeExecuted(e),this.isGlobalFeedSubscribed&&!t&&this.globalFeedCallbacks.onTradeExecuted&&this.globalFeedCallbacks.onTradeExecuted(e)}),this.socket.on(_d,e=>{this.logger.debug(`📡 [Balance Updated] ${e.tokenName}: ${e.tradeType}`),this.bufferEvent(_d,e.tokenName,e);const t=this.roomCallbacks.get(en(e.tokenName));t?.onBalanceUpdated&&t.onBalanceUpdated(e)}),this.socket.on(Rd,e=>{this.logger.debug(`📡 [Pool Metadata Updated] ${e.tokenName}`),this.bufferEvent(Rd,e.tokenName,e);const t=this.roomCallbacks.get(en(e.tokenName));t?.onPoolMetadataUpdated&&t.onPoolMetadataUpdated(e)}),this.socket.on(Bd,e=>{this.logger.debug(`📡 [Pool Social Links Updated] ${e.tokenName}`),this.bufferEvent(Bd,e.tokenName,e);const t=this.roomCallbacks.get(en(e.tokenName));t?.onPoolSocialLinksUpdated&&t.onPoolSocialLinksUpdated(e)}),this.socket.on(Ud,e=>{this.logger.debug(`📡 [Pool Badge Assigned] ${e.tokenName}: ${e.badge}`),this.bufferEvent(Ud,e.tokenName,e);const t=this.roomCallbacks.get(en(e.tokenName));t?.onPoolBadgeAssigned&&t.onPoolBadgeAssigned(e),this.isGlobalFeedSubscribed&&!t&&this.globalFeedCallbacks.onPoolBadgeAssigned&&this.globalFeedCallbacks.onPoolBadgeAssigned(e)}),this.socket.on(Od,e=>{this.logger.debug(`📡 [Holder Count Updated] ${e.tokenName}: ${e.holderCount}`),this.bufferEvent(Od,e.tokenName,e);const t=this.roomCallbacks.get(en(e.tokenName));t?.onHolderCountUpdated&&t.onHolderCountUpdated(e),this.isGlobalFeedSubscribed&&!t&&this.globalFeedCallbacks.onHolderCountUpdated&&this.globalFeedCallbacks.onHolderCountUpdated(e)}),this.socket.on(Sd,e=>{this.logger.debug(`📡 [Comment Created] ${e.tokenName}: ID ${e.id}`),this.bufferEvent(Sd,e.tokenName,e);const t=this.roomCallbacks.get(en(e.tokenName));t?.onCommentCreated&&t.onCommentCreated(e)}),this.socket.on(Ld,e=>{this.logger.debug(`📡 [User Profile Updated] ${e.userAddress}`),this.globalCallbacks.onUserProfileUpdated&&this.globalCallbacks.onUserProfileUpdated(e)}),this.socket.on(Md,e=>{this.logger.debug(`📡 [API Key Created] ${e.keyId}`),this.globalCallbacks.onApiKeyCreated&&this.globalCallbacks.onApiKeyCreated(e)}),this.socket.on($d,e=>{this.logger.debug(`📡 [API Key Updated] ${e.keyId}`),this.globalCallbacks.onApiKeyUpdated&&this.globalCallbacks.onApiKeyUpdated(e)}),this.socket.on(Kd,e=>{this.logger.debug(`📡 [API Key Revoked] ${e.keyId}`),this.globalCallbacks.onApiKeyRevoked&&this.globalCallbacks.onApiKeyRevoked(e)}),this.socket.on(qd,e=>{this.logger.debug(`📡 [Token Config Updated] ${e.tokenName}`),this.bufferEvent(qd,e.tokenName,e);const t=this.roomCallbacks.get(en(e.tokenName));t?.onTokenConfigUpdated&&t.onTokenConfigUpdated(e),this.isGlobalFeedSubscribed&&!t&&this.globalFeedCallbacks.onTokenConfigUpdated&&this.globalFeedCallbacks.onTokenConfigUpdated(e)}),this.socket.on(Fd,e=>{this.logger.debug(`📡 [Stats Updated] ${e.tokenName} price=${e.galaPrice}`),this.bufferEvent(Fd,e.tokenName,e);const t=this.roomCallbacks.get(en(e.tokenName));t?.onStatsUpdated&&t.onStatsUpdated(e),this.globalCallbacks.onStatsUpdated&&this.globalCallbacks.onStatsUpdated(e),this.isGlobalFeedSubscribed&&!t&&this.globalFeedCallbacks.onStatsUpdated&&this.globalFeedCallbacks.onStatsUpdated(e)}),this.socket.on(ld,e=>{this.logger.debug(`📡 [Engagement Stats Updated] ${e.tokenName} chatters=${e.chat.uniqueChatters}`),this.bufferEvent(ld,e.tokenName,e);const t=this.roomCallbacks.get(en(e.tokenName));t?.onEngagementStatsUpdated&&t.onEngagementStatsUpdated(e),this.globalCallbacks.onEngagementStatsUpdated&&this.globalCallbacks.onEngagementStatsUpdated(e)}),this.socket.on(Vd,e=>{if(this.logger.debug("📡 [Global Feed Subscribed]"),this.isGlobalFeedSubscribed=!0,this.pendingGlobalFeedSubscription){const{resolve:t,timeoutId:n}=this.pendingGlobalFeedSubscription;clearTimeout(n),this.pendingGlobalFeedSubscription=null,t(e)}this.globalFeedCallbacks.onGlobalFeedSubscribed&&this.globalFeedCallbacks.onGlobalFeedSubscribed(e)}),this.socket.on(Qd,e=>{this.logger.debug("📡 [Global Feed Unsubscribed]"),this.isGlobalFeedSubscribed=!1,this.globalFeedCallbacks.onGlobalFeedUnsubscribed&&this.globalFeedCallbacks.onGlobalFeedUnsubscribed(e)}),this.socket.on(Gd,e=>{this.logger.debug(`📡 [Site Config Changed] ${e.key}`),this.globalFeedCallbacks.onSiteConfigChanged&&this.globalFeedCallbacks.onSiteConfigChanged(e)}),this.socket.on(cd,e=>{this.logger.debug(`📡 [Stream Global Status] enabled=${e.enabled}`),this.globalFeedCallbacks.onStreamGlobalStatus&&this.globalFeedCallbacks.onStreamGlobalStatus(e)}),this.socket.on(md,e=>{this.logger.debug(`📡 [Chat Global Status] enabled=${e.enabled}`),this.globalFeedCallbacks.onStreamChatGlobalStatus&&this.globalFeedCallbacks.onStreamChatGlobalStatus(e)}),this.socket.on(Wd,e=>{this.logger.debug(`📡 [Comments Global Status] enabled=${e.enabled}`),this.globalFeedCallbacks.onCommentsGlobalStatus&&this.globalFeedCallbacks.onCommentsGlobalStatus(e)}),this.socket.on(Hd,e=>{this.logger.debug(`📡 [Webhooks Global Status] enabled=${e.enabled}`),this.globalFeedCallbacks.onWebhooksGlobalStatus&&this.globalFeedCallbacks.onWebhooksGlobalStatus(e)}))}resubscribeAll(){const e=Array.from(this.subscribedRooms.keys());this.logger.debug(`📡 Re-subscribing to ${e.length} rooms after reconnect`);for(const t of e)try{this.roomCallbacks.get(t)&&this.socket?.emit(Ql,{tokenName:t})}catch(e){this.logger.error(`Failed to re-subscribe to ${t}:`,e)}if(this.isGlobalFeedSubscribed)try{this.logger.debug("📡 Re-subscribing to global feed after reconnect"),this.socket?.emit(zd)}catch(e){this.logger.error("Failed to re-subscribe to global feed:",e)}}async connect(){return new Promise((e,t)=>{ea(async()=>{if(!this.isSocketIOAvailable)throw new St('Socket.IO not available. Install "socket.io-client" package.');this.logger.debug(`🔌 Connecting to Stream WebSocket: ${this.config.url}`);const n={};return""!==this.config.authToken&&(n.token=this.config.authToken),this.socket=h.io(this.config.url,{transports:["websocket"],reconnection:!0,reconnectionAttempts:this.config.reconnectAttempts,reconnectionDelay:this.config.reconnectDelay,auth:n}),this.socket.on("connect",()=>{this.logger.debug(`✅ Stream WebSocket connected: ${String(this.socket?.id)}`),this.reconnectionManager.reset(),this.setupGlobalListeners(),this.subscribedRooms.size>0&&this.resubscribeAll(),e()}),this.socket.on("connect_error",e=>{this.logger.error("❌ Stream WebSocket connection error:",e),t(e)}),this.socket.on("disconnect",e=>{this.logger.debug(`🔌 Stream WebSocket disconnected: ${e}`),this.isAuthenticated=!1}),this.socket.on("error",e=>{this.logger.error("❌ Stream WebSocket error:",e)}),this.socket.io.on("reconnect",e=>{this.logger.debug(`🔄 Stream WebSocket reconnected after ${e} attempts`),this.reconnectionManager.reset(),""!==this.config.authToken&&this.authenticate(this.config.authToken)}),this.socket.io.on("reconnect_attempt",()=>{this.reconnectionManager.recordAttempt(),this.logger.debug(`🔄 Stream WebSocket reconnect attempt ${this.reconnectionManager.getStatusString()}`)}),this.socket.io.on("reconnect_failed",()=>{this.logger.error("❌ Stream WebSocket max reconnection attempts reached")}),Promise.resolve(void 0)},"Stream WebSocket connection setup",this.logger,e=>{throw t(e),e})})}authenticate(e){if(!this.socket?.connected)throw new wt("WebSocket not connected. Call connect() first.","socket","NOT_CONNECTED");if(""===e)throw Ft("token","Authentication token");this.logger.debug("📡 Authenticating with stream server"),this.socket.emit(Jl,{token:e}),this.config.authToken=e}async subscribeToStream(e,t={}){if(""===e)throw Ft("tokenName","Token name");if(!this.socket?.connected)throw new wt("WebSocket not connected. Call connect() first.","socket","NOT_CONNECTED");const n=en(e);return this.subscribedRooms.has(n)?(this.logger.debug(`📡 Already subscribed to ${n}, updating callbacks`),this.roomCallbacks.set(n,t),this.processBufferedEvents(n,t),{tokenName:n,room:this.getRoomName(e)}):new Promise((i,o)=>{const a=setTimeout(()=>{this.pendingSubscriptions.delete(n),o(new Error(`Subscription to ${e} timed out after ${this.config.subscriptionTimeout}ms`))},this.config.subscriptionTimeout);this.pendingSubscriptions.set(n,{resolve:i,reject:o,timeoutId:a}),this.roomCallbacks.set(n,t),this.subscribedRooms.set(n,{tokenName:n,subscribedAt:Date.now()}),this.logger.debug(`📡 Subscribing to stream: ${n}`),this.socket.emit(Ql,{tokenName:n})})}unsubscribeFromStream(e){const t=en(e);if(!this.subscribedRooms.has(t))return void this.logger.debug(`📡 Not subscribed to ${t}, skipping unsubscribe`);this.logger.debug(`📡 Unsubscribing from stream: ${t}`),this.socket?.connected&&this.socket.emit(Xl,{tokenName:t}),this.subscribedRooms.delete(t),this.roomCallbacks.delete(t);const n=this.pendingSubscriptions.get(t);n&&(clearTimeout(n.timeoutId),this.pendingSubscriptions.delete(t));for(const e of this.eventBuffer.keys())if(e.endsWith(`:${t}`)){this.eventBuffer.delete(e);const t=this.eventBufferTimeouts.get(e);t&&(clearTimeout(t),this.eventBufferTimeouts.delete(e))}}sendChatMessage(e,t){if(""===e)throw Ft("tokenName","Token name");if(""===t)throw Ft("content","Message content");if(!this.socket?.connected)throw new wt("WebSocket not connected. Call connect() first.","socket","NOT_CONNECTED");if(!this.isAuthenticated)throw new wt("Not authenticated. Call authenticate() first.","auth","NOT_AUTHENTICATED");const n=en(e);this.logger.debug(`📡 Sending chat message to ${n}: ${t.slice(0,30)}...`),this.socket.emit(Yl,{tokenName:n,content:t})}sendReaction(e,t,n=0){if(""===e)throw Ft("tokenName","Token name");if(""===t)throw Ft("emoji");if(!this.socket?.connected)throw new wt("WebSocket not connected. Call connect() first.","socket","NOT_CONNECTED");if(!this.isAuthenticated)throw new wt("Not authenticated. Call authenticate() first.","auth","NOT_AUTHENTICATED");const i=en(e);this.logger.debug(`📡 Sending reaction to ${i}: ${t}`),this.socket.emit(Zl,{tokenName:i,emoji:t,streamTime:n})}sendTypingStart(e){if(""===e)throw Ft("tokenName","Token name");if(!this.socket?.connected)throw new wt("WebSocket not connected. Call connect() first.","socket","NOT_CONNECTED");if(!this.isAuthenticated)throw new wt("Not authenticated. Call authenticate() first.","auth","NOT_AUTHENTICATED");const t=en(e);this.logger.debug(`📡 Sending typing_start to ${t}`),this.socket.emit(ed,{tokenName:t})}sendTypingStop(e){if(""===e)throw Ft("tokenName","Token name");if(!this.socket?.connected)throw new wt("WebSocket not connected. Call connect() first.","socket","NOT_CONNECTED");if(!this.isAuthenticated)throw new wt("Not authenticated. Call authenticate() first.","auth","NOT_AUTHENTICATED");const t=en(e);this.logger.debug(`📡 Sending typing_stop to ${t}`),this.socket.emit(td,{tokenName:t})}subscribeTokenBanned(e,t){const n=en(e),i={onTokenBanned:t};return this.roomCallbacks.set(n,{...this.roomCallbacks.get(n),...i}),()=>{const e=this.roomCallbacks.get(n);if(e){const{onTokenBanned:t,...i}=e;this.roomCallbacks.set(n,i)}}}subscribeTokenUnbanned(e,t){const n=en(e),i={onTokenUnbanned:t};return this.roomCallbacks.set(n,{...this.roomCallbacks.get(n),...i}),()=>{const e=this.roomCallbacks.get(n);if(e){const{onTokenUnbanned:t,...i}=e;this.roomCallbacks.set(n,i)}}}subscribeModeratorInviteCreated(e,t){const n=en(e),i={onModeratorInviteCreated:t};return this.roomCallbacks.set(n,{...this.roomCallbacks.get(n),...i}),()=>{const e=this.roomCallbacks.get(n);if(e){const{onModeratorInviteCreated:t,...i}=e;this.roomCallbacks.set(n,i)}}}subscribeModeratorAdded(e,t){const n=en(e),i={onModeratorAdded:t};return this.roomCallbacks.set(n,{...this.roomCallbacks.get(n),...i}),()=>{const e=this.roomCallbacks.get(n);if(e){const{onModeratorAdded:t,...i}=e;this.roomCallbacks.set(n,i)}}}subscribeModeratorRemoved(e,t){const n=en(e),i={onModeratorRemoved:t};return this.roomCallbacks.set(n,{...this.roomCallbacks.get(n),...i}),()=>{const e=this.roomCallbacks.get(n);if(e){const{onModeratorRemoved:t,...i}=e;this.roomCallbacks.set(n,i)}}}subscribeModeratorRoleUpdated(e,t){const n=en(e),i={onModeratorRoleUpdated:t};return this.roomCallbacks.set(n,{...this.roomCallbacks.get(n),...i}),()=>{const e=this.roomCallbacks.get(n);if(e){const{onModeratorRoleUpdated:t,...i}=e;this.roomCallbacks.set(n,i)}}}subscribeOverseerAdded(e){return this.globalCallbacks.onOverseerAdded=e,()=>{delete this.globalCallbacks.onOverseerAdded}}subscribeOverseerRemoved(e){return this.globalCallbacks.onOverseerRemoved=e,()=>{delete this.globalCallbacks.onOverseerRemoved}}subscribeTradeExecuted(e,t){const n=en(e),i={onTradeExecuted:t};return this.roomCallbacks.set(n,{...this.roomCallbacks.get(n),...i}),()=>{const e=this.roomCallbacks.get(n);if(e){const{onTradeExecuted:t,...i}=e;this.roomCallbacks.set(n,i)}}}subscribePoolMetadataUpdated(e,t){const n=en(e),i={onPoolMetadataUpdated:t};return this.roomCallbacks.set(n,{...this.roomCallbacks.get(n),...i}),()=>{const e=this.roomCallbacks.get(n);if(e){const{onPoolMetadataUpdated:t,...i}=e;this.roomCallbacks.set(n,i)}}}subscribePoolSocialLinksUpdated(e,t){const n=en(e),i={onPoolSocialLinksUpdated:t};return this.roomCallbacks.set(n,{...this.roomCallbacks.get(n),...i}),()=>{const e=this.roomCallbacks.get(n);if(e){const{onPoolSocialLinksUpdated:t,...i}=e;this.roomCallbacks.set(n,i)}}}subscribePoolBadgeAssigned(e,t){const n=en(e),i={onPoolBadgeAssigned:t};return this.roomCallbacks.set(n,{...this.roomCallbacks.get(n),...i}),()=>{const e=this.roomCallbacks.get(n);if(e){const{onPoolBadgeAssigned:t,...i}=e;this.roomCallbacks.set(n,i)}}}subscribeHolderCountUpdated(e,t){const n=en(e),i={onHolderCountUpdated:t};return this.roomCallbacks.set(n,{...this.roomCallbacks.get(n),...i}),()=>{const e=this.roomCallbacks.get(n);if(e){const{onHolderCountUpdated:t,...i}=e;this.roomCallbacks.set(n,i)}}}subscribeCommentCreated(e,t){const n=en(e),i={onCommentCreated:t};return this.roomCallbacks.set(n,{...this.roomCallbacks.get(n),...i}),()=>{const e=this.roomCallbacks.get(n);if(e){const{onCommentCreated:t,...i}=e;this.roomCallbacks.set(n,i)}}}subscribeUserProfileUpdated(e){return this.globalCallbacks.onUserProfileUpdated=e,()=>{delete this.globalCallbacks.onUserProfileUpdated}}subscribeApiKeyCreated(e){return this.globalCallbacks.onApiKeyCreated=e,()=>{delete this.globalCallbacks.onApiKeyCreated}}subscribeApiKeyUpdated(e){return this.globalCallbacks.onApiKeyUpdated=e,()=>{delete this.globalCallbacks.onApiKeyUpdated}}subscribeApiKeyRevoked(e){return this.globalCallbacks.onApiKeyRevoked=e,()=>{delete this.globalCallbacks.onApiKeyRevoked}}subscribeStatsUpdated(e){return this.globalCallbacks.onStatsUpdated=e,()=>{delete this.globalCallbacks.onStatsUpdated}}subscribeEngagementStatsUpdated(e){return this.globalCallbacks.onEngagementStatsUpdated=e,()=>{delete this.globalCallbacks.onEngagementStatsUpdated}}setGlobalCallbacks(e){this.globalCallbacks=e}getSubscribedTokens(){return Array.from(this.subscribedRooms.keys())}isConnected(){return this.socket?.connected??!1}isAuthenticatedForChat(){return this.isAuthenticated}async subscribeToGlobalFeed(e){if(!this.socket?.connected)throw new wt("WebSocket not connected. Call connect() first.","socket","NOT_CONNECTED");return this.isGlobalFeedSubscribed?(this.logger.debug("📡 Already subscribed to global feed, updating callbacks"),e&&(this.globalFeedCallbacks=e),{room:"global:feed"}):new Promise((t,n)=>{const i=setTimeout(()=>{this.pendingGlobalFeedSubscription=null,n(new Error(`Global feed subscription timed out after ${this.config.subscriptionTimeout}ms`))},this.config.subscriptionTimeout);this.pendingGlobalFeedSubscription={resolve:t,reject:n,timeoutId:i},e&&(this.globalFeedCallbacks=e),this.logger.debug("📡 Subscribing to global feed"),this.socket.emit(zd)})}unsubscribeFromGlobalFeed(){this.isGlobalFeedSubscribed?(this.logger.debug("📡 Unsubscribing from global feed"),this.socket?.connected&&this.socket.emit(jd),this.isGlobalFeedSubscribed=!1,this.globalFeedCallbacks={},this.pendingGlobalFeedSubscription&&(clearTimeout(this.pendingGlobalFeedSubscription.timeoutId),this.pendingGlobalFeedSubscription=null)):this.logger.debug("📡 Not subscribed to global feed, skipping unsubscribe")}isSubscribedToGlobalFeed(){return this.isGlobalFeedSubscribed}setGlobalFeedCallbacks(e){this.globalFeedCallbacks=e,this.logger.debug("📡 Updated global feed callbacks")}getSocket(){return this.socket}disconnect(){if(this.socket){this.logger.debug("🔌 Disconnecting from Stream WebSocket");try{for(const[e,t]of this.pendingSubscriptions){try{clearTimeout(t.timeoutId),t.reject(new Error("WebSocket disconnected"))}catch(t){this.logger.error(`Error cleaning up pending subscription for ${e}:`,t)}this.pendingSubscriptions.delete(e)}}catch(e){this.logger.error("Error clearing pending subscriptions:",e)}try{this.subscribedRooms.clear(),this.roomCallbacks.clear()}catch(e){this.logger.error("Error clearing room tracking:",e)}try{for(const e of this.eventBufferTimeouts.values())clearTimeout(e);this.eventBuffer.clear(),this.eventBufferTimeouts.clear()}catch(e){this.logger.error("Error clearing event buffers:",e)}this.globalCallbacks={};try{this.globalFeedCallbacks={},this.isGlobalFeedSubscribed=!1,this.pendingGlobalFeedSubscription&&(clearTimeout(this.pendingGlobalFeedSubscription.timeoutId),this.pendingGlobalFeedSubscription.reject(new Error("WebSocket disconnected")),this.pendingGlobalFeedSubscription=null)}catch(e){this.logger.error("Error clearing global feed state:",e)}try{this.socket.disconnect()}catch(e){this.logger.error("Error disconnecting socket:",e)}this.socket=null,this.isAuthenticated=!1,this.logger.debug("✅ Stream WebSocket disconnected and cleaned up")}}}function Jd(e){if(Zn(e.tokenName,"tokenName",hn),!(null==(t=e.reason)||""===t||"string"==typeof t&&!ti(t)&&t.length<=vn.BAN_REASON.MAX_LENGTH))throw new wt(`reason must be at most ${vn.BAN_REASON.MAX_LENGTH} characters`,"reason","TOO_LONG");var t}class Yd extends Yr{constructor(e,t,n,i=!1,o){super(e,t,n,i,o)}async banToken(e){Jd(e);const t=Ce,n={tokenName:en(e.tokenName)};if(void 0!==e.reason)try{Hn(e.reason,"reason",!0),e.reason.trim().length>0&&(n.reason=e.reason)}catch{}const i=await this.http.post(t,n,this.getDualAuthHeaders());return{ban:this.extractData(i),tokenName:en(e.tokenName)}}async unbanToken(e){!function(e){Zn(e.tokenName,"tokenName",hn)}(e);const t=this.buildEndpoint(De,{tokenName:e.tokenName});return jt(await this.http.delete(t,void 0,this.getDualAuthHeaders()),"Failed to unban token",!1),{tokenName:en(e.tokenName)}}async listTokenBans(e={}){!function(e){Qn(0,void 0,pn,e.pageSize)}(e);const t=Ie,n=this.buildPaginationParams(e,pn);this.addOptionalFilterParams(n,e,["search"]);const i=await this.http.get(t,n,this.getDualAuthHeaders()),o=this.extractData(i);return{items:o.bans,pageInfo:o.pageInfo}}async getTokenBan(e){!function(e){Zn(e.tokenName,"tokenName",hn)}(e);const t=this.buildEndpoint(Ne,{tokenName:e.tokenName}),n=await this.http.get(t,{},this.getDualAuthHeaders()),i=this.extractDataOrNull(n);return{banned:null!==i,...null!==i&&{ban:i},tokenName:en(e.tokenName)}}async isTokenBanned(e){return this.getTokenBan(e)}}class Zd extends si{constructor(e,t=!1){super(t),this.poolService=e,this.cache=new Map,this.accessOrder=new Set,this.cacheHits=0,this.cacheMisses=0}async resolveTokenToVault(e){if(!jn(e))throw Ft("tokenName","Token name");const t=en(e),n=this.getCacheEntry(t);if(null!==n)return this.cacheHits++,this.updateAccessOrder(t),n.vaultAddress;this.cacheMisses++;try{const n=await this.poolService.resolveTokenNameToVault(e);if(null!==n&&""!==n){const e=this.parseVaultAddressToTokenClassKey(n);this.setCacheEntry(t,{vaultAddress:n,tokenClassKey:e,cachedAt:Date.now()})}return n}catch{return null}}async resolveTokenClassKey(e){if(!jn(e))throw Ft("tokenName","Token name");const t=en(e),n=this.getCacheEntry(t);if(null!==n)return this.cacheHits++,this.updateAccessOrder(t),n.tokenClassKey;this.cacheMisses++;const i=await this.poolService.resolveTokenNameToVault(e);if(null===i||""===i)throw _t(e);const o=this.parseVaultAddressToTokenClassKey(i);return this.setCacheEntry(t,{vaultAddress:i,tokenClassKey:o,cachedAt:Date.now()}),o}get(e){const t=this.getCacheEntry(en(e));return t?.vaultAddress??null}getCacheEntry(e){return this.cache.get(e)??null}setCacheEntry(e,t){this.cache.size>=Zd.MAX_CACHE_SIZE&&!this.cache.has(e)&&this.evictOldestEntry(),this.cache.set(e,t),this.updateAccessOrder(e)}updateAccessOrder(e){this.accessOrder.delete(e),this.accessOrder.add(e)}evictOldestEntry(){const e=this.accessOrder.values().next().value;void 0!==e&&""!==e&&(this.cache.delete(e),this.accessOrder.delete(e))}set(e,t){const n=en(e),i=this.parseVaultAddressToTokenClassKey(t);this.setCacheEntry(n,{vaultAddress:t,tokenClassKey:i,cachedAt:Date.now()})}clear(){this.cache.clear(),this.accessOrder=new Set,this.cacheHits=0,this.cacheMisses=0}getStats(){const e=Array.from(this.cache.values()).map(e=>e.cachedAt),t=this.cacheHits+this.cacheMisses;return{size:this.cache.size,keys:Array.from(this.cache.keys()),maxSize:Zd.MAX_CACHE_SIZE,hits:this.cacheHits,misses:this.cacheMisses,hitRate:t>0?this.cacheHits/t:0,oldestEntry:e.length>0?Math.min(...e):null,newestEntry:e.length>0?Math.max(...e):null}}getCachedTokenClassKey(e){const t=this.getCacheEntry(en(e));return t?.tokenClassKey??null}preWarm(e){for(const{tokenName:t,vaultAddress:n}of e)this.set(t,n)}preWarmWithTokenClassKey(e){for(const{tokenName:t,vaultAddress:n,tokenClassKey:i}of e){const e=en(t);this.setCacheEntry(e,{vaultAddress:n,tokenClassKey:i,cachedAt:Date.now()})}}parseVaultAddressToTokenClassKey(e){try{return ds(e)}catch(e){if(e instanceof wt)throw Rt("vaultAddress","format: service|Token$Unit$SYMBOL$eth:address$launchpad","Vault address");throw e}}}Zd.MAX_CACHE_SIZE=1e4;class eu extends ri{constructor(e,t=!1){super(e,t)}async fetchLaunchpadFee(){return Xt(()=>this.http.get(ze),{errorContext:"Failed to fetch launchpad fee"})}async fetchSaleDetails(e){return Xt(()=>this.http.get(je,{tokenName:e}),{errorContext:`Failed to fetch sale details for ${e}`})}async getTradeQuote(e){return Xt(()=>this.http.get(Ve,{tokenName:e.tokenName,amount:e.amount,type:e.type,method:e.method}),{errorContext:`Failed to get trade quote for ${e.tokenName}`})}async getPremintQuote(e){return Xt(()=>this.http.get(Qe,{amount:e}),{errorContext:"Failed to get premint quote"})}}class tu extends Yr{constructor(e,t,n,i=!1,o){super(e,t,n,i,o)}async emitTradeExecuted(e,t){this.validateTokenName(e,gn);const n={action:"trade_executed",tokenName:e,payload:t};await this.emitEvent(n)}async emitBalanceUpdated(e,t,n){this.validateRequiredString(e,"userAddress","User Address"),this.validateTokenName(t,gn);const i={action:"balance_updated",userAddress:e,tokenName:t,payload:n};await this.emitEvent(i)}async emitStreamStatus(e,t,n=!0){this.validateTokenName(e,gn);const i={action:"stream_status",tokenName:e,payload:t,broadcast:n};await this.emitEvent(i)}async emitViewerCount(e,t,n=!1){this.validateTokenName(e,gn);const i={action:"viewer_count",tokenName:e,payload:t,broadcast:n};await this.emitEvent(i)}async emitStreamCountdown(e,t,n=!0){this.validateTokenName(e,gn);const i={action:"stream_countdown",tokenName:e,payload:t,broadcast:n};await this.emitEvent(i)}async emitChatMessage(e,t,n=!1){this.validateTokenName(e,gn);const i={action:"chat_message",tokenName:e,payload:t,broadcast:n};await this.emitEvent(i)}async emitChatStatus(e,t,n=!1){this.validateTokenName(e,gn);const i={action:"chat_status",tokenName:e,payload:t,broadcast:n};await this.emitEvent(i)}async emitTypingIndicator(e,t,n=!1){this.validateTokenName(e,gn);const i={action:"typing_indicator",tokenName:e,payload:t,broadcast:n};await this.emitEvent(i)}async emitTokenBanned(e,t){this.validateTokenName(e,gn);const n={action:"token_banned",tokenName:e,payload:t,broadcast:!0};await this.emitEvent(n)}async emitTokenUnbanned(e,t){this.validateTokenName(e,gn);const n={action:"token_unbanned",tokenName:e,payload:t,broadcast:!0};await this.emitEvent(n)}async emitModeratorAdded(e,t,n=!1){this.validateTokenName(e,gn);const i={action:"moderator_added",tokenName:e,payload:t,broadcast:n};await this.emitEvent(i)}async emitModeratorRemoved(e,t,n=!1){this.validateTokenName(e,gn);const i={action:"moderator_removed",tokenName:e,payload:t,broadcast:n};await this.emitEvent(i)}async emitUserProfileUpdated(e,t){this.validateRequiredString(e,"userAddress","User Address");const n={action:"user_profile_updated",userAddress:e,payload:t};await this.emitEvent(n)}async emitSiteConfigChanged(e){const t={action:"site_config_changed",payload:e,broadcast:!0};await this.emitEvent(t)}async emitFeatureStatus(e){const t={action:"feature_status",payload:e,broadcast:!0};await this.emitEvent(t)}async emitEvent(e){this.validateRequiredString(e.action,"action","Event Action");const t=this.buildEndpoint("/v1/websocket",{});await this.http.post(t,e,this.getDualAuthHeaders())}async getConnectedClients(e){const t=this.buildEndpoint("/v1/connected-clients",{}),n={};void 0!==e?.room&&""!==e.room&&(n.room=e.room),void 0!==e?.tokenName&&(n.tokenName=e.tokenName),void 0!==e?.userAddress&&(n.userAddress=e.userAddress),void 0!==e?.userAddresses&&(n.userAddresses=e.userAddresses),void 0!==e?.authenticated&&(n.authenticated=e.authenticated),void 0!==e?.isOverseer&&(n.isOverseer=e.isOverseer),void 0!==e?.minConnectionSeconds&&(n.minConnectionSeconds=e.minConnectionSeconds),void 0!==e?.offset&&(n.offset=e.offset),void 0!==e?.limit&&(n.limit=e.limit);const i=await this.http.get(t,Object.keys(n).length>0?n:void 0,this.getDualAuthHeaders());return this.extractData(i)}async isUserOnline(e){this.validateRequiredString(e,"userAddress","User Address");const t=await this.getConnectedClients({userAddress:e,limit:1});if(0===t.clients.length)return{online:!1};const n=t.clients[0];return{online:!0,socketId:n.socketId,rooms:n.rooms,connectedAt:n.connectedAt}}async getOnlineUsers(e){if(!Array.isArray(e)||0===e.length)throw new St("userAddresses must be a non-empty array");const t=await this.getConnectedClients({userAddresses:e.join(","),limit:e.length}),n=t.clients.filter(e=>"string"==typeof e.address&&""!==e.address).map(e=>e.address);return{online:n,offline:e.filter(e=>!n.includes(e)),clients:t.clients.map(e=>{const t={socketId:e.socketId,rooms:e.rooms};return"string"==typeof e.address&&(t.address=e.address),t})}}async getTokenViewers(e,t){this.validateTokenName(e,gn);const n={tokenName:e};void 0!==t?.authenticated&&(n.authenticated=t.authenticated),void 0!==t?.limit&&(n.limit=t.limit);const i=await this.getConnectedClients(n);return{clients:i.clients,count:i.summary.totalConnected,authenticatedCount:i.summary.authenticatedCount}}async getOnlineOverseers(){const e=await this.getConnectedClients({isOverseer:!0});return{clients:e.clients,count:e.summary.totalConnected}}async emitRecordingStatus(e,t){this.validateTokenName(e,gn);const n={action:"recording_status",tokenName:e,payload:t};await this.emitEvent(n)}async emitRecordingsCountUpdated(e,t){this.validateTokenName(e,gn);const n={action:"recordings_count_updated",tokenName:e,payload:t};await this.emitEvent(n)}async emitDownloadReady(e,t){this.validateTokenName(e,gn);const n={action:"download_ready",tokenName:e,payload:t};await this.emitEvent(n)}}var nu,iu;!function(e){e.PROCESSED="PROCESSED",e.COMPLETED="COMPLETED",e.SUCCESS="SUCCESS",e.FAILED="FAILED",e.ERROR="ERROR",e.PROCESSING="PROCESSING",e.PENDING="PENDING"}(nu||(nu={})),function(e){e.PENDING="pending",e.PROCESSING="processing",e.COMPLETED="completed",e.FAILED="failed",e.TIMEOUT="timeout"}(iu||(iu={}));const ou={[nu.PROCESSED]:iu.COMPLETED,[nu.COMPLETED]:iu.COMPLETED,[nu.SUCCESS]:iu.COMPLETED,[nu.FAILED]:iu.FAILED,[nu.ERROR]:iu.FAILED,[nu.PROCESSING]:iu.PROCESSING,[nu.PENDING]:iu.PENDING};class au{constructor(e=0){this.defaultTtlMs=e,this.cache=new Map}get(e){const t=this.cache.get(e);if(t){if(!(void 0!==t.expiresAt&&Date.now()>t.expiresAt))return t.value;this.cache.delete(e)}}set(e,t,n){const i=n??this.defaultTtlMs,o={value:t};i>0&&(o.expiresAt=Date.now()+i),this.cache.set(e,o)}has(e){return void 0!==this.get(e)}delete(e){this.cache.delete(e)}clear(){this.cache.clear()}get size(){return this.cache.size}}async function ru(e,t,n,i={}){const{logger:o,cacheNullish:a=!1,keyGenerator:r}=i,s=r?r(e):String(e),c=n.get(s);if(void 0!==c)return o&&o.debug(`Cache hit for key: ${s}`),c;o&&o.debug(`Cache miss for key: ${s}, fetching...`);const l=await t();return null!=l?(n.set(s,l),o&&o.debug(`Cached result for key: ${s}`)):a&&(n.set(s,l),o&&o.debug(`Cached nullish result for key: ${s}`)),l}class su extends si{constructor(e,t=!1){super(t),this.socket=null,this.listeners=new Map,this.timeouts=new Map,this.hasOnAnyListener=!1,this.MAX_BUFFER_SIZE=1e3,this.config={reconnectAttempts:5,reconnectDelay:2e3,timeout:3e5,...e},this.debug=t,this.reconnectionManager=new dl({maxAttempts:this.config.reconnectAttempts??5,baseDelayMs:this.config.reconnectDelay??2e3}),this.eventBuffer=new au(3e4),this.isSocketIOAvailable=this.checkSocketIOAvailability(),su.instances.add(this)}checkSocketIOAvailability(){try{return"function"==typeof h.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)=>{ea(async()=>{if(!this.isSocketIOAvailable){const e=new Error('Socket.IO not available in current environment. Install "socket.io-client" package.');throw this.logger.error("❌ Socket.IO connection failed:",at(e)),e}this.logger.debug("🔌 Connecting to Socket.IO server:",this.config.url),this.socket=h.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.reconnectionManager.reset(),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],i=n.status??n.Status;"string"==typeof i&&(this.logger.debug(`📡 [Event Buffer] Buffering event for ${e}: ${i}`),this.eventBuffer.size>=this.MAX_BUFFER_SIZE&&this.logger.warn(`📡 [Event Buffer] Buffer approaching limit (${this.eventBuffer.size}/${this.MAX_BUFFER_SIZE})`),this.eventBuffer.set(e,n))}this.debug&&this.logger.debug(`📡 [WebSocket Event] "${e}":`,JSON.stringify(t,null,2))}),this.hasOnAnyListener=!0},"Socket.IO connection failed",this.logger,e=>{throw this.logger.error("Socket.IO connection failed:",e),t(e),e}).catch(t)})}async monitorTransaction(e,t){this.listeners.set(e,t),this.logger.debug(`📡 Starting to monitor transaction: ${e}`),this.logger.debug(`📡 WebSocket connected: ${!!this.socket&&this.socket.connected}`);const n=this.eventBuffer.get(e);null!=n&&(this.logger.debug(`📡 [Event Buffer] Found buffered event for ${e}, delivering immediately`),setImmediate(()=>{this.processTransactionEvent(e,n,t)}),this.eventBuffer.delete(e));const i=this.config.timeout??3e5,o=setTimeout(()=>{if(this.listeners.has(e)){const n=Math.round(i/1e3),o={transactionId:e,status:iu.TIMEOUT,message:`Transaction monitoring timeout - no response after ${n} seconds`,timestamp:Date.now()};this.logger.debug(`📡 Transaction timeout for ${e} (${n}s)`),t(o),this.listeners.delete(e),this.timeouts.delete(e),this.socket?.off(e)}},i);if(this.timeouts.set(e,o),this.socket?.connected)this.socket.off(e),this.logger.debug(`📡 Listening for transaction updates: ${e}`),this.logger.debug(`📡 WebSocket connection ID: ${String(this.socket.id)}`),this.logger.debug(`📡 WebSocket URL: ${this.config.url}`),this.socket.on(e,n=>{this.processTransactionEvent(e,n,t)});else{const n={transactionId:e,status:iu.FAILED,message:"WebSocket not connected - cannot monitor transaction",timestamp:Date.now()};t(n),this.listeners.delete(e),this.timeouts.delete(e)}}processTransactionEvent(e,t,n){this.logger.debug(`📡 Socket.IO transaction update for ${e}:`,JSON.stringify(t,null,2));const i=t,o=i?.data,a=i?.status??i?.Status??o?.status??o?.Status;let r=i?.message??i?.Message??o?.message??o?.Message??i?.error??o?.error;jn(r)||(r=a===nu.FAILED||a===nu.ERROR?"Transaction failed - check transaction details":a===nu.COMPLETED||a===nu.PROCESSED||a===nu.SUCCESS?"Transaction completed successfully":null!=a?`Transaction status: ${String(a)}`:"Unknown transaction status");const s=i?.blockHash??o?.blockHash,c=i?.gasUsed??o?.gasUsed,l=i?.Data??o?.Data,d={transactionId:e,status:this.mapSocketStatus(a),message:"string"==typeof r?r:"Transaction update received",timestamp:Date.now(),...null!=s?{blockHash:s}:{},...null!=c?{gasUsed:c}:{},...null!=l?{data:l}:{}};if(this.logger.debug(`📡 Mapped status for ${e}: ${String(a)} -> ${d.status}`),this.logger.debug(`📡 Final message: "${String(r)}"`),n(d),d.status===iu.COMPLETED||d.status===iu.FAILED){this.listeners.delete(e);const t=this.timeouts.get(e);t&&(clearTimeout(t),this.timeouts.delete(e)),this.socket?.off(e),this.logger.debug(`📡 Cleaned up listener for ${e} (${d.status})`)}}async waitForTransaction(e){return new Promise((t,n)=>{this.monitorTransaction(e,e=>{e.status===iu.COMPLETED?t(e):e.status!==iu.FAILED&&e.status!==iu.TIMEOUT||n(new Error(`Transaction ${String(e.status)}: ${e.message}`))})})}mapSocketStatus(e){const t=e?.toUpperCase();return ou[t]??iu.PENDING}async handleReconnect(){this.reconnectionManager.shouldRetry()?(this.reconnectionManager.recordAttempt(),this.logger.debug(`🔄 Attempting Socket.IO reconnect ${this.reconnectionManager.getStatusString()}`),setTimeout(()=>{this.socket&&!this.socket.connected&&this.socket.connect()},this.reconnectionManager.getNextDelay())):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.logger.debug("🧹 Cleared event buffer"),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")),su.instances.delete(this)}static cleanupAll(e=!1){for(const e of su.instances)e.disconnect();su.instances.clear()}isConnected(){return this.socket?.connected??!1}getSocket(){return this.socket}}su.instances=new Set;class cu extends ri{constructor(e,t=!1){super(e,t)}extractData(e){if(e.error||void 0===e.data){const t=e.message??"Weekly challenge operation failed";throw new Error(t)}return e.data}async getLeaderboard(e={}){const{limit:t=10}=e;if(t<1||t>50)throw new wt("limit must be between 1 and 50","limit","INVALID_LIMIT");this.logger.debug("Fetching weekly challenge leaderboard",{limit:t});const n={limit:String(t)},i=await this.http.get(Xe.GET_LEADERBOARD,n),o=this.extractData(i);return this.logger.debug("Fetched weekly challenge leaderboard",{count:o.count}),{leaderboard:o}}async getTokenHistory(e){const{vaultAddress:t}=e;if("string"!=typeof t||""===t.trim())throw new wt("vaultAddress is required","vaultAddress","REQUIRED_FIELD");this.logger.debug("Fetching token weekly history",{vaultAddress:t});const n=Xe.GET_TOKEN_HISTORY.replace(":vaultAddress",encodeURIComponent(t)),i=await this.http.get(n),o=this.extractData(i);return this.logger.debug("Fetched token weekly history",{count:o.length}),{snapshots:o}}}class lu extends ci{constructor(e=!1){super(e),this.lastFetchedAt=null}normalizeKey(e){return tn(e)}hasItems(){return this.cache.size>0}getAllItems(){return Array.from(this.cache.values())}setAll(e){this.cache.clear();for(const t of e){const e=this.extractKey(t),n=this.normalizeKey(e);this.cache.set(n,t)}this.lastFetchedAt=Date.now(),this.logger.debug(`Cached ${e.length} items`)}merge(e){for(const t of e){const e=this.extractKey(t),n=this.normalizeKey(e);this.cache.set(n,t)}this.lastFetchedAt=Date.now(),this.logger.debug(`Merged ${e.length} items (total: ${this.cache.size})`)}getFetchTimestamp(){return this.lastFetchedAt}clear(){super.clear(),this.lastFetchedAt=null}getByKey(e){const t=this.normalizeKey(e);return this.cache.get(t)}hasKey(e){const t=this.normalizeKey(e);return this.cache.has(t)}buildBaseStats(){return{totalItems:this.cache.size,isPopulated:this.cache.size>0,lastFetchedAt:this.lastFetchedAt}}}class du extends lu{constructor(e=!1){super(e)}extractKey(e){return e.stringifiedTokenClassKey}has(){return this.hasItems()}getAll(){return this.getAllItems()}getByTokenId(e){return this.getByKey(e)}getStats(){const e=this.buildBaseStats();return{...e,tokenCount:e.totalItems}}size(){return this.cache.size}isTokenWrappable(e){return void 0!==this.getByTokenId(e)}getWrapCounterpart(e){const t=this.getByTokenId(e);if(t)return this.getByTokenId(t.wrapCounterpart)}}class uu extends ps{constructor(e,t=!1){super(e,t,"WrappableTokenService"),this.cache=new du(t)}buildApiParams(e){return{wrappable:!0}}transformApiResponse(e){return e.map(e=>{const t={symbol:e.symbol,name:e.name,decimals:e.decimals,galaChainDescriptor:{collection:e.collection,category:e.category,type:e.type,additionalKey:e.additionalKey},stringifiedTokenClassKey:e.stringifiedTokenClassKey,wrapCounterpart:e.wrap,swappable:e.swappable,verified:e.verified};return void 0!==e.channel&&""!==e.channel&&(t.channel=e.channel),void 0!==e.trending&&(t.trending=e.trending),void 0!==e.image&&""!==e.image&&(t.image=e.image),void 0!==e.description&&""!==e.description&&(t.description=e.description),void 0!==e.currentPrices&&(t.currentPrices=e.currentPrices),t})}async fetchWrappableTokens(e={}){const{offset:t=0,limit:n=this.getDefaultLimit()}=e;return this.logger.debug(`Fetching wrappable tokens (offset=${t}, limit=${Math.min(n,this.getMaxLimit())})`),ea(async()=>{const e=(await this.executePaginatedRequest(t,n)).items;try{0===t?this.cache.setAll(e):this.cache.merge(e)}catch(e){this.logger.error("Cache operation failed (non-fatal):",e)}return{tokens:e,fetchedAt:Date.now(),tokenCount:e.length}},"Failed to fetch wrappable tokens",this.logger)}async fetchAllWrappableTokens(){return ea(async()=>{const e=await async function(e){const{cache:t,fetchFn:n,logger:i,itemTypeName:o="items"}=e;if(t.has()){const e=t.getAll();return i&&i.debug(`Returning ${e.length} cached ${o}`),{items:e,fetchedAt:t.getFetchTimestamp()??Date.now(),itemCount:e.length}}i&&i.debug(`Fetching all ${o} (no cache)`);const a=await n();try{t.setAll(a)}catch(e){i&&i.error("Cache operation failed (non-fatal):",e)}return{items:a,fetchedAt:Date.now(),itemCount:a.length}}({cache:this.cache,fetchFn:()=>this.autoPaginateFetch(),logger:this.logger,itemTypeName:"wrappable tokens"});return{tokens:e.items,fetchedAt:e.fetchedAt,tokenCount:e.itemCount}},"Failed to fetch wrappable tokens",this.logger)}async getWrappableToken(e){const t=gs(e),n=this.cache.getByTokenId(t);return n||(await ru("wrappable:all",()=>this.fetchAllWrappableTokens(),{get:()=>this.cache.has()?{}:void 0,set:()=>{}},{logger:this.logger}),this.cache.getByTokenId(t))}async getWrapCounterpart(e){const t=await this.getWrappableToken(e);if(t)return this.getWrappableToken(t.wrapCounterpart)}async isTokenWrappable(e){const t=gs(e);await ru("wrappable:check",()=>this.fetchAllWrappableTokens(),{get:()=>this.cache.has()?{}:void 0,set:()=>{}},{logger:this.logger});const n=this.cache.getByTokenId(t),i=void 0!==n,o={isWrappable:i,tokenId:t};return i&&(o.wrapCounterpart=n.wrapCounterpart),o}getCacheStats(){return this.cache.getStats()}clearCache(){this.cache.clear()}}class hu extends si{constructor(e){super(!1,e.logger),this.galaConnectClient=e.galaConnectClient,this.wrappableTokenService=e.wrappableTokenService,this.wallet=e.wallet,this.walletAddress=e.walletAddress}async wrapToken(e){this.requireWallet();const t=await this.wrappableTokenService.getWrappableToken(e.tokenId);if(!t)throw new wt(`Token not found or not wrappable: ${this.formatTokenId(e.tokenId)}`,"tokenId","TOKEN_NOT_WRAPPABLE");if("asset"===t.channel)throw new wt(`Cannot wrap ${t.symbol} - it's already on asset channel. Use unwrapToken() instead.`,"tokenId","ALREADY_ON_ASSET_CHANNEL");const n=await this.wrappableTokenService.getWrapCounterpart(e.tokenId);if(!n)throw new wt(`Counterpart token not found for ${t.symbol}`,"tokenId","COUNTERPART_NOT_FOUND");return this.executeChannelBridge({sourceToken:t,destinationToken:n,amount:e.amount,...void 0!==e.recipient&&""!==e.recipient&&{recipient:e.recipient},...void 0!==e.memo&&""!==e.memo&&{memo:e.memo},isWrap:!0})}async unwrapToken(e){this.requireWallet();const t=await this.wrappableTokenService.getWrappableToken(e.tokenId);if(!t)throw new wt(`Token not found or not wrappable: ${this.formatTokenId(e.tokenId)}`,"tokenId","TOKEN_NOT_WRAPPABLE");if("asset"!==t.channel)throw new wt(`Cannot unwrap ${t.symbol} - it's not on asset channel. Use wrapToken() instead.`,"tokenId","NOT_ON_ASSET_CHANNEL");const n=await this.wrappableTokenService.getWrapCounterpart(e.tokenId);if(!n)throw new wt(`Counterpart token not found for ${t.symbol}`,"tokenId","COUNTERPART_NOT_FOUND");return this.executeChannelBridge({sourceToken:t,destinationToken:n,amount:e.amount,...void 0!==e.recipient&&""!==e.recipient&&{recipient:e.recipient},...void 0!==e.memo&&""!==e.memo&&{memo:e.memo},isWrap:!1})}async estimateWrapFee(e,t){if("string"==typeof e&&0===e.length)throw Ft("tokenId","Token identifier");if(0===t.length)throw Ft("amount");const n=await this.wrappableTokenService.getWrappableToken(e);if(!n)throw new wt(`Token not found or not wrappable: ${this.formatTokenId(e)}`,"tokenId","TOKEN_NOT_WRAPPABLE");if("asset"===n.channel)throw new wt(`Cannot wrap ${n.symbol} - it's already on asset channel`,"tokenId","ALREADY_ON_ASSET_CHANNEL");const i=this.determineChannelRouting(n,!0);return{fee:"0",feeToken:"GALA",authorizationType:i.authType,feeChannel:i.sourceChannel}}async estimateUnwrapFee(e,t){if("string"==typeof e&&0===e.length)throw Ft("tokenId","Token identifier");if(0===t.length)throw Ft("amount");const n=await this.wrappableTokenService.getWrappableToken(e);if(!n)throw new wt(`Token not found or not wrappable: ${this.formatTokenId(e)}`,"tokenId","TOKEN_NOT_WRAPPABLE");if("asset"!==n.channel)throw new wt(`Cannot unwrap ${n.symbol} - it's not on asset channel`,"tokenId","NOT_ON_ASSET_CHANNEL");const i=this.determineChannelRouting(n,!1);return{fee:"0",feeToken:"GALA",authorizationType:i.authType,feeChannel:i.sourceChannel}}getWrapStatus(e){if(0===e.length)throw Ft("transactionId");return{success:!0,status:"completed",transactionId:e,fromToken:"",toToken:"",amount:"",fromChannel:"",toChannel:""}}async executeChannelBridge(e){const{sourceToken:t,destinationToken:n,amount:i,recipient:o,isWrap:a}=e;if(!this.wallet||void 0===this.walletAddress||0===this.walletAddress.length)throw new wt("Wallet required for wrap/unwrap operations. Initialize SDK with a private key.","wallet","WALLET_REQUIRED");const r=this.walletAddress,s=this.determineChannelRouting(t,a),c=`galaswap-operation-${Va()}`,l=nr,d={quantity:i,tokenInstance:{collection:t.galaChainDescriptor.collection,category:t.galaChainDescriptor.category,type:t.galaChainDescriptor.type,additionalKey:t.galaChainDescriptor.additionalKey,instance:"0"},destinationChainId:s.destinationChannelId,recipient:o??r,wrap:!0,uniqueKey:c};return this.logger.debug(`[WrapService] ${a?"Wrap":"Unwrap"} message (pre-signing)`,{amount:i,uniqueKey:c,sourceChannel:s.sourceChannel}),this.executeWrapBridgeRequest({sourceToken:t,destinationToken:n,amount:i,message:d,routing:s,isWrap:a,senderAddress:r,typedDataTypes:l})}async executeWrapBridgeRequest(e){const{sourceToken:t,destinationToken:n,amount:i,message:o,routing:a,isWrap:s,typedDataTypes:c}=e;try{const e=await this.wallet.signTypedData(Qa,c,o),l=`Ethereum Signed Message:\n${r({domain:Qa,message:o,primaryType:"GalaTransaction",types:c}).length}`,d={...o,signature:e,prefix:l,types:c,domain:Qa};this.logger.debug(`[WrapService] ${s?"Wrap":"Unwrap"} request (signed)`,{sourceToken:t.symbol,destinationToken:n.symbol,amount:i,channel:a.sourceChannel});const u=await this.galaConnectClient.requestBridgeOut(d);if(this.logger.debug("[WrapService] RequestBridgeOut response",{status:u.Status,hasData:"Data"in u}),function(e){return"object"==typeof e&&null!==e&&"Status"in e&&"number"==typeof e.Status&&1!==e.Status}(u)){const e=`Status=${u.Status}`,o="string"==typeof u.Message&&u.Message.length>0?`: ${u.Message}`:"";return{success:!1,fromToken:t.symbol,toToken:n.symbol,amount:i,fromChannel:a.sourceChannel,toChannel:s?"asset":n.channel??"music",error:`GalaChain request failed (${e}${o})`}}if(function(e){return"object"==typeof e&&null!==e&&"Data"in e&&"string"==typeof e.Data}(u)){const e=u.Data;this.logger.debug("[WrapService] Step 1 complete, bridgeRequestId:",e);const o={bridgeFromChannel:a.sourceChannel,bridgeRequestId:e};this.logger.debug("[WrapService] Step 2 - BridgeTokenOut",{bridgeFromChannel:o.bridgeFromChannel});const r=await this.galaConnectClient.bridgeTokenOut(o);return this.logger.debug("[WrapService] BridgeTokenOut response",{status:r.Status,hash:r.Hash}),1!==r.Status?{success:!1,fromToken:t.symbol,toToken:n.symbol,amount:i,fromChannel:a.sourceChannel,toChannel:s?"asset":n.channel??"music",error:`BridgeTokenOut failed: status=${String(r.Status)}`}:{success:!0,transactionId:r.Hash??e,fromToken:t.symbol,toToken:n.symbol,amount:i,fromChannel:a.sourceChannel,toChannel:s?"asset":n.channel??"music",completedAt:Date.now()}}return{success:!1,fromToken:t.symbol,toToken:n.symbol,amount:i,fromChannel:a.sourceChannel,toChannel:s?"asset":n.channel??"music",error:`Unexpected response format from GalaChain (type=${typeof u})`}}catch(e){const o=at(e);return this.logger.error(`[WrapService] ${s?"Wrap":"Unwrap"} operation failed:`,o),{success:!1,fromToken:t.symbol,toToken:n.symbol,amount:i,fromChannel:a.sourceChannel,toChannel:s?"asset":n.channel??"music",error:o}}}determineChannelRouting(e,t){if(t){return{sourceChannel:e.channel??"music",destinationChannelId:ma,authType:"cross_channel_authorization"}}return{sourceChannel:"asset",destinationChannelId:pa,authType:"automatic"}}requireWallet(){if(void 0===this.walletAddress||0===this.walletAddress.length)throw new wt("Wallet required for wrap/unwrap operations. Initialize SDK with a private key.","wallet","WALLET_REQUIRED")}formatTokenId(e){return"string"==typeof e?e:ca(e)}}const gu=new Set(["accesstoken","refreshtoken","token","password","privatekey","secret","apikey","api_key","rawkey","authorization","x-api-key","bearer","streamadminapikey","userapikey","signature","signedpayload"]);function mu(e,t=0){if(t>5)return"[...]";if(null==e)return e;if(Array.isArray(e))return e.map(e=>mu(e,t+1));if(e instanceof Map){const n={};for(const[i,o]of e.entries()){const e=String(i);n[e]=gu.has(e.toLowerCase())?"[REDACTED]":mu(o,t+1)}return n}if("object"==typeof e){const n={};for(const[i,o]of Object.entries(e))n[i]=gu.has(i.toLowerCase())?"[REDACTED]":mu(o,t+1);return n}return e}class pu{constructor(e,t={}){this.auth=e,this.debug=t.debug??!1,this.logger=new an({debug:this.debug,context:"HttpClient"});const n=t.baseUrl??"https://lpad-backend-dev1.defi.gala.com",i=t.timeout??6e4;this.axios=Bc(n,i),t.headers&&(this.axios.defaults.headers.common={...this.axios.defaults.headers.common,...t.headers}),this.setupInterceptors()}async request(e){return ea(async()=>{const t={method:e.method,url:e.url,data:e.data,...void 0!==e.params&&{params:mo(e.params)},...void 0!==e.headers&&{headers:e.headers},...void 0!==e.timeout&&{timeout:e.timeout},...void 0!==e.signal&&{signal:e.signal}};e.data instanceof FormData&&(void 0!==t.headers&&"Content-Type"in t.headers&&delete t.headers["Content-Type"],this.logger.debug("FormData detected - removing Content-Type header for multipart upload")),e.headers&&this.logger.debug("Custom headers provided:",Object.keys(t.headers??{}));const n=e.data instanceof FormData?"[FormData object - multipart/form-data]":mu(e.data);this.logger.debug("Request:",{method:e.method,url:e.url?.split("?")[0],baseURL:this.axios.defaults.baseURL,params:mu(t.params),data:n,isFormData:e.data instanceof FormData,contentType:t.headers?.["Content-Type"]??"not set"});const i=await this.axios.request(t);return this.logger.debug("Response:",{status:i.status,data:mu(i.data)}),i.data},`HTTP ${e.method} ${e.url}`,this.logger)}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){return ea(async()=>{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()}},"Failed to generate custom signature for message",this.logger)}async signWithGalaChain(e,t,i=n.SigningType.SIGN_TYPED_DATA){const o=this.auth.getPrivateKey(),a={};Object.entries(t).forEach(([e,t])=>{void 0!==t&&(a[e]=t)});const r=new n.SigningClient(o);return await r.sign(e,a,i)}setupInterceptors(){this.requestInterceptorId=this.axios.interceptors.request.use(async e=>{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 header keys being sent:",Object.keys(e.headers)),e},e=>Promise.reject(e)),this.responseInterceptorId=this.axios.interceptors.response.use(e=>e,e=>{const t=e;if(void 0!==t.response){const n=t.response.data,i={message:n?.message??at(e),...n&&Boolean(n.error)&&{error:n.error},statusCode:t.response.status,...n&&Boolean(n.details)&&{details:n.details},...n&&Boolean(n.timestamp)&&{timestamp:n.timestamp},...n&&Boolean(n.path)&&{path:n.path}};t.launchpadError=i,this.logger.error("Backend error:",i)}else void 0!==t.request?this.logger.error("Network error:",t.message??"Unknown error"):this.logger.error("Request setup error:",t.message??"Unknown error");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")}}class fu extends Error{constructor(e,t){super(e),this.cause=t,this.name="WebSocketError"}}class yu extends Error{constructor(e,t,n){super(`Transaction ${e} failed with status: ${t}${void 0!==n?` - ${n}`:""}`),this.transactionId=e,this.status=t,this.name="TransactionFailedError"}}function ku(e,t){if(Vn(e))throw new fu(`Invalid WebSocket response received for transaction ${t}: response is null or undefined`);if("object"!=typeof e)throw new fu(`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 fu(`Invalid WebSocket response received for transaction ${t}: missing status field`)}function vu(e,t,n,i){ku(e,t);const o=e,a=o.data??{};if(!function(e){if(Vn(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)}(a))throw new fu(`Invalid trade data received for transaction ${t}`);const r={transactionId:t,type:n,method:"native"===i.type?"native":"exact",inputAmount:a.inputQuantity??i.amount,outputAmount:a.outputQuantity??i.expectedAmount??"0",totalFees:a.totalFees??"0",tokenName:i.tokenName,vaultAddress:a.vaultAddress??"",timestamp:Date.now()};return void 0!==o.blockHash&&(r.blockHash=o.blockHash),void 0!==o.gasUsed&&(r.gasUsed=o.gasUsed),void 0!==i.slippageToleranceFactor&&(r.slippageTolerance=i.slippageToleranceFactor),r}function wu(e){if(e&&function(e){return"privateKey"===e.providerType}(e))return e.getWallet()}class bu{constructor(e){this.logger=e??new an({debug:!1,context:"LiquidityEventExtractor"})}walkPayloadForLiquidityEvents(e,t){const n=[],i=new WeakSet,o=(e,a=0)=>{if(a>50)this.logger.debug("Payload nesting exceeded maximum depth of 50");else if(null!=e&&!jn(e)&&"object"==typeof e){if(i.has(e))return;i.add(e);const r=this.extractLiquidityFromObject(e);r&&!t.has(r.transactionId)&&(n.push(r),t.add(r.transactionId));for(const t of Object.values(e))o(t,a+1)}};return o(e,0),n}extractLiquidityFromObject(e){const t=this.extractTransactionId(e);if(null===t||""===t)return null;const n=e.Data,i=null==n||"object"!=typeof n||Array.isArray(n)?e:n,o=this.extractPositionId(i),a=this.extractPoolHash(i),r=this.extractAmounts(i),s=this.extractUserAddress(i),c=this.extractPoolFee(i);if(null===o||""===o||null===a||""===a||null===r||null===s||""===s||null===c)return null;const l=this.extractPoolAlias(i),d=this.extractUserBalanceDelta(i),u=this.extractTimestamp(i),h=d?.token0Balance?.collection,g=d?.token1Balance?.collection,m={transactionId:t,positionId:o,poolHash:a,poolFee:c,amounts:r,userAddress:s};return void 0!==h&&(m.token0=h),void 0!==g&&(m.token1=g),void 0!==u&&(m.timestamp=u),void 0!==l&&(m.poolAlias=l),void 0!==d&&(m.userBalanceDelta=d),m}extractTransactionId(e){const t=["transactionId","txId","tx_id","hash","txHash","id"];for(const n of t){const t=e[n];if(jn(t))return t}return null}extractPositionId(e){const t=["positionId","position_id","tokenId","nftId"];for(const n of t){const t=e[n];if(jn(t))return t}return null}extractPoolHash(e){const t=["poolHash","pool_hash","pool"];for(const n of t){const t=e[n];if(jn(t))return t}return null}extractPoolAlias(e){const t=["poolAlias","pool_alias"];for(const n of t){const t=e[n];if(jn(t))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,i=e.amount1??e.amount1Desired;return void 0!==n&&void 0!==i?[String(n),String(i)]:null}extractUserAddress(e){const t=["userAddress","user","owner","from","sender","wallet","address"];for(const n of t){const t=e[n];if(jn(t))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(jn(t)){const e=xn(t,NaN);if(isFinite(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(jn(t)){const e=new Date(t).getTime();if(isFinite(e))return e}}}extractUserBalanceDelta(e){const t=e.userBalanceDelta;if(null==t||"object"!=typeof t)return;const n=t,i=this.extractBalanceObject(n.token0Balance),o=this.extractBalanceObject(n.token1Balance);if(!i&&!o)return;const a={};return void 0!==i&&(a.token0Balance=i),void 0!==o&&(a.token1Balance=o),a}extractBalanceObject(e){if(null==e||"object"!=typeof e)return;const t=e,n=t.collection,i=t.category,o=t.type,a=t.additionalKey,r=t.quantity,s=t.owner;return jn(n)&&jn(i)&&jn(o)&&jn(a)&&jn(r)&&jn(s)?{collection:n,category:i,type:o,additionalKey:a,quantity:r,owner:s}:void 0}}class Su{constructor(e){let t,n;e.walletProvider?(t=e.walletProvider,n=wu(e.walletProvider)):e.wallet&&(n=e.wallet,t=oa.fromWallet(e.wallet)),this.wallet=n;let i=null,o="PROD";if(void 0!==e.env?(o=e.env,i=sa(e.env)):e.baseUrl?.includes("dev")||e.baseUrl?.includes("qa")||e.baseUrl?.includes("test")||e.baseUrl?.includes("stage")?(o="STAGE",i=sa("STAGE")):(o="PROD",i=sa("PROD")),this.environment=o,this.config={baseUrl:i.launchpadBaseUrl,galaChainBaseUrl:i.galaChainBaseUrl,bundleBaseUrl:i.bundleBaseUrl,webSocketUrl:i.webSocketUrl,dexApiBaseUrl:i.dexApiBaseUrl,dexBackendBaseUrl:i.dexBackendBaseUrl,launchpadFrontendUrl:i.launchpadFrontendUrl,timeout:3e4,debug:!1,...e},this.logger=new an({debug:this.config.debug??!1,context:"LaunchpadSDK"}),this.validateConfiguration(),this.slippageToleranceFactor=void 0===e.slippageToleranceFactor?Su.DEFAULT_SLIPPAGE_TOLERANCE_FACTOR:this.parseSlippageToleranceFactor(e.slippageToleranceFactor),this.maxAcceptableReverseBondingCurveFeeSlippageFactor=void 0===e.maxAcceptableReverseBondingCurveFeeSlippageFactor?Su.DEFAULT_MAX_ACCEPTABLE_REVERSE_BONDING_CURVE_FEE_SLIPPAGE_FACTOR:this.parseFeeSlippageFactor(e.maxAcceptableReverseBondingCurveFeeSlippageFactor),this.calculateAmountMode=e.calculateAmountMode??Su.DEFAULT_CALCULATE_AMOUNT_MODE,this.pricingConcurrency=e.pricingConcurrency??5,this.galaChainAddressOverride=e.galaChainAddress,this.auth=new aa({wallet:n,walletProvider:t,messagePrefix:"Create a GalaChain Wallet"}),this.jwtAuth=new Zo,void 0!==e.accessToken&&""!==e.accessToken){const t=e.accessTokenExpiresIn??86400;this.jwtAuth.setToken(e.accessToken,t)}this.http=new pu(this.auth,this.config),this.sessionAuth=new na(this.http,this.auth,this.jwtAuth,e.debug??!1),this.galaChainHttp=new pu(this.auth,{...this.config,baseUrl:this.config.galaChainBaseUrl}),this.bundleHttp=new pu(this.auth,{...this.config,baseUrl:this.config.bundleBaseUrl}),this.dexApiHttp=new pu(this.auth,{...this.config,baseUrl:this.config.dexApiBaseUrl}),this.dexBackendHttp=new pu(this.auth,{...this.config,baseUrl:this.config.dexBackendBaseUrl}),this.galaChainPublicAxios=Bc(this.config.galaChainBaseUrl,this.config.timeout??3e4),this.cache=new li(e.debug??!1),this.launchpadService=new tl(this.http,this.jwtAuth),this.tokenResolverService=new Zd(this.launchpadService.poolService),this.launchpadAPI=new Wo(this.http,this.tokenResolverService,this.logger,this.bundleHttp,this.galaChainHttp,this.dexApiHttp,this.calculateAmountMode),this.galaChainService=new Rc(this.galaChainHttp,n,this.tokenResolverService,e.debug??!1,this.galaChainPublicAxios),this.dexService=new Xs(this.dexBackendHttp,this.cache,this.galaChainService,e.debug??!1),this.bundleService=new _s(this.bundleHttp,this.tokenResolverService,this.config.debug??!1,n,n?this.getAddress():void 0,this.slippageToleranceFactor,this.maxAcceptableReverseBondingCurveFeeSlippageFactor),this.websocketService=new su({url:this.config.webSocketUrl},this.config.debug),this.priceHistoryService=new $l(this.dexBackendHttp,this.config.debug??!1,this.tokenResolverService),this.dexQuoteService=new Qs(this.galaChainHttp,this.config.galaChainBaseUrl,e.debug??!1,e.dexQuoteNetworkTimeout??3e4),this.gswapService=new Gc({privateKey:n?.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 js(this.dexBackendHttp,this.config.dexBackendBaseUrl,this.gswapService,this.pricingConcurrency,e.debug??!1),this.nftCollectionService=new Il(this.galaChainHttp),this.tradingQuotesService=new eu(this.http,e.debug??!1)}createOverrideSdk(e){if(!jn(e))throw Ut("Invalid privateKey: must be a non-empty string","privateKey");if(!e.match(/^0x[a-fA-F0-9]{64}$/))throw Ut('Invalid privateKey format: must be "0x" followed by 64 hexadecimal characters',"privateKey");const n=new t.Wallet(e),i={...this.config,wallet:n};return new Su(i)}getAddress(){return void 0!==this.galaChainAddressOverride&&""!==this.galaChainAddressOverride?this.galaChainAddressOverride:(this.validateWallet(),this.auth.getAddress())}getEthereumAddress(){return this.validateWallet(),this.wallet.address}validateWallet(){if(!this.wallet)throw Ft("wallet","Wallet");return this.wallet}setWallet(e){if(null==e||"object"!=typeof e||!("address"in e))throw new wt("Invalid wallet: must be an ethers Wallet instance, received "+typeof e,"wallet","INVALID_WALLET");this.wallet=e,this.auth.setWallet(e)}setWalletProvider(e){this.auth.setWalletProvider(e),this.wallet=e?wu(e):void 0}getWallet(){return this.wallet}get events(){return this.getEventsBatcherService()}getWalletProvider(){return this.auth.getWalletProvider()}hasWallet(){return this.auth.hasWallet()}getConfig(){const{wallet:e,walletProvider:t,streamAdminApiKey:n,userApiKey:i,accessToken:o,headers:a,...r}=this.config;return{...r,...void 0!==n&&{streamAdminApiKey:"[REDACTED]"},...void 0!==i&&{userApiKey:"[REDACTED]"},...void 0!==o&&{accessToken:"[REDACTED]"},...void 0!==a&&{headers:Object.fromEntries(Object.keys(a).map(e=>[e,"[REDACTED]"]))},environment:this.environment,slippageToleranceFactor:this.slippageToleranceFactor,maxAcceptableReverseBondingCurveFeeSlippageFactor:this.maxAcceptableReverseBondingCurveFeeSlippageFactor,calculateAmountMode:this.calculateAmountMode,gasFee:Zt.GAS_FEE}}getVersion(){return Hr}getUrlByTokenName(e){const t=this.config.launchpadFrontendUrl;if(void 0===t||""===t)throw Ut("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.items),t}async fetchAllPools(e){const t=await this.launchpadService.fetchAllPools(e);return await this.warmCacheFromPools(t.items),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 getTopHolders(e,t=10){const n=await this.fetchTokenDistribution(e),i=Math.min(t,100);return n.holders.slice(0,i).map((e,t)=>({...e,rank:t+1}))}async getHolderCount(e){const t=await this.fetchTokenDistribution(e);return{count:t.totalHolders,totalBalance:t.totalSupply}}async getHolderRank(e,t){const n=await this.fetchTokenDistribution(e),i=n.holders.findIndex(e=>e.address.toLowerCase()===t.toLowerCase());return-1===i?null:{rank:i+1,balance:n.holders[i].balance,holderCount:n.totalHolders}}async getHoldersByBalance(e,t){const n=await this.fetchTokenDistribution(e),i=t?.minBalance??0,o=t?.maxBalance??1/0,a=Math.min(t?.limit??100,1e3),r=new Map;return n.holders.forEach((e,t)=>{r.set(e.address,t+1)}),n.holders.filter(e=>{const t=parseFloat(e.balance);return t>=i&&t<=o}).map(e=>({...e,rank:r.get(e.address)??0})).slice(0,a)}async getHolderHistory(e,t,n=20){return this.getTrades({tokenName:e,userAddress:t,pageSize:Math.min(n,100)})}async fetchUserHolderContext(e,t){return this.launchpadService.fetchUserHolderContext(e,t)}async fetchTokenBadges(e){return this.launchpadService.fetchTokenBadges(e)}async fetchTokenPrice(e){const{tokenName:t,tokenId:n}=e,{hasA:i}=Yn(e,"tokenName","tokenId",{description:"token identifier"});if(i&&void 0!==t&&""!==t)return this.dexService.fetchLaunchpadTokenSpotPrice(t,e=>this.launchpadAPI.calculateBuyAmount(e),e=>this.fetchPoolDetails(e));const o=n;try{return await this.dexService.fetchTokenPrice({tokenId:o})}catch(e){const t=function(e){if(ct(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=en((await this.fetchTokenDetails(o)).name);if(!/^[a-z0-9]{3,20}$/.test(t))throw this.logger.error(`Token name extracted from GalaChain doesn't match launchpad format: "${t}"`),e;return this.logger.debug(`Falling back to launchpad pricing using extracted token name: "${t}"`),this.dexService.fetchLaunchpadTokenSpotPrice(t,e=>this.launchpadAPI.calculateBuyAmount(e),e=>this.fetchPoolDetails(e))}catch(t){throw this.logger.error(`Launchpad fallback failed: ${at(t)}`),e}}throw e}}async fetchGalaPrice(){return this.fetchTokenPrice({tokenId:{collection:"GALA",category:"Unit",type:"none",additionalKey:"none"}})}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(null==t)throw new Error(gt(e));const n=(await this.galaChainService.fetchPoolDetails({vaultAddress:t})).Data,i=await this.launchpadAPI.fetchPoolDetailsForCalculation(e);return n.currentSupply=i.currentSupply,n.reverseBondingCurveMaxFeeFactor=i.reverseBondingCurveMaxFeeFactor,n.reverseBondingCurveMinFeeFactor=i.reverseBondingCurveMinFeeFactor,n.reverseBondingCurveNetFeeFactor=i.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 bi}),n=t(e)??this.getAddress();return this.galaChainService.fetchGalaBalance({owner:n,collection:"GALA",category:"Unit",additionalKey:"none",type:"none",instance:"0"})}getBridgeService(e){if(!this._bridgeService){const t=this.getWallet();if(!t)throw new Error("Bridge operations require a wallet. Configure SDK with a wallet first.");const n=e?.solanaPrivateKey??process.env.SOLANA_PRIVATE_KEY;this._bridgeService=new Wr({galaConnectBaseUrl:this.config.dexApiBaseUrl,galaChainWalletAddress:this.getAddress(),ethereumPrivateKey:e?.ethereumPrivateKey??t.privateKey,...void 0!==n&&""!==n?{solanaPrivateKey:n}:{},bridgeableTokenService:this.getBridgeableTokenService(),environment:this.environment,...void 0!==this.config.ethereumRpcUrl&&""!==this.config.ethereumRpcUrl?{ethereumRpcUrl:this.config.ethereumRpcUrl}:{},...void 0!==this.config.solanaRpcUrl&&""!==this.config.solanaRpcUrl?{solanaRpcUrl:this.config.solanaRpcUrl}:{}})}return this._bridgeService}getBridgeableTokenService(){return this._bridgeableTokenService??(this._bridgeableTokenService=new vs(this.dexApiHttp,this.config.debug??!1)),this._bridgeableTokenService}getWrappableTokenService(){return this._wrappableTokenService??(this._wrappableTokenService=new uu(this.dexApiHttp,this.config.debug??!1)),this._wrappableTokenService}getGalaConnectClient(){if(null===this._galaConnectClient||void 0===this._galaConnectClient){const e=this.getAddress();if(void 0===this.config.dexApiBaseUrl||""===this.config.dexApiBaseUrl)throw new Error("DEX API base URL is required for GalaConnectClient. Check SDK configuration.");this._galaConnectClient=new Ur({baseUrl:this.config.dexApiBaseUrl,...void 0!==this.config.galaChainBaseUrl&&""!==this.config.galaChainBaseUrl?{galachainBaseUrl:this.config.galaChainBaseUrl}:{},walletAddress:e})}return this._galaConnectClient}getWrapService(){if(!this._wrapService){const e=this.getWallet();this._wrapService=new hu({galaConnectClient:this.getGalaConnectClient(),wrappableTokenService:this.getWrappableTokenService(),...e&&{walletAddress:this.getAddress(),wallet:e}})}return this._wrapService}getStreamingService(){return this._streamingService??(this._streamingService=new Vl(this.http,this.config.streamAdminApiKey,this.jwtAuth,this.config.debug??!1,this.config.userApiKey)),this._streamingService}getStreamChatService(){return this._streamChatService??(this._streamChatService=new ql(this.http,this.config.streamAdminApiKey,this.jwtAuth,this.config.debug??!1,this.config.userApiKey)),this._streamChatService}getBanService(){return this._banService??(this._banService=new ss(this.http,this.config.streamAdminApiKey,this.jwtAuth,this.config.debug??!1,this.config.userApiKey)),this._banService}getTokenBanService(){return this._tokenBanService??(this._tokenBanService=new Yd(this.http,this.config.streamAdminApiKey,this.jwtAuth,this.config.debug??!1,this.config.userApiKey)),this._tokenBanService}getAIModerationService(){return this._aiModerationService??(this._aiModerationService=new Zr(this.http,this.config.streamAdminApiKey,this.jwtAuth,this.config.debug??!1,this.config.userApiKey)),this._aiModerationService}getWeeklyChallengeService(){return this._weeklyChallengeService??(this._weeklyChallengeService=new cu(this.http,this.config.debug??!1)),this._weeklyChallengeService}getOEmbedService(){return this._oembedService??(this._oembedService=new Dl(this.http,this.config.debug??!1)),this._oembedService}getPlatformStatsService(){return this._platformStatsService??(this._platformStatsService=new Ml(this.http,this.config.debug??!1)),this._platformStatsService}getNotificationService(){return this._notificationService??(this._notificationService=new Nl(this.http,this.config.debug??!1,this.jwtAuth)),this._notificationService}getClientConfigService(){return this._clientConfigService??(this._clientConfigService=new Fs(this.http,this.config.debug??!1)),this._clientConfigService}getApiKeyService(){return this._apiKeyService??(this._apiKeyService=new ns(this.http,this.jwtAuth,this.config.debug??!1)),this._apiKeyService}getModeratorService(){return this._moderatorService??(this._moderatorService=new sl(this.http,this.config.streamAdminApiKey,this.jwtAuth,this.config.debug??!1,this.config.userApiKey)),this._moderatorService}getFlagService(){return this._flagService??(this._flagService=new Ws(this.http,this.config.streamAdminApiKey,this.jwtAuth,this.config.debug??!1,this.config.userApiKey)),this._flagService}getOverseerService(){return this._overseerService??(this._overseerService=new Ol(this.http,this.config.streamAdminApiKey,this.jwtAuth,this.config.debug??!1,this.config.userApiKey)),this._overseerService}getWebSocketAdminService(){return this._websocketAdminService??(this._websocketAdminService=new tu(this.http,this.config.streamAdminApiKey,this.jwtAuth,this.config.debug??!1,this.config.userApiKey)),this._websocketAdminService}getContentReactionService(){return this._contentReactionService??(this._contentReactionService=new zs(this.http,this.config.streamAdminApiKey,this.jwtAuth,this.config.debug??!1,this.config.userApiKey)),this._contentReactionService}getMessagesService(){return this._messagesService??(this._messagesService=new nl(this.http,this.config.streamAdminApiKey,this.jwtAuth,this.config.debug??!1,this.config.userApiKey)),this._messagesService}getRestrictedNamesService(){return this._restrictedNamesService??(this._restrictedNamesService=new Kl(this.http,this.config.streamAdminApiKey,this.jwtAuth,this.config.userApiKey)),this._restrictedNamesService}getPlatformConfigService(){return this._platformConfigService??(this._platformConfigService=new Ll(this.http,this.jwtAuth,this.config.debug??!1)),this._platformConfigService}getEventsBatcherService(){return this._eventsBatcherService??(this._eventsBatcherService=new ic(this.http,this.config.events)),this._eventsBatcherService}getStreamWebSocketService(){if(null===this._streamWebSocketService||void 0===this._streamWebSocketService){if(void 0===this.config.streamWebSocketUrl||""===this.config.streamWebSocketUrl)throw new Error('Stream WebSocket URL is required for real-time streaming features.\n\nConfigure SDK:\n const sdk = createLaunchpadSDK({\n wallet: yourWallet,\n streamWebSocketUrl: "wss://stream.gala.com"\n });\n\nFor MCP Server, set STREAM_WEBSOCKET_URL environment variable.\nSee SDK documentation for streaming configuration details.');this._streamWebSocketService=new Xd({url:this.config.streamWebSocketUrl},this.config.debug??!1)}return this._streamWebSocketService}getStreamingEventService(){return this._streamingEventService??(this._streamingEventService=new Gl(this.config.debug??!1)),this._streamingEventService}async fetchEthereumWalletTokenBalance(e,t){return this.getBridgeService().fetchEthereumWalletTokenBalance(e,t)}async fetchEthereumWalletNativeBalance(e){return this.getBridgeService().fetchEthereumWalletNativeBalance(e)}async fetchSolanaWalletTokenBalance(e,t){return this.getBridgeService().fetchSolanaWalletTokenBalance(e,t)}async fetchSolanaWalletNativeBalance(e){return this.getBridgeService().fetchSolanaWalletNativeBalance(e)}async requestSolanaDevnetAirdrop(e,t){return this.getBridgeService().requestSolanaDevnetAirdrop(e,t)}async getSolanaTransactionStatus(e){return this.getBridgeService().getSolanaTransactionStatus(e)}async getEthereumTransactionStatus(e){return this.getBridgeService().getEthereumTransactionStatus(e)}async fetchEthereumWalletAllBalances(e){return this.getBridgeService().fetchEthereumWalletAllBalances(e)}async fetchSolanaWalletAllBalances(e){return this.getBridgeService().fetchSolanaWalletAllBalances(e)}async fetchBridgeableTokensByNetwork(e){return this.getBridgeableTokenService().fetchBridgeableTokensByNetwork(e)}async fetchAllBridgeableTokensByNetwork(e){return this.getBridgeableTokenService().fetchAllBridgeableTokensByNetwork(e)}async fetchAllTokensBridgeableToEthereum(){return this.getBridgeableTokenService().fetchAllTokensBridgeableToEthereum()}async fetchAllTokensBridgeableToSolana(){return this.getBridgeableTokenService().fetchAllTokensBridgeableToSolana()}async isTokenBridgeableToNetwork(e){return this.getBridgeableTokenService().isTokenBridgeableToNetwork(e)}async isTokenBridgeableToEthereum(e){return this.getBridgeableTokenService().isTokenBridgeableToEthereum(e)}async isTokenBridgeableToSolana(e){return this.getBridgeableTokenService().isTokenBridgeableToSolana(e)}async fetchWrappableTokens(e={}){return this.getWrappableTokenService().fetchWrappableTokens(e)}async fetchAllWrappableTokens(){return this.getWrappableTokenService().fetchAllWrappableTokens()}async getWrappableToken(e){return this.getWrappableTokenService().getWrappableToken(e)}async getWrapCounterpart(e){return this.getWrappableTokenService().getWrapCounterpart(e)}async isTokenWrappable(e){return this.getWrappableTokenService().isTokenWrappable(e)}async wrapToken(e){return this.getWrapService().wrapToken(e)}async unwrapToken(e){return this.getWrapService().unwrapToken(e)}async estimateWrapFee(e,t){return this.getWrapService().estimateWrapFee(e,t)}async estimateUnwrapFee(e,t){return this.getWrapService().estimateUnwrapFee(e,t)}getWrapStatus(e){return this.getWrapService().getWrapStatus(e)}async login(){return this.sessionAuth.login()}async refreshToken(){return this.sessionAuth.refresh()}async logout(e){return this.sessionAuth.logout(e)}isAuthenticated(){return this.sessionAuth.isAuthenticated()}shouldRefreshToken(e){return this.sessionAuth.shouldRefresh(e)}async getSession(){return this.sessionAuth.getSession()}getAccessToken(){return this.sessionAuth.getAccessToken()}async ensureValidToken(e){return this.sessionAuth.ensureValidToken(e)}async startStream(e){return this.getStreamingService().startStream(e)}async stopStream(e){return this.getStreamingService().stopStream(e)}async disableStream(e){return this.getStreamingService().disableStream(e)}async enableStream(e){return this.getStreamingService().enableStream(e)}async resetStreamKey(e){return this.getStreamingService().resetStreamKey(e)}async getStreamCredentials(e){return this.getStreamingService().getStreamCredentials(e)}async getStreamRecordings(e){return this.getStreamingService().getRecordings(e)}async getRecordingDownload(e,t){return this.getStreamingService().getRecordingDownload(e,t)}async deleteRecording(e,t){return this.getStreamingService().deleteRecording(e,t)}async getSimulcastTargets(e){return this.getStreamingService().getSimulcastTargets(e)}async addSimulcastTarget(e){return this.getStreamingService().addSimulcastTarget(e)}async removeSimulcastTarget(e,t){return this.getStreamingService().removeSimulcastTarget(e,t)}async getGlobalStreamingStatus(){return this.getStreamingService().getGlobalStreamingStatus()}async setNextLiveStreamCountdown(e,t){return this.getStreamingService().setNextLiveStreamCountdown(e,t)}async setGlobalStreamingEnabled(e){const t=this.getStreamingService();return e?t.enableGlobalStreaming():t.disableGlobalStreaming()}async getStreamRole(e){return this.getStreamingService().getStreamRole(e)}async getAvailableRoles(){return this.getStreamingService().getAvailableRoles()}async getTokenAccess(e){return this.getStreamingService().getTokenAccess(e)}async getEngagementStats(e){if(null===Fo(e.tokenName))throw Ft("tokenName","tokenName is required and cannot be empty");return this.getStreamChatService().getEngagementStats(e)}async disableChat(e){return this.getStreamChatService().disableChat(e)}async enableChat(e){return this.getStreamChatService().enableChat(e)}async getGlobalChatStatus(){return this.getStreamChatService().getGlobalChatStatus()}async setGlobalChatEnabled(e){const t=this.getStreamChatService();return e?t.enableGlobalChat():t.disableGlobalChat()}async getPinnedChatMessage(e){const t=this.getMessagesService(),n=await t.getPinnedMessage(e);return{tokenName:e,pinnedMessage:n.message?{messageId:n.message.id,content:n.message.content,userAddress:n.message.userAddress,pinnedBy:""!==n.message.pinnedBy?n.message.pinnedBy:n.message.userAddress,pinnedAt:""!==n.message.pinnedAt?n.message.pinnedAt:n.message.createdAt}:null}}async pinChatMessage(e,t){const n=this.getMessagesService();await n.pinMessage(t);const i=await n.getPinnedMessage(e);return{tokenName:e,pinnedMessage:i.message?{messageId:i.message.id,content:i.message.content,userAddress:i.message.userAddress,pinnedBy:""!==i.message.pinnedBy?i.message.pinnedBy:i.message.userAddress,pinnedAt:""!==i.message.pinnedAt?i.message.pinnedAt:i.message.createdAt}:null}}async unpinChatMessage(e){const t=this.getMessagesService(),n=await t.getPinnedMessage(e);return n.message?(await t.unpinMessage(n.message.id),{tokenName:e,unpinned:!0,unpinnedMessageId:n.message.id}):{tokenName:e,unpinned:!1}}async setSlowMode(e,t){return this.getStreamChatService().setSlowMode(e,t)}async createBan(e){return this.getBanService().createBan(e)}async removeBan(e){return this.getBanService().removeBan(e)}async listBans(e){return this.getBanService().listBans(e)}async getBanStatus(e){return this.getBanService().getBanStatus(e)}async getActiveUsers(e){return this.getBanService().getActiveUsers(e)}async createGlobalBan(e){return this.getBanService().createGlobalBan(e)}async removeGlobalBan(e){return this.getBanService().removeGlobalBan(e)}async listGlobalBans(e){return this.getBanService().listGlobalBans(e)}async getGlobalBan(e){return this.getBanService().getGlobalBan(e)}async createApiKey(e){return this.getApiKeyService().create(e)}async listApiKeys(e={}){return this.getApiKeyService().findAll(e)}async getApiKey(e){return this.getApiKeyService().findOne(e)}async updateApiKey(e,t){return this.getApiKeyService().update(e,t)}async revokeApiKey(e){return this.getApiKeyService().revoke(e)}getApiKeyRoles(){return this.getApiKeyService().getRoles()}async createModeratorInvite(e){return this.getModeratorService().createInvite(e)}async claimModeratorInvite(e){return this.getModeratorService().claimInvite(e)}async getModeratedTokens(e){return this.getModeratorService().getModeratedTokens(e)}async listModeratorInvites(e){return this.getModeratorService().listInvites(e)}async revokeModeratorInvite(e){return this.getModeratorService().revokeInvite(e)}async updateModeratorInviteRole(e){return this.getModeratorService().updateInviteRole(e)}async updateModeratorInvite(e){return this.getModeratorService().updateInvite(e)}async getModeratorInviteByCode(e){return this.getModeratorService().getInviteByCode(e)}async createFlag(e){return this.getFlagService().createFlag(e)}async listFlags(e){return this.getFlagService().listFlags(e)}async listGlobalFlags(e={}){return this.getFlagService().listGlobalFlags(e)}async dismissFlag(e){return this.getFlagService().dismissFlag(e)}async actionFlag(e){return this.getFlagService().actionFlag(e)}async createOverseerInvite(e={}){return this.getOverseerService().createInvite(e)}async listOverseerInvites(e={}){return this.getOverseerService().listInvites(e)}async getOverseerInviteByCode(e){return this.getOverseerService().getInviteByCode(e)}async claimOverseerInvite(e){return this.getOverseerService().claimInvite(e)}async revokeOverseerInvite(e){return this.getOverseerService().revokeInvite(e)}async listOverseers(e={}){return this.getOverseerService().listOverseers(e)}async createOverseerDirect(e){return this.getOverseerService().createOverseerDirect(e)}async revokeOverseer(e){return this.getOverseerService().revokeOverseer(e)}async getMyOverseerStatus(){return this.getOverseerService().getMyStatus()}async getOverseerSummary(){return this.getOverseerService().getSummary()}async getBanStats(){return this.getOverseerService().getBanStats()}async getTokenBanStats(){return this.getOverseerService().getTokenBanStats()}async getInviteStats(){return this.getOverseerService().getInviteStats()}async getFlagStats(){return this.getOverseerService().getFlagStats()}async getUserStats(){return this.getOverseerService().getUserStats()}async listOverseerUsers(e){return this.getOverseerService().listOverseerUsers(e)}async getOverseerUserSummary(e){return this.getOverseerService().getOverseerUserSummary(e)}async flushCacheByAddress(e){return this.getOverseerService().flushCacheByAddress(e)}async flushCacheByToken(e){return this.getOverseerService().flushCacheByToken(e)}async flushAllCache(e){return this.getOverseerService().flushAllCache(e)}async flushWalletAliasCache(e){return this.getOverseerService().flushWalletAliasCache(e)}async emitTradeExecuted(e,t){return this.getWebSocketAdminService().emitTradeExecuted(e,t)}async emitBalanceUpdated(e,t,n){return this.getWebSocketAdminService().emitBalanceUpdated(e,t,n)}async emitStreamStatus(e,t){return this.getWebSocketAdminService().emitStreamStatus(e,t)}async emitViewerCount(e,t){return this.getWebSocketAdminService().emitViewerCount(e,t)}async emitStreamCountdown(e,t){return this.getWebSocketAdminService().emitStreamCountdown(e,t)}async emitChatMessage(e,t){return this.getWebSocketAdminService().emitChatMessage(e,t)}async emitChatStatus(e,t){return this.getWebSocketAdminService().emitChatStatus(e,t)}async emitTypingIndicator(e,t){return this.getWebSocketAdminService().emitTypingIndicator(e,t)}async emitTokenBanned(e,t){return this.getWebSocketAdminService().emitTokenBanned(e,t)}async emitTokenUnbanned(e,t){return this.getWebSocketAdminService().emitTokenUnbanned(e,t)}async emitModeratorAdded(e,t){return this.getWebSocketAdminService().emitModeratorAdded(e,t)}async emitModeratorRemoved(e,t){return this.getWebSocketAdminService().emitModeratorRemoved(e,t)}async emitUserProfileUpdated(e,t){return this.getWebSocketAdminService().emitUserProfileUpdated(e,t)}async emitSiteConfigChanged(e){return this.getWebSocketAdminService().emitSiteConfigChanged(e)}async emitFeatureStatus(e){return this.getWebSocketAdminService().emitFeatureStatus(e)}async emitRecordingStatus(e,t){return this.getWebSocketAdminService().emitRecordingStatus(e,t)}async emitRecordingsCountUpdated(e,t){return this.getWebSocketAdminService().emitRecordingsCountUpdated(e,t)}async emitDownloadReady(e,t){return this.getWebSocketAdminService().emitDownloadReady(e,t)}async getConnectedClients(e){return this.getWebSocketAdminService().getConnectedClients(e)}async isUserOnline(e){return this.getWebSocketAdminService().isUserOnline(e)}async getOnlineUsers(e){return this.getWebSocketAdminService().getOnlineUsers(e)}async getTokenViewers(e,t){return this.getWebSocketAdminService().getTokenViewers(e,t)}async getOnlineOverseers(){return this.getWebSocketAdminService().getOnlineOverseers()}async banToken(e){return this.getTokenBanService().banToken(e)}async unbanToken(e){return this.getTokenBanService().unbanToken(e)}async listTokenBans(e={}){return this.getTokenBanService().listTokenBans(e)}async getTokenBan(e){return this.getTokenBanService().getTokenBan(e)}async isTokenBanned(e){return this.getTokenBanService().isTokenBanned(e)}async getAIModerationSettings(){return this.getAIModerationService().getSettings()}async updateAIModerationSettings(e){return this.getAIModerationService().updateSettings(e)}async getAIModeration(e){return this.getAIModerationService().getModeration(e)}async triggerAIModeration(e){return this.getAIModerationService().triggerModeration(e)}async getAIModerationStatus(){return this.getAIModerationService().getStatus()}async getWeeklyChallengeLeaderboard(e={}){return this.getWeeklyChallengeService().getLeaderboard(e)}async getTokenWeeklyHistory(e){return this.getWeeklyChallengeService().getTokenHistory(e)}async getHomeOEmbed(e={}){return this.getOEmbedService().getHomeOEmbedJson(e)}async getPoolOEmbed(e){return this.getOEmbedService().getPoolOEmbedJson(e)}async getProfileOEmbed(e){return this.getOEmbedService().getProfileOEmbedJson(e)}async getPlatformStats(){return this.getPlatformStatsService().getStats()}async registerPushToken(e){return this.getNotificationService().registerPushToken(e)}async unregisterPushToken(e){return this.getNotificationService().unregisterPushToken(e)}async fetchNotificationPreferences(){return this.getNotificationService().getPreferences()}async updateNotificationPreferences(e){return this.getNotificationService().updatePreferences(e)}async fetchClientFlags(){return this.getClientConfigService().getClientFlags()}async addContentReaction(e){return this.getContentReactionService().addContentReaction(e)}async removeContentReaction(e){return this.getContentReactionService().removeContentReaction(e)}async addReactionToChatMessage(e){return this.getContentReactionService().addReactionToChatMessage(e)}async removeReactionFromChatMessage(e){return this.getContentReactionService().removeReactionFromChatMessage(e)}async addReactionToComment(e){return this.getContentReactionService().addReactionToComment(e)}async removeReactionFromComment(e){return this.getContentReactionService().removeReactionFromComment(e)}async getComments(e){const t={type:"COMMENT"};void 0!==e.tokenName&&(t.tokenName=e.tokenName),void 0!==e.userAddress&&(t.userAddress=e.userAddress),void 0!==e.cursor&&(t.cursor=e.cursor),void 0!==e.pageSize&&(t.pageSize=e.pageSize);const n=await this.fetchMessages(t);return{messages:n.messages,pageInfo:n.pageInfo}}async createComment(e){return{comment:(await this.createMessage({type:"COMMENT",tokenName:e.tokenName,content:e.content})).message}}async updateComment(e,t){return{comment:(await this.updateMessage(e,{content:t.content})).message}}async deleteComment(e){return{success:(await this.deleteMessage(e)).success}}async getChatMessages(e){const t={type:"CHAT_MESSAGE"};void 0!==e.tokenName&&(t.tokenName=e.tokenName),void 0!==e.userAddress&&(t.userAddress=e.userAddress),void 0!==e.cursor&&(t.cursor=e.cursor),void 0!==e.pageSize&&(t.pageSize=e.pageSize);const n=await this.fetchMessages(t);return{messages:n.messages,pageInfo:n.pageInfo}}async sendChatMessage(e){return{message:(await this.createMessage({type:"CHAT_MESSAGE",tokenName:e.tokenName,content:e.content})).message}}async updateChatMessage(e,t){return{message:(await this.updateMessage(e,{content:t.content})).message}}async deleteChatMessage(e){await this.deleteMessage(e)}async fetchMessages(e){return this.getMessagesService().fetchMessages(e)}async createMessage(e){return this.getMessagesService().createMessage(e)}async updateMessage(e,t){return this.getMessagesService().updateMessage(e,t)}async deleteMessage(e){return this.getMessagesService().deleteMessage(e)}async getPinnedMessage(e){return this.getMessagesService().getPinnedMessage(e)}async pinMessage(e){return this.getMessagesService().pinMessage(e)}async unpinMessage(e){return this.getMessagesService().unpinMessage(e)}async getMessageStats(){return this.getMessagesService().getMessageStats()}async getPlatformConfig(){return this.getPlatformConfigService().getPlatformConfig()}async updatePlatformConfig(e){return this.getPlatformConfigService().updatePlatformConfig(e)}async getRestrictedNames(){return this.getRestrictedNamesService().getRestrictedNames()}async updateRestrictedNames(e){return this.getRestrictedNamesService().updateRestrictedNames(e)}async updateTokenConfig(e,t){return this.launchpadService.poolService.updateTokenConfig(e,t)}async getTrades(e){return this.launchpadService.getTrades(e)}async getRecentTrades(e,t=50){return this.getTrades({...void 0!==e&&{tokenName:e},pageSize:Math.min(t,500)})}async fetchLaunchpadFee(){return this.tradingQuotesService.fetchLaunchpadFee()}async fetchSaleDetails(e){return this.tradingQuotesService.fetchSaleDetails(e)}async getTradeQuote(e){return this.tradingQuotesService.getTradeQuote(e)}async getPremintQuote(e){return this.tradingQuotesService.getPremintQuote(e)}async connectStreamWebSocket(e){const t=this.getStreamWebSocketService();await t.connect(),e&&t.setGlobalCallbacks(e)}async authenticateStreamWebSocket(){const e=this.getStreamWebSocketService(),t=await this.ensureValidToken();e.authenticate(t)}async subscribeToStream(e,t){return this.getStreamWebSocketService().subscribeToStream(e,t)}unsubscribeFromStream(e){this.getStreamWebSocketService().unsubscribeFromStream(e)}sendStreamChatViaWebSocket(e,t){this.getStreamWebSocketService().sendChatMessage(e,t)}sendStreamReaction(e,t,n=0){this.getStreamWebSocketService().sendReaction(e,t,n)}sendTypingStart(e){return this.getStreamWebSocketService().sendTypingStart(e)}sendTypingStop(e){return this.getStreamWebSocketService().sendTypingStop(e)}disconnectStreamWebSocket(){this._streamWebSocketService&&this._streamWebSocketService.disconnect()}isStreamWebSocketConnected(){return!!this._streamWebSocketService&&this._streamWebSocketService.isConnected()}onStreamStatusChanged(e){return this.getStreamingEventService().onStreamStatusChanged(e)}onUserBanned(e){return this.getStreamingEventService().onUserBanned(e)}onUserUnbanned(e){return this.getStreamingEventService().onUserUnbanned(e)}onBanEnforcement(e){return this.getStreamingEventService().onBanEnforcement(e)}onContentFlagged(e){return this.getStreamingEventService().onContentFlagged(e)}onFlagResolved(e){return this.getStreamingEventService().onFlagResolved(e)}onStreamChatMessage(e){return this.getStreamingEventService().onStreamChatMessage(e)}onStreamChatUpdated(e){return this.getStreamingEventService().onStreamChatUpdated(e)}onStreamChatDeleted(e){return this.getStreamingEventService().onStreamChatDeleted(e)}onStreamChatPinned(e){return this.getStreamingEventService().onStreamChatPinned(e)}onStreamChatUnpinned(e){return this.getStreamingEventService().onStreamChatUnpinned(e)}onChatStatusChanged(e){return this.getStreamingEventService().onChatStatusChanged(e)}onViewerCountChanged(e){return this.getStreamingEventService().onViewerCountChanged(e)}onRecordingStatusChanged(e){return this.getStreamingEventService().onRecordingStatusChanged(e)}onSimulcastStatusChanged(e){return this.getStreamingEventService().onSimulcastStatusChanged(e)}onDownloadReady(e){return this.getStreamingEventService().onDownloadReady(e)}onRecordingsCountUpdated(e){return this.getStreamingEventService().onRecordingsCountUpdated(e)}onUserTyping(e){return this.getStreamingEventService().onUserTyping(e)}onStreamReaction(e){return this.getStreamingEventService().onStreamReaction(e)}onContentReactionAdded(e){return this.getStreamingEventService().onContentReactionAdded(e)}onContentReactionRemoved(e){return this.getStreamingEventService().onContentReactionRemoved(e)}onStreamCountdownUpdated(e){return this.getStreamingEventService().onStreamCountdownUpdated(e)}onStreamLanguageUpdated(e){return this.getStreamingEventService().onStreamLanguageUpdated(e)}onStreamControlStatusChanged(e){return this.getStreamingEventService().onStreamControlStatusChanged(e)}onConnection(e){return this.getStreamingEventService().onConnection(e)}onAuthenticated(e){return this.getStreamingEventService().onAuthenticated(e)}onTokenSubscribed(e){return this.getStreamingEventService().onTokenSubscribed(e)}onTokenUnsubscribed(e){return this.getStreamingEventService().onTokenUnsubscribed(e)}onRoomSubscribed(e){return this.getStreamingEventService().onRoomSubscribed(e)}onRoomLeft(e){return this.getStreamingEventService().onRoomLeft(e)}onEngagementStatsUpdated(e){return this.getStreamWebSocketService().subscribeEngagementStatsUpdated(e)}async estimateBridgeFee(e){return this.getBridgeService().estimateBridgeFee(e)}async bridgeOut(e){return this.getBridgeService().bridgeOut(e)}async bridgeIn(e){return this.getBridgeService().bridgeIn(e)}async getBridgeStatus(e,t){return this.getBridgeService().getBridgeStatus(e,t)}getSupportedBridgeTokens(){const e=this.getBridgeService(),t=e.getSupportedBridgeTokens();return Promise.resolve({tokens:t,totalCount:t.length,supportedChains:e.getSupportedBridgeChains()})}async fetchTokenBalance(e){const{normalizeAddressInput:t}=await Promise.resolve().then(function(){return bi}),n=t(e.address);if(void 0!==e.tokenId&&""!==e.tokenId){const{normalizeToTokenInstanceKey:t}=await Promise.resolve().then(function(){return ms}),i=t(e.tokenId),{collection:o,category:a,type:r,additionalKey:s}=i;return this.galaChainService.fetchTokenBalance({owner:n,collection:o,category:a,additionalKey:s,type:r,instance:"0"},e.withExpired??!1)}if(void 0!==e.tokenName&&""!==e.tokenName){const t=nn(e.tokenName);if("MUSIC"===t||"GMUSIC"===t){const i=`$${t}`;return this.galaChainService.fetchTokenBalance({owner:n,collection:i,category:"Unit",additionalKey:"none",type:"none",instance:"0"},e.withExpired??!1)}}if(void 0!==e.tokenName&&""!==e.tokenName){const t=(await this.fetchTokensHeld({tokenName:e.tokenName,pageSize:1,...void 0!==n?{address:n}:{}})).tokens[0];if(void 0===t)return null;const i=t.collection??"Token",o={collection:i,category:"Unit",type:t.symbol,additionalKey:"none"};return{quantity:t.balance,collection:i,category:"Unit",tokenId:ca(o)}}throw Ft("tokenId or tokenName","Either tokenId or tokenName")}async fetchLockedBalance(e){const t=await this.fetchTokenBalance(e);if(!t)return null;const n="lockedHolds"in t||"lockedQuantity"in t;return{tokenId:t.tokenId,lockedQuantity:n?t.lockedQuantity??"0":"0",lockedHolds:n?t.lockedHolds??[]:[]}}async fetchAvailableBalance(e){const t=await this.fetchTokenBalance(e);if(!t)return null;const n="availableQuantity"in t;return{tokenId:t.tokenId,availableQuantity:n?t.availableQuantity??String(t.quantity):String(t.quantity),totalQuantity:String(t.quantity)}}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:i,privateKey:o,calculateAmountMode:a,currentSupply:r}=e;let s=t;void 0===a&&void 0===r||(s={tokenName:t,...void 0!==a&&{calculateAmountMode:a},...void 0!==r&&{currentSupply:r}});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!==i&&(l.maxAcceptableReverseBondingCurveFeeSlippageFactor=i),void 0!==o&&(l.privateKey=o),await this.buy(l)}async calculateInitialBuyAmount(e){const t={nativeTokenQuantity:e};return this.launchpadAPI.calculateInitialBuyAmount(t)}async buy(e){if(void 0!==e.privateKey&&""!==e.privateKey){const t=this.createOverrideSdk(e.privateKey),{privateKey:n,...i}=e;return t.buy(i)}this.validateWallet(),await this.ensureWebSocketConnection();const t=(await this.bundleService.buyToken(e)).data,n=t?.transactionId;if(void 0===n||""===n)throw Ot("No transaction ID returned from buy operation");return this.waitForConfirmation(n,t=>vu(t,n,"buy",e))}async sell(e){if(void 0!==e.privateKey&&""!==e.privateKey){const t=this.createOverrideSdk(e.privateKey),{privateKey:n,...i}=e;return t.sell(i)}this.validateWallet(),await this.ensureWebSocketConnection();const t=(await this.bundleService.sellToken(e)).data,n=t?.transactionId;if(void 0===n||""===n)throw Ot("No transaction ID returned from sell operation");return this.waitForConfirmation(n,t=>vu(t,n,"sell",e))}async getBundlerTransactionResult(e){return this.bundleService.getBundlerTransactionResult(e)}async launchToken(e){if(void 0!==e.privateKey&&""!==e.privateKey){const t=this.createOverrideSdk(e.privateKey),{privateKey:n,...i}=e;return t.launchToken(i)}this.validateWallet(),await this.ensureWebSocketConnection();const t=await this.launchpadAPI.launchToken(e);return this.waitForConfirmation(t,n=>{ku(n,t);const i=n.data??{};if(!function(e){if(Vn(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)}(i))throw new fu(`Invalid launch data received for transaction ${t}`);const o={transactionId:t,vaultAddress:i.vaultAddress??"",tokenStringKey:i.tokenStringKey??"",tokenName:e.tokenName,tokenSymbol:e.tokenSymbol,creatorAddress:i.creatorAddress??this.getAddress(),timestamp:Date.now(),...void 0!==n.blockHash&&""!==n.blockHash?{blockHash:n.blockHash}:{},...void 0!==n.gasUsed?{gasUsed:n.gasUsed}:{}};return"string"==typeof e.tokenImage&&(o.tokenImage=e.tokenImage),void 0!==e.preBuyQuantity&&(o.preBuyQuantity=e.preBuyQuantity),""!==o.vaultAddress&&this.tokenResolverService.set(e.tokenName,o.vaultAddress),o})}async uploadTokenImage(e){if(void 0!==e.privateKey&&""!==e.privateKey){const t=this.createOverrideSdk(e.privateKey),{privateKey:n,...i}=e;return t.uploadTokenImage(i)}return this.validateWallet(),this.launchpadService.uploadImageByTokenName(e)}async checkPoolExists(e,t){return this.launchpadService.checkPoolExists(e,t)}async isTokenNameAvailable(e){return this.launchpadService.isTokenNameAvailable(e)}async isTokenSymbolAvailable(e){return this.launchpadService.isTokenSymbolAvailable(e)}async fetchToken(e){return this.launchpadService.fetchToken(e)}async validateToken(e){return this.launchpadService.validateToken(e)}async fetchTokenStats(e){return this.launchpadService.fetchTokenStats(e)}async fetchUserBalances(e){return this.launchpadService.fetchUserBalances(e)}async fetchUserReport(e){return this.launchpadService.fetchUserReport(e)}async fetchProfile(e){const{normalizeAddressInput:t}=await Promise.resolve().then(function(){return bi}),n=t(e)??this.getAddress();return this.launchpadService.fetchProfile(n)}async fetchReferralUrl(){this.validateWallet();const e=this.getAddress();return await this.dexApiHttp.get(m,void 0,{"x-wallet-address":e})}async fetchReferrals(e){let t;if(void 0!==e?.address&&""!==e.address)if(e.address.startsWith("client|"))t=e.address;else{const{normalizeAddressInput:n}=await Promise.resolve().then(function(){return bi}),i=n(e.address);if(void 0===i)throw new wt(`Invalid address format: "${e.address}". Expected formats: eth|0x..., 0x..., or client|...`);t=i}else t=this.getAddress();const n=e?.page??1,i=e?.limit??10,o={pageNumber:n,limit:i,sortBy:e?.sortBy??"joined",sortDir:e?.sortDir??"desc"},a=await this.dexApiHttp.get(p,o,{"x-wallet-address":t});if(!Array.isArray(a))throw new bt("Unexpected API response: expected array, got "+typeof a);return{referrals:a,page:n,limit:i,hasMore:a.length===i}}async fetchAllReferrals(e){const t=await cs((t,n)=>this.fetchReferrals({...e,page:t,limit:n}).then(e=>({items:e.referrals,page:e.page,limit:e.limit,total:0,totalPages:0,hasNext:e.hasMore,hasPrevious:e.page>1})),{maxPages:100,pageSize:100,logger:this.logger});return{referrals:t.items,total:t.items.length}}async fetchReferralsSummary(e){let t;if(void 0!==e?.address&&""!==e.address)if(e.address.startsWith("client|"))t=e.address;else{const{normalizeAddressInput:n}=await Promise.resolve().then(function(){return bi}),i=n(e.address);if(void 0===i)throw new wt(`Invalid address format: "${e.address}". Expected formats: eth|0x..., 0x..., or client|...`);t=i}else t=this.getAddress();const n=await this.dexApiHttp.get(f,void 0,{"x-wallet-address":t});if(void 0===n||"number"!=typeof n.referralCount||void 0===n.rewardTotals)throw new bt(`Unexpected API response: expected { referralCount, rewardTotals }, got ${JSON.stringify(n)}`);return n}async updateProfile(e){const{normalizeAddressInput:t}=await Promise.resolve().then(function(){return bi}),n={...e,address:t(e.address)};if(void 0!==n.privateKey&&""!==n.privateKey){const e=this.createOverrideSdk(n.privateKey),{privateKey:t,...i}=n;return e.updateProfile(i)}return this.validateWallet(),this.launchpadService.updateProfile(n)}async uploadProfileImage(e){const{normalizeAddressInput:t}=await Promise.resolve().then(function(){return bi}),n={...e,address:t(e.address)??this.getAddress()};if(void 0!==n.privateKey&&""!==n.privateKey){const e=this.createOverrideSdk(n.privateKey),{privateKey:t,...i}=n;return e.uploadProfileImage(i)}return this.validateWallet(),this.launchpadService.uploadProfileImage(n)}async fetchTokensHeld(e){const{normalizeAddressInput:t}=await Promise.resolve().then(function(){return bi}),n=t(e?.address)??this.getAddress(),i={pageSize:e?.pageSize??10,address:n,...void 0!==e?.cursor?{cursor:e.cursor}:{}};return void 0!==e?.tokenName&&""!==e.tokenName&&(i.tokenName=e.tokenName),void 0!==e?.search&&""!==e.search&&(i.search=e.search),this.launchpadService.fetchTokensHeld(i)}async fetchTokensCreated(e){const{normalizeAddressInput:t}=await Promise.resolve().then(function(){return bi}),n=t(e?.address)??this.getAddress(),i={type:void 0!==e?.status?"created":"DEFI",address:n,pageSize:e?.pageSize??10};return void 0!==e?.cursor&&(i.cursor=e.cursor),void 0!==e?.tokenName&&""!==e.tokenName&&(i.tokenName=e.tokenName),void 0!==e?.search&&""!==e.search&&(i.search=e.search),void 0!==e?.status&&(i.status=e.status),this.launchpadService.fetchTokenList(i)}async getManagedTokens(e){return this.launchpadService.getManagedTokens(e??{})}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 bi}),n={...e,recipientAddress:t(e.recipientAddress)};if(void 0!==n.privateKey&&""!==n.privateKey){const e=this.createOverrideSdk(n.privateKey),{privateKey:t,...i}=n;return e.transferGala(i)}return this.validateWallet(),this.galaChainService.transferGala(n)}async transferToken(e){const{normalizeAddressInput:t}=await Promise.resolve().then(function(){return bi}),n={...e,to:t(e.to)};if(void 0!==n.privateKey&&""!==n.privateKey){const e=this.createOverrideSdk(n.privateKey),{privateKey:t,...i}=n;return e.transferToken(i)}return this.validateWallet(),this.galaChainService.transferToken(n)}async resolveTokenClassKey(e){return this.galaChainService.resolveTokenClassKey(e)}async convertTokenNameToTokenClassKey(e){return this.resolveTokenClassKey(e)}async lockTokens(e){const t=await Promise.all(e.tokens.map(async e=>{if(void 0!==e.lockAuthority&&""!==e.lockAuthority){const{normalizeAddressInput:t}=await Promise.resolve().then(function(){return bi});return{...e,lockAuthority:t(e.lockAuthority)}}return e})),n={...e,tokens:t};if(void 0!==n.privateKey&&""!==n.privateKey){const e=this.createOverrideSdk(n.privateKey),{privateKey:t,...i}=n;return e.lockTokens(i)}return this.validateWallet(),this.galaChainService.lockTokens(n)}async unlockTokens(e){if(void 0!==e.privateKey&&""!==e.privateKey){const t=this.createOverrideSdk(e.privateKey),{privateKey:n,...i}=e;return t.unlockTokens(i)}return this.validateWallet(),this.galaChainService.unlockTokens(e)}async burnTokens(e){if(void 0!==e.privateKey&&""!==e.privateKey){const t=this.createOverrideSdk(e.privateKey),{privateKey:n,...i}=e;return t.burnTokens(i)}return this.validateWallet(),this.galaChainService.burnTokens(e)}async resolveVaultAddress(e){return this.tokenResolverService.resolveTokenToVault(e)}getCacheInfo(){const e={...this.launchpadAPI.getCacheStats()};if(this._bridgeableTokenService){const t=this._bridgeableTokenService.getCacheStats();e.bridgeableTokens={ETHEREUM:t.tokensByNetwork.ETHEREUM,SOLANA:t.tokensByNetwork.SOLANA,total:t.totalTokens}}return this._wrappableTokenService&&(e.wrappableTokens=this._wrappableTokenService.getCacheStats()),e}clearCache(e){this.launchpadAPI.clearCache(e),void 0!==e&&""!==e||(void 0!==this._bridgeableTokenService&&this._bridgeableTokenService.clearCache(),void 0!==this._wrappableTokenService&&this._wrappableTokenService.clearCache())}validateConfiguration(){try{Kn(this.config.timeout,1,3e5,"timeout")}catch{this.logger.warn(`Invalid timeout value: ${this.config.timeout??"undefined"}. Using default 30000ms.`),this.config.timeout=3e4}if(void 0===this.config.baseUrl||""===this.config.baseUrl)throw Ut("baseUrl is required in configuration","baseUrl");if(void 0===this.config.webSocketUrl||""===this.config.webSocketUrl)throw Ut("webSocketUrl is required in configuration","webSocketUrl");try{new URL(this.config.baseUrl)}catch{throw Ut(`Invalid baseUrl format: ${this.config.baseUrl}`,"baseUrl")}try{new URL(this.config.webSocketUrl)}catch{throw Ut(`Invalid webSocketUrl format: ${this.config.webSocketUrl}`,"webSocketUrl")}if(void 0!==this.config.galaChainBaseUrl&&""!==this.config.galaChainBaseUrl)try{new URL(this.config.galaChainBaseUrl)}catch{throw Ut(`Invalid galaChainBaseUrl format: ${this.config.galaChainBaseUrl}`,"galaChainBaseUrl")}if(void 0!==this.config.bundleBaseUrl&&""!==this.config.bundleBaseUrl)try{new URL(this.config.bundleBaseUrl)}catch{throw Ut(`Invalid bundleBaseUrl format: ${this.config.bundleBaseUrl}`,"bundleBaseUrl")}if(void 0!==this.config.launchpadFrontendUrl&&""!==this.config.launchpadFrontendUrl)try{new URL(this.config.launchpadFrontendUrl)}catch{throw Ut(`Invalid launchpadFrontendUrl format: ${this.config.launchpadFrontendUrl}`,"launchpadFrontendUrl")}}parseSlippageToleranceFactor(e){const t=xn("string"==typeof e||"number"==typeof e?e:String(e),Su.DEFAULT_SLIPPAGE_TOLERANCE_FACTOR);return t<0||t>1?(this.logger.warn(`Invalid slippage tolerance factor: ${String(e)}, using default: ${Su.DEFAULT_SLIPPAGE_TOLERANCE_FACTOR}`),Su.DEFAULT_SLIPPAGE_TOLERANCE_FACTOR):t}parseFeeSlippageFactor(e){const t=xn("string"==typeof e||"number"==typeof e?e:String(e),Su.DEFAULT_MAX_ACCEPTABLE_REVERSE_BONDING_CURVE_FEE_SLIPPAGE_FACTOR);return t<0||t>1?(this.logger.warn(`Invalid fee slippage factor: ${String(e)}, using default: ${Su.DEFAULT_MAX_ACCEPTABLE_REVERSE_BONDING_CURVE_FEE_SLIPPAGE_FACTOR}`),Su.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 yu(e,n.status,n.message);let i;try{i=t(n)}catch(t){if(t instanceof fu)throw t;throw new fu(`Failed to transform WebSocket response for transaction ${e}`,ot(t)?t:new Error(at(t)))}return this.logger.debug(`Transaction confirmed: ${e}`,i),i}catch(t){if(this.logger.error(`Transaction confirmation failed: ${e}`,t),t instanceof yu||t instanceof fu)throw t;throw new fu(`WebSocket confirmation failed for transaction ${e}`,ot(t)?t:new Error(at(t)))}}async warmCacheFromPools(e){if(void 0===e||!Array.isArray(e))return;const{extractMetadataFromPoolData:t,isValidPoolForCaching:n}=await Promise.resolve().then(function(){return Eu});e.forEach(e=>{if(!n(e))return;const i=t(e,this.logger);i&&this.launchpadAPI.warmCacheFromPoolData(e.tokenName,i)})}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,i,o,a=.01){return this.validateWallet(),this.gswapService.executeSwap({fromToken:e,toToken:t,inputAmount:n,estimatedOutput:i,feeTier:o,slippageTolerance:a})}async getSwapUserAssets(e){return this.gswapService.getUserAssets(e)}async getAllSwapUserAssets(e){return this.gswapService.getAllUserAssets(e)}async fetchAvailableDexTokens(e={}){return this.gswapService.fetchAvailableDexTokens(e)}async fetchAllAvailableDexTokens(e={}){return this.gswapService.fetchAllAvailableDexTokens(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,i){let o,a;"string"==typeof n?(o=n,a=i):"object"==typeof n?a=n:i&&(a=i);return await this.gswapService.getUserLiquidityPositions(e,t,o,a)}async getAllSwapUserLiquidityPositions(e,t){const n=await this.gswapService.getAllSwapUserLiquidityPositions(e,t);if(!t?.withPrices){if(Array.isArray(n))return n;if(void 0!==n&&"items"in n)return n.items}return n}async getSwapLiquidityPosition(e,t){return this.gswapService.getLiquidityPosition(e,t)}async getSwapLiquidityPositionById(e,t,n,i,o,a,r){return this.gswapService.getLiquidityPositionById(e,t,n,i,o,a,r)}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={token0:e.token0,token1:e.token1,fee:e.feeTier,tickLower:e.tickLower,tickUpper:e.tickUpper,amount0Desired:e.amount0Desired,amount1Desired:e.amount1Desired};return void 0!==e.amount0Min&&(t.amount0Min=e.amount0Min),void 0!==e.amount1Min&&(t.amount1Min=e.amount1Min),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,i=Math.max(t?.intervalMs??3e4,n);void 0!==t?.intervalMs&&t.intervalMs<n&&this.logger.warn(`Poll interval ${t.intervalMs}ms is below minimum 1000ms. Using minimum interval instead.`);const o=t?.minTVL,a=t?.tokens,r=new Map;let s=!0,c=null;let l=0;const d=async()=>{if(s){try{const t=await this.fetchDexPools({limit:20});l>0&&(this.logger.debug("Successfully recovered from polling errors"),l=0),t.items.forEach(t=>{const n=(e=>`${e.token0}-${e.token1}-${e.fee}`)(t);if(!r.has(n)){if((e=>{if(r.set(e,!0),r.size>1e3){const e=r.keys().next().value;void 0!==e&&r.delete(e)}})(n),void 0!==o){if((t.token0Tvl+t.token1Tvl)/2<o)return}if(a&&a.length>0){if(!(a.includes(t.token0)||a.includes(t.token1)))return}e(t)}})}catch(e){l++;const t=at(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=i*Math.pow(2,e);c=setTimeout(()=>{d()},t)}}};return d(),()=>{s=!1,c&&clearTimeout(c),this.logger.debug("Stopped watching for DEX pool creation")}}onLaunchpadTokenCreation(e,t){const n=1e3,i=Math.max(t?.intervalMs??3e4,n);null!=t?.intervalMs&&t.intervalMs<n&&this.logger.warn(`Poll interval ${t.intervalMs}ms is below minimum 1000ms. Using minimum interval instead.`);const o=t?.creatorAddress,a=new Map;let r=!0,s=null;let c=0;const l=async()=>{if(r){try{const t=await this.fetchPools({type:"recent",pageSize:20});c>0&&(this.logger.debug("Successfully recovered from polling errors"),c=0),t.items.forEach(t=>{a.has(t.tokenName)||((e=>{if(a.set(e,!0),a.size>1e3){const e=a.keys().next().value;void 0!==e&&a.delete(e)}})(t.tokenName),void 0!==o&&""!==o&&t.creatorAddress!==o||e(t))})}catch(e){c++;const t=at(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(r){const e=Math.min(Math.max(c-1,0),2),t=i*Math.pow(2,e);s=setTimeout(()=>{l()},t)}}};return l(),()=>{r=!1,s&&clearTimeout(s),this.logger.debug("Stopped watching for launchpad token creation")}}normalizeFee(e){if(null==e)return null;let t;if("string"==typeof e||"number"==typeof e)t=e;else{if("boolean"!=typeof e)return null;t=e?"1":"0"}const n=On(t,Number.NaN);return Number.isNaN(n)?null:1===n||1e4===n?1e4:.3===n||3e3===n?3e3:.05===n||500===n?500:!Number.isInteger(n)||500!==n&&3e3!==n&&1e4!==n?null:n}extractField(e,...t){if("object"!=typeof e||null===e)return"";const n=e;for(const e of t)if(null!==n[e]&&void 0!==n[e]){const t=n[e];return"string"==typeof t?t:"number"==typeof t||"boolean"==typeof t?String(t):"object"==typeof t&&null!==t&&"toString"in t?t.toString():""}return""}looksLikePoolPair(e){if("string"!=typeof e)return null;const t=e.trim();return ll.isValidPoolKey(t)?t:null}buildPoolPairFromObject(e){if("object"!=typeof e||null===e)return null;const t=e,n=this.extractField(t,"token0ClassKey","token0Class","token0","token0Symbol"),i=this.extractField(t,"token1ClassKey","token1Class","token1","token1Symbol"),o=this.normalizeFee(t.feeTier??t.fee??t.feeTierBps??t.liquidityFeeBps??t.feeBps);return""===n||""===i||null===o?null:`${n}/${i}/${o}`}parsePoolPairString(e){const t=ll.parsePoolKey(e);if(!t)return null;const n=Sr(t.token0)?br(t.token0).collection:t.token0,i=Sr(t.token1)?br(t.token1).collection:t.token1,o=t.feeTier.toString();return""===n||""===i||""===o?null:{token0:n,token1:i,fee:o,poolPair:e}}serializeBalanceToken(e){if(null==e||"object"!=typeof e)return"";const t=e,n=t.collection??t.token??"",i=t.category??"",o=t.type??"",a=t.additionalKey??"none";return[""!==n?n:"",""!==i?i:"none",""!==o?o:"none",""!==a?a:"none"].join("|")}buildPoolPairFromBalances(e){if("object"!=typeof e||null===e)return null;const t=e,n=t.userBalanceDelta??t.balanceDelta??t.delta;if(null==n||"object"!=typeof n)return null;const i=n,o=i.token0Balance??i.token0??i.baseBalance??i.primaryBalance,a=i.token1Balance??i.token1??i.quoteBalance??i.secondaryBalance,r=this.serializeBalanceToken(o),s=this.serializeBalanceToken(a),c=this.normalizeFee(t.poolFee??t.feeTier??t.fee??t.feeTierBps??t.liquidityFeeBps);return""===r||""===s||null===c?null:`${r}/${s}/${c}`}extractPoolDataFromPayload(e){if("string"==typeof e){const t=this.looksLikePoolPair(e);return null!=t?this.parsePoolPairString(t):null}if("object"!=typeof e||null===e)return null;const t=e,n=this.looksLikePoolPair(t.poolPair);if(null!=n)return this.parsePoolPairString(n);const i=this.buildPoolPairFromBalances(t);if(null!==i)return this.parsePoolPairString(i);const o=this.buildPoolPairFromObject(t);if(null!==o)return this.parsePoolPairString(o);if(void 0!==t.pool&&null!==t.pool&&"object"==typeof t.pool){const e=this.extractPoolDataFromPayload(t.pool);if(null!==e)return e}return null}matchesPoolFilter(e,t){if(void 0!==t?.tokenFilter&&""!==t.tokenFilter){if(!(e.token0===t.tokenFilter||e.token1===t.tokenFilter))return!1}if(t?.pairTokens){const[n,i]=t.pairTokens,o=e.token0===n||e.token1===n,a=e.token0===i||e.token1===i;if(!o||!a||n===i)return!1}if(void 0!==t?.feeTierFilter){if(this.normalizeFee(t.feeTierFilter)!==this.normalizeFee(e.fee))return!1}return!0}matchesCreatorFilter(e,t){return void 0===t||""===t||e.creatorAddress===t}subscribeToTokenCreations(e,t){if(this.logger.debug("Subscribing to token creation broadcasts"+(void 0!==t?.creatorFilter&&""!==t.creatorFilter?` (filter: ${t.creatorFilter})`:"")),null===this.websocketService||void 0===this.websocketService)throw Ut("WebSocket service not initialized. Ensure websocketUrl is configured and the service is connected.","websocketService");let n=!1,i=null;const o=(n,...i)=>{try{if(i.length>0&&"object"==typeof i[0]&&null!==i[0]){const n=i[0].data;if(void 0!==n?.Data&&null!==n.Data&&"object"==typeof n.Data){const i=n.Data;if("CreateSale"===i.functionName){const n={tokenName:""!==i.tokenName?i.tokenName:"",symbol:""!==i.symbol?i.symbol:"",creatorAddress:""!==i.creatorAddress?i.creatorAddress:"",description:""!==i.description?i.description:"",image:""!==i.image?i.image:"",vaultAddress:""!==i.vaultAddress?i.vaultAddress:"",tokenStringKey:""!==i.tokenStringKey?i.tokenStringKey:"",preBuyQuantity:""!==i.initialBuyQuantity?i.initialBuyQuantity:"0",websiteUrl:""!==i.websiteUrl?i.websiteUrl:"",telegramUrl:""!==i.telegramUrl?i.telegramUrl:"",twitterUrl:""!==i.twitterUrl?i.twitterUrl:"",instagramUrl:""!==i.instagramUrl?i.instagramUrl:"",facebookUrl:""!==i.facebookUrl?i.facebookUrl:"",redditUrl:""!==i.redditUrl?i.redditUrl:"",tiktokUrl:""!==i.tiktokUrl?i.tiktokUrl:"",isFinalized:!1!==i.isFinalized&&null!==i.isFinalized&&void 0!==i.isFinalized&&i.isFinalized};this.matchesCreatorFilter(n,t?.creatorFilter)&&e(n)}}}}catch(e){this.logger.warn(`Error processing token creation broadcast: ${at(e)}`)}};let a=this.websocketService.getSocket();if(a)a.onAny(o),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: ${at(e)}`);this.logger.warn("Failed to establish WebSocket connection:",e),t?.onError&&t.onError(n)});let e=0;const r=()=>{if(a=this.websocketService.getSocket(),!a&&e<Su.TOKEN_CREATION_SOCKET_WAIT_ATTEMPTS)return e++,void(i=setTimeout(()=>r(),Su.TOKEN_CREATION_SOCKET_POLL_INTERVAL_MS));if(!a&&e>=Su.TOKEN_CREATION_SOCKET_WAIT_ATTEMPTS){const e=new Error(`WebSocket not available after ${Su.TOKEN_CREATION_SOCKET_WAIT_ATTEMPTS*Su.TOKEN_CREATION_SOCKET_POLL_INTERVAL_MS}ms`);return this.logger.warn("Token creation broadcast subscription timeout:",e.message),void(t?.onError&&t.onError(e))}a&&(a.onAny(o),n=!0,this.logger.debug("Token creation broadcast listener registered"))};r()}return()=>{try{if(null!==i&&(clearTimeout(i),i=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(o),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 i=[];if("string"==typeof e){const n=this.looksLikePoolPair(e);if(null!=n){const e=this.parsePoolPairString(n);null===e||t.has(e.poolPair)||(t.add(e.poolPair),i.push(e))}return i}if("object"!=typeof e||null===e)return i;if(n.has(e))return i;n.add(e);const o=this.extractPoolDataFromPayload(e);o&&!t.has(o.poolPair)&&(t.add(o.poolPair),i.push(o));for(const o of Object.values(e)){const e=this.walkPayloadForPools(o,t,n);i.push(...e)}return i}subscribeToDexPoolAdded(e,t){if(this.logger.debug("Subscribing to DEX pool creation broadcasts"+(void 0!==t?.tokenFilter&&""!==t.tokenFilter?` (filter: ${t.tokenFilter})`:void 0!==t?.pairTokens?` (pair: ${t.pairTokens.join("/")})`:"")),null===this.websocketService||void 0===this.websocketService)throw Ut("WebSocket service not initialized. Ensure websocketUrl is configured and the service is connected.","websocketService");let n=!1,i=null;const o=new Set,a=(n,...i)=>{try{for(const n of i){const i=this.walkPayloadForPools(n,o);for(const n of i)this.matchesPoolFilter(n,t)&&e(n)}}catch(e){this.logger.warn(`Error processing DEX pool broadcast: ${at(e)}`)}};let r=this.websocketService.getSocket();if(r)r.onAny(a),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: ${at(e)}`);this.logger.warn("Failed to establish WebSocket connection:",e),t?.onError&&t.onError(n)});let e=0;const o=()=>{if(r=this.websocketService.getSocket(),!r&&e<Su.DEX_POOL_SOCKET_WAIT_ATTEMPTS)return e++,void(i=setTimeout(()=>o(),Su.DEX_POOL_SOCKET_POLL_INTERVAL_MS));if(!r&&e>=Su.DEX_POOL_SOCKET_WAIT_ATTEMPTS){const e=new Error(`WebSocket not available after ${Su.DEX_POOL_SOCKET_WAIT_ATTEMPTS*Su.DEX_POOL_SOCKET_POLL_INTERVAL_MS}ms`);return this.logger.warn("DEX pool subscription timeout:",e.message),void(t?.onError&&t.onError(e))}r&&(r.onAny(a),n=!0,this.logger.debug("DEX pool broadcast listener registered"))};o()}return()=>{try{if(null!==i&&(clearTimeout(i),i=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(a),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"+(void 0!==t?.tokenFilter&&""!==t.tokenFilter?` (filter: ${t.tokenFilter})`:void 0!==t?.pairTokens?` (pair: ${t.pairTokens.join("/")})`:"")),null===this.websocketService||void 0===this.websocketService)throw Ut("WebSocket service not initialized. Ensure websocketUrl is configured and the service is connected.","websocketService");let n=null,i=null,o=null,a=!1;const r=async e=>{const t=ll.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));a=!0,i=new kl(c,r,this.dexQuoteService,t??{},this.logger),o=i.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: ${at(e)}`);this.logger.warn("Failed to establish WebSocket connection:",e),t?.onError&&t.onError(n)})),s(),()=>{try{n&&clearTimeout(n),o&&a&&o(),i&&i.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"+(void 0!==t?.tokenFilter&&""!==t.tokenFilter?` (filter: ${t.tokenFilter})`:void 0!==t?.pairTokens?` (pair: ${t.pairTokens.join("/")})`:"")),null===this.websocketService||void 0===this.websocketService)throw Ut("WebSocket service not initialized. Ensure websocketUrl is configured and the service is connected.","websocketService");let n=!1,i=null;const o=new Set,a=new bu(this.logger),r=(n,...i)=>{try{for(const n of i){const i=a.walkPayloadForLiquidityEvents(n,o);for(const n of i)if(this.matchesLiquidityFilter(n,t))try{const t=e(n);t instanceof Promise&&t.catch(e=>{this.logger.warn(`Error in liquidity event callback: ${at(e)}`)})}catch(e){this.logger.warn(`Error in liquidity event callback: ${at(e)}`)}}}catch(e){this.logger.warn(`Error processing DEX liquidity broadcast: ${at(e)}`)}};let s=this.websocketService.getSocket();if(s)s.onAny(r),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: ${at(e)}`);this.logger.warn("Failed to establish WebSocket connection:",e),t?.onError&&t.onError(n)});let e=0;const o=()=>{if(s=this.websocketService.getSocket(),!s&&e<Su.DEX_POOL_SOCKET_WAIT_ATTEMPTS)return e++,void(i=setTimeout(()=>o(),Su.DEX_POOL_SOCKET_POLL_INTERVAL_MS));if(!s&&e>=Su.DEX_POOL_SOCKET_WAIT_ATTEMPTS){const e=new Error(`WebSocket not available after ${Su.DEX_POOL_SOCKET_WAIT_ATTEMPTS*Su.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(r),n=!0,this.logger.debug("DEX liquidity broadcast listener registered"))};o()}return async()=>{try{if(null!==i&&(clearTimeout(i),i=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(r),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(void 0!==t.positionId&&""!==t.positionId&&e.positionId!==t.positionId)return!1;if(void 0!==t.poolHash&&""!==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(void 0!==t.userFilter&&""!==t.userFilter&&e.userAddress!==t.userFilter)return!1;if(void 0!==t.tokenFilter&&""!==t.tokenFilter){const n=void 0!==e.token0&&""!==e.token0&&Bo(e.token0,t.tokenFilter),i=void 0!==e.token1&&""!==e.token1&&Bo(e.token1,t.tokenFilter);if(!n&&!i)return!1}if(void 0!==t.pairTokens){const[n,i]=t.pairTokens,o=e.token0??"",a=e.token1??"",r=Bo(o,n)&&Bo(a,i),s=Bo(o,i)&&Bo(a,n);if(!r&&!s)return!1}if(void 0!==t.minAmount&&""!==t.minAmount){const n=xn(t.minAmount),i=xn(e.amounts[0]),o=xn(e.amounts[1]);if(Math.abs(i)<n&&Math.abs(o)<n)return!1}return!0}normalizeFeeTier(e){if("number"==typeof e)return e>=100?e:Math.round(1e4*e);return function(e,t=3e3){if(sn(e))return t;if("number"==typeof e)return isNaN(e)?t:Math.floor(e);const n=String(e).trim();if(n.endsWith("%")){const e=xn(n.replace("%",""));return Math.floor(1e4*e)}const i=parseFloat(n);return isNaN(i)?t:i<100?Math.floor(1e4*i):Math.floor(i)}(e)}getNftCollectionClaimFee(){return this.nftCollectionService.getCollectionClaimFee()}getNftTokenClassCreateFee(){return this.nftCollectionService.getTokenClassCreateFee()}estimateNftMintFee(e){return this.nftCollectionService.estimateMintFee(e)}estimateNftOperationFees(e){return this.nftCollectionService.estimateNftOperationFees(e)}claimNftCollection(e){return this.nftCollectionService.claimCollection(e)}fetchNftCollections(e){return this.nftCollectionService.fetchUserCollections(e)}isNftCollectionAvailable(e){return this.nftCollectionService.isCollectionAvailable(e)}createNftTokenClass(e){return this.nftCollectionService.createTokenClass(e)}fetchNftTokenClasses(e){return this.nftCollectionService.fetchTokenClasses(e)}mintNft(e){return this.nftCollectionService.mintNft(e)}fetchNftBalances(e,t){return this.nftCollectionService.fetchNftBalances(e,t)}async cleanup(){try{if(this.logger.debug("Starting cleanup..."),void 0!==this._eventsBatcherService)try{await this._eventsBatcherService.shutdown(5e3),this._eventsBatcherService=void 0}catch(e){this.logger.warn("Error during events shutdown:",e)}this.http.cleanup(),null!==this.websocketService&&void 0!==this.websocketService&&this.websocketService.disconnect(),void 0!==this._streamWebSocketService&&(this._streamWebSocketService.disconnect(),this._streamWebSocketService=void 0),this.logger.debug("Cleanup completed")}catch(e){this.logger.error("Error during cleanup:",e)}}static cleanupAll(e=!1){const t=new an({debug:e,context:"LaunchpadSDK"});t.debug("Starting global cleanup..."),su.cleanupAll(e),t.debug("Global cleanup completed")}}Su.DEFAULT_SLIPPAGE_TOLERANCE_FACTOR=.15,Su.DEFAULT_MAX_ACCEPTABLE_REVERSE_BONDING_CURVE_FEE_SLIPPAGE_FACTOR=.01,Su.DEFAULT_LAUNCHPAD_TOKEN_MAX_SUPPLY=Zt.DEFAULT_LAUNCHPAD_TOKEN_MAX_SUPPLY,Su.DEFAULT_CALCULATE_AMOUNT_MODE="local",Su.TOKEN_CREATION_SOCKET_WAIT_ATTEMPTS=30,Su.TOKEN_CREATION_SOCKET_POLL_INTERVAL_MS=100,Su.DEX_POOL_SOCKET_WAIT_ATTEMPTS=30,Su.DEX_POOL_SOCKET_POLL_INTERVAL_MS=100;class Au{static generateWallet(){try{const e=t.Wallet.createRandom();if(null==e.mnemonic?.phrase||""===e.mnemonic.phrase)throw Ft("mnemonic","Mnemonic phrase");const n=this.toGalaAddress(e.address);return{privateKey:e.privateKey,address:e.address,galaAddress:n,mnemonic:e.mnemonic.phrase,wallet:new t.Wallet(e.privateKey)}}catch(e){if("undefined"!=typeof process&&"test"===process.env.NODE_ENV){const e=`test-wallet-${Date.now()}-${++this.testCounter}`,n="0x"+Buffer.from(e).toString("hex").padStart(64,"1").slice(0,64),i=new t.Wallet(n),o=this.toGalaAddress(i.address);return{privateKey:i.privateKey,address:i.address,galaAddress:o,mnemonic:"test test test test test test test test test test test junk",wallet:i}}throw e}}static fromPrivateKey(e){const n=new t.Wallet(e),i=this.toGalaAddress(n.address);return{privateKey:n.privateKey,address:n.address,galaAddress:i,mnemonic:"",wallet:n}}static fromMnemonic(e,n=0){try{const i=t.Mnemonic.fromPhrase(e),o=t.HDNodeWallet.fromMnemonic(i,`m/44'/60'/0'/0/${n}`),a=new t.Wallet(o.privateKey),r=this.toGalaAddress(a.address);return{privateKey:a.privateKey,address:a.address,galaAddress:r,mnemonic:e,wallet:a}}catch(i){if("undefined"!=typeof process&&"test"===process.env.NODE_ENV){const i=`test-mnemonic-index-${n}-${e}`,o="0x"+Buffer.from(i).toString("hex").padStart(64,"1").slice(0,64),a=new t.Wallet(o),r=this.toGalaAddress(a.address);return{privateKey:a.privateKey,address:a.address,galaAddress:r,mnemonic:e,wallet:a}}throw i}}static toGalaAddress(e){const n=pi(e);if(!/^[a-fA-F0-9]{40}$/.test(n))throw Rt("address","a valid Ethereum address (40 hex characters)");return`eth|${t.getAddress(`0x${n}`).slice(2)}`}static toEthereumAddress(e){try{return yi(e)}catch(t){const n=at(t);if(n.includes("required")||!e?.startsWith("eth|"))throw Ft("galaAddress","Gala address starting with eth|");if(n.includes("Invalid backend"))throw Rt("galaAddress","Gala format (eth|{40-hex-chars})");throw t}}static isValidEthereumAddress(e){try{const t=pi(e);return/^[a-fA-F0-9]{40}$/.test(t)}catch{return!1}}static isValidGalaAddress(e){return""!==e&&"backend"===wi(e)}static generateMultipleWallets(e=1){Kn(e,1,100,"count");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}`,i="0x"+Buffer.from(e).toString("hex").padStart(64,"1").slice(0,64);t.push(this.fromPrivateKey(i))}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: ${null!=e.mnemonic&&""!==e.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 Tu(e){if(void 0===e)return Au.generateWallet();const t=e.trim();if(!jn(t))throw Rt("input","a non-empty string");if(function(e){const t=pi(e);return/^[a-fA-F0-9]{64}$/.test(t)}(t))return Au.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 Au.fromMnemonic(t);throw Rt("input","a private key (64 hex characters) or mnemonic (12/24 words)","Wallet input")}Au.testCounter=0;var Eu=Object.freeze({__proto__:null,extractMetadataFromPoolData:function(e,t){const n={};if(void 0!==e.vaultAddress&&(n.vaultAddress=e.vaultAddress),void 0!==e.reverseBondingCurveMinFeePortion){const i=xn(e.reverseBondingCurveMinFeePortion,NaN);isNaN(i)?t&&t.debug(`Skipping invalid reverseBondingCurveMinFeePortion for ${e.tokenName}: "${e.reverseBondingCurveMinFeePortion}"`):n.reverseBondingCurveMinFeeFactor=i}if(void 0!==e.reverseBondingCurveMaxFeePortion){const i=xn(e.reverseBondingCurveMaxFeePortion,NaN);isNaN(i)?t&&t.debug(`Skipping invalid reverseBondingCurveMaxFeePortion for ${e.tokenName}: "${e.reverseBondingCurveMaxFeePortion}"`):n.reverseBondingCurveMaxFeeFactor=i}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}});exports.EVENT_TYPES={LOGIN:"login",LOGOUT:"logout",TOKEN_BUY:"token_buy",TOKEN_SELL:"token_sell",TOKEN_SWAP:"token_swap",TOKEN_CREATED:"token_created",TOKEN_GRADUATED:"token_graduated",PAGE_VIEW:"page_view",PAGE_LEAVE:"page_leave",STREAM_START:"stream_start",STREAM_END:"stream_end",STREAM_VIEWERS_CHANGED:"stream_viewers_changed",CHAT_MESSAGE_SENT:"chat_message_sent",CHAT_MESSAGE_RECEIVED:"chat_message_received",LIQUIDITY_ADDED:"liquidity_added",LIQUIDITY_REMOVED:"liquidity_removed",BRIDGE_DEPOSIT:"bridge_deposit",BRIDGE_WITHDRAW:"bridge_withdraw",PROFILE_UPDATED:"profile_updated",BALANCE_CHANGED:"balance_changed",ERROR_OCCURRED:"error_occurred"},exports.LaunchpadSDK=Su,exports.SDK_VERSION=Hr,exports.TransactionFailedError=yu,exports.WebSocketError=fu,exports.WebSocketTimeoutError=class extends fu{constructor(e,t){super(`WebSocket confirmation timeout for transaction ${e} after ${t}ms`),this.name="WebSocketTimeoutError"}},exports.createLaunchpadSDK=function(e){Vn(e)&&(e={});const{wallet:n,env:i,config:o={},...a}=e,r={...a,...o},{wallet:s,env:c,config:l,...d}=r;let u;if(Vn(n)){u=Tu().wallet}else if("string"==typeof n){u=Tu(n).wallet}else{if(!(n instanceof t.Wallet))throw Ut("Invalid wallet input. Expected string (private key or mnemonic) or Wallet instance.","wallet");u=n}const h={wallet:u,...void 0!==i?{env:i}:{},debug:!1,timeout:3e4,...d};return new Su(h)},exports.createWallet=Tu,exports.getEthereumAddressFromPrivateKey=function(e){if(!e.match(/^0x[a-fA-F0-9]{64}$/))throw Rt("privateKey","0x-prefixed 64 hexadecimal characters","Private key");return t.getAddress(t.computeAddress(e))},exports.getPublicKeyFromPrivateKey=function(e){if(!e.match(/^0x[a-fA-F0-9]{64}$/))throw Rt("privateKey","0x-prefixed 64 hexadecimal characters","Private key");const n=new t.SigningKey(e);return{publicKey:n.publicKey,compressedPublicKey:n.compressedPublicKey}};