@@ -73,6 +73,20 @@ const pluginDisplayName = 'Banxa'
7373
7474const TESTNET_ADDRESS = 'bc1qv752cnr3rcht3yyfq2nn6nv7zwczqjmcm80y6w'
7575
76+ // Cache for max amounts with 2 minute TTL
77+ const maxAmountCache = new Map < string , { amount : string ; timestamp : number } > ( )
78+ const MAX_CACHE_TTL = 2 * 60 * 1000 // 2 minutes
79+
80+ const getCacheKey = (
81+ direction : FiatDirection ,
82+ fiatCode : string ,
83+ banxaCoin : string ,
84+ paymentType : FiatPaymentType ,
85+ amountType : 'fiat' | 'crypto'
86+ ) : string => {
87+ return `${ direction } -${ fiatCode } -${ banxaCoin } -${ paymentType } -${ amountType } `
88+ }
89+
7690type AllowedPaymentTypes = Record <
7791 FiatDirection ,
7892 Partial < Record < FiatPaymentType , boolean > >
@@ -996,31 +1010,50 @@ export const banxaRampPlugin: RampPluginFactory = (
9961010
9971011 let maxAmountString = ''
9981012 if ( isMaxAmount ) {
999- if ( amountType === 'fiat' ) {
1000- maxAmountString = paymentObj . max
1013+ const cacheKey = getCacheKey (
1014+ direction ,
1015+ fiatCode ,
1016+ banxaCoin ,
1017+ paymentType ,
1018+ amountType
1019+ )
1020+ const cached = maxAmountCache . get ( cacheKey )
1021+ const now = Date . now ( )
1022+
1023+ if ( cached != null && now - cached . timestamp < MAX_CACHE_TTL ) {
1024+ maxAmountString = cached . amount
10011025 } else {
1002- // For crypto, we need to fetch a quote with max fiat to get the crypto amount
1003- const maxFiatQueryParams : any = {
1004- account_reference : username ,
1005- payment_method_id : paymentObj . id ,
1006- source : direction === 'buy' ? fiatCode : banxaCoin ,
1007- target : direction === 'buy' ? banxaCoin : fiatCode
1008- }
1009- if ( direction === 'buy' ) {
1010- maxFiatQueryParams . source_amount = paymentObj . max
1026+ if ( amountType === 'fiat' ) {
1027+ maxAmountString = paymentObj . max
10111028 } else {
1012- maxFiatQueryParams . target_amount = paymentObj . max
1029+ // For crypto, we need to fetch a quote with max fiat to get the crypto amount
1030+ const maxFiatQueryParams : any = {
1031+ account_reference : username ,
1032+ payment_method_id : paymentObj . id ,
1033+ source : direction === 'buy' ? fiatCode : banxaCoin ,
1034+ target : direction === 'buy' ? banxaCoin : fiatCode
1035+ }
1036+ if ( direction === 'buy' ) {
1037+ maxFiatQueryParams . source_amount = paymentObj . max
1038+ } else {
1039+ maxFiatQueryParams . target_amount = paymentObj . max
1040+ }
1041+ const maxResponse = await banxaFetch ( {
1042+ method : 'GET' ,
1043+ url : apiUrl ,
1044+ hmacUser,
1045+ path : 'api/prices' ,
1046+ apiKey,
1047+ queryParams : maxFiatQueryParams
1048+ } )
1049+ const maxPrices = asBanxaPricesResponse ( maxResponse )
1050+ maxAmountString = maxPrices . data . prices [ 0 ] . coin_amount
10131051 }
1014- const maxResponse = await banxaFetch ( {
1015- method : 'GET' ,
1016- url : apiUrl ,
1017- hmacUser,
1018- path : 'api/prices' ,
1019- apiKey,
1020- queryParams : maxFiatQueryParams
1052+ // Cache the result
1053+ maxAmountCache . set ( cacheKey , {
1054+ amount : maxAmountString ,
1055+ timestamp : now
10211056 } )
1022- const maxPrices = asBanxaPricesResponse ( maxResponse )
1023- maxAmountString = maxPrices . data . prices [ 0 ] . coin_amount
10241057 }
10251058 }
10261059
0 commit comments