Skip to content

Commit 2d3d995

Browse files
implement adaptor for ethereum gas price
1 parent f223b25 commit 2d3d995

File tree

5 files changed

+63
-0
lines changed

5 files changed

+63
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.blockchain.api.application.exception;
2+
3+
public class GasPriceRetrievalException extends RuntimeException {
4+
public GasPriceRetrievalException(String message, Throwable cause) {
5+
super(message, cause);
6+
}
7+
8+
public static GasPriceRetrievalException build(Throwable e) {
9+
return new GasPriceRetrievalException("error while retrieving gas price", e);
10+
}
11+
}

ethereum-app/src/main/java/com/blockchain/api/domain/service/transfer/TransferClient.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@ public interface TransferClient {
77
void transfer(TransferRequest transferRequest);
88

99
CompletableFuture<BigInteger> getNonce(String address);
10+
11+
CompletableFuture<BigInteger> getGasPrice();
1012
}

ethereum-app/src/main/java/com/blockchain/api/infrastructure/client/transfer/TransferAdaptor.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.blockchain.api.infrastructure.client.transfer;
22

3+
import com.blockchain.api.application.exception.GasPriceRetrievalException;
34
import com.blockchain.api.application.exception.NonceRetrievalException;
45
import com.blockchain.api.domain.service.transfer.TransferClient;
56
import com.blockchain.api.domain.service.transfer.TransferRequest;
@@ -39,4 +40,21 @@ public CompletableFuture<BigInteger> getNonce(String address) {
3940
throw NonceRetrievalException.withAddress(address);
4041
});
4142
}
43+
44+
@Override
45+
public CompletableFuture<BigInteger> getGasPrice() {
46+
return rpcClient
47+
.ethGasPrice()
48+
.sendAsync()
49+
.thenApply(
50+
gasPrice -> {
51+
log.info("Gas price request completed successfully");
52+
return gasPrice.getGasPrice();
53+
})
54+
.exceptionally(
55+
throwable -> {
56+
log.error("Error occurred while fetching gas price", throwable);
57+
throw GasPriceRetrievalException.build(throwable);
58+
});
59+
}
4260
}

ethereum-app/src/test/java/com/blockchain/api/application/EthereumITest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static org.junit.jupiter.api.Assertions.assertNotNull;
44

55
import com.blockchain.api.domain.service.balance.BalanceClient;
6+
import com.blockchain.api.domain.service.transfer.TransferClient;
67
import java.math.BigDecimal;
78
import lombok.extern.slf4j.Slf4j;
89
import org.junit.jupiter.api.Test;
@@ -17,6 +18,8 @@
1718
public class EthereumITest {
1819
@Autowired private BalanceClient balanceClient;
1920

21+
@Autowired private TransferClient transferClient;
22+
2023
@Test
2124
void shouldReturnBalance() {
2225
// given
@@ -29,4 +32,14 @@ void shouldReturnBalance() {
2932
assertNotNull(result);
3033
log.info("Balance BigInteger: {}", Convert.fromWei(new BigDecimal(result), Convert.Unit.ETHER));
3134
}
35+
36+
@Test
37+
void shouldGetGasPrice() {
38+
// when
39+
var result = transferClient.getGasPrice().join();
40+
41+
// then
42+
assertNotNull(result);
43+
log.info("original value {} Gas price: {}", result,Convert.fromWei(new BigDecimal(result), Convert.Unit.ETHER));
44+
}
3245
}

ethereum-app/src/test/java/com/blockchain/api/infrastructure/client/transfer/TransferAdaptorTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.blockchain.api.infrastructure.client.transfer;
22

3+
import static org.assertj.core.api.Assertions.assertThat;
34
import static org.assertj.core.api.Assertions.assertThatThrownBy;
45
import static org.junit.jupiter.api.Assertions.*;
56
import static org.mockito.ArgumentMatchers.eq;
@@ -18,6 +19,7 @@
1819
import org.web3j.protocol.Web3j;
1920
import org.web3j.protocol.core.DefaultBlockParameterName;
2021
import org.web3j.protocol.core.Request;
22+
import org.web3j.protocol.core.methods.response.EthGasPrice;
2123
import org.web3j.protocol.core.methods.response.EthGetTransactionCount;
2224

2325
@ExtendWith(MockitoExtension.class)
@@ -72,4 +74,21 @@ void shouldThrowNonceRetrievalException_whenNonceRequestFails() {
7274

7375
verify(rpcClient).ethGetTransactionCount(eq(address), eq(DefaultBlockParameterName.LATEST));
7476
}
77+
78+
@Test
79+
void shouldRetrieveGasPrice() {
80+
// given
81+
when(rpcClient.ethGasPrice()).thenReturn(mock(Request.class));
82+
var ethGasPrice = mock(EthGasPrice.class);
83+
var gasPrice = BigInteger.valueOf(42);
84+
when(ethGasPrice.getGasPrice()).thenReturn(gasPrice);
85+
when(rpcClient.ethGasPrice().sendAsync())
86+
.thenReturn(CompletableFuture.completedFuture(ethGasPrice));
87+
88+
// when
89+
var result = transferAdaptor.getGasPrice();
90+
91+
// then
92+
assertThat(result.join()).isEqualTo(gasPrice);
93+
}
7594
}

0 commit comments

Comments
 (0)