Add the L1 fee for optimism when calculating profit

This commit is contained in:
Andrew Morris
2023-06-02 18:02:41 +10:00
parent 534c5aa6ae
commit 4af97846bb
2 changed files with 23 additions and 7 deletions

View File

@@ -22,6 +22,7 @@ import BundleTable, { BundleRow, makeHash } from "./BundleTable.ts";
import plus from "./helpers/plus.ts";
import AggregationStrategy from "./AggregationStrategy.ts";
import nil from "../helpers/nil.ts";
import getOptimismL1Fee from "../helpers/getOptimismL1Fee.ts";
export type AddBundleResponse = { hash: string } | {
failures: TransactionFailure[];
@@ -34,6 +35,7 @@ export default class BundleService {
maxAggregationDelayMillis: env.MAX_AGGREGATION_DELAY_MILLIS,
maxUnconfirmedAggregations: env.MAX_UNCONFIRMED_AGGREGATIONS,
maxEligibilityDelay: env.MAX_ELIGIBILITY_DELAY,
isOptimism: env.IS_OPTIMISM,
};
unconfirmedBundles = new Set<Bundle>();
@@ -344,7 +346,7 @@ export default class BundleService {
try {
const balanceBefore = await this.ethereumService.wallet.getBalance();
const receipt = await this.ethereumService.submitBundle(
const { response, receipt } = await this.ethereumService.submitBundle(
aggregateBundle,
Infinity,
300,
@@ -363,7 +365,13 @@ export default class BundleService {
const profit = balanceAfter.sub(balanceBefore);
/** What we paid to process the bundle */
const cost = receipt.gasUsed.mul(receipt.effectiveGasPrice);
let cost = receipt.gasUsed.mul(receipt.effectiveGasPrice);
if (this.config.isOptimism) {
cost = cost.add(
await getOptimismL1Fee(this.ethereumService.provider, response),
);
}
/** Fees collected from users */
const actualFee = profit.add(cost);

View File

@@ -318,7 +318,7 @@ export default class EthereumService {
bundle: Bundle,
maxAttempts = 1,
retryDelay = 300,
): Promise<ethers.providers.TransactionReceipt> {
): Promise<ResponseAndReceipt> {
assert(bundle.operations.length > 0, "Cannot process empty bundle");
assert(maxAttempts > 0, "Must have at least one attempt");
@@ -345,10 +345,10 @@ export default class EthereumService {
};
const attempt = async () => {
let txResponse: ethers.providers.TransactionResponse;
let response: ethers.providers.TransactionResponse;
try {
txResponse = await this.wallet.sendTransaction(txRequest);
response = await this.wallet.sendTransaction(txRequest);
} catch (error) {
if (/\binvalid transaction nonce\b/.test(error.message)) {
// This can occur when the nonce is in the future, which can
@@ -364,7 +364,10 @@ export default class EthereumService {
}
try {
return { type: "receipt" as const, value: await txResponse.wait() };
return {
type: "complete" as const,
value: { response, receipt: await response.wait() },
};
} catch (error) {
return { type: "waitError" as const, value: error };
}
@@ -380,7 +383,7 @@ export default class EthereumService {
const attemptResult = await attempt();
if (attemptResult.type === "receipt") {
if (attemptResult.type === "complete") {
return attemptResult.value;
}
@@ -554,3 +557,8 @@ export default class EthereumService {
return wallet;
}
}
type ResponseAndReceipt = {
response: ethers.providers.TransactionResponse;
receipt: ethers.providers.TransactionReceipt;
};