diff --git a/src/main/java/com/nynja/walletservice/controller/TransactionController.java b/src/main/java/com/nynja/walletservice/controller/TransactionController.java index babf2562114b75a2eb770865c987658d4d413944..4d8c30d4e43048051a594d144f83bc58e27a00d3 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 0000000000000000000000000000000000000000..3eed0d582f96562b6c743f0d0126a32e32d2b817 --- /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 9bf61eb56b635221260a937bee223c6cf846efc2..440b3fe74275cd5d66719f58e505131c0d3d40ce 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(); }