@metamask-previews/transaction-pay-controller 23.4.0-preview-7bc7196a6 → 23.5.0-preview-ddb114f71

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 CHANGED
@@ -7,6 +7,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ### Changed
11
+
12
+ - Make fiat-eligible transaction types remotely configurable via `confirmations_pay_fiat.enabledTransactionTypes` feature flag ([#9050](https://github.com/MetaMask/core/pull/9050))
13
+
14
+ ## [23.5.0]
15
+
16
+ ### Changed
17
+
18
+ - Bump `@metamask/bridge-controller` from `^75.0.0` to `^75.1.0` ([#9072](https://github.com/MetaMask/core/pull/9072))
19
+
20
+ ### Fixed
21
+
22
+ - Fix request amount used for cases when paymentOverride is defined and also isPostQuote is true ([#9070](https://github.com/MetaMask/core/pull/9070))
23
+
10
24
  ## [23.4.0]
11
25
 
12
26
  ### Changed
@@ -1021,7 +1035,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1021
1035
 
1022
1036
  - Initial release ([#6820](https://github.com/MetaMask/core/pull/6820))
1023
1037
 
1024
- [Unreleased]: https://github.com/MetaMask/core/compare/@metamask/transaction-pay-controller@23.4.0...HEAD
1038
+ [Unreleased]: https://github.com/MetaMask/core/compare/@metamask/transaction-pay-controller@23.5.0...HEAD
1039
+ [23.5.0]: https://github.com/MetaMask/core/compare/@metamask/transaction-pay-controller@23.4.0...@metamask/transaction-pay-controller@23.5.0
1025
1040
  [23.4.0]: https://github.com/MetaMask/core/compare/@metamask/transaction-pay-controller@23.3.1...@metamask/transaction-pay-controller@23.4.0
1026
1041
  [23.3.1]: https://github.com/MetaMask/core/compare/@metamask/transaction-pay-controller@23.3.0...@metamask/transaction-pay-controller@23.3.1
1027
1042
  [23.3.0]: https://github.com/MetaMask/core/compare/@metamask/transaction-pay-controller@23.2.0...@metamask/transaction-pay-controller@23.3.0
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.FIAT_ASSET_ID_BY_TX_TYPE = exports.ETH_MAINNET_FIAT_ASSET = exports.DEFAULT_FIAT_CURRENCY = void 0;
3
+ exports.FIAT_ENABLED_TYPES = exports.FIAT_ASSET_ID_BY_TX_TYPE = exports.ETH_MAINNET_FIAT_ASSET = exports.DEFAULT_FIAT_CURRENCY = void 0;
4
4
  const transaction_controller_1 = require("@metamask/transaction-controller");
5
5
  const constants_1 = require("../../constants.cjs");
6
6
  exports.DEFAULT_FIAT_CURRENCY = 'USD';
@@ -17,8 +17,13 @@ exports.ETH_MAINNET_FIAT_ASSET = {
17
17
  chainId: constants_1.CHAIN_ID_MAINNET,
18
18
  };
19
19
  exports.FIAT_ASSET_ID_BY_TX_TYPE = {
20
- [transaction_controller_1.TransactionType.predictDeposit]: POLYGON_POL_FIAT_ASSET,
20
+ [transaction_controller_1.TransactionType.moneyAccountDeposit]: exports.ETH_MAINNET_FIAT_ASSET,
21
21
  [transaction_controller_1.TransactionType.perpsDeposit]: ARBITRUM_ETH_FIAT_ASSET,
22
- [transaction_controller_1.TransactionType.perpsDepositAndOrder]: ARBITRUM_ETH_FIAT_ASSET,
22
+ [transaction_controller_1.TransactionType.predictDeposit]: POLYGON_POL_FIAT_ASSET,
23
23
  };
24
+ exports.FIAT_ENABLED_TYPES = [
25
+ transaction_controller_1.TransactionType.moneyAccountDeposit,
26
+ transaction_controller_1.TransactionType.perpsDeposit,
27
+ transaction_controller_1.TransactionType.predictDeposit,
28
+ ];
24
29
  //# sourceMappingURL=constants.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"constants.cjs","sourceRoot":"","sources":["../../../src/strategy/fiat/constants.ts"],"names":[],"mappings":";;;AAAA,6EAAmE;AAGnE,mDAKyB;AAEZ,QAAA,qBAAqB,GAAG,KAAK,CAAC;AAO3C,MAAM,sBAAsB,GAA4B;IACtD,OAAO,EAAE,4CAA4C;IACrD,OAAO,EAAE,4BAAgB;CAC1B,CAAC;AAEF,MAAM,uBAAuB,GAA4B;IACvD,OAAO,EAAE,gCAAoB;IAC7B,OAAO,EAAE,6BAAiB;CAC3B,CAAC;AAEW,QAAA,sBAAsB,GAA4B;IAC7D,OAAO,EAAE,gCAAoB;IAC7B,OAAO,EAAE,4BAAgB;CAC1B,CAAC;AAEW,QAAA,wBAAwB,GAEjC;IACF,CAAC,wCAAe,CAAC,cAAc,CAAC,EAAE,sBAAsB;IACxD,CAAC,wCAAe,CAAC,YAAY,CAAC,EAAE,uBAAuB;IACvD,CAAC,wCAAe,CAAC,oBAAoB,CAAC,EAAE,uBAAuB;CAChE,CAAC","sourcesContent":["import { TransactionType } from '@metamask/transaction-controller';\nimport type { Hex } from '@metamask/utils';\n\nimport {\n CHAIN_ID_ARBITRUM,\n CHAIN_ID_MAINNET,\n CHAIN_ID_POLYGON,\n NATIVE_TOKEN_ADDRESS,\n} from '../../constants';\n\nexport const DEFAULT_FIAT_CURRENCY = 'USD';\n\nexport type TransactionPayFiatAsset = {\n address: Hex;\n chainId: Hex;\n};\n\nconst POLYGON_POL_FIAT_ASSET: TransactionPayFiatAsset = {\n address: '0x0000000000000000000000000000000000001010',\n chainId: CHAIN_ID_POLYGON,\n};\n\nconst ARBITRUM_ETH_FIAT_ASSET: TransactionPayFiatAsset = {\n address: NATIVE_TOKEN_ADDRESS,\n chainId: CHAIN_ID_ARBITRUM,\n};\n\nexport const ETH_MAINNET_FIAT_ASSET: TransactionPayFiatAsset = {\n address: NATIVE_TOKEN_ADDRESS,\n chainId: CHAIN_ID_MAINNET,\n};\n\nexport const FIAT_ASSET_ID_BY_TX_TYPE: Partial<\n Record<TransactionType, TransactionPayFiatAsset>\n> = {\n [TransactionType.predictDeposit]: POLYGON_POL_FIAT_ASSET,\n [TransactionType.perpsDeposit]: ARBITRUM_ETH_FIAT_ASSET,\n [TransactionType.perpsDepositAndOrder]: ARBITRUM_ETH_FIAT_ASSET,\n};\n"]}
1
+ {"version":3,"file":"constants.cjs","sourceRoot":"","sources":["../../../src/strategy/fiat/constants.ts"],"names":[],"mappings":";;;AAAA,6EAAmE;AAGnE,mDAKyB;AAEZ,QAAA,qBAAqB,GAAG,KAAK,CAAC;AAO3C,MAAM,sBAAsB,GAA4B;IACtD,OAAO,EAAE,4CAA4C;IACrD,OAAO,EAAE,4BAAgB;CAC1B,CAAC;AAEF,MAAM,uBAAuB,GAA4B;IACvD,OAAO,EAAE,gCAAoB;IAC7B,OAAO,EAAE,6BAAiB;CAC3B,CAAC;AAEW,QAAA,sBAAsB,GAA4B;IAC7D,OAAO,EAAE,gCAAoB;IAC7B,OAAO,EAAE,4BAAgB;CAC1B,CAAC;AAEW,QAAA,wBAAwB,GAEjC;IACF,CAAC,wCAAe,CAAC,mBAAmB,CAAC,EAAE,8BAAsB;IAC7D,CAAC,wCAAe,CAAC,YAAY,CAAC,EAAE,uBAAuB;IACvD,CAAC,wCAAe,CAAC,cAAc,CAAC,EAAE,sBAAsB;CACzD,CAAC;AAEW,QAAA,kBAAkB,GAAsB;IACnD,wCAAe,CAAC,mBAAmB;IACnC,wCAAe,CAAC,YAAY;IAC5B,wCAAe,CAAC,cAAc;CAC/B,CAAC","sourcesContent":["import { TransactionType } from '@metamask/transaction-controller';\nimport type { Hex } from '@metamask/utils';\n\nimport {\n CHAIN_ID_ARBITRUM,\n CHAIN_ID_MAINNET,\n CHAIN_ID_POLYGON,\n NATIVE_TOKEN_ADDRESS,\n} from '../../constants';\n\nexport const DEFAULT_FIAT_CURRENCY = 'USD';\n\nexport type TransactionPayFiatAsset = {\n address: Hex;\n chainId: Hex;\n};\n\nconst POLYGON_POL_FIAT_ASSET: TransactionPayFiatAsset = {\n address: '0x0000000000000000000000000000000000001010',\n chainId: CHAIN_ID_POLYGON,\n};\n\nconst ARBITRUM_ETH_FIAT_ASSET: TransactionPayFiatAsset = {\n address: NATIVE_TOKEN_ADDRESS,\n chainId: CHAIN_ID_ARBITRUM,\n};\n\nexport const ETH_MAINNET_FIAT_ASSET: TransactionPayFiatAsset = {\n address: NATIVE_TOKEN_ADDRESS,\n chainId: CHAIN_ID_MAINNET,\n};\n\nexport const FIAT_ASSET_ID_BY_TX_TYPE: Partial<\n Record<TransactionType, TransactionPayFiatAsset>\n> = {\n [TransactionType.moneyAccountDeposit]: ETH_MAINNET_FIAT_ASSET,\n [TransactionType.perpsDeposit]: ARBITRUM_ETH_FIAT_ASSET,\n [TransactionType.predictDeposit]: POLYGON_POL_FIAT_ASSET,\n};\n\nexport const FIAT_ENABLED_TYPES: TransactionType[] = [\n TransactionType.moneyAccountDeposit,\n TransactionType.perpsDeposit,\n TransactionType.predictDeposit,\n];\n"]}
@@ -7,4 +7,5 @@ export type TransactionPayFiatAsset = {
7
7
  };
8
8
  export declare const ETH_MAINNET_FIAT_ASSET: TransactionPayFiatAsset;
9
9
  export declare const FIAT_ASSET_ID_BY_TX_TYPE: Partial<Record<TransactionType, TransactionPayFiatAsset>>;
10
+ export declare const FIAT_ENABLED_TYPES: TransactionType[];
10
11
  //# sourceMappingURL=constants.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"constants.d.cts","sourceRoot":"","sources":["../../../src/strategy/fiat/constants.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,yCAAyC;AACnE,OAAO,KAAK,EAAE,GAAG,EAAE,wBAAwB;AAS3C,eAAO,MAAM,qBAAqB,QAAQ,CAAC;AAE3C,MAAM,MAAM,uBAAuB,GAAG;IACpC,OAAO,EAAE,GAAG,CAAC;IACb,OAAO,EAAE,GAAG,CAAC;CACd,CAAC;AAYF,eAAO,MAAM,sBAAsB,EAAE,uBAGpC,CAAC;AAEF,eAAO,MAAM,wBAAwB,EAAE,OAAO,CAC5C,MAAM,CAAC,eAAe,EAAE,uBAAuB,CAAC,CAKjD,CAAC"}
1
+ {"version":3,"file":"constants.d.cts","sourceRoot":"","sources":["../../../src/strategy/fiat/constants.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,yCAAyC;AACnE,OAAO,KAAK,EAAE,GAAG,EAAE,wBAAwB;AAS3C,eAAO,MAAM,qBAAqB,QAAQ,CAAC;AAE3C,MAAM,MAAM,uBAAuB,GAAG;IACpC,OAAO,EAAE,GAAG,CAAC;IACb,OAAO,EAAE,GAAG,CAAC;CACd,CAAC;AAYF,eAAO,MAAM,sBAAsB,EAAE,uBAGpC,CAAC;AAEF,eAAO,MAAM,wBAAwB,EAAE,OAAO,CAC5C,MAAM,CAAC,eAAe,EAAE,uBAAuB,CAAC,CAKjD,CAAC;AAEF,eAAO,MAAM,kBAAkB,EAAE,eAAe,EAI/C,CAAC"}
@@ -7,4 +7,5 @@ export type TransactionPayFiatAsset = {
7
7
  };
8
8
  export declare const ETH_MAINNET_FIAT_ASSET: TransactionPayFiatAsset;
9
9
  export declare const FIAT_ASSET_ID_BY_TX_TYPE: Partial<Record<TransactionType, TransactionPayFiatAsset>>;
10
+ export declare const FIAT_ENABLED_TYPES: TransactionType[];
10
11
  //# sourceMappingURL=constants.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"constants.d.mts","sourceRoot":"","sources":["../../../src/strategy/fiat/constants.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,yCAAyC;AACnE,OAAO,KAAK,EAAE,GAAG,EAAE,wBAAwB;AAS3C,eAAO,MAAM,qBAAqB,QAAQ,CAAC;AAE3C,MAAM,MAAM,uBAAuB,GAAG;IACpC,OAAO,EAAE,GAAG,CAAC;IACb,OAAO,EAAE,GAAG,CAAC;CACd,CAAC;AAYF,eAAO,MAAM,sBAAsB,EAAE,uBAGpC,CAAC;AAEF,eAAO,MAAM,wBAAwB,EAAE,OAAO,CAC5C,MAAM,CAAC,eAAe,EAAE,uBAAuB,CAAC,CAKjD,CAAC"}
1
+ {"version":3,"file":"constants.d.mts","sourceRoot":"","sources":["../../../src/strategy/fiat/constants.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,yCAAyC;AACnE,OAAO,KAAK,EAAE,GAAG,EAAE,wBAAwB;AAS3C,eAAO,MAAM,qBAAqB,QAAQ,CAAC;AAE3C,MAAM,MAAM,uBAAuB,GAAG;IACpC,OAAO,EAAE,GAAG,CAAC;IACb,OAAO,EAAE,GAAG,CAAC;CACd,CAAC;AAYF,eAAO,MAAM,sBAAsB,EAAE,uBAGpC,CAAC;AAEF,eAAO,MAAM,wBAAwB,EAAE,OAAO,CAC5C,MAAM,CAAC,eAAe,EAAE,uBAAuB,CAAC,CAKjD,CAAC;AAEF,eAAO,MAAM,kBAAkB,EAAE,eAAe,EAI/C,CAAC"}
@@ -14,8 +14,13 @@ export const ETH_MAINNET_FIAT_ASSET = {
14
14
  chainId: CHAIN_ID_MAINNET,
15
15
  };
16
16
  export const FIAT_ASSET_ID_BY_TX_TYPE = {
17
- [TransactionType.predictDeposit]: POLYGON_POL_FIAT_ASSET,
17
+ [TransactionType.moneyAccountDeposit]: ETH_MAINNET_FIAT_ASSET,
18
18
  [TransactionType.perpsDeposit]: ARBITRUM_ETH_FIAT_ASSET,
19
- [TransactionType.perpsDepositAndOrder]: ARBITRUM_ETH_FIAT_ASSET,
19
+ [TransactionType.predictDeposit]: POLYGON_POL_FIAT_ASSET,
20
20
  };
21
+ export const FIAT_ENABLED_TYPES = [
22
+ TransactionType.moneyAccountDeposit,
23
+ TransactionType.perpsDeposit,
24
+ TransactionType.predictDeposit,
25
+ ];
21
26
  //# sourceMappingURL=constants.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"constants.mjs","sourceRoot":"","sources":["../../../src/strategy/fiat/constants.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,yCAAyC;AAGnE,OAAO,EACL,iBAAiB,EACjB,gBAAgB,EAChB,gBAAgB,EAChB,oBAAoB,EACrB,4BAAwB;AAEzB,MAAM,CAAC,MAAM,qBAAqB,GAAG,KAAK,CAAC;AAO3C,MAAM,sBAAsB,GAA4B;IACtD,OAAO,EAAE,4CAA4C;IACrD,OAAO,EAAE,gBAAgB;CAC1B,CAAC;AAEF,MAAM,uBAAuB,GAA4B;IACvD,OAAO,EAAE,oBAAoB;IAC7B,OAAO,EAAE,iBAAiB;CAC3B,CAAC;AAEF,MAAM,CAAC,MAAM,sBAAsB,GAA4B;IAC7D,OAAO,EAAE,oBAAoB;IAC7B,OAAO,EAAE,gBAAgB;CAC1B,CAAC;AAEF,MAAM,CAAC,MAAM,wBAAwB,GAEjC;IACF,CAAC,eAAe,CAAC,cAAc,CAAC,EAAE,sBAAsB;IACxD,CAAC,eAAe,CAAC,YAAY,CAAC,EAAE,uBAAuB;IACvD,CAAC,eAAe,CAAC,oBAAoB,CAAC,EAAE,uBAAuB;CAChE,CAAC","sourcesContent":["import { TransactionType } from '@metamask/transaction-controller';\nimport type { Hex } from '@metamask/utils';\n\nimport {\n CHAIN_ID_ARBITRUM,\n CHAIN_ID_MAINNET,\n CHAIN_ID_POLYGON,\n NATIVE_TOKEN_ADDRESS,\n} from '../../constants';\n\nexport const DEFAULT_FIAT_CURRENCY = 'USD';\n\nexport type TransactionPayFiatAsset = {\n address: Hex;\n chainId: Hex;\n};\n\nconst POLYGON_POL_FIAT_ASSET: TransactionPayFiatAsset = {\n address: '0x0000000000000000000000000000000000001010',\n chainId: CHAIN_ID_POLYGON,\n};\n\nconst ARBITRUM_ETH_FIAT_ASSET: TransactionPayFiatAsset = {\n address: NATIVE_TOKEN_ADDRESS,\n chainId: CHAIN_ID_ARBITRUM,\n};\n\nexport const ETH_MAINNET_FIAT_ASSET: TransactionPayFiatAsset = {\n address: NATIVE_TOKEN_ADDRESS,\n chainId: CHAIN_ID_MAINNET,\n};\n\nexport const FIAT_ASSET_ID_BY_TX_TYPE: Partial<\n Record<TransactionType, TransactionPayFiatAsset>\n> = {\n [TransactionType.predictDeposit]: POLYGON_POL_FIAT_ASSET,\n [TransactionType.perpsDeposit]: ARBITRUM_ETH_FIAT_ASSET,\n [TransactionType.perpsDepositAndOrder]: ARBITRUM_ETH_FIAT_ASSET,\n};\n"]}
1
+ {"version":3,"file":"constants.mjs","sourceRoot":"","sources":["../../../src/strategy/fiat/constants.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,yCAAyC;AAGnE,OAAO,EACL,iBAAiB,EACjB,gBAAgB,EAChB,gBAAgB,EAChB,oBAAoB,EACrB,4BAAwB;AAEzB,MAAM,CAAC,MAAM,qBAAqB,GAAG,KAAK,CAAC;AAO3C,MAAM,sBAAsB,GAA4B;IACtD,OAAO,EAAE,4CAA4C;IACrD,OAAO,EAAE,gBAAgB;CAC1B,CAAC;AAEF,MAAM,uBAAuB,GAA4B;IACvD,OAAO,EAAE,oBAAoB;IAC7B,OAAO,EAAE,iBAAiB;CAC3B,CAAC;AAEF,MAAM,CAAC,MAAM,sBAAsB,GAA4B;IAC7D,OAAO,EAAE,oBAAoB;IAC7B,OAAO,EAAE,gBAAgB;CAC1B,CAAC;AAEF,MAAM,CAAC,MAAM,wBAAwB,GAEjC;IACF,CAAC,eAAe,CAAC,mBAAmB,CAAC,EAAE,sBAAsB;IAC7D,CAAC,eAAe,CAAC,YAAY,CAAC,EAAE,uBAAuB;IACvD,CAAC,eAAe,CAAC,cAAc,CAAC,EAAE,sBAAsB;CACzD,CAAC;AAEF,MAAM,CAAC,MAAM,kBAAkB,GAAsB;IACnD,eAAe,CAAC,mBAAmB;IACnC,eAAe,CAAC,YAAY;IAC5B,eAAe,CAAC,cAAc;CAC/B,CAAC","sourcesContent":["import { TransactionType } from '@metamask/transaction-controller';\nimport type { Hex } from '@metamask/utils';\n\nimport {\n CHAIN_ID_ARBITRUM,\n CHAIN_ID_MAINNET,\n CHAIN_ID_POLYGON,\n NATIVE_TOKEN_ADDRESS,\n} from '../../constants';\n\nexport const DEFAULT_FIAT_CURRENCY = 'USD';\n\nexport type TransactionPayFiatAsset = {\n address: Hex;\n chainId: Hex;\n};\n\nconst POLYGON_POL_FIAT_ASSET: TransactionPayFiatAsset = {\n address: '0x0000000000000000000000000000000000001010',\n chainId: CHAIN_ID_POLYGON,\n};\n\nconst ARBITRUM_ETH_FIAT_ASSET: TransactionPayFiatAsset = {\n address: NATIVE_TOKEN_ADDRESS,\n chainId: CHAIN_ID_ARBITRUM,\n};\n\nexport const ETH_MAINNET_FIAT_ASSET: TransactionPayFiatAsset = {\n address: NATIVE_TOKEN_ADDRESS,\n chainId: CHAIN_ID_MAINNET,\n};\n\nexport const FIAT_ASSET_ID_BY_TX_TYPE: Partial<\n Record<TransactionType, TransactionPayFiatAsset>\n> = {\n [TransactionType.moneyAccountDeposit]: ETH_MAINNET_FIAT_ASSET,\n [TransactionType.perpsDeposit]: ARBITRUM_ETH_FIAT_ASSET,\n [TransactionType.predictDeposit]: POLYGON_POL_FIAT_ASSET,\n};\n\nexport const FIAT_ENABLED_TYPES: TransactionType[] = [\n TransactionType.moneyAccountDeposit,\n TransactionType.perpsDeposit,\n TransactionType.predictDeposit,\n];\n"]}
@@ -8,18 +8,19 @@ const logger_1 = require("../../logger.cjs");
8
8
  const feature_flags_1 = require("../../utils/feature-flags.cjs");
9
9
  const token_1 = require("../../utils/token.cjs");
10
10
  const transaction_1 = require("../../utils/transaction.cjs");
11
- const constants_1 = require("./constants.cjs");
12
11
  const log = (0, utils_1.createModuleLogger)(logger_1.projectLogger, 'fiat-utils');
13
12
  function deriveFiatAssetForFiatPayment(transaction, messenger) {
14
- const txType = resolveTransactionType(transaction);
13
+ const enabledTypes = (0, feature_flags_1.getFiatEnabledTypes)(messenger);
14
+ const txType = resolveTransactionType(transaction, enabledTypes);
15
15
  return (0, feature_flags_1.getFiatAssetPerTransactionType)(messenger, txType);
16
16
  }
17
17
  exports.deriveFiatAssetForFiatPayment = deriveFiatAssetForFiatPayment;
18
- function resolveTransactionType(transaction) {
18
+ function resolveTransactionType(transaction, enabledTypes) {
19
19
  if (transaction.type !== transaction_controller_1.TransactionType.batch) {
20
20
  return transaction.type;
21
21
  }
22
- return transaction.nestedTransactions?.find((tx) => tx.type && constants_1.FIAT_ASSET_ID_BY_TX_TYPE[tx.type] !== undefined)?.type;
22
+ const nestedType = transaction.nestedTransactions?.find((tx) => tx.type && enabledTypes.includes(tx.type))?.type;
23
+ return nestedType ?? transaction.type;
23
24
  }
24
25
  /**
25
26
  * Resolves the raw source amount for a completed fiat order.
@@ -1 +1 @@
1
- {"version":3,"file":"utils.cjs","sourceRoot":"","sources":["../../../src/strategy/fiat/utils.ts"],"names":[],"mappings":";;;AAEA,6EAAmE;AAEnE,2CAAqD;AACrD,+CAAyC;AAEzC,6CAA6C;AAE7C,iEAA2E;AAC3E,iDAAiD;AACjD,6DAAyE;AAGzE,+CAAuD;AAEvD,MAAM,GAAG,GAAG,IAAA,0BAAkB,EAAC,sBAAa,EAAE,YAAY,CAAC,CAAC;AAE5D,SAAgB,6BAA6B,CAC3C,WAA4B,EAC5B,SAA4C;IAE5C,MAAM,MAAM,GAAG,sBAAsB,CAAC,WAAW,CAAC,CAAC;IAEnD,OAAO,IAAA,8CAA8B,EAAC,SAAS,EAAE,MAAM,CAAC,CAAC;AAC3D,CAAC;AAPD,sEAOC;AAED,SAAS,sBAAsB,CAC7B,WAA4B;IAE5B,IAAI,WAAW,CAAC,IAAI,KAAK,wCAAe,CAAC,KAAK,EAAE,CAAC;QAC/C,OAAO,WAAW,CAAC,IAAI,CAAC;IAC1B,CAAC;IAED,OAAO,WAAW,CAAC,kBAAkB,EAAE,IAAI,CACzC,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,CAAC,IAAI,IAAI,oCAAwB,CAAC,EAAE,CAAC,IAAI,CAAC,KAAK,SAAS,CACnE,EAAE,IAAI,CAAC;AACV,CAAC;AAED;;;;;;;;;;;;;GAaG;AACI,KAAK,UAAU,sBAAsB,CAAC,EAC3C,SAAS,EACT,KAAK,EACL,SAAS,EACT,aAAa,GAMd;IACC,IAAI,KAAK,CAAC,MAAM,EAAE,CAAC;QACjB,IAAI,CAAC;YACH,MAAM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AAC,SAAS,CAAC,WAAW,CAAC,CAAC;IAE1D,MAAM,YAAY,GAAG,YAAY;SAC9B,KAAK,CAAC,aAAa,CAAC;SACpB,SAAS,CAAC,YAAY,CAAC;SACvB,YAAY,CAAC,GAAG,CAAC,CAAC;IAErB,GAAG,CAAC,wBAAwB,EAAE;QAC5B,YAAY,EAAE,YAAY,CAAC,OAAO,CAAC,CAAC,CAAC;QACrC,aAAa,EAAE,aAAa,CAAC,OAAO,CAAC,CAAC,CAAC;QACvC,YAAY,EAAE,YAAY,CAAC,OAAO,CAAC,CAAC,CAAC;QACrC,mBAAmB;QACnB,aAAa;KACd,CAAC,CAAC;IAEH,IAAI,YAAY,CAAC,EAAE,CAAC,mBAAmB,CAAC,EAAE,CAAC;QACzC,MAAM,IAAI,KAAK,CACb,4CAA4C;YAC1C,GAAG,YAAY,CAAC,OAAO,CAAC,CAAC,CAAC,aAAa,mBAAmB,OAAO,CACpE,CAAC;IACJ,CAAC;AACH,CAAC;AArDD,wDAqDC;AAED;;;;;;;;GAQG;AACH,SAAgB,mBAAmB,CACjC,QAA4B;IAE5B,IAAI,CAAC,QAAQ,EAAE,CAAC;QACd,OAAO,IAAI,CAAC;IACd,CAAC;IAED,MAAM,KAAK,GAAG,QAAQ,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;IAElD,IAAI,KAAK,CAAC,CAAC,CAAC,KAAK,WAAW,EAAE,CAAC;QAC7B,OAAO,KAAK,CAAC,CAAC,CAAC,IAAI,IAAI,CAAC;IAC1B,CAAC;IAED,OAAO,KAAK,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;AAC9C,CAAC;AAdD,kDAcC","sourcesContent":["import type { RampsOrder } from '@metamask/ramps-controller';\nimport type { TransactionMeta } from '@metamask/transaction-controller';\nimport { TransactionType } from '@metamask/transaction-controller';\nimport type { Hex } from '@metamask/utils';\nimport { createModuleLogger } from '@metamask/utils';\nimport { BigNumber } from 'bignumber.js';\n\nimport { projectLogger } from '../../logger';\nimport type { TransactionPayControllerMessenger } from '../../types';\nimport { getFiatAssetPerTransactionType } from '../../utils/feature-flags';\nimport { getTokenInfo } from '../../utils/token';\nimport { getTransferredAmountFromTxHash } from '../../utils/transaction';\nimport type { RelayQuote } from '../relay/types';\nimport type { TransactionPayFiatAsset } from './constants';\nimport { FIAT_ASSET_ID_BY_TX_TYPE } from './constants';\n\nconst log = createModuleLogger(projectLogger, 'fiat-utils');\n\nexport function deriveFiatAssetForFiatPayment(\n transaction: TransactionMeta,\n messenger: TransactionPayControllerMessenger,\n): TransactionPayFiatAsset {\n const txType = resolveTransactionType(transaction);\n\n return getFiatAssetPerTransactionType(messenger, txType);\n}\n\nfunction resolveTransactionType(\n transaction: TransactionMeta,\n): TransactionType | undefined {\n if (transaction.type !== TransactionType.batch) {\n return transaction.type;\n }\n\n return transaction.nestedTransactions?.find(\n (tx) => tx.type && FIAT_ASSET_ID_BY_TX_TYPE[tx.type] !== undefined,\n )?.type;\n}\n\n/**\n * Resolves the raw source amount for a completed fiat order.\n *\n * Attempts to read the actual transferred amount from the on-chain transaction\n * identified by `order.txHash`. If the on-chain read fails or returns\n * no amount, falls back to computing the amount from `order.cryptoAmount`.\n *\n * @param options - The resolution options.\n * @param options.messenger - Controller messenger for network access.\n * @param options.order - The completed on-ramp order.\n * @param options.fiatAsset - The fiat asset describing the expected token.\n * @param options.walletAddress - Recipient wallet address for on-chain lookup.\n * @returns The raw (atomic) source amount as a decimal string.\n */\nexport async function resolveSourceAmountRaw({\n messenger,\n order,\n fiatAsset,\n walletAddress,\n}: {\n messenger: TransactionPayControllerMessenger;\n order: RampsOrder;\n fiatAsset: TransactionPayFiatAsset;\n walletAddress: Hex;\n}): Promise<string> {\n if (order.txHash) {\n try {\n const onChainAmount = await getTransferredAmountFromTxHash({\n messenger,\n txHash: order.txHash,\n chainId: fiatAsset.chainId,\n tokenAddress: fiatAsset.address,\n walletAddress,\n });\n\n if (onChainAmount) {\n log('Resolved source amount from on-chain transaction', {\n txHash: order.txHash,\n onChainAmount,\n });\n return onChainAmount;\n }\n } catch (error) {\n log(\n 'Failed to read on-chain amount, falling back to order.cryptoAmount',\n { txHash: order.txHash, error },\n );\n }\n }\n\n const tokenInfo = getTokenInfo(\n messenger,\n fiatAsset.address,\n fiatAsset.chainId,\n );\n\n if (!tokenInfo) {\n throw new Error(\n `Unable to resolve token info for fiat asset ${fiatAsset.address} on chain ${fiatAsset.chainId}`,\n );\n }\n\n return getRawSourceAmountFromOrderCryptoAmount({\n cryptoAmount: order.cryptoAmount,\n decimals: tokenInfo.decimals,\n });\n}\n\n/**\n * Converts the order's human-readable crypto amount to a raw token amount.\n *\n * @param options - The conversion options.\n * @param options.cryptoAmount - Human-readable crypto amount from the completed order.\n * @param options.decimals - Token decimals for the fiat asset.\n * @returns The raw token amount as a string.\n */\nexport function getRawSourceAmountFromOrderCryptoAmount({\n cryptoAmount,\n decimals,\n}: {\n cryptoAmount: RampsOrder['cryptoAmount'];\n decimals: number;\n}): string {\n const normalizedAmount = new BigNumber(String(cryptoAmount));\n\n if (!normalizedAmount.isFinite() || normalizedAmount.lte(0)) {\n throw new Error(\n `Invalid fiat order crypto amount: ${String(cryptoAmount)}`,\n );\n }\n\n const rawAmount = normalizedAmount\n .shiftedBy(decimals)\n .decimalPlaces(0, BigNumber.ROUND_DOWN)\n .toFixed(0);\n\n if (!new BigNumber(rawAmount).gt(0)) {\n throw new Error('Computed fiat order source amount is not positive');\n }\n\n return rawAmount;\n}\n\n/**\n * Validates that the relay exchange rate hasn't drifted significantly between\n * the original quoting phase and the post-settlement discovery quote.\n *\n * Compares the USD output/input ratio from both quotes. This normalises for\n * different source amounts (quoting phase uses a theoretical amount, discovery\n * uses the actual settled amount) so the comparison reflects genuine rate\n * movement rather than amount differences.\n *\n * @param options - The validation options.\n * @param options.originalQuote - Relay quote from the original quoting phase.\n * @param options.discoveryQuote - Relay quote from the post-settlement discovery.\n * @param options.maxRateDriftPercent - Maximum allowed rate drift percentage.\n * @param options.transactionId - Transaction ID for error reporting.\n */\nexport function validateRelayRateDrift({\n originalQuote,\n discoveryQuote,\n maxRateDriftPercent,\n transactionId,\n}: {\n originalQuote: RelayQuote;\n discoveryQuote: RelayQuote;\n maxRateDriftPercent: number;\n transactionId: string;\n}): void {\n const originalIn = new BigNumber(originalQuote.details.currencyIn.amountUsd);\n const originalOut = new BigNumber(\n originalQuote.details.currencyOut.amountUsd,\n );\n const discoveryIn = new BigNumber(\n discoveryQuote.details.currencyIn.amountUsd,\n );\n const discoveryOut = new BigNumber(\n discoveryQuote.details.currencyOut.amountUsd,\n );\n\n if (\n !originalIn.gt(0) ||\n !originalOut.gt(0) ||\n !discoveryIn.gt(0) ||\n !discoveryOut.gt(0)\n ) {\n return;\n }\n\n const originalRate = originalOut.dividedBy(originalIn);\n const discoveryRate = discoveryOut.dividedBy(discoveryIn);\n\n const driftPercent = originalRate\n .minus(discoveryRate)\n .dividedBy(originalRate)\n .multipliedBy(100);\n\n log('Relay rate drift check', {\n originalRate: originalRate.toFixed(6),\n discoveryRate: discoveryRate.toFixed(6),\n driftPercent: driftPercent.toFixed(2),\n maxRateDriftPercent,\n transactionId,\n });\n\n if (driftPercent.gt(maxRateDriftPercent)) {\n throw new Error(\n `Relay rate drift too high for transaction ` +\n `${driftPercent.toFixed(2)}% exceeds ${maxRateDriftPercent}% max`,\n );\n }\n}\n\n/**\n * Extracts the provider code from a ramps provider string.\n *\n * Accepts the canonical provider code (e.g. `transak-native`) and, for\n * backwards compatibility, the legacy path form (e.g. `/providers/transak-native`).\n *\n * @param provider - Canonical provider code, or legacy provider path.\n * @returns The provider code, or `null` if the format is invalid.\n */\nexport function extractProviderCode(\n provider: string | undefined,\n): string | null {\n if (!provider) {\n return null;\n }\n\n const parts = provider.split('/').filter(Boolean);\n\n if (parts[0] === 'providers') {\n return parts[1] ?? null;\n }\n\n return parts.length === 1 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AC,aAAa,CAAC;SACpB,SAAS,CAAC,YAAY,CAAC;SACvB,YAAY,CAAC,GAAG,CAAC,CAAC;IAErB,GAAG,CAAC,wBAAwB,EAAE;QAC5B,YAAY,EAAE,YAAY,CAAC,OAAO,CAAC,CAAC,CAAC;QACrC,aAAa,EAAE,aAAa,CAAC,OAAO,CAAC,CAAC,CAAC;QACvC,YAAY,EAAE,YAAY,CAAC,OAAO,CAAC,CAAC,CAAC;QACrC,mBAAmB;QACnB,aAAa;KACd,CAAC,CAAC;IAEH,IAAI,YAAY,CAAC,EAAE,CAAC,mBAAmB,CAAC,EAAE,CAAC;QACzC,MAAM,IAAI,KAAK,CACb,4CAA4C;YAC1C,GAAG,YAAY,CAAC,OAAO,CAAC,CAAC,CAAC,aAAa,mBAAmB,OAAO,CACpE,CAAC;IACJ,CAAC;AACH,CAAC;AArDD,wDAqDC;AAED;;;;;;;;GAQG;AACH,SAAgB,mBAAmB,CACjC,QAA4B;IAE5B,IAAI,CAAC,QAAQ,EAAE,CAAC;QACd,OAAO,IAAI,CAAC;IACd,CAAC;IAED,MAAM,KAAK,GAAG,QAAQ,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;IAElD,IAAI,KAAK,CAAC,CAAC,CAAC,KAAK,WAAW,EAAE,CAAC;QAC7B,OAAO,KAAK,CAAC,CAAC,CAAC,IAAI,IAAI,CAAC;IAC1B,CAAC;IAED,OAAO,KAAK,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;AAC9C,CAAC;AAdD,kDAcC","sourcesContent":["import type { RampsOrder } from '@metamask/ramps-controller';\nimport type { TransactionMeta } from '@metamask/transaction-controller';\nimport { TransactionType } from '@metamask/transaction-controller';\nimport type { Hex } from '@metamask/utils';\nimport { createModuleLogger } from '@metamask/utils';\nimport { BigNumber } from 'bignumber.js';\n\nimport { projectLogger } from '../../logger';\nimport type { TransactionPayControllerMessenger } from '../../types';\nimport {\n getFiatAssetPerTransactionType,\n getFiatEnabledTypes,\n} from '../../utils/feature-flags';\nimport { getTokenInfo } from '../../utils/token';\nimport { getTransferredAmountFromTxHash } from '../../utils/transaction';\nimport type { RelayQuote } from '../relay/types';\nimport type { TransactionPayFiatAsset } from './constants';\n\nconst log = createModuleLogger(projectLogger, 'fiat-utils');\n\nexport function deriveFiatAssetForFiatPayment(\n transaction: TransactionMeta,\n messenger: TransactionPayControllerMessenger,\n): TransactionPayFiatAsset {\n const enabledTypes = getFiatEnabledTypes(messenger);\n const txType = resolveTransactionType(transaction, enabledTypes);\n\n return getFiatAssetPerTransactionType(messenger, txType);\n}\n\nfunction resolveTransactionType(\n transaction: TransactionMeta,\n enabledTypes: TransactionType[],\n): TransactionType | undefined {\n if (transaction.type !== TransactionType.batch) {\n return transaction.type;\n }\n\n const nestedType = transaction.nestedTransactions?.find(\n (tx) => tx.type && enabledTypes.includes(tx.type),\n )?.type;\n\n return nestedType ?? transaction.type;\n}\n\n/**\n * Resolves the raw source amount for a completed fiat order.\n *\n * Attempts to read the actual transferred amount from the on-chain transaction\n * identified by `order.txHash`. If the on-chain read fails or returns\n * no amount, falls back to computing the amount from `order.cryptoAmount`.\n *\n * @param options - The resolution options.\n * @param options.messenger - Controller messenger for network access.\n * @param options.order - The completed on-ramp order.\n * @param options.fiatAsset - The fiat asset describing the expected token.\n * @param options.walletAddress - Recipient wallet address for on-chain lookup.\n * @returns The raw (atomic) source amount as a decimal string.\n */\nexport async function resolveSourceAmountRaw({\n messenger,\n order,\n fiatAsset,\n walletAddress,\n}: {\n messenger: TransactionPayControllerMessenger;\n order: RampsOrder;\n fiatAsset: TransactionPayFiatAsset;\n walletAddress: Hex;\n}): Promise<string> {\n if (order.txHash) {\n try {\n const onChainAmount = await getTransferredAmountFromTxHash({\n messenger,\n txHash: order.txHash,\n chainId: fiatAsset.chainId,\n tokenAddress: fiatAsset.address,\n walletAddress,\n });\n\n if (onChainAmount) {\n log('Resolved source amount from on-chain transaction', {\n txHash: order.txHash,\n onChainAmount,\n });\n return onChainAmount;\n }\n } catch (error) {\n log(\n 'Failed to read on-chain amount, falling back to order.cryptoAmount',\n { txHash: order.txHash, error },\n );\n }\n }\n\n const tokenInfo = getTokenInfo(\n messenger,\n fiatAsset.address,\n fiatAsset.chainId,\n );\n\n if (!tokenInfo) {\n throw new Error(\n `Unable to resolve token info for fiat asset ${fiatAsset.address} on chain ${fiatAsset.chainId}`,\n );\n }\n\n return getRawSourceAmountFromOrderCryptoAmount({\n cryptoAmount: order.cryptoAmount,\n decimals: tokenInfo.decimals,\n });\n}\n\n/**\n * Converts the order's human-readable crypto amount to a raw token amount.\n *\n * @param options - The conversion options.\n * @param options.cryptoAmount - Human-readable crypto amount from the completed order.\n * @param options.decimals - Token decimals for the fiat asset.\n * @returns The raw token amount as a string.\n */\nexport function getRawSourceAmountFromOrderCryptoAmount({\n cryptoAmount,\n decimals,\n}: {\n cryptoAmount: RampsOrder['cryptoAmount'];\n decimals: number;\n}): string {\n const normalizedAmount = new BigNumber(String(cryptoAmount));\n\n if (!normalizedAmount.isFinite() || normalizedAmount.lte(0)) {\n throw new Error(\n `Invalid fiat order crypto amount: ${String(cryptoAmount)}`,\n );\n }\n\n const rawAmount = normalizedAmount\n .shiftedBy(decimals)\n .decimalPlaces(0, BigNumber.ROUND_DOWN)\n .toFixed(0);\n\n if (!new BigNumber(rawAmount).gt(0)) {\n throw new Error('Computed fiat order source amount is not positive');\n }\n\n return rawAmount;\n}\n\n/**\n * Validates that the relay exchange rate hasn't drifted significantly between\n * the original quoting phase and the post-settlement discovery quote.\n *\n * Compares the USD output/input ratio from both quotes. This normalises for\n * different source amounts (quoting phase uses a theoretical amount, discovery\n * uses the actual settled amount) so the comparison reflects genuine rate\n * movement rather than amount differences.\n *\n * @param options - The validation options.\n * @param options.originalQuote - Relay quote from the original quoting phase.\n * @param options.discoveryQuote - Relay quote from the post-settlement discovery.\n * @param options.maxRateDriftPercent - Maximum allowed rate drift percentage.\n * @param options.transactionId - Transaction ID for error reporting.\n */\nexport function validateRelayRateDrift({\n originalQuote,\n discoveryQuote,\n maxRateDriftPercent,\n transactionId,\n}: {\n originalQuote: RelayQuote;\n discoveryQuote: RelayQuote;\n maxRateDriftPercent: number;\n transactionId: string;\n}): void {\n const originalIn = new BigNumber(originalQuote.details.currencyIn.amountUsd);\n const originalOut = new BigNumber(\n originalQuote.details.currencyOut.amountUsd,\n );\n const discoveryIn = new BigNumber(\n discoveryQuote.details.currencyIn.amountUsd,\n );\n const discoveryOut = new BigNumber(\n discoveryQuote.details.currencyOut.amountUsd,\n );\n\n if (\n !originalIn.gt(0) ||\n !originalOut.gt(0) ||\n !discoveryIn.gt(0) ||\n !discoveryOut.gt(0)\n ) {\n return;\n }\n\n const originalRate = originalOut.dividedBy(originalIn);\n const discoveryRate = discoveryOut.dividedBy(discoveryIn);\n\n const driftPercent = originalRate\n .minus(discoveryRate)\n .dividedBy(originalRate)\n .multipliedBy(100);\n\n log('Relay rate drift check', {\n originalRate: originalRate.toFixed(6),\n discoveryRate: discoveryRate.toFixed(6),\n driftPercent: driftPercent.toFixed(2),\n maxRateDriftPercent,\n transactionId,\n });\n\n if (driftPercent.gt(maxRateDriftPercent)) {\n throw new Error(\n `Relay rate drift too high for transaction ` +\n `${driftPercent.toFixed(2)}% exceeds ${maxRateDriftPercent}% max`,\n );\n }\n}\n\n/**\n * Extracts the provider code from a ramps provider string.\n *\n * Accepts the canonical provider code (e.g. `transak-native`) and, for\n * backwards compatibility, the legacy path form (e.g. `/providers/transak-native`).\n *\n * @param provider - Canonical provider code, or legacy provider path.\n * @returns The provider code, or `null` if the format is invalid.\n */\nexport function extractProviderCode(\n provider: string | undefined,\n): string | null {\n if (!provider) {\n return null;\n }\n\n const parts = provider.split('/').filter(Boolean);\n\n if (parts[0] === 'providers') {\n return parts[1] ?? null;\n }\n\n return parts.length === 1 ? parts[0] : null;\n}\n"]}
@@ -1 +1 @@
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,EAAE;IACD,aAAa,EAAE,UAAU,CAAC;IAC1B,cAAc,EAAE,UAAU,CAAC;IAC3B,mBAAmB,EAAE,MAAM,CAAC;IAC5B,aAAa,EAAE,MAAM,CAAC;CACvB,GAAG,IAAI,CA2CP;AAED;;;;;;;;GAQG;AACH,wBAAgB,mBAAmB,CACjC,QAAQ,EAAE,MAAM,GAAG,SAAS,GAC3B,MAAM,GAAG,IAAI,CAYf"}
@@ -2,20 +2,21 @@ import { TransactionType } from "@metamask/transaction-controller";
2
2
  import { createModuleLogger } from "@metamask/utils";
3
3
  import { BigNumber } from "bignumber.js";
4
4
  import { projectLogger } from "../../logger.mjs";
5
- import { getFiatAssetPerTransactionType } from "../../utils/feature-flags.mjs";
5
+ import { getFiatAssetPerTransactionType, getFiatEnabledTypes } from "../../utils/feature-flags.mjs";
6
6
  import { getTokenInfo } from "../../utils/token.mjs";
7
7
  import { getTransferredAmountFromTxHash } from "../../utils/transaction.mjs";
8
- import { FIAT_ASSET_ID_BY_TX_TYPE } from "./constants.mjs";
9
8
  const log = createModuleLogger(projectLogger, 'fiat-utils');
10
9
  export function deriveFiatAssetForFiatPayment(transaction, messenger) {
11
- const txType = resolveTransactionType(transaction);
10
+ const enabledTypes = getFiatEnabledTypes(messenger);
11
+ const txType = resolveTransactionType(transaction, enabledTypes);
12
12
  return getFiatAssetPerTransactionType(messenger, txType);
13
13
  }
14
- function resolveTransactionType(transaction) {
14
+ function resolveTransactionType(transaction, enabledTypes) {
15
15
  if (transaction.type !== TransactionType.batch) {
16
16
  return transaction.type;
17
17
  }
18
- return transaction.nestedTransactions?.find((tx) => tx.type && FIAT_ASSET_ID_BY_TX_TYPE[tx.type] !== undefined)?.type;
18
+ const nestedType = transaction.nestedTransactions?.find((tx) => tx.type && enabledTypes.includes(tx.type))?.type;
19
+ return nestedType ?? transaction.type;
19
20
  }
20
21
  /**
21
22
  * Resolves the raw source amount for a completed fiat order.
@@ -1 +1 @@
1
- {"version":3,"file":"utils.mjs","sourceRo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Y,CAAC;SACvB,YAAY,CAAC,GAAG,CAAC,CAAC;IAErB,GAAG,CAAC,wBAAwB,EAAE;QAC5B,YAAY,EAAE,YAAY,CAAC,OAAO,CAAC,CAAC,CAAC;QACrC,aAAa,EAAE,aAAa,CAAC,OAAO,CAAC,CAAC,CAAC;QACvC,YAAY,EAAE,YAAY,CAAC,OAAO,CAAC,CAAC,CAAC;QACrC,mBAAmB;QACnB,aAAa;KACd,CAAC,CAAC;IAEH,IAAI,YAAY,CAAC,EAAE,CAAC,mBAAmB,CAAC,EAAE,CAAC;QACzC,MAAM,IAAI,KAAK,CACb,4CAA4C;YAC1C,GAAG,YAAY,CAAC,OAAO,CAAC,CAAC,CAAC,aAAa,mBAAmB,OAAO,CACpE,CAAC;IACJ,CAAC;AACH,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,mBAAmB,CACjC,QAA4B;IAE5B,IAAI,CAAC,QAAQ,EAAE,CAAC;QACd,OAAO,IAAI,CAAC;IACd,CAAC;IAED,MAAM,KAAK,GAAG,QAAQ,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;IAElD,IAAI,KAAK,CAAC,CAAC,CAAC,KAAK,WAAW,EAAE,CAAC;QAC7B,OAAO,KAAK,CAAC,CAAC,CAAC,IAAI,IAAI,CAAC;IAC1B,CAAC;IAED,OAAO,KAAK,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;AAC9C,CAAC","sourcesContent":["import type { RampsOrder } from '@metamask/ramps-controller';\nimport type { TransactionMeta } from '@metamask/transaction-controller';\nimport { TransactionType } from '@metamask/transaction-controller';\nimport type { Hex } from '@metamask/utils';\nimport { createModuleLogger } from '@metamask/utils';\nimport { BigNumber } from 'bignumber.js';\n\nimport { projectLogger } from '../../logger';\nimport type { TransactionPayControllerMessenger } from '../../types';\nimport { getFiatAssetPerTransactionType } from '../../utils/feature-flags';\nimport { getTokenInfo } from '../../utils/token';\nimport { getTransferredAmountFromTxHash } from '../../utils/transaction';\nimport type { RelayQuote } from '../relay/types';\nimport type { TransactionPayFiatAsset } from './constants';\nimport { FIAT_ASSET_ID_BY_TX_TYPE } from './constants';\n\nconst log = createModuleLogger(projectLogger, 'fiat-utils');\n\nexport function deriveFiatAssetForFiatPayment(\n transaction: TransactionMeta,\n messenger: TransactionPayControllerMessenger,\n): TransactionPayFiatAsset {\n const txType = resolveTransactionType(transaction);\n\n return getFiatAssetPerTransactionType(messenger, txType);\n}\n\nfunction resolveTransactionType(\n transaction: TransactionMeta,\n): TransactionType | undefined {\n if (transaction.type !== TransactionType.batch) {\n return transaction.type;\n }\n\n return transaction.nestedTransactions?.find(\n (tx) => tx.type && FIAT_ASSET_ID_BY_TX_TYPE[tx.type] !== undefined,\n )?.type;\n}\n\n/**\n * Resolves the raw source amount for a completed fiat order.\n *\n * Attempts to read the actual transferred amount from the on-chain transaction\n * identified by `order.txHash`. If the on-chain read fails or returns\n * no amount, falls back to computing the amount from `order.cryptoAmount`.\n *\n * @param options - The resolution options.\n * @param options.messenger - Controller messenger for network access.\n * @param options.order - The completed on-ramp order.\n * @param options.fiatAsset - The fiat asset describing the expected token.\n * @param options.walletAddress - Recipient wallet address for on-chain lookup.\n * @returns The raw (atomic) source amount as a decimal string.\n */\nexport async function resolveSourceAmountRaw({\n messenger,\n order,\n fiatAsset,\n walletAddress,\n}: {\n messenger: TransactionPayControllerMessenger;\n order: RampsOrder;\n fiatAsset: TransactionPayFiatAsset;\n walletAddress: Hex;\n}): Promise<string> {\n if (order.txHash) {\n try {\n const onChainAmount = await getTransferredAmountFromTxHash({\n messenger,\n txHash: order.txHash,\n chainId: fiatAsset.chainId,\n tokenAddress: fiatAsset.address,\n walletAddress,\n });\n\n if (onChainAmount) {\n log('Resolved source amount from on-chain transaction', {\n txHash: order.txHash,\n onChainAmount,\n });\n return onChainAmount;\n }\n } catch (error) {\n log(\n 'Failed to read on-chain amount, falling back to order.cryptoAmount',\n { txHash: order.txHash, error },\n );\n }\n }\n\n const tokenInfo = getTokenInfo(\n messenger,\n fiatAsset.address,\n fiatAsset.chainId,\n );\n\n if (!tokenInfo) {\n throw new Error(\n `Unable to resolve token info for fiat asset ${fiatAsset.address} on chain ${fiatAsset.chainId}`,\n );\n }\n\n return getRawSourceAmountFromOrderCryptoAmount({\n cryptoAmount: order.cryptoAmount,\n decimals: tokenInfo.decimals,\n });\n}\n\n/**\n * Converts the order's human-readable crypto amount to a raw token amount.\n *\n * @param options - The conversion options.\n * @param options.cryptoAmount - Human-readable crypto amount from the completed order.\n * @param options.decimals - Token decimals for the fiat asset.\n * @returns The raw token amount as a string.\n */\nexport function getRawSourceAmountFromOrderCryptoAmount({\n cryptoAmount,\n decimals,\n}: {\n cryptoAmount: RampsOrder['cryptoAmount'];\n decimals: number;\n}): string {\n const normalizedAmount = new BigNumber(String(cryptoAmount));\n\n if (!normalizedAmount.isFinite() || normalizedAmount.lte(0)) {\n throw new Error(\n `Invalid fiat order crypto amount: ${String(cryptoAmount)}`,\n );\n }\n\n const rawAmount = normalizedAmount\n .shiftedBy(decimals)\n .decimalPlaces(0, BigNumber.ROUND_DOWN)\n .toFixed(0);\n\n if (!new BigNumber(rawAmount).gt(0)) {\n throw new Error('Computed fiat order source amount is not positive');\n }\n\n return rawAmount;\n}\n\n/**\n * Validates that the relay exchange rate hasn't drifted significantly between\n * the original quoting phase and the post-settlement discovery quote.\n *\n * Compares the USD output/input ratio from both quotes. This normalises for\n * different source amounts (quoting phase uses a theoretical amount, discovery\n * uses the actual settled amount) so the comparison reflects genuine rate\n * movement rather than amount differences.\n *\n * @param options - The validation options.\n * @param options.originalQuote - Relay quote from the original quoting phase.\n * @param options.discoveryQuote - Relay quote from the post-settlement discovery.\n * @param options.maxRateDriftPercent - Maximum allowed rate drift percentage.\n * @param options.transactionId - Transaction ID for error reporting.\n */\nexport function validateRelayRateDrift({\n originalQuote,\n discoveryQuote,\n maxRateDriftPercent,\n transactionId,\n}: {\n originalQuote: RelayQuote;\n discoveryQuote: RelayQuote;\n maxRateDriftPercent: number;\n transactionId: string;\n}): void {\n const originalIn = new BigNumber(originalQuote.details.currencyIn.amountUsd);\n const originalOut = new BigNumber(\n originalQuote.details.currencyOut.amountUsd,\n );\n const discoveryIn = new BigNumber(\n discoveryQuote.details.currencyIn.amountUsd,\n );\n const discoveryOut = new BigNumber(\n discoveryQuote.details.currencyOut.amountUsd,\n );\n\n if (\n !originalIn.gt(0) ||\n !originalOut.gt(0) ||\n !discoveryIn.gt(0) ||\n !discoveryOut.gt(0)\n ) {\n return;\n }\n\n const originalRate = originalOut.dividedBy(originalIn);\n const discoveryRate = discoveryOut.dividedBy(discoveryIn);\n\n const driftPercent = originalRate\n .minus(discoveryRate)\n .dividedBy(originalRate)\n .multipliedBy(100);\n\n log('Relay rate drift check', {\n originalRate: originalRate.toFixed(6),\n discoveryRate: discoveryRate.toFixed(6),\n driftPercent: driftPercent.toFixed(2),\n maxRateDriftPercent,\n transactionId,\n });\n\n if (driftPercent.gt(maxRateDriftPercent)) {\n throw new Error(\n `Relay rate drift too high for transaction ` +\n `${driftPercent.toFixed(2)}% exceeds ${maxRateDriftPercent}% max`,\n );\n }\n}\n\n/**\n * Extracts the provider code from a ramps provider string.\n *\n * Accepts the canonical provider code (e.g. `transak-native`) and, for\n * backwards compatibility, the legacy path form (e.g. `/providers/transak-native`).\n *\n * @param provider - Canonical provider code, or legacy provider path.\n * @returns The provider code, or `null` if the format is invalid.\n */\nexport function extractProviderCode(\n provider: string | undefined,\n): string | null {\n if (!provider) {\n return null;\n }\n\n const parts = provider.split('/').filter(Boolean);\n\n if (parts[0] === 'providers') {\n return parts[1] ?? null;\n }\n\n return parts.length === 1 ? parts[0] : null;\n}\n"]}
1
+ {"version":3,"file":"utils.mjs","sourceRoot":"","sources":["../../../src/strategy/fiat/utils.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,eAAe,EAAE,yCAAyC;AA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AC;SACpB,SAAS,CAAC,YAAY,CAAC;SACvB,YAAY,CAAC,GAAG,CAAC,CAAC;IAErB,GAAG,CAAC,wBAAwB,EAAE;QAC5B,YAAY,EAAE,YAAY,CAAC,OAAO,CAAC,CAAC,CAAC;QACrC,aAAa,EAAE,aAAa,CAAC,OAAO,CAAC,CAAC,CAAC;QACvC,YAAY,EAAE,YAAY,CAAC,OAAO,CAAC,CAAC,CAAC;QACrC,mBAAmB;QACnB,aAAa;KACd,CAAC,CAAC;IAEH,IAAI,YAAY,CAAC,EAAE,CAAC,mBAAmB,CAAC,EAAE,CAAC;QACzC,MAAM,IAAI,KAAK,CACb,4CAA4C;YAC1C,GAAG,YAAY,CAAC,OAAO,CAAC,CAAC,CAAC,aAAa,mBAAmB,OAAO,CACpE,CAAC;IACJ,CAAC;AACH,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,mBAAmB,CACjC,QAA4B;IAE5B,IAAI,CAAC,QAAQ,EAAE,CAAC;QACd,OAAO,IAAI,CAAC;IACd,CAAC;IAED,MAAM,KAAK,GAAG,QAAQ,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;IAElD,IAAI,KAAK,CAAC,CAAC,CAAC,KAAK,WAAW,EAAE,CAAC;QAC7B,OAAO,KAAK,CAAC,CAAC,CAAC,IAAI,IAAI,CAAC;IAC1B,CAAC;IAED,OAAO,KAAK,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;AAC9C,CAAC","sourcesContent":["import type { RampsOrder } from '@metamask/ramps-controller';\nimport type { TransactionMeta } from '@metamask/transaction-controller';\nimport { TransactionType } from '@metamask/transaction-controller';\nimport type { Hex } from '@metamask/utils';\nimport { createModuleLogger } from '@metamask/utils';\nimport { BigNumber } from 'bignumber.js';\n\nimport { projectLogger } from '../../logger';\nimport type { TransactionPayControllerMessenger } from '../../types';\nimport {\n getFiatAssetPerTransactionType,\n getFiatEnabledTypes,\n} from '../../utils/feature-flags';\nimport { getTokenInfo } from '../../utils/token';\nimport { getTransferredAmountFromTxHash } from '../../utils/transaction';\nimport type { RelayQuote } from '../relay/types';\nimport type { TransactionPayFiatAsset } from './constants';\n\nconst log = createModuleLogger(projectLogger, 'fiat-utils');\n\nexport function deriveFiatAssetForFiatPayment(\n transaction: TransactionMeta,\n messenger: TransactionPayControllerMessenger,\n): TransactionPayFiatAsset {\n const enabledTypes = getFiatEnabledTypes(messenger);\n const txType = resolveTransactionType(transaction, enabledTypes);\n\n return getFiatAssetPerTransactionType(messenger, txType);\n}\n\nfunction resolveTransactionType(\n transaction: TransactionMeta,\n enabledTypes: TransactionType[],\n): TransactionType | undefined {\n if (transaction.type !== TransactionType.batch) {\n return transaction.type;\n }\n\n const nestedType = transaction.nestedTransactions?.find(\n (tx) => tx.type && enabledTypes.includes(tx.type),\n )?.type;\n\n return nestedType ?? transaction.type;\n}\n\n/**\n * Resolves the raw source amount for a completed fiat order.\n *\n * Attempts to read the actual transferred amount from the on-chain transaction\n * identified by `order.txHash`. If the on-chain read fails or returns\n * no amount, falls back to computing the amount from `order.cryptoAmount`.\n *\n * @param options - The resolution options.\n * @param options.messenger - Controller messenger for network access.\n * @param options.order - The completed on-ramp order.\n * @param options.fiatAsset - The fiat asset describing the expected token.\n * @param options.walletAddress - Recipient wallet address for on-chain lookup.\n * @returns The raw (atomic) source amount as a decimal string.\n */\nexport async function resolveSourceAmountRaw({\n messenger,\n order,\n fiatAsset,\n walletAddress,\n}: {\n messenger: TransactionPayControllerMessenger;\n order: RampsOrder;\n fiatAsset: TransactionPayFiatAsset;\n walletAddress: Hex;\n}): Promise<string> {\n if (order.txHash) {\n try {\n const onChainAmount = await getTransferredAmountFromTxHash({\n messenger,\n txHash: order.txHash,\n chainId: fiatAsset.chainId,\n tokenAddress: fiatAsset.address,\n walletAddress,\n });\n\n if (onChainAmount) {\n log('Resolved source amount from on-chain transaction', {\n txHash: order.txHash,\n onChainAmount,\n });\n return onChainAmount;\n }\n } catch (error) {\n log(\n 'Failed to read on-chain amount, falling back to order.cryptoAmount',\n { txHash: order.txHash, error },\n );\n }\n }\n\n const tokenInfo = getTokenInfo(\n messenger,\n fiatAsset.address,\n fiatAsset.chainId,\n );\n\n if (!tokenInfo) {\n throw new Error(\n `Unable to resolve token info for fiat asset ${fiatAsset.address} on chain ${fiatAsset.chainId}`,\n );\n }\n\n return getRawSourceAmountFromOrderCryptoAmount({\n cryptoAmount: order.cryptoAmount,\n decimals: tokenInfo.decimals,\n });\n}\n\n/**\n * Converts the order's human-readable crypto amount to a raw token amount.\n *\n * @param options - The conversion options.\n * @param options.cryptoAmount - Human-readable crypto amount from the completed order.\n * @param options.decimals - Token decimals for the fiat asset.\n * @returns The raw token amount as a string.\n */\nexport function getRawSourceAmountFromOrderCryptoAmount({\n cryptoAmount,\n decimals,\n}: {\n cryptoAmount: RampsOrder['cryptoAmount'];\n decimals: number;\n}): string {\n const normalizedAmount = new BigNumber(String(cryptoAmount));\n\n if (!normalizedAmount.isFinite() || normalizedAmount.lte(0)) {\n throw new Error(\n `Invalid fiat order crypto amount: ${String(cryptoAmount)}`,\n );\n }\n\n const rawAmount = normalizedAmount\n .shiftedBy(decimals)\n .decimalPlaces(0, BigNumber.ROUND_DOWN)\n .toFixed(0);\n\n if (!new BigNumber(rawAmount).gt(0)) {\n throw new Error('Computed fiat order source amount is not positive');\n }\n\n return rawAmount;\n}\n\n/**\n * Validates that the relay exchange rate hasn't drifted significantly between\n * the original quoting phase and the post-settlement discovery quote.\n *\n * Compares the USD output/input ratio from both quotes. This normalises for\n * different source amounts (quoting phase uses a theoretical amount, discovery\n * uses the actual settled amount) so the comparison reflects genuine rate\n * movement rather than amount differences.\n *\n * @param options - The validation options.\n * @param options.originalQuote - Relay quote from the original quoting phase.\n * @param options.discoveryQuote - Relay quote from the post-settlement discovery.\n * @param options.maxRateDriftPercent - Maximum allowed rate drift percentage.\n * @param options.transactionId - Transaction ID for error reporting.\n */\nexport function validateRelayRateDrift({\n originalQuote,\n discoveryQuote,\n maxRateDriftPercent,\n transactionId,\n}: {\n originalQuote: RelayQuote;\n discoveryQuote: RelayQuote;\n maxRateDriftPercent: number;\n transactionId: string;\n}): void {\n const originalIn = new BigNumber(originalQuote.details.currencyIn.amountUsd);\n const originalOut = new BigNumber(\n originalQuote.details.currencyOut.amountUsd,\n );\n const discoveryIn = new BigNumber(\n discoveryQuote.details.currencyIn.amountUsd,\n );\n const discoveryOut = new BigNumber(\n discoveryQuote.details.currencyOut.amountUsd,\n );\n\n if (\n !originalIn.gt(0) ||\n !originalOut.gt(0) ||\n !discoveryIn.gt(0) ||\n !discoveryOut.gt(0)\n ) {\n return;\n }\n\n const originalRate = originalOut.dividedBy(originalIn);\n const discoveryRate = discoveryOut.dividedBy(discoveryIn);\n\n const driftPercent = originalRate\n .minus(discoveryRate)\n .dividedBy(originalRate)\n .multipliedBy(100);\n\n log('Relay rate drift check', {\n originalRate: originalRate.toFixed(6),\n discoveryRate: discoveryRate.toFixed(6),\n driftPercent: driftPercent.toFixed(2),\n maxRateDriftPercent,\n transactionId,\n });\n\n if (driftPercent.gt(maxRateDriftPercent)) {\n throw new Error(\n `Relay rate drift too high for transaction ` +\n `${driftPercent.toFixed(2)}% exceeds ${maxRateDriftPercent}% max`,\n );\n }\n}\n\n/**\n * Extracts the provider code from a ramps provider string.\n *\n * Accepts the canonical provider code (e.g. `transak-native`) and, for\n * backwards compatibility, the legacy path form (e.g. `/providers/transak-native`).\n *\n * @param provider - Canonical provider code, or legacy provider path.\n * @returns The provider code, or `null` if the format is invalid.\n */\nexport function extractProviderCode(\n provider: string | undefined,\n): string | null {\n if (!provider) {\n return null;\n }\n\n const parts = provider.split('/').filter(Boolean);\n\n if (parts[0] === 'providers') {\n return parts[1] ?? null;\n }\n\n return parts.length === 1 ? parts[0] : null;\n}\n"]}
@@ -277,7 +277,7 @@ async function processMoneyAccountPostQuote(transaction, request, requestBody, m
277
277
  const fundingRecipient = recipient ?? request.from;
278
278
  requestBody.authorizationList = normalizeAuthorizationList(authorizationList);
279
279
  requestBody.tradeType = 'EXACT_OUTPUT';
280
- requestBody.amount = request.sourceTokenAmount;
280
+ requestBody.amount = transactionData?.tokens?.[0]?.amountRaw ?? '0';
281
281
  requestBody.txs = [
282
282
  {
283
283
  to: request.targetTokenAddress,