Skip to content

Commit

Permalink
added intergration tests
Browse files Browse the repository at this point in the history
Signed-off-by: Nischal Sharma <[email protected]>
  • Loading branch information
NickSneo committed Jan 20, 2025
1 parent 0118ad6 commit 8282eb4
Show file tree
Hide file tree
Showing 10 changed files with 160 additions and 38 deletions.
15 changes: 10 additions & 5 deletions core/src/main/java/org/web3j/tx/Contract.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public abstract class Contract extends ManagedTransaction {
* @see org.web3j.tx.gas.DefaultGasProvider
*/
public static final BigInteger GAS_LIMIT = BigInteger.valueOf(4_300_000);

public static final String BIN_NOT_PROVIDED = "Bin file was not provided";
public static final String FUNC_DEPLOY = "deploy";
private static final String ZERO_ADDRESS = "0x0000000000000000000000000000000000000000";
Expand Down Expand Up @@ -400,9 +401,11 @@ TransactionReceipt executeTransaction(
contractAddress,
data,
weiValue,
eip1559GasProvider.getGasLimit(Transaction.createEthCallTransaction(ZERO_ADDRESS, contractAddress, data)),
eip1559GasProvider.getMaxPriorityFeePerGas(funcName),
eip1559GasProvider.getMaxFeePerGas(funcName),
eip1559GasProvider.getGasLimit(
Transaction.createEthCallTransaction(
ZERO_ADDRESS, contractAddress, data)),
eip1559GasProvider.getMaxPriorityFeePerGas(),
eip1559GasProvider.getMaxFeePerGas(),
constructor);
}
}
Expand All @@ -413,8 +416,10 @@ TransactionReceipt executeTransaction(
contractAddress,
data,
weiValue,
gasProvider.getGasPrice(funcName),
gasProvider.getGasLimit(Transaction.createEthCallTransaction(ZERO_ADDRESS, contractAddress, data)),
gasProvider.getGasPrice(),
gasProvider.getGasLimit(
Transaction.createEthCallTransaction(
ZERO_ADDRESS, contractAddress, data)),
constructor);
}
} catch (JsonRpcError error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public interface ContractEIP1559GasProvider extends ContractGasProvider {

long getChainId();

BigInteger getMaxFeePerGas(String contractFunc);
BigInteger getMaxFeePerGas();

BigInteger getMaxPriorityFeePerGas(String contractFunc);
BigInteger getMaxPriorityFeePerGas();
}
4 changes: 2 additions & 2 deletions core/src/main/java/org/web3j/tx/gas/ContractGasProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
*/
package org.web3j.tx.gas;

import org.web3j.protocol.core.methods.request.Transaction;

import java.math.BigInteger;

import org.web3j.protocol.core.methods.request.Transaction;

public interface ContractGasProvider {

BigInteger getGasPrice();
Expand Down
40 changes: 28 additions & 12 deletions core/src/main/java/org/web3j/tx/gas/DynamicEIP1559GasProvider.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
/*
* Copyright 2025 Web3 Labs Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package org.web3j.tx.gas;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;

import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.methods.request.Transaction;
import org.web3j.protocol.core.methods.response.EthGasPrice;
import org.web3j.protocol.core.methods.response.EthMaxPriorityFeePerGas;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;

public class DynamicEIP1559GasProvider implements ContractEIP1559GasProvider, PriorityGasProvider{
public class DynamicEIP1559GasProvider implements ContractEIP1559GasProvider, PriorityGasProvider {
private Web3j web3j;
private long chainId;
private final Priority priority;
Expand All @@ -23,7 +35,8 @@ public DynamicEIP1559GasProvider(Web3j web3j, long chainId, Priority priority) {
this(web3j, chainId, priority, BigDecimal.ONE);
}

public DynamicEIP1559GasProvider(Web3j web3j, long chainId, Priority priority, BigDecimal customMultiplier) {
public DynamicEIP1559GasProvider(
Web3j web3j, long chainId, Priority priority, BigDecimal customMultiplier) {
this.web3j = web3j;
this.chainId = chainId;
this.priority = priority;
Expand All @@ -41,22 +54,24 @@ public long getChainId() {
}

@Override
public BigInteger getMaxFeePerGas(String contractFunc) {
public BigInteger getMaxFeePerGas() {
return getGasPrice();
}

@Override
public BigInteger getMaxPriorityFeePerGas(String contractFunc) {
public BigInteger getMaxPriorityFeePerGas() {
try {
EthMaxPriorityFeePerGas ethMaxPriorityFeePerGas = web3j.ethMaxPriorityFeePerGas().send();
EthMaxPriorityFeePerGas ethMaxPriorityFeePerGas =
web3j.ethMaxPriorityFeePerGas().send();
if (ethMaxPriorityFeePerGas.hasError()) {
throw new RuntimeException("Error fetching ethMaxPriorityFeePerGas: " + ethMaxPriorityFeePerGas.getError().getMessage());
throw new RuntimeException(
"Error fetching ethMaxPriorityFeePerGas: "
+ ethMaxPriorityFeePerGas.getError().getMessage());
}
return ethMaxPriorityFeePerGas.getMaxPriorityFeePerGas();
} catch (IOException e) {
throw new RuntimeException("Failed to get ethMaxPriorityFeePerGas");
}

}

@Override
Expand All @@ -78,7 +93,8 @@ private BigInteger fetchCurrentGasPrice() {
try {
EthGasPrice ethGasPrice = web3j.ethGasPrice().send();
if (ethGasPrice.hasError()) {
throw new RuntimeException("Error fetching gas price: " + ethGasPrice.getError().getMessage());
throw new RuntimeException(
"Error fetching gas price: " + ethGasPrice.getError().getMessage());
}
return ethGasPrice.getGasPrice();
} catch (Exception e) {
Expand Down
28 changes: 20 additions & 8 deletions core/src/main/java/org/web3j/tx/gas/DynamicGasProvider.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
/*
* Copyright 2025 Web3 Labs Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package org.web3j.tx.gas;

import java.math.BigDecimal;
import java.math.BigInteger;

import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.methods.request.Transaction;
import org.web3j.protocol.core.methods.response.EthEstimateGas;
import org.web3j.protocol.core.methods.response.EthGasPrice;
import org.web3j.protocol.core.methods.request.Transaction;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;

public class DynamicGasProvider implements ContractGasProvider, PriorityGasProvider {

Expand Down Expand Up @@ -43,20 +54,21 @@ public BigInteger getGasLimit(Transaction transaction) {
try {
EthEstimateGas ethEstimateGas = web3j.ethEstimateGas(transaction).send();
if (ethEstimateGas.hasError()) {
throw new RuntimeException("Error estimating gas limit: " + ethEstimateGas.getError().getMessage());
throw new RuntimeException(
"Error estimating gas limit: " + ethEstimateGas.getError().getMessage());
}
return ethEstimateGas.getAmountUsed();
} catch (Exception e) {
throw new RuntimeException("Failed to estimate gas limit", e);
}
}


private BigInteger fetchCurrentGasPrice() {
try {
EthGasPrice ethGasPrice = web3j.ethGasPrice().send();
if (ethGasPrice.hasError()) {
throw new RuntimeException("Error fetching gas price: " + ethGasPrice.getError().getMessage());
throw new RuntimeException(
"Error fetching gas price: " + ethGasPrice.getError().getMessage());
}
return ethGasPrice.getGasPrice();
} catch (Exception e) {
Expand Down
19 changes: 17 additions & 2 deletions core/src/main/java/org/web3j/tx/gas/PriorityGasProvider.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
/*
* Copyright 2025 Web3 Labs Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package org.web3j.tx.gas;

import java.math.BigDecimal;
Expand All @@ -10,15 +22,18 @@ enum Priority {
SLOW,
CUSTOM
}
default BigInteger applyPriority(BigInteger baseGasPrice, Priority priority, BigDecimal customMultiplier) {

default BigInteger applyPriority(
BigInteger baseGasPrice, Priority priority, BigDecimal customMultiplier) {

return switch (priority) {
case FAST -> baseGasPrice.multiply(BigInteger.valueOf(2)); // 2x for fast
case NORMAL -> baseGasPrice; // Default gas price
case SLOW -> baseGasPrice.divide(BigInteger.valueOf(2)); // 0.5x for slow
case CUSTOM -> {
if (customMultiplier == null || customMultiplier.compareTo(BigDecimal.ZERO) <= 0) {
throw new IllegalArgumentException("Custom multiplier must be a positive value");
throw new IllegalArgumentException(
"Custom multiplier must be a positive value");
}
yield new BigDecimal(baseGasPrice).multiply(customMultiplier).toBigInteger();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
*/
package org.web3j.tx.gas;

import org.web3j.protocol.core.methods.request.Transaction;

import java.math.BigInteger;

import org.web3j.protocol.core.methods.request.Transaction;

public class StaticEIP1559GasProvider implements ContractEIP1559GasProvider {
private long chainId;
private BigInteger maxFeePerGas;
Expand Down Expand Up @@ -59,12 +59,12 @@ public long getChainId() {
}

@Override
public BigInteger getMaxFeePerGas(String contractFunc) {
public BigInteger getMaxFeePerGas() {
return maxFeePerGas;
}

@Override
public BigInteger getMaxPriorityFeePerGas(String contractFunc) {
public BigInteger getMaxPriorityFeePerGas() {
return maxPriorityFeePerGas;
}
}
4 changes: 2 additions & 2 deletions core/src/main/java/org/web3j/tx/gas/StaticGasProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
*/
package org.web3j.tx.gas;

import org.web3j.protocol.core.methods.request.Transaction;

import java.math.BigInteger;

import org.web3j.protocol.core.methods.request.Transaction;

@SuppressWarnings("deprecation")
public class StaticGasProvider implements ContractGasProvider {
private BigInteger gasPrice;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ public class ArraysIT extends Scenario {

@BeforeAll
public static void setUp(Web3j web3j) throws Exception {
Scenario.web3j = web3j;
ArraysIT.contract =
Arrays.deploy(
web3j,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright 2025 Web3 Labs Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package org.web3j.protocol.scenarios;

import java.math.BigInteger;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import org.web3j.EVMTest;
import org.web3j.NodeType;
import org.web3j.protocol.Web3j;
import org.web3j.test.contract.HumanStandardToken;
import org.web3j.tx.gas.ContractGasProvider;
import org.web3j.tx.gas.DynamicGasProvider;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

@EVMTest(type = NodeType.BESU)
public class DynamicGasProviderIT extends Scenario {
static ContractGasProvider dynamicGasProvider;
static String contractAddress;

@BeforeAll
static void setUp(Web3j web3j) throws Exception {
dynamicGasProvider = new DynamicGasProvider(web3j);
contractAddress = sendTransaction();
}

@Test
public void testContractCreation() {
HumanStandardToken humanStandardToken =
HumanStandardToken.load(contractAddress, web3j, ALICE, dynamicGasProvider);

assertNotNull(humanStandardToken.getTransactionReceipt().get());
assertEquals(
humanStandardToken.getTransactionReceipt().get().getContractAddress(),
contractAddress);
}

@Test
public void callSmartContractFunction() throws Exception {
HumanStandardToken humanStandardToken =
HumanStandardToken.load(contractAddress, web3j, ALICE, dynamicGasProvider);

assertEquals(humanStandardToken.name().send(), "Alice Token");

assertNotNull(humanStandardToken.transfer(BOB.getAddress(), BigInteger.ONE).send());
assertEquals(humanStandardToken.balanceOf(BOB.getAddress()).send(), BigInteger.ONE);
}

private static String sendTransaction() throws Exception {
return HumanStandardToken.deploy(
web3j,
ALICE,
dynamicGasProvider,
BigInteger.valueOf(100L),
"Alice Token",
BigInteger.valueOf(18L),
"ATK")
.send()
.getContractAddress();
}
}

0 comments on commit 8282eb4

Please sign in to comment.