From 5780245243aa3f7342b86419805a4220a23d881b Mon Sep 17 00:00:00 2001 From: metalicn20 Date: Sat, 28 Aug 2021 10:17:44 +0430 Subject: [PATCH 01/37] Complete AssignAddressHandler implementation --- .../postgres/dao/AssignedAddressRepository.kt | 13 ++++++++++--- .../postgres/impl/AssignedAddressHandlerImpl.kt | 14 ++++++++++++-- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/AssignedAddressRepository.kt b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/AssignedAddressRepository.kt index 1c0c1ba2e..e6e7e147c 100644 --- a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/AssignedAddressRepository.kt +++ b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/AssignedAddressRepository.kt @@ -6,10 +6,17 @@ 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.Mono @Repository -interface AssignedAddressRepository: ReactiveCrudRepository { +interface AssignedAddressRepository : ReactiveCrudRepository { @Query("select * from assigned_addresses where uuid = :uuid and addr_type_id in (:addressTypes)") - fun findByUuidAndAddressType(@Param("uuid") uuid: String - , @Param("addressTypes") types: List): Flow + fun findByUuidAndAddressType( + @Param("uuid") uuid: String, @Param("addressTypes") types: List + ): Flow + + @Query("select * from assigned_addresses where address = :address and (:memo is null or memo = :memo)") + fun findByAddressAndMemo( + @Param("address") address: String, @Param("memo") memo: String? + ): Mono } \ No newline at end of file diff --git a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/impl/AssignedAddressHandlerImpl.kt b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/impl/AssignedAddressHandlerImpl.kt index 9c42d6695..1185b5658 100644 --- a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/impl/AssignedAddressHandlerImpl.kt +++ b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/impl/AssignedAddressHandlerImpl.kt @@ -7,9 +7,11 @@ import co.nilin.opex.bcgateway.core.spi.ChainLoader import co.nilin.opex.port.bcgateway.postgres.dao.AddressTypeRepository import co.nilin.opex.port.bcgateway.postgres.dao.AssignedAddressChainRepository import co.nilin.opex.port.bcgateway.postgres.dao.AssignedAddressRepository +import co.nilin.opex.port.bcgateway.postgres.model.AssignedAddressModel import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.toList import kotlinx.coroutines.reactive.awaitFirst +import kotlinx.coroutines.reactive.awaitFirstOrNull import org.springframework.stereotype.Service @Service @@ -42,10 +44,18 @@ class AssignedAddressHandlerImpl( } override suspend fun persist(assignedAddress: AssignedAddress) { - TODO("Not yet implemented") + assignedAddressRepository.save( + AssignedAddressModel( + null, + assignedAddress.uuid, + assignedAddress.address, + assignedAddress.memo, + assignedAddress.type.id + ) + ) } override suspend fun findUuid(address: String, memo: String?): String? { - TODO("Not yet implemented") + return assignedAddressRepository.findByAddressAndMemo(address, memo).awaitFirstOrNull()?.uuid } } \ No newline at end of file From 65ed42d8ba9ae1807f01001e2f59a46003ad11a9 Mon Sep 17 00:00:00 2001 From: metalicn20 Date: Sun, 29 Aug 2021 13:27:24 +0430 Subject: [PATCH 02/37] Rename cached address to reserved address --- .../opex/bcgateway/app/config/AppConfig.kt | 4 +-- .../opex/bcgateway/core/api/InfoService.kt | 2 +- .../opex/bcgateway/core/model/Address.kt | 2 +- .../core/service/AssignAddressServiceImpl.kt | 16 ++++----- .../bcgateway/core/service/InfoServiceImpl.kt | 2 +- .../core/spi/CachedAddressHandler.kt | 10 ------ .../core/spi/ReservedAddressHandler.kt | 9 +++++ .../AssignAddressServiceImplUnitTest.kt | 33 +++++++++---------- .../postgres/config/PostgresConfig.kt | 2 +- .../postgres/dao/CachedAddressRepository.kt | 13 -------- .../postgres/dao/ReservedAddressRepository.kt | 8 +++++ .../postgres/impl/CachedAddressHandlerImpl.kt | 15 --------- .../impl/ReservedAddressHandlerImpl.kt | 15 +++++++++ ...ddressModel.kt => ReservedAddressModel.kt} | 4 +-- 14 files changed, 64 insertions(+), 71 deletions(-) delete mode 100644 BlockchainGateway/bc-gateway-core/src/main/kotlin/co/nilin/opex/bcgateway/core/spi/CachedAddressHandler.kt create mode 100644 BlockchainGateway/bc-gateway-core/src/main/kotlin/co/nilin/opex/bcgateway/core/spi/ReservedAddressHandler.kt delete mode 100644 BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/CachedAddressRepository.kt create mode 100644 BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/ReservedAddressRepository.kt delete mode 100644 BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/impl/CachedAddressHandlerImpl.kt create mode 100644 BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/impl/ReservedAddressHandlerImpl.kt rename BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/model/{CachedAddressModel.kt => ReservedAddressModel.kt} (81%) diff --git a/BlockchainGateway/bc-gateway-app/src/main/kotlin/co/nilin/opex/bcgateway/app/config/AppConfig.kt b/BlockchainGateway/bc-gateway-app/src/main/kotlin/co/nilin/opex/bcgateway/app/config/AppConfig.kt index fc5c07cff..58cc98ade 100644 --- a/BlockchainGateway/bc-gateway-app/src/main/kotlin/co/nilin/opex/bcgateway/app/config/AppConfig.kt +++ b/BlockchainGateway/bc-gateway-app/src/main/kotlin/co/nilin/opex/bcgateway/app/config/AppConfig.kt @@ -21,9 +21,9 @@ class AppConfig { fun assignAddressService( currencyLoader: CurrencyLoader, assignedAddressHandler: AssignedAddressHandler, - cachedAddressHandler: CachedAddressHandler + reservedAddressHandler: ReservedAddressHandler ): AssignAddressService { - return AssignAddressServiceImpl(currencyLoader, assignedAddressHandler, cachedAddressHandler) + return AssignAddressServiceImpl(currencyLoader, assignedAddressHandler, reservedAddressHandler) } @Bean diff --git a/BlockchainGateway/bc-gateway-core/src/main/kotlin/co/nilin/opex/bcgateway/core/api/InfoService.kt b/BlockchainGateway/bc-gateway-core/src/main/kotlin/co/nilin/opex/bcgateway/core/api/InfoService.kt index 6286ab830..838e2a7d8 100644 --- a/BlockchainGateway/bc-gateway-core/src/main/kotlin/co/nilin/opex/bcgateway/core/api/InfoService.kt +++ b/BlockchainGateway/bc-gateway-core/src/main/kotlin/co/nilin/opex/bcgateway/core/api/InfoService.kt @@ -3,6 +3,6 @@ package co.nilin.opex.bcgateway.core.api import co.nilin.opex.bcgateway.core.model.CurrencyInfo interface InfoService { - suspend fun countCachedAddresses(): Long + suspend fun countReservedAddresses(): Long suspend fun getCurrencyInfo(): CurrencyInfo } \ No newline at end of file diff --git a/BlockchainGateway/bc-gateway-core/src/main/kotlin/co/nilin/opex/bcgateway/core/model/Address.kt b/BlockchainGateway/bc-gateway-core/src/main/kotlin/co/nilin/opex/bcgateway/core/model/Address.kt index 14572e8e9..ff7a125fe 100644 --- a/BlockchainGateway/bc-gateway-core/src/main/kotlin/co/nilin/opex/bcgateway/core/model/Address.kt +++ b/BlockchainGateway/bc-gateway-core/src/main/kotlin/co/nilin/opex/bcgateway/core/model/Address.kt @@ -1,7 +1,7 @@ package co.nilin.opex.bcgateway.core.model data class AddressType(val id: Long, val type: String, val addressRegex: String, val memoRegex: String) -data class CachedAddress(val address: String, val memo: String?, val type: AddressType) +data class ReservedAddress(val address: String, val memo: String?, val type: AddressType) data class AssignedAddress(val uuid: String, val address: String, val memo: String?, val type: AddressType, val chains: MutableList ){ override fun equals(other: Any?): Boolean { if (this === other) return true diff --git a/BlockchainGateway/bc-gateway-core/src/main/kotlin/co/nilin/opex/bcgateway/core/service/AssignAddressServiceImpl.kt b/BlockchainGateway/bc-gateway-core/src/main/kotlin/co/nilin/opex/bcgateway/core/service/AssignAddressServiceImpl.kt index 95dddaf43..ef4cb3a45 100644 --- a/BlockchainGateway/bc-gateway-core/src/main/kotlin/co/nilin/opex/bcgateway/core/service/AssignAddressServiceImpl.kt +++ b/BlockchainGateway/bc-gateway-core/src/main/kotlin/co/nilin/opex/bcgateway/core/service/AssignAddressServiceImpl.kt @@ -6,14 +6,14 @@ import co.nilin.opex.bcgateway.core.model.AssignedAddress import co.nilin.opex.bcgateway.core.model.Chain import co.nilin.opex.bcgateway.core.model.Currency import co.nilin.opex.bcgateway.core.spi.AssignedAddressHandler -import co.nilin.opex.bcgateway.core.spi.CachedAddressHandler +import co.nilin.opex.bcgateway.core.spi.ReservedAddressHandler import co.nilin.opex.bcgateway.core.spi.CurrencyLoader import java.lang.RuntimeException class AssignAddressServiceImpl( val currencyLoader: CurrencyLoader, val assignedAddressHandler: AssignedAddressHandler, - val cachedAddressHandler: CachedAddressHandler + val reservedAddressHandler: ReservedAddressHandler ) : AssignAddressService { override suspend fun assignAddress(user: String, currency: Currency): List { @@ -42,19 +42,19 @@ class AssignAddressServiceImpl( } result.add(assigned) } else { - val cachedAddress = cachedAddressHandler.peekCachedAddress(addressType) - if (cachedAddress != null) { + val reservedAddress = reservedAddressHandler.peekReservedAddress(addressType) + if (reservedAddress != null) { val newAssigned = AssignedAddress( user, - cachedAddress.address, - cachedAddress.memo, + reservedAddress.address, + reservedAddress.memo, addressType, chainAddressTypeMap.get(addressType)!! ) - cachedAddressHandler.remove(cachedAddress) + reservedAddressHandler.remove(reservedAddress) result.add(newAssigned) } else - throw RuntimeException("No cached address available for $addressType") + throw RuntimeException("No reserved address available for $addressType") } } diff --git a/BlockchainGateway/bc-gateway-core/src/main/kotlin/co/nilin/opex/bcgateway/core/service/InfoServiceImpl.kt b/BlockchainGateway/bc-gateway-core/src/main/kotlin/co/nilin/opex/bcgateway/core/service/InfoServiceImpl.kt index 5f4d53f49..e600f2369 100644 --- a/BlockchainGateway/bc-gateway-core/src/main/kotlin/co/nilin/opex/bcgateway/core/service/InfoServiceImpl.kt +++ b/BlockchainGateway/bc-gateway-core/src/main/kotlin/co/nilin/opex/bcgateway/core/service/InfoServiceImpl.kt @@ -4,7 +4,7 @@ import co.nilin.opex.bcgateway.core.api.InfoService import co.nilin.opex.bcgateway.core.model.CurrencyInfo class InfoServiceImpl: InfoService { - override suspend fun countCachedAddresses(): Long { + override suspend fun countReservedAddresses(): Long { TODO() } diff --git a/BlockchainGateway/bc-gateway-core/src/main/kotlin/co/nilin/opex/bcgateway/core/spi/CachedAddressHandler.kt b/BlockchainGateway/bc-gateway-core/src/main/kotlin/co/nilin/opex/bcgateway/core/spi/CachedAddressHandler.kt deleted file mode 100644 index c4271ed2b..000000000 --- a/BlockchainGateway/bc-gateway-core/src/main/kotlin/co/nilin/opex/bcgateway/core/spi/CachedAddressHandler.kt +++ /dev/null @@ -1,10 +0,0 @@ -package co.nilin.opex.bcgateway.core.spi - -import co.nilin.opex.bcgateway.core.model.AddressType -import co.nilin.opex.bcgateway.core.model.AssignedAddress -import co.nilin.opex.bcgateway.core.model.CachedAddress - -interface CachedAddressHandler { - suspend fun peekCachedAddress(addressType: AddressType): CachedAddress? - suspend fun remove(cacheAddress: CachedAddress) -} \ No newline at end of file diff --git a/BlockchainGateway/bc-gateway-core/src/main/kotlin/co/nilin/opex/bcgateway/core/spi/ReservedAddressHandler.kt b/BlockchainGateway/bc-gateway-core/src/main/kotlin/co/nilin/opex/bcgateway/core/spi/ReservedAddressHandler.kt new file mode 100644 index 000000000..67a3c837b --- /dev/null +++ b/BlockchainGateway/bc-gateway-core/src/main/kotlin/co/nilin/opex/bcgateway/core/spi/ReservedAddressHandler.kt @@ -0,0 +1,9 @@ +package co.nilin.opex.bcgateway.core.spi + +import co.nilin.opex.bcgateway.core.model.AddressType +import co.nilin.opex.bcgateway.core.model.ReservedAddress + +interface ReservedAddressHandler { + suspend fun peekReservedAddress(addressType: AddressType): ReservedAddress? + suspend fun remove(reservedAddress: ReservedAddress) +} \ No newline at end of file diff --git a/BlockchainGateway/bc-gateway-core/src/test/kotlin/co/nilin/opex/bcgateway/core/service/AssignAddressServiceImplUnitTest.kt b/BlockchainGateway/bc-gateway-core/src/test/kotlin/co/nilin/opex/bcgateway/core/service/AssignAddressServiceImplUnitTest.kt index bd71823f0..be773908d 100644 --- a/BlockchainGateway/bc-gateway-core/src/test/kotlin/co/nilin/opex/bcgateway/core/service/AssignAddressServiceImplUnitTest.kt +++ b/BlockchainGateway/bc-gateway-core/src/test/kotlin/co/nilin/opex/bcgateway/core/service/AssignAddressServiceImplUnitTest.kt @@ -2,13 +2,13 @@ package co.nilin.opex.bcgateway.core.service import co.nilin.opex.bcgateway.core.model.AddressType import co.nilin.opex.bcgateway.core.model.AssignedAddress -import co.nilin.opex.bcgateway.core.model.CachedAddress +import co.nilin.opex.bcgateway.core.model.ReservedAddress import co.nilin.opex.bcgateway.core.model.Chain import co.nilin.opex.bcgateway.core.model.Currency import co.nilin.opex.bcgateway.core.model.CurrencyImplementation import co.nilin.opex.bcgateway.core.model.CurrencyInfo import co.nilin.opex.bcgateway.core.spi.AssignedAddressHandler -import co.nilin.opex.bcgateway.core.spi.CachedAddressHandler +import co.nilin.opex.bcgateway.core.spi.ReservedAddressHandler import co.nilin.opex.bcgateway.core.spi.CurrencyLoader import java.lang.RuntimeException import java.math.BigDecimal @@ -16,7 +16,6 @@ import java.util.UUID import kotlinx.coroutines.runBlocking import org.junit.jupiter.api.Assertions import org.junit.jupiter.api.Test -import org.junit.jupiter.api.function.Executable import org.mockito.Mock import org.mockito.Mockito import org.mockito.MockitoAnnotations @@ -29,7 +28,7 @@ class AssignAddressServiceImplUnitTest { lateinit var assignedAddressHandler: AssignedAddressHandler @Mock - lateinit var cachedAddressHandler: CachedAddressHandler + lateinit var reservedAddressHandler: ReservedAddressHandler val assignAddressServiceImpl: AssignAddressServiceImpl @@ -43,7 +42,7 @@ class AssignAddressServiceImplUnitTest { init { MockitoAnnotations.openMocks(this) assignAddressServiceImpl = AssignAddressServiceImpl( - currencyLoader, assignedAddressHandler, cachedAddressHandler + currencyLoader, assignedAddressHandler, reservedAddressHandler ) runBlocking { val eth = @@ -67,17 +66,17 @@ class AssignAddressServiceImplUnitTest { } @Test - fun givenCachedAddressAndUserWithNoAssignedAddress_whenAssignAddress_thenCachedAddressAssigned() { + fun givenReservedAddressAndUserWithNoAssignedAddress_whenAssignAddress_thenReservedAddressAssigned() { runBlocking { val user = UUID.randomUUID().toString() Mockito.`when`(assignedAddressHandler.fetchAssignedAddresses(user, listOf(ethAddressType, ethMemoAddressType))).thenReturn( emptyList() ) - Mockito.`when`(cachedAddressHandler.peekCachedAddress(ethAddressType)).thenReturn( - CachedAddress("0x1", null, ethAddressType) + Mockito.`when`(reservedAddressHandler.peekReservedAddress(ethAddressType)).thenReturn( + ReservedAddress("0x1", null, ethAddressType) ) - Mockito.`when`(cachedAddressHandler.peekCachedAddress(ethMemoAddressType)).thenReturn( - CachedAddress("0x2", "Memo", ethMemoAddressType) + Mockito.`when`(reservedAddressHandler.peekReservedAddress(ethMemoAddressType)).thenReturn( + ReservedAddress("0x2", "Memo", ethMemoAddressType) ) val assignedAddress = assignAddressServiceImpl.assignAddress(user, currency) Assertions.assertEquals( @@ -102,13 +101,13 @@ class AssignAddressServiceImplUnitTest { } @Test - fun givenNoCachedAddressAndUserWithNoAssignedAddress_whenAssignAddress_thenExcpetion() { + fun givenNoReservedAddressAndUserWithNoAssignedAddress_whenAssignAddress_thenExcpetion() { runBlocking { val user = UUID.randomUUID().toString() Mockito.`when`(assignedAddressHandler.fetchAssignedAddresses(user, listOf(ethAddressType, ethMemoAddressType))).thenReturn( emptyList() ) - Mockito.`when`(cachedAddressHandler.peekCachedAddress(ethAddressType)).thenReturn(null) + Mockito.`when`(reservedAddressHandler.peekReservedAddress(ethAddressType)).thenReturn(null) Assertions.assertThrows(RuntimeException::class.java) { runBlocking { @@ -119,7 +118,7 @@ class AssignAddressServiceImplUnitTest { } @Test - fun givenCachedAddressAndUserOneAssignedAddress_whenAssignAddress_thenCachedAddressAssigned() { + fun givenReservedAddressAndUserOneAssignedAddress_whenAssignAddress_thenReservedAddressAssigned() { runBlocking { val user = UUID.randomUUID().toString() Mockito.`when`(assignedAddressHandler.fetchAssignedAddresses(user, listOf(ethAddressType, ethMemoAddressType))).thenReturn( @@ -132,11 +131,11 @@ class AssignAddressServiceImplUnitTest { ) ) ) - Mockito.`when`(cachedAddressHandler.peekCachedAddress(ethAddressType)).thenReturn( - CachedAddress("0x1", null, ethAddressType) + Mockito.`when`(reservedAddressHandler.peekReservedAddress(ethAddressType)).thenReturn( + ReservedAddress("0x1", null, ethAddressType) ) - Mockito.`when`(cachedAddressHandler.peekCachedAddress(ethMemoAddressType)).thenReturn( - CachedAddress("0x2", "Memo", ethMemoAddressType) + Mockito.`when`(reservedAddressHandler.peekReservedAddress(ethMemoAddressType)).thenReturn( + ReservedAddress("0x2", "Memo", ethMemoAddressType) ) val assignedAddress = assignAddressServiceImpl.assignAddress(user, currency) Assertions.assertEquals( diff --git a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/config/PostgresConfig.kt b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/config/PostgresConfig.kt index 07ca7afa5..8a17e9123 100644 --- a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/config/PostgresConfig.kt +++ b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/config/PostgresConfig.kt @@ -30,7 +30,7 @@ class PostgresConfig(db: DatabaseClient) { assigned_address_id numeric, chain VARCHAR(72) ); - CREATE TABLE IF NOT EXISTS cached_addresses ( + CREATE TABLE IF NOT EXISTS reserved_addresses ( id SERIAL PRIMARY KEY, address VARCHAR(72), memo VARCHAR(72), diff --git a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/CachedAddressRepository.kt b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/CachedAddressRepository.kt deleted file mode 100644 index f3a30bdeb..000000000 --- a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/CachedAddressRepository.kt +++ /dev/null @@ -1,13 +0,0 @@ -package co.nilin.opex.port.bcgateway.postgres.dao - -import co.nilin.opex.port.bcgateway.postgres.model.AssignedAddressChainModel -import co.nilin.opex.port.bcgateway.postgres.model.CachedAddressModel -import co.nilin.opex.port.bcgateway.postgres.model.ChainModel -import kotlinx.coroutines.flow.Flow -import org.springframework.data.r2dbc.repository.Query -import org.springframework.data.repository.query.Param -import org.springframework.data.repository.reactive.ReactiveCrudRepository - -interface CachedAddressRepository : ReactiveCrudRepository { - -} \ No newline at end of file diff --git a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/ReservedAddressRepository.kt b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/ReservedAddressRepository.kt new file mode 100644 index 000000000..7234f5dd8 --- /dev/null +++ b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/ReservedAddressRepository.kt @@ -0,0 +1,8 @@ +package co.nilin.opex.port.bcgateway.postgres.dao + +import co.nilin.opex.port.bcgateway.postgres.model.ReservedAddressModel +import org.springframework.data.repository.reactive.ReactiveCrudRepository + +interface ReservedAddressRepository : ReactiveCrudRepository { + +} \ No newline at end of file diff --git a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/impl/CachedAddressHandlerImpl.kt b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/impl/CachedAddressHandlerImpl.kt deleted file mode 100644 index 52e868cfe..000000000 --- a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/impl/CachedAddressHandlerImpl.kt +++ /dev/null @@ -1,15 +0,0 @@ -package co.nilin.opex.port.bcgateway.postgres.impl - -import co.nilin.opex.bcgateway.core.model.AddressType -import co.nilin.opex.bcgateway.core.model.CachedAddress -import co.nilin.opex.bcgateway.core.spi.CachedAddressHandler - -class CachedAddressHandlerImpl: CachedAddressHandler { - override suspend fun peekCachedAddress(addressType: AddressType): CachedAddress? { - TODO("Not yet implemented") - } - - override suspend fun remove(cacheAddress: CachedAddress) { - TODO("Not yet implemented") - } -} \ No newline at end of file diff --git a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/impl/ReservedAddressHandlerImpl.kt b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/impl/ReservedAddressHandlerImpl.kt new file mode 100644 index 000000000..f799462d1 --- /dev/null +++ b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/impl/ReservedAddressHandlerImpl.kt @@ -0,0 +1,15 @@ +package co.nilin.opex.port.bcgateway.postgres.impl + +import co.nilin.opex.bcgateway.core.model.AddressType +import co.nilin.opex.bcgateway.core.model.ReservedAddress +import co.nilin.opex.bcgateway.core.spi.ReservedAddressHandler + +class ReservedAddressHandlerImpl: ReservedAddressHandler { + override suspend fun peekReservedAddress(addressType: AddressType): ReservedAddress? { + TODO("Not yet implemented") + } + + override suspend fun remove(reservedAddress: ReservedAddress) { + TODO("Not yet implemented") + } +} \ No newline at end of file diff --git a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/model/CachedAddressModel.kt b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/model/ReservedAddressModel.kt similarity index 81% rename from BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/model/CachedAddressModel.kt rename to BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/model/ReservedAddressModel.kt index fbb698b1b..fb2d37358 100644 --- a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/model/CachedAddressModel.kt +++ b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/model/ReservedAddressModel.kt @@ -3,7 +3,7 @@ package co.nilin.opex.port.bcgateway.postgres.model import org.springframework.data.relational.core.mapping.Column import org.springframework.data.relational.core.mapping.Table -@Table("cached_addresses") -data class CachedAddressModel( +@Table("reserved_addresses") +data class ReservedAddressModel( val id: Long?, val address: String, val memo: String?, @Column("address_type") val type: Long ) \ No newline at end of file From 0d9ab130fc5e5204e3964cb94f52bb896854a030 Mon Sep 17 00:00:00 2001 From: metalicn20 Date: Sun, 29 Aug 2021 13:28:31 +0430 Subject: [PATCH 03/37] Add unique constraint to reserved_addresses table --- .../opex/port/bcgateway/postgres/config/PostgresConfig.kt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/config/PostgresConfig.kt b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/config/PostgresConfig.kt index 8a17e9123..9328d6869 100644 --- a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/config/PostgresConfig.kt +++ b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/config/PostgresConfig.kt @@ -34,7 +34,8 @@ class PostgresConfig(db: DatabaseClient) { id SERIAL PRIMARY KEY, address VARCHAR(72), memo VARCHAR(72), - address_type VARCHAR(72) + address_type VARCHAR(72), + UNIQUE (address, memo) ); CREATE TABLE IF NOT EXISTS chain ( name VARCHAR(72) PRIMARY KEY From 0d454fb272e9a2147bbda832753fef2a524bb0e9 Mon Sep 17 00:00:00 2001 From: metalicn20 Date: Sun, 29 Aug 2021 13:36:13 +0430 Subject: [PATCH 04/37] Fix unique constraint in assigned addresses --- .../opex/port/bcgateway/postgres/config/PostgresConfig.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/config/PostgresConfig.kt b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/config/PostgresConfig.kt index 9328d6869..492c978e4 100644 --- a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/config/PostgresConfig.kt +++ b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/config/PostgresConfig.kt @@ -19,11 +19,11 @@ class PostgresConfig(db: DatabaseClient) { ); CREATE TABLE IF NOT EXISTS assigned_addresses ( id SERIAL PRIMARY KEY, - uuid VARCHAR(72) UNIQUE, + uuid VARCHAR(72), address VARCHAR(72), memo VARCHAR(72), addr_type_id numeric, - UNIQUE (address, memo) + UNIQUE (uuid, address, memo) ); CREATE TABLE IF NOT EXISTS assigned_address_chains ( id SERIAL PRIMARY KEY, From ff24577fbec96270c6da6503bba51e88b4bd7c5c Mon Sep 17 00:00:00 2001 From: metalicn20 Date: Sun, 29 Aug 2021 13:49:39 +0430 Subject: [PATCH 05/37] Improve sql initiate script --- .../postgres/config/PostgresConfig.kt | 38 +++++++++---------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/config/PostgresConfig.kt b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/config/PostgresConfig.kt index 492c978e4..51d227fe4 100644 --- a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/config/PostgresConfig.kt +++ b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/config/PostgresConfig.kt @@ -13,28 +13,28 @@ class PostgresConfig(db: DatabaseClient) { """ CREATE TABLE IF NOT EXISTS address_types ( id SERIAL PRIMARY KEY, - address_type VARCHAR(72), - address_regex VARCHAR(72), - memo_regex VARCHAR(72) + address_type VARCHAR(72) NOT NULL, + address_regex VARCHAR(72) NOT NULL, + memo_regex VARCHAR(72) NOT NULL ); CREATE TABLE IF NOT EXISTS assigned_addresses ( id SERIAL PRIMARY KEY, - uuid VARCHAR(72), - address VARCHAR(72), + uuid VARCHAR(72) NOT NULL, + address VARCHAR(72) NOT NULL, memo VARCHAR(72), - addr_type_id numeric, + addr_type_id NUMERIC NOT NULL, UNIQUE (uuid, address, memo) ); CREATE TABLE IF NOT EXISTS assigned_address_chains ( id SERIAL PRIMARY KEY, - assigned_address_id numeric, - chain VARCHAR(72) + assigned_address_id NUMERIC NOT NULL, + chain VARCHAR(72) NOT NULL ); CREATE TABLE IF NOT EXISTS reserved_addresses ( id SERIAL PRIMARY KEY, - address VARCHAR(72), + address VARCHAR(72) NOT NULL, memo VARCHAR(72), - address_type VARCHAR(72), + address_type VARCHAR(72) NOT NULL, UNIQUE (address, memo) ); CREATE TABLE IF NOT EXISTS chain ( @@ -42,27 +42,27 @@ class PostgresConfig(db: DatabaseClient) { ); CREATE TABLE IF NOT EXISTS chain_address_types ( id SERIAL PRIMARY KEY, - chain_name VARCHAR(72), - addr_type_id numeric + chain_name VARCHAR(72) NOT NULL, + addr_type_id NUMERIC NOT NULL ); CREATE TABLE IF NOT EXISTS chain_endpoints ( id SERIAL PRIMARY KEY, - chain_name VARCHAR(72), - endpoint_url VARCHAR(72), + chain_name VARCHAR(72) NOT NULL, + endpoint_url VARCHAR(72) NOT NULL, endpoint_user VARCHAR(72), endpoint_password VARCHAR(72) ); CREATE TABLE IF NOT EXISTS chain_sync_schedule ( chain VARCHAR(72) PRIMARY KEY, retry_time TIMESTAMP, - delay numeric + delay NUMERIC ); CREATE TABLE IF NOT EXISTS chain_sync_record ( chain VARCHAR(72) PRIMARY KEY, - time TIMESTAMP, - endpoint_url VARCHAR(72), - latest_block numeric, - success BOOLEAN, + time TIMESTAMP NOT NULL, + endpoint_url VARCHAR(72) NOT NULL, + latest_block NUMERIC NOT NULL, + success BOOLEAN NOT NULL, error VARCHAR(100) ); """ From b5002b4a1a6627fd648079e3e9fb34d604b07201 Mon Sep 17 00:00:00 2001 From: metalicn20 Date: Sun, 29 Aug 2021 13:50:33 +0430 Subject: [PATCH 06/37] Fix persist method --- .../port/bcgateway/postgres/impl/AssignedAddressHandlerImpl.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/impl/AssignedAddressHandlerImpl.kt b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/impl/AssignedAddressHandlerImpl.kt index 1185b5658..1e7695999 100644 --- a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/impl/AssignedAddressHandlerImpl.kt +++ b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/impl/AssignedAddressHandlerImpl.kt @@ -52,7 +52,7 @@ class AssignedAddressHandlerImpl( assignedAddress.memo, assignedAddress.type.id ) - ) + ).awaitFirst() } override suspend fun findUuid(address: String, memo: String?): String? { From d3fc81ee3c632167f13d2f7b99eba07f20a53078 Mon Sep 17 00:00:00 2001 From: metalicn20 Date: Sun, 29 Aug 2021 13:53:42 +0430 Subject: [PATCH 07/37] Fix uuid unique constraint in assigned addresses --- .../nilin/opex/port/bcgateway/postgres/config/PostgresConfig.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/config/PostgresConfig.kt b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/config/PostgresConfig.kt index 51d227fe4..f18b97dcd 100644 --- a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/config/PostgresConfig.kt +++ b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/config/PostgresConfig.kt @@ -23,7 +23,7 @@ class PostgresConfig(db: DatabaseClient) { address VARCHAR(72) NOT NULL, memo VARCHAR(72), addr_type_id NUMERIC NOT NULL, - UNIQUE (uuid, address, memo) + UNIQUE (address, memo), ); CREATE TABLE IF NOT EXISTS assigned_address_chains ( id SERIAL PRIMARY KEY, From 4aa2a6062d861374170175066489f18d505eb976 Mon Sep 17 00:00:00 2001 From: metalicn20 Date: Sun, 29 Aug 2021 18:17:25 +0430 Subject: [PATCH 08/37] Add currency models --- .../model/CurrencyImplementationModel.kt | 19 +++++++++++++++++++ .../bcgateway/postgres/model/CurrencyModel.kt | 13 +++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/model/CurrencyImplementationModel.kt create mode 100644 BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/model/CurrencyModel.kt diff --git a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/model/CurrencyImplementationModel.kt b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/model/CurrencyImplementationModel.kt new file mode 100644 index 000000000..4756528fa --- /dev/null +++ b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/model/CurrencyImplementationModel.kt @@ -0,0 +1,19 @@ +package co.nilin.opex.port.bcgateway.postgres.model + + +import org.springframework.data.annotation.Id +import org.springframework.data.relational.core.mapping.Column +import org.springframework.data.relational.core.mapping.Table + +@Table("currency_implementations") +class CurrencyImplementationModel( + @Id val id: Long?, + @Column("symbol") val symbol: String, + @Column("chain") val chain: String, + @Column("token") val token: Boolean, + @Column("token_address") val tokenAddress: String?, + @Column("token_name") val tokenName: String?, + @Column("withdraw_enabled") val withdrawEnabled: Boolean, + @Column("withdraw_fee") val withdrawFee: Long, + @Column("withdraw_min") val withdrawMin: Long, +) diff --git a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/model/CurrencyModel.kt b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/model/CurrencyModel.kt new file mode 100644 index 000000000..ec67e4715 --- /dev/null +++ b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/model/CurrencyModel.kt @@ -0,0 +1,13 @@ +package co.nilin.opex.port.bcgateway.postgres.model + + +import org.springframework.data.annotation.Id +import org.springframework.data.relational.core.mapping.Column +import org.springframework.data.relational.core.mapping.Table + +@Table("currency") +class CurrencyModel( + @Id val id: Long?, + @Column("symbol") val symbol: String, + @Column("name") val name: String +) From 281ce00ce6fa67792b75ca799d4775ca5f5240df Mon Sep 17 00:00:00 2001 From: metalicn20 Date: Sun, 29 Aug 2021 18:24:27 +0430 Subject: [PATCH 09/37] Add currency repositories --- .../dao/CurrencyImplementationRepository.kt | 22 +++++++++++++++++++ .../postgres/dao/CurrencyRepository.kt | 18 +++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/CurrencyImplementationRepository.kt create mode 100644 BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/CurrencyRepository.kt diff --git a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/CurrencyImplementationRepository.kt b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/CurrencyImplementationRepository.kt new file mode 100644 index 000000000..ccb5d8a78 --- /dev/null +++ b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/CurrencyImplementationRepository.kt @@ -0,0 +1,22 @@ +package co.nilin.opex.port.bcgateway.postgres.dao + +import co.nilin.opex.port.bcgateway.postgres.model.CurrencyImplementationModel +import co.nilin.opex.port.bcgateway.postgres.model.CurrencyModel +import kotlinx.coroutines.flow.Flow +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.Mono + +@Repository +interface CurrencyImplementationRepository : ReactiveCrudRepository { + @Query("select * from currency_implementations where chain = :chain and (:address is null or token_address = :address)") + fun findByChainAndAddress( + @Param("chain") chain: String, + @Param("address") address: String? + ): Mono + + @Query("select * from currency_implementations where chain = :chain") + fun findByChain(@Param("chain") chain: String): Flow +} diff --git a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/CurrencyRepository.kt b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/CurrencyRepository.kt new file mode 100644 index 000000000..d716bbd9c --- /dev/null +++ b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/CurrencyRepository.kt @@ -0,0 +1,18 @@ +package co.nilin.opex.port.bcgateway.postgres.dao + +import co.nilin.opex.port.bcgateway.postgres.model.CurrencyModel +import kotlinx.coroutines.flow.Flow +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.Mono + +@Repository +interface CurrencyRepository : ReactiveCrudRepository { + @Query("select * from currency where symbol = :symbol") + fun findBySymbol(@Param("symbol") symbol: String): Mono + + @Query("select * from currency where name = :name") + fun findByName(@Param("name") name: String): Mono +} From 9008781cc6dd446c1c6fd2f89cb261932f05a196 Mon Sep 17 00:00:00 2001 From: metalicn20 Date: Mon, 30 Aug 2021 10:01:24 +0430 Subject: [PATCH 10/37] Fix currency implementation --- .../co/nilin/opex/bcgateway/core/spi/CurrencyLoader.kt | 2 +- .../bcgateway/postgres/model/CurrencyImplementationModel.kt | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/BlockchainGateway/bc-gateway-core/src/main/kotlin/co/nilin/opex/bcgateway/core/spi/CurrencyLoader.kt b/BlockchainGateway/bc-gateway-core/src/main/kotlin/co/nilin/opex/bcgateway/core/spi/CurrencyLoader.kt index 2b105b14f..32aa395f1 100644 --- a/BlockchainGateway/bc-gateway-core/src/main/kotlin/co/nilin/opex/bcgateway/core/spi/CurrencyLoader.kt +++ b/BlockchainGateway/bc-gateway-core/src/main/kotlin/co/nilin/opex/bcgateway/core/spi/CurrencyLoader.kt @@ -5,6 +5,6 @@ import co.nilin.opex.bcgateway.core.model.CurrencyInfo interface CurrencyLoader { suspend fun fetchCurrencyInfo(symbol: String): CurrencyInfo - suspend fun findSymbol(chain: String, address: String?): String + suspend fun findSymbol(chain: String, address: String?): String? suspend fun findImplementationsWithTokenOnChain(chain: String): List } \ No newline at end of file diff --git a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/model/CurrencyImplementationModel.kt b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/model/CurrencyImplementationModel.kt index 4756528fa..eeaaa6f59 100644 --- a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/model/CurrencyImplementationModel.kt +++ b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/model/CurrencyImplementationModel.kt @@ -4,6 +4,7 @@ package co.nilin.opex.port.bcgateway.postgres.model import org.springframework.data.annotation.Id import org.springframework.data.relational.core.mapping.Column import org.springframework.data.relational.core.mapping.Table +import java.math.BigDecimal @Table("currency_implementations") class CurrencyImplementationModel( @@ -14,6 +15,6 @@ class CurrencyImplementationModel( @Column("token_address") val tokenAddress: String?, @Column("token_name") val tokenName: String?, @Column("withdraw_enabled") val withdrawEnabled: Boolean, - @Column("withdraw_fee") val withdrawFee: Long, - @Column("withdraw_min") val withdrawMin: Long, + @Column("withdraw_fee") val withdrawFee: BigDecimal, + @Column("withdraw_min") val withdrawMin: BigDecimal, ) From 330c529a29de8cb5d414b713a0e0953a1178391d Mon Sep 17 00:00:00 2001 From: metalicn20 Date: Tue, 31 Aug 2021 12:18:56 +0430 Subject: [PATCH 11/37] Implement currency loader --- .../dao/CurrencyImplementationRepository.kt | 21 ++++---- .../postgres/dao/CurrencyRepository.kt | 5 +- .../postgres/impl/CurrencyLoaderImpl.kt | 52 ++++++++++++++++--- 3 files changed, 58 insertions(+), 20 deletions(-) diff --git a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/CurrencyImplementationRepository.kt b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/CurrencyImplementationRepository.kt index ccb5d8a78..06a30eb6b 100644 --- a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/CurrencyImplementationRepository.kt +++ b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/CurrencyImplementationRepository.kt @@ -1,22 +1,23 @@ package co.nilin.opex.port.bcgateway.postgres.dao import co.nilin.opex.port.bcgateway.postgres.model.CurrencyImplementationModel -import co.nilin.opex.port.bcgateway.postgres.model.CurrencyModel import kotlinx.coroutines.flow.Flow -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.Mono @Repository -interface CurrencyImplementationRepository : ReactiveCrudRepository { - @Query("select * from currency_implementations where chain = :chain and (:address is null or token_address = :address)") +interface CurrencyImplementationRepository : ReactiveCrudRepository { + fun findBySymbol( + symbol: String + ): Flow + + fun findByChain( + chain: String + ): Flow + fun findByChainAndAddress( - @Param("chain") chain: String, - @Param("address") address: String? + chain: String, + address: String? ): Mono - - @Query("select * from currency_implementations where chain = :chain") - fun findByChain(@Param("chain") chain: String): Flow } diff --git a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/CurrencyRepository.kt b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/CurrencyRepository.kt index d716bbd9c..9dc8e114c 100644 --- a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/CurrencyRepository.kt +++ b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/CurrencyRepository.kt @@ -10,9 +10,6 @@ import reactor.core.publisher.Mono @Repository interface CurrencyRepository : ReactiveCrudRepository { - @Query("select * from currency where symbol = :symbol") - fun findBySymbol(@Param("symbol") symbol: String): Mono - @Query("select * from currency where name = :name") - fun findByName(@Param("name") name: String): Mono + fun findBySymbol(symbol: String): Mono } diff --git a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/impl/CurrencyLoaderImpl.kt b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/impl/CurrencyLoaderImpl.kt index 0d519f77b..f4a000214 100644 --- a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/impl/CurrencyLoaderImpl.kt +++ b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/impl/CurrencyLoaderImpl.kt @@ -1,19 +1,59 @@ package co.nilin.opex.port.bcgateway.postgres.impl +import co.nilin.opex.bcgateway.core.model.Chain +import co.nilin.opex.bcgateway.core.model.Currency import co.nilin.opex.bcgateway.core.model.CurrencyImplementation import co.nilin.opex.bcgateway.core.model.CurrencyInfo import co.nilin.opex.bcgateway.core.spi.CurrencyLoader +import co.nilin.opex.port.bcgateway.postgres.dao.CurrencyImplementationRepository +import co.nilin.opex.port.bcgateway.postgres.dao.CurrencyRepository +import kotlinx.coroutines.flow.map +import kotlinx.coroutines.flow.toList +import kotlinx.coroutines.reactive.awaitFirstOrNull +import kotlinx.coroutines.reactive.awaitSingle +import kotlinx.coroutines.reactive.awaitSingleOrNull -class CurrencyLoaderImpl: CurrencyLoader { +class CurrencyLoaderImpl( + private val currencyRepository: CurrencyRepository, + private val currencyImplementationRepository: CurrencyImplementationRepository +) : CurrencyLoader { override suspend fun fetchCurrencyInfo(symbol: String): CurrencyInfo { - TODO("Not yet implemented") + val currencyDao = currencyRepository.findBySymbol(symbol).awaitSingleOrNull() + val currencyImplDao = currencyImplementationRepository.findBySymbol(symbol) + val currency = Currency(currencyDao.symbol, currencyDao.name) + return CurrencyInfo(currency, currencyImplDao.map { + CurrencyImplementation( + currency, + Chain(it.chain, emptyList(), emptyList()), + it.token, + it.tokenAddress, + it.tokenName, + it.withdrawEnabled, + it.withdrawFee, + it.withdrawMin + ) + }.toList()) } - override suspend fun findSymbol(chain: String, address: String?): String { - TODO("Not yet implemented") + override suspend fun findSymbol(chain: String, address: String?): String? { + return currencyImplementationRepository.findByChainAndAddress(chain, address) + .awaitFirstOrNull()?.symbol } override suspend fun findImplementationsWithTokenOnChain(chain: String): List { - TODO("Not yet implemented") + return currencyImplementationRepository.findByChain(chain).map { + val currencyDao = currencyRepository.findBySymbol(it.symbol).awaitSingleOrNull() + val currency = Currency(currencyDao.symbol, currencyDao.name) + CurrencyImplementation( + currency, + Chain(it.chain, emptyList(), emptyList()), + it.token, + it.tokenAddress, + it.tokenName, + it.withdrawEnabled, + it.withdrawFee, + it.withdrawMin + ) + }.toList() } -} \ No newline at end of file +} From e4012442751374bd03d801956e65b70b205a378c Mon Sep 17 00:00:00 2001 From: metalicn20 Date: Tue, 31 Aug 2021 12:20:11 +0430 Subject: [PATCH 12/37] Change currency table primary key to symbol --- .../opex/port/bcgateway/postgres/dao/CurrencyRepository.kt | 2 +- .../nilin/opex/port/bcgateway/postgres/model/CurrencyModel.kt | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/CurrencyRepository.kt b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/CurrencyRepository.kt index 9dc8e114c..aa7aecfee 100644 --- a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/CurrencyRepository.kt +++ b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/CurrencyRepository.kt @@ -9,7 +9,7 @@ import org.springframework.stereotype.Repository import reactor.core.publisher.Mono @Repository -interface CurrencyRepository : ReactiveCrudRepository { +interface CurrencyRepository : ReactiveCrudRepository { fun findBySymbol(symbol: String): Mono } diff --git a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/model/CurrencyModel.kt b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/model/CurrencyModel.kt index ec67e4715..354398e25 100644 --- a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/model/CurrencyModel.kt +++ b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/model/CurrencyModel.kt @@ -7,7 +7,6 @@ import org.springframework.data.relational.core.mapping.Table @Table("currency") class CurrencyModel( - @Id val id: Long?, - @Column("symbol") val symbol: String, + @Id @Column("symbol") val symbol: String, @Column("name") val name: String ) From e43219f46c950bad8e4e5d061c26712416cd8a00 Mon Sep 17 00:00:00 2001 From: metalicn20 Date: Tue, 31 Aug 2021 15:19:37 +0430 Subject: [PATCH 13/37] Add currency tables --- .../bcgateway/postgres/config/PostgresConfig.kt | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/config/PostgresConfig.kt b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/config/PostgresConfig.kt index f18b97dcd..593d6e03d 100644 --- a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/config/PostgresConfig.kt +++ b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/config/PostgresConfig.kt @@ -65,6 +65,20 @@ class PostgresConfig(db: DatabaseClient) { success BOOLEAN NOT NULL, error VARCHAR(100) ); + CREATE TABLE IF NOT EXISTS currency ( + symbol VARCHAR(72) PRIMARY KEY, + name VARCHAR(72) NOT NULL + ); + CREATE TABLE IF NOT EXISTS currency_implementations ( + symbol VARCHAR(72) PRIMARY KEY, + chain VARCHAR(72) NOT NULL, + token BOOLEAN NOT NULL, + token_address VARCHAR(72), + token_name VARCHAR(72), + withdraw_enabled BOOLEAN NOT NULL, + withdraw_fee NUMERIC NOT NULL, + withdraw_min NUMERIC NOT NULL, + ); """ } initDb // initialize the database From 13bd57b5524b8ddabe3c9e702fd09bb1cdc83934 Mon Sep 17 00:00:00 2001 From: metalicn20 Date: Tue, 31 Aug 2021 15:20:17 +0430 Subject: [PATCH 14/37] Format Chain.kt --- .../co/nilin/opex/bcgateway/core/model/Chain.kt | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/BlockchainGateway/bc-gateway-core/src/main/kotlin/co/nilin/opex/bcgateway/core/model/Chain.kt b/BlockchainGateway/bc-gateway-core/src/main/kotlin/co/nilin/opex/bcgateway/core/model/Chain.kt index 73875ced3..c591687f8 100644 --- a/BlockchainGateway/bc-gateway-core/src/main/kotlin/co/nilin/opex/bcgateway/core/model/Chain.kt +++ b/BlockchainGateway/bc-gateway-core/src/main/kotlin/co/nilin/opex/bcgateway/core/model/Chain.kt @@ -6,12 +6,13 @@ import java.time.LocalDateTime data class Endpoint(val url: String) data class Chain(val name: String, val addressTypes: List, val endpoints: List) data class ChainSyncSchedule(val chainName: String, val retryTime: LocalDateTime, val delay: Long) -data class ChainSyncRecord(val chainName: String - , val time: LocalDateTime - , val endpoint: Endpoint - , val latestBlock: Long? - , val success: Boolean - , val error: String? - , val records: List +data class ChainSyncRecord( + val chainName: String, + val time: LocalDateTime, + val endpoint: Endpoint, + val latestBlock: Long?, + val success: Boolean, + val error: String?, + val records: List ) From dd5dbe67be9bb7309036a4212c5d25a01fdd1780 Mon Sep 17 00:00:00 2001 From: metalicn20 Date: Tue, 31 Aug 2021 15:27:43 +0430 Subject: [PATCH 15/37] Fix symbol nullability in WalletSyncServiceImpl.kt --- .../opex/bcgateway/core/service/WalletSyncServiceImpl.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/BlockchainGateway/bc-gateway-core/src/main/kotlin/co/nilin/opex/bcgateway/core/service/WalletSyncServiceImpl.kt b/BlockchainGateway/bc-gateway-core/src/main/kotlin/co/nilin/opex/bcgateway/core/service/WalletSyncServiceImpl.kt index 97d80774b..77228ea44 100644 --- a/BlockchainGateway/bc-gateway-core/src/main/kotlin/co/nilin/opex/bcgateway/core/service/WalletSyncServiceImpl.kt +++ b/BlockchainGateway/bc-gateway-core/src/main/kotlin/co/nilin/opex/bcgateway/core/service/WalletSyncServiceImpl.kt @@ -26,9 +26,9 @@ class WalletSyncServiceImpl( deposits.map { deposit -> async(dispatcher) { val uuid = assignedAddressHandler.findUuid(deposit.depositor, deposit.depositorMemo) - if ( uuid != null ) { + if (uuid != null) { val symbol = currencyLoader.findSymbol(deposit.chain!!, deposit.tokenAddress) - walletProxy.transfer(uuid, symbol, deposit.amount) + if (symbol != null) walletProxy.transfer(uuid, symbol, deposit.amount) } } } From f0c4ee58b17532fa218ad141c560019acbde3f2f Mon Sep 17 00:00:00 2001 From: metalicn20 Date: Tue, 31 Aug 2021 17:37:35 +0430 Subject: [PATCH 16/37] Add blockchain module to docker-compose --- Deployment/docker-compose.yml | 41 +++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/Deployment/docker-compose.yml b/Deployment/docker-compose.yml index 0dda34600..73d17005d 100644 --- a/Deployment/docker-compose.yml +++ b/Deployment/docker-compose.yml @@ -131,6 +131,21 @@ services: deploy: restart_policy: condition: on-failure + postgres-bc-gateway: + image: "postgres" + ports: + - 127.0.0.1:5438:5432 + environment: + - POSTGRES_USER=opex + - POSTGRES_PASSWORD=hiopex + - POSTGRES_DB=opex_api + volumes: + - $PWD/runtime/bc-gateway-data:/var/lib/postgresql/data/ + networks: + - opex + deploy: + restart_policy: + condition: on-failure accountant: container_name: accountant build: @@ -310,6 +325,32 @@ services: - api networks: - opex + bc-gateway: + container_name: bc-gateway + build: + context: ../BlockchainGateway/bc-gateway-app + dockerfile: Dockerfile + ports: + - 127.0.0.1:8094:8094 + - 127.0.0.1:1050:1044 + environment: + - JAVA_OPTS=-Xmx256m -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=1044 + - SPRING_PROFILES_ACTIVE=docker + - KAFKA_IP_PORT=kafka:9092 + - REDIS_HOST=redis + - CONSUL_HOST=consul + - DB_IP_PORT=postgres-api + networks: + - opex + depends_on: + - zookeeper + - kafka + - redis + - consul + - postgres-bc-gateway + deploy: + restart_policy: + condition: on-failure networks: opex: driver: bridge \ No newline at end of file From 857e9388d84e0a030a26412436e3a8aba321864e Mon Sep 17 00:00:00 2001 From: metalicn20 Date: Tue, 31 Aug 2021 17:40:47 +0430 Subject: [PATCH 17/37] Fix blockchain module port configs --- Deployment/docker-compose.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Deployment/docker-compose.yml b/Deployment/docker-compose.yml index 73d17005d..50d396556 100644 --- a/Deployment/docker-compose.yml +++ b/Deployment/docker-compose.yml @@ -331,8 +331,8 @@ services: context: ../BlockchainGateway/bc-gateway-app dockerfile: Dockerfile ports: - - 127.0.0.1:8094:8094 - - 127.0.0.1:1050:1044 + - 127.0.0.1:8095:8095 + - 127.0.0.1:1051:1044 environment: - JAVA_OPTS=-Xmx256m -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=1044 - SPRING_PROFILES_ACTIVE=docker From 9918d4f2ecde67574e2facdde918d6442e870188 Mon Sep 17 00:00:00 2001 From: metalicn20 Date: Tue, 31 Aug 2021 18:12:46 +0430 Subject: [PATCH 18/37] Fix bc-gateway docker config --- Deployment/docker-compose.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Deployment/docker-compose.yml b/Deployment/docker-compose.yml index 50d396556..25ea31d75 100644 --- a/Deployment/docker-compose.yml +++ b/Deployment/docker-compose.yml @@ -339,7 +339,7 @@ services: - KAFKA_IP_PORT=kafka:9092 - REDIS_HOST=redis - CONSUL_HOST=consul - - DB_IP_PORT=postgres-api + - DB_IP_PORT=postgres-bc-gateway networks: - opex depends_on: From b897369057701380c50554a7d0481b03116da6bd Mon Sep 17 00:00:00 2001 From: metalicn20 Date: Tue, 31 Aug 2021 19:11:48 +0430 Subject: [PATCH 19/37] Fix bc-gateway database name in docker config --- Deployment/docker-compose.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Deployment/docker-compose.yml b/Deployment/docker-compose.yml index 25ea31d75..0636b05b5 100644 --- a/Deployment/docker-compose.yml +++ b/Deployment/docker-compose.yml @@ -138,7 +138,7 @@ services: environment: - POSTGRES_USER=opex - POSTGRES_PASSWORD=hiopex - - POSTGRES_DB=opex_api + - POSTGRES_DB=opex_bc_gateway volumes: - $PWD/runtime/bc-gateway-data:/var/lib/postgresql/data/ networks: From f0e8cd432e8d2f469a48f9e1b4ca6c4b52ba79b2 Mon Sep 17 00:00:00 2001 From: metalicn20 Date: Tue, 31 Aug 2021 19:12:53 +0430 Subject: [PATCH 20/37] Add missing @Repository attributes --- .../bcgateway/postgres/dao/ChainAddressTypeRepository.kt | 6 +++--- .../bcgateway/postgres/dao/ChainEndpointRepository.kt | 6 +++--- .../opex/port/bcgateway/postgres/dao/ChainRepository.kt | 8 +++++--- .../bcgateway/postgres/dao/ChainSyncRecordRepository.kt | 6 +++--- .../bcgateway/postgres/dao/ChainSyncScheduleRepository.kt | 6 +++--- .../bcgateway/postgres/dao/ReservedAddressRepository.kt | 6 +++--- .../bcgateway/postgres/impl/ReservedAddressHandlerImpl.kt | 2 ++ 7 files changed, 22 insertions(+), 18 deletions(-) diff --git a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/ChainAddressTypeRepository.kt b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/ChainAddressTypeRepository.kt index 5708cb225..2248a9474 100644 --- a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/ChainAddressTypeRepository.kt +++ b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/ChainAddressTypeRepository.kt @@ -2,7 +2,7 @@ package co.nilin.opex.port.bcgateway.postgres.dao import co.nilin.opex.port.bcgateway.postgres.model.ChainAddressTypeModel import org.springframework.data.repository.reactive.ReactiveCrudRepository +import org.springframework.stereotype.Repository -interface ChainAddressTypeRepository : ReactiveCrudRepository { - -} \ No newline at end of file +@Repository +interface ChainAddressTypeRepository : ReactiveCrudRepository \ No newline at end of file diff --git a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/ChainEndpointRepository.kt b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/ChainEndpointRepository.kt index 9c43fe56c..88e5fdc26 100644 --- a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/ChainEndpointRepository.kt +++ b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/ChainEndpointRepository.kt @@ -2,7 +2,7 @@ package co.nilin.opex.port.bcgateway.postgres.dao import co.nilin.opex.port.bcgateway.postgres.model.ChainEndpointModel import org.springframework.data.repository.reactive.ReactiveCrudRepository +import org.springframework.stereotype.Repository -interface ChainEndpointRepository : ReactiveCrudRepository { - -} \ No newline at end of file +@Repository +interface ChainEndpointRepository : ReactiveCrudRepository \ No newline at end of file diff --git a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/ChainRepository.kt b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/ChainRepository.kt index 35de62148..57859db71 100644 --- a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/ChainRepository.kt +++ b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/ChainRepository.kt @@ -1,12 +1,14 @@ package co.nilin.opex.port.bcgateway.postgres.dao -import co.nilin.opex.port.bcgateway.postgres.model.AssignedAddressChainModel import co.nilin.opex.port.bcgateway.postgres.model.ChainModel +import co.nilin.opex.port.bcgateway.postgres.model.ChainModelProjection import kotlinx.coroutines.flow.Flow 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 +@Repository interface ChainRepository : ReactiveCrudRepository { - + @Query("SELECT * FROM chain WHERE name = :name") + fun findByNameProjected(name: String): Flow } \ No newline at end of file diff --git a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/ChainSyncRecordRepository.kt b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/ChainSyncRecordRepository.kt index 94cb26e68..b3f4ee2b0 100644 --- a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/ChainSyncRecordRepository.kt +++ b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/ChainSyncRecordRepository.kt @@ -7,7 +7,7 @@ import kotlinx.coroutines.flow.Flow 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 -interface ChainSyncRecordRepository : ReactiveCrudRepository { - -} \ No newline at end of file +@Repository +interface ChainSyncRecordRepository : ReactiveCrudRepository \ No newline at end of file diff --git a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/ChainSyncScheduleRepository.kt b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/ChainSyncScheduleRepository.kt index 7455e08bd..48a7a0252 100644 --- a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/ChainSyncScheduleRepository.kt +++ b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/ChainSyncScheduleRepository.kt @@ -7,7 +7,7 @@ import kotlinx.coroutines.flow.Flow 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 -interface ChainSyncScheduleRepository : ReactiveCrudRepository { - -} \ No newline at end of file +@Repository +interface ChainSyncScheduleRepository : ReactiveCrudRepository \ No newline at end of file diff --git a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/ReservedAddressRepository.kt b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/ReservedAddressRepository.kt index 7234f5dd8..a48a80366 100644 --- a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/ReservedAddressRepository.kt +++ b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/ReservedAddressRepository.kt @@ -2,7 +2,7 @@ package co.nilin.opex.port.bcgateway.postgres.dao import co.nilin.opex.port.bcgateway.postgres.model.ReservedAddressModel import org.springframework.data.repository.reactive.ReactiveCrudRepository +import org.springframework.stereotype.Repository -interface ReservedAddressRepository : ReactiveCrudRepository { - -} \ No newline at end of file +@Repository +interface ReservedAddressRepository : ReactiveCrudRepository \ No newline at end of file diff --git a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/impl/ReservedAddressHandlerImpl.kt b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/impl/ReservedAddressHandlerImpl.kt index f799462d1..ed2e0e547 100644 --- a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/impl/ReservedAddressHandlerImpl.kt +++ b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/impl/ReservedAddressHandlerImpl.kt @@ -3,7 +3,9 @@ package co.nilin.opex.port.bcgateway.postgres.impl import co.nilin.opex.bcgateway.core.model.AddressType import co.nilin.opex.bcgateway.core.model.ReservedAddress import co.nilin.opex.bcgateway.core.spi.ReservedAddressHandler +import org.springframework.stereotype.Component +@Component class ReservedAddressHandlerImpl: ReservedAddressHandler { override suspend fun peekReservedAddress(addressType: AddressType): ReservedAddress? { TODO("Not yet implemented") From c37bd9c128812fbb2a92a0f7feb7c8a753026821 Mon Sep 17 00:00:00 2001 From: metalicn20 Date: Tue, 31 Aug 2021 19:13:45 +0430 Subject: [PATCH 21/37] Add bc-gateway-postgres dependency to bc-gateway-app --- BlockchainGateway/bc-gateway-app/pom.xml | 441 ++++++++++++----------- 1 file changed, 223 insertions(+), 218 deletions(-) diff --git a/BlockchainGateway/bc-gateway-app/pom.xml b/BlockchainGateway/bc-gateway-app/pom.xml index fcd1200f7..71355c3df 100644 --- a/BlockchainGateway/bc-gateway-app/pom.xml +++ b/BlockchainGateway/bc-gateway-app/pom.xml @@ -1,227 +1,232 @@ - 4.0.0 - - org.springframework.boot - spring-boot-starter-parent - 2.4.4 - - - co.nilin.opex.external - bc-gateway-app - 1.0-SNAPSHOT - bc-gateway-app - Blockchain gateway app of Opex + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.4.4 + + + co.nilin.opex.external + bc-gateway-app + 1.0-SNAPSHOT + bc-gateway-app + Blockchain gateway app of Opex - - 1.8 - 1.4.31 - ${version} - ${version} - 2020.0.2 - + + 1.8 + 1.4.31 + ${version} + ${version} + 2020.0.2 + - - - org.springframework.boot - spring-boot-starter-webflux - - - com.fasterxml.jackson.module - jackson-module-kotlin - - - - org.jetbrains.kotlin - kotlin-stdlib - - - io.projectreactor.kotlin - reactor-kotlin-extensions - - - org.jetbrains.kotlin - kotlin-reflect - - - org.jetbrains.kotlin - kotlin-stdlib-jdk8 - - - org.jetbrains.kotlinx - kotlinx-coroutines-reactor - - - org.jetbrains.kotlinx - kotlinx-coroutines-core - - - org.springframework.boot - spring-boot-starter-test - test - - - org.junit.vintage - junit-vintage-engine - - - - - io.projectreactor - reactor-test - test - - - org.springframework.cloud - spring-cloud-starter-consul-all - - - org.springframework.boot - spring-boot-starter-actuator - - - org.springframework.boot - spring-boot-starter-security - - - org.springframework.boot - spring-boot-starter-oauth2-resource-server - - - org.bouncycastle - bcprov-jdk15on - 1.60 - - - co.nilin.opex.external - bc-gateway-core - ${bc-gateway.version} - - + + + org.springframework.boot + spring-boot-starter-webflux + + + com.fasterxml.jackson.module + jackson-module-kotlin + + + + org.jetbrains.kotlin + kotlin-stdlib + + + io.projectreactor.kotlin + reactor-kotlin-extensions + + + org.jetbrains.kotlin + kotlin-reflect + + + org.jetbrains.kotlin + kotlin-stdlib-jdk8 + + + org.jetbrains.kotlinx + kotlinx-coroutines-reactor + + + org.jetbrains.kotlinx + kotlinx-coroutines-core + + + org.springframework.boot + spring-boot-starter-test + test + + + org.junit.vintage + junit-vintage-engine + + + + + io.projectreactor + reactor-test + test + + + org.springframework.cloud + spring-cloud-starter-consul-all + + + org.springframework.boot + spring-boot-starter-actuator + + + org.springframework.boot + spring-boot-starter-security + + + org.springframework.boot + spring-boot-starter-oauth2-resource-server + + + org.bouncycastle + bcprov-jdk15on + 1.60 + + + co.nilin.opex.external + bc-gateway-core + ${bc-gateway.version} + + + co.nilin.opex.external + bc-gateway-persister-postgres + ${bc-gateway.version} + + - - - - org.springframework.cloud - spring-cloud-dependencies - ${spring-cloud.version} - pom - import - - - + + + + org.springframework.cloud + spring-cloud-dependencies + ${spring-cloud.version} + pom + import + + + - - ${project.basedir}/src/main/kotlin - ${project.basedir}/src/test/kotlin - - - org.springframework.boot - spring-boot-maven-plugin - - - org.apache.maven.plugins - maven-surefire-plugin - 2.18 - - - ${skip.unit.tests} - - - **/*IntegrationTest.java - - - - - org.codehaus.mojo - build-helper-maven-plugin - - - add-test-source - generate-test-sources - - add-test-source - - - - src/test/java - - - - - compile - - add-source - - - - src/main/java - - - - - - - org.jetbrains.kotlin - kotlin-maven-plugin - ${kotlin.version} - - - compile - compile - - compile - - - - test-compile - test-compile - - test-compile - - - - - - -Xjsr305=strict - - - spring - - 1.8 - - - - org.jetbrains.kotlin - kotlin-maven-allopen - ${kotlin.version} - - - - - org.apache.maven.plugins - maven-compiler-plugin - - - compile - compile - - compile - - - - testCompile - test-compile - - testCompile - - - - - - opex-bc-gateway - + + ${project.basedir}/src/main/kotlin + ${project.basedir}/src/test/kotlin + + + org.springframework.boot + spring-boot-maven-plugin + + + org.apache.maven.plugins + maven-surefire-plugin + 2.18 + + + ${skip.unit.tests} + + + **/*IntegrationTest.java + + + + + org.codehaus.mojo + build-helper-maven-plugin + + + add-test-source + generate-test-sources + + add-test-source + + + + src/test/java + + + + + compile + + add-source + + + + src/main/java + + + + + + + org.jetbrains.kotlin + kotlin-maven-plugin + ${kotlin.version} + + + compile + compile + + compile + + + + test-compile + test-compile + + test-compile + + + + + + -Xjsr305=strict + + + spring + + 1.8 + + + + org.jetbrains.kotlin + kotlin-maven-allopen + ${kotlin.version} + + + + + org.apache.maven.plugins + maven-compiler-plugin + + + compile + compile + + compile + + + + testCompile + test-compile + + testCompile + + + + + + opex-bc-gateway + From 110f394a974665a0ab2828067ba28f7ba9515d1c Mon Sep 17 00:00:00 2001 From: metalicn20 Date: Tue, 31 Aug 2021 19:30:34 +0430 Subject: [PATCH 22/37] Update docker-compose config --- Deployment/docker-compose.yml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/Deployment/docker-compose.yml b/Deployment/docker-compose.yml index 0636b05b5..e10c538b7 100644 --- a/Deployment/docker-compose.yml +++ b/Deployment/docker-compose.yml @@ -45,16 +45,12 @@ services: condition: on-failure redis: image: "redis:alpine" - command: redis-server - ports: - "127.0.0.1:6379:6379" - volumes: - $PWD/runtime/redis-data:/var/lib/redis - $PWD/runtime/redis.conf:/usr/local/etc/redis/redis.conf - environment: - REDIS_REPLICATION_MODE=master networks: From b11ffe61fcfd0b191010eef89c5c35f84184d421 Mon Sep 17 00:00:00 2001 From: metalicn20 Date: Tue, 31 Aug 2021 19:39:41 +0430 Subject: [PATCH 23/37] Fix sql scripts --- .../opex/port/bcgateway/postgres/config/PostgresConfig.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/config/PostgresConfig.kt b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/config/PostgresConfig.kt index 593d6e03d..ed39c1051 100644 --- a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/config/PostgresConfig.kt +++ b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/config/PostgresConfig.kt @@ -23,7 +23,7 @@ class PostgresConfig(db: DatabaseClient) { address VARCHAR(72) NOT NULL, memo VARCHAR(72), addr_type_id NUMERIC NOT NULL, - UNIQUE (address, memo), + UNIQUE (address, memo) ); CREATE TABLE IF NOT EXISTS assigned_address_chains ( id SERIAL PRIMARY KEY, @@ -77,7 +77,7 @@ class PostgresConfig(db: DatabaseClient) { token_name VARCHAR(72), withdraw_enabled BOOLEAN NOT NULL, withdraw_fee NUMERIC NOT NULL, - withdraw_min NUMERIC NOT NULL, + withdraw_min NUMERIC NOT NULL ); """ } From 29d98dccb53dcbd11ec93560ec784c2e291ffa5e Mon Sep 17 00:00:00 2001 From: metalicn20 Date: Wed, 1 Sep 2021 14:05:44 +0430 Subject: [PATCH 24/37] Fix blockchain gateway port --- .../bc-gateway-app/src/main/resources/application-docker.yml | 2 +- .../bc-gateway-app/src/main/resources/application.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/BlockchainGateway/bc-gateway-app/src/main/resources/application-docker.yml b/BlockchainGateway/bc-gateway-app/src/main/resources/application-docker.yml index 37996be60..ffbf14176 100644 --- a/BlockchainGateway/bc-gateway-app/src/main/resources/application-docker.yml +++ b/BlockchainGateway/bc-gateway-app/src/main/resources/application-docker.yml @@ -1,4 +1,4 @@ -server.port: 8091 +server.port: 8095 spring: application: name: opex-bc-gateway diff --git a/BlockchainGateway/bc-gateway-app/src/main/resources/application.yml b/BlockchainGateway/bc-gateway-app/src/main/resources/application.yml index 2c544a1f5..c21510415 100644 --- a/BlockchainGateway/bc-gateway-app/src/main/resources/application.yml +++ b/BlockchainGateway/bc-gateway-app/src/main/resources/application.yml @@ -1,4 +1,4 @@ -server.port: 8091 +server.port: 8095 spring: application: name: opex-bc-gateway From df4a9abebc72fac95aecbdb2fde814a6f76e6311 Mon Sep 17 00:00:00 2001 From: metalicn20 Date: Wed, 1 Sep 2021 14:25:12 +0430 Subject: [PATCH 25/37] Pluralize db names --- .../opex/port/bcgateway/postgres/config/PostgresConfig.kt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/config/PostgresConfig.kt b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/config/PostgresConfig.kt index ed39c1051..358cad6ad 100644 --- a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/config/PostgresConfig.kt +++ b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/config/PostgresConfig.kt @@ -37,7 +37,7 @@ class PostgresConfig(db: DatabaseClient) { address_type VARCHAR(72) NOT NULL, UNIQUE (address, memo) ); - CREATE TABLE IF NOT EXISTS chain ( + CREATE TABLE IF NOT EXISTS chains ( name VARCHAR(72) PRIMARY KEY ); CREATE TABLE IF NOT EXISTS chain_address_types ( @@ -52,12 +52,12 @@ class PostgresConfig(db: DatabaseClient) { endpoint_user VARCHAR(72), endpoint_password VARCHAR(72) ); - CREATE TABLE IF NOT EXISTS chain_sync_schedule ( + CREATE TABLE IF NOT EXISTS chain_sync_schedules ( chain VARCHAR(72) PRIMARY KEY, retry_time TIMESTAMP, delay NUMERIC ); - CREATE TABLE IF NOT EXISTS chain_sync_record ( + CREATE TABLE IF NOT EXISTS chain_sync_records ( chain VARCHAR(72) PRIMARY KEY, time TIMESTAMP NOT NULL, endpoint_url VARCHAR(72) NOT NULL, From cb576eb5db5e09416c33884ac13cd617cda006f0 Mon Sep 17 00:00:00 2001 From: metalicn20 Date: Wed, 1 Sep 2021 16:10:49 +0430 Subject: [PATCH 26/37] Fix chain model table names --- .../co/nilin/opex/port/bcgateway/postgres/model/ChainModel.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/model/ChainModel.kt b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/model/ChainModel.kt index 11c79f3e0..fb91ee878 100644 --- a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/model/ChainModel.kt +++ b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/model/ChainModel.kt @@ -4,7 +4,7 @@ import org.springframework.data.annotation.Id import org.springframework.data.relational.core.mapping.Column import org.springframework.data.relational.core.mapping.Table -@Table("chain") +@Table("chains") data class ChainModel(@Id val name: String) @Table("chain_address_types") From 03a7ea54a01d4cedbe58e5b4646563773fb2b9ea Mon Sep 17 00:00:00 2001 From: metalicn20 Date: Sun, 5 Sep 2021 11:01:59 +0430 Subject: [PATCH 27/37] Fix numeric types in database --- .../port/bcgateway/postgres/config/PostgresConfig.kt | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/config/PostgresConfig.kt b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/config/PostgresConfig.kt index 358cad6ad..0804207c9 100644 --- a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/config/PostgresConfig.kt +++ b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/config/PostgresConfig.kt @@ -22,12 +22,12 @@ class PostgresConfig(db: DatabaseClient) { uuid VARCHAR(72) NOT NULL, address VARCHAR(72) NOT NULL, memo VARCHAR(72), - addr_type_id NUMERIC NOT NULL, + addr_type_id INTEGER NOT NULL, UNIQUE (address, memo) ); CREATE TABLE IF NOT EXISTS assigned_address_chains ( id SERIAL PRIMARY KEY, - assigned_address_id NUMERIC NOT NULL, + assigned_address_id INTEGER NOT NULL, chain VARCHAR(72) NOT NULL ); CREATE TABLE IF NOT EXISTS reserved_addresses ( @@ -42,8 +42,8 @@ class PostgresConfig(db: DatabaseClient) { ); CREATE TABLE IF NOT EXISTS chain_address_types ( id SERIAL PRIMARY KEY, - chain_name VARCHAR(72) NOT NULL, - addr_type_id NUMERIC NOT NULL + chain_name VARCHAR(72) NOT NULL REFERENCES chains (name), + addr_type_id INTEGER NOT NULL REFERENCES address_types (id) ); CREATE TABLE IF NOT EXISTS chain_endpoints ( id SERIAL PRIMARY KEY, @@ -61,7 +61,7 @@ class PostgresConfig(db: DatabaseClient) { chain VARCHAR(72) PRIMARY KEY, time TIMESTAMP NOT NULL, endpoint_url VARCHAR(72) NOT NULL, - latest_block NUMERIC NOT NULL, + latest_block INTEGER NOT NULL, success BOOLEAN NOT NULL, error VARCHAR(100) ); From ec476e5a69b35615cf59df6379b46c1f77ca4bd0 Mon Sep 17 00:00:00 2001 From: metalicn20 Date: Sun, 5 Sep 2021 15:58:37 +0430 Subject: [PATCH 28/37] Fix currency loader --- .../postgres/dao/CurrencyImplementationRepository.kt | 7 ++++--- .../port/bcgateway/postgres/impl/CurrencyLoaderImpl.kt | 5 +++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/CurrencyImplementationRepository.kt b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/CurrencyImplementationRepository.kt index 06a30eb6b..6d00f6a36 100644 --- a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/CurrencyImplementationRepository.kt +++ b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/CurrencyImplementationRepository.kt @@ -2,6 +2,8 @@ package co.nilin.opex.port.bcgateway.postgres.dao import co.nilin.opex.port.bcgateway.postgres.model.CurrencyImplementationModel import kotlinx.coroutines.flow.Flow +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.Mono @@ -16,8 +18,7 @@ interface CurrencyImplementationRepository : ReactiveCrudRepository - fun findByChainAndAddress( - chain: String, - address: String? + fun findByChainAndTokenAddress( + chain: String, tokenAddress: String? ): Mono } diff --git a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/impl/CurrencyLoaderImpl.kt b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/impl/CurrencyLoaderImpl.kt index f4a000214..0dae37112 100644 --- a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/impl/CurrencyLoaderImpl.kt +++ b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/impl/CurrencyLoaderImpl.kt @@ -10,9 +10,10 @@ import co.nilin.opex.port.bcgateway.postgres.dao.CurrencyRepository import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.toList import kotlinx.coroutines.reactive.awaitFirstOrNull -import kotlinx.coroutines.reactive.awaitSingle import kotlinx.coroutines.reactive.awaitSingleOrNull +import org.springframework.stereotype.Component +@Component class CurrencyLoaderImpl( private val currencyRepository: CurrencyRepository, private val currencyImplementationRepository: CurrencyImplementationRepository @@ -36,7 +37,7 @@ class CurrencyLoaderImpl( } override suspend fun findSymbol(chain: String, address: String?): String? { - return currencyImplementationRepository.findByChainAndAddress(chain, address) + return currencyImplementationRepository.findByChainAndTokenAddress(chain, address) .awaitFirstOrNull()?.symbol } From 060d8385c8bcf8eee0c0acdf865e54eff415c2a2 Mon Sep 17 00:00:00 2001 From: metalicn20 Date: Sun, 5 Sep 2021 15:59:57 +0430 Subject: [PATCH 29/37] Update chain repository --- .../opex/port/bcgateway/postgres/dao/ChainRepository.kt | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/ChainRepository.kt b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/ChainRepository.kt index 57859db71..17569dc7d 100644 --- a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/ChainRepository.kt +++ b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/ChainRepository.kt @@ -1,14 +1,11 @@ package co.nilin.opex.port.bcgateway.postgres.dao import co.nilin.opex.port.bcgateway.postgres.model.ChainModel -import co.nilin.opex.port.bcgateway.postgres.model.ChainModelProjection import kotlinx.coroutines.flow.Flow -import org.springframework.data.r2dbc.repository.Query import org.springframework.data.repository.reactive.ReactiveCrudRepository import org.springframework.stereotype.Repository @Repository interface ChainRepository : ReactiveCrudRepository { - @Query("SELECT * FROM chain WHERE name = :name") - fun findByNameProjected(name: String): Flow + fun findByName(name: String): Flow } \ No newline at end of file From 92d8897664ebdfdc644260d47a719ee38718b96e Mon Sep 17 00:00:00 2001 From: metalicn20 Date: Sun, 5 Sep 2021 16:11:07 +0430 Subject: [PATCH 30/37] Add some sub entity queries to chain repository --- .../bcgateway/postgres/dao/ChainRepository.kt | 17 +++++++++++++++++ .../postgres/dao/CurrencyRepository.kt | 1 - 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/ChainRepository.kt b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/ChainRepository.kt index 17569dc7d..86dedbb55 100644 --- a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/ChainRepository.kt +++ b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/ChainRepository.kt @@ -1,11 +1,28 @@ package co.nilin.opex.port.bcgateway.postgres.dao +import co.nilin.opex.port.bcgateway.postgres.model.AddressTypeModel +import co.nilin.opex.port.bcgateway.postgres.model.ChainEndpointModel import co.nilin.opex.port.bcgateway.postgres.model.ChainModel import kotlinx.coroutines.flow.Flow +import org.springframework.data.r2dbc.repository.Query import org.springframework.data.repository.reactive.ReactiveCrudRepository import org.springframework.stereotype.Repository @Repository interface ChainRepository : ReactiveCrudRepository { fun findByName(name: String): Flow + + @Query( + """ + select chain_address_types.chain_name, address_types.address_type, address_types.address_regex, address_types.memo_regex + from chain_address_types + join address_types + on address_types.id = chain_address_types.addr_type_id + where chain_name = :name + """ + ) + fun findAddressTypesByName(name: String): Flow + + @Query("select * from chain_endpoints where chain_name = :name") + fun findEndpointsByName(name: String): Flow } \ No newline at end of file diff --git a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/CurrencyRepository.kt b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/CurrencyRepository.kt index aa7aecfee..f44e25b6b 100644 --- a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/CurrencyRepository.kt +++ b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/CurrencyRepository.kt @@ -10,6 +10,5 @@ import reactor.core.publisher.Mono @Repository interface CurrencyRepository : ReactiveCrudRepository { - fun findBySymbol(symbol: String): Mono } From 26e7478b4a16ae83b12dd6134e76d282246d7ed3 Mon Sep 17 00:00:00 2001 From: metalicn20 Date: Sun, 5 Sep 2021 16:17:18 +0430 Subject: [PATCH 31/37] Add id to chain repository fetch method --- .../nilin/opex/port/bcgateway/postgres/dao/ChainRepository.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/ChainRepository.kt b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/ChainRepository.kt index 86dedbb55..9395b112a 100644 --- a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/ChainRepository.kt +++ b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/dao/ChainRepository.kt @@ -14,7 +14,7 @@ interface ChainRepository : ReactiveCrudRepository { @Query( """ - select chain_address_types.chain_name, address_types.address_type, address_types.address_regex, address_types.memo_regex + select address_types.id, chain_address_types.chain_name, address_types.address_type, address_types.address_regex, address_types.memo_regex from chain_address_types join address_types on address_types.id = chain_address_types.addr_type_id From 6924956741077336d984d70849b9ca5362f84d05 Mon Sep 17 00:00:00 2001 From: metalicn20 Date: Sun, 5 Sep 2021 16:29:08 +0430 Subject: [PATCH 32/37] Add chain model data to currency loader --- .../postgres/impl/CurrencyLoaderImpl.kt | 27 ++++++++++++++----- 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/impl/CurrencyLoaderImpl.kt b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/impl/CurrencyLoaderImpl.kt index 0dae37112..2dac75a5d 100644 --- a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/impl/CurrencyLoaderImpl.kt +++ b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/impl/CurrencyLoaderImpl.kt @@ -1,10 +1,8 @@ package co.nilin.opex.port.bcgateway.postgres.impl -import co.nilin.opex.bcgateway.core.model.Chain -import co.nilin.opex.bcgateway.core.model.Currency -import co.nilin.opex.bcgateway.core.model.CurrencyImplementation -import co.nilin.opex.bcgateway.core.model.CurrencyInfo +import co.nilin.opex.bcgateway.core.model.* import co.nilin.opex.bcgateway.core.spi.CurrencyLoader +import co.nilin.opex.port.bcgateway.postgres.dao.ChainRepository import co.nilin.opex.port.bcgateway.postgres.dao.CurrencyImplementationRepository import co.nilin.opex.port.bcgateway.postgres.dao.CurrencyRepository import kotlinx.coroutines.flow.map @@ -15,6 +13,7 @@ import org.springframework.stereotype.Component @Component class CurrencyLoaderImpl( + private val chainRepository: ChainRepository, private val currencyRepository: CurrencyRepository, private val currencyImplementationRepository: CurrencyImplementationRepository ) : CurrencyLoader { @@ -23,9 +22,17 @@ class CurrencyLoaderImpl( val currencyImplDao = currencyImplementationRepository.findBySymbol(symbol) val currency = Currency(currencyDao.symbol, currencyDao.name) return CurrencyInfo(currency, currencyImplDao.map { + val addressTypesDao = chainRepository.findAddressTypesByName(it.chain) + val addressTypes = addressTypesDao.map { addressType -> + AddressType(addressType.id!!, addressType.type, addressType.addressRegex, addressType.memoRegex) + } + val endpointsDao = chainRepository.findEndpointsByName(it.chain) + val endpoints = endpointsDao.map { endpoint -> + Endpoint(endpoint.url) + } CurrencyImplementation( currency, - Chain(it.chain, emptyList(), emptyList()), + Chain(it.chain, addressTypes.toList(), endpoints.toList()), it.token, it.tokenAddress, it.tokenName, @@ -45,9 +52,17 @@ class CurrencyLoaderImpl( return currencyImplementationRepository.findByChain(chain).map { val currencyDao = currencyRepository.findBySymbol(it.symbol).awaitSingleOrNull() val currency = Currency(currencyDao.symbol, currencyDao.name) + val addressTypesDao = chainRepository.findAddressTypesByName(it.chain) + val addressTypes = addressTypesDao.map { addressType -> + AddressType(addressType.id!!, addressType.type, addressType.addressRegex, addressType.memoRegex) + } + val endpointsDao = chainRepository.findEndpointsByName(it.chain) + val endpoints = endpointsDao.map { endpoint -> + Endpoint(endpoint.url) + } CurrencyImplementation( currency, - Chain(it.chain, emptyList(), emptyList()), + Chain(it.chain, addressTypes.toList(), endpoints.toList()), it.token, it.tokenAddress, it.tokenName, From 6e3118e3557e1dc6f9ee560708c0c20eba19078d Mon Sep 17 00:00:00 2001 From: metalicn20 Date: Sat, 11 Sep 2021 11:54:14 +0430 Subject: [PATCH 33/37] Refactor currency loader --- .../opex/bcgateway/core/spi/CurrencyLoader.kt | 2 +- .../postgres/impl/CurrencyLoaderImpl.kt | 75 +++++++++---------- 2 files changed, 37 insertions(+), 40 deletions(-) diff --git a/BlockchainGateway/bc-gateway-core/src/main/kotlin/co/nilin/opex/bcgateway/core/spi/CurrencyLoader.kt b/BlockchainGateway/bc-gateway-core/src/main/kotlin/co/nilin/opex/bcgateway/core/spi/CurrencyLoader.kt index 32aa395f1..7256225bf 100644 --- a/BlockchainGateway/bc-gateway-core/src/main/kotlin/co/nilin/opex/bcgateway/core/spi/CurrencyLoader.kt +++ b/BlockchainGateway/bc-gateway-core/src/main/kotlin/co/nilin/opex/bcgateway/core/spi/CurrencyLoader.kt @@ -4,7 +4,7 @@ import co.nilin.opex.bcgateway.core.model.CurrencyImplementation import co.nilin.opex.bcgateway.core.model.CurrencyInfo interface CurrencyLoader { - suspend fun fetchCurrencyInfo(symbol: String): CurrencyInfo + suspend fun fetchCurrencyInfo(symbol: String): CurrencyInfo? suspend fun findSymbol(chain: String, address: String?): String? suspend fun findImplementationsWithTokenOnChain(chain: String): List } \ No newline at end of file diff --git a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/impl/CurrencyLoaderImpl.kt b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/impl/CurrencyLoaderImpl.kt index 2dac75a5d..6e1cd2ea9 100644 --- a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/impl/CurrencyLoaderImpl.kt +++ b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/impl/CurrencyLoaderImpl.kt @@ -17,30 +17,31 @@ class CurrencyLoaderImpl( private val currencyRepository: CurrencyRepository, private val currencyImplementationRepository: CurrencyImplementationRepository ) : CurrencyLoader { - override suspend fun fetchCurrencyInfo(symbol: String): CurrencyInfo { + override suspend fun fetchCurrencyInfo(symbol: String): CurrencyInfo? { val currencyDao = currencyRepository.findBySymbol(symbol).awaitSingleOrNull() - val currencyImplDao = currencyImplementationRepository.findBySymbol(symbol) - val currency = Currency(currencyDao.symbol, currencyDao.name) - return CurrencyInfo(currency, currencyImplDao.map { - val addressTypesDao = chainRepository.findAddressTypesByName(it.chain) - val addressTypes = addressTypesDao.map { addressType -> - AddressType(addressType.id!!, addressType.type, addressType.addressRegex, addressType.memoRegex) - } - val endpointsDao = chainRepository.findEndpointsByName(it.chain) - val endpoints = endpointsDao.map { endpoint -> - Endpoint(endpoint.url) + if (currencyDao !== null) { + val currencyImplDao = currencyImplementationRepository.findBySymbol(symbol) + val currency = Currency(currencyDao.symbol, currencyDao.name) + val implementations = currencyImplDao.map { impl -> + val addressTypesDao = chainRepository.findAddressTypesByName(impl.chain) + val addressTypes = addressTypesDao.map { AddressType(it.id!!, it.type, it.addressRegex, it.memoRegex) } + val endpointsDao = chainRepository.findEndpointsByName(impl.chain) + val endpoints = endpointsDao.map { Endpoint(it.url) } + CurrencyImplementation( + currency, + Chain(impl.chain, addressTypes.toList(), endpoints.toList()), + impl.token, + impl.tokenAddress, + impl.tokenName, + impl.withdrawEnabled, + impl.withdrawFee, + impl.withdrawMin + ) } - CurrencyImplementation( - currency, - Chain(it.chain, addressTypes.toList(), endpoints.toList()), - it.token, - it.tokenAddress, - it.tokenName, - it.withdrawEnabled, - it.withdrawFee, - it.withdrawMin - ) - }.toList()) + return CurrencyInfo(currency, implementations.toList()) + } else { + return null + } } override suspend fun findSymbol(chain: String, address: String?): String? { @@ -49,26 +50,22 @@ class CurrencyLoaderImpl( } override suspend fun findImplementationsWithTokenOnChain(chain: String): List { - return currencyImplementationRepository.findByChain(chain).map { - val currencyDao = currencyRepository.findBySymbol(it.symbol).awaitSingleOrNull() + return currencyImplementationRepository.findByChain(chain).map { impl -> + val currencyDao = currencyRepository.findBySymbol(impl.symbol).awaitSingleOrNull() val currency = Currency(currencyDao.symbol, currencyDao.name) - val addressTypesDao = chainRepository.findAddressTypesByName(it.chain) - val addressTypes = addressTypesDao.map { addressType -> - AddressType(addressType.id!!, addressType.type, addressType.addressRegex, addressType.memoRegex) - } - val endpointsDao = chainRepository.findEndpointsByName(it.chain) - val endpoints = endpointsDao.map { endpoint -> - Endpoint(endpoint.url) - } + val addressTypesDao = chainRepository.findAddressTypesByName(impl.chain) + val addressTypes = addressTypesDao.map { AddressType(it.id!!, it.type, it.addressRegex, it.memoRegex) } + val endpointsDao = chainRepository.findEndpointsByName(impl.chain) + val endpoints = endpointsDao.map { Endpoint(it.url) } CurrencyImplementation( currency, - Chain(it.chain, addressTypes.toList(), endpoints.toList()), - it.token, - it.tokenAddress, - it.tokenName, - it.withdrawEnabled, - it.withdrawFee, - it.withdrawMin + Chain(impl.chain, addressTypes.toList(), endpoints.toList()), + impl.token, + impl.tokenAddress, + impl.tokenName, + impl.withdrawEnabled, + impl.withdrawFee, + impl.withdrawMin ) }.toList() } From ac757f23d510d6989dd20ca6d0ead841e2840bed Mon Sep 17 00:00:00 2001 From: metalicn20 Date: Sat, 11 Sep 2021 12:21:47 +0430 Subject: [PATCH 34/37] Apply DRY to currency loader --- .../postgres/impl/CurrencyLoaderImpl.kt | 66 ++++++++----------- 1 file changed, 29 insertions(+), 37 deletions(-) diff --git a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/impl/CurrencyLoaderImpl.kt b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/impl/CurrencyLoaderImpl.kt index 6e1cd2ea9..3e0d753c9 100644 --- a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/impl/CurrencyLoaderImpl.kt +++ b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/impl/CurrencyLoaderImpl.kt @@ -5,6 +5,8 @@ import co.nilin.opex.bcgateway.core.spi.CurrencyLoader import co.nilin.opex.port.bcgateway.postgres.dao.ChainRepository import co.nilin.opex.port.bcgateway.postgres.dao.CurrencyImplementationRepository import co.nilin.opex.port.bcgateway.postgres.dao.CurrencyRepository +import co.nilin.opex.port.bcgateway.postgres.model.CurrencyImplementationModel +import co.nilin.opex.port.bcgateway.postgres.model.CurrencyModel import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.toList import kotlinx.coroutines.reactive.awaitFirstOrNull @@ -19,28 +21,13 @@ class CurrencyLoaderImpl( ) : CurrencyLoader { override suspend fun fetchCurrencyInfo(symbol: String): CurrencyInfo? { val currencyDao = currencyRepository.findBySymbol(symbol).awaitSingleOrNull() - if (currencyDao !== null) { + return if (currencyDao !== null) { val currencyImplDao = currencyImplementationRepository.findBySymbol(symbol) val currency = Currency(currencyDao.symbol, currencyDao.name) - val implementations = currencyImplDao.map { impl -> - val addressTypesDao = chainRepository.findAddressTypesByName(impl.chain) - val addressTypes = addressTypesDao.map { AddressType(it.id!!, it.type, it.addressRegex, it.memoRegex) } - val endpointsDao = chainRepository.findEndpointsByName(impl.chain) - val endpoints = endpointsDao.map { Endpoint(it.url) } - CurrencyImplementation( - currency, - Chain(impl.chain, addressTypes.toList(), endpoints.toList()), - impl.token, - impl.tokenAddress, - impl.tokenName, - impl.withdrawEnabled, - impl.withdrawFee, - impl.withdrawMin - ) - } - return CurrencyInfo(currency, implementations.toList()) + val implementations = currencyImplDao.map { projectCurrencyImplementation(it, currencyDao) } + CurrencyInfo(currency, implementations.toList()) } else { - return null + null } } @@ -50,23 +37,28 @@ class CurrencyLoaderImpl( } override suspend fun findImplementationsWithTokenOnChain(chain: String): List { - return currencyImplementationRepository.findByChain(chain).map { impl -> - val currencyDao = currencyRepository.findBySymbol(impl.symbol).awaitSingleOrNull() - val currency = Currency(currencyDao.symbol, currencyDao.name) - val addressTypesDao = chainRepository.findAddressTypesByName(impl.chain) - val addressTypes = addressTypesDao.map { AddressType(it.id!!, it.type, it.addressRegex, it.memoRegex) } - val endpointsDao = chainRepository.findEndpointsByName(impl.chain) - val endpoints = endpointsDao.map { Endpoint(it.url) } - CurrencyImplementation( - currency, - Chain(impl.chain, addressTypes.toList(), endpoints.toList()), - impl.token, - impl.tokenAddress, - impl.tokenName, - impl.withdrawEnabled, - impl.withdrawFee, - impl.withdrawMin - ) - }.toList() + return currencyImplementationRepository.findByChain(chain).map { projectCurrencyImplementation(it) }.toList() + } + + private suspend fun projectCurrencyImplementation( + implDao: CurrencyImplementationModel, + currencyDao: CurrencyModel? = null + ): CurrencyImplementation { + val addressTypesDao = chainRepository.findAddressTypesByName(implDao.chain) + val addressTypes = addressTypesDao.map { AddressType(it.id!!, it.type, it.addressRegex, it.memoRegex) } + val endpointsDao = chainRepository.findEndpointsByName(implDao.chain) + val endpoints = endpointsDao.map { Endpoint(it.url) } + val currencyDao2 = currencyDao ?: currencyRepository.findBySymbol(implDao.symbol).awaitSingleOrNull() + val currency = Currency(currencyDao2.symbol, currencyDao2.name) + return CurrencyImplementation( + currency, + Chain(implDao.chain, addressTypes.toList(), endpoints.toList()), + implDao.token, + implDao.tokenAddress, + implDao.tokenName, + implDao.withdrawEnabled, + implDao.withdrawFee, + implDao.withdrawMin + ) } } From 23e8d293a8edf077df41b387b1b8aa7a41f366fb Mon Sep 17 00:00:00 2001 From: metalicn20 Date: Sat, 11 Sep 2021 12:30:31 +0430 Subject: [PATCH 35/37] Fix null currency issue in currency loader --- .../opex/port/bcgateway/postgres/impl/CurrencyLoaderImpl.kt | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/impl/CurrencyLoaderImpl.kt b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/impl/CurrencyLoaderImpl.kt index 3e0d753c9..3aa6c099c 100644 --- a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/impl/CurrencyLoaderImpl.kt +++ b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/impl/CurrencyLoaderImpl.kt @@ -10,6 +10,7 @@ import co.nilin.opex.port.bcgateway.postgres.model.CurrencyModel import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.toList import kotlinx.coroutines.reactive.awaitFirstOrNull +import kotlinx.coroutines.reactive.awaitSingle import kotlinx.coroutines.reactive.awaitSingleOrNull import org.springframework.stereotype.Component @@ -48,8 +49,8 @@ class CurrencyLoaderImpl( val addressTypes = addressTypesDao.map { AddressType(it.id!!, it.type, it.addressRegex, it.memoRegex) } val endpointsDao = chainRepository.findEndpointsByName(implDao.chain) val endpoints = endpointsDao.map { Endpoint(it.url) } - val currencyDao2 = currencyDao ?: currencyRepository.findBySymbol(implDao.symbol).awaitSingleOrNull() - val currency = Currency(currencyDao2.symbol, currencyDao2.name) + val currencyDaoVal = currencyDao ?: currencyRepository.findBySymbol(implDao.symbol).awaitSingle() + val currency = Currency(currencyDaoVal.symbol, currencyDaoVal.name) return CurrencyImplementation( currency, Chain(implDao.chain, addressTypes.toList(), endpoints.toList()), From feb139970ede27922f14e945ea42971fafe082f5 Mon Sep 17 00:00:00 2001 From: metalicn20 Date: Sat, 11 Sep 2021 12:36:21 +0430 Subject: [PATCH 36/37] Revert fetchCurrencyInfo() return type changing --- .../opex/bcgateway/core/spi/CurrencyLoader.kt | 2 +- .../bcgateway/postgres/impl/CurrencyLoaderImpl.kt | 14 +++++--------- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/BlockchainGateway/bc-gateway-core/src/main/kotlin/co/nilin/opex/bcgateway/core/spi/CurrencyLoader.kt b/BlockchainGateway/bc-gateway-core/src/main/kotlin/co/nilin/opex/bcgateway/core/spi/CurrencyLoader.kt index 7256225bf..32aa395f1 100644 --- a/BlockchainGateway/bc-gateway-core/src/main/kotlin/co/nilin/opex/bcgateway/core/spi/CurrencyLoader.kt +++ b/BlockchainGateway/bc-gateway-core/src/main/kotlin/co/nilin/opex/bcgateway/core/spi/CurrencyLoader.kt @@ -4,7 +4,7 @@ import co.nilin.opex.bcgateway.core.model.CurrencyImplementation import co.nilin.opex.bcgateway.core.model.CurrencyInfo interface CurrencyLoader { - suspend fun fetchCurrencyInfo(symbol: String): CurrencyInfo? + suspend fun fetchCurrencyInfo(symbol: String): CurrencyInfo suspend fun findSymbol(chain: String, address: String?): String? suspend fun findImplementationsWithTokenOnChain(chain: String): List } \ No newline at end of file diff --git a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/impl/CurrencyLoaderImpl.kt b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/impl/CurrencyLoaderImpl.kt index 3aa6c099c..217c39803 100644 --- a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/impl/CurrencyLoaderImpl.kt +++ b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/impl/CurrencyLoaderImpl.kt @@ -20,16 +20,12 @@ class CurrencyLoaderImpl( private val currencyRepository: CurrencyRepository, private val currencyImplementationRepository: CurrencyImplementationRepository ) : CurrencyLoader { - override suspend fun fetchCurrencyInfo(symbol: String): CurrencyInfo? { + override suspend fun fetchCurrencyInfo(symbol: String): CurrencyInfo { val currencyDao = currencyRepository.findBySymbol(symbol).awaitSingleOrNull() - return if (currencyDao !== null) { - val currencyImplDao = currencyImplementationRepository.findBySymbol(symbol) - val currency = Currency(currencyDao.symbol, currencyDao.name) - val implementations = currencyImplDao.map { projectCurrencyImplementation(it, currencyDao) } - CurrencyInfo(currency, implementations.toList()) - } else { - null - } + val currencyImplDao = currencyImplementationRepository.findBySymbol(symbol) + val currency = Currency(currencyDao.symbol, currencyDao.name) + val implementations = currencyImplDao.map { projectCurrencyImplementation(it, currencyDao) } + return CurrencyInfo(currency, implementations.toList()) } override suspend fun findSymbol(chain: String, address: String?): String? { From d5a663b3a1019a83742d89bf3d525d259a887f92 Mon Sep 17 00:00:00 2001 From: metalicn20 Date: Sat, 11 Sep 2021 12:38:52 +0430 Subject: [PATCH 37/37] Handle not found currency --- .../opex/port/bcgateway/postgres/impl/CurrencyLoaderImpl.kt | 1 + 1 file changed, 1 insertion(+) diff --git a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/impl/CurrencyLoaderImpl.kt b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/impl/CurrencyLoaderImpl.kt index 217c39803..118329469 100644 --- a/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/impl/CurrencyLoaderImpl.kt +++ b/BlockchainGateway/bc-gateway-ports/bc-persister-postgres/src/main/kotlin/co/nilin/opex/port/bcgateway/postgres/impl/CurrencyLoaderImpl.kt @@ -22,6 +22,7 @@ class CurrencyLoaderImpl( ) : CurrencyLoader { override suspend fun fetchCurrencyInfo(symbol: String): CurrencyInfo { val currencyDao = currencyRepository.findBySymbol(symbol).awaitSingleOrNull() + if (currencyDao === null) return CurrencyInfo(Currency("", symbol), emptyList()) val currencyImplDao = currencyImplementationRepository.findBySymbol(symbol) val currency = Currency(currencyDao.symbol, currencyDao.name) val implementations = currencyImplDao.map { projectCurrencyImplementation(it, currencyDao) }