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 Oct 3, 2024
1 parent 419f1a0 commit a22c1af
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 29 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
## 4.14.1

- added: Foreground push notification title data displayed in dropdown
- 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

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 @@ -1455,7 +1455,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 @@ -1285,7 +1285,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
62 changes: 35 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,27 @@ 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(date: string, startingFiatAmount: string, isoFiatCurrencyCode: string) {
let initialValue1: string | undefined
if (isoFiatCurrencyCode !== 'iso:USD') {
const ratePair = `${isoFiatCurrencyCode}_iso:USD`
const rate = await getHistoricalRate(ratePair, date)
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 +246,16 @@ 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 isoNow = new Date().toISOString()
const initialValue1 = requireCrypto ? undefined : await getInitialFiatValue(isoNow, 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,14 @@ 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)

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

0 comments on commit a22c1af

Please sign in to comment.