Skip to content

Commit 77a0d61

Browse files
committed
chore: consolidate usdc valueExchange amount internally
Signed-off-by: Brandon McAnsh <[email protected]>
1 parent 21514e2 commit 77a0d61

File tree

3 files changed

+34
-48
lines changed

3 files changed

+34
-48
lines changed

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

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -152,18 +152,11 @@ internal class CashScreenViewModel @Inject constructor(
152152

153153
val localizedAmount = Fiat(amount, rate.currency)
154154
val token = stateFlow.value.token!!.token
155-
val amountFiat = if (token.address == Mint.usdc) {
156-
LocalFiat(
157-
usdc = localizedAmount.convertingTo(exchange.rateToUsd(rate.currency)!!),
158-
nativeAmount = localizedAmount,
159-
)
160-
} else {
161-
LocalFiat.valueExchangeIn(
162-
localizedAmount,
163-
token = token,
164-
rate = rate,
165-
)
166-
}
155+
val amountFiat = LocalFiat.valueExchangeIn(
156+
amount = Fiat(amount, rate.currency),
157+
token = token,
158+
rate = rate,
159+
)
167160

168161
val neededAmount = amountFiat.underlyingTokenAmount - tokenBalance
169162

@@ -312,20 +305,11 @@ internal class CashScreenViewModel @Inject constructor(
312305
}
313306
}
314307

315-
val localizedAmount = Fiat(data.amountData.amount, rate.currency)
316-
// TOD is there a way to consolidate all of this into valueExchangeIn?
317-
val amountFiat = if (token.address == Mint.usdc) {
318-
LocalFiat(
319-
usdc = localizedAmount.convertingTo(exchange.rateToUsd(rate.currency)!!),
320-
nativeAmount = localizedAmount,
321-
)
322-
} else {
323-
LocalFiat.valueExchangeIn(
324-
amount = localizedAmount,
325-
token = token,
326-
rate = rate,
327-
)
328-
}
308+
val amountFiat = LocalFiat.valueExchangeIn(
309+
amount = Fiat(data.amountData.amount, rate.currency),
310+
token = token,
311+
rate = rate,
312+
)
329313

330314
val bill = Bill.Cash(
331315
token = stateFlow.value.token!!.token,

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

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -284,20 +284,12 @@ internal class WithdrawalViewModel @Inject constructor(
284284
exchange.fetchRatesIfNeeded()
285285
}
286286

287-
val localizedAmount = Fiat(data.amountData.amount, rate.currency)
288287
val token = stateFlow.value.token!!.token
289-
val amountFiat = if (token.address == Mint.usdc) {
290-
LocalFiat(
291-
usdc = localizedAmount.convertingTo(exchange.rateToUsd(rate.currency)!!),
292-
nativeAmount = localizedAmount,
293-
)
294-
} else {
295-
LocalFiat.valueExchangeIn(
296-
localizedAmount,
297-
token = token,
298-
rate = rate,
299-
)
300-
}
288+
val amountFiat = LocalFiat.valueExchangeIn(
289+
amount = Fiat(data.amountData.amount, rate.currency),
290+
token = token,
291+
rate = rate,
292+
)
301293

302294
dispatchEvent(Event.UpdateConfirmingAmountState(loading = false, success = true))
303295
dispatchEvent(Event.OnAmountAccepted(amountFiat))
@@ -433,15 +425,11 @@ internal class WithdrawalViewModel @Inject constructor(
433425
val sendingVault = owner.withTimelockForToken(token)
434426

435427
val feeInMint = feeInUsd?.let { fee ->
436-
if (token.address != Mint.usdc) {
437-
LocalFiat.valueExchangeIn(
438-
fee,
439-
token = token,
440-
rate = exchange.rateToUsd(CurrencyCode.USD)!!
441-
).underlyingTokenAmount
442-
} else {
443-
feeInUsd
444-
}
428+
LocalFiat.valueExchangeIn(
429+
fee,
430+
token = token,
431+
rate = exchange.rateToUsd(CurrencyCode.USD)!!
432+
).underlyingTokenAmount
445433
}
446434

447435
transactionController.withdraw(

services/opencode/src/main/kotlin/com/getcode/opencode/model/financial/LocalFiat.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,20 @@ data class LocalFiat(
7171
amount
7272
}
7373

74+
if (token.address == Mint.usdc) {
75+
// this doesn't need a calcuated value exchange since we are USDC
76+
return if (rate.currency != CurrencyCode.USD) {
77+
LocalFiat(
78+
usdc = usdValue,
79+
nativeAmount = amount
80+
)
81+
} else {
82+
LocalFiat(usdc = usdValue)
83+
}
84+
}
85+
86+
87+
7488
// determine quarks to exchange for the desired amount
7589
val quarks = Estimator.valueExchangeAsQuarks(
7690
valueInQuarks = usdValue.quarks,

0 commit comments

Comments
 (0)