Skip to content

Commit 5044993

Browse files
committed
feat(flipcash): daisy-chain connect and createAndSend transaction deeplinks by inheriting amount initially needed
Signed-off-by: Brandon McAnsh <[email protected]>
1 parent d26136e commit 5044993

File tree

8 files changed

+54
-29
lines changed

8 files changed

+54
-29
lines changed

apps/flipcash/app/src/main/kotlin/com/flipcash/app/internal/ui/navigation/AppScreenContent.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,10 @@ internal fun AppScreenContent(content: @Composable () -> Unit) {
124124

125125
register<NavScreenProvider.HomeScreen.OnRamp.ProviderList> {
126126
OnRampFlowTracker.start(it.from)
127-
OnRampProviderListScreen()
127+
OnRampProviderListScreen(
128+
neededAmount = it.neededAmount?.quarks,
129+
neededCurrency = it.neededAmount?.currencyCode
130+
)
128131
}
129132

130133
register<NavScreenProvider.HomeScreen.OnRamp.Amount> {

apps/flipcash/core/src/main/kotlin/com/flipcash/app/core/NavScreenProvider.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import com.flipcash.app.core.navigation.DeeplinkType
66
import com.flipcash.app.core.transfers.TransferDirection
77
import com.getcode.ed25519.Ed25519
88
import com.getcode.opencode.model.core.ID
9+
import com.getcode.opencode.model.financial.Fiat
910
import com.getcode.ui.core.RestrictionType
1011

1112
sealed class NavScreenProvider : ScreenProvider {
@@ -56,7 +57,10 @@ sealed class NavScreenProvider : ScreenProvider {
5657
data object ShareApp : NavScreenProvider()
5758

5859
sealed class OnRamp {
59-
data class ProviderList(val from: NavScreenProvider? = null): NavScreenProvider()
60+
data class ProviderList(
61+
val from: NavScreenProvider? = null,
62+
val neededAmount: Fiat? = null,
63+
): NavScreenProvider()
6064
data object Amount : NavScreenProvider()
6165
data object Success: NavScreenProvider()
6266
}

apps/flipcash/features/cash/src/main/kotlin/com/flipcash/app/cash/CashScreen.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import com.getcode.ui.components.AppBarDefaults
2525
import com.getcode.ui.components.AppBarWithTitle
2626
import kotlinx.coroutines.flow.filterIsInstance
2727
import kotlinx.coroutines.flow.launchIn
28+
import kotlinx.coroutines.flow.map
2829
import kotlinx.coroutines.flow.onEach
2930
import kotlinx.parcelize.IgnoredOnParcel
3031
import kotlinx.parcelize.Parcelize
@@ -69,11 +70,13 @@ class CashScreen: ModalScreen, Parcelable {
6970
LaunchedEffect(viewModel) {
7071
viewModel.eventFlow
7172
.filterIsInstance<CashScreenViewModel.Event.AddCashToWallet>()
72-
.onEach {
73+
.map { it.amount }
74+
.onEach { amount ->
7375
navigator.push(
7476
ScreenRegistry.get(
7577
NavScreenProvider.HomeScreen.OnRamp.ProviderList(
76-
NavScreenProvider.HomeScreen.Cash
78+
NavScreenProvider.HomeScreen.Cash,
79+
neededAmount = amount
7780
)
7881
)
7982
)

apps/flipcash/features/cash/src/main/kotlin/com/flipcash/app/cash/internal/CashScreenViewModel.kt

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ internal class CashScreenViewModel @Inject constructor(
9090
data object OnGive : Event
9191
data class PresentBill(val bill: Bill.Cash) : Event
9292

93-
data object AddCashToWallet : Event
93+
data class AddCashToWallet(val amount: Fiat) : Event
9494
data class UpdateLoadingState(val loading: Boolean = false, val success: Boolean = false) :
9595
Event
9696
}
@@ -117,7 +117,22 @@ internal class CashScreenViewModel @Inject constructor(
117117
text = resources.getString(R.string.action_addCashToWallet),
118118
style = BottomBarManager.BottomBarButtonStyle.Filled,
119119
) {
120-
dispatchEvent(Event.AddCashToWallet)
120+
viewModelScope.launch {
121+
val rate = exchange.entryRate
122+
// if we are USD we can skip the rate fetch since its 1:1
123+
if (rate.currency != CurrencyCode.USD) {
124+
exchange.fetchRatesIfNeeded()
125+
}
126+
127+
val localizedAmount = Fiat(amount, rate.currency)
128+
129+
val amountFiat = LocalFiat(
130+
usdc = localizedAmount.convertingTo(exchange.rateToUsd(rate.currency)!!),
131+
converted = localizedAmount,
132+
rate = rate,
133+
)
134+
dispatchEvent(Event.AddCashToWallet(amountFiat.usdc))
135+
}
121136
},
122137
BottomBarAction(
123138
text = resources.getString(R.string.action_dismiss),
@@ -297,8 +312,7 @@ internal class CashScreenViewModel @Inject constructor(
297312
)
298313
}
299314

300-
Event.AddCashToWallet -> { state -> state }
301-
315+
is Event.AddCashToWallet -> { state -> state }
302316
is Event.OnMaxDetermined -> { state ->
303317
state.copy(maxForGive = event.max to event.currencyCode)
304318
}

apps/flipcash/features/onramp/src/main/kotlin/com/flipcash/app/onramp/OnRampProviderListScreen.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import com.getcode.navigation.core.LocalCodeNavigator
2222
import com.getcode.navigation.extensions.getStackScopedViewModel
2323
import com.getcode.navigation.modal.ModalScreen
2424
import com.getcode.navigation.screens.NamedScreen
25+
import com.getcode.opencode.model.financial.CurrencyCode
26+
import com.getcode.opencode.model.financial.Fiat
2527
import com.getcode.ui.components.AppBarWithTitle
2628
import kotlinx.coroutines.flow.filterIsInstance
2729
import kotlinx.coroutines.flow.launchIn
@@ -31,7 +33,10 @@ import kotlinx.parcelize.IgnoredOnParcel
3133
import kotlinx.parcelize.Parcelize
3234

3335
@Parcelize
34-
class OnRampProviderListScreen: ModalScreen, NamedScreen, Parcelable {
36+
class OnRampProviderListScreen(
37+
val neededAmount: Long? = null,
38+
val neededCurrency: CurrencyCode? = null,
39+
): ModalScreen, NamedScreen, Parcelable {
3540

3641
@IgnoredOnParcel
3742
override val key: ScreenKey = uniqueScreenKey
@@ -85,6 +90,9 @@ class OnRampProviderListScreen: ModalScreen, NamedScreen, Parcelable {
8590
navigator.push(ScreenRegistry.get(NavScreenProvider.HomeScreen.OnRamp.Amount))
8691
} else {
8792
phantomDepositState.setOrigin(OnRampFlowTracker.source)
93+
if (neededAmount != null && neededCurrency != null) {
94+
phantomDepositState.amount = Fiat(neededAmount, neededCurrency)
95+
}
8896
phantomDepositState.start()
8997
}
9098
}

apps/flipcash/features/pools/src/main/kotlin/com/flipcash/app/pools/internal/betting/PoolBettingViewModel.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ internal class PoolBettingViewModel @Inject constructor(
172172
data class OnFundsDistributed(val isDistributed: Boolean) : Event
173173
data object OnSharePool : Event
174174
data object OnFailedToLoad: Event
175-
data object AddCashToWallet: Event
175+
data class AddCashToWallet(val amount: Fiat): Event
176176
}
177177

178178
init {
@@ -374,7 +374,7 @@ internal class PoolBettingViewModel @Inject constructor(
374374
text = resources.getString(R.string.action_addCashToWallet),
375375
style = BottomBarManager.BottomBarButtonStyle.Filled,
376376
) {
377-
dispatchEvent(Event.AddCashToWallet)
377+
dispatchEvent(Event.AddCashToWallet(stateFlow.value.metadata.buyIn))
378378
},
379379
BottomBarAction(
380380
text = resources.getString(R.string.action_dismiss),

apps/flipcash/shared/phantom-onramp/src/main/kotlin/com/flipcash/app/onramp/PhantomOnRampHandler.kt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,7 @@ fun PhantomOnRampHandler(
126126
message = "phantom connected",
127127
type = TraceType.Process
128128
)
129-
// this will always be present in a modal so we can confidently push it into the stack
130-
// without worrying about the need to show vs. push
131-
delay(300)
132-
navigator.push(ScreenRegistry.get(NavScreenProvider.HomeScreen.OnRamp.Amount))
129+
state.createAndSendTransaction()
133130
}
134131

135132
PhantomDeeplinkState.SIGNING -> {

apps/flipcash/shared/phantom-onramp/src/main/kotlin/com/flipcash/app/onramp/internal/PhantomDepositState.kt

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -67,20 +67,6 @@ class PhantomDepositState(
6767
* The amount to transfer selected by the user
6868
*/
6969
var amount: Fiat? = null
70-
set(value) {
71-
field = value
72-
if (value != null) {
73-
scope.launch {
74-
createTransaction()
75-
.onFailure {
76-
errors.emit(PhantomOnRampError.FailedToCreateTransaction(message = it.message))
77-
}.onSuccess {
78-
unsignedTransaction = it
79-
deeplinkState = PhantomDeeplinkState.SIGNING
80-
}
81-
}
82-
}
83-
}
8470

8571
/**
8672
* The unsigned transaction to be signed by Phantom
@@ -251,6 +237,16 @@ class PhantomDepositState(
251237
signingResult = null
252238
}
253239

240+
suspend fun createAndSendTransaction() {
241+
createTransaction()
242+
.onFailure {
243+
errors.emit(PhantomOnRampError.FailedToCreateTransaction(message = it.message))
244+
}.onSuccess {
245+
unsignedTransaction = it
246+
deeplinkState = PhantomDeeplinkState.SIGNING
247+
}
248+
}
249+
254250
private suspend fun createTransaction(): Result<Transaction> {
255251
return withContext(Dispatchers.IO) {
256252
runCatching {

0 commit comments

Comments
 (0)