Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
min-96 committed Sep 2, 2024
1 parent bed431a commit b933241
Show file tree
Hide file tree
Showing 11 changed files with 112 additions and 53 deletions.
6 changes: 1 addition & 5 deletions src/main/kotlin/com/api/admin/config/RabbitMQConfig.kt
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -33,5 +30,4 @@ class RabbitMQConfig {

@Bean
fun transferExchange() = createExchange("transferExchange")

}
4 changes: 2 additions & 2 deletions src/main/kotlin/com/api/admin/controller/AdminController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class AdminController(
@RequestBody request: WithdrawERC20Request,
): Mono<ResponseEntity<Void>> {
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<Void>()))
.doOnError { e ->
Expand All @@ -53,7 +53,7 @@ class AdminController(
@RequestParam address: String,
@RequestBody request: WithdrawERC721Request,
): Mono<ResponseEntity<Void>> {
return web3jService.createTransactionERC721(address, request.nftId)
return web3jService.processTransactionERC721(address, request.nftId)
.then(Mono.just(ResponseEntity.ok().build<Void>()))
.onErrorResume {
Mono.just(ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build())
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.api.admin.domain.transferFailLog

import org.springframework.data.repository.reactive.ReactiveCrudRepository

interface TransferFailLogRepository: ReactiveCrudRepository<TransferFailLog,Long> {
}
4 changes: 3 additions & 1 deletion src/main/kotlin/com/api/admin/enums/Enum.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,7 @@ enum class TokenType {
MATIC, ETH
}


enum class TransactionStatus {
SUCCESS, FAILURE
}

Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class AdminEventListener(
) {

@EventListener
fun onDepositSend(event: AdminTransferCreatedEvent) {
fun onTransferSend(event: AdminTransferCreatedEvent) {
provider.transferSend(event.transfer)
}
}
24 changes: 24 additions & 0 deletions src/main/kotlin/com/api/admin/service/TransferFailLogService.kt
Original file line number Diff line number Diff line change
@@ -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<Void> {
return transferFailLogRepository.save(
TransferFailLog(
wallet = wallet,
transactionHash = transactionHash,
errorMessage = message
)
).doOnSuccess {

}.then()
}
}
1 change: 0 additions & 1 deletion src/main/kotlin/com/api/admin/service/TransferService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ class TransferService(
transactionHash: String,
accountType: AccountType,
): Mono<Void> {
println("transactionHash : $transactionHash")
return transferRepository.existsByTransactionHash(transactionHash)
.flatMap {
if (it) {
Expand Down
82 changes: 39 additions & 43 deletions src/main/kotlin/com/api/admin/service/Web3jService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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<Void> {
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<Void> {
// 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<Void> {
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,
Expand Down Expand Up @@ -108,8 +100,14 @@ class Web3jService(
}
}

// fun createTransactionERC20(recipientAddress: String, amount: BigDecimal, chainType: ChainType): Mono<Void> {
// Mono.defer { processTransactionERC20(recipientAddress, amount, chainType) }
// .subscribeOn(Schedulers.boundedElastic())
// .subscribe()
// return Mono.empty()
// }

fun createTransactionERC20(
fun processTransactionERC20(
recipientAddress: String,
amount: BigDecimal,
chainType: ChainType
Expand Down Expand Up @@ -165,8 +163,6 @@ class Web3jService(


fun waitForTransactionReceipt(transactionHash: String, chainType: ChainType, maxAttempts: Int = 5, attempt: Int = 1): Mono<String> {


val objectMapper = ObjectMapper()
println("Attempt $attempt for transaction $transactionHash")
return infuraApiService.getTransactionReceipt(chainType, transactionHash)
Expand Down
14 changes: 14 additions & 0 deletions src/main/kotlin/com/api/admin/util/Util.kt
Original file line number Diff line number Diff line change
@@ -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
}

}
8 changes: 8 additions & 0 deletions src/main/resources/db/migration/V1__Initial_schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
);

0 comments on commit b933241

Please sign in to comment.