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
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,3 @@ build/
!/.mvn/

.DS_Store
resources
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ import co.nilin.opex.accountant.core.spi.WalletProxy
import co.nilin.opex.accountant.ports.walletproxy.data.BooleanResponse
import co.nilin.opex.matching.engine.core.eventh.events.SubmitOrderEvent
import co.nilin.opex.matching.engine.core.model.OrderDirection
import co.nilin.opex.utility.error.data.OpexError
import co.nilin.opex.utility.error.data.OpexException
import org.slf4j.LoggerFactory
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController
import java.math.BigDecimal

Expand Down Expand Up @@ -59,9 +62,20 @@ class AccountantController(
return pairConfigLoader.loadPairConfigs()
}

@GetMapping("/config/fee/all")
suspend fun fetchFeeConfigs(): List<PairFeeResponse> {
@GetMapping("/config/fee")
suspend fun getFeeConfigs(): List<PairFeeResponse> {
return pairConfigLoader.loadPairFeeConfigs()
.map { PairFeeResponse(it.pairConfig.pair, it.direction, it.userLevel, it.makerFee, it.takerFee) }
}

@GetMapping("/config/fee/{pair}")
suspend fun getFeeConfig(
@PathVariable pair: String,
@RequestParam(required = false) direction: OrderDirection?,
@RequestParam(required = false) userLevel: String?
): PairFeeResponse {
val fee = pairConfigLoader.loadPairFeeConfigs(pair, direction ?: OrderDirection.BID, userLevel ?: "*")
?: throw OpexException(OpexError.PairFeeNotFound)
return PairFeeResponse(fee.pairConfig.pair, fee.direction, fee.userLevel, fee.makerFee, fee.takerFee)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class RichTrade(
val makerRemainedQuantity: BigDecimal,
val makerCommision: BigDecimal,
val makerCommisionAsset: String,
val matchedPrice: BigDecimal,
val matchedQuantity: BigDecimal,
val tradeDateTime: LocalDateTime
)
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import co.nilin.opex.accountant.core.model.FinancialAction
import co.nilin.opex.accountant.core.model.Order
import co.nilin.opex.accountant.core.spi.*
import co.nilin.opex.matching.engine.core.eventh.events.TradeEvent
import co.nilin.opex.matching.engine.core.model.OrderDirection
import org.slf4j.LoggerFactory
import org.springframework.transaction.annotation.Transactional
import java.math.BigDecimal
Expand Down Expand Up @@ -135,6 +136,9 @@ open class TradeManagerImpl(
financialActions.add(takerFeeAction)
}

val takerPrice = trade.takerPrice.toBigDecimal().multiply(takerOrder.rightSideFraction)
val makerPrice = trade.makerPrice.toBigDecimal().multiply(makerOrder.rightSideFraction)

richTradePublisher.publish(
RichTrade(
trade.tradeId,
Expand All @@ -143,7 +147,7 @@ open class TradeManagerImpl(
trade.takerUuid,
trade.takerOrderId,
trade.takerDirection,
trade.takerPrice.toBigDecimal().multiply(takerOrder.rightSideFraction),
takerPrice,
takerOrder.origQuantity,
takerOrder.origPrice.multiply(takerOrder.origQuantity),
trade.takerRemainedQuantity.toBigDecimal().multiply(takerOrder.leftSideFraction),
Expand All @@ -153,12 +157,13 @@ open class TradeManagerImpl(
trade.makerUuid,
trade.makerOrderId,
trade.makerDirection,
trade.makerPrice.toBigDecimal().multiply(makerOrder.rightSideFraction),
makerPrice,
makerOrder.origQuantity,
makerOrder.origPrice.multiply(makerOrder.origQuantity),
trade.makerRemainedQuantity.toBigDecimal().multiply(makerOrder.leftSideFraction),
feeActions.makerFeeAction.amount,
feeActions.makerFeeAction.symbol,
makerPrice,
trade.matchedQuantity.toBigDecimal().multiply(makerOrder.leftSideFraction),
trade.eventDate
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ interface PairConfigLoader {

suspend fun loadPairFeeConfigs(): List<PairFeeConfig>

suspend fun loadPairFeeConfigs(direction: OrderDirection, userLevel: String): List<PairFeeConfig>

suspend fun loadPairFeeConfigs(pair: String, direction: OrderDirection, userLevel: String): PairFeeConfig?

suspend fun load(pair: String, direction: OrderDirection, userLevel: String): PairFeeConfig

suspend fun load(pair: String, direction: OrderDirection): PairConfig
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,20 @@ import org.springframework.data.r2dbc.repository.Query
import org.springframework.data.repository.query.Param
import org.springframework.data.repository.reactive.ReactiveCrudRepository
import org.springframework.stereotype.Repository
import reactor.core.publisher.Flux
import reactor.core.publisher.Mono

@Repository
interface PairFeeConfigRepository : ReactiveCrudRepository<PairFeeConfigModel, Long> {

@Query("select * from pair_fee_config where pair_config_id = :pair and direction = :direction and user_level = :userLevel")
fun findByPairAndDirectionAndUserLevel(
@Param("pair") pair: String,
@Param("direction") direction: OrderDirection,
@Param("userLevel") userLevel: String
): Mono<PairFeeConfigModel?>

@Query("select * from pair_fee_config where direction = :direction and user_level = :userLevel")
fun findByDirectionAndUserLevel(direction: OrderDirection, userLevel: String): Flux<PairFeeConfigModel>

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import co.nilin.opex.utility.error.data.OpexException
import kotlinx.coroutines.reactive.awaitFirstOrElse
import kotlinx.coroutines.reactive.awaitFirstOrNull
import kotlinx.coroutines.reactive.awaitSingle
import kotlinx.coroutines.reactor.awaitSingleOrNull
import org.springframework.stereotype.Component
import java.math.BigDecimal

@Component
class PairConfigLoaderImpl(
Expand All @@ -38,6 +40,39 @@ class PairConfigLoaderImpl(
}
}

override suspend fun loadPairFeeConfigs(direction: OrderDirection, userLevel: String): List<PairFeeConfig> {
return pairFeeConfigRepository.findByDirectionAndUserLevel(direction, userLevel)
.collectList()
.awaitFirstOrElse { emptyList() }
.map {
val pairConfig = pairConfigRepository.findById(it.pairConfigId).awaitSingle().asPairConfig()
PairFeeConfig(
pairConfig,
it.direction,
it.userLevel,
it.makerFee,
it.takerFee
)
}
}

override suspend fun loadPairFeeConfigs(
pair: String,
direction: OrderDirection,
userLevel: String
): PairFeeConfig? {
val fee = pairFeeConfigRepository.findByPairAndDirectionAndUserLevel(pair, direction, userLevel)
.awaitSingleOrNull() ?: return null
val pairConfig = pairConfigRepository.findById(fee.pairConfigId).awaitSingle().asPairConfig()
return PairFeeConfig(
pairConfig,
fee.direction,
fee.userLevel,
fee.makerFee,
fee.takerFee
)
}

override suspend fun load(pair: String, direction: OrderDirection, userLevel: String): PairFeeConfig {
val pairConfig = pairConfigRepository.findById(pair).awaitFirstOrElse {
val error = OpexError.InvalidPair
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ object Valid {
1.0.toBigDecimal(),
"",
1.0.toBigDecimal(),
1.0.toBigDecimal(),
currentTime
)

Expand Down
4 changes: 2 additions & 2 deletions api/api-app/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@
<artifactId>api-core</artifactId>
</dependency>
<dependency>
<groupId>co.nilin.opex.api.ports.kafka.listener</groupId>
<artifactId>api-eventlistener-kafka</artifactId>
<groupId>co.nilin.opex.api.ports.proxy</groupId>
<artifactId>api-proxy-rest</artifactId>
</dependency>
<dependency>
<groupId>co.nilin.opex.api.ports.binance</groupId>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import javax.annotation.PostConstruct
@Component
@DependsOn("postgresConfig")
class InitializeService(private val symbolMapRepository: SymbolMapRepository) {

@Autowired
private lateinit var preferences: Preferences

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package co.nilin.opex.util.vault
package co.nilin.opex.api.app.utils

import org.springframework.vault.authentication.AppIdUserIdMechanism

class VaultUserIdMechanism() : AppIdUserIdMechanism {
class VaultUserIdMechanism : AppIdUserIdMechanism {
override fun createUserId(): String {
return System.getenv("BACKEND_USER")
}
Expand Down
6 changes: 5 additions & 1 deletion api/api-app/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ spring:
scheme: http
authentication: APPID
app-id:
user-id: co.nilin.opex.util.vault.VaultUserIdMechanism
user-id: co.nilin.opex.api.app.utils.VaultUserIdMechanism
fail-fast: true
kv:
enabled: true
Expand All @@ -51,8 +51,12 @@ app:
url: lb://opex-matching-gateway
wallet:
url: lb://opex-wallet
market:
url: lb://opex-market
opex-bc-gateway:
url: lb://opex-bc-gateway
auth:
cert-url: lb://opex-auth/auth/realms/opex/protocol/openid-connect/certs
binance:
api-url: https://api1.binance.com
swagger.authUrl: ${SWAGGER_AUTH_URL:https://api.opex.dev/auth}/realms/opex/protocol/openid-connect/token

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package co.nilin.opex.api.core.inout

import java.math.BigDecimal

data class BestPrice(
val symbol: String,
val bidPrice: BigDecimal?,
val askPrice: BigDecimal?,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package co.nilin.opex.api.core.inout

data class CountResponse(val value: Long)

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package co.nilin.opex.api.core.inout

import java.math.BigDecimal

data class CurrencyRate(
val base: String,
val quote: String,
val source: String,
val rate: BigDecimal
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package co.nilin.opex.api.core.inout

data class GlobalPrice(
val symbol: String,
val price: Float
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package co.nilin.opex.api.core.inout
import java.math.BigDecimal
import java.util.*

data class MarketTradeResponse(
data class MarketTrade(
val symbol: String,
val baseAsset: String,
val quoteAsset: String,
val id: Long,
val price: BigDecimal,
val qty: BigDecimal,
val quoteQty: BigDecimal,
val quantity: BigDecimal,
val quoteQuantity: BigDecimal,
val time: Date,
val isBestMatch: Boolean,
val isMakerBuyer: Boolean
Expand Down
Loading