Skip to content

Commit

Permalink
improve: Add gasPrice to TransactionCostEstimate type (#795)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholaspai authored Dec 16, 2024
1 parent 1aad2ed commit d794044
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 12 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.3.25",
"version": "3.3.26",
"license": "AGPL-3.0",
"homepage": "https://docs.across.to/reference/sdk",
"files": [
Expand Down
20 changes: 10 additions & 10 deletions src/relayFeeCalculator/chain-queries/baseQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,20 @@ export class QueryBase implements QueryInterface {
const gasTotalMultiplier = toBNWei(1.0 + gasMarkup);

const tx = await populateV3Relay(this.spokePool, deposit, relayer);
const { nativeGasCost, tokenGasCost } = await estimateTotalGasRequiredByUnsignedTransaction(
tx,
relayer,
this.provider,
{
gasPrice,
gasUnits,
transport,
}
);
const {
nativeGasCost,
tokenGasCost,
gasPrice: impliedGasPrice,
} = await estimateTotalGasRequiredByUnsignedTransaction(tx, relayer, this.provider, {
gasPrice,
gasUnits,
transport,
});

return {
nativeGasCost: nativeGasCost.mul(gasTotalMultiplier).div(fixedPointAdjustment),
tokenGasCost: tokenGasCost.mul(gasTotalMultiplier).div(fixedPointAdjustment),
gasPrice: impliedGasPrice,
};
}

Expand Down
4 changes: 4 additions & 0 deletions src/utils/FormattingUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ export function convertFromWei(weiVal: string, decimals: number): string {
return formatFunction(weiVal);
}

export function formatGwei(weiVal: string): string {
return convertFromWei(weiVal, 9);
}

/**
* Shortens a list of addresses to a shorter version with only the first 10 characters.
* @param addresses A list of addresses to shorten.
Expand Down
3 changes: 3 additions & 0 deletions src/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ export function retry<T>(call: () => Promise<T>, times: number, delayS: number):
export type TransactionCostEstimate = {
nativeGasCost: BigNumber; // Units: gas
tokenGasCost: BigNumber; // Units: wei (nativeGasCost * wei/gas)
gasPrice: BigNumber; // Units: wei/gas
};

/**
Expand Down Expand Up @@ -266,6 +267,7 @@ export async function estimateTotalGasRequiredByUnsignedTransaction(

// Estimate the Gas units required to submit this transaction.
const nativeGasCost = gasUnits ? BigNumber.from(gasUnits) : await voidSigner.estimateGas(unsignedTx);
assert(nativeGasCost.gt(0), "Gas cost should not be 0");
let tokenGasCost: BigNumber;

// OP stack is a special case; gas cost is computed by the SDK, without having to query price.
Expand Down Expand Up @@ -303,6 +305,7 @@ export async function estimateTotalGasRequiredByUnsignedTransaction(
return {
nativeGasCost, // Units: gas
tokenGasCost, // Units: wei (nativeGasCost * wei/gas)
gasPrice: tokenGasCost.div(nativeGasCost), // Units: wei/gas
};
}

Expand Down
4 changes: 3 additions & 1 deletion test/relayFeeCalculator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,11 @@ class ExampleQueries implements QueryInterface {
getGasCosts(): Promise<TransactionCostEstimate> {
const getGasCost = () => {
const { defaultGas: gasCost } = this;
const gasPrice = toGWei("1");
return {
nativeGasCost: toBN(gasCost),
tokenGasCost: toBN(gasCost).mul(toGWei("1")),
tokenGasCost: toBN(gasCost).mul(gasPrice),
gasPrice,
};
};

Expand Down

0 comments on commit d794044

Please sign in to comment.