Skip to content

Commit

Permalink
Add gasPrice as required elem to profit client
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholaspai committed Dec 16, 2024
1 parent 7eaaf48 commit 2d845a6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
21 changes: 15 additions & 6 deletions src/clients/ProfitClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import { Deposit, DepositWithBlock, L1Token, SpokePoolClientsByChain } from "../
import { getAcrossHost } from "./AcrossAPIClient";
import { HubPoolClient } from "./HubPoolClient";

type TransactionCostEstimate = sdkUtils.TransactionCostEstimate & { gasPrice?: BigNumber };
export type ProfitClientTransactionCostEstimate = sdkUtils.TransactionCostEstimate & { gasPrice: BigNumber };

const { isError, isEthersError } = typeguards;
const { formatEther } = ethersUtils;
Expand Down Expand Up @@ -92,7 +92,7 @@ export class ProfitClient {
private unprofitableFills: { [chainId: number]: UnprofitableFill[] } = {};

// Track total gas costs of a relay on each chain.
protected totalGasCosts: { [chainId: number]: TransactionCostEstimate } = {};
protected totalGasCosts: { [chainId: number]: ProfitClientTransactionCostEstimate } = {};

// Queries needed to fetch relay gas costs.
private relayerFeeQueries: { [chainId: number]: relayFeeCalculator.QueryInterface } = {};
Expand Down Expand Up @@ -201,9 +201,14 @@ export class ProfitClient {
return price;
}

private async _getTotalGasCost(deposit: Deposit, relayer: string): Promise<TransactionCostEstimate> {
private async _getTotalGasCost(deposit: Deposit, relayer: string): Promise<ProfitClientTransactionCostEstimate> {
try {
return await this.relayerFeeQueries[deposit.destinationChainId].getGasCosts(deposit, relayer);
const totalGasCosts = await this.relayerFeeQueries[deposit.destinationChainId].getGasCosts(deposit, relayer);
const gasPrice = totalGasCosts.tokenGasCost.div(totalGasCosts.nativeGasCost);
return {
...totalGasCosts,
gasPrice,
}
} catch (err) {
const reason = isEthersError(err) ? err.reason : isError(err) ? err.message : "unknown error";
this.logger.warn({
Expand All @@ -213,11 +218,11 @@ export class ProfitClient {
deposit,
notificationPath: "across-unprofitable-fills",
});
return { nativeGasCost: uint256Max, tokenGasCost: uint256Max };
return { nativeGasCost: uint256Max, tokenGasCost: uint256Max, gasPrice: uint256Max };
}
}

async getTotalGasCost(deposit: Deposit): Promise<TransactionCostEstimate> {
async getTotalGasCost(deposit: Deposit): Promise<ProfitClientTransactionCostEstimate> {
const { destinationChainId: chainId } = deposit;

// If there's no attached message, gas consumption from previous fills can be used in most cases.
Expand All @@ -229,6 +234,10 @@ export class ProfitClient {
return this._getTotalGasCost(deposit, this.relayerAddress);
}

getGasCostsForChain(chainId: number): ProfitClientTransactionCostEstimate {
return this.totalGasCosts[chainId];
}

// Estimate the gas cost of filling this relay.
async estimateFillCost(
deposit: Deposit
Expand Down
10 changes: 4 additions & 6 deletions test/mocks/MockProfitClient.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { utils as sdkUtils } from "@across-protocol/sdk";
import { ProfitClient } from "../../src/clients";
import { ProfitClient, ProfitClientTransactionCostEstimate } from "../../src/clients";
import { SpokePoolClientsByChain } from "../../src/interfaces";
import { bnOne, isDefined, TOKEN_SYMBOLS_MAP } from "../../src/utils";
import { BigNumber, toBN, toBNWei, winston } from "../utils";
import { MockHubPoolClient } from "./MockHubPoolClient";

type TransactionCostEstimate = sdkUtils.TransactionCostEstimate;

const defaultFillCost = toBN(100_000); // gas
const defaultGasPrice = bnOne; // wei per gas

Expand Down Expand Up @@ -57,6 +54,7 @@ export class MockProfitClient extends ProfitClient {
const defaultGasCost = {
nativeGasCost: defaultFillCost,
tokenGasCost: defaultGasPrice.mul(defaultFillCost),
gasPrice: defaultGasPrice,
};
Object.values(spokePoolClients).map(({ chainId }) => {
this.setGasCost(chainId, defaultGasCost); // gas/fill
Expand Down Expand Up @@ -93,15 +91,15 @@ export class MockProfitClient extends ProfitClient {
});
}

setGasCost(chainId: number, gas?: TransactionCostEstimate): void {
setGasCost(chainId: number, gas?: ProfitClientTransactionCostEstimate): void {
if (gas) {
this.totalGasCosts[chainId] = gas;
} else {
delete this.totalGasCosts[chainId];
}
}

setGasCosts(gasCosts: { [chainId: number]: TransactionCostEstimate }): void {
setGasCosts(gasCosts: { [chainId: number]: ProfitClientTransactionCostEstimate }): void {
this.totalGasCosts = gasCosts;
}

Expand Down

0 comments on commit 2d845a6

Please sign in to comment.