From 6a8fa8fd029f550149c1f50b85978e9921ab0099 Mon Sep 17 00:00:00 2001 From: Oleg Zhymolokhov Date: Thu, 18 Oct 2018 16:18:42 +0300 Subject: [PATCH] estimate-gas-fix: Fixed gas estimation. --- .../walletservice/controller/TransactionController.java | 5 ++--- .../java/com/nynja/walletservice/service/Web3JService.java | 7 +++++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/nynja/walletservice/controller/TransactionController.java b/src/main/java/com/nynja/walletservice/controller/TransactionController.java index 4d8c30d..a7fe092 100644 --- a/src/main/java/com/nynja/walletservice/controller/TransactionController.java +++ b/src/main/java/com/nynja/walletservice/controller/TransactionController.java @@ -17,7 +17,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; -import org.web3j.protocol.core.methods.response.EthEstimateGas; import org.web3j.protocol.core.methods.response.TransactionReceipt; import javax.validation.Valid; @@ -105,7 +104,7 @@ public class TransactionController { @ApiOperation(value = "Endpoint for transaction gas price estimation", httpMethod = "POST") @ApiResponse(code = 200, message = RETRIEVED) @PostMapping(API_VERSION + "/estimate-gas-price") - public CompletableFuture> estimateGasPrice(@RequestBody EstimateGasDto dto) { - return web3JService.estimateGas(dto).thenApplyAsync(ResponseEntity::ok); + public CompletableFuture> estimateGasPrice(@RequestBody EstimateGasDto dto) { + return web3JService.estimateGasPrice(dto).thenApplyAsync(ResponseEntity::ok); } } diff --git a/src/main/java/com/nynja/walletservice/service/Web3JService.java b/src/main/java/com/nynja/walletservice/service/Web3JService.java index 440b3fe..9ad63f3 100644 --- a/src/main/java/com/nynja/walletservice/service/Web3JService.java +++ b/src/main/java/com/nynja/walletservice/service/Web3JService.java @@ -104,7 +104,7 @@ public class Web3JService { .thenApplyAsync(EthGetTransactionCount::getTransactionCount); } - public CompletableFuture estimateGas(EstimateGasDto dto) { + public CompletableFuture estimateGasPrice(EstimateGasDto dto) { return web3j().ethEstimateGas(new org.web3j.protocol.core.methods.request.Transaction( dto.getFrom(), null, @@ -113,7 +113,10 @@ public class Web3JService { dto.getTo(), dto.getValue(), dto.getData() - )).sendAsync(); + )).sendAsync().thenComposeAsync(ethEstimateGas -> web3j().ethGasPrice().sendAsync() + .thenApplyAsync(ethGasPrice -> + ethGasPrice.getGasPrice().multiply(ethEstimateGas.getAmountUsed())) + ); } private Web3j web3j() { -- GitLab