Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug with wrong taker fee detail and add withdraw flag and ordering to transactions list #397

Merged
merged 2 commits into from
Nov 20, 2023
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 @@ -76,7 +76,7 @@ class FeeCalculatorImpl(
"exchange",
LocalDateTime.now(),
FinancialActionCategory.FEE,
createMap(trade, makerOrder)
createMap(trade, takerOrder)
)
logger.info("trade event takerFeeAction $takerFeeAction")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ internal class FeeCalculatorImplTest {
val actions =
feeCalculator.createFeeActions(Valid.tradeEvent, Valid.makerOrder, Valid.takerOrder, null, null)
assertThat(actions.makerFeeAction).isNotNull
assertThat(actions.makerFeeAction.detail["ouid"]).isEqualTo(Valid.makerOrder.ouid)
assertThat(actions.takerFeeAction).isNotNull
assertThat(actions.takerFeeAction.detail["ouid"]).isEqualTo(Valid.takerOrder.ouid)
}

@Test
Expand All @@ -31,6 +33,7 @@ internal class FeeCalculatorImplTest {
assertThat(pointer).isEqualTo("order_2")
assertThat(receiver).isEqualTo(receiverAddress)
assertThat(receiverWalletType).isEqualTo("exchange")
assertThat(detail["ouid"]).isEqualTo(Valid.makerOrder.ouid)
}

with(actions.takerFeeAction) {
Expand All @@ -40,6 +43,7 @@ internal class FeeCalculatorImplTest {
assertThat(pointer).isEqualTo("order_1")
assertThat(receiver).isEqualTo(receiverAddress)
assertThat(receiverWalletType).isEqualTo("exchange")
assertThat(detail["ouid"]).isEqualTo(Valid.takerOrder.ouid)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class TransactionController(private val manager: TransactionManager) {
request.category,
LocalDateTime.ofInstant(Instant.ofEpochMilli(request.startTime), ZoneId.systemDefault()),
LocalDateTime.ofInstant(Instant.ofEpochMilli(request.endTime), ZoneId.systemDefault()),
request.ascendingByTime,
request.limit,
request.offset
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ data class TransactionRequest(
val startTime: Long,
val endTime: Long,
val limit: Int,
val offset: Int
val offset: Int,
val ascendingByTime: Boolean = false
)
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import co.nilin.opex.wallet.core.model.TransactionHistory
import co.nilin.opex.wallet.core.spi.TransactionManager
import kotlinx.coroutines.runBlocking
import org.junit.jupiter.api.Test
import org.mockito.ArgumentMatchers.*
import org.mockito.Mockito
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient
Expand Down Expand Up @@ -38,16 +37,16 @@ class TransactionControllerTest {
val uuid = "uuid";
val t = System.currentTimeMillis()
val history = TransactionHistory(
1L, "c", BigDecimal.ONE, "desc", "ref", System.currentTimeMillis(), "cat", mapOf(Pair("key1", "val1"))
1L, "c", BigDecimal.ONE, "desc", "ref", System.currentTimeMillis(), "cat", mapOf(Pair("key1", "val1")), true
)
runBlocking {
Mockito.`when`(
manager.findTransactions(
uuid, "c", null, LocalDateTime.ofInstant(Instant.ofEpochMilli(t), ZoneId.systemDefault()), LocalDateTime.ofInstant(Instant.ofEpochMilli(t), ZoneId.systemDefault()), 1, 1
uuid, "c", null, LocalDateTime.ofInstant(Instant.ofEpochMilli(t), ZoneId.systemDefault()), LocalDateTime.ofInstant(Instant.ofEpochMilli(t), ZoneId.systemDefault()), true, 1, 1
)
).thenReturn(listOf(history))
webClient.post().uri("/transaction/$uuid").accept(MediaType.APPLICATION_JSON)
.bodyValue(TransactionRequest("c", null, t, t, 1, 1))
.bodyValue(TransactionRequest("c", null, t, t, 1, 1, true))
.exchange()
.expectStatus().isOk
.expectBodyList(TransactionHistory::class.java)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class TransferManagerImplIT {
val cc = "CC"
val amount = BigDecimal.valueOf(10)
var sourceUuid: String? = null
var destUuid: String? = null

@BeforeEach
fun setup() {
Expand Down Expand Up @@ -222,20 +223,77 @@ class TransferManagerImplIT {
)
val thwMatch = thw.find { th -> th.id.toString().equals(result.tx) }
assertNotNull(thwMatch)

val thdMatch = thd.find { th -> th.id.toString().equals(result.tx) }
assertNotNull(thdMatch)

assertEquals(additionalData, thwMatch!!.additionalData)

val th = transactionManager.findTransactions(
owner.uuid, currency.symbol, "NORMAL", LocalDateTime.now().minusHours(1), LocalDateTime.now(), 100, 0
owner.uuid, currency.symbol, "NORMAL", LocalDateTime.now().minusHours(1), LocalDateTime.now(), true, 100, 0
)

val thMatch = th.find { i -> i.id.toString().equals(result.tx) }
assertEquals(additionalData, thMatch!!.additionalData)
}
}

@Test
fun whenTransfer_thenWithdrawFlagIsCorrect() {
runBlocking {
val currency = currencyService.getCurrency(cc)!!

destUuid = UUID.randomUUID().toString()
setupWallets(destUuid!!)

val sender = walletOwnerManager.findWalletOwner(sourceUuid!!)
val receiver = walletOwnerManager.findWalletOwner(destUuid!!)


val sourceWallet = walletManager.findWalletByOwnerAndCurrencyAndType(sender!!, senderWalletType, currency)
val receiverWallet = walletManager.findWalletByOwnerAndCurrencyAndType(receiver!!, receiverWalletType, currency)

val result = transferManager.transfer(
TransferCommand(
sourceWallet!!,
receiverWallet!!,
Amount(sourceWallet.currency, amount),
"Amount1 ${System.currentTimeMillis()}", "Ref1 ${System.currentTimeMillis()}",
"NORMAL",
emptyMap()
)
)

val thw = transactionManager.findWithdrawTransactions(
sender.uuid, currency.symbol, LocalDateTime.now().minusHours(1), LocalDateTime.now(), 100, 0
)

val thd = transactionManager.findDepositTransactions(
receiver.uuid, currency.symbol, LocalDateTime.now().minusHours(1), LocalDateTime.now(), 100, 0
)

val thwMatch = thw.find { th -> th.id.toString().equals(result.tx) }
assertNotNull(thwMatch)
assertTrue(thwMatch!!.withdraw)

val thdMatch = thd.find { th -> th.id.toString().equals(result.tx) }
assertNotNull(thdMatch)
assertFalse(thdMatch!!.withdraw)

val thSender = transactionManager.findTransactions(
sender.uuid, currency.symbol, "NORMAL", LocalDateTime.now().minusHours(1), LocalDateTime.now(), true, 100, 0
)
val thSenderMatch = thSender.find { i -> i.id.toString().equals(result.tx) }
assertTrue(thSenderMatch!!.withdraw)

val thReceiver = transactionManager.findTransactions(
receiver.uuid, currency.symbol, "NORMAL", LocalDateTime.now().minusHours(1), LocalDateTime.now(), true, 100, 0
)
val thReceiverMatch = thReceiver.find { i -> i.id.toString().equals(result.tx) }
assertFalse(thReceiverMatch!!.withdraw)
}
}

fun setupWallets(sourceUuid: String) {
runBlocking {
var currency = currencyService.getCurrency(cc)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ data class TransactionHistory(
val ref: String?,
val date: Long,
val category: String?,
val additionalData: Map<String, Any>?
val additionalData: Map<String, Any>?,
val withdraw: Boolean
)
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ interface TransactionManager {
offset: Int
): List<TransactionHistory>

suspend fun findTransactions(uuid: String, coin: String?, category: String?, startTime: LocalDateTime, endTime: LocalDateTime, limit: Int, offset: Int): List<TransactionHistory>
suspend fun findTransactions(uuid: String, coin: String?, category: String?, startTime: LocalDateTime, endTime: LocalDateTime, asc: Boolean, limit: Int, offset: Int): List<TransactionHistory>
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package co.nilin.opex.wallet.ports.postgres.dao
import co.nilin.opex.wallet.ports.postgres.dto.DepositWithdrawTransaction
import co.nilin.opex.wallet.ports.postgres.dto.TransactionStat
import co.nilin.opex.wallet.ports.postgres.model.TransactionModel
import org.springframework.data.domain.Pageable
import org.springframework.data.r2dbc.repository.Query
import org.springframework.data.repository.query.Param
import org.springframework.data.repository.reactive.ReactiveCrudRepository
Expand Down Expand Up @@ -84,7 +85,7 @@ interface TransactionRepository : ReactiveCrudRepository<TransactionModel, Long>
@Query(
"""
select distinct t.id, w.currency, t.dest_amount as amount, t.description, t.transfer_ref as ref, t.transaction_date as date
, t.transfer_category as category, t.transfer_detail_json as detail
, t.transfer_category as category, t.transfer_detail_json as detail, t.source_wallet as sender, t.dest_wallet as receiver, w.id as owner
from wallet as w
inner join wallet_owner as wo on (w.owner = wo.id)
inner join transaction as t on (w.id = t.dest_wallet)
Expand All @@ -105,7 +106,7 @@ interface TransactionRepository : ReactiveCrudRepository<TransactionModel, Long>
@Query(
"""
select distinct t.id, w.currency, t.dest_amount as amount, t.description, t.transfer_ref as ref, t.transaction_date as date
, t.transfer_category as category, t.transfer_detail_json as detail
, t.transfer_category as category, t.transfer_detail_json as detail, t.source_wallet as sender, t.dest_wallet as receiver, w.id as owner
from wallet as w
inner join wallet_owner as wo on (w.owner = wo.id)
inner join transaction as t on (w.id = t.source_wallet)
Expand All @@ -115,7 +116,7 @@ interface TransactionRepository : ReactiveCrudRepository<TransactionModel, Long>
limit :limit
"""
)
suspend fun findWithdrawTransactionsByUUID(
fun findWithdrawTransactionsByUUID(
@Param("uuid") uuid: String,
@Param("startTime") startTime: LocalDateTime,
@Param("endTime") endTime: LocalDateTime,
Expand All @@ -125,7 +126,7 @@ interface TransactionRepository : ReactiveCrudRepository<TransactionModel, Long>
@Query(
"""
select distinct t.id, w.currency, t.dest_amount as amount, t.description, t.transfer_ref as ref, t.transaction_date as date
, t.transfer_category as category, t.transfer_detail_json as detail
, t.transfer_category as category, t.transfer_detail_json as detail, t.source_wallet as sender, t.dest_wallet as receiver, w.id as owner
from wallet as w
inner join wallet_owner as wo on (w.owner = wo.id)
inner join transaction as t on (w.id = t.dest_wallet)
Expand All @@ -137,7 +138,7 @@ interface TransactionRepository : ReactiveCrudRepository<TransactionModel, Long>
limit :limit
"""
)
suspend fun findDepositTransactionsByUUIDAndCurrency(
fun findDepositTransactionsByUUIDAndCurrency(
@Param("uuid") uuid: String,
@Param("currency") currency: String,
@Param("startTime") startTime: LocalDateTime,
Expand All @@ -148,7 +149,7 @@ interface TransactionRepository : ReactiveCrudRepository<TransactionModel, Long>
@Query(
"""
select distinct t.id, w.currency, t.dest_amount as amount, t.description, t.transfer_ref as ref, t.transaction_date as date
, t.transfer_category as category, t.transfer_detail_json as detail
, t.transfer_category as category, t.transfer_detail_json as detail, t.source_wallet as sender, t.dest_wallet as receiver, w.id as owner
from wallet as w
inner join wallet_owner as wo on (w.owner = wo.id)
inner join transaction as t on (w.id = t.source_wallet)
Expand All @@ -159,18 +160,18 @@ interface TransactionRepository : ReactiveCrudRepository<TransactionModel, Long>
limit :limit
"""
)
suspend fun findWithdrawTransactionsByUUIDAndCurrency(
fun findWithdrawTransactionsByUUIDAndCurrency(
@Param("uuid") uuid: String,
@Param("currency") currency: String,
@Param("startTime") startTime: LocalDateTime,
@Param("endTime") endTime: LocalDateTime,
@Param("limit") limit: Int,
@Param("limit") limit: Int
): Flux<DepositWithdrawTransaction>

@Query(
"""
select distinct t.id, w.currency, t.dest_amount as amount, t.description, t.transfer_ref as ref, t.transaction_date as date
, t.transfer_category as category, t.transfer_detail_json as detail
, t.transfer_category as category, t.transfer_detail_json as detail, t.source_wallet as sender, t.dest_wallet as receiver, w.id as owner
from wallet as w
inner join wallet_owner as wo on (w.owner = wo.id)
inner join transaction as t on w.id in (t.source_wallet, t.dest_wallet)
Expand All @@ -179,18 +180,15 @@ interface TransactionRepository : ReactiveCrudRepository<TransactionModel, Long>
and t.transaction_date <= :endTime
and (:category is null or t.transfer_category = :category)
and (:currency is null or w.currency = :currency)
limit :limit
offset :offset
"""
)
suspend fun findTransactions(
fun findTransactions(
@Param("uuid") uuid: String,
@Param("currency") currency: String?,
@Param("category") category: String?,
@Param("startTime") startTime: LocalDateTime,
@Param("endTime") endTime: LocalDateTime,
@Param("limit") limit: Int,
@Param("offset") offset: Int,
pageable: Pageable
): Flux<DepositWithdrawTransaction>

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@ data class DepositWithdrawTransaction(
val ref: String?,
val date: LocalDateTime,
val category: String,
val detail: String?
val detail: String?,
val sender: Long?,
val receiver: Long?,
val owner: Long?
)
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import co.nilin.opex.wallet.ports.postgres.model.TransactionModel
import com.fasterxml.jackson.databind.ObjectMapper
import kotlinx.coroutines.reactive.awaitFirstOrElse
import kotlinx.coroutines.reactive.awaitSingle
import org.springframework.data.domain.PageRequest
import org.springframework.data.domain.Sort
import org.springframework.stereotype.Service
import java.time.LocalDateTime
import java.time.ZoneId
Expand Down Expand Up @@ -59,7 +61,8 @@ class TransactionManagerImpl(
it.ref,
it.date.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli(),
it.category,
if (it.detail == null) emptyMap() else objectMapper.readValue(it.detail, Map::class.java) as Map<String, Any>?
if (it.detail == null) emptyMap() else objectMapper.readValue(it.detail, Map::class.java) as Map<String, Any>?,
it.sender == it.owner
)
}
}
Expand Down Expand Up @@ -88,7 +91,8 @@ class TransactionManagerImpl(
it.ref,
it.date.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli(),
it.category,
if (it.detail == null) emptyMap() else objectMapper.readValue(it.detail, Map::class.java) as Map<String, Any>?
if (it.detail == null) emptyMap() else objectMapper.readValue(it.detail, Map::class.java) as Map<String, Any>?,
it.sender == it.owner
)
}
}
Expand All @@ -99,11 +103,12 @@ class TransactionManagerImpl(
category: String?,
startTime: LocalDateTime,
endTime: LocalDateTime,
asc: Boolean,
limit: Int,
offset: Int
): List<TransactionHistory> {
val transactions =
transactionRepository.findTransactions(uuid, coin, category, startTime, endTime, limit, offset)
transactionRepository.findTransactions(uuid, coin, category, startTime, endTime, PageRequest.of(offset, limit, Sort.by(if (asc) Sort.Direction.ASC else Sort.Direction.DESC, "transaction_date")))
return transactions.collectList()
.awaitFirstOrElse { emptyList() }
.map {
Expand All @@ -115,7 +120,8 @@ class TransactionManagerImpl(
it.ref,
it.date.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli(),
it.category,
if (it.detail == null) emptyMap() else objectMapper.readValue(it.detail, Map::class.java) as Map<String, Any>?
if (it.detail == null) emptyMap() else objectMapper.readValue(it.detail, Map::class.java) as Map<String, Any>?,
it.sender == it.owner
)
}
}
Expand Down
Loading