Skip to content

Commit 1300dc3

Browse files
committed
chore(withdrawal): lock to USD
Signed-off-by: Brandon McAnsh <[email protected]>
1 parent 26fd232 commit 1300dc3

File tree

3 files changed

+23
-36
lines changed

3 files changed

+23
-36
lines changed

apps/flipcash/features/withdrawal/src/main/kotlin/com/flipcash/app/withdrawal/WithdrawalViewModel.kt

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import com.getcode.opencode.model.financial.CurrencyCode
2424
import com.getcode.opencode.model.financial.Fiat
2525
import com.getcode.opencode.model.financial.LocalFiat
2626
import com.getcode.opencode.model.financial.Rate
27+
import com.getcode.opencode.model.financial.TokenWithBalance
2728
import com.getcode.opencode.model.financial.TokenWithLocalizedBalance
2829
import com.getcode.opencode.model.financial.minus
2930
import com.getcode.opencode.model.transactions.WithdrawalAvailability
@@ -85,7 +86,7 @@ internal class WithdrawalViewModel @Inject constructor(
8586

8687
internal data class State(
8788
val selectedTokenAddress: Mint? = null,
88-
val token: TokenWithLocalizedBalance? = null,
89+
val token: TokenWithBalance? = null,
8990
val amountEntryState: AmountEntryState = AmountEntryState(),
9091
val destinationState: DestinationState = DestinationState(),
9192
val withdrawalState: LoadingSuccessState = LoadingSuccessState(),
@@ -95,7 +96,7 @@ internal class WithdrawalViewModel @Inject constructor(
9596
?: 0.0) > 0.00
9697

9798
val tokenBalance: Fiat
98-
get() = token?.balance?.nativeAmount ?: Fiat.Zero
99+
get() = token?.balance ?: Fiat.Zero
99100

100101
val isError: Boolean
101102
get() {
@@ -115,7 +116,7 @@ internal class WithdrawalViewModel @Inject constructor(
115116
internal sealed interface Event {
116117
// common
117118
data class OnMintSelected(val mint: Mint) : Event
118-
data class OnTokenUpdated(val token: TokenWithLocalizedBalance) : Event
119+
data class OnTokenUpdated(val token: TokenWithBalance) : Event
119120

120121
// amount
121122
data class OnNumberPressed(val number: Int) : Event
@@ -169,7 +170,7 @@ internal class WithdrawalViewModel @Inject constructor(
169170
fiat = amount,
170171
currencyCode = stateFlow.value.amountEntryState.currencyModel.code ?: CurrencyCode.USD
171172
).convertingTo(conversionRate)
172-
val tokenBalance = stateFlow.value.token?.balance?.nativeAmount ?: Fiat.Zero
173+
val tokenBalance = stateFlow.value.token?.balance ?: Fiat.Zero
173174

174175
val isOverBalance = enteredInUsdc > tokenBalance
175176
if (isOverBalance || conversionRate == Rate.ignore) {
@@ -194,22 +195,18 @@ internal class WithdrawalViewModel @Inject constructor(
194195
combine(
195196
tokenController.tokens,
196197
tokenController.balanceForToken(tokenAddress),
197-
exchange.observeEntryRate(),
198-
) { tokens, balance, rate ->
198+
) { tokens, balance ->
199199
val token = tokens.find { it.address == tokenAddress } ?: return@combine null
200-
TokenWithLocalizedBalance(
200+
TokenWithBalance(
201201
token = token,
202-
balance = LocalFiat(
203-
usdc = balance,
204-
nativeAmount = balance.convertingTo(rate),
205-
)
202+
balance = balance
206203
)
207204
}
208205
}.filterNotNull()
209206
.onEach {
210207
dispatchEvent(Event.OnTokenUpdated(it))
211208
}.mapNotNull { (token, balance) ->
212-
exchange.getCurrency(balance.rate.currency.name)
209+
exchange.getCurrency(balance.currencyCode.name)
213210
}.onEach {
214211
dispatchEvent(Event.OnCurrencyChanged(it))
215212
}.launchIn(viewModelScope)
@@ -277,12 +274,7 @@ internal class WithdrawalViewModel @Inject constructor(
277274
.filterNot { checkBalanceLimit() }
278275
.onEach { data ->
279276
dispatchEvent(Event.UpdateConfirmingAmountState(loading = true))
280-
val rate = exchange.entryRate
281-
// if we are USD we can skip the rate fetch since its 1:1
282-
if (rate.currency != CurrencyCode.USD) {
283-
exchange.fetchRatesIfNeeded()
284-
}
285-
277+
val rate = exchange.rateForUsd()
286278
val token = stateFlow.value.token!!.token
287279
val amountFiat = LocalFiat.valueExchangeIn(
288280
amount = Fiat(data.amountData.amount, rate.currency),

apps/flipcash/features/withdrawal/src/main/kotlin/com/flipcash/app/withdrawal/internal/confirmation/WithdrawalConfirmationScreen.kt

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -134,23 +134,17 @@ private fun WithdrawalConfirmationScreenContent(
134134
CodeTheme.dimens.inset,
135135
)
136136
) {
137-
with(state.token!!.token) {
138-
TransferInfo(
139-
tokenWithBalance = TokenWithBalance(
140-
token = this,
141-
balance = Fiat.tokenBalance(
142-
state.amountEntryState.selectedAmount.underlyingTokenAmount.quarks,
143-
currencyCode = state.amountEntryState.selectedAmount.underlyingTokenAmount.currencyCode,
144-
token = this
145-
)
146-
),
147-
destination = state.destinationState.textFieldState.text.toString(),
148-
fee = state.destinationState.availability?.feeAmount,
149-
onLearnMoreClicked = {
150-
dispatchEvent(WithdrawalViewModel.Event.OnLearnAboutFee)
151-
}
152-
)
153-
}
137+
TransferInfo(
138+
tokenWithBalance = TokenWithBalance(
139+
state.token!!.token,
140+
balance = state.amountEntryState.selectedAmount.nativeAmount,
141+
),
142+
destination = state.destinationState.textFieldState.text.toString(),
143+
fee = state.destinationState.availability?.feeAmount,
144+
onLearnMoreClicked = {
145+
dispatchEvent(WithdrawalViewModel.Event.OnLearnAboutFee)
146+
}
147+
)
154148
}
155149
}
156150
}

apps/flipcash/shared/tokens/src/main/kotlin/SelectTokenViewModel.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import dagger.hilt.android.lifecycle.HiltViewModel
1414
import kotlinx.coroutines.flow.combine
1515
import kotlinx.coroutines.flow.filterIsInstance
1616
import kotlinx.coroutines.flow.flatMapLatest
17+
import kotlinx.coroutines.flow.flowOf
1718
import kotlinx.coroutines.flow.launchIn
1819
import kotlinx.coroutines.flow.map
1920
import kotlinx.coroutines.flow.onEach
@@ -53,7 +54,7 @@ class SelectTokenViewModel @Inject constructor(
5354
when (purpose) {
5455
TokenPurpose.Balance -> exchange.observeBalanceRate()
5556
TokenPurpose.Send -> exchange.observeEntryRate()
56-
TokenPurpose.Withdraw -> exchange.observeEntryRate()
57+
TokenPurpose.Withdraw -> flowOf(exchange.rateForUsd())
5758
TokenPurpose.Deposit -> exchange.observeEntryRate()
5859
TokenPurpose.BillPlayground -> exchange.observeBalanceRate()
5960
}

0 commit comments

Comments
 (0)