Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove redundant gas price call in TransactionResponse #3536

Open
danielbate opened this issue Jan 3, 2025 · 4 comments · May be fixed by #3559
Open

Remove redundant gas price call in TransactionResponse #3536

danielbate opened this issue Jan 3, 2025 · 4 comments · May be fixed by #3559
Assignees
Labels
feat Issue is a feature

Comments

@danielbate
Copy link
Member

danielbate commented Jan 3, 2025

In instances where we are assembling a transaction summary for a transaction where we already have it's status, and therefore it's total fee, we are making a redundant network request to obtain the latest gas price. In these instances, we should instead build the response using the obtained fee, rather than recalculating it.

@danielbate danielbate added the feat Issue is a feature label Jan 3, 2025
@danielbate danielbate self-assigned this Jan 3, 2025
Copy link
Member

arboleya commented Jan 3, 2025

@danielbate What's the difference between what each method will return? Please, consider posting both returned objects in full so we can compare them. While I'm happy with the rationale, I'm not sure the API is quite there yet, but we can discuss it further. 🙂

@nedsalk
Copy link
Contributor

nedsalk commented Jan 3, 2025

Based on this check:

/**
 * If totalFee is provided it means that the TX was already processed and we could extract the fee
 * from its status
 */
if (totalFee) {
  return totalFee;
}

fetching the gasPrice isn't necessary for the API use case you have proposed as the transaction will always be processed because tx.waitForResult is called. What we can do to remove this call is we can conditionally fetch the gasPrice doing something like this:

const { totalFee } = processGraphqlStatus(this.status ?? this.gqlTransaction?.status);
const gasPrice = !totalFee && await this.provider.getLatestGasPrice();

@Torres-ssf
Copy link
Contributor

I agree with @nedsalk's suggestion, it seems the best approach.

Since TransactionResponse.getTransactionSummary is only called after a successful transaction result (i.e. when the status update is successful), the totalFee will always be available within the transaction status object. In this case fetching the gasPrice is not required.

And for cases where the user explicitly calls TransactionResponse.getTransactionSummary, the if statement suggested by @nedsalk should do the trick if the status has not been updated to successful yet.

@danielbate danielbate changed the title Introduce slim TransactionResponse Remove redundant gas price call in TransactionResponse Jan 6, 2025
@danielbate
Copy link
Member Author

This issue initially made the below proposition, but has been superseded by comments from @nedsalk and @Torres-ssf 👏🏻

Our current TransactionResponse instance contains a full transaction summary. This summary performs an additional network request to retrieve the latest gas price in order to compute the transaction fee. For an SDK consumer just concerned with the transaction success this could be a point of redundancy and unnecessary network lag.

We should introduce a slimmer version of the TransactionResponse that contains only what is available from transaction submitted transaction and it's returned receipts.

The proposed API:

const call = contract.functions.increment().call();
const response = await call.waitForResult({ slim: true });
// response.fee = undefined

const txRequest = wallet.createTransfer(recipient, 100));
const tx = wallet.sendTransaction(txRequest);
const response = await tx.waitForResult({ slim: true });
// response.fee = undefined

const txRequest = wallet.createTransfer(recipient, 100);
const tx = wallet.sendTransaction(txRequest);
const response = await tx.waitForResult();
// response.fee = 0x12

We do have the calculateTxFeeForSummary but we may want to make this more explicit (e.g. calculateTxFee) given this won't be so readily available in the response.

@danielbate danielbate linked a pull request Jan 7, 2025 that will close this issue
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat Issue is a feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants