@avalabs/fusion-sdk 0.9.0 → 0.11.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 (49) hide show
  1. package/dist/constants.cjs +1 -1
  2. package/dist/constants.cjs.map +1 -1
  3. package/dist/constants.d.cts +2 -2
  4. package/dist/constants.d.ts +2 -2
  5. package/dist/constants.js +1 -1
  6. package/dist/constants.js.map +1 -1
  7. package/dist/mod.cjs +1 -1
  8. package/dist/mod.d.cts +2 -2
  9. package/dist/mod.d.ts +2 -2
  10. package/dist/mod.js +1 -1
  11. package/dist/transfer-service/avalanche-evm/_handlers/estimate-native-fee.cjs +1 -1
  12. package/dist/transfer-service/avalanche-evm/_handlers/estimate-native-fee.cjs.map +1 -1
  13. package/dist/transfer-service/avalanche-evm/_handlers/estimate-native-fee.js +1 -1
  14. package/dist/transfer-service/avalanche-evm/_handlers/estimate-native-fee.js.map +1 -1
  15. package/dist/transfer-service/avalanche-evm/_handlers/track-transfer.cjs +1 -1
  16. package/dist/transfer-service/avalanche-evm/_handlers/track-transfer.cjs.map +1 -1
  17. package/dist/transfer-service/avalanche-evm/_handlers/track-transfer.js +1 -1
  18. package/dist/transfer-service/avalanche-evm/_handlers/track-transfer.js.map +1 -1
  19. package/dist/transfer-service/lombard/btc-to-btcb/_handlers/estimate-native-fee.cjs +1 -1
  20. package/dist/transfer-service/lombard/btc-to-btcb/_handlers/estimate-native-fee.cjs.map +1 -1
  21. package/dist/transfer-service/lombard/btc-to-btcb/_handlers/estimate-native-fee.js +1 -1
  22. package/dist/transfer-service/lombard/btc-to-btcb/_handlers/estimate-native-fee.js.map +1 -1
  23. package/dist/transfer-service/lombard/btcb-to-btc/_handlers/estimate-native-fee.cjs +1 -1
  24. package/dist/transfer-service/lombard/btcb-to-btc/_handlers/estimate-native-fee.cjs.map +1 -1
  25. package/dist/transfer-service/lombard/btcb-to-btc/_handlers/estimate-native-fee.js +1 -1
  26. package/dist/transfer-service/lombard/btcb-to-btc/_handlers/estimate-native-fee.js.map +1 -1
  27. package/dist/transfer-service/markr/_handlers/estimate-native-fee.cjs +1 -1
  28. package/dist/transfer-service/markr/_handlers/estimate-native-fee.cjs.map +1 -1
  29. package/dist/transfer-service/markr/_handlers/estimate-native-fee.js +1 -1
  30. package/dist/transfer-service/markr/_handlers/estimate-native-fee.js.map +1 -1
  31. package/dist/transfer-service/markr/_handlers/transfer-asset.cjs +1 -1
  32. package/dist/transfer-service/markr/_handlers/transfer-asset.cjs.map +1 -1
  33. package/dist/transfer-service/markr/_handlers/transfer-asset.js +1 -1
  34. package/dist/transfer-service/markr/_handlers/transfer-asset.js.map +1 -1
  35. package/dist/transfer-service/markr/_solana-utils.cjs +2 -0
  36. package/dist/transfer-service/markr/_solana-utils.cjs.map +1 -0
  37. package/dist/transfer-service/markr/_solana-utils.js +2 -0
  38. package/dist/transfer-service/markr/_solana-utils.js.map +1 -0
  39. package/dist/transfer-service/markr/_utils.cjs +1 -1
  40. package/dist/transfer-service/markr/_utils.cjs.map +1 -1
  41. package/dist/transfer-service/markr/_utils.js +1 -1
  42. package/dist/transfer-service/markr/_utils.js.map +1 -1
  43. package/dist/transfer-service/wrap-unwrap/_handlers/estimate-native-fee.cjs +1 -1
  44. package/dist/transfer-service/wrap-unwrap/_handlers/estimate-native-fee.cjs.map +1 -1
  45. package/dist/transfer-service/wrap-unwrap/_handlers/estimate-native-fee.js +1 -1
  46. package/dist/transfer-service/wrap-unwrap/_handlers/estimate-native-fee.js.map +1 -1
  47. package/dist/types/service.d.cts +6 -2
  48. package/dist/types/service.d.ts +6 -2
  49. package/package.json +3 -3
@@ -1 +1 @@
1
- {"version":3,"file":"track-transfer.js","names":[],"sources":["../../../../src/transfer-service/avalanche-evm/_handlers/track-transfer.ts"],"sourcesContent":["import { isAddress, isHash, type Hash } from 'viem';\nimport { ErrorCode } from '../../../errors';\nimport type { Asset } from '../../../types/asset';\nimport type { Caip2ChainId } from '../../../types/caip';\nimport type {\n CompletedTransfer,\n FailedTransfer,\n SourcePendingTransfer,\n TargetPendingTransfer,\n Transfer,\n} from '../../../types/transfer';\nimport type { TransferService } from '../../../types/service';\nimport { awaitOrAbort, getEvmClientForChain } from '../../_utils';\nimport type { EvmConfig } from '../_types';\nimport { getTransferData } from '../_utils/transfer-data';\n\nexport interface TrackTransferFactoryConfig {\n config: EvmConfig;\n}\n\nexport function trackTransferFactory({\n config: evmConfig,\n}: TrackTransferFactoryConfig): TransferService['trackTransfer'] {\n return ({ transfer, updateListener }) => {\n const ac = new AbortController();\n const cancel = () => {\n ac.abort();\n };\n\n // Failed and completed transfers are terminal states.\n if (transfer.status === 'completed' || transfer.status === 'failed') {\n return {\n cancel,\n result: Promise.resolve(transfer),\n };\n }\n\n const { txHash } = transfer.source;\n\n if (!isHash(txHash)) {\n const failedTransfer: FailedTransfer = {\n ...transfer,\n errorCode: ErrorCode.INVALID_PARAMS,\n errorReason: 'Invalid source transaction hash',\n failedAtMs: Date.now(),\n status: 'failed',\n };\n updateListener(failedTransfer);\n\n return {\n cancel,\n result: Promise.resolve(failedTransfer),\n };\n }\n\n const sourceClient = getEvmClientForChain({ chain: transfer.sourceChain });\n const targetClient = getEvmClientForChain({ chain: transfer.targetChain });\n let currentTransfer: Transfer = structuredClone(transfer);\n\n const executeTracking = async (): Promise<Transfer> => {\n // Create a loop to poll for confirmations and update the transfer status until\n // failed or completed.\n while (!ac.signal.aborted) {\n switch (currentTransfer.status) {\n case 'source-pending': {\n currentTransfer = await _trackSourceTx({\n currentTransfer,\n signal: ac.signal,\n sourceClient,\n });\n updateListener(currentTransfer);\n break;\n }\n case 'target-pending': {\n currentTransfer = await _trackTargetTx({\n assetIn: currentTransfer.sourceAsset,\n currentTransfer,\n evmConfig,\n signal: ac.signal,\n sourceChainId: currentTransfer.sourceChain.chainId,\n targetChainId: currentTransfer.targetChain.chainId,\n targetClient,\n });\n updateListener(currentTransfer);\n break;\n }\n default: {\n return currentTransfer;\n }\n }\n }\n\n return currentTransfer;\n };\n\n return {\n cancel,\n result: executeTracking(),\n };\n };\n}\n\nexport interface TrackBaseParams {\n currentTransfer: Transfer;\n signal: AbortSignal;\n}\n\nexport interface TrackSourceTxParams extends TrackBaseParams {\n currentTransfer: SourcePendingTransfer;\n sourceClient: ReturnType<typeof getEvmClientForChain>;\n}\n\nexport interface TrackTargetTxParams extends TrackBaseParams {\n assetIn: Asset;\n currentTransfer: TargetPendingTransfer;\n evmConfig: EvmConfig;\n sourceChainId: Caip2ChainId;\n targetChainId: Caip2ChainId;\n targetClient: ReturnType<typeof getEvmClientForChain>;\n}\n\nexport async function _trackSourceTx({\n currentTransfer,\n signal,\n sourceClient,\n}: TrackSourceTxParams): Promise<Transfer> {\n const { confirmationCount, requiredConfirmationCount, txHash } = currentTransfer.source;\n\n if (!isHash(txHash)) {\n const failedTransfer: FailedTransfer = {\n ...currentTransfer,\n errorCode: ErrorCode.INVALID_PARAMS,\n errorReason: 'Invalid source transaction hash',\n failedAtMs: Date.now(),\n status: 'failed',\n };\n return failedTransfer;\n }\n\n if (confirmationCount >= requiredConfirmationCount) {\n // Move to target-pending\n\n const updatedTransfer: TargetPendingTransfer = {\n ...currentTransfer,\n status: 'target-pending',\n target: {\n confirmationCount: 0,\n requiredConfirmationCount: 1, // EVM txs need 1 confirmation on target chain.\n startedAtMs: Date.now(),\n },\n };\n\n return updatedTransfer;\n }\n\n try {\n const receiptResult = await awaitOrAbort(\n sourceClient.waitForTransactionReceipt({\n confirmations: Math.min(confirmationCount + 1, requiredConfirmationCount),\n hash: txHash,\n }),\n signal,\n );\n\n if (receiptResult.status === 'aborted') {\n return currentTransfer;\n }\n\n const transactionReceipt = receiptResult.value;\n\n if (transactionReceipt.status === 'reverted') {\n return {\n ...currentTransfer,\n errorCode: ErrorCode.TRANSACTION_REVERTED,\n errorReason: 'Source transaction was reverted',\n failedAtMs: Date.now(),\n status: 'failed',\n } satisfies FailedTransfer;\n }\n\n // Either update confirmation count or move to target-pending\n const newConfirmationCount: number = await sourceClient\n .getTransactionConfirmations({\n // hash: txHash,\n transactionReceipt,\n })\n .then((count) => Number(count))\n .catch(() => confirmationCount + 1);\n\n // If still not enough confirmations, just update count\n if (newConfirmationCount < requiredConfirmationCount) {\n return {\n ...currentTransfer,\n source: {\n ...currentTransfer.source,\n confirmationCount: newConfirmationCount,\n },\n } satisfies SourcePendingTransfer;\n }\n\n // Move to target-pending\n return {\n ...currentTransfer,\n source: {\n ...currentTransfer.source,\n confirmationCount: newConfirmationCount,\n },\n status: 'target-pending',\n target: {\n confirmationCount: 0,\n requiredConfirmationCount: 1, // EVM txs need 1 confirmation on target chain.\n startedAtMs: Date.now(),\n },\n } satisfies TargetPendingTransfer;\n } catch {\n return currentTransfer;\n }\n}\n\nexport async function _trackTargetTx({\n assetIn,\n currentTransfer,\n evmConfig,\n signal,\n sourceChainId,\n targetChainId,\n targetClient,\n}: TrackTargetTxParams): Promise<Transfer> {\n const target = currentTransfer.target ?? {\n confirmationCount: 0,\n requiredConfirmationCount: 1,\n startedAtMs: Date.now(),\n };\n\n if (target.confirmationCount >= target.requiredConfirmationCount) {\n // Move to completed\n const completedTransfer: Transfer = {\n ...currentTransfer,\n status: 'completed',\n completedAtMs: Date.now(),\n };\n return completedTransfer;\n }\n\n if (!target.txHash) {\n // Find the target tx hash via logs and then update the transfer.\n const {\n ethToAva,\n target: {\n token: { address: contractAddress },\n },\n } = getTransferData(\n {\n assetIn,\n sourceChainId,\n targetChainId,\n },\n evmConfig,\n );\n\n const targetTxHash = ethToAva\n ? await _getTargetTxHashEthToAva({\n contractAddress,\n sourceTxHash: currentTransfer.source.txHash,\n targetClient,\n targetStartBlockNumber: currentTransfer.source.targetStartBlockNumber,\n })\n : await _getTargetTxHashAvaToEth({\n contractAddress,\n evmConfig,\n fromAddress: currentTransfer.fromAddress,\n targetClient,\n targetStartBlockNumber: currentTransfer.source.targetStartBlockNumber,\n });\n\n if (!targetTxHash) {\n return currentTransfer;\n }\n\n return {\n ...currentTransfer,\n target: {\n ...target,\n txHash: targetTxHash,\n },\n } satisfies TargetPendingTransfer;\n }\n\n if (!isHash(target.txHash)) {\n return {\n ...currentTransfer,\n errorCode: ErrorCode.INVALID_PARAMS,\n errorReason: 'Invalid target transaction hash',\n failedAtMs: Date.now(),\n status: 'failed',\n } satisfies FailedTransfer;\n }\n\n try {\n const receiptResult = await awaitOrAbort(\n targetClient.waitForTransactionReceipt({\n // Wait for all required confirmations (which should just be 1 on target).\n // This is a safety in case it's ever more than 1. We just won't \"poll\" multiple times\n // like we do for the source.\n confirmations: target.requiredConfirmationCount,\n hash: target.txHash,\n }),\n signal,\n );\n\n if (receiptResult.status === 'aborted') {\n return currentTransfer;\n }\n\n const transactionReceipt = receiptResult.value;\n\n if (transactionReceipt.status === 'reverted') {\n return {\n ...currentTransfer,\n errorCode: ErrorCode.TRANSACTION_REVERTED,\n errorReason: 'Target transaction was reverted',\n failedAtMs: Date.now(),\n status: 'failed',\n } satisfies FailedTransfer;\n }\n\n return {\n ...currentTransfer,\n completedAtMs: Date.now(),\n status: 'completed',\n target: {\n ...target,\n confirmationCount: await targetClient\n .getTransactionConfirmations({\n // hash: target.txHash,\n transactionReceipt,\n })\n .then((count) => Number(count))\n .catch(() => target.requiredConfirmationCount),\n },\n } satisfies CompletedTransfer;\n } catch {\n return currentTransfer;\n }\n}\n\nexport async function _getTargetTxHashEthToAva({\n contractAddress,\n sourceTxHash,\n targetClient,\n targetStartBlockNumber,\n}: {\n contractAddress: Hash;\n sourceTxHash: string;\n targetClient: ReturnType<typeof getEvmClientForChain>;\n targetStartBlockNumber: bigint | undefined;\n}): Promise<Hash | null> {\n if (targetStartBlockNumber === undefined) {\n return null;\n }\n\n const logs = await targetClient.getLogs({\n address: contractAddress,\n event: {\n inputs: [\n {\n indexed: false,\n name: 'to',\n type: 'address',\n },\n {\n indexed: false,\n name: 'amount',\n type: 'uint256',\n },\n {\n indexed: false,\n name: 'feeAddress',\n type: 'address',\n },\n {\n indexed: false,\n name: 'feeAmount',\n type: 'uint256',\n },\n {\n indexed: false,\n name: 'originTxId',\n type: 'bytes32',\n },\n ],\n name: 'Mint',\n type: 'event',\n },\n fromBlock: targetStartBlockNumber,\n toBlock: 'latest', // TODO: Consider limiting the range?\n });\n\n // OriginTxId is not indexed. So it cannot be part of the filter.\n const foundLog = logs.find((log) => log.args.originTxId === sourceTxHash); // TODO: Is my assumption correct here? ie that we want to compare the sourceTxHash?\n\n if (!foundLog) {\n return null;\n }\n\n return foundLog.transactionHash;\n}\n\nexport async function _getTargetTxHashAvaToEth({\n contractAddress,\n evmConfig,\n fromAddress,\n targetClient,\n targetStartBlockNumber,\n}: {\n contractAddress: Hash;\n evmConfig: EvmConfig;\n fromAddress: string;\n targetClient: ReturnType<typeof getEvmClientForChain>;\n targetStartBlockNumber: bigint | undefined;\n}): Promise<Hash | null> {\n if (targetStartBlockNumber === undefined) {\n return null;\n }\n\n if (!isAddress(fromAddress)) {\n return null;\n }\n\n const logs = await targetClient.getLogs({\n address: contractAddress,\n args: {\n dst: fromAddress,\n src: evmConfig.walletAddresses.ethereum,\n },\n event: {\n inputs: [\n {\n indexed: true,\n name: 'src',\n type: 'address',\n },\n {\n indexed: true,\n name: 'dst',\n type: 'address',\n },\n {\n indexed: false,\n name: 'wad',\n type: 'uint256',\n },\n ],\n name: 'Transfer',\n type: 'event',\n },\n fromBlock: targetStartBlockNumber,\n toBlock: 'latest', // TODO: Consider limiting the range?\n });\n\n const foundLog = logs.at(0); // TODO: Is this correct? Original code was using -1. But we want \"oldest\" log right? ie the closest to the \"fromBlock\"?\n\n if (!foundLog) {\n return null;\n }\n\n return foundLog.transactionHash;\n}\n"],"mappings":"kOAoBA,SAAgB,EAAqB,CACnC,OAAQ,GACuD,CAC/D,OAAQ,CAAE,WAAU,oBAAqB,CACvC,IAAM,EAAK,IAAI,gBACT,MAAe,CACnB,EAAG,OAAO,EAIZ,GAAI,EAAS,SAAW,aAAe,EAAS,SAAW,SACzD,MAAO,CACL,SACA,OAAQ,QAAQ,QAAQ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P,KAMX,eAAsB,EAAyB,CAC7C,kBACA,YACA,cACA,eACA,0BAOuB,CAKvB,GAJI,IAA2B,IAAA,IAI3B,CAAC,EAAU,EAAY,CACzB,OAAO,KAkCT,IAAM,GA/BO,MAAM,EAAa,QAAQ,CACtC,QAAS,EACT,KAAM,CACJ,IAAK,EACL,IAAK,EAAU,gBAAgB,SAChC,CACD,MAAO,CACL,OAAQ,CACN,CACE,QAAS,GACT,KAAM,MACN,KAAM,UACP,CACD,CACE,QAAS,GACT,KAAM,MACN,KAAM,UACP,CACD,CACE,QAAS,GACT,KAAM,MACN,KAAM,UACP,CACF,CACD,KAAM,WACN,KAAM,QACP,CACD,UAAW,EACX,QAAS,SACV,CAAC,EAEoB,GAAG,EAAE,CAM3B,OAJK,EAIE,EAAS,gBAHP"}
1
+ {"version":3,"file":"track-transfer.js","names":[],"sources":["../../../../src/transfer-service/avalanche-evm/_handlers/track-transfer.ts"],"sourcesContent":["import { isAddress, isHash, type Hash } from 'viem';\nimport { ErrorCode } from '../../../errors';\nimport type { Asset } from '../../../types/asset';\nimport type { Caip2ChainId } from '../../../types/caip';\nimport type {\n CompletedTransfer,\n FailedTransfer,\n SourcePendingTransfer,\n TargetPendingTransfer,\n Transfer,\n} from '../../../types/transfer';\nimport type { TransferService } from '../../../types/service';\nimport { awaitOrAbort, getEvmClientForChain } from '../../_utils';\nimport type { EvmConfig } from '../_types';\nimport { getTransferData } from '../_utils/transfer-data';\n\nconst MAX_LOG_BLOCK_RANGE = 1024n;\nconst TARGET_HASH_POLL_INTERVAL_MS = 15_000;\n\ntype LogSearchRange = {\n fromBlock: bigint | 'earliest';\n toBlock: bigint | 'latest';\n};\n\nexport interface TrackTransferFactoryConfig {\n config: EvmConfig;\n}\n\nexport function trackTransferFactory({\n config: evmConfig,\n}: TrackTransferFactoryConfig): TransferService['trackTransfer'] {\n return ({ transfer, updateListener }) => {\n const ac = new AbortController();\n const cancel = () => {\n ac.abort();\n };\n\n // Failed and completed transfers are terminal states.\n if (transfer.status === 'completed' || transfer.status === 'failed') {\n return {\n cancel,\n result: Promise.resolve(transfer),\n };\n }\n\n const { txHash } = transfer.source;\n\n if (!isHash(txHash)) {\n const failedTransfer: FailedTransfer = {\n ...transfer,\n errorCode: ErrorCode.INVALID_PARAMS,\n errorReason: 'Invalid source transaction hash',\n failedAtMs: Date.now(),\n status: 'failed',\n };\n updateListener(failedTransfer);\n\n return {\n cancel,\n result: Promise.resolve(failedTransfer),\n };\n }\n\n const sourceClient = getEvmClientForChain({ chain: transfer.sourceChain });\n const targetClient = getEvmClientForChain({ chain: transfer.targetChain });\n let currentTransfer: Transfer = structuredClone(transfer);\n\n const executeTracking = async (): Promise<Transfer> => {\n // Create a loop to poll for confirmations and update the transfer status until\n // failed or completed.\n while (!ac.signal.aborted) {\n let shouldDelayAfterUpdate = false;\n\n switch (currentTransfer.status) {\n case 'source-pending': {\n currentTransfer = await _trackSourceTx({\n currentTransfer,\n signal: ac.signal,\n sourceClient,\n });\n updateListener(currentTransfer);\n break;\n }\n case 'target-pending': {\n currentTransfer = await _trackTargetTx({\n assetIn: currentTransfer.sourceAsset,\n currentTransfer,\n evmConfig,\n signal: ac.signal,\n sourceChainId: currentTransfer.sourceChain.chainId,\n targetChainId: currentTransfer.targetChain.chainId,\n targetClient,\n });\n shouldDelayAfterUpdate = currentTransfer.status === 'target-pending' && !currentTransfer.target?.txHash;\n updateListener(currentTransfer);\n\n if (shouldDelayAfterUpdate) {\n await _waitForNextTargetHashPollingCycle({ signal: ac.signal });\n }\n\n break;\n }\n default: {\n return currentTransfer;\n }\n }\n }\n\n return currentTransfer;\n };\n\n return {\n cancel,\n result: executeTracking(),\n };\n };\n}\n\nexport interface TrackBaseParams {\n currentTransfer: Transfer;\n signal: AbortSignal;\n}\n\nexport interface TrackSourceTxParams extends TrackBaseParams {\n currentTransfer: SourcePendingTransfer;\n sourceClient: ReturnType<typeof getEvmClientForChain>;\n}\n\nexport interface TrackTargetTxParams extends TrackBaseParams {\n assetIn: Asset;\n currentTransfer: TargetPendingTransfer;\n evmConfig: EvmConfig;\n sourceChainId: Caip2ChainId;\n targetChainId: Caip2ChainId;\n targetClient: ReturnType<typeof getEvmClientForChain>;\n}\n\nexport async function _trackSourceTx({\n currentTransfer,\n signal,\n sourceClient,\n}: TrackSourceTxParams): Promise<Transfer> {\n const { confirmationCount, requiredConfirmationCount, txHash } = currentTransfer.source;\n\n if (!isHash(txHash)) {\n const failedTransfer: FailedTransfer = {\n ...currentTransfer,\n errorCode: ErrorCode.INVALID_PARAMS,\n errorReason: 'Invalid source transaction hash',\n failedAtMs: Date.now(),\n status: 'failed',\n };\n return failedTransfer;\n }\n\n if (confirmationCount >= requiredConfirmationCount) {\n // Move to target-pending\n\n const updatedTransfer: TargetPendingTransfer = {\n ...currentTransfer,\n status: 'target-pending',\n target: {\n confirmationCount: 0,\n requiredConfirmationCount: 1, // EVM txs need 1 confirmation on target chain.\n startedAtMs: Date.now(),\n },\n };\n\n return updatedTransfer;\n }\n\n try {\n const receiptResult = await awaitOrAbort(\n sourceClient.waitForTransactionReceipt({\n confirmations: Math.min(confirmationCount + 1, requiredConfirmationCount),\n hash: txHash,\n }),\n signal,\n );\n\n if (receiptResult.status === 'aborted') {\n return currentTransfer;\n }\n\n const transactionReceipt = receiptResult.value;\n\n if (transactionReceipt.status === 'reverted') {\n return {\n ...currentTransfer,\n errorCode: ErrorCode.TRANSACTION_REVERTED,\n errorReason: 'Source transaction was reverted',\n failedAtMs: Date.now(),\n status: 'failed',\n } satisfies FailedTransfer;\n }\n\n // Either update confirmation count or move to target-pending\n const newConfirmationCount: number = await sourceClient\n .getTransactionConfirmations({\n // hash: txHash,\n transactionReceipt,\n })\n .then((count) => Number(count))\n .catch(() => confirmationCount + 1);\n\n // If still not enough confirmations, just update count\n if (newConfirmationCount < requiredConfirmationCount) {\n return {\n ...currentTransfer,\n source: {\n ...currentTransfer.source,\n confirmationCount: newConfirmationCount,\n },\n } satisfies SourcePendingTransfer;\n }\n\n // Move to target-pending\n return {\n ...currentTransfer,\n source: {\n ...currentTransfer.source,\n confirmationCount: newConfirmationCount,\n },\n status: 'target-pending',\n target: {\n confirmationCount: 0,\n requiredConfirmationCount: 1, // EVM txs need 1 confirmation on target chain.\n startedAtMs: Date.now(),\n },\n } satisfies TargetPendingTransfer;\n } catch {\n return currentTransfer;\n }\n}\n\nexport async function _trackTargetTx({\n assetIn,\n currentTransfer,\n evmConfig,\n signal,\n sourceChainId,\n targetChainId,\n targetClient,\n}: TrackTargetTxParams): Promise<Transfer> {\n const target = currentTransfer.target ?? {\n confirmationCount: 0,\n requiredConfirmationCount: 1,\n startedAtMs: Date.now(),\n };\n\n if (target.confirmationCount >= target.requiredConfirmationCount) {\n // Move to completed\n const completedTransfer: Transfer = {\n ...currentTransfer,\n status: 'completed',\n completedAtMs: Date.now(),\n };\n return completedTransfer;\n }\n\n if (!target.txHash) {\n // Find the target tx hash via logs and then update the transfer.\n const {\n ethToAva,\n target: {\n token: { address: contractAddress },\n },\n } = getTransferData(\n {\n assetIn,\n sourceChainId,\n targetChainId,\n },\n evmConfig,\n );\n\n const latestBlockNumber =\n typeof targetClient.getBlockNumber === 'function'\n ? await targetClient.getBlockNumber().catch(() => undefined)\n : undefined;\n\n const targetTxHash = await (ethToAva\n ? _getTargetTxHashEthToAva({\n contractAddress,\n latestBlockNumber,\n sourceTxHash: currentTransfer.source.txHash,\n targetClient,\n targetStartBlockNumber: currentTransfer.source.targetStartBlockNumber,\n })\n : _getTargetTxHashAvaToEth({\n contractAddress,\n evmConfig,\n fromAddress: currentTransfer.fromAddress,\n latestBlockNumber,\n targetClient,\n targetStartBlockNumber: currentTransfer.source.targetStartBlockNumber,\n })\n ).catch(() => null);\n\n if (!targetTxHash) {\n if (latestBlockNumber === undefined) {\n return currentTransfer;\n }\n\n return {\n ...currentTransfer,\n source: {\n ...currentTransfer.source,\n targetStartBlockNumber: latestBlockNumber,\n },\n } satisfies TargetPendingTransfer;\n }\n\n return {\n ...currentTransfer,\n target: {\n ...target,\n txHash: targetTxHash,\n },\n } satisfies TargetPendingTransfer;\n }\n\n if (!isHash(target.txHash)) {\n return {\n ...currentTransfer,\n errorCode: ErrorCode.INVALID_PARAMS,\n errorReason: 'Invalid target transaction hash',\n failedAtMs: Date.now(),\n status: 'failed',\n } satisfies FailedTransfer;\n }\n\n try {\n const receiptResult = await awaitOrAbort(\n targetClient.waitForTransactionReceipt({\n // Wait for all required confirmations (which should just be 1 on target).\n // This is a safety in case it's ever more than 1. We just won't \"poll\" multiple times\n // like we do for the source.\n confirmations: target.requiredConfirmationCount,\n hash: target.txHash,\n }),\n signal,\n );\n\n if (receiptResult.status === 'aborted') {\n return currentTransfer;\n }\n\n const transactionReceipt = receiptResult.value;\n\n if (transactionReceipt.status === 'reverted') {\n return {\n ...currentTransfer,\n errorCode: ErrorCode.TRANSACTION_REVERTED,\n errorReason: 'Target transaction was reverted',\n failedAtMs: Date.now(),\n status: 'failed',\n } satisfies FailedTransfer;\n }\n\n return {\n ...currentTransfer,\n completedAtMs: Date.now(),\n status: 'completed',\n target: {\n ...target,\n confirmationCount: await targetClient\n .getTransactionConfirmations({\n // hash: target.txHash,\n transactionReceipt,\n })\n .then((count) => Number(count))\n .catch(() => target.requiredConfirmationCount),\n },\n } satisfies CompletedTransfer;\n } catch {\n return currentTransfer;\n }\n}\n\nexport async function _getTargetTxHashEthToAva({\n contractAddress,\n latestBlockNumber,\n sourceTxHash,\n targetClient,\n targetStartBlockNumber,\n}: {\n contractAddress: Hash;\n latestBlockNumber: bigint | undefined;\n sourceTxHash: string;\n targetClient: ReturnType<typeof getEvmClientForChain>;\n targetStartBlockNumber: bigint | undefined;\n}): Promise<Hash | null> {\n if (targetStartBlockNumber === undefined) {\n return null;\n }\n\n const { fromBlock, toBlock } = _getLogSearchRange({\n latestBlockNumber,\n targetStartBlockNumber,\n });\n\n const logs = await targetClient.getLogs({\n address: contractAddress,\n event: {\n inputs: [\n {\n indexed: false,\n name: 'to',\n type: 'address',\n },\n {\n indexed: false,\n name: 'amount',\n type: 'uint256',\n },\n {\n indexed: false,\n name: 'feeAddress',\n type: 'address',\n },\n {\n indexed: false,\n name: 'feeAmount',\n type: 'uint256',\n },\n {\n indexed: false,\n name: 'originTxId',\n type: 'bytes32',\n },\n ],\n name: 'Mint',\n type: 'event',\n },\n fromBlock,\n toBlock,\n });\n\n // OriginTxId is not indexed. So it cannot be part of the filter.\n const foundLog = logs.find((log) => log.args.originTxId === sourceTxHash);\n\n if (!foundLog) {\n return null;\n }\n\n return foundLog.transactionHash;\n}\n\nexport async function _getTargetTxHashAvaToEth({\n contractAddress,\n evmConfig,\n fromAddress,\n latestBlockNumber,\n targetClient,\n targetStartBlockNumber,\n}: {\n contractAddress: Hash;\n evmConfig: EvmConfig;\n fromAddress: string;\n latestBlockNumber: bigint | undefined;\n targetClient: ReturnType<typeof getEvmClientForChain>;\n targetStartBlockNumber: bigint | undefined;\n}): Promise<Hash | null> {\n if (targetStartBlockNumber === undefined) {\n return null;\n }\n\n if (!isAddress(fromAddress)) {\n return null;\n }\n\n const { fromBlock, toBlock } = _getLogSearchRange({\n latestBlockNumber,\n targetStartBlockNumber,\n });\n\n const logs = await targetClient.getLogs({\n address: contractAddress,\n args: {\n dst: fromAddress,\n src: evmConfig.walletAddresses.ethereum,\n },\n event: {\n inputs: [\n {\n indexed: true,\n name: 'src',\n type: 'address',\n },\n {\n indexed: true,\n name: 'dst',\n type: 'address',\n },\n {\n indexed: false,\n name: 'wad',\n type: 'uint256',\n },\n ],\n name: 'Transfer',\n type: 'event',\n },\n fromBlock,\n toBlock,\n });\n\n const foundLog = logs.at(-1);\n\n if (!foundLog) {\n return null;\n }\n\n return foundLog.transactionHash;\n}\n\nfunction _getLogSearchRange({\n latestBlockNumber,\n targetStartBlockNumber,\n}: {\n latestBlockNumber: bigint | undefined;\n targetStartBlockNumber: bigint;\n}): LogSearchRange {\n const fromBlock =\n targetStartBlockNumber > MAX_LOG_BLOCK_RANGE ? targetStartBlockNumber - MAX_LOG_BLOCK_RANGE : 'earliest';\n const highestBlockNumber = targetStartBlockNumber + MAX_LOG_BLOCK_RANGE;\n const toBlock =\n latestBlockNumber !== undefined && highestBlockNumber < latestBlockNumber ? highestBlockNumber : 'latest';\n\n return {\n fromBlock,\n toBlock,\n };\n}\n\nasync function _waitForNextTargetHashPollingCycle({ signal }: { signal: AbortSignal }): Promise<void> {\n await awaitOrAbort(new Promise<void>((resolve) => setTimeout(resolve, TARGET_HASH_POLL_INTERVAL_MS)), signal);\n}\n"],"mappings":"kOAgBA,MAAM,EAAsB,MAY5B,SAAgB,EAAqB,CACnC,OAAQ,GACuD,CAC/D,OAAQ,CAAE,WAAU,oBAAqB,CACvC,IAAM,EAAK,IAAI,gBACT,MAAe,CACnB,EAAG,OAAO,EAIZ,GAAI,EAAS,SAAW,aAAe,EAAS,SAAW,SACzD,MAAO,CACL,SACA,OAAQ,QAAQ,QAAQ,EAAS,CAClC,CAGH,GAAM,CAAE,UAAW,EAAS,OAE5B,GAAI,CAAC,EAAO,EAAO,CAAE,CACnB,IAAM,EAAiC,CACrC,GAAG,EACH,UAAW,EAAU,eACrB,YAAa,kCACb,WAAY,KAAK,KAAK,CACtB,OAAQ,SACT,CAGD,OAFA,EAAe,EAAe,CAEvB,CACL,SACA,OAAQ,QAAQ,QAAQ,EAAe,CACxC,CAGH,IAAM,EAAe,EAAqB,CAAE,MAAO,EAAS,YAAa,CAAC,CACpE,EAAe,EAAqB,CAAE,MAAO,EAAS,YAAa,CAAC,CACtE,EAA4B,gBAAgB,EAAS,CA8CzD,MAAO,CACL,SACA,QA9CsB,SAA+B,CAGrD,KAAO,CAAC,EAAG,OAAO,SAAS,CACzB,IAAI,EAAyB,GAE7B,OAAQ,EAAgB,OAAxB,CACE,IAAK,iBACH,EAAkB,MAAM,EAAe,CACrC,kBACA,OAAQ,EAAG,OACX,eACD,CAAC,CACF,EAAe,EAAgB,CAC/B,MAEF,IAAK,iBACH,EAAkB,MAAM,EAAe,CACrC,QAAS,EAAgB,Y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,gBAAgB,SAChC,CACD,MAAO,CACL,OAAQ,CACN,CACE,QAAS,GACT,KAAM,MACN,KAAM,UACP,CACD,CACE,QAAS,GACT,KAAM,MACN,KAAM,UACP,CACD,CACE,QAAS,GACT,KAAM,MACN,KAAM,UACP,CACF,CACD,KAAM,WACN,KAAM,QACP,CACD,YACA,UACD,CAAC,EAEoB,GAAG,GAAG,CAM5B,OAJK,EAIE,EAAS,gBAHP,KAMX,SAAS,EAAmB,CAC1B,oBACA,0BAIiB,CACjB,IAAM,EACJ,EAAyB,EAAsB,EAAyB,EAAsB,WAC1F,EAAqB,EAAyB,EAIpD,MAAO,CACL,YACA,QAJA,IAAsB,IAAA,IAAa,EAAqB,EAAoB,EAAqB,SAKlG,CAGH,eAAe,EAAmC,CAAE,UAAkD,CACpG,MAAM,EAAa,IAAI,QAAe,GAAY,WAAW,EAAS,KAA6B,CAAC,CAAE,EAAO"}
@@ -1,2 +1,2 @@
1
- const e=require(`../../../../constants.cjs`),t=require(`../../_utils/utxo.cjs`);function n({bitcoinFunctions:n,config:r}){return async({amountIn:i,fromAddress:a,sourceChain:o},s)=>{let c=e.FEE_RATE_TIER_TO_BITCOIN[s?.overrides?.feeRateTier??`fast`],l=r.sourceChain===e.BitcoinChainIds.MAINNET?`bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq`:`tb1qw508d6qejxtdg4y5r3zarvary0c5xw7kxpjzsx`,u=(await n.getFeeRates())[c],{utxos:d}=await n.getUtxoBalance(a,!1),{fee:f}=await t.selectUtxos(l,a,i,u,d,n);return{asset:o.networkToken,totalFee:BigInt(f)}}}exports.estimateNativeFeeFactory=n;
1
+ const e=require(`../../../../constants.cjs`),t=require(`../../_utils/utxo.cjs`);function n({bitcoinFunctions:n,config:r}){return async({amountIn:i,fromAddress:a,sourceChain:o},s)=>{let c=e.FEE_RATE_TIER_TO_BITCOIN[s?.overrides?.feeRateTier??`fast`],l=r.sourceChain===e.BitcoinChainIds.MAINNET?`bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq`:`tb1qw508d6qejxtdg4y5r3zarvary0c5xw7kxpjzsx`,u=(await n.getFeeRates())[c],{utxos:d}=await n.getUtxoBalance(a,!1),{fee:f}=await t.selectUtxos(l,a,i,u,d,n);return{asset:o.networkToken,totalFee:BigInt(f),totalUpfrontFee:BigInt(f)}}}exports.estimateNativeFeeFactory=n;
2
2
  //# sourceMappingURL=estimate-native-fee.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"estimate-native-fee.cjs","names":["FEE_RATE_TIER_TO_BITCOIN","BitcoinChainIds","selectUtxos"],"sources":["../../../../../src/transfer-service/lombard/btc-to-btcb/_handlers/estimate-native-fee.ts"],"sourcesContent":["import type { BtcToBtcbConfig } from '../../types';\nimport { BitcoinChainIds, FEE_RATE_TIER_TO_BITCOIN } from '../../../../constants';\nimport type { BitcoinFunctions } from '../../../../types/bitcoin';\nimport type { TransferService } from '../../../../types/service';\nimport { selectUtxos } from '../../_utils/utxo';\n\n// Placeholder bech32 addresses for fee estimation (address type affects byte length, not the actual value)\nexport const PLACEHOLDER_MAINNET_ADDRESS = 'bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq';\nexport const PLACEHOLDER_TESTNET_ADDRESS = 'tb1qw508d6qejxtdg4y5r3zarvary0c5xw7kxpjzsx';\n\nexport interface EstimateNativeFeeFactoryOptions {\n bitcoinFunctions: BitcoinFunctions;\n config: BtcToBtcbConfig;\n}\n\nexport function estimateNativeFeeFactory({\n bitcoinFunctions,\n config,\n}: EstimateNativeFeeFactoryOptions): TransferService['estimateNativeFee'] {\n return async ({ amountIn, fromAddress, sourceChain }, options) => {\n const bitcoinFeeRateTier = FEE_RATE_TIER_TO_BITCOIN[options?.overrides?.feeRateTier ?? 'fast'];\n\n // Use a placeholder bech32 address for estimation since BTC fees depend on\n // transaction size (byte length), not the actual destination address.\n // The address type (bech32) affects the output script size, but all bech32\n // addresses of the same type have the same size.\n const isMainnet = config.sourceChain === BitcoinChainIds.MAINNET;\n const placeholderAddress = isMainnet ? PLACEHOLDER_MAINNET_ADDRESS : PLACEHOLDER_TESTNET_ADDRESS;\n\n const feeRates = await bitcoinFunctions.getFeeRates();\n const feeRate = feeRates[bitcoinFeeRateTier];\n\n const { utxos } = await bitcoinFunctions.getUtxoBalance(fromAddress, false);\n\n const { fee } = await selectUtxos(placeholderAddress, fromAddress, amountIn, feeRate, utxos, bitcoinFunctions);\n\n return {\n asset: sourceChain.networkToken,\n totalFee: BigInt(fee),\n };\n };\n}\n"],"mappings":"gFAeA,SAAgB,EAAyB,CACvC,mBACA,UACwE,CACxE,OAAO,MAAO,CAAE,WAAU,cAAa,eAAe,IAAY,CAChE,IAAM,EAAqBA,EAAAA,yBAAyB,GAAS,WAAW,aAAe,QAOjF,EADY,EAAO,cAAgBC,EAAAA,gBAAgB,QAClB,6CAA8B,6CAG/D,GADW,MAAM,EAAiB,aAAa,EAC5B,GAEnB,CAAE,SAAU,MAAM,EAAiB,eAAe,EAAa,GAAM,CAErE,CAAE,OAAQ,MAAMC,EAAAA,YAAY,EAAoB,EAAa,EAAU,EAAS,EAAO,EAAiB,CAE9G,MAAO,CACL,MAAO,EAAY,aACnB,SAAU,OAAO,EAAI,CACtB"}
1
+ {"version":3,"file":"estimate-native-fee.cjs","names":["FEE_RATE_TIER_TO_BITCOIN","BitcoinChainIds","selectUtxos"],"sources":["../../../../../src/transfer-service/lombard/btc-to-btcb/_handlers/estimate-native-fee.ts"],"sourcesContent":["import type { BtcToBtcbConfig } from '../../types';\nimport { BitcoinChainIds, FEE_RATE_TIER_TO_BITCOIN } from '../../../../constants';\nimport type { BitcoinFunctions } from '../../../../types/bitcoin';\nimport type { TransferService } from '../../../../types/service';\nimport { selectUtxos } from '../../_utils/utxo';\n\n// Placeholder bech32 addresses for fee estimation (address type affects byte length, not the actual value)\nexport const PLACEHOLDER_MAINNET_ADDRESS = 'bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq';\nexport const PLACEHOLDER_TESTNET_ADDRESS = 'tb1qw508d6qejxtdg4y5r3zarvary0c5xw7kxpjzsx';\n\nexport interface EstimateNativeFeeFactoryOptions {\n bitcoinFunctions: BitcoinFunctions;\n config: BtcToBtcbConfig;\n}\n\nexport function estimateNativeFeeFactory({\n bitcoinFunctions,\n config,\n}: EstimateNativeFeeFactoryOptions): TransferService['estimateNativeFee'] {\n return async ({ amountIn, fromAddress, sourceChain }, options) => {\n const bitcoinFeeRateTier = FEE_RATE_TIER_TO_BITCOIN[options?.overrides?.feeRateTier ?? 'fast'];\n\n // Use a placeholder bech32 address for estimation since BTC fees depend on\n // transaction size (byte length), not the actual destination address.\n // The address type (bech32) affects the output script size, but all bech32\n // addresses of the same type have the same size.\n const isMainnet = config.sourceChain === BitcoinChainIds.MAINNET;\n const placeholderAddress = isMainnet ? PLACEHOLDER_MAINNET_ADDRESS : PLACEHOLDER_TESTNET_ADDRESS;\n\n const feeRates = await bitcoinFunctions.getFeeRates();\n const feeRate = feeRates[bitcoinFeeRateTier];\n\n const { utxos } = await bitcoinFunctions.getUtxoBalance(fromAddress, false);\n\n const { fee } = await selectUtxos(placeholderAddress, fromAddress, amountIn, feeRate, utxos, bitcoinFunctions);\n\n return {\n asset: sourceChain.networkToken,\n totalFee: BigInt(fee),\n totalUpfrontFee: BigInt(fee),\n };\n };\n}\n"],"mappings":"gFAeA,SAAgB,EAAyB,CACvC,mBACA,UACwE,CACxE,OAAO,MAAO,CAAE,WAAU,cAAa,eAAe,IAAY,CAChE,IAAM,EAAqBA,EAAAA,yBAAyB,GAAS,WAAW,aAAe,QAOjF,EADY,EAAO,cAAgBC,EAAAA,gBAAgB,QAClB,6CAA8B,6CAG/D,GADW,MAAM,EAAiB,aAAa,EAC5B,GAEnB,CAAE,SAAU,MAAM,EAAiB,eAAe,EAAa,GAAM,CAErE,CAAE,OAAQ,MAAMC,EAAAA,YAAY,EAAoB,EAAa,EAAU,EAAS,EAAO,EAAiB,CAE9G,MAAO,CACL,MAAO,EAAY,aACnB,SAAU,OAAO,EAAI,CACrB,gBAAiB,OAAO,EAAI,CAC7B"}
@@ -1,2 +1,2 @@
1
- import{BitcoinChainIds as e,FEE_RATE_TIER_TO_BITCOIN as t}from"../../../../constants.js";import{selectUtxos as n}from"../../_utils/utxo.js";function r({bitcoinFunctions:r,config:i}){return async({amountIn:a,fromAddress:o,sourceChain:s},c)=>{let l=t[c?.overrides?.feeRateTier??`fast`],u=i.sourceChain===e.MAINNET?`bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq`:`tb1qw508d6qejxtdg4y5r3zarvary0c5xw7kxpjzsx`,d=(await r.getFeeRates())[l],{utxos:f}=await r.getUtxoBalance(o,!1),{fee:p}=await n(u,o,a,d,f,r);return{asset:s.networkToken,totalFee:BigInt(p)}}}export{r as estimateNativeFeeFactory};
1
+ import{BitcoinChainIds as e,FEE_RATE_TIER_TO_BITCOIN as t}from"../../../../constants.js";import{selectUtxos as n}from"../../_utils/utxo.js";function r({bitcoinFunctions:r,config:i}){return async({amountIn:a,fromAddress:o,sourceChain:s},c)=>{let l=t[c?.overrides?.feeRateTier??`fast`],u=i.sourceChain===e.MAINNET?`bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq`:`tb1qw508d6qejxtdg4y5r3zarvary0c5xw7kxpjzsx`,d=(await r.getFeeRates())[l],{utxos:f}=await r.getUtxoBalance(o,!1),{fee:p}=await n(u,o,a,d,f,r);return{asset:s.networkToken,totalFee:BigInt(p),totalUpfrontFee:BigInt(p)}}}export{r as estimateNativeFeeFactory};
2
2
  //# sourceMappingURL=estimate-native-fee.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"estimate-native-fee.js","names":[],"sources":["../../../../../src/transfer-service/lombard/btc-to-btcb/_handlers/estimate-native-fee.ts"],"sourcesContent":["import type { BtcToBtcbConfig } from '../../types';\nimport { BitcoinChainIds, FEE_RATE_TIER_TO_BITCOIN } from '../../../../constants';\nimport type { BitcoinFunctions } from '../../../../types/bitcoin';\nimport type { TransferService } from '../../../../types/service';\nimport { selectUtxos } from '../../_utils/utxo';\n\n// Placeholder bech32 addresses for fee estimation (address type affects byte length, not the actual value)\nexport const PLACEHOLDER_MAINNET_ADDRESS = 'bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq';\nexport const PLACEHOLDER_TESTNET_ADDRESS = 'tb1qw508d6qejxtdg4y5r3zarvary0c5xw7kxpjzsx';\n\nexport interface EstimateNativeFeeFactoryOptions {\n bitcoinFunctions: BitcoinFunctions;\n config: BtcToBtcbConfig;\n}\n\nexport function estimateNativeFeeFactory({\n bitcoinFunctions,\n config,\n}: EstimateNativeFeeFactoryOptions): TransferService['estimateNativeFee'] {\n return async ({ amountIn, fromAddress, sourceChain }, options) => {\n const bitcoinFeeRateTier = FEE_RATE_TIER_TO_BITCOIN[options?.overrides?.feeRateTier ?? 'fast'];\n\n // Use a placeholder bech32 address for estimation since BTC fees depend on\n // transaction size (byte length), not the actual destination address.\n // The address type (bech32) affects the output script size, but all bech32\n // addresses of the same type have the same size.\n const isMainnet = config.sourceChain === BitcoinChainIds.MAINNET;\n const placeholderAddress = isMainnet ? PLACEHOLDER_MAINNET_ADDRESS : PLACEHOLDER_TESTNET_ADDRESS;\n\n const feeRates = await bitcoinFunctions.getFeeRates();\n const feeRate = feeRates[bitcoinFeeRateTier];\n\n const { utxos } = await bitcoinFunctions.getUtxoBalance(fromAddress, false);\n\n const { fee } = await selectUtxos(placeholderAddress, fromAddress, amountIn, feeRate, utxos, bitcoinFunctions);\n\n return {\n asset: sourceChain.networkToken,\n totalFee: BigInt(fee),\n };\n };\n}\n"],"mappings":"4IAeA,SAAgB,EAAyB,CACvC,mBACA,UACwE,CACxE,OAAO,MAAO,CAAE,WAAU,cAAa,eAAe,IAAY,CAChE,IAAM,EAAqB,EAAyB,GAAS,WAAW,aAAe,QAOjF,EADY,EAAO,cAAgB,EAAgB,QAClB,6CAA8B,6CAG/D,GADW,MAAM,EAAiB,aAAa,EAC5B,GAEnB,CAAE,SAAU,MAAM,EAAiB,eAAe,EAAa,GAAM,CAErE,CAAE,OAAQ,MAAM,EAAY,EAAoB,EAAa,EAAU,EAAS,EAAO,EAAiB,CAE9G,MAAO,CACL,MAAO,EAAY,aACnB,SAAU,OAAO,EAAI,CACtB"}
1
+ {"version":3,"file":"estimate-native-fee.js","names":[],"sources":["../../../../../src/transfer-service/lombard/btc-to-btcb/_handlers/estimate-native-fee.ts"],"sourcesContent":["import type { BtcToBtcbConfig } from '../../types';\nimport { BitcoinChainIds, FEE_RATE_TIER_TO_BITCOIN } from '../../../../constants';\nimport type { BitcoinFunctions } from '../../../../types/bitcoin';\nimport type { TransferService } from '../../../../types/service';\nimport { selectUtxos } from '../../_utils/utxo';\n\n// Placeholder bech32 addresses for fee estimation (address type affects byte length, not the actual value)\nexport const PLACEHOLDER_MAINNET_ADDRESS = 'bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq';\nexport const PLACEHOLDER_TESTNET_ADDRESS = 'tb1qw508d6qejxtdg4y5r3zarvary0c5xw7kxpjzsx';\n\nexport interface EstimateNativeFeeFactoryOptions {\n bitcoinFunctions: BitcoinFunctions;\n config: BtcToBtcbConfig;\n}\n\nexport function estimateNativeFeeFactory({\n bitcoinFunctions,\n config,\n}: EstimateNativeFeeFactoryOptions): TransferService['estimateNativeFee'] {\n return async ({ amountIn, fromAddress, sourceChain }, options) => {\n const bitcoinFeeRateTier = FEE_RATE_TIER_TO_BITCOIN[options?.overrides?.feeRateTier ?? 'fast'];\n\n // Use a placeholder bech32 address for estimation since BTC fees depend on\n // transaction size (byte length), not the actual destination address.\n // The address type (bech32) affects the output script size, but all bech32\n // addresses of the same type have the same size.\n const isMainnet = config.sourceChain === BitcoinChainIds.MAINNET;\n const placeholderAddress = isMainnet ? PLACEHOLDER_MAINNET_ADDRESS : PLACEHOLDER_TESTNET_ADDRESS;\n\n const feeRates = await bitcoinFunctions.getFeeRates();\n const feeRate = feeRates[bitcoinFeeRateTier];\n\n const { utxos } = await bitcoinFunctions.getUtxoBalance(fromAddress, false);\n\n const { fee } = await selectUtxos(placeholderAddress, fromAddress, amountIn, feeRate, utxos, bitcoinFunctions);\n\n return {\n asset: sourceChain.networkToken,\n totalFee: BigInt(fee),\n totalUpfrontFee: BigInt(fee),\n };\n };\n}\n"],"mappings":"4IAeA,SAAgB,EAAyB,CACvC,mBACA,UACwE,CACxE,OAAO,MAAO,CAAE,WAAU,cAAa,eAAe,IAAY,CAChE,IAAM,EAAqB,EAAyB,GAAS,WAAW,aAAe,QAOjF,EADY,EAAO,cAAgB,EAAgB,QAClB,6CAA8B,6CAG/D,GADW,MAAM,EAAiB,aAAa,EAC5B,GAEnB,CAAE,SAAU,MAAM,EAAiB,eAAe,EAAa,GAAM,CAErE,CAAE,OAAQ,MAAM,EAAY,EAAoB,EAAa,EAAU,EAAS,EAAO,EAAiB,CAE9G,MAAO,CACL,MAAO,EAAY,aACnB,SAAU,OAAO,EAAI,CACrB,gBAAiB,OAAO,EAAI,CAC7B"}
@@ -1,2 +1,2 @@
1
- require(`../../../../_virtual/_rolldown/runtime.cjs`);const e=require(`../../../../errors.cjs`),t=require(`../../../_utils.cjs`),n=require(`../../../_evm-gas.cjs`);let r=require(`viem`);const i=60000n,a=300000n,o=({config:o})=>async({amountIn:s,fromAddress:c,sourceChain:l},u)=>{if(!(0,r.isAddress)(c))throw new e.InvalidParamsError(e.ErrorReason.INVALID_PARAMS,`Invalid fromAddress: ${c}`);let d=t.getEvmClientForChain({chain:l}),f=await d.readContract({address:o.sourceAsset.address,abi:r.erc20Abi,functionName:`allowance`,args:[c,o.adapterAddress]})<s,p=t.applyFeeUnitsBpsMargin(f?i+a:a,u?.feeUnitsMarginBps),m=await n.estimateEvmFeesPerGas(d,l,u?.overrides?.feeRateTier),h=u?.overrides?.maxFeePerGas??m.maxFeePerGas,g=u?.overrides?.maxPriorityFeePerGas??m.maxPriorityFeePerGas,_=p*h;return{asset:l.networkToken,totalFee:_,meta:{approvalFee:f?i*h:void 0,maxFeePerGas:h,maxPriorityFeePerGas:g}}};exports.estimateNativeFeeFactory=o;
1
+ require(`../../../../_virtual/_rolldown/runtime.cjs`);const e=require(`../../../../errors.cjs`),t=require(`../../../_utils.cjs`),n=require(`../../../_evm-gas.cjs`);let r=require(`viem`);const i=60000n,a=300000n,o=({config:o})=>async({amountIn:s,fromAddress:c,sourceChain:l},u)=>{if(!(0,r.isAddress)(c))throw new e.InvalidParamsError(e.ErrorReason.INVALID_PARAMS,`Invalid fromAddress: ${c}`);let d=t.getEvmClientForChain({chain:l}),f=await d.readContract({address:o.sourceAsset.address,abi:r.erc20Abi,functionName:`allowance`,args:[c,o.adapterAddress]})<s,p=t.applyFeeUnitsBpsMargin(f?i+a:a,u?.feeUnitsMarginBps),m=await n.estimateEvmFeesPerGas(d,l,u?.overrides?.feeRateTier),h=u?.overrides?.maxFeePerGas??m.maxFeePerGas,g=u?.overrides?.maxPriorityFeePerGas??m.maxPriorityFeePerGas,_=p*h;return{asset:l.networkToken,totalFee:_,totalUpfrontFee:_,meta:{approvalFee:f?i*h:void 0,maxFeePerGas:h,maxPriorityFeePerGas:g}}};exports.estimateNativeFeeFactory=o;
2
2
  //# sourceMappingURL=estimate-native-fee.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"estimate-native-fee.cjs","names":["InvalidParamsError","ErrorReason","getEvmClientForChain","erc20Abi","applyFeeUnitsBpsMargin","estimateEvmFeesPerGas"],"sources":["../../../../../src/transfer-service/lombard/btcb-to-btc/_handlers/estimate-native-fee.ts"],"sourcesContent":["import { erc20Abi, isAddress } from 'viem';\nimport type { BtcbToBtcConfig } from '../../types';\nimport { ErrorReason, InvalidParamsError } from '../../../../errors';\nimport type { TransferService } from '../../../../types/service';\nimport { applyFeeUnitsBpsMargin, getEvmClientForChain } from '../../../_utils';\nimport { estimateEvmFeesPerGas } from '../../../_evm-gas';\n\n// Hardcoded gas estimates for BTCb redemption on Avalanche.\n// These are approximations used for UI display only - DO NOT use as gasLimit.\n// Based on typical ERC20 approval and AssetRouter.redeemForBtc call costs.\nexport const AVAX_APPROVAL_GAS_ESTIMATE = 60_000n; // Standard ERC20 approval (~55k + buffer)\nexport const AVAX_REDEEM_GAS_ESTIMATE = 300_000n; // AssetRouter.redeemForBtc (~250k + buffer)\n\nexport interface EstimateNativeFeeFactoryOptions {\n config: BtcbToBtcConfig;\n}\n\n/**\n * Estimates gas for BTCb-to-BTC redemption on Avalanche.\n *\n * The redemption consists of up to two transactions:\n * 1. Token spend approval to the adapter contract (if needed)\n * 2. Redeem call via the AssetRouter contract\n *\n * Since the redeem needs the approval to be complete, we can't call estimateGas()\n * on the actual contract. Instead, we use hardcoded estimates.\n */\nexport const estimateNativeFeeFactory = ({\n config,\n}: EstimateNativeFeeFactoryOptions): TransferService['estimateNativeFee'] => {\n return async ({ amountIn, fromAddress, sourceChain }, options) => {\n if (!isAddress(fromAddress)) {\n throw new InvalidParamsError(ErrorReason.INVALID_PARAMS, `Invalid fromAddress: ${fromAddress}`);\n }\n\n const client = getEvmClientForChain({ chain: sourceChain });\n\n // Check if approval is already granted to the adapter\n const allowance = await client.readContract({\n address: config.sourceAsset.address,\n abi: erc20Abi,\n functionName: 'allowance',\n args: [fromAddress, config.adapterAddress],\n });\n\n const needsApproval = allowance < amountIn;\n\n const totalGas = needsApproval ? AVAX_APPROVAL_GAS_ESTIMATE + AVAX_REDEEM_GAS_ESTIMATE : AVAX_REDEEM_GAS_ESTIMATE;\n\n const gasWithMargin = applyFeeUnitsBpsMargin(totalGas, options?.feeUnitsMarginBps);\n\n const fees = await estimateEvmFeesPerGas(client, sourceChain, options?.overrides?.feeRateTier);\n\n const maxFeePerGas = options?.overrides?.maxFeePerGas ?? fees.maxFeePerGas;\n const maxPriorityFeePerGas = options?.overrides?.maxPriorityFeePerGas ?? fees.maxPriorityFeePerGas;\n\n const totalFee = gasWithMargin * maxFeePerGas;\n\n return {\n asset: sourceChain.networkToken,\n totalFee,\n meta: {\n approvalFee: needsApproval ? AVAX_APPROVAL_GAS_ESTIMATE * maxFeePerGas : undefined,\n maxFeePerGas,\n maxPriorityFeePerGas,\n },\n };\n };\n};\n"],"mappings":"0LAUA,MAAa,EAA6B,OAC7B,EAA2B,QAgB3B,GAA4B,CACvC,YAEO,MAAO,CAAE,WAAU,cAAa,eAAe,IAAY,CAChE,GAAI,EAAA,EAAA,EAAA,WAAW,EAAY,CACzB,MAAM,IAAIA,EAAAA,mBAAmBC,EAAAA,YAAY,eAAgB,wBAAwB,IAAc,CAGjG,IAAM,EAASC,EAAAA,qBAAqB,CAAE,MAAO,EAAa,CAAC,CAUrD,EAPY,MAAM,EAAO,aAAa,CAC1C,QAAS,EAAO,YAAY,QAC5B,IAAKC,EAAAA,SACL,aAAc,YACd,KAAM,CAAC,EAAa,EAAO,eAAe,CAC3C,CAAC,CAEgC,EAI5B,EAAgBC,EAAAA,uBAFL,EAAgB,EAA6B,EAA2B,EAElC,GAAS,kBAAkB,CAE5E,EAAO,MAAMC,EAAAA,sBAAsB,EAAQ,EAAa,GAAS,WAAW,YAAY,CAExF,EAAe,GAAS,WAAW,cAAgB,EAAK,aACxD,EAAuB,GAAS,WAAW,sBAAwB,EAAK,qBAExE,EAAW,EAAgB,EAEjC,MAAO,CACL,MAAO,EAAY,aACnB,WACA,KAAM,CACJ,YAAa,EAAgB,EAA6B,EAAe,IAAA,GACzE,eACA,uBACD,CACF"}
1
+ {"version":3,"file":"estimate-native-fee.cjs","names":["InvalidParamsError","ErrorReason","getEvmClientForChain","erc20Abi","applyFeeUnitsBpsMargin","estimateEvmFeesPerGas"],"sources":["../../../../../src/transfer-service/lombard/btcb-to-btc/_handlers/estimate-native-fee.ts"],"sourcesContent":["import { erc20Abi, isAddress } from 'viem';\nimport type { BtcbToBtcConfig } from '../../types';\nimport { ErrorReason, InvalidParamsError } from '../../../../errors';\nimport type { TransferService } from '../../../../types/service';\nimport { applyFeeUnitsBpsMargin, getEvmClientForChain } from '../../../_utils';\nimport { estimateEvmFeesPerGas } from '../../../_evm-gas';\n\n// Hardcoded gas estimates for BTCb redemption on Avalanche.\n// These are approximations used for UI display only - DO NOT use as gasLimit.\n// Based on typical ERC20 approval and AssetRouter.redeemForBtc call costs.\nexport const AVAX_APPROVAL_GAS_ESTIMATE = 60_000n; // Standard ERC20 approval (~55k + buffer)\nexport const AVAX_REDEEM_GAS_ESTIMATE = 300_000n; // AssetRouter.redeemForBtc (~250k + buffer)\n\nexport interface EstimateNativeFeeFactoryOptions {\n config: BtcbToBtcConfig;\n}\n\n/**\n * Estimates gas for BTCb-to-BTC redemption on Avalanche.\n *\n * The redemption consists of up to two transactions:\n * 1. Token spend approval to the adapter contract (if needed)\n * 2. Redeem call via the AssetRouter contract\n *\n * Since the redeem needs the approval to be complete, we can't call estimateGas()\n * on the actual contract. Instead, we use hardcoded estimates.\n */\nexport const estimateNativeFeeFactory = ({\n config,\n}: EstimateNativeFeeFactoryOptions): TransferService['estimateNativeFee'] => {\n return async ({ amountIn, fromAddress, sourceChain }, options) => {\n if (!isAddress(fromAddress)) {\n throw new InvalidParamsError(ErrorReason.INVALID_PARAMS, `Invalid fromAddress: ${fromAddress}`);\n }\n\n const client = getEvmClientForChain({ chain: sourceChain });\n\n // Check if approval is already granted to the adapter\n const allowance = await client.readContract({\n address: config.sourceAsset.address,\n abi: erc20Abi,\n functionName: 'allowance',\n args: [fromAddress, config.adapterAddress],\n });\n\n const needsApproval = allowance < amountIn;\n\n const totalGas = needsApproval ? AVAX_APPROVAL_GAS_ESTIMATE + AVAX_REDEEM_GAS_ESTIMATE : AVAX_REDEEM_GAS_ESTIMATE;\n\n const gasWithMargin = applyFeeUnitsBpsMargin(totalGas, options?.feeUnitsMarginBps);\n\n const fees = await estimateEvmFeesPerGas(client, sourceChain, options?.overrides?.feeRateTier);\n\n const maxFeePerGas = options?.overrides?.maxFeePerGas ?? fees.maxFeePerGas;\n const maxPriorityFeePerGas = options?.overrides?.maxPriorityFeePerGas ?? fees.maxPriorityFeePerGas;\n\n const totalFee = gasWithMargin * maxFeePerGas;\n\n return {\n asset: sourceChain.networkToken,\n totalFee,\n totalUpfrontFee: totalFee,\n meta: {\n approvalFee: needsApproval ? AVAX_APPROVAL_GAS_ESTIMATE * maxFeePerGas : undefined,\n maxFeePerGas,\n maxPriorityFeePerGas,\n },\n };\n };\n};\n"],"mappings":"0LAUA,MAAa,EAA6B,OAC7B,EAA2B,QAgB3B,GAA4B,CACvC,YAEO,MAAO,CAAE,WAAU,cAAa,eAAe,IAAY,CAChE,GAAI,EAAA,EAAA,EAAA,WAAW,EAAY,CACzB,MAAM,IAAIA,EAAAA,mBAAmBC,EAAAA,YAAY,eAAgB,wBAAwB,IAAc,CAGjG,IAAM,EAASC,EAAAA,qBAAqB,CAAE,MAAO,EAAa,CAAC,CAUrD,EAPY,MAAM,EAAO,aAAa,CAC1C,QAAS,EAAO,YAAY,QAC5B,IAAKC,EAAAA,SACL,aAAc,YACd,KAAM,CAAC,EAAa,EAAO,eAAe,CAC3C,CAAC,CAEgC,EAI5B,EAAgBC,EAAAA,uBAFL,EAAgB,EAA6B,EAA2B,EAElC,GAAS,kBAAkB,CAE5E,EAAO,MAAMC,EAAAA,sBAAsB,EAAQ,EAAa,GAAS,WAAW,YAAY,CAExF,EAAe,GAAS,WAAW,cAAgB,EAAK,aACxD,EAAuB,GAAS,WAAW,sBAAwB,EAAK,qBAExE,EAAW,EAAgB,EAEjC,MAAO,CACL,MAAO,EAAY,aACnB,WACA,gBAAiB,EACjB,KAAM,CACJ,YAAa,EAAgB,EAA6B,EAAe,IAAA,GACzE,eACA,uBACD,CACF"}
@@ -1,2 +1,2 @@
1
- import{ErrorReason as e,InvalidParamsError as t}from"../../../../errors.js";import{applyFeeUnitsBpsMargin as n,getEvmClientForChain as r}from"../../../_utils.js";import{estimateEvmFeesPerGas as i}from"../../../_evm-gas.js";import{erc20Abi as a,isAddress as o}from"viem";const s=60000n,c=300000n,l=({config:l})=>async({amountIn:u,fromAddress:d,sourceChain:f},p)=>{if(!o(d))throw new t(e.INVALID_PARAMS,`Invalid fromAddress: ${d}`);let m=r({chain:f}),h=await m.readContract({address:l.sourceAsset.address,abi:a,functionName:`allowance`,args:[d,l.adapterAddress]})<u,g=n(h?s+c:c,p?.feeUnitsMarginBps),_=await i(m,f,p?.overrides?.feeRateTier),v=p?.overrides?.maxFeePerGas??_.maxFeePerGas,y=p?.overrides?.maxPriorityFeePerGas??_.maxPriorityFeePerGas,b=g*v;return{asset:f.networkToken,totalFee:b,meta:{approvalFee:h?s*v:void 0,maxFeePerGas:v,maxPriorityFeePerGas:y}}};export{l as estimateNativeFeeFactory};
1
+ import{ErrorReason as e,InvalidParamsError as t}from"../../../../errors.js";import{applyFeeUnitsBpsMargin as n,getEvmClientForChain as r}from"../../../_utils.js";import{estimateEvmFeesPerGas as i}from"../../../_evm-gas.js";import{erc20Abi as a,isAddress as o}from"viem";const s=60000n,c=300000n,l=({config:l})=>async({amountIn:u,fromAddress:d,sourceChain:f},p)=>{if(!o(d))throw new t(e.INVALID_PARAMS,`Invalid fromAddress: ${d}`);let m=r({chain:f}),h=await m.readContract({address:l.sourceAsset.address,abi:a,functionName:`allowance`,args:[d,l.adapterAddress]})<u,g=n(h?s+c:c,p?.feeUnitsMarginBps),_=await i(m,f,p?.overrides?.feeRateTier),v=p?.overrides?.maxFeePerGas??_.maxFeePerGas,y=p?.overrides?.maxPriorityFeePerGas??_.maxPriorityFeePerGas,b=g*v;return{asset:f.networkToken,totalFee:b,totalUpfrontFee:b,meta:{approvalFee:h?s*v:void 0,maxFeePerGas:v,maxPriorityFeePerGas:y}}};export{l as estimateNativeFeeFactory};
2
2
  //# sourceMappingURL=estimate-native-fee.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"estimate-native-fee.js","names":[],"sources":["../../../../../src/transfer-service/lombard/btcb-to-btc/_handlers/estimate-native-fee.ts"],"sourcesContent":["import { erc20Abi, isAddress } from 'viem';\nimport type { BtcbToBtcConfig } from '../../types';\nimport { ErrorReason, InvalidParamsError } from '../../../../errors';\nimport type { TransferService } from '../../../../types/service';\nimport { applyFeeUnitsBpsMargin, getEvmClientForChain } from '../../../_utils';\nimport { estimateEvmFeesPerGas } from '../../../_evm-gas';\n\n// Hardcoded gas estimates for BTCb redemption on Avalanche.\n// These are approximations used for UI display only - DO NOT use as gasLimit.\n// Based on typical ERC20 approval and AssetRouter.redeemForBtc call costs.\nexport const AVAX_APPROVAL_GAS_ESTIMATE = 60_000n; // Standard ERC20 approval (~55k + buffer)\nexport const AVAX_REDEEM_GAS_ESTIMATE = 300_000n; // AssetRouter.redeemForBtc (~250k + buffer)\n\nexport interface EstimateNativeFeeFactoryOptions {\n config: BtcbToBtcConfig;\n}\n\n/**\n * Estimates gas for BTCb-to-BTC redemption on Avalanche.\n *\n * The redemption consists of up to two transactions:\n * 1. Token spend approval to the adapter contract (if needed)\n * 2. Redeem call via the AssetRouter contract\n *\n * Since the redeem needs the approval to be complete, we can't call estimateGas()\n * on the actual contract. Instead, we use hardcoded estimates.\n */\nexport const estimateNativeFeeFactory = ({\n config,\n}: EstimateNativeFeeFactoryOptions): TransferService['estimateNativeFee'] => {\n return async ({ amountIn, fromAddress, sourceChain }, options) => {\n if (!isAddress(fromAddress)) {\n throw new InvalidParamsError(ErrorReason.INVALID_PARAMS, `Invalid fromAddress: ${fromAddress}`);\n }\n\n const client = getEvmClientForChain({ chain: sourceChain });\n\n // Check if approval is already granted to the adapter\n const allowance = await client.readContract({\n address: config.sourceAsset.address,\n abi: erc20Abi,\n functionName: 'allowance',\n args: [fromAddress, config.adapterAddress],\n });\n\n const needsApproval = allowance < amountIn;\n\n const totalGas = needsApproval ? AVAX_APPROVAL_GAS_ESTIMATE + AVAX_REDEEM_GAS_ESTIMATE : AVAX_REDEEM_GAS_ESTIMATE;\n\n const gasWithMargin = applyFeeUnitsBpsMargin(totalGas, options?.feeUnitsMarginBps);\n\n const fees = await estimateEvmFeesPerGas(client, sourceChain, options?.overrides?.feeRateTier);\n\n const maxFeePerGas = options?.overrides?.maxFeePerGas ?? fees.maxFeePerGas;\n const maxPriorityFeePerGas = options?.overrides?.maxPriorityFeePerGas ?? fees.maxPriorityFeePerGas;\n\n const totalFee = gasWithMargin * maxFeePerGas;\n\n return {\n asset: sourceChain.networkToken,\n totalFee,\n meta: {\n approvalFee: needsApproval ? AVAX_APPROVAL_GAS_ESTIMATE * maxFeePerGas : undefined,\n maxFeePerGas,\n maxPriorityFeePerGas,\n },\n };\n };\n};\n"],"mappings":"8QAUA,MAAa,EAA6B,OAC7B,EAA2B,QAgB3B,GAA4B,CACvC,YAEO,MAAO,CAAE,WAAU,cAAa,eAAe,IAAY,CAChE,GAAI,CAAC,EAAU,EAAY,CACzB,MAAM,IAAI,EAAmB,EAAY,eAAgB,wBAAwB,IAAc,CAGjG,IAAM,EAAS,EAAqB,CAAE,MAAO,EAAa,CAAC,CAUrD,EAPY,MAAM,EAAO,aAAa,CAC1C,QAAS,EAAO,YAAY,QAC5B,IAAK,EACL,aAAc,YACd,KAAM,CAAC,EAAa,EAAO,eAAe,CAC3C,CAAC,CAEgC,EAI5B,EAAgB,EAFL,EAAgB,EAA6B,EAA2B,EAElC,GAAS,kBAAkB,CAE5E,EAAO,MAAM,EAAsB,EAAQ,EAAa,GAAS,WAAW,YAAY,CAExF,EAAe,GAAS,WAAW,cAAgB,EAAK,aACxD,EAAuB,GAAS,WAAW,sBAAwB,EAAK,qBAExE,EAAW,EAAgB,EAEjC,MAAO,CACL,MAAO,EAAY,aACnB,WACA,KAAM,CACJ,YAAa,EAAgB,EAA6B,EAAe,IAAA,GACzE,eACA,uBACD,CACF"}
1
+ {"version":3,"file":"estimate-native-fee.js","names":[],"sources":["../../../../../src/transfer-service/lombard/btcb-to-btc/_handlers/estimate-native-fee.ts"],"sourcesContent":["import { erc20Abi, isAddress } from 'viem';\nimport type { BtcbToBtcConfig } from '../../types';\nimport { ErrorReason, InvalidParamsError } from '../../../../errors';\nimport type { TransferService } from '../../../../types/service';\nimport { applyFeeUnitsBpsMargin, getEvmClientForChain } from '../../../_utils';\nimport { estimateEvmFeesPerGas } from '../../../_evm-gas';\n\n// Hardcoded gas estimates for BTCb redemption on Avalanche.\n// These are approximations used for UI display only - DO NOT use as gasLimit.\n// Based on typical ERC20 approval and AssetRouter.redeemForBtc call costs.\nexport const AVAX_APPROVAL_GAS_ESTIMATE = 60_000n; // Standard ERC20 approval (~55k + buffer)\nexport const AVAX_REDEEM_GAS_ESTIMATE = 300_000n; // AssetRouter.redeemForBtc (~250k + buffer)\n\nexport interface EstimateNativeFeeFactoryOptions {\n config: BtcbToBtcConfig;\n}\n\n/**\n * Estimates gas for BTCb-to-BTC redemption on Avalanche.\n *\n * The redemption consists of up to two transactions:\n * 1. Token spend approval to the adapter contract (if needed)\n * 2. Redeem call via the AssetRouter contract\n *\n * Since the redeem needs the approval to be complete, we can't call estimateGas()\n * on the actual contract. Instead, we use hardcoded estimates.\n */\nexport const estimateNativeFeeFactory = ({\n config,\n}: EstimateNativeFeeFactoryOptions): TransferService['estimateNativeFee'] => {\n return async ({ amountIn, fromAddress, sourceChain }, options) => {\n if (!isAddress(fromAddress)) {\n throw new InvalidParamsError(ErrorReason.INVALID_PARAMS, `Invalid fromAddress: ${fromAddress}`);\n }\n\n const client = getEvmClientForChain({ chain: sourceChain });\n\n // Check if approval is already granted to the adapter\n const allowance = await client.readContract({\n address: config.sourceAsset.address,\n abi: erc20Abi,\n functionName: 'allowance',\n args: [fromAddress, config.adapterAddress],\n });\n\n const needsApproval = allowance < amountIn;\n\n const totalGas = needsApproval ? AVAX_APPROVAL_GAS_ESTIMATE + AVAX_REDEEM_GAS_ESTIMATE : AVAX_REDEEM_GAS_ESTIMATE;\n\n const gasWithMargin = applyFeeUnitsBpsMargin(totalGas, options?.feeUnitsMarginBps);\n\n const fees = await estimateEvmFeesPerGas(client, sourceChain, options?.overrides?.feeRateTier);\n\n const maxFeePerGas = options?.overrides?.maxFeePerGas ?? fees.maxFeePerGas;\n const maxPriorityFeePerGas = options?.overrides?.maxPriorityFeePerGas ?? fees.maxPriorityFeePerGas;\n\n const totalFee = gasWithMargin * maxFeePerGas;\n\n return {\n asset: sourceChain.networkToken,\n totalFee,\n totalUpfrontFee: totalFee,\n meta: {\n approvalFee: needsApproval ? AVAX_APPROVAL_GAS_ESTIMATE * maxFeePerGas : undefined,\n maxFeePerGas,\n maxPriorityFeePerGas,\n },\n };\n };\n};\n"],"mappings":"8QAUA,MAAa,EAA6B,OAC7B,EAA2B,QAgB3B,GAA4B,CACvC,YAEO,MAAO,CAAE,WAAU,cAAa,eAAe,IAAY,CAChE,GAAI,CAAC,EAAU,EAAY,CACzB,MAAM,IAAI,EAAmB,EAAY,eAAgB,wBAAwB,IAAc,CAGjG,IAAM,EAAS,EAAqB,CAAE,MAAO,EAAa,CAAC,CAUrD,EAPY,MAAM,EAAO,aAAa,CAC1C,QAAS,EAAO,YAAY,QAC5B,IAAK,EACL,aAAc,YACd,KAAM,CAAC,EAAa,EAAO,eAAe,CAC3C,CAAC,CAEgC,EAI5B,EAAgB,EAFL,EAAgB,EAA6B,EAA2B,EAElC,GAAS,kBAAkB,CAE5E,EAAO,MAAM,EAAsB,EAAQ,EAAa,GAAS,WAAW,YAAY,CAExF,EAAe,GAAS,WAAW,cAAgB,EAAK,aACxD,EAAuB,GAAS,WAAW,sBAAwB,EAAK,qBAExE,EAAW,EAAgB,EAEjC,MAAO,CACL,MAAO,EAAY,aACnB,WACA,gBAAiB,EACjB,KAAM,CACJ,YAAa,EAAgB,EAA6B,EAAe,IAAA,GACzE,eACA,uBACD,CACF"}
@@ -1,2 +1,2 @@
1
- require(`../../../_virtual/_rolldown/runtime.cjs`);const e=require(`../../../errors.cjs`),t=require(`../../../utils/caip.cjs`),n=require(`../../../_utils/chain.cjs`),r=require(`../../_utils.cjs`),i=require(`../../_evm-gas.cjs`),a=require(`../_api.cjs`),o=require(`../_utils.cjs`),s=require(`../_type-guards.cjs`);let c=require(`viem`),l=require(`@solana/kit`);function u(t){return async(r,i)=>{if(n.isEvmNamespace(r.sourceChain.chainId))return await d(r,i,t);if(n.isSolanaNamespace(r.sourceChain.chainId))return await f(r,i,t);throw new e.InvalidParamsError(e.ErrorReason.INVALID_PARAMS,`Unsupported source chain namespace for estimateNativeFee: ${r.sourceChain.chainId}`)}}async function d(n,l,{apiOptions:u,appId:d}){if(!(0,c.isAddress)(n.fromAddress))throw new e.InvalidParamsError(e.ErrorReason.INVALID_PARAMS,`Invalid fromAddress: ${n.fromAddress}`);let f=r.getEvmClientForChain({chain:n.sourceChain}),m=o.assetToAddressString(n.assetIn,n.sourceChain.chainId);if(!(0,c.isAddress)(m))throw new e.SdkError(e.ErrorReason.INVALID_PARAMS,e.ErrorCode.INVALID_PARAMS,{details:`assetIn address is not a valid EVM address. Can not call estimateGas.`});let h=o.isTokenAddressNative(m),g=n.sourceChain.chainId.toLowerCase()!==n.targetChain.chainId.toLowerCase(),_=0n;if(!h){let r,{address:i}=await a.markrGetSpenderAddress(u,{chainId:t.caip2ToEip155ChainId(n.sourceChain.chainId),crossChainSwap:g,quoteId:n.id});try{r=await f.readContract({address:m,abi:c.erc20Abi,functionName:`allowance`,args:[n.fromAddress,i]})}catch(t){throw new e.SdkError(`Error during allowance check`,e.ErrorCode.VIEM_ERROR,{cause:t,details:`Failed to read ERC20 allowance for Markr spender.`})}if(r<n.amountIn)try{_=await f.estimateGas({account:n.fromAddress,to:m,data:(0,c.encodeFunctionData)({abi:c.erc20Abi,functionName:`approve`,args:[i,n.amountIn]}),value:0n})}catch(t){throw new e.SdkError(`Error during gas estimation`,e.ErrorCode.VIEM_ERROR,{cause:t,details:`Failed to estimate gas for ERC20 approval transaction.`})}}let v=_>0n,y=o.calculateMarkrMinimumAmountOut({amountOut:n.amountOut,assetOut:n.assetOut,slippageBps:n.slippageBps}),b=await a.markrSwap(u,{amountIn:n.amountIn.toString(),appId:d,minAmountOut:y.toString(),tokenIn:m,tokenOut:o.assetToAddressString(n.assetOut,n.targetChain.chainId),uuid:n.id});if(!s.isEvmSwapResponse(b))throw new e.SdkError(e.ErrorReason.CHAIN_NOT_SUPPORTED,e.ErrorCode.INVALID_PARAMS,{details:`Received non-EVM swap response from Markr. Expected EVM transaction data for gas estimation, but got response with chainType ${b.chainType}.`});let x=0n;x=v?n.gasEstimate?r.applyFeeUnitsBpsMargin(n.gasEstimate,l?.feeUnitsMarginBps):r.applyFeeUnitsBpsMargin(n.fees.filter(e=>e.type===`gas`&&e.chainId===n.sourceChain.chainId).reduce((e,t)=>e+t.amount,0n)||700000n,l?.feeUnitsMarginBps):await p({crossChain:g,feeUnitsMarginBps:l?.feeUnitsMarginBps,fromAddress:n.fromAddress,sourceClient:f,swap:b});let S=await i.estimateEvmFeesPerGas(f,n.sourceChain,l?.overrides?.feeRateTier),C=l?.overrides?.maxFeePerGas??S.maxFeePerGas,w=l?.overrides?.maxPriorityFeePerGas??S.maxPriorityFeePerGas,T=(x+r.applyFeeUnitsBpsMargin(_,l?.feeUnitsMarginBps))*C;return{asset:n.sourceChain.networkToken,totalFee:T,meta:{approvalFee:v?r.applyFeeUnitsBpsMargin(_,l?.feeUnitsMarginBps)*C:void 0,maxFeePerGas:C,maxPriorityFeePerGas:w}}}async function f(t,n,{apiOptions:i,appId:c}){let u=o.assetToAddressString(t.assetIn,t.sourceChain.chainId);if(!(0,l.isAddress)(u))throw new e.SdkError(e.ErrorReason.INVALID_PARAMS,e.ErrorCode.INVALID_PARAMS,{details:`assetIn address is not a valid Solana address. Can not call estimateGas.`});let d=o.calculateMarkrMinimumAmountOut({amountOut:t.amountOut,assetOut:t.assetOut,slippageBps:t.slippageBps}),f=await a.markrSwap(i,{amountIn:t.amountIn.toString(),appId:c,minAmountOut:d.toString(),tokenIn:u,tokenOut:o.assetToAddressString(t.assetOut,t.targetChain.chainId),userPublicKey:t.fromAddress,uuid:t.id});if(!s.isSolanaSwapResponse(f))throw new e.SdkError(e.ErrorReason.CHAIN_NOT_SUPPORTED,e.ErrorCode.INVALID_PARAMS,{details:`Received non-Solana swap response from Markr. Expected Solana transaction data for fee estimation.`});let p=r.getSolanaRpcForChain({chain:t.sourceChain}),v=m(f.swapTransaction),y=(0,l.getTransactionDecoder)().decode(v).messageBytes,b=f.swapTransaction,x=(await p.getFeeForMessage(h(y)).send()).value;if(!x){let e=await p.getLatestBlockhash().send(),t=await(0,l.decompileTransactionMessageFetchingLookupTables)((0,l.getCompiledTransactionMessageDecoder)().decode(y),p),n=(0,l.compileTransaction)((0,l.setTransactionMessageLifetimeUsingBlockhash)(e.value,t));y=n.messageBytes,b=(0,l.getBase64EncodedWireTransaction)(n),x=(await p.getFeeForMessage(h(y)).send()).value}if(!x)throw new e.SdkError(`Failed to get fee for message after refreshing blockhash.`,e.ErrorCode.TIMEOUT,{details:`Unable to estimate Solana base fee because the transaction message remained invalid.`});let S=r.applyFeeUnitsBpsMargin((await p.simulateTransaction(b,{encoding:`base64`,sigVerify:!1,replaceRecentBlockhash:!0}).send()).value.unitsConsumed??0n,n?.feeUnitsMarginBps),C=g(y),w=S*C/1000000n,T=await _({fromAddress:t.fromAddress,rpc:p,transactionMessageBytes:y}),E=x+w+T.rentFee;return{asset:t.sourceChain.networkToken,totalFee:E,refundable:T.refundable,meta:{baseFee:x,priorityFee:w,computeUnitPriceMicroLamports:C,unitsConsumed:S,createdAccounts:T.createdAccounts,rentFee:T.rentFee,rentFeeNet:T.rentFeeNet}}}async function p({crossChain:t,feeUnitsMarginBps:n,fromAddress:i,sourceClient:a,swap:s}){try{return r.applyFeeUnitsBpsMargin(await a.estimateGas({account:i,to:s.to,data:s.data,value:s.value}),n)}catch(n){let r=`Failed to estimate gas for Markr swap transaction.`;try{let e=o.decodeMarkrRevertError(await o.getMarkrSwapWrapperAbi(t),n);e&&(r=`${r} Markr revert: ${e}.`)}catch{}throw new e.SdkError(`Error during gas estimation`,e.ErrorCode.VIEM_ERROR,{cause:n,details:r})}}function m(e){return Uint8Array.from(atob(e),e=>e.charCodeAt(0))}function h(e){let t=``;for(let n of e)t+=String.fromCharCode(n);return btoa(t)}function g(e){let t=(0,l.getCompiledTransactionMessageDecoder)().decode(e);for(let e of t.instructions){if(t.staticAccounts[e.programAddressIndex]!==`ComputeBudget111111111111111111111111111111`)continue;let n=e.data;if(!(!n||n.length<9)&&n[0]===3)return new DataView(n.buffer,n.byteOffset,n.byteLength).getBigUint64(1,!0)}return 0n}async function _({fromAddress:e,rpc:t,transactionMessageBytes:n}){let r=(0,l.getCompiledTransactionMessageDecoder)().decode(n),i=new Map,a=new Map,o;for(let e of r.instructions){let n=r.staticAccounts[e.programAddressIndex];if(n===`11111111111111111111111111111111`){let t=v(r,e,1),n=y(e.data);t&&n>0n&&i.set(t,{address:t,kind:`system`,lamports:n});continue}if(n===`ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL`){let n=e.data?.[0];if(!(n===void 0||n===0||n===1))continue;let a=v(r,e,1);if(!a||(await t.getAccountInfo(a).send()).value)continue;o===void 0&&(o=await t.getMinimumBalanceForRentExemption(165n).send()),i.set(a,{address:a,kind:`ata`,lamports:o});continue}if(!(n===`TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA`||n===`TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb`)||e.data?.[0]!==9)continue;let s=v(r,e,0),c=v(r,e,1);s&&c&&a.set(s,c)}let s=(0,l.isAddress)(e)?e:void 0,c=Array.from(i.values()).map(e=>{let t=a.get(e.address),n=s!==void 0&&t===s;return{...e,refunded:n}}),u=c.reduce((e,t)=>e+t.lamports,0n),d=c.reduce((e,t)=>e+(t.refunded?t.lamports:0n),0n);return{createdAccounts:c,rentFee:u,rentFeeNet:u-d,refundable:d}}function v(e,t,n){let r=t.accountIndices?.[n];if(r!==void 0)return e.staticAccounts[r]}function y(e){if(!e||e.length<12)return 0n;let t=new DataView(e.buffer,e.byteOffset,e.byteLength),n=t.getUint32(0,!0);if(n===0&&e.length>=12)return t.getBigUint64(4,!0);if(n===3&&e.length>=44){let n=Number(t.getBigUint64(36,!0));if(!Number.isSafeInteger(n)||n<0)return 0n;let r=44+n;return r+8>e.length?0n:new DataView(e.buffer,e.byteOffset+r,8).getBigUint64(0,!0)}return 0n}exports._estimateGasFromSwapResponse=p,exports.estimateNativeFeeFactory=u;
1
+ require(`../../../_virtual/_rolldown/runtime.cjs`);const e=require(`../../../errors.cjs`),t=require(`../../../utils/caip.cjs`),n=require(`../../../_utils/chain.cjs`),r=require(`../../_utils.cjs`),i=require(`../../_evm-gas.cjs`),a=require(`../_api.cjs`),o=require(`../_utils.cjs`),s=require(`../_type-guards.cjs`),c=require(`../_solana-utils.cjs`);let l=require(`viem`),u=require(`@solana/kit`);const d=1000000n;function f(t){return async(r,i)=>{if(n.isEvmNamespace(r.sourceChain.chainId))return await p(r,i,t);if(n.isSolanaNamespace(r.sourceChain.chainId))return await m(r,i,t);throw new e.InvalidParamsError(e.ErrorReason.INVALID_PARAMS,`Unsupported source chain namespace for estimateNativeFee: ${r.sourceChain.chainId}`)}}async function p(n,c,{apiOptions:u,appId:d}){if(!(0,l.isAddress)(n.fromAddress))throw new e.InvalidParamsError(e.ErrorReason.INVALID_PARAMS,`Invalid fromAddress: ${n.fromAddress}`);let f=r.getEvmClientForChain({chain:n.sourceChain}),p=o.assetToAddressString(n.assetIn,n.sourceChain.chainId);if(!(0,l.isAddress)(p))throw new e.SdkError(e.ErrorReason.INVALID_PARAMS,e.ErrorCode.INVALID_PARAMS,{details:`assetIn address is not a valid EVM address. Can not call estimateGas.`});let m=o.isTokenAddressNative(p),g=n.sourceChain.chainId.toLowerCase()!==n.targetChain.chainId.toLowerCase(),_=0n;if(!m){let r,{address:i}=await a.markrGetSpenderAddress(u,{chainId:t.caip2ToEip155ChainId(n.sourceChain.chainId),crossChainSwap:g,quoteId:n.id});try{r=await f.readContract({address:p,abi:l.erc20Abi,functionName:`allowance`,args:[n.fromAddress,i]})}catch(t){throw new e.SdkError(`Error during allowance check`,e.ErrorCode.VIEM_ERROR,{cause:t,details:`Failed to read ERC20 allowance for Markr spender.`})}if(r<n.amountIn)try{_=await f.estimateGas({account:n.fromAddress,to:p,data:(0,l.encodeFunctionData)({abi:l.erc20Abi,functionName:`approve`,args:[i,n.amountIn]}),value:0n})}catch(t){throw new e.SdkError(`Error during gas estimation`,e.ErrorCode.VIEM_ERROR,{cause:t,details:`Failed to estimate gas for ERC20 approval transaction.`})}}let v=_>0n,y=o.calculateMarkrMinimumAmountOut({amountOut:n.amountOut,assetOut:n.assetOut,slippageBps:n.slippageBps}),b=await a.markrSwap(u,{amountIn:n.amountIn.toString(),appId:d,minAmountOut:y.toString(),tokenIn:p,tokenOut:o.assetToAddressString(n.assetOut,n.targetChain.chainId),uuid:n.id});if(!s.isEvmSwapResponse(b))throw new e.SdkError(e.ErrorReason.CHAIN_NOT_SUPPORTED,e.ErrorCode.INVALID_PARAMS,{details:`Received non-EVM swap response from Markr. Expected EVM transaction data for gas estimation, but got response with chainType ${b.chainType}.`});let x=0n;x=v?n.gasEstimate?r.applyFeeUnitsBpsMargin(n.gasEstimate,c?.feeUnitsMarginBps):r.applyFeeUnitsBpsMargin(n.fees.filter(e=>e.type===`gas`&&e.chainId===n.sourceChain.chainId).reduce((e,t)=>e+t.amount,0n)||700000n,c?.feeUnitsMarginBps):await h({crossChain:g,feeUnitsMarginBps:c?.feeUnitsMarginBps,fromAddress:n.fromAddress,sourceClient:f,swap:b});let S=await i.estimateEvmFeesPerGas(f,n.sourceChain,c?.overrides?.feeRateTier),C=c?.overrides?.maxFeePerGas??S.maxFeePerGas,w=c?.overrides?.maxPriorityFeePerGas??S.maxPriorityFeePerGas,T=(x+r.applyFeeUnitsBpsMargin(_,c?.feeUnitsMarginBps))*C;return{asset:n.sourceChain.networkToken,totalFee:T,totalUpfrontFee:T,meta:{approvalFee:v?r.applyFeeUnitsBpsMargin(_,c?.feeUnitsMarginBps)*C:void 0,maxFeePerGas:C,maxPriorityFeePerGas:w}}}async function m(t,n,{apiOptions:i,appId:l}){let d=o.assetToAddressString(t.assetIn,t.sourceChain.chainId);if(!(0,u.isAddress)(d))throw new e.SdkError(e.ErrorReason.INVALID_PARAMS,e.ErrorCode.INVALID_PARAMS,{details:`assetIn address is not a valid Solana address. Can not call estimateGas.`});let f=o.calculateMarkrMinimumAmountOut({amountOut:t.amountOut,assetOut:t.assetOut,slippageBps:t.slippageBps}),p=await a.markrSwap(i,{amountIn:t.amountIn.toString(),appId:l,minAmountOut:f.toString(),tokenIn:d,tokenOut:o.assetToAddressString(t.assetOut,t.targetChain.chainId),userPublicKey:t.fromAddress,uuid:t.id});if(!s.isSolanaSwapResponse(p))throw new e.SdkError(e.ErrorReason.CHAIN_NOT_SUPPORTED,e.ErrorCode.INVALID_PARAMS,{details:`Received non-Solana swap response from Markr. Expected Solana transaction data for fee estimation.`});let m=r.getSolanaRpcForChain({chain:t.sourceChain}),h=await c.refreshSolanaSwapTransactionBlockhash(p.swapTransaction,m),b=c.decodeSolanaTransactionBase64(h),x=b.messageBytes,S=v(b)*5000n,{computeUnitLimit:C,computeUnitPriceMicroLamports:w}=g(x),T=_(C,w),E=S+T,D=r.applyFeeUnitsBpsMargin(E,n?.feeUnitsMarginBps),O=(await m.simulateTransaction(h,{encoding:`base64`,sigVerify:!1,replaceRecentBlockhash:!0}).send()).value.unitsConsumed??0n,k=await y({fromAddress:t.fromAddress,rpc:m,transaction:b}),A=D+k.rentFee;return{asset:t.sourceChain.networkToken,totalFee:A,totalUpfrontFee:A,refundable:k.refundable,meta:{baseFee:S,priorityFee:T,transactionFee:E,transactionFeeWithMargin:D,computeUnitLimit:C,computeUnitPriceMicroLamports:w,unitsConsumed:O,createdAccounts:k.createdAccounts,rentFee:k.rentFee,rentFeeNet:k.rentFeeNet}}}async function h({crossChain:t,feeUnitsMarginBps:n,fromAddress:i,sourceClient:a,swap:s}){try{return r.applyFeeUnitsBpsMargin(await a.estimateGas({account:i,to:s.to,data:s.data,value:s.value}),n)}catch(n){let r=`Failed to estimate gas for Markr swap transaction.`;try{let e=o.decodeMarkrRevertError(await o.getMarkrSwapWrapperAbi(t),n);e&&(r=`${r} Markr revert: ${e}.`)}catch{}throw new e.SdkError(`Error during gas estimation`,e.ErrorCode.VIEM_ERROR,{cause:n,details:r})}}function g(e){let t=(0,u.getCompiledTransactionMessageDecoder)().decode(e),n=0n,r=0n;for(let e of t.instructions){if(t.staticAccounts[e.programAddressIndex]!==`ComputeBudget111111111111111111111111111111`)continue;let i=e.data;if(!i||i.length<1)continue;let a=i[0],o=new DataView(i.buffer,i.byteOffset,i.byteLength);if(a===2&&i.length>=5){n=BigInt(o.getUint32(1,!0));continue}a===3&&i.length>=9&&(r=o.getBigUint64(1,!0))}return{computeUnitLimit:n,computeUnitPriceMicroLamports:r}}function _(e,t){return e<=0n||t<=0n?0n:(e*t+(d-1n))/d}function v(e){let t=e.signatures;return Array.isArray(t)?BigInt(t.length):t&&typeof t==`object`?BigInt(Object.keys(t).length):0n}async function y({fromAddress:e,rpc:t,transaction:n}){let r=(0,u.getCompiledTransactionMessageDecoder)().decode(n.messageBytes),i=await D(r,t),a=new Map,o=new Map,s;for(let e of r.instructions){let n=i[e.programAddressIndex];if(n===`11111111111111111111111111111111`){let t=O(i,e,1),n=k(e.data);t&&n>0n&&a.set(t,{address:t,kind:`system`,lamports:n});continue}if(n===`ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL`){let n=e.data?.[0];if(!(n===void 0||n===0||n===1))continue;let r=O(i,e,1);if(!r||!(0,u.isAddress)(r)||(await t.getAccountInfo(r).send()).value)continue;s===void 0&&(s=await t.getMinimumBalanceForRentExemption(165n).send()),a.set(r,{address:r,kind:`ata`,lamports:s});continue}if(!(n===`TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA`||n===`TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb`)||e.data?.[0]!==9)continue;let r=O(i,e,0),c=O(i,e,1);r&&c&&o.set(r,c)}let c=(0,u.isAddress)(e)?e:void 0,l=Array.from(a.values()).map(e=>{let t=o.get(e.address),n=c!==void 0&&t===c;return{...e,refunded:n}}),d=l.reduce((e,t)=>e+t.lamports,0n),f=l.reduce((e,t)=>e+(t.refunded?t.lamports:0n),0n);return{createdAccounts:l,rentFee:d,rentFeeNet:d-f,refundable:f}}function b(e){return!!e&&typeof e==`object`}function x(e,t){if(b(e))return Reflect.get(e,t)}function S(e){if(e instanceof Uint8Array)return Array.from(e);if(Array.isArray(e)&&e.every(e=>Number.isInteger(e)&&e>=0))return e}function C(e){let t=x(e,`addressTableLookups`);if(!Array.isArray(t))return[];let n=[];for(let e of t){let t=x(e,`accountKey`),r=S(x(e,`writableIndexes`)),i=S(x(e,`readonlyIndexes`));typeof t!=`string`||!r||!i||n.push({accountKey:t,writableIndexes:r,readonlyIndexes:i})}return n}function w(e){let t=x(e,`value`);if(!b(t))return[];let n=x(t,`addresses`);return Array.isArray(n)?n.filter(e=>typeof e==`string`):[]}async function T(e,t){let n=[],r=[],i=C(e);for(let e of i){if(!(0,u.isAddress)(e.accountKey))continue;let i=await E(t,e.accountKey);for(let t of e.writableIndexes){let e=i[t];e&&n.push(e)}for(let t of e.readonlyIndexes){let e=i[t];e&&r.push(e)}}return{writableAddresses:n,readonlyAddresses:r}}async function E(e,t){let n=x(e,`getAddressLookupTable`);if(typeof n!=`function`)return[];let r=Reflect.apply(n,e,[t]),i=x(r,`send`);return typeof i==`function`?w(await Reflect.apply(i,r,[])):[]}async function D(e,t){let{writableAddresses:n,readonlyAddresses:r}=await T(e,t);return[...e.staticAccounts,...n,...r]}function O(e,t,n){let r=t.accountIndices?.[n];if(r!==void 0)return e[r]}function k(e){if(!e||e.length<12)return 0n;let t=new DataView(e.buffer,e.byteOffset,e.byteLength),n=t.getUint32(0,!0);if(n===0&&e.length>=12)return t.getBigUint64(4,!0);if(n===3&&e.length>=44){let n=Number(t.getBigUint64(36,!0));if(!Number.isSafeInteger(n)||n<0)return 0n;let r=44+n;return r+8>e.length?0n:new DataView(e.buffer,e.byteOffset+r,8).getBigUint64(0,!0)}return 0n}exports._estimateGasFromSwapResponse=h,exports.estimateNativeFeeFactory=f;
2
2
  //# sourceMappingURL=estimate-native-fee.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"estimate-native-fee.cjs","names":["isEvmNamespace","isSolanaNamespace","InvalidParamsError","ErrorReason","getEvmClientForChain","assetToAddressString","SdkError","ErrorCode","isTokenAddressNative","markrGetSpenderAddress","caip2ToEip155ChainId","erc20Abi","calculateMarkrMinimumAmountOut","markrSwap","isEvmSwapResponse","applyFeeUnitsBpsMargin","estimateEvmFeesPerGas","isSolanaSwapResponse","getSolanaRpcForChain","decodeMarkrRevertError","getMarkrSwapWrapperAbi"],"sources":["../../../../src/transfer-service/markr/_handlers/estimate-native-fee.ts"],"sourcesContent":["import {\n compileTransaction,\n decompileTransactionMessageFetchingLookupTables,\n type CompiledTransactionMessage,\n getCompiledTransactionMessageDecoder,\n getBase64EncodedWireTransaction,\n getTransactionDecoder,\n isAddress as isSolanaAddress,\n setTransactionMessageLifetimeUsingBlockhash,\n type Base64EncodedWireTransaction,\n type Rpc,\n type SolanaRpcApi,\n type TransactionMessageBytes,\n type TransactionMessageBytesBase64,\n} from '@solana/kit';\nimport { encodeFunctionData, erc20Abi, type Address as EvmAddress, isAddress as isEvmAddress } from 'viem';\nimport { ErrorCode, ErrorReason, InvalidParamsError, SdkError } from '../../../errors';\nimport type { EstimateNativeFeeOptions, NativeFeeEstimate, TransferService } from '../../../types/service';\nimport { applyFeeUnitsBpsMargin, getEvmClientForChain, getSolanaRpcForChain } from '../../_utils';\nimport { estimateEvmFeesPerGas } from '../../_evm-gas';\nimport { markrGetSpenderAddress, markrSwap, type ApiOptions } from '../_api';\nimport {\n assetToAddressString,\n calculateMarkrMinimumAmountOut,\n decodeMarkrRevertError,\n getMarkrSwapWrapperAbi,\n isTokenAddressNative,\n} from '../_utils';\nimport type { WrappedSwapTransactionResponse } from '../_schema';\nimport { isEvmSwapResponse, isSolanaSwapResponse } from '../_type-guards';\nimport { caip2ToEip155ChainId } from '../../../utils/caip';\nimport type { Quote } from '../../../types/quote';\nimport { isEvmNamespace, isSolanaNamespace } from '../../../_utils/chain';\n\n/**\n * This is just a fallback value used in the case that an\n * allowance approval is needed, and Markr didn't return us any gas estimate data.\n *\n * Just based on my review of some quotes, I think this is a reasonable estimate\n * left on the higher end.\n *\n * Assume this could need to change.\n */\nconst EVM_SWAP_FALLBACK_GAS_ESTIMATE = 700_000n;\n\n/**\n * Compute Budget program address.\n *\n * Used to detect `SetComputeUnitPrice` instructions when deriving\n * the Solana priority fee component from transaction message bytes.\n */\nconst SOLANA_COMPUTE_BUDGET_PROGRAM = 'ComputeBudget111111111111111111111111111111';\n\n/**\n * System Program address.\n *\n * Used to detect account-creation instructions and extract lamports\n * funded upfront for newly created system accounts.\n */\nconst SOLANA_SYSTEM_PROGRAM = '11111111111111111111111111111111';\n\n/**\n * Associated Token Account (ATA) program address.\n *\n * Used to detect ATA create instructions so the estimator can include\n * rent-exempt funding when the ATA does not already exist on-chain.\n */\nconst SOLANA_ASSOCIATED_TOKEN_PROGRAM = 'ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL';\n\n/**\n * Legacy SPL Token program address.\n *\n * Used to detect close-account instructions that may refund lamports\n * from temporary token accounts back to the sender.\n */\nconst SOLANA_SPL_TOKEN_PROGRAM = 'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA';\n\n/**\n * SPL Token-2022 program address.\n *\n * Used alongside the legacy token program for close-account detection\n * when computing expected refundable lamports.\n */\nconst SOLANA_SPL_TOKEN_2022_PROGRAM = 'TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb';\n\n/**\n * SPL token account size for rent-exemption calculations.\n *\n * Used with `getMinimumBalanceForRentExemption` to estimate ATA\n * creation funding requirements.\n */\nconst SPL_TOKEN_ACCOUNT_SIZE_BYTES = 165n;\n\nexport interface EstimateNativeFeeFactoryConfig {\n apiOptions: ApiOptions;\n appId: string;\n}\n\ninterface CreatedRentAccount {\n address: string;\n kind: 'ata' | 'system';\n lamports: bigint;\n refunded: boolean;\n}\n\ninterface SolanaRentEstimate {\n createdAccounts: CreatedRentAccount[];\n rentFee: bigint;\n rentFeeNet: bigint;\n refundable: bigint;\n}\n\nexport function estimateNativeFeeFactory(config: EstimateNativeFeeFactoryConfig): TransferService['estimateNativeFee'] {\n return async (quote, options) => {\n // Either the source chain is EVM or Solana, we need to call different functions that\n // calculate the native fee depending on the chain kind.\n\n if (isEvmNamespace(quote.sourceChain.chainId)) {\n return await _estimateNativeFeeEvm(quote, options, config);\n }\n\n if (isSolanaNamespace(quote.sourceChain.chainId)) {\n return await _estimateNativeFeeSolana(quote, options, config);\n }\n\n throw new InvalidParamsError(\n ErrorReason.INVALID_PARAMS,\n `Unsupported source chain namespace for estimateNativeFee: ${quote.sourceChain.chainId}`,\n );\n };\n}\n\n/** @internal */\nexport async function _estimateNativeFeeEvm(\n quote: Quote,\n options: EstimateNativeFeeOptions | undefined,\n { apiOptions, appId }: EstimateNativeFeeFactoryConfig,\n): Promise<NativeFeeEstimate> {\n if (!isEvmAddress(quote.fromAddress)) {\n throw new InvalidParamsError(ErrorReason.INVALID_PARAMS, `Invalid fromAddress: ${quote.fromAddress}`);\n }\n\n const sourceClient = getEvmClientForChain({ chain: quote.sourceChain });\n\n const assetInAddressString = assetToAddressString(quote.assetIn, quote.sourceChain.chainId);\n\n if (!isEvmAddress(assetInAddressString)) {\n throw new SdkError(ErrorReason.INVALID_PARAMS, ErrorCode.INVALID_PARAMS, {\n details: `assetIn address is not a valid EVM address. Can not call estimateGas.`,\n });\n }\n\n const isAssetInNative = isTokenAddressNative(assetInAddressString);\n const isCrossChainSwap = quote.sourceChain.chainId.toLowerCase() !== quote.targetChain.chainId.toLowerCase();\n\n let allowanceApprovalGas = 0n;\n if (!isAssetInNative) {\n // Check if approval is needed, and if so, calculate the gas cost for the approval.\n let allowance: bigint;\n\n const { address: spenderAddress } = await markrGetSpenderAddress(apiOptions, {\n chainId: caip2ToEip155ChainId(quote.sourceChain.chainId),\n crossChainSwap: isCrossChainSwap,\n quoteId: quote.id,\n });\n\n try {\n allowance = await sourceClient.readContract({\n address: assetInAddressString,\n abi: erc20Abi,\n functionName: 'allowance',\n args: [quote.fromAddress, spenderAddress],\n });\n } catch (error) {\n throw new SdkError('Error during allowance check', ErrorCode.VIEM_ERROR, {\n cause: error,\n details: 'Failed to read ERC20 allowance for Markr spender.',\n });\n }\n\n const approvalNeeded = allowance < quote.amountIn;\n\n if (approvalNeeded) {\n try {\n allowanceApprovalGas = await sourceClient.estimateGas({\n account: quote.fromAddress,\n to: assetInAddressString,\n data: encodeFunctionData({\n abi: erc20Abi,\n functionName: 'approve',\n args: [spenderAddress, quote.amountIn],\n }),\n value: 0n,\n });\n } catch (error) {\n throw new SdkError('Error during gas estimation', ErrorCode.VIEM_ERROR, {\n cause: error,\n details: 'Failed to estimate gas for ERC20 approval transaction.',\n });\n }\n }\n }\n\n const allowanceApprovalIsNeeded = allowanceApprovalGas > 0n;\n\n const minAmountOut = calculateMarkrMinimumAmountOut({\n amountOut: quote.amountOut,\n assetOut: quote.assetOut,\n slippageBps: quote.slippageBps,\n });\n\n const swap = await markrSwap(apiOptions, {\n amountIn: quote.amountIn.toString(),\n appId,\n minAmountOut: minAmountOut.toString(),\n tokenIn: assetInAddressString,\n tokenOut: assetToAddressString(quote.assetOut, quote.targetChain.chainId),\n uuid: quote.id,\n });\n\n if (!isEvmSwapResponse(swap)) {\n // Should hopefully never happen.\n throw new SdkError(ErrorReason.CHAIN_NOT_SUPPORTED, ErrorCode.INVALID_PARAMS, {\n details: `Received non-EVM swap response from Markr. Expected EVM transaction data for gas estimation, but got response with chainType ${swap.chainType}.`,\n });\n }\n\n let gasWithMargin = 0n;\n\n // If an approval is needed, we can't call `eth_estimateGas` on the\n // swap transaction because no actual allowance approval was performed yet,\n // so a gas estimation would fail due to needing allowance.\n //\n // So if an allowance approval is needed, we fall back to some other logic for\n // estimating the swap tx gas.\n if (allowanceApprovalIsNeeded) {\n // Attempt to use the Markr provided `gasEstimate` first if available.\n // Otherwise we attempt to get the gas from the `fees` component that are applicable\n // to the source chain. If neither of those are available, we fall back to a hardcoded value.\n if (quote.gasEstimate) {\n gasWithMargin = applyFeeUnitsBpsMargin(quote.gasEstimate, options?.feeUnitsMarginBps);\n } else {\n const sourceGasFee = quote.fees\n .filter((fee) => fee.type === 'gas' && fee.chainId === quote.sourceChain.chainId)\n .reduce((acc, fee) => acc + fee.amount, 0n);\n\n gasWithMargin = applyFeeUnitsBpsMargin(\n sourceGasFee || EVM_SWAP_FALLBACK_GAS_ESTIMATE,\n options?.feeUnitsMarginBps,\n );\n }\n } else {\n gasWithMargin = await _estimateGasFromSwapResponse({\n crossChain: isCrossChainSwap,\n feeUnitsMarginBps: options?.feeUnitsMarginBps,\n fromAddress: quote.fromAddress,\n sourceClient,\n swap,\n });\n }\n\n const fees = await estimateEvmFeesPerGas(sourceClient, quote.sourceChain, options?.overrides?.feeRateTier);\n\n const maxFeePerGas = options?.overrides?.maxFeePerGas ?? fees.maxFeePerGas;\n const maxPriorityFeePerGas = options?.overrides?.maxPriorityFeePerGas ?? fees.maxPriorityFeePerGas;\n\n const totalFee =\n (gasWithMargin + applyFeeUnitsBpsMargin(allowanceApprovalGas, options?.feeUnitsMarginBps)) * maxFeePerGas;\n\n return {\n asset: quote.sourceChain.networkToken,\n totalFee,\n meta: {\n approvalFee: allowanceApprovalIsNeeded\n ? applyFeeUnitsBpsMargin(allowanceApprovalGas, options?.feeUnitsMarginBps) * maxFeePerGas\n : undefined,\n maxFeePerGas,\n maxPriorityFeePerGas,\n },\n };\n}\n\n/** @internal */\nexport async function _estimateNativeFeeSolana(\n quote: Quote,\n options: EstimateNativeFeeOptions | undefined,\n { apiOptions, appId }: EstimateNativeFeeFactoryConfig,\n): Promise<NativeFeeEstimate> {\n const assetInAddressString = assetToAddressString(quote.assetIn, quote.sourceChain.chainId);\n\n if (!isSolanaAddress(assetInAddressString)) {\n throw new SdkError(ErrorReason.INVALID_PARAMS, ErrorCode.INVALID_PARAMS, {\n details: `assetIn address is not a valid Solana address. Can not call estimateGas.`,\n });\n }\n\n const minAmountOut = calculateMarkrMinimumAmountOut({\n amountOut: quote.amountOut,\n assetOut: quote.assetOut,\n slippageBps: quote.slippageBps,\n });\n\n const swap = await markrSwap(apiOptions, {\n amountIn: quote.amountIn.toString(),\n appId,\n minAmountOut: minAmountOut.toString(),\n tokenIn: assetInAddressString,\n tokenOut: assetToAddressString(quote.assetOut, quote.targetChain.chainId),\n userPublicKey: quote.fromAddress,\n uuid: quote.id,\n });\n\n if (!isSolanaSwapResponse(swap)) {\n // Should hopefully never happen.\n throw new SdkError(ErrorReason.CHAIN_NOT_SUPPORTED, ErrorCode.INVALID_PARAMS, {\n details: 'Received non-Solana swap response from Markr. Expected Solana transaction data for fee estimation.',\n });\n }\n\n const rpc = getSolanaRpcForChain({ chain: quote.sourceChain });\n\n const txBytes = base64ToBytes(swap.swapTransaction);\n const tx = getTransactionDecoder().decode(txBytes);\n\n let transactionMessageBytes = tx.messageBytes;\n let swapTransactionBase64 = swap.swapTransaction as Base64EncodedWireTransaction;\n\n const initialFeeForMessageResponse = await rpc\n .getFeeForMessage(_transactionMessageBytesToBase64(transactionMessageBytes))\n .send();\n\n let baseFeeLamports = initialFeeForMessageResponse.value;\n\n if (!baseFeeLamports) {\n const latestBlockhashResponse = await rpc.getLatestBlockhash().send();\n const compiledMessageWithLifetime = getCompiledTransactionMessageDecoder().decode(transactionMessageBytes);\n const decompiledMessage = await decompileTransactionMessageFetchingLookupTables(compiledMessageWithLifetime, rpc);\n\n const refreshedTxMessage = setTransactionMessageLifetimeUsingBlockhash(\n latestBlockhashResponse.value,\n decompiledMessage,\n );\n\n const refreshedTx = compileTransaction(refreshedTxMessage);\n transactionMessageBytes = refreshedTx.messageBytes;\n swapTransactionBase64 = getBase64EncodedWireTransaction(refreshedTx) as Base64EncodedWireTransaction;\n\n const refreshedFeeForMessageResponse = await rpc\n .getFeeForMessage(_transactionMessageBytesToBase64(transactionMessageBytes))\n .send();\n\n baseFeeLamports = refreshedFeeForMessageResponse.value;\n }\n\n if (!baseFeeLamports) {\n throw new SdkError('Failed to get fee for message after refreshing blockhash.', ErrorCode.TIMEOUT, {\n details: 'Unable to estimate Solana base fee because the transaction message remained invalid.',\n });\n }\n\n const sim = await rpc\n .simulateTransaction(swapTransactionBase64, {\n encoding: 'base64',\n sigVerify: false,\n replaceRecentBlockhash: true, // Helps prevent block hash expiration issues during simulation.\n })\n .send();\n\n const unitsConsumed = applyFeeUnitsBpsMargin(sim.value.unitsConsumed ?? 0n, options?.feeUnitsMarginBps);\n // Lamports\n const computeUnitPriceMicroLamports = _getComputeUnitPriceMicroLamports(transactionMessageBytes);\n const priorityFee = (unitsConsumed * computeUnitPriceMicroLamports) / 1_000_000n;\n const rentEstimate = await _estimateRentFeesFromTransactionMessage({\n fromAddress: quote.fromAddress,\n rpc,\n transactionMessageBytes,\n });\n const totalLamports = baseFeeLamports + priorityFee + rentEstimate.rentFee;\n\n return {\n asset: quote.sourceChain.networkToken,\n totalFee: totalLamports,\n refundable: rentEstimate.refundable,\n meta: {\n baseFee: baseFeeLamports,\n priorityFee,\n computeUnitPriceMicroLamports,\n unitsConsumed,\n createdAccounts: rentEstimate.createdAccounts,\n rentFee: rentEstimate.rentFee,\n rentFeeNet: rentEstimate.rentFeeNet,\n },\n };\n}\n\n/** @internal */\nexport async function _estimateGasFromSwapResponse({\n crossChain,\n feeUnitsMarginBps,\n fromAddress,\n sourceClient,\n swap,\n}: {\n crossChain: boolean;\n feeUnitsMarginBps?: number;\n fromAddress: EvmAddress;\n sourceClient: ReturnType<typeof getEvmClientForChain>;\n swap: WrappedSwapTransactionResponse;\n}): Promise<bigint> {\n try {\n const gasEstimate = await sourceClient.estimateGas({\n account: fromAddress,\n to: swap.to,\n data: swap.data,\n value: swap.value,\n });\n\n return applyFeeUnitsBpsMargin(gasEstimate, feeUnitsMarginBps);\n } catch (err) {\n let details = 'Failed to estimate gas for Markr swap transaction.';\n\n try {\n const markrSwapWrapperAbi = await getMarkrSwapWrapperAbi(crossChain);\n const decodedRevert = decodeMarkrRevertError(markrSwapWrapperAbi, err);\n\n if (decodedRevert) {\n details = `${details} Markr revert: ${decodedRevert}.`;\n }\n } catch {\n // Keep the base details message if revert decoding fails.\n }\n\n throw new SdkError('Error during gas estimation', ErrorCode.VIEM_ERROR, {\n cause: err,\n details,\n });\n }\n}\n\nfunction base64ToBytes(base64: string): Uint8Array {\n return Uint8Array.from(atob(base64), (c) => c.charCodeAt(0));\n}\n\nfunction _transactionMessageBytesToBase64(bytes: TransactionMessageBytes): TransactionMessageBytesBase64 {\n let binary = '';\n for (const b of bytes) {\n binary += String.fromCharCode(b);\n }\n return btoa(binary) as TransactionMessageBytesBase64;\n}\n\nfunction _getComputeUnitPriceMicroLamports(transactionMessageBytes: TransactionMessageBytes): bigint {\n const compiledTxMessage = getCompiledTransactionMessageDecoder().decode(transactionMessageBytes);\n\n for (const instruction of compiledTxMessage.instructions) {\n const programAddress = compiledTxMessage.staticAccounts[instruction.programAddressIndex];\n if (programAddress !== SOLANA_COMPUTE_BUDGET_PROGRAM) {\n continue;\n }\n\n const data = instruction.data;\n\n if (!data || data.length < 1 + 8) {\n continue;\n }\n\n // ComputeBudget: u8 discriminator, then args\n // discriminator 3 = SetComputeUnitPrice, args = u64 microLamports\n const discriminator = data[0];\n if (discriminator !== 3) {\n continue;\n }\n\n const view = new DataView(data.buffer, data.byteOffset, data.byteLength);\n\n return view.getBigUint64(1, true);\n }\n\n return 0n;\n}\n\nasync function _estimateRentFeesFromTransactionMessage({\n fromAddress,\n rpc,\n transactionMessageBytes,\n}: {\n fromAddress: string;\n rpc: Rpc<SolanaRpcApi>;\n transactionMessageBytes: TransactionMessageBytes;\n}): Promise<SolanaRentEstimate> {\n const compiledMessage: CompiledTransactionMessage =\n getCompiledTransactionMessageDecoder().decode(transactionMessageBytes);\n\n const createdAccounts = new Map<string, Omit<CreatedRentAccount, 'refunded'>>();\n const closedAccountDestinationByAddress = new Map<string, string>();\n\n let ataRentLamports: bigint | undefined;\n\n for (const instruction of compiledMessage.instructions) {\n const programAddress = compiledMessage.staticAccounts[instruction.programAddressIndex];\n\n if (programAddress === SOLANA_SYSTEM_PROGRAM) {\n const createdAccountAddress = _getInstructionAccountAddress(compiledMessage, instruction, 1);\n const lamports = _getSystemCreateAccountLamports(instruction.data);\n if (createdAccountAddress && lamports > 0n) {\n createdAccounts.set(createdAccountAddress, {\n address: createdAccountAddress,\n kind: 'system',\n lamports,\n });\n }\n continue;\n }\n\n if (programAddress === SOLANA_ASSOCIATED_TOKEN_PROGRAM) {\n const discriminator = instruction.data?.[0];\n const isAssociatedTokenCreateInstruction =\n discriminator === undefined || discriminator === 0 || discriminator === 1;\n\n if (!isAssociatedTokenCreateInstruction) {\n continue;\n }\n\n const associatedTokenAddress = _getInstructionAccountAddress(compiledMessage, instruction, 1);\n if (!associatedTokenAddress) {\n continue;\n }\n\n const accountInfo = await rpc.getAccountInfo(associatedTokenAddress).send();\n if (accountInfo.value) {\n continue;\n }\n\n if (ataRentLamports === undefined) {\n ataRentLamports = await rpc.getMinimumBalanceForRentExemption(SPL_TOKEN_ACCOUNT_SIZE_BYTES).send();\n }\n\n createdAccounts.set(associatedTokenAddress, {\n address: associatedTokenAddress,\n kind: 'ata',\n lamports: ataRentLamports,\n });\n\n continue;\n }\n\n const isSplTokenProgram =\n programAddress === SOLANA_SPL_TOKEN_PROGRAM || programAddress === SOLANA_SPL_TOKEN_2022_PROGRAM;\n if (!isSplTokenProgram) {\n continue;\n }\n\n const discriminator = instruction.data?.[0];\n if (discriminator !== 9) {\n continue;\n }\n\n const closedAccountAddress = _getInstructionAccountAddress(compiledMessage, instruction, 0);\n const refundDestinationAddress = _getInstructionAccountAddress(compiledMessage, instruction, 1);\n\n if (closedAccountAddress && refundDestinationAddress) {\n closedAccountDestinationByAddress.set(closedAccountAddress, refundDestinationAddress);\n }\n }\n\n const normalizedFromAddress = isSolanaAddress(fromAddress) ? fromAddress : undefined;\n const createdAccountEntries = Array.from(createdAccounts.values()).map((createdAccount) => {\n const refundDestinationAddress = closedAccountDestinationByAddress.get(createdAccount.address);\n const refunded = normalizedFromAddress !== undefined && refundDestinationAddress === normalizedFromAddress;\n\n return {\n ...createdAccount,\n refunded,\n } satisfies CreatedRentAccount;\n });\n\n const rentLamportsUpfront = createdAccountEntries.reduce((acc, account) => acc + account.lamports, 0n);\n const refundedLamports = createdAccountEntries.reduce(\n (acc, account) => acc + (account.refunded ? account.lamports : 0n),\n 0n,\n );\n const rentFeeNet = rentLamportsUpfront - refundedLamports;\n\n return {\n createdAccounts: createdAccountEntries,\n rentFee: rentLamportsUpfront,\n rentFeeNet,\n refundable: refundedLamports,\n };\n}\n\n/**\n * Resolves an account address used by a compiled instruction.\n *\n * Compiled instructions reference accounts by index into the transaction\n * message's `staticAccounts` list. This helper converts an instruction-local\n * `accountIndex` into the corresponding static account address.\n *\n * Returns `undefined` when the instruction does not provide an account at the\n * requested index.\n */\nfunction _getInstructionAccountAddress(\n message: CompiledTransactionMessage,\n instruction: CompiledTransactionMessage['instructions'][number],\n accountIndex: number,\n): CompiledTransactionMessage['staticAccounts'][number] | undefined {\n const messageAccountIndex = instruction.accountIndices?.[accountIndex];\n\n if (messageAccountIndex === undefined) {\n return undefined;\n }\n\n return message.staticAccounts[messageAccountIndex];\n}\n\n/**\n * Extracts lamports from System Program account-creation instruction data.\n *\n * Supported instruction layouts:\n * - `CreateAccount` (`u32 discriminator = 0`): lamports at byte offset `4`.\n * - `CreateAccountWithSeed` (`u32 discriminator = 3`): lamports are located\n * after the variable-length seed field.\n *\n * Returns `0n` when data is missing, malformed, or not one of the supported\n * System Program create-account variants.\n */\nfunction _getSystemCreateAccountLamports(data: CompiledTransactionMessage['instructions'][number]['data']): bigint {\n if (!data || data.length < 12) {\n return 0n;\n }\n\n const view = new DataView(data.buffer, data.byteOffset, data.byteLength);\n const instructionDiscriminator = view.getUint32(0, true);\n\n if (instructionDiscriminator === 0 && data.length >= 12) {\n return view.getBigUint64(4, true);\n }\n\n if (instructionDiscriminator === 3 && data.length >= 44) {\n const seedLenOffset = 36;\n const seedLength = Number(view.getBigUint64(seedLenOffset, true));\n if (!Number.isSafeInteger(seedLength) || seedLength < 0) {\n return 0n;\n }\n\n const lamportsOffset = seedLenOffset + 8 + seedLength;\n if (lamportsOffset + 8 > data.length) {\n return 0n;\n }\n\n return new DataView(data.buffer, data.byteOffset + lamportsOffset, 8).getBigUint64(0, true);\n }\n\n return 0n;\n}\n"],"mappings":"wWAgHA,SAAgB,EAAyB,EAA8E,CACrH,OAAO,MAAO,EAAO,IAAY,CAI/B,GAAIA,EAAAA,eAAe,EAAM,YAAY,QAAQ,CAC3C,OAAO,MAAM,EAAsB,EAAO,EAAS,EAAO,CAG5D,GAAIC,EAAAA,kBAAkB,EAAM,YAAY,QAAQ,CAC9C,OAAO,MAAM,EAAyB,EAAO,EAAS,EAAO,CAG/D,MAAM,IAAIC,EAAAA,mBACRC,EAAAA,YAAY,eACZ,6DAA6D,EAAM,YAAY,UAChF,EAKL,eAAsB,EACpB,EACA,EACA,CAAE,aAAY,SACc,CAC5B,GAAI,EAAA,EAAA,EAAA,WAAc,EAAM,YAAY,CAClC,MAAM,IAAID,EAAAA,mBAAmBC,EAAAA,YAAY,eAAgB,wBAAwB,EAAM,cAAc,CAGvG,IAAM,EAAeC,EAAAA,qBAAqB,CAAE,MAAO,EAAM,YAAa,CAAC,CAEjE,EAAuBC,EAAAA,qBAAqB,EAAM,QAAS,EAAM,YAAY,QAAQ,CAE3F,GAAI,EAAA,EAAA,EAAA,WAAc,EAAqB,CACrC,MAAM,IAAIC,EAAAA,SAASH,EAAAA,YAAY,eAAgBI,EAAAA,UAAU,eAAgB,CACvE,QAAS,wEACV,CAAC,CAGJ,IAAM,EAAkBC,EAAAA,qBAAqB,EAAqB,CAC5D,EAAmB,EAAM,YAAY,QAAQ,aAAa,GAAK,EAAM,YAAY,QAAQ,aAAa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AAe,QAAQ,CACxF,EAAW,IAA0B,IAAA,IAAa,IAA6B,EAErF,MAAO,CACL,GAAG,EACH,WACD,EACD,CAEI,EAAsB,EAAsB,QAAQ,EAAK,IAAY,EAAM,EAAQ,SAAU,GAAG,CAChG,EAAmB,EAAsB,QAC5C,EAAK,IAAY,GAAO,EAAQ,SAAW,EAAQ,SAAW,IAC/D,GACD,CAGD,MAAO,CACL,gBAAiB,EACjB,QAAS,EACT,WALiB,EAAsB,EAMvC,WAAY,EACb,CAaH,SAAS,EACP,EACA,EACA,EACkE,CAClE,IAAM,EAAsB,EAAY,iBAAiB,GAErD,OAAwB,IAAA,GAI5B,OAAO,EAAQ,eAAe,GAchC,SAAS,EAAgC,EAA0E,CACjH,GAAI,CAAC,GAAQ,EAAK,OAAS,GACzB,OAAO,GAGT,IAAM,EAAO,IAAI,SAAS,EAAK,OAAQ,EAAK,WAAY,EAAK,WAAW,CAClE,EAA2B,EAAK,UAAU,EAAG,GAAK,CAExD,GAAI,IAA6B,GAAK,EAAK,QAAU,GACnD,OAAO,EAAK,aAAa,EAAG,GAAK,CAGnC,GAAI,IAA6B,GAAK,EAAK,QAAU,GAAI,CACvD,IACM,EAAa,OAAO,EAAK,aAAa,GAAe,GAAK,CAAC,CACjE,GAAI,CAAC,OAAO,cAAc,EAAW,EAAI,EAAa,EACpD,OAAO,GAGT,IAAM,EAAiB,GAAoB,EAK3C,OAJI,EAAiB,EAAI,EAAK,OACrB,GAGF,IAAI,SAAS,EAAK,OAAQ,EAAK,WAAa,EAAgB,EAAE,CAAC,aAAa,EAAG,GAAK,CAG7F,OAAO"}
1
+ {"version":3,"file":"estimate-native-fee.cjs","names":["isEvmNamespace","isSolanaNamespace","InvalidParamsError","ErrorReason","getEvmClientForChain","assetToAddressString","SdkError","ErrorCode","isTokenAddressNative","markrGetSpenderAddress","caip2ToEip155ChainId","erc20Abi","calculateMarkrMinimumAmountOut","markrSwap","isEvmSwapResponse","applyFeeUnitsBpsMargin","estimateEvmFeesPerGas","isSolanaSwapResponse","getSolanaRpcForChain","refreshSolanaSwapTransactionBlockhash","decodeSolanaTransactionBase64","decodeMarkrRevertError","getMarkrSwapWrapperAbi"],"sources":["../../../../src/transfer-service/markr/_handlers/estimate-native-fee.ts"],"sourcesContent":["import {\n type Address as SolanaAddress,\n type CompiledTransactionMessage,\n getCompiledTransactionMessageDecoder,\n isAddress as isSolanaAddress,\n type Rpc,\n type SolanaRpcApi,\n type Transaction,\n type TransactionMessageBytes,\n} from '@solana/kit';\nimport { encodeFunctionData, erc20Abi, type Address as EvmAddress, isAddress as isEvmAddress } from 'viem';\nimport { ErrorCode, ErrorReason, InvalidParamsError, SdkError } from '../../../errors';\nimport type { EstimateNativeFeeOptions, NativeFeeEstimate, TransferService } from '../../../types/service';\nimport { applyFeeUnitsBpsMargin, getEvmClientForChain, getSolanaRpcForChain } from '../../_utils';\nimport { estimateEvmFeesPerGas } from '../../_evm-gas';\nimport { markrGetSpenderAddress, markrSwap, type ApiOptions } from '../_api';\nimport {\n assetToAddressString,\n calculateMarkrMinimumAmountOut,\n decodeMarkrRevertError,\n getMarkrSwapWrapperAbi,\n isTokenAddressNative,\n} from '../_utils';\nimport type { WrappedSwapTransactionResponse } from '../_schema';\nimport { isEvmSwapResponse, isSolanaSwapResponse } from '../_type-guards';\nimport { caip2ToEip155ChainId } from '../../../utils/caip';\nimport type { Quote } from '../../../types/quote';\nimport { isEvmNamespace, isSolanaNamespace } from '../../../_utils/chain';\nimport { decodeSolanaTransactionBase64, refreshSolanaSwapTransactionBlockhash } from '../_solana-utils';\n\n/**\n * This is just a fallback value used in the case that an\n * allowance approval is needed, and Markr didn't return us any gas estimate data.\n *\n * Just based on my review of some quotes, I think this is a reasonable estimate\n * left on the higher end.\n *\n * Assume this could need to change.\n */\nconst EVM_SWAP_FALLBACK_GAS_ESTIMATE = 700_000n;\n\n/**\n * Compute Budget program address.\n *\n * Used to detect `SetComputeUnitPrice` instructions when deriving\n * the Solana priority fee component from transaction message bytes.\n */\nconst SOLANA_COMPUTE_BUDGET_PROGRAM = 'ComputeBudget111111111111111111111111111111';\n\n/**\n * System Program address.\n *\n * Used to detect account-creation instructions and extract lamports\n * funded upfront for newly created system accounts.\n */\nconst SOLANA_SYSTEM_PROGRAM = '11111111111111111111111111111111';\n\n/**\n * Associated Token Account (ATA) program address.\n *\n * Used to detect ATA create instructions so the estimator can include\n * rent-exempt funding when the ATA does not already exist on-chain.\n */\nconst SOLANA_ASSOCIATED_TOKEN_PROGRAM = 'ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL';\n\n/**\n * Legacy SPL Token program address.\n *\n * Used to detect close-account instructions that may refund lamports\n * from temporary token accounts back to the sender.\n */\nconst SOLANA_SPL_TOKEN_PROGRAM = 'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA';\n\n/**\n * SPL Token-2022 program address.\n *\n * Used alongside the legacy token program for close-account detection\n * when computing expected refundable lamports.\n */\nconst SOLANA_SPL_TOKEN_2022_PROGRAM = 'TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb';\n\n/**\n * SPL token account size for rent-exemption calculations.\n *\n * Used with `getMinimumBalanceForRentExemption` to estimate ATA\n * creation funding requirements.\n */\nconst SPL_TOKEN_ACCOUNT_SIZE_BYTES = 165n;\nconst SOLANA_BASE_FEE_PER_SIGNATURE_LAMPORTS = 5_000n;\nconst MICRO_LAMPORTS_PER_LAMPORT = 1_000_000n;\n\nexport interface EstimateNativeFeeFactoryConfig {\n apiOptions: ApiOptions;\n appId: string;\n}\n\ninterface CreatedRentAccount {\n address: string;\n kind: 'ata' | 'system';\n lamports: bigint;\n refunded: boolean;\n}\n\ninterface SolanaRentEstimate {\n createdAccounts: CreatedRentAccount[];\n rentFee: bigint;\n rentFeeNet: bigint;\n refundable: bigint;\n}\n\ninterface CompiledMessageAddressTableLookup {\n accountKey: string;\n writableIndexes: readonly number[];\n readonlyIndexes: readonly number[];\n}\n\ninterface LookupTableAddresses {\n writableAddresses: string[];\n readonlyAddresses: string[];\n}\n\nexport function estimateNativeFeeFactory(config: EstimateNativeFeeFactoryConfig): TransferService['estimateNativeFee'] {\n return async (quote, options) => {\n // Either the source chain is EVM or Solana, we need to call different functions that\n // calculate the native fee depending on the chain kind.\n\n if (isEvmNamespace(quote.sourceChain.chainId)) {\n return await _estimateNativeFeeEvm(quote, options, config);\n }\n\n if (isSolanaNamespace(quote.sourceChain.chainId)) {\n return await _estimateNativeFeeSolana(quote, options, config);\n }\n\n throw new InvalidParamsError(\n ErrorReason.INVALID_PARAMS,\n `Unsupported source chain namespace for estimateNativeFee: ${quote.sourceChain.chainId}`,\n );\n };\n}\n\n/** @internal */\nexport async function _estimateNativeFeeEvm(\n quote: Quote,\n options: EstimateNativeFeeOptions | undefined,\n { apiOptions, appId }: EstimateNativeFeeFactoryConfig,\n): Promise<NativeFeeEstimate> {\n if (!isEvmAddress(quote.fromAddress)) {\n throw new InvalidParamsError(ErrorReason.INVALID_PARAMS, `Invalid fromAddress: ${quote.fromAddress}`);\n }\n\n const sourceClient = getEvmClientForChain({ chain: quote.sourceChain });\n\n const assetInAddressString = assetToAddressString(quote.assetIn, quote.sourceChain.chainId);\n\n if (!isEvmAddress(assetInAddressString)) {\n throw new SdkError(ErrorReason.INVALID_PARAMS, ErrorCode.INVALID_PARAMS, {\n details: `assetIn address is not a valid EVM address. Can not call estimateGas.`,\n });\n }\n\n const isAssetInNative = isTokenAddressNative(assetInAddressString);\n const isCrossChainSwap = quote.sourceChain.chainId.toLowerCase() !== quote.targetChain.chainId.toLowerCase();\n\n let allowanceApprovalGas = 0n;\n if (!isAssetInNative) {\n // Check if approval is needed, and if so, calculate the gas cost for the approval.\n let allowance: bigint;\n\n const { address: spenderAddress } = await markrGetSpenderAddress(apiOptions, {\n chainId: caip2ToEip155ChainId(quote.sourceChain.chainId),\n crossChainSwap: isCrossChainSwap,\n quoteId: quote.id,\n });\n\n try {\n allowance = await sourceClient.readContract({\n address: assetInAddressString,\n abi: erc20Abi,\n functionName: 'allowance',\n args: [quote.fromAddress, spenderAddress],\n });\n } catch (error) {\n throw new SdkError('Error during allowance check', ErrorCode.VIEM_ERROR, {\n cause: error,\n details: 'Failed to read ERC20 allowance for Markr spender.',\n });\n }\n\n const approvalNeeded = allowance < quote.amountIn;\n\n if (approvalNeeded) {\n try {\n allowanceApprovalGas = await sourceClient.estimateGas({\n account: quote.fromAddress,\n to: assetInAddressString,\n data: encodeFunctionData({\n abi: erc20Abi,\n functionName: 'approve',\n args: [spenderAddress, quote.amountIn],\n }),\n value: 0n,\n });\n } catch (error) {\n throw new SdkError('Error during gas estimation', ErrorCode.VIEM_ERROR, {\n cause: error,\n details: 'Failed to estimate gas for ERC20 approval transaction.',\n });\n }\n }\n }\n\n const allowanceApprovalIsNeeded = allowanceApprovalGas > 0n;\n\n const minAmountOut = calculateMarkrMinimumAmountOut({\n amountOut: quote.amountOut,\n assetOut: quote.assetOut,\n slippageBps: quote.slippageBps,\n });\n\n const swap = await markrSwap(apiOptions, {\n amountIn: quote.amountIn.toString(),\n appId,\n minAmountOut: minAmountOut.toString(),\n tokenIn: assetInAddressString,\n tokenOut: assetToAddressString(quote.assetOut, quote.targetChain.chainId),\n uuid: quote.id,\n });\n\n if (!isEvmSwapResponse(swap)) {\n // Should hopefully never happen.\n throw new SdkError(ErrorReason.CHAIN_NOT_SUPPORTED, ErrorCode.INVALID_PARAMS, {\n details: `Received non-EVM swap response from Markr. Expected EVM transaction data for gas estimation, but got response with chainType ${swap.chainType}.`,\n });\n }\n\n let gasWithMargin = 0n;\n\n // If an approval is needed, we can't call `eth_estimateGas` on the\n // swap transaction because no actual allowance approval was performed yet,\n // so a gas estimation would fail due to needing allowance.\n //\n // So if an allowance approval is needed, we fall back to some other logic for\n // estimating the swap tx gas.\n if (allowanceApprovalIsNeeded) {\n // Attempt to use the Markr provided `gasEstimate` first if available.\n // Otherwise we attempt to get the gas from the `fees` component that are applicable\n // to the source chain. If neither of those are available, we fall back to a hardcoded value.\n if (quote.gasEstimate) {\n gasWithMargin = applyFeeUnitsBpsMargin(quote.gasEstimate, options?.feeUnitsMarginBps);\n } else {\n const sourceGasFee = quote.fees\n .filter((fee) => fee.type === 'gas' && fee.chainId === quote.sourceChain.chainId)\n .reduce((acc, fee) => acc + fee.amount, 0n);\n\n gasWithMargin = applyFeeUnitsBpsMargin(\n sourceGasFee || EVM_SWAP_FALLBACK_GAS_ESTIMATE,\n options?.feeUnitsMarginBps,\n );\n }\n } else {\n gasWithMargin = await _estimateGasFromSwapResponse({\n crossChain: isCrossChainSwap,\n feeUnitsMarginBps: options?.feeUnitsMarginBps,\n fromAddress: quote.fromAddress,\n sourceClient,\n swap,\n });\n }\n\n const fees = await estimateEvmFeesPerGas(sourceClient, quote.sourceChain, options?.overrides?.feeRateTier);\n\n const maxFeePerGas = options?.overrides?.maxFeePerGas ?? fees.maxFeePerGas;\n const maxPriorityFeePerGas = options?.overrides?.maxPriorityFeePerGas ?? fees.maxPriorityFeePerGas;\n\n const totalFee =\n (gasWithMargin + applyFeeUnitsBpsMargin(allowanceApprovalGas, options?.feeUnitsMarginBps)) * maxFeePerGas;\n\n return {\n asset: quote.sourceChain.networkToken,\n totalFee,\n totalUpfrontFee: totalFee,\n meta: {\n approvalFee: allowanceApprovalIsNeeded\n ? applyFeeUnitsBpsMargin(allowanceApprovalGas, options?.feeUnitsMarginBps) * maxFeePerGas\n : undefined,\n maxFeePerGas,\n maxPriorityFeePerGas,\n },\n };\n}\n\n/** @internal */\nexport async function _estimateNativeFeeSolana(\n quote: Quote,\n options: EstimateNativeFeeOptions | undefined,\n { apiOptions, appId }: EstimateNativeFeeFactoryConfig,\n): Promise<NativeFeeEstimate> {\n const assetInAddressString = assetToAddressString(quote.assetIn, quote.sourceChain.chainId);\n\n if (!isSolanaAddress(assetInAddressString)) {\n throw new SdkError(ErrorReason.INVALID_PARAMS, ErrorCode.INVALID_PARAMS, {\n details: `assetIn address is not a valid Solana address. Can not call estimateGas.`,\n });\n }\n\n const minAmountOut = calculateMarkrMinimumAmountOut({\n amountOut: quote.amountOut,\n assetOut: quote.assetOut,\n slippageBps: quote.slippageBps,\n });\n\n const swap = await markrSwap(apiOptions, {\n amountIn: quote.amountIn.toString(),\n appId,\n minAmountOut: minAmountOut.toString(),\n tokenIn: assetInAddressString,\n tokenOut: assetToAddressString(quote.assetOut, quote.targetChain.chainId),\n userPublicKey: quote.fromAddress,\n uuid: quote.id,\n });\n\n if (!isSolanaSwapResponse(swap)) {\n // Should hopefully never happen.\n throw new SdkError(ErrorReason.CHAIN_NOT_SUPPORTED, ErrorCode.INVALID_PARAMS, {\n details: 'Received non-Solana swap response from Markr. Expected Solana transaction data for fee estimation.',\n });\n }\n\n const rpc = getSolanaRpcForChain({ chain: quote.sourceChain });\n const refreshedSwapTransactionBase64 = await refreshSolanaSwapTransactionBlockhash(swap.swapTransaction, rpc);\n const decodedTransaction = decodeSolanaTransactionBase64(refreshedSwapTransactionBase64);\n const transactionMessageBytes = decodedTransaction.messageBytes;\n const signatureCount = _getSignatureCount(decodedTransaction);\n const baseFeeLamports = signatureCount * SOLANA_BASE_FEE_PER_SIGNATURE_LAMPORTS;\n const { computeUnitLimit, computeUnitPriceMicroLamports } = _getComputeBudgetPriceAndLimit(transactionMessageBytes);\n const priorityFee = _calculatePriorityFeeLamports(computeUnitLimit, computeUnitPriceMicroLamports);\n const transactionFee = baseFeeLamports + priorityFee;\n const transactionFeeWithMargin = applyFeeUnitsBpsMargin(transactionFee, options?.feeUnitsMarginBps);\n\n const sim = await rpc\n .simulateTransaction(refreshedSwapTransactionBase64, {\n encoding: 'base64',\n sigVerify: false,\n replaceRecentBlockhash: true, // Helps prevent block hash expiration issues during simulation.\n })\n .send();\n\n const unitsConsumed = sim.value.unitsConsumed ?? 0n;\n const rentEstimate = await _estimateRentFeesFromTransaction({\n fromAddress: quote.fromAddress,\n rpc,\n transaction: decodedTransaction,\n });\n const totalLamports = transactionFeeWithMargin + rentEstimate.rentFee;\n\n return {\n asset: quote.sourceChain.networkToken,\n totalFee: totalLamports,\n totalUpfrontFee: totalLamports,\n refundable: rentEstimate.refundable,\n meta: {\n baseFee: baseFeeLamports,\n priorityFee,\n transactionFee,\n transactionFeeWithMargin,\n computeUnitLimit,\n computeUnitPriceMicroLamports,\n unitsConsumed,\n createdAccounts: rentEstimate.createdAccounts,\n rentFee: rentEstimate.rentFee,\n rentFeeNet: rentEstimate.rentFeeNet,\n },\n };\n}\n\n/** @internal */\nexport async function _estimateGasFromSwapResponse({\n crossChain,\n feeUnitsMarginBps,\n fromAddress,\n sourceClient,\n swap,\n}: {\n crossChain: boolean;\n feeUnitsMarginBps?: number;\n fromAddress: EvmAddress;\n sourceClient: ReturnType<typeof getEvmClientForChain>;\n swap: WrappedSwapTransactionResponse;\n}): Promise<bigint> {\n try {\n const gasEstimate = await sourceClient.estimateGas({\n account: fromAddress,\n to: swap.to,\n data: swap.data,\n value: swap.value,\n });\n\n return applyFeeUnitsBpsMargin(gasEstimate, feeUnitsMarginBps);\n } catch (err) {\n let details = 'Failed to estimate gas for Markr swap transaction.';\n\n try {\n const markrSwapWrapperAbi = await getMarkrSwapWrapperAbi(crossChain);\n const decodedRevert = decodeMarkrRevertError(markrSwapWrapperAbi, err);\n\n if (decodedRevert) {\n details = `${details} Markr revert: ${decodedRevert}.`;\n }\n } catch {\n // Keep the base details message if revert decoding fails.\n }\n\n throw new SdkError('Error during gas estimation', ErrorCode.VIEM_ERROR, {\n cause: err,\n details,\n });\n }\n}\n\nfunction _getComputeBudgetPriceAndLimit(transactionMessageBytes: TransactionMessageBytes): {\n computeUnitLimit: bigint;\n computeUnitPriceMicroLamports: bigint;\n} {\n const compiledTxMessage = getCompiledTransactionMessageDecoder().decode(transactionMessageBytes);\n let computeUnitLimit = 0n;\n let computeUnitPriceMicroLamports = 0n;\n\n for (const instruction of compiledTxMessage.instructions) {\n const programAddress = compiledTxMessage.staticAccounts[instruction.programAddressIndex];\n if (programAddress !== SOLANA_COMPUTE_BUDGET_PROGRAM) {\n continue;\n }\n\n const data = instruction.data;\n\n if (!data || data.length < 1) {\n continue;\n }\n\n const discriminator = data[0];\n const view = new DataView(data.buffer, data.byteOffset, data.byteLength);\n\n // ComputeBudget::SetComputeUnitLimit(u32)\n if (discriminator === 2 && data.length >= 1 + 4) {\n computeUnitLimit = BigInt(view.getUint32(1, true));\n continue;\n }\n\n // ComputeBudget::SetComputeUnitPrice(u64)\n if (discriminator === 3 && data.length >= 1 + 8) {\n computeUnitPriceMicroLamports = view.getBigUint64(1, true);\n }\n }\n\n return {\n computeUnitLimit,\n computeUnitPriceMicroLamports,\n };\n}\n\nfunction _calculatePriorityFeeLamports(computeUnitLimit: bigint, computeUnitPriceMicroLamports: bigint): bigint {\n if (computeUnitLimit <= 0n || computeUnitPriceMicroLamports <= 0n) {\n return 0n;\n }\n\n return (\n (computeUnitLimit * computeUnitPriceMicroLamports + (MICRO_LAMPORTS_PER_LAMPORT - 1n)) / MICRO_LAMPORTS_PER_LAMPORT\n );\n}\n\nfunction _getSignatureCount(transaction: Transaction): bigint {\n const signatures = (transaction as { signatures?: unknown }).signatures;\n\n if (Array.isArray(signatures)) {\n return BigInt(signatures.length);\n }\n\n if (signatures && typeof signatures === 'object') {\n return BigInt(Object.keys(signatures).length);\n }\n\n return 0n;\n}\n\nasync function _estimateRentFeesFromTransaction({\n fromAddress,\n rpc,\n transaction,\n}: {\n fromAddress: string;\n rpc: Rpc<SolanaRpcApi>;\n transaction: Transaction;\n}): Promise<SolanaRentEstimate> {\n const compiledMessage: CompiledTransactionMessage = getCompiledTransactionMessageDecoder().decode(\n transaction.messageBytes,\n );\n const allAccounts = await _resolveAllTransactionAccounts(compiledMessage, rpc);\n\n const createdAccounts = new Map<string, Omit<CreatedRentAccount, 'refunded'>>();\n const closedAccountDestinationByAddress = new Map<string, string>();\n\n let ataRentLamports: bigint | undefined;\n\n for (const instruction of compiledMessage.instructions) {\n const programAddress = allAccounts[instruction.programAddressIndex];\n\n if (programAddress === SOLANA_SYSTEM_PROGRAM) {\n const createdAccountAddress = _getInstructionAccountAddress(allAccounts, instruction, 1);\n const lamports = _getSystemCreateAccountLamports(instruction.data);\n if (createdAccountAddress && lamports > 0n) {\n createdAccounts.set(createdAccountAddress, {\n address: createdAccountAddress,\n kind: 'system',\n lamports,\n });\n }\n continue;\n }\n\n if (programAddress === SOLANA_ASSOCIATED_TOKEN_PROGRAM) {\n const discriminator = instruction.data?.[0];\n const isAssociatedTokenCreateInstruction =\n discriminator === undefined || discriminator === 0 || discriminator === 1;\n\n if (!isAssociatedTokenCreateInstruction) {\n continue;\n }\n\n const associatedTokenAddress = _getInstructionAccountAddress(allAccounts, instruction, 1);\n if (!associatedTokenAddress || !isSolanaAddress(associatedTokenAddress)) {\n continue;\n }\n\n const accountInfo = await rpc.getAccountInfo(associatedTokenAddress).send();\n if (accountInfo.value) {\n continue;\n }\n\n if (ataRentLamports === undefined) {\n ataRentLamports = await rpc.getMinimumBalanceForRentExemption(SPL_TOKEN_ACCOUNT_SIZE_BYTES).send();\n }\n\n createdAccounts.set(associatedTokenAddress, {\n address: associatedTokenAddress,\n kind: 'ata',\n lamports: ataRentLamports,\n });\n\n continue;\n }\n\n const isSplTokenProgram =\n programAddress === SOLANA_SPL_TOKEN_PROGRAM || programAddress === SOLANA_SPL_TOKEN_2022_PROGRAM;\n if (!isSplTokenProgram) {\n continue;\n }\n\n const discriminator = instruction.data?.[0];\n if (discriminator !== 9) {\n continue;\n }\n\n const closedAccountAddress = _getInstructionAccountAddress(allAccounts, instruction, 0);\n const refundDestinationAddress = _getInstructionAccountAddress(allAccounts, instruction, 1);\n\n if (closedAccountAddress && refundDestinationAddress) {\n closedAccountDestinationByAddress.set(closedAccountAddress, refundDestinationAddress);\n }\n }\n\n const normalizedFromAddress = isSolanaAddress(fromAddress) ? fromAddress : undefined;\n const createdAccountEntries = Array.from(createdAccounts.values()).map((createdAccount) => {\n const refundDestinationAddress = closedAccountDestinationByAddress.get(createdAccount.address);\n const refunded = normalizedFromAddress !== undefined && refundDestinationAddress === normalizedFromAddress;\n\n return {\n ...createdAccount,\n refunded,\n } satisfies CreatedRentAccount;\n });\n\n const rentLamportsUpfront = createdAccountEntries.reduce((acc, account) => acc + account.lamports, 0n);\n const refundedLamports = createdAccountEntries.reduce(\n (acc, account) => acc + (account.refunded ? account.lamports : 0n),\n 0n,\n );\n const rentFeeNet = rentLamportsUpfront - refundedLamports;\n\n return {\n createdAccounts: createdAccountEntries,\n rentFee: rentLamportsUpfront,\n rentFeeNet,\n refundable: refundedLamports,\n };\n}\n\nfunction _isObject(value: unknown): value is object {\n return !!value && typeof value === 'object';\n}\n\nfunction _getObjectProperty(value: unknown, key: string): unknown {\n if (!_isObject(value)) {\n return undefined;\n }\n\n return Reflect.get(value, key);\n}\n\nfunction _toNumberIndexArray(value: unknown): number[] | undefined {\n if (value instanceof Uint8Array) {\n return Array.from(value);\n }\n\n if (Array.isArray(value) && value.every((entry) => Number.isInteger(entry) && entry >= 0)) {\n return value;\n }\n\n return undefined;\n}\n\nfunction _getCompiledMessageAddressTableLookups(\n compiledMessage: CompiledTransactionMessage,\n): CompiledMessageAddressTableLookup[] {\n const addressTableLookupsRaw = _getObjectProperty(compiledMessage, 'addressTableLookups');\n if (!Array.isArray(addressTableLookupsRaw)) {\n return [];\n }\n\n const lookups: CompiledMessageAddressTableLookup[] = [];\n\n for (const lookupRaw of addressTableLookupsRaw) {\n const accountKey = _getObjectProperty(lookupRaw, 'accountKey');\n const writableIndexes = _toNumberIndexArray(_getObjectProperty(lookupRaw, 'writableIndexes'));\n const readonlyIndexes = _toNumberIndexArray(_getObjectProperty(lookupRaw, 'readonlyIndexes'));\n\n if (typeof accountKey !== 'string' || !writableIndexes || !readonlyIndexes) {\n continue;\n }\n\n lookups.push({\n accountKey,\n writableIndexes,\n readonlyIndexes,\n });\n }\n\n return lookups;\n}\n\nfunction _extractLookupTableAddresses(response: unknown): string[] {\n const lookupTableValue = _getObjectProperty(response, 'value');\n if (!_isObject(lookupTableValue)) {\n return [];\n }\n\n const addresses = _getObjectProperty(lookupTableValue, 'addresses');\n if (!Array.isArray(addresses)) {\n return [];\n }\n\n return addresses.filter((entry): entry is string => typeof entry === 'string');\n}\n\nasync function _loadLookupTableAddresses(\n compiledMessage: CompiledTransactionMessage,\n rpc: Rpc<SolanaRpcApi>,\n): Promise<LookupTableAddresses> {\n const writableAddresses: string[] = [];\n const readonlyAddresses: string[] = [];\n const addressTableLookups = _getCompiledMessageAddressTableLookups(compiledMessage);\n\n for (const lookup of addressTableLookups) {\n if (!isSolanaAddress(lookup.accountKey)) {\n continue;\n }\n\n const tableAddresses = await _fetchAddressLookupTableAddresses(rpc, lookup.accountKey);\n\n for (const index of lookup.writableIndexes) {\n const address = tableAddresses[index];\n if (address) {\n writableAddresses.push(address);\n }\n }\n\n for (const index of lookup.readonlyIndexes) {\n const address = tableAddresses[index];\n if (address) {\n readonlyAddresses.push(address);\n }\n }\n }\n\n return {\n writableAddresses,\n readonlyAddresses,\n };\n}\n\nasync function _fetchAddressLookupTableAddresses(rpc: Rpc<SolanaRpcApi>, accountKey: SolanaAddress): Promise<string[]> {\n const getAddressLookupTable = _getObjectProperty(rpc, 'getAddressLookupTable');\n if (typeof getAddressLookupTable !== 'function') {\n return [];\n }\n\n const request = Reflect.apply(getAddressLookupTable, rpc, [accountKey]);\n const send = _getObjectProperty(request, 'send');\n\n if (typeof send !== 'function') {\n return [];\n }\n\n const response = await Reflect.apply(send, request, []);\n return _extractLookupTableAddresses(response);\n}\n\nasync function _resolveAllTransactionAccounts(\n compiledMessage: CompiledTransactionMessage,\n rpc: Rpc<SolanaRpcApi>,\n): Promise<string[]> {\n const { writableAddresses, readonlyAddresses } = await _loadLookupTableAddresses(compiledMessage, rpc);\n\n return [...compiledMessage.staticAccounts, ...writableAddresses, ...readonlyAddresses];\n}\n\n/**\n * Resolves an account address used by a compiled instruction.\n *\n * Compiled instructions reference accounts by index into the transaction\n * message account list. For versioned transactions this includes static\n * accounts followed by writable and readonly addresses loaded from lookup\n * tables. This helper converts an instruction-local `accountIndex` into the\n * corresponding account address.\n *\n * Returns `undefined` when the instruction does not provide an account at the\n * requested index.\n */\nfunction _getInstructionAccountAddress(\n allAccounts: readonly string[],\n instruction: CompiledTransactionMessage['instructions'][number],\n accountIndex: number,\n): string | undefined {\n const messageAccountIndex = instruction.accountIndices?.[accountIndex];\n\n if (messageAccountIndex === undefined) {\n return undefined;\n }\n\n return allAccounts[messageAccountIndex];\n}\n\n/**\n * Extracts lamports from System Program account-creation instruction data.\n *\n * Supported instruction layouts:\n * - `CreateAccount` (`u32 discriminator = 0`): lamports at byte offset `4`.\n * - `CreateAccountWithSeed` (`u32 discriminator = 3`): lamports are located\n * after the variable-length seed field.\n *\n * Returns `0n` when data is missing, malformed, or not one of the supported\n * System Program create-account variants.\n */\nfunction _getSystemCreateAccountLamports(data: CompiledTransactionMessage['instructions'][number]['data']): bigint {\n if (!data || data.length < 12) {\n return 0n;\n }\n\n const view = new DataView(data.buffer, data.byteOffset, data.byteLength);\n const instructionDiscriminator = view.getUint32(0, true);\n\n if (instructionDiscriminator === 0 && data.length >= 12) {\n return view.getBigUint64(4, true);\n }\n\n if (instructionDiscriminator === 3 && data.length >= 44) {\n const seedLenOffset = 36;\n const seedLength = Number(view.getBigUint64(seedLenOffset, true));\n if (!Number.isSafeInteger(seedLength) || seedLength < 0) {\n return 0n;\n }\n\n const lamportsOffset = seedLenOffset + 8 + seedLength;\n if (lamportsOffset + 8 > data.length) {\n return 0n;\n }\n\n return new DataView(data.buffer, data.byteOffset + lamportsOffset, 8).getBigUint64(0, true);\n }\n\n return 0n;\n}\n"],"mappings":"0YAuCA,MAkDM,EAA6B,SAgCnC,SAAgB,EAAyB,EAA8E,CACrH,OAAO,MAAO,EAAO,IAAY,CAI/B,GAAIA,EAAAA,eAAe,EAAM,YAAY,QAAQ,CAC3C,OAAO,MAAM,EAAsB,EAAO,EAAS,EAAO,CAG5D,GAAIC,EAAAA,kBAAkB,EAAM,YAAY,QAAQ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B,CAC9E,GAAI,OAAO,GAA0B,WACnC,MAAO,EAAE,CAGX,IAAM,EAAU,QAAQ,MAAM,EAAuB,EAAK,CAAC,EAAW,CAAC,CACjE,EAAO,EAAmB,EAAS,OAAO,CAOhD,OALI,OAAO,GAAS,WAKb,EADU,MAAM,QAAQ,MAAM,EAAM,EAAS,EAAE,CAAC,CACV,CAJpC,EAAE,CAOb,eAAe,EACb,EACA,EACmB,CACnB,GAAM,CAAE,oBAAmB,qBAAsB,MAAM,EAA0B,EAAiB,EAAI,CAEtG,MAAO,CAAC,GAAG,EAAgB,eAAgB,GAAG,EAAmB,GAAG,EAAkB,CAexF,SAAS,EACP,EACA,EACA,EACoB,CACpB,IAAM,EAAsB,EAAY,iBAAiB,GAErD,OAAwB,IAAA,GAI5B,OAAO,EAAY,GAcrB,SAAS,EAAgC,EAA0E,CACjH,GAAI,CAAC,GAAQ,EAAK,OAAS,GACzB,OAAO,GAGT,IAAM,EAAO,IAAI,SAAS,EAAK,OAAQ,EAAK,WAAY,EAAK,WAAW,CAClE,EAA2B,EAAK,UAAU,EAAG,GAAK,CAExD,GAAI,IAA6B,GAAK,EAAK,QAAU,GACnD,OAAO,EAAK,aAAa,EAAG,GAAK,CAGnC,GAAI,IAA6B,GAAK,EAAK,QAAU,GAAI,CACvD,IACM,EAAa,OAAO,EAAK,aAAa,GAAe,GAAK,CAAC,CACjE,GAAI,CAAC,OAAO,cAAc,EAAW,EAAI,EAAa,EACpD,OAAO,GAGT,IAAM,EAAiB,GAAoB,EAK3C,OAJI,EAAiB,EAAI,EAAK,OACrB,GAGF,IAAI,SAAS,EAAK,OAAQ,EAAK,WAAa,EAAgB,EAAE,CAAC,aAAa,EAAG,GAAK,CAG7F,OAAO"}
@@ -1,2 +1,2 @@
1
- import{ErrorCode as e,ErrorReason as t,InvalidParamsError as n,SdkError as r}from"../../../errors.js";import{caip2ToEip155ChainId as i}from"../../../utils/caip.js";import{isEvmNamespace as a,isSolanaNamespace as o}from"../../../_utils/chain.js";import{applyFeeUnitsBpsMargin as s,getEvmClientForChain as c,getSolanaRpcForChain as l}from"../../_utils.js";import{estimateEvmFeesPerGas as u}from"../../_evm-gas.js";import{markrGetSpenderAddress as d,markrSwap as f}from"../_api.js";import{assetToAddressString as p,calculateMarkrMinimumAmountOut as m,decodeMarkrRevertError as h,getMarkrSwapWrapperAbi as g,isTokenAddressNative as _}from"../_utils.js";import{isEvmSwapResponse as v,isSolanaSwapResponse as y}from"../_type-guards.js";import{encodeFunctionData as b,erc20Abi as x,isAddress as S}from"viem";import{compileTransaction as C,decompileTransactionMessageFetchingLookupTables as w,getBase64EncodedWireTransaction as T,getCompiledTransactionMessageDecoder as E,getTransactionDecoder as D,isAddress as O,setTransactionMessageLifetimeUsingBlockhash as k}from"@solana/kit";function A(e){return async(r,i)=>{if(a(r.sourceChain.chainId))return await j(r,i,e);if(o(r.sourceChain.chainId))return await M(r,i,e);throw new n(t.INVALID_PARAMS,`Unsupported source chain namespace for estimateNativeFee: ${r.sourceChain.chainId}`)}}async function j(a,o,{apiOptions:l,appId:h}){if(!S(a.fromAddress))throw new n(t.INVALID_PARAMS,`Invalid fromAddress: ${a.fromAddress}`);let g=c({chain:a.sourceChain}),y=p(a.assetIn,a.sourceChain.chainId);if(!S(y))throw new r(t.INVALID_PARAMS,e.INVALID_PARAMS,{details:`assetIn address is not a valid EVM address. Can not call estimateGas.`});let C=_(y),w=a.sourceChain.chainId.toLowerCase()!==a.targetChain.chainId.toLowerCase(),T=0n;if(!C){let t,{address:n}=await d(l,{chainId:i(a.sourceChain.chainId),crossChainSwap:w,quoteId:a.id});try{t=await g.readContract({address:y,abi:x,functionName:`allowance`,args:[a.fromAddress,n]})}catch(t){throw new r(`Error during allowance check`,e.VIEM_ERROR,{cause:t,details:`Failed to read ERC20 allowance for Markr spender.`})}if(t<a.amountIn)try{T=await g.estimateGas({account:a.fromAddress,to:y,data:b({abi:x,functionName:`approve`,args:[n,a.amountIn]}),value:0n})}catch(t){throw new r(`Error during gas estimation`,e.VIEM_ERROR,{cause:t,details:`Failed to estimate gas for ERC20 approval transaction.`})}}let E=T>0n,D=m({amountOut:a.amountOut,assetOut:a.assetOut,slippageBps:a.slippageBps}),O=await f(l,{amountIn:a.amountIn.toString(),appId:h,minAmountOut:D.toString(),tokenIn:y,tokenOut:p(a.assetOut,a.targetChain.chainId),uuid:a.id});if(!v(O))throw new r(t.CHAIN_NOT_SUPPORTED,e.INVALID_PARAMS,{details:`Received non-EVM swap response from Markr. Expected EVM transaction data for gas estimation, but got response with chainType ${O.chainType}.`});let k=0n;k=E?a.gasEstimate?s(a.gasEstimate,o?.feeUnitsMarginBps):s(a.fees.filter(e=>e.type===`gas`&&e.chainId===a.sourceChain.chainId).reduce((e,t)=>e+t.amount,0n)||700000n,o?.feeUnitsMarginBps):await N({crossChain:w,feeUnitsMarginBps:o?.feeUnitsMarginBps,fromAddress:a.fromAddress,sourceClient:g,swap:O});let A=await u(g,a.sourceChain,o?.overrides?.feeRateTier),j=o?.overrides?.maxFeePerGas??A.maxFeePerGas,M=o?.overrides?.maxPriorityFeePerGas??A.maxPriorityFeePerGas,P=(k+s(T,o?.feeUnitsMarginBps))*j;return{asset:a.sourceChain.networkToken,totalFee:P,meta:{approvalFee:E?s(T,o?.feeUnitsMarginBps)*j:void 0,maxFeePerGas:j,maxPriorityFeePerGas:M}}}async function M(n,i,{apiOptions:a,appId:o}){let c=p(n.assetIn,n.sourceChain.chainId);if(!O(c))throw new r(t.INVALID_PARAMS,e.INVALID_PARAMS,{details:`assetIn address is not a valid Solana address. Can not call estimateGas.`});let u=m({amountOut:n.amountOut,assetOut:n.assetOut,slippageBps:n.slippageBps}),d=await f(a,{amountIn:n.amountIn.toString(),appId:o,minAmountOut:u.toString(),tokenIn:c,tokenOut:p(n.assetOut,n.targetChain.chainId),userPublicKey:n.fromAddress,uuid:n.id});if(!y(d))throw new r(t.CHAIN_NOT_SUPPORTED,e.INVALID_PARAMS,{details:`Received non-Solana swap response from Markr. Expected Solana transaction data for fee estimation.`});let h=l({chain:n.sourceChain}),g=P(d.swapTransaction),_=D().decode(g).messageBytes,v=d.swapTransaction,b=(await h.getFeeForMessage(F(_)).send()).value;if(!b){let e=await h.getLatestBlockhash().send(),t=await w(E().decode(_),h),n=C(k(e.value,t));_=n.messageBytes,v=T(n),b=(await h.getFeeForMessage(F(_)).send()).value}if(!b)throw new r(`Failed to get fee for message after refreshing blockhash.`,e.TIMEOUT,{details:`Unable to estimate Solana base fee because the transaction message remained invalid.`});let x=s((await h.simulateTransaction(v,{encoding:`base64`,sigVerify:!1,replaceRecentBlockhash:!0}).send()).value.unitsConsumed??0n,i?.feeUnitsMarginBps),S=I(_),A=x*S/1000000n,j=await L({fromAddress:n.fromAddress,rpc:h,transactionMessageBytes:_}),M=b+A+j.rentFee;return{asset:n.sourceChain.networkToken,totalFee:M,refundable:j.refundable,meta:{baseFee:b,priorityFee:A,computeUnitPriceMicroLamports:S,unitsConsumed:x,createdAccounts:j.createdAccounts,rentFee:j.rentFee,rentFeeNet:j.rentFeeNet}}}async function N({crossChain:t,feeUnitsMarginBps:n,fromAddress:i,sourceClient:a,swap:o}){try{return s(await a.estimateGas({account:i,to:o.to,data:o.data,value:o.value}),n)}catch(n){let i=`Failed to estimate gas for Markr swap transaction.`;try{let e=h(await g(t),n);e&&(i=`${i} Markr revert: ${e}.`)}catch{}throw new r(`Error during gas estimation`,e.VIEM_ERROR,{cause:n,details:i})}}function P(e){return Uint8Array.from(atob(e),e=>e.charCodeAt(0))}function F(e){let t=``;for(let n of e)t+=String.fromCharCode(n);return btoa(t)}function I(e){let t=E().decode(e);for(let e of t.instructions){if(t.staticAccounts[e.programAddressIndex]!==`ComputeBudget111111111111111111111111111111`)continue;let n=e.data;if(!(!n||n.length<9)&&n[0]===3)return new DataView(n.buffer,n.byteOffset,n.byteLength).getBigUint64(1,!0)}return 0n}async function L({fromAddress:e,rpc:t,transactionMessageBytes:n}){let r=E().decode(n),i=new Map,a=new Map,o;for(let e of r.instructions){let n=r.staticAccounts[e.programAddressIndex];if(n===`11111111111111111111111111111111`){let t=R(r,e,1),n=z(e.data);t&&n>0n&&i.set(t,{address:t,kind:`system`,lamports:n});continue}if(n===`ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL`){let n=e.data?.[0];if(!(n===void 0||n===0||n===1))continue;let a=R(r,e,1);if(!a||(await t.getAccountInfo(a).send()).value)continue;o===void 0&&(o=await t.getMinimumBalanceForRentExemption(165n).send()),i.set(a,{address:a,kind:`ata`,lamports:o});continue}if(!(n===`TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA`||n===`TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb`)||e.data?.[0]!==9)continue;let s=R(r,e,0),c=R(r,e,1);s&&c&&a.set(s,c)}let s=O(e)?e:void 0,c=Array.from(i.values()).map(e=>{let t=a.get(e.address),n=s!==void 0&&t===s;return{...e,refunded:n}}),l=c.reduce((e,t)=>e+t.lamports,0n),u=c.reduce((e,t)=>e+(t.refunded?t.lamports:0n),0n);return{createdAccounts:c,rentFee:l,rentFeeNet:l-u,refundable:u}}function R(e,t,n){let r=t.accountIndices?.[n];if(r!==void 0)return e.staticAccounts[r]}function z(e){if(!e||e.length<12)return 0n;let t=new DataView(e.buffer,e.byteOffset,e.byteLength),n=t.getUint32(0,!0);if(n===0&&e.length>=12)return t.getBigUint64(4,!0);if(n===3&&e.length>=44){let n=Number(t.getBigUint64(36,!0));if(!Number.isSafeInteger(n)||n<0)return 0n;let r=44+n;return r+8>e.length?0n:new DataView(e.buffer,e.byteOffset+r,8).getBigUint64(0,!0)}return 0n}export{N as _estimateGasFromSwapResponse,A as estimateNativeFeeFactory};
1
+ import{ErrorCode as e,ErrorReason as t,InvalidParamsError as n,SdkError as r}from"../../../errors.js";import{caip2ToEip155ChainId as i}from"../../../utils/caip.js";import{isEvmNamespace as a,isSolanaNamespace as o}from"../../../_utils/chain.js";import{applyFeeUnitsBpsMargin as s,getEvmClientForChain as c,getSolanaRpcForChain as l}from"../../_utils.js";import{estimateEvmFeesPerGas as u}from"../../_evm-gas.js";import{markrGetSpenderAddress as d,markrSwap as f}from"../_api.js";import{assetToAddressString as p,calculateMarkrMinimumAmountOut as m,decodeMarkrRevertError as h,getMarkrSwapWrapperAbi as g,isTokenAddressNative as _}from"../_utils.js";import{isEvmSwapResponse as v,isSolanaSwapResponse as y}from"../_type-guards.js";import{decodeSolanaTransactionBase64 as b,refreshSolanaSwapTransactionBlockhash as x}from"../_solana-utils.js";import{encodeFunctionData as S,erc20Abi as C,isAddress as w}from"viem";import{getCompiledTransactionMessageDecoder as T,isAddress as E}from"@solana/kit";const D=1000000n;function O(e){return async(r,i)=>{if(a(r.sourceChain.chainId))return await k(r,i,e);if(o(r.sourceChain.chainId))return await A(r,i,e);throw new n(t.INVALID_PARAMS,`Unsupported source chain namespace for estimateNativeFee: ${r.sourceChain.chainId}`)}}async function k(a,o,{apiOptions:l,appId:h}){if(!w(a.fromAddress))throw new n(t.INVALID_PARAMS,`Invalid fromAddress: ${a.fromAddress}`);let g=c({chain:a.sourceChain}),y=p(a.assetIn,a.sourceChain.chainId);if(!w(y))throw new r(t.INVALID_PARAMS,e.INVALID_PARAMS,{details:`assetIn address is not a valid EVM address. Can not call estimateGas.`});let b=_(y),x=a.sourceChain.chainId.toLowerCase()!==a.targetChain.chainId.toLowerCase(),T=0n;if(!b){let t,{address:n}=await d(l,{chainId:i(a.sourceChain.chainId),crossChainSwap:x,quoteId:a.id});try{t=await g.readContract({address:y,abi:C,functionName:`allowance`,args:[a.fromAddress,n]})}catch(t){throw new r(`Error during allowance check`,e.VIEM_ERROR,{cause:t,details:`Failed to read ERC20 allowance for Markr spender.`})}if(t<a.amountIn)try{T=await g.estimateGas({account:a.fromAddress,to:y,data:S({abi:C,functionName:`approve`,args:[n,a.amountIn]}),value:0n})}catch(t){throw new r(`Error during gas estimation`,e.VIEM_ERROR,{cause:t,details:`Failed to estimate gas for ERC20 approval transaction.`})}}let E=T>0n,D=m({amountOut:a.amountOut,assetOut:a.assetOut,slippageBps:a.slippageBps}),O=await f(l,{amountIn:a.amountIn.toString(),appId:h,minAmountOut:D.toString(),tokenIn:y,tokenOut:p(a.assetOut,a.targetChain.chainId),uuid:a.id});if(!v(O))throw new r(t.CHAIN_NOT_SUPPORTED,e.INVALID_PARAMS,{details:`Received non-EVM swap response from Markr. Expected EVM transaction data for gas estimation, but got response with chainType ${O.chainType}.`});let k=0n;k=E?a.gasEstimate?s(a.gasEstimate,o?.feeUnitsMarginBps):s(a.fees.filter(e=>e.type===`gas`&&e.chainId===a.sourceChain.chainId).reduce((e,t)=>e+t.amount,0n)||700000n,o?.feeUnitsMarginBps):await j({crossChain:x,feeUnitsMarginBps:o?.feeUnitsMarginBps,fromAddress:a.fromAddress,sourceClient:g,swap:O});let A=await u(g,a.sourceChain,o?.overrides?.feeRateTier),M=o?.overrides?.maxFeePerGas??A.maxFeePerGas,N=o?.overrides?.maxPriorityFeePerGas??A.maxPriorityFeePerGas,P=(k+s(T,o?.feeUnitsMarginBps))*M;return{asset:a.sourceChain.networkToken,totalFee:P,totalUpfrontFee:P,meta:{approvalFee:E?s(T,o?.feeUnitsMarginBps)*M:void 0,maxFeePerGas:M,maxPriorityFeePerGas:N}}}async function A(n,i,{apiOptions:a,appId:o}){let c=p(n.assetIn,n.sourceChain.chainId);if(!E(c))throw new r(t.INVALID_PARAMS,e.INVALID_PARAMS,{details:`assetIn address is not a valid Solana address. Can not call estimateGas.`});let u=m({amountOut:n.amountOut,assetOut:n.assetOut,slippageBps:n.slippageBps}),d=await f(a,{amountIn:n.amountIn.toString(),appId:o,minAmountOut:u.toString(),tokenIn:c,tokenOut:p(n.assetOut,n.targetChain.chainId),userPublicKey:n.fromAddress,uuid:n.id});if(!y(d))throw new r(t.CHAIN_NOT_SUPPORTED,e.INVALID_PARAMS,{details:`Received non-Solana swap response from Markr. Expected Solana transaction data for fee estimation.`});let h=l({chain:n.sourceChain}),g=await x(d.swapTransaction,h),_=b(g),v=_.messageBytes,S=P(_)*5000n,{computeUnitLimit:C,computeUnitPriceMicroLamports:w}=M(v),T=N(C,w),D=S+T,O=s(D,i?.feeUnitsMarginBps),k=(await h.simulateTransaction(g,{encoding:`base64`,sigVerify:!1,replaceRecentBlockhash:!0}).send()).value.unitsConsumed??0n,A=await F({fromAddress:n.fromAddress,rpc:h,transaction:_}),j=O+A.rentFee;return{asset:n.sourceChain.networkToken,totalFee:j,totalUpfrontFee:j,refundable:A.refundable,meta:{baseFee:S,priorityFee:T,transactionFee:D,transactionFeeWithMargin:O,computeUnitLimit:C,computeUnitPriceMicroLamports:w,unitsConsumed:k,createdAccounts:A.createdAccounts,rentFee:A.rentFee,rentFeeNet:A.rentFeeNet}}}async function j({crossChain:t,feeUnitsMarginBps:n,fromAddress:i,sourceClient:a,swap:o}){try{return s(await a.estimateGas({account:i,to:o.to,data:o.data,value:o.value}),n)}catch(n){let i=`Failed to estimate gas for Markr swap transaction.`;try{let e=h(await g(t),n);e&&(i=`${i} Markr revert: ${e}.`)}catch{}throw new r(`Error during gas estimation`,e.VIEM_ERROR,{cause:n,details:i})}}function M(e){let t=T().decode(e),n=0n,r=0n;for(let e of t.instructions){if(t.staticAccounts[e.programAddressIndex]!==`ComputeBudget111111111111111111111111111111`)continue;let i=e.data;if(!i||i.length<1)continue;let a=i[0],o=new DataView(i.buffer,i.byteOffset,i.byteLength);if(a===2&&i.length>=5){n=BigInt(o.getUint32(1,!0));continue}a===3&&i.length>=9&&(r=o.getBigUint64(1,!0))}return{computeUnitLimit:n,computeUnitPriceMicroLamports:r}}function N(e,t){return e<=0n||t<=0n?0n:(e*t+(D-1n))/D}function P(e){let t=e.signatures;return Array.isArray(t)?BigInt(t.length):t&&typeof t==`object`?BigInt(Object.keys(t).length):0n}async function F({fromAddress:e,rpc:t,transaction:n}){let r=T().decode(n.messageBytes),i=await U(r,t),a=new Map,o=new Map,s;for(let e of r.instructions){let n=i[e.programAddressIndex];if(n===`11111111111111111111111111111111`){let t=W(i,e,1),n=G(e.data);t&&n>0n&&a.set(t,{address:t,kind:`system`,lamports:n});continue}if(n===`ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL`){let n=e.data?.[0];if(!(n===void 0||n===0||n===1))continue;let r=W(i,e,1);if(!r||!E(r)||(await t.getAccountInfo(r).send()).value)continue;s===void 0&&(s=await t.getMinimumBalanceForRentExemption(165n).send()),a.set(r,{address:r,kind:`ata`,lamports:s});continue}if(!(n===`TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA`||n===`TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb`)||e.data?.[0]!==9)continue;let r=W(i,e,0),c=W(i,e,1);r&&c&&o.set(r,c)}let c=E(e)?e:void 0,l=Array.from(a.values()).map(e=>{let t=o.get(e.address),n=c!==void 0&&t===c;return{...e,refunded:n}}),u=l.reduce((e,t)=>e+t.lamports,0n),d=l.reduce((e,t)=>e+(t.refunded?t.lamports:0n),0n);return{createdAccounts:l,rentFee:u,rentFeeNet:u-d,refundable:d}}function I(e){return!!e&&typeof e==`object`}function L(e,t){if(I(e))return Reflect.get(e,t)}function R(e){if(e instanceof Uint8Array)return Array.from(e);if(Array.isArray(e)&&e.every(e=>Number.isInteger(e)&&e>=0))return e}function z(e){let t=L(e,`addressTableLookups`);if(!Array.isArray(t))return[];let n=[];for(let e of t){let t=L(e,`accountKey`),r=R(L(e,`writableIndexes`)),i=R(L(e,`readonlyIndexes`));typeof t!=`string`||!r||!i||n.push({accountKey:t,writableIndexes:r,readonlyIndexes:i})}return n}function B(e){let t=L(e,`value`);if(!I(t))return[];let n=L(t,`addresses`);return Array.isArray(n)?n.filter(e=>typeof e==`string`):[]}async function V(e,t){let n=[],r=[],i=z(e);for(let e of i){if(!E(e.accountKey))continue;let i=await H(t,e.accountKey);for(let t of e.writableIndexes){let e=i[t];e&&n.push(e)}for(let t of e.readonlyIndexes){let e=i[t];e&&r.push(e)}}return{writableAddresses:n,readonlyAddresses:r}}async function H(e,t){let n=L(e,`getAddressLookupTable`);if(typeof n!=`function`)return[];let r=Reflect.apply(n,e,[t]),i=L(r,`send`);return typeof i==`function`?B(await Reflect.apply(i,r,[])):[]}async function U(e,t){let{writableAddresses:n,readonlyAddresses:r}=await V(e,t);return[...e.staticAccounts,...n,...r]}function W(e,t,n){let r=t.accountIndices?.[n];if(r!==void 0)return e[r]}function G(e){if(!e||e.length<12)return 0n;let t=new DataView(e.buffer,e.byteOffset,e.byteLength),n=t.getUint32(0,!0);if(n===0&&e.length>=12)return t.getBigUint64(4,!0);if(n===3&&e.length>=44){let n=Number(t.getBigUint64(36,!0));if(!Number.isSafeInteger(n)||n<0)return 0n;let r=44+n;return r+8>e.length?0n:new DataView(e.buffer,e.byteOffset+r,8).getBigUint64(0,!0)}return 0n}export{j as _estimateGasFromSwapResponse,O as estimateNativeFeeFactory};
2
2
  //# sourceMappingURL=estimate-native-fee.js.map