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 @@ -11,10 +11,6 @@ import co.nilin.opex.bcgateway.core.spi.WalletProxy
import co.nilin.opex.bcgateway.core.utils.LoggerDelegate
import kotlinx.coroutines.async
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.flow.asFlow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.mapNotNull
import kotlinx.coroutines.flow.toList
import org.slf4j.Logger
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
Expand All @@ -32,15 +28,14 @@ class WalletSyncServiceImpl(

@Transactional
override suspend fun syncTransfers(transfers: List<Transfer>) = coroutineScope {
logger.debug("Received ${transfers.size} number of transfers")
val groupedByChain = currencyHandler.fetchAllImplementations().groupBy { it.chain.name }
val deposits = transfers.map {
async {
coroutineScope {
val currencyImpl = async {
currencyHandler.findByChainAndTokenAddress(it.chain, it.tokenAddress)
?: throw IllegalStateException("Currency implementation not found")
}
val uuid = async { assignedAddressHandler.findUuid(it.receiver.address, it.receiver.memo) }
uuid.await()?.let { it to currencyImpl.await() }
val currencyImpl = groupedByChain[it.chain]?.find { c -> c.tokenAddress == it.tokenAddress }
?: throw IllegalStateException("Currency implementation not found")
assignedAddressHandler.findUuid(it.receiver.address, it.receiver.memo)?.let { it to currencyImpl }
}?.let { (uuid, currencyImpl) ->
sendDeposit(uuid, currencyImpl, it)
logger.info("Deposit synced for $uuid on ${currencyImpl.currency.symbol} - to ${it.receiver.address}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,26 @@ class AssignedAddressHandlerImpl(
) : AssignedAddressHandler {
override suspend fun fetchAssignedAddresses(user: String, addressTypes: List<AddressType>): List<AssignedAddress> {
if (addressTypes.isEmpty()) return emptyList()
val addressTypeMap = addressTypeRepository.findAll().map { aam ->
AddressType(aam.id!!, aam.type, aam.addressRegex, aam.memoRegex)
}.collectMap { it.id }.awaitFirst()
return assignedAddressRepository.findByUuidAndAddressType(
user, addressTypes.map(AddressType::id)
)
.map { model ->
AssignedAddress(
model.uuid, model.address, model.memo,
addressTypeRepository
.findById(model.addressTypeId)
.map { aam ->
AddressType(aam.id!!, aam.type, aam.addressRegex, aam.memoRegex)
}
.awaitFirst(),
assignedAddressChainRepository.findByAssignedAddress(model.id!!)
.map { cm ->
chainLoader.fetchChainInfo(cm.chain)
}
.toList().toMutableList()
)
}.toList()
).map { model ->
AssignedAddress(
model.uuid,
model.address,
model.memo,
addressTypeMap.getValue(model.addressTypeId),
assignedAddressChainRepository.findByAssignedAddress(model.id!!).map { cm ->
chainLoader.fetchChainInfo(cm.chain)
}.toList().toMutableList()
)
}.toList()
}

override suspend fun persist(assignedAddress: AssignedAddress) {
try {
runCatching {
assignedAddressRepository.save(
AssignedAddressModel(
null,
Expand All @@ -55,8 +52,6 @@ class AssignedAddressHandlerImpl(
assignedAddress.type.id
)
).awaitFirst()
} catch (e: Exception) {

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,13 @@ class CurrencyHandlerImpl(
currencyModel: CurrencyModel? = null
): CurrencyImplementation {
val addressTypesModel = chainRepository.findAddressTypesByName(currencyImplementationModel.chain)
val addressTypes = addressTypesModel.map { AddressType(it.id!!, it.type, it.addressRegex, it.memoRegex) }
val addressTypes = addressTypesModel.map { AddressType(it.id!!, it.type, it.addressRegex, it.memoRegex) }.toList()
val currencyModelVal =
currencyModel ?: currencyRepository.findBySymbol(currencyImplementationModel.currencySymbol).awaitSingle()
val currency = Currency(currencyModelVal.symbol, currencyModelVal.name)
return CurrencyImplementation(
currency,
Chain(currencyImplementationModel.chain, addressTypes.toList()),
Chain(currencyImplementationModel.chain, addressTypes),
currencyImplementationModel.token,
currencyImplementationModel.tokenAddress,
currencyImplementationModel.tokenName,
Expand Down