Skip to content

Commit

Permalink
improve(RelayFeeCalculator): Add parameters to getGasCosts method (#818)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholaspai authored Jan 6, 2025
1 parent f120258 commit 4160385
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 9 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.4.4",
"version": "3.4.5",
"license": "AGPL-3.0",
"homepage": "https://docs.across.to/reference/sdk",
"files": [
Expand Down
30 changes: 26 additions & 4 deletions src/relayFeeCalculator/chain-queries/baseQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
bnZero,
assert,
chainIsOPStack,
fixedPointAdjustment,
} from "../../utils";
import { Logger, QueryInterface } from "../relayFeeCalculator";
import { Transport } from "viem";
Expand Down Expand Up @@ -76,10 +77,19 @@ export class QueryBase implements QueryInterface {
gasPrice: BigNumberish;
gasUnits: BigNumberish;
baseFeeMultiplier: BigNumber;
priorityFeeMultiplier: BigNumber;
opStackL1GasCostMultiplier: BigNumber;
transport: Transport;
}> = {}
): Promise<TransactionCostEstimate> {
const { gasPrice = this.fixedGasPrice, gasUnits, baseFeeMultiplier, transport } = options;
const {
gasPrice = this.fixedGasPrice,
gasUnits,
baseFeeMultiplier,
priorityFeeMultiplier,
opStackL1GasCostMultiplier,
transport,
} = options;

const tx = await populateV3Relay(this.spokePool, deposit, relayer);
const {
Expand All @@ -90,6 +100,8 @@ export class QueryBase implements QueryInterface {
gasPrice,
gasUnits,
baseFeeMultiplier,
priorityFeeMultiplier,
opStackL1GasCostMultiplier,
transport,
});

Expand Down Expand Up @@ -119,10 +131,19 @@ export class QueryBase implements QueryInterface {
gasPrice: BigNumberish;
gasUnits: BigNumberish;
baseFeeMultiplier: BigNumber;
priorityFeeMultiplier: BigNumber;
opStackL1GasCostMultiplier: BigNumber;
transport: Transport;
}> = {}
): Promise<TransactionCostEstimate> {
const { gasPrice: _gasPrice, gasUnits, baseFeeMultiplier = toBNWei("1"), transport } = options || {};
const {
gasPrice: _gasPrice,
gasUnits,
baseFeeMultiplier = toBNWei("1"),
priorityFeeMultiplier = toBNWei("1"),
opStackL1GasCostMultiplier = toBNWei("1"),
transport,
} = options || {};

const { chainId } = await provider.getNetwork();
const voidSigner = new VoidSigner(senderAddress, provider);
Expand All @@ -132,7 +153,7 @@ export class QueryBase implements QueryInterface {
gasUnits ? Promise.resolve(BigNumber.from(gasUnits)) : voidSigner.estimateGas(unsignedTx),
_gasPrice
? Promise.resolve({ maxFeePerGas: _gasPrice })
: getGasPriceEstimate(provider, { chainId, baseFeeMultiplier, transport, unsignedTx }),
: getGasPriceEstimate(provider, { chainId, baseFeeMultiplier, priorityFeeMultiplier, transport, unsignedTx }),
] as const;
const [nativeGasCost, { maxFeePerGas: gasPrice }] = await Promise.all(queries);
assert(nativeGasCost.gt(bnZero), "Gas cost should not be 0");
Expand All @@ -146,8 +167,9 @@ export class QueryBase implements QueryInterface {
gasLimit: nativeGasCost, // prevents additional gas estimation call
});
const l1GasCost = await (provider as L2Provider<providers.Provider>).estimateL1GasCost(populatedTransaction);
const scaledL1GasCost = l1GasCost.mul(opStackL1GasCostMultiplier).div(fixedPointAdjustment);
const l2GasCost = nativeGasCost.mul(gasPrice);
tokenGasCost = l1GasCost.add(l2GasCost);
tokenGasCost = scaledL1GasCost.add(l2GasCost);
} else {
tokenGasCost = nativeGasCost.mul(gasPrice);
}
Expand Down
14 changes: 10 additions & 4 deletions src/relayFeeCalculator/relayFeeCalculator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export interface QueryInterface {
gasPrice: BigNumberish;
gasUnits: BigNumberish;
baseFeeMultiplier: BigNumber;
priorityFeeMultiplier: BigNumber;
opStackL1GasCostMultiplier: BigNumber;
transport: Transport;
}>
) => Promise<TransactionCostEstimate>;
Expand Down Expand Up @@ -236,6 +238,7 @@ export class RelayFeeCalculator {
tokenMapping = TOKEN_SYMBOLS_MAP,
gasPrice?: BigNumberish,
gasLimit?: BigNumberish,
_tokenGasCost?: BigNumberish,
transport?: Transport
): Promise<BigNumber> {
if (toBN(amountToRelay).eq(bnZero)) return MAX_BIG_INT;
Expand All @@ -253,6 +256,7 @@ export class RelayFeeCalculator {

const getGasCosts = this.queries
.getGasCosts(deposit, relayerAddress, { gasPrice, gasUnits: gasLimit, transport })
.then(({ tokenGasCost }) => tokenGasCost)
.catch((error) => {
this.logger.error({
at: "sdk/gasFeePercent",
Expand All @@ -263,8 +267,8 @@ export class RelayFeeCalculator {
});
throw error;
});
const [{ tokenGasCost }, tokenPrice] = await Promise.all([
getGasCosts,
const [tokenGasCost, tokenPrice] = await Promise.all([
_tokenGasCost ? Promise.resolve(_tokenGasCost) : getGasCosts,
_tokenPrice ??
this.queries.getTokenPrice(token.symbol).catch((error) => {
this.logger.error({
Expand Down Expand Up @@ -360,7 +364,8 @@ export class RelayFeeCalculator {
relayerAddress = DEFAULT_SIMULATED_RELAYER_ADDRESS,
_tokenPrice?: number,
gasPrice?: BigNumberish,
gasUnits?: BigNumberish
gasUnits?: BigNumberish,
tokenGasCost?: BigNumberish
): Promise<RelayerFeeDetails> {
// If the amount to relay is not provided, then we
// should use the full deposit amount.
Expand All @@ -379,7 +384,8 @@ export class RelayFeeCalculator {
_tokenPrice,
undefined,
gasPrice,
gasUnits
gasUnits,
tokenGasCost
);
const gasFeeTotal = gasFeePercent.mul(amountToRelay).div(fixedPointAdjustment);
const capitalFeePercent = this.capitalFeePercent(
Expand Down
1 change: 1 addition & 0 deletions test/relayFeeCalculator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ describe("RelayFeeCalculator: Composable Bridging", function () {
tokenMap,
undefined,
undefined,
undefined,
customTransport
);
});
Expand Down

0 comments on commit 4160385

Please sign in to comment.