From b933241dee59c748bc350706c7f7dbb3029e61a1 Mon Sep 17 00:00:00 2001 From: min-96 Date: Mon, 2 Sep 2024 20:48:03 +0900 Subject: [PATCH] refactor --- .../com/api/admin/config/RabbitMQConfig.kt | 6 +- .../api/admin/controller/AdminController.kt | 4 +- .../domain/transferFailLog/TransferFailLog.kt | 14 ++++ .../TransferFailLogRepository.kt | 6 ++ src/main/kotlin/com/api/admin/enums/Enum.kt | 4 +- .../rabbitMQ/event/AdminEventListener.kt | 2 +- .../admin/service/TransferFailLogService.kt | 24 ++++++ .../com/api/admin/service/TransferService.kt | 1 - .../com/api/admin/service/Web3jService.kt | 82 +++++++++---------- src/main/kotlin/com/api/admin/util/Util.kt | 14 ++++ .../db/migration/V1__Initial_schema.sql | 8 ++ 11 files changed, 112 insertions(+), 53 deletions(-) create mode 100644 src/main/kotlin/com/api/admin/domain/transferFailLog/TransferFailLog.kt create mode 100644 src/main/kotlin/com/api/admin/domain/transferFailLog/TransferFailLogRepository.kt create mode 100644 src/main/kotlin/com/api/admin/service/TransferFailLogService.kt diff --git a/src/main/kotlin/com/api/admin/config/RabbitMQConfig.kt b/src/main/kotlin/com/api/admin/config/RabbitMQConfig.kt index 7ae9e58..6737dda 100644 --- a/src/main/kotlin/com/api/admin/config/RabbitMQConfig.kt +++ b/src/main/kotlin/com/api/admin/config/RabbitMQConfig.kt @@ -1,10 +1,7 @@ package com.api.admin.config -import org.springframework.amqp.core.Binding -import org.springframework.amqp.core.BindingBuilder -import org.springframework.amqp.core.DirectExchange + import org.springframework.amqp.core.FanoutExchange -import org.springframework.amqp.core.Queue import org.springframework.amqp.rabbit.connection.ConnectionFactory import org.springframework.amqp.rabbit.core.RabbitTemplate import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter @@ -33,5 +30,4 @@ class RabbitMQConfig { @Bean fun transferExchange() = createExchange("transferExchange") - } \ No newline at end of file diff --git a/src/main/kotlin/com/api/admin/controller/AdminController.kt b/src/main/kotlin/com/api/admin/controller/AdminController.kt index 2c96ae1..1c97607 100644 --- a/src/main/kotlin/com/api/admin/controller/AdminController.kt +++ b/src/main/kotlin/com/api/admin/controller/AdminController.kt @@ -37,7 +37,7 @@ class AdminController( @RequestBody request: WithdrawERC20Request, ): Mono> { println("Received withdraw request for address: $address with amount: ${request.amount}") - return web3jService.createTransactionERC20(address, request.amount, request.chainType) + return web3jService.processTransactionERC20(address, request.amount, request.chainType) .doOnSuccess { println("Transaction successful") } .then(Mono.just(ResponseEntity.ok().build())) .doOnError { e -> @@ -53,7 +53,7 @@ class AdminController( @RequestParam address: String, @RequestBody request: WithdrawERC721Request, ): Mono> { - return web3jService.createTransactionERC721(address, request.nftId) + return web3jService.processTransactionERC721(address, request.nftId) .then(Mono.just(ResponseEntity.ok().build())) .onErrorResume { Mono.just(ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build()) diff --git a/src/main/kotlin/com/api/admin/domain/transferFailLog/TransferFailLog.kt b/src/main/kotlin/com/api/admin/domain/transferFailLog/TransferFailLog.kt new file mode 100644 index 0000000..af6894f --- /dev/null +++ b/src/main/kotlin/com/api/admin/domain/transferFailLog/TransferFailLog.kt @@ -0,0 +1,14 @@ +package com.api.admin.domain.transferFailLog + +import org.springframework.data.annotation.Id +import org.springframework.data.relational.core.mapping.Table +import java.sql.Timestamp + +@Table("transfer_fail_log") +data class TransferFailLog( + @Id val id: Long? = null, + val wallet: String, + val timestamp: Long? = System.currentTimeMillis(), + val transactionHash: String?, + val errorMessage: String, + ) diff --git a/src/main/kotlin/com/api/admin/domain/transferFailLog/TransferFailLogRepository.kt b/src/main/kotlin/com/api/admin/domain/transferFailLog/TransferFailLogRepository.kt new file mode 100644 index 0000000..a3ce0b4 --- /dev/null +++ b/src/main/kotlin/com/api/admin/domain/transferFailLog/TransferFailLogRepository.kt @@ -0,0 +1,6 @@ +package com.api.admin.domain.transferFailLog + +import org.springframework.data.repository.reactive.ReactiveCrudRepository + +interface TransferFailLogRepository: ReactiveCrudRepository { +} \ No newline at end of file diff --git a/src/main/kotlin/com/api/admin/enums/Enum.kt b/src/main/kotlin/com/api/admin/enums/Enum.kt index efa0589..09711dd 100644 --- a/src/main/kotlin/com/api/admin/enums/Enum.kt +++ b/src/main/kotlin/com/api/admin/enums/Enum.kt @@ -24,5 +24,7 @@ enum class TokenType { MATIC, ETH } - +enum class TransactionStatus { + SUCCESS, FAILURE +} diff --git a/src/main/kotlin/com/api/admin/rabbitMQ/event/AdminEventListener.kt b/src/main/kotlin/com/api/admin/rabbitMQ/event/AdminEventListener.kt index ce2a555..cd8405e 100644 --- a/src/main/kotlin/com/api/admin/rabbitMQ/event/AdminEventListener.kt +++ b/src/main/kotlin/com/api/admin/rabbitMQ/event/AdminEventListener.kt @@ -11,7 +11,7 @@ class AdminEventListener( ) { @EventListener - fun onDepositSend(event: AdminTransferCreatedEvent) { + fun onTransferSend(event: AdminTransferCreatedEvent) { provider.transferSend(event.transfer) } } \ No newline at end of file diff --git a/src/main/kotlin/com/api/admin/service/TransferFailLogService.kt b/src/main/kotlin/com/api/admin/service/TransferFailLogService.kt new file mode 100644 index 0000000..d4fbf37 --- /dev/null +++ b/src/main/kotlin/com/api/admin/service/TransferFailLogService.kt @@ -0,0 +1,24 @@ +package com.api.admin.service + +import com.api.admin.domain.transferFailLog.TransferFailLog +import com.api.admin.domain.transferFailLog.TransferFailLogRepository +import org.springframework.stereotype.Service +import reactor.core.publisher.Mono + +@Service +class TransferFailLogService( + private val transferFailLogRepository: TransferFailLogRepository, +) { + + fun save(wallet: String, transactionHash: String?, message: String): Mono { + return transferFailLogRepository.save( + TransferFailLog( + wallet = wallet, + transactionHash = transactionHash, + errorMessage = message + ) + ).doOnSuccess { + + }.then() + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/api/admin/service/TransferService.kt b/src/main/kotlin/com/api/admin/service/TransferService.kt index 62cf074..6666091 100644 --- a/src/main/kotlin/com/api/admin/service/TransferService.kt +++ b/src/main/kotlin/com/api/admin/service/TransferService.kt @@ -36,7 +36,6 @@ class TransferService( transactionHash: String, accountType: AccountType, ): Mono { - println("transactionHash : $transactionHash") return transferRepository.existsByTransactionHash(transactionHash) .flatMap { if (it) { diff --git a/src/main/kotlin/com/api/admin/service/Web3jService.kt b/src/main/kotlin/com/api/admin/service/Web3jService.kt index 940b3fa..e1b3b64 100644 --- a/src/main/kotlin/com/api/admin/service/Web3jService.kt +++ b/src/main/kotlin/com/api/admin/service/Web3jService.kt @@ -4,6 +4,7 @@ import com.api.admin.domain.nft.NftRepository import com.api.admin.enums.AccountType import com.api.admin.enums.ChainType import com.api.admin.properties.AdminInfoProperties +import com.api.admin.util.Util.getChainId import com.fasterxml.jackson.databind.JsonNode import com.fasterxml.jackson.databind.ObjectMapper import org.springframework.stereotype.Service @@ -16,6 +17,7 @@ import org.web3j.crypto.RawTransaction import org.web3j.crypto.TransactionEncoder import org.web3j.utils.Numeric import reactor.core.publisher.Mono +import reactor.core.scheduler.Schedulers import java.math.BigDecimal import java.math.BigInteger import java.time.Duration @@ -28,48 +30,38 @@ class Web3jService( private val adminInfoProperties: AdminInfoProperties ) { - // private val privateKey = "4ec9e64419547100af4f38d7ec57ba1de2d5c36a7dfb03f1a349b2c5b62ac0a9" - - private fun getChainId(chain: ChainType): Long { - val chain = when (chain) { - ChainType.ETHEREUM_MAINNET -> 1L - ChainType.POLYGON_MAINNET -> 137L - ChainType.LINEA_MAINNET -> 59144L - ChainType.LINEA_SEPOLIA -> 59140L - ChainType.ETHEREUM_HOLESKY -> 1L - ChainType.ETHEREUM_SEPOLIA -> 11155111L - ChainType.POLYGON_AMOY -> 80002L - } - return chain - } - - fun createTransactionERC721( - toAddress: String, - nftId: Long, - ): Mono { - return nftRepository.findById(nftId).flatMap { nft-> - val credentials = Credentials.create(adminInfoProperties.privatekey) - createERC721TransactionData(credentials, nft.tokenAddress, toAddress, BigInteger(nft.tokenId), nft.chainType) - .flatMap { transactionHash -> - waitForTransactionReceipt(transactionHash, nft.chainType) - .flatMap { - transferService.getTransferData( - wallet = toAddress, - chainType = nft.chainType, - transactionHash = transactionHash, - accountType = AccountType.WITHDRAW - ) - } - } - .doOnError { e -> - println("Error in createTransactionERC20: ${e.message}") - e.printStackTrace() - } - .then() - } +// fun createTransactionERC721(toAddress: String, nftId: Long): Mono { +// return Mono.defer { processTransactionERC721(toAddress, nftId) } +// .subscribeOn(Schedulers.boundedElastic()) +// .onErrorResume { error -> +// transferService.getTransferData(toAddress, nft.chainType, "failed_hash", AccountType.WITHDRAW, TransactionStatus.FAILURE, error.message ?: "Unknown error") +// } +// .then(Mono.empty()) +// } + + fun processTransactionERC721(toAddress: String, nftId: Long): Mono { + return nftRepository.findById(nftId) + .flatMap { nft -> + val credentials = Credentials.create(adminInfoProperties.privatekey) + createERC721TransactionData(credentials, nft.tokenAddress, toAddress, BigInteger(nft.tokenId), nft.chainType) + .flatMap { transactionHash -> + waitForTransactionReceipt(transactionHash, nft.chainType) + .flatMap { + transferService.getTransferData( + wallet = toAddress, + chainType = nft.chainType, + transactionHash = transactionHash, + accountType = AccountType.WITHDRAW + ) + } + } + } + .doOnError { e -> + println("Error in createTransactionERC721: ${e.message}") + e.printStackTrace() + } } - fun createERC721TransactionData( credentials: Credentials, contractAddress: String, @@ -108,8 +100,14 @@ class Web3jService( } } +// fun createTransactionERC20(recipientAddress: String, amount: BigDecimal, chainType: ChainType): Mono { +// Mono.defer { processTransactionERC20(recipientAddress, amount, chainType) } +// .subscribeOn(Schedulers.boundedElastic()) +// .subscribe() +// return Mono.empty() +// } - fun createTransactionERC20( + fun processTransactionERC20( recipientAddress: String, amount: BigDecimal, chainType: ChainType @@ -165,8 +163,6 @@ class Web3jService( fun waitForTransactionReceipt(transactionHash: String, chainType: ChainType, maxAttempts: Int = 5, attempt: Int = 1): Mono { - - val objectMapper = ObjectMapper() println("Attempt $attempt for transaction $transactionHash") return infuraApiService.getTransactionReceipt(chainType, transactionHash) diff --git a/src/main/kotlin/com/api/admin/util/Util.kt b/src/main/kotlin/com/api/admin/util/Util.kt index c39ed4e..1f34eb3 100644 --- a/src/main/kotlin/com/api/admin/util/Util.kt +++ b/src/main/kotlin/com/api/admin/util/Util.kt @@ -1,5 +1,19 @@ package com.api.admin.util +import com.api.admin.enums.ChainType + object Util { + fun getChainId(chain: ChainType): Long { + val chain = when (chain) { + ChainType.ETHEREUM_MAINNET -> 1L + ChainType.POLYGON_MAINNET -> 137L + ChainType.LINEA_MAINNET -> 59144L + ChainType.LINEA_SEPOLIA -> 59140L + ChainType.ETHEREUM_HOLESKY -> 1L + ChainType.ETHEREUM_SEPOLIA -> 11155111L + ChainType.POLYGON_AMOY -> 80002L + } + return chain + } } \ No newline at end of file diff --git a/src/main/resources/db/migration/V1__Initial_schema.sql b/src/main/resources/db/migration/V1__Initial_schema.sql index 863a35f..33d2dd6 100644 --- a/src/main/resources/db/migration/V1__Initial_schema.sql +++ b/src/main/resources/db/migration/V1__Initial_schema.sql @@ -36,3 +36,11 @@ CREATE TABLE IF NOT EXISTS transfer ( transaction_hash VARCHAR(255) NOT NULL, chain_type chain_type NOT NULL ); + +CREATE TABLE IF NOT EXISTS transfer_fail_log ( + id SERIAL PRIMARY KEY, + wallet VARCHAR(255) NOT NULL, + timestamp bigint not null, + transaction_hash VARCHAR(255), + error_message varchar(1000) NOT NULL +);