diff --git a/src/API/api.js b/src/API/api.js index 48a1817..05b1aff 100644 --- a/src/API/api.js +++ b/src/API/api.js @@ -78,6 +78,7 @@ export type World = {| +pools: string, +price: number, +delegators: string, + +saturation: number, |}; export const Sorting = Object.freeze({ @@ -170,15 +171,22 @@ const tail = (input: string): string => { return input?.slice(-10) ?? ''; }; +export type ListBiasedPoolsResponse = {| + pools: Pool[], + saturationLimit: number, +|}; + export async function listBiasedPools( externalSeed: string, searchParams: SearchParams, -): Promise { +): Promise { const unbiasedPoolsResponse = await getPools(searchParams); const unbiasedPools = toPoolArray(unbiasedPoolsResponse.pools); + const saturationLimit = unbiasedPoolsResponse.world.saturation; + if (searchParams.search || searchParams.sort === Sorting.TICKER) { - return unbiasedPools; + return { pools: unbiasedPools, saturationLimit }; } const [p1, p2, p3] = unbiasedPools; @@ -220,9 +228,9 @@ export async function listBiasedPools( allPools.splice(targetIndex, 0, biasedPool); } - return allPools; + return { pools: allPools, saturationLimit }; } catch (err) { - return unbiasedPools; + return { pools: unbiasedPools, saturationLimit }; } } diff --git a/src/containers/Home.js b/src/containers/Home.js index 5f36681..eb2bc9d 100644 --- a/src/containers/Home.js +++ b/src/containers/Home.js @@ -10,7 +10,7 @@ import { YoroiCallback } from '../API/yoroi'; import { DesktopOnly, MobileOnly } from '../components/layout/Breakpoints'; import { listBiasedPools } from '../API/api'; -import type { Pool, SearchParams } from '../API/api'; +import type { ListBiasedPoolsResponse, Pool, SearchParams } from '../API/api'; import DesktopTable from '../components/DesktopTable'; import MobileTable from '../components/MobileTable'; import SortSelect from '../components/SortSelect'; @@ -21,9 +21,6 @@ import SaturatedPoolAlert from '../components/SaturatedPoolAlert'; import cexplorerIconMini from '../assets/cexplorer-logo-mini.svg'; import cexplorerIcon from '../assets/cexplorer-logo-extend.svg'; -// k = 500 -const SATURATION_LIMIT = 63600000000000; - const Header = styled.div` display: flex; align-items: flex-end; @@ -97,6 +94,7 @@ export type DelegationProps = {| |}; function Home(props: HomeProps): Node { + const [saturationLimit, setSaturationLimit] = React.useState(null); const [rowData, setRowData] = React.useState>(null); const [status, setStatus] = React.useState('idle'); const [filterOptions, setFilterOptions] = React.useState({ @@ -113,9 +111,10 @@ function Home(props: HomeProps): Node { useEffect(() => { setStatus('pending'); listBiasedPools(seed, {}) - .then((pools: Pool[]) => { + .then((resp: ListBiasedPoolsResponse) => { setStatus('resolved'); - setRowData(pools); + setRowData(resp.pools); + setSaturationLimit(resp.saturationLimit); }) .catch((err) => { setStatus('rejected'); @@ -131,9 +130,10 @@ function Home(props: HomeProps): Node { setFilterOptions(newSearch); setStatus('pending'); listBiasedPools(seed, newSearch) - .then((pools: Pool[]) => { + .then((resp: ListBiasedPoolsResponse) => { setStatus('resolved'); - setRowData(pools); + setRowData(resp.pools); + setSaturationLimit(resp.saturationLimit); }) .catch((err) => { setStatus('rejected'); @@ -148,9 +148,10 @@ function Home(props: HomeProps): Node { setFilterOptions(newSearch); setStatus('pending'); listBiasedPools(seed, newSearch) - .then((pools: Pool[]) => { + .then((resp: ListBiasedPoolsResponse) => { setStatus('resolved'); - setRowData(pools); + setRowData(resp.pools); + setSaturationLimit(resp.saturationLimit); }) .catch((err) => { setStatus('rejected'); @@ -170,7 +171,8 @@ function Home(props: HomeProps): Node { if (delegation == null) return; const lovelaceDelegation = totalAda == null ? 0 : totalAda * 1000000; - if (Number(delegation.stakepoolTotalStake) + lovelaceDelegation >= SATURATION_LIMIT) { + const limit: ?number = saturationLimit; + if (limit != null && Number(delegation.stakepoolTotalStake) + lovelaceDelegation >= limit) { setDelegationModalData({ ...delegation, totalAda }); setConfirmDelegationModal(true); setOpenModal(true); @@ -184,17 +186,19 @@ function Home(props: HomeProps): Node { function filterPools(pools: ?Array, totalAda: ?number): ?Array { if (pools == null) return pools; + const limit: ?number = saturationLimit; + // don't filter out saturated pools if the user explicitly searches - if (filterOptions.search != null && filterOptions.search !== '') { + if (limit == null || (filterOptions.search != null && filterOptions.search !== '')) { return pools; } const lovelaceDelegation = totalAda == null ? 0 : totalAda * 1000000; - if (lovelaceDelegation > SATURATION_LIMIT) return pools; + if (lovelaceDelegation > limit) return pools; return pools.filter((item) => { - return item != null && Number(item.total_stake) + lovelaceDelegation < SATURATION_LIMIT; + return item != null && Number(item.total_stake) + lovelaceDelegation < limit; }); } diff --git a/src/containers/HomeRevamp.js b/src/containers/HomeRevamp.js index fceb841..024cec9 100644 --- a/src/containers/HomeRevamp.js +++ b/src/containers/HomeRevamp.js @@ -9,7 +9,7 @@ import { SendFirstAdapool, YoroiCallback } from '../API/yoroi'; import { DesktopOnly, MobileOnly } from '../components/layout/Breakpoints'; import { listBiasedPools, Sorting, SortingDirections } from '../API/api'; -import type { Pool, SearchParams } from '../API/api'; +import type { ListBiasedPoolsResponse, Pool, SearchParams } from '../API/api'; import SortSelect from '../components/SortSelect'; import type { QueryState } from '../utils/types'; @@ -22,9 +22,6 @@ import SearchRevamp from '../components/SearchRevamp'; import MobileTableRevamp from '../components/MobileTableRevamp'; import { formatCostLabel } from '../utils/utils'; -// k = 500 -const SATURATION_LIMIT = 63600000000000; - const Header = styled.div` display: flex; align-items: flex-end; @@ -148,6 +145,7 @@ const SORTING_FUNCTIONS = { const defaultActiveSort = { sort: Sorting.TICKER, sortDirection: '' }; function Home(props: HomeProps): Node { + const [saturationLimit, setSaturationLimit] = React.useState(null); const [rowData, setRowData] = React.useState>(null); const [rowDataSorted, setRowDataSorted] = React.useState>(null); const [status, setStatus] = React.useState('idle'); @@ -167,11 +165,12 @@ function Home(props: HomeProps): Node { useEffect(() => { setStatus('pending'); listBiasedPools(seed, {}) - .then((pools: Pool[]) => { + .then((resp: ListBiasedPoolsResponse) => { setStatus('resolved'); - setRowData(pools); + setRowData(resp.pools); + setSaturationLimit(resp.saturationLimit); // used to show the first pool in revamp banner - SendFirstAdapool(pools[0]); + SendFirstAdapool(resp.pools[0]); }) .catch((err) => { setStatus('rejected'); @@ -187,11 +186,12 @@ function Home(props: HomeProps): Node { setFilterOptions(newSearch); setStatus('pending'); listBiasedPools(seed, newSearch) - .then((pools: Pool[]) => { + .then((resp: ListBiasedPoolsResponse) => { setStatus('resolved'); - setRowData(pools); - setRowDataSorted(pools); + setRowData(resp.pools); + setRowDataSorted(resp.pools); setActiveSort(defaultActiveSort); + setSaturationLimit(resp.saturationLimit); }) .catch((err) => { setStatus('rejected'); @@ -211,7 +211,8 @@ function Home(props: HomeProps): Node { if (delegation == null) return; const lovelaceDelegation = totalAda == null ? 0 : totalAda * 1000000; - if (Number(delegation.stakepoolTotalStake) + lovelaceDelegation >= SATURATION_LIMIT) { + const limit: ?number = saturationLimit; + if (limit != null && Number(delegation.stakepoolTotalStake) + lovelaceDelegation >= limit) { setDelegationModalData({ ...delegation, totalAda }); setConfirmDelegationModal(true); setOpenModal(true); @@ -225,17 +226,19 @@ function Home(props: HomeProps): Node { function filterPools(pools: ?Array, totalAda: ?number): ?Array { if (pools == null) return pools; + const limit: ?number = saturationLimit; + // don't filter out saturated pools if the user explicitly searches - if (filterOptions.search != null && filterOptions.search !== '') { + if (limit == null || (filterOptions.search != null && filterOptions.search !== '')) { return pools; } const lovelaceDelegation = totalAda == null ? 0 : totalAda * 1000000; - if (lovelaceDelegation > SATURATION_LIMIT) return pools; + if (lovelaceDelegation > limit) return pools; return pools.filter((item) => { - return item != null && Number(item.total_stake) + lovelaceDelegation < SATURATION_LIMIT; + return item != null && Number(item.total_stake) + lovelaceDelegation < limit; }); }