@metamask/transaction-pay-controller 21.0.0 → 21.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 +17 -1
- package/dist/TransactionPayController.cjs +25 -0
- package/dist/TransactionPayController.cjs.map +1 -1
- package/dist/TransactionPayController.d.cts.map +1 -1
- package/dist/TransactionPayController.d.mts.map +1 -1
- package/dist/TransactionPayController.mjs +26 -1
- package/dist/TransactionPayController.mjs.map +1 -1
- package/dist/strategy/fiat/fiat-submit.cjs +264 -5
- package/dist/strategy/fiat/fiat-submit.cjs.map +1 -1
- package/dist/strategy/fiat/fiat-submit.d.cts +9 -4
- package/dist/strategy/fiat/fiat-submit.d.cts.map +1 -1
- package/dist/strategy/fiat/fiat-submit.d.mts +9 -4
- package/dist/strategy/fiat/fiat-submit.d.mts.map +1 -1
- package/dist/strategy/fiat/fiat-submit.mjs +264 -5
- package/dist/strategy/fiat/fiat-submit.mjs.map +1 -1
- package/dist/strategy/relay/relay-quotes.cjs +7 -4
- package/dist/strategy/relay/relay-quotes.cjs.map +1 -1
- package/dist/strategy/relay/relay-quotes.d.cts.map +1 -1
- package/dist/strategy/relay/relay-quotes.d.mts.map +1 -1
- package/dist/strategy/relay/relay-quotes.mjs +7 -4
- package/dist/strategy/relay/relay-quotes.mjs.map +1 -1
- package/dist/types.cjs +2 -1
- package/dist/types.cjs.map +1 -1
- package/dist/types.d.cts +5 -3
- package/dist/types.d.cts.map +1 -1
- package/dist/types.d.mts +5 -3
- package/dist/types.d.mts.map +1 -1
- package/dist/types.mjs +2 -1
- package/dist/types.mjs.map +1 -1
- package/package.json +6 -6
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,21 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [21.1.0]
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
|
|
14
|
+
- Allow EIP-7702 authorizations from accounts in the Money keyring ([#8687](https://github.com/MetaMask/core/pull/8687))
|
|
15
|
+
- Implement fiat strategy submit flow with order polling and relay execution ([#8347](https://github.com/MetaMask/core/pull/8347))
|
|
16
|
+
|
|
17
|
+
### Changed
|
|
18
|
+
|
|
19
|
+
- Bump `@metamask/bridge-controller` from `^71.0.0` to `^71.1.1` ([#8706](https://github.com/MetaMask/core/pull/8706), [#8721](https://github.com/MetaMask/core/pull/8721))
|
|
20
|
+
- Bump `@metamask/transaction-controller` from `^65.0.0` to `^65.1.0` ([#8691](https://github.com/MetaMask/core/pull/8691))
|
|
21
|
+
- Bump `@metamask/ramps-controller` from `^13.2.0` to `^13.3.0` ([#8698](https://github.com/MetaMask/core/pull/8698))
|
|
22
|
+
- Bump `@metamask/assets-controller` from `^6.3.0` to `^6.4.0` ([#8721](https://github.com/MetaMask/core/pull/8721))
|
|
23
|
+
- Bump `@metamask/assets-controllers` from `^105.1.0` to `^106.0.0` ([#8721](https://github.com/MetaMask/core/pull/8721))
|
|
24
|
+
|
|
10
25
|
## [21.0.0]
|
|
11
26
|
|
|
12
27
|
### Added
|
|
@@ -764,7 +779,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
764
779
|
|
|
765
780
|
- Initial release ([#6820](https://github.com/MetaMask/core/pull/6820))
|
|
766
781
|
|
|
767
|
-
[Unreleased]: https://github.com/MetaMask/core/compare/@metamask/transaction-pay-controller@21.
|
|
782
|
+
[Unreleased]: https://github.com/MetaMask/core/compare/@metamask/transaction-pay-controller@21.1.0...HEAD
|
|
783
|
+
[21.1.0]: https://github.com/MetaMask/core/compare/@metamask/transaction-pay-controller@21.0.0...@metamask/transaction-pay-controller@21.1.0
|
|
768
784
|
[21.0.0]: https://github.com/MetaMask/core/compare/@metamask/transaction-pay-controller@20.2.0...@metamask/transaction-pay-controller@21.0.0
|
|
769
785
|
[20.2.0]: https://github.com/MetaMask/core/compare/@metamask/transaction-pay-controller@20.1.0...@metamask/transaction-pay-controller@20.2.0
|
|
770
786
|
[20.1.0]: https://github.com/MetaMask/core/compare/@metamask/transaction-pay-controller@20.0.1...@metamask/transaction-pay-controller@20.1.0
|
|
@@ -19,6 +19,7 @@ const update_fiat_payment_1 = require("./actions/update-fiat-payment.cjs");
|
|
|
19
19
|
const update_payment_token_1 = require("./actions/update-payment-token.cjs");
|
|
20
20
|
const constants_1 = require("./constants.cjs");
|
|
21
21
|
const QuoteRefresher_1 = require("./helpers/QuoteRefresher.cjs");
|
|
22
|
+
const utils_1 = require("./strategy/fiat/utils.cjs");
|
|
22
23
|
const feature_flags_1 = require("./utils/feature-flags.cjs");
|
|
23
24
|
const quotes_1 = require("./utils/quotes.cjs");
|
|
24
25
|
const source_amounts_1 = require("./utils/source-amounts.cjs");
|
|
@@ -163,6 +164,7 @@ _TransactionPayController_getDelegationTransaction = new WeakMap(), _Transaction
|
|
|
163
164
|
});
|
|
164
165
|
}, _TransactionPayController_updateTransactionData = function _TransactionPayController_updateTransactionData(transactionId, fn) {
|
|
165
166
|
let shouldUpdateQuotes = false;
|
|
167
|
+
let shouldUpdateFiatToken = false;
|
|
166
168
|
this.update((state) => {
|
|
167
169
|
const { transactionData } = state;
|
|
168
170
|
let current = transactionData[transactionId];
|
|
@@ -171,6 +173,8 @@ _TransactionPayController_getDelegationTransaction = new WeakMap(), _Transaction
|
|
|
171
173
|
const originalIsMaxAmount = current?.isMaxAmount;
|
|
172
174
|
const originalIsPostQuote = current?.isPostQuote;
|
|
173
175
|
const originalAccountOverride = current?.accountOverride;
|
|
176
|
+
const originalFiatPaymentAmount = current?.fiatPayment?.amountFiat;
|
|
177
|
+
const originalFiatPaymentMethodId = current?.fiatPayment?.selectedPaymentMethodId;
|
|
174
178
|
if (!current) {
|
|
175
179
|
transactionData[transactionId] = {
|
|
176
180
|
fiatPayment: {},
|
|
@@ -187,6 +191,9 @@ _TransactionPayController_getDelegationTransaction = new WeakMap(), _Transaction
|
|
|
187
191
|
const isIsMaxUpdated = current.isMaxAmount !== originalIsMaxAmount;
|
|
188
192
|
const isPostQuoteUpdated = current.isPostQuote !== originalIsPostQuote;
|
|
189
193
|
const isAccountOverrideUpdated = current.accountOverride !== originalAccountOverride;
|
|
194
|
+
const isFiatAmountUpdated = current.fiatPayment?.amountFiat !== originalFiatPaymentAmount;
|
|
195
|
+
const isFiatPaymentMethodUpdated = current.fiatPayment?.selectedPaymentMethodId !==
|
|
196
|
+
originalFiatPaymentMethodId;
|
|
190
197
|
if (isPaymentTokenUpdated ||
|
|
191
198
|
isIsMaxUpdated ||
|
|
192
199
|
isTokensUpdated ||
|
|
@@ -195,7 +202,25 @@ _TransactionPayController_getDelegationTransaction = new WeakMap(), _Transaction
|
|
|
195
202
|
(0, source_amounts_1.updateSourceAmounts)(transactionId, current, this.messenger);
|
|
196
203
|
shouldUpdateQuotes = true;
|
|
197
204
|
}
|
|
205
|
+
if (isFiatAmountUpdated || isFiatPaymentMethodUpdated) {
|
|
206
|
+
shouldUpdateQuotes = true;
|
|
207
|
+
}
|
|
208
|
+
if (isFiatPaymentMethodUpdated) {
|
|
209
|
+
shouldUpdateFiatToken = true;
|
|
210
|
+
}
|
|
198
211
|
});
|
|
212
|
+
if (shouldUpdateFiatToken) {
|
|
213
|
+
const transaction = (0, transaction_1.getTransaction)(transactionId, this.messenger);
|
|
214
|
+
const fiatAsset = (0, utils_1.deriveFiatAssetForFiatPayment)(transaction);
|
|
215
|
+
if (fiatAsset) {
|
|
216
|
+
try {
|
|
217
|
+
this.messenger.call('RampsController:setSelectedToken', fiatAsset.caipAssetId);
|
|
218
|
+
}
|
|
219
|
+
catch {
|
|
220
|
+
// Intentionally no-op — tokens may not be loaded in RampsController yet.
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
}
|
|
199
224
|
if (shouldUpdateQuotes) {
|
|
200
225
|
(0, quotes_1.updateQuotes)({
|
|
201
226
|
getStrategies: __classPrivateFieldGet(this, _TransactionPayController_instances, "m", _TransactionPayController_getStrategiesWithFallback).bind(this),
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TransactionPayController.cjs","sourceRoot":"","sources":["../src/TransactionPayController.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AACA,+DAA2D;AAG3D,mCAA8B;AAE9B,2EAAkE;AAClE,6EAAoE;AACpE,+CAIqB;AACrB,iEAA0D;AAW1D,6DAAyD;AACzD,+CAA8C;AAC9C,+DAA6D;AAC7D,yDAA6D;AAE7D,MAAM,yBAAyB,GAAG;IAChC,0BAA0B;IAC1B,aAAa;IACb,sBAAsB;IACtB,mBAAmB;IACnB,oBAAoB;CACZ,CAAC;AAEX,MAAM,aAAa,GAAiD;IAClE,eAAe,EAAE;QACf,sBAAsB,EAAE,KAAK;QAC7B,kBAAkB,EAAE,IAAI;QACxB,OAAO,EAAE,KAAK;QACd,QAAQ,EAAE,IAAI;KACf;CACF,CAAC;AAEF,MAAM,eAAe,GAAG,GAAkC,EAAE,CAAC,CAAC;IAC5D,eAAe,EAAE,EAAE;CACpB,CAAC,CAAC;AAEH,MAAa,wBAAyB,SAAQ,gCAI7C;IAWC,YAAY,EACV,wBAAwB,EACxB,WAAW,EACX,aAAa,EACb,SAAS,EACT,KAAK,GAC2B;QAChC,KAAK,CAAC;YACJ,IAAI,EAAE,2BAAe;YACrB,QAAQ,EAAE,aAAa;YACvB,SAAS;YACT,KAAK,EAAE,EAAE,GAAG,eAAe,EAAE,EAAE,GAAG,KAAK,EAAE;SAC1C,CAAC,CAAC;;QAtBI,qEAA4D;QAE5D,wDAEmB;QAEnB,0DAEqB;QAgB5B,uBAAA,IAAI,sDAA6B,wBAAwB,MAAA,CAAC;QAC1D,uBAAA,IAAI,yCAAgB,WAAW,MAAA,CAAC;QAChC,uBAAA,IAAI,2CAAkB,aAAa,MAAA,CAAC;QAEpC,IAAI,CAAC,SAAS,CAAC,4BAA4B,CACzC,IAAI,EACJ,yBAAyB,CAC1B,CAAC;QAEF,IAAA,oCAAs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AC,IAAI,CAAC;YACzD,SAAS,EAAE,IAAI,CAAC,SAAS;YACzB,eAAe,EAAE,IAAI,CAAC,KAAK,CAAC,eAAe,CAAC,aAAa,CAAC;YAC1D,aAAa;YACb,qBAAqB,EAAE,uBAAA,IAAI,4FAAuB,CAAC,IAAI,CAAC,IAAI,CAAC;SAC9D,CAAC,CAAC,KAAK,CAAC,aAAI,CAAC,CAAC;IACjB,CAAC;AACH,CAAC,qHAGC,WAA4B;IAE5B,MAAM,kBAAkB,GACtB,uBAAA,IAAI,+CAAe,EAAE,KAArB,IAAI,EAAkB,WAAW,CAAC;QAClC,CAAC,uBAAA,IAAI,6CAAa,CAAC,CAAC,CAAC,CAAC,uBAAA,IAAI,6CAAa,MAAjB,IAAI,EAAc,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;IAE9D,MAAM,eAAe,GAAG,kBAAkB,CAAC,MAAM,CAC/C,CAAC,QAAQ,EAAsC,EAAE,CAC/C,IAAA,oCAAwB,EAAC,QAAQ,CAAC,CACrC,CAAC;IAEF,IAAI,eAAe,CAAC,MAAM,EAAE,CAAC;QAC3B,OAAO,eAAe,CAAC;IACzB,CAAC;IAED,MAAM,YAAY,GAChB,IAAI,CAAC,KAAK,CAAC,eAAe,CAAC,WAAW,CAAC,EAAE,CAAC,EAAE,YAAY,CAAC;IAE3D,OAAO,IAAA,gCAAgB,EACrB,IAAI,CAAC,SAAS,EACd,YAAY,EAAE,OAAO,EACrB,YAAY,EAAE,OAAO,EACrB,WAAW,CAAC,IAAI,CACjB,CAAC;AACJ,CAAC","sourcesContent":["import type { StateMetadata } from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport type { TransactionMeta } from '@metamask/transaction-controller';\nimport type { Draft } from 'immer';\nimport { noop } from 'lodash';\n\nimport { updateFiatPayment } from './actions/update-fiat-payment';\nimport { updatePaymentToken } from './actions/update-payment-token';\nimport {\n CONTROLLER_NAME,\n isTransactionPayStrategy,\n TransactionPayStrategy,\n} from './constants';\nimport { QuoteRefresher } from './helpers/QuoteRefresher';\nimport type {\n GetDelegationTransactionCallback,\n TransactionConfigCallback,\n TransactionData,\n TransactionPayControllerMessenger,\n TransactionPayControllerOptions,\n TransactionPayControllerState,\n UpdateFiatPaymentRequest,\n UpdatePaymentTokenRequest,\n} from './types';\nimport { getStrategyOrder } from './utils/feature-flags';\nimport { updateQuotes } from './utils/quotes';\nimport { updateSourceAmounts } from './utils/source-amounts';\nimport { pollTransactionChanges } from './utils/transaction';\n\nconst MESSENGER_EXPOSED_METHODS = [\n 'getDelegationTransaction',\n 'getStrategy',\n 'setTransactionConfig',\n 'updateFiatPayment',\n 'updatePaymentToken',\n] as const;\n\nconst stateMetadata: StateMetadata<TransactionPayControllerState> = {\n transactionData: {\n includeInDebugSnapshot: false,\n includeInStateLogs: true,\n persist: false,\n usedInUi: true,\n },\n};\n\nconst getDefaultState = (): TransactionPayControllerState => ({\n transactionData: {},\n});\n\nexport class TransactionPayController extends BaseController<\n typeof CONTROLLER_NAME,\n TransactionPayControllerState,\n TransactionPayControllerMessenger\n> {\n readonly #getDelegationTransaction: GetDelegationTransactionCallback;\n\n readonly #getStrategy?: (\n transaction: TransactionMeta,\n ) => TransactionPayStrategy;\n\n readonly #getStrategies?: (\n transaction: TransactionMeta,\n ) => TransactionPayStrategy[];\n\n constructor({\n getDelegationTransaction,\n getStrategy,\n getStrategies,\n messenger,\n state,\n }: TransactionPayControllerOptions) {\n super({\n name: CONTROLLER_NAME,\n metadata: stateMetadata,\n messenger,\n state: { ...getDefaultState(), ...state },\n });\n\n this.#getDelegationTransaction = getDelegationTransaction;\n this.#getStrategy = getStrategy;\n this.#getStrategies = getStrategies;\n\n this.messenger.registerMethodActionHandlers(\n this,\n MESSENGER_EXPOSED_METHODS,\n );\n\n pollTransactionChanges(\n messenger,\n this.#updateTransactionData.bind(this),\n this.#removeTransactionData.bind(this),\n );\n\n // eslint-disable-next-line no-new\n new QuoteRefresher({\n getStrategies: this.#getStrategiesWithFallback.bind(this),\n messenger,\n updateTransactionData: this.#updateTransactionData.bind(this),\n });\n }\n\n /**\n * Sets the transaction configuration.\n *\n * The callback receives the current configuration properties and can mutate\n * them in place. Updated values are written back to the transaction data.\n *\n * @param transactionId - The ID of the transaction to configure.\n * @param callback - A callback that receives a mutable {@link TransactionConfig} object.\n */\n setTransactionConfig(\n transactionId: string,\n callback: TransactionConfigCallback,\n ): void {\n this.#updateTransactionData(transactionId, (transactionData) => {\n const config = {\n isMaxAmount: transactionData.isMaxAmount,\n isPostQuote: transactionData.isPostQuote,\n isHyperliquidSource: transactionData.isHyperliquidSource,\n refundTo: transactionData.refundTo,\n accountOverride: transactionData.accountOverride,\n };\n\n const previousAccountOverride = config.accountOverride;\n\n callback(config);\n\n transactionData.accountOverride = config.accountOverride;\n transactionData.isMaxAmount = config.isMaxAmount;\n transactionData.isPostQuote = config.isPostQuote;\n transactionData.isHyperliquidSource = config.isHyperliquidSource;\n transactionData.refundTo = config.refundTo;\n\n if (config.accountOverride !== previousAccountOverride) {\n transactionData.paymentToken = undefined;\n }\n });\n }\n\n /**\n * Updates the payment token for a transaction.\n *\n * Resolves token metadata and balances, then stores the new payment token\n * in the transaction data. This triggers recalculation of source amounts\n * and quote retrieval.\n *\n * @param request - The payment token update request containing the\n * transaction ID, token address, and chain ID.\n */\n updatePaymentToken(request: UpdatePaymentTokenRequest): void {\n updatePaymentToken(request, {\n messenger: this.messenger,\n updateTransactionData: this.#updateTransactionData.bind(this),\n });\n }\n\n /**\n * Updates the fiat payment state for a transaction.\n *\n * The request callback receives the current fiat payment state and can\n * mutate it to update properties such as the selected payment method or\n * fiat amount.\n *\n * @param request - The fiat payment update request containing the\n * transaction ID and a callback to mutate fiat payment state.\n */\n updateFiatPayment(request: UpdateFiatPaymentRequest): void {\n updateFiatPayment(request, {\n messenger: this.messenger,\n updateTransactionData: this.#updateTransactionData.bind(this),\n });\n }\n\n /**\n * Gets the delegation transaction for a given transaction.\n *\n * Converts the provided transaction into a redeem delegation by delegating\n * to the configured callback. Returns the delegation transaction data\n * including the encoded call data, target address, value, and an optional\n * authorization list.\n *\n * @param args - The arguments forwarded to the {@link GetDelegationTransactionCallback},\n * containing the transaction metadata.\n * @returns A promise resolving to the delegation transaction data.\n */\n getDelegationTransaction(\n ...args: Parameters<GetDelegationTransactionCallback>\n ): ReturnType<GetDelegationTransactionCallback> {\n return this.#getDelegationTransaction(...args);\n }\n\n /**\n * Gets the preferred strategy for a transaction.\n *\n * Returns the first strategy from the ordered list of strategies applicable\n * to the given transaction. Falls back to the default strategy order derived\n * from feature flags when no custom strategy callback is configured.\n *\n * @param transaction - The transaction metadata to determine the strategy for.\n * @returns The preferred {@link TransactionPayStrategy} for the transaction.\n */\n getStrategy(transaction: TransactionMeta): TransactionPayStrategy {\n return this.#getStrategiesWithFallback(transaction)[0];\n }\n\n #removeTransactionData(transactionId: string): void {\n this.update((state) => {\n delete state.transactionData[transactionId];\n });\n }\n\n #updateTransactionData(\n transactionId: string,\n fn: (transactionData: Draft<TransactionData>) => void,\n ): void {\n let shouldUpdateQuotes = false;\n\n this.update((state) => {\n const { transactionData } = state;\n let current = transactionData[transactionId];\n const originalPaymentToken = current?.paymentToken;\n const originalTokens = current?.tokens;\n const originalIsMaxAmount = current?.isMaxAmount;\n const originalIsPostQuote = current?.isPostQuote;\n const originalAccountOverride = current?.accountOverride;\n\n if (!current) {\n transactionData[transactionId] = {\n fiatPayment: {},\n isLoading: false,\n tokens: [],\n };\n\n current = transactionData[transactionId];\n }\n\n fn(current);\n\n const isPaymentTokenUpdated =\n current.paymentToken?.address?.toLowerCase() !==\n originalPaymentToken?.address?.toLowerCase() ||\n current.paymentToken?.chainId !== originalPaymentToken?.chainId;\n\n const isTokensUpdated = current.tokens !== originalTokens;\n const isIsMaxUpdated = current.isMaxAmount !== originalIsMaxAmount;\n const isPostQuoteUpdated = current.isPostQuote !== originalIsPostQuote;\n const isAccountOverrideUpdated =\n current.accountOverride !== originalAccountOverride;\n\n if (\n isPaymentTokenUpdated ||\n isIsMaxUpdated ||\n isTokensUpdated ||\n isPostQuoteUpdated ||\n isAccountOverrideUpdated\n ) {\n updateSourceAmounts(transactionId, current as never, this.messenger);\n\n shouldUpdateQuotes = true;\n }\n });\n\n if (shouldUpdateQuotes) {\n updateQuotes({\n getStrategies: this.#getStrategiesWithFallback.bind(this),\n messenger: this.messenger,\n transactionData: this.state.transactionData[transactionId],\n transactionId,\n updateTransactionData: this.#updateTransactionData.bind(this),\n }).catch(noop);\n }\n }\n\n #getStrategiesWithFallback(\n transaction: TransactionMeta,\n ): TransactionPayStrategy[] {\n const strategyCandidates: unknown[] =\n this.#getStrategies?.(transaction) ??\n (this.#getStrategy ? [this.#getStrategy(transaction)] : []);\n\n const validStrategies = strategyCandidates.filter(\n (strategy): strategy is TransactionPayStrategy =>\n isTransactionPayStrategy(strategy),\n );\n\n if (validStrategies.length) {\n return validStrategies;\n }\n\n const paymentToken =\n this.state.transactionData[transaction.id]?.paymentToken;\n\n return getStrategyOrder(\n this.messenger,\n paymentToken?.chainId,\n paymentToken?.address,\n transaction.type,\n );\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"TransactionPayController.cjs","sourceRoot":"","sources":["../src/TransactionPayController.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AACA,+DAA2D;AAG3D,mCAA8B;AAE9B,2EAAkE;AAClE,6EAAoE;AACpE,+CAIqB;AACrB,iEAA0D;AAC1D,qDAAsE;AAWtE,6DAAyD;AACzD,+CAA8C;AAC9C,+DAA6D;AAC7D,yDAA6E;AAE7E,MAAM,yBAAyB,GAAG;IAChC,0BAA0B;IAC1B,aAAa;IACb,sBAAsB;IACtB,mBAAmB;IACnB,oBAAoB;CACZ,CAAC;AAEX,MAAM,aAAa,GAAiD;IAClE,eAAe,EAAE;QACf,sBAAsB,EAAE,KAAK;QAC7B,kBAAkB,EAAE,IAAI;QACxB,OAAO,EAAE,KAAK;QACd,QAAQ,EAAE,IAAI;KACf;CACF,CAAC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B,YAAY,EAAE,OAAO,EACrB,WAAW,CAAC,IAAI,CACjB,CAAC;AACJ,CAAC","sourcesContent":["import type { StateMetadata } from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport type { TransactionMeta } from '@metamask/transaction-controller';\nimport type { Draft } from 'immer';\nimport { noop } from 'lodash';\n\nimport { updateFiatPayment } from './actions/update-fiat-payment';\nimport { updatePaymentToken } from './actions/update-payment-token';\nimport {\n CONTROLLER_NAME,\n isTransactionPayStrategy,\n TransactionPayStrategy,\n} from './constants';\nimport { QuoteRefresher } from './helpers/QuoteRefresher';\nimport { deriveFiatAssetForFiatPayment } from './strategy/fiat/utils';\nimport type {\n GetDelegationTransactionCallback,\n TransactionConfigCallback,\n TransactionData,\n TransactionPayControllerMessenger,\n TransactionPayControllerOptions,\n TransactionPayControllerState,\n UpdateFiatPaymentRequest,\n UpdatePaymentTokenRequest,\n} from './types';\nimport { getStrategyOrder } from './utils/feature-flags';\nimport { updateQuotes } from './utils/quotes';\nimport { updateSourceAmounts } from './utils/source-amounts';\nimport { getTransaction, pollTransactionChanges } from './utils/transaction';\n\nconst MESSENGER_EXPOSED_METHODS = [\n 'getDelegationTransaction',\n 'getStrategy',\n 'setTransactionConfig',\n 'updateFiatPayment',\n 'updatePaymentToken',\n] as const;\n\nconst stateMetadata: StateMetadata<TransactionPayControllerState> = {\n transactionData: {\n includeInDebugSnapshot: false,\n includeInStateLogs: true,\n persist: false,\n usedInUi: true,\n },\n};\n\nconst getDefaultState = (): TransactionPayControllerState => ({\n transactionData: {},\n});\n\nexport class TransactionPayController extends BaseController<\n typeof CONTROLLER_NAME,\n TransactionPayControllerState,\n TransactionPayControllerMessenger\n> {\n readonly #getDelegationTransaction: GetDelegationTransactionCallback;\n\n readonly #getStrategy?: (\n transaction: TransactionMeta,\n ) => TransactionPayStrategy;\n\n readonly #getStrategies?: (\n transaction: TransactionMeta,\n ) => TransactionPayStrategy[];\n\n constructor({\n getDelegationTransaction,\n getStrategy,\n getStrategies,\n messenger,\n state,\n }: TransactionPayControllerOptions) {\n super({\n name: CONTROLLER_NAME,\n metadata: stateMetadata,\n messenger,\n state: { ...getDefaultState(), ...state },\n });\n\n this.#getDelegationTransaction = getDelegationTransaction;\n this.#getStrategy = getStrategy;\n this.#getStrategies = getStrategies;\n\n this.messenger.registerMethodActionHandlers(\n this,\n MESSENGER_EXPOSED_METHODS,\n );\n\n pollTransactionChanges(\n messenger,\n this.#updateTransactionData.bind(this),\n this.#removeTransactionData.bind(this),\n );\n\n // eslint-disable-next-line no-new\n new QuoteRefresher({\n getStrategies: this.#getStrategiesWithFallback.bind(this),\n messenger,\n updateTransactionData: this.#updateTransactionData.bind(this),\n });\n }\n\n /**\n * Sets the transaction configuration.\n *\n * The callback receives the current configuration properties and can mutate\n * them in place. Updated values are written back to the transaction data.\n *\n * @param transactionId - The ID of the transaction to configure.\n * @param callback - A callback that receives a mutable {@link TransactionConfig} object.\n */\n setTransactionConfig(\n transactionId: string,\n callback: TransactionConfigCallback,\n ): void {\n this.#updateTransactionData(transactionId, (transactionData) => {\n const config = {\n isMaxAmount: transactionData.isMaxAmount,\n isPostQuote: transactionData.isPostQuote,\n isHyperliquidSource: transactionData.isHyperliquidSource,\n refundTo: transactionData.refundTo,\n accountOverride: transactionData.accountOverride,\n };\n\n const previousAccountOverride = config.accountOverride;\n\n callback(config);\n\n transactionData.accountOverride = config.accountOverride;\n transactionData.isMaxAmount = config.isMaxAmount;\n transactionData.isPostQuote = config.isPostQuote;\n transactionData.isHyperliquidSource = config.isHyperliquidSource;\n transactionData.refundTo = config.refundTo;\n\n if (config.accountOverride !== previousAccountOverride) {\n transactionData.paymentToken = undefined;\n }\n });\n }\n\n /**\n * Updates the payment token for a transaction.\n *\n * Resolves token metadata and balances, then stores the new payment token\n * in the transaction data. This triggers recalculation of source amounts\n * and quote retrieval.\n *\n * @param request - The payment token update request containing the\n * transaction ID, token address, and chain ID.\n */\n updatePaymentToken(request: UpdatePaymentTokenRequest): void {\n updatePaymentToken(request, {\n messenger: this.messenger,\n updateTransactionData: this.#updateTransactionData.bind(this),\n });\n }\n\n /**\n * Updates the fiat payment state for a transaction.\n *\n * The request callback receives the current fiat payment state and can\n * mutate it to update properties such as the selected payment method or\n * fiat amount.\n *\n * @param request - The fiat payment update request containing the\n * transaction ID and a callback to mutate fiat payment state.\n */\n updateFiatPayment(request: UpdateFiatPaymentRequest): void {\n updateFiatPayment(request, {\n messenger: this.messenger,\n updateTransactionData: this.#updateTransactionData.bind(this),\n });\n }\n\n /**\n * Gets the delegation transaction for a given transaction.\n *\n * Converts the provided transaction into a redeem delegation by delegating\n * to the configured callback. Returns the delegation transaction data\n * including the encoded call data, target address, value, and an optional\n * authorization list.\n *\n * @param args - The arguments forwarded to the {@link GetDelegationTransactionCallback},\n * containing the transaction metadata.\n * @returns A promise resolving to the delegation transaction data.\n */\n getDelegationTransaction(\n ...args: Parameters<GetDelegationTransactionCallback>\n ): ReturnType<GetDelegationTransactionCallback> {\n return this.#getDelegationTransaction(...args);\n }\n\n /**\n * Gets the preferred strategy for a transaction.\n *\n * Returns the first strategy from the ordered list of strategies applicable\n * to the given transaction. Falls back to the default strategy order derived\n * from feature flags when no custom strategy callback is configured.\n *\n * @param transaction - The transaction metadata to determine the strategy for.\n * @returns The preferred {@link TransactionPayStrategy} for the transaction.\n */\n getStrategy(transaction: TransactionMeta): TransactionPayStrategy {\n return this.#getStrategiesWithFallback(transaction)[0];\n }\n\n #removeTransactionData(transactionId: string): void {\n this.update((state) => {\n delete state.transactionData[transactionId];\n });\n }\n\n #updateTransactionData(\n transactionId: string,\n fn: (transactionData: Draft<TransactionData>) => void,\n ): void {\n let shouldUpdateQuotes = false;\n let shouldUpdateFiatToken = false;\n\n this.update((state) => {\n const { transactionData } = state;\n let current = transactionData[transactionId];\n const originalPaymentToken = current?.paymentToken;\n const originalTokens = current?.tokens;\n const originalIsMaxAmount = current?.isMaxAmount;\n const originalIsPostQuote = current?.isPostQuote;\n const originalAccountOverride = current?.accountOverride;\n const originalFiatPaymentAmount = current?.fiatPayment?.amountFiat;\n const originalFiatPaymentMethodId =\n current?.fiatPayment?.selectedPaymentMethodId;\n\n if (!current) {\n transactionData[transactionId] = {\n fiatPayment: {},\n isLoading: false,\n tokens: [],\n };\n\n current = transactionData[transactionId];\n }\n\n fn(current);\n\n const isPaymentTokenUpdated =\n current.paymentToken?.address?.toLowerCase() !==\n originalPaymentToken?.address?.toLowerCase() ||\n current.paymentToken?.chainId !== originalPaymentToken?.chainId;\n\n const isTokensUpdated = current.tokens !== originalTokens;\n const isIsMaxUpdated = current.isMaxAmount !== originalIsMaxAmount;\n const isPostQuoteUpdated = current.isPostQuote !== originalIsPostQuote;\n const isAccountOverrideUpdated =\n current.accountOverride !== originalAccountOverride;\n const isFiatAmountUpdated =\n current.fiatPayment?.amountFiat !== originalFiatPaymentAmount;\n const isFiatPaymentMethodUpdated =\n current.fiatPayment?.selectedPaymentMethodId !==\n originalFiatPaymentMethodId;\n\n if (\n isPaymentTokenUpdated ||\n isIsMaxUpdated ||\n isTokensUpdated ||\n isPostQuoteUpdated ||\n isAccountOverrideUpdated\n ) {\n updateSourceAmounts(transactionId, current as never, this.messenger);\n\n shouldUpdateQuotes = true;\n }\n\n if (isFiatAmountUpdated || isFiatPaymentMethodUpdated) {\n shouldUpdateQuotes = true;\n }\n\n if (isFiatPaymentMethodUpdated) {\n shouldUpdateFiatToken = true;\n }\n });\n\n if (shouldUpdateFiatToken) {\n const transaction = getTransaction(\n transactionId,\n this.messenger,\n ) as TransactionMeta;\n const fiatAsset = deriveFiatAssetForFiatPayment(transaction);\n if (fiatAsset) {\n try {\n this.messenger.call(\n 'RampsController:setSelectedToken',\n fiatAsset.caipAssetId,\n );\n } catch {\n // Intentionally no-op — tokens may not be loaded in RampsController yet.\n }\n }\n }\n\n if (shouldUpdateQuotes) {\n updateQuotes({\n getStrategies: this.#getStrategiesWithFallback.bind(this),\n messenger: this.messenger,\n transactionData: this.state.transactionData[transactionId],\n transactionId,\n updateTransactionData: this.#updateTransactionData.bind(this),\n }).catch(noop);\n }\n }\n\n #getStrategiesWithFallback(\n transaction: TransactionMeta,\n ): TransactionPayStrategy[] {\n const strategyCandidates: unknown[] =\n this.#getStrategies?.(transaction) ??\n (this.#getStrategy ? [this.#getStrategy(transaction)] : []);\n\n const validStrategies = strategyCandidates.filter(\n (strategy): strategy is TransactionPayStrategy =>\n isTransactionPayStrategy(strategy),\n );\n\n if (validStrategies.length) {\n return validStrategies;\n }\n\n const paymentToken =\n this.state.transactionData[transaction.id]?.paymentToken;\n\n return getStrategyOrder(\n this.messenger,\n paymentToken?.chainId,\n paymentToken?.address,\n transaction.type,\n );\n }\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TransactionPayController.d.cts","sourceRoot":"","sources":["../src/TransactionPayController.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,KAAK,EAAE,eAAe,EAAE,yCAAyC;AAMxE,OAAO,EACL,eAAe,EAEf,sBAAsB,EACvB,wBAAoB;
|
|
1
|
+
{"version":3,"file":"TransactionPayController.d.cts","sourceRoot":"","sources":["../src/TransactionPayController.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,KAAK,EAAE,eAAe,EAAE,yCAAyC;AAMxE,OAAO,EACL,eAAe,EAEf,sBAAsB,EACvB,wBAAoB;AAGrB,OAAO,KAAK,EACV,gCAAgC,EAChC,yBAAyB,EAEzB,iCAAiC,EACjC,+BAA+B,EAC/B,6BAA6B,EAC7B,wBAAwB,EACxB,yBAAyB,EAC1B,oBAAgB;AA2BjB,qBAAa,wBAAyB,SAAQ,cAAc,CAC1D,OAAO,eAAe,EACtB,6BAA6B,EAC7B,iCAAiC,CAClC;;gBAWa,EACV,wBAAwB,EACxB,WAAW,EACX,aAAa,EACb,SAAS,EACT,KAAK,GACN,EAAE,+BAA+B;IA+BlC;;;;;;;;OAQG;IACH,oBAAoB,CAClB,aAAa,EAAE,MAAM,EACrB,QAAQ,EAAE,yBAAyB,GAClC,IAAI;IA0BP;;;;;;;;;OASG;IACH,kBAAkB,CAAC,OAAO,EAAE,yBAAyB,GAAG,IAAI;IAO5D;;;;;;;;;OASG;IACH,iBAAiB,CAAC,OAAO,EAAE,wBAAwB,GAAG,IAAI;IAO1D;;;;;;;;;;;OAWG;IACH,wBAAwB,CACtB,GAAG,IAAI,EAAE,UAAU,CAAC,gCAAgC,CAAC,GACpD,UAAU,CAAC,gCAAgC,CAAC;IAI/C;;;;;;;;;OASG;IACH,WAAW,CAAC,WAAW,EAAE,eAAe,GAAG,sBAAsB;CAqIlE"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TransactionPayController.d.mts","sourceRoot":"","sources":["../src/TransactionPayController.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,KAAK,EAAE,eAAe,EAAE,yCAAyC;AAMxE,OAAO,EACL,eAAe,EAEf,sBAAsB,EACvB,wBAAoB;
|
|
1
|
+
{"version":3,"file":"TransactionPayController.d.mts","sourceRoot":"","sources":["../src/TransactionPayController.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,KAAK,EAAE,eAAe,EAAE,yCAAyC;AAMxE,OAAO,EACL,eAAe,EAEf,sBAAsB,EACvB,wBAAoB;AAGrB,OAAO,KAAK,EACV,gCAAgC,EAChC,yBAAyB,EAEzB,iCAAiC,EACjC,+BAA+B,EAC/B,6BAA6B,EAC7B,wBAAwB,EACxB,yBAAyB,EAC1B,oBAAgB;AA2BjB,qBAAa,wBAAyB,SAAQ,cAAc,CAC1D,OAAO,eAAe,EACtB,6BAA6B,EAC7B,iCAAiC,CAClC;;gBAWa,EACV,wBAAwB,EACxB,WAAW,EACX,aAAa,EACb,SAAS,EACT,KAAK,GACN,EAAE,+BAA+B;IA+BlC;;;;;;;;OAQG;IACH,oBAAoB,CAClB,aAAa,EAAE,MAAM,EACrB,QAAQ,EAAE,yBAAyB,GAClC,IAAI;IA0BP;;;;;;;;;OASG;IACH,kBAAkB,CAAC,OAAO,EAAE,yBAAyB,GAAG,IAAI;IAO5D;;;;;;;;;OASG;IACH,iBAAiB,CAAC,OAAO,EAAE,wBAAwB,GAAG,IAAI;IAO1D;;;;;;;;;;;OAWG;IACH,wBAAwB,CACtB,GAAG,IAAI,EAAE,UAAU,CAAC,gCAAgC,CAAC,GACpD,UAAU,CAAC,gCAAgC,CAAC;IAI/C;;;;;;;;;OASG;IACH,WAAW,CAAC,WAAW,EAAE,eAAe,GAAG,sBAAsB;CAqIlE"}
|
|
@@ -17,10 +17,11 @@ import { updateFiatPayment } from "./actions/update-fiat-payment.mjs";
|
|
|
17
17
|
import { updatePaymentToken } from "./actions/update-payment-token.mjs";
|
|
18
18
|
import { CONTROLLER_NAME, isTransactionPayStrategy, TransactionPayStrategy } from "./constants.mjs";
|
|
19
19
|
import { QuoteRefresher } from "./helpers/QuoteRefresher.mjs";
|
|
20
|
+
import { deriveFiatAssetForFiatPayment } from "./strategy/fiat/utils.mjs";
|
|
20
21
|
import { getStrategyOrder } from "./utils/feature-flags.mjs";
|
|
21
22
|
import { updateQuotes } from "./utils/quotes.mjs";
|
|
22
23
|
import { updateSourceAmounts } from "./utils/source-amounts.mjs";
|
|
23
|
-
import { pollTransactionChanges } from "./utils/transaction.mjs";
|
|
24
|
+
import { getTransaction, pollTransactionChanges } from "./utils/transaction.mjs";
|
|
24
25
|
const MESSENGER_EXPOSED_METHODS = [
|
|
25
26
|
'getDelegationTransaction',
|
|
26
27
|
'getStrategy',
|
|
@@ -160,6 +161,7 @@ _TransactionPayController_getDelegationTransaction = new WeakMap(), _Transaction
|
|
|
160
161
|
});
|
|
161
162
|
}, _TransactionPayController_updateTransactionData = function _TransactionPayController_updateTransactionData(transactionId, fn) {
|
|
162
163
|
let shouldUpdateQuotes = false;
|
|
164
|
+
let shouldUpdateFiatToken = false;
|
|
163
165
|
this.update((state) => {
|
|
164
166
|
const { transactionData } = state;
|
|
165
167
|
let current = transactionData[transactionId];
|
|
@@ -168,6 +170,8 @@ _TransactionPayController_getDelegationTransaction = new WeakMap(), _Transaction
|
|
|
168
170
|
const originalIsMaxAmount = current?.isMaxAmount;
|
|
169
171
|
const originalIsPostQuote = current?.isPostQuote;
|
|
170
172
|
const originalAccountOverride = current?.accountOverride;
|
|
173
|
+
const originalFiatPaymentAmount = current?.fiatPayment?.amountFiat;
|
|
174
|
+
const originalFiatPaymentMethodId = current?.fiatPayment?.selectedPaymentMethodId;
|
|
171
175
|
if (!current) {
|
|
172
176
|
transactionData[transactionId] = {
|
|
173
177
|
fiatPayment: {},
|
|
@@ -184,6 +188,9 @@ _TransactionPayController_getDelegationTransaction = new WeakMap(), _Transaction
|
|
|
184
188
|
const isIsMaxUpdated = current.isMaxAmount !== originalIsMaxAmount;
|
|
185
189
|
const isPostQuoteUpdated = current.isPostQuote !== originalIsPostQuote;
|
|
186
190
|
const isAccountOverrideUpdated = current.accountOverride !== originalAccountOverride;
|
|
191
|
+
const isFiatAmountUpdated = current.fiatPayment?.amountFiat !== originalFiatPaymentAmount;
|
|
192
|
+
const isFiatPaymentMethodUpdated = current.fiatPayment?.selectedPaymentMethodId !==
|
|
193
|
+
originalFiatPaymentMethodId;
|
|
187
194
|
if (isPaymentTokenUpdated ||
|
|
188
195
|
isIsMaxUpdated ||
|
|
189
196
|
isTokensUpdated ||
|
|
@@ -192,7 +199,25 @@ _TransactionPayController_getDelegationTransaction = new WeakMap(), _Transaction
|
|
|
192
199
|
updateSourceAmounts(transactionId, current, this.messenger);
|
|
193
200
|
shouldUpdateQuotes = true;
|
|
194
201
|
}
|
|
202
|
+
if (isFiatAmountUpdated || isFiatPaymentMethodUpdated) {
|
|
203
|
+
shouldUpdateQuotes = true;
|
|
204
|
+
}
|
|
205
|
+
if (isFiatPaymentMethodUpdated) {
|
|
206
|
+
shouldUpdateFiatToken = true;
|
|
207
|
+
}
|
|
195
208
|
});
|
|
209
|
+
if (shouldUpdateFiatToken) {
|
|
210
|
+
const transaction = getTransaction(transactionId, this.messenger);
|
|
211
|
+
const fiatAsset = deriveFiatAssetForFiatPayment(transaction);
|
|
212
|
+
if (fiatAsset) {
|
|
213
|
+
try {
|
|
214
|
+
this.messenger.call('RampsController:setSelectedToken', fiatAsset.caipAssetId);
|
|
215
|
+
}
|
|
216
|
+
catch {
|
|
217
|
+
// Intentionally no-op — tokens may not be loaded in RampsController yet.
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
196
221
|
if (shouldUpdateQuotes) {
|
|
197
222
|
updateQuotes({
|
|
198
223
|
getStrategies: __classPrivateFieldGet(this, _TransactionPayController_instances, "m", _TransactionPayController_getStrategiesWithFallback).bind(this),
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TransactionPayController.mjs","sourceRoot":"","sources":["../src/TransactionPayController.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,OAAO,EAAE,cAAc,EAAE,kCAAkC;;;AAK3D,OAAO,EAAE,iBAAiB,EAAE,0CAAsC;AAClE,OAAO,EAAE,kBAAkB,EAAE,2CAAuC;AACpE,OAAO,EACL,eAAe,EACf,wBAAwB,EACxB,sBAAsB,EACvB,wBAAoB;AACrB,OAAO,EAAE,cAAc,EAAE,qCAAiC;AAW1D,OAAO,EAAE,gBAAgB,EAAE,kCAA8B;AACzD,OAAO,EAAE,YAAY,EAAE,2BAAuB;AAC9C,OAAO,EAAE,mBAAmB,EAAE,mCAA+B;AAC7D,OAAO,EAAE,sBAAsB,EAAE,gCAA4B;AAE7D,MAAM,yBAAyB,GAAG;IAChC,0BAA0B;IAC1B,aAAa;IACb,sBAAsB;IACtB,mBAAmB;IACnB,oBAAoB;CACZ,CAAC;AAEX,MAAM,aAAa,GAAiD;IAClE,eAAe,EAAE;QACf,sBAAsB,EAAE,KAAK;QAC7B,kBAAkB,EAAE,IAAI;QACx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,CAAC,MAAM,CAC/C,CAAC,QAAQ,EAAsC,EAAE,CAC/C,wBAAwB,CAAC,QAAQ,CAAC,CACrC,CAAC;IAEF,IAAI,eAAe,CAAC,MAAM,EAAE,CAAC;QAC3B,OAAO,eAAe,CAAC;IACzB,CAAC;IAED,MAAM,YAAY,GAChB,IAAI,CAAC,KAAK,CAAC,eAAe,CAAC,WAAW,CAAC,EAAE,CAAC,EAAE,YAAY,CAAC;IAE3D,OAAO,gBAAgB,CACrB,IAAI,CAAC,SAAS,EACd,YAAY,EAAE,OAAO,EACrB,YAAY,EAAE,OAAO,EACrB,WAAW,CAAC,IAAI,CACjB,CAAC;AACJ,CAAC","sourcesContent":["import type { StateMetadata } from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport type { TransactionMeta } from '@metamask/transaction-controller';\nimport type { Draft } from 'immer';\nimport { noop } from 'lodash';\n\nimport { updateFiatPayment } from './actions/update-fiat-payment';\nimport { updatePaymentToken } from './actions/update-payment-token';\nimport {\n CONTROLLER_NAME,\n isTransactionPayStrategy,\n TransactionPayStrategy,\n} from './constants';\nimport { QuoteRefresher } from './helpers/QuoteRefresher';\nimport type {\n GetDelegationTransactionCallback,\n TransactionConfigCallback,\n TransactionData,\n TransactionPayControllerMessenger,\n TransactionPayControllerOptions,\n TransactionPayControllerState,\n UpdateFiatPaymentRequest,\n UpdatePaymentTokenRequest,\n} from './types';\nimport { getStrategyOrder } from './utils/feature-flags';\nimport { updateQuotes } from './utils/quotes';\nimport { updateSourceAmounts } from './utils/source-amounts';\nimport { pollTransactionChanges } from './utils/transaction';\n\nconst MESSENGER_EXPOSED_METHODS = [\n 'getDelegationTransaction',\n 'getStrategy',\n 'setTransactionConfig',\n 'updateFiatPayment',\n 'updatePaymentToken',\n] as const;\n\nconst stateMetadata: StateMetadata<TransactionPayControllerState> = {\n transactionData: {\n includeInDebugSnapshot: false,\n includeInStateLogs: true,\n persist: false,\n usedInUi: true,\n },\n};\n\nconst getDefaultState = (): TransactionPayControllerState => ({\n transactionData: {},\n});\n\nexport class TransactionPayController extends BaseController<\n typeof CONTROLLER_NAME,\n TransactionPayControllerState,\n TransactionPayControllerMessenger\n> {\n readonly #getDelegationTransaction: GetDelegationTransactionCallback;\n\n readonly #getStrategy?: (\n transaction: TransactionMeta,\n ) => TransactionPayStrategy;\n\n readonly #getStrategies?: (\n transaction: TransactionMeta,\n ) => TransactionPayStrategy[];\n\n constructor({\n getDelegationTransaction,\n getStrategy,\n getStrategies,\n messenger,\n state,\n }: TransactionPayControllerOptions) {\n super({\n name: CONTROLLER_NAME,\n metadata: stateMetadata,\n messenger,\n state: { ...getDefaultState(), ...state },\n });\n\n this.#getDelegationTransaction = getDelegationTransaction;\n this.#getStrategy = getStrategy;\n this.#getStrategies = getStrategies;\n\n this.messenger.registerMethodActionHandlers(\n this,\n MESSENGER_EXPOSED_METHODS,\n );\n\n pollTransactionChanges(\n messenger,\n this.#updateTransactionData.bind(this),\n this.#removeTransactionData.bind(this),\n );\n\n // eslint-disable-next-line no-new\n new QuoteRefresher({\n getStrategies: this.#getStrategiesWithFallback.bind(this),\n messenger,\n updateTransactionData: this.#updateTransactionData.bind(this),\n });\n }\n\n /**\n * Sets the transaction configuration.\n *\n * The callback receives the current configuration properties and can mutate\n * them in place. Updated values are written back to the transaction data.\n *\n * @param transactionId - The ID of the transaction to configure.\n * @param callback - A callback that receives a mutable {@link TransactionConfig} object.\n */\n setTransactionConfig(\n transactionId: string,\n callback: TransactionConfigCallback,\n ): void {\n this.#updateTransactionData(transactionId, (transactionData) => {\n const config = {\n isMaxAmount: transactionData.isMaxAmount,\n isPostQuote: transactionData.isPostQuote,\n isHyperliquidSource: transactionData.isHyperliquidSource,\n refundTo: transactionData.refundTo,\n accountOverride: transactionData.accountOverride,\n };\n\n const previousAccountOverride = config.accountOverride;\n\n callback(config);\n\n transactionData.accountOverride = config.accountOverride;\n transactionData.isMaxAmount = config.isMaxAmount;\n transactionData.isPostQuote = config.isPostQuote;\n transactionData.isHyperliquidSource = config.isHyperliquidSource;\n transactionData.refundTo = config.refundTo;\n\n if (config.accountOverride !== previousAccountOverride) {\n transactionData.paymentToken = undefined;\n }\n });\n }\n\n /**\n * Updates the payment token for a transaction.\n *\n * Resolves token metadata and balances, then stores the new payment token\n * in the transaction data. This triggers recalculation of source amounts\n * and quote retrieval.\n *\n * @param request - The payment token update request containing the\n * transaction ID, token address, and chain ID.\n */\n updatePaymentToken(request: UpdatePaymentTokenRequest): void {\n updatePaymentToken(request, {\n messenger: this.messenger,\n updateTransactionData: this.#updateTransactionData.bind(this),\n });\n }\n\n /**\n * Updates the fiat payment state for a transaction.\n *\n * The request callback receives the current fiat payment state and can\n * mutate it to update properties such as the selected payment method or\n * fiat amount.\n *\n * @param request - The fiat payment update request containing the\n * transaction ID and a callback to mutate fiat payment state.\n */\n updateFiatPayment(request: UpdateFiatPaymentRequest): void {\n updateFiatPayment(request, {\n messenger: this.messenger,\n updateTransactionData: this.#updateTransactionData.bind(this),\n });\n }\n\n /**\n * Gets the delegation transaction for a given transaction.\n *\n * Converts the provided transaction into a redeem delegation by delegating\n * to the configured callback. Returns the delegation transaction data\n * including the encoded call data, target address, value, and an optional\n * authorization list.\n *\n * @param args - The arguments forwarded to the {@link GetDelegationTransactionCallback},\n * containing the transaction metadata.\n * @returns A promise resolving to the delegation transaction data.\n */\n getDelegationTransaction(\n ...args: Parameters<GetDelegationTransactionCallback>\n ): ReturnType<GetDelegationTransactionCallback> {\n return this.#getDelegationTransaction(...args);\n }\n\n /**\n * Gets the preferred strategy for a transaction.\n *\n * Returns the first strategy from the ordered list of strategies applicable\n * to the given transaction. Falls back to the default strategy order derived\n * from feature flags when no custom strategy callback is configured.\n *\n * @param transaction - The transaction metadata to determine the strategy for.\n * @returns The preferred {@link TransactionPayStrategy} for the transaction.\n */\n getStrategy(transaction: TransactionMeta): TransactionPayStrategy {\n return this.#getStrategiesWithFallback(transaction)[0];\n }\n\n #removeTransactionData(transactionId: string): void {\n this.update((state) => {\n delete state.transactionData[transactionId];\n });\n }\n\n #updateTransactionData(\n transactionId: string,\n fn: (transactionData: Draft<TransactionData>) => void,\n ): void {\n let shouldUpdateQuotes = false;\n\n this.update((state) => {\n const { transactionData } = state;\n let current = transactionData[transactionId];\n const originalPaymentToken = current?.paymentToken;\n const originalTokens = current?.tokens;\n const originalIsMaxAmount = current?.isMaxAmount;\n const originalIsPostQuote = current?.isPostQuote;\n const originalAccountOverride = current?.accountOverride;\n\n if (!current) {\n transactionData[transactionId] = {\n fiatPayment: {},\n isLoading: false,\n tokens: [],\n };\n\n current = transactionData[transactionId];\n }\n\n fn(current);\n\n const isPaymentTokenUpdated =\n current.paymentToken?.address?.toLowerCase() !==\n originalPaymentToken?.address?.toLowerCase() ||\n current.paymentToken?.chainId !== originalPaymentToken?.chainId;\n\n const isTokensUpdated = current.tokens !== originalTokens;\n const isIsMaxUpdated = current.isMaxAmount !== originalIsMaxAmount;\n const isPostQuoteUpdated = current.isPostQuote !== originalIsPostQuote;\n const isAccountOverrideUpdated =\n current.accountOverride !== originalAccountOverride;\n\n if (\n isPaymentTokenUpdated ||\n isIsMaxUpdated ||\n isTokensUpdated ||\n isPostQuoteUpdated ||\n isAccountOverrideUpdated\n ) {\n updateSourceAmounts(transactionId, current as never, this.messenger);\n\n shouldUpdateQuotes = true;\n }\n });\n\n if (shouldUpdateQuotes) {\n updateQuotes({\n getStrategies: this.#getStrategiesWithFallback.bind(this),\n messenger: this.messenger,\n transactionData: this.state.transactionData[transactionId],\n transactionId,\n updateTransactionData: this.#updateTransactionData.bind(this),\n }).catch(noop);\n }\n }\n\n #getStrategiesWithFallback(\n transaction: TransactionMeta,\n ): TransactionPayStrategy[] {\n const strategyCandidates: unknown[] =\n this.#getStrategies?.(transaction) ??\n (this.#getStrategy ? [this.#getStrategy(transaction)] : []);\n\n const validStrategies = strategyCandidates.filter(\n (strategy): strategy is TransactionPayStrategy =>\n isTransactionPayStrategy(strategy),\n );\n\n if (validStrategies.length) {\n return validStrategies;\n }\n\n const paymentToken =\n this.state.transactionData[transaction.id]?.paymentToken;\n\n return getStrategyOrder(\n this.messenger,\n paymentToken?.chainId,\n paymentToken?.address,\n transaction.type,\n );\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"TransactionPayController.mjs","sourceRoot":"","sources":["../src/TransactionPayController.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,OAAO,EAAE,cAAc,EAAE,kCAAkC;;;AAK3D,OAAO,EAAE,iBAAiB,EAAE,0CAAsC;AAClE,OAAO,EAAE,kBAAkB,EAAE,2CAAuC;AACpE,OAAO,EACL,eAAe,EACf,wBAAwB,EACxB,sBAAsB,EACvB,wBAAoB;AACrB,OAAO,EAAE,cAAc,EAAE,qCAAiC;AAC1D,OAAO,EAAE,6BAA6B,EAAE,kCAA8B;AAWtE,OAAO,EAAE,gBAAgB,EAAE,kCAA8B;AACzD,OAAO,EAAE,YAAY,EAAE,2BAAuB;AAC9C,OAAO,EAAE,mBAAmB,EAAE,mCAA+B;AAC7D,OAAO,EAAE,cAAc,EAAE,sBAAsB,EAAE,gCAA4B;AAE7E,MAAM,yBAAyB,GAAG;IAChC,0BAA0B;IAC1B,aAAa;IACb,sBAAsB;IACtB,mBAAmB;IACnB,oBAAoB;CACZ,CAAC;AAEX,MAAM,aAAa,GAAiD;IAClE,eAAe,EAAE;QACf,sBAAsB,EAAE,KAAK;QAC7B,kBAAkB,EAAE,IAAI;QACxB,OAAO,EAAE,KAAK;QACd,QAAQ,EAAE,IAAI;KACf;CACF,CAAC;AAEF,MAAM,eAAe,GAAG,GAAkC,EAAE,CAAC,CAAC;IAC5D,eAAe,EAAE,EAAE;CACpB,CAAC,CAAC;AAEH,MAAM,OAAO,wBAAyB,SAAQ,cAI7C;IAWC,YAAY,EACV,wBAAwB,EACxB,WAAW,EACX,aAAa,EACb,SAAS,EACT,KAAK,GAC2B;QAChC,KAAK,CAAC;YACJ,IAAI,EAAE,eAAe;YACrB,QAAQ,EAAE,aAAa;YACvB,SAAS;Y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IAAI,CAAC,SAAS;YACzB,eAAe,EAAE,IAAI,CAAC,KAAK,CAAC,eAAe,CAAC,aAAa,CAAC;YAC1D,aAAa;YACb,qBAAqB,EAAE,uBAAA,IAAI,4FAAuB,CAAC,IAAI,CAAC,IAAI,CAAC;SAC9D,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IACjB,CAAC;AACH,CAAC,qHAGC,WAA4B;IAE5B,MAAM,kBAAkB,GACtB,uBAAA,IAAI,+CAAe,EAAE,KAArB,IAAI,EAAkB,WAAW,CAAC;QAClC,CAAC,uBAAA,IAAI,6CAAa,CAAC,CAAC,CAAC,CAAC,uBAAA,IAAI,6CAAa,MAAjB,IAAI,EAAc,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;IAE9D,MAAM,eAAe,GAAG,kBAAkB,CAAC,MAAM,CAC/C,CAAC,QAAQ,EAAsC,EAAE,CAC/C,wBAAwB,CAAC,QAAQ,CAAC,CACrC,CAAC;IAEF,IAAI,eAAe,CAAC,MAAM,EAAE,CAAC;QAC3B,OAAO,eAAe,CAAC;IACzB,CAAC;IAED,MAAM,YAAY,GAChB,IAAI,CAAC,KAAK,CAAC,eAAe,CAAC,WAAW,CAAC,EAAE,CAAC,EAAE,YAAY,CAAC;IAE3D,OAAO,gBAAgB,CACrB,IAAI,CAAC,SAAS,EACd,YAAY,EAAE,OAAO,EACrB,YAAY,EAAE,OAAO,EACrB,WAAW,CAAC,IAAI,CACjB,CAAC;AACJ,CAAC","sourcesContent":["import type { StateMetadata } from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport type { TransactionMeta } from '@metamask/transaction-controller';\nimport type { Draft } from 'immer';\nimport { noop } from 'lodash';\n\nimport { updateFiatPayment } from './actions/update-fiat-payment';\nimport { updatePaymentToken } from './actions/update-payment-token';\nimport {\n CONTROLLER_NAME,\n isTransactionPayStrategy,\n TransactionPayStrategy,\n} from './constants';\nimport { QuoteRefresher } from './helpers/QuoteRefresher';\nimport { deriveFiatAssetForFiatPayment } from './strategy/fiat/utils';\nimport type {\n GetDelegationTransactionCallback,\n TransactionConfigCallback,\n TransactionData,\n TransactionPayControllerMessenger,\n TransactionPayControllerOptions,\n TransactionPayControllerState,\n UpdateFiatPaymentRequest,\n UpdatePaymentTokenRequest,\n} from './types';\nimport { getStrategyOrder } from './utils/feature-flags';\nimport { updateQuotes } from './utils/quotes';\nimport { updateSourceAmounts } from './utils/source-amounts';\nimport { getTransaction, pollTransactionChanges } from './utils/transaction';\n\nconst MESSENGER_EXPOSED_METHODS = [\n 'getDelegationTransaction',\n 'getStrategy',\n 'setTransactionConfig',\n 'updateFiatPayment',\n 'updatePaymentToken',\n] as const;\n\nconst stateMetadata: StateMetadata<TransactionPayControllerState> = {\n transactionData: {\n includeInDebugSnapshot: false,\n includeInStateLogs: true,\n persist: false,\n usedInUi: true,\n },\n};\n\nconst getDefaultState = (): TransactionPayControllerState => ({\n transactionData: {},\n});\n\nexport class TransactionPayController extends BaseController<\n typeof CONTROLLER_NAME,\n TransactionPayControllerState,\n TransactionPayControllerMessenger\n> {\n readonly #getDelegationTransaction: GetDelegationTransactionCallback;\n\n readonly #getStrategy?: (\n transaction: TransactionMeta,\n ) => TransactionPayStrategy;\n\n readonly #getStrategies?: (\n transaction: TransactionMeta,\n ) => TransactionPayStrategy[];\n\n constructor({\n getDelegationTransaction,\n getStrategy,\n getStrategies,\n messenger,\n state,\n }: TransactionPayControllerOptions) {\n super({\n name: CONTROLLER_NAME,\n metadata: stateMetadata,\n messenger,\n state: { ...getDefaultState(), ...state },\n });\n\n this.#getDelegationTransaction = getDelegationTransaction;\n this.#getStrategy = getStrategy;\n this.#getStrategies = getStrategies;\n\n this.messenger.registerMethodActionHandlers(\n this,\n MESSENGER_EXPOSED_METHODS,\n );\n\n pollTransactionChanges(\n messenger,\n this.#updateTransactionData.bind(this),\n this.#removeTransactionData.bind(this),\n );\n\n // eslint-disable-next-line no-new\n new QuoteRefresher({\n getStrategies: this.#getStrategiesWithFallback.bind(this),\n messenger,\n updateTransactionData: this.#updateTransactionData.bind(this),\n });\n }\n\n /**\n * Sets the transaction configuration.\n *\n * The callback receives the current configuration properties and can mutate\n * them in place. Updated values are written back to the transaction data.\n *\n * @param transactionId - The ID of the transaction to configure.\n * @param callback - A callback that receives a mutable {@link TransactionConfig} object.\n */\n setTransactionConfig(\n transactionId: string,\n callback: TransactionConfigCallback,\n ): void {\n this.#updateTransactionData(transactionId, (transactionData) => {\n const config = {\n isMaxAmount: transactionData.isMaxAmount,\n isPostQuote: transactionData.isPostQuote,\n isHyperliquidSource: transactionData.isHyperliquidSource,\n refundTo: transactionData.refundTo,\n accountOverride: transactionData.accountOverride,\n };\n\n const previousAccountOverride = config.accountOverride;\n\n callback(config);\n\n transactionData.accountOverride = config.accountOverride;\n transactionData.isMaxAmount = config.isMaxAmount;\n transactionData.isPostQuote = config.isPostQuote;\n transactionData.isHyperliquidSource = config.isHyperliquidSource;\n transactionData.refundTo = config.refundTo;\n\n if (config.accountOverride !== previousAccountOverride) {\n transactionData.paymentToken = undefined;\n }\n });\n }\n\n /**\n * Updates the payment token for a transaction.\n *\n * Resolves token metadata and balances, then stores the new payment token\n * in the transaction data. This triggers recalculation of source amounts\n * and quote retrieval.\n *\n * @param request - The payment token update request containing the\n * transaction ID, token address, and chain ID.\n */\n updatePaymentToken(request: UpdatePaymentTokenRequest): void {\n updatePaymentToken(request, {\n messenger: this.messenger,\n updateTransactionData: this.#updateTransactionData.bind(this),\n });\n }\n\n /**\n * Updates the fiat payment state for a transaction.\n *\n * The request callback receives the current fiat payment state and can\n * mutate it to update properties such as the selected payment method or\n * fiat amount.\n *\n * @param request - The fiat payment update request containing the\n * transaction ID and a callback to mutate fiat payment state.\n */\n updateFiatPayment(request: UpdateFiatPaymentRequest): void {\n updateFiatPayment(request, {\n messenger: this.messenger,\n updateTransactionData: this.#updateTransactionData.bind(this),\n });\n }\n\n /**\n * Gets the delegation transaction for a given transaction.\n *\n * Converts the provided transaction into a redeem delegation by delegating\n * to the configured callback. Returns the delegation transaction data\n * including the encoded call data, target address, value, and an optional\n * authorization list.\n *\n * @param args - The arguments forwarded to the {@link GetDelegationTransactionCallback},\n * containing the transaction metadata.\n * @returns A promise resolving to the delegation transaction data.\n */\n getDelegationTransaction(\n ...args: Parameters<GetDelegationTransactionCallback>\n ): ReturnType<GetDelegationTransactionCallback> {\n return this.#getDelegationTransaction(...args);\n }\n\n /**\n * Gets the preferred strategy for a transaction.\n *\n * Returns the first strategy from the ordered list of strategies applicable\n * to the given transaction. Falls back to the default strategy order derived\n * from feature flags when no custom strategy callback is configured.\n *\n * @param transaction - The transaction metadata to determine the strategy for.\n * @returns The preferred {@link TransactionPayStrategy} for the transaction.\n */\n getStrategy(transaction: TransactionMeta): TransactionPayStrategy {\n return this.#getStrategiesWithFallback(transaction)[0];\n }\n\n #removeTransactionData(transactionId: string): void {\n this.update((state) => {\n delete state.transactionData[transactionId];\n });\n }\n\n #updateTransactionData(\n transactionId: string,\n fn: (transactionData: Draft<TransactionData>) => void,\n ): void {\n let shouldUpdateQuotes = false;\n let shouldUpdateFiatToken = false;\n\n this.update((state) => {\n const { transactionData } = state;\n let current = transactionData[transactionId];\n const originalPaymentToken = current?.paymentToken;\n const originalTokens = current?.tokens;\n const originalIsMaxAmount = current?.isMaxAmount;\n const originalIsPostQuote = current?.isPostQuote;\n const originalAccountOverride = current?.accountOverride;\n const originalFiatPaymentAmount = current?.fiatPayment?.amountFiat;\n const originalFiatPaymentMethodId =\n current?.fiatPayment?.selectedPaymentMethodId;\n\n if (!current) {\n transactionData[transactionId] = {\n fiatPayment: {},\n isLoading: false,\n tokens: [],\n };\n\n current = transactionData[transactionId];\n }\n\n fn(current);\n\n const isPaymentTokenUpdated =\n current.paymentToken?.address?.toLowerCase() !==\n originalPaymentToken?.address?.toLowerCase() ||\n current.paymentToken?.chainId !== originalPaymentToken?.chainId;\n\n const isTokensUpdated = current.tokens !== originalTokens;\n const isIsMaxUpdated = current.isMaxAmount !== originalIsMaxAmount;\n const isPostQuoteUpdated = current.isPostQuote !== originalIsPostQuote;\n const isAccountOverrideUpdated =\n current.accountOverride !== originalAccountOverride;\n const isFiatAmountUpdated =\n current.fiatPayment?.amountFiat !== originalFiatPaymentAmount;\n const isFiatPaymentMethodUpdated =\n current.fiatPayment?.selectedPaymentMethodId !==\n originalFiatPaymentMethodId;\n\n if (\n isPaymentTokenUpdated ||\n isIsMaxUpdated ||\n isTokensUpdated ||\n isPostQuoteUpdated ||\n isAccountOverrideUpdated\n ) {\n updateSourceAmounts(transactionId, current as never, this.messenger);\n\n shouldUpdateQuotes = true;\n }\n\n if (isFiatAmountUpdated || isFiatPaymentMethodUpdated) {\n shouldUpdateQuotes = true;\n }\n\n if (isFiatPaymentMethodUpdated) {\n shouldUpdateFiatToken = true;\n }\n });\n\n if (shouldUpdateFiatToken) {\n const transaction = getTransaction(\n transactionId,\n this.messenger,\n ) as TransactionMeta;\n const fiatAsset = deriveFiatAssetForFiatPayment(transaction);\n if (fiatAsset) {\n try {\n this.messenger.call(\n 'RampsController:setSelectedToken',\n fiatAsset.caipAssetId,\n );\n } catch {\n // Intentionally no-op — tokens may not be loaded in RampsController yet.\n }\n }\n }\n\n if (shouldUpdateQuotes) {\n updateQuotes({\n getStrategies: this.#getStrategiesWithFallback.bind(this),\n messenger: this.messenger,\n transactionData: this.state.transactionData[transactionId],\n transactionId,\n updateTransactionData: this.#updateTransactionData.bind(this),\n }).catch(noop);\n }\n }\n\n #getStrategiesWithFallback(\n transaction: TransactionMeta,\n ): TransactionPayStrategy[] {\n const strategyCandidates: unknown[] =\n this.#getStrategies?.(transaction) ??\n (this.#getStrategy ? [this.#getStrategy(transaction)] : []);\n\n const validStrategies = strategyCandidates.filter(\n (strategy): strategy is TransactionPayStrategy =>\n isTransactionPayStrategy(strategy),\n );\n\n if (validStrategies.length) {\n return validStrategies;\n }\n\n const paymentToken =\n this.state.transactionData[transaction.id]?.paymentToken;\n\n return getStrategyOrder(\n this.messenger,\n paymentToken?.chainId,\n paymentToken?.address,\n transaction.type,\n );\n }\n}\n"]}
|
|
@@ -1,14 +1,273 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.submitFiatQuotes = void 0;
|
|
4
|
+
const ramps_controller_1 = require("@metamask/ramps-controller");
|
|
5
|
+
const utils_1 = require("@metamask/utils");
|
|
6
|
+
const bignumber_js_1 = require("bignumber.js");
|
|
7
|
+
const logger_1 = require("../../logger.cjs");
|
|
8
|
+
const relay_quotes_1 = require("../relay/relay-quotes.cjs");
|
|
9
|
+
const relay_submit_1 = require("../relay/relay-submit.cjs");
|
|
10
|
+
const utils_2 = require("./utils.cjs");
|
|
11
|
+
const log = (0, utils_1.createModuleLogger)(logger_1.projectLogger, 'fiat-submit');
|
|
12
|
+
const ORDER_POLL_INTERVAL_MS = 1000;
|
|
13
|
+
const ORDER_POLL_TIMEOUT_MS = 10 * 60 * 1000;
|
|
14
|
+
const MAX_SLIPPAGE_PERCENT = 5;
|
|
15
|
+
const TERMINAL_FAILURE_STATUSES = [
|
|
16
|
+
ramps_controller_1.RampsOrderStatus.Cancelled,
|
|
17
|
+
ramps_controller_1.RampsOrderStatus.Failed,
|
|
18
|
+
ramps_controller_1.RampsOrderStatus.IdExpired,
|
|
19
|
+
];
|
|
4
20
|
/**
|
|
5
|
-
*
|
|
21
|
+
* Submits fiat strategy quotes by polling the on-ramp order until completion,
|
|
22
|
+
* then re-quoting and submitting the relay leg with the settled crypto amount.
|
|
6
23
|
*
|
|
7
|
-
* @param
|
|
8
|
-
* @
|
|
24
|
+
* @param request - Strategy execute request containing fiat quotes, messenger, and transaction metadata.
|
|
25
|
+
* @param request.messenger - Controller messenger for cross-controller calls.
|
|
26
|
+
* @param request.quotes - Fiat quotes to execute (exactly one expected).
|
|
27
|
+
* @param request.transaction - Original transaction metadata.
|
|
28
|
+
* @param request.isSmartTransaction - Callback to check smart transaction eligibility.
|
|
29
|
+
* @returns An object containing the relay transaction hash if available.
|
|
9
30
|
*/
|
|
10
|
-
async function submitFiatQuotes(
|
|
11
|
-
|
|
31
|
+
async function submitFiatQuotes(request) {
|
|
32
|
+
const { messenger, transaction } = request;
|
|
33
|
+
const transactionId = transaction.id;
|
|
34
|
+
const walletAddress = transaction.txParams.from;
|
|
35
|
+
if (!walletAddress) {
|
|
36
|
+
throw new Error('Missing wallet address for fiat submission');
|
|
37
|
+
}
|
|
38
|
+
const state = messenger.call('TransactionPayController:getState');
|
|
39
|
+
const orderId = state.transactionData[transactionId]?.fiatPayment?.orderId;
|
|
40
|
+
if (!orderId) {
|
|
41
|
+
throw new Error('Missing order ID for fiat submission');
|
|
42
|
+
}
|
|
43
|
+
const parsedOrder = parseOrderId(orderId);
|
|
44
|
+
if (!parsedOrder) {
|
|
45
|
+
throw new Error(`Invalid order ID format: ${orderId}`);
|
|
46
|
+
}
|
|
47
|
+
log('Starting fiat order polling', {
|
|
48
|
+
orderId,
|
|
49
|
+
providerCode: parsedOrder.providerCode,
|
|
50
|
+
transactionId,
|
|
51
|
+
});
|
|
52
|
+
const order = await waitForOrderCompletion({
|
|
53
|
+
messenger,
|
|
54
|
+
orderCode: parsedOrder.orderCode,
|
|
55
|
+
providerCode: parsedOrder.providerCode,
|
|
56
|
+
transactionId,
|
|
57
|
+
walletAddress,
|
|
58
|
+
});
|
|
59
|
+
log('Fiat order completed', {
|
|
60
|
+
cryptoAmount: order.cryptoAmount,
|
|
61
|
+
orderId,
|
|
62
|
+
transactionId,
|
|
63
|
+
});
|
|
64
|
+
return await submitRelayAfterFiatCompletion({ order, request });
|
|
12
65
|
}
|
|
13
66
|
exports.submitFiatQuotes = submitFiatQuotes;
|
|
67
|
+
/**
|
|
68
|
+
* Parses a normalized order ID string into its provider and order components.
|
|
69
|
+
*
|
|
70
|
+
* @param orderId - Order ID in `/providers/{providerCode}/orders/{orderCode}` format.
|
|
71
|
+
* @returns The parsed provider and order codes, or `null` if the format is invalid.
|
|
72
|
+
*/
|
|
73
|
+
function parseOrderId(orderId) {
|
|
74
|
+
const parts = orderId.split('/').filter(Boolean);
|
|
75
|
+
if (parts.length < 4 || parts[0] !== 'providers' || parts[2] !== 'orders') {
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
return { orderCode: parts[3], providerCode: parts[1] };
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Converts the order's human-readable crypto amount to a raw token amount.
|
|
82
|
+
*
|
|
83
|
+
* @param options - The conversion options.
|
|
84
|
+
* @param options.cryptoAmount - Human-readable crypto amount from the completed order.
|
|
85
|
+
* @param options.decimals - Token decimals for the fiat asset.
|
|
86
|
+
* @returns The raw token amount as a string.
|
|
87
|
+
*/
|
|
88
|
+
function getRawSourceAmountFromOrder({ cryptoAmount, decimals, }) {
|
|
89
|
+
const normalizedAmount = new bignumber_js_1.BigNumber(String(cryptoAmount));
|
|
90
|
+
if (!normalizedAmount.isFinite() || normalizedAmount.lte(0)) {
|
|
91
|
+
throw new Error(`Invalid fiat order crypto amount: ${String(cryptoAmount)}`);
|
|
92
|
+
}
|
|
93
|
+
const rawAmount = normalizedAmount
|
|
94
|
+
.shiftedBy(decimals)
|
|
95
|
+
.decimalPlaces(0, bignumber_js_1.BigNumber.ROUND_DOWN)
|
|
96
|
+
.toFixed(0);
|
|
97
|
+
if (!new bignumber_js_1.BigNumber(rawAmount).gt(0)) {
|
|
98
|
+
throw new Error('Computed fiat order source amount is not positive');
|
|
99
|
+
}
|
|
100
|
+
return rawAmount;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Validates that the completed order's crypto asset matches the expected fiat asset.
|
|
104
|
+
*
|
|
105
|
+
* @param options - The validation options.
|
|
106
|
+
* @param options.expectedAsset - The expected fiat asset derived from the transaction type.
|
|
107
|
+
* @param options.orderCrypto - The crypto currency information from the completed order.
|
|
108
|
+
* @param options.transactionId - Transaction ID for error reporting.
|
|
109
|
+
*/
|
|
110
|
+
function validateOrderAsset({ expectedAsset, orderCrypto, transactionId, }) {
|
|
111
|
+
const orderAssetId = orderCrypto?.assetId?.toLowerCase();
|
|
112
|
+
const expectedAssetId = expectedAsset.caipAssetId.toLowerCase();
|
|
113
|
+
const expectedChainId = expectedAssetId.split('/')[0];
|
|
114
|
+
const orderChainId = orderCrypto?.chainId?.toLowerCase();
|
|
115
|
+
if (orderAssetId && orderAssetId !== expectedAssetId) {
|
|
116
|
+
throw new Error(`Fiat order asset mismatch for transaction ${transactionId}: ` +
|
|
117
|
+
`expected ${expectedAssetId}, got ${orderAssetId}`);
|
|
118
|
+
}
|
|
119
|
+
if (orderChainId && orderChainId !== expectedChainId) {
|
|
120
|
+
throw new Error(`Fiat order chain mismatch for transaction ${transactionId}: ` +
|
|
121
|
+
`expected ${expectedChainId}, got ${orderChainId}`);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Validates that the re-quoted relay target output hasn't drifted beyond the
|
|
126
|
+
* acceptable slippage threshold compared to the original quote shown to the user.
|
|
127
|
+
*
|
|
128
|
+
* @param options - The validation options.
|
|
129
|
+
* @param options.originalTargetRaw - Raw target amount from the original relay quote.
|
|
130
|
+
* @param options.reQuotedTargetRaw - Raw target amount from the re-quoted relay.
|
|
131
|
+
* @param options.transactionId - Transaction ID for error reporting.
|
|
132
|
+
*/
|
|
133
|
+
function validateRelaySlippage({ originalTargetRaw, reQuotedTargetRaw, transactionId, }) {
|
|
134
|
+
const original = new bignumber_js_1.BigNumber(originalTargetRaw);
|
|
135
|
+
const reQuoted = new bignumber_js_1.BigNumber(reQuotedTargetRaw);
|
|
136
|
+
if (!original.gt(0) || !reQuoted.gt(0)) {
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
const slippagePercent = original
|
|
140
|
+
.minus(reQuoted)
|
|
141
|
+
.dividedBy(original)
|
|
142
|
+
.multipliedBy(100);
|
|
143
|
+
log('Relay slippage check', {
|
|
144
|
+
originalTargetRaw,
|
|
145
|
+
reQuotedTargetRaw,
|
|
146
|
+
slippagePercent: slippagePercent.toFixed(2),
|
|
147
|
+
transactionId,
|
|
148
|
+
});
|
|
149
|
+
if (slippagePercent.gt(MAX_SLIPPAGE_PERCENT)) {
|
|
150
|
+
throw new Error(`Relay re-quote slippage too high for transaction ` +
|
|
151
|
+
`${slippagePercent.toFixed(2)}% exceeds ${MAX_SLIPPAGE_PERCENT}% max`);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Polls the on-ramp order until it reaches a terminal status.
|
|
156
|
+
*
|
|
157
|
+
* @param options - The polling options.
|
|
158
|
+
* @param options.messenger - Controller messenger for calling `RampsController:getOrder`.
|
|
159
|
+
* @param options.orderCode - The order identifier within the provider.
|
|
160
|
+
* @param options.providerCode - The on-ramp provider code (e.g. "transak").
|
|
161
|
+
* @param options.transactionId - Transaction ID for logging.
|
|
162
|
+
* @param options.walletAddress - Wallet address associated with the order.
|
|
163
|
+
* @returns The completed order data.
|
|
164
|
+
*/
|
|
165
|
+
async function waitForOrderCompletion({ messenger, orderCode, providerCode, transactionId, walletAddress, }) {
|
|
166
|
+
const startTime = Date.now();
|
|
167
|
+
let lastStatus;
|
|
168
|
+
while (true) {
|
|
169
|
+
let order;
|
|
170
|
+
try {
|
|
171
|
+
order = await messenger.call('RampsController:getOrder', providerCode, orderCode, walletAddress);
|
|
172
|
+
}
|
|
173
|
+
catch (error) {
|
|
174
|
+
log('Order polling network error', error);
|
|
175
|
+
}
|
|
176
|
+
if (order) {
|
|
177
|
+
lastStatus = order.status;
|
|
178
|
+
log('Polled fiat order', {
|
|
179
|
+
orderStatus: order.status,
|
|
180
|
+
providerCode,
|
|
181
|
+
transactionId,
|
|
182
|
+
});
|
|
183
|
+
if (order.status === ramps_controller_1.RampsOrderStatus.Completed) {
|
|
184
|
+
return order;
|
|
185
|
+
}
|
|
186
|
+
if (TERMINAL_FAILURE_STATUSES.includes(order.status)) {
|
|
187
|
+
throw new Error(`Fiat order ${order.status.toLowerCase()}`);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
if (Date.now() - startTime >= ORDER_POLL_TIMEOUT_MS) {
|
|
191
|
+
throw new Error(`Fiat order polling timed out (last status: ${lastStatus})`);
|
|
192
|
+
}
|
|
193
|
+
await new Promise((resolve) => setTimeout(resolve, ORDER_POLL_INTERVAL_MS));
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Re-quotes and submits the relay leg using the settled amount from a completed fiat order.
|
|
198
|
+
*
|
|
199
|
+
* @param options - The submission options.
|
|
200
|
+
* @param options.order - The completed on-ramp order containing the settled crypto amount.
|
|
201
|
+
* @param options.request - The original fiat strategy execute request.
|
|
202
|
+
* @returns An object containing the relay transaction hash if available.
|
|
203
|
+
*/
|
|
204
|
+
async function submitRelayAfterFiatCompletion({ order, request, }) {
|
|
205
|
+
const { messenger, quotes, transaction } = request;
|
|
206
|
+
const transactionId = transaction.id;
|
|
207
|
+
if (!quotes.length) {
|
|
208
|
+
throw new Error('Missing fiat quote for relay submission');
|
|
209
|
+
}
|
|
210
|
+
if (quotes.length > 1) {
|
|
211
|
+
throw new Error('Multiple fiat quotes are not supported for submission');
|
|
212
|
+
}
|
|
213
|
+
const fiatAsset = (0, utils_2.deriveFiatAssetForFiatPayment)(transaction);
|
|
214
|
+
if (!fiatAsset) {
|
|
215
|
+
throw new Error(`Missing fiat asset mapping for transaction type: ${String(transaction.type)}`);
|
|
216
|
+
}
|
|
217
|
+
validateOrderAsset({
|
|
218
|
+
expectedAsset: fiatAsset,
|
|
219
|
+
orderCrypto: order.cryptoCurrency,
|
|
220
|
+
transactionId,
|
|
221
|
+
});
|
|
222
|
+
const sourceAmountRaw = getRawSourceAmountFromOrder({
|
|
223
|
+
cryptoAmount: order.cryptoAmount,
|
|
224
|
+
decimals: fiatAsset.decimals,
|
|
225
|
+
});
|
|
226
|
+
const baseRequest = quotes[0].request;
|
|
227
|
+
const relayRequest = {
|
|
228
|
+
...baseRequest,
|
|
229
|
+
isMaxAmount: true,
|
|
230
|
+
isPostQuote: false,
|
|
231
|
+
sourceBalanceRaw: sourceAmountRaw,
|
|
232
|
+
sourceTokenAmount: sourceAmountRaw,
|
|
233
|
+
};
|
|
234
|
+
log('Re-quoting relay from completed fiat order', {
|
|
235
|
+
completedOrderAmount: order.cryptoAmount,
|
|
236
|
+
relayRequest,
|
|
237
|
+
sourceAmountRaw,
|
|
238
|
+
transactionId,
|
|
239
|
+
});
|
|
240
|
+
const relayQuotes = await (0, relay_quotes_1.getRelayQuotes)({
|
|
241
|
+
accountSupports7702: request.accountSupports7702,
|
|
242
|
+
messenger,
|
|
243
|
+
requests: [relayRequest],
|
|
244
|
+
transaction,
|
|
245
|
+
});
|
|
246
|
+
if (!relayQuotes.length) {
|
|
247
|
+
throw new Error('No relay quotes returned for completed fiat order');
|
|
248
|
+
}
|
|
249
|
+
const originalRelayQuote = quotes[0].original.relayQuote;
|
|
250
|
+
validateRelaySlippage({
|
|
251
|
+
originalTargetRaw: originalRelayQuote.details.currencyOut.amount,
|
|
252
|
+
reQuotedTargetRaw: relayQuotes[0].original.details.currencyOut.amount,
|
|
253
|
+
transactionId,
|
|
254
|
+
});
|
|
255
|
+
log('Received relay quotes for completed fiat order', {
|
|
256
|
+
relayQuoteCount: relayQuotes.length,
|
|
257
|
+
transactionId,
|
|
258
|
+
});
|
|
259
|
+
const relaySubmitRequest = {
|
|
260
|
+
accountSupports7702: request.accountSupports7702,
|
|
261
|
+
isSmartTransaction: request.isSmartTransaction,
|
|
262
|
+
messenger,
|
|
263
|
+
quotes: relayQuotes,
|
|
264
|
+
transaction,
|
|
265
|
+
};
|
|
266
|
+
const relayResult = await (0, relay_submit_1.submitRelayQuotes)(relaySubmitRequest);
|
|
267
|
+
log('Relay submission completed after fiat order', {
|
|
268
|
+
relayResult,
|
|
269
|
+
transactionId,
|
|
270
|
+
});
|
|
271
|
+
return relayResult;
|
|
272
|
+
}
|
|
14
273
|
//# sourceMappingURL=fiat-submit.cjs.map
|