|
1 | | -package co.nilin.opex.bcgateway.core.service |
2 | | - |
3 | | -import co.nilin.opex.bcgateway.core.api.WalletSyncService |
4 | | -import co.nilin.opex.bcgateway.core.model.CurrencyImplementation |
5 | | -import co.nilin.opex.bcgateway.core.model.Deposit |
6 | | -import co.nilin.opex.bcgateway.core.model.WalletSyncRecord |
7 | | -import co.nilin.opex.bcgateway.core.spi.* |
8 | | -import kotlinx.coroutines.ExecutorCoroutineDispatcher |
9 | | -import kotlinx.coroutines.async |
10 | | -import kotlinx.coroutines.awaitAll |
11 | | -import kotlinx.coroutines.withContext |
12 | | -import org.slf4j.LoggerFactory |
13 | | -import java.math.BigDecimal |
14 | | -import java.time.LocalDateTime |
15 | | -import java.time.temporal.ChronoUnit |
16 | | -import kotlin.coroutines.coroutineContext |
17 | | - |
18 | | -class WalletSyncServiceImpl( |
19 | | - private val syncSchedulerHandler: WalletSyncSchedulerHandler, |
20 | | - private val walletProxy: WalletProxy, |
21 | | - private val walletSyncRecordHandler: WalletSyncRecordHandler, |
22 | | - private val assignedAddressHandler: AssignedAddressHandler, |
23 | | - private val currencyHandler: CurrencyHandler, |
24 | | - private val dispatcher: ExecutorCoroutineDispatcher |
25 | | -) : WalletSyncService { |
26 | | - |
27 | | - private val logger = LoggerFactory.getLogger(ChainSyncServiceImpl::class.java) |
28 | | - |
29 | | - override suspend fun startSyncWithWallet() { |
30 | | - withContext(coroutineContext) { |
31 | | - val schedule = syncSchedulerHandler.fetchActiveSchedule(LocalDateTime.now()) |
32 | | - if (schedule != null) { |
33 | | - val deposits = walletSyncRecordHandler.findReadyToSyncTransfers(schedule.batchSize) |
34 | | - logger.info("syncing ${deposits.size} deposits") |
35 | | - |
36 | | - val result = deposits.map { deposit -> |
37 | | - async(dispatcher) { |
38 | | - var deposited = false |
39 | | - val uuid = assignedAddressHandler.findUuid( |
40 | | - deposit.depositor, |
41 | | - deposit.depositorMemo?.lowercase() |
42 | | - ) |
43 | | - if (uuid != null) { |
44 | | - logger.info("deposit came for $uuid - to ${deposit.depositor}") |
45 | | - val symbol = currencyHandler.findByChainAndTokenAddress(deposit.chain, deposit.tokenAddress) |
46 | | - if (symbol != null) { |
47 | | - sendDeposit(uuid, symbol, deposit) |
48 | | - deposited = true |
49 | | - } |
50 | | - } |
51 | | - Pair(deposit, deposited) |
52 | | - } |
53 | | - }.awaitAll() |
54 | | - |
55 | | - walletSyncRecordHandler.saveWalletSyncRecord( |
56 | | - WalletSyncRecord( |
57 | | - LocalDateTime.now(), |
58 | | - true, |
59 | | - null |
60 | | - ), |
61 | | - result.filter { it.second }.map { it.first }, |
62 | | - result.filter { !it.second }.map { it.first } |
63 | | - ) |
64 | | - |
65 | | - syncSchedulerHandler.prepareScheduleForNextTry( |
66 | | - schedule, LocalDateTime.now() |
67 | | - .plus(schedule.delay, ChronoUnit.SECONDS) |
68 | | - ) |
69 | | - } |
70 | | - } |
71 | | - } |
72 | | - |
73 | | - private suspend fun sendDeposit(uuid: String, currencyImpl: CurrencyImplementation, deposit: Deposit) { |
74 | | - val amount = deposit.amount.divide(BigDecimal(10).pow(currencyImpl.decimal)) |
75 | | - val symbol = currencyImpl.currency.symbol |
76 | | - logger.info("sending deposit to $uuid - $amount $symbol") |
77 | | - walletProxy.transfer(uuid, symbol, amount, deposit.hash) |
78 | | - } |
79 | | -} |
| 1 | +package co.nilin.opex.bcgateway.core.service |
| 2 | + |
| 3 | +import co.nilin.opex.bcgateway.core.api.WalletSyncService |
| 4 | +import co.nilin.opex.bcgateway.core.model.CurrencyImplementation |
| 5 | +import co.nilin.opex.bcgateway.core.model.Deposit |
| 6 | +import co.nilin.opex.bcgateway.core.model.WalletSyncRecord |
| 7 | +import co.nilin.opex.bcgateway.core.spi.* |
| 8 | +import kotlinx.coroutines.ExecutorCoroutineDispatcher |
| 9 | +import kotlinx.coroutines.async |
| 10 | +import kotlinx.coroutines.awaitAll |
| 11 | +import kotlinx.coroutines.withContext |
| 12 | +import org.slf4j.LoggerFactory |
| 13 | +import java.math.BigDecimal |
| 14 | +import java.time.LocalDateTime |
| 15 | +import java.time.temporal.ChronoUnit |
| 16 | +import kotlin.coroutines.coroutineContext |
| 17 | + |
| 18 | +class WalletSyncServiceImpl( |
| 19 | + private val syncSchedulerHandler: WalletSyncSchedulerHandler, |
| 20 | + private val walletProxy: WalletProxy, |
| 21 | + private val walletSyncRecordHandler: WalletSyncRecordHandler, |
| 22 | + private val assignedAddressHandler: AssignedAddressHandler, |
| 23 | + private val currencyHandler: CurrencyHandler, |
| 24 | + private val dispatcher: ExecutorCoroutineDispatcher |
| 25 | +) : WalletSyncService { |
| 26 | + |
| 27 | + private val logger = LoggerFactory.getLogger(ChainSyncServiceImpl::class.java) |
| 28 | + |
| 29 | + override suspend fun startSyncWithWallet() { |
| 30 | + withContext(coroutineContext) { |
| 31 | + val schedule = syncSchedulerHandler.fetchActiveSchedule(LocalDateTime.now()) |
| 32 | + if (schedule != null) { |
| 33 | + val deposits = walletSyncRecordHandler.findReadyToSyncTransfers(schedule.batchSize) |
| 34 | + logger.info("syncing ${deposits.size} deposits") |
| 35 | + |
| 36 | + val result = deposits.map { deposit -> |
| 37 | + async(dispatcher) { |
| 38 | + var deposited = false |
| 39 | + val uuid = assignedAddressHandler.findUuid( |
| 40 | + deposit.depositor, |
| 41 | + deposit.depositorMemo |
| 42 | + ) |
| 43 | + if (uuid != null) { |
| 44 | + logger.info("deposit came for $uuid - to ${deposit.depositor}") |
| 45 | + val symbol = currencyHandler.findByChainAndTokenAddress(deposit.chain, deposit.tokenAddress) |
| 46 | + if (symbol != null) { |
| 47 | + sendDeposit(uuid, symbol, deposit) |
| 48 | + deposited = true |
| 49 | + } |
| 50 | + } |
| 51 | + Pair(deposit, deposited) |
| 52 | + } |
| 53 | + }.awaitAll() |
| 54 | + |
| 55 | + walletSyncRecordHandler.saveWalletSyncRecord( |
| 56 | + WalletSyncRecord( |
| 57 | + LocalDateTime.now(), |
| 58 | + true, |
| 59 | + null |
| 60 | + ), |
| 61 | + result.filter { it.second }.map { it.first }, |
| 62 | + result.filter { !it.second }.map { it.first } |
| 63 | + ) |
| 64 | + |
| 65 | + syncSchedulerHandler.prepareScheduleForNextTry( |
| 66 | + schedule, LocalDateTime.now() |
| 67 | + .plus(schedule.delay, ChronoUnit.SECONDS) |
| 68 | + ) |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + private suspend fun sendDeposit(uuid: String, currencyImpl: CurrencyImplementation, deposit: Deposit) { |
| 74 | + val amount = deposit.amount.divide(BigDecimal(10).pow(currencyImpl.decimal)) |
| 75 | + val symbol = currencyImpl.currency.symbol |
| 76 | + logger.info("sending deposit to $uuid - $amount $symbol") |
| 77 | + walletProxy.transfer(uuid, symbol, amount, deposit.hash) |
| 78 | + } |
| 79 | +} |
0 commit comments