@exodus/web3-ethereum-utils 3.30.0 → 3.31.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.
@@ -1,3 +1,3 @@
1
1
  import type { EthAggregatedTransactionSimulationResult, EthSimulateTransactionParams } from './types.js';
2
2
  import type { CreateSimulateTransactionsParams } from '@exodus/web3-types';
3
- export declare const createSimulateTransactions: ({ apiEndpoint, headers }: Required<CreateSimulateTransactionsParams>) => ({ transactions, asset, origin, overrideApiEndpoint, shouldSimulate, }: EthSimulateTransactionParams) => Promise<EthAggregatedTransactionSimulationResult>;
3
+ export declare const createSimulateTransactions: ({ apiEndpoint, headers }: Required<CreateSimulateTransactionsParams>) => ({ transactions, asset, origin, overrideApiEndpoint, }: EthSimulateTransactionParams) => Promise<EthAggregatedTransactionSimulationResult>;
@@ -1,9 +1,9 @@
1
1
  import { createEmptySimulationResult } from '@exodus/web3-utils';
2
2
  import { estimateFee } from './simulation/estimateFee.js';
3
- import { simulateTransactions } from './simulation/simulateTransactions.js';
3
+ import { simulateTransactions, } from './simulation/simulateTransactions.js';
4
4
  import { tryEstimatingChangesLocally } from './simulation/tryEstimatingChangesLocally.js';
5
5
  import { getDisplayDetails, getTxFeeDetails } from './transactions.js';
6
- export const createSimulateTransactions = ({ apiEndpoint, headers }) => async ({ transactions, asset, origin, overrideApiEndpoint, shouldSimulate = true, }) => {
6
+ export const createSimulateTransactions = ({ apiEndpoint, headers }) => async ({ transactions, asset, origin, overrideApiEndpoint, }) => {
7
7
  // Ethereum transactions from web3 providers are always one item in an array.
8
8
  const transaction = transactions[0];
9
9
  const simulationResult = createEmptySimulationResult({
@@ -16,11 +16,7 @@ export const createSimulateTransactions = ({ apiEndpoint, headers }) => async ({
16
16
  if (!transaction.to) {
17
17
  return simulationResult;
18
18
  }
19
- if (!shouldSimulate) {
20
- tryEstimatingChangesLocally({ asset, simulationResult, transaction });
21
- return simulationResult;
22
- }
23
- await simulateTransactions({
19
+ const simulationTransactionInternalResult = await simulateTransactions({
24
20
  asset,
25
21
  transactions,
26
22
  apiEndpoint: overrideApiEndpoint || apiEndpoint,
@@ -28,6 +24,12 @@ export const createSimulateTransactions = ({ apiEndpoint, headers }) => async ({
28
24
  simulationResult,
29
25
  headers,
30
26
  });
27
+ if (simulationTransactionInternalResult.kind === 'error' &&
28
+ simulationTransactionInternalResult.error ===
29
+ 0 /* SimulationErrorMessages.SimulationNotSupported */) {
30
+ tryEstimatingChangesLocally({ asset, simulationResult, transaction });
31
+ return simulationResult;
32
+ }
31
33
  simulationResult.displayDetails = getDisplayDetails(simulationResult.balanceChanges.willApprove);
32
34
  return simulationResult;
33
35
  };
@@ -17,10 +17,6 @@ export const estimateSimpleTransfer = ({ asset, transaction, }) => {
17
17
  if (!['0x', null, undefined].includes(transaction.data)) {
18
18
  throw new NotSimpleTransferError('The transaction input should be empty ("0x").');
19
19
  }
20
- // Simple transfers should consume 21.000 (constant) units of gas.
21
- if (![undefined, '0x5208'].includes(transaction.gas)) {
22
- throw new NotSimpleTransferError('The gas value must precisely be 21,000 units.');
23
- }
24
20
  const txValue = hexToBN(transaction.value || '0x0').toString();
25
21
  return asset.currency.baseUnit(txValue);
26
22
  };
@@ -1,8 +1,11 @@
1
1
  import type { EthAggregatedTransactionSimulationResult, EthSimulateTransactionParams } from '../types.js';
2
- import type { CreateSimulateTransactionsParams } from '@exodus/web3-types';
2
+ import type { CreateSimulateTransactionsParams, SimulateTransactionsInternalResult } from '@exodus/web3-types';
3
3
  declare type SimulateTransactionParams = Required<Pick<CreateSimulateTransactionsParams, 'apiEndpoint'>> & Required<Pick<EthSimulateTransactionParams, 'asset' | 'transactions' | 'origin'>> & {
4
4
  simulationResult: EthAggregatedTransactionSimulationResult;
5
5
  headers?: Record<string, string>;
6
6
  };
7
- export declare const simulateTransactions: ({ transactions, apiEndpoint, origin, simulationResult, headers, asset, }: SimulateTransactionParams) => Promise<void>;
7
+ export declare const enum SimulationErrorMessages {
8
+ 'SimulationNotSupported' = 0
9
+ }
10
+ export declare const simulateTransactions: ({ transactions, apiEndpoint, origin, simulationResult, headers, asset, }: SimulateTransactionParams) => SimulateTransactionsInternalResult<undefined, SimulationErrorMessages>;
8
11
  export {};
@@ -106,11 +106,21 @@ const simulationResultHasAssets = (expectedStateChanges) => {
106
106
  return true;
107
107
  };
108
108
  export const simulateTransactions = async ({ transactions, apiEndpoint, origin, simulationResult, headers, asset, }) => {
109
+ const successDefaultResult = {
110
+ kind: 'success',
111
+ value: undefined,
112
+ };
113
+ if (!BLOWFISH_EVM_CHAINS[asset.name]) {
114
+ return {
115
+ kind: 'error',
116
+ error: 0 /* SimulationErrorMessages.SimulationNotSupported */,
117
+ };
118
+ }
109
119
  const userAddress = transactions[0].from;
110
120
  const payload = convertTransactionToApiPayload(transactions, origin);
111
121
  if (!Object.hasOwnProperty.call(BLOWFISH_EVM_CHAINS, asset.name)) {
112
122
  // ToDo: accept a logger dep and log the error
113
- return;
123
+ return successDefaultResult;
114
124
  }
115
125
  const { network, chain } = BLOWFISH_EVM_CHAINS[asset.name];
116
126
  const response = await makeSimulationAPICall({
@@ -121,13 +131,13 @@ export const simulateTransactions = async ({ transactions, apiEndpoint, origin,
121
131
  headers,
122
132
  });
123
133
  if (!response) {
124
- return;
134
+ return successDefaultResult;
125
135
  }
126
136
  const { expectedStateChanges = {}, error } = response.simulationResults.aggregated;
127
137
  if (error) {
128
138
  // ToDo: accept a logger dep and log the error
129
139
  // ToDo: consider pushing an error to the "warnings" list
130
- return;
140
+ return successDefaultResult;
131
141
  }
132
142
  const noExpectedStateChangesDetected = Object.keys(expectedStateChanges).length === 0;
133
143
  // This is a self-send transaction, adding a zero "transfer" so that the UI can display it.
@@ -139,7 +149,7 @@ export const simulateTransactions = async ({ transactions, apiEndpoint, origin,
139
149
  denominator: asset.currency.defaultUnit.power,
140
150
  }),
141
151
  });
142
- return;
152
+ return successDefaultResult;
143
153
  }
144
154
  const userExpectedChanges = Object.keys(expectedStateChanges).reduce((prev, address) => {
145
155
  if (userAddress.toLowerCase() !== address.toLowerCase()) {
@@ -153,9 +163,10 @@ export const simulateTransactions = async ({ transactions, apiEndpoint, origin,
153
163
  // Return and log if asset is not present in the expectedStateChanges
154
164
  if (!simulationResultHasAssets(userExpectedChanges)) {
155
165
  // ToDo: accept a logger dep and log the error
156
- return;
166
+ return successDefaultResult;
157
167
  }
158
168
  userExpectedChanges.forEach((expectedChange) => {
159
169
  handleExpectedBalanceChange(expectedChange, simulationResult, { network });
160
170
  });
171
+ return successDefaultResult;
161
172
  };
package/lib/types.d.ts CHANGED
@@ -46,7 +46,6 @@ export interface EthWalletWatchAsset {
46
46
  }
47
47
  export interface EthSimulateTransactionParams extends SimulateTransactionParams {
48
48
  transactions: EthTransaction[];
49
- shouldSimulate?: boolean;
50
49
  }
51
50
  export interface EthFeeDetails {
52
51
  gasLimit: BN;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@exodus/web3-ethereum-utils",
3
3
  "description": "Web3 Ethereum utils.",
4
- "version": "3.30.0",
4
+ "version": "3.31.0",
5
5
  "type": "module",
6
6
  "sideEffects": false,
7
7
  "module": "lib/index.js",
@@ -36,5 +36,5 @@
36
36
  "@exodus/web3-utils": "^1.45.0",
37
37
  "@metamask/eth-sig-util": "^4.0.1"
38
38
  },
39
- "gitHead": "96fc062090ba251a05e619e49fb812ecb0f3ae60"
39
+ "gitHead": "312f25a38096f8a1dcb9bfb7124d59e89d8be873"
40
40
  }