From 2fb2bacd6814992db4dbb9bfaaba60f2b304e77e Mon Sep 17 00:00:00 2001 From: min-96 Date: Tue, 23 Jul 2024 16:11:26 +0900 Subject: [PATCH] apply config --- build.gradle.kts | 8 ++++++ .../kotlin/com/api/admin/AdminApplication.kt | 2 ++ .../admin/properties/AdminInfoProperties.kt | 10 +++++++ .../admin/properties/InfuraApiProperties.kt | 8 ++++++ .../api/admin/properties/NftApiProperties.kt | 8 ++++++ .../rabbitMQ/event/AdminEventListener.kt | 1 - .../com/api/admin/service/InfuraApiService.kt | 19 +++++++------ .../com/api/admin/service/NftApiService.kt | 11 ++++---- .../com/api/admin/service/TransferService.kt | 27 +++++-------------- .../com/api/admin/service/Web3jService.kt | 8 +++--- src/main/resources/application.yml | 23 +++++----------- src/main/resources/bootstrap.yaml | 9 +++++++ 12 files changed, 76 insertions(+), 58 deletions(-) create mode 100644 src/main/kotlin/com/api/admin/properties/AdminInfoProperties.kt create mode 100644 src/main/kotlin/com/api/admin/properties/InfuraApiProperties.kt create mode 100644 src/main/kotlin/com/api/admin/properties/NftApiProperties.kt create mode 100644 src/main/resources/bootstrap.yaml diff --git a/build.gradle.kts b/build.gradle.kts index dabda35..b4f0256 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -17,8 +17,10 @@ java { repositories { mavenCentral() } +extra["springCloudVersion"] = "2023.0.2" dependencies { + implementation("org.springframework.cloud:spring-cloud-starter-config") implementation("org.springframework.boot:spring-boot-starter-data-r2dbc") implementation("org.springframework.boot:spring-boot-starter-webflux") implementation("com.fasterxml.jackson.module:jackson-module-kotlin") @@ -40,6 +42,12 @@ dependencies { testRuntimeOnly("org.junit.platform:junit-platform-launcher") } +dependencyManagement { + imports { + mavenBom("org.springframework.cloud:spring-cloud-dependencies:${property("springCloudVersion")}") + } +} + tasks.withType { kotlinOptions { freeCompilerArgs += "-Xjsr305=strict" diff --git a/src/main/kotlin/com/api/admin/AdminApplication.kt b/src/main/kotlin/com/api/admin/AdminApplication.kt index 728b526..071f1ee 100644 --- a/src/main/kotlin/com/api/admin/AdminApplication.kt +++ b/src/main/kotlin/com/api/admin/AdminApplication.kt @@ -1,9 +1,11 @@ package com.api.admin import org.springframework.boot.autoconfigure.SpringBootApplication +import org.springframework.boot.context.properties.ConfigurationPropertiesScan import org.springframework.boot.runApplication @SpringBootApplication +@ConfigurationPropertiesScan class AdminApplication fun main(args: Array) { diff --git a/src/main/kotlin/com/api/admin/properties/AdminInfoProperties.kt b/src/main/kotlin/com/api/admin/properties/AdminInfoProperties.kt new file mode 100644 index 0000000..447b332 --- /dev/null +++ b/src/main/kotlin/com/api/admin/properties/AdminInfoProperties.kt @@ -0,0 +1,10 @@ +package com.api.admin.properties + +import org.springframework.boot.context.properties.ConfigurationProperties + + +@ConfigurationProperties(prefix = "admin") +data class AdminInfoProperties( + val address: String, + val privatekey: String, +) diff --git a/src/main/kotlin/com/api/admin/properties/InfuraApiProperties.kt b/src/main/kotlin/com/api/admin/properties/InfuraApiProperties.kt new file mode 100644 index 0000000..d501670 --- /dev/null +++ b/src/main/kotlin/com/api/admin/properties/InfuraApiProperties.kt @@ -0,0 +1,8 @@ +package com.api.admin.properties + +import org.springframework.boot.context.properties.ConfigurationProperties + +@ConfigurationProperties(prefix = "apikey") +data class InfuraApiProperties( + val infura: String, +) diff --git a/src/main/kotlin/com/api/admin/properties/NftApiProperties.kt b/src/main/kotlin/com/api/admin/properties/NftApiProperties.kt new file mode 100644 index 0000000..ec26cc3 --- /dev/null +++ b/src/main/kotlin/com/api/admin/properties/NftApiProperties.kt @@ -0,0 +1,8 @@ +package com.api.admin.properties + +import org.springframework.boot.context.properties.ConfigurationProperties + +@ConfigurationProperties(prefix = "nft") +data class NftApiProperties( + val uri: String +) 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 36b4870..ce2a555 100644 --- a/src/main/kotlin/com/api/admin/rabbitMQ/event/AdminEventListener.kt +++ b/src/main/kotlin/com/api/admin/rabbitMQ/event/AdminEventListener.kt @@ -1,7 +1,6 @@ package com.api.admin.rabbitMQ.event import com.api.admin.rabbitMQ.event.dto.AdminTransferCreatedEvent -import com.api.admin.rabbitMQ.event.dto.AdminTransferResponse import com.api.admin.rabbitMQ.sender.RabbitMQSender import org.springframework.context.event.EventListener import org.springframework.stereotype.Component diff --git a/src/main/kotlin/com/api/admin/service/InfuraApiService.kt b/src/main/kotlin/com/api/admin/service/InfuraApiService.kt index b396414..7fcac50 100644 --- a/src/main/kotlin/com/api/admin/service/InfuraApiService.kt +++ b/src/main/kotlin/com/api/admin/service/InfuraApiService.kt @@ -1,20 +1,19 @@ package com.api.admin.service import com.api.admin.enums.ChainType +import com.api.admin.properties.InfuraApiProperties import com.api.admin.service.dto.InfuraRequest import com.api.admin.service.dto.InfuraResponse import com.api.admin.service.dto.InfuraTransferResponse import org.springframework.http.MediaType import org.springframework.stereotype.Service import org.springframework.web.reactive.function.client.WebClient -import org.web3j.protocol.core.methods.response.TransactionReceipt import reactor.core.publisher.Mono @Service -class InfuraApiService { - - private val apiKey = "98b672d2ce9a4089a3a5cb5081dde2fa" - +class InfuraApiService( + private val infuraApiProperties: InfuraApiProperties, +) { fun urlByChain(chainType: ChainType) : WebClient { val baseUrl = when (chainType) { ChainType.ETHEREUM_MAINNET -> "https://mainnet.infura.io" @@ -35,7 +34,7 @@ class InfuraApiService { val webClient = urlByChain(chainType) return webClient.post() - .uri("/v3/$apiKey") + .uri("/v3/${infuraApiProperties.infura}") .contentType(MediaType.APPLICATION_JSON) .bodyValue(requestBody) .retrieve() @@ -48,7 +47,7 @@ class InfuraApiService { val webClient = urlByChain(chainType) return webClient.post() - .uri("/v3/$apiKey") + .uri("/v3/${infuraApiProperties.infura}") .contentType(MediaType.APPLICATION_JSON) .bodyValue(requestBody) .retrieve() @@ -62,7 +61,7 @@ class InfuraApiService { val webClient = urlByChain(chainType) return webClient.post() - .uri("/v3/$apiKey") + .uri("/v3/${infuraApiProperties.infura}") .contentType(MediaType.APPLICATION_JSON) .bodyValue(requestBody) .retrieve() @@ -76,7 +75,7 @@ class InfuraApiService { val webClient = urlByChain(chainType) return webClient.post() - .uri("/v3/$apiKey") + .uri("/v3/${infuraApiProperties.infura}") .contentType(MediaType.APPLICATION_JSON) .bodyValue(requestBody) .retrieve() @@ -90,7 +89,7 @@ class InfuraApiService { val webClient = urlByChain(chainType) return webClient.post() - .uri("/v3/$apiKey") + .uri("/v3/${infuraApiProperties.infura}") .contentType(MediaType.APPLICATION_JSON) .bodyValue(requestBody) .retrieve() diff --git a/src/main/kotlin/com/api/admin/service/NftApiService.kt b/src/main/kotlin/com/api/admin/service/NftApiService.kt index 85c76bc..9333328 100644 --- a/src/main/kotlin/com/api/admin/service/NftApiService.kt +++ b/src/main/kotlin/com/api/admin/service/NftApiService.kt @@ -1,5 +1,6 @@ package com.api.admin.service +import com.api.admin.properties.NftApiProperties import com.api.admin.service.dto.NftRequest import com.api.admin.service.dto.NftResponse import org.springframework.http.MediaType @@ -8,11 +9,13 @@ import org.springframework.web.reactive.function.client.WebClient import reactor.core.publisher.Mono @Service -class NftApiService { +class NftApiService( + nftApiProperties: NftApiProperties, +) { private val webClient = WebClient.builder() - .baseUrl(uri) + .baseUrl(nftApiProperties.uri) .build() fun getNftSave(request: NftRequest): Mono { @@ -23,8 +26,4 @@ class NftApiService { .bodyToMono(NftResponse::class.java) } - - companion object { - val uri = "http://localhost:8082/v1/nft" - } } \ 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 0831c33..000b1df 100644 --- a/src/main/kotlin/com/api/admin/service/TransferService.kt +++ b/src/main/kotlin/com/api/admin/service/TransferService.kt @@ -1,11 +1,11 @@ package com.api.admin.service -import com.api.admin.domain.nft.NftRepository import com.api.admin.domain.transfer.Transfer import com.api.admin.domain.transfer.TransferRepository import com.api.admin.enums.AccountType import com.api.admin.enums.ChainType import com.api.admin.enums.TransferType +import com.api.admin.properties.AdminInfoProperties import com.api.admin.rabbitMQ.event.dto.AdminTransferCreatedEvent import com.api.admin.rabbitMQ.event.dto.AdminTransferResponse import com.api.admin.service.dto.InfuraTransferDetail @@ -19,31 +19,17 @@ import java.time.Instant @Service class TransferService( - private val nftRepository: NftRepository, private val transferRepository: TransferRepository, private val eventPublisher: ApplicationEventPublisher, private val infuraApiService: InfuraApiService, private val nftService: NftService, + private val adminInfoProperties: AdminInfoProperties, ) { - private val adminAddress = "0x9bDeF468ae33b09b12a057B4c9211240D63BaE65" private val transferEventSignature = "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef" private val nativeTransferEventSignature = "0xe6497e3ee548a3372136af2fcb0696db31fc6cf20260707645068bd3fe97f3c4" - // fun accountTransfer( - // wallet: String, - // chainType: ChainType, - // transactionHash: String, - // accountType: AccountType, - // transferType: TransferType, - // ) { - // return when(accountType) { - // AccountType.DEPOSIT -> getTransferData(wallet,chainType,transactionHash, accountType) - // - // } - // } - fun getTransferData( wallet: String, chainType: ChainType, @@ -51,7 +37,6 @@ class TransferService( accountType: AccountType, ): Mono { println("transactionHash : $transactionHash") - println("여기까지는 와야됨 최소한") return transferRepository.existsByTransactionHash(transactionHash) .flatMap { if (it) { @@ -120,8 +105,8 @@ class TransferService( println("amount : " + amount) val isRelevantTransfer = when (accountType) { - AccountType.DEPOSIT -> from.equals(wallet, ignoreCase = true) && to.equals(adminAddress, ignoreCase = true) - AccountType.WITHDRAW -> from.equals(adminAddress, ignoreCase = true) && to.equals(wallet, ignoreCase = true) + AccountType.DEPOSIT -> from.equals(wallet, ignoreCase = true) && to.equals(adminInfoProperties.address, ignoreCase = true) + AccountType.WITHDRAW -> from.equals(adminInfoProperties.address, ignoreCase = true) && to.equals(wallet, ignoreCase = true) } return if (isRelevantTransfer) { @@ -151,8 +136,8 @@ class TransferService( val tokenId = BigInteger(log.topics[3].removePrefix("0x"), 16).toString() val isRelevantTransfer = when (accountType) { - AccountType.DEPOSIT -> from.equals(wallet, ignoreCase = true) && to.equals(adminAddress, ignoreCase = true) - AccountType.WITHDRAW -> from.equals(adminAddress, ignoreCase = true) && to.equals(wallet, ignoreCase = true) + AccountType.DEPOSIT -> from.equals(wallet, ignoreCase = true) && to.equals(adminInfoProperties.address, ignoreCase = true) + AccountType.WITHDRAW -> from.equals(adminInfoProperties.address, ignoreCase = true) && to.equals(wallet, ignoreCase = true) } return if (isRelevantTransfer) { diff --git a/src/main/kotlin/com/api/admin/service/Web3jService.kt b/src/main/kotlin/com/api/admin/service/Web3jService.kt index 441580f..940b3fa 100644 --- a/src/main/kotlin/com/api/admin/service/Web3jService.kt +++ b/src/main/kotlin/com/api/admin/service/Web3jService.kt @@ -3,6 +3,7 @@ package com.api.admin.service 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.fasterxml.jackson.databind.JsonNode import com.fasterxml.jackson.databind.ObjectMapper import org.springframework.stereotype.Service @@ -24,9 +25,10 @@ class Web3jService( private val infuraApiService: InfuraApiService, private val transferService: TransferService, private val nftRepository: NftRepository, + private val adminInfoProperties: AdminInfoProperties ) { - private val privateKey = "4ec9e64419547100af4f38d7ec57ba1de2d5c36a7dfb03f1a349b2c5b62ac0a9" + // private val privateKey = "4ec9e64419547100af4f38d7ec57ba1de2d5c36a7dfb03f1a349b2c5b62ac0a9" private fun getChainId(chain: ChainType): Long { val chain = when (chain) { @@ -47,7 +49,7 @@ class Web3jService( nftId: Long, ): Mono { return nftRepository.findById(nftId).flatMap { nft-> - val credentials = Credentials.create(privateKey) + val credentials = Credentials.create(adminInfoProperties.privatekey) createERC721TransactionData(credentials, nft.tokenAddress, toAddress, BigInteger(nft.tokenId), nft.chainType) .flatMap { transactionHash -> waitForTransactionReceipt(transactionHash, nft.chainType) @@ -112,7 +114,7 @@ class Web3jService( amount: BigDecimal, chainType: ChainType ): Mono { - val credentials = Credentials.create(privateKey) + val credentials = Credentials.create(adminInfoProperties.privatekey) val weiAmount = amountToWei(amount) return createERC20TransactionData(credentials, recipientAddress, weiAmount, chainType) .flatMap { transactionHash -> diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index f35b490..9999397 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -1,20 +1,9 @@ -server: - port: 8084 spring: application: name: admin - datasource: - url: jdbc:postgresql://localhost:5435/admin - username: admin - password: admin - flyway: - locations: classpath:db/migration - r2dbc: - url: r2dbc:postgresql://localhost:5435/admin - username: admin - password: admin - rabbitmq: - host: localhost - port: 5672 - username: closeSea - password: closeSeaP@ssword + config: + import: "optional:configserver:http://localhost:9000" + cloud: + config: + fail-fast: true + diff --git a/src/main/resources/bootstrap.yaml b/src/main/resources/bootstrap.yaml new file mode 100644 index 0000000..9282f74 --- /dev/null +++ b/src/main/resources/bootstrap.yaml @@ -0,0 +1,9 @@ +spring: + application: + name: admin + cloud: + config: + uri: https://localhost:9000 + fail-fast: true + profiles: + active: local