Skip to content

Commit 408874a

Browse files
committed
fix(localfiat): cap entered amount for USDC value exchanges as well
With our switch to rounding formatted Fiat values using HALF_UP (closest), we introduced an internal balance "capping" to the actual underlying transfer to allow bills to still render at the entered amount (the "native amount"), while sending the min(balance, usdAmount) as the underlying USDC value. However, this was only taken into account for custom tokens and was not applied to straight USDC transfers as well. Signed-off-by: Brandon McAnsh <[email protected]>
1 parent ac0082b commit 408874a

File tree

1 file changed

+9
-9
lines changed
  • services/opencode/src/main/kotlin/com/getcode/opencode/model/financial

1 file changed

+9
-9
lines changed

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ data class LocalFiat(
4949
)
5050
)
5151

52-
constructor(usdc: Usd) : this(
52+
constructor(usdc: Usd, rate: Rate = Rate.oneToOne, mint: Mint = Mint.usdc) : this(
5353
underlyingTokenAmount = usdc,
54-
nativeAmount = usdc,
55-
mint = Mint.usdc,
56-
rate = Rate.oneToOne
54+
nativeAmount = usdc.convertingTo(rate),
55+
mint = mint,
56+
rate = rate
5757
)
5858

5959
companion object {
@@ -73,25 +73,25 @@ data class LocalFiat(
7373
trace: Boolean = true,
7474
): LocalFiat {
7575
val usdValue = amount.convertingToUsdIfNeeded(rate)
76+
// cap the entered amount as well, since our display rounds HALF_UP
77+
// e,g entered 0.02 USD, but balance is 0.016 USD
78+
val cappedValue = balance?.let { min(it, usdValue) } ?: usdValue
7679

7780
if (token.address == Mint.usdc) {
7881
// this doesn't need a calculated value exchange since we are USDC
7982
return if (rate.currency != CurrencyCode.USD) {
8083
LocalFiat(
81-
underlyingTokenAmount = usdValue,
82-
nativeAmount = amount,
84+
usdc = cappedValue,
8385
rate = rate,
8486
mint = token.address,
8587
)
8688
} else {
87-
LocalFiat(usdc = usdValue)
89+
LocalFiat(usdc = cappedValue)
8890
}
8991
}
9092

9193
val valueLocked = token.launchpadMetadata?.coreMintLockedQuarks ?: 0
9294

93-
val cappedValue = balance?.let { min(it, usdValue) } ?: usdValue
94-
9595
// determine quarks to exchange for the desired amount
9696
val quarks = Estimator.valueExchangeAsQuarks(
9797
valueInQuarks = cappedValue.quarks,

0 commit comments

Comments
 (0)