From f0f1fad9a3871638f7579e077db4b19e77c93b46 Mon Sep 17 00:00:00 2001 From: Oleg Zhymolokhov Date: Fri, 21 Sep 2018 14:28:45 +0300 Subject: [PATCH] estimate-gas: Implemented API for gas-price estimation. --- .../controller/TransactionController.java | 9 +++++++++ .../walletservice/dto/EstimateGasDto.java | 18 ++++++++++++++++++ .../walletservice/service/Web3JService.java | 13 +++++++++++++ 3 files changed, 40 insertions(+) create mode 100644 src/main/java/com/nynja/walletservice/dto/EstimateGasDto.java diff --git a/src/main/java/com/nynja/walletservice/controller/TransactionController.java b/src/main/java/com/nynja/walletservice/controller/TransactionController.java index babf256..4d8c30d 100644 --- a/src/main/java/com/nynja/walletservice/controller/TransactionController.java +++ b/src/main/java/com/nynja/walletservice/controller/TransactionController.java @@ -4,6 +4,7 @@ import com.nynja.walletservice.blockexplorer.EtherScanConsumer; import com.nynja.walletservice.blockexplorer.dto.Response; import com.nynja.walletservice.blockexplorer.dto.TxListItemDto; import com.nynja.walletservice.constant.enums.Sort; +import com.nynja.walletservice.dto.EstimateGasDto; import com.nynja.walletservice.dto.SignedTransactionDto; import com.nynja.walletservice.dto.TransactionResponseDto; import com.nynja.walletservice.service.TokenService; @@ -16,6 +17,7 @@ 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; @@ -99,4 +101,11 @@ public class TransactionController { address, startBlock, endBlock, sort )); } + + @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); + } } diff --git a/src/main/java/com/nynja/walletservice/dto/EstimateGasDto.java b/src/main/java/com/nynja/walletservice/dto/EstimateGasDto.java new file mode 100644 index 0000000..3eed0d5 --- /dev/null +++ b/src/main/java/com/nynja/walletservice/dto/EstimateGasDto.java @@ -0,0 +1,18 @@ +package com.nynja.walletservice.dto; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.math.BigInteger; + +@Data +@NoArgsConstructor +@AllArgsConstructor +public class EstimateGasDto { + private String from; + private String to; + private BigInteger gas; + private BigInteger value; + private String data; +} diff --git a/src/main/java/com/nynja/walletservice/service/Web3JService.java b/src/main/java/com/nynja/walletservice/service/Web3JService.java index 9bf61eb..440b3fe 100644 --- a/src/main/java/com/nynja/walletservice/service/Web3JService.java +++ b/src/main/java/com/nynja/walletservice/service/Web3JService.java @@ -1,6 +1,7 @@ package com.nynja.walletservice.service; import com.nynja.walletservice.config.EthereumConfig; +import com.nynja.walletservice.dto.EstimateGasDto; import com.nynja.walletservice.dto.TransactionResponseDto; import com.nynja.walletservice.exception.TransactionFailedException; import com.nynja.walletservice.exception.TransactionNotFoundException; @@ -103,6 +104,18 @@ public class Web3JService { .thenApplyAsync(EthGetTransactionCount::getTransactionCount); } + public CompletableFuture estimateGas(EstimateGasDto dto) { + return web3j().ethEstimateGas(new org.web3j.protocol.core.methods.request.Transaction( + dto.getFrom(), + null, + null, + dto.getGas(), + dto.getTo(), + dto.getValue(), + dto.getData() + )).sendAsync(); + } + private Web3j web3j() { return web3jProvider.get(); } -- GitLab