Skip to content

Commit

Permalink
refactor: improve getGasCosts rpc calls (#660)
Browse files Browse the repository at this point in the history
  • Loading branch information
dohaki authored Jun 12, 2024
1 parent 8e32dab commit 1a4e5f2
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 17 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@across-protocol/sdk",
"author": "UMA Team",
"version": "3.0.1",
"version": "3.0.2",
"license": "AGPL-3.0",
"homepage": "https://docs.across.to/reference/sdk",
"files": [
Expand Down
15 changes: 7 additions & 8 deletions src/relayFeeCalculator/chain-queries/baseQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,16 @@ export class QueryBase implements QueryInterface {
* Retrieves the current gas costs of performing a fillRelay contract at the referenced SpokePool.
* @param deposit V3 deposit instance.
* @param relayerAddress Relayer address to simulate with.
* @param gasPrice Optional gas price to use for the simulation.
* @returns The gas estimate for this function call (multplied with the optional buffer).
*/
async getGasCosts(deposit: Deposit, relayer = DEFAULT_SIMULATED_RELAYER_ADDRESS): Promise<TransactionCostEstimate> {
async getGasCosts(
deposit: Deposit,
relayer = DEFAULT_SIMULATED_RELAYER_ADDRESS,
gasPrice = this.fixedGasPrice
): Promise<TransactionCostEstimate> {
const tx = await populateV3Relay(this.spokePool, deposit, relayer);
return estimateTotalGasRequiredByUnsignedTransaction(
tx,
relayer,
this.provider,
this.gasMarkup,
this.fixedGasPrice
);
return estimateTotalGasRequiredByUnsignedTransaction(tx, relayer, this.provider, this.gasMarkup, gasPrice);
}

/**
Expand Down
14 changes: 9 additions & 5 deletions src/relayFeeCalculator/relayFeeCalculator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {

// This needs to be implemented for every chain and passed into RelayFeeCalculator
export interface QueryInterface {
getGasCosts: (deposit: Deposit, relayer: string) => Promise<TransactionCostEstimate>;
getGasCosts: (deposit: Deposit, relayer: string, gasPrice?: BigNumberish) => Promise<TransactionCostEstimate>;
getTokenPrice: (tokenSymbol: string) => Promise<number>;
getTokenDecimals: (tokenSymbol: string) => number;
}
Expand Down Expand Up @@ -224,7 +224,8 @@ export class RelayFeeCalculator {
simulateZeroFill = false,
relayerAddress = DEFAULT_SIMULATED_RELAYER_ADDRESS,
_tokenPrice?: number,
tokenMapping = TOKEN_SYMBOLS_MAP
tokenMapping = TOKEN_SYMBOLS_MAP,
gasPrice?: BigNumberish
): Promise<BigNumber> {
if (toBN(amountToRelay).eq(bnZero)) return MAX_BIG_INT;

Expand All @@ -239,7 +240,7 @@ export class RelayFeeCalculator {
const simulatedAmount = simulateZeroFill ? safeOutputAmount : toBN(amountToRelay);
deposit = { ...deposit, outputAmount: simulatedAmount };

const getGasCosts = this.queries.getGasCosts(deposit, relayerAddress).catch((error) => {
const getGasCosts = this.queries.getGasCosts(deposit, relayerAddress, gasPrice).catch((error) => {
this.logger.error({
at: "sdk/gasFeePercent",
message: "Error while fetching gas costs",
Expand Down Expand Up @@ -343,7 +344,8 @@ export class RelayFeeCalculator {
amountToRelay?: BigNumberish,
simulateZeroFill = false,
relayerAddress = DEFAULT_SIMULATED_RELAYER_ADDRESS,
_tokenPrice?: number
_tokenPrice?: number,
gasPrice?: BigNumberish
): Promise<RelayerFeeDetails> {
// If the amount to relay is not provided, then we
// should use the full deposit amount.
Expand All @@ -359,7 +361,9 @@ export class RelayFeeCalculator {
amountToRelay,
simulateZeroFill,
relayerAddress,
_tokenPrice
_tokenPrice,
undefined,
gasPrice
);
const gasFeeTotal = gasFeePercent.mul(amountToRelay).div(fixedPointAdjustment);
const capitalFeePercent = this.capitalFeePercent(
Expand Down
15 changes: 12 additions & 3 deletions src/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,11 +267,20 @@ export async function estimateTotalGasRequiredByUnsignedTransaction(
if (chainIsOPStack(chainId)) {
assert(isOptimismL2Provider(provider), `Unexpected provider for chain ID ${chainId}.`);
assert(gasPrice === undefined, `Gas price (${gasPrice}) supplied for Optimism gas estimation (unused).`);
const populatedTransaction = await voidSigner.populateTransaction(unsignedTx);
tokenGasCost = await provider.estimateTotalGasCost(populatedTransaction);
const populatedTransaction = await voidSigner.populateTransaction({
...unsignedTx,
gasLimit: nativeGasCost, // prevents additional gas estimation call
});
// Concurrently estimate the gas cost on L1 and L2 instead of calling
// `provider.estimateTotalGasCost` to improve performance.
const [l1GasCost, l2GasCost] = await Promise.all([
provider.estimateL1GasCost(populatedTransaction),
provider.estimateL2GasCost(populatedTransaction),
]);
tokenGasCost = l1GasCost.add(l2GasCost);
} else {
if (!gasPrice) {
const gasPriceEstimate = await getGasPriceEstimate(provider);
const gasPriceEstimate = await getGasPriceEstimate(provider, chainId);
gasPrice = gasPriceEstimate.maxFeePerGas;
}
tokenGasCost = nativeGasCost.mul(gasPrice);
Expand Down

0 comments on commit 1a4e5f2

Please sign in to comment.