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
Expand Up @@ -355,9 +355,9 @@ fun Client.sendRemotely(
}


fun Client.requestFirstKinAirdrop(
suspend fun Client.requestFirstKinAirdrop(
owner: KeyPair,
): Single<KinAmount> {
): Result<KinAmount> {
return transactionRepository.requestFirstKinAirdrop(owner)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.filterNot
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.map
Expand Down Expand Up @@ -343,7 +344,7 @@ class TransactionRepository @Inject constructor(

// TODO: potentially make this more generic in the event we introduce more airdrop types
// that can be requested for
fun requestFirstKinAirdrop(owner: Ed25519.KeyPair): Single<KinAmount> {
suspend fun requestFirstKinAirdrop(owner: KeyPair): Result<KinAmount> {
val request = TransactionService.AirdropRequest.newBuilder()
.setOwner(owner.publicKeyBytes.toSolanaAccount())
.setAirdropType(TransactionService.AirdropType.GET_FIRST_KIN)
Expand All @@ -354,25 +355,30 @@ class TransactionRepository @Inject constructor(
}
.build()

return transactionApi.airdrop(request).flatMap {
when (it.result) {
TransactionService.AirdropResponse.Result.OK -> {
Single.just(KinAmount.fromProtoExchangeData(it.exchangeData))
?: Single.error(IllegalStateException())
}
return runCatching {
transactionApi.airdrop(request)
.toFlowable()
.asFlow()
.flowOn(Dispatchers.IO)
.map {
when (it.result) {
TransactionService.AirdropResponse.Result.OK -> {
KinAmount.fromProtoExchangeData(it.exchangeData)
}

TransactionService.AirdropResponse.Result.ALREADY_CLAIMED -> {
Single.error(AirdropException.AlreadyClaimedException())
}
TransactionService.AirdropResponse.Result.ALREADY_CLAIMED -> {
throw AirdropException.AlreadyClaimedException()
}

TransactionService.AirdropResponse.Result.UNAVAILABLE -> {
Single.error(AirdropException.UnavailableException())
}
TransactionService.AirdropResponse.Result.UNAVAILABLE -> {
throw AirdropException.UnavailableException()
}

else -> {
Single.error(AirdropException.UnknownException())
}
}
else -> {
throw AirdropException.UnknownException()
}
}
}.first()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.getcode.model.PrefsBool
import com.getcode.network.BalanceController
import com.getcode.network.client.Client
import com.getcode.network.client.fetchPaymentHistoryDelta
import com.getcode.network.client.receiveFromPrimaryIfWithinLimits
import com.getcode.network.client.requestFirstKinAirdrop
import com.getcode.network.repository.PrefRepository
import com.getcode.network.repository.TransactionRepository
Expand All @@ -33,6 +34,7 @@ import kotlinx.coroutines.reactive.asFlow
import java.util.concurrent.TimeUnit
import javax.inject.Inject
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.Duration.Companion.seconds

@HiltViewModel
class GetKinSheetViewModel @Inject constructor(
Expand Down Expand Up @@ -96,17 +98,16 @@ class GetKinSheetViewModel @Inject constructor(

SessionManager.getKeyPair()
}.onEach { dispatchEvent(Event.OnLoadingChanged(true)) }
.flatMapLatest { owner ->
client.requestFirstKinAirdrop(owner)
.subscribeOn(Schedulers.computation())
.delay(1, TimeUnit.SECONDS)
.toFlowable().asFlow()
}
.catchSafely(
action = { amount ->
action = { owner ->
delay(1.seconds)
val amount = client.requestFirstKinAirdrop(owner).getOrThrow()

dispatchEvent(Event.OnGetEligibilityChanged(eligible = false, fromEvent = true))
dispatchEvent(Event.OnLoadingChanged(false))
dispatchEvent(Event.OnKinRequestSuccessful(amount))

balanceController.fetchBalanceSuspend()
},
onFailure = {
if (it is TransactionRepository.AirdropException.AlreadyClaimedException) {
Expand All @@ -121,11 +122,12 @@ class GetKinSheetViewModel @Inject constructor(
}
)
.flatMapLatest {
Completable.concatArray(
balanceController.fetchBalance(),
client.fetchPaymentHistoryDelta(owner = SessionManager.getKeyPair()!!)
.ignoreElement()
).toFlowable<Any>().asFlow()
val organizer = SessionManager.getOrganizer()
val receiveWithinLimits = organizer?.let {
client.receiveFromPrimaryIfWithinLimits(it)
} ?: Completable.complete()

receiveWithinLimits.toFlowable<Any>().asFlow()
}
.launchIn(viewModelScope)

Expand Down