Skip to content

Commit e5a9fee

Browse files
add check sufficient balance for amount with gas price
1 parent 331c12b commit e5a9fee

File tree

4 files changed

+45
-7
lines changed

4 files changed

+45
-7
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ public class TransferService {
3030

3131
public void transfer(TransferRequest transactionRequest) {
3232
var toAddress = transactionRequest.to();
33+
var transactionFee = transactionFeeService.getTransactionFee();
3334
var amount = transactionRequest.amount();
34-
isBalanceSufficient(amount);
35+
var totalAmount = new BigDecimal(amount).add(transactionFee);
36+
isBalanceSufficient(totalAmount);
3537
var fromAddress = credentialService.getCredential().getAddress();
3638
var nonce = transferClient.getNonce(fromAddress).join();
3739
var gasPrice = transferClient.getGasPrice().join();
@@ -69,9 +71,9 @@ private byte[] signTransaction(RawTransaction transaction) {
6971
transaction, SEPOLIA_CHAIN_ID, credentialService.getCredential());
7072
}
7173

72-
private void isBalanceSufficient(String amount) {
74+
private void isBalanceSufficient(BigDecimal amount) {
7375
var toAddress = credentialService.getCredential().getAddress();
74-
if (!balanceService.isBalanceSufficient(toAddress, new BigDecimal(amount))) {
76+
if (!balanceService.isBalanceSufficient(toAddress, amount)) {
7577
throw InsufficientBalanceException.create(
7678
"Insufficient balance for address: %s".formatted(toAddress));
7779
}

ethereum-app/src/test/java/com/blockchain/api/application/controller/businesstest/TransferControllerBusinessTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ void shouldTransferEth() {
9292
var transferRequestDto =
9393
EthTransferRequestDto.builder()
9494
.to("0x5d940f3947c4ab1fbdbf1f540a10019931065f7a")
95-
.amount("0.1")
95+
.amount("0.0001")
9696
.build();
9797
stubGetNonce();
9898
stubGasPrice();
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.blockchain.api.domain.service.transfer;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import com.blockchain.api.infrastructure.client.BaseEthereumTest;
6+
import java.math.BigDecimal;
7+
import lombok.extern.slf4j.Slf4j;
8+
import org.junit.jupiter.api.Test;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.boot.test.context.SpringBootTest;
11+
import org.springframework.test.context.ActiveProfiles;
12+
13+
@SpringBootTest
14+
@ActiveProfiles("test")
15+
@Slf4j
16+
class TransactionFeeServiceITest extends BaseEthereumTest {
17+
18+
@Autowired private TransactionFeeService transactionFeeService;
19+
20+
@Test
21+
void shouldReturnTransactionFee_whenTransactionFeeIsCalculated() {
22+
// given
23+
stubGasPrice();
24+
var expectedTransactionFee = new BigDecimal("0.000234686314653000");
25+
26+
// when
27+
var transactionFee = transactionFeeService.getTransactionFee();
28+
29+
// then
30+
assertThat(transactionFee).isEqualByComparingTo(expectedTransactionFee);
31+
}
32+
}

ethereum-app/src/test/java/com/blockchain/api/domain/service/transfer/TransferServiceTest.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ void shouldTransferEth() {
4444
when(transferClient.getNonce(fromAddress)).thenReturn(completedFuture(BigInteger.ONE));
4545
when(transferClient.getGasPrice()).thenReturn(completedFuture(BigInteger.ONE));
4646
when(credentialService.getCredential()).thenReturn(Credentials.create("0x123"));
47-
when(balanceService.isBalanceSufficient(fromAddress, new BigDecimal("0.1"))).thenReturn(true);
47+
when(transactionFeeService.getTransactionFee()).thenReturn(new BigDecimal("0.1"));
48+
when(balanceService.isBalanceSufficient(fromAddress, new BigDecimal("0.2"))).thenReturn(true);
4849

4950
// when
5051
transferService.transfer(transactionRequest);
@@ -66,7 +67,8 @@ void shouldThrowNonceRetrievalException() {
6667
when(transferClient.getNonce(fromAddress))
6768
.thenReturn(failedFuture(new NonceRetrievalException(address)));
6869
when(credentialService.getCredential()).thenReturn(Credentials.create("0x123"));
69-
when(balanceService.isBalanceSufficient(fromAddress, new BigDecimal("0.1"))).thenReturn(true);
70+
when(transactionFeeService.getTransactionFee()).thenReturn(new BigDecimal("0.1"));
71+
when(balanceService.isBalanceSufficient(fromAddress, new BigDecimal("0.2"))).thenReturn(true);
7072

7173
// when
7274
var thrownException =
@@ -91,7 +93,9 @@ void shouldThrowGasPriceRetrievalException() {
9193

9294
when(transferClient.getNonce(fromAddress)).thenReturn(completedFuture(BigInteger.ONE));
9395
when(credentialService.getCredential()).thenReturn(Credentials.create("0x123"));
94-
when(balanceService.isBalanceSufficient(fromAddress, new BigDecimal("0.1"))).thenReturn(true);
96+
when(transactionFeeService.getTransactionFee()).thenReturn(new BigDecimal("0.1"));
97+
when(balanceService.isBalanceSufficient(fromAddress, new BigDecimal("0.2"))).thenReturn(true);
98+
when(transferClient.getGasPrice()).thenReturn(failedFuture(new RuntimeException("RPC error")));
9599
when(transferClient.getGasPrice())
96100
.thenReturn(CompletableFuture.failedFuture(new RuntimeException("RPC error")));
97101

0 commit comments

Comments
 (0)