@avalabs/fusion-sdk 0.15.1 → 0.16.0

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.
@@ -1,2 +1,2 @@
1
- require(`../_virtual/_rolldown/runtime.cjs`);const e=require(`../constants.cjs`),t=require(`../type-guards.cjs`),n=require(`./_utils.cjs`),r=require(`./constants.cjs`);let i=require(`zod`),a=require(`viem`);var o=class{clock;props;pruneIntervalMs;transferServices;refreshBufferSeconds;quotes=[];subscribers=new Set;started=!1;done=!1;pruneTimerId=null;lastBestId=null;hasReceivedAnyQuote=!1;id=crypto.randomUUID();serviceState=new Map;constructor(e,t,n={}){this.clock=n.clock??(()=>Math.floor(Date.now()/1e3)),this.props=e,this.pruneIntervalMs=n.pruneIntervalMs??1e3,this.transferServices=t,this.refreshBufferSeconds=n.refreshBufferSeconds??5}getQuotes(){let e=this.clock(),t=n.sortQuotes(n.pruneExpiredQuotes({quotes:this.quotes,nowSeconds:e}));return[t[0]??null,t]}subscribe(e){this.subscribers.add(e);let t=!1;return this.started||this.start(),()=>{if(!t){if(t=!0,this.subscribers.size===1&&this.subscribers.has(e)){this.complete(`unsubscribed`);return}this.subscribers.delete(e)}}}start(){if(this.started=!0,this.done=!1,this.quotes=[],this.lastBestId=null,this.hasReceivedAnyQuote=!1,c(this.props)){this.complete(`no-eligible-services`);return}let e=!1;for(let t of this.transferServices)t.analyzeSupport({sourceAsset:this.props.sourceAsset,sourceChainId:this.props.sourceChain.chainId,targetAsset:this.props.targetAsset,targetChainId:this.props.targetChain.chainId})&&(e=!0,this.startStreamForService(t));if(!e){this.complete(`no-eligible-services`);return}this.pruneTimerId=setInterval(()=>this.onPruneTick(),this.pruneIntervalMs)}stop(){this.started=!1,this.pruneTimerId&&=(clearInterval(this.pruneTimerId),null);for(let[e,t]of this.serviceState){try{t.cancel()}catch{}t.refreshTimer&&clearTimeout(t.refreshTimer),t.retryTimer&&clearTimeout(t.retryTimer),this.serviceState.set(e,{cancel:()=>{},done:!1,hasErrored:!1,hasReturnedQuote:!1,retryAttempt:0,retryTimer:null,refreshTimer:null,refreshAtSeconds:null})}}startStreamForService(e){let t=this.serviceState.get(e.type);if(t){try{t.cancel()}catch{}t.refreshTimer&&clearTimeout(t.refreshTimer),t.retryTimer&&clearTimeout(t.retryTimer)}let n=this.makeServiceHandler(e.type),{cancel:r}=e.streamQuotes(this.props,n);this.serviceState.set(e.type,{cancel:r,done:!1,hasErrored:!1,hasReturnedQuote:!1,retryAttempt:t?.retryAttempt??0,retryTimer:null,refreshTimer:null,refreshAtSeconds:null})}makeServiceHandler(e){return(t,...n)=>{if(this.started){if(t===`quote`){let t=n[0];if(l(t)){if(t.serviceType!==e)return;let n=this.serviceState.get(e);n&&(n.hasReturnedQuote=!0),this.onIncomingQuote(t),this.scheduleServiceRefresh(e)}return}if(t===`done`){this.onServiceDone(e);return}if(t===`error`){let t=this.serviceState.get(e);t&&(t.hasErrored=!0);let r=n[0];r instanceof Error&&this.emitError(r)}}}}onIncomingQuote(e){let t=this.clock();if(n.isQuoteExpired({quote:e,nowSeconds:t}))return;this.hasReceivedAnyQuote=!0,this.resetRetryForService(e.serviceType),this.quotes=n.sortQuotes(n.pruneExpiredQuotes({quotes:n.upsertQuote(this.quotes,e),nowSeconds:t}));let r=this.quotes[0];r&&(this.lastBestId=r.id,this.emitQuote({bestQuote:r,quote:e,quotes:this.quotes}))}resetRetryForService(e){let t=this.serviceState.get(e);t&&(t.retryAttempt=0,t.retryTimer&&=(clearTimeout(t.retryTimer),null))}scheduleServiceRetry(e){let t=this.serviceState.get(e);if(!t)return;t.retryTimer&&=(clearTimeout(t.retryTimer),null);let n=Math.min(r.QUOTER_EMPTY_RETRY_MAX_DELAY_MS,r.QUOTER_EMPTY_RETRY_BASE_DELAY_MS*2**t.retryAttempt);t.retryAttempt+=1,t.retryTimer=setTimeout(()=>{let t=this.serviceState.get(e);if(t&&(t.retryTimer=null),!this.started)return;let n=this.transferServices.find(t=>t.type===e);n&&this.startStreamForService(n)},Math.max(0,n))}onServiceDone(e){let t=this.serviceState.get(e);t&&(t.done=!0,this.scheduleServiceRefresh(e),this.hasReceivedAnyQuote&&!t.hasErrored&&!t.hasReturnedQuote&&this.scheduleServiceRetry(e),this.maybeCompleteNoQuotes())}maybeCompleteNoQuotes(){!this.started||this.done||this.hasReceivedAnyQuote||this.serviceState.size===0||[...this.serviceState.values()].every(e=>e.done)&&this.complete(`no-quotes`)}complete(e){if(this.done)return;let t=this.makeDonePayload(e);this.done=!0,this.stop();let n=[...this.subscribers];this.subscribers.clear();for(let e of n)e(`done`,t)}getInitializedServices(){return[...new Set(this.transferServices.map(e=>e.type))]}getEligibleServices(){return[...this.serviceState.keys()]}makeDonePayload(e){if(e===`unsubscribed`)return{reason:e,data:void 0};let t=this.getInitializedServices();return e===`no-eligible-services`?{reason:e,data:{initializedServices:t,quoterProps:this.props}}:{reason:e,data:{eligibleServices:this.getEligibleServices(),initializedServices:t,quoterProps:this.props}}}onPruneTick(){let e=this.clock(),t=n.pruneExpiredQuotes({quotes:this.quotes,nowSeconds:e});if(t.length!==this.quotes.length){this.quotes=n.sortQuotes(t);let e=this.quotes[0]??null,r=e?e.id:null;e&&r!==this.lastBestId?(this.lastBestId=r,this.emitQuote({bestQuote:e,quote:e,quotes:this.quotes})):e||(this.lastBestId=null)}for(let e of this.transferServices)this.serviceState.has(e.type)&&this.scheduleServiceRefresh(e.type)}scheduleServiceRefresh(e){let t=this.serviceState.get(e);if(!t)return;let r=this.clock(),i=n.earliestExpirationForService({quotes:this.quotes,serviceType:e,nowSeconds:r});if(i===null){t.refreshTimer&&=(clearTimeout(t.refreshTimer),null),t.refreshAtSeconds=null;return}let a=Math.max(0,i-this.refreshBufferSeconds);if(a<=r||t.refreshAtSeconds===a&&t.refreshTimer)return;t.refreshTimer&&clearTimeout(t.refreshTimer);let o=Math.max(0,(a-r)*1e3);t.refreshAtSeconds=a,t.refreshTimer=setTimeout(()=>{let t=this.serviceState.get(e);t&&(t.refreshTimer=null,t.refreshAtSeconds=null);let n=this.transferServices.find(t=>t.type===e);n&&this.startStreamForService(n)},o)}emitQuote(e){for(let t of this.subscribers)t(`quote`,e)}emitError(e){for(let t of this.subscribers)t(`error`,e)}};const s=i.z.object({id:i.z.string(),expiresAt:i.z.number().int().nonnegative(),amountOut:i.z.bigint().nonnegative(),amountIn:i.z.bigint().nonnegative(),serviceType:i.z.string()});function c(n){let{sourceAsset:r,sourceChain:i,targetAsset:o,targetChain:s}=n;return i.chainId===s.chainId?t.isNativeAsset(r)&&t.isNativeAsset(o)?!0:r.type===o.type?r.type===e.TokenType.ERC20&&o.type===e.TokenType.ERC20?(0,a.isAddressEqual)(r.address,o.address):r.type===e.TokenType.SPL&&o.type===e.TokenType.SPL?r.address===o.address:!1:!1:!1}function l(e){return s.safeParse(e).success}exports.Quoter=o;
1
+ require(`../_virtual/_rolldown/runtime.cjs`);const e=require(`../constants.cjs`),t=require(`../type-guards.cjs`),n=require(`./_utils.cjs`),r=require(`./constants.cjs`);let i=require(`zod`),a=require(`viem`);var o=class{clock;props;pruneIntervalMs;transferServices;refreshBufferSeconds;quotes=[];subscribers=new Set;started=!1;done=!1;isStartingFromSubscribe=!1;pruneTimerId=null;lastBestId=null;hasReceivedAnyQuote=!1;id=crypto.randomUUID();serviceState=new Map;constructor(e,t,n={}){this.clock=n.clock??(()=>Math.floor(Date.now()/1e3)),this.props=e,this.pruneIntervalMs=n.pruneIntervalMs??1e3,this.transferServices=t,this.refreshBufferSeconds=n.refreshBufferSeconds??5}getQuotes(){let e=this.clock(),t=n.sortQuotes(n.pruneExpiredQuotes({quotes:this.quotes,nowSeconds:e}));return[t[0]??null,t]}subscribe(e){this.subscribers.add(e);let t=!1;if(!this.started){this.isStartingFromSubscribe=!0;try{this.start()}finally{this.isStartingFromSubscribe=!1}}return()=>{if(!t){if(t=!0,this.done){this.subscribers.delete(e);return}if(this.subscribers.size===1&&this.subscribers.has(e)){this.complete(`unsubscribed`);return}this.subscribers.delete(e)}}}start(){if(this.started=!0,this.done=!1,this.quotes=[],this.lastBestId=null,this.hasReceivedAnyQuote=!1,c(this.props)){this.complete(`no-eligible-services`);return}let e=!1;for(let t of this.transferServices)t.analyzeSupport({sourceAsset:this.props.sourceAsset,sourceChainId:this.props.sourceChain.chainId,targetAsset:this.props.targetAsset,targetChainId:this.props.targetChain.chainId})&&(e=!0,this.startStreamForService(t));if(!e){this.complete(`no-eligible-services`);return}this.pruneTimerId=setInterval(()=>this.onPruneTick(),this.pruneIntervalMs)}stop(){this.started=!1,this.pruneTimerId&&=(clearInterval(this.pruneTimerId),null);for(let[e,t]of this.serviceState){try{t.cancel()}catch{}t.refreshTimer&&clearTimeout(t.refreshTimer),t.retryTimer&&clearTimeout(t.retryTimer),this.serviceState.set(e,{cancel:()=>{},done:!1,hasErrored:!1,hasReturnedQuote:!1,retryAttempt:0,retryTimer:null,refreshTimer:null,refreshAtSeconds:null})}}startStreamForService(e){let t=this.serviceState.get(e.type);if(t){try{t.cancel()}catch{}t.refreshTimer&&clearTimeout(t.refreshTimer),t.retryTimer&&clearTimeout(t.retryTimer)}let n=this.makeServiceHandler(e.type),r=[],i=!0,{cancel:a}=e.streamQuotes(this.props,(...e)=>{if(i){r.push(e);return}n(...e)});i=!1,r.length>0&&queueMicrotask(()=>{for(let e of r)n(...e)}),this.serviceState.set(e.type,{cancel:a,done:!1,hasErrored:!1,hasReturnedQuote:!1,retryAttempt:t?.retryAttempt??0,retryTimer:null,refreshTimer:null,refreshAtSeconds:null})}makeServiceHandler(e){return(t,...n)=>{if(this.started){if(t===`quote`){let t=n[0];if(l(t)){if(t.serviceType!==e)return;let n=this.serviceState.get(e);n&&(n.hasReturnedQuote=!0),this.onIncomingQuote(t),this.scheduleServiceRefresh(e)}return}if(t===`done`){this.onServiceDone(e);return}if(t===`error`){let t=this.serviceState.get(e);t&&(t.hasErrored=!0);let r=n[0];r instanceof Error&&this.emitError(r)}}}}onIncomingQuote(e){let t=this.clock();if(n.isQuoteExpired({quote:e,nowSeconds:t}))return;this.hasReceivedAnyQuote=!0,this.resetRetryForService(e.serviceType),this.quotes=n.sortQuotes(n.pruneExpiredQuotes({quotes:n.upsertQuote(this.quotes,e),nowSeconds:t}));let r=this.quotes[0];r&&(this.lastBestId=r.id,this.emitQuote({bestQuote:r,quote:e,quotes:this.quotes}))}resetRetryForService(e){let t=this.serviceState.get(e);t&&(t.retryAttempt=0,t.retryTimer&&=(clearTimeout(t.retryTimer),null))}scheduleServiceRetry(e){let t=this.serviceState.get(e);if(!t)return;t.retryTimer&&=(clearTimeout(t.retryTimer),null);let n=Math.min(r.QUOTER_EMPTY_RETRY_MAX_DELAY_MS,r.QUOTER_EMPTY_RETRY_BASE_DELAY_MS*2**t.retryAttempt);t.retryAttempt+=1,t.retryTimer=setTimeout(()=>{let t=this.serviceState.get(e);if(t&&(t.retryTimer=null),!this.started)return;let n=this.transferServices.find(t=>t.type===e);n&&this.startStreamForService(n)},Math.max(0,n))}onServiceDone(e){let t=this.serviceState.get(e);t&&(t.done=!0,this.scheduleServiceRefresh(e),this.hasReceivedAnyQuote&&!t.hasErrored&&!t.hasReturnedQuote&&this.scheduleServiceRetry(e),this.maybeCompleteNoQuotes())}maybeCompleteNoQuotes(){!this.started||this.done||this.hasReceivedAnyQuote||this.serviceState.size===0||[...this.serviceState.values()].every(e=>e.done)&&this.complete(`no-quotes`)}complete(e){if(this.done)return;let t=this.makeDonePayload(e);if(this.done=!0,this.stop(),this.isStartingFromSubscribe){queueMicrotask(()=>{let e=[...this.subscribers];this.subscribers.clear();for(let n of e)n(`done`,t)});return}let n=[...this.subscribers];this.subscribers.clear();for(let e of n)e(`done`,t)}getInitializedServices(){return[...new Set(this.transferServices.map(e=>e.type))]}getEligibleServices(){return[...this.serviceState.keys()]}makeDonePayload(e){if(e===`unsubscribed`)return{reason:e,data:void 0};let t=this.getInitializedServices();return e===`no-eligible-services`?{reason:e,data:{initializedServices:t,quoterProps:this.props}}:{reason:e,data:{eligibleServices:this.getEligibleServices(),initializedServices:t,quoterProps:this.props}}}onPruneTick(){let e=this.clock(),t=n.pruneExpiredQuotes({quotes:this.quotes,nowSeconds:e});if(t.length!==this.quotes.length){this.quotes=n.sortQuotes(t);let e=this.quotes[0]??null,r=e?e.id:null;e&&r!==this.lastBestId?(this.lastBestId=r,this.emitQuote({bestQuote:e,quote:e,quotes:this.quotes})):e||(this.lastBestId=null)}for(let e of this.transferServices)this.serviceState.has(e.type)&&this.scheduleServiceRefresh(e.type)}scheduleServiceRefresh(e){let t=this.serviceState.get(e);if(!t)return;let r=this.clock(),i=n.earliestExpirationForService({quotes:this.quotes,serviceType:e,nowSeconds:r});if(i===null){t.refreshTimer&&=(clearTimeout(t.refreshTimer),null),t.refreshAtSeconds=null;return}let a=Math.max(0,i-this.refreshBufferSeconds);if(a<=r||t.refreshAtSeconds===a&&t.refreshTimer)return;t.refreshTimer&&clearTimeout(t.refreshTimer);let o=Math.max(0,(a-r)*1e3);t.refreshAtSeconds=a,t.refreshTimer=setTimeout(()=>{let t=this.serviceState.get(e);t&&(t.refreshTimer=null,t.refreshAtSeconds=null);let n=this.transferServices.find(t=>t.type===e);n&&this.startStreamForService(n)},o)}emitQuote(e){for(let t of this.subscribers)t(`quote`,e)}emitError(e){for(let t of this.subscribers)t(`error`,e)}};const s=i.z.object({id:i.z.string(),expiresAt:i.z.number().int().nonnegative(),amountOut:i.z.bigint().nonnegative(),amountIn:i.z.bigint().nonnegative(),serviceType:i.z.string()});function c(n){let{sourceAsset:r,sourceChain:i,targetAsset:o,targetChain:s}=n;return i.chainId===s.chainId?t.isNativeAsset(r)&&t.isNativeAsset(o)?!0:r.type===o.type?r.type===e.TokenType.ERC20&&o.type===e.TokenType.ERC20?(0,a.isAddressEqual)(r.address,o.address):r.type===e.TokenType.SPL&&o.type===e.TokenType.SPL?r.address===o.address:!1:!1:!1}function l(e){return s.safeParse(e).success}exports.Quoter=o;
2
2
  //# sourceMappingURL=quoter.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"quoter.cjs","names":["sortQuotes","pruneExpiredQuotes","isQuoteExpired","upsertQuote","QUOTER_EMPTY_RETRY_MAX_DELAY_MS","QUOTER_EMPTY_RETRY_BASE_DELAY_MS","earliestExpirationForService","z","isNativeAsset","TokenType"],"sources":["../../src/quoter/quoter.ts"],"sourcesContent":["import { z } from 'zod';\nimport type {\n Quote,\n QuoterDonePayload,\n QuoterDoneReason,\n QuoterEventHandler,\n QuoterInterface,\n QuoterProps,\n QuotesTuple,\n ServiceQuoteEventHandler,\n} from '../types/quote';\nimport type { TransferService } from '../types/service';\nimport { earliestExpirationForService, isQuoteExpired, pruneExpiredQuotes, sortQuotes, upsertQuote } from './_utils';\nimport {\n QUOTER_DEFAULT_PRUNE_INTERVAL_MS,\n QUOTER_DEFAULT_REFRESH_BUFFER_SECONDS,\n QUOTER_EMPTY_RETRY_BASE_DELAY_MS,\n QUOTER_EMPTY_RETRY_MAX_DELAY_MS,\n} from './constants';\nimport { ServiceType, TokenType } from '../constants';\nimport { isNativeAsset } from '../type-guards';\nimport { isAddressEqual } from 'viem';\n\n/**\n * Function that returns the current UNIX time in seconds.\n * Injected for deterministic testing.\n */\nexport type Clock = () => number;\n\n/**\n * Options for constructing a Quoter instance.\n *\n * These options tune how often quote state is maintained and when services are\n * proactively restarted before quote expiry.\n */\nexport interface QuoterOptions {\n /** Clock function returning current time in seconds (defaults to Date.now()/1000). */\n readonly clock?: Clock;\n /** Interval for pruning expired quotes (milliseconds).\n *\n * @default 1_000\n */\n readonly pruneIntervalMs?: number;\n /**\n * Amount of seconds to pre-buffer a stream restart before the earliest service quote\n * expiration. Helps ensure continuity before actual expiry is reached.\n *\n * @default 5\n */\n readonly refreshBufferSeconds?: number;\n}\n\n/**\n * Quoter orchestrates quote streaming across multiple transfer services and emits a unified event stream.\n *\n * High-level lifecycle:\n * - Idle until first subscriber.\n * - On first subscribe, starts all eligible services.\n * - Collects and ranks active quotes, pruning expired ones over time.\n * - Completes when explicitly unsubscribed (last subscriber), when no service is eligible,\n * or when all eligible services finish without producing any quote (`no-quotes`).\n *\n * Event model:\n * - Service `quote` -> Quoter upserts quote, recomputes best quote, emits `quote`.\n * - Service `error` -> Quoter emits `error` (non-terminal by itself).\n * - Service `done` -> Quoter marks that service attempt as completed and evaluates retry/complete rules.\n *\n * Retry/refresh behavior:\n * - Refresh: services that have active quotes are restarted shortly before the earliest quote expires.\n * - Retry: services that complete with no quote and no error are only retried after the quoter has\n * already observed at least one quote from any service in this session.\n *\n * This design keeps first-pass \"no quotes anywhere\" terminal and fast, while still allowing services\n * like Markr to be retried in sessions where other providers are returning quotes.\n */\nexport class Quoter implements QuoterInterface {\n private readonly clock: Clock;\n private readonly props: QuoterProps;\n private readonly pruneIntervalMs: number;\n private readonly transferServices: readonly TransferService[];\n private readonly refreshBufferSeconds: number;\n\n private quotes: Quote[] = [];\n private subscribers: Set<QuoterEventHandler> = new Set();\n private started = false;\n private done = false;\n private pruneTimerId: ReturnType<typeof setInterval> | null = null;\n private lastBestId: string | null = null;\n private hasReceivedAnyQuote = false;\n\n public readonly id: string = crypto.randomUUID();\n\n /**\n * Per-service runtime state for the active quote session.\n *\n * This tracks whether a service has completed, errored, produced quotes,\n * and any pending timers used for retry/refresh orchestration.\n */\n private serviceState: Map<\n TransferService['type'],\n {\n cancel: () => void;\n done: boolean;\n hasErrored: boolean;\n hasReturnedQuote: boolean;\n retryAttempt: number;\n retryTimer: ReturnType<typeof setTimeout> | null;\n refreshTimer: ReturnType<typeof setTimeout> | null;\n refreshAtSeconds: number | null;\n }\n > = new Map();\n\n /**\n * Create a new Quoter instance.\n *\n * @param props Quoting request parameters shared across all services.\n * @param transferServices Candidate services; eligibility is resolved at `start()` time.\n * @param options Optional runtime tuning for pruning and refresh behavior.\n */\n constructor(props: QuoterProps, transferServices: readonly TransferService[], options: QuoterOptions = {}) {\n this.clock = options.clock ?? (() => Math.floor(Date.now() / 1_000));\n this.props = props;\n this.pruneIntervalMs = options.pruneIntervalMs ?? QUOTER_DEFAULT_PRUNE_INTERVAL_MS;\n this.transferServices = transferServices;\n this.refreshBufferSeconds = options.refreshBufferSeconds ?? QUOTER_DEFAULT_REFRESH_BUFFER_SECONDS;\n }\n\n /**\n * Get the current best quote and all active quotes (sorted by desirability).\n */\n public getQuotes(): QuotesTuple {\n const now = this.clock();\n const active = pruneExpiredQuotes({ quotes: this.quotes, nowSeconds: now });\n const sorted = sortQuotes(active);\n const best = sorted[0] ?? null;\n\n return [best, sorted];\n }\n\n /**\n * Subscribe for quoter events.\n *\n * First subscriber lazily starts orchestration.\n * Last subscriber triggers terminal completion with reason `unsubscribed`.\n *\n * @returns Unsubscribe function (idempotent).\n */\n public subscribe(handler: QuoterEventHandler): () => void {\n this.subscribers.add(handler);\n let unsubscribed = false;\n\n if (!this.started) {\n this.start();\n }\n\n return () => {\n if (unsubscribed) {\n return;\n }\n unsubscribed = true;\n\n const wasLastSubscriber = this.subscribers.size === 1 && this.subscribers.has(handler);\n if (wasLastSubscriber) {\n this.complete('unsubscribed');\n return;\n }\n\n this.subscribers.delete(handler);\n };\n }\n\n // ----- Internal orchestration -----\n\n /**\n * Start a fresh quote session.\n *\n * Resets session-local state, validates basic request viability,\n * starts streams for eligible services, and begins prune ticks.\n */\n private start(): void {\n this.started = true;\n this.done = false;\n this.quotes = [];\n this.lastBestId = null;\n this.hasReceivedAnyQuote = false;\n\n // Same-chain quotes for the *same* asset are not a valid transfer scenario.\n if (isInvalidSameChainQuoteRequest(this.props)) {\n this.complete('no-eligible-services');\n return;\n }\n\n let hasEligibleService = false;\n\n // Start streams for eligible services\n for (const svc of this.transferServices) {\n const eligible = svc.analyzeSupport({\n sourceAsset: this.props.sourceAsset,\n sourceChainId: this.props.sourceChain.chainId,\n targetAsset: this.props.targetAsset,\n targetChainId: this.props.targetChain.chainId,\n });\n\n if (!eligible) continue;\n hasEligibleService = true;\n\n this.startStreamForService(svc);\n }\n\n if (!hasEligibleService) {\n this.complete('no-eligible-services');\n return;\n }\n\n // Start periodic prune\n this.pruneTimerId = setInterval(() => this.onPruneTick(), this.pruneIntervalMs);\n }\n\n /**\n * Stop all streams and timers.\n * Quotes are retained for snapshot access via getQuotes(), though they\n * can still be pruned due to expiration.\n */\n private stop(): void {\n this.started = false;\n\n if (this.pruneTimerId) {\n clearInterval(this.pruneTimerId);\n this.pruneTimerId = null;\n }\n\n for (const [type, state] of this.serviceState) {\n try {\n state.cancel();\n } catch {\n /* ignore */\n }\n\n if (state.refreshTimer) {\n clearTimeout(state.refreshTimer);\n }\n\n if (state.retryTimer) {\n clearTimeout(state.retryTimer);\n }\n\n this.serviceState.set(type, {\n cancel: () => {},\n done: false,\n hasErrored: false,\n hasReturnedQuote: false,\n retryAttempt: 0,\n retryTimer: null,\n refreshTimer: null,\n refreshAtSeconds: null,\n });\n }\n }\n\n /**\n * Begin or restart streaming for a specific service.\n *\n * Any previous stream/timers for that service are canceled before starting a new attempt.\n */\n private startStreamForService(svc: TransferService): void {\n // Clean up any existing stream and refresh timer\n const existing = this.serviceState.get(svc.type);\n\n if (existing) {\n try {\n existing.cancel();\n } catch {\n /* ignore */\n }\n\n if (existing.refreshTimer) clearTimeout(existing.refreshTimer);\n if (existing.retryTimer) clearTimeout(existing.retryTimer);\n }\n\n const handler = this.makeServiceHandler(svc.type);\n const { cancel } = svc.streamQuotes(this.props, handler);\n\n this.serviceState.set(svc.type, {\n cancel,\n done: false,\n hasErrored: false,\n hasReturnedQuote: false,\n retryAttempt: existing?.retryAttempt ?? 0,\n retryTimer: null,\n refreshTimer: null,\n refreshAtSeconds: null,\n });\n }\n\n /**\n * Create the service-scoped event handler consumed by `TransferService.streamQuotes`.\n *\n * The handler enforces service/quote consistency and translates service events into\n * quoter state transitions.\n */\n private makeServiceHandler(serviceType: TransferService['type']): ServiceQuoteEventHandler {\n return (event, ...args) => {\n // Ignore any service events once stopped to prevent post-stop mutations.\n if (!this.started) {\n return;\n }\n if (event === 'quote') {\n const maybeQuote = args[0];\n if (isQuoteValue(maybeQuote)) {\n // Enforce the quote belongs to the emitting service\n if (maybeQuote.serviceType !== serviceType) {\n // Ignore quotes mismatched to service; defensive\n return;\n }\n const state = this.serviceState.get(serviceType);\n if (state) {\n state.hasReturnedQuote = true;\n }\n this.onIncomingQuote(maybeQuote);\n this.scheduleServiceRefresh(serviceType);\n }\n\n return;\n }\n if (event === 'done') {\n this.onServiceDone(serviceType);\n\n return;\n }\n if (event === 'error') {\n const state = this.serviceState.get(serviceType);\n if (state) {\n state.hasErrored = true;\n }\n const maybeErr = args[0];\n if (maybeErr instanceof Error) {\n this.emitError(maybeErr);\n }\n }\n };\n }\n\n /**\n * Handle an incoming quote event.\n *\n * Expired quotes are ignored. Valid quotes are upserted into active state,\n * then sorted/pruned and emitted to subscribers.\n */\n private onIncomingQuote(quote: Quote): void {\n const now = this.clock();\n\n if (isQuoteExpired({ quote, nowSeconds: now })) {\n return;\n }\n\n this.hasReceivedAnyQuote = true;\n\n this.resetRetryForService(quote.serviceType);\n\n this.quotes = sortQuotes(pruneExpiredQuotes({ quotes: upsertQuote(this.quotes, quote), nowSeconds: now }));\n\n // Always emit on every incoming quote\n const best = this.quotes[0];\n if (best) {\n this.lastBestId = best.id;\n this.emitQuote({ bestQuote: best, quote, quotes: this.quotes });\n }\n }\n\n private resetRetryForService(serviceType: TransferService['type']): void {\n const state = this.serviceState.get(serviceType);\n\n if (!state) {\n return;\n }\n\n state.retryAttempt = 0;\n if (state.retryTimer) {\n clearTimeout(state.retryTimer);\n state.retryTimer = null;\n }\n }\n\n /**\n * Schedule a retry attempt for a service using exponential backoff.\n *\n * This is used only for quote-less successful completions once the overall session\n * has already produced at least one quote from some service.\n */\n private scheduleServiceRetry(serviceType: TransferService['type']): void {\n const state = this.serviceState.get(serviceType);\n\n if (!state) {\n return;\n }\n\n if (state.retryTimer) {\n clearTimeout(state.retryTimer);\n state.retryTimer = null;\n }\n\n const delayMs = Math.min(\n QUOTER_EMPTY_RETRY_MAX_DELAY_MS,\n QUOTER_EMPTY_RETRY_BASE_DELAY_MS * 2 ** state.retryAttempt,\n );\n state.retryAttempt += 1;\n\n state.retryTimer = setTimeout(\n () => {\n const current = this.serviceState.get(serviceType);\n if (current) {\n current.retryTimer = null;\n }\n\n if (!this.started) {\n return;\n }\n\n const svc = this.transferServices.find((s) => s.type === serviceType);\n if (svc) {\n this.startStreamForService(svc);\n }\n },\n Math.max(0, delayMs),\n );\n }\n\n /**\n * Handle service completion (`done`).\n *\n * A completion may schedule:\n * - refresh (if the service has active quotes), and/or\n * - retry (quote-less/non-error completion, but only after any quote has existed in session).\n *\n * Then evaluates whether the full quoter can complete with `no-quotes`.\n */\n private onServiceDone(serviceType: TransferService['type']): void {\n const state = this.serviceState.get(serviceType);\n\n if (!state) {\n return;\n }\n\n state.done = true;\n this.scheduleServiceRefresh(serviceType);\n\n if (this.hasReceivedAnyQuote && !state.hasErrored && !state.hasReturnedQuote) {\n this.scheduleServiceRetry(serviceType);\n }\n\n this.maybeCompleteNoQuotes();\n }\n\n /**\n * Complete with `no-quotes` when every eligible service has finished and\n * no quote was ever observed during this session.\n */\n private maybeCompleteNoQuotes(): void {\n if (!this.started || this.done || this.hasReceivedAnyQuote || this.serviceState.size === 0) {\n return;\n }\n\n const allServicesDone = [...this.serviceState.values()].every((state) => state.done);\n\n if (allServicesDone) {\n this.complete('no-quotes');\n }\n }\n\n /**\n * Finalize the quoter session and broadcast terminal reason exactly once.\n */\n private complete(reason: QuoterDoneReason): void {\n if (this.done) {\n return;\n }\n\n const payload = this.makeDonePayload(reason);\n\n this.done = true;\n this.stop();\n\n const handlers = [...this.subscribers];\n this.subscribers.clear();\n\n for (const handler of handlers) {\n handler('done', payload);\n }\n }\n\n private getInitializedServices(): ServiceType[] {\n return [...new Set(this.transferServices.map((service) => service.type))];\n }\n\n private getEligibleServices(): ServiceType[] {\n return [...this.serviceState.keys()];\n }\n\n private makeDonePayload(reason: QuoterDoneReason): QuoterDonePayload {\n if (reason === 'unsubscribed') {\n return { reason, data: undefined };\n }\n\n const initializedServices = this.getInitializedServices();\n\n if (reason === 'no-eligible-services') {\n return {\n reason,\n data: {\n initializedServices,\n quoterProps: this.props,\n },\n };\n }\n\n return {\n reason,\n data: {\n eligibleServices: this.getEligibleServices(),\n initializedServices,\n quoterProps: this.props,\n },\n };\n }\n\n /** Periodic prune tick to evict expired quotes and emit best changes. */\n private onPruneTick(): void {\n const now = this.clock();\n const pruned = pruneExpiredQuotes({ quotes: this.quotes, nowSeconds: now });\n\n if (pruned.length !== this.quotes.length) {\n this.quotes = sortQuotes(pruned);\n const best = this.quotes[0] ?? null;\n const bestId = best ? best.id : null;\n\n if (best && bestId !== this.lastBestId) {\n this.lastBestId = bestId;\n this.emitQuote({ bestQuote: best, quote: best, quotes: this.quotes });\n } else if (!best) {\n this.lastBestId = null;\n }\n }\n\n // Re-evaluate refresh scheduling for all services on prune tick\n for (const svc of this.transferServices) {\n if (this.serviceState.has(svc.type)) {\n this.scheduleServiceRefresh(svc.type);\n }\n }\n }\n\n /** Schedule a refresh (restart) of streaming for the service at its earliest quote expiration. */\n private scheduleServiceRefresh(serviceType: TransferService['type']): void {\n const state = this.serviceState.get(serviceType);\n\n if (!state) return;\n\n const now = this.clock();\n const earliest = earliestExpirationForService({ quotes: this.quotes, serviceType, nowSeconds: now });\n\n if (earliest === null) {\n // No quotes -> clear any pending refresh\n if (state.refreshTimer) {\n clearTimeout(state.refreshTimer);\n state.refreshTimer = null;\n }\n state.refreshAtSeconds = null;\n return;\n }\n\n // Only reschedule if earlier than currently scheduled or no timer\n const targetAt = Math.max(0, earliest - this.refreshBufferSeconds);\n\n // If target time has already passed, do not schedule another refresh here.\n if (targetAt <= now) {\n return;\n }\n\n // Reschedule only when the desired refresh time changes (earlier OR later).\n // We key refresh off the soonest-expiring active quote for this service.\n if (state.refreshAtSeconds === targetAt && state.refreshTimer) {\n return;\n }\n\n if (state.refreshTimer) {\n clearTimeout(state.refreshTimer);\n }\n\n const delayMs = Math.max(0, (targetAt - now) * 1_000);\n state.refreshAtSeconds = targetAt;\n state.refreshTimer = setTimeout(() => {\n // Clear timer/marker before restarting to avoid stale state if restart fails.\n const current = this.serviceState.get(serviceType);\n if (current) {\n current.refreshTimer = null;\n current.refreshAtSeconds = null;\n }\n\n // Restart stream and clear timer/marker\n const svc = this.transferServices.find((s) => s.type === serviceType);\n\n if (svc) {\n this.startStreamForService(svc);\n }\n }, delayMs);\n }\n\n // ----- Emission helpers -----\n /** Emit a quote event to all subscribers. */\n private emitQuote(payload: { bestQuote: Quote; quote: Quote; quotes: readonly Quote[] }): void {\n for (const handler of this.subscribers) {\n handler('quote', payload);\n }\n }\n\n /** Emit an error event to all subscribers. */\n private emitError(error: Error): void {\n for (const handler of this.subscribers) {\n handler('error', error);\n }\n }\n}\n\n/**\n * Zod schema to validate the minimal Quote fields used for runtime gating.\n *\n * This is a simple schema, and is not a fully safe runtime validate for Quote.\n * That isn't necessary here since we only need to verify basic structure before\n * using the quote in internal logic.\n *\n * @internal\n */\nconst QuoteSchema = z.object({\n id: z.string(),\n expiresAt: z.number().int().nonnegative(),\n amountOut: z.bigint().nonnegative(),\n amountIn: z.bigint().nonnegative(),\n serviceType: z.string(),\n});\n\nfunction isInvalidSameChainQuoteRequest(props: QuoterProps): boolean {\n const { sourceAsset, sourceChain, targetAsset, targetChain } = props;\n\n if (sourceChain.chainId !== targetChain.chainId) {\n return false;\n }\n\n // Native -> native on the same chain is a no-op.\n if (isNativeAsset(sourceAsset) && isNativeAsset(targetAsset)) {\n return true;\n }\n\n // If token types differ, it's potentially a swap on the same chain.\n if (sourceAsset.type !== targetAsset.type) {\n return false;\n }\n\n // Same token type and address on the same chain is a no-op.\n if (sourceAsset.type === TokenType.ERC20 && targetAsset.type === TokenType.ERC20) {\n return isAddressEqual(sourceAsset.address, targetAsset.address);\n }\n\n if (sourceAsset.type === TokenType.SPL && targetAsset.type === TokenType.SPL) {\n return sourceAsset.address === targetAsset.address;\n }\n\n return false;\n}\n\nfunction isQuoteValue(value: unknown): value is Quote {\n const result = QuoteSchema.safe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F,KAAK,sBAAsB,EAAI,EAEhC,EAAQ,CAKb,UAAkB,EAA6E,CAC7F,IAAK,IAAM,KAAW,KAAK,YACzB,EAAQ,QAAS,EAAQ,CAK7B,UAAkB,EAAoB,CACpC,IAAK,IAAM,KAAW,KAAK,YACzB,EAAQ,QAAS,EAAM,GAc7B,MAAM,EAAcC,EAAAA,EAAE,OAAO,CAC3B,GAAIA,EAAAA,EAAE,QAAQ,CACd,UAAWA,EAAAA,EAAE,QAAQ,CAAC,KAAK,CAAC,aAAa,CACzC,UAAWA,EAAAA,EAAE,QAAQ,CAAC,aAAa,CACnC,SAAUA,EAAAA,EAAE,QAAQ,CAAC,aAAa,CAClC,YAAaA,EAAAA,EAAE,QAAQ,CACxB,CAAC,CAEF,SAAS,EAA+B,EAA6B,CACnE,GAAM,CAAE,cAAa,cAAa,cAAa,eAAgB,EAyB/D,OAvBI,EAAY,UAAY,EAAY,QAKpCC,EAAAA,cAAc,EAAY,EAAIA,EAAAA,cAAc,EAAY,CACnD,GAIL,EAAY,OAAS,EAAY,KAKjC,EAAY,OAASC,EAAAA,UAAU,OAAS,EAAY,OAASA,EAAAA,UAAU,OACzE,EAAA,EAAA,gBAAsB,EAAY,QAAS,EAAY,QAAQ,CAG7D,EAAY,OAASA,EAAAA,UAAU,KAAO,EAAY,OAASA,EAAAA,UAAU,IAChE,EAAY,UAAY,EAAY,QAGtC,GAZE,GAVA,GAyBX,SAAS,EAAa,EAAgC,CAGpD,OAFe,EAAY,UAAU,EAAM,CAE7B"}
1
+ {"version":3,"file":"quoter.cjs","names":["sortQuotes","pruneExpiredQuotes","isQuoteExpired","upsertQuote","QUOTER_EMPTY_RETRY_MAX_DELAY_MS","QUOTER_EMPTY_RETRY_BASE_DELAY_MS","earliestExpirationForService","z","isNativeAsset","TokenType"],"sources":["../../src/quoter/quoter.ts"],"sourcesContent":["import { z } from 'zod';\nimport type {\n Quote,\n QuoterDonePayload,\n QuoterDoneReason,\n QuoterEventHandler,\n QuoterInterface,\n QuoterProps,\n ServiceQuoteEventArgs,\n QuotesTuple,\n ServiceQuoteEventHandler,\n} from '../types/quote';\nimport type { TransferService } from '../types/service';\nimport { earliestExpirationForService, isQuoteExpired, pruneExpiredQuotes, sortQuotes, upsertQuote } from './_utils';\nimport {\n QUOTER_DEFAULT_PRUNE_INTERVAL_MS,\n QUOTER_DEFAULT_REFRESH_BUFFER_SECONDS,\n QUOTER_EMPTY_RETRY_BASE_DELAY_MS,\n QUOTER_EMPTY_RETRY_MAX_DELAY_MS,\n} from './constants';\nimport { ServiceType, TokenType } from '../constants';\nimport { isNativeAsset } from '../type-guards';\nimport { isAddressEqual } from 'viem';\n\n/**\n * Function that returns the current UNIX time in seconds.\n * Injected for deterministic testing.\n */\nexport type Clock = () => number;\n\n/**\n * Options for constructing a Quoter instance.\n *\n * These options tune how often quote state is maintained and when services are\n * proactively restarted before quote expiry.\n */\nexport interface QuoterOptions {\n /** Clock function returning current time in seconds (defaults to Date.now()/1000). */\n readonly clock?: Clock;\n /** Interval for pruning expired quotes (milliseconds).\n *\n * @default 1_000\n */\n readonly pruneIntervalMs?: number;\n /**\n * Amount of seconds to pre-buffer a stream restart before the earliest service quote\n * expiration. Helps ensure continuity before actual expiry is reached.\n *\n * @default 5\n */\n readonly refreshBufferSeconds?: number;\n}\n\n/**\n * Quoter orchestrates quote streaming across multiple transfer services and emits a unified event stream.\n *\n * High-level lifecycle:\n * - Idle until first subscriber.\n * - On first subscribe, starts all eligible services.\n * - Collects and ranks active quotes, pruning expired ones over time.\n * - Completes when explicitly unsubscribed (last subscriber), when no service is eligible,\n * or when all eligible services finish without producing any quote (`no-quotes`).\n *\n * Event model:\n * - Service `quote` -> Quoter upserts quote, recomputes best quote, emits `quote`.\n * - Service `error` -> Quoter emits `error` (non-terminal by itself).\n * - Service `done` -> Quoter marks that service attempt as completed and evaluates retry/complete rules.\n *\n * Retry/refresh behavior:\n * - Refresh: services that have active quotes are restarted shortly before the earliest quote expires.\n * - Retry: services that complete with no quote and no error are only retried after the quoter has\n * already observed at least one quote from any service in this session.\n *\n * This design keeps first-pass \"no quotes anywhere\" terminal and fast, while still allowing services\n * like Markr to be retried in sessions where other providers are returning quotes.\n */\nexport class Quoter implements QuoterInterface {\n private readonly clock: Clock;\n private readonly props: QuoterProps;\n private readonly pruneIntervalMs: number;\n private readonly transferServices: readonly TransferService[];\n private readonly refreshBufferSeconds: number;\n\n private quotes: Quote[] = [];\n private subscribers: Set<QuoterEventHandler> = new Set();\n private started = false;\n private done = false;\n private isStartingFromSubscribe = false;\n private pruneTimerId: ReturnType<typeof setInterval> | null = null;\n private lastBestId: string | null = null;\n private hasReceivedAnyQuote = false;\n\n public readonly id: string = crypto.randomUUID();\n\n /**\n * Per-service runtime state for the active quote session.\n *\n * This tracks whether a service has completed, errored, produced quotes,\n * and any pending timers used for retry/refresh orchestration.\n */\n private serviceState: Map<\n TransferService['type'],\n {\n cancel: () => void;\n done: boolean;\n hasErrored: boolean;\n hasReturnedQuote: boolean;\n retryAttempt: number;\n retryTimer: ReturnType<typeof setTimeout> | null;\n refreshTimer: ReturnType<typeof setTimeout> | null;\n refreshAtSeconds: number | null;\n }\n > = new Map();\n\n /**\n * Create a new Quoter instance.\n *\n * @param props Quoting request parameters shared across all services.\n * @param transferServices Candidate services; eligibility is resolved at `start()` time.\n * @param options Optional runtime tuning for pruning and refresh behavior.\n */\n constructor(props: QuoterProps, transferServices: readonly TransferService[], options: QuoterOptions = {}) {\n this.clock = options.clock ?? (() => Math.floor(Date.now() / 1_000));\n this.props = props;\n this.pruneIntervalMs = options.pruneIntervalMs ?? QUOTER_DEFAULT_PRUNE_INTERVAL_MS;\n this.transferServices = transferServices;\n this.refreshBufferSeconds = options.refreshBufferSeconds ?? QUOTER_DEFAULT_REFRESH_BUFFER_SECONDS;\n }\n\n /**\n * Get the current best quote and all active quotes (sorted by desirability).\n */\n public getQuotes(): QuotesTuple {\n const now = this.clock();\n const active = pruneExpiredQuotes({ quotes: this.quotes, nowSeconds: now });\n const sorted = sortQuotes(active);\n const best = sorted[0] ?? null;\n\n return [best, sorted];\n }\n\n /**\n * Subscribe for quoter events.\n *\n * First subscriber lazily starts orchestration.\n * Last subscriber triggers terminal completion with reason `unsubscribed`.\n *\n * @returns Unsubscribe function (idempotent).\n */\n public subscribe(handler: QuoterEventHandler): () => void {\n this.subscribers.add(handler);\n let unsubscribed = false;\n\n if (!this.started) {\n this.isStartingFromSubscribe = true;\n try {\n this.start();\n } finally {\n this.isStartingFromSubscribe = false;\n }\n }\n\n return () => {\n if (unsubscribed) {\n return;\n }\n unsubscribed = true;\n\n if (this.done) {\n this.subscribers.delete(handler);\n return;\n }\n\n const wasLastSubscriber = this.subscribers.size === 1 && this.subscribers.has(handler);\n if (wasLastSubscriber) {\n this.complete('unsubscribed');\n return;\n }\n\n this.subscribers.delete(handler);\n };\n }\n\n // ----- Internal orchestration -----\n\n /**\n * Start a fresh quote session.\n *\n * Resets session-local state, validates basic request viability,\n * starts streams for eligible services, and begins prune ticks.\n */\n private start(): void {\n this.started = true;\n this.done = false;\n this.quotes = [];\n this.lastBestId = null;\n this.hasReceivedAnyQuote = false;\n\n // Same-chain quotes for the *same* asset are not a valid transfer scenario.\n if (isInvalidSameChainQuoteRequest(this.props)) {\n this.complete('no-eligible-services');\n return;\n }\n\n let hasEligibleService = false;\n\n // Start streams for eligible services\n for (const svc of this.transferServices) {\n const eligible = svc.analyzeSupport({\n sourceAsset: this.props.sourceAsset,\n sourceChainId: this.props.sourceChain.chainId,\n targetAsset: this.props.targetAsset,\n targetChainId: this.props.targetChain.chainId,\n });\n\n if (!eligible) continue;\n hasEligibleService = true;\n\n this.startStreamForService(svc);\n }\n\n if (!hasEligibleService) {\n this.complete('no-eligible-services');\n return;\n }\n\n // Start periodic prune\n this.pruneTimerId = setInterval(() => this.onPruneTick(), this.pruneIntervalMs);\n }\n\n /**\n * Stop all streams and timers.\n * Quotes are retained for snapshot access via getQuotes(), though they\n * can still be pruned due to expiration.\n */\n private stop(): void {\n this.started = false;\n\n if (this.pruneTimerId) {\n clearInterval(this.pruneTimerId);\n this.pruneTimerId = null;\n }\n\n for (const [type, state] of this.serviceState) {\n try {\n state.cancel();\n } catch {\n /* ignore */\n }\n\n if (state.refreshTimer) {\n clearTimeout(state.refreshTimer);\n }\n\n if (state.retryTimer) {\n clearTimeout(state.retryTimer);\n }\n\n this.serviceState.set(type, {\n cancel: () => {},\n done: false,\n hasErrored: false,\n hasReturnedQuote: false,\n retryAttempt: 0,\n retryTimer: null,\n refreshTimer: null,\n refreshAtSeconds: null,\n });\n }\n }\n\n /**\n * Begin or restart streaming for a specific service.\n *\n * Any previous stream/timers for that service are canceled before starting a new attempt.\n */\n private startStreamForService(svc: TransferService): void {\n // Clean up any existing stream and refresh timer\n const existing = this.serviceState.get(svc.type);\n\n if (existing) {\n try {\n existing.cancel();\n } catch {\n /* ignore */\n }\n\n if (existing.refreshTimer) clearTimeout(existing.refreshTimer);\n if (existing.retryTimer) clearTimeout(existing.retryTimer);\n }\n\n const handler = this.makeServiceHandler(svc.type);\n const bufferedEvents: ServiceQuoteEventArgs[] = [];\n let isStartingStream = true;\n\n // Some services emit quote/done synchronously inside streamQuotes().\n // If those events run before subscribe() returns, consumers can hit TDZ\n // when they reference the returned unsubscribe function in their callback.\n // Buffer startup emissions and flush them after stream initialization.\n const guardedHandler: ServiceQuoteEventHandler = (...eventArgs) => {\n if (isStartingStream) {\n bufferedEvents.push(eventArgs);\n return;\n }\n\n handler(...eventArgs);\n };\n\n const { cancel } = svc.streamQuotes(this.props, guardedHandler);\n isStartingStream = false;\n\n if (bufferedEvents.length > 0) {\n // Use a microtask so subscribe() can return before the first callback fires.\n // This preserves event order while avoiding re-entrancy during subscription.\n queueMicrotask(() => {\n for (const bufferedEvent of bufferedEvents) {\n handler(...bufferedEvent);\n }\n });\n }\n\n this.serviceState.set(svc.type, {\n cancel,\n done: false,\n hasErrored: false,\n hasReturnedQuote: false,\n retryAttempt: existing?.retryAttempt ?? 0,\n retryTimer: null,\n refreshTimer: null,\n refreshAtSeconds: null,\n });\n }\n\n /**\n * Create the service-scoped event handler consumed by `TransferService.streamQuotes`.\n *\n * The handler enforces service/quote consistency and translates service events into\n * quoter state transitions.\n */\n private makeServiceHandler(serviceType: TransferService['type']): ServiceQuoteEventHandler {\n return (event, ...args) => {\n // Ignore any service events once stopped to prevent post-stop mutations.\n if (!this.started) {\n return;\n }\n if (event === 'quote') {\n const maybeQuote = args[0];\n if (isQuoteValue(maybeQuote)) {\n // Enforce the quote belongs to the emitting service\n if (maybeQuote.serviceType !== serviceType) {\n // Ignore quotes mismatched to service; defensive\n return;\n }\n const state = this.serviceState.get(serviceType);\n if (state) {\n state.hasReturnedQuote = true;\n }\n this.onIncomingQuote(maybeQuote);\n this.scheduleServiceRefresh(serviceType);\n }\n\n return;\n }\n if (event === 'done') {\n this.onServiceDone(serviceType);\n\n return;\n }\n if (event === 'error') {\n const state = this.serviceState.get(serviceType);\n if (state) {\n state.hasErrored = true;\n }\n const maybeErr = args[0];\n if (maybeErr instanceof Error) {\n this.emitError(maybeErr);\n }\n }\n };\n }\n\n /**\n * Handle an incoming quote event.\n *\n * Expired quotes are ignored. Valid quotes are upserted into active state,\n * then sorted/pruned and emitted to subscribers.\n */\n private onIncomingQuote(quote: Quote): void {\n const now = this.clock();\n\n if (isQuoteExpired({ quote, nowSeconds: now })) {\n return;\n }\n\n this.hasReceivedAnyQuote = true;\n\n this.resetRetryForService(quote.serviceType);\n\n this.quotes = sortQuotes(pruneExpiredQuotes({ quotes: upsertQuote(this.quotes, quote), nowSeconds: now }));\n\n // Always emit on every incoming quote\n const best = this.quotes[0];\n if (best) {\n this.lastBestId = best.id;\n this.emitQuote({ bestQuote: best, quote, quotes: this.quotes });\n }\n }\n\n private resetRetryForService(serviceType: TransferService['type']): void {\n const state = this.serviceState.get(serviceType);\n\n if (!state) {\n return;\n }\n\n state.retryAttempt = 0;\n if (state.retryTimer) {\n clearTimeout(state.retryTimer);\n state.retryTimer = null;\n }\n }\n\n /**\n * Schedule a retry attempt for a service using exponential backoff.\n *\n * This is used only for quote-less successful completions once the overall session\n * has already produced at least one quote from some service.\n */\n private scheduleServiceRetry(serviceType: TransferService['type']): void {\n const state = this.serviceState.get(serviceType);\n\n if (!state) {\n return;\n }\n\n if (state.retryTimer) {\n clearTimeout(state.retryTimer);\n state.retryTimer = null;\n }\n\n const delayMs = Math.min(\n QUOTER_EMPTY_RETRY_MAX_DELAY_MS,\n QUOTER_EMPTY_RETRY_BASE_DELAY_MS * 2 ** state.retryAttempt,\n );\n state.retryAttempt += 1;\n\n state.retryTimer = setTimeout(\n () => {\n const current = this.serviceState.get(serviceType);\n if (current) {\n current.retryTimer = null;\n }\n\n if (!this.started) {\n return;\n }\n\n const svc = this.transferServices.find((s) => s.type === serviceType);\n if (svc) {\n this.startStreamForService(svc);\n }\n },\n Math.max(0, delayMs),\n );\n }\n\n /**\n * Handle service completion (`done`).\n *\n * A completion may schedule:\n * - refresh (if the service has active quotes), and/or\n * - retry (quote-less/non-error completion, but only after any quote has existed in session).\n *\n * Then evaluates whether the full quoter can complete with `no-quotes`.\n */\n private onServiceDone(serviceType: TransferService['type']): void {\n const state = this.serviceState.get(serviceType);\n\n if (!state) {\n return;\n }\n\n state.done = true;\n this.scheduleServiceRefresh(serviceType);\n\n if (this.hasReceivedAnyQuote && !state.hasErrored && !state.hasReturnedQuote) {\n this.scheduleServiceRetry(serviceType);\n }\n\n this.maybeCompleteNoQuotes();\n }\n\n /**\n * Complete with `no-quotes` when every eligible service has finished and\n * no quote was ever observed during this session.\n */\n private maybeCompleteNoQuotes(): void {\n if (!this.started || this.done || this.hasReceivedAnyQuote || this.serviceState.size === 0) {\n return;\n }\n\n const allServicesDone = [...this.serviceState.values()].every((state) => state.done);\n\n if (allServicesDone) {\n this.complete('no-quotes');\n }\n }\n\n /**\n * Finalize the quoter session and broadcast terminal reason exactly once.\n */\n private complete(reason: QuoterDoneReason): void {\n if (this.done) {\n return;\n }\n\n const payload = this.makeDonePayload(reason);\n\n this.done = true;\n this.stop();\n\n // When complete() happens during subscribe()->start(), emit done on a microtask\n // so callers can safely reference the returned unsubscribe in their handler.\n if (this.isStartingFromSubscribe) {\n queueMicrotask(() => {\n const handlers = [...this.subscribers];\n this.subscribers.clear();\n\n for (const handler of handlers) {\n handler('done', payload);\n }\n });\n return;\n }\n\n const handlers = [...this.subscribers];\n this.subscribers.clear();\n\n for (const handler of handlers) {\n handler('done', payload);\n }\n }\n\n private getInitializedServices(): ServiceType[] {\n return [...new Set(this.transferServices.map((service) => service.type))];\n }\n\n private getEligibleServices(): ServiceType[] {\n return [...this.serviceState.keys()];\n }\n\n private makeDonePayload(reason: QuoterDoneReason): QuoterDonePayload {\n if (reason === 'unsubscribed') {\n return { reason, data: undefined };\n }\n\n const initializedServices = this.getInitializedServices();\n\n if (reason === 'no-eligible-services') {\n return {\n reason,\n data: {\n initializedServices,\n quoterProps: this.props,\n },\n };\n }\n\n return {\n reason,\n data: {\n eligibleServices: this.getEligibleServices(),\n initializedServices,\n quoterProps: this.props,\n },\n };\n }\n\n /** Periodic prune tick to evict expired quotes and emit best changes. */\n private onPruneTick(): void {\n const now = this.clock();\n const pruned = pruneExpiredQuotes({ quotes: this.quotes, nowSeconds: now });\n\n if (pruned.length !== this.quotes.length) {\n this.quotes = sortQuotes(pruned);\n const best = this.quotes[0] ?? null;\n const bestId = best ? best.id : null;\n\n if (best && bestId !== this.lastBestId) {\n this.lastBestId = bestId;\n this.emitQuote({ bestQuote: best, quote: best, quotes: this.quotes });\n } else if (!best) {\n this.lastBestId = null;\n }\n }\n\n // Re-evaluate refresh scheduling for all services on prune tick\n for (const svc of this.transferServices) {\n if (this.serviceState.has(svc.type)) {\n this.scheduleServiceRefresh(svc.type);\n }\n }\n }\n\n /** Schedule a refresh (restart) of streaming for the service at its earliest quote expiration. */\n private scheduleServiceRefresh(serviceType: TransferService['type']): void {\n const state = this.serviceState.get(serviceType);\n\n if (!state) return;\n\n const now = this.clock();\n const earliest = earliestExpirationForService({ quotes: this.quotes, serviceType, nowSeconds: now });\n\n if (earliest === null) {\n // No quotes -> clear any pending refresh\n if (state.refreshTimer) {\n clearTimeout(state.refreshTimer);\n state.refreshTimer = null;\n }\n state.refreshAtSeconds = null;\n return;\n }\n\n // Only reschedule if earlier than currently scheduled or no timer\n const targetAt = Math.max(0, earliest - this.refreshBufferSeconds);\n\n // If target time has already passed, do not schedule another refresh here.\n if (targetAt <= now) {\n return;\n }\n\n // Reschedule only when the desired refresh time changes (earlier OR later).\n // We key refresh off the soonest-expiring active quote for this service.\n if (state.refreshAtSeconds === targetAt && state.refreshTimer) {\n return;\n }\n\n if (state.refreshTimer) {\n clearTimeout(state.refreshTimer);\n }\n\n const delayMs = Math.max(0, (targetAt - now) * 1_000);\n state.refreshAtSeconds = targetAt;\n state.refreshTimer = setTimeout(() => {\n // Clear timer/marker before restarting to avoid stale state if restart fails.\n const current = this.serviceState.get(serviceType);\n if (current) {\n current.refreshTimer = null;\n current.refreshAtSeconds = null;\n }\n\n // Restart stream and clear timer/marker\n const svc = this.transferServices.find((s) => s.type === serviceType);\n\n if (svc) {\n this.startStreamForService(svc);\n }\n }, delayMs);\n }\n\n // ----- Emission helpers -----\n /** Emit a quote event to all subscribers. */\n private emitQuote(payload: { bestQuote: Quote; quote: Quote; quotes: readonly Quote[] }): void {\n for (const handler of this.subscribers) {\n handler('quote', payload);\n }\n }\n\n /** Emit an error event to all subscribers. */\n private emitError(error: Error): void {\n for (const handler of this.subscribers) {\n handler('error', error);\n }\n }\n}\n\n/**\n * Zod schema to validate the minimal Quote fields used for runtime gating.\n *\n * This is a simple schema, and is not a fully safe runtime validate for Quote.\n * That isn't necessary here since we only need to verify basic structure before\n * using the quote in internal logic.\n *\n * @internal\n */\nconst QuoteSchema = z.object({\n id: z.string(),\n expiresAt: z.number().int().nonnegative(),\n amountOut: z.bigint().nonnegative(),\n amountIn: z.bigint().nonnegative(),\n serviceType: z.string(),\n});\n\nfunction isInvalidSameChainQuoteRequest(props: QuoterProps): boolean {\n const { sourceAsset, sourceChain, targetAsset, targetChain } = props;\n\n if (sourceChain.chainId !== targetChain.chainId) {\n return false;\n }\n\n // Native -> native on the same chain is a no-op.\n if (isNativeAsset(sourceAsset) && isNativeAsset(targetAsset)) {\n return true;\n }\n\n // If token types differ, it's potentially a swap on the same chain.\n if (sourceAsset.type !== targetAsset.type) {\n return false;\n }\n\n // Same token type and address on the same chain is a no-op.\n if (sourceAsset.type === TokenType.ERC20 && targetAsset.type === TokenType.ERC20) {\n return isAddressEqual(sourceAsset.address, targetAsset.address);\n }\n\n if (sourceAsset.type === TokenType.SPL && targetAsset.type === TokenType.SPL) {\n return sourceAsset.address === targetAsset.address;\n }\n\n return false;\n}\n\nfunction isQuoteValue(value: unknown): value is Quote {\n const result = QuoteSchema.safeParse(value);\n\n return result.success;\n}\n"],"mappings":"+MA4EA,IAAa,EAAb,KAA+C,CAC7C,MACA,MACA,gBACA,iBACA,qBAEA,OAA0B,EAAE,CAC5B,YAA+C,IAAI,IACnD,QAAkB,GAClB,KAAe,GACf,wBAAkC,GAClC,aAA8D,KAC9D,WAAoC,KACpC,oBAA8B,GAE9B,GAA6B,OAAO,YAAY,CAQhD,aAYI,IAAI,IASR,YAAY,EAAoB,EAA8C,EAAyB,EAAE,CAAE,CACzG,KAAK,MAAQ,EAAQ,YAAgB,KAAK,MAAM,KAAK,KAAK,CAAG,IAAM,EACnE,KAAK,MAAQ,EACb,KAAK,gBAAkB,EAAQ,iBAAA,IAC/B,KAAK,iBAAmB,EACxB,KAAK,qBAAuB,EAAQ,sBAAA,EAMtC,WAAgC,CAC9B,IAAM,EAAM,KAAK,OAAO,CAElB,EAASA,EAAAA,WADAC,EAAAA,mBAAmB,CAAE,OAAQ,KAAK,OAAQ,WAAY,EAAK,CAAC,CAC1C,CAGjC,MAAO,CAFM,EAAO,IAAM,KAEZ,EAAO,CAWvB,UAAiB,EAAyC,CACxD,KAAK,YAAY,IAAI,EAAQ,CAC7B,IAAI,EAAe,GAEnB,GA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Ac7B,MAAM,EAAcC,EAAAA,EAAE,OAAO,CAC3B,GAAIA,EAAAA,EAAE,QAAQ,CACd,UAAWA,EAAAA,EAAE,QAAQ,CAAC,KAAK,CAAC,aAAa,CACzC,UAAWA,EAAAA,EAAE,QAAQ,CAAC,aAAa,CACnC,SAAUA,EAAAA,EAAE,QAAQ,CAAC,aAAa,CAClC,YAAaA,EAAAA,EAAE,QAAQ,CACxB,CAAC,CAEF,SAAS,EAA+B,EAA6B,CACnE,GAAM,CAAE,cAAa,cAAa,cAAa,eAAgB,EAyB/D,OAvBI,EAAY,UAAY,EAAY,QAKpCC,EAAAA,cAAc,EAAY,EAAIA,EAAAA,cAAc,EAAY,CACnD,GAIL,EAAY,OAAS,EAAY,KAKjC,EAAY,OAASC,EAAAA,UAAU,OAAS,EAAY,OAASA,EAAAA,UAAU,OACzE,EAAA,EAAA,gBAAsB,EAAY,QAAS,EAAY,QAAQ,CAG7D,EAAY,OAASA,EAAAA,UAAU,KAAO,EAAY,OAASA,EAAAA,UAAU,IAChE,EAAY,UAAY,EAAY,QAGtC,GAZE,GAVA,GAyBX,SAAS,EAAa,EAAgC,CAGpD,OAFe,EAAY,UAAU,EAAM,CAE7B"}
@@ -1,2 +1,2 @@
1
- import{TokenType as e}from"../constants.js";import{isNativeAsset as t}from"../type-guards.js";import{earliestExpirationForService as n,isQuoteExpired as r,pruneExpiredQuotes as i,sortQuotes as a,upsertQuote as o}from"./_utils.js";import{QUOTER_EMPTY_RETRY_BASE_DELAY_MS as s,QUOTER_EMPTY_RETRY_MAX_DELAY_MS as c}from"./constants.js";import{z as l}from"zod";import{isAddressEqual as u}from"viem";var d=class{clock;props;pruneIntervalMs;transferServices;refreshBufferSeconds;quotes=[];subscribers=new Set;started=!1;done=!1;pruneTimerId=null;lastBestId=null;hasReceivedAnyQuote=!1;id=crypto.randomUUID();serviceState=new Map;constructor(e,t,n={}){this.clock=n.clock??(()=>Math.floor(Date.now()/1e3)),this.props=e,this.pruneIntervalMs=n.pruneIntervalMs??1e3,this.transferServices=t,this.refreshBufferSeconds=n.refreshBufferSeconds??5}getQuotes(){let e=this.clock(),t=a(i({quotes:this.quotes,nowSeconds:e}));return[t[0]??null,t]}subscribe(e){this.subscribers.add(e);let t=!1;return this.started||this.start(),()=>{if(!t){if(t=!0,this.subscribers.size===1&&this.subscribers.has(e)){this.complete(`unsubscribed`);return}this.subscribers.delete(e)}}}start(){if(this.started=!0,this.done=!1,this.quotes=[],this.lastBestId=null,this.hasReceivedAnyQuote=!1,p(this.props)){this.complete(`no-eligible-services`);return}let e=!1;for(let t of this.transferServices)t.analyzeSupport({sourceAsset:this.props.sourceAsset,sourceChainId:this.props.sourceChain.chainId,targetAsset:this.props.targetAsset,targetChainId:this.props.targetChain.chainId})&&(e=!0,this.startStreamForService(t));if(!e){this.complete(`no-eligible-services`);return}this.pruneTimerId=setInterval(()=>this.onPruneTick(),this.pruneIntervalMs)}stop(){this.started=!1,this.pruneTimerId&&=(clearInterval(this.pruneTimerId),null);for(let[e,t]of this.serviceState){try{t.cancel()}catch{}t.refreshTimer&&clearTimeout(t.refreshTimer),t.retryTimer&&clearTimeout(t.retryTimer),this.serviceState.set(e,{cancel:()=>{},done:!1,hasErrored:!1,hasReturnedQuote:!1,retryAttempt:0,retryTimer:null,refreshTimer:null,refreshAtSeconds:null})}}startStreamForService(e){let t=this.serviceState.get(e.type);if(t){try{t.cancel()}catch{}t.refreshTimer&&clearTimeout(t.refreshTimer),t.retryTimer&&clearTimeout(t.retryTimer)}let n=this.makeServiceHandler(e.type),{cancel:r}=e.streamQuotes(this.props,n);this.serviceState.set(e.type,{cancel:r,done:!1,hasErrored:!1,hasReturnedQuote:!1,retryAttempt:t?.retryAttempt??0,retryTimer:null,refreshTimer:null,refreshAtSeconds:null})}makeServiceHandler(e){return(t,...n)=>{if(this.started){if(t===`quote`){let t=n[0];if(m(t)){if(t.serviceType!==e)return;let n=this.serviceState.get(e);n&&(n.hasReturnedQuote=!0),this.onIncomingQuote(t),this.scheduleServiceRefresh(e)}return}if(t===`done`){this.onServiceDone(e);return}if(t===`error`){let t=this.serviceState.get(e);t&&(t.hasErrored=!0);let r=n[0];r instanceof Error&&this.emitError(r)}}}}onIncomingQuote(e){let t=this.clock();if(r({quote:e,nowSeconds:t}))return;this.hasReceivedAnyQuote=!0,this.resetRetryForService(e.serviceType),this.quotes=a(i({quotes:o(this.quotes,e),nowSeconds:t}));let n=this.quotes[0];n&&(this.lastBestId=n.id,this.emitQuote({bestQuote:n,quote:e,quotes:this.quotes}))}resetRetryForService(e){let t=this.serviceState.get(e);t&&(t.retryAttempt=0,t.retryTimer&&=(clearTimeout(t.retryTimer),null))}scheduleServiceRetry(e){let t=this.serviceState.get(e);if(!t)return;t.retryTimer&&=(clearTimeout(t.retryTimer),null);let n=Math.min(c,s*2**t.retryAttempt);t.retryAttempt+=1,t.retryTimer=setTimeout(()=>{let t=this.serviceState.get(e);if(t&&(t.retryTimer=null),!this.started)return;let n=this.transferServices.find(t=>t.type===e);n&&this.startStreamForService(n)},Math.max(0,n))}onServiceDone(e){let t=this.serviceState.get(e);t&&(t.done=!0,this.scheduleServiceRefresh(e),this.hasReceivedAnyQuote&&!t.hasErrored&&!t.hasReturnedQuote&&this.scheduleServiceRetry(e),this.maybeCompleteNoQuotes())}maybeCompleteNoQuotes(){!this.started||this.done||this.hasReceivedAnyQuote||this.serviceState.size===0||[...this.serviceState.values()].every(e=>e.done)&&this.complete(`no-quotes`)}complete(e){if(this.done)return;let t=this.makeDonePayload(e);this.done=!0,this.stop();let n=[...this.subscribers];this.subscribers.clear();for(let e of n)e(`done`,t)}getInitializedServices(){return[...new Set(this.transferServices.map(e=>e.type))]}getEligibleServices(){return[...this.serviceState.keys()]}makeDonePayload(e){if(e===`unsubscribed`)return{reason:e,data:void 0};let t=this.getInitializedServices();return e===`no-eligible-services`?{reason:e,data:{initializedServices:t,quoterProps:this.props}}:{reason:e,data:{eligibleServices:this.getEligibleServices(),initializedServices:t,quoterProps:this.props}}}onPruneTick(){let e=this.clock(),t=i({quotes:this.quotes,nowSeconds:e});if(t.length!==this.quotes.length){this.quotes=a(t);let e=this.quotes[0]??null,n=e?e.id:null;e&&n!==this.lastBestId?(this.lastBestId=n,this.emitQuote({bestQuote:e,quote:e,quotes:this.quotes})):e||(this.lastBestId=null)}for(let e of this.transferServices)this.serviceState.has(e.type)&&this.scheduleServiceRefresh(e.type)}scheduleServiceRefresh(e){let t=this.serviceState.get(e);if(!t)return;let r=this.clock(),i=n({quotes:this.quotes,serviceType:e,nowSeconds:r});if(i===null){t.refreshTimer&&=(clearTimeout(t.refreshTimer),null),t.refreshAtSeconds=null;return}let a=Math.max(0,i-this.refreshBufferSeconds);if(a<=r||t.refreshAtSeconds===a&&t.refreshTimer)return;t.refreshTimer&&clearTimeout(t.refreshTimer);let o=Math.max(0,(a-r)*1e3);t.refreshAtSeconds=a,t.refreshTimer=setTimeout(()=>{let t=this.serviceState.get(e);t&&(t.refreshTimer=null,t.refreshAtSeconds=null);let n=this.transferServices.find(t=>t.type===e);n&&this.startStreamForService(n)},o)}emitQuote(e){for(let t of this.subscribers)t(`quote`,e)}emitError(e){for(let t of this.subscribers)t(`error`,e)}};const f=l.object({id:l.string(),expiresAt:l.number().int().nonnegative(),amountOut:l.bigint().nonnegative(),amountIn:l.bigint().nonnegative(),serviceType:l.string()});function p(n){let{sourceAsset:r,sourceChain:i,targetAsset:a,targetChain:o}=n;return i.chainId===o.chainId?t(r)&&t(a)?!0:r.type===a.type?r.type===e.ERC20&&a.type===e.ERC20?u(r.address,a.address):r.type===e.SPL&&a.type===e.SPL?r.address===a.address:!1:!1:!1}function m(e){return f.safeParse(e).success}export{d as Quoter};
1
+ import{TokenType as e}from"../constants.js";import{isNativeAsset as t}from"../type-guards.js";import{earliestExpirationForService as n,isQuoteExpired as r,pruneExpiredQuotes as i,sortQuotes as a,upsertQuote as o}from"./_utils.js";import{QUOTER_EMPTY_RETRY_BASE_DELAY_MS as s,QUOTER_EMPTY_RETRY_MAX_DELAY_MS as c}from"./constants.js";import{z as l}from"zod";import{isAddressEqual as u}from"viem";var d=class{clock;props;pruneIntervalMs;transferServices;refreshBufferSeconds;quotes=[];subscribers=new Set;started=!1;done=!1;isStartingFromSubscribe=!1;pruneTimerId=null;lastBestId=null;hasReceivedAnyQuote=!1;id=crypto.randomUUID();serviceState=new Map;constructor(e,t,n={}){this.clock=n.clock??(()=>Math.floor(Date.now()/1e3)),this.props=e,this.pruneIntervalMs=n.pruneIntervalMs??1e3,this.transferServices=t,this.refreshBufferSeconds=n.refreshBufferSeconds??5}getQuotes(){let e=this.clock(),t=a(i({quotes:this.quotes,nowSeconds:e}));return[t[0]??null,t]}subscribe(e){this.subscribers.add(e);let t=!1;if(!this.started){this.isStartingFromSubscribe=!0;try{this.start()}finally{this.isStartingFromSubscribe=!1}}return()=>{if(!t){if(t=!0,this.done){this.subscribers.delete(e);return}if(this.subscribers.size===1&&this.subscribers.has(e)){this.complete(`unsubscribed`);return}this.subscribers.delete(e)}}}start(){if(this.started=!0,this.done=!1,this.quotes=[],this.lastBestId=null,this.hasReceivedAnyQuote=!1,p(this.props)){this.complete(`no-eligible-services`);return}let e=!1;for(let t of this.transferServices)t.analyzeSupport({sourceAsset:this.props.sourceAsset,sourceChainId:this.props.sourceChain.chainId,targetAsset:this.props.targetAsset,targetChainId:this.props.targetChain.chainId})&&(e=!0,this.startStreamForService(t));if(!e){this.complete(`no-eligible-services`);return}this.pruneTimerId=setInterval(()=>this.onPruneTick(),this.pruneIntervalMs)}stop(){this.started=!1,this.pruneTimerId&&=(clearInterval(this.pruneTimerId),null);for(let[e,t]of this.serviceState){try{t.cancel()}catch{}t.refreshTimer&&clearTimeout(t.refreshTimer),t.retryTimer&&clearTimeout(t.retryTimer),this.serviceState.set(e,{cancel:()=>{},done:!1,hasErrored:!1,hasReturnedQuote:!1,retryAttempt:0,retryTimer:null,refreshTimer:null,refreshAtSeconds:null})}}startStreamForService(e){let t=this.serviceState.get(e.type);if(t){try{t.cancel()}catch{}t.refreshTimer&&clearTimeout(t.refreshTimer),t.retryTimer&&clearTimeout(t.retryTimer)}let n=this.makeServiceHandler(e.type),r=[],i=!0,{cancel:a}=e.streamQuotes(this.props,(...e)=>{if(i){r.push(e);return}n(...e)});i=!1,r.length>0&&queueMicrotask(()=>{for(let e of r)n(...e)}),this.serviceState.set(e.type,{cancel:a,done:!1,hasErrored:!1,hasReturnedQuote:!1,retryAttempt:t?.retryAttempt??0,retryTimer:null,refreshTimer:null,refreshAtSeconds:null})}makeServiceHandler(e){return(t,...n)=>{if(this.started){if(t===`quote`){let t=n[0];if(m(t)){if(t.serviceType!==e)return;let n=this.serviceState.get(e);n&&(n.hasReturnedQuote=!0),this.onIncomingQuote(t),this.scheduleServiceRefresh(e)}return}if(t===`done`){this.onServiceDone(e);return}if(t===`error`){let t=this.serviceState.get(e);t&&(t.hasErrored=!0);let r=n[0];r instanceof Error&&this.emitError(r)}}}}onIncomingQuote(e){let t=this.clock();if(r({quote:e,nowSeconds:t}))return;this.hasReceivedAnyQuote=!0,this.resetRetryForService(e.serviceType),this.quotes=a(i({quotes:o(this.quotes,e),nowSeconds:t}));let n=this.quotes[0];n&&(this.lastBestId=n.id,this.emitQuote({bestQuote:n,quote:e,quotes:this.quotes}))}resetRetryForService(e){let t=this.serviceState.get(e);t&&(t.retryAttempt=0,t.retryTimer&&=(clearTimeout(t.retryTimer),null))}scheduleServiceRetry(e){let t=this.serviceState.get(e);if(!t)return;t.retryTimer&&=(clearTimeout(t.retryTimer),null);let n=Math.min(c,s*2**t.retryAttempt);t.retryAttempt+=1,t.retryTimer=setTimeout(()=>{let t=this.serviceState.get(e);if(t&&(t.retryTimer=null),!this.started)return;let n=this.transferServices.find(t=>t.type===e);n&&this.startStreamForService(n)},Math.max(0,n))}onServiceDone(e){let t=this.serviceState.get(e);t&&(t.done=!0,this.scheduleServiceRefresh(e),this.hasReceivedAnyQuote&&!t.hasErrored&&!t.hasReturnedQuote&&this.scheduleServiceRetry(e),this.maybeCompleteNoQuotes())}maybeCompleteNoQuotes(){!this.started||this.done||this.hasReceivedAnyQuote||this.serviceState.size===0||[...this.serviceState.values()].every(e=>e.done)&&this.complete(`no-quotes`)}complete(e){if(this.done)return;let t=this.makeDonePayload(e);if(this.done=!0,this.stop(),this.isStartingFromSubscribe){queueMicrotask(()=>{let e=[...this.subscribers];this.subscribers.clear();for(let n of e)n(`done`,t)});return}let n=[...this.subscribers];this.subscribers.clear();for(let e of n)e(`done`,t)}getInitializedServices(){return[...new Set(this.transferServices.map(e=>e.type))]}getEligibleServices(){return[...this.serviceState.keys()]}makeDonePayload(e){if(e===`unsubscribed`)return{reason:e,data:void 0};let t=this.getInitializedServices();return e===`no-eligible-services`?{reason:e,data:{initializedServices:t,quoterProps:this.props}}:{reason:e,data:{eligibleServices:this.getEligibleServices(),initializedServices:t,quoterProps:this.props}}}onPruneTick(){let e=this.clock(),t=i({quotes:this.quotes,nowSeconds:e});if(t.length!==this.quotes.length){this.quotes=a(t);let e=this.quotes[0]??null,n=e?e.id:null;e&&n!==this.lastBestId?(this.lastBestId=n,this.emitQuote({bestQuote:e,quote:e,quotes:this.quotes})):e||(this.lastBestId=null)}for(let e of this.transferServices)this.serviceState.has(e.type)&&this.scheduleServiceRefresh(e.type)}scheduleServiceRefresh(e){let t=this.serviceState.get(e);if(!t)return;let r=this.clock(),i=n({quotes:this.quotes,serviceType:e,nowSeconds:r});if(i===null){t.refreshTimer&&=(clearTimeout(t.refreshTimer),null),t.refreshAtSeconds=null;return}let a=Math.max(0,i-this.refreshBufferSeconds);if(a<=r||t.refreshAtSeconds===a&&t.refreshTimer)return;t.refreshTimer&&clearTimeout(t.refreshTimer);let o=Math.max(0,(a-r)*1e3);t.refreshAtSeconds=a,t.refreshTimer=setTimeout(()=>{let t=this.serviceState.get(e);t&&(t.refreshTimer=null,t.refreshAtSeconds=null);let n=this.transferServices.find(t=>t.type===e);n&&this.startStreamForService(n)},o)}emitQuote(e){for(let t of this.subscribers)t(`quote`,e)}emitError(e){for(let t of this.subscribers)t(`error`,e)}};const f=l.object({id:l.string(),expiresAt:l.number().int().nonnegative(),amountOut:l.bigint().nonnegative(),amountIn:l.bigint().nonnegative(),serviceType:l.string()});function p(n){let{sourceAsset:r,sourceChain:i,targetAsset:a,targetChain:o}=n;return i.chainId===o.chainId?t(r)&&t(a)?!0:r.type===a.type?r.type===e.ERC20&&a.type===e.ERC20?u(r.address,a.address):r.type===e.SPL&&a.type===e.SPL?r.address===a.address:!1:!1:!1}function m(e){return f.safeParse(e).success}export{d as Quoter};
2
2
  //# sourceMappingURL=quoter.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"quoter.js","names":[],"sources":["../../src/quoter/quoter.ts"],"sourcesContent":["import { z } from 'zod';\nimport type {\n Quote,\n QuoterDonePayload,\n QuoterDoneReason,\n QuoterEventHandler,\n QuoterInterface,\n QuoterProps,\n QuotesTuple,\n ServiceQuoteEventHandler,\n} from '../types/quote';\nimport type { TransferService } from '../types/service';\nimport { earliestExpirationForService, isQuoteExpired, pruneExpiredQuotes, sortQuotes, upsertQuote } from './_utils';\nimport {\n QUOTER_DEFAULT_PRUNE_INTERVAL_MS,\n QUOTER_DEFAULT_REFRESH_BUFFER_SECONDS,\n QUOTER_EMPTY_RETRY_BASE_DELAY_MS,\n QUOTER_EMPTY_RETRY_MAX_DELAY_MS,\n} from './constants';\nimport { ServiceType, TokenType } from '../constants';\nimport { isNativeAsset } from '../type-guards';\nimport { isAddressEqual } from 'viem';\n\n/**\n * Function that returns the current UNIX time in seconds.\n * Injected for deterministic testing.\n */\nexport type Clock = () => number;\n\n/**\n * Options for constructing a Quoter instance.\n *\n * These options tune how often quote state is maintained and when services are\n * proactively restarted before quote expiry.\n */\nexport interface QuoterOptions {\n /** Clock function returning current time in seconds (defaults to Date.now()/1000). */\n readonly clock?: Clock;\n /** Interval for pruning expired quotes (milliseconds).\n *\n * @default 1_000\n */\n readonly pruneIntervalMs?: number;\n /**\n * Amount of seconds to pre-buffer a stream restart before the earliest service quote\n * expiration. Helps ensure continuity before actual expiry is reached.\n *\n * @default 5\n */\n readonly refreshBufferSeconds?: number;\n}\n\n/**\n * Quoter orchestrates quote streaming across multiple transfer services and emits a unified event stream.\n *\n * High-level lifecycle:\n * - Idle until first subscriber.\n * - On first subscribe, starts all eligible services.\n * - Collects and ranks active quotes, pruning expired ones over time.\n * - Completes when explicitly unsubscribed (last subscriber), when no service is eligible,\n * or when all eligible services finish without producing any quote (`no-quotes`).\n *\n * Event model:\n * - Service `quote` -> Quoter upserts quote, recomputes best quote, emits `quote`.\n * - Service `error` -> Quoter emits `error` (non-terminal by itself).\n * - Service `done` -> Quoter marks that service attempt as completed and evaluates retry/complete rules.\n *\n * Retry/refresh behavior:\n * - Refresh: services that have active quotes are restarted shortly before the earliest quote expires.\n * - Retry: services that complete with no quote and no error are only retried after the quoter has\n * already observed at least one quote from any service in this session.\n *\n * This design keeps first-pass \"no quotes anywhere\" terminal and fast, while still allowing services\n * like Markr to be retried in sessions where other providers are returning quotes.\n */\nexport class Quoter implements QuoterInterface {\n private readonly clock: Clock;\n private readonly props: QuoterProps;\n private readonly pruneIntervalMs: number;\n private readonly transferServices: readonly TransferService[];\n private readonly refreshBufferSeconds: number;\n\n private quotes: Quote[] = [];\n private subscribers: Set<QuoterEventHandler> = new Set();\n private started = false;\n private done = false;\n private pruneTimerId: ReturnType<typeof setInterval> | null = null;\n private lastBestId: string | null = null;\n private hasReceivedAnyQuote = false;\n\n public readonly id: string = crypto.randomUUID();\n\n /**\n * Per-service runtime state for the active quote session.\n *\n * This tracks whether a service has completed, errored, produced quotes,\n * and any pending timers used for retry/refresh orchestration.\n */\n private serviceState: Map<\n TransferService['type'],\n {\n cancel: () => void;\n done: boolean;\n hasErrored: boolean;\n hasReturnedQuote: boolean;\n retryAttempt: number;\n retryTimer: ReturnType<typeof setTimeout> | null;\n refreshTimer: ReturnType<typeof setTimeout> | null;\n refreshAtSeconds: number | null;\n }\n > = new Map();\n\n /**\n * Create a new Quoter instance.\n *\n * @param props Quoting request parameters shared across all services.\n * @param transferServices Candidate services; eligibility is resolved at `start()` time.\n * @param options Optional runtime tuning for pruning and refresh behavior.\n */\n constructor(props: QuoterProps, transferServices: readonly TransferService[], options: QuoterOptions = {}) {\n this.clock = options.clock ?? (() => Math.floor(Date.now() / 1_000));\n this.props = props;\n this.pruneIntervalMs = options.pruneIntervalMs ?? QUOTER_DEFAULT_PRUNE_INTERVAL_MS;\n this.transferServices = transferServices;\n this.refreshBufferSeconds = options.refreshBufferSeconds ?? QUOTER_DEFAULT_REFRESH_BUFFER_SECONDS;\n }\n\n /**\n * Get the current best quote and all active quotes (sorted by desirability).\n */\n public getQuotes(): QuotesTuple {\n const now = this.clock();\n const active = pruneExpiredQuotes({ quotes: this.quotes, nowSeconds: now });\n const sorted = sortQuotes(active);\n const best = sorted[0] ?? null;\n\n return [best, sorted];\n }\n\n /**\n * Subscribe for quoter events.\n *\n * First subscriber lazily starts orchestration.\n * Last subscriber triggers terminal completion with reason `unsubscribed`.\n *\n * @returns Unsubscribe function (idempotent).\n */\n public subscribe(handler: QuoterEventHandler): () => void {\n this.subscribers.add(handler);\n let unsubscribed = false;\n\n if (!this.started) {\n this.start();\n }\n\n return () => {\n if (unsubscribed) {\n return;\n }\n unsubscribed = true;\n\n const wasLastSubscriber = this.subscribers.size === 1 && this.subscribers.has(handler);\n if (wasLastSubscriber) {\n this.complete('unsubscribed');\n return;\n }\n\n this.subscribers.delete(handler);\n };\n }\n\n // ----- Internal orchestration -----\n\n /**\n * Start a fresh quote session.\n *\n * Resets session-local state, validates basic request viability,\n * starts streams for eligible services, and begins prune ticks.\n */\n private start(): void {\n this.started = true;\n this.done = false;\n this.quotes = [];\n this.lastBestId = null;\n this.hasReceivedAnyQuote = false;\n\n // Same-chain quotes for the *same* asset are not a valid transfer scenario.\n if (isInvalidSameChainQuoteRequest(this.props)) {\n this.complete('no-eligible-services');\n return;\n }\n\n let hasEligibleService = false;\n\n // Start streams for eligible services\n for (const svc of this.transferServices) {\n const eligible = svc.analyzeSupport({\n sourceAsset: this.props.sourceAsset,\n sourceChainId: this.props.sourceChain.chainId,\n targetAsset: this.props.targetAsset,\n targetChainId: this.props.targetChain.chainId,\n });\n\n if (!eligible) continue;\n hasEligibleService = true;\n\n this.startStreamForService(svc);\n }\n\n if (!hasEligibleService) {\n this.complete('no-eligible-services');\n return;\n }\n\n // Start periodic prune\n this.pruneTimerId = setInterval(() => this.onPruneTick(), this.pruneIntervalMs);\n }\n\n /**\n * Stop all streams and timers.\n * Quotes are retained for snapshot access via getQuotes(), though they\n * can still be pruned due to expiration.\n */\n private stop(): void {\n this.started = false;\n\n if (this.pruneTimerId) {\n clearInterval(this.pruneTimerId);\n this.pruneTimerId = null;\n }\n\n for (const [type, state] of this.serviceState) {\n try {\n state.cancel();\n } catch {\n /* ignore */\n }\n\n if (state.refreshTimer) {\n clearTimeout(state.refreshTimer);\n }\n\n if (state.retryTimer) {\n clearTimeout(state.retryTimer);\n }\n\n this.serviceState.set(type, {\n cancel: () => {},\n done: false,\n hasErrored: false,\n hasReturnedQuote: false,\n retryAttempt: 0,\n retryTimer: null,\n refreshTimer: null,\n refreshAtSeconds: null,\n });\n }\n }\n\n /**\n * Begin or restart streaming for a specific service.\n *\n * Any previous stream/timers for that service are canceled before starting a new attempt.\n */\n private startStreamForService(svc: TransferService): void {\n // Clean up any existing stream and refresh timer\n const existing = this.serviceState.get(svc.type);\n\n if (existing) {\n try {\n existing.cancel();\n } catch {\n /* ignore */\n }\n\n if (existing.refreshTimer) clearTimeout(existing.refreshTimer);\n if (existing.retryTimer) clearTimeout(existing.retryTimer);\n }\n\n const handler = this.makeServiceHandler(svc.type);\n const { cancel } = svc.streamQuotes(this.props, handler);\n\n this.serviceState.set(svc.type, {\n cancel,\n done: false,\n hasErrored: false,\n hasReturnedQuote: false,\n retryAttempt: existing?.retryAttempt ?? 0,\n retryTimer: null,\n refreshTimer: null,\n refreshAtSeconds: null,\n });\n }\n\n /**\n * Create the service-scoped event handler consumed by `TransferService.streamQuotes`.\n *\n * The handler enforces service/quote consistency and translates service events into\n * quoter state transitions.\n */\n private makeServiceHandler(serviceType: TransferService['type']): ServiceQuoteEventHandler {\n return (event, ...args) => {\n // Ignore any service events once stopped to prevent post-stop mutations.\n if (!this.started) {\n return;\n }\n if (event === 'quote') {\n const maybeQuote = args[0];\n if (isQuoteValue(maybeQuote)) {\n // Enforce the quote belongs to the emitting service\n if (maybeQuote.serviceType !== serviceType) {\n // Ignore quotes mismatched to service; defensive\n return;\n }\n const state = this.serviceState.get(serviceType);\n if (state) {\n state.hasReturnedQuote = true;\n }\n this.onIncomingQuote(maybeQuote);\n this.scheduleServiceRefresh(serviceType);\n }\n\n return;\n }\n if (event === 'done') {\n this.onServiceDone(serviceType);\n\n return;\n }\n if (event === 'error') {\n const state = this.serviceState.get(serviceType);\n if (state) {\n state.hasErrored = true;\n }\n const maybeErr = args[0];\n if (maybeErr instanceof Error) {\n this.emitError(maybeErr);\n }\n }\n };\n }\n\n /**\n * Handle an incoming quote event.\n *\n * Expired quotes are ignored. Valid quotes are upserted into active state,\n * then sorted/pruned and emitted to subscribers.\n */\n private onIncomingQuote(quote: Quote): void {\n const now = this.clock();\n\n if (isQuoteExpired({ quote, nowSeconds: now })) {\n return;\n }\n\n this.hasReceivedAnyQuote = true;\n\n this.resetRetryForService(quote.serviceType);\n\n this.quotes = sortQuotes(pruneExpiredQuotes({ quotes: upsertQuote(this.quotes, quote), nowSeconds: now }));\n\n // Always emit on every incoming quote\n const best = this.quotes[0];\n if (best) {\n this.lastBestId = best.id;\n this.emitQuote({ bestQuote: best, quote, quotes: this.quotes });\n }\n }\n\n private resetRetryForService(serviceType: TransferService['type']): void {\n const state = this.serviceState.get(serviceType);\n\n if (!state) {\n return;\n }\n\n state.retryAttempt = 0;\n if (state.retryTimer) {\n clearTimeout(state.retryTimer);\n state.retryTimer = null;\n }\n }\n\n /**\n * Schedule a retry attempt for a service using exponential backoff.\n *\n * This is used only for quote-less successful completions once the overall session\n * has already produced at least one quote from some service.\n */\n private scheduleServiceRetry(serviceType: TransferService['type']): void {\n const state = this.serviceState.get(serviceType);\n\n if (!state) {\n return;\n }\n\n if (state.retryTimer) {\n clearTimeout(state.retryTimer);\n state.retryTimer = null;\n }\n\n const delayMs = Math.min(\n QUOTER_EMPTY_RETRY_MAX_DELAY_MS,\n QUOTER_EMPTY_RETRY_BASE_DELAY_MS * 2 ** state.retryAttempt,\n );\n state.retryAttempt += 1;\n\n state.retryTimer = setTimeout(\n () => {\n const current = this.serviceState.get(serviceType);\n if (current) {\n current.retryTimer = null;\n }\n\n if (!this.started) {\n return;\n }\n\n const svc = this.transferServices.find((s) => s.type === serviceType);\n if (svc) {\n this.startStreamForService(svc);\n }\n },\n Math.max(0, delayMs),\n );\n }\n\n /**\n * Handle service completion (`done`).\n *\n * A completion may schedule:\n * - refresh (if the service has active quotes), and/or\n * - retry (quote-less/non-error completion, but only after any quote has existed in session).\n *\n * Then evaluates whether the full quoter can complete with `no-quotes`.\n */\n private onServiceDone(serviceType: TransferService['type']): void {\n const state = this.serviceState.get(serviceType);\n\n if (!state) {\n return;\n }\n\n state.done = true;\n this.scheduleServiceRefresh(serviceType);\n\n if (this.hasReceivedAnyQuote && !state.hasErrored && !state.hasReturnedQuote) {\n this.scheduleServiceRetry(serviceType);\n }\n\n this.maybeCompleteNoQuotes();\n }\n\n /**\n * Complete with `no-quotes` when every eligible service has finished and\n * no quote was ever observed during this session.\n */\n private maybeCompleteNoQuotes(): void {\n if (!this.started || this.done || this.hasReceivedAnyQuote || this.serviceState.size === 0) {\n return;\n }\n\n const allServicesDone = [...this.serviceState.values()].every((state) => state.done);\n\n if (allServicesDone) {\n this.complete('no-quotes');\n }\n }\n\n /**\n * Finalize the quoter session and broadcast terminal reason exactly once.\n */\n private complete(reason: QuoterDoneReason): void {\n if (this.done) {\n return;\n }\n\n const payload = this.makeDonePayload(reason);\n\n this.done = true;\n this.stop();\n\n const handlers = [...this.subscribers];\n this.subscribers.clear();\n\n for (const handler of handlers) {\n handler('done', payload);\n }\n }\n\n private getInitializedServices(): ServiceType[] {\n return [...new Set(this.transferServices.map((service) => service.type))];\n }\n\n private getEligibleServices(): ServiceType[] {\n return [...this.serviceState.keys()];\n }\n\n private makeDonePayload(reason: QuoterDoneReason): QuoterDonePayload {\n if (reason === 'unsubscribed') {\n return { reason, data: undefined };\n }\n\n const initializedServices = this.getInitializedServices();\n\n if (reason === 'no-eligible-services') {\n return {\n reason,\n data: {\n initializedServices,\n quoterProps: this.props,\n },\n };\n }\n\n return {\n reason,\n data: {\n eligibleServices: this.getEligibleServices(),\n initializedServices,\n quoterProps: this.props,\n },\n };\n }\n\n /** Periodic prune tick to evict expired quotes and emit best changes. */\n private onPruneTick(): void {\n const now = this.clock();\n const pruned = pruneExpiredQuotes({ quotes: this.quotes, nowSeconds: now });\n\n if (pruned.length !== this.quotes.length) {\n this.quotes = sortQuotes(pruned);\n const best = this.quotes[0] ?? null;\n const bestId = best ? best.id : null;\n\n if (best && bestId !== this.lastBestId) {\n this.lastBestId = bestId;\n this.emitQuote({ bestQuote: best, quote: best, quotes: this.quotes });\n } else if (!best) {\n this.lastBestId = null;\n }\n }\n\n // Re-evaluate refresh scheduling for all services on prune tick\n for (const svc of this.transferServices) {\n if (this.serviceState.has(svc.type)) {\n this.scheduleServiceRefresh(svc.type);\n }\n }\n }\n\n /** Schedule a refresh (restart) of streaming for the service at its earliest quote expiration. */\n private scheduleServiceRefresh(serviceType: TransferService['type']): void {\n const state = this.serviceState.get(serviceType);\n\n if (!state) return;\n\n const now = this.clock();\n const earliest = earliestExpirationForService({ quotes: this.quotes, serviceType, nowSeconds: now });\n\n if (earliest === null) {\n // No quotes -> clear any pending refresh\n if (state.refreshTimer) {\n clearTimeout(state.refreshTimer);\n state.refreshTimer = null;\n }\n state.refreshAtSeconds = null;\n return;\n }\n\n // Only reschedule if earlier than currently scheduled or no timer\n const targetAt = Math.max(0, earliest - this.refreshBufferSeconds);\n\n // If target time has already passed, do not schedule another refresh here.\n if (targetAt <= now) {\n return;\n }\n\n // Reschedule only when the desired refresh time changes (earlier OR later).\n // We key refresh off the soonest-expiring active quote for this service.\n if (state.refreshAtSeconds === targetAt && state.refreshTimer) {\n return;\n }\n\n if (state.refreshTimer) {\n clearTimeout(state.refreshTimer);\n }\n\n const delayMs = Math.max(0, (targetAt - now) * 1_000);\n state.refreshAtSeconds = targetAt;\n state.refreshTimer = setTimeout(() => {\n // Clear timer/marker before restarting to avoid stale state if restart fails.\n const current = this.serviceState.get(serviceType);\n if (current) {\n current.refreshTimer = null;\n current.refreshAtSeconds = null;\n }\n\n // Restart stream and clear timer/marker\n const svc = this.transferServices.find((s) => s.type === serviceType);\n\n if (svc) {\n this.startStreamForService(svc);\n }\n }, delayMs);\n }\n\n // ----- Emission helpers -----\n /** Emit a quote event to all subscribers. */\n private emitQuote(payload: { bestQuote: Quote; quote: Quote; quotes: readonly Quote[] }): void {\n for (const handler of this.subscribers) {\n handler('quote', payload);\n }\n }\n\n /** Emit an error event to all subscribers. */\n private emitError(error: Error): void {\n for (const handler of this.subscribers) {\n handler('error', error);\n }\n }\n}\n\n/**\n * Zod schema to validate the minimal Quote fields used for runtime gating.\n *\n * This is a simple schema, and is not a fully safe runtime validate for Quote.\n * That isn't necessary here since we only need to verify basic structure before\n * using the quote in internal logic.\n *\n * @internal\n */\nconst QuoteSchema = z.object({\n id: z.string(),\n expiresAt: z.number().int().nonnegative(),\n amountOut: z.bigint().nonnegative(),\n amountIn: z.bigint().nonnegative(),\n serviceType: z.string(),\n});\n\nfunction isInvalidSameChainQuoteRequest(props: QuoterProps): boolean {\n const { sourceAsset, sourceChain, targetAsset, targetChain } = props;\n\n if (sourceChain.chainId !== targetChain.chainId) {\n return false;\n }\n\n // Native -> native on the same chain is a no-op.\n if (isNativeAsset(sourceAsset) && isNativeAsset(targetAsset)) {\n return true;\n }\n\n // If token types differ, it's potentially a swap on the same chain.\n if (sourceAsset.type !== targetAsset.type) {\n return false;\n }\n\n // Same token type and address on the same chain is a no-op.\n if (sourceAsset.type === TokenType.ERC20 && targetAsset.type === TokenType.ERC20) {\n return isAddressEqual(sourceAsset.address, targetAsset.address);\n }\n\n if (sourceAsset.type === TokenType.SPL && targetAsset.type === TokenType.SPL) {\n return sourceAsset.address === targetAsset.address;\n }\n\n return false;\n}\n\nfunction isQuoteValue(value: unknown): value is Quote {\n const result = QuoteSchema.safeParse(value);\n\n return result.success;\n}\n"],"mappings":"2YA2EA,IAAa,EAAb,KAA+C,CAC7C,MACA,MACA,gBACA,iBACA,qBAEA,OAA0B,EAAE,CAC5B,YAA+C,IAAI,IACnD,QAAkB,GAClB,KAAe,GACf,aAA8D,KAC9D,WAAoC,KACpC,oBAA8B,GAE9B,GAA6B,OAAO,YAAY,CAQhD,aAYI,IAAI,IASR,YAAY,EAAoB,EAA8C,EAAyB,EAAE,CAAE,CACzG,KAAK,MAAQ,EAAQ,YAAgB,KAAK,MAAM,KAAK,KAAK,CAAG,IAAM,EACnE,KAAK,MAAQ,EACb,KAAK,gBAAkB,EAAQ,iBAAA,IAC/B,KAAK,iBAAmB,EACxB,KAAK,qBAAuB,EAAQ,sBAAA,EAMtC,WAAgC,CAC9B,IAAM,EAAM,KAAK,OAAO,CAElB,EAAS,EADA,EAAmB,CAAE,OAAQ,KAAK,OAAQ,WAAY,EAAK,CAAC,CAC1C,CAGjC,MAAO,CAFM,EAAO,IAAM,KAEZ,EAAO,CAWvB,UAAiB,EAAyC,CACxD,KAAK,YAAY,IAAI,EAAQ,CAC7B,IAAI,EAAe,GAMnB,OAJK,KAAK,SACR,KAAK,OAAO,KAGD,CACP,MAMJ,IAHA,EAAe,GAEW,KAAK,YAAY,OAAS,GAAK,KAAK,YAAY,IAAI,EAAQ,CAC/D,CACrB,KAAK,SAAS,eAAe,CAC7B,OAGF,KAAK,YAAY,OAAO,EAAQ,GAYpC,OAAsB,CAQpB,GAPA,KAAK,QAAU,GACf,KAAK,KAAO,GACZ,KAAK,OAAS,EAAE,CAChB,KAAK,WAA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,KAAK,aAAa,IAAI,EAAY,CAC9C,IACF,EAAQ,aAAe,KACvB,EAAQ,iBAAmB,MAI7B,IAAM,EAAM,KAAK,iBAAiB,KAAM,GAAM,EAAE,OAAS,EAAY,CAEjE,GACF,KAAK,sBAAsB,EAAI,EAEhC,EAAQ,CAKb,UAAkB,EAA6E,CAC7F,IAAK,IAAM,KAAW,KAAK,YACzB,EAAQ,QAAS,EAAQ,CAK7B,UAAkB,EAAoB,CACpC,IAAK,IAAM,KAAW,KAAK,YACzB,EAAQ,QAAS,EAAM,GAc7B,MAAM,EAAc,EAAE,OAAO,CAC3B,GAAI,EAAE,QAAQ,CACd,UAAW,EAAE,QAAQ,CAAC,KAAK,CAAC,aAAa,CACzC,UAAW,EAAE,QAAQ,CAAC,aAAa,CACnC,SAAU,EAAE,QAAQ,CAAC,aAAa,CAClC,YAAa,EAAE,QAAQ,CACxB,CAAC,CAEF,SAAS,EAA+B,EAA6B,CACnE,GAAM,CAAE,cAAa,cAAa,cAAa,eAAgB,EAyB/D,OAvBI,EAAY,UAAY,EAAY,QAKpC,EAAc,EAAY,EAAI,EAAc,EAAY,CACnD,GAIL,EAAY,OAAS,EAAY,KAKjC,EAAY,OAAS,EAAU,OAAS,EAAY,OAAS,EAAU,MAClE,EAAe,EAAY,QAAS,EAAY,QAAQ,CAG7D,EAAY,OAAS,EAAU,KAAO,EAAY,OAAS,EAAU,IAChE,EAAY,UAAY,EAAY,QAGtC,GAZE,GAVA,GAyBX,SAAS,EAAa,EAAgC,CAGpD,OAFe,EAAY,UAAU,EAAM,CAE7B"}
1
+ {"version":3,"file":"quoter.js","names":[],"sources":["../../src/quoter/quoter.ts"],"sourcesContent":["import { z } from 'zod';\nimport type {\n Quote,\n QuoterDonePayload,\n QuoterDoneReason,\n QuoterEventHandler,\n QuoterInterface,\n QuoterProps,\n ServiceQuoteEventArgs,\n QuotesTuple,\n ServiceQuoteEventHandler,\n} from '../types/quote';\nimport type { TransferService } from '../types/service';\nimport { earliestExpirationForService, isQuoteExpired, pruneExpiredQuotes, sortQuotes, upsertQuote } from './_utils';\nimport {\n QUOTER_DEFAULT_PRUNE_INTERVAL_MS,\n QUOTER_DEFAULT_REFRESH_BUFFER_SECONDS,\n QUOTER_EMPTY_RETRY_BASE_DELAY_MS,\n QUOTER_EMPTY_RETRY_MAX_DELAY_MS,\n} from './constants';\nimport { ServiceType, TokenType } from '../constants';\nimport { isNativeAsset } from '../type-guards';\nimport { isAddressEqual } from 'viem';\n\n/**\n * Function that returns the current UNIX time in seconds.\n * Injected for deterministic testing.\n */\nexport type Clock = () => number;\n\n/**\n * Options for constructing a Quoter instance.\n *\n * These options tune how often quote state is maintained and when services are\n * proactively restarted before quote expiry.\n */\nexport interface QuoterOptions {\n /** Clock function returning current time in seconds (defaults to Date.now()/1000). */\n readonly clock?: Clock;\n /** Interval for pruning expired quotes (milliseconds).\n *\n * @default 1_000\n */\n readonly pruneIntervalMs?: number;\n /**\n * Amount of seconds to pre-buffer a stream restart before the earliest service quote\n * expiration. Helps ensure continuity before actual expiry is reached.\n *\n * @default 5\n */\n readonly refreshBufferSeconds?: number;\n}\n\n/**\n * Quoter orchestrates quote streaming across multiple transfer services and emits a unified event stream.\n *\n * High-level lifecycle:\n * - Idle until first subscriber.\n * - On first subscribe, starts all eligible services.\n * - Collects and ranks active quotes, pruning expired ones over time.\n * - Completes when explicitly unsubscribed (last subscriber), when no service is eligible,\n * or when all eligible services finish without producing any quote (`no-quotes`).\n *\n * Event model:\n * - Service `quote` -> Quoter upserts quote, recomputes best quote, emits `quote`.\n * - Service `error` -> Quoter emits `error` (non-terminal by itself).\n * - Service `done` -> Quoter marks that service attempt as completed and evaluates retry/complete rules.\n *\n * Retry/refresh behavior:\n * - Refresh: services that have active quotes are restarted shortly before the earliest quote expires.\n * - Retry: services that complete with no quote and no error are only retried after the quoter has\n * already observed at least one quote from any service in this session.\n *\n * This design keeps first-pass \"no quotes anywhere\" terminal and fast, while still allowing services\n * like Markr to be retried in sessions where other providers are returning quotes.\n */\nexport class Quoter implements QuoterInterface {\n private readonly clock: Clock;\n private readonly props: QuoterProps;\n private readonly pruneIntervalMs: number;\n private readonly transferServices: readonly TransferService[];\n private readonly refreshBufferSeconds: number;\n\n private quotes: Quote[] = [];\n private subscribers: Set<QuoterEventHandler> = new Set();\n private started = false;\n private done = false;\n private isStartingFromSubscribe = false;\n private pruneTimerId: ReturnType<typeof setInterval> | null = null;\n private lastBestId: string | null = null;\n private hasReceivedAnyQuote = false;\n\n public readonly id: string = crypto.randomUUID();\n\n /**\n * Per-service runtime state for the active quote session.\n *\n * This tracks whether a service has completed, errored, produced quotes,\n * and any pending timers used for retry/refresh orchestration.\n */\n private serviceState: Map<\n TransferService['type'],\n {\n cancel: () => void;\n done: boolean;\n hasErrored: boolean;\n hasReturnedQuote: boolean;\n retryAttempt: number;\n retryTimer: ReturnType<typeof setTimeout> | null;\n refreshTimer: ReturnType<typeof setTimeout> | null;\n refreshAtSeconds: number | null;\n }\n > = new Map();\n\n /**\n * Create a new Quoter instance.\n *\n * @param props Quoting request parameters shared across all services.\n * @param transferServices Candidate services; eligibility is resolved at `start()` time.\n * @param options Optional runtime tuning for pruning and refresh behavior.\n */\n constructor(props: QuoterProps, transferServices: readonly TransferService[], options: QuoterOptions = {}) {\n this.clock = options.clock ?? (() => Math.floor(Date.now() / 1_000));\n this.props = props;\n this.pruneIntervalMs = options.pruneIntervalMs ?? QUOTER_DEFAULT_PRUNE_INTERVAL_MS;\n this.transferServices = transferServices;\n this.refreshBufferSeconds = options.refreshBufferSeconds ?? QUOTER_DEFAULT_REFRESH_BUFFER_SECONDS;\n }\n\n /**\n * Get the current best quote and all active quotes (sorted by desirability).\n */\n public getQuotes(): QuotesTuple {\n const now = this.clock();\n const active = pruneExpiredQuotes({ quotes: this.quotes, nowSeconds: now });\n const sorted = sortQuotes(active);\n const best = sorted[0] ?? null;\n\n return [best, sorted];\n }\n\n /**\n * Subscribe for quoter events.\n *\n * First subscriber lazily starts orchestration.\n * Last subscriber triggers terminal completion with reason `unsubscribed`.\n *\n * @returns Unsubscribe function (idempotent).\n */\n public subscribe(handler: QuoterEventHandler): () => void {\n this.subscribers.add(handler);\n let unsubscribed = false;\n\n if (!this.started) {\n this.isStartingFromSubscribe = true;\n try {\n this.start();\n } finally {\n this.isStartingFromSubscribe = false;\n }\n }\n\n return () => {\n if (unsubscribed) {\n return;\n }\n unsubscribed = true;\n\n if (this.done) {\n this.subscribers.delete(handler);\n return;\n }\n\n const wasLastSubscriber = this.subscribers.size === 1 && this.subscribers.has(handler);\n if (wasLastSubscriber) {\n this.complete('unsubscribed');\n return;\n }\n\n this.subscribers.delete(handler);\n };\n }\n\n // ----- Internal orchestration -----\n\n /**\n * Start a fresh quote session.\n *\n * Resets session-local state, validates basic request viability,\n * starts streams for eligible services, and begins prune ticks.\n */\n private start(): void {\n this.started = true;\n this.done = false;\n this.quotes = [];\n this.lastBestId = null;\n this.hasReceivedAnyQuote = false;\n\n // Same-chain quotes for the *same* asset are not a valid transfer scenario.\n if (isInvalidSameChainQuoteRequest(this.props)) {\n this.complete('no-eligible-services');\n return;\n }\n\n let hasEligibleService = false;\n\n // Start streams for eligible services\n for (const svc of this.transferServices) {\n const eligible = svc.analyzeSupport({\n sourceAsset: this.props.sourceAsset,\n sourceChainId: this.props.sourceChain.chainId,\n targetAsset: this.props.targetAsset,\n targetChainId: this.props.targetChain.chainId,\n });\n\n if (!eligible) continue;\n hasEligibleService = true;\n\n this.startStreamForService(svc);\n }\n\n if (!hasEligibleService) {\n this.complete('no-eligible-services');\n return;\n }\n\n // Start periodic prune\n this.pruneTimerId = setInterval(() => this.onPruneTick(), this.pruneIntervalMs);\n }\n\n /**\n * Stop all streams and timers.\n * Quotes are retained for snapshot access via getQuotes(), though they\n * can still be pruned due to expiration.\n */\n private stop(): void {\n this.started = false;\n\n if (this.pruneTimerId) {\n clearInterval(this.pruneTimerId);\n this.pruneTimerId = null;\n }\n\n for (const [type, state] of this.serviceState) {\n try {\n state.cancel();\n } catch {\n /* ignore */\n }\n\n if (state.refreshTimer) {\n clearTimeout(state.refreshTimer);\n }\n\n if (state.retryTimer) {\n clearTimeout(state.retryTimer);\n }\n\n this.serviceState.set(type, {\n cancel: () => {},\n done: false,\n hasErrored: false,\n hasReturnedQuote: false,\n retryAttempt: 0,\n retryTimer: null,\n refreshTimer: null,\n refreshAtSeconds: null,\n });\n }\n }\n\n /**\n * Begin or restart streaming for a specific service.\n *\n * Any previous stream/timers for that service are canceled before starting a new attempt.\n */\n private startStreamForService(svc: TransferService): void {\n // Clean up any existing stream and refresh timer\n const existing = this.serviceState.get(svc.type);\n\n if (existing) {\n try {\n existing.cancel();\n } catch {\n /* ignore */\n }\n\n if (existing.refreshTimer) clearTimeout(existing.refreshTimer);\n if (existing.retryTimer) clearTimeout(existing.retryTimer);\n }\n\n const handler = this.makeServiceHandler(svc.type);\n const bufferedEvents: ServiceQuoteEventArgs[] = [];\n let isStartingStream = true;\n\n // Some services emit quote/done synchronously inside streamQuotes().\n // If those events run before subscribe() returns, consumers can hit TDZ\n // when they reference the returned unsubscribe function in their callback.\n // Buffer startup emissions and flush them after stream initialization.\n const guardedHandler: ServiceQuoteEventHandler = (...eventArgs) => {\n if (isStartingStream) {\n bufferedEvents.push(eventArgs);\n return;\n }\n\n handler(...eventArgs);\n };\n\n const { cancel } = svc.streamQuotes(this.props, guardedHandler);\n isStartingStream = false;\n\n if (bufferedEvents.length > 0) {\n // Use a microtask so subscribe() can return before the first callback fires.\n // This preserves event order while avoiding re-entrancy during subscription.\n queueMicrotask(() => {\n for (const bufferedEvent of bufferedEvents) {\n handler(...bufferedEvent);\n }\n });\n }\n\n this.serviceState.set(svc.type, {\n cancel,\n done: false,\n hasErrored: false,\n hasReturnedQuote: false,\n retryAttempt: existing?.retryAttempt ?? 0,\n retryTimer: null,\n refreshTimer: null,\n refreshAtSeconds: null,\n });\n }\n\n /**\n * Create the service-scoped event handler consumed by `TransferService.streamQuotes`.\n *\n * The handler enforces service/quote consistency and translates service events into\n * quoter state transitions.\n */\n private makeServiceHandler(serviceType: TransferService['type']): ServiceQuoteEventHandler {\n return (event, ...args) => {\n // Ignore any service events once stopped to prevent post-stop mutations.\n if (!this.started) {\n return;\n }\n if (event === 'quote') {\n const maybeQuote = args[0];\n if (isQuoteValue(maybeQuote)) {\n // Enforce the quote belongs to the emitting service\n if (maybeQuote.serviceType !== serviceType) {\n // Ignore quotes mismatched to service; defensive\n return;\n }\n const state = this.serviceState.get(serviceType);\n if (state) {\n state.hasReturnedQuote = true;\n }\n this.onIncomingQuote(maybeQuote);\n this.scheduleServiceRefresh(serviceType);\n }\n\n return;\n }\n if (event === 'done') {\n this.onServiceDone(serviceType);\n\n return;\n }\n if (event === 'error') {\n const state = this.serviceState.get(serviceType);\n if (state) {\n state.hasErrored = true;\n }\n const maybeErr = args[0];\n if (maybeErr instanceof Error) {\n this.emitError(maybeErr);\n }\n }\n };\n }\n\n /**\n * Handle an incoming quote event.\n *\n * Expired quotes are ignored. Valid quotes are upserted into active state,\n * then sorted/pruned and emitted to subscribers.\n */\n private onIncomingQuote(quote: Quote): void {\n const now = this.clock();\n\n if (isQuoteExpired({ quote, nowSeconds: now })) {\n return;\n }\n\n this.hasReceivedAnyQuote = true;\n\n this.resetRetryForService(quote.serviceType);\n\n this.quotes = sortQuotes(pruneExpiredQuotes({ quotes: upsertQuote(this.quotes, quote), nowSeconds: now }));\n\n // Always emit on every incoming quote\n const best = this.quotes[0];\n if (best) {\n this.lastBestId = best.id;\n this.emitQuote({ bestQuote: best, quote, quotes: this.quotes });\n }\n }\n\n private resetRetryForService(serviceType: TransferService['type']): void {\n const state = this.serviceState.get(serviceType);\n\n if (!state) {\n return;\n }\n\n state.retryAttempt = 0;\n if (state.retryTimer) {\n clearTimeout(state.retryTimer);\n state.retryTimer = null;\n }\n }\n\n /**\n * Schedule a retry attempt for a service using exponential backoff.\n *\n * This is used only for quote-less successful completions once the overall session\n * has already produced at least one quote from some service.\n */\n private scheduleServiceRetry(serviceType: TransferService['type']): void {\n const state = this.serviceState.get(serviceType);\n\n if (!state) {\n return;\n }\n\n if (state.retryTimer) {\n clearTimeout(state.retryTimer);\n state.retryTimer = null;\n }\n\n const delayMs = Math.min(\n QUOTER_EMPTY_RETRY_MAX_DELAY_MS,\n QUOTER_EMPTY_RETRY_BASE_DELAY_MS * 2 ** state.retryAttempt,\n );\n state.retryAttempt += 1;\n\n state.retryTimer = setTimeout(\n () => {\n const current = this.serviceState.get(serviceType);\n if (current) {\n current.retryTimer = null;\n }\n\n if (!this.started) {\n return;\n }\n\n const svc = this.transferServices.find((s) => s.type === serviceType);\n if (svc) {\n this.startStreamForService(svc);\n }\n },\n Math.max(0, delayMs),\n );\n }\n\n /**\n * Handle service completion (`done`).\n *\n * A completion may schedule:\n * - refresh (if the service has active quotes), and/or\n * - retry (quote-less/non-error completion, but only after any quote has existed in session).\n *\n * Then evaluates whether the full quoter can complete with `no-quotes`.\n */\n private onServiceDone(serviceType: TransferService['type']): void {\n const state = this.serviceState.get(serviceType);\n\n if (!state) {\n return;\n }\n\n state.done = true;\n this.scheduleServiceRefresh(serviceType);\n\n if (this.hasReceivedAnyQuote && !state.hasErrored && !state.hasReturnedQuote) {\n this.scheduleServiceRetry(serviceType);\n }\n\n this.maybeCompleteNoQuotes();\n }\n\n /**\n * Complete with `no-quotes` when every eligible service has finished and\n * no quote was ever observed during this session.\n */\n private maybeCompleteNoQuotes(): void {\n if (!this.started || this.done || this.hasReceivedAnyQuote || this.serviceState.size === 0) {\n return;\n }\n\n const allServicesDone = [...this.serviceState.values()].every((state) => state.done);\n\n if (allServicesDone) {\n this.complete('no-quotes');\n }\n }\n\n /**\n * Finalize the quoter session and broadcast terminal reason exactly once.\n */\n private complete(reason: QuoterDoneReason): void {\n if (this.done) {\n return;\n }\n\n const payload = this.makeDonePayload(reason);\n\n this.done = true;\n this.stop();\n\n // When complete() happens during subscribe()->start(), emit done on a microtask\n // so callers can safely reference the returned unsubscribe in their handler.\n if (this.isStartingFromSubscribe) {\n queueMicrotask(() => {\n const handlers = [...this.subscribers];\n this.subscribers.clear();\n\n for (const handler of handlers) {\n handler('done', payload);\n }\n });\n return;\n }\n\n const handlers = [...this.subscribers];\n this.subscribers.clear();\n\n for (const handler of handlers) {\n handler('done', payload);\n }\n }\n\n private getInitializedServices(): ServiceType[] {\n return [...new Set(this.transferServices.map((service) => service.type))];\n }\n\n private getEligibleServices(): ServiceType[] {\n return [...this.serviceState.keys()];\n }\n\n private makeDonePayload(reason: QuoterDoneReason): QuoterDonePayload {\n if (reason === 'unsubscribed') {\n return { reason, data: undefined };\n }\n\n const initializedServices = this.getInitializedServices();\n\n if (reason === 'no-eligible-services') {\n return {\n reason,\n data: {\n initializedServices,\n quoterProps: this.props,\n },\n };\n }\n\n return {\n reason,\n data: {\n eligibleServices: this.getEligibleServices(),\n initializedServices,\n quoterProps: this.props,\n },\n };\n }\n\n /** Periodic prune tick to evict expired quotes and emit best changes. */\n private onPruneTick(): void {\n const now = this.clock();\n const pruned = pruneExpiredQuotes({ quotes: this.quotes, nowSeconds: now });\n\n if (pruned.length !== this.quotes.length) {\n this.quotes = sortQuotes(pruned);\n const best = this.quotes[0] ?? null;\n const bestId = best ? best.id : null;\n\n if (best && bestId !== this.lastBestId) {\n this.lastBestId = bestId;\n this.emitQuote({ bestQuote: best, quote: best, quotes: this.quotes });\n } else if (!best) {\n this.lastBestId = null;\n }\n }\n\n // Re-evaluate refresh scheduling for all services on prune tick\n for (const svc of this.transferServices) {\n if (this.serviceState.has(svc.type)) {\n this.scheduleServiceRefresh(svc.type);\n }\n }\n }\n\n /** Schedule a refresh (restart) of streaming for the service at its earliest quote expiration. */\n private scheduleServiceRefresh(serviceType: TransferService['type']): void {\n const state = this.serviceState.get(serviceType);\n\n if (!state) return;\n\n const now = this.clock();\n const earliest = earliestExpirationForService({ quotes: this.quotes, serviceType, nowSeconds: now });\n\n if (earliest === null) {\n // No quotes -> clear any pending refresh\n if (state.refreshTimer) {\n clearTimeout(state.refreshTimer);\n state.refreshTimer = null;\n }\n state.refreshAtSeconds = null;\n return;\n }\n\n // Only reschedule if earlier than currently scheduled or no timer\n const targetAt = Math.max(0, earliest - this.refreshBufferSeconds);\n\n // If target time has already passed, do not schedule another refresh here.\n if (targetAt <= now) {\n return;\n }\n\n // Reschedule only when the desired refresh time changes (earlier OR later).\n // We key refresh off the soonest-expiring active quote for this service.\n if (state.refreshAtSeconds === targetAt && state.refreshTimer) {\n return;\n }\n\n if (state.refreshTimer) {\n clearTimeout(state.refreshTimer);\n }\n\n const delayMs = Math.max(0, (targetAt - now) * 1_000);\n state.refreshAtSeconds = targetAt;\n state.refreshTimer = setTimeout(() => {\n // Clear timer/marker before restarting to avoid stale state if restart fails.\n const current = this.serviceState.get(serviceType);\n if (current) {\n current.refreshTimer = null;\n current.refreshAtSeconds = null;\n }\n\n // Restart stream and clear timer/marker\n const svc = this.transferServices.find((s) => s.type === serviceType);\n\n if (svc) {\n this.startStreamForService(svc);\n }\n }, delayMs);\n }\n\n // ----- Emission helpers -----\n /** Emit a quote event to all subscribers. */\n private emitQuote(payload: { bestQuote: Quote; quote: Quote; quotes: readonly Quote[] }): void {\n for (const handler of this.subscribers) {\n handler('quote', payload);\n }\n }\n\n /** Emit an error event to all subscribers. */\n private emitError(error: Error): void {\n for (const handler of this.subscribers) {\n handler('error', error);\n }\n }\n}\n\n/**\n * Zod schema to validate the minimal Quote fields used for runtime gating.\n *\n * This is a simple schema, and is not a fully safe runtime validate for Quote.\n * That isn't necessary here since we only need to verify basic structure before\n * using the quote in internal logic.\n *\n * @internal\n */\nconst QuoteSchema = z.object({\n id: z.string(),\n expiresAt: z.number().int().nonnegative(),\n amountOut: z.bigint().nonnegative(),\n amountIn: z.bigint().nonnegative(),\n serviceType: z.string(),\n});\n\nfunction isInvalidSameChainQuoteRequest(props: QuoterProps): boolean {\n const { sourceAsset, sourceChain, targetAsset, targetChain } = props;\n\n if (sourceChain.chainId !== targetChain.chainId) {\n return false;\n }\n\n // Native -> native on the same chain is a no-op.\n if (isNativeAsset(sourceAsset) && isNativeAsset(targetAsset)) {\n return true;\n }\n\n // If token types differ, it's potentially a swap on the same chain.\n if (sourceAsset.type !== targetAsset.type) {\n return false;\n }\n\n // Same token type and address on the same chain is a no-op.\n if (sourceAsset.type === TokenType.ERC20 && targetAsset.type === TokenType.ERC20) {\n return isAddressEqual(sourceAsset.address, targetAsset.address);\n }\n\n if (sourceAsset.type === TokenType.SPL && targetAsset.type === TokenType.SPL) {\n return sourceAsset.address === targetAsset.address;\n }\n\n return false;\n}\n\nfunction isQuoteValue(value: unknown): value is Quote {\n const result = QuoteSchema.safeParse(value);\n\n return result.success;\n}\n"],"mappings":"2YA4EA,IAAa,EAAb,KAA+C,CAC7C,MACA,MACA,gBACA,iBACA,qBAEA,OAA0B,EAAE,CAC5B,YAA+C,IAAI,IACnD,QAAkB,GAClB,KAAe,GACf,wBAAkC,GAClC,aAA8D,KAC9D,WAAoC,KACpC,oBAA8B,GAE9B,GAA6B,OAAO,YAAY,CAQhD,aAYI,IAAI,IASR,YAAY,EAAoB,EAA8C,EAAyB,EAAE,CAAE,CACzG,KAAK,MAAQ,EAAQ,YAAgB,KAAK,MAAM,KAAK,KAAK,CAAG,IAAM,EACnE,KAAK,MAAQ,EACb,KAAK,gBAAkB,EAAQ,iBAAA,IAC/B,KAAK,iBAAmB,EACxB,KAAK,qBAAuB,EAAQ,sBAAA,EAMtC,WAAgC,CAC9B,IAAM,EAAM,KAAK,OAAO,CAElB,EAAS,EADA,EAAmB,CAAE,OAAQ,KAAK,OAAQ,WAAY,EAAK,CAAC,CAC1C,CAGjC,MAAO,CAFM,EAAO,IAAM,KAEZ,EAAO,CAWvB,UAAiB,EAAyC,CACxD,KAAK,YAAY,IAAI,EAAQ,CAC7B,IAAI,EAAe,GAEnB,GAAI,CAAC,KAAK,QAAS,CACjB,KAAK,wBAA0B,GAC/B,GAAI,CACF,KAAK,OAAO,QACJ,CACR,KAAK,wBAA0B,IAInC,UAAa,CACP,MAKJ,IAFA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CACxB,CAAC,CAEF,SAAS,EAA+B,EAA6B,CACnE,GAAM,CAAE,cAAa,cAAa,cAAa,eAAgB,EAyB/D,OAvBI,EAAY,UAAY,EAAY,QAKpC,EAAc,EAAY,EAAI,EAAc,EAAY,CACnD,GAIL,EAAY,OAAS,EAAY,KAKjC,EAAY,OAAS,EAAU,OAAS,EAAY,OAAS,EAAU,MAClE,EAAe,EAAY,QAAS,EAAY,QAAQ,CAG7D,EAAY,OAAS,EAAU,KAAO,EAAY,OAAS,EAAU,IAChE,EAAY,UAAY,EAAY,QAGtC,GAZE,GAVA,GAyBX,SAAS,EAAa,EAAgC,CAGpD,OAFe,EAAY,UAAU,EAAM,CAE7B"}
@@ -1,2 +1,2 @@
1
- require(`../../_virtual/_rolldown/runtime.cjs`);const e=require(`../../utils/caip.cjs`),t=require(`../service-schemas.cjs`);let n=require(`zod`);const r=n.z.string().refine(t=>e.isCaip2ChainId(t),{message:`Invalid CAIP-2 chain ID`}).transform(e=>e),i=n.z.object({chainId:n.z.union([n.z.number(),n.z.string()]),chainType:n.z.string(),enabled_services:n.z.array(n.z.enum([`cross-chain-swap`,`cross-chain-quote`,`quote`,`swap`,`token-list`])),logo_url:n.z.url(),name:n.z.string(),lanes:n.z.array(n.z.union([n.z.coerce.number().int().nonnegative(),r]))}),a=i.extend({chainId:n.z.coerce.number().int().nonnegative(),chainType:n.z.literal(`evm`),router:t.EvmAddressSchema.optional(),wrapped_token:t.EvmAddressSchema.optional()}),o=i.extend({chainId:n.z.string().refine(t=>e.isCaip2ChainId(t)&&t.startsWith(`solana:`),{error:`Is not a valid Solana CAIP-2 ID`}).transform(e=>e),chainType:n.z.literal(`svm`)}),s=n.z.array(n.z.union([a,o,i.refine(e=>e.chainType!==`evm`&&e.chainType!==`svm`,`Known chain types must match their expected schema`)])),c=n.z.array(n.z.object({address:t.EvmAddressSchema,decimals:n.z.number().int().nonnegative().max(18),logo_url:n.z.url(),name:n.z.string(),symbol:n.z.string()})),l=n.z.union([n.z.object({aggregator:n.z.object({id:n.z.string(),logo_url:n.z.url(),name:n.z.string()}),amountIn:n.z.coerce.bigint().nonnegative(),amountOut:n.z.coerce.bigint().nonnegative(),chainId:n.z.union([n.z.number().int().nonnegative(),r]),expiredAt:n.z.number().int().nonnegative(),fees:n.z.array(n.z.object({type:n.z.enum([`protocol`,`gas`,`bridge`,`slippage`,`swap`,`other`]),name:n.z.string(),amount:n.z.coerce.bigint().nonnegative(),token:n.z.object({chainId:n.z.union([n.z.number().int().nonnegative(),r]),address:n.z.union([t.EvmAddressSchema,t.SolAddressSchema])}),extra:n.z.boolean().optional()})).optional(),gasEstimate:n.z.coerce.bigint().nonnegative().optional(),recommendedSlippage:n.z.number().int().nonnegative(),tokenIn:n.z.union([t.EvmAddressSchema,t.SolAddressSchema]),tokenInDecimals:n.z.number().int().nonnegative().max(18),tokenOut:n.z.union([t.EvmAddressSchema,t.SolAddressSchema]),tokenOutDecimals:n.z.number().int().nonnegative().max(18),uuid:n.z.uuid()}),n.z.object({done:n.z.literal(!0)})]),u=n.z.object({data:t.HexSchema,to:t.EvmAddressSchema,value:n.z.coerce.bigint().nonnegative()}),d=n.z.object({chainType:n.z.literal(`svm`),swapTransaction:n.z.base64()}),f=n.z.union([u,d]),p=n.z.object({fee:n.z.int().nonnegative(),name:n.z.string()}),m=/[zZ]|[+-]\d{2}:?\d{2}$/,h=n.z.iso.datetime({local:!0}).refine(e=>{let t=m.test(e)?e:e+`Z`;return n.z.iso.datetime().safeParse(t).success},{error:`Invalid ISO-8601 datetime (must be valid with timezone, or local assumed UTC)`}).transform(e=>m.test(e)?e:e+`Z`),g=n.z.object({messageId:n.z.string().nullable(),status:n.z.enum([`pending`,`committed`,`pending_execution`,`completed`,`failed`,`refunded`,`unknown`]),description:n.z.string(),sourceChain:n.z.object({name:n.z.string(),transactionHash:n.z.union([t.HashSchema,t.SolSignatureSchema]),timestamp:h,finalized:h.or(n.z.boolean()).nullable()}),destinationChain:n.z.object({name:n.z.string().nullable(),transactionHash:n.z.union([t.HashSchema,t.SolSignatureSchema]).nullable(),bridgeHash:n.z.union([t.HashSchema,t.SolSignatureSchema]).nullable().optional(),timestamp:h.nullable(),finalized:h.nullable()}),progress:n.z.object({committed:n.z.boolean(),commitTimestamp:h.nullable().optional(),executed:n.z.boolean()}),fees:n.z.object({token:n.z.union([t.EvmAddressSchema,t.SolAddressSchema]).nullable(),amount:n.z.coerce.bigint().nonnegative().nullable()}).nullable(),transferredTokens:n.z.array(n.z.object({token:n.z.union([t.EvmAddressSchema,t.SolAddressSchema]),amount:n.z.coerce.bigint().nonnegative()})),debug:n.z.looseObject({messageId:n.z.string(),status:n.z.string(),destinationChain:n.z.string(),destinationTxHash:n.z.string(),relayTxHash:n.z.string(),retryCount:n.z.number().int().nonnegative(),lastRetryAt:n.z.iso.datetime(),sourceChain:n.z.string(),sourceTxHash:n.z.string(),createdAt:n.z.iso.datetime()}).partial().nullable().optional()}),_=n.z.object({address:t.EvmAddressSchema});exports.CrossChainStatusResponseSchema=g,exports.PartnerInfoResponseSchema=p,exports.QuoteResponseSchema=l,exports.SpenderAddressResponseSchema=_,exports.SupportedChainsResponseSchema=s,exports.SwapResponseSchema=f,exports.TokenListResponseSchema=c;
1
+ require(`../../_virtual/_rolldown/runtime.cjs`);const e=require(`../../utils/caip.cjs`),t=require(`../service-schemas.cjs`);let n=require(`zod`);require(`viem`);const r=n.z.string().refine(t=>e.isCaip2ChainId(t),{message:`Invalid CAIP-2 chain ID`}).transform(e=>e),i=n.z.object({chainId:n.z.union([n.z.number(),n.z.string()]),chainType:n.z.string(),enabled_services:n.z.array(n.z.enum([`cross-chain-swap`,`cross-chain-quote`,`quote`,`swap`,`token-list`])),logo_url:n.z.url(),name:n.z.string(),lanes:n.z.array(n.z.union([n.z.coerce.number().int().nonnegative(),r]))}),a=i.extend({chainId:n.z.coerce.number().int().nonnegative(),chainType:n.z.literal(`evm`),router:t.EvmAddressSchema.optional(),wrapped_token:t.EvmAddressSchema.optional()}),o=i.extend({chainId:n.z.string().refine(t=>e.isCaip2ChainId(t)&&t.startsWith(`solana:`),{error:`Is not a valid Solana CAIP-2 ID`}).transform(e=>e),chainType:n.z.literal(`svm`)}),s=n.z.array(n.z.union([a,o,i.refine(e=>e.chainType!==`evm`&&e.chainType!==`svm`,`Known chain types must match their expected schema`)])),c=n.z.array(n.z.object({address:t.EvmAddressSchema,decimals:n.z.number().int().nonnegative().max(18),logo_url:n.z.url(),name:n.z.string(),symbol:n.z.string()})),l=n.z.union([t.EvmAddressSchema,t.SolAddressSchema,t.HyperliquidAddressSchema]),u=n.z.union([n.z.object({aggregator:n.z.object({id:n.z.string(),logo_url:n.z.url(),name:n.z.string()}),amountIn:n.z.coerce.bigint().nonnegative(),amountOut:n.z.coerce.bigint().nonnegative(),chainId:n.z.union([n.z.number().int().nonnegative(),r]),expiredAt:n.z.number().int().nonnegative(),fees:n.z.array(n.z.object({type:n.z.enum([`protocol`,`gas`,`bridge`,`slippage`,`swap`,`other`,`relay`]),name:n.z.string(),amount:n.z.coerce.bigint().nonnegative(),token:n.z.object({chainId:n.z.union([n.z.number().int().nonnegative(),r]),address:l}),extra:n.z.boolean().optional()})).optional(),gasEstimate:n.z.coerce.bigint().nonnegative().optional(),recommendedSlippage:n.z.number().int().nonnegative(),tokenIn:l,tokenInDecimals:n.z.number().int().nonnegative().max(18),tokenOut:l,tokenOutDecimals:n.z.number().int().nonnegative().max(18),uuid:n.z.uuid()}),n.z.object({done:n.z.literal(!0)})]),d=n.z.object({data:t.HexSchema,to:t.EvmAddressSchema,value:n.z.coerce.bigint().nonnegative()}),f=n.z.object({chainType:n.z.literal(`svm`),swapTransaction:n.z.base64()}),p=n.z.union([d,f]),m=n.z.object({fee:n.z.int().nonnegative(),name:n.z.string()}),h=/[zZ]|[+-]\d{2}:?\d{2}$/,g=n.z.iso.datetime({local:!0}).refine(e=>{let t=h.test(e)?e:`${e}Z`;return n.z.iso.datetime().safeParse(t).success},{error:`Invalid ISO-8601 datetime (must be valid with timezone, or local assumed UTC)`}).transform(e=>h.test(e)?e:`${e}Z`),_=n.z.object({messageId:n.z.string().nullable(),status:n.z.enum([`pending`,`committed`,`pending_execution`,`completed`,`failed`,`refunded`,`unknown`]),description:n.z.string(),sourceChain:n.z.object({name:n.z.string(),transactionHash:n.z.union([t.HashSchema,t.SolSignatureSchema]),timestamp:g,finalized:g.or(n.z.boolean()).nullable()}),destinationChain:n.z.object({name:n.z.string().nullable(),transactionHash:n.z.union([t.HashSchema,t.SolSignatureSchema]).nullable(),bridgeHash:n.z.union([t.HashSchema,t.SolSignatureSchema]).nullable().optional(),timestamp:g.nullable(),finalized:g.nullable()}),progress:n.z.object({committed:n.z.boolean(),commitTimestamp:g.nullable().optional(),executed:n.z.boolean()}),fees:n.z.object({token:n.z.union([t.EvmAddressSchema,t.SolAddressSchema]).nullable(),amount:n.z.coerce.bigint().nonnegative().nullable()}).nullable(),transferredTokens:n.z.array(n.z.object({token:n.z.union([t.EvmAddressSchema,t.SolAddressSchema]),amount:n.z.coerce.bigint().nonnegative()})),debug:n.z.looseObject({messageId:n.z.string(),status:n.z.string(),destinationChain:n.z.string(),destinationTxHash:n.z.string(),relayTxHash:n.z.string(),retryCount:n.z.number().int().nonnegative(),lastRetryAt:n.z.iso.datetime(),sourceChain:n.z.string(),sourceTxHash:n.z.string(),createdAt:n.z.iso.datetime()}).partial().nullable().optional()}),v=n.z.object({address:t.EvmAddressSchema});exports.CrossChainStatusResponseSchema=_,exports.PartnerInfoResponseSchema=m,exports.QuoteResponseSchema=u,exports.SpenderAddressResponseSchema=v,exports.SupportedChainsResponseSchema=s,exports.SwapResponseSchema=p,exports.TokenListResponseSchema=c;
2
2
  //# sourceMappingURL=_schema.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"_schema.cjs","names":["z","isCaip2ChainId","EvmAddressSchema","SolAddressSchema","HexSchema","HashSchema","SolSignatureSchema"],"sources":["../../../src/transfer-service/markr/_schema.ts"],"sourcesContent":["import type { Signature, Address as SolAddress } from '@solana/kit';\nimport { z } from 'zod';\nimport type { Address as EvmAddress, Hash } from 'viem';\nimport { EvmAddressSchema, HashSchema, HexSchema, SolAddressSchema, SolSignatureSchema } from '../service-schemas';\nimport { isCaip2ChainId } from '../../utils/caip';\nimport type { Caip2ChainId } from '../../types/caip';\nimport type { Hex } from '../../types/signer';\n\ninterface SupportedChainsResponseItemBase {\n chainId: number | string;\n chainType: string;\n /** Available services on the chain. Each service has specific functionality */\n enabled_services: Array<'cross-chain-swap' | 'cross-chain-quote' | 'quote' | 'swap' | 'token-list'>;\n /** Chain logo URL */\n logo_url: string;\n /** Chain display name */\n name: string;\n /**\n * List of chain IDs that this chain can swap to via cross-chain aggregators.\n *\n * Empty array means no cross-chain routes available.\n */\n lanes: ReadonlyArray<number | Caip2ChainId>;\n}\n\nexport interface SupportedChainsResponseItemEvm extends SupportedChainsResponseItemBase {\n /** Chain identifier */\n chainId: number;\n /** Chain virtual machine type */\n chainType: 'evm';\n /** Router contract address */\n router?: EvmAddress;\n /** Native token wrapped contract address */\n wrapped_token?: EvmAddress;\n}\n\nexport interface SupportedChainsResponseItemSvm extends SupportedChainsResponseItemBase {\n /** Chain identifier */\n chainId: Caip2ChainId;\n /** Chain virtual machine type */\n chainType: 'svm';\n}\n\nexport type SupportedChainsResponse = Array<\n SupportedChainsResponseItemEvm | SupportedChainsResponseItemSvm | SupportedChainsResponseItemBase\n>;\n\nexport const Caip2ChainIdSchema: z.ZodType<Caip2ChainId> = z\n .string()\n .refine((value) => isCaip2ChainId(value), {\n message: 'Invalid CAIP-2 chain ID',\n })\n .transform((value) => value as Caip2ChainId);\n\nconst SupportedChainsResponseItemBaseSchema = z.object({\n chainId: z.union([z.number(), z.string()]),\n chainType: z.string(),\n enabled_services: z.array(z.enum(['cross-chain-swap', 'cross-chain-quote', 'quote', 'swap', 'token-list'])),\n logo_url: z.url(),\n name: z.string(),\n lanes: z.array(z.union([z.coerce.number().int().nonnegative(), Caip2ChainIdSchema])),\n});\n\nconst SupportedChainsResponseItemEvmSchema: z.ZodType<SupportedChainsResponseItemEvm> =\n SupportedChainsResponseItemBaseSchema.extend({\n chainId: z.coerce.number().int().nonnegative(),\n chainType: z.literal('evm'),\n router: EvmAddressSchema.optional(),\n wrapped_token: EvmAddressSchema.optional(),\n });\n\nconst SupportedChainsResponseItemSvmSchema: z.ZodType<SupportedChainsResponseItemSvm> =\n SupportedChainsResponseItemBaseSchema.extend({\n chainId: z\n .string()\n .refine((value) => isCaip2ChainId(value) && value.startsWith('solana:'), {\n error: 'Is not a valid Solana CAIP-2 ID',\n })\n .transform((value) => value as Caip2ChainId),\n chainType: z.literal('svm'),\n });\n\n/**\n * Schema for the response from Markr's /info/chains endpoint.\n *\n * @see https://orchestrator-docs.markr.io/#/paths/~1info~1chains/get\n */\nexport const SupportedChainsResponseSchema: z.ZodType<SupportedChainsResponse> = z.array(\n z.union([\n SupportedChainsResponseItemEvmSchema,\n SupportedChainsResponseItemSvmSchema,\n SupportedChainsResponseItemBaseSchema.refine(\n (item) => item.chainType !== 'evm' && item.chainType !== 'svm',\n 'Known chain types must match their expected schema',\n ),\n ]),\n);\n\nexport type TokenListResponse = Array<{\n address: EvmAddress;\n decimals: number;\n logo_url: string;\n name: string;\n symbol: string;\n}>;\n\n/**\n * Schema for the response Markr's /tokens/{chainId}/list endpoint.\n *\n * @see https://orchestrator-docs.markr.io/#/paths/~1tokens~1%7BchainId%7D~1list/get\n */\nexport const TokenListResponseSchema: z.ZodType<TokenListResponse> = z.array(\n z.object({\n address: EvmAddressSchema,\n decimals: z.number().int().nonnegative().max(18),\n logo_url: z.url(),\n name: z.string(),\n symbol: z.string(),\n }),\n);\n\nexport interface QuoteResponseData {\n aggregator: {\n id: string;\n logo_url: string;\n name: string;\n };\n amountIn: bigint;\n amountOut: bigint;\n /** Chain identifier (number for EVM chains, CAIP-2 ID for SVM chains) */\n chainId: number | Caip2ChainId;\n expiredAt: number;\n /**\n * Fee breakdown for cross-chain bridge transactions.\n *\n * Only included in quotes from cross-chain aggregators (DeBridge, LiFi).\n */\n fees?: ReadonlyArray<{\n type: 'protocol' | 'gas' | 'bridge' | 'slippage' | 'swap' | 'other';\n /** Human-readable fee name */\n name: string;\n /** Fee amount in token's smallest unit */\n amount: bigint;\n /**\n * When `true`, indicates the fee is an additional fee on top of the input\n * amount that the user needs to pay. These fees need checked against the user's\n * balance to prevent insufficient balance errors.\n */\n extra?: boolean;\n /** Token the fee is deducted from */\n token: {\n /** Chain ID where fee is deducted */\n chainId: number | Caip2ChainId;\n /** Token address (EVM hex or Solana base58) */\n address: EvmAddress | SolAddress;\n };\n }>;\n gasEstimate?: bigint;\n recommendedSlippage: number;\n /** Input token address (EVM hex or Solana base58) */\n tokenIn: EvmAddress | SolAddress;\n tokenInDecimals: number;\n /** Output token address (EVM hex or Solana base58) */\n tokenOut: EvmAddress | SolAddress;\n tokenOutDecimals: number;\n uuid: string;\n}\n\nexport interface QuoteResponseDataDone {\n done: true;\n}\n\nexport type QuoteResponse = QuoteResponseData | QuoteResponseDataDone;\n\n/**\n * Schema for the response from Markr's /quote endpoint.\n *\n * @see https://orchestrator-docs.markr.io/#/paths/~1quote/post\n */\nexport const QuoteResponseSchema: z.ZodType<QuoteResponse> = z.union([\n z.object({\n aggregator: z.object({\n id: z.string(),\n logo_url: z.url(),\n name: z.string(),\n }),\n amountIn: z.coerce.bigint().nonnegative(),\n amountOut: z.coerce.bigint().nonnegative(),\n chainId: z.union([z.number().int().nonnegative(), Caip2ChainIdSchema]),\n /**\n * Unix time in seconds when the quote expires.\n */\n expiredAt: z.number().int().nonnegative(),\n fees: z\n .array(\n z.object({\n type: z.enum(['protocol', 'gas', 'bridge', 'slippage', 'swap', 'other']),\n name: z.string(),\n amount: z.coerce.bigint().nonnegative(),\n token: z.object({\n chainId: z.union([z.number().int().nonnegative(), Caip2ChainIdSchema]),\n address: z.union([EvmAddressSchema, SolAddressSchema]),\n }),\n extra: z.boolean().optional(),\n }),\n )\n .optional(),\n /**\n * Estimated gas for the swap transaction.\n * Markr estimates already include a buffer.\n *\n * The API docs do not specify this field as optional, but in practice it can be missing.\n */\n gasEstimate: z.coerce.bigint().nonnegative().optional(),\n /** Recommended slippage in basis points. */\n recommendedSlippage: z.number().int().nonnegative(),\n tokenIn: z.union([EvmAddressSchema, SolAddressSchema]),\n tokenInDecimals: z.number().int().nonnegative().max(18),\n tokenOut: z.union([EvmAddressSchema, SolAddressSchema]),\n tokenOutDecimals: z.number().int().nonnegative().max(18),\n uuid: z.uuid(),\n }),\n z.object({\n done: z.literal(true),\n }),\n]);\n\n/**\n * Response type for Markr's /swap endpoint.\n *\n * @see https://orchestrator-docs.markr.io/#/paths/~1swap/post\n */\nexport interface WrappedSwapTransactionResponse {\n data: Hex;\n to: EvmAddress;\n value: bigint;\n}\n\nexport const WrappedSwapTransactionResponseSchema: z.ZodType<WrappedSwapTransactionResponse> = z.object({\n data: HexSchema,\n to: EvmAddressSchema,\n value: z.coerce.bigint().nonnegative(),\n});\n\n/**\n * Solana swap transaction (returned for SVM chains).\n */\nexport interface SolanaSwapTransactionResponse {\n /** Chain type identifier */\n chainType: 'svm';\n /** Base64-encoded Solana transaction to be signed and sent by the client */\n swapTransaction: string;\n}\n\nexport const SolanaSwapTransactionResponseSchema: z.ZodType<SolanaSwapTransactionResponse> = z.object({\n chainType: z.literal('svm'),\n swapTransaction: z.base64(),\n});\n\nexport type SwapResponse = WrappedSwapTransactionResponse | SolanaSwapTransactionResponse;\n\nexport const SwapResponseSchema: z.ZodType<SwapResponse> = z.union([\n WrappedSwapTransactionResponseSchema,\n SolanaSwapTransactionResponseSchema,\n]);\n\nexport interface PartnerInfoResponse {\n /**\n * The partner fee in basis points collected on each swap.\n */\n fee: number;\n name: string;\n}\n\nexport const PartnerInfoResponseSchema: z.ZodType<PartnerInfoResponse> = z.object({\n fee: z.int().nonnegative(),\n name: z.string(),\n});\n\n/**\n * If the destination token is USDC, the flow is:\n * - pending -> committed -> completed\n *\n * If the destination token is not USDC, the flow is:\n * - pending -> committed -> pending_execution -> completed\n *\n * If the transaction fails at any point, the status will be 'failed'.\n */\nexport type CrossChainStatus =\n | /** Transaction submitted to CCIP */\n 'pending'\n /** Message committed, awaiting execution on destination chain */\n | 'committed'\n /** Message received on destination, swap pending execution by relayer */\n | 'pending_execution'\n /** Message executed successfully on destination chain */\n | 'completed'\n /** Swap expired, and funds were refunded (CCIP only) */\n | 'refunded'\n /** Execution failed */\n | 'failed'\n /** Unknown state */\n | 'unknown';\n\nexport interface CrossChainStatusResponse {\n /**\n * Cross-chain transaction identifier.\n *\n * For CCIP: The CCIP message ID.\n * For DeBridge: The DeBridge order ID.\n */\n messageId: string | null;\n /** Current status of the cross-chain transaction. */\n status: CrossChainStatus;\n /** Human-readable status description */\n description: string;\n /** Source chain information */\n sourceChain: {\n /** Source chain network name */\n name: string;\n /** Source chain transaction hash */\n transactionHash: Hash | Signature;\n /** Transaction timestamp on source chain (ISO 8601) */\n timestamp: string;\n /** When the source transaction was finalized (ISO 8601) */\n finalized: string | boolean | null;\n };\n /** Destination chain information */\n destinationChain: {\n /** Destination chain network name */\n name: string | null;\n /**\n * Destination chain tx hash.\n *\n * - For direct transfers (USDC -> USDC): The CCIP bridge transaction hash.\n * - For successful swaps: The actual swap transaction hash from the relayer.\n * - For pending swaps: null (swap not yet executed).\n */\n transactionHash: Hash | Signature | null;\n /**\n * CCIP bridge tx hash (only present for swaps, not direct transfers).\n *\n * This is the transaction that delivered USDC to the destination chain via CCIP.\n * The `transactionHash` field contains the subsequent swap transaction.\n */\n bridgeHash?: Hash | Signature | null;\n /** Transaction timestamp on destination chain (null if not yet executed) (ISO 8601) */\n timestamp: string | null;\n /** When the destination transaction was finalized (null if not yet executed) (ISO 8601) */\n finalized: string | null;\n };\n /** Transaction progress information */\n progress: {\n /** Whether the message has been committed on destination chain */\n committed: boolean;\n /** When the message was committed (null if not committed) (ISO 8601) */\n commitTimestamp?: string | null;\n /** Whether the message has been executed on destination chain */\n executed: boolean;\n };\n /** Fee information */\n fees: {\n /** Fee token address */\n token: EvmAddress | SolAddress | null;\n /** Fee amount in wei */\n amount: bigint | null;\n } | null;\n /** Tokens transferred in this cross-chain transaction */\n transferredTokens: ReadonlyArray<{\n /** Token address */\n token: EvmAddress | SolAddress;\n /** Token amount */\n amount: bigint;\n }>;\n /**\n * Debug information from Markr transaction API (only present for swaps that have been executed).\n *\n * Contains detailed information about the relayer execution, retry attempts, and transaction details.\n */\n debug?: Partial<{\n /** CCIP message ID */\n messageId: string;\n /** Execution status from relayer */\n status: string;\n /** Destination chain name */\n destinationChain: string;\n /** Destination swap transaction hash */\n destinationTxHash: string;\n /** Relay transaction hash */\n relayTxHash: string;\n /** Number of retry attempts */\n retryCount: number;\n /** Timestamp of last retry attempt (ISO 8601) */\n lastRetryAt: string;\n /** Source chain name */\n sourceChain: string;\n /** Source transaction hash */\n sourceTxHash: string;\n /** When the record was created (ISO 8601) */\n createdAt: string;\n }> | null;\n}\n\nconst ISO_8601_TIMEZONE_DESIGNATOR_REGEX = /[zZ]|[+-]\\d{2}:?\\d{2}$/;\n\n// Markr API returns ISO-8601 datetime strings that are sometimes missing a timezone designator (\"local\" ISO).\n// We assume these are meant to be UTC and append 'Z' if missing, then validate as strict ISO-8601 with timezone.\nconst datetime = z.iso\n .datetime({ local: true })\n .refine(\n (value) => {\n // Accept at least local ISO format\n // If missing timezone, treat as UTC\n const hasTimezone = ISO_8601_TIMEZONE_DESIGNATOR_REGEX.test(value);\n const isoString = hasTimezone ? value : value + 'Z';\n // Validate as strict ISO-8601 with timezone\n return z.iso.datetime().safeParse(isoString).success;\n },\n {\n error: 'Invalid ISO-8601 datetime (must be valid with timezone, or local assumed UTC)',\n },\n )\n .transform((value) => {\n const hasTimezone = ISO_8601_TIMEZONE_DESIGNATOR_REGEX.test(value);\n return hasTimezone ? value : value + 'Z';\n });\n\nexport const CrossChainStatusResponseSchema: z.ZodType<CrossChainStatusResponse> = z.object({\n messageId: z.string().nullable(),\n status: z.enum(['pending', 'committed', 'pending_execution', 'completed', 'failed', 'refunded', 'unknown']),\n description: z.string(),\n sourceChain: z.object({\n name: z.string(),\n transactionHash: z.union([HashSchema, SolSignatureSchema]),\n timestamp: datetime,\n finalized: datetime.or(z.boolean()).nullable(),\n }),\n destinationChain: z.object({\n name: z.string().nullable(),\n transactionHash: z.union([HashSchema, SolSignatureSchema]).nullable(),\n bridgeHash: z.union([HashSchema, SolSignatureSchema]).nullable().optional(),\n timestamp: datetime.nullable(),\n finalized: datetime.nullable(),\n }),\n progress: z.object({\n committed: z.boolean(),\n commitTimestamp: datetime.nullable().optional(),\n executed: z.boolean(),\n }),\n fees: z\n .object({\n token: z.union([EvmAddressSchema, SolAddressSchema]).nullable(),\n amount: z.coerce.bigint().nonnegative().nullable(),\n })\n .nullable(),\n transferredTokens: z.array(\n z.object({\n token: z.union([EvmAddressSchema, SolAddressSchema]),\n amount: z.coerce.bigint().nonnegative(),\n }),\n ),\n debug: z\n .looseObject({\n messageId: z.string(),\n status: z.string(),\n destinationChain: z.string(),\n destinationTxHash: z.string(),\n relayTxHash: z.string(),\n retryCount: z.number().int().nonnegative(),\n lastRetryAt: z.iso.datetime(),\n sourceChain: z.string(),\n sourceTxHash: z.string(),\n createdAt: z.iso.datetime(),\n })\n .partial()\n .nullable()\n .optional(),\n});\n\nexport interface SpenderAddressResponse {\n address: EvmAddress;\n}\n\nexport const SpenderAddressResponseSchema: z.ZodType<SpenderAddressResponse> = z.object({\n address: EvmAddressSchema,\n});\n"],"mappings":"iJA+CA,MAAa,EAA8CA,EAAAA,EACxD,QAAQ,CACR,OAAQ,GAAUC,EAAAA,eAAe,EAAM,CAAE,CACxC,QAAS,0BACV,CAAC,CACD,UAAW,GAAU,EAAsB,CAExC,EAAwCD,EAAAA,EAAE,OAAO,CACrD,QAASA,EAAAA,EAAE,MAAM,CAACA,EAAAA,EAAE,QAAQ,CAAEA,EAAAA,EAAE,QAAQ,CAAC,CAAC,CAC1C,UAAWA,EAAAA,EAAE,QAAQ,CACrB,iBAAkBA,EAAAA,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AAQ,CAC7B,YAAaA,EAAAA,EAAE,QAAQ,CACvB,WAAYA,EAAAA,EAAE,QAAQ,CAAC,KAAK,CAAC,aAAa,CAC1C,YAAaA,EAAAA,EAAE,IAAI,UAAU,CAC7B,YAAaA,EAAAA,EAAE,QAAQ,CACvB,aAAcA,EAAAA,EAAE,QAAQ,CACxB,UAAWA,EAAAA,EAAE,IAAI,UAAU,CAC5B,CAAC,CACD,SAAS,CACT,UAAU,CACV,UAAU,CACd,CAAC,CAMW,EAAkEA,EAAAA,EAAE,OAAO,CACtF,QAASE,EAAAA,iBACV,CAAC"}
1
+ {"version":3,"file":"_schema.cjs","names":["z","isCaip2ChainId","EvmAddressSchema","SolAddressSchema","HyperliquidAddressSchema","HexSchema","HashSchema","SolSignatureSchema"],"sources":["../../../src/transfer-service/markr/_schema.ts"],"sourcesContent":["import type { Signature, Address as SolAddress } from '@solana/kit';\nimport { z } from 'zod';\nimport { type Address as EvmAddress, type Hash } from 'viem';\nimport {\n EvmAddressSchema,\n HashSchema,\n HexSchema,\n HyperliquidAddressSchema,\n SolAddressSchema,\n SolSignatureSchema,\n} from '../service-schemas';\nimport { isCaip2ChainId } from '../../utils/caip';\nimport type { Caip2ChainId } from '../../types/caip';\nimport type { Hex } from '../../types/signer';\n\ninterface SupportedChainsResponseItemBase {\n chainId: number | string;\n chainType: string;\n /** Available services on the chain. Each service has specific functionality */\n enabled_services: Array<'cross-chain-swap' | 'cross-chain-quote' | 'quote' | 'swap' | 'token-list'>;\n /** Chain logo URL */\n logo_url: string;\n /** Chain display name */\n name: string;\n /**\n * List of chain IDs that this chain can swap to via cross-chain aggregators.\n *\n * Empty array means no cross-chain routes available.\n */\n lanes: ReadonlyArray<number | Caip2ChainId>;\n}\n\nexport interface SupportedChainsResponseItemEvm extends SupportedChainsResponseItemBase {\n /** Chain identifier */\n chainId: number;\n /** Chain virtual machine type */\n chainType: 'evm';\n /** Router contract address */\n router?: EvmAddress;\n /** Native token wrapped contract address */\n wrapped_token?: EvmAddress;\n}\n\nexport interface SupportedChainsResponseItemSvm extends SupportedChainsResponseItemBase {\n /** Chain identifier */\n chainId: Caip2ChainId;\n /** Chain virtual machine type */\n chainType: 'svm';\n}\n\nexport type SupportedChainsResponse = Array<\n SupportedChainsResponseItemEvm | SupportedChainsResponseItemSvm | SupportedChainsResponseItemBase\n>;\n\nexport const Caip2ChainIdSchema: z.ZodType<Caip2ChainId> = z\n .string()\n .refine((value) => isCaip2ChainId(value), {\n message: 'Invalid CAIP-2 chain ID',\n })\n .transform((value) => value as Caip2ChainId);\n\nconst SupportedChainsResponseItemBaseSchema = z.object({\n chainId: z.union([z.number(), z.string()]),\n chainType: z.string(),\n enabled_services: z.array(z.enum(['cross-chain-swap', 'cross-chain-quote', 'quote', 'swap', 'token-list'])),\n logo_url: z.url(),\n name: z.string(),\n lanes: z.array(z.union([z.coerce.number().int().nonnegative(), Caip2ChainIdSchema])),\n});\n\nconst SupportedChainsResponseItemEvmSchema: z.ZodType<SupportedChainsResponseItemEvm> =\n SupportedChainsResponseItemBaseSchema.extend({\n chainId: z.coerce.number().int().nonnegative(),\n chainType: z.literal('evm'),\n router: EvmAddressSchema.optional(),\n wrapped_token: EvmAddressSchema.optional(),\n });\n\nconst SupportedChainsResponseItemSvmSchema: z.ZodType<SupportedChainsResponseItemSvm> =\n SupportedChainsResponseItemBaseSchema.extend({\n chainId: z\n .string()\n .refine((value) => isCaip2ChainId(value) && value.startsWith('solana:'), {\n error: 'Is not a valid Solana CAIP-2 ID',\n })\n .transform((value) => value as Caip2ChainId),\n chainType: z.literal('svm'),\n });\n\n/**\n * Schema for the response from Markr's /info/chains endpoint.\n *\n * @see https://orchestrator-docs.markr.io/#/paths/~1info~1chains/get\n */\nexport const SupportedChainsResponseSchema: z.ZodType<SupportedChainsResponse> = z.array(\n z.union([\n SupportedChainsResponseItemEvmSchema,\n SupportedChainsResponseItemSvmSchema,\n SupportedChainsResponseItemBaseSchema.refine(\n (item) => item.chainType !== 'evm' && item.chainType !== 'svm',\n 'Known chain types must match their expected schema',\n ),\n ]),\n);\n\nexport type TokenListResponse = Array<{\n address: EvmAddress;\n decimals: number;\n logo_url: string;\n name: string;\n symbol: string;\n}>;\n\n/**\n * Schema for the response Markr's /tokens/{chainId}/list endpoint.\n *\n * @see https://orchestrator-docs.markr.io/#/paths/~1tokens~1%7BchainId%7D~1list/get\n */\nexport const TokenListResponseSchema: z.ZodType<TokenListResponse> = z.array(\n z.object({\n address: EvmAddressSchema,\n decimals: z.number().int().nonnegative().max(18),\n logo_url: z.url(),\n name: z.string(),\n symbol: z.string(),\n }),\n);\n\nexport interface QuoteResponseData {\n aggregator: {\n id: string;\n logo_url: string;\n name: string;\n };\n amountIn: bigint;\n amountOut: bigint;\n /** Chain identifier (number for EVM chains, CAIP-2 ID for SVM chains) */\n chainId: number | Caip2ChainId;\n expiredAt: number;\n /**\n * Fee breakdown for cross-chain bridge transactions.\n *\n * Only included in quotes from cross-chain aggregators (DeBridge, LiFi).\n */\n fees?: ReadonlyArray<{\n type: 'protocol' | 'gas' | 'bridge' | 'slippage' | 'swap' | 'other' | 'relay';\n /** Human-readable fee name */\n name: string;\n /** Fee amount in token's smallest unit */\n amount: bigint;\n /**\n * When `true`, indicates the fee is an additional fee on top of the input\n * amount that the user needs to pay. These fees need checked against the user's\n * balance to prevent insufficient balance errors.\n */\n extra?: boolean;\n /** Token the fee is deducted from */\n token: {\n /** Chain ID where fee is deducted */\n chainId: number | Caip2ChainId;\n /** Token address (EVM hex or Solana base58) */\n address: EvmAddress | SolAddress;\n };\n }>;\n gasEstimate?: bigint;\n recommendedSlippage: number;\n /** Input token address (EVM hex or Solana base58) */\n tokenIn: EvmAddress | SolAddress;\n tokenInDecimals: number;\n /** Output token address (EVM hex or Solana base58) */\n tokenOut: EvmAddress | SolAddress;\n tokenOutDecimals: number;\n uuid: string;\n}\n\nexport interface QuoteResponseDataDone {\n done: true;\n}\n\nexport type QuoteResponse = QuoteResponseData | QuoteResponseDataDone;\n\nconst MarkrQuoteTokenAddressSchema = z.union([EvmAddressSchema, SolAddressSchema, HyperliquidAddressSchema]);\n\n/**\n * Schema for the response from Markr's /quote endpoint.\n *\n * @see https://orchestrator-docs.markr.io/#/paths/~1quote/post\n */\nexport const QuoteResponseSchema: z.ZodType<QuoteResponse> = z.union([\n z.object({\n aggregator: z.object({\n id: z.string(),\n logo_url: z.url(),\n name: z.string(),\n }),\n amountIn: z.coerce.bigint().nonnegative(),\n amountOut: z.coerce.bigint().nonnegative(),\n chainId: z.union([z.number().int().nonnegative(), Caip2ChainIdSchema]),\n /**\n * Unix time in seconds when the quote expires.\n */\n expiredAt: z.number().int().nonnegative(),\n fees: z\n .array(\n z.object({\n type: z.enum(['protocol', 'gas', 'bridge', 'slippage', 'swap', 'other', 'relay']),\n name: z.string(),\n amount: z.coerce.bigint().nonnegative(),\n token: z.object({\n chainId: z.union([z.number().int().nonnegative(), Caip2ChainIdSchema]),\n address: MarkrQuoteTokenAddressSchema,\n }),\n extra: z.boolean().optional(),\n }),\n )\n .optional(),\n /**\n * Estimated gas for the swap transaction.\n * Markr estimates already include a buffer.\n *\n * The API docs do not specify this field as optional, but in practice it can be missing.\n */\n gasEstimate: z.coerce.bigint().nonnegative().optional(),\n /** Recommended slippage in basis points. */\n recommendedSlippage: z.number().int().nonnegative(),\n tokenIn: MarkrQuoteTokenAddressSchema,\n tokenInDecimals: z.number().int().nonnegative().max(18),\n tokenOut: MarkrQuoteTokenAddressSchema,\n tokenOutDecimals: z.number().int().nonnegative().max(18),\n uuid: z.uuid(),\n }),\n z.object({\n done: z.literal(true),\n }),\n]);\n\n/**\n * Response type for Markr's /swap endpoint.\n *\n * @see https://orchestrator-docs.markr.io/#/paths/~1swap/post\n */\nexport interface WrappedSwapTransactionResponse {\n data: Hex;\n to: EvmAddress;\n value: bigint;\n}\n\nexport const WrappedSwapTransactionResponseSchema: z.ZodType<WrappedSwapTransactionResponse> = z.object({\n data: HexSchema,\n to: EvmAddressSchema,\n value: z.coerce.bigint().nonnegative(),\n});\n\n/**\n * Solana swap transaction (returned for SVM chains).\n */\nexport interface SolanaSwapTransactionResponse {\n /** Chain type identifier */\n chainType: 'svm';\n /** Base64-encoded Solana transaction to be signed and sent by the client */\n swapTransaction: string;\n}\n\nexport const SolanaSwapTransactionResponseSchema: z.ZodType<SolanaSwapTransactionResponse> = z.object({\n chainType: z.literal('svm'),\n swapTransaction: z.base64(),\n});\n\nexport type SwapResponse = WrappedSwapTransactionResponse | SolanaSwapTransactionResponse;\n\nexport const SwapResponseSchema: z.ZodType<SwapResponse> = z.union([\n WrappedSwapTransactionResponseSchema,\n SolanaSwapTransactionResponseSchema,\n]);\n\nexport interface PartnerInfoResponse {\n /**\n * The partner fee in basis points collected on each swap.\n */\n fee: number;\n name: string;\n}\n\nexport const PartnerInfoResponseSchema: z.ZodType<PartnerInfoResponse> = z.object({\n fee: z.int().nonnegative(),\n name: z.string(),\n});\n\n/**\n * If the destination token is USDC, the flow is:\n * - pending -> committed -> completed\n *\n * If the destination token is not USDC, the flow is:\n * - pending -> committed -> pending_execution -> completed\n *\n * If the transaction fails at any point, the status will be 'failed'.\n */\nexport type CrossChainStatus =\n | /** Transaction submitted to CCIP */\n 'pending'\n /** Message committed, awaiting execution on destination chain */\n | 'committed'\n /** Message received on destination, swap pending execution by relayer */\n | 'pending_execution'\n /** Message executed successfully on destination chain */\n | 'completed'\n /** Swap expired, and funds were refunded (CCIP only) */\n | 'refunded'\n /** Execution failed */\n | 'failed'\n /** Unknown state */\n | 'unknown';\n\nexport interface CrossChainStatusResponse {\n /**\n * Cross-chain transaction identifier.\n *\n * For CCIP: The CCIP message ID.\n * For DeBridge: The DeBridge order ID.\n */\n messageId: string | null;\n /** Current status of the cross-chain transaction. */\n status: CrossChainStatus;\n /** Human-readable status description */\n description: string;\n /** Source chain information */\n sourceChain: {\n /** Source chain network name */\n name: string;\n /** Source chain transaction hash */\n transactionHash: Hash | Signature;\n /** Transaction timestamp on source chain (ISO 8601) */\n timestamp: string;\n /** When the source transaction was finalized (ISO 8601) */\n finalized: string | boolean | null;\n };\n /** Destination chain information */\n destinationChain: {\n /** Destination chain network name */\n name: string | null;\n /**\n * Destination chain tx hash.\n *\n * - For direct transfers (USDC -> USDC): The CCIP bridge transaction hash.\n * - For successful swaps: The actual swap transaction hash from the relayer.\n * - For pending swaps: null (swap not yet executed).\n */\n transactionHash: Hash | Signature | null;\n /**\n * CCIP bridge tx hash (only present for swaps, not direct transfers).\n *\n * This is the transaction that delivered USDC to the destination chain via CCIP.\n * The `transactionHash` field contains the subsequent swap transaction.\n */\n bridgeHash?: Hash | Signature | null;\n /** Transaction timestamp on destination chain (null if not yet executed) (ISO 8601) */\n timestamp: string | null;\n /** When the destination transaction was finalized (null if not yet executed) (ISO 8601) */\n finalized: string | null;\n };\n /** Transaction progress information */\n progress: {\n /** Whether the message has been committed on destination chain */\n committed: boolean;\n /** When the message was committed (null if not committed) (ISO 8601) */\n commitTimestamp?: string | null;\n /** Whether the message has been executed on destination chain */\n executed: boolean;\n };\n /** Fee information */\n fees: {\n /** Fee token address */\n token: EvmAddress | SolAddress | null;\n /** Fee amount in wei */\n amount: bigint | null;\n } | null;\n /** Tokens transferred in this cross-chain transaction */\n transferredTokens: ReadonlyArray<{\n /** Token address */\n token: EvmAddress | SolAddress;\n /** Token amount */\n amount: bigint;\n }>;\n /**\n * Debug information from Markr transaction API (only present for swaps that have been executed).\n *\n * Contains detailed information about the relayer execution, retry attempts, and transaction details.\n */\n debug?: Partial<{\n /** CCIP message ID */\n messageId: string;\n /** Execution status from relayer */\n status: string;\n /** Destination chain name */\n destinationChain: string;\n /** Destination swap transaction hash */\n destinationTxHash: string;\n /** Relay transaction hash */\n relayTxHash: string;\n /** Number of retry attempts */\n retryCount: number;\n /** Timestamp of last retry attempt (ISO 8601) */\n lastRetryAt: string;\n /** Source chain name */\n sourceChain: string;\n /** Source transaction hash */\n sourceTxHash: string;\n /** When the record was created (ISO 8601) */\n createdAt: string;\n }> | null;\n}\n\nconst ISO_8601_TIMEZONE_DESIGNATOR_REGEX = /[zZ]|[+-]\\d{2}:?\\d{2}$/;\n\n// Markr API returns ISO-8601 datetime strings that are sometimes missing a timezone designator (\"local\" ISO).\n// We assume these are meant to be UTC and append 'Z' if missing, then validate as strict ISO-8601 with timezone.\nconst datetime = z.iso\n .datetime({ local: true })\n .refine(\n (value) => {\n // Accept at least local ISO format\n // If missing timezone, treat as UTC\n const hasTimezone = ISO_8601_TIMEZONE_DESIGNATOR_REGEX.test(value);\n const isoString = hasTimezone ? value : `${value}Z`;\n // Validate as strict ISO-8601 with timezone\n return z.iso.datetime().safeParse(isoString).success;\n },\n {\n error: 'Invalid ISO-8601 datetime (must be valid with timezone, or local assumed UTC)',\n },\n )\n .transform((value) => {\n const hasTimezone = ISO_8601_TIMEZONE_DESIGNATOR_REGEX.test(value);\n return hasTimezone ? value : `${value}Z`;\n });\n\nexport const CrossChainStatusResponseSchema: z.ZodType<CrossChainStatusResponse> = z.object({\n messageId: z.string().nullable(),\n status: z.enum(['pending', 'committed', 'pending_execution', 'completed', 'failed', 'refunded', 'unknown']),\n description: z.string(),\n sourceChain: z.object({\n name: z.string(),\n transactionHash: z.union([HashSchema, SolSignatureSchema]),\n timestamp: datetime,\n finalized: datetime.or(z.boolean()).nullable(),\n }),\n destinationChain: z.object({\n name: z.string().nullable(),\n transactionHash: z.union([HashSchema, SolSignatureSchema]).nullable(),\n bridgeHash: z.union([HashSchema, SolSignatureSchema]).nullable().optional(),\n timestamp: datetime.nullable(),\n finalized: datetime.nullable(),\n }),\n progress: z.object({\n committed: z.boolean(),\n commitTimestamp: datetime.nullable().optional(),\n executed: z.boolean(),\n }),\n fees: z\n .object({\n token: z.union([EvmAddressSchema, SolAddressSchema]).nullable(),\n amount: z.coerce.bigint().nonnegative().nullable(),\n })\n .nullable(),\n transferredTokens: z.array(\n z.object({\n token: z.union([EvmAddressSchema, SolAddressSchema]),\n amount: z.coerce.bigint().nonnegative(),\n }),\n ),\n debug: z\n .looseObject({\n messageId: z.string(),\n status: z.string(),\n destinationChain: z.string(),\n destinationTxHash: z.string(),\n relayTxHash: z.string(),\n retryCount: z.number().int().nonnegative(),\n lastRetryAt: z.iso.datetime(),\n sourceChain: z.string(),\n sourceTxHash: z.string(),\n createdAt: z.iso.datetime(),\n })\n .partial()\n .nullable()\n .optional(),\n});\n\nexport interface SpenderAddressResponse {\n address: EvmAddress;\n}\n\nexport const SpenderAddressResponseSchema: z.ZodType<SpenderAddressResponse> = z.object({\n address: EvmAddressSchema,\n});\n"],"mappings":"iKAsDA,MAAa,EAA8CA,EAAAA,EACxD,QAAQ,CACR,OAAQ,GAAUC,EAAAA,eAAe,EAAM,CAAE,CACxC,QAAS,0BACV,CAAC,CACD,UAAW,GAAU,EAAsB,CAExC,EAAwCD,EAAAA,EAAE,OAAO,CACrD,QAASA,EAAAA,EAAE,MAAM,CAACA,EAAAA,EAAE,QAAQ,CAAEA,EAAAA,EAAE,QAAQ,CAAC,CAAC,CAC1C,UAAWA,EAAAA,EAAE,QAAQ,CACrB,iBAAkBA,EAAAA,EAAE,MAAMA,EAAAA,EAAE,KAAK,CAAC,mBAAoB,oBAAqB,QAAS,OAAQ,aAAa,CAAC,CAAC,CAC3G,SAAUA,EAAAA,EAAE,KAAK,CACjB,KAAMA,EAAAA,EAAE,QAAQ,CAChB,MAAOA,EAAAA,EAAE,MAAMA,EAAAA,EAAE,MAAM,CAACA,EAAAA,EAAE,OAAO,QAAQ,CAAC,KAAK,CAAC,aAAa,CAAE,EAAmB,CAAC,CAAC,CACrF,CAAC,CAEI,EACJ,EAAsC,OAAO,CAC3C,QAASA,EAAAA,EAAE,OAAO,QAAQ,CAAC,KAAK,CAAC,aAAa,CAC9C,UAAWA,EAAAA,EAAE,QAAQ,MAAM,CAC3B,OAAQE,EAAAA,iBAAiB,UAAU,CACnC,cAAeA,EAAAA,iBAAiB,UAAU,CAC3C,CAAC,CAEE,EACJ,EAAsC,OAAO,CAC3C,QAASF,EAAAA,EACN,QAAQ,CACR,OAA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ACF,KAAMA,EAAAA,EACH,OAAO,CACN,MAAOA,EAAAA,EAAE,MAAM,CAACE,EAAAA,iBAAkBC,EAAAA,iBAAiB,CAAC,CAAC,UAAU,CAC/D,OAAQH,EAAAA,EAAE,OAAO,QAAQ,CAAC,aAAa,CAAC,UAAU,CACnD,CAAC,CACD,UAAU,CACb,kBAAmBA,EAAAA,EAAE,MACnBA,EAAAA,EAAE,OAAO,CACP,MAAOA,EAAAA,EAAE,MAAM,CAACE,EAAAA,iBAAkBC,EAAAA,iBAAiB,CAAC,CACpD,OAAQH,EAAAA,EAAE,OAAO,QAAQ,CAAC,aAAa,CACxC,CAAC,CACH,CACD,MAAOA,EAAAA,EACJ,YAAY,CACX,UAAWA,EAAAA,EAAE,QAAQ,CACrB,OAAQA,EAAAA,EAAE,QAAQ,CAClB,iBAAkBA,EAAAA,EAAE,QAAQ,CAC5B,kBAAmBA,EAAAA,EAAE,QAAQ,CAC7B,YAAaA,EAAAA,EAAE,QAAQ,CACvB,WAAYA,EAAAA,EAAE,QAAQ,CAAC,KAAK,CAAC,aAAa,CAC1C,YAAaA,EAAAA,EAAE,IAAI,UAAU,CAC7B,YAAaA,EAAAA,EAAE,QAAQ,CACvB,aAAcA,EAAAA,EAAE,QAAQ,CACxB,UAAWA,EAAAA,EAAE,IAAI,UAAU,CAC5B,CAAC,CACD,SAAS,CACT,UAAU,CACV,UAAU,CACd,CAAC,CAMW,EAAkEA,EAAAA,EAAE,OAAO,CACtF,QAASE,EAAAA,iBACV,CAAC"}
@@ -1,2 +1,2 @@
1
- import{isCaip2ChainId as e}from"../../utils/caip.js";import{EvmAddressSchema as t,HashSchema as n,HexSchema as r,SolAddressSchema as i,SolSignatureSchema as a}from"../service-schemas.js";import{z as o}from"zod";const s=o.string().refine(t=>e(t),{message:`Invalid CAIP-2 chain ID`}).transform(e=>e),c=o.object({chainId:o.union([o.number(),o.string()]),chainType:o.string(),enabled_services:o.array(o.enum([`cross-chain-swap`,`cross-chain-quote`,`quote`,`swap`,`token-list`])),logo_url:o.url(),name:o.string(),lanes:o.array(o.union([o.coerce.number().int().nonnegative(),s]))}),l=c.extend({chainId:o.coerce.number().int().nonnegative(),chainType:o.literal(`evm`),router:t.optional(),wrapped_token:t.optional()}),u=c.extend({chainId:o.string().refine(t=>e(t)&&t.startsWith(`solana:`),{error:`Is not a valid Solana CAIP-2 ID`}).transform(e=>e),chainType:o.literal(`svm`)}),d=o.array(o.union([l,u,c.refine(e=>e.chainType!==`evm`&&e.chainType!==`svm`,`Known chain types must match their expected schema`)])),f=o.array(o.object({address:t,decimals:o.number().int().nonnegative().max(18),logo_url:o.url(),name:o.string(),symbol:o.string()})),p=o.union([o.object({aggregator:o.object({id:o.string(),logo_url:o.url(),name:o.string()}),amountIn:o.coerce.bigint().nonnegative(),amountOut:o.coerce.bigint().nonnegative(),chainId:o.union([o.number().int().nonnegative(),s]),expiredAt:o.number().int().nonnegative(),fees:o.array(o.object({type:o.enum([`protocol`,`gas`,`bridge`,`slippage`,`swap`,`other`]),name:o.string(),amount:o.coerce.bigint().nonnegative(),token:o.object({chainId:o.union([o.number().int().nonnegative(),s]),address:o.union([t,i])}),extra:o.boolean().optional()})).optional(),gasEstimate:o.coerce.bigint().nonnegative().optional(),recommendedSlippage:o.number().int().nonnegative(),tokenIn:o.union([t,i]),tokenInDecimals:o.number().int().nonnegative().max(18),tokenOut:o.union([t,i]),tokenOutDecimals:o.number().int().nonnegative().max(18),uuid:o.uuid()}),o.object({done:o.literal(!0)})]),m=o.object({data:r,to:t,value:o.coerce.bigint().nonnegative()}),h=o.object({chainType:o.literal(`svm`),swapTransaction:o.base64()}),g=o.union([m,h]),_=o.object({fee:o.int().nonnegative(),name:o.string()}),v=/[zZ]|[+-]\d{2}:?\d{2}$/,y=o.iso.datetime({local:!0}).refine(e=>{let t=v.test(e)?e:e+`Z`;return o.iso.datetime().safeParse(t).success},{error:`Invalid ISO-8601 datetime (must be valid with timezone, or local assumed UTC)`}).transform(e=>v.test(e)?e:e+`Z`),b=o.object({messageId:o.string().nullable(),status:o.enum([`pending`,`committed`,`pending_execution`,`completed`,`failed`,`refunded`,`unknown`]),description:o.string(),sourceChain:o.object({name:o.string(),transactionHash:o.union([n,a]),timestamp:y,finalized:y.or(o.boolean()).nullable()}),destinationChain:o.object({name:o.string().nullable(),transactionHash:o.union([n,a]).nullable(),bridgeHash:o.union([n,a]).nullable().optional(),timestamp:y.nullable(),finalized:y.nullable()}),progress:o.object({committed:o.boolean(),commitTimestamp:y.nullable().optional(),executed:o.boolean()}),fees:o.object({token:o.union([t,i]).nullable(),amount:o.coerce.bigint().nonnegative().nullable()}).nullable(),transferredTokens:o.array(o.object({token:o.union([t,i]),amount:o.coerce.bigint().nonnegative()})),debug:o.looseObject({messageId:o.string(),status:o.string(),destinationChain:o.string(),destinationTxHash:o.string(),relayTxHash:o.string(),retryCount:o.number().int().nonnegative(),lastRetryAt:o.iso.datetime(),sourceChain:o.string(),sourceTxHash:o.string(),createdAt:o.iso.datetime()}).partial().nullable().optional()}),x=o.object({address:t});export{b as CrossChainStatusResponseSchema,_ as PartnerInfoResponseSchema,p as QuoteResponseSchema,x as SpenderAddressResponseSchema,d as SupportedChainsResponseSchema,g as SwapResponseSchema,f as TokenListResponseSchema};
1
+ import{isCaip2ChainId as e}from"../../utils/caip.js";import{EvmAddressSchema as t,HashSchema as n,HexSchema as r,HyperliquidAddressSchema as i,SolAddressSchema as a,SolSignatureSchema as o}from"../service-schemas.js";import{z as s}from"zod";import"viem";const c=s.string().refine(t=>e(t),{message:`Invalid CAIP-2 chain ID`}).transform(e=>e),l=s.object({chainId:s.union([s.number(),s.string()]),chainType:s.string(),enabled_services:s.array(s.enum([`cross-chain-swap`,`cross-chain-quote`,`quote`,`swap`,`token-list`])),logo_url:s.url(),name:s.string(),lanes:s.array(s.union([s.coerce.number().int().nonnegative(),c]))}),u=l.extend({chainId:s.coerce.number().int().nonnegative(),chainType:s.literal(`evm`),router:t.optional(),wrapped_token:t.optional()}),d=l.extend({chainId:s.string().refine(t=>e(t)&&t.startsWith(`solana:`),{error:`Is not a valid Solana CAIP-2 ID`}).transform(e=>e),chainType:s.literal(`svm`)}),f=s.array(s.union([u,d,l.refine(e=>e.chainType!==`evm`&&e.chainType!==`svm`,`Known chain types must match their expected schema`)])),p=s.array(s.object({address:t,decimals:s.number().int().nonnegative().max(18),logo_url:s.url(),name:s.string(),symbol:s.string()})),m=s.union([t,a,i]),h=s.union([s.object({aggregator:s.object({id:s.string(),logo_url:s.url(),name:s.string()}),amountIn:s.coerce.bigint().nonnegative(),amountOut:s.coerce.bigint().nonnegative(),chainId:s.union([s.number().int().nonnegative(),c]),expiredAt:s.number().int().nonnegative(),fees:s.array(s.object({type:s.enum([`protocol`,`gas`,`bridge`,`slippage`,`swap`,`other`,`relay`]),name:s.string(),amount:s.coerce.bigint().nonnegative(),token:s.object({chainId:s.union([s.number().int().nonnegative(),c]),address:m}),extra:s.boolean().optional()})).optional(),gasEstimate:s.coerce.bigint().nonnegative().optional(),recommendedSlippage:s.number().int().nonnegative(),tokenIn:m,tokenInDecimals:s.number().int().nonnegative().max(18),tokenOut:m,tokenOutDecimals:s.number().int().nonnegative().max(18),uuid:s.uuid()}),s.object({done:s.literal(!0)})]),g=s.object({data:r,to:t,value:s.coerce.bigint().nonnegative()}),_=s.object({chainType:s.literal(`svm`),swapTransaction:s.base64()}),v=s.union([g,_]),y=s.object({fee:s.int().nonnegative(),name:s.string()}),b=/[zZ]|[+-]\d{2}:?\d{2}$/,x=s.iso.datetime({local:!0}).refine(e=>{let t=b.test(e)?e:`${e}Z`;return s.iso.datetime().safeParse(t).success},{error:`Invalid ISO-8601 datetime (must be valid with timezone, or local assumed UTC)`}).transform(e=>b.test(e)?e:`${e}Z`),S=s.object({messageId:s.string().nullable(),status:s.enum([`pending`,`committed`,`pending_execution`,`completed`,`failed`,`refunded`,`unknown`]),description:s.string(),sourceChain:s.object({name:s.string(),transactionHash:s.union([n,o]),timestamp:x,finalized:x.or(s.boolean()).nullable()}),destinationChain:s.object({name:s.string().nullable(),transactionHash:s.union([n,o]).nullable(),bridgeHash:s.union([n,o]).nullable().optional(),timestamp:x.nullable(),finalized:x.nullable()}),progress:s.object({committed:s.boolean(),commitTimestamp:x.nullable().optional(),executed:s.boolean()}),fees:s.object({token:s.union([t,a]).nullable(),amount:s.coerce.bigint().nonnegative().nullable()}).nullable(),transferredTokens:s.array(s.object({token:s.union([t,a]),amount:s.coerce.bigint().nonnegative()})),debug:s.looseObject({messageId:s.string(),status:s.string(),destinationChain:s.string(),destinationTxHash:s.string(),relayTxHash:s.string(),retryCount:s.number().int().nonnegative(),lastRetryAt:s.iso.datetime(),sourceChain:s.string(),sourceTxHash:s.string(),createdAt:s.iso.datetime()}).partial().nullable().optional()}),C=s.object({address:t});export{S as CrossChainStatusResponseSchema,y as PartnerInfoResponseSchema,h as QuoteResponseSchema,C as SpenderAddressResponseSchema,f as SupportedChainsResponseSchema,v as SwapResponseSchema,p as TokenListResponseSchema};
2
2
  //# sourceMappingURL=_schema.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"_schema.js","names":[],"sources":["../../../src/transfer-service/markr/_schema.ts"],"sourcesContent":["import type { Signature, Address as SolAddress } from '@solana/kit';\nimport { z } from 'zod';\nimport type { Address as EvmAddress, Hash } from 'viem';\nimport { EvmAddressSchema, HashSchema, HexSchema, SolAddressSchema, SolSignatureSchema } from '../service-schemas';\nimport { isCaip2ChainId } from '../../utils/caip';\nimport type { Caip2ChainId } from '../../types/caip';\nimport type { Hex } from '../../types/signer';\n\ninterface SupportedChainsResponseItemBase {\n chainId: number | string;\n chainType: string;\n /** Available services on the chain. Each service has specific functionality */\n enabled_services: Array<'cross-chain-swap' | 'cross-chain-quote' | 'quote' | 'swap' | 'token-list'>;\n /** Chain logo URL */\n logo_url: string;\n /** Chain display name */\n name: string;\n /**\n * List of chain IDs that this chain can swap to via cross-chain aggregators.\n *\n * Empty array means no cross-chain routes available.\n */\n lanes: ReadonlyArray<number | Caip2ChainId>;\n}\n\nexport interface SupportedChainsResponseItemEvm extends SupportedChainsResponseItemBase {\n /** Chain identifier */\n chainId: number;\n /** Chain virtual machine type */\n chainType: 'evm';\n /** Router contract address */\n router?: EvmAddress;\n /** Native token wrapped contract address */\n wrapped_token?: EvmAddress;\n}\n\nexport interface SupportedChainsResponseItemSvm extends SupportedChainsResponseItemBase {\n /** Chain identifier */\n chainId: Caip2ChainId;\n /** Chain virtual machine type */\n chainType: 'svm';\n}\n\nexport type SupportedChainsResponse = Array<\n SupportedChainsResponseItemEvm | SupportedChainsResponseItemSvm | SupportedChainsResponseItemBase\n>;\n\nexport const Caip2ChainIdSchema: z.ZodType<Caip2ChainId> = z\n .string()\n .refine((value) => isCaip2ChainId(value), {\n message: 'Invalid CAIP-2 chain ID',\n })\n .transform((value) => value as Caip2ChainId);\n\nconst SupportedChainsResponseItemBaseSchema = z.object({\n chainId: z.union([z.number(), z.string()]),\n chainType: z.string(),\n enabled_services: z.array(z.enum(['cross-chain-swap', 'cross-chain-quote', 'quote', 'swap', 'token-list'])),\n logo_url: z.url(),\n name: z.string(),\n lanes: z.array(z.union([z.coerce.number().int().nonnegative(), Caip2ChainIdSchema])),\n});\n\nconst SupportedChainsResponseItemEvmSchema: z.ZodType<SupportedChainsResponseItemEvm> =\n SupportedChainsResponseItemBaseSchema.extend({\n chainId: z.coerce.number().int().nonnegative(),\n chainType: z.literal('evm'),\n router: EvmAddressSchema.optional(),\n wrapped_token: EvmAddressSchema.optional(),\n });\n\nconst SupportedChainsResponseItemSvmSchema: z.ZodType<SupportedChainsResponseItemSvm> =\n SupportedChainsResponseItemBaseSchema.extend({\n chainId: z\n .string()\n .refine((value) => isCaip2ChainId(value) && value.startsWith('solana:'), {\n error: 'Is not a valid Solana CAIP-2 ID',\n })\n .transform((value) => value as Caip2ChainId),\n chainType: z.literal('svm'),\n });\n\n/**\n * Schema for the response from Markr's /info/chains endpoint.\n *\n * @see https://orchestrator-docs.markr.io/#/paths/~1info~1chains/get\n */\nexport const SupportedChainsResponseSchema: z.ZodType<SupportedChainsResponse> = z.array(\n z.union([\n SupportedChainsResponseItemEvmSchema,\n SupportedChainsResponseItemSvmSchema,\n SupportedChainsResponseItemBaseSchema.refine(\n (item) => item.chainType !== 'evm' && item.chainType !== 'svm',\n 'Known chain types must match their expected schema',\n ),\n ]),\n);\n\nexport type TokenListResponse = Array<{\n address: EvmAddress;\n decimals: number;\n logo_url: string;\n name: string;\n symbol: string;\n}>;\n\n/**\n * Schema for the response Markr's /tokens/{chainId}/list endpoint.\n *\n * @see https://orchestrator-docs.markr.io/#/paths/~1tokens~1%7BchainId%7D~1list/get\n */\nexport const TokenListResponseSchema: z.ZodType<TokenListResponse> = z.array(\n z.object({\n address: EvmAddressSchema,\n decimals: z.number().int().nonnegative().max(18),\n logo_url: z.url(),\n name: z.string(),\n symbol: z.string(),\n }),\n);\n\nexport interface QuoteResponseData {\n aggregator: {\n id: string;\n logo_url: string;\n name: string;\n };\n amountIn: bigint;\n amountOut: bigint;\n /** Chain identifier (number for EVM chains, CAIP-2 ID for SVM chains) */\n chainId: number | Caip2ChainId;\n expiredAt: number;\n /**\n * Fee breakdown for cross-chain bridge transactions.\n *\n * Only included in quotes from cross-chain aggregators (DeBridge, LiFi).\n */\n fees?: ReadonlyArray<{\n type: 'protocol' | 'gas' | 'bridge' | 'slippage' | 'swap' | 'other';\n /** Human-readable fee name */\n name: string;\n /** Fee amount in token's smallest unit */\n amount: bigint;\n /**\n * When `true`, indicates the fee is an additional fee on top of the input\n * amount that the user needs to pay. These fees need checked against the user's\n * balance to prevent insufficient balance errors.\n */\n extra?: boolean;\n /** Token the fee is deducted from */\n token: {\n /** Chain ID where fee is deducted */\n chainId: number | Caip2ChainId;\n /** Token address (EVM hex or Solana base58) */\n address: EvmAddress | SolAddress;\n };\n }>;\n gasEstimate?: bigint;\n recommendedSlippage: number;\n /** Input token address (EVM hex or Solana base58) */\n tokenIn: EvmAddress | SolAddress;\n tokenInDecimals: number;\n /** Output token address (EVM hex or Solana base58) */\n tokenOut: EvmAddress | SolAddress;\n tokenOutDecimals: number;\n uuid: string;\n}\n\nexport interface QuoteResponseDataDone {\n done: true;\n}\n\nexport type QuoteResponse = QuoteResponseData | QuoteResponseDataDone;\n\n/**\n * Schema for the response from Markr's /quote endpoint.\n *\n * @see https://orchestrator-docs.markr.io/#/paths/~1quote/post\n */\nexport const QuoteResponseSchema: z.ZodType<QuoteResponse> = z.union([\n z.object({\n aggregator: z.object({\n id: z.string(),\n logo_url: z.url(),\n name: z.string(),\n }),\n amountIn: z.coerce.bigint().nonnegative(),\n amountOut: z.coerce.bigint().nonnegative(),\n chainId: z.union([z.number().int().nonnegative(), Caip2ChainIdSchema]),\n /**\n * Unix time in seconds when the quote expires.\n */\n expiredAt: z.number().int().nonnegative(),\n fees: z\n .array(\n z.object({\n type: z.enum(['protocol', 'gas', 'bridge', 'slippage', 'swap', 'other']),\n name: z.string(),\n amount: z.coerce.bigint().nonnegative(),\n token: z.object({\n chainId: z.union([z.number().int().nonnegative(), Caip2ChainIdSchema]),\n address: z.union([EvmAddressSchema, SolAddressSchema]),\n }),\n extra: z.boolean().optional(),\n }),\n )\n .optional(),\n /**\n * Estimated gas for the swap transaction.\n * Markr estimates already include a buffer.\n *\n * The API docs do not specify this field as optional, but in practice it can be missing.\n */\n gasEstimate: z.coerce.bigint().nonnegative().optional(),\n /** Recommended slippage in basis points. */\n recommendedSlippage: z.number().int().nonnegative(),\n tokenIn: z.union([EvmAddressSchema, SolAddressSchema]),\n tokenInDecimals: z.number().int().nonnegative().max(18),\n tokenOut: z.union([EvmAddressSchema, SolAddressSchema]),\n tokenOutDecimals: z.number().int().nonnegative().max(18),\n uuid: z.uuid(),\n }),\n z.object({\n done: z.literal(true),\n }),\n]);\n\n/**\n * Response type for Markr's /swap endpoint.\n *\n * @see https://orchestrator-docs.markr.io/#/paths/~1swap/post\n */\nexport interface WrappedSwapTransactionResponse {\n data: Hex;\n to: EvmAddress;\n value: bigint;\n}\n\nexport const WrappedSwapTransactionResponseSchema: z.ZodType<WrappedSwapTransactionResponse> = z.object({\n data: HexSchema,\n to: EvmAddressSchema,\n value: z.coerce.bigint().nonnegative(),\n});\n\n/**\n * Solana swap transaction (returned for SVM chains).\n */\nexport interface SolanaSwapTransactionResponse {\n /** Chain type identifier */\n chainType: 'svm';\n /** Base64-encoded Solana transaction to be signed and sent by the client */\n swapTransaction: string;\n}\n\nexport const SolanaSwapTransactionResponseSchema: z.ZodType<SolanaSwapTransactionResponse> = z.object({\n chainType: z.literal('svm'),\n swapTransaction: z.base64(),\n});\n\nexport type SwapResponse = WrappedSwapTransactionResponse | SolanaSwapTransactionResponse;\n\nexport const SwapResponseSchema: z.ZodType<SwapResponse> = z.union([\n WrappedSwapTransactionResponseSchema,\n SolanaSwapTransactionResponseSchema,\n]);\n\nexport interface PartnerInfoResponse {\n /**\n * The partner fee in basis points collected on each swap.\n */\n fee: number;\n name: string;\n}\n\nexport const PartnerInfoResponseSchema: z.ZodType<PartnerInfoResponse> = z.object({\n fee: z.int().nonnegative(),\n name: z.string(),\n});\n\n/**\n * If the destination token is USDC, the flow is:\n * - pending -> committed -> completed\n *\n * If the destination token is not USDC, the flow is:\n * - pending -> committed -> pending_execution -> completed\n *\n * If the transaction fails at any point, the status will be 'failed'.\n */\nexport type CrossChainStatus =\n | /** Transaction submitted to CCIP */\n 'pending'\n /** Message committed, awaiting execution on destination chain */\n | 'committed'\n /** Message received on destination, swap pending execution by relayer */\n | 'pending_execution'\n /** Message executed successfully on destination chain */\n | 'completed'\n /** Swap expired, and funds were refunded (CCIP only) */\n | 'refunded'\n /** Execution failed */\n | 'failed'\n /** Unknown state */\n | 'unknown';\n\nexport interface CrossChainStatusResponse {\n /**\n * Cross-chain transaction identifier.\n *\n * For CCIP: The CCIP message ID.\n * For DeBridge: The DeBridge order ID.\n */\n messageId: string | null;\n /** Current status of the cross-chain transaction. */\n status: CrossChainStatus;\n /** Human-readable status description */\n description: string;\n /** Source chain information */\n sourceChain: {\n /** Source chain network name */\n name: string;\n /** Source chain transaction hash */\n transactionHash: Hash | Signature;\n /** Transaction timestamp on source chain (ISO 8601) */\n timestamp: string;\n /** When the source transaction was finalized (ISO 8601) */\n finalized: string | boolean | null;\n };\n /** Destination chain information */\n destinationChain: {\n /** Destination chain network name */\n name: string | null;\n /**\n * Destination chain tx hash.\n *\n * - For direct transfers (USDC -> USDC): The CCIP bridge transaction hash.\n * - For successful swaps: The actual swap transaction hash from the relayer.\n * - For pending swaps: null (swap not yet executed).\n */\n transactionHash: Hash | Signature | null;\n /**\n * CCIP bridge tx hash (only present for swaps, not direct transfers).\n *\n * This is the transaction that delivered USDC to the destination chain via CCIP.\n * The `transactionHash` field contains the subsequent swap transaction.\n */\n bridgeHash?: Hash | Signature | null;\n /** Transaction timestamp on destination chain (null if not yet executed) (ISO 8601) */\n timestamp: string | null;\n /** When the destination transaction was finalized (null if not yet executed) (ISO 8601) */\n finalized: string | null;\n };\n /** Transaction progress information */\n progress: {\n /** Whether the message has been committed on destination chain */\n committed: boolean;\n /** When the message was committed (null if not committed) (ISO 8601) */\n commitTimestamp?: string | null;\n /** Whether the message has been executed on destination chain */\n executed: boolean;\n };\n /** Fee information */\n fees: {\n /** Fee token address */\n token: EvmAddress | SolAddress | null;\n /** Fee amount in wei */\n amount: bigint | null;\n } | null;\n /** Tokens transferred in this cross-chain transaction */\n transferredTokens: ReadonlyArray<{\n /** Token address */\n token: EvmAddress | SolAddress;\n /** Token amount */\n amount: bigint;\n }>;\n /**\n * Debug information from Markr transaction API (only present for swaps that have been executed).\n *\n * Contains detailed information about the relayer execution, retry attempts, and transaction details.\n */\n debug?: Partial<{\n /** CCIP message ID */\n messageId: string;\n /** Execution status from relayer */\n status: string;\n /** Destination chain name */\n destinationChain: string;\n /** Destination swap transaction hash */\n destinationTxHash: string;\n /** Relay transaction hash */\n relayTxHash: string;\n /** Number of retry attempts */\n retryCount: number;\n /** Timestamp of last retry attempt (ISO 8601) */\n lastRetryAt: string;\n /** Source chain name */\n sourceChain: string;\n /** Source transaction hash */\n sourceTxHash: string;\n /** When the record was created (ISO 8601) */\n createdAt: string;\n }> | null;\n}\n\nconst ISO_8601_TIMEZONE_DESIGNATOR_REGEX = /[zZ]|[+-]\\d{2}:?\\d{2}$/;\n\n// Markr API returns ISO-8601 datetime strings that are sometimes missing a timezone designator (\"local\" ISO).\n// We assume these are meant to be UTC and append 'Z' if missing, then validate as strict ISO-8601 with timezone.\nconst datetime = z.iso\n .datetime({ local: true })\n .refine(\n (value) => {\n // Accept at least local ISO format\n // If missing timezone, treat as UTC\n const hasTimezone = ISO_8601_TIMEZONE_DESIGNATOR_REGEX.test(value);\n const isoString = hasTimezone ? value : value + 'Z';\n // Validate as strict ISO-8601 with timezone\n return z.iso.datetime().safeParse(isoString).success;\n },\n {\n error: 'Invalid ISO-8601 datetime (must be valid with timezone, or local assumed UTC)',\n },\n )\n .transform((value) => {\n const hasTimezone = ISO_8601_TIMEZONE_DESIGNATOR_REGEX.test(value);\n return hasTimezone ? value : value + 'Z';\n });\n\nexport const CrossChainStatusResponseSchema: z.ZodType<CrossChainStatusResponse> = z.object({\n messageId: z.string().nullable(),\n status: z.enum(['pending', 'committed', 'pending_execution', 'completed', 'failed', 'refunded', 'unknown']),\n description: z.string(),\n sourceChain: z.object({\n name: z.string(),\n transactionHash: z.union([HashSchema, SolSignatureSchema]),\n timestamp: datetime,\n finalized: datetime.or(z.boolean()).nullable(),\n }),\n destinationChain: z.object({\n name: z.string().nullable(),\n transactionHash: z.union([HashSchema, SolSignatureSchema]).nullable(),\n bridgeHash: z.union([HashSchema, SolSignatureSchema]).nullable().optional(),\n timestamp: datetime.nullable(),\n finalized: datetime.nullable(),\n }),\n progress: z.object({\n committed: z.boolean(),\n commitTimestamp: datetime.nullable().optional(),\n executed: z.boolean(),\n }),\n fees: z\n .object({\n token: z.union([EvmAddressSchema, SolAddressSchema]).nullable(),\n amount: z.coerce.bigint().nonnegative().nullable(),\n })\n .nullable(),\n transferredTokens: z.array(\n z.object({\n token: z.union([EvmAddressSchema, SolAddressSchema]),\n amount: z.coerce.bigint().nonnegative(),\n }),\n ),\n debug: z\n .looseObject({\n messageId: z.string(),\n status: z.string(),\n destinationChain: z.string(),\n destinationTxHash: z.string(),\n relayTxHash: z.string(),\n retryCount: z.number().int().nonnegative(),\n lastRetryAt: z.iso.datetime(),\n sourceChain: z.string(),\n sourceTxHash: z.string(),\n createdAt: z.iso.datetime(),\n })\n .partial()\n .nullable()\n .optional(),\n});\n\nexport interface SpenderAddressResponse {\n address: EvmAddress;\n}\n\nexport const SpenderAddressResponseSchema: z.ZodType<SpenderAddressResponse> = z.object({\n address: EvmAddressSchema,\n});\n"],"mappings":"mNA+CA,MAAa,EAA8C,EACxD,QAAQ,CACR,OAAQ,GAAU,EAAe,EAAM,CAAE,CACxC,QAAS,0BACV,CAAC,CACD,UAAW,GAAU,EAAsB,CAExC,EAAwC,EAAE,OAAO,CACrD,QAAS,EAAE,MAAM,CAAC,EAAE,QAAQ,CAAE,EAAE,QAAQ,CAAC,CAAC,CAC1C,UAAW,EAAE,QAAQ,CACrB,iBAAkB,EAAE,MAAM,EAAE,KAAK,CAAC,mBAAoB,oBAAqB,QAAS,OAAQ,aAAa,CAAC,CAAC,CAC3G,SAAU,EAAE,KAAK,CACjB,KAAM,EAAE,QAAQ,CAChB,MAAO,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,OAAO,QAAQ,CAAC,KAAK,CAAC,aAAa,CAAE,EAAmB,CAAC,CAAC,CACrF,CAAC,CAEI,EACJ,EAAsC,OAAO,CAC3C,QAAS,EAAE,OAAO,QAAQ,CAAC,KAAK,CAAC,aAAa,CAC9C,UAAW,EAAE,QAAQ,MAAM,CAC3B,OAAQ,EAAiB,UAAU,CACnC,cAAe,EAAiB,UAAU,CAC3C,CAAC,CAEE,EACJ,EAAsC,OAAO,CAC3C,QAAS,EACN,QAAQ,CACR,OAAQ,GAAU,EAAe,EAAM,EAAI,EAAM,WAAW,UAAU,CAAE,CACvE,MAAO,k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UAAU,CACV,UAAU,CACd,CAAC,CAMW,EAAkE,EAAE,OAAO,CACtF,QAAS,EACV,CAAC"}
1
+ {"version":3,"file":"_schema.js","names":[],"sources":["../../../src/transfer-service/markr/_schema.ts"],"sourcesContent":["import type { Signature, Address as SolAddress } from '@solana/kit';\nimport { z } from 'zod';\nimport { type Address as EvmAddress, type Hash } from 'viem';\nimport {\n EvmAddressSchema,\n HashSchema,\n HexSchema,\n HyperliquidAddressSchema,\n SolAddressSchema,\n SolSignatureSchema,\n} from '../service-schemas';\nimport { isCaip2ChainId } from '../../utils/caip';\nimport type { Caip2ChainId } from '../../types/caip';\nimport type { Hex } from '../../types/signer';\n\ninterface SupportedChainsResponseItemBase {\n chainId: number | string;\n chainType: string;\n /** Available services on the chain. Each service has specific functionality */\n enabled_services: Array<'cross-chain-swap' | 'cross-chain-quote' | 'quote' | 'swap' | 'token-list'>;\n /** Chain logo URL */\n logo_url: string;\n /** Chain display name */\n name: string;\n /**\n * List of chain IDs that this chain can swap to via cross-chain aggregators.\n *\n * Empty array means no cross-chain routes available.\n */\n lanes: ReadonlyArray<number | Caip2ChainId>;\n}\n\nexport interface SupportedChainsResponseItemEvm extends SupportedChainsResponseItemBase {\n /** Chain identifier */\n chainId: number;\n /** Chain virtual machine type */\n chainType: 'evm';\n /** Router contract address */\n router?: EvmAddress;\n /** Native token wrapped contract address */\n wrapped_token?: EvmAddress;\n}\n\nexport interface SupportedChainsResponseItemSvm extends SupportedChainsResponseItemBase {\n /** Chain identifier */\n chainId: Caip2ChainId;\n /** Chain virtual machine type */\n chainType: 'svm';\n}\n\nexport type SupportedChainsResponse = Array<\n SupportedChainsResponseItemEvm | SupportedChainsResponseItemSvm | SupportedChainsResponseItemBase\n>;\n\nexport const Caip2ChainIdSchema: z.ZodType<Caip2ChainId> = z\n .string()\n .refine((value) => isCaip2ChainId(value), {\n message: 'Invalid CAIP-2 chain ID',\n })\n .transform((value) => value as Caip2ChainId);\n\nconst SupportedChainsResponseItemBaseSchema = z.object({\n chainId: z.union([z.number(), z.string()]),\n chainType: z.string(),\n enabled_services: z.array(z.enum(['cross-chain-swap', 'cross-chain-quote', 'quote', 'swap', 'token-list'])),\n logo_url: z.url(),\n name: z.string(),\n lanes: z.array(z.union([z.coerce.number().int().nonnegative(), Caip2ChainIdSchema])),\n});\n\nconst SupportedChainsResponseItemEvmSchema: z.ZodType<SupportedChainsResponseItemEvm> =\n SupportedChainsResponseItemBaseSchema.extend({\n chainId: z.coerce.number().int().nonnegative(),\n chainType: z.literal('evm'),\n router: EvmAddressSchema.optional(),\n wrapped_token: EvmAddressSchema.optional(),\n });\n\nconst SupportedChainsResponseItemSvmSchema: z.ZodType<SupportedChainsResponseItemSvm> =\n SupportedChainsResponseItemBaseSchema.extend({\n chainId: z\n .string()\n .refine((value) => isCaip2ChainId(value) && value.startsWith('solana:'), {\n error: 'Is not a valid Solana CAIP-2 ID',\n })\n .transform((value) => value as Caip2ChainId),\n chainType: z.literal('svm'),\n });\n\n/**\n * Schema for the response from Markr's /info/chains endpoint.\n *\n * @see https://orchestrator-docs.markr.io/#/paths/~1info~1chains/get\n */\nexport const SupportedChainsResponseSchema: z.ZodType<SupportedChainsResponse> = z.array(\n z.union([\n SupportedChainsResponseItemEvmSchema,\n SupportedChainsResponseItemSvmSchema,\n SupportedChainsResponseItemBaseSchema.refine(\n (item) => item.chainType !== 'evm' && item.chainType !== 'svm',\n 'Known chain types must match their expected schema',\n ),\n ]),\n);\n\nexport type TokenListResponse = Array<{\n address: EvmAddress;\n decimals: number;\n logo_url: string;\n name: string;\n symbol: string;\n}>;\n\n/**\n * Schema for the response Markr's /tokens/{chainId}/list endpoint.\n *\n * @see https://orchestrator-docs.markr.io/#/paths/~1tokens~1%7BchainId%7D~1list/get\n */\nexport const TokenListResponseSchema: z.ZodType<TokenListResponse> = z.array(\n z.object({\n address: EvmAddressSchema,\n decimals: z.number().int().nonnegative().max(18),\n logo_url: z.url(),\n name: z.string(),\n symbol: z.string(),\n }),\n);\n\nexport interface QuoteResponseData {\n aggregator: {\n id: string;\n logo_url: string;\n name: string;\n };\n amountIn: bigint;\n amountOut: bigint;\n /** Chain identifier (number for EVM chains, CAIP-2 ID for SVM chains) */\n chainId: number | Caip2ChainId;\n expiredAt: number;\n /**\n * Fee breakdown for cross-chain bridge transactions.\n *\n * Only included in quotes from cross-chain aggregators (DeBridge, LiFi).\n */\n fees?: ReadonlyArray<{\n type: 'protocol' | 'gas' | 'bridge' | 'slippage' | 'swap' | 'other' | 'relay';\n /** Human-readable fee name */\n name: string;\n /** Fee amount in token's smallest unit */\n amount: bigint;\n /**\n * When `true`, indicates the fee is an additional fee on top of the input\n * amount that the user needs to pay. These fees need checked against the user's\n * balance to prevent insufficient balance errors.\n */\n extra?: boolean;\n /** Token the fee is deducted from */\n token: {\n /** Chain ID where fee is deducted */\n chainId: number | Caip2ChainId;\n /** Token address (EVM hex or Solana base58) */\n address: EvmAddress | SolAddress;\n };\n }>;\n gasEstimate?: bigint;\n recommendedSlippage: number;\n /** Input token address (EVM hex or Solana base58) */\n tokenIn: EvmAddress | SolAddress;\n tokenInDecimals: number;\n /** Output token address (EVM hex or Solana base58) */\n tokenOut: EvmAddress | SolAddress;\n tokenOutDecimals: number;\n uuid: string;\n}\n\nexport interface QuoteResponseDataDone {\n done: true;\n}\n\nexport type QuoteResponse = QuoteResponseData | QuoteResponseDataDone;\n\nconst MarkrQuoteTokenAddressSchema = z.union([EvmAddressSchema, SolAddressSchema, HyperliquidAddressSchema]);\n\n/**\n * Schema for the response from Markr's /quote endpoint.\n *\n * @see https://orchestrator-docs.markr.io/#/paths/~1quote/post\n */\nexport const QuoteResponseSchema: z.ZodType<QuoteResponse> = z.union([\n z.object({\n aggregator: z.object({\n id: z.string(),\n logo_url: z.url(),\n name: z.string(),\n }),\n amountIn: z.coerce.bigint().nonnegative(),\n amountOut: z.coerce.bigint().nonnegative(),\n chainId: z.union([z.number().int().nonnegative(), Caip2ChainIdSchema]),\n /**\n * Unix time in seconds when the quote expires.\n */\n expiredAt: z.number().int().nonnegative(),\n fees: z\n .array(\n z.object({\n type: z.enum(['protocol', 'gas', 'bridge', 'slippage', 'swap', 'other', 'relay']),\n name: z.string(),\n amount: z.coerce.bigint().nonnegative(),\n token: z.object({\n chainId: z.union([z.number().int().nonnegative(), Caip2ChainIdSchema]),\n address: MarkrQuoteTokenAddressSchema,\n }),\n extra: z.boolean().optional(),\n }),\n )\n .optional(),\n /**\n * Estimated gas for the swap transaction.\n * Markr estimates already include a buffer.\n *\n * The API docs do not specify this field as optional, but in practice it can be missing.\n */\n gasEstimate: z.coerce.bigint().nonnegative().optional(),\n /** Recommended slippage in basis points. */\n recommendedSlippage: z.number().int().nonnegative(),\n tokenIn: MarkrQuoteTokenAddressSchema,\n tokenInDecimals: z.number().int().nonnegative().max(18),\n tokenOut: MarkrQuoteTokenAddressSchema,\n tokenOutDecimals: z.number().int().nonnegative().max(18),\n uuid: z.uuid(),\n }),\n z.object({\n done: z.literal(true),\n }),\n]);\n\n/**\n * Response type for Markr's /swap endpoint.\n *\n * @see https://orchestrator-docs.markr.io/#/paths/~1swap/post\n */\nexport interface WrappedSwapTransactionResponse {\n data: Hex;\n to: EvmAddress;\n value: bigint;\n}\n\nexport const WrappedSwapTransactionResponseSchema: z.ZodType<WrappedSwapTransactionResponse> = z.object({\n data: HexSchema,\n to: EvmAddressSchema,\n value: z.coerce.bigint().nonnegative(),\n});\n\n/**\n * Solana swap transaction (returned for SVM chains).\n */\nexport interface SolanaSwapTransactionResponse {\n /** Chain type identifier */\n chainType: 'svm';\n /** Base64-encoded Solana transaction to be signed and sent by the client */\n swapTransaction: string;\n}\n\nexport const SolanaSwapTransactionResponseSchema: z.ZodType<SolanaSwapTransactionResponse> = z.object({\n chainType: z.literal('svm'),\n swapTransaction: z.base64(),\n});\n\nexport type SwapResponse = WrappedSwapTransactionResponse | SolanaSwapTransactionResponse;\n\nexport const SwapResponseSchema: z.ZodType<SwapResponse> = z.union([\n WrappedSwapTransactionResponseSchema,\n SolanaSwapTransactionResponseSchema,\n]);\n\nexport interface PartnerInfoResponse {\n /**\n * The partner fee in basis points collected on each swap.\n */\n fee: number;\n name: string;\n}\n\nexport const PartnerInfoResponseSchema: z.ZodType<PartnerInfoResponse> = z.object({\n fee: z.int().nonnegative(),\n name: z.string(),\n});\n\n/**\n * If the destination token is USDC, the flow is:\n * - pending -> committed -> completed\n *\n * If the destination token is not USDC, the flow is:\n * - pending -> committed -> pending_execution -> completed\n *\n * If the transaction fails at any point, the status will be 'failed'.\n */\nexport type CrossChainStatus =\n | /** Transaction submitted to CCIP */\n 'pending'\n /** Message committed, awaiting execution on destination chain */\n | 'committed'\n /** Message received on destination, swap pending execution by relayer */\n | 'pending_execution'\n /** Message executed successfully on destination chain */\n | 'completed'\n /** Swap expired, and funds were refunded (CCIP only) */\n | 'refunded'\n /** Execution failed */\n | 'failed'\n /** Unknown state */\n | 'unknown';\n\nexport interface CrossChainStatusResponse {\n /**\n * Cross-chain transaction identifier.\n *\n * For CCIP: The CCIP message ID.\n * For DeBridge: The DeBridge order ID.\n */\n messageId: string | null;\n /** Current status of the cross-chain transaction. */\n status: CrossChainStatus;\n /** Human-readable status description */\n description: string;\n /** Source chain information */\n sourceChain: {\n /** Source chain network name */\n name: string;\n /** Source chain transaction hash */\n transactionHash: Hash | Signature;\n /** Transaction timestamp on source chain (ISO 8601) */\n timestamp: string;\n /** When the source transaction was finalized (ISO 8601) */\n finalized: string | boolean | null;\n };\n /** Destination chain information */\n destinationChain: {\n /** Destination chain network name */\n name: string | null;\n /**\n * Destination chain tx hash.\n *\n * - For direct transfers (USDC -> USDC): The CCIP bridge transaction hash.\n * - For successful swaps: The actual swap transaction hash from the relayer.\n * - For pending swaps: null (swap not yet executed).\n */\n transactionHash: Hash | Signature | null;\n /**\n * CCIP bridge tx hash (only present for swaps, not direct transfers).\n *\n * This is the transaction that delivered USDC to the destination chain via CCIP.\n * The `transactionHash` field contains the subsequent swap transaction.\n */\n bridgeHash?: Hash | Signature | null;\n /** Transaction timestamp on destination chain (null if not yet executed) (ISO 8601) */\n timestamp: string | null;\n /** When the destination transaction was finalized (null if not yet executed) (ISO 8601) */\n finalized: string | null;\n };\n /** Transaction progress information */\n progress: {\n /** Whether the message has been committed on destination chain */\n committed: boolean;\n /** When the message was committed (null if not committed) (ISO 8601) */\n commitTimestamp?: string | null;\n /** Whether the message has been executed on destination chain */\n executed: boolean;\n };\n /** Fee information */\n fees: {\n /** Fee token address */\n token: EvmAddress | SolAddress | null;\n /** Fee amount in wei */\n amount: bigint | null;\n } | null;\n /** Tokens transferred in this cross-chain transaction */\n transferredTokens: ReadonlyArray<{\n /** Token address */\n token: EvmAddress | SolAddress;\n /** Token amount */\n amount: bigint;\n }>;\n /**\n * Debug information from Markr transaction API (only present for swaps that have been executed).\n *\n * Contains detailed information about the relayer execution, retry attempts, and transaction details.\n */\n debug?: Partial<{\n /** CCIP message ID */\n messageId: string;\n /** Execution status from relayer */\n status: string;\n /** Destination chain name */\n destinationChain: string;\n /** Destination swap transaction hash */\n destinationTxHash: string;\n /** Relay transaction hash */\n relayTxHash: string;\n /** Number of retry attempts */\n retryCount: number;\n /** Timestamp of last retry attempt (ISO 8601) */\n lastRetryAt: string;\n /** Source chain name */\n sourceChain: string;\n /** Source transaction hash */\n sourceTxHash: string;\n /** When the record was created (ISO 8601) */\n createdAt: string;\n }> | null;\n}\n\nconst ISO_8601_TIMEZONE_DESIGNATOR_REGEX = /[zZ]|[+-]\\d{2}:?\\d{2}$/;\n\n// Markr API returns ISO-8601 datetime strings that are sometimes missing a timezone designator (\"local\" ISO).\n// We assume these are meant to be UTC and append 'Z' if missing, then validate as strict ISO-8601 with timezone.\nconst datetime = z.iso\n .datetime({ local: true })\n .refine(\n (value) => {\n // Accept at least local ISO format\n // If missing timezone, treat as UTC\n const hasTimezone = ISO_8601_TIMEZONE_DESIGNATOR_REGEX.test(value);\n const isoString = hasTimezone ? value : `${value}Z`;\n // Validate as strict ISO-8601 with timezone\n return z.iso.datetime().safeParse(isoString).success;\n },\n {\n error: 'Invalid ISO-8601 datetime (must be valid with timezone, or local assumed UTC)',\n },\n )\n .transform((value) => {\n const hasTimezone = ISO_8601_TIMEZONE_DESIGNATOR_REGEX.test(value);\n return hasTimezone ? value : `${value}Z`;\n });\n\nexport const CrossChainStatusResponseSchema: z.ZodType<CrossChainStatusResponse> = z.object({\n messageId: z.string().nullable(),\n status: z.enum(['pending', 'committed', 'pending_execution', 'completed', 'failed', 'refunded', 'unknown']),\n description: z.string(),\n sourceChain: z.object({\n name: z.string(),\n transactionHash: z.union([HashSchema, SolSignatureSchema]),\n timestamp: datetime,\n finalized: datetime.or(z.boolean()).nullable(),\n }),\n destinationChain: z.object({\n name: z.string().nullable(),\n transactionHash: z.union([HashSchema, SolSignatureSchema]).nullable(),\n bridgeHash: z.union([HashSchema, SolSignatureSchema]).nullable().optional(),\n timestamp: datetime.nullable(),\n finalized: datetime.nullable(),\n }),\n progress: z.object({\n committed: z.boolean(),\n commitTimestamp: datetime.nullable().optional(),\n executed: z.boolean(),\n }),\n fees: z\n .object({\n token: z.union([EvmAddressSchema, SolAddressSchema]).nullable(),\n amount: z.coerce.bigint().nonnegative().nullable(),\n })\n .nullable(),\n transferredTokens: z.array(\n z.object({\n token: z.union([EvmAddressSchema, SolAddressSchema]),\n amount: z.coerce.bigint().nonnegative(),\n }),\n ),\n debug: z\n .looseObject({\n messageId: z.string(),\n status: z.string(),\n destinationChain: z.string(),\n destinationTxHash: z.string(),\n relayTxHash: z.string(),\n retryCount: z.number().int().nonnegative(),\n lastRetryAt: z.iso.datetime(),\n sourceChain: z.string(),\n sourceTxHash: z.string(),\n createdAt: z.iso.datetime(),\n })\n .partial()\n .nullable()\n .optional(),\n});\n\nexport interface SpenderAddressResponse {\n address: EvmAddress;\n}\n\nexport const SpenderAddressResponseSchema: z.ZodType<SpenderAddressResponse> = z.object({\n address: EvmAddressSchema,\n});\n"],"mappings":"8PAsDA,MAAa,EAA8C,EACxD,QAAQ,CACR,OAAQ,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AiB,EAAS,UAAU,CAAC,UAAU,CAC/C,SAAU,EAAE,SAAS,CACtB,CAAC,CACF,KAAM,EACH,OAAO,CACN,MAAO,EAAE,MAAM,CAAC,EAAkB,EAAiB,CAAC,CAAC,UAAU,CAC/D,OAAQ,EAAE,OAAO,QAAQ,CAAC,aAAa,CAAC,UAAU,CACnD,CAAC,CACD,UAAU,CACb,kBAAmB,EAAE,MACnB,EAAE,OAAO,CACP,MAAO,EAAE,MAAM,CAAC,EAAkB,EAAiB,CAAC,CACpD,OAAQ,EAAE,OAAO,QAAQ,CAAC,aAAa,CACxC,CAAC,CACH,CACD,MAAO,EACJ,YAAY,CACX,UAAW,EAAE,QAAQ,CACrB,OAAQ,EAAE,QAAQ,CAClB,iBAAkB,EAAE,QAAQ,CAC5B,kBAAmB,EAAE,QAAQ,CAC7B,YAAa,EAAE,QAAQ,CACvB,WAAY,EAAE,QAAQ,CAAC,KAAK,CAAC,aAAa,CAC1C,YAAa,EAAE,IAAI,UAAU,CAC7B,YAAa,EAAE,QAAQ,CACvB,aAAc,EAAE,QAAQ,CACxB,UAAW,EAAE,IAAI,UAAU,CAC5B,CAAC,CACD,SAAS,CACT,UAAU,CACV,UAAU,CACd,CAAC,CAMW,EAAkE,EAAE,OAAO,CACtF,QAAS,EACV,CAAC"}
@@ -1,2 +1,2 @@
1
- require(`../_virtual/_rolldown/runtime.cjs`);const e=require(`../utils/bitcoin-address.cjs`);let t=require(`zod`),n=require(`viem`),r=require(`@solana/kit`);const i=t.z.string().refine(n.isAddress,{message:`Invalid EVM address`}).transform(e=>e),a=t.z.string().refine(n.isHex,{message:`Invalid hex string`}).transform(e=>e),o=t.z.string().refine(e.isBech32Address,{message:`Invalid BTC address`}),s=t.z.string().refine(n.isHash,{message:`Invalid hash string`}).transform(e=>e),c=t.z.string().refine(r.isAddress,{message:`Invalid Solana address`}).transform(e=>e),l=t.z.string().refine(r.isSignature,{message:`Invalid Solana signature`}).transform(e=>e);exports.BtcAddressSchema=o,exports.EvmAddressSchema=i,exports.HashSchema=s,exports.HexSchema=a,exports.SolAddressSchema=c,exports.SolSignatureSchema=l;
1
+ require(`../_virtual/_rolldown/runtime.cjs`);const e=require(`../utils/bitcoin-address.cjs`);let t=require(`zod`),n=require(`viem`),r=require(`@solana/kit`);const i=t.z.string().refine(n.isAddress,{message:`Invalid EVM address`}).transform(e=>e),a=t.z.string().refine(e=>e.startsWith(`0x`)&&/^0x[0-9a-fA-F]+$/.test(e),{message:`Invalid Hyperliquid quote hex address`}).refine(e=>{let t=e.length-2;return t>0&&t<40}).transform(e=>`0x${e.slice(2).padStart(40,`0`)}`).refine(n.isAddress,{message:`Invalid EVM address after Hyperliquid quote padding`}).transform(e=>e),o=t.z.string().refine(n.isHex,{message:`Invalid hex string`}).transform(e=>e),s=t.z.string().refine(e.isBech32Address,{message:`Invalid BTC address`}),c=t.z.string().refine(n.isHash,{message:`Invalid hash string`}).transform(e=>e),l=t.z.string().refine(r.isAddress,{message:`Invalid Solana address`}).transform(e=>e),u=t.z.string().refine(r.isSignature,{message:`Invalid Solana signature`}).transform(e=>e);exports.BtcAddressSchema=s,exports.EvmAddressSchema=i,exports.HashSchema=c,exports.HexSchema=o,exports.HyperliquidAddressSchema=a,exports.SolAddressSchema=l,exports.SolSignatureSchema=u;
2
2
  //# sourceMappingURL=service-schemas.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"service-schemas.cjs","names":["z","isEvmAddress","isHex","isBech32Address","isHash","isSolAddress","isSignature"],"sources":["../../src/transfer-service/service-schemas.ts"],"sourcesContent":["import { type Signature, type Address as SolAddress, isSignature, isAddress as isSolAddress } from '@solana/kit';\nimport { z } from 'zod';\nimport { isAddress as isEvmAddress, isHash, isHex, type Address as EvmAddress, type Hash } from 'viem';\nimport type { Hex } from '../types/signer';\nimport { isBech32Address } from '../utils/bitcoin-address';\n\nexport const EvmAddressSchema: z.ZodType<EvmAddress> = z\n .string()\n .refine(isEvmAddress, { message: 'Invalid EVM address' })\n .transform((addr) => addr as EvmAddress);\n\nexport const HexSchema: z.ZodType<Hex> = z\n .string()\n .refine(isHex, { message: 'Invalid hex string' })\n .transform((hex) => hex as Hex);\n\nexport const BtcAddressSchema: z.ZodType<string> = z\n .string()\n .refine(isBech32Address, { message: 'Invalid BTC address' });\n\nexport const HashSchema: z.ZodType<Hash> = z\n .string()\n .refine(isHash, { message: 'Invalid hash string' })\n .transform((hash) => hash as Hash);\n\nexport const SolAddressSchema: z.ZodType<SolAddress> = z\n .string()\n .refine(isSolAddress, { message: 'Invalid Solana address' })\n .transform((addr) => addr as SolAddress);\n\nexport const SolSignatureSchema: z.ZodType<Signature> = z\n .string()\n .refine(isSignature, { message: 'Invalid Solana signature' })\n .transform((sig) => sig as Signature);\n"],"mappings":"6JAMA,MAAa,EAA0CA,EAAAA,EACpD,QAAQ,CACR,OAAOC,EAAAA,UAAc,CAAE,QAAS,sBAAuB,CAAC,CACxD,UAAW,GAAS,EAAmB,CAE7B,EAA4BD,EAAAA,EACtC,QAAQ,CACR,OAAOE,EAAAA,MAAO,CAAE,QAAS,qBAAsB,CAAC,CAChD,UAAW,GAAQ,EAAW,CAEpB,EAAsCF,EAAAA,EAChD,QAAQ,CACR,OAAOG,EAAAA,gBAAiB,CAAE,QAAS,sBAAuB,CAAC,CAEjD,EAA8BH,EAAAA,EACxC,QAAQ,CACR,OAAOI,EAAAA,OAAQ,CAAE,QAAS,sBAAuB,CAAC,CAClD,UAAW,GAAS,EAAa,CAEvB,EAA0CJ,EAAAA,EACpD,QAAQ,CACR,OAAOK,EAAAA,UAAc,CAAE,QAAS,yBAA0B,CAAC,CAC3D,UAAW,GAAS,EAAmB,CAE7B,EAA2CL,EAAAA,EACrD,QAAQ,CACR,OAAOM,EAAAA,YAAa,CAAE,QAAS,2BAA4B,CAAC,CAC5D,UAAW,GAAQ,EAAiB"}
1
+ {"version":3,"file":"service-schemas.cjs","names":["z","isEvmAddress","isHex","isBech32Address","isHash","isSolAddress","isSignature"],"sources":["../../src/transfer-service/service-schemas.ts"],"sourcesContent":["import { type Signature, type Address as SolAddress, isSignature, isAddress as isSolAddress } from '@solana/kit';\nimport { z } from 'zod';\nimport { isAddress as isEvmAddress, isHash, isHex, type Address as EvmAddress, type Hash } from 'viem';\nimport type { Hex } from '../types/signer';\nimport { isBech32Address } from '../utils/bitcoin-address';\n\nexport const EvmAddressSchema: z.ZodType<EvmAddress> = z\n .string()\n .refine(isEvmAddress, { message: 'Invalid EVM address' })\n .transform((addr) => addr as EvmAddress);\n\n/**\n * **Hyperliquid spot `tokenId` (16-byte hex)** — In `spotMeta` / token APIs, each spot token has a\n * `tokenId` that Hyperliquid documents as a **34-character** hex string: `0x` plus **32** hex digits\n * (128 bits), e.g. `0x6d1e7cde53ba9467b783cb7c530ce054`. The `tokenDetails` info request uses the same\n * shape (`tokenId` parameter; see Hyperliquid API docs: Spot → tokenDetails).\n *\n * That is **not** the same as **HyperCore ↔ HyperEVM system addresses**: those are full **20-byte**\n * addresses whose leading byte is `0x20` with the token index in the tail (big-endian), or the HYPE\n * sentinel `0x2222…2222` (see Hyperliquid docs: HyperCore <> HyperEVM transfers → System Addresses).\n *\n * Aggregator quote payloads (e.g. Markr) may carry Hyperliquid **tokenId**-width hex where downstream\n * code expects a 20-byte EVM `0x` string. **Left-pad** the hex body to 40 nybbles, then validate with\n * viem `isAddress`. Standard 42-character EVM addresses are parsed by {@link EvmAddressSchema} instead\n * (union order in callers).\n *\n * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/info-endpoint/spot\n * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/hyperevm/hypercore-less-than-greater-than-hyperevm-transfers\n */\nexport const HyperliquidAddressSchema: z.ZodType<EvmAddress> = z\n .string()\n .refine((value) => value.startsWith('0x') && /^0x[0-9a-fA-F]+$/.test(value), {\n message: 'Invalid Hyperliquid quote hex address',\n })\n .refine((value) => {\n const nybbles = value.length - 2;\n return nybbles > 0 && nybbles < 40;\n })\n .transform((value) => `0x${value.slice(2).padStart(40, '0')}`)\n .refine(isEvmAddress, { message: 'Invalid EVM address after Hyperliquid quote padding' })\n .transform((addr) => addr as EvmAddress);\n\nexport const HexSchema: z.ZodType<Hex> = z\n .string()\n .refine(isHex, { message: 'Invalid hex string' })\n .transform((hex) => hex as Hex);\n\nexport const BtcAddressSchema: z.ZodType<string> = z\n .string()\n .refine(isBech32Address, { message: 'Invalid BTC address' });\n\nexport const HashSchema: z.ZodType<Hash> = z\n .string()\n .refine(isHash, { message: 'Invalid hash string' })\n .transform((hash) => hash as Hash);\n\nexport const SolAddressSchema: z.ZodType<SolAddress> = z\n .string()\n .refine(isSolAddress, { message: 'Invalid Solana address' })\n .transform((addr) => addr as SolAddress);\n\nexport const SolSignatureSchema: z.ZodType<Signature> = z\n .string()\n .refine(isSignature, { message: 'Invalid Solana signature' })\n .transform((sig) => sig as Signature);\n"],"mappings":"6JAMA,MAAa,EAA0CA,EAAAA,EACpD,QAAQ,CACR,OAAOC,EAAAA,UAAc,CAAE,QAAS,sBAAuB,CAAC,CACxD,UAAW,GAAS,EAAmB,CAoB7B,EAAkDD,EAAAA,EAC5D,QAAQ,CACR,OAAQ,GAAU,EAAM,WAAW,KAAK,EAAI,mBAAmB,KAAK,EAAM,CAAE,CAC3E,QAAS,wCACV,CAAC,CACD,OAAQ,GAAU,CACjB,IAAM,EAAU,EAAM,OAAS,EAC/B,OAAO,EAAU,GAAK,EAAU,IAChC,CACD,UAAW,GAAU,KAAK,EAAM,MAAM,EAAE,CAAC,SAAS,GAAI,IAAI,GAAG,CAC7D,OAAOC,EAAAA,UAAc,CAAE,QAAS,sDAAuD,CAAC,CACxF,UAAW,GAAS,EAAmB,CAE7B,EAA4BD,EAAAA,EACtC,QAAQ,CACR,OAAOE,EAAAA,MAAO,CAAE,QAAS,qBAAsB,CAAC,CAChD,UAAW,GAAQ,EAAW,CAEpB,EAAsCF,EAAAA,EAChD,QAAQ,CACR,OAAOG,EAAAA,gBAAiB,CAAE,QAAS,sBAAuB,CAAC,CAEjD,EAA8BH,EAAAA,EACxC,QAAQ,CACR,OAAOI,EAAAA,OAAQ,CAAE,QAAS,sBAAuB,CAAC,CAClD,UAAW,GAAS,EAAa,CAEvB,EAA0CJ,EAAAA,EACpD,QAAQ,CACR,OAAOK,EAAAA,UAAc,CAAE,QAAS,yBAA0B,CAAC,CAC3D,UAAW,GAAS,EAAmB,CAE7B,EAA2CL,EAAAA,EACrD,QAAQ,CACR,OAAOM,EAAAA,YAAa,CAAE,QAAS,2BAA4B,CAAC,CAC5D,UAAW,GAAQ,EAAiB"}
@@ -1,2 +1,2 @@
1
- import{isBech32Address as e}from"../utils/bitcoin-address.js";import{z as t}from"zod";import{isAddress as n,isHash as r,isHex as i}from"viem";import{isAddress as a,isSignature as o}from"@solana/kit";const s=t.string().refine(n,{message:`Invalid EVM address`}).transform(e=>e),c=t.string().refine(i,{message:`Invalid hex string`}).transform(e=>e),l=t.string().refine(e,{message:`Invalid BTC address`}),u=t.string().refine(r,{message:`Invalid hash string`}).transform(e=>e),d=t.string().refine(a,{message:`Invalid Solana address`}).transform(e=>e),f=t.string().refine(o,{message:`Invalid Solana signature`}).transform(e=>e);export{l as BtcAddressSchema,s as EvmAddressSchema,u as HashSchema,c as HexSchema,d as SolAddressSchema,f as SolSignatureSchema};
1
+ import{isBech32Address as e}from"../utils/bitcoin-address.js";import{z as t}from"zod";import{isAddress as n,isHash as r,isHex as i}from"viem";import{isAddress as a,isSignature as o}from"@solana/kit";const s=t.string().refine(n,{message:`Invalid EVM address`}).transform(e=>e),c=t.string().refine(e=>e.startsWith(`0x`)&&/^0x[0-9a-fA-F]+$/.test(e),{message:`Invalid Hyperliquid quote hex address`}).refine(e=>{let t=e.length-2;return t>0&&t<40}).transform(e=>`0x${e.slice(2).padStart(40,`0`)}`).refine(n,{message:`Invalid EVM address after Hyperliquid quote padding`}).transform(e=>e),l=t.string().refine(i,{message:`Invalid hex string`}).transform(e=>e),u=t.string().refine(e,{message:`Invalid BTC address`}),d=t.string().refine(r,{message:`Invalid hash string`}).transform(e=>e),f=t.string().refine(a,{message:`Invalid Solana address`}).transform(e=>e),p=t.string().refine(o,{message:`Invalid Solana signature`}).transform(e=>e);export{u as BtcAddressSchema,s as EvmAddressSchema,d as HashSchema,l as HexSchema,c as HyperliquidAddressSchema,f as SolAddressSchema,p as SolSignatureSchema};
2
2
  //# sourceMappingURL=service-schemas.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"service-schemas.js","names":["isEvmAddress","isSolAddress"],"sources":["../../src/transfer-service/service-schemas.ts"],"sourcesContent":["import { type Signature, type Address as SolAddress, isSignature, isAddress as isSolAddress } from '@solana/kit';\nimport { z } from 'zod';\nimport { isAddress as isEvmAddress, isHash, isHex, type Address as EvmAddress, type Hash } from 'viem';\nimport type { Hex } from '../types/signer';\nimport { isBech32Address } from '../utils/bitcoin-address';\n\nexport const EvmAddressSchema: z.ZodType<EvmAddress> = z\n .string()\n .refine(isEvmAddress, { message: 'Invalid EVM address' })\n .transform((addr) => addr as EvmAddress);\n\nexport const HexSchema: z.ZodType<Hex> = z\n .string()\n .refine(isHex, { message: 'Invalid hex string' })\n .transform((hex) => hex as Hex);\n\nexport const BtcAddressSchema: z.ZodType<string> = z\n .string()\n .refine(isBech32Address, { message: 'Invalid BTC address' });\n\nexport const HashSchema: z.ZodType<Hash> = z\n .string()\n .refine(isHash, { message: 'Invalid hash string' })\n .transform((hash) => hash as Hash);\n\nexport const SolAddressSchema: z.ZodType<SolAddress> = z\n .string()\n .refine(isSolAddress, { message: 'Invalid Solana address' })\n .transform((addr) => addr as SolAddress);\n\nexport const SolSignatureSchema: z.ZodType<Signature> = z\n .string()\n .refine(isSignature, { message: 'Invalid Solana signature' })\n .transform((sig) => sig as Signature);\n"],"mappings":"uMAMA,MAAa,EAA0C,EACpD,QAAQ,CACR,OAAOA,EAAc,CAAE,QAAS,sBAAuB,CAAC,CACxD,UAAW,GAAS,EAAmB,CAE7B,EAA4B,EACtC,QAAQ,CACR,OAAO,EAAO,CAAE,QAAS,qBAAsB,CAAC,CAChD,UAAW,GAAQ,EAAW,CAEpB,EAAsC,EAChD,QAAQ,CACR,OAAO,EAAiB,CAAE,QAAS,sBAAuB,CAAC,CAEjD,EAA8B,EACxC,QAAQ,CACR,OAAO,EAAQ,CAAE,QAAS,sBAAuB,CAAC,CAClD,UAAW,GAAS,EAAa,CAEvB,EAA0C,EACpD,QAAQ,CACR,OAAOC,EAAc,CAAE,QAAS,yBAA0B,CAAC,CAC3D,UAAW,GAAS,EAAmB,CAE7B,EAA2C,EACrD,QAAQ,CACR,OAAO,EAAa,CAAE,QAAS,2BAA4B,CAAC,CAC5D,UAAW,GAAQ,EAAiB"}
1
+ {"version":3,"file":"service-schemas.js","names":["isEvmAddress","isSolAddress"],"sources":["../../src/transfer-service/service-schemas.ts"],"sourcesContent":["import { type Signature, type Address as SolAddress, isSignature, isAddress as isSolAddress } from '@solana/kit';\nimport { z } from 'zod';\nimport { isAddress as isEvmAddress, isHash, isHex, type Address as EvmAddress, type Hash } from 'viem';\nimport type { Hex } from '../types/signer';\nimport { isBech32Address } from '../utils/bitcoin-address';\n\nexport const EvmAddressSchema: z.ZodType<EvmAddress> = z\n .string()\n .refine(isEvmAddress, { message: 'Invalid EVM address' })\n .transform((addr) => addr as EvmAddress);\n\n/**\n * **Hyperliquid spot `tokenId` (16-byte hex)** — In `spotMeta` / token APIs, each spot token has a\n * `tokenId` that Hyperliquid documents as a **34-character** hex string: `0x` plus **32** hex digits\n * (128 bits), e.g. `0x6d1e7cde53ba9467b783cb7c530ce054`. The `tokenDetails` info request uses the same\n * shape (`tokenId` parameter; see Hyperliquid API docs: Spot → tokenDetails).\n *\n * That is **not** the same as **HyperCore ↔ HyperEVM system addresses**: those are full **20-byte**\n * addresses whose leading byte is `0x20` with the token index in the tail (big-endian), or the HYPE\n * sentinel `0x2222…2222` (see Hyperliquid docs: HyperCore <> HyperEVM transfers → System Addresses).\n *\n * Aggregator quote payloads (e.g. Markr) may carry Hyperliquid **tokenId**-width hex where downstream\n * code expects a 20-byte EVM `0x` string. **Left-pad** the hex body to 40 nybbles, then validate with\n * viem `isAddress`. Standard 42-character EVM addresses are parsed by {@link EvmAddressSchema} instead\n * (union order in callers).\n *\n * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/api/info-endpoint/spot\n * @see https://hyperliquid.gitbook.io/hyperliquid-docs/for-developers/hyperevm/hypercore-less-than-greater-than-hyperevm-transfers\n */\nexport const HyperliquidAddressSchema: z.ZodType<EvmAddress> = z\n .string()\n .refine((value) => value.startsWith('0x') && /^0x[0-9a-fA-F]+$/.test(value), {\n message: 'Invalid Hyperliquid quote hex address',\n })\n .refine((value) => {\n const nybbles = value.length - 2;\n return nybbles > 0 && nybbles < 40;\n })\n .transform((value) => `0x${value.slice(2).padStart(40, '0')}`)\n .refine(isEvmAddress, { message: 'Invalid EVM address after Hyperliquid quote padding' })\n .transform((addr) => addr as EvmAddress);\n\nexport const HexSchema: z.ZodType<Hex> = z\n .string()\n .refine(isHex, { message: 'Invalid hex string' })\n .transform((hex) => hex as Hex);\n\nexport const BtcAddressSchema: z.ZodType<string> = z\n .string()\n .refine(isBech32Address, { message: 'Invalid BTC address' });\n\nexport const HashSchema: z.ZodType<Hash> = z\n .string()\n .refine(isHash, { message: 'Invalid hash string' })\n .transform((hash) => hash as Hash);\n\nexport const SolAddressSchema: z.ZodType<SolAddress> = z\n .string()\n .refine(isSolAddress, { message: 'Invalid Solana address' })\n .transform((addr) => addr as SolAddress);\n\nexport const SolSignatureSchema: z.ZodType<Signature> = z\n .string()\n .refine(isSignature, { message: 'Invalid Solana signature' })\n .transform((sig) => sig as Signature);\n"],"mappings":"uMAMA,MAAa,EAA0C,EACpD,QAAQ,CACR,OAAOA,EAAc,CAAE,QAAS,sBAAuB,CAAC,CACxD,UAAW,GAAS,EAAmB,CAoB7B,EAAkD,EAC5D,QAAQ,CACR,OAAQ,GAAU,EAAM,WAAW,KAAK,EAAI,mBAAmB,KAAK,EAAM,CAAE,CAC3E,QAAS,wCACV,CAAC,CACD,OAAQ,GAAU,CACjB,IAAM,EAAU,EAAM,OAAS,EAC/B,OAAO,EAAU,GAAK,EAAU,IAChC,CACD,UAAW,GAAU,KAAK,EAAM,MAAM,EAAE,CAAC,SAAS,GAAI,IAAI,GAAG,CAC7D,OAAOA,EAAc,CAAE,QAAS,sDAAuD,CAAC,CACxF,UAAW,GAAS,EAAmB,CAE7B,EAA4B,EACtC,QAAQ,CACR,OAAO,EAAO,CAAE,QAAS,qBAAsB,CAAC,CAChD,UAAW,GAAQ,EAAW,CAEpB,EAAsC,EAChD,QAAQ,CACR,OAAO,EAAiB,CAAE,QAAS,sBAAuB,CAAC,CAEjD,EAA8B,EACxC,QAAQ,CACR,OAAO,EAAQ,CAAE,QAAS,sBAAuB,CAAC,CAClD,UAAW,GAAS,EAAa,CAEvB,EAA0C,EACpD,QAAQ,CACR,OAAOC,EAAc,CAAE,QAAS,yBAA0B,CAAC,CAC3D,UAAW,GAAS,EAAmB,CAE7B,EAA2C,EACrD,QAAQ,CACR,OAAO,EAAa,CAAE,QAAS,2BAA4B,CAAC,CAC5D,UAAW,GAAQ,EAAiB"}
@@ -12,7 +12,7 @@ import { Address as Address$1 } from "@solana/kit";
12
12
  * - "partner" fee is the fee charged by Core to facilitate the transfer.
13
13
  * It is already removed from the `amountOut` and is included here for transparency.
14
14
  */
15
- type QuoteFeeType = "partner" | "protocol" | "gas" | "bridge" | "slippage" | "swap" | "other";
15
+ type QuoteFeeType = "partner" | "protocol" | "gas" | "bridge" | "slippage" | "swap" | "other" | "relay";
16
16
  type QuoteFeeToken = {
17
17
  type: TokenType.NATIVE;
18
18
  } | {
@@ -12,7 +12,7 @@ import { Address as Address$1 } from "@solana/kit";
12
12
  * - "partner" fee is the fee charged by Core to facilitate the transfer.
13
13
  * It is already removed from the `amountOut` and is included here for transparency.
14
14
  */
15
- type QuoteFeeType = "partner" | "protocol" | "gas" | "bridge" | "slippage" | "swap" | "other";
15
+ type QuoteFeeType = "partner" | "protocol" | "gas" | "bridge" | "slippage" | "swap" | "other" | "relay";
16
16
  type QuoteFeeToken = {
17
17
  type: TokenType.NATIVE;
18
18
  } | {
@@ -1,2 +1,2 @@
1
- require(`../_virtual/_rolldown/runtime.cjs`);const e=require(`../constants.cjs`);let t=require(`zod`);const n=`__type`,r=`bigint`,i=`value`;function a(e){return JSON.stringify(e,(e,t)=>typeof t==`bigint`?{[n]:r,[i]:t.toString()}:t)}function o(e){let t=JSON.parse(e,(e,t)=>t&&typeof t==`object`&&t[n]===r&&typeof t[i]==`string`?BigInt(t[i]):t),a=p.safeParse(t);if(!a.success)throw Error(`Failed to parse transfer: ${a.error.message}`);return a.data}const s=t.z.object({name:t.z.string(),symbol:t.z.string(),decimals:t.z.int().nonnegative(),type:t.z.enum(e.TokenType)}),c=s.extend({type:t.z.literal(e.TokenType.NATIVE)}),l=s.extend({type:t.z.literal(e.TokenType.ERC20),address:t.z.string()}),u=s.extend({type:t.z.literal(e.TokenType.SPL),address:t.z.string()}),d=t.z.discriminatedUnion(`type`,[c,l,u]),f=t.z.object({chainId:t.z.string(),chainName:t.z.string(),networkToken:c,rpcUrl:t.z.string(),utilityAddresses:t.z.object({multicall:t.z.string()}).optional()}),p=t.z.object({amountIn:t.z.bigint().nonnegative(),amountOut:t.z.bigint().nonnegative(),completedAtMs:t.z.int().nonnegative().optional(),environment:t.z.enum(e.Environment),errorCode:t.z.number().optional(),errorReason:t.z.string().optional(),failedAtMs:t.z.int().nonnegative().optional(),fees:t.z.array(t.z.object({type:t.z.enum([`partner`,`protocol`,`gas`,`bridge`,`slippage`,`swap`,`other`]),fundingModel:t.z.enum([`included`,`additive`]).default(`included`),name:t.z.string(),amount:t.z.bigint().nonnegative(),chainId:t.z.string(),token:t.z.discriminatedUnion(`type`,[t.z.object({type:t.z.literal(e.TokenType.NATIVE)}),t.z.object({type:t.z.literal(e.TokenType.ERC20),address:t.z.string()}),t.z.object({type:t.z.literal(e.TokenType.SPL),address:t.z.string()})])})),fromAddress:t.z.string(),id:t.z.string(),metadata:t.z.record(t.z.string(),t.z.unknown()).optional(),partnerFeeBps:t.z.int().nonnegative().nullable(),refund:t.z.object({amount:t.z.bigint().nonnegative(),asset:d.nullable(),chainId:t.z.string(),txHash:t.z.string().nullable(),timestampMs:t.z.int().nonnegative()}).optional(),source:t.z.object({confirmationCount:t.z.int().nonnegative(),requiredConfirmationCount:t.z.int().nonnegative(),startedAtMs:t.z.int().nonnegative(),targetStartBlockNumber:t.z.bigint().nonnegative().optional(),txHash:t.z.string()}).optional(),target:t.z.object({confirmationCount:t.z.int().nonnegative(),requiredConfirmationCount:t.z.int().nonnegative(),startedAtMs:t.z.int().nonnegative(),txHash:t.z.string().optional()}).nullable().optional(),sourceAsset:d,sourceChain:f,status:t.z.enum([`created`,`source-pending`,`source-completed`,`target-pending`,`refunded`,`completed`,`failed`]),targetAsset:d,targetChain:f,toAddress:t.z.string(),type:t.z.enum(e.ServiceType)});exports.parseTransfer=o,exports.stringifyTransfer=a;
1
+ require(`../_virtual/_rolldown/runtime.cjs`);const e=require(`../constants.cjs`);let t=require(`zod`);const n=`__type`,r=`bigint`,i=`value`;function a(e){return JSON.stringify(e,(e,t)=>typeof t==`bigint`?{[n]:r,[i]:t.toString()}:t)}function o(e){let t=JSON.parse(e,(e,t)=>t&&typeof t==`object`&&t[n]===r&&typeof t[i]==`string`?BigInt(t[i]):t),a=p.safeParse(t);if(!a.success)throw Error(`Failed to parse transfer: ${a.error.message}`);return a.data}const s=t.z.object({name:t.z.string(),symbol:t.z.string(),decimals:t.z.int().nonnegative(),type:t.z.enum(e.TokenType)}),c=s.extend({type:t.z.literal(e.TokenType.NATIVE)}),l=s.extend({type:t.z.literal(e.TokenType.ERC20),address:t.z.string()}),u=s.extend({type:t.z.literal(e.TokenType.SPL),address:t.z.string()}),d=t.z.discriminatedUnion(`type`,[c,l,u]),f=t.z.object({chainId:t.z.string(),chainName:t.z.string(),networkToken:c,rpcUrl:t.z.string(),utilityAddresses:t.z.object({multicall:t.z.string()}).optional()}),p=t.z.object({amountIn:t.z.bigint().nonnegative(),amountOut:t.z.bigint().nonnegative(),completedAtMs:t.z.int().nonnegative().optional(),environment:t.z.enum(e.Environment),errorCode:t.z.number().optional(),errorReason:t.z.string().optional(),failedAtMs:t.z.int().nonnegative().optional(),fees:t.z.array(t.z.object({type:t.z.enum([`partner`,`protocol`,`gas`,`bridge`,`slippage`,`swap`,`other`,`relay`]),fundingModel:t.z.enum([`included`,`additive`]).default(`included`),name:t.z.string(),amount:t.z.bigint().nonnegative(),chainId:t.z.string(),token:t.z.discriminatedUnion(`type`,[t.z.object({type:t.z.literal(e.TokenType.NATIVE)}),t.z.object({type:t.z.literal(e.TokenType.ERC20),address:t.z.string()}),t.z.object({type:t.z.literal(e.TokenType.SPL),address:t.z.string()})])})),fromAddress:t.z.string(),id:t.z.string(),metadata:t.z.record(t.z.string(),t.z.unknown()).optional(),partnerFeeBps:t.z.int().nonnegative().nullable(),refund:t.z.object({amount:t.z.bigint().nonnegative(),asset:d.nullable(),chainId:t.z.string(),txHash:t.z.string().nullable(),timestampMs:t.z.int().nonnegative()}).optional(),source:t.z.object({confirmationCount:t.z.int().nonnegative(),requiredConfirmationCount:t.z.int().nonnegative(),startedAtMs:t.z.int().nonnegative(),targetStartBlockNumber:t.z.bigint().nonnegative().optional(),txHash:t.z.string()}).optional(),target:t.z.object({confirmationCount:t.z.int().nonnegative(),requiredConfirmationCount:t.z.int().nonnegative(),startedAtMs:t.z.int().nonnegative(),txHash:t.z.string().optional()}).nullable().optional(),sourceAsset:d,sourceChain:f,status:t.z.enum([`created`,`source-pending`,`source-completed`,`target-pending`,`refunded`,`completed`,`failed`]),targetAsset:d,targetChain:f,toAddress:t.z.string(),type:t.z.enum(e.ServiceType)});exports.parseTransfer=o,exports.stringifyTransfer=a;
2
2
  //# sourceMappingURL=transfer-utils.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"transfer-utils.cjs","names":["z","TokenType","Environment","ServiceType"],"sources":["../../src/utils/transfer-utils.ts"],"sourcesContent":["import { z } from 'zod';\nimport { Environment, ServiceType, TokenType } from '../constants';\nimport type { Transfer } from '../types/transfer';\n\nconst STRINGIFY_TYPE_TAG = '__type';\nconst BIGINT_TYPE = 'bigint';\nconst STRINGIFY_VALUE = 'value';\n\nexport function stringifyTransfer(transfer: Transfer): string {\n return JSON.stringify(transfer, (_key, value) => {\n if (typeof value === 'bigint') {\n return { [STRINGIFY_TYPE_TAG]: BIGINT_TYPE, [STRINGIFY_VALUE]: value.toString() };\n }\n\n return value;\n });\n}\n\nexport function parseTransfer(transferString: string): Transfer {\n const rawJSON = JSON.parse(transferString, (_key, value) => {\n if (\n value &&\n typeof value === 'object' &&\n value[STRINGIFY_TYPE_TAG] === BIGINT_TYPE &&\n typeof value[STRINGIFY_VALUE] === 'string'\n ) {\n return BigInt(value[STRINGIFY_VALUE]);\n }\n\n return value;\n });\n\n const parsed = LooseTransferSchema.safeParse(rawJSON);\n\n if (!parsed.success) {\n throw new Error(`Failed to parse transfer: ${parsed.error.message}`);\n }\n\n return parsed.data as Transfer;\n}\n\nconst AssetBaseSchema = z.object({\n name: z.string(),\n symbol: z.string(),\n decimals: z.int().nonnegative(),\n type: z.enum(TokenType),\n});\n\nconst NativeAssetSchema = AssetBaseSchema.extend({\n type: z.literal(TokenType.NATIVE),\n});\n\nconst LooseErc20AssetSchema = AssetBaseSchema.extend({\n type: z.literal(TokenType.ERC20),\n address: z.string(),\n});\n\nconst LooseSplAssetSchema = AssetBaseSchema.extend({\n type: z.literal(TokenType.SPL),\n address: z.string(),\n});\n\nconst LooseAssetSchema = z.discriminatedUnion('type', [NativeAssetSchema, LooseErc20AssetSchema, LooseSplAssetSchema]);\n\nconst ChainSchema = z.object({\n chainId: z.string(),\n chainName: z.string(),\n networkToken: NativeAssetSchema,\n rpcUrl: z.string(),\n utilityAddresses: z\n .object({\n multicall: z.string(),\n })\n .optional(),\n});\n\nconst LooseTransferSchema = z.object({\n amountIn: z.bigint().nonnegative(),\n amountOut: z.bigint().nonnegative(),\n completedAtMs: z.int().nonnegative().optional(),\n environment: z.enum(Environment),\n errorCode: z.number().optional(),\n errorReason: z.string().optional(),\n failedAtMs: z.int().nonnegative().optional(),\n fees: z.array(\n z.object({\n type: z.enum(['partner', 'protocol', 'gas', 'bridge', 'slippage', 'swap', 'other']),\n fundingModel: z.enum(['included', 'additive']).default('included'),\n name: z.string(),\n amount: z.bigint().nonnegative(),\n chainId: z.string(),\n token: z.discriminatedUnion('type', [\n z.object({\n type: z.literal(TokenType.NATIVE),\n }),\n z.object({\n type: z.literal(TokenType.ERC20),\n address: z.string(),\n }),\n z.object({\n type: z.literal(TokenType.SPL),\n address: z.string(),\n }),\n ]),\n }),\n ),\n fromAddress: z.string(),\n id: z.string(),\n metadata: z.record(z.string(), z.unknown()).optional(),\n partnerFeeBps: z.int().nonnegative().nullable(),\n refund: z\n .object({\n amount: z.bigint().nonnegative(),\n asset: LooseAssetSchema.nullable(),\n chainId: z.string(),\n txHash: z.string().nullable(),\n timestampMs: z.int().nonnegative(),\n })\n .optional(),\n source: z\n .object({\n confirmationCount: z.int().nonnegative(),\n requiredConfirmationCount: z.int().nonnegative(),\n startedAtMs: z.int().nonnegative(),\n targetStartBlockNumber: z.bigint().nonnegative().optional(),\n txHash: z.string(),\n })\n .optional(),\n target: z\n .object({\n confirmationCount: z.int().nonnegative(),\n requiredConfirmationCount: z.int().nonnegative(),\n startedAtMs: z.int().nonnegative(),\n txHash: z.string().optional(),\n })\n .nullable()\n .optional(),\n sourceAsset: LooseAssetSchema,\n sourceChain: ChainSchema,\n status: z.enum([\n 'created',\n 'source-pending',\n 'source-completed',\n 'target-pending',\n 'refunded',\n 'completed',\n 'failed',\n ]),\n targetAsset: LooseAssetSchema,\n targetChain: ChainSchema,\n toAddress: z.string(),\n type: z.enum(ServiceType),\n});\n"],"mappings":"sGAIA,MAAM,EAAqB,SACrB,EAAc,SACd,EAAkB,QAExB,SAAgB,EAAkB,EAA4B,CAC5D,OAAO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SAAUA,EAAAA,EAAE,OAAOA,EAAAA,EAAE,QAAQ,CAAEA,EAAAA,EAAE,SAAS,CAAC,CAAC,UAAU,CACtD,cAAeA,EAAAA,EAAE,KAAK,CAAC,aAAa,CAAC,UAAU,CAC/C,OAAQA,EAAAA,EACL,OAAO,CACN,OAAQA,EAAAA,EAAE,QAAQ,CAAC,aAAa,CAChC,MAAO,EAAiB,UAAU,CAClC,QAASA,EAAAA,EAAE,QAAQ,CACnB,OAAQA,EAAAA,EAAE,QAAQ,CAAC,UAAU,CAC7B,YAAaA,EAAAA,EAAE,KAAK,CAAC,aAAa,CACnC,CAAC,CACD,UAAU,CACb,OAAQA,EAAAA,EACL,OAAO,CACN,kBAAmBA,EAAAA,EAAE,KAAK,CAAC,aAAa,CACxC,0BAA2BA,EAAAA,EAAE,KAAK,CAAC,aAAa,CAChD,YAAaA,EAAAA,EAAE,KAAK,CAAC,aAAa,CAClC,uBAAwBA,EAAAA,EAAE,QAAQ,CAAC,aAAa,CAAC,UAAU,CAC3D,OAAQA,EAAAA,EAAE,QAAQ,CACnB,CAAC,CACD,UAAU,CACb,OAAQA,EAAAA,EACL,OAAO,CACN,kBAAmBA,EAAAA,EAAE,KAAK,CAAC,aAAa,CACxC,0BAA2BA,EAAAA,EAAE,KAAK,CAAC,aAAa,CAChD,YAAaA,EAAAA,EAAE,KAAK,CAAC,aAAa,CAClC,OAAQA,EAAAA,EAAE,QAAQ,CAAC,UAAU,CAC9B,CAAC,CACD,UAAU,CACV,UAAU,CACb,YAAa,EACb,YAAa,EACb,OAAQA,EAAAA,EAAE,KAAK,CACb,UACA,iBACA,mBACA,iBACA,WACA,YACA,SACD,CAAC,CACF,YAAa,EACb,YAAa,EACb,UAAWA,EAAAA,EAAE,QAAQ,CACrB,KAAMA,EAAAA,EAAE,KAAKG,EAAAA,YAAY,CAC1B,CAAC"}
1
+ {"version":3,"file":"transfer-utils.cjs","names":["z","TokenType","Environment","ServiceType"],"sources":["../../src/utils/transfer-utils.ts"],"sourcesContent":["import { z } from 'zod';\nimport { Environment, ServiceType, TokenType } from '../constants';\nimport type { Transfer } from '../types/transfer';\n\nconst STRINGIFY_TYPE_TAG = '__type';\nconst BIGINT_TYPE = 'bigint';\nconst STRINGIFY_VALUE = 'value';\n\nexport function stringifyTransfer(transfer: Transfer): string {\n return JSON.stringify(transfer, (_key, value) => {\n if (typeof value === 'bigint') {\n return { [STRINGIFY_TYPE_TAG]: BIGINT_TYPE, [STRINGIFY_VALUE]: value.toString() };\n }\n\n return value;\n });\n}\n\nexport function parseTransfer(transferString: string): Transfer {\n const rawJSON = JSON.parse(transferString, (_key, value) => {\n if (\n value &&\n typeof value === 'object' &&\n value[STRINGIFY_TYPE_TAG] === BIGINT_TYPE &&\n typeof value[STRINGIFY_VALUE] === 'string'\n ) {\n return BigInt(value[STRINGIFY_VALUE]);\n }\n\n return value;\n });\n\n const parsed = LooseTransferSchema.safeParse(rawJSON);\n\n if (!parsed.success) {\n throw new Error(`Failed to parse transfer: ${parsed.error.message}`);\n }\n\n return parsed.data as Transfer;\n}\n\nconst AssetBaseSchema = z.object({\n name: z.string(),\n symbol: z.string(),\n decimals: z.int().nonnegative(),\n type: z.enum(TokenType),\n});\n\nconst NativeAssetSchema = AssetBaseSchema.extend({\n type: z.literal(TokenType.NATIVE),\n});\n\nconst LooseErc20AssetSchema = AssetBaseSchema.extend({\n type: z.literal(TokenType.ERC20),\n address: z.string(),\n});\n\nconst LooseSplAssetSchema = AssetBaseSchema.extend({\n type: z.literal(TokenType.SPL),\n address: z.string(),\n});\n\nconst LooseAssetSchema = z.discriminatedUnion('type', [NativeAssetSchema, LooseErc20AssetSchema, LooseSplAssetSchema]);\n\nconst ChainSchema = z.object({\n chainId: z.string(),\n chainName: z.string(),\n networkToken: NativeAssetSchema,\n rpcUrl: z.string(),\n utilityAddresses: z\n .object({\n multicall: z.string(),\n })\n .optional(),\n});\n\nconst LooseTransferSchema = z.object({\n amountIn: z.bigint().nonnegative(),\n amountOut: z.bigint().nonnegative(),\n completedAtMs: z.int().nonnegative().optional(),\n environment: z.enum(Environment),\n errorCode: z.number().optional(),\n errorReason: z.string().optional(),\n failedAtMs: z.int().nonnegative().optional(),\n fees: z.array(\n z.object({\n type: z.enum(['partner', 'protocol', 'gas', 'bridge', 'slippage', 'swap', 'other', 'relay']),\n fundingModel: z.enum(['included', 'additive']).default('included'),\n name: z.string(),\n amount: z.bigint().nonnegative(),\n chainId: z.string(),\n token: z.discriminatedUnion('type', [\n z.object({\n type: z.literal(TokenType.NATIVE),\n }),\n z.object({\n type: z.literal(TokenType.ERC20),\n address: z.string(),\n }),\n z.object({\n type: z.literal(TokenType.SPL),\n address: z.string(),\n }),\n ]),\n }),\n ),\n fromAddress: z.string(),\n id: z.string(),\n metadata: z.record(z.string(), z.unknown()).optional(),\n partnerFeeBps: z.int().nonnegative().nullable(),\n refund: z\n .object({\n amount: z.bigint().nonnegative(),\n asset: LooseAssetSchema.nullable(),\n chainId: z.string(),\n txHash: z.string().nullable(),\n timestampMs: z.int().nonnegative(),\n })\n .optional(),\n source: z\n .object({\n confirmationCount: z.int().nonnegative(),\n requiredConfirmationCount: z.int().nonnegative(),\n startedAtMs: z.int().nonnegative(),\n targetStartBlockNumber: z.bigint().nonnegative().optional(),\n txHash: z.string(),\n })\n .optional(),\n target: z\n .object({\n confirmationCount: z.int().nonnegative(),\n requiredConfirmationCount: z.int().nonnegative(),\n startedAtMs: z.int().nonnegative(),\n txHash: z.string().optional(),\n })\n .nullable()\n .optional(),\n sourceAsset: LooseAssetSchema,\n sourceChain: ChainSchema,\n status: z.enum([\n 'created',\n 'source-pending',\n 'source-completed',\n 'target-pending',\n 'refunded',\n 'completed',\n 'failed',\n ]),\n targetAsset: LooseAssetSchema,\n targetChain: ChainSchema,\n toAddress: z.string(),\n type: z.enum(ServiceType),\n});\n"],"mappings":"sGAIA,MAAM,EAAqB,SACrB,EAAc,SACd,EAAkB,QAExB,SAAgB,EAAkB,EAA4B,CAC5D,OAAO,KAAK,UAA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bigint`,o=`value`;function s(e){return JSON.stringify(e,(e,t)=>typeof t==`bigint`?{[i]:a,[o]:t.toString()}:t)}function c(e){let t=JSON.parse(e,(e,t)=>t&&typeof t==`object`&&t[i]===a&&typeof t[o]==`string`?BigInt(t[o]):t),n=h.safeParse(t);if(!n.success)throw Error(`Failed to parse transfer: ${n.error.message}`);return n.data}const l=r.object({name:r.string(),symbol:r.string(),decimals:r.int().nonnegative(),type:r.enum(n)}),u=l.extend({type:r.literal(n.NATIVE)}),d=l.extend({type:r.literal(n.ERC20),address:r.string()}),f=l.extend({type:r.literal(n.SPL),address:r.string()}),p=r.discriminatedUnion(`type`,[u,d,f]),m=r.object({chainId:r.string(),chainName:r.string(),networkToken:u,rpcUrl:r.string(),utilityAddresses:r.object({multicall:r.string()}).optional()}),h=r.object({amountIn:r.bigint().nonnegative(),amountOut:r.bigint().nonnegative(),completedAtMs:r.int().nonnegative().optional(),environment:r.enum(e),errorCode:r.number().optional(),errorReason:r.string().optional(),failedAtMs:r.int().nonnegative().optional(),fees:r.array(r.object({type:r.enum([`partner`,`protocol`,`gas`,`bridge`,`slippage`,`swap`,`other`]),fundingModel:r.enum([`included`,`additive`]).default(`included`),name:r.string(),amount:r.bigint().nonnegative(),chainId:r.string(),token:r.discriminatedUnion(`type`,[r.object({type:r.literal(n.NATIVE)}),r.object({type:r.literal(n.ERC20),address:r.string()}),r.object({type:r.literal(n.SPL),address:r.string()})])})),fromAddress:r.string(),id:r.string(),metadata:r.record(r.string(),r.unknown()).optional(),partnerFeeBps:r.int().nonnegative().nullable(),refund:r.object({amount:r.bigint().nonnegative(),asset:p.nullable(),chainId:r.string(),txHash:r.string().nullable(),timestampMs:r.int().nonnegative()}).optional(),source:r.object({confirmationCount:r.int().nonnegative(),requiredConfirmationCount:r.int().nonnegative(),startedAtMs:r.int().nonnegative(),targetStartBlockNumber:r.bigint().nonnegative().optional(),txHash:r.string()}).optional(),target:r.object({confirmationCount:r.int().nonnegative(),requiredConfirmationCount:r.int().nonnegative(),startedAtMs:r.int().nonnegative(),txHash:r.string().optional()}).nullable().optional(),sourceAsset:p,sourceChain:m,status:r.enum([`created`,`source-pending`,`source-completed`,`target-pending`,`refunded`,`completed`,`failed`]),targetAsset:p,targetChain:m,toAddress:r.string(),type:r.enum(t)});export{c as parseTransfer,s as stringifyTransfer};
1
+ import{Environment as e,ServiceType as t,TokenType as n}from"../constants.js";import{z as r}from"zod";const i=`__type`,a=`bigint`,o=`value`;function s(e){return JSON.stringify(e,(e,t)=>typeof t==`bigint`?{[i]:a,[o]:t.toString()}:t)}function c(e){let t=JSON.parse(e,(e,t)=>t&&typeof t==`object`&&t[i]===a&&typeof t[o]==`string`?BigInt(t[o]):t),n=h.safeParse(t);if(!n.success)throw Error(`Failed to parse transfer: ${n.error.message}`);return n.data}const l=r.object({name:r.string(),symbol:r.string(),decimals:r.int().nonnegative(),type:r.enum(n)}),u=l.extend({type:r.literal(n.NATIVE)}),d=l.extend({type:r.literal(n.ERC20),address:r.string()}),f=l.extend({type:r.literal(n.SPL),address:r.string()}),p=r.discriminatedUnion(`type`,[u,d,f]),m=r.object({chainId:r.string(),chainName:r.string(),networkToken:u,rpcUrl:r.string(),utilityAddresses:r.object({multicall:r.string()}).optional()}),h=r.object({amountIn:r.bigint().nonnegative(),amountOut:r.bigint().nonnegative(),completedAtMs:r.int().nonnegative().optional(),environment:r.enum(e),errorCode:r.number().optional(),errorReason:r.string().optional(),failedAtMs:r.int().nonnegative().optional(),fees:r.array(r.object({type:r.enum([`partner`,`protocol`,`gas`,`bridge`,`slippage`,`swap`,`other`,`relay`]),fundingModel:r.enum([`included`,`additive`]).default(`included`),name:r.string(),amount:r.bigint().nonnegative(),chainId:r.string(),token:r.discriminatedUnion(`type`,[r.object({type:r.literal(n.NATIVE)}),r.object({type:r.literal(n.ERC20),address:r.string()}),r.object({type:r.literal(n.SPL),address:r.string()})])})),fromAddress:r.string(),id:r.string(),metadata:r.record(r.string(),r.unknown()).optional(),partnerFeeBps:r.int().nonnegative().nullable(),refund:r.object({amount:r.bigint().nonnegative(),asset:p.nullable(),chainId:r.string(),txHash:r.string().nullable(),timestampMs:r.int().nonnegative()}).optional(),source:r.object({confirmationCount:r.int().nonnegative(),requiredConfirmationCount:r.int().nonnegative(),startedAtMs:r.int().nonnegative(),targetStartBlockNumber:r.bigint().nonnegative().optional(),txHash:r.string()}).optional(),target:r.object({confirmationCount:r.int().nonnegative(),requiredConfirmationCount:r.int().nonnegative(),startedAtMs:r.int().nonnegative(),txHash:r.string().optional()}).nullable().optional(),sourceAsset:p,sourceChain:m,status:r.enum([`created`,`source-pending`,`source-completed`,`target-pending`,`refunded`,`completed`,`failed`]),targetAsset:p,targetChain:m,toAddress:r.string(),type:r.enum(t)});export{c as parseTransfer,s as stringifyTransfer};
2
2
  //# sourceMappingURL=transfer-utils.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"transfer-utils.js","names":[],"sources":["../../src/utils/transfer-utils.ts"],"sourcesContent":["import { z } from 'zod';\nimport { Environment, ServiceType, TokenType } from '../constants';\nimport type { Transfer } from '../types/transfer';\n\nconst STRINGIFY_TYPE_TAG = '__type';\nconst BIGINT_TYPE = 'bigint';\nconst STRINGIFY_VALUE = 'value';\n\nexport function stringifyTransfer(transfer: Transfer): string {\n return JSON.stringify(transfer, (_key, value) => {\n if (typeof value === 'bigint') {\n return { [STRINGIFY_TYPE_TAG]: BIGINT_TYPE, [STRINGIFY_VALUE]: value.toString() };\n }\n\n return value;\n });\n}\n\nexport function parseTransfer(transferString: string): Transfer {\n const rawJSON = JSON.parse(transferString, (_key, value) => {\n if (\n value &&\n typeof value === 'object' &&\n value[STRINGIFY_TYPE_TAG] === BIGINT_TYPE &&\n typeof value[STRINGIFY_VALUE] === 'string'\n ) {\n return BigInt(value[STRINGIFY_VALUE]);\n }\n\n return value;\n });\n\n const parsed = LooseTransferSchema.safeParse(rawJSON);\n\n if (!parsed.success) {\n throw new Error(`Failed to parse transfer: ${parsed.error.message}`);\n }\n\n return parsed.data as Transfer;\n}\n\nconst AssetBaseSchema = z.object({\n name: z.string(),\n symbol: z.string(),\n decimals: z.int().nonnegative(),\n type: z.enum(TokenType),\n});\n\nconst NativeAssetSchema = AssetBaseSchema.extend({\n type: z.literal(TokenType.NATIVE),\n});\n\nconst LooseErc20AssetSchema = AssetBaseSchema.extend({\n type: z.literal(TokenType.ERC20),\n address: z.string(),\n});\n\nconst LooseSplAssetSchema = AssetBaseSchema.extend({\n type: z.literal(TokenType.SPL),\n address: z.string(),\n});\n\nconst LooseAssetSchema = z.discriminatedUnion('type', [NativeAssetSchema, LooseErc20AssetSchema, LooseSplAssetSchema]);\n\nconst ChainSchema = z.object({\n chainId: z.string(),\n chainName: z.string(),\n networkToken: NativeAssetSchema,\n rpcUrl: z.string(),\n utilityAddresses: z\n .object({\n multicall: z.string(),\n })\n .optional(),\n});\n\nconst LooseTransferSchema = z.object({\n amountIn: z.bigint().nonnegative(),\n amountOut: z.bigint().nonnegative(),\n completedAtMs: z.int().nonnegative().optional(),\n environment: z.enum(Environment),\n errorCode: z.number().optional(),\n errorReason: z.string().optional(),\n failedAtMs: z.int().nonnegative().optional(),\n fees: z.array(\n z.object({\n type: z.enum(['partner', 'protocol', 'gas', 'bridge', 'slippage', 'swap', 'other']),\n fundingModel: z.enum(['included', 'additive']).default('included'),\n name: z.string(),\n amount: z.bigint().nonnegative(),\n chainId: z.string(),\n token: z.discriminatedUnion('type', [\n z.object({\n type: z.literal(TokenType.NATIVE),\n }),\n z.object({\n type: z.literal(TokenType.ERC20),\n address: z.string(),\n }),\n z.object({\n type: z.literal(TokenType.SPL),\n address: z.string(),\n }),\n ]),\n }),\n ),\n fromAddress: z.string(),\n id: z.string(),\n metadata: z.record(z.string(), z.unknown()).optional(),\n partnerFeeBps: z.int().nonnegative().nullable(),\n refund: z\n .object({\n amount: z.bigint().nonnegative(),\n asset: LooseAssetSchema.nullable(),\n chainId: z.string(),\n txHash: z.string().nullable(),\n timestampMs: z.int().nonnegative(),\n })\n .optional(),\n source: z\n .object({\n confirmationCount: z.int().nonnegative(),\n requiredConfirmationCount: z.int().nonnegative(),\n startedAtMs: z.int().nonnegative(),\n targetStartBlockNumber: z.bigint().nonnegative().optional(),\n txHash: z.string(),\n })\n .optional(),\n target: z\n .object({\n confirmationCount: z.int().nonnegative(),\n requiredConfirmationCount: z.int().nonnegative(),\n startedAtMs: z.int().nonnegative(),\n txHash: z.string().optional(),\n })\n .nullable()\n .optional(),\n sourceAsset: LooseAssetSchema,\n sourceChain: ChainSchema,\n status: z.enum([\n 'created',\n 'source-pending',\n 'source-completed',\n 'target-pending',\n 'refunded',\n 'completed',\n 'failed',\n ]),\n targetAsset: LooseAssetSchema,\n targetChain: ChainSchema,\n toAddress: z.string(),\n type: z.enum(ServiceType),\n});\n"],"mappings":"sGAIA,MAAM,EAAqB,SACrB,EAAc,SACd,EAAkB,QAExB,SAAgB,EAAkB,EAA4B,CAC5D,OAAO,KAAK,UAAU,GAAW,EAAM,IACjC,OAAO,GAAU,SACZ,EAAG,GAAqB,GAAc,GAAkB,EAAM,UAAU,CAAE,CAG5E,EACP,CAGJ,SAAgB,EAAc,EAAkC,CAC9D,IAAM,EAAU,KAAK,MAAM,GAAiB,EAAM,IAE9C,GACA,OAAO,GAAU,UACjB,EAAM,KAAwB,GAC9B,OAAO,EAAM,IAAqB,SAE3B,OAAO,EAAM,GAAiB,CAGhC,EACP,CAEI,EAAS,EAAoB,UAAU,EAAQ,CAErD,GAAI,CAAC,EAAO,QACV,MAAU,MAAM,6BAA6B,EAAO,MAAM,UAAU,CAGtE,OAAO,EAAO,KAGhB,MAAM,EAAkB,EAAE,OAAO,CAC/B,KAAM,EAAE,QAAQ,CAChB,OAAQ,EAAE,QAAQ,CAClB,SAAU,EAAE,KAAK,CAAC,aAAa,CAC/B,KAAM,EAAE,KAAK,EAAU,CACxB,CAAC,CAEI,EAAoB,EAAgB,OAAO,CAC/C,KAAM,EAAE,QAAQ,EAAU,OAAO,CAClC,CAAC,CAEI,EAAwB,E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AO,CACP,KAAM,EAAE,QAAQ,EAAU,IAAI,CAC9B,QAAS,EAAE,QAAQ,CACpB,CAAC,CACH,CAAC,CACH,CAAC,CACH,CACD,YAAa,EAAE,QAAQ,CACvB,GAAI,EAAE,QAAQ,CACd,SAAU,EAAE,OAAO,EAAE,QAAQ,CAAE,EAAE,SAAS,CAAC,CAAC,UAAU,CACtD,cAAe,EAAE,KAAK,CAAC,aAAa,CAAC,UAAU,CAC/C,OAAQ,EACL,OAAO,CACN,OAAQ,EAAE,QAAQ,CAAC,aAAa,CAChC,MAAO,EAAiB,UAAU,CAClC,QAAS,EAAE,QAAQ,CACnB,OAAQ,EAAE,QAAQ,CAAC,UAAU,CAC7B,YAAa,EAAE,KAAK,CAAC,aAAa,CACnC,CAAC,CACD,UAAU,CACb,OAAQ,EACL,OAAO,CACN,kBAAmB,EAAE,KAAK,CAAC,aAAa,CACxC,0BAA2B,EAAE,KAAK,CAAC,aAAa,CAChD,YAAa,EAAE,KAAK,CAAC,aAAa,CAClC,uBAAwB,EAAE,QAAQ,CAAC,aAAa,CAAC,UAAU,CAC3D,OAAQ,EAAE,QAAQ,CACnB,CAAC,CACD,UAAU,CACb,OAAQ,EACL,OAAO,CACN,kBAAmB,EAAE,KAAK,CAAC,aAAa,CACxC,0BAA2B,EAAE,KAAK,CAAC,aAAa,CAChD,YAAa,EAAE,KAAK,CAAC,aAAa,CAClC,OAAQ,EAAE,QAAQ,CAAC,UAAU,CAC9B,CAAC,CACD,UAAU,CACV,UAAU,CACb,YAAa,EACb,YAAa,EACb,OAAQ,EAAE,KAAK,CACb,UACA,iBACA,mBACA,iBACA,WACA,YACA,SACD,CAAC,CACF,YAAa,EACb,YAAa,EACb,UAAW,EAAE,QAAQ,CACrB,KAAM,EAAE,KAAK,EAAY,CAC1B,CAAC"}
1
+ {"version":3,"file":"transfer-utils.js","names":[],"sources":["../../src/utils/transfer-utils.ts"],"sourcesContent":["import { z } from 'zod';\nimport { Environment, ServiceType, TokenType } from '../constants';\nimport type { Transfer } from '../types/transfer';\n\nconst STRINGIFY_TYPE_TAG = '__type';\nconst BIGINT_TYPE = 'bigint';\nconst STRINGIFY_VALUE = 'value';\n\nexport function stringifyTransfer(transfer: Transfer): string {\n return JSON.stringify(transfer, (_key, value) => {\n if (typeof value === 'bigint') {\n return { [STRINGIFY_TYPE_TAG]: BIGINT_TYPE, [STRINGIFY_VALUE]: value.toString() };\n }\n\n return value;\n });\n}\n\nexport function parseTransfer(transferString: string): Transfer {\n const rawJSON = JSON.parse(transferString, (_key, value) => {\n if (\n value &&\n typeof value === 'object' &&\n value[STRINGIFY_TYPE_TAG] === BIGINT_TYPE &&\n typeof value[STRINGIFY_VALUE] === 'string'\n ) {\n return BigInt(value[STRINGIFY_VALUE]);\n }\n\n return value;\n });\n\n const parsed = LooseTransferSchema.safeParse(rawJSON);\n\n if (!parsed.success) {\n throw new Error(`Failed to parse transfer: ${parsed.error.message}`);\n }\n\n return parsed.data as Transfer;\n}\n\nconst AssetBaseSchema = z.object({\n name: z.string(),\n symbol: z.string(),\n decimals: z.int().nonnegative(),\n type: z.enum(TokenType),\n});\n\nconst NativeAssetSchema = AssetBaseSchema.extend({\n type: z.literal(TokenType.NATIVE),\n});\n\nconst LooseErc20AssetSchema = AssetBaseSchema.extend({\n type: z.literal(TokenType.ERC20),\n address: z.string(),\n});\n\nconst LooseSplAssetSchema = AssetBaseSchema.extend({\n type: z.literal(TokenType.SPL),\n address: z.string(),\n});\n\nconst LooseAssetSchema = z.discriminatedUnion('type', [NativeAssetSchema, LooseErc20AssetSchema, LooseSplAssetSchema]);\n\nconst ChainSchema = z.object({\n chainId: z.string(),\n chainName: z.string(),\n networkToken: NativeAssetSchema,\n rpcUrl: z.string(),\n utilityAddresses: z\n .object({\n multicall: z.string(),\n })\n .optional(),\n});\n\nconst LooseTransferSchema = z.object({\n amountIn: z.bigint().nonnegative(),\n amountOut: z.bigint().nonnegative(),\n completedAtMs: z.int().nonnegative().optional(),\n environment: z.enum(Environment),\n errorCode: z.number().optional(),\n errorReason: z.string().optional(),\n failedAtMs: z.int().nonnegative().optional(),\n fees: z.array(\n z.object({\n type: z.enum(['partner', 'protocol', 'gas', 'bridge', 'slippage', 'swap', 'other', 'relay']),\n fundingModel: z.enum(['included', 'additive']).default('included'),\n name: z.string(),\n amount: z.bigint().nonnegative(),\n chainId: z.string(),\n token: z.discriminatedUnion('type', [\n z.object({\n type: z.literal(TokenType.NATIVE),\n }),\n z.object({\n type: z.literal(TokenType.ERC20),\n address: z.string(),\n }),\n z.object({\n type: z.literal(TokenType.SPL),\n address: z.string(),\n }),\n ]),\n }),\n ),\n fromAddress: z.string(),\n id: z.string(),\n metadata: z.record(z.string(), z.unknown()).optional(),\n partnerFeeBps: z.int().nonnegative().nullable(),\n refund: z\n .object({\n amount: z.bigint().nonnegative(),\n asset: LooseAssetSchema.nullable(),\n chainId: z.string(),\n txHash: z.string().nullable(),\n timestampMs: z.int().nonnegative(),\n })\n .optional(),\n source: z\n .object({\n confirmationCount: z.int().nonnegative(),\n requiredConfirmationCount: z.int().nonnegative(),\n startedAtMs: z.int().nonnegative(),\n targetStartBlockNumber: z.bigint().nonnegative().optional(),\n txHash: z.string(),\n })\n .optional(),\n target: z\n .object({\n confirmationCount: z.int().nonnegative(),\n requiredConfirmationCount: z.int().nonnegative(),\n startedAtMs: z.int().nonnegative(),\n txHash: z.string().optional(),\n })\n .nullable()\n .optional(),\n sourceAsset: LooseAssetSchema,\n sourceChain: ChainSchema,\n status: z.enum([\n 'created',\n 'source-pending',\n 'source-completed',\n 'target-pending',\n 'refunded',\n 'completed',\n 'failed',\n ]),\n targetAsset: LooseAssetSchema,\n targetChain: ChainSchema,\n toAddress: z.string(),\n type: z.enum(ServiceType),\n});\n"],"mappings":"sGAIA,MAAM,EAAqB,SACrB,EAAc,SACd,EAAkB,QAExB,SAAgB,EAAkB,EAA4B,CAC5D,OAAO,KAAK,UAAU,GAAW,EAAM,IACjC,OAAO,GAAU,SACZ,EAAG,GAAqB,GAA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AAwB,EAAE,QAAQ,CAAC,aAAa,CAAC,UAAU,CAC3D,OAAQ,EAAE,QAAQ,CACnB,CAAC,CACD,UAAU,CACb,OAAQ,EACL,OAAO,CACN,kBAAmB,EAAE,KAAK,CAAC,aAAa,CACxC,0BAA2B,EAAE,KAAK,CAAC,aAAa,CAChD,YAAa,EAAE,KAAK,CAAC,aAAa,CAClC,OAAQ,EAAE,QAAQ,CAAC,UAAU,CAC9B,CAAC,CACD,UAAU,CACV,UAAU,CACb,YAAa,EACb,YAAa,EACb,OAAQ,EAAE,KAAK,CACb,UACA,iBACA,mBACA,iBACA,WACA,YACA,SACD,CAAC,CACF,YAAa,EACb,YAAa,EACb,UAAW,EAAE,QAAQ,CACrB,KAAM,EAAE,KAAK,EAAY,CAC1B,CAAC"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@avalabs/fusion-sdk",
3
3
  "license": "Limited Ecosystem License",
4
- "version": "0.15.1",
4
+ "version": "0.16.0",
5
5
  "type": "module",
6
6
  "main": "./dist/mod.cjs",
7
7
  "module": "./dist/mod.js",