Skip to content

Commit

Permalink
apply config
Browse files Browse the repository at this point in the history
  • Loading branch information
min-96 committed Jul 23, 2024
1 parent 906e009 commit 2fb2bac
Show file tree
Hide file tree
Showing 12 changed files with 76 additions and 58 deletions.
8 changes: 8 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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<KotlinCompile> {
kotlinOptions {
freeCompilerArgs += "-Xjsr305=strict"
Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/com/api/admin/AdminApplication.kt
Original file line number Diff line number Diff line change
@@ -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<String>) {
Expand Down
10 changes: 10 additions & 0 deletions src/main/kotlin/com/api/admin/properties/AdminInfoProperties.kt
Original file line number Diff line number Diff line change
@@ -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,
)
Original file line number Diff line number Diff line change
@@ -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,
)
8 changes: 8 additions & 0 deletions src/main/kotlin/com/api/admin/properties/NftApiProperties.kt
Original file line number Diff line number Diff line change
@@ -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
)
Original file line number Diff line number Diff line change
@@ -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
Expand Down
19 changes: 9 additions & 10 deletions src/main/kotlin/com/api/admin/service/InfuraApiService.kt
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand Down
11 changes: 5 additions & 6 deletions src/main/kotlin/com/api/admin/service/NftApiService.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<NftResponse> {
Expand All @@ -23,8 +26,4 @@ class NftApiService {
.bodyToMono(NftResponse::class.java)
}


companion object {
val uri = "http://localhost:8082/v1/nft"
}
}
27 changes: 6 additions & 21 deletions src/main/kotlin/com/api/admin/service/TransferService.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -19,39 +19,24 @@ 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,
transactionHash: String,
accountType: AccountType,
): Mono<Void> {
println("transactionHash : $transactionHash")
println("여기까지는 와야됨 최소한")
return transferRepository.existsByTransactionHash(transactionHash)
.flatMap {
if (it) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
8 changes: 5 additions & 3 deletions src/main/kotlin/com/api/admin/service/Web3jService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand All @@ -47,7 +49,7 @@ class Web3jService(
nftId: Long,
): Mono<Void> {
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)
Expand Down Expand Up @@ -112,7 +114,7 @@ class Web3jService(
amount: BigDecimal,
chainType: ChainType
): Mono<Void> {
val credentials = Credentials.create(privateKey)
val credentials = Credentials.create(adminInfoProperties.privatekey)
val weiAmount = amountToWei(amount)
return createERC20TransactionData(credentials, recipientAddress, weiAmount, chainType)
.flatMap { transactionHash ->
Expand Down
23 changes: 6 additions & 17 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -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

9 changes: 9 additions & 0 deletions src/main/resources/bootstrap.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
spring:
application:
name: admin
cloud:
config:
uri: https://localhost:9000
fail-fast: true
profiles:
active: local

0 comments on commit 2fb2bac

Please sign in to comment.