@exodus/web3-ethereum-utils 3.30.0 → 3.31.1
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/lib/createSimulateTransactions.d.ts +1 -1
- package/lib/createSimulateTransactions.js +9 -7
- package/lib/simulation/estimateSimpleTransfer.js +0 -4
- package/lib/simulation/simulateTransactions.d.ts +5 -2
- package/lib/simulation/simulateTransactions.js +16 -5
- package/lib/simulation/tryEstimatingChangesLocally.js +7 -2
- package/lib/types.d.ts +0 -1
- package/package.json +2 -2
|
@@ -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,
|
|
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,
|
|
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
|
-
|
|
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
|
|
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
|
};
|
|
@@ -4,8 +4,13 @@ import { estimateSimpleTransfer, NotSimpleTransferError, } from './estimateSimpl
|
|
|
4
4
|
*/
|
|
5
5
|
export const tryEstimatingChangesLocally = ({ asset, simulationResult, transaction, }) => {
|
|
6
6
|
try {
|
|
7
|
-
const
|
|
8
|
-
|
|
7
|
+
const estimatedValue = estimateSimpleTransfer({ asset, transaction });
|
|
8
|
+
const actualTransferValue = transaction.from.toLowerCase() === transaction.to.toLowerCase()
|
|
9
|
+
? asset.currency.ZERO
|
|
10
|
+
: estimatedValue;
|
|
11
|
+
simulationResult.balanceChanges.willSend.push({
|
|
12
|
+
balance: actualTransferValue,
|
|
13
|
+
});
|
|
9
14
|
}
|
|
10
15
|
catch (err) {
|
|
11
16
|
const expectedError = err instanceof NotSimpleTransferError;
|
package/lib/types.d.ts
CHANGED
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.
|
|
4
|
+
"version": "3.31.1",
|
|
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": "
|
|
39
|
+
"gitHead": "640977164352986134b0f62de8522d20283b6ba2"
|
|
40
40
|
}
|