### Question:
EthereumWalletService { public BigInteger validateBalanceAndReturnGasLimit(String fromAddress, String toAddress, Function function, BigInteger weiValue, BigInteger gasPrice) { BigInteger gasLimit = estimateGasLimit(fromAddress, toAddress, function, weiValue); BigInteger balance = getEthBalanceWei(fromAddress); BigInteger fee = gasLimit.multiply(gasPrice); BigInteger requiredAmount = fee.add(weiValue); if (balance.compareTo(requiredAmount) < 0) { throw new NotSufficientFundsException("Not enough eth to pay fee + amount. Current balance - " + balance + ", required " + requiredAmount); } return gasLimit; } @Inject  EthereumWalletService(PropertiesHolder propertiesHolder, KeyStoreService keyStoreService, DexEthService dexEthService, UserErrorMessageDao userErrorMessageDao,
                                 DexBeanProducer dexBeanProducer); @PostConstruct void init(); EthWalletBalanceInfo balanceInfo(String address); BigInteger getOnlyEthBalanceWei(String address); BigInteger getEthOrPaxBalanceWei(String address, DexCurrency dexCurrency); int getNumberOfConfirmations(String txHash); BigInteger getPaxBalanceWei(String address); BigInteger getEthBalanceWei(String address); String transfer(String passphrase, long accountId, String fromAddress, String toAddress, BigDecimal amountEth, Long gasPrice, DexCurrency currencies); String sendApproveTransaction(EthWalletKey ethWalletKey, String spenderAddress, BigInteger value); EthBlock.Block getLastBlock(); BigInteger getAllowance(String spenderAddress, String owner, String contractAddress); String transferEth(Credentials credentials, String toAddress, BigInteger amountWei, Long gasPrice); Optional<TransactionReceipt> getReceipt(String transactionHash); BigInteger estimateGasLimit(String fromAddress, String toAddress, Function function, BigInteger weiValue); BigInteger validateBalanceAndReturnGasLimit(String fromAddress, String toAddress, Function function, BigInteger weiValue, BigInteger gasPrice); final String PAX_CONTRACT_ADDRESS; }### Answer:
@Test void testValidateBalance() throws IOException { mockEstimateGasRequestResponse(null, BigInteger.TEN); Request balanceRequest = mock(Request.class); doReturn(balanceRequest).when(web3j).ethGetBalance(accountAddress, DefaultBlockParameterName.LATEST); EthGetBalance getBalanceResponse = new EthGetBalance(); getBalanceResponse.setResult("0x64"); doReturn(getBalanceResponse).when(balanceRequest).send(); BigInteger gasLimit = service.validateBalanceAndReturnGasLimit(accountAddress, "", function, BigInteger.TEN, BigInteger.TWO); assertEquals(BigInteger.valueOf(11), gasLimit); }
@Test void testValidateBalanceMatchExact() throws IOException { mockEstimateGasRequestResponse(null, BigInteger.valueOf(5)); Request balanceRequest = mock(Request.class); doReturn(balanceRequest).when(web3j).ethGetBalance(accountAddress, DefaultBlockParameterName.LATEST); EthGetBalance getBalanceResponse = new EthGetBalance(); getBalanceResponse.setResult("0x64"); doReturn(getBalanceResponse).when(balanceRequest).send(); BigInteger gasLimit = service.validateBalanceAndReturnGasLimit(accountAddress, "", function, BigInteger.valueOf(90), BigInteger.TWO); assertEquals(BigInteger.valueOf(5), gasLimit); }
@Test void testValidateBalanceNotEnoughFundsToPayFee() throws IOException { mockEstimateGasRequestResponse(null, BigInteger.TEN); Request balanceRequest = mock(Request.class); doReturn(balanceRequest).when(web3j).ethGetBalance(accountAddress, DefaultBlockParameterName.LATEST); EthGetBalance getBalanceResponse = new EthGetBalance(); getBalanceResponse.setResult("0x64"); doReturn(getBalanceResponse).when(balanceRequest).send(); assertThrows(NotSufficientFundsException.class, () -> service.validateBalanceAndReturnGasLimit(accountAddress, "", function, BigInteger.valueOf(79), BigInteger.TWO)); }