Skip to content

Commit 8282eb4

Browse files
committed
added intergration tests
Signed-off-by: Nischal Sharma <[email protected]>
1 parent 0118ad6 commit 8282eb4

File tree

10 files changed

+160
-38
lines changed

10 files changed

+160
-38
lines changed

core/src/main/java/org/web3j/tx/Contract.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public abstract class Contract extends ManagedTransaction {
7070
* @see org.web3j.tx.gas.DefaultGasProvider
7171
*/
7272
public static final BigInteger GAS_LIMIT = BigInteger.valueOf(4_300_000);
73+
7374
public static final String BIN_NOT_PROVIDED = "Bin file was not provided";
7475
public static final String FUNC_DEPLOY = "deploy";
7576
private static final String ZERO_ADDRESS = "0x0000000000000000000000000000000000000000";
@@ -400,9 +401,11 @@ TransactionReceipt executeTransaction(
400401
contractAddress,
401402
data,
402403
weiValue,
403-
eip1559GasProvider.getGasLimit(Transaction.createEthCallTransaction(ZERO_ADDRESS, contractAddress, data)),
404-
eip1559GasProvider.getMaxPriorityFeePerGas(funcName),
405-
eip1559GasProvider.getMaxFeePerGas(funcName),
404+
eip1559GasProvider.getGasLimit(
405+
Transaction.createEthCallTransaction(
406+
ZERO_ADDRESS, contractAddress, data)),
407+
eip1559GasProvider.getMaxPriorityFeePerGas(),
408+
eip1559GasProvider.getMaxFeePerGas(),
406409
constructor);
407410
}
408411
}
@@ -413,8 +416,10 @@ TransactionReceipt executeTransaction(
413416
contractAddress,
414417
data,
415418
weiValue,
416-
gasProvider.getGasPrice(funcName),
417-
gasProvider.getGasLimit(Transaction.createEthCallTransaction(ZERO_ADDRESS, contractAddress, data)),
419+
gasProvider.getGasPrice(),
420+
gasProvider.getGasLimit(
421+
Transaction.createEthCallTransaction(
422+
ZERO_ADDRESS, contractAddress, data)),
418423
constructor);
419424
}
420425
} catch (JsonRpcError error) {

core/src/main/java/org/web3j/tx/gas/ContractEIP1559GasProvider.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public interface ContractEIP1559GasProvider extends ContractGasProvider {
1919

2020
long getChainId();
2121

22-
BigInteger getMaxFeePerGas(String contractFunc);
22+
BigInteger getMaxFeePerGas();
2323

24-
BigInteger getMaxPriorityFeePerGas(String contractFunc);
24+
BigInteger getMaxPriorityFeePerGas();
2525
}

core/src/main/java/org/web3j/tx/gas/ContractGasProvider.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
*/
1313
package org.web3j.tx.gas;
1414

15-
import org.web3j.protocol.core.methods.request.Transaction;
16-
1715
import java.math.BigInteger;
1816

17+
import org.web3j.protocol.core.methods.request.Transaction;
18+
1919
public interface ContractGasProvider {
2020

2121
BigInteger getGasPrice();

core/src/main/java/org/web3j/tx/gas/DynamicEIP1559GasProvider.java

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
1+
/*
2+
* Copyright 2025 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+
*/
113
package org.web3j.tx.gas;
214

15+
import java.io.IOException;
16+
import java.math.BigDecimal;
17+
import java.math.BigInteger;
18+
319
import org.web3j.protocol.Web3j;
420
import org.web3j.protocol.core.methods.request.Transaction;
521
import org.web3j.protocol.core.methods.response.EthGasPrice;
622
import org.web3j.protocol.core.methods.response.EthMaxPriorityFeePerGas;
723

8-
import java.io.IOException;
9-
import java.math.BigDecimal;
10-
import java.math.BigInteger;
11-
12-
public class DynamicEIP1559GasProvider implements ContractEIP1559GasProvider, PriorityGasProvider{
24+
public class DynamicEIP1559GasProvider implements ContractEIP1559GasProvider, PriorityGasProvider {
1325
private Web3j web3j;
1426
private long chainId;
1527
private final Priority priority;
@@ -23,7 +35,8 @@ public DynamicEIP1559GasProvider(Web3j web3j, long chainId, Priority priority) {
2335
this(web3j, chainId, priority, BigDecimal.ONE);
2436
}
2537

26-
public DynamicEIP1559GasProvider(Web3j web3j, long chainId, Priority priority, BigDecimal customMultiplier) {
38+
public DynamicEIP1559GasProvider(
39+
Web3j web3j, long chainId, Priority priority, BigDecimal customMultiplier) {
2740
this.web3j = web3j;
2841
this.chainId = chainId;
2942
this.priority = priority;
@@ -41,22 +54,24 @@ public long getChainId() {
4154
}
4255

4356
@Override
44-
public BigInteger getMaxFeePerGas(String contractFunc) {
57+
public BigInteger getMaxFeePerGas() {
4558
return getGasPrice();
4659
}
4760

4861
@Override
49-
public BigInteger getMaxPriorityFeePerGas(String contractFunc) {
62+
public BigInteger getMaxPriorityFeePerGas() {
5063
try {
51-
EthMaxPriorityFeePerGas ethMaxPriorityFeePerGas = web3j.ethMaxPriorityFeePerGas().send();
64+
EthMaxPriorityFeePerGas ethMaxPriorityFeePerGas =
65+
web3j.ethMaxPriorityFeePerGas().send();
5266
if (ethMaxPriorityFeePerGas.hasError()) {
53-
throw new RuntimeException("Error fetching ethMaxPriorityFeePerGas: " + ethMaxPriorityFeePerGas.getError().getMessage());
67+
throw new RuntimeException(
68+
"Error fetching ethMaxPriorityFeePerGas: "
69+
+ ethMaxPriorityFeePerGas.getError().getMessage());
5470
}
5571
return ethMaxPriorityFeePerGas.getMaxPriorityFeePerGas();
5672
} catch (IOException e) {
5773
throw new RuntimeException("Failed to get ethMaxPriorityFeePerGas");
5874
}
59-
6075
}
6176

6277
@Override
@@ -78,7 +93,8 @@ private BigInteger fetchCurrentGasPrice() {
7893
try {
7994
EthGasPrice ethGasPrice = web3j.ethGasPrice().send();
8095
if (ethGasPrice.hasError()) {
81-
throw new RuntimeException("Error fetching gas price: " + ethGasPrice.getError().getMessage());
96+
throw new RuntimeException(
97+
"Error fetching gas price: " + ethGasPrice.getError().getMessage());
8298
}
8399
return ethGasPrice.getGasPrice();
84100
} catch (Exception e) {

core/src/main/java/org/web3j/tx/gas/DynamicGasProvider.java

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
1+
/*
2+
* Copyright 2025 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+
*/
113
package org.web3j.tx.gas;
214

15+
import java.math.BigDecimal;
16+
import java.math.BigInteger;
17+
318
import org.web3j.protocol.Web3j;
19+
import org.web3j.protocol.core.methods.request.Transaction;
420
import org.web3j.protocol.core.methods.response.EthEstimateGas;
521
import org.web3j.protocol.core.methods.response.EthGasPrice;
6-
import org.web3j.protocol.core.methods.request.Transaction;
7-
8-
import java.io.IOException;
9-
import java.math.BigDecimal;
10-
import java.math.BigInteger;
1122

1223
public class DynamicGasProvider implements ContractGasProvider, PriorityGasProvider {
1324

@@ -43,20 +54,21 @@ public BigInteger getGasLimit(Transaction transaction) {
4354
try {
4455
EthEstimateGas ethEstimateGas = web3j.ethEstimateGas(transaction).send();
4556
if (ethEstimateGas.hasError()) {
46-
throw new RuntimeException("Error estimating gas limit: " + ethEstimateGas.getError().getMessage());
57+
throw new RuntimeException(
58+
"Error estimating gas limit: " + ethEstimateGas.getError().getMessage());
4759
}
4860
return ethEstimateGas.getAmountUsed();
4961
} catch (Exception e) {
5062
throw new RuntimeException("Failed to estimate gas limit", e);
5163
}
5264
}
5365

54-
5566
private BigInteger fetchCurrentGasPrice() {
5667
try {
5768
EthGasPrice ethGasPrice = web3j.ethGasPrice().send();
5869
if (ethGasPrice.hasError()) {
59-
throw new RuntimeException("Error fetching gas price: " + ethGasPrice.getError().getMessage());
70+
throw new RuntimeException(
71+
"Error fetching gas price: " + ethGasPrice.getError().getMessage());
6072
}
6173
return ethGasPrice.getGasPrice();
6274
} catch (Exception e) {

core/src/main/java/org/web3j/tx/gas/PriorityGasProvider.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
/*
2+
* Copyright 2025 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+
*/
113
package org.web3j.tx.gas;
214

315
import java.math.BigDecimal;
@@ -10,15 +22,18 @@ enum Priority {
1022
SLOW,
1123
CUSTOM
1224
}
13-
default BigInteger applyPriority(BigInteger baseGasPrice, Priority priority, BigDecimal customMultiplier) {
25+
26+
default BigInteger applyPriority(
27+
BigInteger baseGasPrice, Priority priority, BigDecimal customMultiplier) {
1428

1529
return switch (priority) {
1630
case FAST -> baseGasPrice.multiply(BigInteger.valueOf(2)); // 2x for fast
1731
case NORMAL -> baseGasPrice; // Default gas price
1832
case SLOW -> baseGasPrice.divide(BigInteger.valueOf(2)); // 0.5x for slow
1933
case CUSTOM -> {
2034
if (customMultiplier == null || customMultiplier.compareTo(BigDecimal.ZERO) <= 0) {
21-
throw new IllegalArgumentException("Custom multiplier must be a positive value");
35+
throw new IllegalArgumentException(
36+
"Custom multiplier must be a positive value");
2237
}
2338
yield new BigDecimal(baseGasPrice).multiply(customMultiplier).toBigInteger();
2439
}

core/src/main/java/org/web3j/tx/gas/StaticEIP1559GasProvider.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
*/
1313
package org.web3j.tx.gas;
1414

15-
import org.web3j.protocol.core.methods.request.Transaction;
16-
1715
import java.math.BigInteger;
1816

17+
import org.web3j.protocol.core.methods.request.Transaction;
18+
1919
public class StaticEIP1559GasProvider implements ContractEIP1559GasProvider {
2020
private long chainId;
2121
private BigInteger maxFeePerGas;
@@ -59,12 +59,12 @@ public long getChainId() {
5959
}
6060

6161
@Override
62-
public BigInteger getMaxFeePerGas(String contractFunc) {
62+
public BigInteger getMaxFeePerGas() {
6363
return maxFeePerGas;
6464
}
6565

6666
@Override
67-
public BigInteger getMaxPriorityFeePerGas(String contractFunc) {
67+
public BigInteger getMaxPriorityFeePerGas() {
6868
return maxPriorityFeePerGas;
6969
}
7070
}

core/src/main/java/org/web3j/tx/gas/StaticGasProvider.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
*/
1313
package org.web3j.tx.gas;
1414

15-
import org.web3j.protocol.core.methods.request.Transaction;
16-
1715
import java.math.BigInteger;
1816

17+
import org.web3j.protocol.core.methods.request.Transaction;
18+
1919
@SuppressWarnings("deprecation")
2020
public class StaticGasProvider implements ContractGasProvider {
2121
private BigInteger gasPrice;

integration-tests/src/test/java/org/web3j/protocol/scenarios/ArraysIT.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ public class ArraysIT extends Scenario {
3939

4040
@BeforeAll
4141
public static void setUp(Web3j web3j) throws Exception {
42-
Scenario.web3j = web3j;
4342
ArraysIT.contract =
4443
Arrays.deploy(
4544
web3j,
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright 2025 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.scenarios;
14+
15+
import java.math.BigInteger;
16+
17+
import org.junit.jupiter.api.BeforeAll;
18+
import org.junit.jupiter.api.Test;
19+
20+
import org.web3j.EVMTest;
21+
import org.web3j.NodeType;
22+
import org.web3j.protocol.Web3j;
23+
import org.web3j.test.contract.HumanStandardToken;
24+
import org.web3j.tx.gas.ContractGasProvider;
25+
import org.web3j.tx.gas.DynamicGasProvider;
26+
27+
import static org.junit.jupiter.api.Assertions.assertEquals;
28+
import static org.junit.jupiter.api.Assertions.assertNotNull;
29+
30+
@EVMTest(type = NodeType.BESU)
31+
public class DynamicGasProviderIT extends Scenario {
32+
static ContractGasProvider dynamicGasProvider;
33+
static String contractAddress;
34+
35+
@BeforeAll
36+
static void setUp(Web3j web3j) throws Exception {
37+
dynamicGasProvider = new DynamicGasProvider(web3j);
38+
contractAddress = sendTransaction();
39+
}
40+
41+
@Test
42+
public void testContractCreation() {
43+
HumanStandardToken humanStandardToken =
44+
HumanStandardToken.load(contractAddress, web3j, ALICE, dynamicGasProvider);
45+
46+
assertNotNull(humanStandardToken.getTransactionReceipt().get());
47+
assertEquals(
48+
humanStandardToken.getTransactionReceipt().get().getContractAddress(),
49+
contractAddress);
50+
}
51+
52+
@Test
53+
public void callSmartContractFunction() throws Exception {
54+
HumanStandardToken humanStandardToken =
55+
HumanStandardToken.load(contractAddress, web3j, ALICE, dynamicGasProvider);
56+
57+
assertEquals(humanStandardToken.name().send(), "Alice Token");
58+
59+
assertNotNull(humanStandardToken.transfer(BOB.getAddress(), BigInteger.ONE).send());
60+
assertEquals(humanStandardToken.balanceOf(BOB.getAddress()).send(), BigInteger.ONE);
61+
}
62+
63+
private static String sendTransaction() throws Exception {
64+
return HumanStandardToken.deploy(
65+
web3j,
66+
ALICE,
67+
dynamicGasProvider,
68+
BigInteger.valueOf(100L),
69+
"Alice Token",
70+
BigInteger.valueOf(18L),
71+
"ATK")
72+
.send()
73+
.getContractAddress();
74+
}
75+
}

0 commit comments

Comments
 (0)