Skip to content

Commit 7b9bc40

Browse files
committed
new approach to calculate blobBaseFee
Signed-off-by: Nischal Sharma <[email protected]>
1 parent a87c302 commit 7b9bc40

File tree

11 files changed

+225
-43
lines changed

11 files changed

+225
-43
lines changed

core/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ dependencies {
2121
"io.github.adraffy:ens-normalize:$ensAdraffyVersion",
2222
"io.tmio:tuweni-bytes:$tuweniVersion",
2323
"io.tmio:tuweni-units:$tuweniVersion"
24+
"io.vertx:vertx-core:YourVertxVersion:4.4.6"
2425
testImplementation project(path: ':crypto', configuration: 'testArtifacts'),
2526
"nl.jqno.equalsverifier:equalsverifier:$equalsverifierVersion",
2627
"ch.qos.logback:logback-classic:$logbackVersion"

core/src/main/java/org/web3j/protocol/Web3j.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@
1515
import java.util.concurrent.ScheduledExecutorService;
1616

1717
import org.web3j.protocol.core.Batcher;
18+
import org.web3j.protocol.core.BlobFee;
1819
import org.web3j.protocol.core.Ethereum;
1920
import org.web3j.protocol.core.JsonRpc2_0Web3j;
2021
import org.web3j.protocol.rx.Web3jRx;
2122

2223
/** JSON-RPC Request object building factory. */
23-
public interface Web3j extends Ethereum, Web3jRx, Batcher {
24+
public interface Web3j extends Ethereum, Web3jRx, Batcher, BlobFee {
2425

2526
/**
2627
* Construct a new Web3j instance.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright 2024 Web3 Labs Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5+
* the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
* specific language governing permissions and limitations under the License.
12+
*/
13+
package org.web3j.protocol.core;
14+
15+
import java.math.BigInteger;
16+
17+
public interface BlobFee {
18+
/**
19+
* Calculating Base Fee per Blob Gas
20+
*
21+
* @return baseFee value.
22+
*/
23+
BigInteger ethGetBaseFeePerBlobGas();
24+
}

core/src/main/java/org/web3j/protocol/core/JsonRpc2_0Web3j.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@
9999
public class JsonRpc2_0Web3j implements Web3j {
100100

101101
public static final int DEFAULT_BLOCK_TIME = 15 * 1000;
102+
private static final BigInteger MIN_BLOB_BASE_FEE = new BigInteger("1");
103+
private static final BigInteger BLOB_BASE_FEE_UPDATE_FRACTION = new BigInteger("3338477");
102104

103105
protected final Web3jService web3jService;
104106
private final JsonRpc2_0Rx web3jRx;
@@ -876,4 +878,32 @@ public void shutdown() {
876878
public BatchRequest newBatch() {
877879
return new BatchRequest(web3jService);
878880
}
881+
882+
@Override
883+
public BigInteger ethGetBaseFeePerBlobGas() {
884+
try {
885+
EthBlock ethBlock =
886+
web3jService.send(
887+
ethGetBlockByNumber(DefaultBlockParameter.valueOf("latest"), false),
888+
EthBlock.class);
889+
return fakeExponential(ethBlock.getBlock().getExcessBlobGas());
890+
} catch (IOException e) {
891+
throw new RuntimeException("Failed to get baseFeePerBlobGas value", e);
892+
}
893+
}
894+
895+
private static BigInteger fakeExponential(BigInteger numerator) {
896+
BigInteger i = BigInteger.ONE;
897+
BigInteger output = BigInteger.ZERO;
898+
BigInteger numeratorAccum = MIN_BLOB_BASE_FEE.multiply(BLOB_BASE_FEE_UPDATE_FRACTION);
899+
while (numeratorAccum.compareTo(BigInteger.ZERO) > 0) {
900+
output = output.add(numeratorAccum);
901+
numeratorAccum =
902+
numeratorAccum
903+
.multiply(numerator)
904+
.divide(BLOB_BASE_FEE_UPDATE_FRACTION.multiply(i));
905+
i = i.add(BigInteger.ONE);
906+
}
907+
return output.divide(BLOB_BASE_FEE_UPDATE_FRACTION);
908+
}
879909
}

core/src/main/java/org/web3j/protocol/core/methods/request/Transaction.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,9 @@
1313
package org.web3j.protocol.core.methods.request;
1414

1515
import java.math.BigInteger;
16-
import java.util.List;
1716

1817
import com.fasterxml.jackson.annotation.JsonInclude;
1918

20-
import org.apache.tuweni.bytes.Bytes;
2119
import org.web3j.utils.Numeric;
2220

2321
/**

core/src/main/java/org/web3j/protocol/core/methods/response/EthBlock.java

Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import org.web3j.protocol.ObjectMapperFactory;
3030
import org.web3j.protocol.core.Response;
3131
import org.web3j.utils.Numeric;
32-
import org.apache.tuweni.bytes.Bytes;
3332

3433
/**
3534
* Block object returned by:
@@ -147,6 +146,59 @@ public Block(
147146
this.excessBlobGas = excessBlobGas;
148147
}
149148

149+
public Block(
150+
String number,
151+
String hash,
152+
String parentHash,
153+
String nonce,
154+
String sha3Uncles,
155+
String logsBloom,
156+
String transactionsRoot,
157+
String stateRoot,
158+
String receiptsRoot,
159+
String author,
160+
String miner,
161+
String mixHash,
162+
String difficulty,
163+
String totalDifficulty,
164+
String extraData,
165+
String size,
166+
String gasLimit,
167+
String gasUsed,
168+
String timestamp,
169+
List<TransactionResult> transactions,
170+
List<String> uncles,
171+
List<String> sealFields,
172+
String baseFeePerGas,
173+
String withdrawalsRoot,
174+
List<Withdrawal> withdrawals) {
175+
this.number = number;
176+
this.hash = hash;
177+
this.parentHash = parentHash;
178+
this.nonce = nonce;
179+
this.sha3Uncles = sha3Uncles;
180+
this.logsBloom = logsBloom;
181+
this.transactionsRoot = transactionsRoot;
182+
this.stateRoot = stateRoot;
183+
this.receiptsRoot = receiptsRoot;
184+
this.author = author;
185+
this.miner = miner;
186+
this.mixHash = mixHash;
187+
this.difficulty = difficulty;
188+
this.totalDifficulty = totalDifficulty;
189+
this.extraData = extraData;
190+
this.size = size;
191+
this.gasLimit = gasLimit;
192+
this.gasUsed = gasUsed;
193+
this.timestamp = timestamp;
194+
this.transactions = transactions;
195+
this.uncles = uncles;
196+
this.sealFields = sealFields;
197+
this.baseFeePerGas = baseFeePerGas;
198+
this.withdrawalsRoot = withdrawalsRoot;
199+
this.withdrawals = withdrawals;
200+
}
201+
150202
public BigInteger getNumber() {
151203
return Numeric.decodeQuantity(number);
152204
}
@@ -556,8 +608,6 @@ public boolean equals(Object o) {
556608
return getWithdrawals() != null
557609
? getWithdrawals().equals(block.getWithdrawals())
558610
: block.getWithdrawals() == null;
559-
560-
561611
}
562612

563613
@Override
@@ -601,8 +651,14 @@ public int hashCode() {
601651
31 * result
602652
+ (getWithdrawalsRoot() != null ? getWithdrawalsRoot().hashCode() : 0);
603653
result = 31 * result + (getWithdrawals() != null ? getWithdrawals().hashCode() : 0);
604-
result = 31 * result + (getBlobGasUsedRaw() != null ? getBlobGasUsedRaw().hashCode() : 0);
605-
result = 31 * result + (getExcessBlobGasRaw() != null ? getExcessBlobGasRaw().hashCode() : 0);
654+
result =
655+
31 * result
656+
+ (getBlobGasUsedRaw() != null ? getBlobGasUsedRaw().hashCode() : 0);
657+
result =
658+
31 * result
659+
+ (getExcessBlobGasRaw() != null
660+
? getExcessBlobGasRaw().hashCode()
661+
: 0);
606662
return result;
607663
}
608664
}
@@ -776,7 +832,7 @@ public TransactionObject(
776832
String maxPriorityFeePerGas,
777833
List<AccessListObject> accessList,
778834
String maxFeePerBlobGas,
779-
List<Bytes> versionedHashes) {
835+
List<String> versionedHashes) {
780836
super(
781837
hash,
782838
nonce,

core/src/main/java/org/web3j/protocol/core/methods/response/Transaction.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import java.math.BigInteger;
1616
import java.util.List;
1717

18-
import org.apache.tuweni.bytes.Bytes;
1918
import org.web3j.crypto.TransactionUtils;
2019
import org.web3j.utils.Numeric;
2120

@@ -45,7 +44,7 @@ public class Transaction {
4544
private String maxPriorityFeePerGas;
4645
private List<AccessListObject> accessList;
4746
private String maxFeePerBlobGas;
48-
private List<Bytes> versionedHashes;
47+
private List<String> versionedHashes;
4948

5049
public Transaction() {}
5150

@@ -170,7 +169,7 @@ public Transaction(
170169
String maxPriorityFeePerGas,
171170
List accessList,
172171
String maxFeePerBlobGas,
173-
List<Bytes> versionedHashes) {
172+
List versionedHashes) {
174173
this.hash = hash;
175174
this.nonce = nonce;
176175
this.blockHash = blockHash;
@@ -450,11 +449,11 @@ public void setMaxFeePerBlobGas(String maxFeePerBlobGas) {
450449
this.maxFeePerBlobGas = maxFeePerBlobGas;
451450
}
452451

453-
public List<Bytes> getVersionedHashes() {
452+
public List<String> getVersionedHashes() {
454453
return versionedHashes;
455454
}
456455

457-
public void setVersionedHashes(List<Bytes> versionedHashes) {
456+
public void setVersionedHashes(List<String> versionedHashes) {
458457
this.versionedHashes = versionedHashes;
459458
}
460459

@@ -611,7 +610,11 @@ public int hashCode() {
611610
+ (getMaxPriorityFeePerGasRaw() != null
612611
? getMaxPriorityFeePerGasRaw().hashCode()
613612
: 0);
614-
result = 31 * result + (getMaxFeePerBlobGasRaw() != null ? getMaxFeePerBlobGasRaw().hashCode() : 0);
613+
result =
614+
31 * result
615+
+ (getMaxFeePerBlobGasRaw() != null
616+
? getMaxFeePerBlobGasRaw().hashCode()
617+
: 0);
615618
result = 31 * result + (getVersionedHashes() != null ? getVersionedHashes().hashCode() : 0);
616619
result = 31 * result + (getAccessList() != null ? getAccessList().hashCode() : 0);
617620
return result;

core/src/main/java/org/web3j/protocol/core/methods/response/TransactionReceipt.java

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ public class TransactionReceipt {
3737
private String revertReason;
3838
private String type;
3939
private String effectiveGasPrice;
40+
private String blobGasPrice;
41+
private String blobGasUsed;
4042

4143
public TransactionReceipt() {}
4244

@@ -75,6 +77,45 @@ public TransactionReceipt(
7577
this.effectiveGasPrice = effectiveGasPrice;
7678
}
7779

80+
public TransactionReceipt(
81+
String transactionHash,
82+
String transactionIndex,
83+
String blockHash,
84+
String blockNumber,
85+
String cumulativeGasUsed,
86+
String gasUsed,
87+
String contractAddress,
88+
String root,
89+
String status,
90+
String from,
91+
String to,
92+
List<Log> logs,
93+
String logsBloom,
94+
String revertReason,
95+
String type,
96+
String effectiveGasPrice,
97+
String blobGasPrice,
98+
String blobGasUsed) {
99+
this.transactionHash = transactionHash;
100+
this.transactionIndex = transactionIndex;
101+
this.blockHash = blockHash;
102+
this.blockNumber = blockNumber;
103+
this.cumulativeGasUsed = cumulativeGasUsed;
104+
this.gasUsed = gasUsed;
105+
this.contractAddress = contractAddress;
106+
this.root = root;
107+
this.status = status;
108+
this.from = from;
109+
this.to = to;
110+
this.logs = logs;
111+
this.logsBloom = logsBloom;
112+
this.revertReason = revertReason;
113+
this.type = type;
114+
this.effectiveGasPrice = effectiveGasPrice;
115+
this.blobGasPrice = blobGasPrice;
116+
this.blobGasUsed = blobGasUsed;
117+
}
118+
78119
public String getTransactionHash() {
79120
return transactionHash;
80121
}
@@ -227,6 +268,22 @@ public void setEffectiveGasPrice(String effectiveGasPrice) {
227268
this.effectiveGasPrice = effectiveGasPrice;
228269
}
229270

271+
public String getBlobGasPrice() {
272+
return blobGasPrice;
273+
}
274+
275+
public void setBlobGasPrice(String blobGasPrice) {
276+
this.blobGasPrice = blobGasPrice;
277+
}
278+
279+
public String getBlobGasUsed() {
280+
return blobGasUsed;
281+
}
282+
283+
public void setBlobGasUsed(String blobGasUsed) {
284+
this.blobGasUsed = blobGasUsed;
285+
}
286+
230287
@Override
231288
public boolean equals(Object o) {
232289
if (this == o) {
@@ -302,6 +359,18 @@ public boolean equals(Object o) {
302359
: that.getEffectiveGasPrice() != null) {
303360
return false;
304361
}
362+
363+
if (blobGasPrice != null
364+
? !blobGasPrice.equals(that.blobGasPrice)
365+
: that.blobGasPrice != null) {
366+
return false;
367+
}
368+
369+
if (blobGasUsed != null
370+
? !blobGasUsed.equals(that.blobGasUsed)
371+
: that.blobGasUsed != null) {
372+
return false;
373+
}
305374
return getRevertReason() != null
306375
? getRevertReason().equals(that.getRevertReason())
307376
: that.getRevertReason() == null;
@@ -327,6 +396,8 @@ public int hashCode() {
327396
result =
328397
31 * result
329398
+ (getEffectiveGasPrice() != null ? getEffectiveGasPrice().hashCode() : 0);
399+
result = 31 * result + (blobGasPrice != null ? blobGasPrice.hashCode() : 0);
400+
result = 31 * result + (blobGasUsed != null ? blobGasUsed.hashCode() : 0);
330401
return result;
331402
}
332403

@@ -380,6 +451,12 @@ public String toString() {
380451
+ ", effectiveGasPrice='"
381452
+ effectiveGasPrice
382453
+ '\''
454+
+ ", blobGasPrice='"
455+
+ blobGasPrice
456+
+ '\''
457+
+ ", blobGasused='"
458+
+ blobGasUsed
459+
+ '\''
383460
+ '}';
384461
}
385462
}

0 commit comments

Comments
 (0)