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 @@ -12,7 +12,7 @@ spring:
r2dbc:
url: r2dbc:postgresql://${DB_IP_PORT:localhost}/opex
username: ${dbusername:opex}
password: ${dbpassword:hiopex}
password: ${DB_PASS:hiopex}
initialization-mode: always
cloud:
bootstrap:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import org.springframework.data.r2dbc.repository.Query
import org.springframework.data.repository.reactive.ReactiveCrudRepository
import org.springframework.stereotype.Repository
import reactor.core.publisher.Mono
import java.math.BigDecimal

@Repository
interface PairSettingRepository : ReactiveCrudRepository<PairSettingModel, String> {
fun findByPair(pair: String): Mono<PairSettingModel>

@Query("insert into pair_setting(pair,is_available) values(:pair,:isAvailable) ")
fun insert(pair: String, isAvailable: Boolean): Mono<PairSettingModel>
@Query("insert into pair_setting(pair,is_available,min_order,max_order,order_types) values(:pair,:isAvailable,:minOrder,:maxOrder,:orderTypes) ")
fun insert(pair: String, isAvailable: Boolean , minOrder : BigDecimal, maxOrder : BigDecimal,orderTypes : String): Mono<Void>
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package co.nilin.opex.matching.gateway.ports.postgres.service

import co.nilin.opex.common.OpexError
import co.nilin.opex.matching.gateway.ports.postgres.dao.PairSettingRepository
import co.nilin.opex.matching.gateway.ports.postgres.dto.PairSetting
import co.nilin.opex.matching.gateway.ports.postgres.util.CacheManager
Expand All @@ -12,6 +11,7 @@ import kotlinx.coroutines.reactive.awaitFirstOrNull
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Service
import java.math.BigDecimal
import java.util.concurrent.TimeUnit
import javax.annotation.PostConstruct

Expand All @@ -30,26 +30,37 @@ class PairSettingInitializer(
fun initialize() {
logger.info(
"""
================================================
Initialize Pair Settings
================================================
================================================================================================
Initialize Pair Settings
================================================================================================
"""
)
scope.launch {
try {
symbols.split(",").forEach { pair ->
val existingPair = pairSettingRepository.findByPair(pair).awaitFirstOrNull()

val pairToCache = existingPair ?: pairSettingRepository.insert(pair, false).awaitFirstOrNull()
?: throw OpexError.BadRequest.exception()
logger.info("Added Pair: $pair")
val pairToCache = existingPair ?: pairSettingRepository.insert(
pair,
false,
BigDecimal.ONE,
BigDecimal.ONE,
"LIMIT_ORDER,MARKET_ORDER"
).then(pairSettingRepository.findByPair(pair)).awaitFirstOrNull()
.also { if (it == null) logger.warn("Failed to insert pair: $pair") }
?: return@forEach

cacheManager.put(pair, pairToCache.toPairSetting(), 5, TimeUnit.MINUTES)
if (existingPair != null) logger.info("Pair already exists: $pair") else logger.info("Added Pair: $pair")

if (existingPair != null) {
logger.info("Pair already exists: $pair")
}
cacheManager.put(pair, pairToCache.toPairSetting(), 5, TimeUnit.MINUTES)
}
logger.info(
"""
================================================================================================
Completed Successfully
================================================================================================
"""
)
} catch (e: Exception) {
logger.error("Error initializing Pair Settings: ${e.message}")
throw e
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ $$
IF NOT EXISTS (SELECT 1
FROM information_schema.columns
WHERE table_name = 'pair_setting' AND column_name = 'min_order') THEN ALTER TABLE pair_setting
ADD COLUMN min_order DECIMAL NOT NULL default 0.000001;
ADD COLUMN min_order DECIMAL NOT NULL default 1;
END IF;
IF NOT EXISTS (SELECT 1
FROM information_schema.columns
WHERE table_name = 'pair_setting' AND column_name = 'max_order') THEN ALTER TABLE pair_setting
ADD COLUMN max_order DECIMAL NOT NULL default 100;
ADD COLUMN max_order DECIMAL NOT NULL default 1;
END IF;
IF NOT EXISTS (SELECT 1
FROM information_schema.columns
Expand Down