From 4af97846bb427ca9f605395f6cad7380ce34ab0a Mon Sep 17 00:00:00 2001 From: Andrew Morris Date: Fri, 2 Jun 2023 18:02:41 +1000 Subject: [PATCH] Add the L1 fee for optimism when calculating profit --- aggregator/src/app/BundleService.ts | 12 ++++++++++-- aggregator/src/app/EthereumService.ts | 18 +++++++++++++----- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/aggregator/src/app/BundleService.ts b/aggregator/src/app/BundleService.ts index 0e1207c9..928119bb 100644 --- a/aggregator/src/app/BundleService.ts +++ b/aggregator/src/app/BundleService.ts @@ -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(); @@ -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); diff --git a/aggregator/src/app/EthereumService.ts b/aggregator/src/app/EthereumService.ts index 4ad75fd5..9f871c77 100644 --- a/aggregator/src/app/EthereumService.ts +++ b/aggregator/src/app/EthereumService.ts @@ -318,7 +318,7 @@ export default class EthereumService { bundle: Bundle, maxAttempts = 1, retryDelay = 300, - ): Promise { + ): Promise { 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; +};