Skip to content

Commit

Permalink
Shuffle test
Browse files Browse the repository at this point in the history
  • Loading branch information
mimi-imtbl committed Apr 12, 2024
1 parent 6ab5553 commit 3cf3726
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ describe('sortAndDeduplicateCurrencies', () => {
it('should remove duplicates and sort currencies by: base -> settlement -> swappable', () => {
const allCurrencies: SaleWidgetCurrency[] = [
{
base: true,
decimals: 6,
name: 'USDC',
address: '0x3b2d8a1931736fc321c24864bceee981b11c3c57',
exchangeId: 'usd-coin',
currencyType: SaleWidgetCurrencyType.SETTLEMENT,
name: 'Monopoly',
symbol: 'MPLY',
address: '0x5fc1aBC911386e2A9FEfc874ab15E20A3434D2B9',
decimals: 18,
currencyType: SaleWidgetCurrencyType.SWAPPABLE,
},
{
base: false,
Expand Down Expand Up @@ -56,6 +55,13 @@ describe('sortAndDeduplicateCurrencies', () => {
decimals: 18,
currencyType: SaleWidgetCurrencyType.SWAPPABLE,
},
{
name: 'zkSRE',
symbol: 'zkSRE',
address: '0x43566cAB87CC147C95e2895E7b972E19993520e4',
decimals: 18,
currencyType: SaleWidgetCurrencyType.SWAPPABLE,
},
{
name: 'zkPSP',
symbol: 'zkPSP',
Expand All @@ -77,6 +83,14 @@ describe('sortAndDeduplicateCurrencies', () => {
decimals: 18,
currencyType: SaleWidgetCurrencyType.SWAPPABLE,
},
{
base: true,
decimals: 6,
name: 'USDC',
address: '0x3b2d8a1931736fc321c24864bceee981b11c3c57',
exchangeId: 'usd-coin',
currencyType: SaleWidgetCurrencyType.SETTLEMENT,
},
{
name: 'zkCORE',
symbol: 'zkCORE',
Expand All @@ -91,13 +105,6 @@ describe('sortAndDeduplicateCurrencies', () => {
decimals: 6,
currencyType: SaleWidgetCurrencyType.SWAPPABLE,
},
{
name: 'Monopoly',
symbol: 'MPLY',
address: '0x5fc1aBC911386e2A9FEfc874ab15E20A3434D2B9',
decimals: 18,
currencyType: SaleWidgetCurrencyType.SWAPPABLE,
},
];

const transformedCurrencies: SaleWidgetCurrency[] = [
Expand Down Expand Up @@ -125,6 +132,13 @@ describe('sortAndDeduplicateCurrencies', () => {
exchangeId: 'ethereum',
currencyType: SaleWidgetCurrencyType.SETTLEMENT,
},
{
name: 'Monopoly',
symbol: 'MPLY',
address: '0x5fc1aBC911386e2A9FEfc874ab15E20A3434D2B9',
decimals: 18,
currencyType: SaleWidgetCurrencyType.SWAPPABLE,
},
{
name: 'Immutable Token',
symbol: 'IMX',
Expand All @@ -146,6 +160,13 @@ describe('sortAndDeduplicateCurrencies', () => {
decimals: 18,
currencyType: SaleWidgetCurrencyType.SWAPPABLE,
},
{
name: 'zkSRE',
symbol: 'zkSRE',
address: '0x43566cAB87CC147C95e2895E7b972E19993520e4',
decimals: 18,
currencyType: SaleWidgetCurrencyType.SWAPPABLE,
},
{
name: 'zkPSP',
symbol: 'zkPSP',
Expand All @@ -160,27 +181,13 @@ describe('sortAndDeduplicateCurrencies', () => {
decimals: 18,
currencyType: SaleWidgetCurrencyType.SWAPPABLE,
},
{
name: 'zkSRE',
symbol: 'zkSRE',
address: '0x43566cAB87CC147C95e2895E7b972E19993520e4',
decimals: 18,
currencyType: SaleWidgetCurrencyType.SWAPPABLE,
},
{
name: 'zkCORE',
symbol: 'zkCORE',
address: '0x4B96E7b7eA673A996F140d5De411a97b7eab934E',
decimals: 18,
currencyType: SaleWidgetCurrencyType.SWAPPABLE,
},
{
name: 'Monopoly',
symbol: 'MPLY',
address: '0x5fc1aBC911386e2A9FEfc874ab15E20A3434D2B9',
decimals: 18,
currencyType: SaleWidgetCurrencyType.SWAPPABLE,
},
];

expect(sortAndDeduplicateCurrencies(allCurrencies)).toStrictEqual(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,38 @@ import { SaleWidgetCurrency, SaleWidgetCurrencyType } from '../types';
export const sortAndDeduplicateCurrencies = (
currencies: SaleWidgetCurrency[],
): SaleWidgetCurrency[] => {
const currenciesMap = new Map<string, SaleWidgetCurrency>();
const settlementCurrencies = new Map<string, SaleWidgetCurrency>();
const swappableCurrencies = new Map<string, SaleWidgetCurrency>();

let baseCurrency: SaleWidgetCurrency | undefined;

for (const currency of currencies) {
const currencyNameKey = currency.name.toLowerCase();
if (
currency.base && currency.currencyType === SaleWidgetCurrencyType.SETTLEMENT
currency.base
&& currency.currencyType === SaleWidgetCurrencyType.SETTLEMENT
) {
if (!baseCurrency) {
baseCurrency = currency;
}
} else {
const existingCurrency = currenciesMap.get(currencyNameKey);
if (
!existingCurrency || (existingCurrency.currencyType === SaleWidgetCurrencyType.SWAPPABLE
&& currency.currencyType === SaleWidgetCurrencyType.SETTLEMENT)
) {
currenciesMap.set(currencyNameKey, currency);
}
}
}

if (baseCurrency) {
const baseCurrencyKey = baseCurrency.name.toLowerCase();
if (currenciesMap.has(baseCurrencyKey)) {
currenciesMap.delete(baseCurrencyKey);
if (baseCurrency?.name !== currency.name) {
if (currency.currencyType === SaleWidgetCurrencyType.SETTLEMENT) {
settlementCurrencies.set(currencyNameKey, currency);
} else if (currency.currencyType === SaleWidgetCurrencyType.SWAPPABLE) {
if (!settlementCurrencies.has(currencyNameKey)) {
swappableCurrencies.set(currencyNameKey, currency);
}
}
}
}

const sortedAndUniqueCurrencies = baseCurrency
? [baseCurrency, ...currenciesMap.values()]
: [...currenciesMap.values()];
const sortedAndUniqueCurrencies = [
...(baseCurrency ? [baseCurrency] : []),
...settlementCurrencies.values(),
...swappableCurrencies.values(),
];

return sortedAndUniqueCurrencies;
};

0 comments on commit 3cf3726

Please sign in to comment.