|
1 | 1 | package com.getcode.opencode.internal.network.services |
2 | 2 |
|
3 | 3 | import com.codeinc.opencode.gen.balance.v1.OcpBalanceService |
| 4 | +import com.getcode.opencode.internal.domain.mapping.OwnerBalanceMapper |
4 | 5 | import com.getcode.opencode.internal.network.api.BalanceApi |
5 | 6 | import com.getcode.opencode.internal.network.extensions.foldWithSuppression |
6 | | -import com.getcode.opencode.model.core.errors.GetBalanceError |
7 | | -import com.getcode.opencode.model.financial.CurrencyCode |
8 | | -import com.getcode.opencode.model.financial.Fiat |
| 7 | +import com.getcode.opencode.model.core.errors.GetBalancesError |
| 8 | +import com.getcode.opencode.model.financial.OwnerBalance |
9 | 9 | import com.getcode.opencode.utils.toValidationOrElse |
10 | 10 | import com.getcode.solana.keys.PublicKey |
11 | 11 | import javax.inject.Inject |
12 | 12 |
|
13 | 13 | internal class BalanceService @Inject constructor( |
14 | 14 | private val api: BalanceApi, |
| 15 | + private val ownerBalanceMapper: OwnerBalanceMapper, |
15 | 16 | ) { |
16 | | - suspend fun getBalance(owner: PublicKey): Result<Fiat> { |
| 17 | + suspend fun getBalances( |
| 18 | + owners: List<PublicKey>, |
| 19 | + mints: List<PublicKey> = emptyList(), |
| 20 | + ): Result<List<OwnerBalance>> { |
17 | 21 | return runCatching { |
18 | | - api.getBalance(owner) |
| 22 | + api.getBalances(owners, mints) |
19 | 23 | }.foldWithSuppression( |
20 | 24 | onSuccess = { response -> |
21 | 25 | when (response.result) { |
22 | | - OcpBalanceService.GetBalanceResponse.Result.OK -> Result.success( |
23 | | - Fiat(quarks = response.coreMintValue, currencyCode = CurrencyCode.USD) |
| 26 | + OcpBalanceService.GetBalancesResponse.Result.OK -> Result.success( |
| 27 | + response.balancesByOwnerMap.values.map { ownerBalanceMapper.map(it) } |
24 | 28 | ) |
25 | | - OcpBalanceService.GetBalanceResponse.Result.DENIED -> Result.failure( |
26 | | - GetBalanceError.Denied()) |
27 | | - OcpBalanceService.GetBalanceResponse.Result.NOT_FOUND -> Result.failure( |
28 | | - GetBalanceError.NotFound()) |
29 | | - OcpBalanceService.GetBalanceResponse.Result.UNRECOGNIZED -> Result.failure( |
30 | | - GetBalanceError.Unrecognized()) |
31 | | - else -> Result.failure(GetBalanceError.Other()) |
| 29 | + OcpBalanceService.GetBalancesResponse.Result.DENIED -> Result.failure( |
| 30 | + GetBalancesError.Denied()) |
| 31 | + OcpBalanceService.GetBalancesResponse.Result.UNRECOGNIZED -> Result.failure( |
| 32 | + GetBalancesError.Unrecognized()) |
| 33 | + else -> Result.failure(GetBalancesError.Other()) |
32 | 34 | } |
33 | 35 | }, |
34 | 36 | onFailure = { cause -> |
35 | | - Result.failure(cause.toValidationOrElse { GetBalanceError.Other(cause = it) }) |
| 37 | + Result.failure(cause.toValidationOrElse { GetBalancesError.Other(cause = it) }) |
36 | 38 | } |
37 | 39 | ) |
38 | 40 | } |
|
0 commit comments