Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,17 @@ package co.nilin.opex.bcgateway.app.config


import co.nilin.opex.bcgateway.core.api.AssignAddressService
import co.nilin.opex.bcgateway.core.api.ChainSyncService
import co.nilin.opex.bcgateway.core.api.InfoService
import co.nilin.opex.bcgateway.core.api.WalletSyncService
import co.nilin.opex.bcgateway.core.service.AssignAddressServiceImpl
import co.nilin.opex.bcgateway.core.service.ChainSyncServiceImpl
import co.nilin.opex.bcgateway.core.service.InfoServiceImpl
import co.nilin.opex.bcgateway.core.service.WalletSyncServiceImpl
import co.nilin.opex.bcgateway.core.spi.*
import co.nilin.opex.bcgateway.core.spi.AssignedAddressHandler
import co.nilin.opex.bcgateway.core.spi.CurrencyHandler
import co.nilin.opex.bcgateway.core.spi.ReservedAddressHandler
import co.nilin.opex.bcgateway.ports.kafka.listener.consumer.AdminEventKafkaListener
import co.nilin.opex.bcgateway.ports.kafka.listener.spi.AdminEventListener
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.transaction.reactive.TransactionalOperator

@Configuration
class AppConfig {
Expand All @@ -29,46 +26,6 @@ class AppConfig {
return AssignAddressServiceImpl(currencyHandler, assignedAddressHandler, reservedAddressHandler)
}

@Bean
fun chainSyncService(
chainSyncSchedulerHandler: ChainSyncSchedulerHandler,
chainEndpointHandler: ChainEndpointHandler,
chainSyncRecordHandler: ChainSyncRecordHandler,
walletSyncRecordHandler: WalletSyncRecordHandler,
chainSyncRetryHandler: ChainSyncRetryHandler,
currencyHandler: CurrencyHandler,
operator: TransactionalOperator
): ChainSyncService {
return ChainSyncServiceImpl(
chainSyncSchedulerHandler,
chainEndpointHandler,
chainSyncRecordHandler,
walletSyncRecordHandler,
chainSyncRetryHandler,
currencyHandler,
operator,
AppDispatchers.chainSyncExecutor
)
}

@Bean
fun walletSyncService(
syncSchedulerHandler: WalletSyncSchedulerHandler,
walletProxy: WalletProxy,
walletSyncRecordHandler: WalletSyncRecordHandler,
assignedAddressHandler: AssignedAddressHandler,
currencyHandler: CurrencyHandler
): WalletSyncService {
return WalletSyncServiceImpl(
syncSchedulerHandler,
walletProxy,
walletSyncRecordHandler,
assignedAddressHandler,
currencyHandler,
AppDispatchers.walletSyncExecutor
)
}

@Bean
fun infoService(): InfoService {
return InfoServiceImpl()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package co.nilin.opex.bcgateway.app.config

import co.nilin.opex.bcgateway.ports.postgres.dao.*
import co.nilin.opex.bcgateway.ports.postgres.model.*
import co.nilin.opex.bcgateway.ports.postgres.model.AddressTypeModel
import co.nilin.opex.bcgateway.ports.postgres.model.ChainAddressTypeModel
import co.nilin.opex.bcgateway.ports.postgres.model.CurrencyImplementationModel
import co.nilin.opex.utility.preferences.AddressType
import co.nilin.opex.utility.preferences.Chain
import co.nilin.opex.utility.preferences.Currency
Expand All @@ -13,7 +15,6 @@ import kotlinx.coroutines.runBlocking
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.annotation.DependsOn
import org.springframework.stereotype.Component
import java.time.LocalDateTime
import javax.annotation.PostConstruct

@Component
Expand All @@ -22,11 +23,8 @@ class InitializeService(
private val addressTypeRepository: AddressTypeRepository,
private val chainRepository: ChainRepository,
private val chainAddressTypeRepository: ChainAddressTypeRepository,
private val chainEndpointRepository: ChainEndpointRepository,
private val currencyRepository: CurrencyRepository,
private val currencyImplementationRepository: CurrencyImplementationRepository,
private val chainSyncScheduleRepository: ChainSyncScheduleRepository,
private val walletSyncScheduleRepository: WalletSyncScheduleRepository
) {
@Autowired
private lateinit var preferences: Preferences
Expand All @@ -36,7 +34,6 @@ class InitializeService(
addAddressTypes(preferences.addressTypes)
addChains(preferences.chains)
addCurrencies(preferences.currencies)
addSchedules(preferences)
}

private suspend fun addAddressTypes(data: List<AddressType>) = coroutineScope {
Expand All @@ -54,8 +51,6 @@ class InitializeService(
ChainAddressTypeModel(null, it.name, addressTypeId)
}
runCatching { chainAddressTypeRepository.saveAll(items1).collectList().awaitSingleOrNull() }
val items2 = data.map { ChainEndpointModel(null, it.name, it.endpointUrl, null, null) }
runCatching { chainEndpointRepository.saveAll(items2).collectList().awaitSingleOrNull() }
}

private suspend fun addCurrencies(data: List<Currency>) = coroutineScope {
Expand All @@ -80,18 +75,4 @@ class InitializeService(
}
runCatching { currencyImplementationRepository.saveAll(items).collectList().awaitSingleOrNull() }
}

private suspend fun addSchedules(data: Preferences) = coroutineScope {
data.chains.map {
chainSyncScheduleRepository.insert(it.name, it.schedule.delay.toInt(), it.schedule.errorDelay.toInt())
.awaitSingleOrNull()
}
if (walletSyncScheduleRepository.existsById(1).awaitSingle()) null
else {
val item = WalletSyncScheduleModel(
null, LocalDateTime.now(), data.wallet.schedule.delay, data.wallet.schedule.batchSize
)
runCatching { walletSyncScheduleRepository.save(item).awaitSingleOrNull() }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class SecurityConfig(@Qualifier("loadBalanced") private val webClient: WebClient
.pathMatchers("/actuator/**").permitAll()
.pathMatchers("/swagger-ui/**").permitAll()
.pathMatchers("/swagger-resources/**").permitAll()
.pathMatchers("/wallet-sync/**").permitAll()
.pathMatchers("/filter/**").hasAuthority("SCOPE_trust")
.pathMatchers("/admin/**").hasRole("SCOPE_trust", "system-admin")
.pathMatchers("/address/**").permitAll()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import co.nilin.opex.bcgateway.app.dto.*
import co.nilin.opex.bcgateway.app.service.AdminService
import co.nilin.opex.bcgateway.core.model.AddressType
import co.nilin.opex.bcgateway.core.spi.AddressTypeHandler
import co.nilin.opex.bcgateway.core.spi.ChainEndpointHandler
import co.nilin.opex.bcgateway.core.spi.ChainLoader
import co.nilin.opex.bcgateway.core.spi.CurrencyHandler
import co.nilin.opex.utility.error.data.OpexError
Expand All @@ -18,14 +17,12 @@ class AdminController(
private val service: AdminService,
private val chainLoader: ChainLoader,
private val currencyHandler: CurrencyHandler,
private val addressTypeHandler: AddressTypeHandler,
private val chainEndpointHandler: ChainEndpointHandler
private val addressTypeHandler: AddressTypeHandler
) {

@GetMapping("/chain")
suspend fun getChains(): List<ChainResponse> {
return chainLoader.fetchAllChains()
.map { c -> ChainResponse(c.name, c.addressTypes.map { it.type }, c.endpoints.map { it.url }) }
return chainLoader.fetchAllChains().map { c -> ChainResponse(c.name, c.addressTypes.map { it.type }) }
}

@PostMapping("/chain")
Expand All @@ -35,16 +32,6 @@ class AdminController(
service.addChain(body)
}

@PostMapping("/chain/{chain}/endpoint")
suspend fun addChainEndpoint(@PathVariable chain: String, @RequestBody body: ChainEndpointRequest) {
chainEndpointHandler.addEndpoint(chain, body.url, body.username, body.password)
}

@DeleteMapping("/chain/{chain}/endpoint")
suspend fun deleteChainEndpoint(@PathVariable chain: String, @RequestParam url: String) {
chainEndpointHandler.deleteEndpoint(chain, url)
}

@GetMapping("/address/type")
suspend fun getAddressTypes(): List<AddressType> {
return addressTypeHandler.fetchAll()
Expand Down Expand Up @@ -108,4 +95,4 @@ class AdminController(
service.changeTokenWithdrawStatus(symbol, chain, status)
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RestController

@RestController
class NetworkController(val currencyHandler: CurrencyHandler) {
class CurrencyController(val currencyHandler: CurrencyHandler) {

@GetMapping("currency/{currency}")
suspend fun fetchCurrencyInfo(@PathVariable("currency") currency: String): CurrencyInfo {
return currencyHandler.fetchCurrencyInfo(currency)
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package co.nilin.opex.bcgateway.app.controller

import co.nilin.opex.bcgateway.core.api.WalletSyncService
import co.nilin.opex.bcgateway.core.model.Transfer
import co.nilin.opex.bcgateway.ports.postgres.impl.ChainHandler
import co.nilin.opex.utility.error.data.OpexError
import co.nilin.opex.utility.error.data.OpexException
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PutMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RestController

@RestController
class WalletSyncController(private val chainHandler: ChainHandler, private val walletSyncService: WalletSyncService) {
@PutMapping("wallet-sync/{chain}")
suspend fun syncTransferOnChain(@PathVariable chain: String, @RequestBody transfers: List<Transfer>) {
runCatching {
chainHandler.fetchChainInfo(chain)
}.onFailure {
throw OpexException(OpexError.NotFound)
}.onSuccess {
walletSyncService.syncTransfers(transfers)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@ package co.nilin.opex.bcgateway.app.dto

data class ChainResponse(
val name: String,
val addressTypes: List<String>,
val endpoints: List<String>
)
val addressTypes: List<String>
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ package co.nilin.opex.bcgateway.app.service
import co.nilin.opex.bcgateway.app.dto.AddChainRequest
import co.nilin.opex.bcgateway.app.dto.TokenRequest
import co.nilin.opex.bcgateway.core.model.CurrencyImplementation
import co.nilin.opex.bcgateway.core.spi.*
import co.nilin.opex.bcgateway.core.spi.AddressTypeHandler
import co.nilin.opex.bcgateway.core.spi.ChainLoader
import co.nilin.opex.bcgateway.core.spi.CurrencyHandler
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

@Service
class AdminService(
private val chainLoader: ChainLoader,
private val currencyHandler: CurrencyHandler,
private val chainScheduler: ChainSyncSchedulerHandler,
private val addressTypeHandler: AddressTypeHandler,
private val chainEndpointHandler: ChainEndpointHandler,
private val addressTypeHandler: AddressTypeHandler
) {

suspend fun addCurrency(name: String, symbol: String) {
Expand All @@ -30,10 +30,7 @@ class AdminService(

@Transactional
suspend fun addChain(body: AddChainRequest) {
val chain = chainLoader.addChain(body.name!!, body.addressType!!)
chainScheduler.scheduleChain(chain.name, body.scheduleDelaySeconds, body.scheduleErrorDelaySeconds)
if (body.scannerEndpoint != null)
chainEndpointHandler.addEndpoint(chain.name, body.scannerEndpoint, null, null)
chainLoader.addChain(body.name!!, body.addressType!!)
}

suspend fun addAddressType(name: String, addressRegex: String, memoRegex: String?) {
Expand All @@ -60,4 +57,4 @@ class AdminService(
currencyHandler.changeWithdrawStatus(symbol, chain, status)
}

}
}

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package co.nilin.opex.bcgateway.core.api

import co.nilin.opex.bcgateway.core.model.Transfer

interface WalletSyncService {
suspend fun startSyncWithWallet()
}
suspend fun syncTransfers(transfers: List<Transfer>)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package co.nilin.opex.bcgateway.core.model

data class AddressType(val id: Long, val type: String, val addressRegex: String, val memoRegex: String?)
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package co.nilin.opex.bcgateway.core.model

data class AddressType(val id: Long, val type: String, val addressRegex: String, val memoRegex: String?)
data class ReservedAddress(val address: String, val memo: String?, val type: AddressType)
data class AssignedAddress(
val uuid: String,
val address: String,
Expand Down
Loading