@metamask/transaction-pay-controller 1.0.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 -0
- package/LICENSE +20 -0
- package/README.md +19 -0
- package/dist/TransactionPayController.cjs +96 -0
- package/dist/TransactionPayController.cjs.map +1 -0
- package/dist/TransactionPayController.d.cts +9 -0
- package/dist/TransactionPayController.d.cts.map +1 -0
- package/dist/TransactionPayController.d.mts +9 -0
- package/dist/TransactionPayController.d.mts.map +1 -0
- package/dist/TransactionPayController.mjs +93 -0
- package/dist/TransactionPayController.mjs.map +1 -0
- package/dist/actions/update-payment-token.cjs +79 -0
- package/dist/actions/update-payment-token.cjs.map +1 -0
- package/dist/actions/update-payment-token.d.cts +14 -0
- package/dist/actions/update-payment-token.d.cts.map +1 -0
- package/dist/actions/update-payment-token.d.mts +14 -0
- package/dist/actions/update-payment-token.d.mts.map +1 -0
- package/dist/actions/update-payment-token.mjs +75 -0
- package/dist/actions/update-payment-token.mjs.map +1 -0
- package/dist/constants.cjs +12 -0
- package/dist/constants.cjs.map +1 -0
- package/dist/constants.d.cts +8 -0
- package/dist/constants.d.cts.map +1 -0
- package/dist/constants.d.mts +8 -0
- package/dist/constants.d.mts.map +1 -0
- package/dist/constants.mjs +9 -0
- package/dist/constants.mjs.map +1 -0
- package/dist/helpers/TransactionPayPublishHook.cjs +61 -0
- package/dist/helpers/TransactionPayPublishHook.cjs.map +1 -0
- package/dist/helpers/TransactionPayPublishHook.d.cts +12 -0
- package/dist/helpers/TransactionPayPublishHook.d.cts.map +1 -0
- package/dist/helpers/TransactionPayPublishHook.d.mts +12 -0
- package/dist/helpers/TransactionPayPublishHook.d.mts.map +1 -0
- package/dist/helpers/TransactionPayPublishHook.mjs +57 -0
- package/dist/helpers/TransactionPayPublishHook.mjs.map +1 -0
- package/dist/index.cjs +10 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +6 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +6 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +4 -0
- package/dist/index.mjs.map +1 -0
- package/dist/logger.cjs +8 -0
- package/dist/logger.cjs.map +1 -0
- package/dist/logger.d.cts +5 -0
- package/dist/logger.d.cts.map +1 -0
- package/dist/logger.d.mts +5 -0
- package/dist/logger.d.mts.map +1 -0
- package/dist/logger.mjs +5 -0
- package/dist/logger.mjs.map +1 -0
- package/dist/strategy/bridge/BridgeStrategy.cjs +30 -0
- package/dist/strategy/bridge/BridgeStrategy.cjs.map +1 -0
- package/dist/strategy/bridge/BridgeStrategy.d.cts +11 -0
- package/dist/strategy/bridge/BridgeStrategy.d.cts.map +1 -0
- package/dist/strategy/bridge/BridgeStrategy.d.mts +11 -0
- package/dist/strategy/bridge/BridgeStrategy.d.mts.map +1 -0
- package/dist/strategy/bridge/BridgeStrategy.mjs +26 -0
- package/dist/strategy/bridge/BridgeStrategy.mjs.map +1 -0
- package/dist/strategy/bridge/bridge-quotes.cjs +386 -0
- package/dist/strategy/bridge/bridge-quotes.cjs.map +1 -0
- package/dist/strategy/bridge/bridge-quotes.d.cts +35 -0
- package/dist/strategy/bridge/bridge-quotes.d.cts.map +1 -0
- package/dist/strategy/bridge/bridge-quotes.d.mts +35 -0
- package/dist/strategy/bridge/bridge-quotes.d.mts.map +1 -0
- package/dist/strategy/bridge/bridge-quotes.mjs +380 -0
- package/dist/strategy/bridge/bridge-quotes.mjs.map +1 -0
- package/dist/strategy/bridge/bridge-submit.cjs +155 -0
- package/dist/strategy/bridge/bridge-submit.cjs.map +1 -0
- package/dist/strategy/bridge/bridge-submit.d.cts +19 -0
- package/dist/strategy/bridge/bridge-submit.d.cts.map +1 -0
- package/dist/strategy/bridge/bridge-submit.d.mts +19 -0
- package/dist/strategy/bridge/bridge-submit.d.mts.map +1 -0
- package/dist/strategy/bridge/bridge-submit.mjs +152 -0
- package/dist/strategy/bridge/bridge-submit.mjs.map +1 -0
- package/dist/strategy/bridge/types.cjs +3 -0
- package/dist/strategy/bridge/types.cjs.map +1 -0
- package/dist/strategy/bridge/types.d.cts +27 -0
- package/dist/strategy/bridge/types.d.cts.map +1 -0
- package/dist/strategy/bridge/types.d.mts +27 -0
- package/dist/strategy/bridge/types.d.mts.map +1 -0
- package/dist/strategy/bridge/types.mjs +2 -0
- package/dist/strategy/bridge/types.mjs.map +1 -0
- package/dist/strategy/relay/RelayStrategy.cjs +15 -0
- package/dist/strategy/relay/RelayStrategy.cjs.map +1 -0
- package/dist/strategy/relay/RelayStrategy.d.cts +9 -0
- package/dist/strategy/relay/RelayStrategy.d.cts.map +1 -0
- package/dist/strategy/relay/RelayStrategy.d.mts +9 -0
- package/dist/strategy/relay/RelayStrategy.d.mts.map +1 -0
- package/dist/strategy/relay/RelayStrategy.mjs +11 -0
- package/dist/strategy/relay/RelayStrategy.mjs.map +1 -0
- package/dist/strategy/relay/constants.cjs +9 -0
- package/dist/strategy/relay/constants.cjs.map +1 -0
- package/dist/strategy/relay/constants.d.cts +6 -0
- package/dist/strategy/relay/constants.d.cts.map +1 -0
- package/dist/strategy/relay/constants.d.mts +6 -0
- package/dist/strategy/relay/constants.d.mts.map +1 -0
- package/dist/strategy/relay/constants.mjs +6 -0
- package/dist/strategy/relay/constants.mjs.map +1 -0
- package/dist/strategy/relay/relay-quotes.cjs +210 -0
- package/dist/strategy/relay/relay-quotes.cjs.map +1 -0
- package/dist/strategy/relay/relay-quotes.d.cts +10 -0
- package/dist/strategy/relay/relay-quotes.d.cts.map +1 -0
- package/dist/strategy/relay/relay-quotes.d.mts +10 -0
- package/dist/strategy/relay/relay-quotes.d.mts.map +1 -0
- package/dist/strategy/relay/relay-quotes.mjs +206 -0
- package/dist/strategy/relay/relay-quotes.mjs.map +1 -0
- package/dist/strategy/relay/relay-submit.cjs +137 -0
- package/dist/strategy/relay/relay-submit.cjs.map +1 -0
- package/dist/strategy/relay/relay-submit.d.cts +13 -0
- package/dist/strategy/relay/relay-submit.d.cts.map +1 -0
- package/dist/strategy/relay/relay-submit.d.mts +13 -0
- package/dist/strategy/relay/relay-submit.d.mts.map +1 -0
- package/dist/strategy/relay/relay-submit.mjs +133 -0
- package/dist/strategy/relay/relay-submit.mjs.map +1 -0
- package/dist/strategy/relay/types.cjs +3 -0
- package/dist/strategy/relay/types.cjs.map +1 -0
- package/dist/strategy/relay/types.d.cts +52 -0
- package/dist/strategy/relay/types.d.cts.map +1 -0
- package/dist/strategy/relay/types.d.mts +52 -0
- package/dist/strategy/relay/types.d.mts.map +1 -0
- package/dist/strategy/relay/types.mjs +2 -0
- package/dist/strategy/relay/types.mjs.map +1 -0
- package/dist/strategy/test/TestStrategy.cjs +48 -0
- package/dist/strategy/test/TestStrategy.cjs.map +1 -0
- package/dist/strategy/test/TestStrategy.d.cts +9 -0
- package/dist/strategy/test/TestStrategy.d.cts.map +1 -0
- package/dist/strategy/test/TestStrategy.d.mts +9 -0
- package/dist/strategy/test/TestStrategy.d.mts.map +1 -0
- package/dist/strategy/test/TestStrategy.mjs +44 -0
- package/dist/strategy/test/TestStrategy.mjs.map +1 -0
- package/dist/tests/messenger-mock.cjs +76 -0
- package/dist/tests/messenger-mock.cjs.map +1 -0
- package/dist/tests/messenger-mock.d.cts +215 -0
- package/dist/tests/messenger-mock.d.cts.map +1 -0
- package/dist/tests/messenger-mock.d.mts +215 -0
- package/dist/tests/messenger-mock.d.mts.map +1 -0
- package/dist/tests/messenger-mock.mjs +72 -0
- package/dist/tests/messenger-mock.mjs.map +1 -0
- package/dist/types.cjs +3 -0
- package/dist/types.cjs.map +1 -0
- package/dist/types.d.cts +264 -0
- package/dist/types.d.cts.map +1 -0
- package/dist/types.d.mts +264 -0
- package/dist/types.d.mts.map +1 -0
- package/dist/types.mjs +2 -0
- package/dist/types.mjs.map +1 -0
- package/dist/utils/gas.cjs +87 -0
- package/dist/utils/gas.cjs.map +1 -0
- package/dist/utils/gas.d.cts +33 -0
- package/dist/utils/gas.d.cts.map +1 -0
- package/dist/utils/gas.d.mts +33 -0
- package/dist/utils/gas.d.mts.map +1 -0
- package/dist/utils/gas.mjs +82 -0
- package/dist/utils/gas.mjs.map +1 -0
- package/dist/utils/quotes.cjs +168 -0
- package/dist/utils/quotes.cjs.map +1 -0
- package/dist/utils/quotes.d.cts +21 -0
- package/dist/utils/quotes.d.cts.map +1 -0
- package/dist/utils/quotes.d.mts +21 -0
- package/dist/utils/quotes.d.mts.map +1 -0
- package/dist/utils/quotes.mjs +163 -0
- package/dist/utils/quotes.mjs.map +1 -0
- package/dist/utils/required-tokens.cjs +179 -0
- package/dist/utils/required-tokens.cjs.map +1 -0
- package/dist/utils/required-tokens.d.cts +11 -0
- package/dist/utils/required-tokens.d.cts.map +1 -0
- package/dist/utils/required-tokens.d.mts +11 -0
- package/dist/utils/required-tokens.d.mts.map +1 -0
- package/dist/utils/required-tokens.mjs +175 -0
- package/dist/utils/required-tokens.mjs.map +1 -0
- package/dist/utils/source-amounts.cjs +72 -0
- package/dist/utils/source-amounts.cjs.map +1 -0
- package/dist/utils/source-amounts.d.cts +11 -0
- package/dist/utils/source-amounts.d.cts.map +1 -0
- package/dist/utils/source-amounts.d.mts +11 -0
- package/dist/utils/source-amounts.d.mts.map +1 -0
- package/dist/utils/source-amounts.mjs +68 -0
- package/dist/utils/source-amounts.mjs.map +1 -0
- package/dist/utils/strategy.cjs +39 -0
- package/dist/utils/strategy.cjs.map +1 -0
- package/dist/utils/strategy.d.cts +19 -0
- package/dist/utils/strategy.d.cts.map +1 -0
- package/dist/utils/strategy.d.mts +19 -0
- package/dist/utils/strategy.d.mts.map +1 -0
- package/dist/utils/strategy.mjs +34 -0
- package/dist/utils/strategy.mjs.map +1 -0
- package/dist/utils/token.cjs +161 -0
- package/dist/utils/token.cjs.map +1 -0
- package/dist/utils/token.d.cts +53 -0
- package/dist/utils/token.d.cts.map +1 -0
- package/dist/utils/token.d.mts +53 -0
- package/dist/utils/token.d.mts.map +1 -0
- package/dist/utils/token.mjs +154 -0
- package/dist/utils/token.mjs.map +1 -0
- package/dist/utils/totals.cjs +69 -0
- package/dist/utils/totals.cjs.map +1 -0
- package/dist/utils/totals.d.cts +11 -0
- package/dist/utils/totals.d.cts.map +1 -0
- package/dist/utils/totals.d.mts +11 -0
- package/dist/utils/totals.d.mts.map +1 -0
- package/dist/utils/totals.mjs +65 -0
- package/dist/utils/totals.mjs.map +1 -0
- package/dist/utils/transaction.cjs +132 -0
- package/dist/utils/transaction.cjs.map +1 -0
- package/dist/utils/transaction.d.cts +42 -0
- package/dist/utils/transaction.d.cts.map +1 -0
- package/dist/utils/transaction.d.mts +42 -0
- package/dist/utils/transaction.d.mts.map +1 -0
- package/dist/utils/transaction.mjs +126 -0
- package/dist/utils/transaction.mjs.map +1 -0
- package/package.json +97 -0
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { TransactionMeta } from "@metamask/transaction-controller";
|
|
2
|
+
import type { TransactionPayControllerMessenger, TransactionPayRequiredToken } from "../types.mjs";
|
|
3
|
+
/**
|
|
4
|
+
* Parse required tokens from a transaction.
|
|
5
|
+
*
|
|
6
|
+
* @param transaction - Transaction metadata.
|
|
7
|
+
* @param messenger - Controller messenger.
|
|
8
|
+
* @returns An array of required tokens.
|
|
9
|
+
*/
|
|
10
|
+
export declare function parseRequiredTokens(transaction: TransactionMeta, messenger: TransactionPayControllerMessenger): TransactionPayRequiredToken[];
|
|
11
|
+
//# sourceMappingURL=required-tokens.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"required-tokens.d.mts","sourceRoot":"","sources":["../../src/utils/required-tokens.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,eAAe,EAAE,yCAAyC;AAUxE,OAAO,KAAK,EAEV,iCAAiC,EACjC,2BAA2B,EAC5B,qBAAiB;AAIlB;;;;;;GAMG;AACH,wBAAgB,mBAAmB,CACjC,WAAW,EAAE,eAAe,EAC5B,SAAS,EAAE,iCAAiC,GAC3C,2BAA2B,EAAE,CAK/B"}
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import { Interface } from "@ethersproject/abi";
|
|
2
|
+
import { toHex } from "@metamask/controller-utils";
|
|
3
|
+
import { abiERC20 } from "@metamask/metamask-eth-abis";
|
|
4
|
+
import { add0x } from "@metamask/utils";
|
|
5
|
+
import { BigNumber } from "bignumber.js";
|
|
6
|
+
import { getNativeToken, getTokenBalance, getTokenFiatRate, getTokenInfo } from "./token.mjs";
|
|
7
|
+
const FOUR_BYTE_TOKEN_TRANSFER = '0xa9059cbb';
|
|
8
|
+
/**
|
|
9
|
+
* Parse required tokens from a transaction.
|
|
10
|
+
*
|
|
11
|
+
* @param transaction - Transaction metadata.
|
|
12
|
+
* @param messenger - Controller messenger.
|
|
13
|
+
* @returns An array of required tokens.
|
|
14
|
+
*/
|
|
15
|
+
export function parseRequiredTokens(transaction, messenger) {
|
|
16
|
+
return [
|
|
17
|
+
getTokenTransferToken(transaction, messenger),
|
|
18
|
+
getGasFeeToken(transaction, messenger),
|
|
19
|
+
].filter(Boolean);
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Parse a required token from a token transfer.
|
|
23
|
+
*
|
|
24
|
+
* @param transaction - Transaction metadata.
|
|
25
|
+
* @param messenger - Controller messenger.
|
|
26
|
+
* @returns The required token or undefined if the transaction is not a token transfer.
|
|
27
|
+
*/
|
|
28
|
+
function getTokenTransferToken(transaction, messenger) {
|
|
29
|
+
const { data, to } = getTokenTransferData(transaction) ?? {};
|
|
30
|
+
if (!to || !data) {
|
|
31
|
+
return undefined;
|
|
32
|
+
}
|
|
33
|
+
let transferAmount;
|
|
34
|
+
try {
|
|
35
|
+
const result = new Interface(abiERC20).decodeFunctionData('transfer', data);
|
|
36
|
+
transferAmount = toHex(result._value);
|
|
37
|
+
}
|
|
38
|
+
catch {
|
|
39
|
+
// Intentionally empty
|
|
40
|
+
}
|
|
41
|
+
if (transferAmount === undefined) {
|
|
42
|
+
return undefined;
|
|
43
|
+
}
|
|
44
|
+
return buildRequiredToken(transaction, to, transferAmount, messenger);
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Get the gas fee token required for a transaction.
|
|
48
|
+
*
|
|
49
|
+
* @param transaction - Transaction metadata.
|
|
50
|
+
* @param messenger - Controller messenger.
|
|
51
|
+
* @returns The gas fee token or undefined if it could not be determined.
|
|
52
|
+
*/
|
|
53
|
+
function getGasFeeToken(transaction, messenger) {
|
|
54
|
+
const { chainId, txParams } = transaction;
|
|
55
|
+
const { gas, maxFeePerGas } = txParams;
|
|
56
|
+
const nativeTokenAddress = getNativeToken(chainId);
|
|
57
|
+
const maxGasCostRawHex = add0x(new BigNumber(gas ?? '0x0')
|
|
58
|
+
.multipliedBy(new BigNumber(maxFeePerGas ?? '0x0'))
|
|
59
|
+
.toString(16));
|
|
60
|
+
const token = buildRequiredToken(transaction, nativeTokenAddress, maxGasCostRawHex, messenger);
|
|
61
|
+
if (!token) {
|
|
62
|
+
return undefined;
|
|
63
|
+
}
|
|
64
|
+
const amountUsdValue = new BigNumber(token.amountUsd);
|
|
65
|
+
const hasBalance = new BigNumber(token.balanceRaw).isGreaterThanOrEqualTo(token.amountRaw);
|
|
66
|
+
if (hasBalance || amountUsdValue.isGreaterThanOrEqualTo(1)) {
|
|
67
|
+
return {
|
|
68
|
+
...token,
|
|
69
|
+
allowUnderMinimum: true,
|
|
70
|
+
skipIfBalance: true,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
const fiatRates = getTokenFiatRate(messenger, nativeTokenAddress, chainId);
|
|
74
|
+
const oneDollarRawHex = add0x(new BigNumber(1).dividedBy(fiatRates.usdRate).shiftedBy(18).toString(16));
|
|
75
|
+
const oneDollarToken = buildRequiredToken(transaction, nativeTokenAddress, oneDollarRawHex, messenger);
|
|
76
|
+
/* istanbul ignore next */
|
|
77
|
+
if (!oneDollarToken) {
|
|
78
|
+
return undefined;
|
|
79
|
+
}
|
|
80
|
+
return {
|
|
81
|
+
...oneDollarToken,
|
|
82
|
+
allowUnderMinimum: true,
|
|
83
|
+
skipIfBalance: true,
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Get the full token properties for a specific token and amount.
|
|
88
|
+
*
|
|
89
|
+
* @param transaction - Transaction metadata.
|
|
90
|
+
* @param tokenAddress - Token address.
|
|
91
|
+
* @param amountRawHex - Raw token amount in hexadecimal format.
|
|
92
|
+
* @param messenger - Controller messenger.
|
|
93
|
+
* @returns The full token properties or undefined if the token data could not be retrieved.
|
|
94
|
+
*/
|
|
95
|
+
function buildRequiredToken(transaction, tokenAddress, amountRawHex, messenger) {
|
|
96
|
+
const { chainId, txParams } = transaction;
|
|
97
|
+
const from = txParams.from;
|
|
98
|
+
const { decimals: tokenDecimals, symbol } = getTokenInfo(messenger, tokenAddress, chainId) ?? {};
|
|
99
|
+
const fiatRates = getTokenFiatRate(messenger, tokenAddress, chainId);
|
|
100
|
+
const tokenBalance = getTokenBalance(messenger, from, chainId, tokenAddress);
|
|
101
|
+
if (tokenDecimals === undefined || !symbol || fiatRates === undefined) {
|
|
102
|
+
return undefined;
|
|
103
|
+
}
|
|
104
|
+
const { amountHuman: balanceHuman, amountRaw: balanceRaw, amountFiat: balanceFiat, amountUsd: balanceUsd, } = calculateAmounts(tokenBalance, tokenDecimals, fiatRates);
|
|
105
|
+
const { amountHuman, amountRaw, amountFiat, amountUsd } = calculateAmounts(amountRawHex, tokenDecimals, fiatRates);
|
|
106
|
+
return {
|
|
107
|
+
address: tokenAddress,
|
|
108
|
+
allowUnderMinimum: false,
|
|
109
|
+
amountFiat,
|
|
110
|
+
amountHuman,
|
|
111
|
+
amountRaw,
|
|
112
|
+
amountUsd,
|
|
113
|
+
balanceFiat,
|
|
114
|
+
balanceHuman,
|
|
115
|
+
balanceRaw,
|
|
116
|
+
balanceUsd,
|
|
117
|
+
chainId,
|
|
118
|
+
decimals: tokenDecimals,
|
|
119
|
+
skipIfBalance: false,
|
|
120
|
+
symbol,
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Calculates the various amount representations for a token value.
|
|
125
|
+
*
|
|
126
|
+
* @param amountRawInput - Raw amount.
|
|
127
|
+
* @param decimals - Number of decimals for the token.
|
|
128
|
+
* @param fiatRates - Fiat rates for the token.
|
|
129
|
+
* @returns Object containing amount in fiat, human-readable, raw, and USD formats.
|
|
130
|
+
*/
|
|
131
|
+
function calculateAmounts(amountRawInput, decimals, fiatRates) {
|
|
132
|
+
const amountRawValue = new BigNumber(amountRawInput);
|
|
133
|
+
const amountHumanValue = amountRawValue.shiftedBy(-decimals);
|
|
134
|
+
const amountFiat = amountHumanValue
|
|
135
|
+
.multipliedBy(fiatRates.fiatRate)
|
|
136
|
+
.toString(10);
|
|
137
|
+
const amountUsd = amountHumanValue
|
|
138
|
+
.multipliedBy(fiatRates.usdRate)
|
|
139
|
+
.toString(10);
|
|
140
|
+
const amountRaw = amountRawValue.toFixed(0);
|
|
141
|
+
const amountHuman = amountHumanValue.toString(10);
|
|
142
|
+
return {
|
|
143
|
+
amountFiat,
|
|
144
|
+
amountHuman,
|
|
145
|
+
amountRaw,
|
|
146
|
+
amountUsd,
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Find token transfer data in a transaction.
|
|
151
|
+
*
|
|
152
|
+
* @param transactionMeta - Transaction metadata.
|
|
153
|
+
* @returns - Token transfer data or undefined if not found.
|
|
154
|
+
*/
|
|
155
|
+
function getTokenTransferData(transactionMeta) {
|
|
156
|
+
const { nestedTransactions, txParams } = transactionMeta;
|
|
157
|
+
const { data: singleData } = txParams;
|
|
158
|
+
const singleTo = txParams?.to;
|
|
159
|
+
if (singleData?.startsWith(FOUR_BYTE_TOKEN_TRANSFER) && singleTo) {
|
|
160
|
+
return { data: singleData, to: singleTo, index: undefined };
|
|
161
|
+
}
|
|
162
|
+
const nestedCallIndex = nestedTransactions?.findIndex((call) => call.data?.startsWith(FOUR_BYTE_TOKEN_TRANSFER));
|
|
163
|
+
const nestedCall = nestedCallIndex !== undefined
|
|
164
|
+
? nestedTransactions?.[nestedCallIndex]
|
|
165
|
+
: undefined;
|
|
166
|
+
if (nestedCall?.data && nestedCall.to) {
|
|
167
|
+
return {
|
|
168
|
+
data: nestedCall.data,
|
|
169
|
+
to: nestedCall.to,
|
|
170
|
+
index: nestedCallIndex,
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
return undefined;
|
|
174
|
+
}
|
|
175
|
+
//# sourceMappingURL=required-tokens.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"required-tokens.mjs","sourceRoot":"","sources":["../../src/utils/required-tokens.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,2BAA2B;AAC/C,OAAO,EAAE,KAAK,EAAE,mCAAmC;AACnD,OAAO,EAAE,QAAQ,EAAE,oCAAoC;AAEvD,OAAO,EAAE,KAAK,EAAY,wBAAwB;AAClD,OAAO,EAAE,SAAS,EAAE,qBAAqB;AAEzC,OAAO,EACL,cAAc,EACd,eAAe,EACf,gBAAgB,EAChB,YAAY,EACb,oBAAgB;AAOjB,MAAM,wBAAwB,GAAG,YAAY,CAAC;AAE9C;;;;;;GAMG;AACH,MAAM,UAAU,mBAAmB,CACjC,WAA4B,EAC5B,SAA4C;IAE5C,OAAO;QACL,qBAAqB,CAAC,WAAW,EAAE,SAAS,CAAC;QAC7C,cAAc,CAAC,WAAW,EAAE,SAAS,CAAC;KACvC,CAAC,MAAM,CAAC,OAAO,CAAkC,CAAC;AACrD,CAAC;AAED;;;;;;GAMG;AACH,SAAS,qBAAqB,CAC5B,WAA4B,EAC5B,SAA4C;IAE5C,MAAM,EAAE,IAAI,EAAE,EAAE,EAAE,GAAG,oBAAoB,CAAC,WAAW,CAAC,IAAI,EAAE,CAAC;IAE7D,IAAI,CAAC,EAAE,IAAI,CAAC,IAAI,EAAE;QAChB,OAAO,SAAS,CAAC;KAClB;IAED,IAAI,cAA+B,CAA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SAAS,oBAAoB,CAAC,eAAgC;IAO5D,MAAM,EAAE,kBAAkB,EAAE,QAAQ,EAAE,GAAG,eAAe,CAAC;IACzD,MAAM,EAAE,IAAI,EAAE,UAAU,EAAE,GAAG,QAAQ,CAAC;IACtC,MAAM,QAAQ,GAAG,QAAQ,EAAE,EAAqB,CAAC;IAEjD,IAAI,UAAU,EAAE,UAAU,CAAC,wBAAwB,CAAC,IAAI,QAAQ,EAAE;QAChE,OAAO,EAAE,IAAI,EAAE,UAAiB,EAAE,EAAE,EAAE,QAAQ,EAAE,KAAK,EAAE,SAAS,EAAE,CAAC;KACpE;IAED,MAAM,eAAe,GAAG,kBAAkB,EAAE,SAAS,CAAC,CAAC,IAAI,EAAE,EAAE,CAC7D,IAAI,CAAC,IAAI,EAAE,UAAU,CAAC,wBAAwB,CAAC,CAChD,CAAC;IAEF,MAAM,UAAU,GACd,eAAe,KAAK,SAAS;QAC3B,CAAC,CAAC,kBAAkB,EAAE,CAAC,eAAe,CAAC;QACvC,CAAC,CAAC,SAAS,CAAC;IAEhB,IAAI,UAAU,EAAE,IAAI,IAAI,UAAU,CAAC,EAAE,EAAE;QACrC,OAAO;YACL,IAAI,EAAE,UAAU,CAAC,IAAI;YACrB,EAAE,EAAE,UAAU,CAAC,EAAE;YACjB,KAAK,EAAE,eAAe;SACvB,CAAC;KACH;IAED,OAAO,SAAS,CAAC;AACnB,CAAC","sourcesContent":["import { Interface } from '@ethersproject/abi';\nimport { toHex } from '@metamask/controller-utils';\nimport { abiERC20 } from '@metamask/metamask-eth-abis';\nimport type { TransactionMeta } from '@metamask/transaction-controller';\nimport { add0x, type Hex } from '@metamask/utils';\nimport { BigNumber } from 'bignumber.js';\n\nimport {\n getNativeToken,\n getTokenBalance,\n getTokenFiatRate,\n getTokenInfo,\n} from './token';\nimport type {\n FiatRates,\n TransactionPayControllerMessenger,\n TransactionPayRequiredToken,\n} from '../types';\n\nconst FOUR_BYTE_TOKEN_TRANSFER = '0xa9059cbb';\n\n/**\n * Parse required tokens from a transaction.\n *\n * @param transaction - Transaction metadata.\n * @param messenger - Controller messenger.\n * @returns An array of required tokens.\n */\nexport function parseRequiredTokens(\n transaction: TransactionMeta,\n messenger: TransactionPayControllerMessenger,\n): TransactionPayRequiredToken[] {\n return [\n getTokenTransferToken(transaction, messenger),\n getGasFeeToken(transaction, messenger),\n ].filter(Boolean) as TransactionPayRequiredToken[];\n}\n\n/**\n * Parse a required token from a token transfer.\n *\n * @param transaction - Transaction metadata.\n * @param messenger - Controller messenger.\n * @returns The required token or undefined if the transaction is not a token transfer.\n */\nfunction getTokenTransferToken(\n transaction: TransactionMeta,\n messenger: TransactionPayControllerMessenger,\n): TransactionPayRequiredToken | undefined {\n const { data, to } = getTokenTransferData(transaction) ?? {};\n\n if (!to || !data) {\n return undefined;\n }\n\n let transferAmount: Hex | undefined;\n\n try {\n const result = new Interface(abiERC20).decodeFunctionData('transfer', data);\n transferAmount = toHex(result._value);\n } catch {\n // Intentionally empty\n }\n\n if (transferAmount === undefined) {\n return undefined;\n }\n\n return buildRequiredToken(transaction, to, transferAmount, messenger);\n}\n\n/**\n * Get the gas fee token required for a transaction.\n *\n * @param transaction - Transaction metadata.\n * @param messenger - Controller messenger.\n * @returns The gas fee token or undefined if it could not be determined.\n */\nfunction getGasFeeToken(\n transaction: TransactionMeta,\n messenger: TransactionPayControllerMessenger,\n): TransactionPayRequiredToken | undefined {\n const { chainId, txParams } = transaction;\n const { gas, maxFeePerGas } = txParams;\n const nativeTokenAddress = getNativeToken(chainId);\n\n const maxGasCostRawHex = add0x(\n new BigNumber(gas ?? '0x0')\n .multipliedBy(new BigNumber(maxFeePerGas ?? '0x0'))\n .toString(16),\n );\n\n const token = buildRequiredToken(\n transaction,\n nativeTokenAddress,\n maxGasCostRawHex,\n messenger,\n );\n\n if (!token) {\n return undefined;\n }\n\n const amountUsdValue = new BigNumber(token.amountUsd);\n\n const hasBalance = new BigNumber(token.balanceRaw).isGreaterThanOrEqualTo(\n token.amountRaw,\n );\n\n if (hasBalance || amountUsdValue.isGreaterThanOrEqualTo(1)) {\n return {\n ...token,\n allowUnderMinimum: true,\n skipIfBalance: true,\n };\n }\n\n const fiatRates = getTokenFiatRate(\n messenger,\n nativeTokenAddress,\n chainId,\n ) as FiatRates;\n\n const oneDollarRawHex = add0x(\n new BigNumber(1).dividedBy(fiatRates.usdRate).shiftedBy(18).toString(16),\n );\n\n const oneDollarToken = buildRequiredToken(\n transaction,\n nativeTokenAddress,\n oneDollarRawHex,\n messenger,\n );\n\n /* istanbul ignore next */\n if (!oneDollarToken) {\n return undefined;\n }\n\n return {\n ...oneDollarToken,\n allowUnderMinimum: true,\n skipIfBalance: true,\n };\n}\n\n/**\n * Get the full token properties for a specific token and amount.\n *\n * @param transaction - Transaction metadata.\n * @param tokenAddress - Token address.\n * @param amountRawHex - Raw token amount in hexadecimal format.\n * @param messenger - Controller messenger.\n * @returns The full token properties or undefined if the token data could not be retrieved.\n */\nfunction buildRequiredToken(\n transaction: TransactionMeta,\n tokenAddress: Hex,\n amountRawHex: Hex,\n messenger: TransactionPayControllerMessenger,\n): TransactionPayRequiredToken | undefined {\n const { chainId, txParams } = transaction;\n const from = txParams.from as Hex;\n\n const { decimals: tokenDecimals, symbol } =\n getTokenInfo(messenger, tokenAddress, chainId) ?? {};\n\n const fiatRates = getTokenFiatRate(messenger, tokenAddress, chainId);\n const tokenBalance = getTokenBalance(messenger, from, chainId, tokenAddress);\n\n if (tokenDecimals === undefined || !symbol || fiatRates === undefined) {\n return undefined;\n }\n\n const {\n amountHuman: balanceHuman,\n amountRaw: balanceRaw,\n amountFiat: balanceFiat,\n amountUsd: balanceUsd,\n } = calculateAmounts(tokenBalance, tokenDecimals, fiatRates);\n\n const { amountHuman, amountRaw, amountFiat, amountUsd } = calculateAmounts(\n amountRawHex,\n tokenDecimals,\n fiatRates,\n );\n\n return {\n address: tokenAddress,\n allowUnderMinimum: false,\n amountFiat,\n amountHuman,\n amountRaw,\n amountUsd,\n balanceFiat,\n balanceHuman,\n balanceRaw,\n balanceUsd,\n chainId,\n decimals: tokenDecimals,\n skipIfBalance: false,\n symbol,\n };\n}\n\n/**\n * Calculates the various amount representations for a token value.\n *\n * @param amountRawInput - Raw amount.\n * @param decimals - Number of decimals for the token.\n * @param fiatRates - Fiat rates for the token.\n * @returns Object containing amount in fiat, human-readable, raw, and USD formats.\n */\nfunction calculateAmounts(\n amountRawInput: BigNumber.Value,\n decimals: number,\n fiatRates: FiatRates,\n) {\n const amountRawValue = new BigNumber(amountRawInput);\n const amountHumanValue = amountRawValue.shiftedBy(-decimals);\n\n const amountFiat = amountHumanValue\n .multipliedBy(fiatRates.fiatRate)\n .toString(10);\n\n const amountUsd = amountHumanValue\n .multipliedBy(fiatRates.usdRate)\n .toString(10);\n\n const amountRaw = amountRawValue.toFixed(0);\n const amountHuman = amountHumanValue.toString(10);\n\n return {\n amountFiat,\n amountHuman,\n amountRaw,\n amountUsd,\n };\n}\n\n/**\n * Find token transfer data in a transaction.\n *\n * @param transactionMeta - Transaction metadata.\n * @returns - Token transfer data or undefined if not found.\n */\nfunction getTokenTransferData(transactionMeta: TransactionMeta):\n | {\n data: Hex;\n to: Hex;\n index?: number;\n }\n | undefined {\n const { nestedTransactions, txParams } = transactionMeta;\n const { data: singleData } = txParams;\n const singleTo = txParams?.to as Hex | undefined;\n\n if (singleData?.startsWith(FOUR_BYTE_TOKEN_TRANSFER) && singleTo) {\n return { data: singleData as Hex, to: singleTo, index: undefined };\n }\n\n const nestedCallIndex = nestedTransactions?.findIndex((call) =>\n call.data?.startsWith(FOUR_BYTE_TOKEN_TRANSFER),\n );\n\n const nestedCall =\n nestedCallIndex !== undefined\n ? nestedTransactions?.[nestedCallIndex]\n : undefined;\n\n if (nestedCall?.data && nestedCall.to) {\n return {\n data: nestedCall.data,\n to: nestedCall.to,\n index: nestedCallIndex,\n };\n }\n\n return undefined;\n}\n"]}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.updateSourceAmounts = void 0;
|
|
4
|
+
const utils_1 = require("@metamask/utils");
|
|
5
|
+
const bignumber_js_1 = require("bignumber.js");
|
|
6
|
+
const token_1 = require("./token.cjs");
|
|
7
|
+
const logger_1 = require("../logger.cjs");
|
|
8
|
+
const log = (0, utils_1.createModuleLogger)(logger_1.projectLogger, 'source-amounts');
|
|
9
|
+
/**
|
|
10
|
+
* Update the source amounts for a transaction.
|
|
11
|
+
*
|
|
12
|
+
* @param transactionId - ID of the transaction to update.
|
|
13
|
+
* @param transactionData - Existing transaction data.
|
|
14
|
+
* @param messenger - Controller messenger.
|
|
15
|
+
*/
|
|
16
|
+
function updateSourceAmounts(transactionId, transactionData, messenger) {
|
|
17
|
+
if (!transactionData) {
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
const { paymentToken, tokens } = transactionData;
|
|
21
|
+
if (!tokens.length || !paymentToken) {
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
const sourceAmounts = tokens
|
|
25
|
+
.map((t) => calculateSourceAmount(paymentToken, t, messenger))
|
|
26
|
+
.filter(Boolean);
|
|
27
|
+
log('Updated source amounts', { transactionId, sourceAmounts });
|
|
28
|
+
transactionData.sourceAmounts = sourceAmounts;
|
|
29
|
+
}
|
|
30
|
+
exports.updateSourceAmounts = updateSourceAmounts;
|
|
31
|
+
/**
|
|
32
|
+
* Calculate the required source amount for a payment token to cover a target token.
|
|
33
|
+
*
|
|
34
|
+
* @param paymentToken - Selected payment token.
|
|
35
|
+
* @param token - Target token to cover.
|
|
36
|
+
* @param messenger - Controller messenger.
|
|
37
|
+
* @returns The source amount or undefined if calculation failed.
|
|
38
|
+
*/
|
|
39
|
+
function calculateSourceAmount(paymentToken, token, messenger) {
|
|
40
|
+
const paymentTokenFiatRate = (0, token_1.getTokenFiatRate)(messenger, paymentToken.address, paymentToken.chainId);
|
|
41
|
+
if (!paymentTokenFiatRate) {
|
|
42
|
+
return undefined;
|
|
43
|
+
}
|
|
44
|
+
const hasBalance = new bignumber_js_1.BigNumber(token.balanceRaw).gte(token.amountRaw);
|
|
45
|
+
const isSameTokenSelected = token.address.toLowerCase() === paymentToken.address.toLowerCase() &&
|
|
46
|
+
token.chainId === paymentToken.chainId;
|
|
47
|
+
if (token.skipIfBalance && hasBalance) {
|
|
48
|
+
log('Skipping token as sufficient balance', {
|
|
49
|
+
tokenAddress: token.address,
|
|
50
|
+
});
|
|
51
|
+
return undefined;
|
|
52
|
+
}
|
|
53
|
+
if (isSameTokenSelected) {
|
|
54
|
+
log('Skipping token as same as payment token');
|
|
55
|
+
return undefined;
|
|
56
|
+
}
|
|
57
|
+
const sourceAmountHumanValue = new bignumber_js_1.BigNumber(token.amountUsd).div(paymentTokenFiatRate.usdRate);
|
|
58
|
+
const sourceAmountHuman = sourceAmountHumanValue.toString(10);
|
|
59
|
+
const sourceAmountRaw = sourceAmountHumanValue
|
|
60
|
+
.shiftedBy(paymentToken.decimals)
|
|
61
|
+
.toFixed(0);
|
|
62
|
+
if (token.amountRaw === '0') {
|
|
63
|
+
log('Skipping token as zero amount', { tokenAddress: token.address });
|
|
64
|
+
return undefined;
|
|
65
|
+
}
|
|
66
|
+
return {
|
|
67
|
+
sourceAmountHuman,
|
|
68
|
+
sourceAmountRaw,
|
|
69
|
+
targetTokenAddress: token.address,
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
//# sourceMappingURL=source-amounts.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"source-amounts.cjs","sourceRoot":"","sources":["../../src/utils/source-amounts.ts"],"names":[],"mappings":";;;AAAA,2CAAqD;AACrD,+CAAyC;AAEzC,uCAA2C;AAK3C,0CAA0C;AAO1C,MAAM,GAAG,GAAG,IAAA,0BAAkB,EAAC,sBAAa,EAAE,gBAAgB,CAAC,CAAC;AAEhE;;;;;;GAMG;AACH,SAAgB,mBAAmB,CACjC,aAAqB,EACrB,eAA4C,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,EAAE;QACvB,GAAG,CAAC,yCAAyC,CAAC,CAAC;QAC/C,OAAO,SAAS,CAAC;KAClB;IAED,MAAM,sBAAsB,GAAG,IAAI,wBAAS,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC,GAAG,CAC/D,oBAAoB,CAAC,OAAO,CAC7B,CAAC;IAEF,MAAM,iBAAiB,GAAG,sBAAsB,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC;IAE9D,MAAM,eAAe,GAAG,sBAAsB;SAC3C,SAAS,CAAC,YAAY,CAAC,QAAQ,CAAC;SAChC,OAAO,CAAC,CAAC,CAAC,CAAC;IAEd,IAAI,KAAK,CAAC,SAAS,KAAK,GAAG,EAAE;QAC3B,GAAG,CAAC,+BAA+B,EAAE,EAAE,YAAY,EAAE,KAAK,CAAC,OAAO,EAAE,CAAC,CAAC;QACtE,OAAO,SAAS,CAAC;KAClB;IAED,OAAO;QACL,iBAAiB;QACjB,eAAe;QACf,kBAAkB,EAAE,KAAK,CAAC,OAAO;KAClC,CAAC;AACJ,CAAC","sourcesContent":["import { createModuleLogger } from '@metamask/utils';\nimport { BigNumber } from 'bignumber.js';\n\nimport { getTokenFiatRate } from './token';\nimport type {\n TransactionPayControllerMessenger,\n TransactionPaymentToken,\n} from '..';\nimport { projectLogger } from '../logger';\nimport type {\n TransactionPaySourceAmount,\n TransactionData,\n TransactionPayRequiredToken,\n} from '../types';\n\nconst log = createModuleLogger(projectLogger, 'source-amounts');\n\n/**\n * Update the source amounts for a transaction.\n *\n * @param transactionId - ID of the transaction to update.\n * @param transactionData - Existing transaction data.\n * @param messenger - Controller messenger.\n */\nexport function updateSourceAmounts(\n transactionId: string,\n transactionData: TransactionData | undefined,\n messenger: TransactionPayControllerMessenger,\n) {\n if (!transactionData) {\n return;\n }\n\n const { paymentToken, tokens } = transactionData;\n\n if (!tokens.length || !paymentToken) {\n return;\n }\n\n const sourceAmounts = tokens\n .map((t) => calculateSourceAmount(paymentToken, t, messenger))\n .filter(Boolean) as TransactionPaySourceAmount[];\n\n log('Updated source amounts', { transactionId, sourceAmounts });\n\n transactionData.sourceAmounts = sourceAmounts;\n}\n\n/**\n * Calculate the required source amount for a payment token to cover a target token.\n *\n * @param paymentToken - Selected payment token.\n * @param token - Target token to cover.\n * @param messenger - Controller messenger.\n * @returns The source amount or undefined if calculation failed.\n */\nfunction calculateSourceAmount(\n paymentToken: TransactionPaymentToken,\n token: TransactionPayRequiredToken,\n messenger: TransactionPayControllerMessenger,\n): TransactionPaySourceAmount | undefined {\n const paymentTokenFiatRate = getTokenFiatRate(\n messenger,\n paymentToken.address,\n paymentToken.chainId,\n );\n\n if (!paymentTokenFiatRate) {\n return undefined;\n }\n\n const hasBalance = new BigNumber(token.balanceRaw).gte(token.amountRaw);\n\n const isSameTokenSelected =\n token.address.toLowerCase() === paymentToken.address.toLowerCase() &&\n token.chainId === paymentToken.chainId;\n\n if (token.skipIfBalance && hasBalance) {\n log('Skipping token as sufficient balance', {\n tokenAddress: token.address,\n });\n return undefined;\n }\n\n if (isSameTokenSelected) {\n log('Skipping token as same as payment token');\n return undefined;\n }\n\n const sourceAmountHumanValue = new BigNumber(token.amountUsd).div(\n paymentTokenFiatRate.usdRate,\n );\n\n const sourceAmountHuman = sourceAmountHumanValue.toString(10);\n\n const sourceAmountRaw = sourceAmountHumanValue\n .shiftedBy(paymentToken.decimals)\n .toFixed(0);\n\n if (token.amountRaw === '0') {\n log('Skipping token as zero amount', { tokenAddress: token.address });\n return undefined;\n }\n\n return {\n sourceAmountHuman,\n sourceAmountRaw,\n targetTokenAddress: token.address,\n };\n}\n"]}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { TransactionPayControllerMessenger } from "../index.cjs";
|
|
2
|
+
import type { TransactionData } from "../types.cjs";
|
|
3
|
+
/**
|
|
4
|
+
* Update the source amounts for a transaction.
|
|
5
|
+
*
|
|
6
|
+
* @param transactionId - ID of the transaction to update.
|
|
7
|
+
* @param transactionData - Existing transaction data.
|
|
8
|
+
* @param messenger - Controller messenger.
|
|
9
|
+
*/
|
|
10
|
+
export declare function updateSourceAmounts(transactionId: string, transactionData: TransactionData | undefined, messenger: TransactionPayControllerMessenger): void;
|
|
11
|
+
//# sourceMappingURL=source-amounts.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"source-amounts.d.cts","sourceRoot":"","sources":["../../src/utils/source-amounts.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EACV,iCAAiC,EAElC,qBAAW;AAEZ,OAAO,KAAK,EAEV,eAAe,EAEhB,qBAAiB;AAIlB;;;;;;GAMG;AACH,wBAAgB,mBAAmB,CACjC,aAAa,EAAE,MAAM,EACrB,eAAe,EAAE,eAAe,GAAG,SAAS,EAC5C,SAAS,EAAE,iCAAiC,QAmB7C"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { TransactionPayControllerMessenger } from "../index.mjs";
|
|
2
|
+
import type { TransactionData } from "../types.mjs";
|
|
3
|
+
/**
|
|
4
|
+
* Update the source amounts for a transaction.
|
|
5
|
+
*
|
|
6
|
+
* @param transactionId - ID of the transaction to update.
|
|
7
|
+
* @param transactionData - Existing transaction data.
|
|
8
|
+
* @param messenger - Controller messenger.
|
|
9
|
+
*/
|
|
10
|
+
export declare function updateSourceAmounts(transactionId: string, transactionData: TransactionData | undefined, messenger: TransactionPayControllerMessenger): void;
|
|
11
|
+
//# sourceMappingURL=source-amounts.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"source-amounts.d.mts","sourceRoot":"","sources":["../../src/utils/source-amounts.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EACV,iCAAiC,EAElC,qBAAW;AAEZ,OAAO,KAAK,EAEV,eAAe,EAEhB,qBAAiB;AAIlB;;;;;;GAMG;AACH,wBAAgB,mBAAmB,CACjC,aAAa,EAAE,MAAM,EACrB,eAAe,EAAE,eAAe,GAAG,SAAS,EAC5C,SAAS,EAAE,iCAAiC,QAmB7C"}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { createModuleLogger } from "@metamask/utils";
|
|
2
|
+
import { BigNumber } from "bignumber.js";
|
|
3
|
+
import { getTokenFiatRate } from "./token.mjs";
|
|
4
|
+
import { projectLogger } from "../logger.mjs";
|
|
5
|
+
const log = createModuleLogger(projectLogger, 'source-amounts');
|
|
6
|
+
/**
|
|
7
|
+
* Update the source amounts for a transaction.
|
|
8
|
+
*
|
|
9
|
+
* @param transactionId - ID of the transaction to update.
|
|
10
|
+
* @param transactionData - Existing transaction data.
|
|
11
|
+
* @param messenger - Controller messenger.
|
|
12
|
+
*/
|
|
13
|
+
export function updateSourceAmounts(transactionId, transactionData, messenger) {
|
|
14
|
+
if (!transactionData) {
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
const { paymentToken, tokens } = transactionData;
|
|
18
|
+
if (!tokens.length || !paymentToken) {
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
const sourceAmounts = tokens
|
|
22
|
+
.map((t) => calculateSourceAmount(paymentToken, t, messenger))
|
|
23
|
+
.filter(Boolean);
|
|
24
|
+
log('Updated source amounts', { transactionId, sourceAmounts });
|
|
25
|
+
transactionData.sourceAmounts = sourceAmounts;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Calculate the required source amount for a payment token to cover a target token.
|
|
29
|
+
*
|
|
30
|
+
* @param paymentToken - Selected payment token.
|
|
31
|
+
* @param token - Target token to cover.
|
|
32
|
+
* @param messenger - Controller messenger.
|
|
33
|
+
* @returns The source amount or undefined if calculation failed.
|
|
34
|
+
*/
|
|
35
|
+
function calculateSourceAmount(paymentToken, token, messenger) {
|
|
36
|
+
const paymentTokenFiatRate = getTokenFiatRate(messenger, paymentToken.address, paymentToken.chainId);
|
|
37
|
+
if (!paymentTokenFiatRate) {
|
|
38
|
+
return undefined;
|
|
39
|
+
}
|
|
40
|
+
const hasBalance = new BigNumber(token.balanceRaw).gte(token.amountRaw);
|
|
41
|
+
const isSameTokenSelected = token.address.toLowerCase() === paymentToken.address.toLowerCase() &&
|
|
42
|
+
token.chainId === paymentToken.chainId;
|
|
43
|
+
if (token.skipIfBalance && hasBalance) {
|
|
44
|
+
log('Skipping token as sufficient balance', {
|
|
45
|
+
tokenAddress: token.address,
|
|
46
|
+
});
|
|
47
|
+
return undefined;
|
|
48
|
+
}
|
|
49
|
+
if (isSameTokenSelected) {
|
|
50
|
+
log('Skipping token as same as payment token');
|
|
51
|
+
return undefined;
|
|
52
|
+
}
|
|
53
|
+
const sourceAmountHumanValue = new BigNumber(token.amountUsd).div(paymentTokenFiatRate.usdRate);
|
|
54
|
+
const sourceAmountHuman = sourceAmountHumanValue.toString(10);
|
|
55
|
+
const sourceAmountRaw = sourceAmountHumanValue
|
|
56
|
+
.shiftedBy(paymentToken.decimals)
|
|
57
|
+
.toFixed(0);
|
|
58
|
+
if (token.amountRaw === '0') {
|
|
59
|
+
log('Skipping token as zero amount', { tokenAddress: token.address });
|
|
60
|
+
return undefined;
|
|
61
|
+
}
|
|
62
|
+
return {
|
|
63
|
+
sourceAmountHuman,
|
|
64
|
+
sourceAmountRaw,
|
|
65
|
+
targetTokenAddress: token.address,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
//# sourceMappingURL=source-amounts.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"source-amounts.mjs","sourceRoot":"","sources":["../../src/utils/source-amounts.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,wBAAwB;AACrD,OAAO,EAAE,SAAS,EAAE,qBAAqB;AAEzC,OAAO,EAAE,gBAAgB,EAAE,oBAAgB;AAK3C,OAAO,EAAE,aAAa,EAAE,sBAAkB;AAO1C,MAAM,GAAG,GAAG,kBAAkB,CAAC,aAAa,EAAE,gBAAgB,CAAC,CAAC;AAEhE;;;;;;GAMG;AACH,MAAM,UAAU,mBAAmB,CACjC,aAAqB,EACrB,eAA4C,EAC5C,S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C,CAAC,CAAC;QAC/C,OAAO,SAAS,CAAC;KAClB;IAED,MAAM,sBAAsB,GAAG,IAAI,SAAS,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC,GAAG,CAC/D,oBAAoB,CAAC,OAAO,CAC7B,CAAC;IAEF,MAAM,iBAAiB,GAAG,sBAAsB,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC;IAE9D,MAAM,eAAe,GAAG,sBAAsB;SAC3C,SAAS,CAAC,YAAY,CAAC,QAAQ,CAAC;SAChC,OAAO,CAAC,CAAC,CAAC,CAAC;IAEd,IAAI,KAAK,CAAC,SAAS,KAAK,GAAG,EAAE;QAC3B,GAAG,CAAC,+BAA+B,EAAE,EAAE,YAAY,EAAE,KAAK,CAAC,OAAO,EAAE,CAAC,CAAC;QACtE,OAAO,SAAS,CAAC;KAClB;IAED,OAAO;QACL,iBAAiB;QACjB,eAAe;QACf,kBAAkB,EAAE,KAAK,CAAC,OAAO;KAClC,CAAC;AACJ,CAAC","sourcesContent":["import { createModuleLogger } from '@metamask/utils';\nimport { BigNumber } from 'bignumber.js';\n\nimport { getTokenFiatRate } from './token';\nimport type {\n TransactionPayControllerMessenger,\n TransactionPaymentToken,\n} from '..';\nimport { projectLogger } from '../logger';\nimport type {\n TransactionPaySourceAmount,\n TransactionData,\n TransactionPayRequiredToken,\n} from '../types';\n\nconst log = createModuleLogger(projectLogger, 'source-amounts');\n\n/**\n * Update the source amounts for a transaction.\n *\n * @param transactionId - ID of the transaction to update.\n * @param transactionData - Existing transaction data.\n * @param messenger - Controller messenger.\n */\nexport function updateSourceAmounts(\n transactionId: string,\n transactionData: TransactionData | undefined,\n messenger: TransactionPayControllerMessenger,\n) {\n if (!transactionData) {\n return;\n }\n\n const { paymentToken, tokens } = transactionData;\n\n if (!tokens.length || !paymentToken) {\n return;\n }\n\n const sourceAmounts = tokens\n .map((t) => calculateSourceAmount(paymentToken, t, messenger))\n .filter(Boolean) as TransactionPaySourceAmount[];\n\n log('Updated source amounts', { transactionId, sourceAmounts });\n\n transactionData.sourceAmounts = sourceAmounts;\n}\n\n/**\n * Calculate the required source amount for a payment token to cover a target token.\n *\n * @param paymentToken - Selected payment token.\n * @param token - Target token to cover.\n * @param messenger - Controller messenger.\n * @returns The source amount or undefined if calculation failed.\n */\nfunction calculateSourceAmount(\n paymentToken: TransactionPaymentToken,\n token: TransactionPayRequiredToken,\n messenger: TransactionPayControllerMessenger,\n): TransactionPaySourceAmount | undefined {\n const paymentTokenFiatRate = getTokenFiatRate(\n messenger,\n paymentToken.address,\n paymentToken.chainId,\n );\n\n if (!paymentTokenFiatRate) {\n return undefined;\n }\n\n const hasBalance = new BigNumber(token.balanceRaw).gte(token.amountRaw);\n\n const isSameTokenSelected =\n token.address.toLowerCase() === paymentToken.address.toLowerCase() &&\n token.chainId === paymentToken.chainId;\n\n if (token.skipIfBalance && hasBalance) {\n log('Skipping token as sufficient balance', {\n tokenAddress: token.address,\n });\n return undefined;\n }\n\n if (isSameTokenSelected) {\n log('Skipping token as same as payment token');\n return undefined;\n }\n\n const sourceAmountHumanValue = new BigNumber(token.amountUsd).div(\n paymentTokenFiatRate.usdRate,\n );\n\n const sourceAmountHuman = sourceAmountHumanValue.toString(10);\n\n const sourceAmountRaw = sourceAmountHumanValue\n .shiftedBy(paymentToken.decimals)\n .toFixed(0);\n\n if (token.amountRaw === '0') {\n log('Skipping token as zero amount', { tokenAddress: token.address });\n return undefined;\n }\n\n return {\n sourceAmountHuman,\n sourceAmountRaw,\n targetTokenAddress: token.address,\n };\n}\n"]}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getStrategyByName = exports.getStrategy = void 0;
|
|
4
|
+
const constants_1 = require("../constants.cjs");
|
|
5
|
+
const BridgeStrategy_1 = require("../strategy/bridge/BridgeStrategy.cjs");
|
|
6
|
+
const RelayStrategy_1 = require("../strategy/relay/RelayStrategy.cjs");
|
|
7
|
+
const TestStrategy_1 = require("../strategy/test/TestStrategy.cjs");
|
|
8
|
+
/**
|
|
9
|
+
* Get the payment strategy instance.
|
|
10
|
+
*
|
|
11
|
+
* @param messenger - Controller messenger
|
|
12
|
+
* @param transaction - Transaction to get the strategy for.
|
|
13
|
+
* @returns The payment strategy instance.
|
|
14
|
+
*/
|
|
15
|
+
async function getStrategy(messenger, transaction) {
|
|
16
|
+
const strategyName = await messenger.call('TransactionPayController:getStrategy', transaction);
|
|
17
|
+
return getStrategyByName(strategyName);
|
|
18
|
+
}
|
|
19
|
+
exports.getStrategy = getStrategy;
|
|
20
|
+
/**
|
|
21
|
+
* Get strategy instance by name.
|
|
22
|
+
*
|
|
23
|
+
* @param strategyName - Strategy name.
|
|
24
|
+
* @returns - Strategy instance.
|
|
25
|
+
*/
|
|
26
|
+
function getStrategyByName(strategyName) {
|
|
27
|
+
switch (strategyName) {
|
|
28
|
+
case constants_1.TransactionPayStrategy.Bridge:
|
|
29
|
+
return new BridgeStrategy_1.BridgeStrategy();
|
|
30
|
+
case constants_1.TransactionPayStrategy.Relay:
|
|
31
|
+
return new RelayStrategy_1.RelayStrategy();
|
|
32
|
+
case constants_1.TransactionPayStrategy.Test:
|
|
33
|
+
return new TestStrategy_1.TestStrategy();
|
|
34
|
+
default:
|
|
35
|
+
throw new Error(`Unknown strategy: ${strategyName}`);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
exports.getStrategyByName = getStrategyByName;
|
|
39
|
+
//# sourceMappingURL=strategy.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"strategy.cjs","sourceRoot":"","sources":["../../src/utils/strategy.ts"],"names":[],"mappings":";;;AAEA,gDAAsD;AACtD,0EAAmE;AACnE,uEAAgE;AAChE,oEAA6D;AAG7D;;;;;;GAMG;AACI,KAAK,UAAU,WAAW,CAC/B,SAA4C,EAC5C,WAA4B;IAE5B,MAAM,YAAY,GAAG,MAAM,SAAS,CAAC,IAAI,CACvC,sCAAsC,EACtC,WAAW,CACZ,CAAC;IAEF,OAAO,iBAAiB,CAAC,YAAY,CAAC,CAAC;AACzC,CAAC;AAVD,kCAUC;AAED;;;;;GAKG;AACH,SAAgB,iBAAiB,CAC/B,YAAoC;IAEpC,QAAQ,YAAY,EAAE;QACpB,KAAK,kCAAsB,CAAC,MAAM;YAChC,OAAO,IAAI,+BAAc,EAAW,CAAC;QAEvC,KAAK,kCAAsB,CAAC,KAAK;YAC/B,OAAO,IAAI,6BAAa,EAAW,CAAC;QAEtC,KAAK,kCAAsB,CAAC,IAAI;YAC9B,OAAO,IAAI,2BAAY,EAAW,CAAC;QAErC;YACE,MAAM,IAAI,KAAK,CAAC,qBAAqB,YAAsB,EAAE,CAAC,CAAC;KAClE;AACH,CAAC;AAhBD,8CAgBC","sourcesContent":["import type { TransactionMeta } from '@metamask/transaction-controller';\n\nimport { TransactionPayStrategy } from '../constants';\nimport { BridgeStrategy } from '../strategy/bridge/BridgeStrategy';\nimport { RelayStrategy } from '../strategy/relay/RelayStrategy';\nimport { TestStrategy } from '../strategy/test/TestStrategy';\nimport type { PayStrategy, TransactionPayControllerMessenger } from '../types';\n\n/**\n * Get the payment strategy instance.\n *\n * @param messenger - Controller messenger\n * @param transaction - Transaction to get the strategy for.\n * @returns The payment strategy instance.\n */\nexport async function getStrategy(\n messenger: TransactionPayControllerMessenger,\n transaction: TransactionMeta,\n): Promise<PayStrategy<unknown>> {\n const strategyName = await messenger.call(\n 'TransactionPayController:getStrategy',\n transaction,\n );\n\n return getStrategyByName(strategyName);\n}\n\n/**\n * Get strategy instance by name.\n *\n * @param strategyName - Strategy name.\n * @returns - Strategy instance.\n */\nexport function getStrategyByName(\n strategyName: TransactionPayStrategy,\n): PayStrategy<unknown> {\n switch (strategyName) {\n case TransactionPayStrategy.Bridge:\n return new BridgeStrategy() as never;\n\n case TransactionPayStrategy.Relay:\n return new RelayStrategy() as never;\n\n case TransactionPayStrategy.Test:\n return new TestStrategy() as never;\n\n default:\n throw new Error(`Unknown strategy: ${strategyName as string}`);\n }\n}\n"]}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { TransactionMeta } from "@metamask/transaction-controller";
|
|
2
|
+
import { TransactionPayStrategy } from "../constants.cjs";
|
|
3
|
+
import type { PayStrategy, TransactionPayControllerMessenger } from "../types.cjs";
|
|
4
|
+
/**
|
|
5
|
+
* Get the payment strategy instance.
|
|
6
|
+
*
|
|
7
|
+
* @param messenger - Controller messenger
|
|
8
|
+
* @param transaction - Transaction to get the strategy for.
|
|
9
|
+
* @returns The payment strategy instance.
|
|
10
|
+
*/
|
|
11
|
+
export declare function getStrategy(messenger: TransactionPayControllerMessenger, transaction: TransactionMeta): Promise<PayStrategy<unknown>>;
|
|
12
|
+
/**
|
|
13
|
+
* Get strategy instance by name.
|
|
14
|
+
*
|
|
15
|
+
* @param strategyName - Strategy name.
|
|
16
|
+
* @returns - Strategy instance.
|
|
17
|
+
*/
|
|
18
|
+
export declare function getStrategyByName(strategyName: TransactionPayStrategy): PayStrategy<unknown>;
|
|
19
|
+
//# sourceMappingURL=strategy.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"strategy.d.cts","sourceRoot":"","sources":["../../src/utils/strategy.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,yCAAyC;AAExE,OAAO,EAAE,sBAAsB,EAAE,yBAAqB;AAItD,OAAO,KAAK,EAAE,WAAW,EAAE,iCAAiC,EAAE,qBAAiB;AAE/E;;;;;;GAMG;AACH,wBAAsB,WAAW,CAC/B,SAAS,EAAE,iCAAiC,EAC5C,WAAW,EAAE,eAAe,GAC3B,OAAO,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC,CAO/B;AAED;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAC/B,YAAY,EAAE,sBAAsB,GACnC,WAAW,CAAC,OAAO,CAAC,CActB"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { TransactionMeta } from "@metamask/transaction-controller";
|
|
2
|
+
import { TransactionPayStrategy } from "../constants.mjs";
|
|
3
|
+
import type { PayStrategy, TransactionPayControllerMessenger } from "../types.mjs";
|
|
4
|
+
/**
|
|
5
|
+
* Get the payment strategy instance.
|
|
6
|
+
*
|
|
7
|
+
* @param messenger - Controller messenger
|
|
8
|
+
* @param transaction - Transaction to get the strategy for.
|
|
9
|
+
* @returns The payment strategy instance.
|
|
10
|
+
*/
|
|
11
|
+
export declare function getStrategy(messenger: TransactionPayControllerMessenger, transaction: TransactionMeta): Promise<PayStrategy<unknown>>;
|
|
12
|
+
/**
|
|
13
|
+
* Get strategy instance by name.
|
|
14
|
+
*
|
|
15
|
+
* @param strategyName - Strategy name.
|
|
16
|
+
* @returns - Strategy instance.
|
|
17
|
+
*/
|
|
18
|
+
export declare function getStrategyByName(strategyName: TransactionPayStrategy): PayStrategy<unknown>;
|
|
19
|
+
//# sourceMappingURL=strategy.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"strategy.d.mts","sourceRoot":"","sources":["../../src/utils/strategy.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,yCAAyC;AAExE,OAAO,EAAE,sBAAsB,EAAE,yBAAqB;AAItD,OAAO,KAAK,EAAE,WAAW,EAAE,iCAAiC,EAAE,qBAAiB;AAE/E;;;;;;GAMG;AACH,wBAAsB,WAAW,CAC/B,SAAS,EAAE,iCAAiC,EAC5C,WAAW,EAAE,eAAe,GAC3B,OAAO,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC,CAO/B;AAED;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAC/B,YAAY,EAAE,sBAAsB,GACnC,WAAW,CAAC,OAAO,CAAC,CActB"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { TransactionPayStrategy } from "../constants.mjs";
|
|
2
|
+
import { BridgeStrategy } from "../strategy/bridge/BridgeStrategy.mjs";
|
|
3
|
+
import { RelayStrategy } from "../strategy/relay/RelayStrategy.mjs";
|
|
4
|
+
import { TestStrategy } from "../strategy/test/TestStrategy.mjs";
|
|
5
|
+
/**
|
|
6
|
+
* Get the payment strategy instance.
|
|
7
|
+
*
|
|
8
|
+
* @param messenger - Controller messenger
|
|
9
|
+
* @param transaction - Transaction to get the strategy for.
|
|
10
|
+
* @returns The payment strategy instance.
|
|
11
|
+
*/
|
|
12
|
+
export async function getStrategy(messenger, transaction) {
|
|
13
|
+
const strategyName = await messenger.call('TransactionPayController:getStrategy', transaction);
|
|
14
|
+
return getStrategyByName(strategyName);
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Get strategy instance by name.
|
|
18
|
+
*
|
|
19
|
+
* @param strategyName - Strategy name.
|
|
20
|
+
* @returns - Strategy instance.
|
|
21
|
+
*/
|
|
22
|
+
export function getStrategyByName(strategyName) {
|
|
23
|
+
switch (strategyName) {
|
|
24
|
+
case TransactionPayStrategy.Bridge:
|
|
25
|
+
return new BridgeStrategy();
|
|
26
|
+
case TransactionPayStrategy.Relay:
|
|
27
|
+
return new RelayStrategy();
|
|
28
|
+
case TransactionPayStrategy.Test:
|
|
29
|
+
return new TestStrategy();
|
|
30
|
+
default:
|
|
31
|
+
throw new Error(`Unknown strategy: ${strategyName}`);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=strategy.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"strategy.mjs","sourceRoot":"","sources":["../../src/utils/strategy.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,sBAAsB,EAAE,yBAAqB;AACtD,OAAO,EAAE,cAAc,EAAE,8CAA0C;AACnE,OAAO,EAAE,aAAa,EAAE,4CAAwC;AAChE,OAAO,EAAE,YAAY,EAAE,0CAAsC;AAG7D;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,WAAW,CAC/B,SAA4C,EAC5C,WAA4B;IAE5B,MAAM,YAAY,GAAG,MAAM,SAAS,CAAC,IAAI,CACvC,sCAAsC,EACtC,WAAW,CACZ,CAAC;IAEF,OAAO,iBAAiB,CAAC,YAAY,CAAC,CAAC;AACzC,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,iBAAiB,CAC/B,YAAoC;IAEpC,QAAQ,YAAY,EAAE;QACpB,KAAK,sBAAsB,CAAC,MAAM;YAChC,OAAO,IAAI,cAAc,EAAW,CAAC;QAEvC,KAAK,sBAAsB,CAAC,KAAK;YAC/B,OAAO,IAAI,aAAa,EAAW,CAAC;QAEtC,KAAK,sBAAsB,CAAC,IAAI;YAC9B,OAAO,IAAI,YAAY,EAAW,CAAC;QAErC;YACE,MAAM,IAAI,KAAK,CAAC,qBAAqB,YAAsB,EAAE,CAAC,CAAC;KAClE;AACH,CAAC","sourcesContent":["import type { TransactionMeta } from '@metamask/transaction-controller';\n\nimport { TransactionPayStrategy } from '../constants';\nimport { BridgeStrategy } from '../strategy/bridge/BridgeStrategy';\nimport { RelayStrategy } from '../strategy/relay/RelayStrategy';\nimport { TestStrategy } from '../strategy/test/TestStrategy';\nimport type { PayStrategy, TransactionPayControllerMessenger } from '../types';\n\n/**\n * Get the payment strategy instance.\n *\n * @param messenger - Controller messenger\n * @param transaction - Transaction to get the strategy for.\n * @returns The payment strategy instance.\n */\nexport async function getStrategy(\n messenger: TransactionPayControllerMessenger,\n transaction: TransactionMeta,\n): Promise<PayStrategy<unknown>> {\n const strategyName = await messenger.call(\n 'TransactionPayController:getStrategy',\n transaction,\n );\n\n return getStrategyByName(strategyName);\n}\n\n/**\n * Get strategy instance by name.\n *\n * @param strategyName - Strategy name.\n * @returns - Strategy instance.\n */\nexport function getStrategyByName(\n strategyName: TransactionPayStrategy,\n): PayStrategy<unknown> {\n switch (strategyName) {\n case TransactionPayStrategy.Bridge:\n return new BridgeStrategy() as never;\n\n case TransactionPayStrategy.Relay:\n return new RelayStrategy() as never;\n\n case TransactionPayStrategy.Test:\n return new TestStrategy() as never;\n\n default:\n throw new Error(`Unknown strategy: ${strategyName as string}`);\n }\n}\n"]}
|