@avalabs/fusion-sdk 0.12.0 → 0.14.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.
Files changed (41) hide show
  1. package/dist/mod.cjs +1 -1
  2. package/dist/mod.d.cts +3 -2
  3. package/dist/mod.d.ts +3 -2
  4. package/dist/mod.js +1 -1
  5. package/dist/quoter/quoter.cjs +1 -1
  6. package/dist/quoter/quoter.cjs.map +1 -1
  7. package/dist/quoter/quoter.js +1 -1
  8. package/dist/quoter/quoter.js.map +1 -1
  9. package/dist/transfer-service/avalanche-evm/_handlers/stream-quotes.cjs +1 -1
  10. package/dist/transfer-service/avalanche-evm/_handlers/stream-quotes.cjs.map +1 -1
  11. package/dist/transfer-service/avalanche-evm/_handlers/stream-quotes.js +1 -1
  12. package/dist/transfer-service/avalanche-evm/_handlers/stream-quotes.js.map +1 -1
  13. package/dist/transfer-service/lombard/btc-to-btcb/_handlers/stream-quotes.cjs +1 -1
  14. package/dist/transfer-service/lombard/btc-to-btcb/_handlers/stream-quotes.cjs.map +1 -1
  15. package/dist/transfer-service/lombard/btc-to-btcb/_handlers/stream-quotes.js +1 -1
  16. package/dist/transfer-service/lombard/btc-to-btcb/_handlers/stream-quotes.js.map +1 -1
  17. package/dist/transfer-service/lombard/btcb-to-btc/_handlers/stream-quotes.cjs +1 -1
  18. package/dist/transfer-service/lombard/btcb-to-btc/_handlers/stream-quotes.cjs.map +1 -1
  19. package/dist/transfer-service/lombard/btcb-to-btc/_handlers/stream-quotes.js +1 -1
  20. package/dist/transfer-service/lombard/btcb-to-btc/_handlers/stream-quotes.js.map +1 -1
  21. package/dist/transfer-service/markr/_schema.cjs +1 -1
  22. package/dist/transfer-service/markr/_schema.cjs.map +1 -1
  23. package/dist/transfer-service/markr/_schema.js +1 -1
  24. package/dist/transfer-service/markr/_schema.js.map +1 -1
  25. package/dist/transfer-service/markr/_utils.cjs +1 -1
  26. package/dist/transfer-service/markr/_utils.cjs.map +1 -1
  27. package/dist/transfer-service/markr/_utils.js +1 -1
  28. package/dist/transfer-service/markr/_utils.js.map +1 -1
  29. package/dist/types/quote.d.cts +30 -5
  30. package/dist/types/quote.d.ts +30 -5
  31. package/dist/utils/price-impact.cjs +2 -0
  32. package/dist/utils/price-impact.cjs.map +1 -0
  33. package/dist/utils/price-impact.d.cts +31 -0
  34. package/dist/utils/price-impact.d.ts +31 -0
  35. package/dist/utils/price-impact.js +2 -0
  36. package/dist/utils/price-impact.js.map +1 -0
  37. package/dist/utils/transfer-utils.cjs +1 -1
  38. package/dist/utils/transfer-utils.cjs.map +1 -1
  39. package/dist/utils/transfer-utils.js +1 -1
  40. package/dist/utils/transfer-utils.js.map +1 -1
  41. package/package.json +3 -3
package/dist/mod.cjs CHANGED
@@ -1 +1 @@
1
- Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require(`./constants.cjs`),t=require(`./errors.cjs`),n=require(`./type-guards.cjs`),r=require(`./transfer-manager.cjs`),i=require(`./utils/caip.cjs`),a=require(`./utils/transfer-utils.cjs`);exports.AVALANCHE_FUJI_CHAIN=e.AVALANCHE_FUJI_CHAIN,exports.AVALANCHE_MAINNET_CHAIN=e.AVALANCHE_MAINNET_CHAIN,exports.AbortedError=t.AbortedError,exports.AvalancheChainIds=e.AvalancheChainIds,exports.BITCOIN_MAINNET_CHAIN=e.BITCOIN_MAINNET_CHAIN,exports.BITCOIN_TESTNET_CHAIN=e.BITCOIN_TESTNET_CHAIN,exports.BTC_SERVICE_TYPES=e.BTC_SERVICE_TYPES,exports.BitcoinChainIds=e.BitcoinChainIds,exports.Blockchain=e.Blockchain,exports.ERC_ZERO_ADDRESS=e.ERC_ZERO_ADDRESS,exports.ETHEREUM_MAINNET_CHAIN=e.ETHEREUM_MAINNET_CHAIN,exports.ETHEREUM_SEPOLIA_CHAIN=e.ETHEREUM_SEPOLIA_CHAIN,exports.EVM_SERVICE_TYPES=e.EVM_SERVICE_TYPES,exports.Environment=e.Environment,exports.ErrorCode=t.ErrorCode,exports.ErrorReason=t.ErrorReason,exports.EthereumChainIds=e.EthereumChainIds,exports.EvmChainId=e.EvmChainId,exports.FEE_RATE_TIER_TO_BITCOIN=e.FEE_RATE_TIER_TO_BITCOIN,exports.HttpError=t.HttpError,exports.InvalidParamsError=t.InvalidParamsError,exports.NATIVE_AVAX=e.NATIVE_AVAX,exports.NATIVE_ETH=e.NATIVE_ETH,exports.NATIVE_SOL_ADDRESS=e.NATIVE_SOL_ADDRESS,exports.ResponseValidationError=t.ResponseValidationError,exports.SOLANA_DEVNET_CHAIN=e.SOLANA_DEVNET_CHAIN,exports.SOLANA_MAINNET_CHAIN=e.SOLANA_MAINNET_CHAIN,exports.SdkError=t.SdkError,exports.ServiceInitializationError=t.ServiceInitializationError,exports.ServiceType=e.ServiceType,exports.ServiceUnavailableError=t.ServiceUnavailableError,exports.SolanaChainIds=e.SolanaChainIds,exports.TimeoutError=t.TimeoutError,exports.TokenType=e.TokenType,exports.TransferSignatureReason=e.TransferSignatureReason,exports.caip2ToEip155ChainId=i.caip2ToEip155ChainId,exports.caip2ToEip155HexChainId=i.caip2ToEip155HexChainId,exports.createTransferManager=r.createTransferManager,exports.eip155ChainIdToCaip2=i.eip155ChainIdToCaip2,exports.isAbortedError=t.isAbortedError,exports.isCaip2ChainId=i.isCaip2ChainId,exports.isEnvironment=n.isEnvironment,exports.isErc20Asset=n.isErc20Asset,exports.isEvmBridgeInitializer=n.isEvmBridgeInitializer,exports.isHttpError=t.isHttpError,exports.isInvalidParamsError=t.isInvalidParamsError,exports.isLombardServiceInitializer=n.isLombardServiceInitializer,exports.isMarkrServiceInitializer=n.isMarkrServiceInitializer,exports.isNativeAsset=n.isNativeAsset,exports.isResponseValidationError=t.isResponseValidationError,exports.isSdkError=t.isSdkError,exports.isServiceInitializationError=t.isServiceInitializationError,exports.isServiceInitializer=n.isServiceInitializer,exports.isServiceUnavailableError=t.isServiceUnavailableError,exports.isSplAsset=n.isSplAsset,exports.isTimeoutError=t.isTimeoutError,exports.parseTransfer=a.parseTransfer,exports.splitCaip2ChainId=i.splitCaip2ChainId,exports.stringifyTransfer=a.stringifyTransfer;
1
+ Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require(`./constants.cjs`),t=require(`./errors.cjs`),n=require(`./type-guards.cjs`),r=require(`./transfer-manager.cjs`),i=require(`./utils/caip.cjs`),a=require(`./utils/price-impact.cjs`),o=require(`./utils/transfer-utils.cjs`);exports.AVALANCHE_FUJI_CHAIN=e.AVALANCHE_FUJI_CHAIN,exports.AVALANCHE_MAINNET_CHAIN=e.AVALANCHE_MAINNET_CHAIN,exports.AbortedError=t.AbortedError,exports.AvalancheChainIds=e.AvalancheChainIds,exports.BITCOIN_MAINNET_CHAIN=e.BITCOIN_MAINNET_CHAIN,exports.BITCOIN_TESTNET_CHAIN=e.BITCOIN_TESTNET_CHAIN,exports.BTC_SERVICE_TYPES=e.BTC_SERVICE_TYPES,exports.BitcoinChainIds=e.BitcoinChainIds,exports.Blockchain=e.Blockchain,exports.ERC_ZERO_ADDRESS=e.ERC_ZERO_ADDRESS,exports.ETHEREUM_MAINNET_CHAIN=e.ETHEREUM_MAINNET_CHAIN,exports.ETHEREUM_SEPOLIA_CHAIN=e.ETHEREUM_SEPOLIA_CHAIN,exports.EVM_SERVICE_TYPES=e.EVM_SERVICE_TYPES,exports.Environment=e.Environment,exports.ErrorCode=t.ErrorCode,exports.ErrorReason=t.ErrorReason,exports.EthereumChainIds=e.EthereumChainIds,exports.EvmChainId=e.EvmChainId,exports.FEE_RATE_TIER_TO_BITCOIN=e.FEE_RATE_TIER_TO_BITCOIN,exports.HttpError=t.HttpError,exports.InvalidParamsError=t.InvalidParamsError,exports.NATIVE_AVAX=e.NATIVE_AVAX,exports.NATIVE_ETH=e.NATIVE_ETH,exports.NATIVE_SOL_ADDRESS=e.NATIVE_SOL_ADDRESS,exports.ResponseValidationError=t.ResponseValidationError,exports.SOLANA_DEVNET_CHAIN=e.SOLANA_DEVNET_CHAIN,exports.SOLANA_MAINNET_CHAIN=e.SOLANA_MAINNET_CHAIN,exports.SdkError=t.SdkError,exports.ServiceInitializationError=t.ServiceInitializationError,exports.ServiceType=e.ServiceType,exports.ServiceUnavailableError=t.ServiceUnavailableError,exports.SolanaChainIds=e.SolanaChainIds,exports.TimeoutError=t.TimeoutError,exports.TokenType=e.TokenType,exports.TransferSignatureReason=e.TransferSignatureReason,exports.caip2ToEip155ChainId=i.caip2ToEip155ChainId,exports.caip2ToEip155HexChainId=i.caip2ToEip155HexChainId,exports.calculatePriceImpactFromQuote=a.calculatePriceImpactFromQuote,exports.createTransferManager=r.createTransferManager,exports.eip155ChainIdToCaip2=i.eip155ChainIdToCaip2,exports.isAbortedError=t.isAbortedError,exports.isCaip2ChainId=i.isCaip2ChainId,exports.isEnvironment=n.isEnvironment,exports.isErc20Asset=n.isErc20Asset,exports.isEvmBridgeInitializer=n.isEvmBridgeInitializer,exports.isHttpError=t.isHttpError,exports.isInvalidParamsError=t.isInvalidParamsError,exports.isLombardServiceInitializer=n.isLombardServiceInitializer,exports.isMarkrServiceInitializer=n.isMarkrServiceInitializer,exports.isNativeAsset=n.isNativeAsset,exports.isResponseValidationError=t.isResponseValidationError,exports.isSdkError=t.isSdkError,exports.isServiceInitializationError=t.isServiceInitializationError,exports.isServiceInitializer=n.isServiceInitializer,exports.isServiceUnavailableError=t.isServiceUnavailableError,exports.isSplAsset=n.isSplAsset,exports.isTimeoutError=t.isTimeoutError,exports.parseTransfer=o.parseTransfer,exports.splitCaip2ChainId=i.splitCaip2ChainId,exports.stringifyTransfer=o.stringifyTransfer;
package/dist/mod.d.cts CHANGED
@@ -4,7 +4,7 @@ import { Chain } from "./types/chain.cjs";
4
4
  import { FeeRateTier } from "./types/fee.cjs";
5
5
  import { AVALANCHE_FUJI_CHAIN, AVALANCHE_MAINNET_CHAIN, AvalancheChainIds, BITCOIN_MAINNET_CHAIN, BITCOIN_TESTNET_CHAIN, BTC_SERVICE_TYPES, BitcoinChainIds, Blockchain, ERC_ZERO_ADDRESS, ETHEREUM_MAINNET_CHAIN, ETHEREUM_SEPOLIA_CHAIN, EVM_SERVICE_TYPES, Environment, EthereumChainIds, EvmChainId, FEE_RATE_TIER_TO_BITCOIN, NATIVE_AVAX, NATIVE_ETH, NATIVE_SOL_ADDRESS, SOLANA_DEVNET_CHAIN, SOLANA_MAINNET_CHAIN, ServiceType, SolanaChainIds, TokenType, TransferSignatureReason } from "./constants.cjs";
6
6
  import { Asset, BridgeableUiAsset, ChainAssetMap, DestinationInfo, Erc20Asset, NativeAsset, SplAsset, TransferableAsset } from "./types/asset.cjs";
7
- import { Quote, QuoteFee, QuoteFeeToken, QuoteFeeType, QuoteFees, QuoterDoneReason, QuoterEventArgs, QuoterEventHandler, QuoterInterface, QuoterProps, QuotesTuple, ServiceQuoteEventArgs, ServiceQuoteEventHandler } from "./types/quote.cjs";
7
+ import { Quote, QuoteFee, QuoteFeeFundingModel, QuoteFeeToken, QuoteFeeType, QuoteFees, QuoterDonePayload, QuoterDonePayloadNoEligibleServices, QuoterDonePayloadNoQuotes, QuoterDonePayloadUnsubscribed, QuoterDoneReason, QuoterEventArgs, QuoterEventHandler, QuoterInterface, QuoterProps, QuotesTuple, ServiceQuoteEventArgs, ServiceQuoteEventHandler } from "./types/quote.cjs";
8
8
  import { AbortedError, ErrorCode, ErrorReason, HttpError, InvalidParamsError, ResponseValidationError, SdkError, ServiceInitializationError, ServiceUnavailableError, TimeoutError, isAbortedError, isHttpError, isInvalidParamsError, isResponseValidationError, isSdkError, isServiceInitializationError, isServiceUnavailableError, isTimeoutError } from "./errors.cjs";
9
9
  import { CompletedTransfer, FailedTransfer, RefundedTransfer, SourceCompletedTransfer, SourcePendingTransfer, TargetPendingTransfer, Transfer, TransferBase, TransferSourceProgress, TransferStatus, TransferStepDetails, TransferTargetProgress } from "./types/transfer.cjs";
10
10
  import { SolanaSendOptions, SolanaTransactionParams } from "./utils/solana-transaction.cjs";
@@ -15,5 +15,6 @@ import { AssetBridgeMap, CreateTransferManagerOptions, ServiceStatus, ServiceSta
15
15
  import { isEnvironment, isErc20Asset, isEvmBridgeInitializer, isLombardServiceInitializer, isMarkrServiceInitializer, isNativeAsset, isServiceInitializer, isSplAsset } from "./type-guards.cjs";
16
16
  import { createTransferManager } from "./transfer-manager.cjs";
17
17
  import { caip2ToEip155ChainId, caip2ToEip155HexChainId, eip155ChainIdToCaip2, isCaip2ChainId, splitCaip2ChainId } from "./utils/caip.cjs";
18
+ import { calculatePriceImpactFromQuote } from "./utils/price-impact.cjs";
18
19
  import { parseTransfer, stringifyTransfer } from "./utils/transfer-utils.cjs";
19
- export { AVALANCHE_FUJI_CHAIN, AVALANCHE_MAINNET_CHAIN, AbortedError, ArrayElement, Asset, AssetBridgeMap, AvalancheChainIds, BITCOIN_MAINNET_CHAIN, BITCOIN_TESTNET_CHAIN, BTC_SERVICE_TYPES, BitcoinChainIds, BitcoinFeeRateTier, BitcoinFunctions, BitcoinInputUTXO, BitcoinInputUTXOWithOptionalScript, BitcoinOutputUTXO, BitcoinTx, Blockchain, BridgeableUiAsset, BtcDispatch, BtcSign, BtcSigner, BtcTransactionRequest, Caip10AccountId, Caip2ChainId, Chain, ChainAssetMap, CompletedTransfer, CreateTransferManagerOptions, DeepMutable, DestinationInfo, ERC_ZERO_ADDRESS, ETHEREUM_MAINNET_CHAIN, ETHEREUM_SEPOLIA_CHAIN, EVM_SERVICE_TYPES, Environment, Erc20Asset, ErrorCode, ErrorReason, EstimateNativeFeeOptions, EthereumChainIds, EvmChainId, EvmDispatch, EvmServiceInitializer, EvmSign, EvmSignBatch, EvmSignMessage, EvmSigner, EvmSignerWithMessage, EvmTransactionRequest, FEE_RATE_TIER_TO_BITCOIN, FailedTransfer, FeeRateTier, Fetch, GasSettings, GetBridgeableAssetsProps, GetMinimumTransferAmountProps, GetSupportedChainsResult, Hex, HttpError, InvalidParamsError, LombardServiceInitializer, MarkrServiceInitializer, Mutable, MutableGetSupportedChainsResult, NATIVE_AVAX, NATIVE_ETH, NATIVE_SOL_ADDRESS, NativeAsset, NativeFeeEstimate, Quote, QuoteFee, QuoteFeeToken, QuoteFeeType, QuoteFees, QuoterDoneReason, QuoterEventArgs, QuoterEventHandler, QuoterInterface, QuoterProps, QuotesTuple, RefundedTransfer, ResponseValidationError, SOLANA_DEVNET_CHAIN, SOLANA_MAINNET_CHAIN, SdkError, ServiceInitializationError, ServiceInitializer, ServiceQuoteEventArgs, ServiceQuoteEventHandler, ServiceStatus, ServiceStatusBaseRecord, ServiceStatusErrorRecord, ServiceStatusInitializedRecord, ServiceStatusRecord, ServiceStatusUnsupportedEnvironmentRecord, ServiceType, ServiceUnavailableError, SolanaChainIds, SolanaSendOptions, SolanaSign, SolanaSigner, SolanaTransactionParams, SourceCompletedTransfer, SourcePendingTransfer, SplAsset, TargetPendingTransfer, TimeoutError, TokenType, TrackTransferProps, Transfer, TransferAssetProps, TransferBase, TransferManager, TransferManagerStatus, TransferManagerStatusServicesRecord, TransferService, TransferSignatureReason, TransferSourceProgress, TransferStatus, TransferStepDetails, TransferTargetProgress, TransferableAsset, caip2ToEip155ChainId, caip2ToEip155HexChainId, createTransferManager, eip155ChainIdToCaip2, isAbortedError, isCaip2ChainId, isEnvironment, isErc20Asset, isEvmBridgeInitializer, isHttpError, isInvalidParamsError, isLombardServiceInitializer, isMarkrServiceInitializer, isNativeAsset, isResponseValidationError, isSdkError, isServiceInitializationError, isServiceInitializer, isServiceUnavailableError, isSplAsset, isTimeoutError, parseTransfer, splitCaip2ChainId, stringifyTransfer };
20
+ export { AVALANCHE_FUJI_CHAIN, AVALANCHE_MAINNET_CHAIN, AbortedError, ArrayElement, Asset, AssetBridgeMap, AvalancheChainIds, BITCOIN_MAINNET_CHAIN, BITCOIN_TESTNET_CHAIN, BTC_SERVICE_TYPES, BitcoinChainIds, BitcoinFeeRateTier, BitcoinFunctions, BitcoinInputUTXO, BitcoinInputUTXOWithOptionalScript, BitcoinOutputUTXO, BitcoinTx, Blockchain, BridgeableUiAsset, BtcDispatch, BtcSign, BtcSigner, BtcTransactionRequest, Caip10AccountId, Caip2ChainId, Chain, ChainAssetMap, CompletedTransfer, CreateTransferManagerOptions, DeepMutable, DestinationInfo, ERC_ZERO_ADDRESS, ETHEREUM_MAINNET_CHAIN, ETHEREUM_SEPOLIA_CHAIN, EVM_SERVICE_TYPES, Environment, Erc20Asset, ErrorCode, ErrorReason, EstimateNativeFeeOptions, EthereumChainIds, EvmChainId, EvmDispatch, EvmServiceInitializer, EvmSign, EvmSignBatch, EvmSignMessage, EvmSigner, EvmSignerWithMessage, EvmTransactionRequest, FEE_RATE_TIER_TO_BITCOIN, FailedTransfer, FeeRateTier, Fetch, GasSettings, GetBridgeableAssetsProps, GetMinimumTransferAmountProps, GetSupportedChainsResult, Hex, HttpError, InvalidParamsError, LombardServiceInitializer, MarkrServiceInitializer, Mutable, MutableGetSupportedChainsResult, NATIVE_AVAX, NATIVE_ETH, NATIVE_SOL_ADDRESS, NativeAsset, NativeFeeEstimate, Quote, QuoteFee, QuoteFeeFundingModel, QuoteFeeToken, QuoteFeeType, QuoteFees, QuoterDonePayload, QuoterDonePayloadNoEligibleServices, QuoterDonePayloadNoQuotes, QuoterDonePayloadUnsubscribed, QuoterDoneReason, QuoterEventArgs, QuoterEventHandler, QuoterInterface, QuoterProps, QuotesTuple, RefundedTransfer, ResponseValidationError, SOLANA_DEVNET_CHAIN, SOLANA_MAINNET_CHAIN, SdkError, ServiceInitializationError, ServiceInitializer, ServiceQuoteEventArgs, ServiceQuoteEventHandler, ServiceStatus, ServiceStatusBaseRecord, ServiceStatusErrorRecord, ServiceStatusInitializedRecord, ServiceStatusRecord, ServiceStatusUnsupportedEnvironmentRecord, ServiceType, ServiceUnavailableError, SolanaChainIds, SolanaSendOptions, SolanaSign, SolanaSigner, SolanaTransactionParams, SourceCompletedTransfer, SourcePendingTransfer, SplAsset, TargetPendingTransfer, TimeoutError, TokenType, TrackTransferProps, Transfer, TransferAssetProps, TransferBase, TransferManager, TransferManagerStatus, TransferManagerStatusServicesRecord, TransferService, TransferSignatureReason, TransferSourceProgress, TransferStatus, TransferStepDetails, TransferTargetProgress, TransferableAsset, caip2ToEip155ChainId, caip2ToEip155HexChainId, calculatePriceImpactFromQuote, createTransferManager, eip155ChainIdToCaip2, isAbortedError, isCaip2ChainId, isEnvironment, isErc20Asset, isEvmBridgeInitializer, isHttpError, isInvalidParamsError, isLombardServiceInitializer, isMarkrServiceInitializer, isNativeAsset, isResponseValidationError, isSdkError, isServiceInitializationError, isServiceInitializer, isServiceUnavailableError, isSplAsset, isTimeoutError, parseTransfer, splitCaip2ChainId, stringifyTransfer };
package/dist/mod.d.ts CHANGED
@@ -4,7 +4,7 @@ import { Chain } from "./types/chain.js";
4
4
  import { FeeRateTier } from "./types/fee.js";
5
5
  import { AVALANCHE_FUJI_CHAIN, AVALANCHE_MAINNET_CHAIN, AvalancheChainIds, BITCOIN_MAINNET_CHAIN, BITCOIN_TESTNET_CHAIN, BTC_SERVICE_TYPES, BitcoinChainIds, Blockchain, ERC_ZERO_ADDRESS, ETHEREUM_MAINNET_CHAIN, ETHEREUM_SEPOLIA_CHAIN, EVM_SERVICE_TYPES, Environment, EthereumChainIds, EvmChainId, FEE_RATE_TIER_TO_BITCOIN, NATIVE_AVAX, NATIVE_ETH, NATIVE_SOL_ADDRESS, SOLANA_DEVNET_CHAIN, SOLANA_MAINNET_CHAIN, ServiceType, SolanaChainIds, TokenType, TransferSignatureReason } from "./constants.js";
6
6
  import { Asset, BridgeableUiAsset, ChainAssetMap, DestinationInfo, Erc20Asset, NativeAsset, SplAsset, TransferableAsset } from "./types/asset.js";
7
- import { Quote, QuoteFee, QuoteFeeToken, QuoteFeeType, QuoteFees, QuoterDoneReason, QuoterEventArgs, QuoterEventHandler, QuoterInterface, QuoterProps, QuotesTuple, ServiceQuoteEventArgs, ServiceQuoteEventHandler } from "./types/quote.js";
7
+ import { Quote, QuoteFee, QuoteFeeFundingModel, QuoteFeeToken, QuoteFeeType, QuoteFees, QuoterDonePayload, QuoterDonePayloadNoEligibleServices, QuoterDonePayloadNoQuotes, QuoterDonePayloadUnsubscribed, QuoterDoneReason, QuoterEventArgs, QuoterEventHandler, QuoterInterface, QuoterProps, QuotesTuple, ServiceQuoteEventArgs, ServiceQuoteEventHandler } from "./types/quote.js";
8
8
  import { AbortedError, ErrorCode, ErrorReason, HttpError, InvalidParamsError, ResponseValidationError, SdkError, ServiceInitializationError, ServiceUnavailableError, TimeoutError, isAbortedError, isHttpError, isInvalidParamsError, isResponseValidationError, isSdkError, isServiceInitializationError, isServiceUnavailableError, isTimeoutError } from "./errors.js";
9
9
  import { CompletedTransfer, FailedTransfer, RefundedTransfer, SourceCompletedTransfer, SourcePendingTransfer, TargetPendingTransfer, Transfer, TransferBase, TransferSourceProgress, TransferStatus, TransferStepDetails, TransferTargetProgress } from "./types/transfer.js";
10
10
  import { SolanaSendOptions, SolanaTransactionParams } from "./utils/solana-transaction.js";
@@ -15,5 +15,6 @@ import { AssetBridgeMap, CreateTransferManagerOptions, ServiceStatus, ServiceSta
15
15
  import { isEnvironment, isErc20Asset, isEvmBridgeInitializer, isLombardServiceInitializer, isMarkrServiceInitializer, isNativeAsset, isServiceInitializer, isSplAsset } from "./type-guards.js";
16
16
  import { createTransferManager } from "./transfer-manager.js";
17
17
  import { caip2ToEip155ChainId, caip2ToEip155HexChainId, eip155ChainIdToCaip2, isCaip2ChainId, splitCaip2ChainId } from "./utils/caip.js";
18
+ import { calculatePriceImpactFromQuote } from "./utils/price-impact.js";
18
19
  import { parseTransfer, stringifyTransfer } from "./utils/transfer-utils.js";
19
- export { AVALANCHE_FUJI_CHAIN, AVALANCHE_MAINNET_CHAIN, AbortedError, ArrayElement, Asset, AssetBridgeMap, AvalancheChainIds, BITCOIN_MAINNET_CHAIN, BITCOIN_TESTNET_CHAIN, BTC_SERVICE_TYPES, BitcoinChainIds, BitcoinFeeRateTier, BitcoinFunctions, BitcoinInputUTXO, BitcoinInputUTXOWithOptionalScript, BitcoinOutputUTXO, BitcoinTx, Blockchain, BridgeableUiAsset, BtcDispatch, BtcSign, BtcSigner, BtcTransactionRequest, Caip10AccountId, Caip2ChainId, Chain, ChainAssetMap, CompletedTransfer, CreateTransferManagerOptions, DeepMutable, DestinationInfo, ERC_ZERO_ADDRESS, ETHEREUM_MAINNET_CHAIN, ETHEREUM_SEPOLIA_CHAIN, EVM_SERVICE_TYPES, Environment, Erc20Asset, ErrorCode, ErrorReason, EstimateNativeFeeOptions, EthereumChainIds, EvmChainId, EvmDispatch, EvmServiceInitializer, EvmSign, EvmSignBatch, EvmSignMessage, EvmSigner, EvmSignerWithMessage, EvmTransactionRequest, FEE_RATE_TIER_TO_BITCOIN, FailedTransfer, FeeRateTier, Fetch, GasSettings, GetBridgeableAssetsProps, GetMinimumTransferAmountProps, GetSupportedChainsResult, Hex, HttpError, InvalidParamsError, LombardServiceInitializer, MarkrServiceInitializer, Mutable, MutableGetSupportedChainsResult, NATIVE_AVAX, NATIVE_ETH, NATIVE_SOL_ADDRESS, NativeAsset, NativeFeeEstimate, Quote, QuoteFee, QuoteFeeToken, QuoteFeeType, QuoteFees, QuoterDoneReason, QuoterEventArgs, QuoterEventHandler, QuoterInterface, QuoterProps, QuotesTuple, RefundedTransfer, ResponseValidationError, SOLANA_DEVNET_CHAIN, SOLANA_MAINNET_CHAIN, SdkError, ServiceInitializationError, ServiceInitializer, ServiceQuoteEventArgs, ServiceQuoteEventHandler, ServiceStatus, ServiceStatusBaseRecord, ServiceStatusErrorRecord, ServiceStatusInitializedRecord, ServiceStatusRecord, ServiceStatusUnsupportedEnvironmentRecord, ServiceType, ServiceUnavailableError, SolanaChainIds, SolanaSendOptions, SolanaSign, SolanaSigner, SolanaTransactionParams, SourceCompletedTransfer, SourcePendingTransfer, SplAsset, TargetPendingTransfer, TimeoutError, TokenType, TrackTransferProps, Transfer, TransferAssetProps, TransferBase, TransferManager, TransferManagerStatus, TransferManagerStatusServicesRecord, TransferService, TransferSignatureReason, TransferSourceProgress, TransferStatus, TransferStepDetails, TransferTargetProgress, TransferableAsset, caip2ToEip155ChainId, caip2ToEip155HexChainId, createTransferManager, eip155ChainIdToCaip2, isAbortedError, isCaip2ChainId, isEnvironment, isErc20Asset, isEvmBridgeInitializer, isHttpError, isInvalidParamsError, isLombardServiceInitializer, isMarkrServiceInitializer, isNativeAsset, isResponseValidationError, isSdkError, isServiceInitializationError, isServiceInitializer, isServiceUnavailableError, isSplAsset, isTimeoutError, parseTransfer, splitCaip2ChainId, stringifyTransfer };
20
+ export { AVALANCHE_FUJI_CHAIN, AVALANCHE_MAINNET_CHAIN, AbortedError, ArrayElement, Asset, AssetBridgeMap, AvalancheChainIds, BITCOIN_MAINNET_CHAIN, BITCOIN_TESTNET_CHAIN, BTC_SERVICE_TYPES, BitcoinChainIds, BitcoinFeeRateTier, BitcoinFunctions, BitcoinInputUTXO, BitcoinInputUTXOWithOptionalScript, BitcoinOutputUTXO, BitcoinTx, Blockchain, BridgeableUiAsset, BtcDispatch, BtcSign, BtcSigner, BtcTransactionRequest, Caip10AccountId, Caip2ChainId, Chain, ChainAssetMap, CompletedTransfer, CreateTransferManagerOptions, DeepMutable, DestinationInfo, ERC_ZERO_ADDRESS, ETHEREUM_MAINNET_CHAIN, ETHEREUM_SEPOLIA_CHAIN, EVM_SERVICE_TYPES, Environment, Erc20Asset, ErrorCode, ErrorReason, EstimateNativeFeeOptions, EthereumChainIds, EvmChainId, EvmDispatch, EvmServiceInitializer, EvmSign, EvmSignBatch, EvmSignMessage, EvmSigner, EvmSignerWithMessage, EvmTransactionRequest, FEE_RATE_TIER_TO_BITCOIN, FailedTransfer, FeeRateTier, Fetch, GasSettings, GetBridgeableAssetsProps, GetMinimumTransferAmountProps, GetSupportedChainsResult, Hex, HttpError, InvalidParamsError, LombardServiceInitializer, MarkrServiceInitializer, Mutable, MutableGetSupportedChainsResult, NATIVE_AVAX, NATIVE_ETH, NATIVE_SOL_ADDRESS, NativeAsset, NativeFeeEstimate, Quote, QuoteFee, QuoteFeeFundingModel, QuoteFeeToken, QuoteFeeType, QuoteFees, QuoterDonePayload, QuoterDonePayloadNoEligibleServices, QuoterDonePayloadNoQuotes, QuoterDonePayloadUnsubscribed, QuoterDoneReason, QuoterEventArgs, QuoterEventHandler, QuoterInterface, QuoterProps, QuotesTuple, RefundedTransfer, ResponseValidationError, SOLANA_DEVNET_CHAIN, SOLANA_MAINNET_CHAIN, SdkError, ServiceInitializationError, ServiceInitializer, ServiceQuoteEventArgs, ServiceQuoteEventHandler, ServiceStatus, ServiceStatusBaseRecord, ServiceStatusErrorRecord, ServiceStatusInitializedRecord, ServiceStatusRecord, ServiceStatusUnsupportedEnvironmentRecord, ServiceType, ServiceUnavailableError, SolanaChainIds, SolanaSendOptions, SolanaSign, SolanaSigner, SolanaTransactionParams, SourceCompletedTransfer, SourcePendingTransfer, SplAsset, TargetPendingTransfer, TimeoutError, TokenType, TrackTransferProps, Transfer, TransferAssetProps, TransferBase, TransferManager, TransferManagerStatus, TransferManagerStatusServicesRecord, TransferService, TransferSignatureReason, TransferSourceProgress, TransferStatus, TransferStepDetails, TransferTargetProgress, TransferableAsset, caip2ToEip155ChainId, caip2ToEip155HexChainId, calculatePriceImpactFromQuote, createTransferManager, eip155ChainIdToCaip2, isAbortedError, isCaip2ChainId, isEnvironment, isErc20Asset, isEvmBridgeInitializer, isHttpError, isInvalidParamsError, isLombardServiceInitializer, isMarkrServiceInitializer, isNativeAsset, isResponseValidationError, isSdkError, isServiceInitializationError, isServiceInitializer, isServiceUnavailableError, isSplAsset, isTimeoutError, parseTransfer, splitCaip2ChainId, stringifyTransfer };
package/dist/mod.js CHANGED
@@ -1 +1 @@
1
- import{AVALANCHE_FUJI_CHAIN as e,AVALANCHE_MAINNET_CHAIN as t,AvalancheChainIds as n,BITCOIN_MAINNET_CHAIN as r,BITCOIN_TESTNET_CHAIN as i,BTC_SERVICE_TYPES as a,BitcoinChainIds as o,Blockchain as s,ERC_ZERO_ADDRESS as c,ETHEREUM_MAINNET_CHAIN as l,ETHEREUM_SEPOLIA_CHAIN as u,EVM_SERVICE_TYPES as d,Environment as f,EthereumChainIds as p,EvmChainId as m,FEE_RATE_TIER_TO_BITCOIN as h,NATIVE_AVAX as g,NATIVE_ETH as _,NATIVE_SOL_ADDRESS as v,SOLANA_DEVNET_CHAIN as y,SOLANA_MAINNET_CHAIN as b,ServiceType as x,SolanaChainIds as S,TokenType as C,TransferSignatureReason as w}from"./constants.js";import{AbortedError as T,ErrorCode as E,ErrorReason as D,HttpError as O,InvalidParamsError as k,ResponseValidationError as A,SdkError as j,ServiceInitializationError as M,ServiceUnavailableError as N,TimeoutError as P,isAbortedError as F,isHttpError as I,isInvalidParamsError as L,isResponseValidationError as R,isSdkError as z,isServiceInitializationError as B,isServiceUnavailableError as V,isTimeoutError as H}from"./errors.js";import{isEnvironment as U,isErc20Asset as W,isEvmBridgeInitializer as G,isLombardServiceInitializer as K,isMarkrServiceInitializer as q,isNativeAsset as J,isServiceInitializer as Y,isSplAsset as X}from"./type-guards.js";import{createTransferManager as Z}from"./transfer-manager.js";import{caip2ToEip155ChainId as Q,caip2ToEip155HexChainId as $,eip155ChainIdToCaip2 as ee,isCaip2ChainId as te,splitCaip2ChainId as ne}from"./utils/caip.js";import{parseTransfer as re,stringifyTransfer as ie}from"./utils/transfer-utils.js";export{e as AVALANCHE_FUJI_CHAIN,t as AVALANCHE_MAINNET_CHAIN,T as AbortedError,n as AvalancheChainIds,r as BITCOIN_MAINNET_CHAIN,i as BITCOIN_TESTNET_CHAIN,a as BTC_SERVICE_TYPES,o as BitcoinChainIds,s as Blockchain,c as ERC_ZERO_ADDRESS,l as ETHEREUM_MAINNET_CHAIN,u as ETHEREUM_SEPOLIA_CHAIN,d as EVM_SERVICE_TYPES,f as Environment,E as ErrorCode,D as ErrorReason,p as EthereumChainIds,m as EvmChainId,h as FEE_RATE_TIER_TO_BITCOIN,O as HttpError,k as InvalidParamsError,g as NATIVE_AVAX,_ as NATIVE_ETH,v as NATIVE_SOL_ADDRESS,A as ResponseValidationError,y as SOLANA_DEVNET_CHAIN,b as SOLANA_MAINNET_CHAIN,j as SdkError,M as ServiceInitializationError,x as ServiceType,N as ServiceUnavailableError,S as SolanaChainIds,P as TimeoutError,C as TokenType,w as TransferSignatureReason,Q as caip2ToEip155ChainId,$ as caip2ToEip155HexChainId,Z as createTransferManager,ee as eip155ChainIdToCaip2,F as isAbortedError,te as isCaip2ChainId,U as isEnvironment,W as isErc20Asset,G as isEvmBridgeInitializer,I as isHttpError,L as isInvalidParamsError,K as isLombardServiceInitializer,q as isMarkrServiceInitializer,J as isNativeAsset,R as isResponseValidationError,z as isSdkError,B as isServiceInitializationError,Y as isServiceInitializer,V as isServiceUnavailableError,X as isSplAsset,H as isTimeoutError,re as parseTransfer,ne as splitCaip2ChainId,ie as stringifyTransfer};
1
+ import{AVALANCHE_FUJI_CHAIN as e,AVALANCHE_MAINNET_CHAIN as t,AvalancheChainIds as n,BITCOIN_MAINNET_CHAIN as r,BITCOIN_TESTNET_CHAIN as i,BTC_SERVICE_TYPES as a,BitcoinChainIds as o,Blockchain as s,ERC_ZERO_ADDRESS as c,ETHEREUM_MAINNET_CHAIN as l,ETHEREUM_SEPOLIA_CHAIN as u,EVM_SERVICE_TYPES as d,Environment as f,EthereumChainIds as p,EvmChainId as m,FEE_RATE_TIER_TO_BITCOIN as h,NATIVE_AVAX as g,NATIVE_ETH as _,NATIVE_SOL_ADDRESS as v,SOLANA_DEVNET_CHAIN as y,SOLANA_MAINNET_CHAIN as b,ServiceType as x,SolanaChainIds as S,TokenType as C,TransferSignatureReason as w}from"./constants.js";import{AbortedError as T,ErrorCode as E,ErrorReason as D,HttpError as O,InvalidParamsError as k,ResponseValidationError as A,SdkError as j,ServiceInitializationError as M,ServiceUnavailableError as N,TimeoutError as P,isAbortedError as F,isHttpError as I,isInvalidParamsError as L,isResponseValidationError as R,isSdkError as z,isServiceInitializationError as B,isServiceUnavailableError as V,isTimeoutError as H}from"./errors.js";import{isEnvironment as U,isErc20Asset as W,isEvmBridgeInitializer as G,isLombardServiceInitializer as K,isMarkrServiceInitializer as q,isNativeAsset as J,isServiceInitializer as Y,isSplAsset as X}from"./type-guards.js";import{createTransferManager as Z}from"./transfer-manager.js";import{caip2ToEip155ChainId as Q,caip2ToEip155HexChainId as $,eip155ChainIdToCaip2 as ee,isCaip2ChainId as te,splitCaip2ChainId as ne}from"./utils/caip.js";import{calculatePriceImpactFromQuote as re}from"./utils/price-impact.js";import{parseTransfer as ie,stringifyTransfer as ae}from"./utils/transfer-utils.js";export{e as AVALANCHE_FUJI_CHAIN,t as AVALANCHE_MAINNET_CHAIN,T as AbortedError,n as AvalancheChainIds,r as BITCOIN_MAINNET_CHAIN,i as BITCOIN_TESTNET_CHAIN,a as BTC_SERVICE_TYPES,o as BitcoinChainIds,s as Blockchain,c as ERC_ZERO_ADDRESS,l as ETHEREUM_MAINNET_CHAIN,u as ETHEREUM_SEPOLIA_CHAIN,d as EVM_SERVICE_TYPES,f as Environment,E as ErrorCode,D as ErrorReason,p as EthereumChainIds,m as EvmChainId,h as FEE_RATE_TIER_TO_BITCOIN,O as HttpError,k as InvalidParamsError,g as NATIVE_AVAX,_ as NATIVE_ETH,v as NATIVE_SOL_ADDRESS,A as ResponseValidationError,y as SOLANA_DEVNET_CHAIN,b as SOLANA_MAINNET_CHAIN,j as SdkError,M as ServiceInitializationError,x as ServiceType,N as ServiceUnavailableError,S as SolanaChainIds,P as TimeoutError,C as TokenType,w as TransferSignatureReason,Q as caip2ToEip155ChainId,$ as caip2ToEip155HexChainId,re as calculatePriceImpactFromQuote,Z as createTransferManager,ee as eip155ChainIdToCaip2,F as isAbortedError,te as isCaip2ChainId,U as isEnvironment,W as isErc20Asset,G as isEvmBridgeInitializer,I as isHttpError,L as isInvalidParamsError,K as isLombardServiceInitializer,q as isMarkrServiceInitializer,J as isNativeAsset,R as isResponseValidationError,z as isSdkError,B as isServiceInitializationError,Y as isServiceInitializer,V as isServiceUnavailableError,X as isSplAsset,H as isTimeoutError,ie as parseTransfer,ne as splitCaip2ChainId,ae as stringifyTransfer};
@@ -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??r.QUOTER_DEFAULT_PRUNE_INTERVAL_MS,this.transferServices=t,this.refreshBufferSeconds=n.refreshBufferSeconds??r.QUOTER_DEFAULT_REFRESH_BUFFER_SECONDS}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;this.done=!0,this.stop();let t=[...this.subscribers];this.subscribers.clear();for(let n of t)n(`done`,{reason:e})}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;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??r.QUOTER_DEFAULT_PRUNE_INTERVAL_MS,this.transferServices=t,this.refreshBufferSeconds=n.refreshBufferSeconds??r.QUOTER_DEFAULT_REFRESH_BUFFER_SECONDS}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;
2
2
  //# sourceMappingURL=quoter.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"quoter.cjs","names":["QUOTER_DEFAULT_PRUNE_INTERVAL_MS","QUOTER_DEFAULT_REFRESH_BUFFER_SECONDS","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 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 { 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 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', { reason });\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":"+MA0EA,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,iBAAmBA,EAAAA,iCAClD,KAAK,iBAAmB,EACxB,KAAK,qBAAuB,EAAQ,sBAAwBC,EAAAA,sCAM9D,WAAgC,CAC9B,IAAM,EAAM,KAAK,OAAO,CAElB,EAASC,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,GAMnB,OAJK,KAAK,SACR,KAAK,OAAO,KAGD,CACP,MAM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,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":["QUOTER_DEFAULT_PRUNE_INTERVAL_MS","QUOTER_DEFAULT_REFRESH_BUFFER_SECONDS","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.safeParse(value);\n\n return result.success;\n}\n"],"mappings":"+MA2EA,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,CA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,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_DEFAULT_PRUNE_INTERVAL_MS as s,QUOTER_DEFAULT_REFRESH_BUFFER_SECONDS as c,QUOTER_EMPTY_RETRY_BASE_DELAY_MS as l,QUOTER_EMPTY_RETRY_MAX_DELAY_MS as u}from"./constants.js";import{z as d}from"zod";import{isAddressEqual as f}from"viem";var p=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??s,this.transferServices=t,this.refreshBufferSeconds=n.refreshBufferSeconds??c}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,h(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(g(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(u,l*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;this.done=!0,this.stop();let t=[...this.subscribers];this.subscribers.clear();for(let n of t)n(`done`,{reason:e})}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 m=d.object({id:d.string(),expiresAt:d.number().int().nonnegative(),amountOut:d.bigint().nonnegative(),amountIn:d.bigint().nonnegative(),serviceType:d.string()});function h(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?f(r.address,a.address):r.type===e.SPL&&a.type===e.SPL?r.address===a.address:!1:!1:!1}function g(e){return m.safeParse(e).success}export{p 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_DEFAULT_PRUNE_INTERVAL_MS as s,QUOTER_DEFAULT_REFRESH_BUFFER_SECONDS as c,QUOTER_EMPTY_RETRY_BASE_DELAY_MS as l,QUOTER_EMPTY_RETRY_MAX_DELAY_MS as u}from"./constants.js";import{z as d}from"zod";import{isAddressEqual as f}from"viem";var p=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??s,this.transferServices=t,this.refreshBufferSeconds=n.refreshBufferSeconds??c}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,h(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(g(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(u,l*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 m=d.object({id:d.string(),expiresAt:d.number().int().nonnegative(),amountOut:d.bigint().nonnegative(),amountIn:d.bigint().nonnegative(),serviceType:d.string()});function h(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?f(r.address,a.address):r.type===e.SPL&&a.type===e.SPL?r.address===a.address:!1:!1:!1}function g(e){return m.safeParse(e).success}export{p 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 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 { 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 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', { reason });\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":"4dA0EA,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,iBAAmB,EAClD,KAAK,iBAAmB,EACxB,KAAK,qBAAuB,EAAQ,sBAAwB,EAM9D,WAAgC,CAC9B,IAAM,EAAM,KAAK,OAAO,CAElB,EAAS,EADA,EAAmB,CAAE,OAAQ,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ACF,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 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":"4dA2EA,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,iBAAmB,EAClD,KAAK,iB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C,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(`../../../constants.cjs`),t=require(`../../../errors.cjs`),n=require(`../_utils/validations.cjs`),r=require(`./analyze-support.cjs`),i=require(`../_utils/fee.cjs`),a=require(`./get-minimum-transfer-amount.cjs`),o=require(`../../../utils/quote-fees.cjs`);let s=require(`viem`);function c({config:c,feeEstimationMultiplier:l,serviceAssets:u}){let d=r.analyzeSupportFactory({serviceAssets:u});return({amount:r,fromAddress:u,slippageBps:f,sourceAsset:p,sourceChain:m,targetAsset:h,targetChain:g,toAddress:_},v)=>{let y=new AbortController,b=()=>{y.abort()},x=d({sourceAsset:p,sourceChainId:m.chainId,targetAsset:h,targetChainId:g.chainId});if(!((0,s.isAddress)(u)&&(0,s.isAddress)(_))||!x)return y.signal.aborted||(v(`error`,new t.InvalidParamsError(t.ErrorReason.INVALID_PARAMS,`Invalid addresses, assets, or chains provided.`)),v(`done`)),{cancel:b};try{n.validateTransferAddressesOrThrow(u,_),n.validateTransferAddressesNotBlockedOrThrow([u,_],c.addressBlocklist)}catch{return y.signal.aborted||(v(`error`,new t.InvalidParamsError(t.ErrorReason.INVALID_PARAMS,`Transfer addresses are not equal or blocked.`)),v(`done`)),{cancel:b}}return(async()=>{let n=await a.calculateMinimumTransferAmount({sourceAsset:p,sourceChainId:m.chainId,targetAsset:h,targetChainId:g.chainId},c)*l;if(r<n)y.signal.aborted||(v(`error`,new t.InvalidParamsError(t.ErrorReason.INVALID_PARAMS,`Transfer amount is below the minimum transfer amount of ${n.toString()}`)),v(`done`));else{let[t]=i.getFee({amountIn:r,evmConfig:c,sourceAsset:p,sourceChainId:m.chainId,targetChainId:g.chainId}),n={aggregator:{name:`Avalanche Bridge™`,id:`avalanche-bridge-evm`},amountIn:r,amountOut:r-t,assetIn:p,assetOut:h,expiresAt:Math.floor(Date.now()/1e3)+60,fees:[{type:`bridge`,name:`Bridge Fee`,amount:t,chainId:m.chainId,token:o.assetToQuoteFeeToken(p)}],fromAddress:u,id:crypto.randomUUID(),partnerFeeBps:null,serviceType:e.ServiceType.AVALANCHE_EVM,slippageBps:f??0,sourceChain:m,targetChain:g,toAddress:_};y.signal.aborted||(v(`quote`,n),v(`done`))}})().catch(e=>{y.signal.aborted||(v(`error`,new t.SdkError(t.ErrorReason.UNKNOWN,t.ErrorCode.UNKNOWN,{cause:e,details:`Failed to stream quotes`})),v(`done`))}),{cancel:b}}}exports.streamQuotesFactory=c;
1
+ require(`../../../_virtual/_rolldown/runtime.cjs`);const e=require(`../../../constants.cjs`),t=require(`../../../errors.cjs`),n=require(`../_utils/validations.cjs`),r=require(`./analyze-support.cjs`),i=require(`../_utils/fee.cjs`),a=require(`./get-minimum-transfer-amount.cjs`),o=require(`../../../utils/quote-fees.cjs`);let s=require(`viem`);function c({config:c,feeEstimationMultiplier:l,serviceAssets:u}){let d=r.analyzeSupportFactory({serviceAssets:u});return({amount:r,fromAddress:u,slippageBps:f,sourceAsset:p,sourceChain:m,targetAsset:h,targetChain:g,toAddress:_},v)=>{let y=new AbortController,b=()=>{y.abort()},x=d({sourceAsset:p,sourceChainId:m.chainId,targetAsset:h,targetChainId:g.chainId});if(!((0,s.isAddress)(u)&&(0,s.isAddress)(_))||!x)return y.signal.aborted||(v(`error`,new t.InvalidParamsError(t.ErrorReason.INVALID_PARAMS,`Invalid addresses, assets, or chains provided.`)),v(`done`)),{cancel:b};try{n.validateTransferAddressesOrThrow(u,_),n.validateTransferAddressesNotBlockedOrThrow([u,_],c.addressBlocklist)}catch{return y.signal.aborted||(v(`error`,new t.InvalidParamsError(t.ErrorReason.INVALID_PARAMS,`Transfer addresses are not equal or blocked.`)),v(`done`)),{cancel:b}}return(async()=>{let n=await a.calculateMinimumTransferAmount({sourceAsset:p,sourceChainId:m.chainId,targetAsset:h,targetChainId:g.chainId},c)*l;if(r<n)y.signal.aborted||(v(`error`,new t.InvalidParamsError(t.ErrorReason.INVALID_PARAMS,`Transfer amount is below the minimum transfer amount of ${n.toString()}`)),v(`done`));else{let[t]=i.getFee({amountIn:r,evmConfig:c,sourceAsset:p,sourceChainId:m.chainId,targetChainId:g.chainId}),n={aggregator:{name:`Avalanche Bridge™`,id:`avalanche-bridge-evm`},amountIn:r,amountOut:r-t,assetIn:p,assetOut:h,expiresAt:Math.floor(Date.now()/1e3)+60,fees:[{type:`bridge`,fundingModel:`included`,name:`Bridge Fee`,amount:t,chainId:m.chainId,token:o.assetToQuoteFeeToken(p)}],fromAddress:u,id:crypto.randomUUID(),partnerFeeBps:null,serviceType:e.ServiceType.AVALANCHE_EVM,slippageBps:f??0,sourceChain:m,targetChain:g,toAddress:_};y.signal.aborted||(v(`quote`,n),v(`done`))}})().catch(e=>{y.signal.aborted||(v(`error`,new t.SdkError(t.ErrorReason.UNKNOWN,t.ErrorCode.UNKNOWN,{cause:e,details:`Failed to stream quotes`})),v(`done`))}),{cancel:b}}}exports.streamQuotesFactory=c;
2
2
  //# sourceMappingURL=stream-quotes.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"stream-quotes.cjs","names":["analyzeSupportFactory","InvalidParamsError","ErrorReason","calculateMinimumTransferAmount","getFee","assetToQuoteFeeToken","ServiceType","SdkError","ErrorCode"],"sources":["../../../../src/transfer-service/avalanche-evm/_handlers/stream-quotes.ts"],"sourcesContent":["import { ServiceType } from '../../../constants';\nimport { ErrorCode, ErrorReason, InvalidParamsError, SdkError } from '../../../errors';\nimport type { ChainAssetMap } from '../../../types/asset';\nimport type { Quote } from '../../../types/quote';\nimport type { TransferService } from '../../../types/service';\nimport type { Mutable } from '../../../types/utility-types';\nimport { isAddress } from 'viem';\nimport { validateTransferAddressesNotBlockedOrThrow, validateTransferAddressesOrThrow } from '../_utils/validations';\nimport type { EvmConfig } from '../_types';\nimport { analyzeSupportFactory } from './analyze-support';\nimport { calculateMinimumTransferAmount } from './get-minimum-transfer-amount';\nimport { getFee } from '../_utils/fee';\nimport { assetToQuoteFeeToken } from '../../../utils/quote-fees';\n\nexport interface StreamQuotesFactoryConfig {\n config: EvmConfig;\n feeEstimationMultiplier: bigint;\n serviceAssets: ChainAssetMap;\n}\n\nexport function streamQuotesFactory({\n config,\n feeEstimationMultiplier,\n serviceAssets,\n}: StreamQuotesFactoryConfig): TransferService['streamQuotes'] {\n const analyzeSupport = analyzeSupportFactory({ serviceAssets });\n\n return (\n { amount, fromAddress, slippageBps, sourceAsset, sourceChain, targetAsset, targetChain, toAddress },\n handler,\n ) => {\n const ac = new AbortController();\n const cancel = () => {\n ac.abort();\n };\n\n const hasValidAssetsAndChains = analyzeSupport({\n sourceAsset,\n sourceChainId: sourceChain.chainId,\n targetAsset,\n targetChainId: targetChain.chainId,\n });\n\n // Validate to/from addresses are EVM.\n // Verify assets and chains are supported.\n if (!(isAddress(fromAddress) && isAddress(toAddress)) || !hasValidAssetsAndChains) {\n if (!ac.signal.aborted) {\n handler(\n 'error',\n new InvalidParamsError(ErrorReason.INVALID_PARAMS, 'Invalid addresses, assets, or chains provided.'),\n );\n handler('done');\n }\n\n return { cancel };\n }\n\n // Validate addresses match and are not block listed.\n try {\n validateTransferAddressesOrThrow(fromAddress, toAddress);\n validateTransferAddressesNotBlockedOrThrow([fromAddress, toAddress], config.addressBlocklist);\n } catch {\n if (!ac.signal.aborted) {\n handler(\n 'error',\n new InvalidParamsError(ErrorReason.INVALID_PARAMS, 'Transfer addresses are not equal or blocked.'),\n );\n handler('done');\n }\n\n return { cancel };\n }\n\n (async () => {\n // Validate minimum transfer amount\n const minimumAmount = await calculateMinimumTransferAmount(\n {\n sourceAsset,\n sourceChainId: sourceChain.chainId,\n targetAsset,\n targetChainId: targetChain.chainId,\n },\n config,\n );\n\n const minimumTransferAmount = minimumAmount * feeEstimationMultiplier;\n\n if (amount < minimumTransferAmount) {\n if (!ac.signal.aborted) {\n handler(\n 'error',\n new InvalidParamsError(\n ErrorReason.INVALID_PARAMS,\n `Transfer amount is below the minimum transfer amount of ${minimumTransferAmount.toString()}`,\n ),\n );\n handler('done');\n }\n } else {\n const [fee] = getFee({\n amountIn: amount,\n evmConfig: config,\n sourceAsset,\n sourceChainId: sourceChain.chainId,\n targetChainId: targetChain.chainId,\n });\n\n // Create a quote.\n const quote: Mutable<Quote> = {\n aggregator: {\n name: 'Avalanche Bridge™',\n id: 'avalanche-bridge-evm',\n },\n amountIn: amount,\n amountOut: amount - fee,\n assetIn: sourceAsset,\n assetOut: targetAsset,\n // Set the expiration further out since the amountOut isn't expected to change\n // since the Warden config data isn't refreshed. The only expected change would be estimated gas.\n expiresAt: Math.floor(Date.now() / 1_000) + 60,\n fees: [\n {\n type: 'bridge',\n name: 'Bridge Fee',\n amount: fee,\n chainId: sourceChain.chainId,\n token: assetToQuoteFeeToken(sourceAsset),\n },\n ],\n fromAddress,\n id: crypto.randomUUID(),\n partnerFeeBps: null,\n serviceType: ServiceType.AVALANCHE_EVM,\n slippageBps: slippageBps ?? 0,\n sourceChain,\n targetChain,\n toAddress,\n };\n\n if (!ac.signal.aborted) {\n handler('quote', quote);\n handler('done');\n }\n }\n })().catch((error) => {\n if (!ac.signal.aborted) {\n handler(\n 'error',\n new SdkError(ErrorReason.UNKNOWN, ErrorCode.UNKNOWN, { cause: error, details: 'Failed to stream quotes' }),\n );\n handler('done');\n }\n });\n\n return {\n cancel,\n };\n };\n}\n"],"mappings":"uVAoBA,SAAgB,EAAoB,CAClC,SACA,0BACA,iBAC6D,CAC7D,IAAM,EAAiBA,EAAAA,sBAAsB,CAAE,gBAAe,CAAC,CAE/D,OACE,CAAE,SAAQ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KAAM,aACN,OAAQ,EACR,QAAS,EAAY,QACrB,MAAOC,EAAAA,qBAAqB,EAAY,CACzC,CACF,CACD,cACA,GAAI,OAAO,YAAY,CACvB,cAAe,KACf,YAAaC,EAAAA,YAAY,cACzB,YAAa,GAAe,EAC5B,cACA,cACA,YACD,CAEI,EAAG,OAAO,UACb,EAAQ,QAAS,EAAM,CACvB,EAAQ,OAAO,MAGjB,CAAC,MAAO,GAAU,CACf,EAAG,OAAO,UACb,EACE,QACA,IAAIC,EAAAA,SAASL,EAAAA,YAAY,QAASM,EAAAA,UAAU,QAAS,CAAE,MAAO,EAAO,QAAS,0BAA2B,CAAC,CAC3G,CACD,EAAQ,OAAO,GAEjB,CAEK,CACL,SACD"}
1
+ {"version":3,"file":"stream-quotes.cjs","names":["analyzeSupportFactory","InvalidParamsError","ErrorReason","calculateMinimumTransferAmount","getFee","assetToQuoteFeeToken","ServiceType","SdkError","ErrorCode"],"sources":["../../../../src/transfer-service/avalanche-evm/_handlers/stream-quotes.ts"],"sourcesContent":["import { ServiceType } from '../../../constants';\nimport { ErrorCode, ErrorReason, InvalidParamsError, SdkError } from '../../../errors';\nimport type { ChainAssetMap } from '../../../types/asset';\nimport type { Quote } from '../../../types/quote';\nimport type { TransferService } from '../../../types/service';\nimport type { Mutable } from '../../../types/utility-types';\nimport { isAddress } from 'viem';\nimport { validateTransferAddressesNotBlockedOrThrow, validateTransferAddressesOrThrow } from '../_utils/validations';\nimport type { EvmConfig } from '../_types';\nimport { analyzeSupportFactory } from './analyze-support';\nimport { calculateMinimumTransferAmount } from './get-minimum-transfer-amount';\nimport { getFee } from '../_utils/fee';\nimport { assetToQuoteFeeToken } from '../../../utils/quote-fees';\n\nexport interface StreamQuotesFactoryConfig {\n config: EvmConfig;\n feeEstimationMultiplier: bigint;\n serviceAssets: ChainAssetMap;\n}\n\nexport function streamQuotesFactory({\n config,\n feeEstimationMultiplier,\n serviceAssets,\n}: StreamQuotesFactoryConfig): TransferService['streamQuotes'] {\n const analyzeSupport = analyzeSupportFactory({ serviceAssets });\n\n return (\n { amount, fromAddress, slippageBps, sourceAsset, sourceChain, targetAsset, targetChain, toAddress },\n handler,\n ) => {\n const ac = new AbortController();\n const cancel = () => {\n ac.abort();\n };\n\n const hasValidAssetsAndChains = analyzeSupport({\n sourceAsset,\n sourceChainId: sourceChain.chainId,\n targetAsset,\n targetChainId: targetChain.chainId,\n });\n\n // Validate to/from addresses are EVM.\n // Verify assets and chains are supported.\n if (!(isAddress(fromAddress) && isAddress(toAddress)) || !hasValidAssetsAndChains) {\n if (!ac.signal.aborted) {\n handler(\n 'error',\n new InvalidParamsError(ErrorReason.INVALID_PARAMS, 'Invalid addresses, assets, or chains provided.'),\n );\n handler('done');\n }\n\n return { cancel };\n }\n\n // Validate addresses match and are not block listed.\n try {\n validateTransferAddressesOrThrow(fromAddress, toAddress);\n validateTransferAddressesNotBlockedOrThrow([fromAddress, toAddress], config.addressBlocklist);\n } catch {\n if (!ac.signal.aborted) {\n handler(\n 'error',\n new InvalidParamsError(ErrorReason.INVALID_PARAMS, 'Transfer addresses are not equal or blocked.'),\n );\n handler('done');\n }\n\n return { cancel };\n }\n\n (async () => {\n // Validate minimum transfer amount\n const minimumAmount = await calculateMinimumTransferAmount(\n {\n sourceAsset,\n sourceChainId: sourceChain.chainId,\n targetAsset,\n targetChainId: targetChain.chainId,\n },\n config,\n );\n\n const minimumTransferAmount = minimumAmount * feeEstimationMultiplier;\n\n if (amount < minimumTransferAmount) {\n if (!ac.signal.aborted) {\n handler(\n 'error',\n new InvalidParamsError(\n ErrorReason.INVALID_PARAMS,\n `Transfer amount is below the minimum transfer amount of ${minimumTransferAmount.toString()}`,\n ),\n );\n handler('done');\n }\n } else {\n const [fee] = getFee({\n amountIn: amount,\n evmConfig: config,\n sourceAsset,\n sourceChainId: sourceChain.chainId,\n targetChainId: targetChain.chainId,\n });\n\n // Create a quote.\n const quote: Mutable<Quote> = {\n aggregator: {\n name: 'Avalanche Bridge™',\n id: 'avalanche-bridge-evm',\n },\n amountIn: amount,\n amountOut: amount - fee,\n assetIn: sourceAsset,\n assetOut: targetAsset,\n // Set the expiration further out since the amountOut isn't expected to change\n // since the Warden config data isn't refreshed. The only expected change would be estimated gas.\n expiresAt: Math.floor(Date.now() / 1_000) + 60,\n fees: [\n {\n type: 'bridge',\n fundingModel: 'included',\n name: 'Bridge Fee',\n amount: fee,\n chainId: sourceChain.chainId,\n token: assetToQuoteFeeToken(sourceAsset),\n },\n ],\n fromAddress,\n id: crypto.randomUUID(),\n partnerFeeBps: null,\n serviceType: ServiceType.AVALANCHE_EVM,\n slippageBps: slippageBps ?? 0,\n sourceChain,\n targetChain,\n toAddress,\n };\n\n if (!ac.signal.aborted) {\n handler('quote', quote);\n handler('done');\n }\n }\n })().catch((error) => {\n if (!ac.signal.aborted) {\n handler(\n 'error',\n new SdkError(ErrorReason.UNKNOWN, ErrorCode.UNKNOWN, { cause: error, details: 'Failed to stream quotes' }),\n );\n handler('done');\n }\n });\n\n return {\n cancel,\n };\n };\n}\n"],"mappings":"uVAoBA,SAAgB,EAAoB,CAClC,SACA,0BACA,iBAC6D,CAC7D,IAAM,EAAiBA,EAAAA,sBAAsB,CAAE,gBAAe,CAAC,CAE/D,OACE,CAAE,SAAQ,cAAa,cAAa,cAAa,cAAa,cAAa,cAAa,aACxF,IACG,CACH,IAAM,EAAK,IAAI,gBACT,MAAe,CACnB,EAAG,OAAO,EAGN,EAA0B,EAAe,CAC7C,cACA,cAAe,EAAY,QAC3B,cACA,cAAe,EAAY,QAC5B,CAAC,CAIF,GAAI,GAAA,EAAA,EAAA,WAAY,EAAY,GAAA,EAAA,EAAA,WAAc,EAAU,GAAK,CAAC,EASxD,OARK,EAAG,OAAO,UACb,EACE,QACA,IAAIC,EAAAA,mBAAmBC,EAAAA,YAAY,eAAgB,iDAAiD,CACrG,CACD,EAAQ,OAAO,EAGV,CAAE,SAAQ,CAInB,GAAI,CACF,EAAA,iCAAiC,EAAa,EAAU,CACxD,EAAA,2CA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"}
@@ -1,2 +1,2 @@
1
- import{ServiceType as e}from"../../../constants.js";import{ErrorCode as t,ErrorReason as n,InvalidParamsError as r,SdkError as i}from"../../../errors.js";import{validateTransferAddressesNotBlockedOrThrow as a,validateTransferAddressesOrThrow as o}from"../_utils/validations.js";import{analyzeSupportFactory as s}from"./analyze-support.js";import{getFee as c}from"../_utils/fee.js";import{calculateMinimumTransferAmount as l}from"./get-minimum-transfer-amount.js";import{assetToQuoteFeeToken as u}from"../../../utils/quote-fees.js";import{isAddress as d}from"viem";function f({config:f,feeEstimationMultiplier:p,serviceAssets:m}){let h=s({serviceAssets:m});return({amount:s,fromAddress:m,slippageBps:g,sourceAsset:_,sourceChain:v,targetAsset:y,targetChain:b,toAddress:x},S)=>{let C=new AbortController,w=()=>{C.abort()},T=h({sourceAsset:_,sourceChainId:v.chainId,targetAsset:y,targetChainId:b.chainId});if(!(d(m)&&d(x))||!T)return C.signal.aborted||(S(`error`,new r(n.INVALID_PARAMS,`Invalid addresses, assets, or chains provided.`)),S(`done`)),{cancel:w};try{o(m,x),a([m,x],f.addressBlocklist)}catch{return C.signal.aborted||(S(`error`,new r(n.INVALID_PARAMS,`Transfer addresses are not equal or blocked.`)),S(`done`)),{cancel:w}}return(async()=>{let t=await l({sourceAsset:_,sourceChainId:v.chainId,targetAsset:y,targetChainId:b.chainId},f)*p;if(s<t)C.signal.aborted||(S(`error`,new r(n.INVALID_PARAMS,`Transfer amount is below the minimum transfer amount of ${t.toString()}`)),S(`done`));else{let[t]=c({amountIn:s,evmConfig:f,sourceAsset:_,sourceChainId:v.chainId,targetChainId:b.chainId}),n={aggregator:{name:`Avalanche Bridge™`,id:`avalanche-bridge-evm`},amountIn:s,amountOut:s-t,assetIn:_,assetOut:y,expiresAt:Math.floor(Date.now()/1e3)+60,fees:[{type:`bridge`,name:`Bridge Fee`,amount:t,chainId:v.chainId,token:u(_)}],fromAddress:m,id:crypto.randomUUID(),partnerFeeBps:null,serviceType:e.AVALANCHE_EVM,slippageBps:g??0,sourceChain:v,targetChain:b,toAddress:x};C.signal.aborted||(S(`quote`,n),S(`done`))}})().catch(e=>{C.signal.aborted||(S(`error`,new i(n.UNKNOWN,t.UNKNOWN,{cause:e,details:`Failed to stream quotes`})),S(`done`))}),{cancel:w}}}export{f as streamQuotesFactory};
1
+ import{ServiceType as e}from"../../../constants.js";import{ErrorCode as t,ErrorReason as n,InvalidParamsError as r,SdkError as i}from"../../../errors.js";import{validateTransferAddressesNotBlockedOrThrow as a,validateTransferAddressesOrThrow as o}from"../_utils/validations.js";import{analyzeSupportFactory as s}from"./analyze-support.js";import{getFee as c}from"../_utils/fee.js";import{calculateMinimumTransferAmount as l}from"./get-minimum-transfer-amount.js";import{assetToQuoteFeeToken as u}from"../../../utils/quote-fees.js";import{isAddress as d}from"viem";function f({config:f,feeEstimationMultiplier:p,serviceAssets:m}){let h=s({serviceAssets:m});return({amount:s,fromAddress:m,slippageBps:g,sourceAsset:_,sourceChain:v,targetAsset:y,targetChain:b,toAddress:x},S)=>{let C=new AbortController,w=()=>{C.abort()},T=h({sourceAsset:_,sourceChainId:v.chainId,targetAsset:y,targetChainId:b.chainId});if(!(d(m)&&d(x))||!T)return C.signal.aborted||(S(`error`,new r(n.INVALID_PARAMS,`Invalid addresses, assets, or chains provided.`)),S(`done`)),{cancel:w};try{o(m,x),a([m,x],f.addressBlocklist)}catch{return C.signal.aborted||(S(`error`,new r(n.INVALID_PARAMS,`Transfer addresses are not equal or blocked.`)),S(`done`)),{cancel:w}}return(async()=>{let t=await l({sourceAsset:_,sourceChainId:v.chainId,targetAsset:y,targetChainId:b.chainId},f)*p;if(s<t)C.signal.aborted||(S(`error`,new r(n.INVALID_PARAMS,`Transfer amount is below the minimum transfer amount of ${t.toString()}`)),S(`done`));else{let[t]=c({amountIn:s,evmConfig:f,sourceAsset:_,sourceChainId:v.chainId,targetChainId:b.chainId}),n={aggregator:{name:`Avalanche Bridge™`,id:`avalanche-bridge-evm`},amountIn:s,amountOut:s-t,assetIn:_,assetOut:y,expiresAt:Math.floor(Date.now()/1e3)+60,fees:[{type:`bridge`,fundingModel:`included`,name:`Bridge Fee`,amount:t,chainId:v.chainId,token:u(_)}],fromAddress:m,id:crypto.randomUUID(),partnerFeeBps:null,serviceType:e.AVALANCHE_EVM,slippageBps:g??0,sourceChain:v,targetChain:b,toAddress:x};C.signal.aborted||(S(`quote`,n),S(`done`))}})().catch(e=>{C.signal.aborted||(S(`error`,new i(n.UNKNOWN,t.UNKNOWN,{cause:e,details:`Failed to stream quotes`})),S(`done`))}),{cancel:w}}}export{f as streamQuotesFactory};
2
2
  //# sourceMappingURL=stream-quotes.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"stream-quotes.js","names":[],"sources":["../../../../src/transfer-service/avalanche-evm/_handlers/stream-quotes.ts"],"sourcesContent":["import { ServiceType } from '../../../constants';\nimport { ErrorCode, ErrorReason, InvalidParamsError, SdkError } from '../../../errors';\nimport type { ChainAssetMap } from '../../../types/asset';\nimport type { Quote } from '../../../types/quote';\nimport type { TransferService } from '../../../types/service';\nimport type { Mutable } from '../../../types/utility-types';\nimport { isAddress } from 'viem';\nimport { validateTransferAddressesNotBlockedOrThrow, validateTransferAddressesOrThrow } from '../_utils/validations';\nimport type { EvmConfig } from '../_types';\nimport { analyzeSupportFactory } from './analyze-support';\nimport { calculateMinimumTransferAmount } from './get-minimum-transfer-amount';\nimport { getFee } from '../_utils/fee';\nimport { assetToQuoteFeeToken } from '../../../utils/quote-fees';\n\nexport interface StreamQuotesFactoryConfig {\n config: EvmConfig;\n feeEstimationMultiplier: bigint;\n serviceAssets: ChainAssetMap;\n}\n\nexport function streamQuotesFactory({\n config,\n feeEstimationMultiplier,\n serviceAssets,\n}: StreamQuotesFactoryConfig): TransferService['streamQuotes'] {\n const analyzeSupport = analyzeSupportFactory({ serviceAssets });\n\n return (\n { amount, fromAddress, slippageBps, sourceAsset, sourceChain, targetAsset, targetChain, toAddress },\n handler,\n ) => {\n const ac = new AbortController();\n const cancel = () => {\n ac.abort();\n };\n\n const hasValidAssetsAndChains = analyzeSupport({\n sourceAsset,\n sourceChainId: sourceChain.chainId,\n targetAsset,\n targetChainId: targetChain.chainId,\n });\n\n // Validate to/from addresses are EVM.\n // Verify assets and chains are supported.\n if (!(isAddress(fromAddress) && isAddress(toAddress)) || !hasValidAssetsAndChains) {\n if (!ac.signal.aborted) {\n handler(\n 'error',\n new InvalidParamsError(ErrorReason.INVALID_PARAMS, 'Invalid addresses, assets, or chains provided.'),\n );\n handler('done');\n }\n\n return { cancel };\n }\n\n // Validate addresses match and are not block listed.\n try {\n validateTransferAddressesOrThrow(fromAddress, toAddress);\n validateTransferAddressesNotBlockedOrThrow([fromAddress, toAddress], config.addressBlocklist);\n } catch {\n if (!ac.signal.aborted) {\n handler(\n 'error',\n new InvalidParamsError(ErrorReason.INVALID_PARAMS, 'Transfer addresses are not equal or blocked.'),\n );\n handler('done');\n }\n\n return { cancel };\n }\n\n (async () => {\n // Validate minimum transfer amount\n const minimumAmount = await calculateMinimumTransferAmount(\n {\n sourceAsset,\n sourceChainId: sourceChain.chainId,\n targetAsset,\n targetChainId: targetChain.chainId,\n },\n config,\n );\n\n const minimumTransferAmount = minimumAmount * feeEstimationMultiplier;\n\n if (amount < minimumTransferAmount) {\n if (!ac.signal.aborted) {\n handler(\n 'error',\n new InvalidParamsError(\n ErrorReason.INVALID_PARAMS,\n `Transfer amount is below the minimum transfer amount of ${minimumTransferAmount.toString()}`,\n ),\n );\n handler('done');\n }\n } else {\n const [fee] = getFee({\n amountIn: amount,\n evmConfig: config,\n sourceAsset,\n sourceChainId: sourceChain.chainId,\n targetChainId: targetChain.chainId,\n });\n\n // Create a quote.\n const quote: Mutable<Quote> = {\n aggregator: {\n name: 'Avalanche Bridge™',\n id: 'avalanche-bridge-evm',\n },\n amountIn: amount,\n amountOut: amount - fee,\n assetIn: sourceAsset,\n assetOut: targetAsset,\n // Set the expiration further out since the amountOut isn't expected to change\n // since the Warden config data isn't refreshed. The only expected change would be estimated gas.\n expiresAt: Math.floor(Date.now() / 1_000) + 60,\n fees: [\n {\n type: 'bridge',\n name: 'Bridge Fee',\n amount: fee,\n chainId: sourceChain.chainId,\n token: assetToQuoteFeeToken(sourceAsset),\n },\n ],\n fromAddress,\n id: crypto.randomUUID(),\n partnerFeeBps: null,\n serviceType: ServiceType.AVALANCHE_EVM,\n slippageBps: slippageBps ?? 0,\n sourceChain,\n targetChain,\n toAddress,\n };\n\n if (!ac.signal.aborted) {\n handler('quote', quote);\n handler('done');\n }\n }\n })().catch((error) => {\n if (!ac.signal.aborted) {\n handler(\n 'error',\n new SdkError(ErrorReason.UNKNOWN, ErrorCode.UNKNOWN, { cause: error, details: 'Failed to stream quotes' }),\n );\n handler('done');\n }\n });\n\n return {\n cancel,\n };\n };\n}\n"],"mappings":"ojBAoBA,SAAgB,EAAoB,CAClC,SACA,0BACA,iBAC6D,CAC7D,IAAM,EAAiB,EAAsB,CAAE,gBAAe,CAAC,CAE/D,OACE,CAAE,SAAQ,cAAa,cAAa,cAAa,cAAa,cAAa,cAAa,aACxF,IACG,CACH,IAAM,EAAK,IAAI,gBACT,MAAe,CACnB,EAAG,OAAO,EAGN,EAA0B,EAAe,CAC7C,cACA,cAAe,EAAY,QAC3B,cACA,cAAe,EAAY,QAC5B,CAAC,CAIF,GAAI,EAAE,EAAU,EAAY,EAAI,EAAU,EAAU,GAAK,CAAC,EASxD,OARK,EAAG,OAAO,UACb,EACE,QACA,IAAI,EAAmB,EAAY,eAAgB,iDAAiD,CACrG,CACD,EAAQ,OAAO,EAGV,CAAE,SAAQ,CAInB,GAAI,CACF,EAAiC,EAAa,EAAU,CACxD,EAA2C,CAAC,EAAa,EAAU,CAAE,EAAO,iBAAiB,MACvF,CASN,OARK,EAAG,OAAO,UACb,EACE,QACA,IAAI,EAAmB,EAAY,eAAgB,+CAA+C,CACnG,CACD,EAAQ,OAAO,EAGV,CAAE,SAAQ,CAoFnB,OAjFC,SAAY,CAYX,IAAM,EAVgB,MAAM,EAC1B,CACE,cACA,cAAe,EAAY,QAC3B,cACA,cAAe,EAAY,QAC5B,CACD,EACD,CAE6C,EAE9C,GAAI,EAAS,EACN,EAAG,OAAO,UACb,EACE,QACA,IAAI,EACF,EAAY,eACZ,2DAA2D,EAAsB,UAAU,GAC5F,CACF,CACD,EAAQ,OAAO,MAEZ,CACL,GAAM,CAAC,GAAO,EAAO,CACnB,SAAU,EACV,UAAW,EACX,cACA,cAAe,EAAY,QAC3B,cAAe,EAAY,QAC5B,CAAC,CAGI,EAAwB,CAC5B,WAAY,CACV,KAAM,oBACN,GAAI,uBACL,CACD,SAAU,EACV,UAAW,EAAS,EACpB,QAAS,EACT,SAAU,EAGV,UAAW,KAAK,MAAM,KAAK,KAAK,CAAG,IAAM,CAAG,GAC5C,KAAM,CACJ,CACE,KAAM,SACN,KAAM,aACN,OAAQ,EACR,QAAS,EAAY,QACrB,MAAO,EAAqB,EAAY,CACzC,CACF,CACD,cACA,GAAI,OAAO,YAAY,CACvB,cAAe,KACf,YAAa,EAAY,cACzB,YAAa,GAAe,EAC5B,cACA,cACA,YACD,CAEI,EAAG,OAAO,UACb,EAAQ,QAAS,EAAM,CACvB,EAAQ,OAAO,MAGjB,CAAC,MAAO,GAAU,CACf,EAAG,OAAO,UACb,EACE,QACA,IAAI,EAAS,EAAY,QAAS,EAAU,QAAS,CAAE,MAAO,EAAO,QAAS,0BAA2B,CAAC,CAC3G,CACD,EAAQ,OAAO,GAEjB,CAEK,CACL,SACD"}
1
+ {"version":3,"file":"stream-quotes.js","names":[],"sources":["../../../../src/transfer-service/avalanche-evm/_handlers/stream-quotes.ts"],"sourcesContent":["import { ServiceType } from '../../../constants';\nimport { ErrorCode, ErrorReason, InvalidParamsError, SdkError } from '../../../errors';\nimport type { ChainAssetMap } from '../../../types/asset';\nimport type { Quote } from '../../../types/quote';\nimport type { TransferService } from '../../../types/service';\nimport type { Mutable } from '../../../types/utility-types';\nimport { isAddress } from 'viem';\nimport { validateTransferAddressesNotBlockedOrThrow, validateTransferAddressesOrThrow } from '../_utils/validations';\nimport type { EvmConfig } from '../_types';\nimport { analyzeSupportFactory } from './analyze-support';\nimport { calculateMinimumTransferAmount } from './get-minimum-transfer-amount';\nimport { getFee } from '../_utils/fee';\nimport { assetToQuoteFeeToken } from '../../../utils/quote-fees';\n\nexport interface StreamQuotesFactoryConfig {\n config: EvmConfig;\n feeEstimationMultiplier: bigint;\n serviceAssets: ChainAssetMap;\n}\n\nexport function streamQuotesFactory({\n config,\n feeEstimationMultiplier,\n serviceAssets,\n}: StreamQuotesFactoryConfig): TransferService['streamQuotes'] {\n const analyzeSupport = analyzeSupportFactory({ serviceAssets });\n\n return (\n { amount, fromAddress, slippageBps, sourceAsset, sourceChain, targetAsset, targetChain, toAddress },\n handler,\n ) => {\n const ac = new AbortController();\n const cancel = () => {\n ac.abort();\n };\n\n const hasValidAssetsAndChains = analyzeSupport({\n sourceAsset,\n sourceChainId: sourceChain.chainId,\n targetAsset,\n targetChainId: targetChain.chainId,\n });\n\n // Validate to/from addresses are EVM.\n // Verify assets and chains are supported.\n if (!(isAddress(fromAddress) && isAddress(toAddress)) || !hasValidAssetsAndChains) {\n if (!ac.signal.aborted) {\n handler(\n 'error',\n new InvalidParamsError(ErrorReason.INVALID_PARAMS, 'Invalid addresses, assets, or chains provided.'),\n );\n handler('done');\n }\n\n return { cancel };\n }\n\n // Validate addresses match and are not block listed.\n try {\n validateTransferAddressesOrThrow(fromAddress, toAddress);\n validateTransferAddressesNotBlockedOrThrow([fromAddress, toAddress], config.addressBlocklist);\n } catch {\n if (!ac.signal.aborted) {\n handler(\n 'error',\n new InvalidParamsError(ErrorReason.INVALID_PARAMS, 'Transfer addresses are not equal or blocked.'),\n );\n handler('done');\n }\n\n return { cancel };\n }\n\n (async () => {\n // Validate minimum transfer amount\n const minimumAmount = await calculateMinimumTransferAmount(\n {\n sourceAsset,\n sourceChainId: sourceChain.chainId,\n targetAsset,\n targetChainId: targetChain.chainId,\n },\n config,\n );\n\n const minimumTransferAmount = minimumAmount * feeEstimationMultiplier;\n\n if (amount < minimumTransferAmount) {\n if (!ac.signal.aborted) {\n handler(\n 'error',\n new InvalidParamsError(\n ErrorReason.INVALID_PARAMS,\n `Transfer amount is below the minimum transfer amount of ${minimumTransferAmount.toString()}`,\n ),\n );\n handler('done');\n }\n } else {\n const [fee] = getFee({\n amountIn: amount,\n evmConfig: config,\n sourceAsset,\n sourceChainId: sourceChain.chainId,\n targetChainId: targetChain.chainId,\n });\n\n // Create a quote.\n const quote: Mutable<Quote> = {\n aggregator: {\n name: 'Avalanche Bridge™',\n id: 'avalanche-bridge-evm',\n },\n amountIn: amount,\n amountOut: amount - fee,\n assetIn: sourceAsset,\n assetOut: targetAsset,\n // Set the expiration further out since the amountOut isn't expected to change\n // since the Warden config data isn't refreshed. The only expected change would be estimated gas.\n expiresAt: Math.floor(Date.now() / 1_000) + 60,\n fees: [\n {\n type: 'bridge',\n fundingModel: 'included',\n name: 'Bridge Fee',\n amount: fee,\n chainId: sourceChain.chainId,\n token: assetToQuoteFeeToken(sourceAsset),\n },\n ],\n fromAddress,\n id: crypto.randomUUID(),\n partnerFeeBps: null,\n serviceType: ServiceType.AVALANCHE_EVM,\n slippageBps: slippageBps ?? 0,\n sourceChain,\n targetChain,\n toAddress,\n };\n\n if (!ac.signal.aborted) {\n handler('quote', quote);\n handler('done');\n }\n }\n })().catch((error) => {\n if (!ac.signal.aborted) {\n handler(\n 'error',\n new SdkError(ErrorReason.UNKNOWN, ErrorCode.UNKNOWN, { cause: error, details: 'Failed to stream quotes' }),\n );\n handler('done');\n }\n });\n\n return {\n cancel,\n };\n };\n}\n"],"mappings":"ojBAoBA,SAAgB,EAAoB,CAClC,SACA,0BACA,iBAC6D,CAC7D,IAAM,EAAiB,EAAsB,CAAE,gBAAe,CAAC,CAE/D,OACE,CAAE,SAAQ,cAAa,cAAa,cAAa,cAAa,cAAa,cAAa,aACxF,IACG,CACH,IAAM,EAAK,IAAI,gBACT,MAAe,CACnB,EAAG,O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,QAAS,EAAM,CACvB,EAAQ,OAAO,MAGjB,CAAC,MAAO,GAAU,CACf,EAAG,OAAO,UACb,EACE,QACA,IAAI,EAAS,EAAY,QAAS,EAAU,QAAS,CAAE,MAAO,EAAO,QAAS,0BAA2B,CAAC,CAC3G,CACD,EAAQ,OAAO,GAEjB,CAEK,CACL,SACD"}
@@ -1,2 +1,2 @@
1
- const e=require(`../../../../constants.cjs`),t=require(`../../../../errors.cjs`),n=require(`../../../../utils/bitcoin-address.cjs`),r=require(`../../../../utils/quote-fees.cjs`),i=require(`../../../../utils/evm-address.cjs`),a=require(`./analyze-support.cjs`),o=require(`./get-minimum-transfer-amount.cjs`);function s({config:s}){let c=a.analyzeSupportFactory({config:s});return(a,l)=>{let u=new AbortController,d=()=>{u.abort()};return!c({sourceAsset:a.sourceAsset,sourceChainId:a.sourceChain.chainId,targetAsset:a.targetAsset,targetChainId:a.targetChain.chainId})||!n.isBech32AddressInNetwork(a.fromAddress,s.sourceChain===e.BitcoinChainIds.MAINNET)||!i.isEvmAddress(a.toAddress)?(u.signal.aborted||(l(`error`,new t.InvalidParamsError(t.ErrorReason.INVALID_PARAMS,`Invalid addresses, assets, or chains provided.`)),l(`done`)),{cancel:d}):((async()=>{let n=o.getMinimumTransferAmount();if(a.amount<n){u.signal.aborted||(l(`error`,new t.InvalidParamsError(t.ErrorReason.INVALID_PARAMS,`Transfer amount is below the minimum transfer amount of ${n.toString()}.`)),l(`done`));return}let i={aggregator:{name:`Lombard`,id:`lombard-btc-to-btcb`},amountIn:a.amount,amountOut:a.amount-s.mintingFee,assetIn:a.sourceAsset,assetOut:a.targetAsset,expiresAt:Math.floor(Date.now()/1e3)+60,fees:[{type:`bridge`,name:`Bridge Fee`,amount:s.mintingFee,chainId:a.sourceChain.chainId,token:r.assetToQuoteFeeToken(a.sourceAsset)}],fromAddress:a.fromAddress,id:crypto.randomUUID(),partnerFeeBps:null,serviceType:e.ServiceType.LOMBARD_BTC_TO_BTCB,slippageBps:a.slippageBps??0,sourceChain:a.sourceChain,targetChain:a.targetChain,toAddress:a.toAddress};u.signal.aborted||(l(`quote`,i),l(`done`))})().catch(e=>{u.signal.aborted||(l(`error`,new t.SdkError(t.ErrorReason.UNKNOWN,t.ErrorCode.UNKNOWN,{cause:e,details:`Failed to stream quotes`})),l(`done`))}),{cancel:d})}}exports.streamQuotesFactory=s;
1
+ const e=require(`../../../../constants.cjs`),t=require(`../../../../errors.cjs`),n=require(`../../../../utils/bitcoin-address.cjs`),r=require(`../../../../utils/quote-fees.cjs`),i=require(`../../../../utils/evm-address.cjs`),a=require(`./analyze-support.cjs`),o=require(`./get-minimum-transfer-amount.cjs`);function s({config:s}){let c=a.analyzeSupportFactory({config:s});return(a,l)=>{let u=new AbortController,d=()=>{u.abort()};return!c({sourceAsset:a.sourceAsset,sourceChainId:a.sourceChain.chainId,targetAsset:a.targetAsset,targetChainId:a.targetChain.chainId})||!n.isBech32AddressInNetwork(a.fromAddress,s.sourceChain===e.BitcoinChainIds.MAINNET)||!i.isEvmAddress(a.toAddress)?(u.signal.aborted||(l(`error`,new t.InvalidParamsError(t.ErrorReason.INVALID_PARAMS,`Invalid addresses, assets, or chains provided.`)),l(`done`)),{cancel:d}):((async()=>{let n=o.getMinimumTransferAmount();if(a.amount<n){u.signal.aborted||(l(`error`,new t.InvalidParamsError(t.ErrorReason.INVALID_PARAMS,`Transfer amount is below the minimum transfer amount of ${n.toString()}.`)),l(`done`));return}let i={aggregator:{name:`Lombard`,id:`lombard-btc-to-btcb`},amountIn:a.amount,amountOut:a.amount-s.mintingFee,assetIn:a.sourceAsset,assetOut:a.targetAsset,expiresAt:Math.floor(Date.now()/1e3)+60,fees:[{type:`bridge`,fundingModel:`included`,name:`Bridge Fee`,amount:s.mintingFee,chainId:a.sourceChain.chainId,token:r.assetToQuoteFeeToken(a.sourceAsset)}],fromAddress:a.fromAddress,id:crypto.randomUUID(),partnerFeeBps:null,serviceType:e.ServiceType.LOMBARD_BTC_TO_BTCB,slippageBps:a.slippageBps??0,sourceChain:a.sourceChain,targetChain:a.targetChain,toAddress:a.toAddress};u.signal.aborted||(l(`quote`,i),l(`done`))})().catch(e=>{u.signal.aborted||(l(`error`,new t.SdkError(t.ErrorReason.UNKNOWN,t.ErrorCode.UNKNOWN,{cause:e,details:`Failed to stream quotes`})),l(`done`))}),{cancel:d})}}exports.streamQuotesFactory=s;
2
2
  //# sourceMappingURL=stream-quotes.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"stream-quotes.cjs","names":["analyzeSupportFactory","isBech32AddressInNetwork","BitcoinChainIds","isEvmAddress","InvalidParamsError","ErrorReason","getMinimumTransferAmount","assetToQuoteFeeToken","ServiceType","SdkError","ErrorCode"],"sources":["../../../../../src/transfer-service/lombard/btc-to-btcb/_handlers/stream-quotes.ts"],"sourcesContent":["import { BitcoinChainIds, ServiceType } from '../../../../constants';\nimport { ErrorCode, ErrorReason, InvalidParamsError, SdkError } from '../../../../errors';\nimport type { Quote } from '../../../../types/quote';\nimport type { TransferService } from '../../../../types/service';\nimport type { Mutable } from '../../../../types/utility-types';\nimport { isBech32AddressInNetwork } from '../../../../utils/bitcoin-address';\nimport { isEvmAddress } from '../../../../utils/evm-address';\nimport { assetToQuoteFeeToken } from '../../../../utils/quote-fees';\nimport type { BtcToBtcbConfig } from '../../types';\nimport { analyzeSupportFactory } from './analyze-support';\nimport { getMinimumTransferAmount } from './get-minimum-transfer-amount';\n\nexport interface StreamQuotesFactoryOptions {\n config: BtcToBtcbConfig;\n}\n\nexport function streamQuotesFactory({ config }: StreamQuotesFactoryOptions): TransferService['streamQuotes'] {\n const analyzeSupport = analyzeSupportFactory({ config });\n\n return (props, handler) => {\n const ac = new AbortController();\n const cancel = () => {\n ac.abort();\n };\n\n const hasValidAssetsAndChains = analyzeSupport({\n sourceAsset: props.sourceAsset,\n sourceChainId: props.sourceChain.chainId,\n targetAsset: props.targetAsset,\n targetChainId: props.targetChain.chainId,\n });\n\n if (\n !hasValidAssetsAndChains ||\n !isBech32AddressInNetwork(props.fromAddress, config.sourceChain === BitcoinChainIds.MAINNET) ||\n !isEvmAddress(props.toAddress)\n ) {\n if (!ac.signal.aborted) {\n handler(\n 'error',\n new InvalidParamsError(ErrorReason.INVALID_PARAMS, 'Invalid addresses, assets, or chains provided.'),\n );\n handler('done');\n }\n\n return { cancel };\n }\n\n (async () => {\n const minimumAmount = getMinimumTransferAmount();\n\n if (props.amount < minimumAmount) {\n if (!ac.signal.aborted) {\n handler(\n 'error',\n new InvalidParamsError(\n ErrorReason.INVALID_PARAMS,\n `Transfer amount is below the minimum transfer amount of ${minimumAmount.toString()}.`,\n ),\n );\n handler('done');\n }\n\n return;\n }\n\n const quote: Mutable<Quote> = {\n aggregator: {\n name: 'Lombard',\n id: 'lombard-btc-to-btcb',\n },\n amountIn: props.amount,\n amountOut: props.amount - config.mintingFee,\n assetIn: props.sourceAsset,\n assetOut: props.targetAsset,\n // Set the expiration time further out since the amountOut isn't expected to change,\n // since the fee is fixed in the config at service initialization.\n expiresAt: Math.floor(Date.now() / 1_000) + 60,\n fees: [\n {\n type: 'bridge',\n name: 'Bridge Fee',\n amount: config.mintingFee,\n chainId: props.sourceChain.chainId,\n token: assetToQuoteFeeToken(props.sourceAsset),\n },\n ],\n fromAddress: props.fromAddress,\n id: crypto.randomUUID(),\n partnerFeeBps: null,\n serviceType: ServiceType.LOMBARD_BTC_TO_BTCB,\n slippageBps: props.slippageBps ?? 0,\n sourceChain: props.sourceChain,\n targetChain: props.targetChain,\n toAddress: props.toAddress,\n };\n\n if (!ac.signal.aborted) {\n handler('quote', quote);\n handler('done');\n }\n })().catch((error) => {\n if (!ac.signal.aborted) {\n handler(\n 'error',\n new SdkError(ErrorReason.UNKNOWN, ErrorCode.UNKNOWN, { cause: error, details: 'Failed to stream quotes' }),\n );\n handler('done');\n }\n });\n\n return {\n cancel,\n };\n };\n}\n"],"mappings":"mTAgBA,SAAgB,EAAoB,CAAE,UAAuE,CAC3G,IAAM,EAAiBA,EAAAA,sBAAs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B,EAAQ,OAAO,KAEf,CAAC,MAAO,GAAU,CACf,EAAG,OAAO,UACb,EACE,QACA,IAAIC,EAAAA,SAASJ,EAAAA,YAAY,QAASK,EAAAA,UAAU,QAAS,CAAE,MAAO,EAAO,QAAS,0BAA2B,CAAC,CAC3G,CACD,EAAQ,OAAO,GAEjB,CAEK,CACL,SACD"}
1
+ {"version":3,"file":"stream-quotes.cjs","names":["analyzeSupportFactory","isBech32AddressInNetwork","BitcoinChainIds","isEvmAddress","InvalidParamsError","ErrorReason","getMinimumTransferAmount","assetToQuoteFeeToken","ServiceType","SdkError","ErrorCode"],"sources":["../../../../../src/transfer-service/lombard/btc-to-btcb/_handlers/stream-quotes.ts"],"sourcesContent":["import { BitcoinChainIds, ServiceType } from '../../../../constants';\nimport { ErrorCode, ErrorReason, InvalidParamsError, SdkError } from '../../../../errors';\nimport type { Quote } from '../../../../types/quote';\nimport type { TransferService } from '../../../../types/service';\nimport type { Mutable } from '../../../../types/utility-types';\nimport { isBech32AddressInNetwork } from '../../../../utils/bitcoin-address';\nimport { isEvmAddress } from '../../../../utils/evm-address';\nimport { assetToQuoteFeeToken } from '../../../../utils/quote-fees';\nimport type { BtcToBtcbConfig } from '../../types';\nimport { analyzeSupportFactory } from './analyze-support';\nimport { getMinimumTransferAmount } from './get-minimum-transfer-amount';\n\nexport interface StreamQuotesFactoryOptions {\n config: BtcToBtcbConfig;\n}\n\nexport function streamQuotesFactory({ config }: StreamQuotesFactoryOptions): TransferService['streamQuotes'] {\n const analyzeSupport = analyzeSupportFactory({ config });\n\n return (props, handler) => {\n const ac = new AbortController();\n const cancel = () => {\n ac.abort();\n };\n\n const hasValidAssetsAndChains = analyzeSupport({\n sourceAsset: props.sourceAsset,\n sourceChainId: props.sourceChain.chainId,\n targetAsset: props.targetAsset,\n targetChainId: props.targetChain.chainId,\n });\n\n if (\n !hasValidAssetsAndChains ||\n !isBech32AddressInNetwork(props.fromAddress, config.sourceChain === BitcoinChainIds.MAINNET) ||\n !isEvmAddress(props.toAddress)\n ) {\n if (!ac.signal.aborted) {\n handler(\n 'error',\n new InvalidParamsError(ErrorReason.INVALID_PARAMS, 'Invalid addresses, assets, or chains provided.'),\n );\n handler('done');\n }\n\n return { cancel };\n }\n\n (async () => {\n const minimumAmount = getMinimumTransferAmount();\n\n if (props.amount < minimumAmount) {\n if (!ac.signal.aborted) {\n handler(\n 'error',\n new InvalidParamsError(\n ErrorReason.INVALID_PARAMS,\n `Transfer amount is below the minimum transfer amount of ${minimumAmount.toString()}.`,\n ),\n );\n handler('done');\n }\n\n return;\n }\n\n const quote: Mutable<Quote> = {\n aggregator: {\n name: 'Lombard',\n id: 'lombard-btc-to-btcb',\n },\n amountIn: props.amount,\n amountOut: props.amount - config.mintingFee,\n assetIn: props.sourceAsset,\n assetOut: props.targetAsset,\n // Set the expiration time further out since the amountOut isn't expected to change,\n // since the fee is fixed in the config at service initialization.\n expiresAt: Math.floor(Date.now() / 1_000) + 60,\n fees: [\n {\n type: 'bridge',\n fundingModel: 'included',\n name: 'Bridge Fee',\n amount: config.mintingFee,\n chainId: props.sourceChain.chainId,\n token: assetToQuoteFeeToken(props.sourceAsset),\n },\n ],\n fromAddress: props.fromAddress,\n id: crypto.randomUUID(),\n partnerFeeBps: null,\n serviceType: ServiceType.LOMBARD_BTC_TO_BTCB,\n slippageBps: props.slippageBps ?? 0,\n sourceChain: props.sourceChain,\n targetChain: props.targetChain,\n toAddress: props.toAddress,\n };\n\n if (!ac.signal.aborted) {\n handler('quote', quote);\n handler('done');\n }\n })().catch((error) => {\n if (!ac.signal.aborted) {\n handler(\n 'error',\n new SdkError(ErrorReason.UNKNOWN, ErrorCode.UNKNOWN, { cause: error, details: 'Failed to stream quotes' }),\n );\n handler('done');\n }\n });\n\n return {\n cancel,\n };\n };\n}\n"],"mappings":"mTAgBA,SAAgB,EAAoB,CAAE,UAAuE,CAC3G,IAAM,EAAiBA,EAAAA,sBAAsB,CAAE,SAAQ,CAAC,CAExD,OAAQ,EAAO,IAA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