@metamask/bridge-status-controller 70.0.0 → 70.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +14 -1
- package/dist/bridge-status-controller.cjs +48 -172
- package/dist/bridge-status-controller.cjs.map +1 -1
- package/dist/bridge-status-controller.d.cts +2 -1
- package/dist/bridge-status-controller.d.cts.map +1 -1
- package/dist/bridge-status-controller.d.mts +2 -1
- package/dist/bridge-status-controller.d.mts.map +1 -1
- package/dist/bridge-status-controller.intent.cjs +7 -19
- package/dist/bridge-status-controller.intent.cjs.map +1 -1
- package/dist/bridge-status-controller.intent.d.cts +2 -10
- package/dist/bridge-status-controller.intent.d.cts.map +1 -1
- package/dist/bridge-status-controller.intent.d.mts +2 -10
- package/dist/bridge-status-controller.intent.d.mts.map +1 -1
- package/dist/bridge-status-controller.intent.mjs +7 -19
- package/dist/bridge-status-controller.intent.mjs.map +1 -1
- package/dist/bridge-status-controller.mjs +51 -175
- package/dist/bridge-status-controller.mjs.map +1 -1
- package/dist/types.cjs.map +1 -1
- package/dist/types.d.cts +10 -4
- package/dist/types.d.cts.map +1 -1
- package/dist/types.d.mts +10 -4
- package/dist/types.d.mts.map +1 -1
- package/dist/types.mjs.map +1 -1
- package/dist/utils/bridge-status.cjs +17 -1
- package/dist/utils/bridge-status.cjs.map +1 -1
- package/dist/utils/bridge-status.d.cts +8 -2
- package/dist/utils/bridge-status.d.cts.map +1 -1
- package/dist/utils/bridge-status.d.mts +8 -2
- package/dist/utils/bridge-status.d.mts.map +1 -1
- package/dist/utils/bridge-status.mjs +15 -0
- package/dist/utils/bridge-status.mjs.map +1 -1
- package/dist/utils/gas.cjs +1 -58
- package/dist/utils/gas.cjs.map +1 -1
- package/dist/utils/gas.d.cts +3 -26
- package/dist/utils/gas.d.cts.map +1 -1
- package/dist/utils/gas.d.mts +3 -26
- package/dist/utils/gas.d.mts.map +1 -1
- package/dist/utils/gas.mjs +0 -55
- package/dist/utils/gas.mjs.map +1 -1
- package/dist/utils/history.cjs +9 -8
- package/dist/utils/history.cjs.map +1 -1
- package/dist/utils/history.d.cts +3 -2
- package/dist/utils/history.d.cts.map +1 -1
- package/dist/utils/history.d.mts +3 -2
- package/dist/utils/history.d.mts.map +1 -1
- package/dist/utils/history.mjs +9 -8
- package/dist/utils/history.mjs.map +1 -1
- package/dist/utils/intent-api.cjs +26 -26
- package/dist/utils/intent-api.cjs.map +1 -1
- package/dist/utils/intent-api.d.cts +7 -2
- package/dist/utils/intent-api.d.cts.map +1 -1
- package/dist/utils/intent-api.d.mts +7 -2
- package/dist/utils/intent-api.d.mts.map +1 -1
- package/dist/utils/intent-api.mjs +24 -25
- package/dist/utils/intent-api.mjs.map +1 -1
- package/dist/utils/snaps.cjs +5 -5
- package/dist/utils/snaps.cjs.map +1 -1
- package/dist/utils/snaps.d.cts +3 -17
- package/dist/utils/snaps.d.cts.map +1 -1
- package/dist/utils/snaps.d.mts +3 -17
- package/dist/utils/snaps.d.mts.map +1 -1
- package/dist/utils/snaps.mjs +6 -6
- package/dist/utils/snaps.mjs.map +1 -1
- package/dist/utils/transaction.cjs +250 -24
- package/dist/utils/transaction.cjs.map +1 -1
- package/dist/utils/transaction.d.cts +91 -169
- package/dist/utils/transaction.d.cts.map +1 -1
- package/dist/utils/transaction.d.mts +91 -169
- package/dist/utils/transaction.d.mts.map +1 -1
- package/dist/utils/transaction.mjs +234 -19
- package/dist/utils/transaction.mjs.map +1 -1
- package/package.json +3 -3
package/dist/utils/snaps.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"snaps.cjs","sourceRoot":"","sources":["../../src/utils/snaps.ts"],"names":[],"mappings":";;;AAOA,mEAMqC;AACrC,6EAG0C;AAG1C,+BAAkC;AAOlC;;;;;;;;;;GAUG;AACI,MAAM,8BAA8B,GAAG,CAC5C,MAAc,EACd,WAAmB,EACnB,KAAkB,EAClB,SAAiB,EACjB,OAAiC,EACjC,E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EAAE,SAAS,EAAE;QAC3D,IAAI,EAAE,UAAU,CAAC,CAAC,CAAC,wCAAe,CAAC,MAAM,CAAC,CAAC,CAAC,wCAAe,CAAC,IAAI;QAChE,MAAM,EAAE,0CAAiB,CAAC,SAAS;QACnC,IAAI,EAAE,wCAAwC;QAC9C,MAAM,EAAE,MAAM;QACd,6DAA6D;QAC7D,QAAQ,EAAE,IAAI,EAAE,+DAA+D;QAC/E,UAAU;KACX,CAAC;AACJ,CAAC,CAAC;AAvEW,QAAA,sBAAsB,0BAuEjC;AAEF;;;;;;;;;;;GAWG;AACI,MAAM,cAAc,GAAG,KAAK,EACjC,SAA0C,EAC1C,KAAY,EACZ,aAA0D,EAC1D,eAAgF,EACtD,EAAE;IAC5B,IAAI,CAAC,eAAe,CAAC,QAAQ,EAAE,IAAI,EAAE,EAAE,EAAE,CAAC;QACxC,MAAM,IAAI,KAAK,CACb,kEAAkE,CACnE,CAAC;IACJ,CAAC;IAED,MAAM,OAAO,GAAG,IAAA,wBAAgB,EAC9B,KAAK,EACL,aAAa,CAAC,KAAK,CAAC,UAAU,EAC9B,eAAe,CAAC,EAAE,EAClB,eAAe,CAAC,QAAQ,EAAE,IAAI,EAAE,EAAE,CACnC,CAAC;IACF,MAAM,eAAe,GAAG,CAAC,MAAM,SAAS,CAAC,IAAI,CAC3C,8BAA8B,EAC9B,OAAO,CACR,CAIwB,CAAC;IAE1B,MAAM,MAAM,GAAG,IAAA,8BAAsB,EACnC,eAAe,EACf,KAAK,EACL,aAAa,EACb,eAAe,CAChB,CAAC;IAEF,iFAAiF;IACjF,uNAAuN;IACvN,OAAO,MAAM,CAAC;AAChB,CAAC,CAAC;AArCW,QAAA,cAAc,kBAqCzB","sourcesContent":["/* eslint-disable @typescript-eslint/explicit-function-return-type */\nimport type { AccountsControllerState } from '@metamask/accounts-controller';\nimport type {\n QuoteMetadata,\n QuoteResponse,\n Trade,\n} from '@metamask/bridge-controller';\nimport {\n extractTradeData,\n formatChainIdToCaip,\n formatChainIdToHex,\n isCrossChain,\n isTronTrade,\n} from '@metamask/bridge-controller';\nimport {\n TransactionStatus,\n TransactionType,\n} from '@metamask/transaction-controller';\nimport type { TransactionMeta } from '@metamask/transaction-controller';\nimport type { CaipChainId, Hex } from '@metamask/utils';\nimport { v4 as uuid } from 'uuid';\n\nimport type {\n BridgeStatusControllerMessenger,\n SolanaTransactionMeta,\n} from '../types';\n\n/**\n * Creates a client request object for signing and sending a transaction\n * Works for Solana, BTC, Tron, and other non-EVM networks\n *\n * @param snapId - The snap ID to send the request to\n * @param transaction - The base64 encoded transaction string\n * @param scope - The CAIP-2 chain scope\n * @param accountId - The account ID\n * @param options - Optional network-specific options\n * @returns The snap request object\n */\nexport const createClientTransactionRequest = (\n snapId: string,\n transaction: string,\n scope: CaipChainId,\n accountId: string,\n options?: Record<string, unknown>,\n) => {\n return {\n // TODO: remove 'as never' typing.\n snapId: snapId as never,\n origin: 'metamask',\n handler: 'onClientRequest' as never,\n request: {\n id: uuid(),\n jsonrpc: '2.0',\n method: 'signAndSendTransaction',\n params: {\n transaction,\n scope,\n accountId,\n ...(options && { options }),\n },\n },\n };\n};\n\n/**\n * Creates a request to sign and send a transaction for non-EVM chains\n * Uses the new unified ClientRequest:signAndSendTransaction interface\n *\n * @param trade - The trade data\n * @param srcChainId - The source chain ID\n * @param accountId - The selected account ID\n * @param snapId - The snap ID\n * @returns The snap request object for signing and sending transaction\n */\nexport const getClientRequest = (\n trade: Trade,\n srcChainId: number,\n accountId: string,\n snapId: string,\n) => {\n const scope = formatChainIdToCaip(srcChainId);\n\n const transactionData = extractTradeData(trade);\n\n // Tron trades need the visible flag and contract type to be included in the request options\n const options = isTronTrade(trade)\n ? {\n visible: trade.visible,\n type: trade.raw_data?.contract?.[0]?.type,\n }\n : undefined;\n\n // Use the new unified interface\n return createClientTransactionRequest(\n snapId,\n transactionData,\n scope,\n accountId,\n options,\n );\n};\n\nexport const getTxMetaFields = (\n quoteResponse: Omit<QuoteResponse<Trade, Trade>, 'approval' | 'trade'> &\n QuoteMetadata,\n approvalTxId?: string,\n): Omit<\n TransactionMeta,\n 'networkClientId' | 'status' | 'time' | 'txParams' | 'id' | 'chainId'\n> => {\n // Handle destination chain ID - should always be convertible for EVM destinations\n let destinationChainId;\n try {\n destinationChainId = formatChainIdToHex(quoteResponse.quote.destChainId);\n } catch {\n // Fallback for non-EVM destination (shouldn't happen for BTC->EVM)\n destinationChainId = '0x1' as `0x${string}`; // Default to mainnet\n }\n\n return {\n destinationChainId,\n sourceTokenAmount: quoteResponse.quote.srcTokenAmount,\n sourceTokenSymbol: quoteResponse.quote.srcAsset.symbol,\n sourceTokenDecimals: quoteResponse.quote.srcAsset.decimals,\n sourceTokenAddress: quoteResponse.quote.srcAsset.address,\n\n destinationTokenAmount: quoteResponse.quote.destTokenAmount,\n destinationTokenSymbol: quoteResponse.quote.destAsset.symbol,\n destinationTokenDecimals: quoteResponse.quote.destAsset.decimals,\n destinationTokenAddress: quoteResponse.quote.destAsset.address,\n\n // chainId is now excluded from this function and handled by the caller\n approvalTxId,\n // this is the decimal (non atomic) amount (not USD value) of source token to swap\n swapTokenValue: quoteResponse.sentAmount.amount,\n };\n};\n\n/**\n * Handles the response from non-EVM transaction submission\n * Works with the new unified ClientRequest:signAndSendTransaction interface\n * Supports Solana, Bitcoin, and other non-EVM chains\n *\n * @param snapResponse - The response from the snap after transaction submission\n * @param trade - The non-evm trade or approval data\n * @param quoteResponse - The quote response containing trade details and metadata\n * @param selectedAccount - The selected account information\n * @returns The transaction metadata including non-EVM specific fields\n */\nexport const handleNonEvmTxResponse = (\n snapResponse:\n | string\n | { transactionId: string } // New unified interface response\n | { result: Record<string, string> }\n | { signature: string },\n trade: Trade,\n quoteResponse: Omit<QuoteResponse<Trade>, 'trade' | 'approval'> &\n QuoteMetadata,\n selectedAccount: AccountsControllerState['internalAccounts']['accounts'][string],\n): TransactionMeta & SolanaTransactionMeta => {\n const selectedAccountAddress = selectedAccount.address;\n const snapId = selectedAccount.metadata.snap?.id;\n let hash;\n // Handle different response formats\n if (typeof snapResponse === 'string') {\n hash = snapResponse;\n } else if (snapResponse && typeof snapResponse === 'object') {\n // Check for new unified interface response format first\n if ('transactionId' in snapResponse && snapResponse.transactionId) {\n hash = snapResponse.transactionId;\n } else if (\n 'result' in snapResponse &&\n snapResponse.result &&\n typeof snapResponse.result === 'object'\n ) {\n // Try to extract signature from common locations in response object\n hash =\n snapResponse.result.signature ||\n snapResponse.result.txid ||\n snapResponse.result.hash ||\n snapResponse.result.txHash;\n } else if (\n 'signature' in snapResponse &&\n snapResponse.signature &&\n typeof snapResponse.signature === 'string'\n ) {\n hash = snapResponse.signature;\n }\n }\n\n const isBridgeTx = isCrossChain(\n quoteResponse.quote.srcChainId,\n quoteResponse.quote.destChainId,\n );\n\n let hexChainId: Hex;\n try {\n hexChainId = formatChainIdToHex(quoteResponse.quote.srcChainId);\n } catch {\n hexChainId = '0x1';\n }\n // Extract the transaction data for storage\n const tradeData = extractTradeData(trade);\n\n // Create a transaction meta object with bridge-specific fields\n return {\n ...getTxMetaFields(quoteResponse),\n time: Date.now(),\n id: hash ?? uuid(),\n chainId: hexChainId,\n networkClientId: snapId ?? 'mainnet',\n txParams: { from: selectedAccountAddress, data: tradeData },\n type: isBridgeTx ? TransactionType.bridge : TransactionType.swap,\n status: TransactionStatus.submitted,\n hash, // Add the transaction signature as hash\n origin: snapId,\n // Add an explicit flag to mark this as a non-EVM transaction\n isSolana: true, // TODO deprecate this and use chainId to detect non-EVM chains\n isBridgeTx,\n };\n};\n\n/**\n * Submits the transaction to the snap using the new unified ClientRequest interface\n * Works for all non-EVM chains (Solana, BTC, Tron)\n * This adds an approval tx to the ApprovalsController in the background\n * The client needs to handle the approval tx by redirecting to the confirmation page with the approvalTxId in the URL\n *\n * @param messenger - The BridgeStatusControllerMessenger instance\n * @param trade - The trade data (can be approval or main trade)\n * @param quoteResponse - The quote response containing metadata\n * @param selectedAccount - The account to submit the transaction for\n * @returns The transaction meta\n */\nexport const handleNonEvmTx = async (\n messenger: BridgeStatusControllerMessenger,\n trade: Trade,\n quoteResponse: QuoteResponse<Trade, Trade> & QuoteMetadata,\n selectedAccount: AccountsControllerState['internalAccounts']['accounts'][string],\n): Promise<TransactionMeta> => {\n if (!selectedAccount.metadata?.snap?.id) {\n throw new Error(\n 'Failed to submit cross-chain swap transaction: undefined snap id',\n );\n }\n\n const request = getClientRequest(\n trade,\n quoteResponse.quote.srcChainId,\n selectedAccount.id,\n selectedAccount.metadata?.snap?.id,\n );\n const requestResponse = (await messenger.call(\n 'SnapController:handleRequest',\n request,\n )) as\n | string\n | { transactionId: string }\n | { result: Record<string, string> }\n | { signature: string };\n\n const txMeta = handleNonEvmTxResponse(\n requestResponse,\n trade,\n quoteResponse,\n selectedAccount,\n );\n\n // TODO remove this eventually, just returning it now to match extension behavior\n // OR if the snap can propagate the snapRequestId or keyringReqId to the ApprovalsController, this can return the approvalTxId instead and clients won't need to subscribe to the ApprovalsController state to redirect\n return txMeta;\n};\n"]}
|
|
1
|
+
{"version":3,"file":"snaps.cjs","sourceRoot":"","sources":["../../src/utils/snaps.ts"],"names":[],"mappings":";;;AAOA,mEAMqC;AACrC,mEAA6D;AAC7D,6EAI0C;AAG1C,+BAAkC;AAOlC;;;;;;;;;;GAUG;AACI,MAAM,8BAA8B,GAAG,CAC5C,MAAc,EACd,WAAmB,EACnB,KAAkB,EAClB,SAAiB,EACjB,OAAiC,EACjC,EAAE;IACF,OAAO;QACL,kCAAkC;QAClC,MAAM,EAAE,MAAe;QACvB,MAAM,EAAE,UAAU;QAClB,OAAO,EAAE,iBAA0B;QACnC,OAAO,EAAE;YACP,EAAE,EAAE,IAAA,SAAI,GAAE;YACV,OAAO,EAAE,KAAK;YACd,MAAM,EAAE,wBAAwB;YAChC,MAAM,EAAE;gBACN,WAAW;gBACX,KAAK;gBACL,SAAS;gBACT,GAAG,CAAC,OAAO,IAAI,EAAE,OAAO,EAAE,CAAC;aAC5B;SACF;KACF,CAAC;AACJ,CAAC,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;;;;;;GAWG;AACI,MAAM,cAAc,GAAG,KAAK,EACjC,SAA0C,EAC1C,KAAY,EACZ,aAA0D,EAC1D,eAAgF,EACtD,EAAE;IAC5B,IAAI,CAAC,eAAe,CAAC,QAAQ,EAAE,IAAI,EAAE,EAAE,EAAE,CAAC;QACxC,MAAM,IAAI,KAAK,CACb,kEAAkE,CACnE,CAAC;IACJ,CAAC;IAED,MAAM,OAAO,GAAG,IAAA,wBAAgB,EAC9B,KAAK,EACL,aAAa,CAAC,KAAK,CAAC,UAAU,EAC9B,eAAe,CAAC,EAAE,EAClB,eAAe,CAAC,QAAQ,EAAE,IAAI,EAAE,EAAE,CACnC,CAAC;IACF,MAAM,eAAe,GAAG,CAAC,MAAM,SAAS,CAAC,IAAI,CAC3C,8BAA8B,EAC9B,OAAO,CACR,CAIwB,CAAC;IAE1B,MAAM,MAAM,GAAG,IAAA,8BAAsB,EACnC,eAAe,EACf,KAAK,EACL,aAAa,EACb,eAAe,CAChB,CAAC;IAEF,iFAAiF;IACjF,uNAAuN;IACvN,OAAO,MAAM,CAAC;AAChB,CAAC,CAAC;AArCW,QAAA,cAAc,kBAqCzB","sourcesContent":["/* eslint-disable @typescript-eslint/explicit-function-return-type */\nimport type { AccountsControllerState } from '@metamask/accounts-controller';\nimport type {\n QuoteMetadata,\n QuoteResponse,\n Trade,\n} from '@metamask/bridge-controller';\nimport {\n extractTradeData,\n formatChainIdToCaip,\n formatChainIdToHex,\n isCrossChain,\n isTronTrade,\n} from '@metamask/bridge-controller';\nimport { SnapController } from '@metamask/snaps-controllers';\nimport {\n CHAIN_IDS,\n TransactionStatus,\n TransactionType,\n} from '@metamask/transaction-controller';\nimport type { TransactionMeta } from '@metamask/transaction-controller';\nimport type { CaipChainId, Hex } from '@metamask/utils';\nimport { v4 as uuid } from 'uuid';\n\nimport type {\n BridgeStatusControllerMessenger,\n SolanaTransactionMeta,\n} from '../types';\n\n/**\n * Creates a client request object for signing and sending a transaction\n * Works for Solana, BTC, Tron, and other non-EVM networks\n *\n * @param snapId - The snap ID to send the request to\n * @param transaction - The base64 encoded transaction string\n * @param scope - The CAIP-2 chain scope\n * @param accountId - The account ID\n * @param options - Optional network-specific options\n * @returns The snap request object\n */\nexport const createClientTransactionRequest = (\n snapId: string,\n transaction: string,\n scope: CaipChainId,\n accountId: string,\n options?: Record<string, unknown>,\n) => {\n return {\n // TODO: remove 'as never' typing.\n snapId: snapId as never,\n origin: 'metamask',\n handler: 'onClientRequest' as never,\n request: {\n id: uuid(),\n jsonrpc: '2.0',\n method: 'signAndSendTransaction',\n params: {\n transaction,\n scope,\n accountId,\n ...(options && { options }),\n },\n },\n };\n};\n\n/**\n * Creates a request to sign and send a transaction for non-EVM chains\n * Uses the new unified ClientRequest:signAndSendTransaction interface\n *\n * @param trade - The trade data\n * @param srcChainId - The source chain ID\n * @param accountId - The account ID\n * @param snapId - The snap ID\n * @returns The snap request object for signing and sending transaction\n */\nexport const getClientRequest = (\n trade: Trade,\n srcChainId: number,\n accountId: AccountsControllerState['internalAccounts']['accounts'][string]['id'],\n snapId: string,\n): Parameters<SnapController['handleRequest']>[0] => {\n const scope = formatChainIdToCaip(srcChainId);\n\n const transaction = extractTradeData(trade);\n\n // Tron trades need the visible flag and contract type to be included in the request options\n const options = isTronTrade(trade)\n ? {\n visible: trade.visible,\n type: trade.raw_data?.contract?.[0]?.type,\n }\n : undefined;\n\n return createClientTransactionRequest(\n snapId,\n transaction,\n scope,\n accountId,\n options,\n );\n};\n\nexport const getTxMetaFields = (\n quoteResponse: Omit<QuoteResponse<Trade, Trade>, 'approval' | 'trade'> &\n QuoteMetadata,\n approvalTxId?: string,\n): Omit<\n TransactionMeta,\n 'networkClientId' | 'status' | 'time' | 'txParams' | 'id' | 'chainId'\n> => {\n // Handle destination chain ID - should always be convertible for EVM destinations\n let destinationChainId;\n try {\n destinationChainId = formatChainIdToHex(quoteResponse.quote.destChainId);\n } catch {\n // Fallback for non-EVM destination (shouldn't happen for BTC->EVM)\n destinationChainId = CHAIN_IDS.MAINNET; // Default to mainnet\n }\n\n return {\n destinationChainId,\n sourceTokenAmount: quoteResponse.quote.srcTokenAmount,\n sourceTokenSymbol: quoteResponse.quote.srcAsset.symbol,\n sourceTokenDecimals: quoteResponse.quote.srcAsset.decimals,\n sourceTokenAddress: quoteResponse.quote.srcAsset.address,\n\n destinationTokenAmount: quoteResponse.quote.destTokenAmount,\n destinationTokenSymbol: quoteResponse.quote.destAsset.symbol,\n destinationTokenDecimals: quoteResponse.quote.destAsset.decimals,\n destinationTokenAddress: quoteResponse.quote.destAsset.address,\n\n // chainId is now excluded from this function and handled by the caller\n approvalTxId,\n // this is the decimal (non atomic) amount (not USD value) of source token to swap\n swapTokenValue: quoteResponse.sentAmount.amount,\n };\n};\n\n/**\n * Handles the response from non-EVM transaction submission\n * Works with the new unified ClientRequest:signAndSendTransaction interface\n * Supports Solana, Bitcoin, and other non-EVM chains\n *\n * @param snapResponse - The response from the snap after transaction submission\n * @param trade - The non-evm trade or approval data\n * @param quoteResponse - The quote response containing trade details and metadata\n * @param selectedAccount - The selected account information\n * @returns The transaction metadata including non-EVM specific fields\n */\nexport const handleNonEvmTxResponse = (\n snapResponse:\n | string\n | { transactionId: string } // New unified interface response\n | { result: Record<string, string> }\n | { signature: string },\n trade: Trade,\n quoteResponse: Omit<QuoteResponse<Trade>, 'trade' | 'approval'> &\n QuoteMetadata,\n selectedAccount: AccountsControllerState['internalAccounts']['accounts'][string],\n): TransactionMeta & SolanaTransactionMeta => {\n const selectedAccountAddress = selectedAccount.address;\n const snapId = selectedAccount.metadata.snap?.id;\n let hash;\n // Handle different response formats\n if (typeof snapResponse === 'string') {\n hash = snapResponse;\n } else if (snapResponse && typeof snapResponse === 'object') {\n // Check for new unified interface response format first\n if ('transactionId' in snapResponse && snapResponse.transactionId) {\n hash = snapResponse.transactionId;\n } else if (\n 'result' in snapResponse &&\n snapResponse.result &&\n typeof snapResponse.result === 'object'\n ) {\n // Try to extract signature from common locations in response object\n hash =\n snapResponse.result.signature ||\n snapResponse.result.txid ||\n snapResponse.result.hash ||\n snapResponse.result.txHash;\n } else if (\n 'signature' in snapResponse &&\n snapResponse.signature &&\n typeof snapResponse.signature === 'string'\n ) {\n hash = snapResponse.signature;\n }\n }\n\n const isBridgeTx = isCrossChain(\n quoteResponse.quote.srcChainId,\n quoteResponse.quote.destChainId,\n );\n\n let hexChainId: Hex;\n try {\n hexChainId = formatChainIdToHex(quoteResponse.quote.srcChainId);\n } catch {\n hexChainId = '0x1';\n }\n // Extract the transaction data for storage\n const tradeData = extractTradeData(trade);\n\n // Create a transaction meta object with bridge-specific fields\n return {\n ...getTxMetaFields(quoteResponse),\n time: Date.now(),\n id: hash ?? uuid(),\n chainId: hexChainId,\n networkClientId: snapId ?? 'mainnet',\n txParams: { from: selectedAccountAddress, data: tradeData },\n type: isBridgeTx ? TransactionType.bridge : TransactionType.swap,\n status: TransactionStatus.submitted,\n hash, // Add the transaction signature as hash\n origin: snapId,\n // Add an explicit flag to mark this as a non-EVM transaction\n isSolana: true, // TODO deprecate this and use chainId to detect non-EVM chains\n isBridgeTx,\n };\n};\n\n/**\n * Submits the transaction to the snap using the new unified ClientRequest interface\n * Works for all non-EVM chains (Solana, BTC, Tron)\n * This adds an approval tx to the ApprovalsController in the background\n * The client needs to handle the approval tx by redirecting to the confirmation page with the approvalTxId in the URL\n *\n * @param messenger - The BridgeStatusControllerMessenger instance\n * @param trade - The trade data (can be approval or main trade)\n * @param quoteResponse - The quote response containing metadata\n * @param selectedAccount - The account to submit the transaction for\n * @returns The transaction meta\n */\nexport const handleNonEvmTx = async (\n messenger: BridgeStatusControllerMessenger,\n trade: Trade,\n quoteResponse: QuoteResponse<Trade, Trade> & QuoteMetadata,\n selectedAccount: AccountsControllerState['internalAccounts']['accounts'][string],\n): Promise<TransactionMeta> => {\n if (!selectedAccount.metadata?.snap?.id) {\n throw new Error(\n 'Failed to submit cross-chain swap transaction: undefined snap id',\n );\n }\n\n const request = getClientRequest(\n trade,\n quoteResponse.quote.srcChainId,\n selectedAccount.id,\n selectedAccount.metadata?.snap?.id,\n );\n const requestResponse = (await messenger.call(\n 'SnapController:handleRequest',\n request,\n )) as\n | string\n | { transactionId: string }\n | { result: Record<string, string> }\n | { signature: string };\n\n const txMeta = handleNonEvmTxResponse(\n requestResponse,\n trade,\n quoteResponse,\n selectedAccount,\n );\n\n // TODO remove this eventually, just returning it now to match extension behavior\n // OR if the snap can propagate the snapRequestId or keyringReqId to the ApprovalsController, this can return the approvalTxId instead and clients won't need to subscribe to the ApprovalsController state to redirect\n return txMeta;\n};\n"]}
|
package/dist/utils/snaps.d.cts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { AccountsControllerState } from "@metamask/accounts-controller";
|
|
2
2
|
import type { QuoteMetadata, QuoteResponse, Trade } from "@metamask/bridge-controller";
|
|
3
|
+
import { SnapController } from "@metamask/snaps-controllers";
|
|
3
4
|
import type { TransactionMeta } from "@metamask/transaction-controller";
|
|
4
5
|
import type { CaipChainId } from "@metamask/utils";
|
|
5
6
|
import type { BridgeStatusControllerMessenger, SolanaTransactionMeta } from "../types.cjs";
|
|
@@ -36,26 +37,11 @@ export declare const createClientTransactionRequest: (snapId: string, transactio
|
|
|
36
37
|
*
|
|
37
38
|
* @param trade - The trade data
|
|
38
39
|
* @param srcChainId - The source chain ID
|
|
39
|
-
* @param accountId - The
|
|
40
|
+
* @param accountId - The account ID
|
|
40
41
|
* @param snapId - The snap ID
|
|
41
42
|
* @returns The snap request object for signing and sending transaction
|
|
42
43
|
*/
|
|
43
|
-
export declare const getClientRequest: (trade: Trade, srcChainId: number, accountId: string, snapId: string) =>
|
|
44
|
-
snapId: never;
|
|
45
|
-
origin: string;
|
|
46
|
-
handler: never;
|
|
47
|
-
request: {
|
|
48
|
-
id: string;
|
|
49
|
-
jsonrpc: string;
|
|
50
|
-
method: string;
|
|
51
|
-
params: {
|
|
52
|
-
options?: Record<string, unknown> | undefined;
|
|
53
|
-
transaction: string;
|
|
54
|
-
scope: `${string}:${string}`;
|
|
55
|
-
accountId: string;
|
|
56
|
-
};
|
|
57
|
-
};
|
|
58
|
-
};
|
|
44
|
+
export declare const getClientRequest: (trade: Trade, srcChainId: number, accountId: AccountsControllerState['internalAccounts']['accounts'][string]['id'], snapId: string) => Parameters<SnapController['handleRequest']>[0];
|
|
59
45
|
export declare const getTxMetaFields: (quoteResponse: Omit<QuoteResponse<Trade, Trade>, 'approval' | 'trade'> & QuoteMetadata, approvalTxId?: string) => Omit<TransactionMeta, 'networkClientId' | 'status' | 'time' | 'txParams' | 'id' | 'chainId'>;
|
|
60
46
|
/**
|
|
61
47
|
* Handles the response from non-EVM transaction submission
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"snaps.d.cts","sourceRoot":"","sources":["../../src/utils/snaps.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,uBAAuB,EAAE,sCAAsC;AAC7E,OAAO,KAAK,EACV,aAAa,EACb,aAAa,EACb,KAAK,EACN,oCAAoC;
|
|
1
|
+
{"version":3,"file":"snaps.d.cts","sourceRoot":"","sources":["../../src/utils/snaps.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,uBAAuB,EAAE,sCAAsC;AAC7E,OAAO,KAAK,EACV,aAAa,EACb,aAAa,EACb,KAAK,EACN,oCAAoC;AAQrC,OAAO,EAAE,cAAc,EAAE,oCAAoC;AAM7D,OAAO,KAAK,EAAE,eAAe,EAAE,yCAAyC;AACxE,OAAO,KAAK,EAAE,WAAW,EAAO,wBAAwB;AAGxD,OAAO,KAAK,EACV,+BAA+B,EAC/B,qBAAqB,EACtB,qBAAiB;AAElB;;;;;;;;;;GAUG;AACH,eAAO,MAAM,8BAA8B,WACjC,MAAM,eACD,MAAM,SACZ,WAAW,aACP,MAAM,YACP,OAAO,MAAM,EAAE,OAAO,CAAC;;;;;;;;;;;;;;;CAmBlC,CAAC;AAEF;;;;;;;;;GASG;AACH,eAAO,MAAM,gBAAgB,UACpB,KAAK,cACA,MAAM,aACP,uBAAuB,CAAC,kBAAkB,CAAC,CAAC,UAAU,CAAC,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,UACxE,MAAM,KACb,WAAW,cAAc,CAAC,eAAe,CAAC,CAAC,CAAC,CAAC,CAoB/C,CAAC;AAEF,eAAO,MAAM,eAAe,kBACX,KAAK,cAAc,KAAK,EAAE,KAAK,CAAC,EAAE,UAAU,GAAG,OAAO,CAAC,GACpE,aAAa,iBACA,MAAM,KACpB,KACD,eAAe,EACf,iBAAiB,GAAG,QAAQ,GAAG,MAAM,GAAG,UAAU,GAAG,IAAI,GAAG,SAAS,CA4BtE,CAAC;AAEF;;;;;;;;;;GAUG;AACH,eAAO,MAAM,sBAAsB,iBAE7B,MAAM,GACN;IAAE,aAAa,EAAE,MAAM,CAAA;CAAE,GACzB;IAAE,MAAM,EAAE,OAAO,MAAM,EAAE,MAAM,CAAC,CAAA;CAAE,GAClC;IAAE,SAAS,EAAE,MAAM,CAAA;CAAE,SAClB,KAAK,iBACG,KAAK,cAAc,KAAK,CAAC,EAAE,OAAO,GAAG,UAAU,CAAC,GAC7D,aAAa,mBACE,uBAAuB,CAAC,kBAAkB,CAAC,CAAC,UAAU,CAAC,CAAC,MAAM,CAAC,KAC/E,eAAe,GAAG,qBA6DpB,CAAC;AAEF;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,cAAc,sDAElB,KAAK,iBACG,cAAc,KAAK,EAAE,KAAK,CAAC,GAAG,aAAa,mBACzC,uBAAuB,CAAC,kBAAkB,CAAC,CAAC,UAAU,CAAC,CAAC,MAAM,CAAC,KAC/E,QAAQ,eAAe,CAgCzB,CAAC"}
|
package/dist/utils/snaps.d.mts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { AccountsControllerState } from "@metamask/accounts-controller";
|
|
2
2
|
import type { QuoteMetadata, QuoteResponse, Trade } from "@metamask/bridge-controller";
|
|
3
|
+
import { SnapController } from "@metamask/snaps-controllers";
|
|
3
4
|
import type { TransactionMeta } from "@metamask/transaction-controller";
|
|
4
5
|
import type { CaipChainId } from "@metamask/utils";
|
|
5
6
|
import type { BridgeStatusControllerMessenger, SolanaTransactionMeta } from "../types.mjs";
|
|
@@ -36,26 +37,11 @@ export declare const createClientTransactionRequest: (snapId: string, transactio
|
|
|
36
37
|
*
|
|
37
38
|
* @param trade - The trade data
|
|
38
39
|
* @param srcChainId - The source chain ID
|
|
39
|
-
* @param accountId - The
|
|
40
|
+
* @param accountId - The account ID
|
|
40
41
|
* @param snapId - The snap ID
|
|
41
42
|
* @returns The snap request object for signing and sending transaction
|
|
42
43
|
*/
|
|
43
|
-
export declare const getClientRequest: (trade: Trade, srcChainId: number, accountId: string, snapId: string) =>
|
|
44
|
-
snapId: never;
|
|
45
|
-
origin: string;
|
|
46
|
-
handler: never;
|
|
47
|
-
request: {
|
|
48
|
-
id: string;
|
|
49
|
-
jsonrpc: string;
|
|
50
|
-
method: string;
|
|
51
|
-
params: {
|
|
52
|
-
options?: Record<string, unknown> | undefined;
|
|
53
|
-
transaction: string;
|
|
54
|
-
scope: `${string}:${string}`;
|
|
55
|
-
accountId: string;
|
|
56
|
-
};
|
|
57
|
-
};
|
|
58
|
-
};
|
|
44
|
+
export declare const getClientRequest: (trade: Trade, srcChainId: number, accountId: AccountsControllerState['internalAccounts']['accounts'][string]['id'], snapId: string) => Parameters<SnapController['handleRequest']>[0];
|
|
59
45
|
export declare const getTxMetaFields: (quoteResponse: Omit<QuoteResponse<Trade, Trade>, 'approval' | 'trade'> & QuoteMetadata, approvalTxId?: string) => Omit<TransactionMeta, 'networkClientId' | 'status' | 'time' | 'txParams' | 'id' | 'chainId'>;
|
|
60
46
|
/**
|
|
61
47
|
* Handles the response from non-EVM transaction submission
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"snaps.d.mts","sourceRoot":"","sources":["../../src/utils/snaps.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,uBAAuB,EAAE,sCAAsC;AAC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;CAAE,GAClC;IAAE,SAAS,EAAE,MAAM,CAAA;CAAE,SAClB,KAAK,iBACG,KAAK,cAAc,KAAK,CAAC,EAAE,OAAO,GAAG,UAAU,CAAC,GAC7D,aAAa,mBACE,uBAAuB,CAAC,kBAAkB,CAAC,CAAC,UAAU,CAAC,CAAC,MAAM,CAAC,KAC/E,eAAe,GAAG,qBA6DpB,CAAC;AAEF;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,cAAc,sDAElB,KAAK,iBACG,cAAc,KAAK,EAAE,KAAK,CAAC,GAAG,aAAa,mBACzC,uBAAuB,CAAC,kBAAkB,CAAC,CAAC,UAAU,CAAC,CAAC,MAAM,CAAC,KAC/E,QAAQ,eAAe,CAgCzB,CAAC"}
|
package/dist/utils/snaps.mjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { extractTradeData, formatChainIdToCaip, formatChainIdToHex, isCrossChain, isTronTrade } from "@metamask/bridge-controller";
|
|
2
|
-
import {
|
|
2
|
+
import { SnapController } from "@metamask/snaps-controllers";
|
|
3
|
+
import { CHAIN_IDS, TransactionStatus, TransactionType } from "@metamask/transaction-controller";
|
|
3
4
|
import { v4 as uuid } from "uuid";
|
|
4
5
|
/**
|
|
5
6
|
* Creates a client request object for signing and sending a transaction
|
|
@@ -37,13 +38,13 @@ export const createClientTransactionRequest = (snapId, transaction, scope, accou
|
|
|
37
38
|
*
|
|
38
39
|
* @param trade - The trade data
|
|
39
40
|
* @param srcChainId - The source chain ID
|
|
40
|
-
* @param accountId - The
|
|
41
|
+
* @param accountId - The account ID
|
|
41
42
|
* @param snapId - The snap ID
|
|
42
43
|
* @returns The snap request object for signing and sending transaction
|
|
43
44
|
*/
|
|
44
45
|
export const getClientRequest = (trade, srcChainId, accountId, snapId) => {
|
|
45
46
|
const scope = formatChainIdToCaip(srcChainId);
|
|
46
|
-
const
|
|
47
|
+
const transaction = extractTradeData(trade);
|
|
47
48
|
// Tron trades need the visible flag and contract type to be included in the request options
|
|
48
49
|
const options = isTronTrade(trade)
|
|
49
50
|
? {
|
|
@@ -51,8 +52,7 @@ export const getClientRequest = (trade, srcChainId, accountId, snapId) => {
|
|
|
51
52
|
type: trade.raw_data?.contract?.[0]?.type,
|
|
52
53
|
}
|
|
53
54
|
: undefined;
|
|
54
|
-
|
|
55
|
-
return createClientTransactionRequest(snapId, transactionData, scope, accountId, options);
|
|
55
|
+
return createClientTransactionRequest(snapId, transaction, scope, accountId, options);
|
|
56
56
|
};
|
|
57
57
|
export const getTxMetaFields = (quoteResponse, approvalTxId) => {
|
|
58
58
|
// Handle destination chain ID - should always be convertible for EVM destinations
|
|
@@ -62,7 +62,7 @@ export const getTxMetaFields = (quoteResponse, approvalTxId) => {
|
|
|
62
62
|
}
|
|
63
63
|
catch {
|
|
64
64
|
// Fallback for non-EVM destination (shouldn't happen for BTC->EVM)
|
|
65
|
-
destinationChainId =
|
|
65
|
+
destinationChainId = CHAIN_IDS.MAINNET; // Default to mainnet
|
|
66
66
|
}
|
|
67
67
|
return {
|
|
68
68
|
destinationChainId,
|
package/dist/utils/snaps.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"snaps.mjs","sourceRoot":"","sources":["../../src/utils/snaps.ts"],"names":[],"mappings":"AAOA,OAAO,EACL,gBAAgB,EAChB,mBAAmB,EACnB,kBAAkB,EAClB,YAAY,EACZ,WAAW,EACZ,oCAAoC;AACrC,OAAO,EACL,iBAAiB,EACjB,eAAe,EAChB,yCAAyC;AAG1C,OAAO,EAAE,EAAE,IAAI,IAAI,EAAE,aAAa;AAOlC;;;;;;;;;;GAUG;AACH,MAAM,CAAC,MAAM,8BAA8B,GAAG,CAC5C,MAAc,EACd,WAAmB,EACnB,KAAkB,EAClB,SAAiB,EACjB,OAAiC,EACjC,EAAE;IACF,OAAO;QACL,kCAAkC;QAClC,MAAM,EAAE,MAAe;QACvB,MAAM,EAAE,UAAU;QAClB,OAAO,EAAE,iBAA0B;QACnC,OAAO,EAAE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EAAE,+DAA+D;QAC/E,UAAU;KACX,CAAC;AACJ,CAAC,CAAC;AAEF;;;;;;;;;;;GAWG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,KAAK,EACjC,SAA0C,EAC1C,KAAY,EACZ,aAA0D,EAC1D,eAAgF,EACtD,EAAE;IAC5B,IAAI,CAAC,eAAe,CAAC,QAAQ,EAAE,IAAI,EAAE,EAAE,EAAE,CAAC;QACxC,MAAM,IAAI,KAAK,CACb,kEAAkE,CACnE,CAAC;IACJ,CAAC;IAED,MAAM,OAAO,GAAG,gBAAgB,CAC9B,KAAK,EACL,aAAa,CAAC,KAAK,CAAC,UAAU,EAC9B,eAAe,CAAC,EAAE,EAClB,eAAe,CAAC,QAAQ,EAAE,IAAI,EAAE,EAAE,CACnC,CAAC;IACF,MAAM,eAAe,GAAG,CAAC,MAAM,SAAS,CAAC,IAAI,CAC3C,8BAA8B,EAC9B,OAAO,CACR,CAIwB,CAAC;IAE1B,MAAM,MAAM,GAAG,sBAAsB,CACnC,eAAe,EACf,KAAK,EACL,aAAa,EACb,eAAe,CAChB,CAAC;IAEF,iFAAiF;IACjF,uNAAuN;IACvN,OAAO,MAAM,CAAC;AAChB,CAAC,CAAC","sourcesContent":["/* eslint-disable @typescript-eslint/explicit-function-return-type */\nimport type { AccountsControllerState } from '@metamask/accounts-controller';\nimport type {\n QuoteMetadata,\n QuoteResponse,\n Trade,\n} from '@metamask/bridge-controller';\nimport {\n extractTradeData,\n formatChainIdToCaip,\n formatChainIdToHex,\n isCrossChain,\n isTronTrade,\n} from '@metamask/bridge-controller';\nimport {\n TransactionStatus,\n TransactionType,\n} from '@metamask/transaction-controller';\nimport type { TransactionMeta } from '@metamask/transaction-controller';\nimport type { CaipChainId, Hex } from '@metamask/utils';\nimport { v4 as uuid } from 'uuid';\n\nimport type {\n BridgeStatusControllerMessenger,\n SolanaTransactionMeta,\n} from '../types';\n\n/**\n * Creates a client request object for signing and sending a transaction\n * Works for Solana, BTC, Tron, and other non-EVM networks\n *\n * @param snapId - The snap ID to send the request to\n * @param transaction - The base64 encoded transaction string\n * @param scope - The CAIP-2 chain scope\n * @param accountId - The account ID\n * @param options - Optional network-specific options\n * @returns The snap request object\n */\nexport const createClientTransactionRequest = (\n snapId: string,\n transaction: string,\n scope: CaipChainId,\n accountId: string,\n options?: Record<string, unknown>,\n) => {\n return {\n // TODO: remove 'as never' typing.\n snapId: snapId as never,\n origin: 'metamask',\n handler: 'onClientRequest' as never,\n request: {\n id: uuid(),\n jsonrpc: '2.0',\n method: 'signAndSendTransaction',\n params: {\n transaction,\n scope,\n accountId,\n ...(options && { options }),\n },\n },\n };\n};\n\n/**\n * Creates a request to sign and send a transaction for non-EVM chains\n * Uses the new unified ClientRequest:signAndSendTransaction interface\n *\n * @param trade - The trade data\n * @param srcChainId - The source chain ID\n * @param accountId - The selected account ID\n * @param snapId - The snap ID\n * @returns The snap request object for signing and sending transaction\n */\nexport const getClientRequest = (\n trade: Trade,\n srcChainId: number,\n accountId: string,\n snapId: string,\n) => {\n const scope = formatChainIdToCaip(srcChainId);\n\n const transactionData = extractTradeData(trade);\n\n // Tron trades need the visible flag and contract type to be included in the request options\n const options = isTronTrade(trade)\n ? {\n visible: trade.visible,\n type: trade.raw_data?.contract?.[0]?.type,\n }\n : undefined;\n\n // Use the new unified interface\n return createClientTransactionRequest(\n snapId,\n transactionData,\n scope,\n accountId,\n options,\n );\n};\n\nexport const getTxMetaFields = (\n quoteResponse: Omit<QuoteResponse<Trade, Trade>, 'approval' | 'trade'> &\n QuoteMetadata,\n approvalTxId?: string,\n): Omit<\n TransactionMeta,\n 'networkClientId' | 'status' | 'time' | 'txParams' | 'id' | 'chainId'\n> => {\n // Handle destination chain ID - should always be convertible for EVM destinations\n let destinationChainId;\n try {\n destinationChainId = formatChainIdToHex(quoteResponse.quote.destChainId);\n } catch {\n // Fallback for non-EVM destination (shouldn't happen for BTC->EVM)\n destinationChainId = '0x1' as `0x${string}`; // Default to mainnet\n }\n\n return {\n destinationChainId,\n sourceTokenAmount: quoteResponse.quote.srcTokenAmount,\n sourceTokenSymbol: quoteResponse.quote.srcAsset.symbol,\n sourceTokenDecimals: quoteResponse.quote.srcAsset.decimals,\n sourceTokenAddress: quoteResponse.quote.srcAsset.address,\n\n destinationTokenAmount: quoteResponse.quote.destTokenAmount,\n destinationTokenSymbol: quoteResponse.quote.destAsset.symbol,\n destinationTokenDecimals: quoteResponse.quote.destAsset.decimals,\n destinationTokenAddress: quoteResponse.quote.destAsset.address,\n\n // chainId is now excluded from this function and handled by the caller\n approvalTxId,\n // this is the decimal (non atomic) amount (not USD value) of source token to swap\n swapTokenValue: quoteResponse.sentAmount.amount,\n };\n};\n\n/**\n * Handles the response from non-EVM transaction submission\n * Works with the new unified ClientRequest:signAndSendTransaction interface\n * Supports Solana, Bitcoin, and other non-EVM chains\n *\n * @param snapResponse - The response from the snap after transaction submission\n * @param trade - The non-evm trade or approval data\n * @param quoteResponse - The quote response containing trade details and metadata\n * @param selectedAccount - The selected account information\n * @returns The transaction metadata including non-EVM specific fields\n */\nexport const handleNonEvmTxResponse = (\n snapResponse:\n | string\n | { transactionId: string } // New unified interface response\n | { result: Record<string, string> }\n | { signature: string },\n trade: Trade,\n quoteResponse: Omit<QuoteResponse<Trade>, 'trade' | 'approval'> &\n QuoteMetadata,\n selectedAccount: AccountsControllerState['internalAccounts']['accounts'][string],\n): TransactionMeta & SolanaTransactionMeta => {\n const selectedAccountAddress = selectedAccount.address;\n const snapId = selectedAccount.metadata.snap?.id;\n let hash;\n // Handle different response formats\n if (typeof snapResponse === 'string') {\n hash = snapResponse;\n } else if (snapResponse && typeof snapResponse === 'object') {\n // Check for new unified interface response format first\n if ('transactionId' in snapResponse && snapResponse.transactionId) {\n hash = snapResponse.transactionId;\n } else if (\n 'result' in snapResponse &&\n snapResponse.result &&\n typeof snapResponse.result === 'object'\n ) {\n // Try to extract signature from common locations in response object\n hash =\n snapResponse.result.signature ||\n snapResponse.result.txid ||\n snapResponse.result.hash ||\n snapResponse.result.txHash;\n } else if (\n 'signature' in snapResponse &&\n snapResponse.signature &&\n typeof snapResponse.signature === 'string'\n ) {\n hash = snapResponse.signature;\n }\n }\n\n const isBridgeTx = isCrossChain(\n quoteResponse.quote.srcChainId,\n quoteResponse.quote.destChainId,\n );\n\n let hexChainId: Hex;\n try {\n hexChainId = formatChainIdToHex(quoteResponse.quote.srcChainId);\n } catch {\n hexChainId = '0x1';\n }\n // Extract the transaction data for storage\n const tradeData = extractTradeData(trade);\n\n // Create a transaction meta object with bridge-specific fields\n return {\n ...getTxMetaFields(quoteResponse),\n time: Date.now(),\n id: hash ?? uuid(),\n chainId: hexChainId,\n networkClientId: snapId ?? 'mainnet',\n txParams: { from: selectedAccountAddress, data: tradeData },\n type: isBridgeTx ? TransactionType.bridge : TransactionType.swap,\n status: TransactionStatus.submitted,\n hash, // Add the transaction signature as hash\n origin: snapId,\n // Add an explicit flag to mark this as a non-EVM transaction\n isSolana: true, // TODO deprecate this and use chainId to detect non-EVM chains\n isBridgeTx,\n };\n};\n\n/**\n * Submits the transaction to the snap using the new unified ClientRequest interface\n * Works for all non-EVM chains (Solana, BTC, Tron)\n * This adds an approval tx to the ApprovalsController in the background\n * The client needs to handle the approval tx by redirecting to the confirmation page with the approvalTxId in the URL\n *\n * @param messenger - The BridgeStatusControllerMessenger instance\n * @param trade - The trade data (can be approval or main trade)\n * @param quoteResponse - The quote response containing metadata\n * @param selectedAccount - The account to submit the transaction for\n * @returns The transaction meta\n */\nexport const handleNonEvmTx = async (\n messenger: BridgeStatusControllerMessenger,\n trade: Trade,\n quoteResponse: QuoteResponse<Trade, Trade> & QuoteMetadata,\n selectedAccount: AccountsControllerState['internalAccounts']['accounts'][string],\n): Promise<TransactionMeta> => {\n if (!selectedAccount.metadata?.snap?.id) {\n throw new Error(\n 'Failed to submit cross-chain swap transaction: undefined snap id',\n );\n }\n\n const request = getClientRequest(\n trade,\n quoteResponse.quote.srcChainId,\n selectedAccount.id,\n selectedAccount.metadata?.snap?.id,\n );\n const requestResponse = (await messenger.call(\n 'SnapController:handleRequest',\n request,\n )) as\n | string\n | { transactionId: string }\n | { result: Record<string, string> }\n | { signature: string };\n\n const txMeta = handleNonEvmTxResponse(\n requestResponse,\n trade,\n quoteResponse,\n selectedAccount,\n );\n\n // TODO remove this eventually, just returning it now to match extension behavior\n // OR if the snap can propagate the snapRequestId or keyringReqId to the ApprovalsController, this can return the approvalTxId instead and clients won't need to subscribe to the ApprovalsController state to redirect\n return txMeta;\n};\n"]}
|
|
1
|
+
{"version":3,"file":"snaps.mjs","sourceRoot":"","sources":["../../src/utils/snaps.ts"],"names":[],"mappings":"AAOA,OAAO,EACL,gBAAgB,EAChB,mBAAmB,EACnB,kBAAkB,EAClB,YAAY,EACZ,WAAW,EACZ,oCAAoC;AACrC,OAAO,EAAE,cAAc,EAAE,oCAAoC;AAC7D,OAAO,EACL,SAAS,EACT,iBAAiB,EACjB,eAAe,EAChB,yCAAyC;AAG1C,OAAO,EAAE,EAAE,IAAI,IAAI,EAAE,aAAa;AAOlC;;;;;;;;;;GAUG;AACH,MAAM,CAAC,MAAM,8BAA8B,GAAG,CAC5C,MAAc,EACd,WAAmB,EACnB,KAAkB,EAClB,SAAiB,EACjB,OAAiC,EACjC,EAAE;IACF,OAAO;QACL,kCAAkC;QAClC,MAAM,EAAE,MAAe;QACvB,MAAM,EAAE,UAAU;QAClB,OAAO,EAAE,iBAA0B;QACnC,OAAO,EAAE;YACP,EAAE,EAAE,IAAI,EAAE;YACV,OAAO,EAAE,KAAK;YACd,MAAM,EAAE,wBAAwB;YAChC,MAAM,EAAE;gBACN,WAAW;gBACX,KAAK;gBACL,SAAS;gBACT,GAAG,CAAC,OAAO,IAAI,EAAE,OAAO,EAAE,CAAC;aAC5B;SACF;KACF,CAAC;AACJ,CAAC,CAAC;AAEF;;;;;;;;;GASG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAC9B,KAAY,EACZ,UAAkB,EAClB,SAAgF,EAChF,MAAc,EACk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GAAG,gBAAgB,CAC9B,KAAK,EACL,aAAa,CAAC,KAAK,CAAC,UAAU,EAC9B,eAAe,CAAC,EAAE,EAClB,eAAe,CAAC,QAAQ,EAAE,IAAI,EAAE,EAAE,CACnC,CAAC;IACF,MAAM,eAAe,GAAG,CAAC,MAAM,SAAS,CAAC,IAAI,CAC3C,8BAA8B,EAC9B,OAAO,CACR,CAIwB,CAAC;IAE1B,MAAM,MAAM,GAAG,sBAAsB,CACnC,eAAe,EACf,KAAK,EACL,aAAa,EACb,eAAe,CAChB,CAAC;IAEF,iFAAiF;IACjF,uNAAuN;IACvN,OAAO,MAAM,CAAC;AAChB,CAAC,CAAC","sourcesContent":["/* eslint-disable @typescript-eslint/explicit-function-return-type */\nimport type { AccountsControllerState } from '@metamask/accounts-controller';\nimport type {\n QuoteMetadata,\n QuoteResponse,\n Trade,\n} from '@metamask/bridge-controller';\nimport {\n extractTradeData,\n formatChainIdToCaip,\n formatChainIdToHex,\n isCrossChain,\n isTronTrade,\n} from '@metamask/bridge-controller';\nimport { SnapController } from '@metamask/snaps-controllers';\nimport {\n CHAIN_IDS,\n TransactionStatus,\n TransactionType,\n} from '@metamask/transaction-controller';\nimport type { TransactionMeta } from '@metamask/transaction-controller';\nimport type { CaipChainId, Hex } from '@metamask/utils';\nimport { v4 as uuid } from 'uuid';\n\nimport type {\n BridgeStatusControllerMessenger,\n SolanaTransactionMeta,\n} from '../types';\n\n/**\n * Creates a client request object for signing and sending a transaction\n * Works for Solana, BTC, Tron, and other non-EVM networks\n *\n * @param snapId - The snap ID to send the request to\n * @param transaction - The base64 encoded transaction string\n * @param scope - The CAIP-2 chain scope\n * @param accountId - The account ID\n * @param options - Optional network-specific options\n * @returns The snap request object\n */\nexport const createClientTransactionRequest = (\n snapId: string,\n transaction: string,\n scope: CaipChainId,\n accountId: string,\n options?: Record<string, unknown>,\n) => {\n return {\n // TODO: remove 'as never' typing.\n snapId: snapId as never,\n origin: 'metamask',\n handler: 'onClientRequest' as never,\n request: {\n id: uuid(),\n jsonrpc: '2.0',\n method: 'signAndSendTransaction',\n params: {\n transaction,\n scope,\n accountId,\n ...(options && { options }),\n },\n },\n };\n};\n\n/**\n * Creates a request to sign and send a transaction for non-EVM chains\n * Uses the new unified ClientRequest:signAndSendTransaction interface\n *\n * @param trade - The trade data\n * @param srcChainId - The source chain ID\n * @param accountId - The account ID\n * @param snapId - The snap ID\n * @returns The snap request object for signing and sending transaction\n */\nexport const getClientRequest = (\n trade: Trade,\n srcChainId: number,\n accountId: AccountsControllerState['internalAccounts']['accounts'][string]['id'],\n snapId: string,\n): Parameters<SnapController['handleRequest']>[0] => {\n const scope = formatChainIdToCaip(srcChainId);\n\n const transaction = extractTradeData(trade);\n\n // Tron trades need the visible flag and contract type to be included in the request options\n const options = isTronTrade(trade)\n ? {\n visible: trade.visible,\n type: trade.raw_data?.contract?.[0]?.type,\n }\n : undefined;\n\n return createClientTransactionRequest(\n snapId,\n transaction,\n scope,\n accountId,\n options,\n );\n};\n\nexport const getTxMetaFields = (\n quoteResponse: Omit<QuoteResponse<Trade, Trade>, 'approval' | 'trade'> &\n QuoteMetadata,\n approvalTxId?: string,\n): Omit<\n TransactionMeta,\n 'networkClientId' | 'status' | 'time' | 'txParams' | 'id' | 'chainId'\n> => {\n // Handle destination chain ID - should always be convertible for EVM destinations\n let destinationChainId;\n try {\n destinationChainId = formatChainIdToHex(quoteResponse.quote.destChainId);\n } catch {\n // Fallback for non-EVM destination (shouldn't happen for BTC->EVM)\n destinationChainId = CHAIN_IDS.MAINNET; // Default to mainnet\n }\n\n return {\n destinationChainId,\n sourceTokenAmount: quoteResponse.quote.srcTokenAmount,\n sourceTokenSymbol: quoteResponse.quote.srcAsset.symbol,\n sourceTokenDecimals: quoteResponse.quote.srcAsset.decimals,\n sourceTokenAddress: quoteResponse.quote.srcAsset.address,\n\n destinationTokenAmount: quoteResponse.quote.destTokenAmount,\n destinationTokenSymbol: quoteResponse.quote.destAsset.symbol,\n destinationTokenDecimals: quoteResponse.quote.destAsset.decimals,\n destinationTokenAddress: quoteResponse.quote.destAsset.address,\n\n // chainId is now excluded from this function and handled by the caller\n approvalTxId,\n // this is the decimal (non atomic) amount (not USD value) of source token to swap\n swapTokenValue: quoteResponse.sentAmount.amount,\n };\n};\n\n/**\n * Handles the response from non-EVM transaction submission\n * Works with the new unified ClientRequest:signAndSendTransaction interface\n * Supports Solana, Bitcoin, and other non-EVM chains\n *\n * @param snapResponse - The response from the snap after transaction submission\n * @param trade - The non-evm trade or approval data\n * @param quoteResponse - The quote response containing trade details and metadata\n * @param selectedAccount - The selected account information\n * @returns The transaction metadata including non-EVM specific fields\n */\nexport const handleNonEvmTxResponse = (\n snapResponse:\n | string\n | { transactionId: string } // New unified interface response\n | { result: Record<string, string> }\n | { signature: string },\n trade: Trade,\n quoteResponse: Omit<QuoteResponse<Trade>, 'trade' | 'approval'> &\n QuoteMetadata,\n selectedAccount: AccountsControllerState['internalAccounts']['accounts'][string],\n): TransactionMeta & SolanaTransactionMeta => {\n const selectedAccountAddress = selectedAccount.address;\n const snapId = selectedAccount.metadata.snap?.id;\n let hash;\n // Handle different response formats\n if (typeof snapResponse === 'string') {\n hash = snapResponse;\n } else if (snapResponse && typeof snapResponse === 'object') {\n // Check for new unified interface response format first\n if ('transactionId' in snapResponse && snapResponse.transactionId) {\n hash = snapResponse.transactionId;\n } else if (\n 'result' in snapResponse &&\n snapResponse.result &&\n typeof snapResponse.result === 'object'\n ) {\n // Try to extract signature from common locations in response object\n hash =\n snapResponse.result.signature ||\n snapResponse.result.txid ||\n snapResponse.result.hash ||\n snapResponse.result.txHash;\n } else if (\n 'signature' in snapResponse &&\n snapResponse.signature &&\n typeof snapResponse.signature === 'string'\n ) {\n hash = snapResponse.signature;\n }\n }\n\n const isBridgeTx = isCrossChain(\n quoteResponse.quote.srcChainId,\n quoteResponse.quote.destChainId,\n );\n\n let hexChainId: Hex;\n try {\n hexChainId = formatChainIdToHex(quoteResponse.quote.srcChainId);\n } catch {\n hexChainId = '0x1';\n }\n // Extract the transaction data for storage\n const tradeData = extractTradeData(trade);\n\n // Create a transaction meta object with bridge-specific fields\n return {\n ...getTxMetaFields(quoteResponse),\n time: Date.now(),\n id: hash ?? uuid(),\n chainId: hexChainId,\n networkClientId: snapId ?? 'mainnet',\n txParams: { from: selectedAccountAddress, data: tradeData },\n type: isBridgeTx ? TransactionType.bridge : TransactionType.swap,\n status: TransactionStatus.submitted,\n hash, // Add the transaction signature as hash\n origin: snapId,\n // Add an explicit flag to mark this as a non-EVM transaction\n isSolana: true, // TODO deprecate this and use chainId to detect non-EVM chains\n isBridgeTx,\n };\n};\n\n/**\n * Submits the transaction to the snap using the new unified ClientRequest interface\n * Works for all non-EVM chains (Solana, BTC, Tron)\n * This adds an approval tx to the ApprovalsController in the background\n * The client needs to handle the approval tx by redirecting to the confirmation page with the approvalTxId in the URL\n *\n * @param messenger - The BridgeStatusControllerMessenger instance\n * @param trade - The trade data (can be approval or main trade)\n * @param quoteResponse - The quote response containing metadata\n * @param selectedAccount - The account to submit the transaction for\n * @returns The transaction meta\n */\nexport const handleNonEvmTx = async (\n messenger: BridgeStatusControllerMessenger,\n trade: Trade,\n quoteResponse: QuoteResponse<Trade, Trade> & QuoteMetadata,\n selectedAccount: AccountsControllerState['internalAccounts']['accounts'][string],\n): Promise<TransactionMeta> => {\n if (!selectedAccount.metadata?.snap?.id) {\n throw new Error(\n 'Failed to submit cross-chain swap transaction: undefined snap id',\n );\n }\n\n const request = getClientRequest(\n trade,\n quoteResponse.quote.srcChainId,\n selectedAccount.id,\n selectedAccount.metadata?.snap?.id,\n );\n const requestResponse = (await messenger.call(\n 'SnapController:handleRequest',\n request,\n )) as\n | string\n | { transactionId: string }\n | { result: Record<string, string> }\n | { signature: string };\n\n const txMeta = handleNonEvmTxResponse(\n requestResponse,\n trade,\n quoteResponse,\n selectedAccount,\n );\n\n // TODO remove this eventually, just returning it now to match extension behavior\n // OR if the snap can propagate the snapRequestId or keyringReqId to the ApprovalsController, this can return the approvalTxId instead and clients won't need to subscribe to the ApprovalsController state to redirect\n return txMeta;\n};\n"]}
|
|
@@ -1,28 +1,144 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.findAndUpdateTransactionsInBatch = exports.getAddTransactionBatchParams = exports.toBatchTxParams = exports.waitForTxConfirmation = exports.handleMobileHardwareWalletDelay = exports.handleApprovalDelay = exports.
|
|
3
|
+
exports.submitEvmTransaction = exports.addTransactionBatch = exports.findAndUpdateTransactionsInBatch = exports.getAddTransactionBatchParams = exports.toBatchTxParams = exports.waitForTxConfirmation = exports.handleMobileHardwareWalletDelay = exports.handleApprovalDelay = exports.addSyntheticTransaction = exports.generateActionId = exports.addTransaction = exports.checkIsDelegatedAccount = exports.updateTransaction = exports.getTransactionMetaByHash = exports.getTransactionMetaById = exports.getTransactions = exports.calculateGasFees = exports.getTxGasEstimates = exports.getGasFeeEstimates = void 0;
|
|
4
4
|
/* eslint-disable @typescript-eslint/explicit-function-return-type */
|
|
5
5
|
const bridge_controller_1 = require("@metamask/bridge-controller");
|
|
6
6
|
const controller_utils_1 = require("@metamask/controller-utils");
|
|
7
7
|
const transaction_controller_1 = require("@metamask/transaction-controller");
|
|
8
8
|
const utils_1 = require("@metamask/utils");
|
|
9
|
+
const bignumber_js_1 = require("bignumber.js");
|
|
9
10
|
const accounts_1 = require("./accounts.cjs");
|
|
10
|
-
const gas_1 = require("./gas.cjs");
|
|
11
11
|
const network_1 = require("./network.cjs");
|
|
12
12
|
const constants_1 = require("../constants.cjs");
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
const getGasFeeEstimates = async (messenger, args) => {
|
|
14
|
+
const { estimates } = await messenger.call('TransactionController:estimateGasFee', args);
|
|
15
|
+
if (bridge_controller_1.BRIDGE_PREFERRED_GAS_ESTIMATE in estimates &&
|
|
16
|
+
typeof estimates[bridge_controller_1.BRIDGE_PREFERRED_GAS_ESTIMATE] === 'object' &&
|
|
17
|
+
'maxFeePerGas' in estimates[bridge_controller_1.BRIDGE_PREFERRED_GAS_ESTIMATE] &&
|
|
18
|
+
'maxPriorityFeePerGas' in estimates[bridge_controller_1.BRIDGE_PREFERRED_GAS_ESTIMATE]) {
|
|
19
|
+
return estimates[bridge_controller_1.BRIDGE_PREFERRED_GAS_ESTIMATE];
|
|
20
|
+
}
|
|
21
|
+
return {};
|
|
22
|
+
};
|
|
23
|
+
exports.getGasFeeEstimates = getGasFeeEstimates;
|
|
24
|
+
/**
|
|
25
|
+
* Get the gas fee estimates for a transaction
|
|
26
|
+
*
|
|
27
|
+
* @param messenger - The messenger for the gas fee estimates
|
|
28
|
+
* @param estimateGasFeeParams - The parameters for the {@link TransactionController.estimateGasFee} method
|
|
29
|
+
|
|
30
|
+
* @returns The gas fee estimates for the transaction
|
|
31
|
+
*/
|
|
32
|
+
const getTxGasEstimates = async (messenger, estimateGasFeeParams) => {
|
|
33
|
+
const { gasFeeEstimates } = messenger.call('GasFeeController:getState');
|
|
34
|
+
const estimatedBaseFee = 'estimatedBaseFee' in gasFeeEstimates
|
|
35
|
+
? gasFeeEstimates.estimatedBaseFee
|
|
36
|
+
: '0';
|
|
37
|
+
// Get transaction's 1559 gas fee estimates
|
|
38
|
+
const { maxFeePerGas, maxPriorityFeePerGas } = await (0, exports.getGasFeeEstimates)(messenger, estimateGasFeeParams);
|
|
39
|
+
/**
|
|
40
|
+
* @deprecated this is unused
|
|
41
|
+
*/
|
|
42
|
+
const baseAndPriorityFeePerGas = maxPriorityFeePerGas
|
|
43
|
+
? new bignumber_js_1.BigNumber(estimatedBaseFee, 10)
|
|
44
|
+
.times(10 ** 9)
|
|
45
|
+
.plus(maxPriorityFeePerGas, 16)
|
|
46
|
+
: undefined;
|
|
16
47
|
return {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
48
|
+
baseAndPriorityFeePerGas,
|
|
49
|
+
maxFeePerGas,
|
|
50
|
+
maxPriorityFeePerGas,
|
|
51
|
+
};
|
|
52
|
+
};
|
|
53
|
+
exports.getTxGasEstimates = getTxGasEstimates;
|
|
54
|
+
const calculateGasFees = async (skipGasFields, messenger, { chainId: _, gasLimit, ...trade }, networkClientId, chainId, txFee) => {
|
|
55
|
+
if (skipGasFields) {
|
|
56
|
+
return {};
|
|
57
|
+
}
|
|
58
|
+
if (txFee) {
|
|
59
|
+
return { ...txFee, gas: gasLimit?.toString() };
|
|
60
|
+
}
|
|
61
|
+
const transactionParams = {
|
|
62
|
+
...trade,
|
|
63
|
+
gas: gasLimit?.toString(),
|
|
64
|
+
data: trade.data,
|
|
65
|
+
to: trade.to,
|
|
66
|
+
value: trade.value,
|
|
23
67
|
};
|
|
68
|
+
const { maxFeePerGas, maxPriorityFeePerGas } = await (0, exports.getTxGasEstimates)(messenger, {
|
|
69
|
+
transactionParams,
|
|
70
|
+
networkClientId,
|
|
71
|
+
chainId,
|
|
72
|
+
});
|
|
73
|
+
const maxGasLimit = (0, controller_utils_1.toHex)(transactionParams.gas ?? 0);
|
|
74
|
+
return {
|
|
75
|
+
maxFeePerGas,
|
|
76
|
+
maxPriorityFeePerGas,
|
|
77
|
+
gas: maxGasLimit,
|
|
78
|
+
};
|
|
79
|
+
};
|
|
80
|
+
exports.calculateGasFees = calculateGasFees;
|
|
81
|
+
const getTransactions = (messenger) => {
|
|
82
|
+
return messenger.call('TransactionController:getState').transactions ?? [];
|
|
83
|
+
};
|
|
84
|
+
exports.getTransactions = getTransactions;
|
|
85
|
+
const getTransactionMetaById = (messenger, txId) => {
|
|
86
|
+
return (0, exports.getTransactions)(messenger).find((tx) => tx.id === txId);
|
|
87
|
+
};
|
|
88
|
+
exports.getTransactionMetaById = getTransactionMetaById;
|
|
89
|
+
const getTransactionMetaByHash = (messenger, txHash) => {
|
|
90
|
+
return (0, exports.getTransactions)(messenger).find((tx) => tx.hash === txHash);
|
|
91
|
+
};
|
|
92
|
+
exports.getTransactionMetaByHash = getTransactionMetaByHash;
|
|
93
|
+
const updateTransaction = (messenger, txMeta, txMetaUpdates, note) => {
|
|
94
|
+
return messenger.call('TransactionController:updateTransaction', { ...txMeta, ...txMetaUpdates }, note);
|
|
95
|
+
};
|
|
96
|
+
exports.updateTransaction = updateTransaction;
|
|
97
|
+
const checkIsDelegatedAccount = async (messenger, fromAddress, chainIds) => {
|
|
98
|
+
try {
|
|
99
|
+
const atomicBatchSupport = await messenger.call('TransactionController:isAtomicBatchSupported', {
|
|
100
|
+
address: fromAddress,
|
|
101
|
+
chainIds,
|
|
102
|
+
});
|
|
103
|
+
return atomicBatchSupport.some((entry) => entry.isSupported && entry.delegationAddress);
|
|
104
|
+
}
|
|
105
|
+
catch {
|
|
106
|
+
return false;
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
exports.checkIsDelegatedAccount = checkIsDelegatedAccount;
|
|
110
|
+
const waitForHashAndReturnFinalTxMeta = async (messenger, hashPromise) => {
|
|
111
|
+
const txHash = await hashPromise;
|
|
112
|
+
const finalTransactionMeta = (0, exports.getTransactionMetaByHash)(messenger, txHash);
|
|
113
|
+
if (!finalTransactionMeta) {
|
|
114
|
+
throw new Error('Failed to submit cross-chain swap tx: txMeta for txHash was not found');
|
|
115
|
+
}
|
|
116
|
+
return finalTransactionMeta;
|
|
117
|
+
};
|
|
118
|
+
const addTransaction = async (messenger, ...args) => {
|
|
119
|
+
const { result } = await messenger.call('TransactionController:addTransaction', ...args);
|
|
120
|
+
return await waitForHashAndReturnFinalTxMeta(messenger, result);
|
|
121
|
+
};
|
|
122
|
+
exports.addTransaction = addTransaction;
|
|
123
|
+
const generateActionId = () => (Date.now() + Math.random()).toString();
|
|
124
|
+
exports.generateActionId = generateActionId;
|
|
125
|
+
/**
|
|
126
|
+
* Adds a synthetic transaction to the TransactionController to display pending intent orders in the UI
|
|
127
|
+
*
|
|
128
|
+
* @param messenger - The messenger to use for the transaction
|
|
129
|
+
* @param args - The arguments for the transaction
|
|
130
|
+
* @returns The transaction meta
|
|
131
|
+
*/
|
|
132
|
+
const addSyntheticTransaction = async (messenger, ...args) => {
|
|
133
|
+
const { transactionMeta } = await messenger.call('TransactionController:addTransaction', args[0], {
|
|
134
|
+
origin: 'metamask',
|
|
135
|
+
actionId: (0, exports.generateActionId)(),
|
|
136
|
+
isStateOnly: true,
|
|
137
|
+
...args[1],
|
|
138
|
+
});
|
|
139
|
+
return transactionMeta;
|
|
24
140
|
};
|
|
25
|
-
exports.
|
|
141
|
+
exports.addSyntheticTransaction = addSyntheticTransaction;
|
|
26
142
|
const handleApprovalDelay = async (srcChainId) => {
|
|
27
143
|
if ([bridge_controller_1.ChainId.LINEA, bridge_controller_1.ChainId.BASE].includes(srcChainId)) {
|
|
28
144
|
const debugLog = (0, utils_1.createProjectLogger)('bridge');
|
|
@@ -46,11 +162,21 @@ const handleMobileHardwareWalletDelay = async (requireApproval) => {
|
|
|
46
162
|
}
|
|
47
163
|
};
|
|
48
164
|
exports.handleMobileHardwareWalletDelay = handleMobileHardwareWalletDelay;
|
|
165
|
+
/**
|
|
166
|
+
* Waits until a given transaction (by id) reaches confirmed/finalized status or fails/times out.
|
|
167
|
+
*
|
|
168
|
+
* @deprecated use addTransaction util
|
|
169
|
+
* @param messenger - the BridgeStatusControllerMessenger
|
|
170
|
+
* @param txId - the transaction ID
|
|
171
|
+
* @param options - the options for the timeout and poll
|
|
172
|
+
* @param options.timeoutMs - the timeout in milliseconds
|
|
173
|
+
* @param options.pollMs - the poll interval in milliseconds
|
|
174
|
+
* @returns the transaction meta
|
|
175
|
+
*/
|
|
49
176
|
const waitForTxConfirmation = async (messenger, txId, { timeoutMs = 5 * 60000, pollMs = 3000, } = {}) => {
|
|
50
177
|
const start = Date.now();
|
|
51
178
|
while (true) {
|
|
52
|
-
const
|
|
53
|
-
const meta = transactions.find((tx) => tx.id === txId);
|
|
179
|
+
const meta = (0, exports.getTransactionMetaById)(messenger, txId);
|
|
54
180
|
if (meta) {
|
|
55
181
|
if (meta.status === transaction_controller_1.TransactionStatus.confirmed) {
|
|
56
182
|
return meta;
|
|
@@ -87,6 +213,7 @@ const toBatchTxParams = (skipGasFields, { chainId, gasLimit, ...trade }, { maxFe
|
|
|
87
213
|
};
|
|
88
214
|
exports.toBatchTxParams = toBatchTxParams;
|
|
89
215
|
const getAddTransactionBatchParams = async ({ messenger, isBridgeTx, approval, resetApproval, trade, quoteResponse: { quote: { feeData: { txFee }, gasIncluded, gasIncluded7702, gasSponsored, }, sentAmount, toTokenAmount, }, requireApproval = false, isDelegatedAccount = false, }) => {
|
|
216
|
+
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
|
|
90
217
|
const isGasless = gasIncluded || gasIncluded7702;
|
|
91
218
|
const selectedAccount = (0, accounts_1.getAccountByAddress)(messenger, trade.from);
|
|
92
219
|
if (!selectedAccount) {
|
|
@@ -99,10 +226,14 @@ const getAddTransactionBatchParams = async ({ messenger, isBridgeTx, approval, r
|
|
|
99
226
|
// Enable 7702 batching when the quote includes gasless 7702 support,
|
|
100
227
|
// or when the account is already delegated (to avoid the in-flight
|
|
101
228
|
// transaction limit for delegated accounts)
|
|
102
|
-
|
|
229
|
+
let disable7702 = !skipGasFields && !isDelegatedAccount;
|
|
230
|
+
// For gasless transactions with STX/sendBundle we keep disabling 7702.
|
|
231
|
+
if (gasIncluded && !gasIncluded7702) {
|
|
232
|
+
disable7702 = true;
|
|
233
|
+
}
|
|
103
234
|
const transactions = [];
|
|
104
235
|
if (resetApproval) {
|
|
105
|
-
const gasFees = await (0,
|
|
236
|
+
const gasFees = await (0, exports.calculateGasFees)(skipGasFields, messenger, resetApproval, networkClientId, hexChainId, isGasless ? txFee : undefined);
|
|
106
237
|
transactions.push({
|
|
107
238
|
type: isBridgeTx
|
|
108
239
|
? transaction_controller_1.TransactionType.bridgeApproval
|
|
@@ -111,7 +242,7 @@ const getAddTransactionBatchParams = async ({ messenger, isBridgeTx, approval, r
|
|
|
111
242
|
});
|
|
112
243
|
}
|
|
113
244
|
if (approval) {
|
|
114
|
-
const gasFees = await (0,
|
|
245
|
+
const gasFees = await (0, exports.calculateGasFees)(skipGasFields, messenger, approval, networkClientId, hexChainId, isGasless ? txFee : undefined);
|
|
115
246
|
transactions.push({
|
|
116
247
|
type: isBridgeTx
|
|
117
248
|
? transaction_controller_1.TransactionType.bridgeApproval
|
|
@@ -119,7 +250,7 @@ const getAddTransactionBatchParams = async ({ messenger, isBridgeTx, approval, r
|
|
|
119
250
|
params: (0, exports.toBatchTxParams)(skipGasFields, approval, gasFees),
|
|
120
251
|
});
|
|
121
252
|
}
|
|
122
|
-
const gasFees = await (0,
|
|
253
|
+
const gasFees = await (0, exports.calculateGasFees)(skipGasFields, messenger, trade, networkClientId, hexChainId, isGasless ? txFee : undefined);
|
|
123
254
|
transactions.push({
|
|
124
255
|
type: isBridgeTx ? transaction_controller_1.TransactionType.bridge : transaction_controller_1.TransactionType.swap,
|
|
125
256
|
params: (0, exports.toBatchTxParams)(skipGasFields, trade, gasFees),
|
|
@@ -142,14 +273,15 @@ const getAddTransactionBatchParams = async ({ messenger, isBridgeTx, approval, r
|
|
|
142
273
|
};
|
|
143
274
|
exports.getAddTransactionBatchParams = getAddTransactionBatchParams;
|
|
144
275
|
const findAndUpdateTransactionsInBatch = ({ messenger, batchId, txDataByType, }) => {
|
|
145
|
-
const txs =
|
|
276
|
+
const txs = (0, exports.getTransactions)(messenger);
|
|
146
277
|
const txBatch = {
|
|
147
278
|
approvalMeta: undefined,
|
|
148
279
|
tradeMeta: undefined,
|
|
149
280
|
};
|
|
150
281
|
// This is a workaround to update the tx type after the tx is signed
|
|
151
282
|
// TODO: remove this once the tx type for batch txs is preserved in the tx controller
|
|
152
|
-
Object.entries(txDataByType)
|
|
283
|
+
const txEntries = Object.entries(txDataByType);
|
|
284
|
+
txEntries.forEach(([txType, txData]) => {
|
|
153
285
|
// Skip types not present in the batch (e.g. swap entry is undefined for bridge txs)
|
|
154
286
|
if (txData === undefined) {
|
|
155
287
|
return;
|
|
@@ -184,13 +316,107 @@ const findAndUpdateTransactionsInBatch = ({ messenger, batchId, txDataByType, })
|
|
|
184
316
|
});
|
|
185
317
|
if (txMeta) {
|
|
186
318
|
const updatedTx = { ...txMeta, type: txType };
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
319
|
+
(0, exports.updateTransaction)(messenger, txMeta, { type: txType }, `Update tx type to ${txType}`);
|
|
320
|
+
const txTypes = [
|
|
321
|
+
transaction_controller_1.TransactionType.bridgeApproval,
|
|
322
|
+
transaction_controller_1.TransactionType.swapApproval,
|
|
323
|
+
];
|
|
324
|
+
txBatch[txTypes.includes(txType) ? 'approvalMeta' : 'tradeMeta'] =
|
|
325
|
+
updatedTx;
|
|
191
326
|
}
|
|
192
327
|
});
|
|
193
328
|
return txBatch;
|
|
194
329
|
};
|
|
195
330
|
exports.findAndUpdateTransactionsInBatch = findAndUpdateTransactionsInBatch;
|
|
331
|
+
const addTransactionBatch = async (messenger, addTransactionBatchFn, ...args) => {
|
|
332
|
+
const txDataByType = {
|
|
333
|
+
[transaction_controller_1.TransactionType.bridgeApproval]: args[0].transactions.find(({ type }) => type === transaction_controller_1.TransactionType.bridgeApproval)?.params.data,
|
|
334
|
+
[transaction_controller_1.TransactionType.swapApproval]: args[0].transactions.find(({ type }) => type === transaction_controller_1.TransactionType.swapApproval)?.params.data,
|
|
335
|
+
[transaction_controller_1.TransactionType.bridge]: args[0].transactions.find(({ type }) => type === transaction_controller_1.TransactionType.bridge)?.params.data,
|
|
336
|
+
[transaction_controller_1.TransactionType.swap]: args[0].transactions.find(({ type }) => type === transaction_controller_1.TransactionType.swap)?.params.data,
|
|
337
|
+
};
|
|
338
|
+
const { batchId } = await addTransactionBatchFn(...args);
|
|
339
|
+
const { approvalMeta, tradeMeta } = (0, exports.findAndUpdateTransactionsInBatch)({
|
|
340
|
+
messenger,
|
|
341
|
+
batchId,
|
|
342
|
+
txDataByType,
|
|
343
|
+
});
|
|
344
|
+
if (!tradeMeta) {
|
|
345
|
+
throw new Error('Failed to update cross-chain swap transaction batch: tradeMeta not found');
|
|
346
|
+
}
|
|
347
|
+
return { approvalMeta, tradeMeta };
|
|
348
|
+
};
|
|
349
|
+
exports.addTransactionBatch = addTransactionBatch;
|
|
350
|
+
// TODO rename
|
|
351
|
+
const getGasFeesForSubmission = async (messenger, transactionParams, networkClientId, chainId, txFee) => {
|
|
352
|
+
const { gas } = transactionParams;
|
|
353
|
+
// If txFee is provided (gasIncluded case), use the quote's gas fees
|
|
354
|
+
// Convert to hex since txFee values from the quote are decimal strings
|
|
355
|
+
if (txFee) {
|
|
356
|
+
return {
|
|
357
|
+
maxFeePerGas: (0, controller_utils_1.toHex)(txFee.maxFeePerGas),
|
|
358
|
+
maxPriorityFeePerGas: (0, controller_utils_1.toHex)(txFee.maxPriorityFeePerGas),
|
|
359
|
+
gas: gas ? (0, controller_utils_1.toHex)(gas) : undefined,
|
|
360
|
+
};
|
|
361
|
+
}
|
|
362
|
+
const { maxFeePerGas, maxPriorityFeePerGas } = await (0, exports.getTxGasEstimates)(messenger, {
|
|
363
|
+
transactionParams,
|
|
364
|
+
chainId,
|
|
365
|
+
networkClientId,
|
|
366
|
+
});
|
|
367
|
+
return {
|
|
368
|
+
maxFeePerGas,
|
|
369
|
+
maxPriorityFeePerGas,
|
|
370
|
+
gas: gas ? (0, controller_utils_1.toHex)(gas) : undefined,
|
|
371
|
+
};
|
|
372
|
+
};
|
|
373
|
+
/**
|
|
374
|
+
* Submits an EVM transaction to the TransactionController
|
|
375
|
+
*
|
|
376
|
+
* @param params - The parameters for the transaction
|
|
377
|
+
* @param params.transactionType - The type of transaction to submit
|
|
378
|
+
* @param params.trade - The trade data to confirm
|
|
379
|
+
* @param params.requireApproval - Whether to require approval for the transaction
|
|
380
|
+
* @param params.txFee - Optional gas fee parameters from the quote (used when gasIncluded is true)
|
|
381
|
+
* @param params.txFee.maxFeePerGas - The maximum fee per gas from the quote
|
|
382
|
+
* @param params.txFee.maxPriorityFeePerGas - The maximum priority fee per gas from the quote
|
|
383
|
+
* @param params.actionId - Optional actionId for pre-submission history (if not provided, one is generated)
|
|
384
|
+
* @param params.messenger - The messenger to use for the transaction
|
|
385
|
+
* @returns The transaction meta
|
|
386
|
+
*/
|
|
387
|
+
const submitEvmTransaction = async ({ messenger, trade, transactionType, requireApproval = false, txFee,
|
|
388
|
+
// Use provided actionId (for pre-submission history) or generate one
|
|
389
|
+
actionId = (0, exports.generateActionId)(), }) => {
|
|
390
|
+
const selectedAccount = (0, accounts_1.getAccountByAddress)(messenger, trade.from);
|
|
391
|
+
if (!selectedAccount) {
|
|
392
|
+
throw new Error('Failed to submit cross-chain swap transaction: unknown account in trade data');
|
|
393
|
+
}
|
|
394
|
+
const hexChainId = (0, bridge_controller_1.formatChainIdToHex)(trade.chainId);
|
|
395
|
+
const networkClientId = (0, network_1.getNetworkClientIdByChainId)(messenger, hexChainId);
|
|
396
|
+
const requestOptions = {
|
|
397
|
+
actionId,
|
|
398
|
+
networkClientId,
|
|
399
|
+
requireApproval,
|
|
400
|
+
type: transactionType,
|
|
401
|
+
origin: 'metamask',
|
|
402
|
+
};
|
|
403
|
+
// Exclude gasLimit from trade to avoid type issues (it can be null)
|
|
404
|
+
const { gasLimit: tradeGasLimit, ...tradeWithoutGasLimit } = trade;
|
|
405
|
+
const transactionParams = {
|
|
406
|
+
...tradeWithoutGasLimit,
|
|
407
|
+
chainId: hexChainId,
|
|
408
|
+
// Only add gasLimit and gas if they're valid (not undefined/null/zero)
|
|
409
|
+
...(tradeGasLimit &&
|
|
410
|
+
tradeGasLimit !== 0 && {
|
|
411
|
+
gasLimit: tradeGasLimit.toString(),
|
|
412
|
+
gas: tradeGasLimit.toString(),
|
|
413
|
+
}),
|
|
414
|
+
};
|
|
415
|
+
const transactionParamsWithMaxGas = {
|
|
416
|
+
...transactionParams,
|
|
417
|
+
...(await getGasFeesForSubmission(messenger, transactionParams, networkClientId, hexChainId, txFee)),
|
|
418
|
+
};
|
|
419
|
+
return await (0, exports.addTransaction)(messenger, transactionParamsWithMaxGas, requestOptions);
|
|
420
|
+
};
|
|
421
|
+
exports.submitEvmTransaction = submitEvmTransaction;
|
|
196
422
|
//# sourceMappingURL=transaction.cjs.map
|