@metamask-previews/transaction-pay-controller 10.6.0-preview-cb897e9 → 10.6.0-preview-6a568504
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 +12 -0
- package/dist/TransactionPayController.cjs +9 -1
- package/dist/TransactionPayController.cjs.map +1 -1
- package/dist/TransactionPayController.d.cts +1 -0
- package/dist/TransactionPayController.d.cts.map +1 -1
- package/dist/TransactionPayController.d.mts +1 -0
- package/dist/TransactionPayController.d.mts.map +1 -1
- package/dist/TransactionPayController.mjs +9 -1
- package/dist/TransactionPayController.mjs.map +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +1 -1
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs.map +1 -1
- package/dist/strategy/bridge/bridge-quotes.cjs +1 -0
- package/dist/strategy/bridge/bridge-quotes.cjs.map +1 -1
- package/dist/strategy/bridge/bridge-quotes.mjs +1 -0
- package/dist/strategy/bridge/bridge-quotes.mjs.map +1 -1
- package/dist/strategy/relay/relay-quotes.cjs +23 -11
- package/dist/strategy/relay/relay-quotes.cjs.map +1 -1
- package/dist/strategy/relay/relay-quotes.mjs +23 -11
- package/dist/strategy/relay/relay-quotes.mjs.map +1 -1
- package/dist/strategy/relay/types.cjs.map +1 -1
- package/dist/strategy/relay/types.d.cts +2 -1
- package/dist/strategy/relay/types.d.cts.map +1 -1
- package/dist/strategy/relay/types.d.mts +2 -1
- package/dist/strategy/relay/types.d.mts.map +1 -1
- package/dist/strategy/relay/types.mjs.map +1 -1
- package/dist/strategy/test/TestStrategy.cjs +6 -0
- package/dist/strategy/test/TestStrategy.cjs.map +1 -1
- package/dist/strategy/test/TestStrategy.d.cts.map +1 -1
- package/dist/strategy/test/TestStrategy.d.mts.map +1 -1
- package/dist/strategy/test/TestStrategy.mjs +6 -0
- package/dist/strategy/test/TestStrategy.mjs.map +1 -1
- package/dist/types.cjs.map +1 -1
- package/dist/types.d.cts +14 -1
- package/dist/types.d.cts.map +1 -1
- package/dist/types.d.mts +14 -1
- package/dist/types.d.mts.map +1 -1
- package/dist/types.mjs.map +1 -1
- package/dist/utils/quotes.cjs +8 -3
- package/dist/utils/quotes.cjs.map +1 -1
- package/dist/utils/quotes.d.cts.map +1 -1
- package/dist/utils/quotes.d.mts.map +1 -1
- package/dist/utils/quotes.mjs +8 -3
- package/dist/utils/quotes.mjs.map +1 -1
- package/dist/utils/source-amounts.cjs +11 -3
- package/dist/utils/source-amounts.cjs.map +1 -1
- package/dist/utils/source-amounts.d.cts.map +1 -1
- package/dist/utils/source-amounts.d.mts.map +1 -1
- package/dist/utils/source-amounts.mjs +11 -3
- package/dist/utils/source-amounts.mjs.map +1 -1
- package/dist/utils/totals.cjs +7 -3
- package/dist/utils/totals.cjs.map +1 -1
- package/dist/utils/totals.d.cts +3 -1
- package/dist/utils/totals.d.cts.map +1 -1
- package/dist/utils/totals.d.mts +3 -1
- package/dist/utils/totals.d.mts.map +1 -1
- package/dist/utils/totals.mjs +7 -3
- package/dist/utils/totals.mjs.map +1 -1
- package/package.json +4 -4
package/dist/utils/quotes.mjs
CHANGED
|
@@ -22,9 +22,10 @@ export async function updateQuotes(request) {
|
|
|
22
22
|
return false;
|
|
23
23
|
}
|
|
24
24
|
log('Updating quotes', { transactionId });
|
|
25
|
-
const { paymentToken, sourceAmounts, tokens } = transactionData;
|
|
25
|
+
const { isMaxAmount, paymentToken, sourceAmounts, tokens } = transactionData;
|
|
26
26
|
const requests = buildQuoteRequests({
|
|
27
27
|
from: transaction.txParams.from,
|
|
28
|
+
isMaxAmount: isMaxAmount ?? false,
|
|
28
29
|
paymentToken,
|
|
29
30
|
sourceAmounts,
|
|
30
31
|
tokens,
|
|
@@ -36,8 +37,9 @@ export async function updateQuotes(request) {
|
|
|
36
37
|
try {
|
|
37
38
|
const { batchTransactions, quotes } = await getQuotes(transaction, requests, messenger);
|
|
38
39
|
const totals = calculateTotals({
|
|
39
|
-
|
|
40
|
+
isMaxAmount,
|
|
40
41
|
messenger,
|
|
42
|
+
quotes: quotes,
|
|
41
43
|
tokens,
|
|
42
44
|
transaction,
|
|
43
45
|
});
|
|
@@ -87,6 +89,7 @@ function syncTransaction({ batchTransactions, messenger, paymentToken, totals, t
|
|
|
87
89
|
bridgeFeeFiat: totals.fees.provider.usd,
|
|
88
90
|
chainId: paymentToken.chainId,
|
|
89
91
|
networkFeeFiat: totals.fees.sourceNetwork.estimate.usd,
|
|
92
|
+
targetFiat: totals.targetAmount.usd,
|
|
90
93
|
tokenAddress: paymentToken.address,
|
|
91
94
|
totalFiat: totals.total.usd,
|
|
92
95
|
};
|
|
@@ -133,13 +136,14 @@ export async function refreshQuotes(messenger, updateTransactionData) {
|
|
|
133
136
|
*
|
|
134
137
|
* @param request - Request parameters.
|
|
135
138
|
* @param request.from - Address from which the transaction is sent.
|
|
139
|
+
* @param request.isMaxAmount - Whether the transaction is a maximum amount transaction.
|
|
136
140
|
* @param request.paymentToken - Payment token used for the transaction.
|
|
137
141
|
* @param request.sourceAmounts - Source amounts for the transaction.
|
|
138
142
|
* @param request.tokens - Required tokens for the transaction.
|
|
139
143
|
* @param request.transactionId - ID of the transaction.
|
|
140
144
|
* @returns Array of quote requests.
|
|
141
145
|
*/
|
|
142
|
-
function buildQuoteRequests({ from, paymentToken, sourceAmounts, tokens, transactionId, }) {
|
|
146
|
+
function buildQuoteRequests({ from, isMaxAmount, paymentToken, sourceAmounts, tokens, transactionId, }) {
|
|
143
147
|
if (!paymentToken) {
|
|
144
148
|
return [];
|
|
145
149
|
}
|
|
@@ -147,6 +151,7 @@ function buildQuoteRequests({ from, paymentToken, sourceAmounts, tokens, transac
|
|
|
147
151
|
const token = tokens.find((singleToken) => singleToken.address === sourceAmount.targetTokenAddress);
|
|
148
152
|
return {
|
|
149
153
|
from,
|
|
154
|
+
isMaxAmount,
|
|
150
155
|
sourceBalanceRaw: paymentToken.balanceRaw,
|
|
151
156
|
sourceTokenAmount: sourceAmount.sourceAmountRaw,
|
|
152
157
|
sourceChainId: paymentToken.chainId,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"quotes.mjs","sourceRoot":"","sources":["../../src/utils/quotes.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,yCAAyC;AAIrE,OAAO,EAAE,kBAAkB,EAAE,wBAAwB;AAErD,OAAO,EAAE,WAAW,EAAE,iBAAiB,EAAE,uBAAmB;AAC5D,OAAO,EAAE,eAAe,EAAE,qBAAiB;AAC3C,OAAO,EAAE,cAAc,EAAE,iBAAiB,EAAE,0BAAsB;AAClE,OAAO,EAAE,aAAa,EAAE,sBAAkB;AAa1C,MAAM,wBAAwB,GAAG,EAAE,GAAG,IAAI,CAAC,CAAC,aAAa;AAEzD,MAAM,GAAG,GAAG,kBAAkB,CAAC,aAAa,EAAE,QAAQ,CAAC,CAAC;AASxD;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,YAAY,CAChC,OAA4B;IAE5B,MAAM,EAAE,SAAS,EAAE,eAAe,EAAE,aAAa,EAAE,qBAAqB,EAAE,GACxE,OAAO,CAAC;IAEV,MAAM,WAAW,GAAG,cAAc,CAAC,aAAa,EAAE,SAAS,CAAC,CAAC;IAE7D,IAAI,CAAC,WAAW,IAAI,CAAC,eAAe,EAAE,CAAC;QACrC,MAAM,IAAI,KAAK,CAAC,uBAAuB,CAAC,CAAC;IAC3C,CAAC;IAED,IAAI,WAAW,EAAE,MAAM,KAAK,iBAAiB,CAAC,UAAU,EAAE,CAAC;QACzD,OAAO,KAAK,CAAC;IACf,CAAC;IAED,GAAG,CAAC,iBAAiB,EAAE,EAAE,aAAa,EAAE,CAAC,CAAC;IAE1C,MAAM,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,EAAE,GAAG,eAAe,CAAC;IAEhE,MAAM,QAAQ,GAAG,kBAAkB,CAAC;QAClC,IAAI,EAAE,WAAW,CAAC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,CAAC","sourcesContent":["import { TransactionStatus } from '@metamask/transaction-controller';\nimport type { BatchTransaction } from '@metamask/transaction-controller';\nimport type { TransactionMeta } from '@metamask/transaction-controller';\nimport type { Hex, Json } from '@metamask/utils';\nimport { createModuleLogger } from '@metamask/utils';\n\nimport { getStrategy, getStrategyByName } from './strategy';\nimport { calculateTotals } from './totals';\nimport { getTransaction, updateTransaction } from './transaction';\nimport { projectLogger } from '../logger';\nimport type {\n QuoteRequest,\n TransactionData,\n TransactionPayControllerMessenger,\n TransactionPayQuote,\n TransactionPayRequiredToken,\n TransactionPaySourceAmount,\n TransactionPayTotals,\n TransactionPaymentToken,\n UpdateTransactionDataCallback,\n} from '../types';\n\nconst DEFAULT_REFRESH_INTERVAL = 30 * 1000; // 30 Seconds\n\nconst log = createModuleLogger(projectLogger, 'quotes');\n\nexport type UpdateQuotesRequest = {\n messenger: TransactionPayControllerMessenger;\n transactionData: TransactionData | undefined;\n transactionId: string;\n updateTransactionData: UpdateTransactionDataCallback;\n};\n\n/**\n * Update the quotes for a specific transaction.\n *\n * @param request - Request parameters.\n * @returns Boolean indicating if the quotes were updated.\n */\nexport async function updateQuotes(\n request: UpdateQuotesRequest,\n): Promise<boolean> {\n const { messenger, transactionData, transactionId, updateTransactionData } =\n request;\n\n const transaction = getTransaction(transactionId, messenger);\n\n if (!transaction || !transactionData) {\n throw new Error('Transaction not found');\n }\n\n if (transaction?.status !== TransactionStatus.unapproved) {\n return false;\n }\n\n log('Updating quotes', { transactionId });\n\n const { paymentToken, sourceAmounts, tokens } = transactionData;\n\n const requests = buildQuoteRequests({\n from: transaction.txParams.from as Hex,\n paymentToken,\n sourceAmounts,\n tokens,\n transactionId,\n });\n\n updateTransactionData(transactionId, (data) => {\n data.isLoading = true;\n });\n\n try {\n const { batchTransactions, quotes } = await getQuotes(\n transaction,\n requests,\n messenger,\n );\n\n const totals = calculateTotals({\n quotes: quotes as TransactionPayQuote<unknown>[],\n messenger,\n tokens,\n transaction,\n });\n\n log('Calculated totals', { transactionId, totals });\n\n syncTransaction({\n batchTransactions,\n messenger: messenger as never,\n paymentToken,\n totals,\n transactionId,\n });\n\n updateTransactionData(transactionId, (data) => {\n data.quotes = quotes as never;\n data.quotesLastUpdated = Date.now();\n data.totals = totals;\n });\n } finally {\n updateTransactionData(transactionId, (data) => {\n data.isLoading = false;\n });\n }\n\n return true;\n}\n\n/**\n * Sync batch transactions to the transaction meta.\n *\n * @param request - Request object.\n * @param request.batchTransactions - Batch transactions to sync.\n * @param request.messenger - Messenger instance.\n * @param request.paymentToken - Payment token used.\n * @param request.totals - Calculated totals.\n * @param request.transactionId - ID of the transaction to sync.\n */\nfunction syncTransaction({\n batchTransactions,\n messenger,\n paymentToken,\n totals,\n transactionId,\n}: {\n batchTransactions: BatchTransaction[];\n messenger: TransactionPayControllerMessenger;\n paymentToken: TransactionPaymentToken | undefined;\n totals: TransactionPayTotals;\n transactionId: string;\n}): void {\n if (!paymentToken) {\n return;\n }\n\n updateTransaction(\n {\n transactionId,\n messenger: messenger as never,\n note: 'Update transaction pay data',\n },\n (tx: TransactionMeta) => {\n tx.batchTransactions = batchTransactions;\n tx.batchTransactionsOptions = {};\n\n tx.metamaskPay = {\n bridgeFeeFiat: totals.fees.provider.usd,\n chainId: paymentToken.chainId,\n networkFeeFiat: totals.fees.sourceNetwork.estimate.usd,\n tokenAddress: paymentToken.address,\n totalFiat: totals.total.usd,\n };\n },\n );\n}\n\n/**\n * Refresh quotes for all transactions if expired.\n *\n * @param messenger - Messenger instance.\n * @param updateTransactionData - Callback to update transaction data.\n */\nexport async function refreshQuotes(\n messenger: TransactionPayControllerMessenger,\n updateTransactionData: UpdateTransactionDataCallback,\n): Promise<void> {\n const state = messenger.call('TransactionPayController:getState');\n const transactionIds = Object.keys(state.transactionData);\n\n for (const transactionId of transactionIds) {\n const transactionData = state.transactionData[transactionId];\n const { isLoading, quotes, quotesLastUpdated } = transactionData;\n\n if (isLoading || !quotes?.length) {\n continue;\n }\n\n const strategyName = quotes[0].strategy;\n const strategy = getStrategyByName(strategyName);\n\n const refreshInterval =\n (await strategy.getRefreshInterval?.({\n chainId: quotes[0].request.sourceChainId,\n messenger,\n })) ?? DEFAULT_REFRESH_INTERVAL;\n\n const isExpired = Date.now() - (quotesLastUpdated ?? 0) > refreshInterval;\n\n if (!isExpired) {\n continue;\n }\n\n const isUpdated = await updateQuotes({\n messenger,\n transactionData,\n transactionId,\n updateTransactionData,\n });\n\n if (isUpdated) {\n log('Refreshed quotes', { transactionId, strategy: strategyName });\n }\n }\n}\n\n/**\n * Build quote requests required to retrieve quotes.\n *\n * @param request - Request parameters.\n * @param request.from - Address from which the transaction is sent.\n * @param request.paymentToken - Payment token used for the transaction.\n * @param request.sourceAmounts - Source amounts for the transaction.\n * @param request.tokens - Required tokens for the transaction.\n * @param request.transactionId - ID of the transaction.\n * @returns Array of quote requests.\n */\nfunction buildQuoteRequests({\n from,\n paymentToken,\n sourceAmounts,\n tokens,\n transactionId,\n}: {\n from: Hex;\n paymentToken: TransactionPaymentToken | undefined;\n sourceAmounts: TransactionPaySourceAmount[] | undefined;\n tokens: TransactionPayRequiredToken[];\n transactionId: string;\n}): QuoteRequest[] {\n if (!paymentToken) {\n return [];\n }\n\n const requests = (sourceAmounts ?? []).map((sourceAmount) => {\n const token = tokens.find(\n (singleToken) => singleToken.address === sourceAmount.targetTokenAddress,\n ) as TransactionPayRequiredToken;\n\n return {\n from,\n sourceBalanceRaw: paymentToken.balanceRaw,\n sourceTokenAmount: sourceAmount.sourceAmountRaw,\n sourceChainId: paymentToken.chainId,\n sourceTokenAddress: paymentToken.address,\n targetAmountMinimum: token.allowUnderMinimum ? '0' : token.amountRaw,\n targetChainId: token.chainId,\n targetTokenAddress: token.address,\n };\n });\n\n if (!requests.length) {\n log('No quote requests', { transactionId });\n }\n\n return requests;\n}\n\n/**\n * Retrieve quotes for a transaction.\n *\n * @param transaction - Transaction metadata.\n * @param requests - Quote requests.\n * @param messenger - Controller messenger.\n * @returns An object containing batch transactions and quotes.\n */\nasync function getQuotes(\n transaction: TransactionMeta,\n requests: QuoteRequest[],\n messenger: TransactionPayControllerMessenger,\n): Promise<{\n batchTransactions: BatchTransaction[];\n quotes: TransactionPayQuote<Json>[];\n}> {\n const { id: transactionId } = transaction;\n const strategy = getStrategy(messenger as never, transaction);\n let quotes: TransactionPayQuote<Json>[] | undefined = [];\n\n try {\n quotes = requests?.length\n ? ((await strategy.getQuotes({\n messenger,\n requests,\n transaction,\n })) as TransactionPayQuote<Json>[])\n : [];\n } catch (error) {\n log('Error fetching quotes', { error, transactionId });\n }\n\n log('Updated', { transactionId, quotes });\n\n const batchTransactions =\n quotes?.length && strategy.getBatchTransactions\n ? await strategy.getBatchTransactions({\n messenger,\n quotes,\n })\n : [];\n\n log('Batch transactions', { transactionId, batchTransactions });\n\n return {\n batchTransactions,\n quotes,\n };\n}\n"]}
|
|
1
|
+
{"version":3,"file":"quotes.mjs","sourceRoot":"","sources":["../../src/utils/quotes.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,yCAAyC;AAIrE,OAAO,EAAE,kBAAkB,EAAE,wBAAwB;AAErD,OAAO,EAAE,WAAW,EAAE,iBAAiB,EAAE,uBAAmB;AAC5D,OAAO,EAAE,eAAe,EAAE,qBAAiB;AAC3C,OAAO,EAAE,cAAc,EAAE,iBAAiB,EAAE,0BAAsB;AAClE,OAAO,EAAE,aAAa,EAAE,sBAAkB;AAa1C,MAAM,wBAAwB,GAAG,EAAE,GAAG,IAAI,CAAC,CAAC,aAAa;AAEzD,MAAM,GAAG,GAAG,kBAAkB,CAAC,aAAa,EAAE,QAAQ,CAAC,CAAC;AASxD;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,YAAY,CAChC,OAA4B;IAE5B,MAAM,EAAE,SAAS,EAAE,eAAe,EAAE,aAAa,EAAE,qBAAqB,EAAE,GACxE,OAAO,CAAC;IAEV,MAAM,WAAW,GAAG,cAAc,CAAC,aAAa,EAAE,SAAS,CAA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ACZ,CAAC,CAAiC;YACrC,CAAC,CAAC,EAAE,CAAC;IACT,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,GAAG,CAAC,uBAAuB,EAAE,EAAE,KAAK,EAAE,aAAa,EAAE,CAAC,CAAC;IACzD,CAAC;IAED,GAAG,CAAC,SAAS,EAAE,EAAE,aAAa,EAAE,MAAM,EAAE,CAAC,CAAC;IAE1C,MAAM,iBAAiB,GACrB,MAAM,EAAE,MAAM,IAAI,QAAQ,CAAC,oBAAoB;QAC7C,CAAC,CAAC,MAAM,QAAQ,CAAC,oBAAoB,CAAC;YAClC,SAAS;YACT,MAAM;SACP,CAAC;QACJ,CAAC,CAAC,EAAE,CAAC;IAET,GAAG,CAAC,oBAAoB,EAAE,EAAE,aAAa,EAAE,iBAAiB,EAAE,CAAC,CAAC;IAEhE,OAAO;QACL,iBAAiB;QACjB,MAAM;KACP,CAAC;AACJ,CAAC","sourcesContent":["import { TransactionStatus } from '@metamask/transaction-controller';\nimport type { BatchTransaction } from '@metamask/transaction-controller';\nimport type { TransactionMeta } from '@metamask/transaction-controller';\nimport type { Hex, Json } from '@metamask/utils';\nimport { createModuleLogger } from '@metamask/utils';\n\nimport { getStrategy, getStrategyByName } from './strategy';\nimport { calculateTotals } from './totals';\nimport { getTransaction, updateTransaction } from './transaction';\nimport { projectLogger } from '../logger';\nimport type {\n QuoteRequest,\n TransactionData,\n TransactionPayControllerMessenger,\n TransactionPayQuote,\n TransactionPayRequiredToken,\n TransactionPaySourceAmount,\n TransactionPayTotals,\n TransactionPaymentToken,\n UpdateTransactionDataCallback,\n} from '../types';\n\nconst DEFAULT_REFRESH_INTERVAL = 30 * 1000; // 30 Seconds\n\nconst log = createModuleLogger(projectLogger, 'quotes');\n\nexport type UpdateQuotesRequest = {\n messenger: TransactionPayControllerMessenger;\n transactionData: TransactionData | undefined;\n transactionId: string;\n updateTransactionData: UpdateTransactionDataCallback;\n};\n\n/**\n * Update the quotes for a specific transaction.\n *\n * @param request - Request parameters.\n * @returns Boolean indicating if the quotes were updated.\n */\nexport async function updateQuotes(\n request: UpdateQuotesRequest,\n): Promise<boolean> {\n const { messenger, transactionData, transactionId, updateTransactionData } =\n request;\n\n const transaction = getTransaction(transactionId, messenger);\n\n if (!transaction || !transactionData) {\n throw new Error('Transaction not found');\n }\n\n if (transaction?.status !== TransactionStatus.unapproved) {\n return false;\n }\n\n log('Updating quotes', { transactionId });\n\n const { isMaxAmount, paymentToken, sourceAmounts, tokens } = transactionData;\n\n const requests = buildQuoteRequests({\n from: transaction.txParams.from as Hex,\n isMaxAmount: isMaxAmount ?? false,\n paymentToken,\n sourceAmounts,\n tokens,\n transactionId,\n });\n\n updateTransactionData(transactionId, (data) => {\n data.isLoading = true;\n });\n\n try {\n const { batchTransactions, quotes } = await getQuotes(\n transaction,\n requests,\n messenger,\n );\n\n const totals = calculateTotals({\n isMaxAmount,\n messenger,\n quotes: quotes as TransactionPayQuote<unknown>[],\n tokens,\n transaction,\n });\n\n log('Calculated totals', { transactionId, totals });\n\n syncTransaction({\n batchTransactions,\n messenger: messenger as never,\n paymentToken,\n totals,\n transactionId,\n });\n\n updateTransactionData(transactionId, (data) => {\n data.quotes = quotes as never;\n data.quotesLastUpdated = Date.now();\n data.totals = totals;\n });\n } finally {\n updateTransactionData(transactionId, (data) => {\n data.isLoading = false;\n });\n }\n\n return true;\n}\n\n/**\n * Sync batch transactions to the transaction meta.\n *\n * @param request - Request object.\n * @param request.batchTransactions - Batch transactions to sync.\n * @param request.messenger - Messenger instance.\n * @param request.paymentToken - Payment token used.\n * @param request.totals - Calculated totals.\n * @param request.transactionId - ID of the transaction to sync.\n */\nfunction syncTransaction({\n batchTransactions,\n messenger,\n paymentToken,\n totals,\n transactionId,\n}: {\n batchTransactions: BatchTransaction[];\n messenger: TransactionPayControllerMessenger;\n paymentToken: TransactionPaymentToken | undefined;\n totals: TransactionPayTotals;\n transactionId: string;\n}): void {\n if (!paymentToken) {\n return;\n }\n\n updateTransaction(\n {\n transactionId,\n messenger: messenger as never,\n note: 'Update transaction pay data',\n },\n (tx: TransactionMeta) => {\n tx.batchTransactions = batchTransactions;\n tx.batchTransactionsOptions = {};\n\n tx.metamaskPay = {\n bridgeFeeFiat: totals.fees.provider.usd,\n chainId: paymentToken.chainId,\n networkFeeFiat: totals.fees.sourceNetwork.estimate.usd,\n targetFiat: totals.targetAmount.usd,\n tokenAddress: paymentToken.address,\n totalFiat: totals.total.usd,\n };\n },\n );\n}\n\n/**\n * Refresh quotes for all transactions if expired.\n *\n * @param messenger - Messenger instance.\n * @param updateTransactionData - Callback to update transaction data.\n */\nexport async function refreshQuotes(\n messenger: TransactionPayControllerMessenger,\n updateTransactionData: UpdateTransactionDataCallback,\n): Promise<void> {\n const state = messenger.call('TransactionPayController:getState');\n const transactionIds = Object.keys(state.transactionData);\n\n for (const transactionId of transactionIds) {\n const transactionData = state.transactionData[transactionId];\n const { isLoading, quotes, quotesLastUpdated } = transactionData;\n\n if (isLoading || !quotes?.length) {\n continue;\n }\n\n const strategyName = quotes[0].strategy;\n const strategy = getStrategyByName(strategyName);\n\n const refreshInterval =\n (await strategy.getRefreshInterval?.({\n chainId: quotes[0].request.sourceChainId,\n messenger,\n })) ?? DEFAULT_REFRESH_INTERVAL;\n\n const isExpired = Date.now() - (quotesLastUpdated ?? 0) > refreshInterval;\n\n if (!isExpired) {\n continue;\n }\n\n const isUpdated = await updateQuotes({\n messenger,\n transactionData,\n transactionId,\n updateTransactionData,\n });\n\n if (isUpdated) {\n log('Refreshed quotes', { transactionId, strategy: strategyName });\n }\n }\n}\n\n/**\n * Build quote requests required to retrieve quotes.\n *\n * @param request - Request parameters.\n * @param request.from - Address from which the transaction is sent.\n * @param request.isMaxAmount - Whether the transaction is a maximum amount transaction.\n * @param request.paymentToken - Payment token used for the transaction.\n * @param request.sourceAmounts - Source amounts for the transaction.\n * @param request.tokens - Required tokens for the transaction.\n * @param request.transactionId - ID of the transaction.\n * @returns Array of quote requests.\n */\nfunction buildQuoteRequests({\n from,\n isMaxAmount,\n paymentToken,\n sourceAmounts,\n tokens,\n transactionId,\n}: {\n from: Hex;\n isMaxAmount: boolean;\n paymentToken: TransactionPaymentToken | undefined;\n sourceAmounts: TransactionPaySourceAmount[] | undefined;\n tokens: TransactionPayRequiredToken[];\n transactionId: string;\n}): QuoteRequest[] {\n if (!paymentToken) {\n return [];\n }\n\n const requests = (sourceAmounts ?? []).map((sourceAmount) => {\n const token = tokens.find(\n (singleToken) => singleToken.address === sourceAmount.targetTokenAddress,\n ) as TransactionPayRequiredToken;\n\n return {\n from,\n isMaxAmount,\n sourceBalanceRaw: paymentToken.balanceRaw,\n sourceTokenAmount: sourceAmount.sourceAmountRaw,\n sourceChainId: paymentToken.chainId,\n sourceTokenAddress: paymentToken.address,\n targetAmountMinimum: token.allowUnderMinimum ? '0' : token.amountRaw,\n targetChainId: token.chainId,\n targetTokenAddress: token.address,\n };\n });\n\n if (!requests.length) {\n log('No quote requests', { transactionId });\n }\n\n return requests;\n}\n\n/**\n * Retrieve quotes for a transaction.\n *\n * @param transaction - Transaction metadata.\n * @param requests - Quote requests.\n * @param messenger - Controller messenger.\n * @returns An object containing batch transactions and quotes.\n */\nasync function getQuotes(\n transaction: TransactionMeta,\n requests: QuoteRequest[],\n messenger: TransactionPayControllerMessenger,\n): Promise<{\n batchTransactions: BatchTransaction[];\n quotes: TransactionPayQuote<Json>[];\n}> {\n const { id: transactionId } = transaction;\n const strategy = getStrategy(messenger as never, transaction);\n let quotes: TransactionPayQuote<Json>[] | undefined = [];\n\n try {\n quotes = requests?.length\n ? ((await strategy.getQuotes({\n messenger,\n requests,\n transaction,\n })) as TransactionPayQuote<Json>[])\n : [];\n } catch (error) {\n log('Error fetching quotes', { error, transactionId });\n }\n\n log('Updated', { transactionId, quotes });\n\n const batchTransactions =\n quotes?.length && strategy.getBatchTransactions\n ? await strategy.getBatchTransactions({\n messenger,\n quotes,\n })\n : [];\n\n log('Batch transactions', { transactionId, batchTransactions });\n\n return {\n batchTransactions,\n quotes,\n };\n}\n"]}
|
|
@@ -20,12 +20,12 @@ function updateSourceAmounts(transactionId, transactionData, messenger) {
|
|
|
20
20
|
if (!transactionData) {
|
|
21
21
|
return;
|
|
22
22
|
}
|
|
23
|
-
const { paymentToken, tokens } = transactionData;
|
|
23
|
+
const { isMaxAmount, paymentToken, tokens } = transactionData;
|
|
24
24
|
if (!tokens.length || !paymentToken) {
|
|
25
25
|
return;
|
|
26
26
|
}
|
|
27
27
|
const sourceAmounts = tokens
|
|
28
|
-
.map((singleToken) => calculateSourceAmount(paymentToken, singleToken, messenger, transactionId))
|
|
28
|
+
.map((singleToken) => calculateSourceAmount(paymentToken, singleToken, messenger, transactionId, isMaxAmount ?? false))
|
|
29
29
|
.filter(Boolean);
|
|
30
30
|
log('Updated source amounts', { transactionId, sourceAmounts });
|
|
31
31
|
transactionData.sourceAmounts = sourceAmounts;
|
|
@@ -38,9 +38,10 @@ exports.updateSourceAmounts = updateSourceAmounts;
|
|
|
38
38
|
* @param token - Target token to cover.
|
|
39
39
|
* @param messenger - Controller messenger.
|
|
40
40
|
* @param transactionId - ID of the transaction.
|
|
41
|
+
* @param isMaxAmount - Whether the transaction is a maximum amount transaction.
|
|
41
42
|
* @returns The source amount or undefined if calculation failed.
|
|
42
43
|
*/
|
|
43
|
-
function calculateSourceAmount(paymentToken, token, messenger, transactionId) {
|
|
44
|
+
function calculateSourceAmount(paymentToken, token, messenger, transactionId, isMaxAmount) {
|
|
44
45
|
const paymentTokenFiatRate = (0, token_1.getTokenFiatRate)(messenger, paymentToken.address, paymentToken.chainId);
|
|
45
46
|
if (!paymentTokenFiatRate) {
|
|
46
47
|
return undefined;
|
|
@@ -69,6 +70,13 @@ function calculateSourceAmount(paymentToken, token, messenger, transactionId) {
|
|
|
69
70
|
log('Skipping token as zero amount', { tokenAddress: token.address });
|
|
70
71
|
return undefined;
|
|
71
72
|
}
|
|
73
|
+
if (isMaxAmount) {
|
|
74
|
+
return {
|
|
75
|
+
sourceAmountHuman: paymentToken.balanceHuman,
|
|
76
|
+
sourceAmountRaw: paymentToken.balanceRaw,
|
|
77
|
+
targetTokenAddress: token.address,
|
|
78
|
+
};
|
|
79
|
+
}
|
|
72
80
|
return {
|
|
73
81
|
sourceAmountHuman,
|
|
74
82
|
sourceAmountRaw,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"source-amounts.cjs","sourceRoot":"","sources":["../../src/utils/source-amounts.ts"],"names":[],"mappings":";;;AAAA,2CAAqD;AACrD,+CAAyC;AAEzC,uCAA2C;AAC3C,mDAA+C;AAK/C,oCAA4C;AAE5C,gDAAwE;AACxE,0CAA0C;AAO1C,MAAM,GAAG,GAAG,IAAA,0BAAkB,EAAC,sBAAa,EAAE,gBAAgB,CAAC,CAAC;AAEhE;;;;;;GAMG;AACH,SAAgB,mBAAmB,CACjC,aAAqB,EACrB,eAA4C,EAC5C,SAA4C;IAE5C,IAAI,CAAC,eAAe,EAAE,CAAC;QACrB,OAAO;IACT,CAAC;IAED,MAAM,EAAE,YAAY,EAAE,MAAM,EAAE,GAAG,eAAe,CAAC;
|
|
1
|
+
{"version":3,"file":"source-amounts.cjs","sourceRoot":"","sources":["../../src/utils/source-amounts.ts"],"names":[],"mappings":";;;AAAA,2CAAqD;AACrD,+CAAyC;AAEzC,uCAA2C;AAC3C,mDAA+C;AAK/C,oCAA4C;AAE5C,gDAAwE;AACxE,0CAA0C;AAO1C,MAAM,GAAG,GAAG,IAAA,0BAAkB,EAAC,sBAAa,EAAE,gBAAgB,CAAC,CAAC;AAEhE;;;;;;GAMG;AACH,SAAgB,mBAAmB,CACjC,aAAqB,EACrB,eAA4C,EAC5C,SAA4C;IAE5C,IAAI,CAAC,eAAe,EAAE,CAAC;QACrB,OAAO;IACT,CAAC;IAED,MAAM,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,EAAE,GAAG,eAAe,CAAC;IAE9D,IAAI,CAAC,MAAM,CAAC,MAAM,IAAI,CAAC,YAAY,EAAE,CAAC;QAC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AAe,CACtB,aAAqB,EACrB,SAA4C;IAE5C,MAAM,WAAW,GAAG,IAAA,4BAAc,EAChC,aAAa,EACb,SAAS,CACS,CAAC;IAErB,OAAO,SAAS,CAAC,IAAI,CAAC,sCAAsC,EAAE,WAAW,CAAC,CAAC;AAC7E,CAAC","sourcesContent":["import { createModuleLogger } from '@metamask/utils';\nimport { BigNumber } from 'bignumber.js';\n\nimport { getTokenFiatRate } from './token';\nimport { getTransaction } from './transaction';\nimport type {\n TransactionPayControllerMessenger,\n TransactionPaymentToken,\n} from '..';\nimport { TransactionPayStrategy } from '..';\nimport type { TransactionMeta } from '../../../transaction-controller/src';\nimport { ARBITRUM_USDC_ADDRESS, CHAIN_ID_ARBITRUM } from '../constants';\nimport { projectLogger } from '../logger';\nimport type {\n TransactionPaySourceAmount,\n TransactionData,\n TransactionPayRequiredToken,\n} from '../types';\n\nconst log = createModuleLogger(projectLogger, 'source-amounts');\n\n/**\n * Update the source amounts for a transaction.\n *\n * @param transactionId - ID of the transaction to update.\n * @param transactionData - Existing transaction data.\n * @param messenger - Controller messenger.\n */\nexport function updateSourceAmounts(\n transactionId: string,\n transactionData: TransactionData | undefined,\n messenger: TransactionPayControllerMessenger,\n): void {\n if (!transactionData) {\n return;\n }\n\n const { isMaxAmount, paymentToken, tokens } = transactionData;\n\n if (!tokens.length || !paymentToken) {\n return;\n }\n\n const sourceAmounts = tokens\n .map((singleToken) =>\n calculateSourceAmount(\n paymentToken,\n singleToken,\n messenger,\n transactionId,\n isMaxAmount ?? false,\n ),\n )\n .filter(Boolean) as TransactionPaySourceAmount[];\n\n log('Updated source amounts', { transactionId, sourceAmounts });\n\n transactionData.sourceAmounts = sourceAmounts;\n}\n\n/**\n * Calculate the required source amount for a payment token to cover a target token.\n *\n * @param paymentToken - Selected payment token.\n * @param token - Target token to cover.\n * @param messenger - Controller messenger.\n * @param transactionId - ID of the transaction.\n * @param isMaxAmount - Whether the transaction is a maximum amount transaction.\n * @returns The source amount or undefined if calculation failed.\n */\nfunction calculateSourceAmount(\n paymentToken: TransactionPaymentToken,\n token: TransactionPayRequiredToken,\n messenger: TransactionPayControllerMessenger,\n transactionId: string,\n isMaxAmount: boolean,\n): TransactionPaySourceAmount | undefined {\n const paymentTokenFiatRate = getTokenFiatRate(\n messenger,\n paymentToken.address,\n paymentToken.chainId,\n );\n\n if (!paymentTokenFiatRate) {\n return undefined;\n }\n\n const hasBalance = new BigNumber(token.balanceRaw).gte(token.amountRaw);\n\n if (token.skipIfBalance && hasBalance) {\n log('Skipping token as sufficient balance', {\n tokenAddress: token.address,\n });\n return undefined;\n }\n\n const strategy = getStrategyType(transactionId, messenger);\n\n const isSameTokenSelected =\n token.address.toLowerCase() === paymentToken.address.toLowerCase() &&\n token.chainId === paymentToken.chainId;\n\n const isAlwaysRequired = isQuoteAlwaysRequired(token, strategy);\n\n if (isSameTokenSelected && !isAlwaysRequired) {\n log('Skipping token as same as payment token');\n return undefined;\n }\n\n const sourceAmountHumanValue = new BigNumber(token.amountUsd).div(\n paymentTokenFiatRate.usdRate,\n );\n\n const sourceAmountHuman = sourceAmountHumanValue.toString(10);\n\n const sourceAmountRaw = sourceAmountHumanValue\n .shiftedBy(paymentToken.decimals)\n .toFixed(0);\n\n if (token.amountRaw === '0') {\n log('Skipping token as zero amount', { tokenAddress: token.address });\n return undefined;\n }\n\n if (isMaxAmount) {\n return {\n sourceAmountHuman: paymentToken.balanceHuman,\n sourceAmountRaw: paymentToken.balanceRaw,\n targetTokenAddress: token.address,\n };\n }\n\n return {\n sourceAmountHuman,\n sourceAmountRaw,\n targetTokenAddress: token.address,\n };\n}\n\n/**\n * Determine if a quote is always required for a token and strategy.\n *\n * @param token - Target token.\n * @param strategy - Payment strategy.\n * @returns True if a quote is always required, false otherwise.\n */\nfunction isQuoteAlwaysRequired(\n token: TransactionPayRequiredToken,\n strategy: TransactionPayStrategy,\n): boolean {\n const isHyperliquidDeposit =\n token.chainId === CHAIN_ID_ARBITRUM &&\n token.address.toLowerCase() === ARBITRUM_USDC_ADDRESS.toLowerCase();\n\n return strategy === TransactionPayStrategy.Relay && isHyperliquidDeposit;\n}\n\n/**\n * Get the strategy type for a transaction.\n *\n * @param transactionId - ID of the transaction.\n * @param messenger - Controller messenger.\n * @returns Payment strategy type.\n */\nfunction getStrategyType(\n transactionId: string,\n messenger: TransactionPayControllerMessenger,\n): TransactionPayStrategy {\n const transaction = getTransaction(\n transactionId,\n messenger,\n ) as TransactionMeta;\n\n return messenger.call('TransactionPayController:getStrategy', transaction);\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"source-amounts.d.cts","sourceRoot":"","sources":["../../src/utils/source-amounts.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EACV,iCAAiC,EAElC,qBAAW;AAKZ,OAAO,KAAK,EAEV,eAAe,EAEhB,qBAAiB;AAIlB;;;;;;GAMG;AACH,wBAAgB,mBAAmB,CACjC,aAAa,EAAE,MAAM,EACrB,eAAe,EAAE,eAAe,GAAG,SAAS,EAC5C,SAAS,EAAE,iCAAiC,GAC3C,IAAI,
|
|
1
|
+
{"version":3,"file":"source-amounts.d.cts","sourceRoot":"","sources":["../../src/utils/source-amounts.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EACV,iCAAiC,EAElC,qBAAW;AAKZ,OAAO,KAAK,EAEV,eAAe,EAEhB,qBAAiB;AAIlB;;;;;;GAMG;AACH,wBAAgB,mBAAmB,CACjC,aAAa,EAAE,MAAM,EACrB,eAAe,EAAE,eAAe,GAAG,SAAS,EAC5C,SAAS,EAAE,iCAAiC,GAC3C,IAAI,CA0BN"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"source-amounts.d.mts","sourceRoot":"","sources":["../../src/utils/source-amounts.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EACV,iCAAiC,EAElC,qBAAW;AAKZ,OAAO,KAAK,EAEV,eAAe,EAEhB,qBAAiB;AAIlB;;;;;;GAMG;AACH,wBAAgB,mBAAmB,CACjC,aAAa,EAAE,MAAM,EACrB,eAAe,EAAE,eAAe,GAAG,SAAS,EAC5C,SAAS,EAAE,iCAAiC,GAC3C,IAAI,
|
|
1
|
+
{"version":3,"file":"source-amounts.d.mts","sourceRoot":"","sources":["../../src/utils/source-amounts.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EACV,iCAAiC,EAElC,qBAAW;AAKZ,OAAO,KAAK,EAEV,eAAe,EAEhB,qBAAiB;AAIlB;;;;;;GAMG;AACH,wBAAgB,mBAAmB,CACjC,aAAa,EAAE,MAAM,EACrB,eAAe,EAAE,eAAe,GAAG,SAAS,EAC5C,SAAS,EAAE,iCAAiC,GAC3C,IAAI,CA0BN"}
|
|
@@ -17,12 +17,12 @@ export function updateSourceAmounts(transactionId, transactionData, messenger) {
|
|
|
17
17
|
if (!transactionData) {
|
|
18
18
|
return;
|
|
19
19
|
}
|
|
20
|
-
const { paymentToken, tokens } = transactionData;
|
|
20
|
+
const { isMaxAmount, paymentToken, tokens } = transactionData;
|
|
21
21
|
if (!tokens.length || !paymentToken) {
|
|
22
22
|
return;
|
|
23
23
|
}
|
|
24
24
|
const sourceAmounts = tokens
|
|
25
|
-
.map((singleToken) => calculateSourceAmount(paymentToken, singleToken, messenger, transactionId))
|
|
25
|
+
.map((singleToken) => calculateSourceAmount(paymentToken, singleToken, messenger, transactionId, isMaxAmount ?? false))
|
|
26
26
|
.filter(Boolean);
|
|
27
27
|
log('Updated source amounts', { transactionId, sourceAmounts });
|
|
28
28
|
transactionData.sourceAmounts = sourceAmounts;
|
|
@@ -34,9 +34,10 @@ export function updateSourceAmounts(transactionId, transactionData, messenger) {
|
|
|
34
34
|
* @param token - Target token to cover.
|
|
35
35
|
* @param messenger - Controller messenger.
|
|
36
36
|
* @param transactionId - ID of the transaction.
|
|
37
|
+
* @param isMaxAmount - Whether the transaction is a maximum amount transaction.
|
|
37
38
|
* @returns The source amount or undefined if calculation failed.
|
|
38
39
|
*/
|
|
39
|
-
function calculateSourceAmount(paymentToken, token, messenger, transactionId) {
|
|
40
|
+
function calculateSourceAmount(paymentToken, token, messenger, transactionId, isMaxAmount) {
|
|
40
41
|
const paymentTokenFiatRate = getTokenFiatRate(messenger, paymentToken.address, paymentToken.chainId);
|
|
41
42
|
if (!paymentTokenFiatRate) {
|
|
42
43
|
return undefined;
|
|
@@ -65,6 +66,13 @@ function calculateSourceAmount(paymentToken, token, messenger, transactionId) {
|
|
|
65
66
|
log('Skipping token as zero amount', { tokenAddress: token.address });
|
|
66
67
|
return undefined;
|
|
67
68
|
}
|
|
69
|
+
if (isMaxAmount) {
|
|
70
|
+
return {
|
|
71
|
+
sourceAmountHuman: paymentToken.balanceHuman,
|
|
72
|
+
sourceAmountRaw: paymentToken.balanceRaw,
|
|
73
|
+
targetTokenAddress: token.address,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
68
76
|
return {
|
|
69
77
|
sourceAmountHuman,
|
|
70
78
|
sourceAmountRaw,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"source-amounts.mjs","sourceRoot":"","sources":["../../src/utils/source-amounts.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,wBAAwB;AACrD,OAAO,EAAE,SAAS,EAAE,qBAAqB;AAEzC,OAAO,EAAE,gBAAgB,EAAE,oBAAgB;AAC3C,OAAO,EAAE,cAAc,EAAE,0BAAsB;AAK/C,OAAO,EAAE,sBAAsB,EAAE,qBAAW;AAE5C,OAAO,EAAE,qBAAqB,EAAE,iBAAiB,EAAE,yBAAqB;AACxE,OAAO,EAAE,aAAa,EAAE,sBAAkB;AAO1C,MAAM,GAAG,GAAG,kBAAkB,CAAC,aAAa,EAAE,gBAAgB,CAAC,CAAC;AAEhE;;;;;;GAMG;AACH,MAAM,UAAU,mBAAmB,CACjC,aAAqB,EACrB,eAA4C,EAC5C,SAA4C;IAE5C,IAAI,CAAC,eAAe,EAAE,CAAC;QACrB,OAAO;IACT,CAAC;IAED,MAAM,EAAE,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AAI,KAAK,CAAC,SAAS,KAAK,GAAG,EAAE,CAAC;QAC5B,GAAG,CAAC,+BAA+B,EAAE,EAAE,YAAY,EAAE,KAAK,CAAC,OAAO,EAAE,CAAC,CAAC;QACtE,OAAO,SAAS,CAAC;IACnB,CAAC;IAED,IAAI,WAAW,EAAE,CAAC;QAChB,OAAO;YACL,iBAAiB,EAAE,YAAY,CAAC,YAAY;YAC5C,eAAe,EAAE,YAAY,CAAC,UAAU;YACxC,kBAAkB,EAAE,KAAK,CAAC,OAAO;SAClC,CAAC;IACJ,CAAC;IAED,OAAO;QACL,iBAAiB;QACjB,eAAe;QACf,kBAAkB,EAAE,KAAK,CAAC,OAAO;KAClC,CAAC;AACJ,CAAC;AAED;;;;;;GAMG;AACH,SAAS,qBAAqB,CAC5B,KAAkC,EAClC,QAAgC;IAEhC,MAAM,oBAAoB,GACxB,KAAK,CAAC,OAAO,KAAK,iBAAiB;QACnC,KAAK,CAAC,OAAO,CAAC,WAAW,EAAE,KAAK,qBAAqB,CAAC,WAAW,EAAE,CAAC;IAEtE,OAAO,QAAQ,KAAK,sBAAsB,CAAC,KAAK,IAAI,oBAAoB,CAAC;AAC3E,CAAC;AAED;;;;;;GAMG;AACH,SAAS,eAAe,CACtB,aAAqB,EACrB,SAA4C;IAE5C,MAAM,WAAW,GAAG,cAAc,CAChC,aAAa,EACb,SAAS,CACS,CAAC;IAErB,OAAO,SAAS,CAAC,IAAI,CAAC,sCAAsC,EAAE,WAAW,CAAC,CAAC;AAC7E,CAAC","sourcesContent":["import { createModuleLogger } from '@metamask/utils';\nimport { BigNumber } from 'bignumber.js';\n\nimport { getTokenFiatRate } from './token';\nimport { getTransaction } from './transaction';\nimport type {\n TransactionPayControllerMessenger,\n TransactionPaymentToken,\n} from '..';\nimport { TransactionPayStrategy } from '..';\nimport type { TransactionMeta } from '../../../transaction-controller/src';\nimport { ARBITRUM_USDC_ADDRESS, CHAIN_ID_ARBITRUM } from '../constants';\nimport { projectLogger } from '../logger';\nimport type {\n TransactionPaySourceAmount,\n TransactionData,\n TransactionPayRequiredToken,\n} from '../types';\n\nconst log = createModuleLogger(projectLogger, 'source-amounts');\n\n/**\n * Update the source amounts for a transaction.\n *\n * @param transactionId - ID of the transaction to update.\n * @param transactionData - Existing transaction data.\n * @param messenger - Controller messenger.\n */\nexport function updateSourceAmounts(\n transactionId: string,\n transactionData: TransactionData | undefined,\n messenger: TransactionPayControllerMessenger,\n): void {\n if (!transactionData) {\n return;\n }\n\n const { isMaxAmount, paymentToken, tokens } = transactionData;\n\n if (!tokens.length || !paymentToken) {\n return;\n }\n\n const sourceAmounts = tokens\n .map((singleToken) =>\n calculateSourceAmount(\n paymentToken,\n singleToken,\n messenger,\n transactionId,\n isMaxAmount ?? false,\n ),\n )\n .filter(Boolean) as TransactionPaySourceAmount[];\n\n log('Updated source amounts', { transactionId, sourceAmounts });\n\n transactionData.sourceAmounts = sourceAmounts;\n}\n\n/**\n * Calculate the required source amount for a payment token to cover a target token.\n *\n * @param paymentToken - Selected payment token.\n * @param token - Target token to cover.\n * @param messenger - Controller messenger.\n * @param transactionId - ID of the transaction.\n * @param isMaxAmount - Whether the transaction is a maximum amount transaction.\n * @returns The source amount or undefined if calculation failed.\n */\nfunction calculateSourceAmount(\n paymentToken: TransactionPaymentToken,\n token: TransactionPayRequiredToken,\n messenger: TransactionPayControllerMessenger,\n transactionId: string,\n isMaxAmount: boolean,\n): TransactionPaySourceAmount | undefined {\n const paymentTokenFiatRate = getTokenFiatRate(\n messenger,\n paymentToken.address,\n paymentToken.chainId,\n );\n\n if (!paymentTokenFiatRate) {\n return undefined;\n }\n\n const hasBalance = new BigNumber(token.balanceRaw).gte(token.amountRaw);\n\n if (token.skipIfBalance && hasBalance) {\n log('Skipping token as sufficient balance', {\n tokenAddress: token.address,\n });\n return undefined;\n }\n\n const strategy = getStrategyType(transactionId, messenger);\n\n const isSameTokenSelected =\n token.address.toLowerCase() === paymentToken.address.toLowerCase() &&\n token.chainId === paymentToken.chainId;\n\n const isAlwaysRequired = isQuoteAlwaysRequired(token, strategy);\n\n if (isSameTokenSelected && !isAlwaysRequired) {\n log('Skipping token as same as payment token');\n return undefined;\n }\n\n const sourceAmountHumanValue = new BigNumber(token.amountUsd).div(\n paymentTokenFiatRate.usdRate,\n );\n\n const sourceAmountHuman = sourceAmountHumanValue.toString(10);\n\n const sourceAmountRaw = sourceAmountHumanValue\n .shiftedBy(paymentToken.decimals)\n .toFixed(0);\n\n if (token.amountRaw === '0') {\n log('Skipping token as zero amount', { tokenAddress: token.address });\n return undefined;\n }\n\n if (isMaxAmount) {\n return {\n sourceAmountHuman: paymentToken.balanceHuman,\n sourceAmountRaw: paymentToken.balanceRaw,\n targetTokenAddress: token.address,\n };\n }\n\n return {\n sourceAmountHuman,\n sourceAmountRaw,\n targetTokenAddress: token.address,\n };\n}\n\n/**\n * Determine if a quote is always required for a token and strategy.\n *\n * @param token - Target token.\n * @param strategy - Payment strategy.\n * @returns True if a quote is always required, false otherwise.\n */\nfunction isQuoteAlwaysRequired(\n token: TransactionPayRequiredToken,\n strategy: TransactionPayStrategy,\n): boolean {\n const isHyperliquidDeposit =\n token.chainId === CHAIN_ID_ARBITRUM &&\n token.address.toLowerCase() === ARBITRUM_USDC_ADDRESS.toLowerCase();\n\n return strategy === TransactionPayStrategy.Relay && isHyperliquidDeposit;\n}\n\n/**\n * Get the strategy type for a transaction.\n *\n * @param transactionId - ID of the transaction.\n * @param messenger - Controller messenger.\n * @returns Payment strategy type.\n */\nfunction getStrategyType(\n transactionId: string,\n messenger: TransactionPayControllerMessenger,\n): TransactionPayStrategy {\n const transaction = getTransaction(\n transactionId,\n messenger,\n ) as TransactionMeta;\n\n return messenger.call('TransactionPayController:getStrategy', transaction);\n}\n"]}
|
package/dist/utils/totals.cjs
CHANGED
|
@@ -7,13 +7,14 @@ const gas_1 = require("./gas.cjs");
|
|
|
7
7
|
* Calculate totals for a list of quotes and tokens.
|
|
8
8
|
*
|
|
9
9
|
* @param request - Request parameters.
|
|
10
|
+
* @param request.isMaxAmount - Whether the transaction is a maximum amount transaction.
|
|
10
11
|
* @param request.quotes - List of bridge quotes.
|
|
11
12
|
* @param request.messenger - Controller messenger.
|
|
12
13
|
* @param request.tokens - List of required tokens.
|
|
13
14
|
* @param request.transaction - Transaction metadata.
|
|
14
15
|
* @returns The calculated totals in USD and fiat currency.
|
|
15
16
|
*/
|
|
16
|
-
function calculateTotals({ quotes, messenger, tokens, transaction, }) {
|
|
17
|
+
function calculateTotals({ isMaxAmount, quotes, messenger, tokens, transaction, }) {
|
|
17
18
|
const providerFee = sumFiat(quotes.map((quote) => quote.fees.provider));
|
|
18
19
|
const sourceNetworkFeeMax = sumAmounts(quotes.map((quote) => quote.fees.sourceNetwork.max));
|
|
19
20
|
const sourceNetworkFeeEstimate = sumAmounts(quotes.map((quote) => quote.fees.sourceNetwork.estimate));
|
|
@@ -25,18 +26,20 @@ function calculateTotals({ quotes, messenger, tokens, transaction, }) {
|
|
|
25
26
|
}
|
|
26
27
|
: transactionNetworkFee;
|
|
27
28
|
const sourceAmount = sumAmounts(quotes.map((quote) => quote.sourceAmount));
|
|
29
|
+
const targetAmount = sumAmounts(quotes.map((quote) => quote.targetAmount));
|
|
28
30
|
const quoteTokens = tokens.filter((singleToken) => !singleToken.skipIfBalance);
|
|
29
31
|
const amountFiat = sumProperty(quoteTokens, (token) => token.amountFiat);
|
|
30
32
|
const amountUsd = sumProperty(quoteTokens, (token) => token.amountUsd);
|
|
33
|
+
const hasQuotes = quotes.length > 0;
|
|
31
34
|
const totalFiat = new bignumber_js_1.BigNumber(providerFee.fiat)
|
|
32
35
|
.plus(sourceNetworkFeeEstimate.fiat)
|
|
33
36
|
.plus(targetNetworkFee.fiat)
|
|
34
|
-
.plus(amountFiat)
|
|
37
|
+
.plus(isMaxAmount && hasQuotes ? targetAmount.fiat : amountFiat)
|
|
35
38
|
.toString(10);
|
|
36
39
|
const totalUsd = new bignumber_js_1.BigNumber(providerFee.usd)
|
|
37
40
|
.plus(sourceNetworkFeeEstimate.usd)
|
|
38
41
|
.plus(targetNetworkFee.usd)
|
|
39
|
-
.plus(amountUsd)
|
|
42
|
+
.plus(isMaxAmount && hasQuotes ? targetAmount.usd : amountUsd)
|
|
40
43
|
.toString(10);
|
|
41
44
|
const estimatedDuration = Number(sumProperty(quotes, (quote) => quote.estimatedDuration));
|
|
42
45
|
const isSourceGasFeeToken = quotes.some((quote) => quote.fees.isSourceGasFeeToken);
|
|
@@ -55,6 +58,7 @@ function calculateTotals({ quotes, messenger, tokens, transaction, }) {
|
|
|
55
58
|
targetNetwork: targetNetworkFee,
|
|
56
59
|
},
|
|
57
60
|
sourceAmount,
|
|
61
|
+
targetAmount,
|
|
58
62
|
total: {
|
|
59
63
|
fiat: totalFiat,
|
|
60
64
|
usd: totalUsd,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"totals.cjs","sourceRoot":"","sources":["../../src/utils/totals.ts"],"names":[],"mappings":";;;AACA,+CAAyC;AAEzC,mCAAoD;AAUpD
|
|
1
|
+
{"version":3,"file":"totals.cjs","sourceRoot":"","sources":["../../src/utils/totals.ts"],"names":[],"mappings":";;;AACA,+CAAyC;AAEzC,mCAAoD;AAUpD;;;;;;;;;;GAUG;AACH,SAAgB,eAAe,CAAC,EAC9B,WAAW,EACX,MAAM,EACN,SAAS,EACT,MAAM,EACN,WAAW,GAOZ;IACC,MAAM,WAAW,GAAG,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC;IAExE,MAAM,mBAAmB,GAAG,UAAU,CACpC,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,aAAa,CAAC,GAAG,CAAC,CACpD,CAAC;IAEF,MAAM,wBAAwB,GAAG,UAAU,CACzC,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,C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SAAS,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAChC,MAAM,KAAK,GAAG,WAAW,CAAC,IAAI,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IACtD,MAAM,GAAG,GAAG,WAAW,CAAC,IAAI,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IAElD,OAAO;QACL,GAAG,SAAS;QACZ,KAAK;QACL,GAAG;KACJ,CAAC;AACJ,CAAC;AAED;;;;;GAKG;AACH,SAAS,OAAO,CAAC,IAAiB;IAChC,MAAM,IAAI,GAAG,WAAW,CAAC,IAAI,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACpD,MAAM,GAAG,GAAG,WAAW,CAAC,IAAI,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IAClD,OAAO,EAAE,IAAI,EAAE,GAAG,EAAE,CAAC;AACvB,CAAC;AAED;;;;;;GAMG;AACH,SAAS,WAAW,CAClB,IAAgB,EAChB,WAAgD;IAEhD,OAAO,IAAI;SACR,GAAG,CAAC,WAAW,CAAC;SAChB,MAAM,CAAY,CAAC,KAAK,EAAE,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,IAAI,wBAAS,CAAC,CAAC,CAAC,CAAC;SACxE,QAAQ,CAAC,EAAE,CAAC,CAAC;AAClB,CAAC","sourcesContent":["import type { TransactionMeta } from '@metamask/transaction-controller';\nimport { BigNumber } from 'bignumber.js';\n\nimport { calculateTransactionGasCost } from './gas';\nimport type {\n Amount,\n FiatValue,\n TransactionPayControllerMessenger,\n TransactionPayQuote,\n TransactionPayRequiredToken,\n TransactionPayTotals,\n} from '../types';\n\n/**\n * Calculate totals for a list of quotes and tokens.\n *\n * @param request - Request parameters.\n * @param request.isMaxAmount - Whether the transaction is a maximum amount transaction.\n * @param request.quotes - List of bridge quotes.\n * @param request.messenger - Controller messenger.\n * @param request.tokens - List of required tokens.\n * @param request.transaction - Transaction metadata.\n * @returns The calculated totals in USD and fiat currency.\n */\nexport function calculateTotals({\n isMaxAmount,\n quotes,\n messenger,\n tokens,\n transaction,\n}: {\n isMaxAmount?: boolean;\n quotes: TransactionPayQuote<unknown>[];\n messenger: TransactionPayControllerMessenger;\n tokens: TransactionPayRequiredToken[];\n transaction: TransactionMeta;\n}): TransactionPayTotals {\n const providerFee = sumFiat(quotes.map((quote) => quote.fees.provider));\n\n const sourceNetworkFeeMax = sumAmounts(\n quotes.map((quote) => quote.fees.sourceNetwork.max),\n );\n\n const sourceNetworkFeeEstimate = sumAmounts(\n quotes.map((quote) => quote.fees.sourceNetwork.estimate),\n );\n\n const transactionNetworkFee = calculateTransactionGasCost(\n transaction,\n messenger,\n );\n\n const targetNetworkFee = quotes?.length\n ? {\n ...sumFiat(quotes.map((quote) => quote.fees.targetNetwork)),\n isGasFeeToken: false,\n }\n : transactionNetworkFee;\n\n const sourceAmount = sumAmounts(quotes.map((quote) => quote.sourceAmount));\n const targetAmount = sumAmounts(quotes.map((quote) => quote.targetAmount));\n\n const quoteTokens = tokens.filter(\n (singleToken) => !singleToken.skipIfBalance,\n );\n\n const amountFiat = sumProperty(quoteTokens, (token) => token.amountFiat);\n const amountUsd = sumProperty(quoteTokens, (token) => token.amountUsd);\n const hasQuotes = quotes.length > 0;\n\n const totalFiat = new BigNumber(providerFee.fiat)\n .plus(sourceNetworkFeeEstimate.fiat)\n .plus(targetNetworkFee.fiat)\n .plus(isMaxAmount && hasQuotes ? targetAmount.fiat : amountFiat)\n .toString(10);\n\n const totalUsd = new BigNumber(providerFee.usd)\n .plus(sourceNetworkFeeEstimate.usd)\n .plus(targetNetworkFee.usd)\n .plus(isMaxAmount && hasQuotes ? targetAmount.usd : amountUsd)\n .toString(10);\n\n const estimatedDuration = Number(\n sumProperty(quotes, (quote) => quote.estimatedDuration),\n );\n\n const isSourceGasFeeToken = quotes.some(\n (quote) => quote.fees.isSourceGasFeeToken,\n );\n\n const isTargetGasFeeToken =\n Boolean(targetNetworkFee.isGasFeeToken) ||\n quotes.some((quote) => quote.fees.isTargetGasFeeToken);\n\n return {\n estimatedDuration,\n fees: {\n isSourceGasFeeToken,\n isTargetGasFeeToken,\n provider: providerFee,\n sourceNetwork: {\n estimate: sourceNetworkFeeEstimate,\n max: sourceNetworkFeeMax,\n },\n targetNetwork: targetNetworkFee,\n },\n sourceAmount,\n targetAmount,\n total: {\n fiat: totalFiat,\n usd: totalUsd,\n },\n };\n}\n\n/**\n * Sum a list of amounts.\n *\n * @param data - List of amounts.\n * @returns Total amount.\n */\nfunction sumAmounts(data: Amount[]): Amount {\n const fiatValue = sumFiat(data);\n const human = sumProperty(data, (item) => item.human);\n const raw = sumProperty(data, (item) => item.raw);\n\n return {\n ...fiatValue,\n human,\n raw,\n };\n}\n\n/**\n * Sum a list of fiat value.\n *\n * @param data - List of fiat values.\n * @returns Total fiat value.\n */\nfunction sumFiat(data: FiatValue[]): FiatValue {\n const fiat = sumProperty(data, (item) => item.fiat);\n const usd = sumProperty(data, (item) => item.usd);\n return { fiat, usd };\n}\n\n/**\n * Sum a specific property from a list of items.\n *\n * @param data - List of items.\n * @param getProperty - Function to extract the property to sum from each item.\n * @returns The summed value as a string.\n */\nfunction sumProperty<DataType>(\n data: DataType[],\n getProperty: (item: DataType) => BigNumber.Value,\n): string {\n return data\n .map(getProperty)\n .reduce<BigNumber>((total, value) => total.plus(value), new BigNumber(0))\n .toString(10);\n}\n"]}
|
package/dist/utils/totals.d.cts
CHANGED
|
@@ -4,13 +4,15 @@ import type { TransactionPayControllerMessenger, TransactionPayQuote, Transactio
|
|
|
4
4
|
* Calculate totals for a list of quotes and tokens.
|
|
5
5
|
*
|
|
6
6
|
* @param request - Request parameters.
|
|
7
|
+
* @param request.isMaxAmount - Whether the transaction is a maximum amount transaction.
|
|
7
8
|
* @param request.quotes - List of bridge quotes.
|
|
8
9
|
* @param request.messenger - Controller messenger.
|
|
9
10
|
* @param request.tokens - List of required tokens.
|
|
10
11
|
* @param request.transaction - Transaction metadata.
|
|
11
12
|
* @returns The calculated totals in USD and fiat currency.
|
|
12
13
|
*/
|
|
13
|
-
export declare function calculateTotals({ quotes, messenger, tokens, transaction, }: {
|
|
14
|
+
export declare function calculateTotals({ isMaxAmount, quotes, messenger, tokens, transaction, }: {
|
|
15
|
+
isMaxAmount?: boolean;
|
|
14
16
|
quotes: TransactionPayQuote<unknown>[];
|
|
15
17
|
messenger: TransactionPayControllerMessenger;
|
|
16
18
|
tokens: TransactionPayRequiredToken[];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"totals.d.cts","sourceRoot":"","sources":["../../src/utils/totals.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,yCAAyC;AAIxE,OAAO,KAAK,EAGV,iCAAiC,EACjC,mBAAmB,EACnB,2BAA2B,EAC3B,oBAAoB,EACrB,qBAAiB;AAElB
|
|
1
|
+
{"version":3,"file":"totals.d.cts","sourceRoot":"","sources":["../../src/utils/totals.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,yCAAyC;AAIxE,OAAO,KAAK,EAGV,iCAAiC,EACjC,mBAAmB,EACnB,2BAA2B,EAC3B,oBAAoB,EACrB,qBAAiB;AAElB;;;;;;;;;;GAUG;AACH,wBAAgB,eAAe,CAAC,EAC9B,WAAW,EACX,MAAM,EACN,SAAS,EACT,MAAM,EACN,WAAW,GACZ,EAAE;IACD,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,MAAM,EAAE,mBAAmB,CAAC,OAAO,CAAC,EAAE,CAAC;IACvC,SAAS,EAAE,iCAAiC,CAAC;IAC7C,MAAM,EAAE,2BAA2B,EAAE,CAAC;IACtC,WAAW,EAAE,eAAe,CAAC;CAC9B,GAAG,oBAAoB,CA6EvB"}
|
package/dist/utils/totals.d.mts
CHANGED
|
@@ -4,13 +4,15 @@ import type { TransactionPayControllerMessenger, TransactionPayQuote, Transactio
|
|
|
4
4
|
* Calculate totals for a list of quotes and tokens.
|
|
5
5
|
*
|
|
6
6
|
* @param request - Request parameters.
|
|
7
|
+
* @param request.isMaxAmount - Whether the transaction is a maximum amount transaction.
|
|
7
8
|
* @param request.quotes - List of bridge quotes.
|
|
8
9
|
* @param request.messenger - Controller messenger.
|
|
9
10
|
* @param request.tokens - List of required tokens.
|
|
10
11
|
* @param request.transaction - Transaction metadata.
|
|
11
12
|
* @returns The calculated totals in USD and fiat currency.
|
|
12
13
|
*/
|
|
13
|
-
export declare function calculateTotals({ quotes, messenger, tokens, transaction, }: {
|
|
14
|
+
export declare function calculateTotals({ isMaxAmount, quotes, messenger, tokens, transaction, }: {
|
|
15
|
+
isMaxAmount?: boolean;
|
|
14
16
|
quotes: TransactionPayQuote<unknown>[];
|
|
15
17
|
messenger: TransactionPayControllerMessenger;
|
|
16
18
|
tokens: TransactionPayRequiredToken[];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"totals.d.mts","sourceRoot":"","sources":["../../src/utils/totals.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,yCAAyC;AAIxE,OAAO,KAAK,EAGV,iCAAiC,EACjC,mBAAmB,EACnB,2BAA2B,EAC3B,oBAAoB,EACrB,qBAAiB;AAElB
|
|
1
|
+
{"version":3,"file":"totals.d.mts","sourceRoot":"","sources":["../../src/utils/totals.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,yCAAyC;AAIxE,OAAO,KAAK,EAGV,iCAAiC,EACjC,mBAAmB,EACnB,2BAA2B,EAC3B,oBAAoB,EACrB,qBAAiB;AAElB;;;;;;;;;;GAUG;AACH,wBAAgB,eAAe,CAAC,EAC9B,WAAW,EACX,MAAM,EACN,SAAS,EACT,MAAM,EACN,WAAW,GACZ,EAAE;IACD,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,MAAM,EAAE,mBAAmB,CAAC,OAAO,CAAC,EAAE,CAAC;IACvC,SAAS,EAAE,iCAAiC,CAAC;IAC7C,MAAM,EAAE,2BAA2B,EAAE,CAAC;IACtC,WAAW,EAAE,eAAe,CAAC;CAC9B,GAAG,oBAAoB,CA6EvB"}
|
package/dist/utils/totals.mjs
CHANGED
|
@@ -4,13 +4,14 @@ import { calculateTransactionGasCost } from "./gas.mjs";
|
|
|
4
4
|
* Calculate totals for a list of quotes and tokens.
|
|
5
5
|
*
|
|
6
6
|
* @param request - Request parameters.
|
|
7
|
+
* @param request.isMaxAmount - Whether the transaction is a maximum amount transaction.
|
|
7
8
|
* @param request.quotes - List of bridge quotes.
|
|
8
9
|
* @param request.messenger - Controller messenger.
|
|
9
10
|
* @param request.tokens - List of required tokens.
|
|
10
11
|
* @param request.transaction - Transaction metadata.
|
|
11
12
|
* @returns The calculated totals in USD and fiat currency.
|
|
12
13
|
*/
|
|
13
|
-
export function calculateTotals({ quotes, messenger, tokens, transaction, }) {
|
|
14
|
+
export function calculateTotals({ isMaxAmount, quotes, messenger, tokens, transaction, }) {
|
|
14
15
|
const providerFee = sumFiat(quotes.map((quote) => quote.fees.provider));
|
|
15
16
|
const sourceNetworkFeeMax = sumAmounts(quotes.map((quote) => quote.fees.sourceNetwork.max));
|
|
16
17
|
const sourceNetworkFeeEstimate = sumAmounts(quotes.map((quote) => quote.fees.sourceNetwork.estimate));
|
|
@@ -22,18 +23,20 @@ export function calculateTotals({ quotes, messenger, tokens, transaction, }) {
|
|
|
22
23
|
}
|
|
23
24
|
: transactionNetworkFee;
|
|
24
25
|
const sourceAmount = sumAmounts(quotes.map((quote) => quote.sourceAmount));
|
|
26
|
+
const targetAmount = sumAmounts(quotes.map((quote) => quote.targetAmount));
|
|
25
27
|
const quoteTokens = tokens.filter((singleToken) => !singleToken.skipIfBalance);
|
|
26
28
|
const amountFiat = sumProperty(quoteTokens, (token) => token.amountFiat);
|
|
27
29
|
const amountUsd = sumProperty(quoteTokens, (token) => token.amountUsd);
|
|
30
|
+
const hasQuotes = quotes.length > 0;
|
|
28
31
|
const totalFiat = new BigNumber(providerFee.fiat)
|
|
29
32
|
.plus(sourceNetworkFeeEstimate.fiat)
|
|
30
33
|
.plus(targetNetworkFee.fiat)
|
|
31
|
-
.plus(amountFiat)
|
|
34
|
+
.plus(isMaxAmount && hasQuotes ? targetAmount.fiat : amountFiat)
|
|
32
35
|
.toString(10);
|
|
33
36
|
const totalUsd = new BigNumber(providerFee.usd)
|
|
34
37
|
.plus(sourceNetworkFeeEstimate.usd)
|
|
35
38
|
.plus(targetNetworkFee.usd)
|
|
36
|
-
.plus(amountUsd)
|
|
39
|
+
.plus(isMaxAmount && hasQuotes ? targetAmount.usd : amountUsd)
|
|
37
40
|
.toString(10);
|
|
38
41
|
const estimatedDuration = Number(sumProperty(quotes, (quote) => quote.estimatedDuration));
|
|
39
42
|
const isSourceGasFeeToken = quotes.some((quote) => quote.fees.isSourceGasFeeToken);
|
|
@@ -52,6 +55,7 @@ export function calculateTotals({ quotes, messenger, tokens, transaction, }) {
|
|
|
52
55
|
targetNetwork: targetNetworkFee,
|
|
53
56
|
},
|
|
54
57
|
sourceAmount,
|
|
58
|
+
targetAmount,
|
|
55
59
|
total: {
|
|
56
60
|
fiat: totalFiat,
|
|
57
61
|
usd: totalUsd,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"totals.mjs","sourceRoot":"","sources":["../../src/utils/totals.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,qBAAqB;AAEzC,OAAO,EAAE,2BAA2B,EAAE,kBAAc;AAUpD
|
|
1
|
+
{"version":3,"file":"totals.mjs","sourceRoot":"","sources":["../../src/utils/totals.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,qBAAqB;AAEzC,OAAO,EAAE,2BAA2B,EAAE,kBAAc;AAUpD;;;;;;;;;;GAUG;AACH,MAAM,UAAU,eAAe,CAAC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,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,IAAI,SAAS,CAAC,CAAC,CAAC,CAAC;SACxE,QAAQ,CAAC,EAAE,CAAC,CAAC;AAClB,CAAC","sourcesContent":["import type { TransactionMeta } from '@metamask/transaction-controller';\nimport { BigNumber } from 'bignumber.js';\n\nimport { calculateTransactionGasCost } from './gas';\nimport type {\n Amount,\n FiatValue,\n TransactionPayControllerMessenger,\n TransactionPayQuote,\n TransactionPayRequiredToken,\n TransactionPayTotals,\n} from '../types';\n\n/**\n * Calculate totals for a list of quotes and tokens.\n *\n * @param request - Request parameters.\n * @param request.isMaxAmount - Whether the transaction is a maximum amount transaction.\n * @param request.quotes - List of bridge quotes.\n * @param request.messenger - Controller messenger.\n * @param request.tokens - List of required tokens.\n * @param request.transaction - Transaction metadata.\n * @returns The calculated totals in USD and fiat currency.\n */\nexport function calculateTotals({\n isMaxAmount,\n quotes,\n messenger,\n tokens,\n transaction,\n}: {\n isMaxAmount?: boolean;\n quotes: TransactionPayQuote<unknown>[];\n messenger: TransactionPayControllerMessenger;\n tokens: TransactionPayRequiredToken[];\n transaction: TransactionMeta;\n}): TransactionPayTotals {\n const providerFee = sumFiat(quotes.map((quote) => quote.fees.provider));\n\n const sourceNetworkFeeMax = sumAmounts(\n quotes.map((quote) => quote.fees.sourceNetwork.max),\n );\n\n const sourceNetworkFeeEstimate = sumAmounts(\n quotes.map((quote) => quote.fees.sourceNetwork.estimate),\n );\n\n const transactionNetworkFee = calculateTransactionGasCost(\n transaction,\n messenger,\n );\n\n const targetNetworkFee = quotes?.length\n ? {\n ...sumFiat(quotes.map((quote) => quote.fees.targetNetwork)),\n isGasFeeToken: false,\n }\n : transactionNetworkFee;\n\n const sourceAmount = sumAmounts(quotes.map((quote) => quote.sourceAmount));\n const targetAmount = sumAmounts(quotes.map((quote) => quote.targetAmount));\n\n const quoteTokens = tokens.filter(\n (singleToken) => !singleToken.skipIfBalance,\n );\n\n const amountFiat = sumProperty(quoteTokens, (token) => token.amountFiat);\n const amountUsd = sumProperty(quoteTokens, (token) => token.amountUsd);\n const hasQuotes = quotes.length > 0;\n\n const totalFiat = new BigNumber(providerFee.fiat)\n .plus(sourceNetworkFeeEstimate.fiat)\n .plus(targetNetworkFee.fiat)\n .plus(isMaxAmount && hasQuotes ? targetAmount.fiat : amountFiat)\n .toString(10);\n\n const totalUsd = new BigNumber(providerFee.usd)\n .plus(sourceNetworkFeeEstimate.usd)\n .plus(targetNetworkFee.usd)\n .plus(isMaxAmount && hasQuotes ? targetAmount.usd : amountUsd)\n .toString(10);\n\n const estimatedDuration = Number(\n sumProperty(quotes, (quote) => quote.estimatedDuration),\n );\n\n const isSourceGasFeeToken = quotes.some(\n (quote) => quote.fees.isSourceGasFeeToken,\n );\n\n const isTargetGasFeeToken =\n Boolean(targetNetworkFee.isGasFeeToken) ||\n quotes.some((quote) => quote.fees.isTargetGasFeeToken);\n\n return {\n estimatedDuration,\n fees: {\n isSourceGasFeeToken,\n isTargetGasFeeToken,\n provider: providerFee,\n sourceNetwork: {\n estimate: sourceNetworkFeeEstimate,\n max: sourceNetworkFeeMax,\n },\n targetNetwork: targetNetworkFee,\n },\n sourceAmount,\n targetAmount,\n total: {\n fiat: totalFiat,\n usd: totalUsd,\n },\n };\n}\n\n/**\n * Sum a list of amounts.\n *\n * @param data - List of amounts.\n * @returns Total amount.\n */\nfunction sumAmounts(data: Amount[]): Amount {\n const fiatValue = sumFiat(data);\n const human = sumProperty(data, (item) => item.human);\n const raw = sumProperty(data, (item) => item.raw);\n\n return {\n ...fiatValue,\n human,\n raw,\n };\n}\n\n/**\n * Sum a list of fiat value.\n *\n * @param data - List of fiat values.\n * @returns Total fiat value.\n */\nfunction sumFiat(data: FiatValue[]): FiatValue {\n const fiat = sumProperty(data, (item) => item.fiat);\n const usd = sumProperty(data, (item) => item.usd);\n return { fiat, usd };\n}\n\n/**\n * Sum a specific property from a list of items.\n *\n * @param data - List of items.\n * @param getProperty - Function to extract the property to sum from each item.\n * @returns The summed value as a string.\n */\nfunction sumProperty<DataType>(\n data: DataType[],\n getProperty: (item: DataType) => BigNumber.Value,\n): string {\n return data\n .map(getProperty)\n .reduce<BigNumber>((total, value) => total.plus(value), new BigNumber(0))\n .toString(10);\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@metamask-previews/transaction-pay-controller",
|
|
3
|
-
"version": "10.6.0-preview-
|
|
3
|
+
"version": "10.6.0-preview-6a568504",
|
|
4
4
|
"description": "Manages alternate payment strategies to provide required funds for transactions in MetaMask",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"MetaMask",
|
|
@@ -53,15 +53,15 @@
|
|
|
53
53
|
"@ethersproject/contracts": "^5.7.0",
|
|
54
54
|
"@metamask/assets-controllers": "^95.0.0",
|
|
55
55
|
"@metamask/base-controller": "^9.0.0",
|
|
56
|
-
"@metamask/bridge-controller": "^64.
|
|
57
|
-
"@metamask/bridge-status-controller": "^64.
|
|
56
|
+
"@metamask/bridge-controller": "^64.4.0",
|
|
57
|
+
"@metamask/bridge-status-controller": "^64.4.1",
|
|
58
58
|
"@metamask/controller-utils": "^11.18.0",
|
|
59
59
|
"@metamask/gas-fee-controller": "^26.0.0",
|
|
60
60
|
"@metamask/messenger": "^0.3.0",
|
|
61
61
|
"@metamask/metamask-eth-abis": "^3.1.1",
|
|
62
62
|
"@metamask/network-controller": "^27.2.0",
|
|
63
63
|
"@metamask/remote-feature-flag-controller": "^4.0.0",
|
|
64
|
-
"@metamask/transaction-controller": "^62.
|
|
64
|
+
"@metamask/transaction-controller": "^62.8.0",
|
|
65
65
|
"@metamask/utils": "^11.9.0",
|
|
66
66
|
"bignumber.js": "^9.1.2",
|
|
67
67
|
"bn.js": "^5.2.1",
|