@exodus/web3-ethereum-utils 1.11.0 → 1.13.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.js +10 -0
- package/lib/simulation/estimateFee.d.ts +6 -0
- package/lib/simulation/estimateFee.js +19 -0
- package/lib/simulation/estimateSimpleTransfer.d.ts +3 -0
- package/lib/simulation/estimateSimpleTransfer.js +7 -2
- package/lib/simulation/simulateTransaction.d.ts +7 -0
- package/lib/simulation/simulateTransaction.js +26 -0
- package/lib/simulation/tryEstimatingChangesLocally.d.ts +7 -0
- package/lib/simulation/tryEstimatingChangesLocally.js +16 -0
- package/lib/types.d.ts +5 -5
- package/package.json +4 -4
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { createEmptySimulationResult } from '@exodus/web3-utils';
|
|
2
|
+
import { estimateFee } from './simulation/estimateFee.js';
|
|
3
|
+
import { tryEstimatingChangesLocally } from './simulation/tryEstimatingChangesLocally.js';
|
|
2
4
|
const SIMULATION_API_DEFAULT = 'https://simulation.a.exodus.io/simulate';
|
|
3
5
|
const SERVICE_PROVIDER_DEFAULT = 'blowfish';
|
|
4
6
|
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
@@ -6,6 +8,14 @@ export const createSimulateTransaction = ({ serviceProvider = SERVICE_PROVIDER_D
|
|
|
6
8
|
if (!transaction.to) {
|
|
7
9
|
return createEmptySimulationResult({ asset });
|
|
8
10
|
}
|
|
11
|
+
const simulationResult = createEmptySimulationResult({ asset });
|
|
12
|
+
simulationResult.balanceChanges.willPayFee.push({
|
|
13
|
+
balance: estimateFee({ asset, transaction }),
|
|
14
|
+
});
|
|
15
|
+
if (!shouldCallPreviewApi) {
|
|
16
|
+
tryEstimatingChangesLocally({ asset, simulationResult, transaction });
|
|
17
|
+
return simulationResult;
|
|
18
|
+
}
|
|
9
19
|
// ToDo: finish with an actual simulation attempt
|
|
10
20
|
return createEmptySimulationResult({ asset });
|
|
11
21
|
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/*
|
|
2
|
+
This utility determines a fee a transaction is going to pay based on
|
|
3
|
+
its "gas", "gasPrice" and "maxFeePerGas" properties.
|
|
4
|
+
|
|
5
|
+
Note: if EIP-1559 is enabled a transaction will have the "maxFeePerGas" field. In that case
|
|
6
|
+
a final fee will be calculated on-chain (the utility returns a maximum possible value).
|
|
7
|
+
If a transaction has just "gasPrice" it can be considered as a total fee.
|
|
8
|
+
*/
|
|
9
|
+
import { hexToBN } from '@exodus/web3-utils';
|
|
10
|
+
export const estimateFee = ({ asset, transaction, }) => {
|
|
11
|
+
const gas = hexToBN(transaction.gas);
|
|
12
|
+
if (transaction.maxFeePerGas) {
|
|
13
|
+
const maxFeePerGas = hexToBN(transaction.maxFeePerGas);
|
|
14
|
+
const maximumFee = gas.mul(maxFeePerGas);
|
|
15
|
+
return asset.currency.baseUnit(maximumFee);
|
|
16
|
+
}
|
|
17
|
+
const gasPrice = hexToBN(transaction.gasPrice);
|
|
18
|
+
return asset.currency.baseUnit(gas.mul(gasPrice));
|
|
19
|
+
};
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import type { EthTransaction } from '../types.js';
|
|
2
2
|
import type { NumberUnit } from '@exodus/web3-types';
|
|
3
3
|
import type { Asset } from '@exodus/web3-types';
|
|
4
|
+
export declare class NotSimpleTransferError extends Error {
|
|
5
|
+
constructor(msg: string);
|
|
6
|
+
}
|
|
4
7
|
export declare const estimateSimpleTransfer: ({ asset, transaction, }: {
|
|
5
8
|
asset: Asset;
|
|
6
9
|
transaction: EthTransaction;
|
|
@@ -7,14 +7,19 @@
|
|
|
7
7
|
with a gas limit exceeding 21000 units.
|
|
8
8
|
*/
|
|
9
9
|
import { hexToBN } from '@exodus/web3-utils';
|
|
10
|
+
export class NotSimpleTransferError extends Error {
|
|
11
|
+
constructor(msg) {
|
|
12
|
+
super(msg);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
10
15
|
export const estimateSimpleTransfer = ({ asset, transaction, }) => {
|
|
11
16
|
// Transaction shouldn't provide any parameters to a smart contract.
|
|
12
17
|
if (!['0x', null, undefined].includes(transaction.data)) {
|
|
13
|
-
throw new
|
|
18
|
+
throw new NotSimpleTransferError('The transaction input should be empty ("0x").');
|
|
14
19
|
}
|
|
15
20
|
// Simple transfers should consume 21.000 (constant) units of gas.
|
|
16
21
|
if (![undefined, '0x5208'].includes(transaction.gas)) {
|
|
17
|
-
throw new
|
|
22
|
+
throw new NotSimpleTransferError('The gas value must precisely be 21,000 units.');
|
|
18
23
|
}
|
|
19
24
|
const txValue = hexToBN(transaction.value || '0x0').toString();
|
|
20
25
|
return asset.currency.baseUnit(txValue);
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { EthSimulateTransactionParams } from '../types.js';
|
|
2
|
+
import type { AggregatedTransactionSimulationResult, CreateSimulateTransactionParams } from '@exodus/web3-types';
|
|
3
|
+
declare type SimulateTransactionParams = Required<CreateSimulateTransactionParams> & Pick<EthSimulateTransactionParams, 'transaction' | 'origin'> & {
|
|
4
|
+
simulationResult: AggregatedTransactionSimulationResult;
|
|
5
|
+
};
|
|
6
|
+
export declare const simulateTransaction: ({ transaction, apiEndpoint, origin, }: SimulateTransactionParams) => Promise<void> | never;
|
|
7
|
+
export {};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/*
|
|
2
|
+
This utility makes an API call to a simulation service, parses the response and mutates the supplied "simulationResult" object
|
|
3
|
+
with the according balance changes.
|
|
4
|
+
*/
|
|
5
|
+
import { makeSimulationAPICall } from '@exodus/web3-utils';
|
|
6
|
+
const convertTransactionToApiPayload = (transaction, origin) => ({
|
|
7
|
+
txObject: {
|
|
8
|
+
from: transaction.from,
|
|
9
|
+
to: transaction.to,
|
|
10
|
+
data: transaction.data,
|
|
11
|
+
value: transaction.value,
|
|
12
|
+
},
|
|
13
|
+
metadata: {
|
|
14
|
+
origin,
|
|
15
|
+
},
|
|
16
|
+
userAccount: transaction.from,
|
|
17
|
+
});
|
|
18
|
+
export const simulateTransaction = async ({ transaction, apiEndpoint, origin, }) => {
|
|
19
|
+
const payload = convertTransactionToApiPayload(transaction, origin);
|
|
20
|
+
await makeSimulationAPICall({
|
|
21
|
+
url: apiEndpoint,
|
|
22
|
+
network: 'ethereum',
|
|
23
|
+
payload,
|
|
24
|
+
});
|
|
25
|
+
// ToDo: parse the response and mutate the simulation result
|
|
26
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { EthTransaction } from '../types.js';
|
|
2
|
+
import type { AggregatedTransactionSimulationResult, Asset } from '@exodus/web3-types';
|
|
3
|
+
export declare const tryEstimatingChangesLocally: ({ asset, simulationResult, transaction, }: {
|
|
4
|
+
asset: Asset;
|
|
5
|
+
simulationResult: AggregatedTransactionSimulationResult;
|
|
6
|
+
transaction: EthTransaction;
|
|
7
|
+
}) => void;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { estimateSimpleTransfer, NotSimpleTransferError, } from './estimateSimpleTransfer.js';
|
|
2
|
+
/*
|
|
3
|
+
Mutates the provided 'simulationResult' adding a 'willSend' entry if the transaction is a simple transfer.
|
|
4
|
+
*/
|
|
5
|
+
export const tryEstimatingChangesLocally = ({ asset, simulationResult, transaction, }) => {
|
|
6
|
+
try {
|
|
7
|
+
const transferValue = estimateSimpleTransfer({ asset, transaction });
|
|
8
|
+
simulationResult.balanceChanges.willSend.push({ balance: transferValue });
|
|
9
|
+
}
|
|
10
|
+
catch (err) {
|
|
11
|
+
const expectedError = err instanceof NotSimpleTransferError;
|
|
12
|
+
if (!expectedError) {
|
|
13
|
+
// ToDo: accept a logger dep and log an error here
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
};
|
package/lib/types.d.ts
CHANGED
|
@@ -15,14 +15,14 @@ export interface EthTransactionParams {
|
|
|
15
15
|
maxFeePerGas?: string;
|
|
16
16
|
}
|
|
17
17
|
export interface EthTransaction {
|
|
18
|
-
chainId
|
|
18
|
+
chainId: string;
|
|
19
19
|
from: string;
|
|
20
20
|
to?: string;
|
|
21
|
-
gas
|
|
22
|
-
gasPrice
|
|
23
|
-
value
|
|
21
|
+
gas: string;
|
|
22
|
+
gasPrice: string;
|
|
23
|
+
value: string;
|
|
24
24
|
data: string;
|
|
25
|
-
nonce
|
|
25
|
+
nonce: string;
|
|
26
26
|
maxPriorityFeePerGas?: string;
|
|
27
27
|
maxFeePerGas?: string;
|
|
28
28
|
}
|
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": "1.
|
|
4
|
+
"version": "1.13.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.19.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.9.0",
|
|
35
35
|
"@metamask/eth-sig-util": "^4.0.1"
|
|
36
36
|
},
|
|
37
|
-
"gitHead": "
|
|
37
|
+
"gitHead": "ee026a5dba0f36d61545cbb7fbecbbd143355ded"
|
|
38
38
|
}
|