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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package co.nilin.opex.bcgateway.app.controller

import co.nilin.opex.bcgateway.core.model.CurrencyInfo
import co.nilin.opex.bcgateway.core.spi.CurrencyLoader
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RestController

@RestController
class NetworkController(val currencyLoader: CurrencyLoader) {

@GetMapping("currency/{currency}")
suspend fun fetchCurrencyInfo(@PathVariable("currency") currency: String): CurrencyInfo {
return currencyLoader.fetchCurrencyInfo(currency)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package co.nilin.opex.bcgateway.core.model

data class AddressType(val id: Long, val type: String, val addressRegex: String, val memoRegex: String)
data class AddressType(val id: Long, val type: String, val addressRegex: String, val memoRegex: String?)
data class ReservedAddress(val address: String, val memo: String?, val type: AddressType)
data class AssignedAddress(
val uuid: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package co.nilin.opex.port.bcgateway.postgres.config

import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Profile
import org.springframework.r2dbc.core.DatabaseClient

@Configuration
@Profile("demo")
class DemoPostgresConfig(db: DatabaseClient) {

init {
val initDb = db.sql {
"""
insert into currency values ('BTC', 'Bitcoin') ON CONFLICT DO NOTHING;
insert into chains values ('Bit') ON CONFLICT DO NOTHING;
insert into chains values ('Bsc') ON CONFLICT DO NOTHING;
insert into address_types(id, address_type, address_regex) values (1, 'BTC', '.*') ON CONFLICT DO NOTHING;
insert into address_types(id, address_type, address_regex) values (2, 'ETH', '.*') ON CONFLICT DO NOTHING;
insert into chain_address_types (chain_name, addr_type_id) values ('Bit', 1) ON CONFLICT DO NOTHING;
insert into chain_address_types (chain_name, addr_type_id) values ('Bsc', 2) ON CONFLICT DO NOTHING;
insert into currency_implementations (
id,
symbol,
chain,
token,
token_address,
token_name,
withdraw_enabled,
withdraw_fee,
withdraw_min
)values(1, 'BTC', 'Bit', false, null, null, true, 0.0001, 0.0001)
,(2, 'BTC', 'Bsc', true, '0x1111', 'WBTC', true, 0.00001, 0.000001) ON CONFLICT DO NOTHING;
"""
}
initDb // initialize the database
.then()
.subscribe() // execute
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ class PostgresConfig(db: DatabaseClient) {
"""
CREATE TABLE IF NOT EXISTS address_types (
id SERIAL PRIMARY KEY,
address_type VARCHAR(72) NOT NULL,
address_type VARCHAR(20) NOT NULL,
address_regex VARCHAR(72) NOT NULL,
memo_regex VARCHAR(72) NOT NULL
memo_regex VARCHAR(72)
);
CREATE TABLE IF NOT EXISTS assigned_addresses (
id SERIAL PRIMARY KEY,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@ class CurrencyLoaderImpl(
private val currencyImplementationRepository: CurrencyImplementationRepository
) : 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) }
val symbol = symbol.toUpperCase()
val currencyModel = currencyRepository.findBySymbol(symbol).awaitSingleOrNull()
if (currencyModel === null) {
return CurrencyInfo(Currency("", symbol), emptyList())
}
val currencyImplModel = currencyImplementationRepository.findBySymbol(symbol)
val currency = Currency(currencyModel.symbol, currencyModel.name)
val implementations = currencyImplModel.map { projectCurrencyImplementation(it, currencyModel) }
return CurrencyInfo(currency, implementations.toList())
}

Expand All @@ -39,24 +42,24 @@ class CurrencyLoaderImpl(
}

private suspend fun projectCurrencyImplementation(
implDao: CurrencyImplementationModel,
currencyDao: CurrencyModel? = null
currencyImplementationModel: CurrencyImplementationModel,
currencyModel: 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 currencyDaoVal = currencyDao ?: currencyRepository.findBySymbol(implDao.symbol).awaitSingle()
val currency = Currency(currencyDaoVal.symbol, currencyDaoVal.name)
val addressTypesModel = chainRepository.findAddressTypesByName(currencyImplementationModel.chain)
val addressTypes = addressTypesModel.map { AddressType(it.id!!, it.type, it.addressRegex, it.memoRegex) }
val endpointsModel = chainRepository.findEndpointsByName(currencyImplementationModel.chain)
val endpoints = endpointsModel.map { Endpoint(it.url) }
val currencyModelVal = currencyModel ?: currencyRepository.findBySymbol(currencyImplementationModel.symbol).awaitSingle()
val currency = Currency(currencyModelVal.symbol, currencyModelVal.name)
return CurrencyImplementation(
currency,
Chain(implDao.chain, addressTypes.toList(), endpoints.toList()),
implDao.token,
implDao.tokenAddress,
implDao.tokenName,
implDao.withdrawEnabled,
implDao.withdrawFee,
implDao.withdrawMin
Chain(currencyImplementationModel.chain, addressTypes.toList(), endpoints.toList()),
currencyImplementationModel.token,
currencyImplementationModel.tokenAddress,
currencyImplementationModel.tokenName,
currencyImplementationModel.withdrawEnabled,
currencyImplementationModel.withdrawFee,
currencyImplementationModel.withdrawMin
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ data class AddressTypeModel(
@Id val id: Long?,
@Column("address_type") val type: String,
@Column("address_regex") val addressRegex: String,
@Column("memo_regex") val memoRegex: String
@Column("memo_regex") val memoRegex: String?
)
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,36 @@

import java.util.*;


public class FormDataWorkaroundFilter implements WebFilter {

@Override
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
final ServerHttpRequest request = exchange.getRequest();

final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<>();

//add all content from form data to query params
exchange.getFormData().subscribe(queryParams::putAll);

exchange.getMultipartData().subscribe(map -> {
map.forEach((key, value) -> {
List<?> list = value;
list.forEach(item -> {
//add each form field parts to query params
if (item instanceof FormFieldPart) {
final FormFieldPart formFieldPart = (FormFieldPart) item;
queryParams.add(key, formFieldPart.value());
}
});

});
});

//add original query params to win identical name war
queryParams.putAll(request.getQueryParams());

return chain.filter(new FormDataServerWebExchangeDecorator(queryParams, exchange));
}

@Override
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
final ServerHttpRequest request = exchange.getRequest();

final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<>();

return exchange.getFormData()
.doOnNext(queryParams::putAll)
.flatMap( it ->
exchange.getMultipartData().map(map -> {
map.forEach((key, value) -> {
value.forEach(item -> {
//add each form field parts to query params
if (item instanceof FormFieldPart) {
final FormFieldPart formFieldPart = (FormFieldPart) item;
queryParams.add(key, formFieldPart.value());
}
});

});
return map;
})
)
.doOnNext(it ->
queryParams.putAll(request.getQueryParams()))
.flatMap(it ->
chain.filter(new FormDataServerWebExchangeDecorator(queryParams, exchange))
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class TransferController(
Amount(sourceWallet.currency(), amount),
description, transferRef, emptyMap()
)
)
).transferResult
}

@PostMapping("/deposit/{amount}_{symbol}/{receiverUuid}_{receiverWalletType}")
Expand Down Expand Up @@ -122,6 +122,6 @@ class TransferController(
Amount(sourceWallet.currency(), amount),
description, transferRef, emptyMap()
)
)
).transferResult
}
}
Loading