Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions BlockchainGateway/bc-gateway-app/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@
<artifactId>bc-gateway-persister-postgres</artifactId>
<version>${bc-gateway.version}</version>
</dependency>
<dependency>
<groupId>co.nilin.opex.external</groupId>
<artifactId>wallet-proxy</artifactId>
<version>${bc-gateway.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ spring:

app:
auth:
cert-url: lb://opex-auth/auth/realms/opex/protocol/openid-connect/certs
cert-url: lb://opex-auth/auth/realms/opex/protocol/openid-connect/certs
wallet:
url: lb://wallet
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ package co.nilin.opex.bcgateway.core.spi
import java.math.BigDecimal

interface WalletProxy {
fun transfer(uuid: String, symbol: String, amount: BigDecimal)
}
suspend fun transfer(uuid: String, symbol: String, amount: BigDecimal)
}
36 changes: 36 additions & 0 deletions BlockchainGateway/bc-gateway-ports/wallet-proxy/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
HELP.md
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/

### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/

### VS Code ###
.vscode/
!/.mvn/

.DS_Store
100 changes: 100 additions & 0 deletions BlockchainGateway/bc-gateway-ports/wallet-proxy/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<modelVersion>4.0.0</modelVersion>

<groupId>co.nilin.opex.external</groupId>
<artifactId>wallet-proxy</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<java.version>1.8</java.version>
<kotlin.version>1.4.31</kotlin.version>
<bc-gateway.version>${version}</bc-gateway.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
<groupId>io.projectreactor.kotlin</groupId>
<artifactId>reactor-kotlin-extensions</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlinx</groupId>
<artifactId>kotlinx-coroutines-reactor</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlinx</groupId>
<artifactId>kotlinx-coroutines-core</artifactId>
</dependency>
<dependency>
<groupId>co.nilin.opex.external</groupId>
<artifactId>bc-gateway-core</artifactId>
<version>${bc-gateway.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webflux</artifactId>
</dependency>
<dependency>
<groupId>co.nilin.opex</groupId>
<artifactId>wallet-core</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>

<build>
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<configuration>
<args>
<arg>-Xjsr305=strict</arg>
</args>
<compilerPlugins>
<plugin>spring</plugin>
</compilerPlugins>
</configuration>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-allopen</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>

<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package co.nilin.opex.port.bcgateway.walletproxy.impl

import co.nilin.opex.bcgateway.core.spi.WalletProxy
import co.nilin.opex.wallet.core.inout.TransferResult
import kotlinx.coroutines.reactive.awaitFirst
import org.springframework.beans.factory.annotation.Value
import org.springframework.core.ParameterizedTypeReference
import org.springframework.stereotype.Component
import org.springframework.web.reactive.function.client.WebClient
import java.math.BigDecimal
import java.net.URI

inline fun <reified T : Any> typeRef(): ParameterizedTypeReference<T> = object : ParameterizedTypeReference<T>() {}

@Component
class WalletProxyImpl(private val webClient: WebClient) : WalletProxy {
@Value("\${app.wallet.url}")
private lateinit var baseUrl: String

override suspend fun transfer(uuid: String, symbol: String, amount: BigDecimal) {
webClient.post()
.uri(URI.create("$baseUrl/deposit/${amount}_$symbol/${uuid}_main"))
.header("Content-Type", "application/json")
.retrieve()
.onStatus({ t -> t.isError }, { it.createException() })
.bodyToMono(typeRef<TransferResult>())
.log()
.awaitFirst()
}
}
29 changes: 15 additions & 14 deletions BlockchainGateway/pom.xml
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>co.nilin.opex.external</groupId>
<artifactId>bc-gateway</artifactId>
<version>1.0-SNAPSHOT</version>
<name>bc-gateway</name>
<packaging>pom</packaging>
<description>Blockchain gateway root of opex</description>
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>co.nilin.opex.external</groupId>
<artifactId>bc-gateway</artifactId>
<version>1.0-SNAPSHOT</version>
<name>bc-gateway</name>
<packaging>pom</packaging>
<description>Blockchain gateway root of opex</description>

<modules>
<module>bc-gateway-core</module>
<module>bc-gateway-app</module>
<module>bc-gateway-ports/bc-persister-postgres</module>
<module>bc-gateway-ports/bc-chain-proxy</module>
</modules>
<modules>
<module>bc-gateway-core</module>
<module>bc-gateway-app</module>
<module>bc-gateway-ports/bc-persister-postgres</module>
<module>bc-gateway-ports/bc-chain-proxy</module>
<module>bc-gateway-ports/wallet-proxy</module>
</modules>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ import java.math.BigDecimal

@RestController
class TransferController(
val transferService: TransferService
, val currencyService: CurrencyService
, val walletManager: WalletManager
, val walletOwnerManager: WalletOwnerManager
val transferService: TransferService,
val currencyService: CurrencyService,
val walletManager: WalletManager,
val walletOwnerManager: WalletOwnerManager
) {
@PostMapping("/transfer/{amount}_{symbol}/from/{senderUuid}_{senderWalletType}/to/{receiverUuid}_{receiverWalletType}")
@ApiResponse(
Expand All @@ -45,8 +45,7 @@ class TransferController(
@PathVariable("description") description: String?,
@PathVariable("transferRef") transferRef: String?
): TransferResult {
if ( senderWalletType.equals("cashout")
|| receiverWalletType.equals("cashout") )
if (senderWalletType == "cashout" || receiverWalletType == "cashout")
throw IllegalArgumentException("Use withdraw services")
val currency = currencyService.getCurrency(symbol)
val sourceOwner = walletOwnerManager.findWalletOwner(senderUuid) ?: throw IllegalArgumentException()
Expand Down Expand Up @@ -75,4 +74,53 @@ class TransferController(
)
)
}
}

@PostMapping("/deposit/{amount}_{symbol}/{receiverUuid}_{receiverWalletType}")
@ApiResponse(
message = "OK",
code = 200,
examples = Example(
ExampleProperty(
value = "{ }",
mediaType = "application/json"
)
)
)
suspend fun deposit(
@PathVariable("symbol") symbol: String,
@PathVariable("receiverUuid") receiverUuid: String,
@PathVariable("receiverWalletType") receiverWalletType: String,
@PathVariable("amount") amount: BigDecimal,
@PathVariable("description") description: String?,
@PathVariable("transferRef") transferRef: String?
): TransferResult {
if (receiverWalletType == "cashout") throw IllegalArgumentException("Use withdraw services")
val systemUuid = "1"
val currency = currencyService.getCurrency(symbol)
val sourceOwner = walletOwnerManager.findWalletOwner(systemUuid) ?: throw IllegalArgumentException()
val sourceWallet =
walletManager.findWalletByOwnerAndCurrencyAndType(sourceOwner, "main", currency)
?: throw IllegalArgumentException()
val receiverOwner = walletOwnerManager.findWalletOwner(receiverUuid) ?: walletOwnerManager.createWalletOwner(
systemUuid,
"not set",
""
)
val receiverWallet = walletManager.findWalletByOwnerAndCurrencyAndType(
receiverOwner, receiverWalletType, currency
) ?: walletManager.createWallet(
receiverOwner,
Amount(currency, BigDecimal.ZERO),
currency,
receiverWalletType
)
return transferService.transfer(
TransferCommand(
sourceWallet,
receiverWallet,
Amount(sourceWallet.currency(), amount),
description, transferRef, emptyMap()
)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ import java.security.Principal

@RestController
class WithdrawController(
val withdrawRepository: WithdrawRepository
, val transferService: TransferService
, val walletManager: WalletManager
, val walletOwnerManager: WalletOwnerManager
, val currencyService: CurrencyService
, @Value("\${app.system.uuid}") val systemUuid: String
val withdrawRepository: WithdrawRepository,
val transferService: TransferService,
val walletManager: WalletManager,
val walletOwnerManager: WalletOwnerManager,
val currencyService: CurrencyService,
@Value("\${app.system.uuid}") val systemUuid: String
) {

@GetMapping("/admin/withdraw")
Expand Down Expand Up @@ -73,13 +73,18 @@ class WithdrawController(
@RequestParam("status", required = false) status: List<String>?
): List<WithdrawModel> {
return withdrawRepository
.findByCriteria(principal.name, txRef, destTxRef, destAddress, status?.isEmpty() ?: true, status ?: listOf(""))
.findByCriteria(
principal.name,
txRef,
destTxRef,
destAddress,
status?.isEmpty() ?: true,
status ?: listOf("")
)
.toList()
}

@PostMapping(
"/withdraw/{amount}_{symbol}"
)
@PostMapping("/withdraw/{amount}_{symbol}")
@ApiResponse(
message = "OK",
code = 200,
Expand Down Expand Up @@ -148,7 +153,8 @@ class WithdrawController(
@RequestParam("statusReason", required = false) statusReason: String?,
@RequestParam("destNote", required = false) destNote: String?
): TransferResult {
val withdraw = withdrawRepository.findById(withdrawId).awaitFirstOrElse { throw RuntimeException("No matching withdraw request") }
val withdraw = withdrawRepository.findById(withdrawId)
.awaitFirstOrElse { throw RuntimeException("No matching withdraw request") }
val sourceWallet = walletManager.findWalletById(withdraw.wallet) ?: throw RuntimeException("Wallet not found")
val receiverWallet = walletManager.findWalletByOwnerAndCurrencyAndType(
sourceWallet.owner(), "main", sourceWallet.currency()
Expand All @@ -173,9 +179,7 @@ class WithdrawController(
)
}

@PostMapping(
"/admin/withdraw/{id}/accept"
)
@PostMapping("/admin/withdraw/{id}/accept")
@ApiResponse(
message = "OK",
code = 200,
Expand All @@ -192,7 +196,8 @@ class WithdrawController(
@RequestParam("destNote", required = false) destNote: String?
): TransferResult {
val system = walletOwnerManager.findWalletOwner(systemUuid) ?: throw IllegalArgumentException()
val withdraw = withdrawRepository.findById(withdrawId).awaitFirstOrElse { throw RuntimeException("No matching withdraw request") }
val withdraw = withdrawRepository.findById(withdrawId)
.awaitFirstOrElse { throw RuntimeException("No matching withdraw request") }
val sourceWallet = walletManager.findWalletById(withdraw.wallet) ?: throw RuntimeException("Wallet not found")
val receiverWallet = walletManager.findWalletByOwnerAndCurrencyAndType(
system, "main", sourceWallet.currency()
Expand All @@ -216,4 +221,4 @@ class WithdrawController(
)
)
}
}
}
Loading