Skip to content

Commit 2e697a4

Browse files
committed
Add ramp plugin amount type filtering
1 parent 976a320 commit 2e697a4

File tree

9 files changed

+89
-24
lines changed

9 files changed

+89
-24
lines changed

src/components/scenes/TradeCreateScene.tsx

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ import { FLAG_LOGO_URL } from '../../constants/CdnConstants'
1111
import { COUNTRY_CODES, FIAT_COUNTRY } from '../../constants/CountryConstants'
1212
import { useHandler } from '../../hooks/useHandler'
1313
import { useRampQuotes } from '../../hooks/useRampQuotes'
14-
import { useSupportedPlugins } from '../../hooks/useSupportedPlugins'
14+
import {
15+
type SupportedPluginResult,
16+
useSupportedPlugins
17+
} from '../../hooks/useSupportedPlugins'
1518
import { useWatch } from '../../hooks/useWatch'
1619
import { lstrings } from '../../locales/strings'
1720
import type { RampQuoteRequest } from '../../plugins/ramps/rampPluginTypes'
@@ -49,6 +52,47 @@ export interface TradeCreateParams {
4952

5053
interface Props extends BuyTabSceneProps<'pluginListBuy'> {}
5154

55+
// Helper function to determine which input types should be disabled
56+
interface AmountTypeSupport {
57+
fiatInputDisabled: boolean
58+
cryptoInputDisabled: boolean
59+
}
60+
61+
function getAmountTypeSupport(
62+
supportedPlugins: SupportedPluginResult[]
63+
): AmountTypeSupport {
64+
if (supportedPlugins.length === 0) {
65+
return { fiatInputDisabled: false, cryptoInputDisabled: false }
66+
}
67+
68+
// Collect all supported amount types from all plugins
69+
const allSupportedTypes = new Set<'fiat' | 'crypto'>()
70+
71+
for (const { supportResult } of supportedPlugins) {
72+
if (supportResult.supportedAmountTypes != null) {
73+
for (const type of supportResult.supportedAmountTypes) {
74+
allSupportedTypes.add(type)
75+
}
76+
} else {
77+
// If a plugin doesn't specify supported types, assume both are supported
78+
allSupportedTypes.add('fiat')
79+
allSupportedTypes.add('crypto')
80+
}
81+
}
82+
83+
// If all plugins only support fiat, disable crypto input
84+
const onlyFiat =
85+
allSupportedTypes.has('fiat') && !allSupportedTypes.has('crypto')
86+
// If all plugins only support crypto, disable fiat input
87+
const onlyCrypto =
88+
allSupportedTypes.has('crypto') && !allSupportedTypes.has('fiat')
89+
90+
return {
91+
fiatInputDisabled: onlyCrypto,
92+
cryptoInputDisabled: onlyFiat
93+
}
94+
}
95+
5296
export const TradeCreateScene = (props: Props): React.ReactElement => {
5397
const { navigation, route } = props
5498
const { regionCode: initialRegionCode, forcedWalletResult } =
@@ -223,13 +267,17 @@ export const TradeCreateScene = (props: Props): React.ReactElement => {
223267
} = useRampQuotes({
224268
rampQuoteRequest,
225269
plugins: Object.fromEntries(
226-
supportedPlugins.map(plugin => [plugin.pluginId, plugin])
270+
supportedPlugins.map(result => [result.plugin.pluginId, result.plugin])
227271
)
228272
})
229273

230274
// Get the best quote
231275
const bestQuote = sortedQuotes[0]
232276

277+
// Determine which input types should be disabled
278+
const { fiatInputDisabled, cryptoInputDisabled } =
279+
getAmountTypeSupport(supportedPlugins)
280+
233281
// Calculate exchange rate from best quote
234282
const quoteExchangeRate = React.useMemo(() => {
235283
if (!bestQuote?.cryptoAmount || !bestQuote.fiatAmount) return 0
@@ -516,6 +564,7 @@ export const TradeCreateScene = (props: Props): React.ReactElement => {
516564
keyboardType="decimal-pad"
517565
numeric
518566
showSpinner={isFetchingQuotes && lastUsedInput === 'crypto'}
567+
disabled={fiatInputDisabled}
519568
/>
520569
</InputContainer>
521570
</InputRow>
@@ -545,6 +594,7 @@ export const TradeCreateScene = (props: Props): React.ReactElement => {
545594
keyboardType="decimal-pad"
546595
numeric
547596
showSpinner={isFetchingQuotes && lastUsedInput === 'fiat'}
597+
disabled={cryptoInputDisabled}
548598
/>
549599
{/* MAX Button */}
550600
<MaxButton active={isMaxAmount} onPress={handleMaxPress}>

src/components/scenes/TradeOptionSelectScene.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export const TradeOptionSelectScene = (props: Props): React.JSX.Element => {
6565

6666
// Use supported plugins
6767
const pluginsToUse = Object.fromEntries(
68-
supportedPlugins.map(plugin => [plugin.pluginId, plugin])
68+
supportedPlugins.map(result => [result.plugin.pluginId, result.plugin])
6969
)
7070

7171
// Use the ramp quotes hook

src/hooks/useSupportedPlugins.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import * as React from 'react'
55
import type { FiatPluginRegionCode } from '../plugins/gui/fiatPluginTypes'
66
import type {
77
RampCheckSupportRequest,
8-
RampPlugin
8+
RampPlugin,
9+
RampSupportResult
910
} from '../plugins/ramps/rampPluginTypes'
1011

1112
interface UseSupportedPluginsParams {
@@ -21,8 +22,13 @@ interface UseSupportedPluginsParams {
2122
direction?: 'buy' | 'sell'
2223
}
2324

25+
export interface SupportedPluginResult {
26+
plugin: RampPlugin
27+
supportResult: RampSupportResult
28+
}
29+
2430
interface UseSupportedPluginsResult {
25-
supportedPlugins: RampPlugin[]
31+
supportedPlugins: SupportedPluginResult[]
2632
isLoading: boolean
2733
error: Error | null
2834
}
@@ -60,7 +66,7 @@ export const useSupportedPlugins = ({
6066
data: supportedPlugins = [],
6167
isLoading,
6268
error
63-
} = useQuery<RampPlugin[]>({
69+
} = useQuery<SupportedPluginResult[]>({
6470
queryKey,
6571
queryFn: async () => {
6672
// Early return if required params are missing
@@ -90,10 +96,10 @@ export const useSupportedPlugins = ({
9096
const supportChecks = await Promise.all(
9197
Object.values(plugins).map(async plugin => {
9298
try {
93-
const result = await plugin.checkSupport(checkSupportRequest)
99+
const supportResult = await plugin.checkSupport(checkSupportRequest)
94100
return {
95101
plugin,
96-
supported: result.supported
102+
supportResult
97103
}
98104
} catch (error) {
99105
console.warn(
@@ -102,16 +108,14 @@ export const useSupportedPlugins = ({
102108
)
103109
return {
104110
plugin,
105-
supported: false
111+
supportResult: { supported: false }
106112
}
107113
}
108114
})
109115
)
110116

111117
// Filter only supported plugins
112-
return supportChecks
113-
.filter(check => check.supported)
114-
.map(check => check.plugin)
118+
return supportChecks.filter(check => check.supportResult.supported)
115119
},
116120
enabled: Boolean(
117121
selectedWallet && selectedCrypto && selectedFiatCurrencyCode && regionCode

src/plugins/ramps/banxa/banxaRampPlugin.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -867,7 +867,10 @@ export const banxaRampPlugin: RampPluginFactory = (
867867
return { supported: false }
868868
}
869869

870-
return { supported: true }
870+
return {
871+
supported: true,
872+
supportedAmountTypes: ['fiat', 'crypto']
873+
}
871874
} catch (error) {
872875
console.error('Banxa: Error in checkSupport:', error)
873876
return { supported: false }

src/plugins/ramps/bity/bityRampPlugin.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,10 @@ export const bityRampPlugin = (pluginConfig: RampPluginConfig): RampPlugin => {
694694
}
695695

696696
// All checks passed
697-
return { supported: true }
697+
return {
698+
supported: true,
699+
supportedAmountTypes: ['fiat', 'crypto']
700+
}
698701
} catch (error) {
699702
// Log error and return false for any unexpected errors
700703
console.error('Bity checkSupport error:', error)

src/plugins/ramps/paybis/paybisRampPlugin.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -759,7 +759,10 @@ export const paybisRampPlugin: RampPluginFactory = (
759759
}
760760

761761
// If we get here, it's supported
762-
return { supported: true }
762+
return {
763+
supported: true,
764+
supportedAmountTypes: ['fiat', 'crypto']
765+
}
763766
} catch (error) {
764767
// Only throw for actual errors (network issues, etc)
765768
// Never throw for unsupported combinations

src/plugins/ramps/rampPluginTypes.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ export interface RampCheckSupportRequest {
3131

3232
export interface RampSupportResult {
3333
supported: boolean
34+
// Indicates which amount types are supported by this plugin
35+
supportedAmountTypes?: Array<'fiat' | 'crypto'>
3436
}
3537

3638
export interface RampQuoteRequest {

src/plugins/ramps/revolut/revolutRampPlugin.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,10 @@ export const revolutRampPlugin: RampPluginFactory = (
171171
assetMap
172172
})
173173

174-
return { supported: assetsSupported }
174+
return {
175+
supported: assetsSupported,
176+
supportedAmountTypes: assetsSupported ? ['fiat'] : undefined
177+
}
175178
} catch (error) {
176179
console.error('Failed to check Revolut support:', error)
177180
return { supported: false }
@@ -200,13 +203,7 @@ export const revolutRampPlugin: RampPluginFactory = (
200203
return []
201204
}
202205

203-
// Only support fiat amount type (Revolut requires fiat-based quotes)
204-
if (request.amountType !== 'fiat') {
205-
throw new FiatProviderError({
206-
providerId: pluginId,
207-
errorType: 'amountTypeUnsupported'
208-
})
209-
}
206+
// Amount type check is now handled in checkSupport phase
210207

211208
try {
212209
// Fetch provider configuration (will use cache if valid)

src/plugins/ramps/simplex/simplexRampPlugin.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,10 @@ export const simplexRampPlugin: RampPluginFactory = (
575575
}
576576

577577
// All validations passed
578-
return { supported: true }
578+
return {
579+
supported: true,
580+
supportedAmountTypes: ['fiat', 'crypto']
581+
}
579582
} catch (error) {
580583
// Only throw for actual errors (network issues, etc)
581584
// Never throw for unsupported combinations

0 commit comments

Comments
 (0)