Skip to content

Commit

Permalink
feat: Add gasLimitNoBuffer to transactionMeta (#5113)
Browse files Browse the repository at this point in the history
## Explanation

<!--
Thanks for your contribution! Take a moment to answer these questions so
that reviewers have the information they need to properly understand
your changes:

* What is the current state of things and why does it need to change?
* What is the solution your changes offer and how does it work?
* Are there any changes whose purpose might not obvious to those
unfamiliar with the domain?
* If your primary goal was to update one package but you found you had
to update another one along the way, why did you do so?
* If you had to upgrade a dependency, why did you do so?
-->

This PR aims to add `gasLimitNoBuffer` to `transactionMeta` to show
minimum value while calculating network fee.

## References

<!--
Are there any issues that this pull request is tied to?
Are there other links that reviewers should consult to understand these
changes better?
Are there client or consumer pull requests to adopt any breaking
changes?

For example:

* Fixes #12345
* Related to #67890
-->

* Fixes MetaMask/MetaMask-planning#3773

## Changelog

<!--
If you're making any consumer-facing changes, list those changes here as
if you were updating a changelog, using the template below as a guide.

(CATEGORY is one of BREAKING, ADDED, CHANGED, DEPRECATED, REMOVED, or
FIXED. For security-related issues, follow the Security Advisory
process.)

Please take care to name the exact pieces of the API you've added or
changed (e.g. types, interfaces, functions, or methods).

If there are any breaking changes, make sure to offer a solution for
consumers to follow once they upgrade to the changes.

Finally, if you're only making changes to development scripts or tests,
you may replace the template below with "None".
-->

### `@metamask/transaction-controller`

- **Added**: Add `gasLimitNoBuffer` to `transactionMeta`
- `gasLimitNoBuffer` is the estimated gas (via `eth_estimateGas`) for
the transaction without buffer.

## Checklist

- [X] I've updated the test suite for new or updated code as appropriate
- [X] I've updated documentation (JSDoc, Markdown, etc.) for new or
updated code as appropriate
- [X] I've highlighted breaking changes using the "BREAKING" category
above as appropriate
- [X] I've prepared draft pull requests for clients and consumer
packages to resolve any breaking changes
  • Loading branch information
OGPoyraz authored Jan 9, 2025
1 parent 3535d58 commit 3b94a7c
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
5 changes: 5 additions & 0 deletions packages/transaction-controller/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Add `gasLimitNoBuffer` property to `TransactionMeta` type ([#5113](https://github.com/MetaMask/core/pull/5113))
- `gasLimitNoBuffer` is the estimated gas for the transaction without any buffer applied.

## [42.1.0]

### Added
Expand Down
5 changes: 5 additions & 0 deletions packages/transaction-controller/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,11 @@ type TransactionMetaBase = {
/** Whether the gas fee estimates have been checked at least once. */
gasFeeEstimatesLoaded?: boolean;

/**
* The estimated gas for the transaction without any buffer applied.
*/
gasLimitNoBuffer?: string;

/**
* A hex string of the transaction hash, used to identify the transaction on the network.
*/
Expand Down
9 changes: 9 additions & 0 deletions packages/transaction-controller/src/utils/gas.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,9 @@ describe('gas', () => {
expect(updateGasRequest.txMeta.originalGasEstimate).toBe(
updateGasRequest.txMeta.txParams.gas,
);
expect(updateGasRequest.txMeta.gasLimitNoBuffer).toBe(
toHex(estimatedGas),
);
});

it('to padded estimate using chain multiplier if padded estimate less than percentage of block gas limit', async () => {
Expand All @@ -219,6 +222,9 @@ describe('gas', () => {
expect(updateGasRequest.txMeta.originalGasEstimate).toBe(
updateGasRequest.txMeta.txParams.gas,
);
expect(updateGasRequest.txMeta.gasLimitNoBuffer).toBe(
toHex(estimatedGas),
);
});

it('to percentage of block gas limit if padded estimate only is greater than percentage of block gas limit', async () => {
Expand All @@ -241,6 +247,9 @@ describe('gas', () => {
expect(updateGasRequest.txMeta.originalGasEstimate).toBe(
updateGasRequest.txMeta.txParams.gas,
);
expect(updateGasRequest.txMeta.gasLimitNoBuffer).toBe(
toHex(estimatedGas),
);
});

describe('to fixed value', () => {
Expand Down
13 changes: 7 additions & 6 deletions packages/transaction-controller/src/utils/gas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ export async function updateGas(request: UpdateGasRequest) {
const { txMeta } = request;
const initialParams = { ...txMeta.txParams };

const [gas, simulationFails] = await getGas(request);
const [gas, simulationFails, gasLimitNoBuffer] = await getGas(request);

txMeta.txParams.gas = gas;
txMeta.simulationFails = simulationFails;
txMeta.gasLimitNoBuffer = gasLimitNoBuffer;

if (!initialParams.gas) {
txMeta.originalGasEstimate = txMeta.txParams.gas;
Expand Down Expand Up @@ -132,17 +133,17 @@ export function addGasBuffer(

async function getGas(
request: UpdateGasRequest,
): Promise<[string, TransactionMeta['simulationFails']?]> {
): Promise<[string, TransactionMeta['simulationFails']?, string?]> {
const { isCustomNetwork, chainId, txMeta } = request;

if (txMeta.txParams.gas) {
log('Using value from request', txMeta.txParams.gas);
return [txMeta.txParams.gas];
return [txMeta.txParams.gas, undefined, txMeta.txParams.gas];
}

if (await requiresFixedGas(request)) {
log('Using fixed value', FIXED_GAS);
return [FIXED_GAS];
return [FIXED_GAS, undefined, FIXED_GAS];
}

const { blockGasLimit, estimatedGas, simulationFails } = await estimateGas(
Expand All @@ -156,7 +157,7 @@ async function getGas(
? 'Using original estimate as custom network'
: 'Using original fallback estimate as simulation failed',
);
return [estimatedGas, simulationFails];
return [estimatedGas, simulationFails, estimatedGas];
}

const bufferMultiplier =
Expand All @@ -170,7 +171,7 @@ async function getGas(
bufferMultiplier,
);

return [bufferedGas, simulationFails];
return [bufferedGas, simulationFails, estimatedGas];
}

async function requiresFixedGas({
Expand Down

0 comments on commit 3b94a7c

Please sign in to comment.