@metamask/transaction-pay-controller 19.0.3 → 19.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +16 -1
- package/dist/strategy/across/AcrossStrategy.cjs +3 -2
- package/dist/strategy/across/AcrossStrategy.cjs.map +1 -1
- package/dist/strategy/across/AcrossStrategy.d.cts.map +1 -1
- package/dist/strategy/across/AcrossStrategy.d.mts.map +1 -1
- package/dist/strategy/across/AcrossStrategy.mjs +3 -2
- package/dist/strategy/across/AcrossStrategy.mjs.map +1 -1
- package/dist/strategy/across/across-actions.cjs +7 -0
- package/dist/strategy/across/across-actions.cjs.map +1 -1
- package/dist/strategy/across/across-actions.d.cts.map +1 -1
- package/dist/strategy/across/across-actions.d.mts.map +1 -1
- package/dist/strategy/across/across-actions.mjs +7 -0
- package/dist/strategy/across/across-actions.mjs.map +1 -1
- package/dist/strategy/across/across-quotes.cjs +4 -2
- package/dist/strategy/across/across-quotes.cjs.map +1 -1
- package/dist/strategy/across/across-quotes.d.cts.map +1 -1
- package/dist/strategy/across/across-quotes.d.mts.map +1 -1
- package/dist/strategy/across/across-quotes.mjs +4 -2
- package/dist/strategy/across/across-quotes.mjs.map +1 -1
- package/dist/strategy/across/perps.cjs +56 -0
- package/dist/strategy/across/perps.cjs.map +1 -0
- package/dist/strategy/across/perps.d.cts +32 -0
- package/dist/strategy/across/perps.d.cts.map +1 -0
- package/dist/strategy/across/perps.d.mts +32 -0
- package/dist/strategy/across/perps.d.mts.map +1 -0
- package/dist/strategy/across/perps.mjs +51 -0
- package/dist/strategy/across/perps.mjs.map +1 -0
- package/dist/tests/messenger-mock.cjs.map +1 -1
- package/dist/tests/messenger-mock.d.cts.map +1 -1
- package/dist/tests/messenger-mock.d.mts.map +1 -1
- package/dist/tests/messenger-mock.mjs.map +1 -1
- package/dist/utils/source-amounts.cjs +23 -17
- 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 +23 -17
- package/dist/utils/source-amounts.mjs.map +1 -1
- package/package.json +4 -3
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"source-amounts.d.cts","sourceRoot":"","sources":["../../src/utils/source-amounts.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"source-amounts.d.cts","sourceRoot":"","sources":["../../src/utils/source-amounts.ts"],"names":[],"mappings":"AAMA,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,CAyCN"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"source-amounts.d.mts","sourceRoot":"","sources":["../../src/utils/source-amounts.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"source-amounts.d.mts","sourceRoot":"","sources":["../../src/utils/source-amounts.ts"],"names":[],"mappings":"AAMA,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,CAyCN"}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { TransactionType } from "@metamask/transaction-controller";
|
|
1
2
|
import { createModuleLogger } from "@metamask/utils";
|
|
2
3
|
import { BigNumber } from "bignumber.js";
|
|
3
4
|
import { getTokenFiatRate, isSameToken } from "./token.mjs";
|
|
@@ -24,7 +25,8 @@ export function updateSourceAmounts(transactionId, transactionData, messenger) {
|
|
|
24
25
|
// For post-quote flows, source amounts are calculated differently
|
|
25
26
|
// The source is the transaction's required token, not the selected token
|
|
26
27
|
if (isPostQuote) {
|
|
27
|
-
const
|
|
28
|
+
const { isHyperliquidSource } = transactionData;
|
|
29
|
+
const sourceAmounts = calculatePostQuoteSourceAmounts(tokens, paymentToken, isMaxAmount ?? false, isHyperliquidSource);
|
|
28
30
|
log('Updated post-quote source amounts', { transactionId, sourceAmounts });
|
|
29
31
|
transactionData.sourceAmounts = sourceAmounts;
|
|
30
32
|
return;
|
|
@@ -43,9 +45,10 @@ export function updateSourceAmounts(transactionId, transactionData, messenger) {
|
|
|
43
45
|
* @param tokens - Required tokens from the transaction.
|
|
44
46
|
* @param paymentToken - Selected payment/destination token.
|
|
45
47
|
* @param isMaxAmount - Whether the transaction is a maximum amount transaction.
|
|
48
|
+
* @param isHyperliquidSource - Whether the source is HyperLiquid (perps withdrawal).
|
|
46
49
|
* @returns Array of source amounts.
|
|
47
50
|
*/
|
|
48
|
-
function calculatePostQuoteSourceAmounts(tokens, paymentToken, isMaxAmount) {
|
|
51
|
+
function calculatePostQuoteSourceAmounts(tokens, paymentToken, isMaxAmount, isHyperliquidSource) {
|
|
49
52
|
return tokens
|
|
50
53
|
.filter((token) => {
|
|
51
54
|
if (token.skipIfBalance) {
|
|
@@ -56,8 +59,11 @@ function calculatePostQuoteSourceAmounts(tokens, paymentToken, isMaxAmount) {
|
|
|
56
59
|
log('Skipping token as zero amount', { tokenAddress: token.address });
|
|
57
60
|
return false;
|
|
58
61
|
}
|
|
59
|
-
// Skip same token on same chain
|
|
60
|
-
|
|
62
|
+
// Skip same token on same chain, unless the source is HyperLiquid.
|
|
63
|
+
// For HyperLiquid withdrawals the relay strategy renormalizes the
|
|
64
|
+
// source from Arbitrum USDC to HyperCore USDC (a different chain),
|
|
65
|
+
// so the tokens are not actually the same after normalization.
|
|
66
|
+
if (isSameToken(token, paymentToken) && !isHyperliquidSource) {
|
|
61
67
|
log('Skipping token as same as destination token');
|
|
62
68
|
return false;
|
|
63
69
|
}
|
|
@@ -94,8 +100,8 @@ function calculateSourceAmount(paymentToken, token, messenger, transactionId, is
|
|
|
94
100
|
});
|
|
95
101
|
return undefined;
|
|
96
102
|
}
|
|
97
|
-
const strategy =
|
|
98
|
-
const isAlwaysRequired = isQuoteAlwaysRequired(token, strategy);
|
|
103
|
+
const { parentTransactionType, strategy } = getStrategyContext(transactionId, messenger);
|
|
104
|
+
const isAlwaysRequired = isQuoteAlwaysRequired(token, strategy, parentTransactionType);
|
|
99
105
|
if (isSameToken(token, paymentToken) && !isAlwaysRequired) {
|
|
100
106
|
log('Skipping token as same as payment token');
|
|
101
107
|
return undefined;
|
|
@@ -127,22 +133,22 @@ function calculateSourceAmount(paymentToken, token, messenger, transactionId, is
|
|
|
127
133
|
*
|
|
128
134
|
* @param token - Target token.
|
|
129
135
|
* @param strategy - Payment strategy.
|
|
136
|
+
* @param parentTransactionType - Parent transaction type, if available.
|
|
130
137
|
* @returns True if a quote is always required, false otherwise.
|
|
131
138
|
*/
|
|
132
|
-
function isQuoteAlwaysRequired(token, strategy) {
|
|
139
|
+
function isQuoteAlwaysRequired(token, strategy, parentTransactionType) {
|
|
133
140
|
const isHyperliquidDeposit = token.chainId === CHAIN_ID_ARBITRUM &&
|
|
134
141
|
token.address.toLowerCase() === ARBITRUM_USDC_ADDRESS.toLowerCase();
|
|
135
|
-
return
|
|
142
|
+
return (isHyperliquidDeposit &&
|
|
143
|
+
(strategy === TransactionPayStrategy.Relay ||
|
|
144
|
+
(strategy === TransactionPayStrategy.Across &&
|
|
145
|
+
parentTransactionType === TransactionType.perpsDeposit)));
|
|
136
146
|
}
|
|
137
|
-
|
|
138
|
-
* Get the strategy type for a transaction.
|
|
139
|
-
*
|
|
140
|
-
* @param transactionId - ID of the transaction.
|
|
141
|
-
* @param messenger - Controller messenger.
|
|
142
|
-
* @returns Payment strategy type.
|
|
143
|
-
*/
|
|
144
|
-
function getStrategyType(transactionId, messenger) {
|
|
147
|
+
function getStrategyContext(transactionId, messenger) {
|
|
145
148
|
const transaction = getTransaction(transactionId, messenger);
|
|
146
|
-
return
|
|
149
|
+
return {
|
|
150
|
+
parentTransactionType: transaction.type,
|
|
151
|
+
strategy: messenger.call('TransactionPayController:getStrategy', transaction),
|
|
152
|
+
};
|
|
147
153
|
}
|
|
148
154
|
//# sourceMappingURL=source-amounts.mjs.map
|
|
@@ -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,WAAW,EAAE,oBAAgB;AACxD,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,WAAW,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,EAAE,GAAG,eAAe,CAAC;IAE3E,IAAI,CAAC,MAAM,CAAC,MAAM,IAAI,CAAC,YAAY,EAAE,CAAC;QACpC,OAAO;IACT,CAAC;IAED,kEAAkE;IAClE,yEAAyE;IACzE,IAAI,WAAW,EAAE,CAAC;QAChB,MAAM,aAAa,GAAG,+BAA+B,CACnD,MAAM,EACN,YAAY,EACZ,WAAW,IAAI,KAAK,CACrB,CAAC;QACF,GAAG,CAAC,mCAAmC,EAA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iatRate, isSameToken } 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, isPostQuote, paymentToken, tokens } = transactionData;\n\n if (!tokens.length || !paymentToken) {\n return;\n }\n\n // For post-quote flows, source amounts are calculated differently\n // The source is the transaction's required token, not the selected token\n if (isPostQuote) {\n const sourceAmounts = calculatePostQuoteSourceAmounts(\n tokens,\n paymentToken,\n isMaxAmount ?? false,\n );\n log('Updated post-quote source amounts', { transactionId, sourceAmounts });\n transactionData.sourceAmounts = sourceAmounts;\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 source amounts for post-quote flows.\n * In this flow, the required tokens ARE the source tokens,\n * and the payment token is the target (destination).\n *\n * @param tokens - Required tokens from the transaction.\n * @param paymentToken - Selected payment/destination token.\n * @param isMaxAmount - Whether the transaction is a maximum amount transaction.\n * @returns Array of source amounts.\n */\nfunction calculatePostQuoteSourceAmounts(\n tokens: TransactionPayRequiredToken[],\n paymentToken: TransactionPaymentToken,\n isMaxAmount: boolean,\n): TransactionPaySourceAmount[] {\n return tokens\n .filter((token) => {\n if (token.skipIfBalance) {\n return false;\n }\n\n // Skip zero amounts (unless max amount, where we use balance)\n if (token.amountRaw === '0' && !isMaxAmount) {\n log('Skipping token as zero amount', { tokenAddress: token.address });\n return false;\n }\n\n // Skip same token on same chain\n if (isSameToken(token, paymentToken)) {\n log('Skipping token as same as destination token');\n return false;\n }\n\n return true;\n })\n .map((token) => ({\n sourceAmountHuman: isMaxAmount ? token.balanceHuman : token.amountHuman,\n sourceAmountRaw: isMaxAmount ? token.balanceRaw : token.amountRaw,\n sourceBalanceRaw: token.balanceRaw,\n sourceChainId: token.chainId,\n sourceTokenAddress: token.address,\n targetTokenAddress: paymentToken.address,\n }));\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 const isAlwaysRequired = isQuoteAlwaysRequired(token, strategy);\n\n if (isSameToken(token, paymentToken) && !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
|
+
{"version":3,"file":"source-amounts.mjs","sourceRoot":"","sources":["../../src/utils/source-amounts.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,yCAAyC;AACnE,OAAO,EAAE,kBAAkB,EAAE,wBAAwB;AACrD,OAAO,EAAE,SAAS,EAAE,qBAAqB;AAEzC,OAAO,EAAE,gBAAgB,EAAE,WAAW,EAAE,oBAAgB;AACxD,OAAO,EAAE,cAAc,EAAE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AAO,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;;;;;;;GAOG;AACH,SAAS,qBAAqB,CAC5B,KAAkC,EAClC,QAAgC,EAChC,qBAAuC;IAEvC,MAAM,oBAAoB,GACxB,KAAK,CAAC,OAAO,KAAK,iBAAiB;QACnC,KAAK,CAAC,OAAO,CAAC,WAAW,EAAE,KAAK,qBAAqB,CAAC,WAAW,EAAE,CAAC;IAEtE,OAAO,CACL,oBAAoB;QACpB,CAAC,QAAQ,KAAK,sBAAsB,CAAC,KAAK;YACxC,CAAC,QAAQ,KAAK,sBAAsB,CAAC,MAAM;gBACzC,qBAAqB,KAAK,eAAe,CAAC,YAAY,CAAC,CAAC,CAC7D,CAAC;AACJ,CAAC;AAED,SAAS,kBAAkB,CACzB,aAAqB,EACrB,SAA4C;IAK5C,MAAM,WAAW,GAAG,cAAc,CAChC,aAAa,EACb,SAAS,CACS,CAAC;IAErB,OAAO;QACL,qBAAqB,EAAE,WAAW,CAAC,IAAI;QACvC,QAAQ,EAAE,SAAS,CAAC,IAAI,CACtB,sCAAsC,EACtC,WAAW,CACZ;KACF,CAAC;AACJ,CAAC","sourcesContent":["import { TransactionType } from '@metamask/transaction-controller';\nimport { createModuleLogger } from '@metamask/utils';\nimport { BigNumber } from 'bignumber.js';\n\nimport { getTokenFiatRate, isSameToken } 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, isPostQuote, paymentToken, tokens } = transactionData;\n\n if (!tokens.length || !paymentToken) {\n return;\n }\n\n // For post-quote flows, source amounts are calculated differently\n // The source is the transaction's required token, not the selected token\n if (isPostQuote) {\n const { isHyperliquidSource } = transactionData;\n const sourceAmounts = calculatePostQuoteSourceAmounts(\n tokens,\n paymentToken,\n isMaxAmount ?? false,\n isHyperliquidSource,\n );\n log('Updated post-quote source amounts', { transactionId, sourceAmounts });\n transactionData.sourceAmounts = sourceAmounts;\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 source amounts for post-quote flows.\n * In this flow, the required tokens ARE the source tokens,\n * and the payment token is the target (destination).\n *\n * @param tokens - Required tokens from the transaction.\n * @param paymentToken - Selected payment/destination token.\n * @param isMaxAmount - Whether the transaction is a maximum amount transaction.\n * @param isHyperliquidSource - Whether the source is HyperLiquid (perps withdrawal).\n * @returns Array of source amounts.\n */\nfunction calculatePostQuoteSourceAmounts(\n tokens: TransactionPayRequiredToken[],\n paymentToken: TransactionPaymentToken,\n isMaxAmount: boolean,\n isHyperliquidSource?: boolean,\n): TransactionPaySourceAmount[] {\n return tokens\n .filter((token) => {\n if (token.skipIfBalance) {\n return false;\n }\n\n // Skip zero amounts (unless max amount, where we use balance)\n if (token.amountRaw === '0' && !isMaxAmount) {\n log('Skipping token as zero amount', { tokenAddress: token.address });\n return false;\n }\n\n // Skip same token on same chain, unless the source is HyperLiquid.\n // For HyperLiquid withdrawals the relay strategy renormalizes the\n // source from Arbitrum USDC to HyperCore USDC (a different chain),\n // so the tokens are not actually the same after normalization.\n if (isSameToken(token, paymentToken) && !isHyperliquidSource) {\n log('Skipping token as same as destination token');\n return false;\n }\n\n return true;\n })\n .map((token) => ({\n sourceAmountHuman: isMaxAmount ? token.balanceHuman : token.amountHuman,\n sourceAmountRaw: isMaxAmount ? token.balanceRaw : token.amountRaw,\n sourceBalanceRaw: token.balanceRaw,\n sourceChainId: token.chainId,\n sourceTokenAddress: token.address,\n targetTokenAddress: paymentToken.address,\n }));\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 { parentTransactionType, strategy } = getStrategyContext(\n transactionId,\n messenger,\n );\n const isAlwaysRequired = isQuoteAlwaysRequired(\n token,\n strategy,\n parentTransactionType,\n );\n\n if (isSameToken(token, paymentToken) && !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 * @param parentTransactionType - Parent transaction type, if available.\n * @returns True if a quote is always required, false otherwise.\n */\nfunction isQuoteAlwaysRequired(\n token: TransactionPayRequiredToken,\n strategy: TransactionPayStrategy,\n parentTransactionType?: TransactionType,\n): boolean {\n const isHyperliquidDeposit =\n token.chainId === CHAIN_ID_ARBITRUM &&\n token.address.toLowerCase() === ARBITRUM_USDC_ADDRESS.toLowerCase();\n\n return (\n isHyperliquidDeposit &&\n (strategy === TransactionPayStrategy.Relay ||\n (strategy === TransactionPayStrategy.Across &&\n parentTransactionType === TransactionType.perpsDeposit))\n );\n}\n\nfunction getStrategyContext(\n transactionId: string,\n messenger: TransactionPayControllerMessenger,\n): {\n parentTransactionType?: TransactionType;\n strategy: TransactionPayStrategy;\n} {\n const transaction = getTransaction(\n transactionId,\n messenger,\n ) as TransactionMeta;\n\n return {\n parentTransactionType: transaction.type,\n strategy: messenger.call(\n 'TransactionPayController:getStrategy',\n transaction,\n ),\n };\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@metamask/transaction-pay-controller",
|
|
3
|
-
"version": "19.0
|
|
3
|
+
"version": "19.1.0",
|
|
4
4
|
"description": "Manages alternate payment strategies to provide required funds for transactions in MetaMask",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"MetaMask",
|
|
@@ -40,7 +40,8 @@
|
|
|
40
40
|
"build:docs": "typedoc",
|
|
41
41
|
"changelog:update": "../../scripts/update-changelog.sh @metamask/transaction-pay-controller",
|
|
42
42
|
"changelog:validate": "../../scripts/validate-changelog.sh @metamask/transaction-pay-controller",
|
|
43
|
-
"
|
|
43
|
+
"messenger-action-types:check": "tsx ../../packages/messenger-cli/src/cli.ts --check",
|
|
44
|
+
"messenger-action-types:generate": "tsx ../../packages/messenger-cli/src/cli.ts --generate",
|
|
44
45
|
"prepare-manifest:preview": "../../scripts/prepare-preview-manifest.sh",
|
|
45
46
|
"since-latest-release": "../../scripts/since-latest-release.sh",
|
|
46
47
|
"test": "NODE_OPTIONS=--experimental-vm-modules jest --reporters=jest-silent-reporter",
|
|
@@ -52,7 +53,7 @@
|
|
|
52
53
|
"@ethersproject/abi": "^5.7.0",
|
|
53
54
|
"@ethersproject/contracts": "^5.7.0",
|
|
54
55
|
"@ethersproject/providers": "^5.7.0",
|
|
55
|
-
"@metamask/assets-controller": "^
|
|
56
|
+
"@metamask/assets-controller": "^5.0.0",
|
|
56
57
|
"@metamask/assets-controllers": "^103.1.1",
|
|
57
58
|
"@metamask/base-controller": "^9.0.1",
|
|
58
59
|
"@metamask/bridge-controller": "^70.0.1",
|