@exodus/web3-ethereum-utils 1.24.0 → 2.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/lib/createSimulateTransaction.d.ts +3 -3
- package/lib/createSimulateTransaction.js +9 -3
- package/lib/simulation/simulateTransaction.d.ts +12 -4
- package/lib/simulation/simulateTransaction.js +30 -2
- package/lib/simulation/tryEstimatingChangesLocally.d.ts +3 -3
- package/lib/transactions.d.ts +10 -0
- package/lib/transactions.js +21 -0
- package/lib/types.d.ts +8 -5
- package/package.json +4 -4
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import type { EthSimulateTransactionParams } from './types.js';
|
|
2
|
-
import type {
|
|
3
|
-
export declare const createSimulateTransaction: ({ serviceProvider, apiEndpoint, headers, }
|
|
1
|
+
import type { EthAggregatedTransactionSimulationResult, EthSimulateTransactionParams } from './types.js';
|
|
2
|
+
import type { CreateSimulateTransactionParams } from '@exodus/web3-types';
|
|
3
|
+
export declare const createSimulateTransaction: ({ serviceProvider, apiEndpoint, headers, shouldSimulate, }: CreateSimulateTransactionParams) => ({ transaction, asset, origin, }: EthSimulateTransactionParams) => Promise<EthAggregatedTransactionSimulationResult>;
|
|
@@ -2,21 +2,25 @@ import { createEmptySimulationResult } from '@exodus/web3-utils';
|
|
|
2
2
|
import { estimateFee } from './simulation/estimateFee.js';
|
|
3
3
|
import { simulateTransaction } from './simulation/simulateTransaction.js';
|
|
4
4
|
import { tryEstimatingChangesLocally } from './simulation/tryEstimatingChangesLocally.js';
|
|
5
|
+
import { getDisplayDetails, getTxFeeDetails } from './transactions.js';
|
|
5
6
|
const SIMULATION_API_DEFAULT = 'https://simulation.a.exodus.io/simulate';
|
|
6
7
|
const SERVICE_PROVIDER_DEFAULT = 'blowfish';
|
|
7
8
|
const SIMULATION_HEADERS = {
|
|
8
9
|
'X-Api-Version': '2023-06-05',
|
|
9
10
|
};
|
|
10
11
|
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
11
|
-
export const createSimulateTransaction = ({ serviceProvider = SERVICE_PROVIDER_DEFAULT, apiEndpoint = SIMULATION_API_DEFAULT, headers = SIMULATION_HEADERS,
|
|
12
|
-
const simulationResult = createEmptySimulationResult({
|
|
12
|
+
export const createSimulateTransaction = ({ serviceProvider = SERVICE_PROVIDER_DEFAULT, apiEndpoint = SIMULATION_API_DEFAULT, headers = SIMULATION_HEADERS, shouldSimulate = true, }) => async ({ transaction, asset, origin, }) => {
|
|
13
|
+
const simulationResult = createEmptySimulationResult({
|
|
14
|
+
asset,
|
|
15
|
+
});
|
|
13
16
|
simulationResult.balanceChanges.willPayFee.push({
|
|
14
17
|
balance: estimateFee({ asset, transaction }),
|
|
18
|
+
feeDetails: getTxFeeDetails(transaction),
|
|
15
19
|
});
|
|
16
20
|
if (!transaction.to) {
|
|
17
21
|
return simulationResult;
|
|
18
22
|
}
|
|
19
|
-
if (!
|
|
23
|
+
if (!shouldSimulate) {
|
|
20
24
|
tryEstimatingChangesLocally({ asset, simulationResult, transaction });
|
|
21
25
|
return simulationResult;
|
|
22
26
|
}
|
|
@@ -26,6 +30,8 @@ export const createSimulateTransaction = ({ serviceProvider = SERVICE_PROVIDER_D
|
|
|
26
30
|
origin,
|
|
27
31
|
simulationResult,
|
|
28
32
|
headers,
|
|
33
|
+
assetName: asset.name,
|
|
29
34
|
});
|
|
35
|
+
simulationResult.displayDetails = getDisplayDetails(simulationResult.balanceChanges.willApprove);
|
|
30
36
|
return simulationResult;
|
|
31
37
|
};
|
|
@@ -1,8 +1,16 @@
|
|
|
1
|
-
import type { EthSimulateTransactionParams } from '../types.js';
|
|
2
|
-
import type {
|
|
1
|
+
import type { EthAggregatedTransactionSimulationResult, EthSimulateTransactionParams } from '../types.js';
|
|
2
|
+
import type { CreateSimulateTransactionParams } from '@exodus/web3-types';
|
|
3
|
+
interface BlowfishEvmChain {
|
|
4
|
+
[key: string]: {
|
|
5
|
+
network: string;
|
|
6
|
+
chain: string;
|
|
7
|
+
};
|
|
8
|
+
}
|
|
9
|
+
export declare const BLOWFISH_EVM_CHAINS: BlowfishEvmChain;
|
|
3
10
|
declare type SimulateTransactionParams = Required<Pick<CreateSimulateTransactionParams, 'apiEndpoint'>> & Required<Pick<EthSimulateTransactionParams, 'transaction' | 'origin'>> & {
|
|
4
|
-
simulationResult:
|
|
11
|
+
simulationResult: EthAggregatedTransactionSimulationResult;
|
|
5
12
|
headers?: Record<string, string>;
|
|
13
|
+
assetName: string;
|
|
6
14
|
};
|
|
7
|
-
export declare const simulateTransaction: ({ transaction, apiEndpoint, origin, simulationResult, headers, }: SimulateTransactionParams) => Promise<void> | never;
|
|
15
|
+
export declare const simulateTransaction: ({ transaction, apiEndpoint, origin, simulationResult, headers, assetName, }: SimulateTransactionParams) => Promise<void> | never;
|
|
8
16
|
export {};
|
|
@@ -3,6 +3,28 @@
|
|
|
3
3
|
with the according balance changes.
|
|
4
4
|
*/
|
|
5
5
|
import { createCurrency, makeSimulationAPICall } from '@exodus/web3-utils';
|
|
6
|
+
export const BLOWFISH_EVM_CHAINS = {
|
|
7
|
+
ethereum: {
|
|
8
|
+
network: 'ethereum',
|
|
9
|
+
chain: 'mainnet',
|
|
10
|
+
},
|
|
11
|
+
matic: {
|
|
12
|
+
network: 'polygon',
|
|
13
|
+
chain: 'mainnet',
|
|
14
|
+
},
|
|
15
|
+
bsc: {
|
|
16
|
+
network: 'bnb',
|
|
17
|
+
chain: 'mainnet',
|
|
18
|
+
},
|
|
19
|
+
ethereumarbone: {
|
|
20
|
+
network: 'arbitrum',
|
|
21
|
+
chain: 'one',
|
|
22
|
+
},
|
|
23
|
+
optimism: {
|
|
24
|
+
network: 'optimism',
|
|
25
|
+
chain: 'goerli',
|
|
26
|
+
},
|
|
27
|
+
};
|
|
6
28
|
const convertTransactionToApiPayload = (transaction, origin) => ({
|
|
7
29
|
txObjects: [
|
|
8
30
|
{
|
|
@@ -86,12 +108,18 @@ const simulationResultHasAssets = (expectedStateChanges) => {
|
|
|
86
108
|
}
|
|
87
109
|
return true;
|
|
88
110
|
};
|
|
89
|
-
export const simulateTransaction = async ({ transaction, apiEndpoint, origin, simulationResult, headers, }) => {
|
|
111
|
+
export const simulateTransaction = async ({ transaction, apiEndpoint, origin, simulationResult, headers, assetName, }) => {
|
|
90
112
|
const userAddress = transaction.from;
|
|
91
113
|
const payload = convertTransactionToApiPayload(transaction, origin);
|
|
114
|
+
if (!Object.hasOwnProperty.call(BLOWFISH_EVM_CHAINS, assetName)) {
|
|
115
|
+
// ToDo: accept a logger dep and log the error
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
const { network, chain } = BLOWFISH_EVM_CHAINS[assetName];
|
|
92
119
|
const response = await makeSimulationAPICall({
|
|
93
120
|
url: apiEndpoint,
|
|
94
|
-
|
|
121
|
+
chain,
|
|
122
|
+
network,
|
|
95
123
|
payload,
|
|
96
124
|
headers,
|
|
97
125
|
});
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import type { EthTransaction } from '../types.js';
|
|
2
|
-
import type {
|
|
1
|
+
import type { EthAggregatedTransactionSimulationResult, EthTransaction } from '../types.js';
|
|
2
|
+
import type { Asset } from '@exodus/web3-types';
|
|
3
3
|
export declare const tryEstimatingChangesLocally: ({ asset, simulationResult, transaction, }: {
|
|
4
4
|
asset: Asset;
|
|
5
|
-
simulationResult:
|
|
5
|
+
simulationResult: EthAggregatedTransactionSimulationResult;
|
|
6
6
|
transaction: EthTransaction;
|
|
7
7
|
}) => void;
|
package/lib/transactions.d.ts
CHANGED
|
@@ -1,2 +1,12 @@
|
|
|
1
1
|
/// <reference types="node" resolution-mode="require"/>
|
|
2
|
+
import type { EthTransaction } from './types.js';
|
|
3
|
+
import type { ApprovalBalanceChange } from '@exodus/web3-types';
|
|
2
4
|
export declare function getTransactionId(serializedTransaction: Buffer, isEIP1559Transaction?: boolean): string;
|
|
5
|
+
export declare function getDisplayDetails(willApprove: ApprovalBalanceChange[]): {
|
|
6
|
+
title: string;
|
|
7
|
+
};
|
|
8
|
+
export declare const getTxFeeDetails: (transaction: EthTransaction) => {
|
|
9
|
+
gasLimit: import("bn.js");
|
|
10
|
+
maxFeePerGas: import("bn.js");
|
|
11
|
+
gasPrice: import("bn.js");
|
|
12
|
+
};
|
package/lib/transactions.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { FeeMarketEIP1559Transaction, Transaction as LegacyTransaction, } from '@exodus/ethereumjs-tx';
|
|
2
|
+
import { hexToBN } from '@exodus/web3-utils';
|
|
2
3
|
export function getTransactionId(serializedTransaction, isEIP1559Transaction = false) {
|
|
3
4
|
const Transaction = isEIP1559Transaction
|
|
4
5
|
? FeeMarketEIP1559Transaction
|
|
@@ -6,3 +7,23 @@ export function getTransactionId(serializedTransaction, isEIP1559Transaction = f
|
|
|
6
7
|
const transaction = Transaction.fromSerializedTx(serializedTransaction);
|
|
7
8
|
return `0x${transaction.hash().toString('hex')}`;
|
|
8
9
|
}
|
|
10
|
+
export function getDisplayDetails(willApprove) {
|
|
11
|
+
let title = 'Approve Transaction?';
|
|
12
|
+
const isApproveTransaction = willApprove.length;
|
|
13
|
+
if (isApproveTransaction) {
|
|
14
|
+
const tokenName = willApprove[0].unitName;
|
|
15
|
+
// Don't change the title if 'tokenName' is undefined
|
|
16
|
+
title = tokenName ? `Approve ${tokenName} Transactions?` : title;
|
|
17
|
+
}
|
|
18
|
+
return { title };
|
|
19
|
+
}
|
|
20
|
+
export const getTxFeeDetails = (transaction) => {
|
|
21
|
+
const maxFeePerGas = hexToBN(transaction.maxFeePerGas);
|
|
22
|
+
const gasPrice = hexToBN(transaction.gasPrice);
|
|
23
|
+
const gasLimit = hexToBN(transaction.gas);
|
|
24
|
+
return {
|
|
25
|
+
gasLimit,
|
|
26
|
+
maxFeePerGas,
|
|
27
|
+
gasPrice,
|
|
28
|
+
};
|
|
29
|
+
};
|
package/lib/types.d.ts
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
import type { SimulateTransactionParams } from '@exodus/web3-types';
|
|
2
|
-
export interface EthChainParam {
|
|
3
|
-
chainId: string;
|
|
4
|
-
}
|
|
1
|
+
import type { AggregatedTransactionSimulationResult, BN, SimulateTransactionParams } from '@exodus/web3-types';
|
|
5
2
|
export interface EthTransactionParams {
|
|
6
3
|
chainId: string;
|
|
7
4
|
from: string;
|
|
@@ -24,7 +21,7 @@ export interface EthTransaction {
|
|
|
24
21
|
data: string;
|
|
25
22
|
nonce: string;
|
|
26
23
|
maxPriorityFeePerGas?: string;
|
|
27
|
-
maxFeePerGas
|
|
24
|
+
maxFeePerGas: string;
|
|
28
25
|
}
|
|
29
26
|
export declare type EthTransactionHash = string;
|
|
30
27
|
export declare type EthParentCapability = 'eth_accounts';
|
|
@@ -44,3 +41,9 @@ export interface EthWalletWatchAsset {
|
|
|
44
41
|
export interface EthSimulateTransactionParams extends SimulateTransactionParams {
|
|
45
42
|
transaction: EthTransaction;
|
|
46
43
|
}
|
|
44
|
+
export interface EthFeeDetails {
|
|
45
|
+
gasLimit: BN;
|
|
46
|
+
maxFeePerGas: BN;
|
|
47
|
+
gasPrice: BN;
|
|
48
|
+
}
|
|
49
|
+
export declare type EthAggregatedTransactionSimulationResult = AggregatedTransactionSimulationResult<EthFeeDetails>;
|
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": "
|
|
4
|
+
"version": "2.0.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
7
7
|
"module": "lib/index.js",
|
|
@@ -26,13 +26,13 @@
|
|
|
26
26
|
"build": "yarn clean && tsc"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
|
-
"@exodus/web3-types": "^1.
|
|
29
|
+
"@exodus/web3-types": "^1.29.0"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
32
|
"@exodus/ethereumjs-common": "^2.4.0-exodus.6",
|
|
33
33
|
"@exodus/ethereumjs-tx": "^3.3.0-exodus.6",
|
|
34
|
-
"@exodus/web3-utils": "^1.
|
|
34
|
+
"@exodus/web3-utils": "^1.21.0",
|
|
35
35
|
"@metamask/eth-sig-util": "^4.0.1"
|
|
36
36
|
},
|
|
37
|
-
"gitHead": "
|
|
37
|
+
"gitHead": "8ac7c36af33c692d577d3a53a4edad0187fee33b"
|
|
38
38
|
}
|