Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort token list update #574

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 47 additions & 5 deletions src/components/SearchModal/CurrencyList/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { SorobanContextType, useSorobanReact } from '@soroban-react/core';
import BigNumber from 'bignumber.js';
import { CSSProperties, MutableRefObject, useCallback, useMemo } from 'react';
import { CSSProperties, MutableRefObject, useCallback, useEffect, useMemo, useState } from 'react';
import { Check } from 'react-feather';
import { FixedSizeList } from 'react-window';
import { AccountResponse } from '@stellar/stellar-sdk/lib/horizon';
Expand All @@ -11,7 +11,7 @@ import Column, { AutoColumn } from 'components/Column';
import Loader from 'components/Icons/LoadingSpinner';
import CurrencyLogo from 'components/Logo/CurrencyLogo';
import Row, { RowFixed } from 'components/Row';
import { TokenType } from '../../../interfaces';
import { TokenType, TokenVolumeData } from '../../../interfaces';
import StyledRow from '../../Row';
import { LoadingRows, MenuItem } from '../styleds';

Expand Down Expand Up @@ -258,13 +258,55 @@ export default function CurrencyList({
isAddressSearch: string | false;
isLoading?: boolean;
}) {
const [tokenVolumes, setTokenVolumes] = useState<TokenVolumeData[] | null>(null);

const [sortedItemData, setSortedItemData] = useState<TokenType[]>([]);

const fetchTokenVolumes = async (network: string): Promise<TokenVolumeData[]> => {
const response = await fetch(`https://info.soroswap.finance/api/tokens?network=${network}`);
if (!response.ok) {
throw new Error('Failed to fetch token volumes');
}
const data = await response.json();
return data.map((token: any) => ({
asset: token.asset,
volume24h: token.volume24h,
}));
};

useEffect(() => {
const getVolumes = async () => {
try {
const volumes = await fetchTokenVolumes('MAINNET');
setTokenVolumes(volumes);
} catch (error) {
console.error(error);
}
};
getVolumes();
}, []);

const itemData: TokenType[] = useMemo(() => {
if (otherListTokens && otherListTokens?.length > 0) {
return [...currencies, ...otherListTokens];
}
return currencies;
}, [currencies, otherListTokens]);

useEffect(() => {
if (tokenVolumes) {
const sorted = [...itemData];
sorted.sort((a, b) => {
const volumeA =
tokenVolumes.find((item) => item.asset.contract === a.contract)?.volume24h || 0;
const volumeB =
tokenVolumes.find((item) => item.asset.contract === b.contract)?.volume24h || 0;
return volumeB - volumeA;
});
setSortedItemData(sorted);
}
}, [itemData, tokenVolumes]);

const Row = useCallback(
function TokenRow({ data, index, style }: TokenRowProps) {
const row: TokenType = data[index];
Expand Down Expand Up @@ -314,7 +356,7 @@ export default function CurrencyList({
],
);

const itemKey = useCallback((index: number, data: typeof itemData) => {
const itemKey = useCallback((index: number, data: typeof sortedItemData) => {
const currency = data[index];
return currencyKey(currency);
}, []);
Expand All @@ -330,8 +372,8 @@ export default function CurrencyList({
height={height}
ref={fixedListRef as any}
width="100%"
itemData={itemData}
itemCount={itemData.length}
itemData={sortedItemData}
itemCount={sortedItemData.length}
itemSize={56}
itemKey={itemKey}
>
Expand Down
25 changes: 14 additions & 11 deletions src/hooks/tokens/useToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ export const findToken = async (
if (!tokenAddress || tokenAddress === '') return undefined;

const classicAssetSearch = getClassicAssetSorobanAddress(tokenAddress!, sorobanContext);

const formattedAddress = isAddress(classicAssetSearch ? classicAssetSearch : tokenAddress);
if (!formattedAddress) return undefined;

const fromMap = tokensAsMap[formattedAddress];

if (fromMap) return fromMap;

const token = await getToken(sorobanContext, formattedAddress);
// const token: TokenType = {
// contract: formattedAddress,
Expand All @@ -33,7 +33,7 @@ export const findToken = async (
// decimals,
// icon: logo,
// };

if (!token?.name || !token?.code) return undefined;
// Here from token.name we will try to understand if this is a classic asset (even if we got a soroban contracta as address).
const stellarAsset = getClassicStellarAsset(token.name);
Expand All @@ -51,10 +51,9 @@ export const findToken = async (
decimals: 7,
icon: '',
};
} else {
return token;
}
else {
return token
};
};

const revalidateKeysCondition = (key: any) => {
Expand Down Expand Up @@ -96,8 +95,12 @@ export function useToken(tokenAddress: string | undefined) {
const bothLoading = isLoading || isStellarClassicAssetLoading;

const needsWrapping = !data && isStellarClassicAsset;

const checkContractId = (contractId: string, code: string, issuer: string): boolean | undefined => {

const checkContractId = (
contractId: string,
code: string,
issuer: string,
): boolean | undefined => {
if (!issuer) {
return undefined;
}
Expand All @@ -107,7 +110,7 @@ export function useToken(tokenAddress: string | undefined) {
} else {
return false;
}
}
};
const isSafe = data ? checkContractId(data.contract, data.code, data.issuer!) : false;

const needsWrappingOnAddLiquidity = (!data && isStellarClassicAsset) || !name;
Expand Down
11 changes: 11 additions & 0 deletions src/interfaces/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,14 @@ export type TokenMapType = {
export type TokenBalancesMap = {
[tokenAddress: string]: { usdValue: number; balance: string };
};

export interface TokenVolumeData {
asset: {
name: string;
contract: string;
code: string;
icon: string;
decimals: number;
};
volume24h: number;
}
Loading