Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
Signed-off-by: Fabio Di Fabio <[email protected]>
  • Loading branch information
fab-10 committed May 24, 2024
1 parent db386f0 commit 4670ab7
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
*/
package net.consensys.linea.bl;

import static net.consensys.linea.config.LineaProfitabilityConfiguration.WEI_IN_KWEI;

import java.math.BigDecimal;

import lombok.extern.slf4j.Slf4j;
Expand All @@ -25,7 +27,6 @@

@Slf4j
public class TransactionProfitabilityCalculator {
private static final long TO_WEI_MULTIPLIER = 1_000;
private final LineaProfitabilityConfiguration profitabilityConf;

public TransactionProfitabilityCalculator(
Expand All @@ -37,19 +38,19 @@ public Wei profitablePriorityFeePerGas(
final Transaction transaction,
final double minMargin,
final long gas,
final Wei minGasPrice) {
final Wei minGasPriceWei) {
final int compressedTxSize = getCompressedTxSize(transaction);

final long variableCostKWei =
profitabilityConf.extraDataPricingEnabled()
? profitabilityConf.variableCostKWei()
: minGasPrice.divide(TO_WEI_MULTIPLIER).toLong();
: minGasPriceWei.divide(WEI_IN_KWEI).toLong();

final var profitAtKWei =
minMargin * (variableCostKWei * compressedTxSize / gas + profitabilityConf.fixedCostKWei());

final var profitAtWei =
Wei.ofNumber(BigDecimal.valueOf(profitAtKWei).toBigInteger()).multiply(TO_WEI_MULTIPLIER);
Wei.ofNumber(BigDecimal.valueOf(profitAtKWei).toBigInteger()).multiply(WEI_IN_KWEI);

log.atDebug()
.setMessage(
Expand All @@ -73,10 +74,10 @@ public boolean isProfitable(
final double minMargin,
final Wei effectiveGasPrice,
final long gas,
final Wei minGasPrice) {
final Wei minGasPriceWei) {

final Wei profitablePriorityFee =
profitablePriorityFeePerGas(transaction, minMargin, gas, minGasPrice);
profitablePriorityFeePerGas(transaction, minMargin, gas, minGasPriceWei);

if (effectiveGasPrice.lessThan(profitablePriorityFee)) {
log(
Expand All @@ -87,7 +88,7 @@ public boolean isProfitable(
effectiveGasPrice,
profitablePriorityFee,
gas,
minGasPrice);
minGasPriceWei);
return false;
}

Expand All @@ -99,7 +100,7 @@ public boolean isProfitable(
effectiveGasPrice,
profitablePriorityFee,
gas,
minGasPrice);
minGasPriceWei);
return true;
}

Expand All @@ -116,7 +117,7 @@ private void log(
final Wei effectiveGasPrice,
final Wei profitableGasPrice,
final long gasUsed,
final Wei minGasPrice) {
final Wei minGasPriceWei) {

leb.setMessage(
"Context {}. Transaction {} has a margin of {}, minMargin={}, effectiveGasPrice={},"
Expand All @@ -136,7 +137,7 @@ private void log(
() ->
profitabilityConf.extraDataPricingEnabled()
? profitabilityConf.variableCostKWei()
: minGasPrice.divide(TO_WEI_MULTIPLIER).toLong())
: minGasPriceWei.divide(WEI_IN_KWEI).toLong())
.addArgument(gasUsed)
.log();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
@Getter
@ToString
public class LineaProfitabilityConfiguration {
public static final int WEI_IN_KWEI = 1_000;

private long fixedCostKWei;
private long variableCostKWei;
private double minMargin;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

package net.consensys.linea.extradata;

import static net.consensys.linea.config.LineaProfitabilityConfiguration.WEI_IN_KWEI;

import java.util.function.Consumer;
import java.util.function.Function;

Expand Down Expand Up @@ -119,7 +121,7 @@ public synchronized void parse(final Bytes extraData) {
}

void updateMinGasPrice(final Long minGasPriceKWei) {
final var minGasPriceWei = Wei.of(minGasPriceKWei).multiply(1000);
final var minGasPriceWei = Wei.of(minGasPriceKWei).multiply(WEI_IN_KWEI);
final var resp =
rpcEndpointService.call(
"miner_setMinGasPrice", new Object[] {minGasPriceWei.toShortHexString()});
Expand Down

0 comments on commit 4670ab7

Please sign in to comment.