Skip to content

Commit

Permalink
Fix default fiat amounts for light accounts being over limit for non-…
Browse files Browse the repository at this point in the history
…usd fiats
  • Loading branch information
Jon-edge committed Sep 30, 2024
1 parent 0063412 commit e3a9b00
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 29 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
- fixed: Round Kado-provided amounts during sell
- fixed: "Contacts Access" setting could be out of sync with the OS-level contacts access setting

## 4.14.1

- changed: More informative buy amount limit errors for light accounts
- fixed: Default buy amounts for light accounts can potentially be over limit for non-USD currencies

## 4.14.0

- added: `ExpandableList` component, replacing the address hint dropdown in `AddressFormScene`
Expand Down
2 changes: 1 addition & 1 deletion src/locales/en_US.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1458,7 +1458,7 @@ const strings = {
fiat_plugin_sell_failed_try_again: 'Sell order failed. Please try again.',
fiat_plugin_sell_failed_to_send_try_again: 'Failed to send funds for sell transaction. Please try again.',
fiat_plugin_cannot_continue_camera_permission: 'Cannot continue. Camera permission needed for ID verifications',
fiat_plugin_purchase_limit_error: 'Please create a full account to increase the purchase limit',
fiat_plugin_purchase_limit_error_2s: 'Please create a full account to increase the purchase limit above %1$s %2$s',
fiat_plugin_max_buy_quote_error: 'Provider cannot create max buy quote',
fiat_plugin_max_sell_quote_error: 'Provider cannot create max sell quote',
fiat_plugin_max_sell_quote_error_1s: 'Cannot create max sell quote for %1$s',
Expand Down
2 changes: 1 addition & 1 deletion src/locales/strings/enUS.json
Original file line number Diff line number Diff line change
Expand Up @@ -1289,7 +1289,7 @@
"fiat_plugin_sell_failed_try_again": "Sell order failed. Please try again.",
"fiat_plugin_sell_failed_to_send_try_again": "Failed to send funds for sell transaction. Please try again.",
"fiat_plugin_cannot_continue_camera_permission": "Cannot continue. Camera permission needed for ID verifications",
"fiat_plugin_purchase_limit_error": "Please create a full account to increase the purchase limit",
"fiat_plugin_purchase_limit_error_2s": "Please create a full account to increase the purchase limit above %1$s %2$s",
"fiat_plugin_max_buy_quote_error": "Provider cannot create max buy quote",
"fiat_plugin_max_sell_quote_error": "Provider cannot create max sell quote",
"fiat_plugin_max_sell_quote_error_1s": "Cannot create max sell quote for %1$s",
Expand Down
63 changes: 36 additions & 27 deletions src/plugins/gui/amountQuotePlugin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { div, eq, gt, mul, round, toFixed } from 'biggystring'
import { div, eq, gt, round, toFixed } from 'biggystring'
import { asNumber, asObject } from 'cleaners'
import { sprintf } from 'sprintf-js'

Expand Down Expand Up @@ -61,6 +61,28 @@ const MAX_QUOTE_VALUE = '10000000000'

const NO_PROVIDER_TOAST_DURATION_MS = 10000

/** Pick a default fiat amount in the foreign fiat amount that is roughly equal
to the default USD fiat amount. Prioritizes rounding up/down to a nice looking
whole number. */
async function getInitialFiatValue(startingFiatAmount: string, isoFiatCurrencyCode: string) {
let initialValue1: string | undefined
if (isoFiatCurrencyCode !== 'iso:USD') {
const isoNow = new Date().toISOString()
const ratePair = `${isoFiatCurrencyCode}_iso:USD`
const rate = await getHistoricalRate(ratePair, isoNow)
initialValue1 = div(startingFiatAmount, String(rate), DECIMAL_PRECISION)

// Round out all decimals
initialValue1 = round(initialValue1, 0)

// Keep only the first decimal place
initialValue1 = round(initialValue1, initialValue1.length - 1)
} else {
initialValue1 = startingFiatAmount
}
return initialValue1
}

export const amountQuoteFiatPlugin: FiatPluginFactory = async (params: FiatPluginFactoryArgs) => {
const { account, guiPlugin, longPress = false, pluginUtils, showUi } = params
const { pluginId } = guiPlugin
Expand Down Expand Up @@ -225,31 +247,15 @@ export const amountQuoteFiatPlugin: FiatPluginFactory = async (params: FiatPlugi
let goodQuotes: FiatProviderQuote[] = []
let lastSourceFieldNum: number

const fiatCurrencyCode = forceFiatCurrencyCode ?? defaultIsoFiat
const displayFiatCurrencyCode = removeIsoPrefix(fiatCurrencyCode)
const isoFiatCurrencyCode = forceFiatCurrencyCode ?? defaultIsoFiat
const displayFiatCurrencyCode = removeIsoPrefix(isoFiatCurrencyCode)
const isBuy = direction === 'buy'
const disableInput = requireCrypto ? 1 : requireFiat ? 2 : undefined

logEvent(isBuy ? 'Buy_Quote' : 'Sell_Quote')

// Pick a default fiat amount in the foreign fiat amount that is roughly
// equal to the default USD fiat amount
let initialValue1: string | undefined
const startingFiatAmount = isLightAccount ? DEFAULT_FIAT_AMOUNT_LIGHT_ACCOUNT : defaultFiatAmount ?? DEFAULT_FIAT_AMOUNT
if (displayFiatCurrencyCode !== 'USD' && defaultFiatAmount == null) {
const isoFiatCurrencyCode = `iso:${displayFiatCurrencyCode}`
const isoNow = new Date().toISOString()
const ratePair = `${isoFiatCurrencyCode}_iso:USD`
const rate = await getHistoricalRate(ratePair, isoNow)
initialValue1 = div(startingFiatAmount, String(rate), DECIMAL_PRECISION)
// Round out a decimals
initialValue1 = round(initialValue1, 0)

// Only leave the first decimal
initialValue1 = round(initialValue1, initialValue1.length - 1)
} else {
initialValue1 = requireCrypto ? undefined : startingFiatAmount
}
const initialValue1 = requireCrypto ? undefined : await getInitialFiatValue(startingFiatAmount, isoFiatCurrencyCode)

// Navigate to scene to have user enter amount
const enterAmount: InternalFiatPluginEnterAmountParams = {
Expand Down Expand Up @@ -382,7 +388,7 @@ export const amountQuoteFiatPlugin: FiatPluginFactory = async (params: FiatPlugi
pluginUtils,
tokenId,
exchangeAmount: value,
fiatCurrencyCode,
fiatCurrencyCode: isoFiatCurrencyCode,
amountType: 'fiat',
direction,
paymentTypes,
Expand All @@ -398,7 +404,7 @@ export const amountQuoteFiatPlugin: FiatPluginFactory = async (params: FiatPlugi
pluginUtils,
tokenId,
exchangeAmount: value,
fiatCurrencyCode,
fiatCurrencyCode: isoFiatCurrencyCode,
amountType: 'crypto',
direction,
paymentTypes,
Expand Down Expand Up @@ -513,12 +519,15 @@ export const amountQuoteFiatPlugin: FiatPluginFactory = async (params: FiatPlugi
// Restrict light accounts from buying more than $50 at a time
if (isLightAccount && isBuy) {
const quoteFiatCurrencyCode = bestQuote.fiatCurrencyCode
const quoteFiatRate =
quoteFiatCurrencyCode === 'iso:USD' ? '1' : (await getHistoricalRate(`${quoteFiatCurrencyCode}_iso:USD`, new Date().toISOString())).toString()
const quoteDollarValue = mul(bestQuote.fiatAmount, quoteFiatRate)
const maxAmount = await getInitialFiatValue(startingFiatAmount, isoFiatCurrencyCode)

if (gt(quoteDollarValue, '50')) {
stateManager.update({ statusText: { content: lstrings.fiat_plugin_purchase_limit_error, textType: 'error' } })
if (gt(bestQuote.fiatAmount, maxAmount)) {
stateManager.update({
statusText: {
content: sprintf(lstrings.fiat_plugin_purchase_limit_error_2s, maxAmount, removeIsoPrefix(quoteFiatCurrencyCode)),
textType: 'error'
}
})
return
}
}
Expand Down

0 comments on commit e3a9b00

Please sign in to comment.