Skip to content

Commit 868b980

Browse files
committed
Integrate preferProviders
1 parent 924e107 commit 868b980

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased (develop)
44

5+
- added: `preferProviders` support to override default priority when selecting a fiat provider
6+
57
## 4.25.0 (staging)
68

79
- fixed: Fix yesterday rate lookup in `exchangeRates`

src/plugins/gui/amountQuotePlugin.ts

+31-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { div, eq, gt, round, toFixed } from 'biggystring'
22
import { asNumber, asObject } from 'cleaners'
3+
import { PluginPromotion } from 'edge-info-server'
34
import { sprintf } from 'sprintf-js'
45

56
import { getFirstOpenInfo } from '../../actions/FirstOpenActions'
@@ -144,7 +145,7 @@ export const amountQuoteFiatPlugin: FiatPluginFactory = async (params: FiatPlugi
144145
try {
145146
if (infoServerData.rollup?.fiatPluginPriority != null) {
146147
providerPriority = infoServerData.rollup.fiatPluginPriority
147-
priorityArray = createPriorityArray(providerPriority[paymentTypes[0]])
148+
priorityArray = createPriorityArray(providerPriority[paymentTypes[0]], pluginPromotions)
148149
}
149150
} catch (e: any) {
150151
console.warn('Failed to fetch provider priorities:', e)
@@ -606,12 +607,37 @@ export const amountQuoteFiatPlugin: FiatPluginFactory = async (params: FiatPlugi
606607
// showing the quotes.
607608
// TODO: conflict: also defines whether or not to accept a quote from the
608609
// provider
609-
export const createPriorityArray = (providerPriorityMap?: ProviderPriorityMap): PriorityArray => {
610+
export const createPriorityArray = (providerPriorityMap?: ProviderPriorityMap, pluginPromotions: PluginPromotion[] = []): PriorityArray => {
610611
const priorityArray: PriorityArray = []
611612
if (providerPriorityMap != null) {
613+
// Create a copy of the providerPriorityMap to avoid modifying the original
614+
const priorityMapCopy = { ...providerPriorityMap }
615+
616+
// Create a separate object for preferred providers
617+
const preferredProviders: { [pluginId: string]: boolean } = {}
618+
619+
// Identify all preferred providers from pluginPromotions
620+
for (const pluginPromotion of pluginPromotions) {
621+
if (pluginPromotion.preferProviders != null) {
622+
for (const provider of pluginPromotion.preferProviders) {
623+
// Add to preferred providers list
624+
preferredProviders[provider] = true
625+
// Remove from the regular priority map if it exists there, so they
626+
// are omitted from normal ordering logic
627+
delete priorityMapCopy[provider]
628+
}
629+
}
630+
}
631+
632+
// If we have preferred providers, add them as the first priority group
633+
if (Object.keys(preferredProviders).length > 0) {
634+
priorityArray.push(preferredProviders)
635+
}
636+
637+
// Process the remaining providers by priority
612638
const tempPriorityList: Array<{ pluginId: string; priority: number }> = []
613-
for (const pluginId in providerPriorityMap) {
614-
tempPriorityList.push({ pluginId, priority: providerPriorityMap[pluginId] })
639+
for (const pluginId in priorityMapCopy) {
640+
tempPriorityList.push({ pluginId, priority: priorityMapCopy[pluginId] })
615641
}
616642
tempPriorityList.sort((a, b) => b.priority - a.priority)
617643
let currentPriority = Infinity
@@ -625,6 +651,7 @@ export const createPriorityArray = (providerPriorityMap?: ProviderPriorityMap):
625651
priorityObj[tempPriority.pluginId] = true
626652
}
627653
}
654+
628655
return priorityArray
629656
}
630657

0 commit comments

Comments
 (0)