@exodus/web3-ethereum-utils 3.29.1 → 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.
- package/lib/createSimulateTransactions.d.ts +1 -1
- package/lib/createSimulateTransactions.js +10 -8
- package/lib/simulation/estimateSimpleTransfer.js +0 -4
- package/lib/simulation/simulateTransactions.d.ts +6 -4
- package/lib/simulation/simulateTransactions.js +30 -7
- 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,18 +16,20 @@ export const createSimulateTransactions = ({ apiEndpoint, headers }) => async ({
|
|
|
16
16
|
if (!transaction.to) {
|
|
17
17
|
return simulationResult;
|
|
18
18
|
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
return simulationResult;
|
|
22
|
-
}
|
|
23
|
-
await simulateTransactions({
|
|
19
|
+
const simulationTransactionInternalResult = await simulateTransactions({
|
|
20
|
+
asset,
|
|
24
21
|
transactions,
|
|
25
22
|
apiEndpoint: overrideApiEndpoint || apiEndpoint,
|
|
26
23
|
origin,
|
|
27
24
|
simulationResult,
|
|
28
25
|
headers,
|
|
29
|
-
assetName: asset.name,
|
|
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,9 +1,11 @@
|
|
|
1
1
|
import type { EthAggregatedTransactionSimulationResult, EthSimulateTransactionParams } from '../types.js';
|
|
2
|
-
import type { CreateSimulateTransactionsParams } from '@exodus/web3-types';
|
|
3
|
-
declare type SimulateTransactionParams = Required<Pick<CreateSimulateTransactionsParams, 'apiEndpoint'>> & Required<Pick<EthSimulateTransactionParams, 'transactions' | 'origin'>> & {
|
|
2
|
+
import type { CreateSimulateTransactionsParams, SimulateTransactionsInternalResult } from '@exodus/web3-types';
|
|
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
|
-
assetName: string;
|
|
7
6
|
};
|
|
8
|
-
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>;
|
|
9
11
|
export {};
|
|
@@ -105,14 +105,24 @@ const simulationResultHasAssets = (expectedStateChanges) => {
|
|
|
105
105
|
}
|
|
106
106
|
return true;
|
|
107
107
|
};
|
|
108
|
-
export const simulateTransactions = async ({ transactions, apiEndpoint, origin, simulationResult, headers,
|
|
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
|
-
if (!Object.hasOwnProperty.call(BLOWFISH_EVM_CHAINS,
|
|
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
|
-
const { network, chain } = BLOWFISH_EVM_CHAINS[
|
|
125
|
+
const { network, chain } = BLOWFISH_EVM_CHAINS[asset.name];
|
|
116
126
|
const response = await makeSimulationAPICall({
|
|
117
127
|
url: apiEndpoint,
|
|
118
128
|
chain,
|
|
@@ -121,13 +131,25 @@ 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;
|
|
141
|
+
}
|
|
142
|
+
const noExpectedStateChangesDetected = Object.keys(expectedStateChanges).length === 0;
|
|
143
|
+
// This is a self-send transaction, adding a zero "transfer" so that the UI can display it.
|
|
144
|
+
if (noExpectedStateChangesDetected) {
|
|
145
|
+
simulationResult.balanceChanges.willSend.push({
|
|
146
|
+
balance: createCurrency({
|
|
147
|
+
amount: 0,
|
|
148
|
+
symbol: asset.displayTicker,
|
|
149
|
+
denominator: asset.currency.defaultUnit.power,
|
|
150
|
+
}),
|
|
151
|
+
});
|
|
152
|
+
return successDefaultResult;
|
|
131
153
|
}
|
|
132
154
|
const userExpectedChanges = Object.keys(expectedStateChanges).reduce((prev, address) => {
|
|
133
155
|
if (userAddress.toLowerCase() !== address.toLowerCase()) {
|
|
@@ -141,9 +163,10 @@ export const simulateTransactions = async ({ transactions, apiEndpoint, origin,
|
|
|
141
163
|
// Return and log if asset is not present in the expectedStateChanges
|
|
142
164
|
if (!simulationResultHasAssets(userExpectedChanges)) {
|
|
143
165
|
// ToDo: accept a logger dep and log the error
|
|
144
|
-
return;
|
|
166
|
+
return successDefaultResult;
|
|
145
167
|
}
|
|
146
168
|
userExpectedChanges.forEach((expectedChange) => {
|
|
147
169
|
handleExpectedBalanceChange(expectedChange, simulationResult, { network });
|
|
148
170
|
});
|
|
171
|
+
return successDefaultResult;
|
|
149
172
|
};
|
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.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": "
|
|
39
|
+
"gitHead": "312f25a38096f8a1dcb9bfb7124d59e89d8be873"
|
|
40
40
|
}
|