Skip to content

Commit b7e1802

Browse files
authored
Merge pull request #137 from code-payments/fix/gives-after-first-kin-airdrop
fix(airdrop): receiveFromPrimary after receiving first airdrop to all…
2 parents 38f70a1 + d852e2c commit b7e1802

3 files changed

Lines changed: 39 additions & 31 deletions

File tree

api/src/main/java/com/getcode/network/client/Client_Transaction.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,9 +355,9 @@ fun Client.sendRemotely(
355355
}
356356

357357

358-
fun Client.requestFirstKinAirdrop(
358+
suspend fun Client.requestFirstKinAirdrop(
359359
owner: KeyPair,
360-
): Single<KinAmount> {
360+
): Result<KinAmount> {
361361
return transactionRepository.requestFirstKinAirdrop(owner)
362362
}
363363

api/src/main/java/com/getcode/network/repository/TransactionRepository.kt

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import kotlinx.coroutines.flow.MutableStateFlow
4545
import kotlinx.coroutines.flow.StateFlow
4646
import kotlinx.coroutines.flow.asStateFlow
4747
import kotlinx.coroutines.flow.filterNot
48+
import kotlinx.coroutines.flow.first
4849
import kotlinx.coroutines.flow.firstOrNull
4950
import kotlinx.coroutines.flow.flowOn
5051
import kotlinx.coroutines.flow.map
@@ -343,7 +344,7 @@ class TransactionRepository @Inject constructor(
343344

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

357-
return transactionApi.airdrop(request).flatMap {
358-
when (it.result) {
359-
TransactionService.AirdropResponse.Result.OK -> {
360-
Single.just(KinAmount.fromProtoExchangeData(it.exchangeData))
361-
?: Single.error(IllegalStateException())
362-
}
358+
return runCatching {
359+
transactionApi.airdrop(request)
360+
.toFlowable()
361+
.asFlow()
362+
.flowOn(Dispatchers.IO)
363+
.map {
364+
when (it.result) {
365+
TransactionService.AirdropResponse.Result.OK -> {
366+
KinAmount.fromProtoExchangeData(it.exchangeData)
367+
}
363368

364-
TransactionService.AirdropResponse.Result.ALREADY_CLAIMED -> {
365-
Single.error(AirdropException.AlreadyClaimedException())
366-
}
369+
TransactionService.AirdropResponse.Result.ALREADY_CLAIMED -> {
370+
throw AirdropException.AlreadyClaimedException()
371+
}
367372

368-
TransactionService.AirdropResponse.Result.UNAVAILABLE -> {
369-
Single.error(AirdropException.UnavailableException())
370-
}
373+
TransactionService.AirdropResponse.Result.UNAVAILABLE -> {
374+
throw AirdropException.UnavailableException()
375+
}
371376

372-
else -> {
373-
Single.error(AirdropException.UnknownException())
374-
}
375-
}
377+
else -> {
378+
throw AirdropException.UnknownException()
379+
}
380+
}
381+
}.first()
376382
}
377383
}
378384

app/src/main/java/com/getcode/view/main/getKin/GetKinSheetViewModel.kt

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import com.getcode.model.PrefsBool
99
import com.getcode.network.BalanceController
1010
import com.getcode.network.client.Client
1111
import com.getcode.network.client.fetchPaymentHistoryDelta
12+
import com.getcode.network.client.receiveFromPrimaryIfWithinLimits
1213
import com.getcode.network.client.requestFirstKinAirdrop
1314
import com.getcode.network.repository.PrefRepository
1415
import com.getcode.network.repository.TransactionRepository
@@ -33,6 +34,7 @@ import kotlinx.coroutines.reactive.asFlow
3334
import java.util.concurrent.TimeUnit
3435
import javax.inject.Inject
3536
import kotlin.time.Duration.Companion.milliseconds
37+
import kotlin.time.Duration.Companion.seconds
3638

3739
@HiltViewModel
3840
class GetKinSheetViewModel @Inject constructor(
@@ -96,17 +98,16 @@ class GetKinSheetViewModel @Inject constructor(
9698

9799
SessionManager.getKeyPair()
98100
}.onEach { dispatchEvent(Event.OnLoadingChanged(true)) }
99-
.flatMapLatest { owner ->
100-
client.requestFirstKinAirdrop(owner)
101-
.subscribeOn(Schedulers.computation())
102-
.delay(1, TimeUnit.SECONDS)
103-
.toFlowable().asFlow()
104-
}
105101
.catchSafely(
106-
action = { amount ->
102+
action = { owner ->
103+
delay(1.seconds)
104+
val amount = client.requestFirstKinAirdrop(owner).getOrThrow()
105+
107106
dispatchEvent(Event.OnGetEligibilityChanged(eligible = false, fromEvent = true))
108107
dispatchEvent(Event.OnLoadingChanged(false))
109108
dispatchEvent(Event.OnKinRequestSuccessful(amount))
109+
110+
balanceController.fetchBalanceSuspend()
110111
},
111112
onFailure = {
112113
if (it is TransactionRepository.AirdropException.AlreadyClaimedException) {
@@ -121,11 +122,12 @@ class GetKinSheetViewModel @Inject constructor(
121122
}
122123
)
123124
.flatMapLatest {
124-
Completable.concatArray(
125-
balanceController.fetchBalance(),
126-
client.fetchPaymentHistoryDelta(owner = SessionManager.getKeyPair()!!)
127-
.ignoreElement()
128-
).toFlowable<Any>().asFlow()
125+
val organizer = SessionManager.getOrganizer()
126+
val receiveWithinLimits = organizer?.let {
127+
client.receiveFromPrimaryIfWithinLimits(it)
128+
} ?: Completable.complete()
129+
130+
receiveWithinLimits.toFlowable<Any>().asFlow()
129131
}
130132
.launchIn(viewModelScope)
131133

0 commit comments

Comments
 (0)